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