Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [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 | |
| 5 | package gc |
| 6 | |
| 7 | import ( |
| 8 | "bytes" |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 9 | "cmd/compile/internal/ssa" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 10 | "cmd/internal/obj" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 13 | const ( |
Rob Pike | 24a43e6 | 2015-03-05 10:39:23 -0800 | [diff] [blame] | 14 | UINF = 100 |
Rob Pike | 24a43e6 | 2015-03-05 10:39:23 -0800 | [diff] [blame] | 15 | BADWIDTH = -1000000000 |
| 16 | MaxStackVarSize = 10 * 1024 * 1024 |
| 17 | ) |
| 18 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 19 | type Val struct { |
Josh Bleecher Snyder | 13485be | 2015-05-14 17:57:42 -0700 | [diff] [blame] | 20 | // U contains one of: |
Russ Cox | 71080fb | 2015-05-26 22:50:45 -0400 | [diff] [blame] | 21 | // bool bool when n.ValCtype() == CTBOOL |
| 22 | // *Mpint int when n.ValCtype() == CTINT, rune when n.ValCtype() == CTRUNE |
| 23 | // *Mpflt float when n.ValCtype() == CTFLT |
| 24 | // *Mpcplx pair of floats when n.ValCtype() == CTCPLX |
| 25 | // string string when n.ValCtype() == CTSTR |
| 26 | // *Nilval when n.ValCtype() == CTNIL |
Josh Bleecher Snyder | 13485be | 2015-05-14 17:57:42 -0700 | [diff] [blame] | 27 | U interface{} |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 28 | } |
| 29 | |
Russ Cox | 71080fb | 2015-05-26 22:50:45 -0400 | [diff] [blame] | 30 | type NilVal struct{} |
| 31 | |
Robert Griesemer | 53d43cb | 2015-10-26 16:00:59 -0700 | [diff] [blame] | 32 | func (v Val) Ctype() Ctype { |
Russ Cox | 71080fb | 2015-05-26 22:50:45 -0400 | [diff] [blame] | 33 | switch x := v.U.(type) { |
| 34 | default: |
HÃ¥vard Haugen | 3c9fa38 | 2015-08-30 23:10:03 +0200 | [diff] [blame] | 35 | Fatalf("unexpected Ctype for %T", v.U) |
Russ Cox | a53710f | 2015-06-03 14:16:01 -0400 | [diff] [blame] | 36 | panic("not reached") |
| 37 | case nil: |
Russ Cox | 71080fb | 2015-05-26 22:50:45 -0400 | [diff] [blame] | 38 | return 0 |
| 39 | case *NilVal: |
| 40 | return CTNIL |
| 41 | case bool: |
| 42 | return CTBOOL |
| 43 | case *Mpint: |
| 44 | if x.Rune { |
| 45 | return CTRUNE |
| 46 | } |
| 47 | return CTINT |
| 48 | case *Mpflt: |
| 49 | return CTFLT |
| 50 | case *Mpcplx: |
| 51 | return CTCPLX |
| 52 | case string: |
| 53 | return CTSTR |
| 54 | } |
| 55 | } |
| 56 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 57 | type Pkg struct { |
David Chase | 8eec2bb | 2016-03-11 00:10:52 -0500 | [diff] [blame] | 58 | Name string // package name, e.g. "sys" |
| 59 | Path string // string literal used in import statement, e.g. "runtime/internal/sys" |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 60 | Pathsym *Sym |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 61 | Prefix string // escaped path for use in symbol table |
Marvin Stenger | e03c789 | 2015-09-08 05:46:31 +0200 | [diff] [blame] | 62 | Imported bool // export data of this package was parsed |
| 63 | Exported bool // import line written in export data |
| 64 | Direct bool // imported directly |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 65 | Safe bool // whether the package is marked as safe |
Russ Cox | d0b59de | 2015-03-02 16:21:15 -0500 | [diff] [blame] | 66 | Syms map[string]*Sym |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | type Sym struct { |
Robert Griesemer | 37c2972 | 2016-03-01 16:37:20 -0800 | [diff] [blame] | 70 | Flags SymFlags |
Matthew Dempsky | 071e43a | 2016-02-26 01:37:28 -0800 | [diff] [blame] | 71 | Link *Sym |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 72 | Importdef *Pkg // where imported definition was found |
| 73 | Linkname string // link name |
| 74 | |
| 75 | // saved and restored by dcopy |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 76 | Pkg *Pkg |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 77 | Name string // variable name |
| 78 | Def *Node // definition: ONAME OTYPE OPACK or OLITERAL |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 79 | Block int32 // blocknumber to catch redeclaration |
| 80 | Lastlineno int32 // last declaration for diagnostic |
Robert Griesemer | 157f069 | 2016-03-10 15:07:08 -0800 | [diff] [blame] | 81 | |
| 82 | Label *Label // corresponding label (ephemeral) |
| 83 | Origpkg *Pkg // original package for . import |
| 84 | Lsym *obj.LSym |
| 85 | Fsym *Sym // funcsym |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 88 | type Label struct { |
Robert Griesemer | def9c0b | 2016-03-10 20:35:27 -0800 | [diff] [blame] | 89 | Sym *Sym |
| 90 | Def *Node |
| 91 | Use []*Node |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 92 | |
| 93 | // for use during gen |
| 94 | Gotopc *obj.Prog // pointer to unresolved gotos |
| 95 | Labelpc *obj.Prog // pointer to code |
| 96 | Breakpc *obj.Prog // pointer to code |
| 97 | Continpc *obj.Prog // pointer to code |
Dave Cheney | 8712e18 | 2015-09-07 11:11:14 +1000 | [diff] [blame] | 98 | |
| 99 | Used bool |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Robert Griesemer | 37c2972 | 2016-03-01 16:37:20 -0800 | [diff] [blame] | 102 | type SymFlags uint8 |
| 103 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 104 | const ( |
Robert Griesemer | 37c2972 | 2016-03-01 16:37:20 -0800 | [diff] [blame] | 105 | SymExport SymFlags = 1 << iota // to be exported |
| 106 | SymPackage |
| 107 | SymExported // already written out by export |
| 108 | SymUniq |
| 109 | SymSiggen |
| 110 | SymAsm |
| 111 | SymAlgGen |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 112 | ) |
| 113 | |
| 114 | var dclstack *Sym |
| 115 | |
Robert Griesemer | 53d43cb | 2015-10-26 16:00:59 -0700 | [diff] [blame] | 116 | // Ctype describes the constant kind of an "ideal" (untyped) constant. |
| 117 | type Ctype int8 |
| 118 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 119 | const ( |
Robert Griesemer | 53d43cb | 2015-10-26 16:00:59 -0700 | [diff] [blame] | 120 | CTxxx Ctype = iota |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 121 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 122 | CTINT |
| 123 | CTRUNE |
| 124 | CTFLT |
| 125 | CTCPLX |
| 126 | CTSTR |
| 127 | CTBOOL |
| 128 | CTNIL |
| 129 | ) |
| 130 | |
Brad Fitzpatrick | 386c0e6 | 2016-04-03 22:58:10 +0000 | [diff] [blame] | 131 | // ChanDir is whether a channel can send, receive, or both. |
| 132 | type ChanDir uint8 |
| 133 | |
| 134 | func (c ChanDir) CanRecv() bool { return c&Crecv != 0 } |
| 135 | func (c ChanDir) CanSend() bool { return c&Csend != 0 } |
| 136 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 137 | const ( |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 138 | // types of channel |
David Crawshaw | e6d6ad4 | 2016-02-16 15:30:32 -0500 | [diff] [blame] | 139 | // must match ../../../../reflect/type.go:/ChanDir |
Brad Fitzpatrick | 386c0e6 | 2016-04-03 22:58:10 +0000 | [diff] [blame] | 140 | Crecv ChanDir = 1 << 0 |
| 141 | Csend ChanDir = 1 << 1 |
| 142 | Cboth ChanDir = Crecv | Csend |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 143 | ) |
| 144 | |
Robert Griesemer | cd7d738 | 2015-10-26 14:57:36 -0700 | [diff] [blame] | 145 | // The Class of a variable/function describes the "storage class" |
| 146 | // of a variable or function. During parsing, storage classes are |
| 147 | // called declaration contexts. |
| 148 | type Class uint8 |
| 149 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 150 | const ( |
Robert Griesemer | cd7d738 | 2015-10-26 14:57:36 -0700 | [diff] [blame] | 151 | Pxxx Class = iota |
| 152 | PEXTERN // global variable |
| 153 | PAUTO // local variables |
| 154 | PPARAM // input arguments |
| 155 | PPARAMOUT // output results |
| 156 | PPARAMREF // closure variable reference |
| 157 | PFUNC // global function |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 158 | |
| 159 | PDISCARD // discard during parse of duplicate import |
| 160 | |
Robert Griesemer | cd7d738 | 2015-10-26 14:57:36 -0700 | [diff] [blame] | 161 | PHEAP = 1 << 7 // an extra bit to identify an escaped variable |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 162 | ) |
| 163 | |
| 164 | const ( |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 165 | Etop = 1 << 1 // evaluated at statement level |
| 166 | Erv = 1 << 2 // evaluated in value context |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 167 | Etype = 1 << 3 |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 168 | Ecall = 1 << 4 // call-only expressions are ok |
| 169 | Efnstruct = 1 << 5 // multivalue function returns are ok |
| 170 | Eiota = 1 << 6 // iota is ok |
| 171 | Easgn = 1 << 7 // assigning to expression |
| 172 | Eindir = 1 << 8 // indirecting through expression |
| 173 | Eaddr = 1 << 9 // taking address of expression |
| 174 | Eproc = 1 << 10 // inside a go statement |
| 175 | Ecomplit = 1 << 11 // type in composite literal |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 176 | ) |
| 177 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 178 | type Sig struct { |
| 179 | name string |
| 180 | pkg *Pkg |
| 181 | isym *Sym |
| 182 | tsym *Sym |
| 183 | type_ *Type |
| 184 | mtype *Type |
| 185 | offset int32 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 186 | } |
| 187 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 188 | // note this is the runtime representation |
| 189 | // of the compilers arrays. |
| 190 | // |
| 191 | // typedef struct |
| 192 | // { // must not move anything |
| 193 | // uchar array[8]; // pointer to data |
| 194 | // uchar nel[4]; // number of elements |
| 195 | // uchar cap[4]; // allocated number of elements |
| 196 | // } Array; |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 197 | var Array_array int // runtime offsetof(Array,array) - same for String |
| 198 | |
| 199 | var Array_nel int // runtime offsetof(Array,nel) - same for String |
| 200 | |
| 201 | var Array_cap int // runtime offsetof(Array,cap) |
| 202 | |
| 203 | var sizeof_Array int // runtime sizeof(Array) |
| 204 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 205 | // note this is the runtime representation |
| 206 | // of the compilers strings. |
| 207 | // |
| 208 | // typedef struct |
| 209 | // { // must not move anything |
| 210 | // uchar array[8]; // pointer to data |
| 211 | // uchar nel[4]; // number of elements |
| 212 | // } String; |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 213 | var sizeof_String int // runtime sizeof(String) |
| 214 | |
Matthew Dempsky | e0fa809 | 2016-02-25 16:07:04 -0800 | [diff] [blame] | 215 | // lexlineno is the line number _after_ the most recently read rune. |
| 216 | // In particular, it's advanced (or rewound) as newlines are read (or unread). |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 217 | var lexlineno int32 |
| 218 | |
Matthew Dempsky | e0fa809 | 2016-02-25 16:07:04 -0800 | [diff] [blame] | 219 | // lineno is the line number at the start of the most recently lexed token. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 220 | var lineno int32 |
| 221 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 222 | var pragcgobuf string |
| 223 | |
| 224 | var infile string |
| 225 | |
| 226 | var outfile string |
| 227 | |
| 228 | var bout *obj.Biobuf |
| 229 | |
| 230 | var nerrors int |
| 231 | |
| 232 | var nsavederrors int |
| 233 | |
| 234 | var nsyntaxerrors int |
| 235 | |
Josh Bleecher Snyder | 2b063bd | 2015-05-14 19:33:31 -0700 | [diff] [blame] | 236 | var decldepth int32 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 237 | |
| 238 | var safemode int |
| 239 | |
| 240 | var nolocalimports int |
| 241 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 242 | var lexbuf bytes.Buffer |
| 243 | var strbuf bytes.Buffer |
Robert Griesemer | c8bc7f1 | 2015-11-13 14:04:40 -0800 | [diff] [blame] | 244 | var litbuf string // LLITERAL value for use in syntax error messages |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 245 | |
| 246 | var Debug [256]int |
| 247 | |
| 248 | var debugstr string |
| 249 | |
| 250 | var Debug_checknil int |
Russ Cox | 4224d81 | 2015-03-20 00:06:10 -0400 | [diff] [blame] | 251 | var Debug_typeassert int |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 252 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 253 | var localpkg *Pkg // package being compiled |
| 254 | |
| 255 | var importpkg *Pkg // package being imported |
| 256 | |
Michel Lespinasse | 859b63c | 2016-03-18 17:21:33 -0700 | [diff] [blame] | 257 | var itabpkg *Pkg // fake pkg for itab entries |
Michel Lespinasse | f00bbd5 | 2016-03-17 06:18:13 -0700 | [diff] [blame] | 258 | |
| 259 | var itablinkpkg *Pkg // fake package for runtime itab entries |
| 260 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 261 | var Runtimepkg *Pkg // package runtime |
| 262 | |
| 263 | var racepkg *Pkg // package runtime/race |
| 264 | |
Ian Lance Taylor | 0c69f13 | 2015-10-21 07:04:10 -0700 | [diff] [blame] | 265 | var msanpkg *Pkg // package runtime/msan |
| 266 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 267 | var typepkg *Pkg // fake package for runtime type info (headers) |
| 268 | |
| 269 | var typelinkpkg *Pkg // fake package for runtime type info (data) |
| 270 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 271 | var unsafepkg *Pkg // package unsafe |
| 272 | |
| 273 | var trackpkg *Pkg // fake package for field tracking |
| 274 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 275 | var Tptr EType // either TPTR32 or TPTR64 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 276 | |
| 277 | var myimportpath string |
| 278 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 279 | var localimport string |
| 280 | |
| 281 | var asmhdr string |
| 282 | |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 283 | var Simtype [NTYPE]EType |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 284 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 285 | var ( |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 286 | isforw [NTYPE]bool |
| 287 | Isint [NTYPE]bool |
| 288 | Isfloat [NTYPE]bool |
| 289 | Iscomplex [NTYPE]bool |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 290 | issimple [NTYPE]bool |
| 291 | ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 292 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 293 | var ( |
| 294 | okforeq [NTYPE]bool |
| 295 | okforadd [NTYPE]bool |
| 296 | okforand [NTYPE]bool |
| 297 | okfornone [NTYPE]bool |
| 298 | okforcmp [NTYPE]bool |
| 299 | okforbool [NTYPE]bool |
| 300 | okforcap [NTYPE]bool |
| 301 | okforlen [NTYPE]bool |
| 302 | okforarith [NTYPE]bool |
| 303 | okforconst [NTYPE]bool |
| 304 | ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 305 | |
Josh Bleecher Snyder | 25da594 | 2015-03-01 07:54:01 +0000 | [diff] [blame] | 306 | var ( |
| 307 | okfor [OEND][]bool |
| 308 | iscmp [OEND]bool |
| 309 | ) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 310 | |
| 311 | var Minintval [NTYPE]*Mpint |
| 312 | |
| 313 | var Maxintval [NTYPE]*Mpint |
| 314 | |
| 315 | var minfltval [NTYPE]*Mpflt |
| 316 | |
| 317 | var maxfltval [NTYPE]*Mpflt |
| 318 | |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 319 | var xtop []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 320 | |
Dave Cheney | d5fe165 | 2015-09-10 15:57:39 +1000 | [diff] [blame] | 321 | var externdcl []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 322 | |
Robert Griesemer | 3d3bc88 | 2015-08-12 14:29:50 -0700 | [diff] [blame] | 323 | var exportlist []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 324 | |
HÃ¥vard Haugen | 391cc54 | 2015-09-06 22:38:49 +0200 | [diff] [blame] | 325 | var importlist []*Node // imported functions and methods with inlinable bodies |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 326 | |
Robert Griesemer | a2119ac | 2015-10-05 16:33:53 -0700 | [diff] [blame] | 327 | var funcsyms []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 328 | |
Robert Griesemer | cd7d738 | 2015-10-26 14:57:36 -0700 | [diff] [blame] | 329 | var dclcontext Class // PEXTERN/PAUTO |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 330 | |
| 331 | var incannedimport int |
| 332 | |
| 333 | var statuniqgen int // name generator for static temps |
| 334 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 335 | var iota_ int32 |
| 336 | |
Ian Lance Taylor | f444b8a | 2016-03-09 20:29:21 -0800 | [diff] [blame] | 337 | var lastconst []*Node |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 338 | |
| 339 | var lasttype *Node |
| 340 | |
| 341 | var Maxarg int64 |
| 342 | |
| 343 | var Stksize int64 // stack size for current frame |
| 344 | |
| 345 | var stkptrsize int64 // prefix of stack containing pointers |
| 346 | |
| 347 | var blockgen int32 // max block number |
| 348 | |
| 349 | var block int32 // current block number |
| 350 | |
HÃ¥vard Haugen | 2594664 | 2015-09-07 22:19:30 +0200 | [diff] [blame] | 351 | var hasdefer bool // flag that curfn has defer statement |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 352 | |
| 353 | var Curfn *Node |
| 354 | |
| 355 | var Widthptr int |
| 356 | |
| 357 | var Widthint int |
| 358 | |
| 359 | var Widthreg int |
| 360 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 361 | var nblank *Node |
| 362 | |
Dave Cheney | e498181 | 2015-03-10 09:58:01 +1100 | [diff] [blame] | 363 | var Funcdepth int32 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 364 | |
HÃ¥vard Haugen | dc3540d | 2015-08-30 23:56:40 +0200 | [diff] [blame] | 365 | var typecheckok bool |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 366 | |
| 367 | var compiling_runtime int |
| 368 | |
| 369 | var compiling_wrappers int |
| 370 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 371 | var use_writebarrier int |
| 372 | |
| 373 | var pure_go int |
| 374 | |
| 375 | var flag_installsuffix string |
| 376 | |
| 377 | var flag_race int |
| 378 | |
Ian Lance Taylor | 0c69f13 | 2015-10-21 07:04:10 -0700 | [diff] [blame] | 379 | var flag_msan int |
| 380 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 381 | var flag_largemodel int |
| 382 | |
Ian Lance Taylor | 9e902f0 | 2015-10-20 10:00:07 -0700 | [diff] [blame] | 383 | // Whether we are adding any sort of code instrumentation, such as |
| 384 | // when the race detector is enabled. |
| 385 | var instrumenting bool |
| 386 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 387 | var debuglive int |
| 388 | |
| 389 | var Ctxt *obj.Link |
| 390 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 391 | var writearchive int |
| 392 | |
| 393 | var bstdout obj.Biobuf |
| 394 | |
| 395 | var Nacl bool |
| 396 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 397 | var continpc *obj.Prog |
| 398 | |
| 399 | var breakpc *obj.Prog |
| 400 | |
| 401 | var Pc *obj.Prog |
| 402 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 403 | var nodfp *Node |
| 404 | |
| 405 | var Disable_checknil int |
| 406 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 407 | type Flow struct { |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 408 | Prog *obj.Prog // actual instruction |
| 409 | P1 *Flow // predecessors of this instruction: p1, |
| 410 | P2 *Flow // and then p2 linked though p2link. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 411 | P2link *Flow |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 412 | S1 *Flow // successors of this instruction (at most two: s1 and s2). |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 413 | S2 *Flow |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 414 | Link *Flow // next instruction in function code |
| 415 | |
| 416 | Active int32 // usable by client |
| 417 | |
| 418 | Id int32 // sequence number in flow graph |
| 419 | Rpo int32 // reverse post ordering |
| 420 | Loop uint16 // x5 for every loop |
Marvin Stenger | 9ac0fff | 2015-09-08 03:51:30 +0200 | [diff] [blame] | 421 | Refset bool // diagnostic generated |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 422 | |
| 423 | Data interface{} // for use by client |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | type Graph struct { |
| 427 | Start *Flow |
| 428 | Num int |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 429 | |
| 430 | // After calling flowrpo, rpo lists the flow nodes in reverse postorder, |
| 431 | // and each non-dead Flow node f has g->rpo[f->rpo] == f. |
| 432 | Rpo []*Flow |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 433 | } |
| 434 | |
Jeremy Jackins | 6327e8d | 2015-10-22 09:51:12 +0900 | [diff] [blame] | 435 | // interface to back end |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 436 | |
| 437 | const ( |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 438 | // Pseudo-op, like TEXT, GLOBL, TYPE, PCDATA, FUNCDATA. |
| 439 | Pseudo = 1 << 1 |
| 440 | |
| 441 | // There's nothing to say about the instruction, |
| 442 | // but it's still okay to see. |
| 443 | OK = 1 << 2 |
| 444 | |
| 445 | // Size of right-side write, or right-side read if no write. |
| 446 | SizeB = 1 << 3 |
| 447 | SizeW = 1 << 4 |
| 448 | SizeL = 1 << 5 |
| 449 | SizeQ = 1 << 6 |
| 450 | SizeF = 1 << 7 |
| 451 | SizeD = 1 << 8 |
| 452 | |
| 453 | // Left side (Prog.from): address taken, read, write. |
| 454 | LeftAddr = 1 << 9 |
| 455 | LeftRead = 1 << 10 |
| 456 | LeftWrite = 1 << 11 |
| 457 | |
| 458 | // Register in middle (Prog.reg); only ever read. (arm, ppc64) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 459 | RegRead = 1 << 12 |
| 460 | CanRegRead = 1 << 13 |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 461 | |
| 462 | // Right side (Prog.to): address taken, read, write. |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 463 | RightAddr = 1 << 14 |
| 464 | RightRead = 1 << 15 |
| 465 | RightWrite = 1 << 16 |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 466 | |
| 467 | // Instruction kinds |
| 468 | Move = 1 << 17 // straight move |
| 469 | Conv = 1 << 18 // size conversion |
| 470 | Cjmp = 1 << 19 // conditional jump |
| 471 | Break = 1 << 20 // breaks control flow (no fallthrough) |
| 472 | Call = 1 << 21 // function call |
| 473 | Jump = 1 << 22 // jump |
| 474 | Skip = 1 << 23 // data instruction |
| 475 | |
| 476 | // Set, use, or kill of carry bit. |
| 477 | // Kill means we never look at the carry bit after this kind of instruction. |
David Chase | 8eec2bb | 2016-03-11 00:10:52 -0500 | [diff] [blame] | 478 | // Originally for understanding ADC, RCR, and so on, but now also |
| 479 | // tracks set, use, and kill of the zero and overflow bits as well. |
| 480 | // TODO rename to {Set,Use,Kill}Flags |
Russ Cox | cdb7d7d | 2015-03-05 13:57:36 -0500 | [diff] [blame] | 481 | SetCarry = 1 << 24 |
| 482 | UseCarry = 1 << 25 |
| 483 | KillCarry = 1 << 26 |
| 484 | |
| 485 | // Special cases for register use. (amd64, 386) |
| 486 | ShiftCX = 1 << 27 // possible shift by CX |
| 487 | ImulAXDX = 1 << 28 // possible multiply into DX:AX |
| 488 | |
| 489 | // Instruction updates whichever of from/to is type D_OREG. (ppc64) |
| 490 | PostInc = 1 << 29 |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 491 | ) |
| 492 | |
| 493 | type Arch struct { |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 494 | Thechar int |
| 495 | Thestring string |
| 496 | Thelinkarch *obj.LinkArch |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 497 | REGSP int |
| 498 | REGCTXT int |
| 499 | REGCALLX int // BX |
| 500 | REGCALLX2 int // AX |
| 501 | REGRETURN int // AX |
| 502 | REGMIN int |
| 503 | REGMAX int |
Dave Cheney | 888d44d | 2015-04-09 21:25:48 +1000 | [diff] [blame] | 504 | REGZERO int // architectural zero register, if available |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 505 | FREGMIN int |
| 506 | FREGMAX int |
| 507 | MAXWIDTH int64 |
| 508 | ReservedRegs []int |
| 509 | |
| 510 | AddIndex func(*Node, int64, *Node) bool // optional |
| 511 | Betypeinit func() |
Josh Bleecher Snyder | 4a7e5bc | 2015-04-06 19:36:36 -0700 | [diff] [blame] | 512 | Bgen_float func(*Node, bool, int, *obj.Prog) // optional |
| 513 | Cgen64 func(*Node, *Node) // only on 32-bit systems |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 514 | Cgenindex func(*Node, *Node, bool) *obj.Prog |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 515 | Cgen_bmul func(Op, *Node, *Node, *Node) bool |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 516 | Cgen_float func(*Node, *Node) // optional |
| 517 | Cgen_hmul func(*Node, *Node, *Node) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 518 | Cgen_shift func(Op, bool, *Node, *Node, *Node) |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 519 | Clearfat func(*Node) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 520 | Cmp64 func(*Node, *Node, Op, int, *obj.Prog) // only on 32-bit systems |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 521 | Defframe func(*obj.Prog) |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 522 | Dodiv func(Op, *Node, *Node, *Node) |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 523 | Excise func(*Flow) |
| 524 | Expandchecks func(*obj.Prog) |
Russ Cox | 92c826b | 2015-04-03 12:23:28 -0400 | [diff] [blame] | 525 | Getg func(*Node) |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 526 | Gins func(obj.As, *Node, *Node) *obj.Prog |
Russ Cox | f8d14fc | 2015-05-06 12:28:19 -0400 | [diff] [blame] | 527 | |
| 528 | // Ginscmp generates code comparing n1 to n2 and jumping away if op is satisfied. |
| 529 | // The returned prog should be Patch'ed with the jump target. |
| 530 | // If op is not satisfied, code falls through to the next emitted instruction. |
| 531 | // Likely is the branch prediction hint: +1 for likely, -1 for unlikely, 0 for no opinion. |
| 532 | // |
| 533 | // Ginscmp must be able to handle all kinds of arguments for n1 and n2, |
| 534 | // not just simple registers, although it can assume that there are no |
Russ Cox | 6e8bcbb | 2015-05-15 16:11:25 -0400 | [diff] [blame] | 535 | // function calls needed during the evaluation, and on 32-bit systems |
| 536 | // the values are guaranteed not to be 64-bit values, so no in-memory |
| 537 | // temporaries are necessary. |
Marvin Stenger | 8e7a3ea | 2015-09-24 23:21:18 +0200 | [diff] [blame] | 538 | Ginscmp func(op Op, t *Type, n1, n2 *Node, likely int) *obj.Prog |
Russ Cox | f8d14fc | 2015-05-06 12:28:19 -0400 | [diff] [blame] | 539 | |
Josh Bleecher Snyder | 13cb62c | 2015-04-08 09:54:15 -0700 | [diff] [blame] | 540 | // Ginsboolval inserts instructions to convert the result |
| 541 | // of a just-completed comparison to a boolean value. |
| 542 | // The first argument is the conditional jump instruction |
| 543 | // corresponding to the desired value. |
| 544 | // The second argument is the destination. |
| 545 | // If not present, Ginsboolval will be emulated with jumps. |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 546 | Ginsboolval func(obj.As, *Node) |
Russ Cox | f8d14fc | 2015-05-06 12:28:19 -0400 | [diff] [blame] | 547 | |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 548 | Ginscon func(obj.As, int64, *Node) |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 549 | Ginsnop func() |
| 550 | Gmove func(*Node, *Node) |
| 551 | Igenindex func(*Node, *Node, bool) *obj.Prog |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 552 | Peep func(*obj.Prog) |
| 553 | Proginfo func(*obj.Prog) // fills in Prog.Info |
| 554 | Regtyp func(*obj.Addr) bool |
| 555 | Sameaddr func(*obj.Addr, *obj.Addr) bool |
| 556 | Smallindir func(*obj.Addr, *obj.Addr) bool |
| 557 | Stackaddr func(*obj.Addr) bool |
Shenghou Ma | e7dd288 | 2015-04-08 13:34:42 -0400 | [diff] [blame] | 558 | Blockcopy func(*Node, *Node, int64, int64, int64) |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 559 | Sudoaddable func(obj.As, *Node, *obj.Addr) bool |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 560 | Sudoclean func() |
| 561 | Excludedregs func() uint64 |
| 562 | RtoB func(int) uint64 |
| 563 | FtoB func(int) uint64 |
| 564 | BtoR func(uint64) int |
| 565 | BtoF func(uint64) int |
Matthew Dempsky | 0d9258a | 2016-03-07 18:00:08 -0800 | [diff] [blame] | 566 | Optoas func(Op, *Type) obj.As |
Russ Cox | b115c35 | 2015-03-18 17:26:36 -0400 | [diff] [blame] | 567 | Doregbits func(int) uint64 |
| 568 | Regnames func(*int) []string |
Dave Cheney | 01d005c | 2015-03-25 09:17:09 +1100 | [diff] [blame] | 569 | Use387 bool // should 8g use 387 FP instructions instead of sse2. |
Michael Pratt | a4e31d4 | 2016-03-12 14:07:40 -0800 | [diff] [blame] | 570 | |
| 571 | // SSARegToReg maps ssa register numbers to obj register numbers. |
| 572 | SSARegToReg []int16 |
| 573 | |
| 574 | // SSAMarkMoves marks any MOVXconst ops that need to avoid clobbering flags. |
| 575 | SSAMarkMoves func(*SSAGenState, *ssa.Block) |
| 576 | |
| 577 | // SSAGenValue emits Prog(s) for the Value. |
| 578 | SSAGenValue func(*SSAGenState, *ssa.Value) |
| 579 | |
| 580 | // SSAGenBlock emits end-of-block Progs. SSAGenValue should be called |
| 581 | // for all values in the block before SSAGenBlock. |
| 582 | SSAGenBlock func(s *SSAGenState, b, next *ssa.Block) |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | var pcloc int32 |
| 586 | |
| 587 | var Thearch Arch |
| 588 | |
| 589 | var Newproc *Node |
| 590 | |
| 591 | var Deferproc *Node |
| 592 | |
| 593 | var Deferreturn *Node |
| 594 | |
| 595 | var Panicindex *Node |
| 596 | |
| 597 | var panicslice *Node |
| 598 | |
David Chase | 18559e2 | 2015-10-28 13:55:46 -0400 | [diff] [blame] | 599 | var panicdivide *Node |
| 600 | |
Russ Cox | 8c195bd | 2015-02-13 14:40:36 -0500 | [diff] [blame] | 601 | var throwreturn *Node |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 602 | |
| 603 | var growslice *Node |
| 604 | |
Keith Randall | 5ba3194 | 2016-01-25 17:06:54 -0800 | [diff] [blame] | 605 | var writebarrierptr *Node |
| 606 | var typedmemmove *Node |
Keith Randall | 8c5bfcc | 2015-09-18 15:11:30 -0700 | [diff] [blame] | 607 | |
| 608 | var panicdottype *Node |