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