1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
// +build darwin linux,!baremetal,!wasi freebsd,!baremetal
// +build !nintendoswitch
package runtime
import (
"unsafe"
)
//export putchar
func _putchar(c int) int
//export usleep
func usleep(usec uint) int
//export malloc
func malloc(size uintptr) unsafe.Pointer
// void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
//export mmap
func mmap(addr unsafe.Pointer, length uintptr, prot, flags, fd int, offset int) unsafe.Pointer
//export abort
func abort()
//export exit
func exit(code int)
//export clock_gettime
func clock_gettime(clk_id int32, ts *timespec)
type timeUnit int64
// Note: tv_sec and tv_nsec vary in size by platform. They are 32-bit on 32-bit
// systems and 64-bit on 64-bit systems (at least on macOS/Linux), so we can
// simply use the 'int' type which does the same.
type timespec struct {
tv_sec int // time_t: follows the platform bitness
tv_nsec int // long: on Linux and macOS, follows the platform bitness
}
var stackTop uintptr
func postinit() {}
// Entry point for Go. Initialize all packages and call main.main().
//export main
func main(argc int32, argv *unsafe.Pointer) int {
preinit()
// Make args global big enough so that it can store all command line
// arguments. Unfortunately this has to be done with some magic as the heap
// is not yet initialized.
argsSlice := (*struct {
ptr unsafe.Pointer
len uintptr
cap uintptr
})(unsafe.Pointer(&args))
argsSlice.ptr = malloc(uintptr(argc) * (unsafe.Sizeof(uintptr(0))) * 3)
argsSlice.len = uintptr(argc)
argsSlice.cap = uintptr(argc)
// Initialize command line parameters.
for i := 0; i < int(argc); i++ {
// Convert the C string to a Go string.
length := strlen(*argv)
arg := (*_string)(unsafe.Pointer(&args[i]))
arg.length = length
arg.ptr = (*byte)(*argv)
// This is the Go equivalent of "argc++" in C.
argv = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(argv)) + unsafe.Sizeof(argv)))
}
// Obtain the initial stack pointer right before calling the run() function.
// The run function has been moved to a separate (non-inlined) function so
// that the correct stack pointer is read.
stackTop = getCurrentStackPointer()
runMain()
// For libc compatibility.
return 0
}
// Must be a separate function to get the correct stack pointer.
//go:noinline
func runMain() {
run()
}
//go:extern environ
var environ *unsafe.Pointer
//go:linkname syscall_runtime_envs syscall.runtime_envs
func syscall_runtime_envs() []string {
// Count how many environment variables there are.
env := environ
numEnvs := 0
for *env != nil {
numEnvs++
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
}
// Create a string slice of all environment variables.
// This requires just a single heap allocation.
env = environ
envs := make([]string, 0, numEnvs)
for *env != nil {
ptr := *env
length := strlen(ptr)
s := _string{
ptr: (*byte)(ptr),
length: length,
}
envs = append(envs, *(*string)(unsafe.Pointer(&s)))
env = (*unsafe.Pointer)(unsafe.Pointer(uintptr(unsafe.Pointer(env)) + unsafe.Sizeof(environ)))
}
return envs
}
func putchar(c byte) {
_putchar(int(c))
}
func ticksToNanoseconds(ticks timeUnit) int64 {
// The OS API works in nanoseconds so no conversion necessary.
return int64(ticks)
}
func nanosecondsToTicks(ns int64) timeUnit {
// The OS API works in nanoseconds so no conversion necessary.
return timeUnit(ns)
}
func sleepTicks(d timeUnit) {
// timeUnit is in nanoseconds, so need to convert to microseconds here.
usleep(uint(d) / 1000)
}
func getTime(clock int32) uint64 {
ts := timespec{}
clock_gettime(clock, &ts)
return uint64(ts.tv_sec)*1000*1000*1000 + uint64(ts.tv_nsec)
}
// Return monotonic time in nanoseconds.
func monotime() uint64 {
return getTime(clock_MONOTONIC_RAW)
}
func ticks() timeUnit {
return timeUnit(monotime())
}
//go:linkname now time.now
func now() (sec int64, nsec int32, mono int64) {
ts := timespec{}
clock_gettime(clock_REALTIME, &ts)
sec = int64(ts.tv_sec)
nsec = int32(ts.tv_nsec)
mono = nanotime()
return
}
//go:linkname syscall_Exit syscall.Exit
func syscall_Exit(code int) {
exit(code)
}
func extalloc(size uintptr) unsafe.Pointer {
return malloc(size)
}
//export free
func extfree(ptr unsafe.Pointer)
// TinyGo does not yet support any form of parallelism on an OS, so these can be
// left empty.
//go:linkname procPin sync/atomic.runtime_procPin
func procPin() {
}
//go:linkname procUnpin sync/atomic.runtime_procUnpin
func procUnpin() {
}
|