github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/src/cmd/compile/internal/arm64/ggen.go (about) 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 arm64 6 7 import ( 8 "cmd/compile/internal/gc" 9 "cmd/internal/obj" 10 "cmd/internal/obj/arm64" 11 "fmt" 12 ) 13 14 func defframe(ptxt *obj.Prog) { 15 var n *gc.Node 16 17 // fill in argument size, stack size 18 ptxt.To.Type = obj.TYPE_TEXTSIZE 19 20 ptxt.To.Val = int32(gc.Rnd(gc.Curfn.Type.Argwid, int64(gc.Widthptr))) 21 frame := uint32(gc.Rnd(gc.Stksize+gc.Maxarg, int64(gc.Widthreg))) 22 23 // arm64 requires that the frame size (not counting saved LR) 24 // be empty or be 8 mod 16. If not, pad it. 25 if frame != 0 && frame%16 != 8 { 26 frame += 8 27 } 28 29 ptxt.To.Offset = int64(frame) 30 31 // insert code to zero ambiguously live variables 32 // so that the garbage collector only sees initialized values 33 // when it looks for pointers. 34 p := ptxt 35 36 hi := int64(0) 37 lo := hi 38 39 // iterate through declarations - they are sorted in decreasing xoffset order. 40 for l := gc.Curfn.Func.Dcl; l != nil; l = l.Next { 41 n = l.N 42 if !n.Name.Needzero { 43 continue 44 } 45 if n.Class != gc.PAUTO { 46 gc.Fatalf("needzero class %d", n.Class) 47 } 48 if n.Type.Width%int64(gc.Widthptr) != 0 || n.Xoffset%int64(gc.Widthptr) != 0 || n.Type.Width == 0 { 49 gc.Fatalf("var %v has size %d offset %d", gc.Nconv(n, obj.FmtLong), int(n.Type.Width), int(n.Xoffset)) 50 } 51 52 if lo != hi && n.Xoffset+n.Type.Width >= lo-int64(2*gc.Widthreg) { 53 // merge with range we already have 54 lo = n.Xoffset 55 56 continue 57 } 58 59 // zero old range 60 p = zerorange(p, int64(frame), lo, hi) 61 62 // set new range 63 hi = n.Xoffset + n.Type.Width 64 65 lo = n.Xoffset 66 } 67 68 // zero final range 69 zerorange(p, int64(frame), lo, hi) 70 } 71 72 var darwin = obj.Getgoos() == "darwin" 73 74 func zerorange(p *obj.Prog, frame int64, lo int64, hi int64) *obj.Prog { 75 cnt := hi - lo 76 if cnt == 0 { 77 return p 78 } 79 if cnt < int64(4*gc.Widthptr) { 80 for i := int64(0); i < cnt; i += int64(gc.Widthptr) { 81 p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGZERO, 0, obj.TYPE_MEM, arm64.REGSP, 8+frame+lo+i) 82 } 83 } else if cnt <= int64(128*gc.Widthptr) && !darwin { // darwin ld64 cannot handle BR26 reloc with non-zero addend 84 p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGSP, 0, obj.TYPE_REG, arm64.REGRT1, 0) 85 p = appendpp(p, arm64.AADD, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, arm64.REGRT1, 0) 86 p.Reg = arm64.REGRT1 87 p = appendpp(p, obj.ADUFFZERO, obj.TYPE_NONE, 0, 0, obj.TYPE_MEM, 0, 0) 88 f := gc.Sysfunc("duffzero") 89 gc.Naddr(&p.To, f) 90 gc.Afunclit(&p.To, f) 91 p.To.Offset = 4 * (128 - cnt/int64(gc.Widthptr)) 92 } else { 93 p = appendpp(p, arm64.AMOVD, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, arm64.REGTMP, 0) 94 p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGSP, 0, obj.TYPE_REG, arm64.REGRT1, 0) 95 p = appendpp(p, arm64.AADD, obj.TYPE_REG, arm64.REGTMP, 0, obj.TYPE_REG, arm64.REGRT1, 0) 96 p.Reg = arm64.REGRT1 97 p = appendpp(p, arm64.AMOVD, obj.TYPE_CONST, 0, cnt, obj.TYPE_REG, arm64.REGTMP, 0) 98 p = appendpp(p, arm64.AADD, obj.TYPE_REG, arm64.REGTMP, 0, obj.TYPE_REG, arm64.REGRT2, 0) 99 p.Reg = arm64.REGRT1 100 p = appendpp(p, arm64.AMOVD, obj.TYPE_REG, arm64.REGZERO, 0, obj.TYPE_MEM, arm64.REGRT1, int64(gc.Widthptr)) 101 p.Scond = arm64.C_XPRE 102 p1 := p 103 p = appendpp(p, arm64.ACMP, obj.TYPE_REG, arm64.REGRT1, 0, obj.TYPE_NONE, 0, 0) 104 p.Reg = arm64.REGRT2 105 p = appendpp(p, arm64.ABNE, obj.TYPE_NONE, 0, 0, obj.TYPE_BRANCH, 0, 0) 106 gc.Patch(p, p1) 107 } 108 109 return p 110 } 111 112 func appendpp(p *obj.Prog, as int, ftype int, freg int, foffset int64, ttype int, treg int, toffset int64) *obj.Prog { 113 q := gc.Ctxt.NewProg() 114 gc.Clearp(q) 115 q.As = int16(as) 116 q.Lineno = p.Lineno 117 q.From.Type = int16(ftype) 118 q.From.Reg = int16(freg) 119 q.From.Offset = foffset 120 q.To.Type = int16(ttype) 121 q.To.Reg = int16(treg) 122 q.To.Offset = toffset 123 q.Link = p.Link 124 p.Link = q 125 return q 126 } 127 128 func ginsnop() { 129 var con gc.Node 130 gc.Nodconst(&con, gc.Types[gc.TINT], 0) 131 gins(arm64.AHINT, &con, nil) 132 } 133 134 var panicdiv *gc.Node 135 136 /* 137 * generate division. 138 * generates one of: 139 * res = nl / nr 140 * res = nl % nr 141 * according to op. 142 */ 143 func dodiv(op gc.Op, nl *gc.Node, nr *gc.Node, res *gc.Node) { 144 // Have to be careful about handling 145 // most negative int divided by -1 correctly. 146 // The hardware will generate undefined result. 147 // Also need to explicitly trap on division on zero, 148 // the hardware will silently generate undefined result. 149 // DIVW will leave unpredicable result in higher 32-bit, 150 // so always use DIVD/DIVDU. 151 t := nl.Type 152 153 t0 := t 154 check := false 155 if gc.Issigned[t.Etype] { 156 check = true 157 if gc.Isconst(nl, gc.CTINT) && nl.Int() != -(1<<uint64(t.Width*8-1)) { 158 check = false 159 } else if gc.Isconst(nr, gc.CTINT) && nr.Int() != -1 { 160 check = false 161 } 162 } 163 164 if t.Width < 8 { 165 if gc.Issigned[t.Etype] { 166 t = gc.Types[gc.TINT64] 167 } else { 168 t = gc.Types[gc.TUINT64] 169 } 170 check = false 171 } 172 173 a := optoas(gc.ODIV, t) 174 175 var tl gc.Node 176 gc.Regalloc(&tl, t0, nil) 177 var tr gc.Node 178 gc.Regalloc(&tr, t0, nil) 179 if nl.Ullman >= nr.Ullman { 180 gc.Cgen(nl, &tl) 181 gc.Cgen(nr, &tr) 182 } else { 183 gc.Cgen(nr, &tr) 184 gc.Cgen(nl, &tl) 185 } 186 187 if t != t0 { 188 // Convert 189 tl2 := tl 190 191 tr2 := tr 192 tl.Type = t 193 tr.Type = t 194 gmove(&tl2, &tl) 195 gmove(&tr2, &tr) 196 } 197 198 // Handle divide-by-zero panic. 199 p1 := gins(optoas(gc.OCMP, t), &tr, nil) 200 p1.Reg = arm64.REGZERO 201 p1 = gc.Gbranch(optoas(gc.ONE, t), nil, +1) 202 if panicdiv == nil { 203 panicdiv = gc.Sysfunc("panicdivide") 204 } 205 gc.Ginscall(panicdiv, -1) 206 gc.Patch(p1, gc.Pc) 207 208 var p2 *obj.Prog 209 if check { 210 var nm1 gc.Node 211 gc.Nodconst(&nm1, t, -1) 212 gcmp(optoas(gc.OCMP, t), &tr, &nm1) 213 p1 := gc.Gbranch(optoas(gc.ONE, t), nil, +1) 214 if op == gc.ODIV { 215 // a / (-1) is -a. 216 gins(optoas(gc.OMINUS, t), &tl, &tl) 217 218 gmove(&tl, res) 219 } else { 220 // a % (-1) is 0. 221 var nz gc.Node 222 gc.Nodconst(&nz, t, 0) 223 224 gmove(&nz, res) 225 } 226 227 p2 = gc.Gbranch(obj.AJMP, nil, 0) 228 gc.Patch(p1, gc.Pc) 229 } 230 231 p1 = gins(a, &tr, &tl) 232 if op == gc.ODIV { 233 gc.Regfree(&tr) 234 gmove(&tl, res) 235 } else { 236 // A%B = A-(A/B*B) 237 var tm gc.Node 238 gc.Regalloc(&tm, t, nil) 239 240 // patch div to use the 3 register form 241 // TODO(minux): add gins3? 242 p1.Reg = p1.To.Reg 243 244 p1.To.Reg = tm.Reg 245 gins(optoas(gc.OMUL, t), &tr, &tm) 246 gc.Regfree(&tr) 247 gins(optoas(gc.OSUB, t), &tm, &tl) 248 gc.Regfree(&tm) 249 gmove(&tl, res) 250 } 251 252 gc.Regfree(&tl) 253 if check { 254 gc.Patch(p2, gc.Pc) 255 } 256 } 257 258 /* 259 * generate high multiply: 260 * res = (nl*nr) >> width 261 */ 262 func cgen_hmul(nl *gc.Node, nr *gc.Node, res *gc.Node) { 263 // largest ullman on left. 264 if nl.Ullman < nr.Ullman { 265 nl, nr = nr, nl 266 } 267 268 t := (*gc.Type)(nl.Type) 269 w := int(int(t.Width * 8)) 270 var n1 gc.Node 271 gc.Cgenr(nl, &n1, res) 272 var n2 gc.Node 273 gc.Cgenr(nr, &n2, nil) 274 switch gc.Simtype[t.Etype] { 275 case gc.TINT8, 276 gc.TINT16, 277 gc.TINT32: 278 gins(optoas(gc.OMUL, t), &n2, &n1) 279 p := (*obj.Prog)(gins(arm64.AASR, nil, &n1)) 280 p.From.Type = obj.TYPE_CONST 281 p.From.Offset = int64(w) 282 283 case gc.TUINT8, 284 gc.TUINT16, 285 gc.TUINT32: 286 gins(optoas(gc.OMUL, t), &n2, &n1) 287 p := (*obj.Prog)(gins(arm64.ALSR, nil, &n1)) 288 p.From.Type = obj.TYPE_CONST 289 p.From.Offset = int64(w) 290 291 case gc.TINT64, 292 gc.TUINT64: 293 if gc.Issigned[t.Etype] { 294 gins(arm64.ASMULH, &n2, &n1) 295 } else { 296 gins(arm64.AUMULH, &n2, &n1) 297 } 298 299 default: 300 gc.Fatalf("cgen_hmul %v", t) 301 } 302 303 gc.Cgen(&n1, res) 304 gc.Regfree(&n1) 305 gc.Regfree(&n2) 306 } 307 308 /* 309 * generate shift according to op, one of: 310 * res = nl << nr 311 * res = nl >> nr 312 */ 313 func cgen_shift(op gc.Op, bounded bool, nl *gc.Node, nr *gc.Node, res *gc.Node) { 314 a := int(optoas(op, nl.Type)) 315 316 if nr.Op == gc.OLITERAL { 317 var n1 gc.Node 318 gc.Regalloc(&n1, nl.Type, res) 319 gc.Cgen(nl, &n1) 320 sc := uint64(nr.Int()) 321 if sc >= uint64(nl.Type.Width*8) { 322 // large shift gets 2 shifts by width-1 323 var n3 gc.Node 324 gc.Nodconst(&n3, gc.Types[gc.TUINT32], nl.Type.Width*8-1) 325 326 gins(a, &n3, &n1) 327 gins(a, &n3, &n1) 328 } else { 329 gins(a, nr, &n1) 330 } 331 gmove(&n1, res) 332 gc.Regfree(&n1) 333 return 334 } 335 336 if nl.Ullman >= gc.UINF { 337 var n4 gc.Node 338 gc.Tempname(&n4, nl.Type) 339 gc.Cgen(nl, &n4) 340 nl = &n4 341 } 342 343 if nr.Ullman >= gc.UINF { 344 var n5 gc.Node 345 gc.Tempname(&n5, nr.Type) 346 gc.Cgen(nr, &n5) 347 nr = &n5 348 } 349 350 // Allow either uint32 or uint64 as shift type, 351 // to avoid unnecessary conversion from uint32 to uint64 352 // just to do the comparison. 353 tcount := gc.Types[gc.Simtype[nr.Type.Etype]] 354 355 if tcount.Etype < gc.TUINT32 { 356 tcount = gc.Types[gc.TUINT32] 357 } 358 359 var n1 gc.Node 360 gc.Regalloc(&n1, nr.Type, nil) // to hold the shift type in CX 361 var n3 gc.Node 362 gc.Regalloc(&n3, tcount, &n1) // to clear high bits of CX 363 364 var n2 gc.Node 365 gc.Regalloc(&n2, nl.Type, res) 366 367 if nl.Ullman >= nr.Ullman { 368 gc.Cgen(nl, &n2) 369 gc.Cgen(nr, &n1) 370 gmove(&n1, &n3) 371 } else { 372 gc.Cgen(nr, &n1) 373 gmove(&n1, &n3) 374 gc.Cgen(nl, &n2) 375 } 376 377 gc.Regfree(&n3) 378 379 // test and fix up large shifts 380 if !bounded { 381 gc.Nodconst(&n3, tcount, nl.Type.Width*8) 382 gcmp(optoas(gc.OCMP, tcount), &n1, &n3) 383 p1 := (*obj.Prog)(gc.Gbranch(optoas(gc.OLT, tcount), nil, +1)) 384 if op == gc.ORSH && gc.Issigned[nl.Type.Etype] { 385 gc.Nodconst(&n3, gc.Types[gc.TUINT32], nl.Type.Width*8-1) 386 gins(a, &n3, &n2) 387 } else { 388 gc.Nodconst(&n3, nl.Type, 0) 389 gmove(&n3, &n2) 390 } 391 392 gc.Patch(p1, gc.Pc) 393 } 394 395 gins(a, &n1, &n2) 396 397 gmove(&n2, res) 398 399 gc.Regfree(&n1) 400 gc.Regfree(&n2) 401 } 402 403 func clearfat(nl *gc.Node) { 404 /* clear a fat object */ 405 if gc.Debug['g'] != 0 { 406 fmt.Printf("clearfat %v (%v, size: %d)\n", nl, nl.Type, nl.Type.Width) 407 } 408 409 w := uint64(uint64(nl.Type.Width)) 410 411 // Avoid taking the address for simple enough types. 412 if gc.Componentgen(nil, nl) { 413 return 414 } 415 416 c := uint64(w % 8) // bytes 417 q := uint64(w / 8) // dwords 418 419 var r0 gc.Node 420 gc.Nodreg(&r0, gc.Types[gc.TUINT64], arm64.REGZERO) 421 var dst gc.Node 422 423 // REGRT1 is reserved on arm64, see arm64/gsubr.go. 424 gc.Nodreg(&dst, gc.Types[gc.Tptr], arm64.REGRT1) 425 gc.Agen(nl, &dst) 426 427 var boff uint64 428 if q > 128 { 429 p := gins(arm64.ASUB, nil, &dst) 430 p.From.Type = obj.TYPE_CONST 431 p.From.Offset = 8 432 433 var end gc.Node 434 gc.Regalloc(&end, gc.Types[gc.Tptr], nil) 435 p = gins(arm64.AMOVD, &dst, &end) 436 p.From.Type = obj.TYPE_ADDR 437 p.From.Offset = int64(q * 8) 438 439 p = gins(arm64.AMOVD, &r0, &dst) 440 p.To.Type = obj.TYPE_MEM 441 p.To.Offset = 8 442 p.Scond = arm64.C_XPRE 443 pl := (*obj.Prog)(p) 444 445 p = gcmp(arm64.ACMP, &dst, &end) 446 gc.Patch(gc.Gbranch(arm64.ABNE, nil, 0), pl) 447 448 gc.Regfree(&end) 449 450 // The loop leaves R16 on the last zeroed dword 451 boff = 8 452 } else if q >= 4 && !darwin { // darwin ld64 cannot handle BR26 reloc with non-zero addend 453 p := gins(arm64.ASUB, nil, &dst) 454 p.From.Type = obj.TYPE_CONST 455 p.From.Offset = 8 456 f := (*gc.Node)(gc.Sysfunc("duffzero")) 457 p = gins(obj.ADUFFZERO, nil, f) 458 gc.Afunclit(&p.To, f) 459 460 // 4 and 128 = magic constants: see ../../runtime/asm_arm64x.s 461 p.To.Offset = int64(4 * (128 - q)) 462 463 // duffzero leaves R16 on the last zeroed dword 464 boff = 8 465 } else { 466 var p *obj.Prog 467 for t := uint64(0); t < q; t++ { 468 p = gins(arm64.AMOVD, &r0, &dst) 469 p.To.Type = obj.TYPE_MEM 470 p.To.Offset = int64(8 * t) 471 } 472 473 boff = 8 * q 474 } 475 476 var p *obj.Prog 477 for t := uint64(0); t < c; t++ { 478 p = gins(arm64.AMOVB, &r0, &dst) 479 p.To.Type = obj.TYPE_MEM 480 p.To.Offset = int64(t + boff) 481 } 482 } 483 484 // Called after regopt and peep have run. 485 // Expand CHECKNIL pseudo-op into actual nil pointer check. 486 func expandchecks(firstp *obj.Prog) { 487 var p1 *obj.Prog 488 489 for p := (*obj.Prog)(firstp); p != nil; p = p.Link { 490 if gc.Debug_checknil != 0 && gc.Ctxt.Debugvlog != 0 { 491 fmt.Printf("expandchecks: %v\n", p) 492 } 493 if p.As != obj.ACHECKNIL { 494 continue 495 } 496 if gc.Debug_checknil != 0 && p.Lineno > 1 { // p->lineno==1 in generated wrappers 497 gc.Warnl(int(p.Lineno), "generated nil check") 498 } 499 if p.From.Type != obj.TYPE_REG { 500 gc.Fatalf("invalid nil check %v\n", p) 501 } 502 503 // check is 504 // CBNZ arg, 2(PC) 505 // MOVD ZR, 0(arg) 506 p1 = gc.Ctxt.NewProg() 507 gc.Clearp(p1) 508 p1.Link = p.Link 509 p.Link = p1 510 p1.Lineno = p.Lineno 511 p1.Pc = 9999 512 513 p.As = arm64.ACBNZ 514 p.To.Type = obj.TYPE_BRANCH 515 p.To.Val = p1.Link 516 517 // crash by write to memory address 0. 518 p1.As = arm64.AMOVD 519 p1.From.Type = obj.TYPE_REG 520 p1.From.Reg = arm64.REGZERO 521 p1.To.Type = obj.TYPE_MEM 522 p1.To.Reg = p.From.Reg 523 p1.To.Offset = 0 524 } 525 } 526 527 // res = runtime.getg() 528 func getg(res *gc.Node) { 529 var n1 gc.Node 530 gc.Nodreg(&n1, res.Type, arm64.REGG) 531 gmove(&n1, res) 532 }