github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime.go (about) 1 package runtime 2 3 import ( 4 "unsafe" 5 ) 6 7 //go:generate go run ../../tools/gen-critical-atomics -out ./atomics_critical.go 8 9 const Compiler = "tinygo" 10 11 // The compiler will fill this with calls to the initialization function of each 12 // package. 13 func initAll() 14 15 //go:linkname callMain main.main 16 func callMain() 17 18 func GOMAXPROCS(n int) int { 19 // Note: setting GOMAXPROCS is ignored. 20 return 1 21 } 22 23 func GOROOT() string { 24 // TODO: don't hardcode but take the one at compile time. 25 return "/usr/local/go" 26 } 27 28 // Copy size bytes from src to dst. The memory areas must not overlap. 29 // This function is implemented by the compiler as a call to a LLVM intrinsic 30 // like llvm.memcpy.p0.p0.i32(dst, src, size, false). 31 func memcpy(dst, src unsafe.Pointer, size uintptr) 32 33 // Copy size bytes from src to dst. The memory areas may overlap and will do the 34 // correct thing. 35 // This function is implemented by the compiler as a call to a LLVM intrinsic 36 // like llvm.memmove.p0.p0.i32(dst, src, size, false). 37 func memmove(dst, src unsafe.Pointer, size uintptr) 38 39 // Set the given number of bytes to zero. 40 // This function is implemented by the compiler as a call to a LLVM intrinsic 41 // like llvm.memset.p0.i32(ptr, 0, size, false). 42 func memzero(ptr unsafe.Pointer, size uintptr) 43 44 // This intrinsic returns the current stack pointer. 45 // It is normally used together with llvm.stackrestore but also works to get the 46 // current stack pointer in a platform-independent way. 47 // 48 //export llvm.stacksave 49 func stacksave() unsafe.Pointer 50 51 //export strlen 52 func strlen(ptr unsafe.Pointer) uintptr 53 54 //export malloc 55 func malloc(size uintptr) unsafe.Pointer 56 57 // Compare two same-size buffers for equality. 58 func memequal(x, y unsafe.Pointer, n uintptr) bool { 59 for i := uintptr(0); i < n; i++ { 60 cx := *(*uint8)(unsafe.Add(x, i)) 61 cy := *(*uint8)(unsafe.Add(y, i)) 62 if cx != cy { 63 return false 64 } 65 } 66 return true 67 } 68 69 func nanotime() int64 { 70 return ticksToNanoseconds(ticks()) 71 } 72 73 // Copied from the Go runtime source code. 74 // 75 //go:linkname os_sigpipe os.sigpipe 76 func os_sigpipe() { 77 runtimePanic("too many writes on closed pipe") 78 } 79 80 // LockOSThread wires the calling goroutine to its current operating system thread. 81 // Stub for now 82 // Called by go1.18 standard library on windows, see https://github.com/golang/go/issues/49320 83 func LockOSThread() { 84 } 85 86 // UnlockOSThread undoes an earlier call to LockOSThread. 87 // Stub for now 88 func UnlockOSThread() { 89 } 90 91 // KeepAlive makes sure the value in the interface is alive until at least the 92 // point of the call. 93 func KeepAlive(x interface{}) 94 95 var godebugUpdate func(string, string) 96 97 //go:linkname godebug_setUpdate internal/godebug.setUpdate 98 func godebug_setUpdate(update func(string, string)) { 99 // The 'update' function needs to be called whenever the GODEBUG environment 100 // variable changes (for example, via os.Setenv). 101 godebugUpdate = update 102 } 103 104 //go:linkname godebug_setNewIncNonDefault internal/godebug.setNewIncNonDefault 105 func godebug_setNewIncNonDefault(newIncNonDefault func(string) func()) { 106 // Dummy function necessary in Go 1.21. 107 } 108 109 // Write to the given file descriptor. 110 // This is called from internal/godebug starting with Go 1.21, and only seems to 111 // be called with the stderr file descriptor. 112 func write(fd uintptr, p unsafe.Pointer, n int32) int32 { 113 if fd == 2 { // stderr 114 // Convert to a string, because we know that p won't change during the 115 // call to printstring. 116 // TODO: use unsafe.String instead once we require Go 1.20. 117 s := _string{ 118 ptr: (*byte)(p), 119 length: uintptr(n), 120 } 121 str := *(*string)(unsafe.Pointer(&s)) 122 printstring(str) 123 return n 124 } 125 return 0 126 }