Exemple #1
0
// logical generates code for || and && operators.
func (c *compiler) logical(op scan.Type, e *ast.BinaryExpr, lv *arch.LV) *node {
	l := []*ast.BinaryExpr{e}
	for x := e.X; ; {
		y, ok := x.(*ast.BinaryExpr)
		if !ok || y.Op.Type != op {
			break
		}
		l = append(l, y)
		x = y.X
	}

	var lx arch.LV
	n := c.exprInternal(l[len(l)-1].X, lv)
	for i := len(l) - 1; i >= 0; i-- {
		var lv2 arch.LV
		if lx.Addr == 0 {
			lx.Addr = c.cg.Label()
		}

		n = c.rvalue(n, lv)
		n2 := c.exprInternal(l[i].Y, &lv2)
		n2 = c.rvalue(n2, &lv2)
		if op == scan.Lor {
			n = newNode(opBrTrue, &lx, nil, n, n2)
		} else {
			n = newNode(opBrFalse, &lx, nil, n, n2)
		}
	}
	n = newNode(opLab, &lx, nil, n, nil)
	n = newNode(opBool, nil, nil, n, nil)
	lv.Type = types.Typ[types.Int]
	lv.Addressable = false

	return n
}
Exemple #2
0
// 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)
	}
}
Exemple #3
0
// condExpr generates code for ? and : operators.
func (c *compiler) condExpr(e *ast.CondExpr, lv *arch.LV) *node {
	l := []*ast.CondExpr{e}
	for x := e.Cond; ; {
		y, ok := x.(*ast.CondExpr)
		if !ok {
			break
		}
		l = append(l, y)
		x = y.Cond
	}

	var lx arch.LV
	var l2 int
	var typ types.Type
	n := c.exprInternal(l[len(l)-1].Cond, lv)
	for i := len(l) - 1; i >= 0; i-- {
		var lv2 arch.LV

		n = c.rvalue(n, lv)
		l1 := c.cg.Label()
		if l2 == 0 {
			l2 = c.cg.Label()
		}

		n2 := c.exprInternal(l[i].X, &lv2)
		n2 = c.rvalue(n2, &lv2)
		lx.Addr = l1
		n = newNode(opBrFalse, &lx, nil, n, n2)
		if typ == nil {
			typ = lv2.Type
		}

		n2 = c.exprInternal(l[i].Y, &lv2)
		n2 = c.rvalue(n2, &lv2)
		n = newNode(opGlue, nil, nil, n, n2)
	}

	lx.Addr = l2
	n = newNode(opIfElse, &lx, nil, n, nil)
	lv.Type = typ
	lv.Addressable = false
	return n
}
Exemple #4
0
// defineLocal defines a variable at a function scope.
func (c *compiler) defineLocal(v *types.Var, lv *arch.LV) {
	val := c.cg.Label()
	lv.Addr = val
	c.cg.Data()

	intSize := c.cg.Int()
	ptrSize := c.cg.Pointer()
	size := 1

	typ := v.Type().Underlying()
	prim := primType(typ)
	array, isArray := typ.(*types.Array)
	isRecord := isRecord(typ, true)

	if isArray {
		size = int(array.Len())
	}

	init := 0
	if x := v.Value(); x != nil {
		init, _ = strconv.Atoi(x.String())
	}

	if !isArray && !isRecord {
		c.cg.Lab(val)
	}

	switch {
	case isRecord:
		c.cg.BSS(c.cg.Labname(val), c.cg.Sizeof(typ), true)

	case prim == types.Typ[types.Char]:
		if isArray {
			c.cg.BSS(c.cg.Labname(val), size, true)
		} else {
			c.cg.Defb(init)
			c.cg.Align(1, intSize)
		}

	case prim == types.Typ[types.Int]:
		if isArray {
			c.cg.BSS(c.cg.Labname(val), size*intSize, true)
		} else {
			c.cg.Defw(init)
		}

	default:
		if isArray {
			c.cg.BSS(c.cg.Labname(val), size*ptrSize, true)
		} else {
			c.cg.Defp(init)
		}
	}
}
Exemple #5
0
// 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
}