github.com/rakyll/go@v0.0.0-20170216000551-64c02460d703/src/cmd/compile/internal/mips/ggen.go (about)

     1  // Copyright 2016 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 mips
     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  // TODO(mips): implement DUFFZERO
    62  func zerorange(p *obj.Prog, frame int64, lo int64, hi int64) *obj.Prog {
    63  
    64  	cnt := hi - lo
    65  	if cnt == 0 {
    66  		return p
    67  	}
    68  	if cnt < int64(4*gc.Widthptr) {
    69  		for i := int64(0); i < cnt; i += int64(gc.Widthptr) {
    70  			p = gc.Appendpp(p, mips.AMOVW, obj.TYPE_REG, mips.REGZERO, 0, obj.TYPE_MEM, mips.REGSP, gc.Ctxt.FixedFrameSize()+frame+lo+i)
    71  		}
    72  	} else {
    73  		//fmt.Printf("zerorange frame:%v, lo: %v, hi:%v \n", frame ,lo, hi)
    74  		//	ADD 	$(FIXED_FRAME+frame+lo-4), SP, r1
    75  		//	ADD 	$cnt, r1, r2
    76  		// loop:
    77  		//	MOVW	R0, (Widthptr)r1
    78  		//	ADD 	$Widthptr, r1
    79  		//	BNE		r1, r2, loop
    80  		p = gc.Appendpp(p, mips.AADD, obj.TYPE_CONST, 0, gc.Ctxt.FixedFrameSize()+frame+lo-4, obj.TYPE_REG, mips.REGRT1, 0)
    81  		p.Reg = mips.REGSP
    82  		p = gc.Appendpp(p, mips.AADD, obj.TYPE_CONST, 0, cnt, obj.TYPE_REG, mips.REGRT2, 0)
    83  		p.Reg = mips.REGRT1
    84  		p = gc.Appendpp(p, mips.AMOVW, obj.TYPE_REG, mips.REGZERO, 0, obj.TYPE_MEM, mips.REGRT1, int64(gc.Widthptr))
    85  		p1 := p
    86  		p = gc.Appendpp(p, mips.AADD, obj.TYPE_CONST, 0, int64(gc.Widthptr), obj.TYPE_REG, mips.REGRT1, 0)
    87  		p = gc.Appendpp(p, mips.ABNE, obj.TYPE_REG, mips.REGRT1, 0, obj.TYPE_BRANCH, 0, 0)
    88  		p.Reg = mips.REGRT2
    89  		gc.Patch(p, p1)
    90  	}
    91  
    92  	return p
    93  }
    94  
    95  func ginsnop() {
    96  	p := gc.Prog(mips.ANOR)
    97  	p.From.Type = obj.TYPE_REG
    98  	p.From.Reg = mips.REG_R0
    99  	p.To.Type = obj.TYPE_REG
   100  	p.To.Reg = mips.REG_R0
   101  }