Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 5 | /* |
| 6 | * basic types |
| 7 | */ |
| 8 | typedef signed char int8; |
| 9 | typedef unsigned char uint8; |
| 10 | typedef signed short int16; |
| 11 | typedef unsigned short uint16; |
| 12 | typedef signed int int32; |
| 13 | typedef unsigned int uint32; |
| 14 | typedef signed long long int int64; |
| 15 | typedef unsigned long long int uint64; |
| 16 | typedef float float32; |
| 17 | typedef double float64; |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 18 | |
| 19 | #ifdef _64BIT |
Russ Cox | 75647d2 | 2008-11-17 12:32:35 -0800 | [diff] [blame] | 20 | typedef uint64 uintptr; |
Devon H. O'Dell | 659780b | 2009-11-18 09:11:39 -0800 | [diff] [blame] | 21 | typedef int64 intptr; |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 22 | #else |
| 23 | typedef uint32 uintptr; |
Devon H. O'Dell | 659780b | 2009-11-18 09:11:39 -0800 | [diff] [blame] | 24 | typedef int32 intptr; |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 25 | #endif |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
| 28 | * get rid of C types |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 29 | * the / / / forces a syntax error immediately, |
| 30 | * which will show "last name: XXunsigned". |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 31 | */ |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 32 | #define unsigned XXunsigned / / / |
| 33 | #define signed XXsigned / / / |
| 34 | #define char XXchar / / / |
| 35 | #define short XXshort / / / |
| 36 | #define int XXint / / / |
| 37 | #define long XXlong / / / |
| 38 | #define float XXfloat / / / |
| 39 | #define double XXdouble / / / |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 40 | |
| 41 | /* |
| 42 | * defined types |
| 43 | */ |
| 44 | typedef uint8 bool; |
| 45 | typedef uint8 byte; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 46 | typedef struct Alg Alg; |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 47 | typedef struct Func Func; |
| 48 | typedef struct G G; |
| 49 | typedef struct Gobuf Gobuf; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 50 | typedef struct Lock Lock; |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 51 | typedef struct M M; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 52 | typedef struct Mem Mem; |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 53 | typedef union Note Note; |
Rob Pike | 87f2208 | 2009-08-25 15:54:25 -0700 | [diff] [blame] | 54 | typedef struct Slice Slice; |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 55 | typedef struct Stktop Stktop; |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 56 | typedef struct String String; |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 57 | typedef struct Usema Usema; |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 58 | typedef struct SigTab SigTab; |
| 59 | typedef struct MCache MCache; |
| 60 | typedef struct Iface Iface; |
Russ Cox | c7513ea | 2009-07-07 11:02:54 -0700 | [diff] [blame] | 61 | typedef struct Itab Itab; |
Ken Thompson | f59cb49 | 2010-02-19 20:42:50 -0800 | [diff] [blame] | 62 | typedef struct Eface Eface; |
Russ Cox | c7513ea | 2009-07-07 11:02:54 -0700 | [diff] [blame] | 63 | typedef struct Type Type; |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 64 | typedef struct Defer Defer; |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 65 | typedef struct hash Hmap; |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 66 | typedef struct Hchan Hchan; |
Ken Thompson | f59cb49 | 2010-02-19 20:42:50 -0800 | [diff] [blame] | 67 | typedef struct Complex64 Complex64; |
| 68 | typedef struct Complex128 Complex128; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 69 | |
| 70 | /* |
Rob Pike | d08f006 | 2009-08-11 13:30:35 -0700 | [diff] [blame] | 71 | * per-cpu declaration. |
| 72 | * "extern register" is a special storage class implemented by 6c, 8c, etc. |
| 73 | * on machines with lots of registers, it allocates a register that will not be |
| 74 | * used in generated code. on the x86, it allocates a slot indexed by a |
| 75 | * segment register. |
| 76 | * |
| 77 | * amd64: allocated downwards from R15 |
| 78 | * x86: allocated upwards from 0(FS) |
Kai Backman | ebf32c5 | 2010-02-06 21:59:46 -0800 | [diff] [blame] | 79 | * arm: allocated downwards from R10 |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame] | 80 | * |
Rob Pike | d08f006 | 2009-08-11 13:30:35 -0700 | [diff] [blame] | 81 | * every C file linked into a Go program must include runtime.h |
| 82 | * so that the C compiler knows to avoid other uses of these registers. |
| 83 | * the Go compilers know to avoid them. |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 84 | */ |
Rob Pike | d08f006 | 2009-08-11 13:30:35 -0700 | [diff] [blame] | 85 | extern register G* g; |
| 86 | extern register M* m; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * defined constants |
| 90 | */ |
| 91 | enum |
| 92 | { |
| 93 | // G status |
| 94 | Gidle, |
| 95 | Grunnable, |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 96 | Grunning, |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 97 | Gsyscall, |
Ken Thompson | 5262003 | 2008-07-14 14:33:39 -0700 | [diff] [blame] | 98 | Gwaiting, |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 99 | Gmoribund, |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 100 | Gdead, |
| 101 | }; |
| 102 | enum |
| 103 | { |
| 104 | true = 1, |
| 105 | false = 0, |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * structures |
| 110 | */ |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 111 | struct Lock |
| 112 | { |
| 113 | uint32 key; |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 114 | #ifdef __MINGW__ |
| 115 | void* event; |
| 116 | #else |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 117 | uint32 sema; // for OS X |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 118 | #endif |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 119 | }; |
Russ Cox | 5ff12f8 | 2008-09-24 10:25:28 -0700 | [diff] [blame] | 120 | struct Usema |
| 121 | { |
| 122 | uint32 u; |
| 123 | uint32 k; |
| 124 | }; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 125 | union Note |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 126 | { |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 127 | struct { // Linux |
| 128 | Lock lock; |
| 129 | }; |
| 130 | struct { // OS X |
| 131 | int32 wakeup; |
Russ Cox | 5ff12f8 | 2008-09-24 10:25:28 -0700 | [diff] [blame] | 132 | Usema sema; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 133 | }; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 134 | }; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 135 | struct String |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 136 | { |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 137 | byte* str; |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 138 | int32 len; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 139 | }; |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 140 | struct Iface |
| 141 | { |
Russ Cox | c7513ea | 2009-07-07 11:02:54 -0700 | [diff] [blame] | 142 | Itab* tab; |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 143 | void* data; |
Russ Cox | c3077f7 | 2008-12-19 17:11:54 -0800 | [diff] [blame] | 144 | }; |
Russ Cox | 2da5022 | 2009-05-20 14:57:55 -0700 | [diff] [blame] | 145 | struct Eface |
| 146 | { |
Russ Cox | c7513ea | 2009-07-07 11:02:54 -0700 | [diff] [blame] | 147 | Type* type; |
Russ Cox | 2da5022 | 2009-05-20 14:57:55 -0700 | [diff] [blame] | 148 | void* data; |
| 149 | }; |
Ken Thompson | f59cb49 | 2010-02-19 20:42:50 -0800 | [diff] [blame] | 150 | struct Complex64 |
| 151 | { |
| 152 | float32 real; |
| 153 | float32 imag; |
| 154 | }; |
| 155 | struct Complex128 |
| 156 | { |
| 157 | float64 real; |
| 158 | float64 imag; |
| 159 | }; |
Ken Thompson | 66a603c | 2008-08-27 17:28:30 -0700 | [diff] [blame] | 160 | |
Rob Pike | 87f2208 | 2009-08-25 15:54:25 -0700 | [diff] [blame] | 161 | struct Slice |
Ken Thompson | 66a603c | 2008-08-27 17:28:30 -0700 | [diff] [blame] | 162 | { // must not move anything |
| 163 | byte* array; // actual data |
Rob Pike | 87f2208 | 2009-08-25 15:54:25 -0700 | [diff] [blame] | 164 | uint32 len; // number of elements |
Russ Cox | 75647d2 | 2008-11-17 12:32:35 -0800 | [diff] [blame] | 165 | uint32 cap; // allocated number of elements |
Ken Thompson | 66a603c | 2008-08-27 17:28:30 -0700 | [diff] [blame] | 166 | }; |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 167 | struct Gobuf |
| 168 | { |
Russ Cox | 3802009 | 2009-06-17 16:31:02 -0700 | [diff] [blame] | 169 | // The offsets of these fields are known to (hard-coded in) libmach. |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 170 | byte* sp; |
| 171 | byte* pc; |
| 172 | G* g; |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 173 | }; |
Ken Thompson | 7b454bb | 2008-07-09 11:35:26 -0700 | [diff] [blame] | 174 | struct G |
Ken Thompson | 4528854 | 2008-07-08 17:19:17 -0700 | [diff] [blame] | 175 | { |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 176 | byte* stackguard; // cannot move - also known to linker, libmach, libcgo |
| 177 | byte* stackbase; // cannot move - also known to libmach, libcgo |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 178 | Defer* defer; |
Russ Cox | 3802009 | 2009-06-17 16:31:02 -0700 | [diff] [blame] | 179 | Gobuf sched; // cannot move - also known to libmach |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 180 | byte* stack0; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 181 | byte* entry; // initial function |
Rob Pike | 3835e01 | 2008-07-28 11:29:41 -0700 | [diff] [blame] | 182 | G* alllink; // on allg |
Ken Thompson | e963cba | 2008-07-25 15:55:12 -0700 | [diff] [blame] | 183 | void* param; // passed parameter on wakeup |
| 184 | int16 status; |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 185 | int32 goid; |
Adam Langley | 50d6c81 | 2009-12-18 12:25:53 -0800 | [diff] [blame] | 186 | uint32 selgen; // valid sudog pointer |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 187 | G* schedlink; |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 188 | bool readyonstop; |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 189 | M* m; // for debuggers, but offset not hard-coded |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 190 | M* lockedm; |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 191 | void (*cgofn)(void*); // for cgo/ffi |
| 192 | void *cgoarg; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 193 | }; |
Ken Thompson | 4528854 | 2008-07-08 17:19:17 -0700 | [diff] [blame] | 194 | struct M |
| 195 | { |
Russ Cox | 3802009 | 2009-06-17 16:31:02 -0700 | [diff] [blame] | 196 | // The offsets of these fields are known to (hard-coded in) libmach. |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 197 | G* g0; // goroutine with scheduling stack |
| 198 | void (*morepc)(void); |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 199 | void* morefp; // frame pointer for more stack |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 200 | Gobuf morebuf; // gobuf arg to morestack |
| 201 | |
Russ Cox | 3802009 | 2009-06-17 16:31:02 -0700 | [diff] [blame] | 202 | // Fields not known to debuggers. |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 203 | uint32 moreframe; // size arguments to morestack |
| 204 | uint32 moreargs; |
| 205 | uintptr cret; // return value from C |
| 206 | uint64 procid; // for debuggers, but offset not hard-coded |
| 207 | G* gsignal; // signal-handling G |
| 208 | uint32 tls[8]; // thread-local storage (for 386 extern register) |
| 209 | Gobuf sched; // scheduling stack |
| 210 | G* curg; // current running goroutine |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 211 | int32 id; |
Russ Cox | da0a7d7 | 2008-12-19 03:13:39 -0800 | [diff] [blame] | 212 | int32 mallocing; |
Russ Cox | 8c357ce | 2009-06-15 21:31:56 -0700 | [diff] [blame] | 213 | int32 gcing; |
Russ Cox | 1ce1791 | 2009-01-26 17:37:05 -0800 | [diff] [blame] | 214 | int32 locks; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 215 | int32 waitnextg; |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 216 | Note havenextg; |
| 217 | G* nextg; |
Russ Cox | 93689d8 | 2009-10-09 15:35:33 -0700 | [diff] [blame] | 218 | M* alllink; // on allm |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 219 | M* schedlink; |
Russ Cox | 376898c | 2008-09-09 11:50:14 -0700 | [diff] [blame] | 220 | uint32 machport; // Return address for Mach IPC (OS X) |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 221 | MCache *mcache; |
Russ Cox | 218c393 | 2009-07-13 17:28:39 -0700 | [diff] [blame] | 222 | G* lockedg; |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 223 | #ifdef __MINGW__ |
| 224 | void* return_address; // saved return address and stack |
| 225 | void* stack_pointer; // pointer for Windows stdcall |
| 226 | void* os_stack_pointer; |
| 227 | #endif |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 228 | }; |
Ken Thompson | 5262003 | 2008-07-14 14:33:39 -0700 | [diff] [blame] | 229 | struct Stktop |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 230 | { |
Russ Cox | 3802009 | 2009-06-17 16:31:02 -0700 | [diff] [blame] | 231 | // The offsets of these fields are known to (hard-coded in) libmach. |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 232 | uint8* stackguard; |
| 233 | uint8* stackbase; |
| 234 | Gobuf gobuf; |
| 235 | uint32 args; |
Russ Cox | bba278a | 2009-07-08 18:16:09 -0700 | [diff] [blame] | 236 | |
| 237 | // Frame pointer: where args start in old frame. |
| 238 | // fp == gobuf.sp except in the case of a reflected |
| 239 | // function call, which uses an off-stack argument frame. |
| 240 | uint8* fp; |
Ken Thompson | 4528854 | 2008-07-08 17:19:17 -0700 | [diff] [blame] | 241 | }; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 242 | struct Alg |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 243 | { |
Russ Cox | a52fb81 | 2009-06-04 21:09:06 -0700 | [diff] [blame] | 244 | uintptr (*hash)(uint32, void*); |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 245 | uint32 (*equal)(uint32, void*, void*); |
| 246 | void (*print)(uint32, void*); |
| 247 | void (*copy)(uint32, void*, void*); |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 248 | }; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 249 | struct SigTab |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 250 | { |
Russ Cox | dfa5893 | 2008-12-03 14:21:28 -0800 | [diff] [blame] | 251 | int32 flags; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 252 | int8 *name; |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 253 | }; |
Russ Cox | dfa5893 | 2008-12-03 14:21:28 -0800 | [diff] [blame] | 254 | enum |
| 255 | { |
| 256 | SigCatch = 1<<0, |
| 257 | SigIgnore = 1<<1, |
| 258 | SigRestart = 1<<2, |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 259 | SigQueue = 1<<3, |
Russ Cox | dfa5893 | 2008-12-03 14:21:28 -0800 | [diff] [blame] | 260 | }; |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 261 | |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 262 | // (will be) shared with go; edit ../cmd/6g/sys.go too. |
| 263 | // should move out of sys.go eventually. |
| 264 | // also eventually, the loaded symbol table should |
| 265 | // be closer to this form. |
| 266 | struct Func |
| 267 | { |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 268 | String name; |
| 269 | String type; // go type string |
| 270 | String src; // src file name |
Russ Cox | a543336 | 2008-11-25 09:23:36 -0800 | [diff] [blame] | 271 | uint64 entry; // entry pc |
| 272 | int64 frame; // stack frame size |
Rob Pike | 87f2208 | 2009-08-25 15:54:25 -0700 | [diff] [blame] | 273 | Slice pcln; // pc/ln tab for this func |
Russ Cox | a543336 | 2008-11-25 09:23:36 -0800 | [diff] [blame] | 274 | int64 pc0; // starting pc, ln for table |
| 275 | int32 ln0; |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 276 | int32 args; // number of 32-bit in/out args |
| 277 | int32 locals; // number of 32-bit locals |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 278 | }; |
| 279 | |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 280 | /* |
| 281 | * defined macros |
| 282 | * you need super-goru privilege |
| 283 | * to add this list. |
| 284 | */ |
| 285 | #define nelem(x) (sizeof(x)/sizeof((x)[0])) |
| 286 | #define nil ((void*)0) |
| 287 | |
| 288 | /* |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 289 | * known to compiler |
| 290 | */ |
| 291 | enum |
| 292 | { |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 293 | AMEM, |
| 294 | ANOEQ, |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 295 | ASTRING, |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 296 | AINTER, |
Russ Cox | 2da5022 | 2009-05-20 14:57:55 -0700 | [diff] [blame] | 297 | ANILINTER, |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 298 | AFAKE, |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 299 | Amax |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 300 | }; |
| 301 | |
Russ Cox | 29aa3ff | 2009-07-02 21:25:46 -0700 | [diff] [blame] | 302 | |
| 303 | enum { |
| 304 | Structrnd = sizeof(uintptr) |
| 305 | }; |
| 306 | |
Russ Cox | eee50ae | 2008-12-19 12:05:22 -0800 | [diff] [blame] | 307 | /* |
Ken Thompson | 47ab1c1 | 2009-01-27 13:23:28 -0800 | [diff] [blame] | 308 | * deferred subroutine calls |
Ken Thompson | 1e1cc4e | 2009-01-27 12:03:53 -0800 | [diff] [blame] | 309 | */ |
| 310 | struct Defer |
| 311 | { |
| 312 | int32 siz; |
| 313 | byte* sp; |
| 314 | byte* fn; |
| 315 | Defer* link; |
| 316 | byte args[8]; // padded to actual size |
| 317 | }; |
| 318 | |
| 319 | /* |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 320 | * external data |
| 321 | */ |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 322 | extern Alg algarray[Amax]; |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 323 | extern String emptystring; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 324 | G* allg; |
Russ Cox | 93689d8 | 2009-10-09 15:35:33 -0700 | [diff] [blame] | 325 | M* allm; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 326 | int32 goidgen; |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 327 | extern int32 gomaxprocs; |
Rob Pike | 6e8dbc2 | 2008-09-12 09:44:41 -0700 | [diff] [blame] | 328 | extern int32 panicking; |
Ken Thompson | 79fbbe3 | 2008-11-05 21:50:28 -0800 | [diff] [blame] | 329 | extern int32 maxround; |
Rob Pike | f6d67c9 | 2009-10-13 22:48:03 -0700 | [diff] [blame] | 330 | extern int32 fd; // usually 1; set to 2 when panicking |
Russ Cox | 5328df6 | 2010-01-09 09:47:45 -0800 | [diff] [blame] | 331 | extern int32 gcwaiting; // gc is waiting to run |
Russ Cox | 1b14bdb | 2009-09-22 16:28:32 -0700 | [diff] [blame] | 332 | int8* goos; |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 333 | |
| 334 | /* |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 335 | * common functions and data |
| 336 | */ |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 337 | int32 strcmp(byte*, byte*); |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 338 | int32 findnull(byte*); |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 339 | int32 findnullw(uint16*); |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 340 | void dump(byte*, int32); |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 341 | int32 runetochar(byte*, int32); |
Rob Pike | 54ec719 | 2009-04-12 17:01:17 -0700 | [diff] [blame] | 342 | int32 charntorune(int32*, uint8*, int32); |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 343 | |
Rob Pike | 8e82a67 | 2008-06-30 11:50:36 -0700 | [diff] [blame] | 344 | /* |
Ken Thompson | 4e8142c | 2008-06-16 22:34:50 -0700 | [diff] [blame] | 345 | * very low level c-called |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 346 | */ |
Russ Cox | 7343e03 | 2009-06-17 15:12:16 -0700 | [diff] [blame] | 347 | void gogo(Gobuf*, uintptr); |
| 348 | void gogocall(Gobuf*, void(*)(void)); |
| 349 | uintptr gosave(Gobuf*); |
Russ Cox | 718be32 | 2010-01-25 18:52:55 -0800 | [diff] [blame] | 350 | void ·lessstack(void); |
Russ Cox | 3609624 | 2009-01-16 14:58:14 -0800 | [diff] [blame] | 351 | void goargs(void); |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 352 | void FLUSH(void*); |
Rob Pike | d3204ef | 2008-06-30 14:39:47 -0700 | [diff] [blame] | 353 | void* getu(void); |
Ken Thompson | 4e8142c | 2008-06-16 22:34:50 -0700 | [diff] [blame] | 354 | void throw(int8*); |
Ken Thompson | 594175d | 2008-07-13 14:29:46 -0700 | [diff] [blame] | 355 | uint32 rnd(uint32, uint32); |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 356 | void prints(int8*); |
Russ Cox | efc86a7 | 2008-11-25 16:48:10 -0800 | [diff] [blame] | 357 | void printf(int8*, ...); |
Russ Cox | 3aa063d | 2008-11-23 17:08:55 -0800 | [diff] [blame] | 358 | byte* mchr(byte*, byte, byte*); |
Ken Thompson | e1a06cc | 2008-06-15 20:24:30 -0700 | [diff] [blame] | 359 | void mcpy(byte*, byte*, uint32); |
Russ Cox | 9b6d385 | 2009-01-26 12:36:21 -0800 | [diff] [blame] | 360 | int32 mcmp(byte*, byte*, uint32); |
Russ Cox | 652f556 | 2009-11-20 09:11:46 -0800 | [diff] [blame] | 361 | void memmove(void*, void*, uint32); |
Russ Cox | 596c16e | 2010-03-23 20:48:23 -0700 | [diff] [blame^] | 362 | void* mal(uintptr); |
| 363 | void* malx(uintptr size, int32 skip_delta); |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 364 | uint32 cmpstring(String, String); |
| 365 | String gostring(byte*); |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 366 | String gostringw(uint16*); |
Rob Pike | aeb4398 | 2008-06-21 15:36:23 -0700 | [diff] [blame] | 367 | void initsig(void); |
Russ Cox | fb40f88 | 2008-09-22 13:47:53 -0700 | [diff] [blame] | 368 | int32 gotraceback(void); |
Ken Thompson | 751ce3a | 2008-07-11 19:16:39 -0700 | [diff] [blame] | 369 | void traceback(uint8 *pc, uint8 *sp, G* gp); |
Rob Pike | 3835e01 | 2008-07-28 11:29:41 -0700 | [diff] [blame] | 370 | void tracebackothers(G*); |
Rob Pike | c870ac2 | 2008-07-14 20:54:55 -0700 | [diff] [blame] | 371 | int32 open(byte*, int32, ...); |
Rob Pike | c870ac2 | 2008-07-14 20:54:55 -0700 | [diff] [blame] | 372 | int32 write(int32, void*, int32); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 373 | bool cas(uint32*, uint32, uint32); |
Hector Chu | 6bfe5f5 | 2010-01-06 17:58:55 -0800 | [diff] [blame] | 374 | bool casp(void**, void*, void*); |
| 375 | uint32 xadd(uint32 volatile*, int32); |
Russ Cox | aa3222d8 | 2009-06-02 23:02:12 -0700 | [diff] [blame] | 376 | void jmpdefer(byte*, void*); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 377 | void exit1(int32); |
| 378 | void ready(G*); |
| 379 | byte* getenv(int8*); |
| 380 | int32 atoi(byte*); |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 381 | void newosproc(M *m, G *g, void *stk, void (*fn)(void)); |
Russ Cox | a67258f | 2008-09-18 15:56:46 -0700 | [diff] [blame] | 382 | void signalstack(byte*, int32); |
| 383 | G* malg(int32); |
| 384 | void minit(void); |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 385 | Func* findfunc(uintptr); |
Russ Cox | a543336 | 2008-11-25 09:23:36 -0800 | [diff] [blame] | 386 | int32 funcline(Func*, uint64); |
Russ Cox | 79e1db2 | 2008-12-04 08:30:54 -0800 | [diff] [blame] | 387 | void* stackalloc(uint32); |
| 388 | void stackfree(void*); |
Russ Cox | e29ce17 | 2008-12-18 15:42:28 -0800 | [diff] [blame] | 389 | MCache* allocmcache(void); |
| 390 | void mallocinit(void); |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 391 | bool ifaceeq(Iface, Iface); |
Russ Cox | 2da5022 | 2009-05-20 14:57:55 -0700 | [diff] [blame] | 392 | bool efaceeq(Eface, Eface); |
Russ Cox | a52fb81 | 2009-06-04 21:09:06 -0700 | [diff] [blame] | 393 | uintptr ifacehash(Iface); |
| 394 | uintptr efacehash(Eface); |
| 395 | uintptr nohash(uint32, void*); |
Russ Cox | a7f6d40 | 2009-01-26 09:56:42 -0800 | [diff] [blame] | 396 | uint32 noequal(uint32, void*, void*); |
Russ Cox | 1ce1791 | 2009-01-26 17:37:05 -0800 | [diff] [blame] | 397 | void* malloc(uintptr size); |
Russ Cox | 1ce1791 | 2009-01-26 17:37:05 -0800 | [diff] [blame] | 398 | void free(void *v); |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 399 | void addfinalizer(void*, void(*fn)(void*), int32); |
Russ Cox | 918afd94 | 2009-05-08 15:21:41 -0700 | [diff] [blame] | 400 | void exit(int32); |
| 401 | void breakpoint(void); |
| 402 | void gosched(void); |
| 403 | void goexit(void); |
Russ Cox | 133a158 | 2009-10-03 10:37:12 -0700 | [diff] [blame] | 404 | void runcgo(void (*fn)(void*), void*); |
Russ Cox | 718be32 | 2010-01-25 18:52:55 -0800 | [diff] [blame] | 405 | void ·entersyscall(void); |
| 406 | void ·exitsyscall(void); |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 407 | void newproc1(byte*, byte*, int32, int32); |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 408 | void siginit(void); |
Russ Cox | 08579c2 | 2009-12-16 20:20:50 -0800 | [diff] [blame] | 409 | bool sigsend(int32 sig); |
Russ Cox | e4f0681 | 2010-02-08 14:32:22 -0800 | [diff] [blame] | 410 | void gettime(int64*, int32*); |
Russ Cox | 2b7d147 | 2010-03-23 17:01:17 -0700 | [diff] [blame] | 411 | int32 callers(int32, uintptr*, int32); |
Russ Cox | e4f0681 | 2010-02-08 14:32:22 -0800 | [diff] [blame] | 412 | int64 nanotime(void); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 413 | |
Russ Cox | 5bb0c4f | 2008-12-15 10:50:33 -0800 | [diff] [blame] | 414 | #pragma varargck argpos printf 1 |
| 415 | |
| 416 | #pragma varargck type "d" int32 |
| 417 | #pragma varargck type "d" uint32 |
| 418 | #pragma varargck type "D" int64 |
| 419 | #pragma varargck type "D" uint64 |
| 420 | #pragma varargck type "x" int32 |
| 421 | #pragma varargck type "x" uint32 |
| 422 | #pragma varargck type "X" int64 |
| 423 | #pragma varargck type "X" uint64 |
| 424 | #pragma varargck type "p" void* |
Russ Cox | 0d3a043 | 2009-03-30 00:01:07 -0700 | [diff] [blame] | 425 | #pragma varargck type "p" uintptr |
Russ Cox | 5bb0c4f | 2008-12-15 10:50:33 -0800 | [diff] [blame] | 426 | #pragma varargck type "s" int8* |
| 427 | #pragma varargck type "s" uint8* |
Ken Thompson | 3657061 | 2009-04-09 18:16:21 -0700 | [diff] [blame] | 428 | #pragma varargck type "S" String |
Russ Cox | 5bb0c4f | 2008-12-15 10:50:33 -0800 | [diff] [blame] | 429 | |
Russ Cox | 3f8aa66 | 2008-12-05 15:24:18 -0800 | [diff] [blame] | 430 | // TODO(rsc): Remove. These are only temporary, |
| 431 | // for the mark and sweep collector. |
| 432 | void stoptheworld(void); |
| 433 | void starttheworld(void); |
| 434 | |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 435 | /* |
| 436 | * mutual exclusion locks. in the uncontended case, |
| 437 | * as fast as spin locks (just a few user-level instructions), |
| 438 | * but on the contention path they sleep in the kernel. |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 439 | * a zeroed Lock is unlocked (no need to initialize each lock). |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 440 | */ |
| 441 | void lock(Lock*); |
| 442 | void unlock(Lock*); |
Russ Cox | 62d627f | 2010-02-08 21:41:54 -0800 | [diff] [blame] | 443 | void destroylock(Lock*); |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 444 | |
| 445 | /* |
Russ Cox | a68592a | 2009-10-14 13:02:05 -0700 | [diff] [blame] | 446 | * sleep and wakeup on one-time events. |
Russ Cox | f7f6329 | 2008-08-05 14:21:42 -0700 | [diff] [blame] | 447 | * before any calls to notesleep or notewakeup, |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 448 | * must call noteclear to initialize the Note. |
| 449 | * then, any number of threads can call notesleep |
| 450 | * and exactly one thread can call notewakeup (once). |
| 451 | * once notewakeup has been called, all the notesleeps |
| 452 | * will return. future notesleeps will return immediately. |
Russ Cox | d28acc4 | 2008-08-04 16:43:49 -0700 | [diff] [blame] | 453 | */ |
Russ Cox | 9682400 | 2008-08-05 14:18:47 -0700 | [diff] [blame] | 454 | void noteclear(Note*); |
| 455 | void notesleep(Note*); |
| 456 | void notewakeup(Note*); |
Ken Thompson | 4e8142c | 2008-06-16 22:34:50 -0700 | [diff] [blame] | 457 | |
| 458 | /* |
Ian Lance Taylor | 9b8da82 | 2009-01-13 09:55:24 -0800 | [diff] [blame] | 459 | * Redefine methods for the benefit of gcc, which does not support |
| 460 | * UTF-8 characters in identifiers. |
| 461 | */ |
| 462 | #ifndef __GNUC__ |
Russ Cox | 718be32 | 2010-01-25 18:52:55 -0800 | [diff] [blame] | 463 | #define runtime_memclr ·memclr |
| 464 | #define runtime_getcallerpc ·getcallerpc |
| 465 | #define runtime_mmap ·mmap |
| 466 | #define runtime_printslice ·printslice |
| 467 | #define runtime_printbool ·printbool |
| 468 | #define runtime_printfloat ·printfloat |
| 469 | #define runtime_printhex ·printhex |
| 470 | #define runtime_printint ·printint |
| 471 | #define runtime_printiface ·printiface |
| 472 | #define runtime_printeface ·printeface |
| 473 | #define runtime_printpc ·printpc |
| 474 | #define runtime_printpointer ·printpointer |
| 475 | #define runtime_printstring ·printstring |
| 476 | #define runtime_printuint ·printuint |
Ken Thompson | f59cb49 | 2010-02-19 20:42:50 -0800 | [diff] [blame] | 477 | #define runtime_printcomplex ·printcomplex |
Russ Cox | 718be32 | 2010-01-25 18:52:55 -0800 | [diff] [blame] | 478 | #define runtime_setcallerpc ·setcallerpc |
Ian Lance Taylor | 9b8da82 | 2009-01-13 09:55:24 -0800 | [diff] [blame] | 479 | #endif |
| 480 | |
| 481 | /* |
Adam Langley | 3f7a324 | 2009-11-13 10:08:51 -0800 | [diff] [blame] | 482 | * This is consistent across Linux and BSD. |
| 483 | * If a new OS is added that is different, move this to |
| 484 | * $GOOS/$GOARCH/defs.h. |
| 485 | */ |
| 486 | #define EACCES 13 |
| 487 | |
| 488 | /* |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 489 | * low level go-called |
Ken Thompson | 4e8142c | 2008-06-16 22:34:50 -0700 | [diff] [blame] | 490 | */ |
Russ Cox | 22a5c78 | 2009-10-15 23:10:49 -0700 | [diff] [blame] | 491 | uint8* runtime_mmap(byte*, uint32, int32, int32, int32, uint32); |
| 492 | void runtime_memclr(byte*, uint32); |
| 493 | void runtime_setcallerpc(void*, void*); |
| 494 | void* runtime_getcallerpc(void*); |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 495 | |
| 496 | /* |
Ken Thompson | 4e8142c | 2008-06-16 22:34:50 -0700 | [diff] [blame] | 497 | * runtime go-called |
Ken Thompson | bbb2073 | 2008-06-05 19:38:39 -0700 | [diff] [blame] | 498 | */ |
Russ Cox | 22a5c78 | 2009-10-15 23:10:49 -0700 | [diff] [blame] | 499 | void runtime_printbool(bool); |
| 500 | void runtime_printfloat(float64); |
| 501 | void runtime_printint(int64); |
| 502 | void runtime_printiface(Iface); |
| 503 | void runtime_printeface(Eface); |
| 504 | void runtime_printstring(String); |
| 505 | void runtime_printpc(void*); |
| 506 | void runtime_printpointer(void*); |
| 507 | void runtime_printuint(uint64); |
| 508 | void runtime_printhex(uint64); |
| 509 | void runtime_printslice(Slice); |
Ken Thompson | f59cb49 | 2010-02-19 20:42:50 -0800 | [diff] [blame] | 510 | void runtime_printcomplex(Complex128); |
Russ Cox | a186b77 | 2010-01-27 15:37:08 -0800 | [diff] [blame] | 511 | void ·panicl(int32); |
Rob Pike | 6db99de | 2008-07-08 10:36:43 -0700 | [diff] [blame] | 512 | |
| 513 | /* |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 514 | * wrapped for go users |
Rob Pike | 6db99de | 2008-07-08 10:36:43 -0700 | [diff] [blame] | 515 | */ |
Russ Cox | 1f8a40d | 2009-01-22 16:23:44 -0800 | [diff] [blame] | 516 | float64 Inf(int32 sign); |
| 517 | float64 NaN(void); |
| 518 | float32 float32frombits(uint32 i); |
| 519 | uint32 float32tobits(float32 f); |
| 520 | float64 float64frombits(uint64 i); |
| 521 | uint64 float64tobits(float64 f); |
| 522 | float64 frexp(float64 d, int32 *ep); |
| 523 | bool isInf(float64 f, int32 sign); |
| 524 | bool isNaN(float64 f); |
| 525 | float64 ldexp(float64 d, int32 e); |
| 526 | float64 modf(float64 d, float64 *ip); |
| 527 | void semacquire(uint32*); |
| 528 | void semrelease(uint32*); |
David Symonds | b586649 | 2009-12-15 18:21:29 -0800 | [diff] [blame] | 529 | String signame(int32 sig); |
| 530 | |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 531 | |
Russ Cox | 764b6ec | 2009-07-08 13:55:57 -0700 | [diff] [blame] | 532 | void mapassign(Hmap*, byte*, byte*); |
| 533 | void mapaccess(Hmap*, byte*, byte*, bool*); |
| 534 | struct hash_iter* mapiterinit(Hmap*); |
| 535 | void mapiternext(struct hash_iter*); |
| 536 | bool mapiterkey(struct hash_iter*, void*); |
| 537 | void mapiterkeyvalue(struct hash_iter*, void*, void*); |
Russ Cox | 7a0f4ca | 2009-09-08 13:46:54 -0700 | [diff] [blame] | 538 | Hmap* makemap(Type*, Type*, uint32); |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 539 | |
Russ Cox | 7a0f4ca | 2009-09-08 13:46:54 -0700 | [diff] [blame] | 540 | Hchan* makechan(Type*, uint32); |
Russ Cox | 5ddaf9a | 2009-07-08 15:00:54 -0700 | [diff] [blame] | 541 | void chansend(Hchan*, void*, bool*); |
| 542 | void chanrecv(Hchan*, void*, bool*); |
Russ Cox | 653cef1 | 2009-08-26 10:47:18 -0700 | [diff] [blame] | 543 | void chanclose(Hchan*); |
| 544 | bool chanclosed(Hchan*); |
Russ Cox | de7920e | 2009-08-26 12:42:22 -0700 | [diff] [blame] | 545 | int32 chanlen(Hchan*); |
| 546 | int32 chancap(Hchan*); |
Russ Cox | 92e9257 | 2009-07-10 16:32:26 -0700 | [diff] [blame] | 547 | |
| 548 | void ifaceE2I(struct InterfaceType*, Eface, Iface*); |