github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/baremetal.go (about) 1 //go:build baremetal 2 3 package runtime 4 5 import ( 6 "unsafe" 7 ) 8 9 //go:extern _heap_start 10 var heapStartSymbol [0]byte 11 12 //go:extern _heap_end 13 var heapEndSymbol [0]byte 14 15 //go:extern _globals_start 16 var globalsStartSymbol [0]byte 17 18 //go:extern _globals_end 19 var globalsEndSymbol [0]byte 20 21 //go:extern _stack_top 22 var stackTopSymbol [0]byte 23 24 var ( 25 heapStart = uintptr(unsafe.Pointer(&heapStartSymbol)) 26 heapEnd = uintptr(unsafe.Pointer(&heapEndSymbol)) 27 globalsStart = uintptr(unsafe.Pointer(&globalsStartSymbol)) 28 globalsEnd = uintptr(unsafe.Pointer(&globalsEndSymbol)) 29 stackTop = uintptr(unsafe.Pointer(&stackTopSymbol)) 30 ) 31 32 // growHeap tries to grow the heap size. It returns true if it succeeds, false 33 // otherwise. 34 func growHeap() bool { 35 // On baremetal, there is no way the heap can be grown. 36 return false 37 } 38 39 //export malloc 40 func libc_malloc(size uintptr) unsafe.Pointer { 41 // Note: this zeroes the returned buffer which is not necessary. 42 // The same goes for bytealg.MakeNoZero. 43 return alloc(size, nil) 44 } 45 46 //export calloc 47 func libc_calloc(nmemb, size uintptr) unsafe.Pointer { 48 // No difference between calloc and malloc. 49 return libc_malloc(nmemb * size) 50 } 51 52 //export free 53 func libc_free(ptr unsafe.Pointer) { 54 free(ptr) 55 } 56 57 //export runtime_putchar 58 func runtime_putchar(c byte) { 59 putchar(c) 60 } 61 62 //go:linkname syscall_Exit syscall.Exit 63 func syscall_Exit(code int) { 64 exit(code) 65 } 66 67 const baremetal = true 68 69 // timeOffset is how long the monotonic clock started after the Unix epoch. It 70 // should be a positive integer under normal operation or zero when it has not 71 // been set. 72 var timeOffset int64 73 74 //go:linkname now time.now 75 func now() (sec int64, nsec int32, mono int64) { 76 mono = nanotime() 77 sec = (mono + timeOffset) / (1000 * 1000 * 1000) 78 nsec = int32((mono + timeOffset) - sec*(1000*1000*1000)) 79 return 80 } 81 82 // AdjustTimeOffset adds the given offset to the built-in time offset. A 83 // positive value adds to the time (skipping some time), a negative value moves 84 // the clock into the past. 85 func AdjustTimeOffset(offset int64) { 86 // TODO: do this atomically? 87 timeOffset += offset 88 }