Beispiel #1
0
func declareBuiltin(b *builder, builtin *link8.Pkg) {
	pindex := b.p.Require(builtin)

	o := func(name string, as string, t *types.Func) {
		sym, index := builtin.SymbolByName(name)
		if sym == nil {
			b.Errorf(nil, "builtin symbol %s missing", name)
			return
		}

		ref := ir.FuncSym(pindex, index, nil) // a reference to the function
		obj := &objFunc{as, newRef(t, ref), nil}
		pre := b.scope.Declare(sym8.Make(as, symFunc, obj, nil))
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
		}
	}

	o("PrintInt32", "printInt", types.NewVoidFunc(types.Int))
	o("PrintUint32", "printUint", types.NewVoidFunc(types.Uint))
	o("PrintChar", "printChar", types.NewVoidFunc(types.Uint8))

	c := func(name string, t types.T, r ir.Ref) {
		obj := &objConst{name, newRef(t, r)}
		pre := b.scope.Declare(sym8.Make(name, symConst, obj, nil))
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
		}
	}

	c("true", types.Bool, ir.Snum(1))
	c("false", types.Bool, ir.Snum(0))

	t := func(name string, t types.T) {
		obj := &objType{name, newRef(&types.Type{t}, nil)}
		pre := b.scope.Declare(sym8.Make(name, symType, obj, nil))
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
		}
	}

	t("int", types.Int)
	t("uint", types.Uint)
	t("int32", types.Int)
	t("uint32", types.Uint)
	t("int8", types.Int8)
	t("uint8", types.Uint8)
	t("char", types.Int8)
	t("byte", types.Uint8)
	t("bool", types.Bool)
	t("float", types.Float32)
	// t("float32", types.Float32)
	// t("string", types.String)
}
Beispiel #2
0
func buildInt(b *builder, op *lex8.Token) *ref {
	ret, e := strconv.ParseInt(op.Lit, 0, 32)
	if e != nil {
		b.Errorf(op.Pos, "invalid integer: %s", e)
		return nil
	}

	if ret < math.MinInt32 {
		b.Errorf(op.Pos, "integer too small to fit in 32-bit")
		return nil
	} else if ret > math.MaxUint32 {
		b.Errorf(op.Pos, "integer too large to fit in 32-bit")
		return nil
	} else if ret > math.MaxInt32 {
		// must be unsigned integer
		return newRef(types.Uint, ir.Num(uint32(ret)))
	}

	return newRef(types.Int, ir.Snum(int32(ret)))
}