Esempio n. 1
0
func buildConst(b *builder, c *tast.Const) *ref {
	if _, ok := types.NumConst(c.T); ok {
		// untyped consts are just types.
		return newRef(c.T, nil)
	}

	if t, ok := c.T.(types.Basic); ok {
		v := c.ConstValue.(int64)
		switch t {
		case types.Int, types.Uint:
			return newRef(c.T, ir.Num(uint32(v)))
		case types.Int8, types.Uint8, types.Bool:
			return newRef(c.T, ir.Byt(uint8(v)))
		default:
			panic("other basic types not supported yet")
		}
	}

	if c.T == types.String {
		s := c.ConstValue.(string)
		ret := b.newTemp(c.T)
		b.b.Arith(ret.IR(), nil, "makeStr", b.p.NewString(s))
		return ret
	}

	panic("other const types not supported")
}
Esempio n. 2
0
func constNumIr(v int64, t types.T) ir.Ref {
	b, ok := t.(types.Basic)
	if ok {
		switch b {
		case types.Int:
			return ir.Snum(int32(v))
		case types.Uint:
			return ir.Num(uint32(v))
		case types.Int8, types.Uint8:
			return ir.Byt(uint8(v))
		}
	}
	panic("expect an integer type")
}
Esempio n. 3
0
package g8

import (
	"e8vm.io/e8vm/g8/ir"
	"e8vm.io/e8vm/g8/tast"
	"e8vm.io/e8vm/g8/types"
	"e8vm.io/e8vm/link8"
	"e8vm.io/e8vm/sym8"
)

var (
	refTrue  = newRef(types.Bool, ir.Byt(1))
	refFalse = newRef(types.Bool, ir.Byt(0))
	refNil   = newRef(types.Nil(), nil)
)

func declareBuiltin(b *builder, builtin *link8.Pkg) {
	path := builtin.Path()
	e := b.p.HookBuiltin(builtin)
	if e != nil {
		b.Errorf(nil, e.Error())
		return
	}

	o := func(name, as string, t *types.Func) ir.Ref {
		sym := builtin.SymbolByName(name)
		if sym == nil {
			b.Errorf(nil, "builtin symbol %s missing", name)
			return nil
		} else if sym.Type != link8.SymFunc {
			b.Errorf(nil, "builtin symbol %s is not a function", name)