コード例 #1
0
ファイル: expr.go プロジェクト: qeedquan/gosubc
// ident generates code for an identifier by loading it into the accumulator.
func (c *compiler) ident(e *ast.Ident, lv *arch.LV, tv types.TypeAndValue) *node {
	lv.Ident = true
	lv.Type = tv.Type.Underlying()
	lv.Name = e.Name

	_, isFunc := tv.Type.(*types.Signature)
	if isFunc {
		if x, ok := c.Uses[e]; ok {
			switch x := x.(type) {
			case *types.Func:
				lv.Storage = x.Storage()
				return newNode(opAddr, lv, nil, nil, nil)
			case *types.Fwrd:
				return newNode(opAddr, lv, nil, nil, nil)
			}
		}
	}

	v, found := c.variable(e, c.Uses)
	if !found {
		lv.Ident = false
		return nil
	}

	s, found := c.symbol(v)
	if !found {
		lv.Ident = false
		return nil
	}

	_, isRecord := lv.Type.(*types.Record)

	// arrays decay to pointers, so we need to get the original type
	// because the type and value by the typechecker is recorded as a pointer
	array, isArray := v.Type().(*types.Array)

	lv.Addr = s.Addr
	lv.Value = s.Value
	lv.Storage = v.Storage()
	switch {
	// constants
	case tv.Value != nil:
		lv.Value = tv.Value
		return newNode(opLit, lv, nil, nil, nil)

	case isArray:
		lv.Type = types.NewPointer(array.Elem(), nil)
		return newNode(opAddr, lv, nil, nil, nil)

	case isRecord:
		lv.Ident = false
		return newNode(opAddr, lv, nil, nil, nil)

	// variable that a integer or a pointer
	default:
		lv.Addressable = true
		return newNode(opIdent, lv, nil, nil, nil)
	}
}
コード例 #2
0
ファイル: opt.go プロジェクト: qeedquan/gosubc
// reduce transforms expressions into equivalent but faster expressions.
func (c *compiler) reduce(n *node) *node {
	var vl, vr int
	var lv arch.LV

	op := n.op
	cl := n.left.op == opLit
	cr := n.right.op == opLit
	if cl {
		vl, _ = strconv.Atoi(n.left.lv[0].Value.String())
	}
	if cr {
		vr, _ = strconv.Atoi(n.right.lv[0].Value.String())
	}

	switch {
	case (op == opPlus || op == opAdd) && cr && vr == 0: // x+0 -> x
		return n.left

	case (op == opPlus || op == opAdd) && cl && vl == 0: // 0+x -> x
		return n.right

	case op == opSub && cr && vr == 0: // x-0 -> x
		return n.left

	case op == opSub && cl && vl == 0: // 0-x -> -x
		return newNode(opNeg, nil, nil, n.right, nil)

	case op == opMul && ((cl && vl == 0) || (cr && vr == 0)): // 0*x -> 0 || x*0 -> 0
		lv.Value = constant.MakeInt64(0)
		return newNode(opLit, &lv, nil, nil, nil)

	case op == opMul || op == opDiv: // reduce x*(2^n) or x/(2^n) to x<<n or x>>n
		lim := c.cg.Int()*8 - 1
		for i, k := 0, 1; i < lim; i, k = i+1, k<<1 {
			lv.Value = constant.MakeInt64(int64(i))
			m := newNode(opLit, &lv, nil, nil, nil)
			if cr && k == vr {
				if op == opMul {
					return newNode(opLsh, nil, nil, n.left, m)
				} else {
					return newNode(opRsh, nil, nil, n.left, m)
				}
			} else if cl && k == vl && op == opMul {
				return newNode(opLsh, nil, nil, n.right, m)
			}
		}
	}

	return n
}
コード例 #3
0
ファイル: expr.go プロジェクト: qeedquan/gosubc
// sizeofExpr generates code for a sizeof(x) expression.
func (c *compiler) sizeofExpr(e *ast.SizeofExpr, lv *arch.LV, tv types.TypeAndValue) *node {
	lv.Value = tv.Value
	lv.Addressable = false
	n := newNode(opLit, lv, nil, nil, nil)
	return n

}
コード例 #4
0
ファイル: expr.go プロジェクト: qeedquan/gosubc
// selectorExpr generates code for record accesses (a.x, a->x, etc)
func (c *compiler) selectorExpr(e *ast.SelectorExpr, lv *arch.LV) *node {
	sel, found := c.Selections[e]
	if !found {
		c.errorf(e.Op.Pos, "no selection expression information found")
		return nil
	}

	n := c.exprInternal(e.X, lv)
	if sel.Indirect() {
		n = c.rvalue(n, lv)
		lv.Ident = false
	}

	lv.Addressable = true

	if !sel.IsUnion() && sel.Offset() != 0 {
		lv.Value = constant.MakeInt64(sel.Offset())
		m := newNode(opLit, lv, nil, nil, nil)
		n = newNode(opAdd, lv, nil, n, m)
	}

	if isArray(sel.Obj().Type()) {
		lv.Addressable = false
	}

	lv.Type = sel.Type().Underlying()

	return n
}
コード例 #5
0
ファイル: opt.go プロジェクト: qeedquan/gosubc
// fold1 folds constant unary expressions.
func (c *compiler) fold1(n *node) *node {
	var lv arch.LV

	switch n.op {
	case opScale:
		v, _ := strconv.Atoi(n.left.lv[0].Value.String())
		lv.Value = constant.MakeInt64(int64(v * c.cg.Int()))
	default:
		return n
	}
	return newNode(opLit, &lv, nil, nil, nil)
}
コード例 #6
0
ファイル: expr.go プロジェクト: qeedquan/gosubc
// exprInternal is the main function for generating code by figuring
// out what kind of expression it is.
func (c *compiler) exprInternal(e ast.Expr, lv *arch.LV) *node {
	lv.Ident = false

	pos := e.Span().Start
	tv, found := c.typAndValue(e)
	if !found {
		return nil
	}

	// for constants we can just get the value right away
	if tv.Value != nil {
		lv.Type = tv.Type
		lv.Value = tv.Value
		switch tv.Type {
		case types.Typ[types.UntypedString]:
			str, err := strconv.Unquote(tv.Value.String())
			if err != nil {
				c.errorf(pos, "invalid constant %v: %v", tv.Value, err)
			}
			c.cg.Data()
			lab := c.cg.Label()
			c.cg.Lab(lab)
			c.cg.Defs(str)
			c.cg.Defb(0)
			c.cg.Align(len(str)+1, c.cg.Int())
			lv.Addr = lab
			return newNode(opLdlab, lv, nil, nil, nil)

		default:
			return newNode(opLit, lv, nil, nil, nil)
		}
	}

	switch e := e.(type) {
	case *ast.BinaryExpr:
		return c.binaryExpr(e, lv)

	case *ast.UnaryExpr:
		return c.unaryExpr(e, lv)

	case *ast.SizeofExpr:
		return c.sizeofExpr(e, lv, tv)

	case *ast.StarExpr:
		return c.starExpr(e, lv)

	case *ast.CallExpr:
		return c.callExpr(e, lv)

	case *ast.Ident:
		return c.ident(e, lv, tv)

	case *ast.ParenExpr:
		return c.exprInternal(e.X, lv)

	case *ast.IndexExpr:
		return c.indexExpr(e, lv)

	case *ast.SelectorExpr:
		return c.selectorExpr(e, lv)

	case *ast.CondExpr:
		return c.condExpr(e, lv)

	case *ast.CastExpr:
		return c.castExpr(e, lv, tv)

	case *ast.BasicType:
		lv.Type = tv.Type

	default:
		c.errorf(pos, "invalid expression: %T", e)
	}

	return nil
}