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 mips64 6 7 import ( 8 "cmd/compile/internal/gc" 9 "cmd/internal/obj" 10 "cmd/internal/obj/mips" 11 ) 12 13 func defframe(ptxt *obj.Prog) { 14 // fill in argument size, stack size 15 ptxt.To.Type = obj.TYPE_TEXTSIZE 16 17 ptxt.To.Val = int32(gc.Rnd(gc.Curfn.Type.ArgWidth(), int64(gc.Widthptr))) 18 frame := uint32(gc.Rnd(gc.Stksize+gc.Maxarg, int64(gc.Widthreg))) 19 ptxt.To.Offset = int64(frame) 20 21 // insert code to zero ambiguously live variables 22 // so that the garbage collector only sees initialized values 23 // when it looks for pointers. 24 p := ptxt 25 26 hi := int64(0) 27 lo := hi 28 29 // iterate through declarations - they are sorted in decreasing xoffset order. 30 for _, n := range gc.Curfn.Func.Dcl { 31 if !n.Name.Needzero { 32 continue 33 } 34 if n.Class != gc.PAUTO { 35 gc.Fatalf("needzero class %d", n.Class) 36 } 37 if n.Type.Width%int64(gc.Widthptr) != 0 || n.Xoffset%int64(gc.Widthptr) != 0 || n.Type.Width == 0 { 38 gc.Fatalf("var %L has size %d offset %d", n, int(n.Type.Width), int(n.Xoffset)) 39 } 40 41 if lo != hi && n.Xoffset+n.Type.Width >= lo-int64(2*gc.Widthreg) { 42 // merge with range we already have 43 lo = n.Xoffset 44 45 continue 46 } 47 48 // zero old range 49 p = zerorange(p, int64(frame), lo, hi) 50 51 // set new range 52 hi = n.Xoffset + n.Type.Width 53 54 lo = n.Xoffset 55 } 56 57 // zero final range 58 zerorange(p, int64(frame), lo, hi) 59 } 60 61 func zerorange(p *obj.Prog, frame int64, lo int64, hi int64) *obj.Prog { 62 cnt := hi - lo 63 if cnt == 0 { 64 return p 65 } 66 if cnt < int64(4*gc.Widthptr) { 67 for i := int64(0); i < cnt; i += int64(gc.Widthptr) { 68 p = gc.Appendpp(p, mips.AMOVV, obj.TYPE_REG, mips.REGZERO, 0, obj.TYPE_MEM, mips.REGSP, 8+frame+lo+i) 69 } 70 } else if cnt <= int64(128*gc.Widthptr) { 71 p = gc.Appendpp(p, mips.AADDV, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, mips.REGRT1, 0) 72 p.Reg = mips.REGSP 73 p = gc.Appendpp(p, obj.ADUFFZERO, obj.TYPE_NONE, 0, 0, obj.TYPE_MEM, 0, 0) 74 gc.Naddr(&p.To, gc.Sysfunc("duffzero")) 75 p.To.Offset = 8 * (128 - cnt/int64(gc.Widthptr)) 76 } else { 77 // ADDV $(8+frame+lo-8), SP, r1 78 // ADDV $cnt, r1, r2 79 // loop: 80 // MOVV R0, (Widthptr)r1 81 // ADDV $Widthptr, r1 82 // BNE r1, r2, loop 83 p = gc.Appendpp(p, mips.AADDV, obj.TYPE_CONST, 0, 8+frame+lo-8, obj.TYPE_REG, mips.REGRT1, 0) 84 p.Reg = mips.REGSP 85 p = gc.Appendpp(p, mips.AADDV, obj.TYPE_CONST, 0, cnt, obj.TYPE_REG, mips.REGRT2, 0) 86 p.Reg = mips.REGRT1 87 p = gc.Appendpp(p, mips.AMOVV, obj.TYPE_REG, mips.REGZERO, 0, obj.TYPE_MEM, mips.REGRT1, int64(gc.Widthptr)) 88 p1 := p 89 p = gc.Appendpp(p, mips.AADDV, obj.TYPE_CONST, 0, int64(gc.Widthptr), obj.TYPE_REG, mips.REGRT1, 0) 90 p = gc.Appendpp(p, mips.ABNE, obj.TYPE_REG, mips.REGRT1, 0, obj.TYPE_BRANCH, 0, 0) 91 p.Reg = mips.REGRT2 92 gc.Patch(p, p1) 93 } 94 95 return p 96 } 97 98 func ginsnop() { 99 p := gc.Prog(mips.ANOR) 100 p.From.Type = obj.TYPE_REG 101 p.From.Reg = mips.REG_R0 102 p.To.Type = obj.TYPE_REG 103 p.To.Reg = mips.REG_R0 104 } 105