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