Example #1
0
func buildPkgSym(b *builder, m *tast.MemberExpr, pkg *types.Pkg) *ref {
	sym := m.Symbol
	if pkg.Lang == "asm8" {
		switch sym.Type {
		case asm8.SymVar:
			if m.Type() != types.Uint {
				panic("bug")
			}
			ptr := b.newTemp(types.Uint)
			s := ir.NewHeapSym(sym.Pkg(), sym.Name(), 0, false, false)
			b.b.Arith(ptr.IR(), nil, "&", s)
			return ptr
		case asm8.SymFunc:
			if !types.SameType(m.Type(), types.VoidFunc) {
				panic("bug")
			}
			return newRef(
				types.VoidFunc,
				ir.NewFuncSym(sym.Pkg(), sym.Name(), ir.VoidFuncSig),
			)
		}
		panic("bug")
	}

	switch sym.Type {
	case tast.SymConst:
		return sym.Obj.(*objConst).ref
	case tast.SymVar:
		return sym.Obj.(*objVar).ref
	case tast.SymFunc:
		return sym.Obj.(*objFunc).ref
	}
	panic("bug")
}
Example #2
0
func fillFuncAlias(funcs []*tast.FuncAlias) {
	for _, f := range funcs {
		sym := f.Sym
		name := sym.Name()
		t := sym.ObjType.(*types.Func)
		sig := makeFuncSig(t)
		fsym := ir.NewFuncSym(f.Of.Pkg(), f.Of.Name(), sig)
		f.Sym.Obj = &objFunc{
			name:    name,
			ref:     newRef(t, fsym),
			isAlias: true,
		}
	}
}
Example #3
0
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)
			return nil
		}

		ref := ir.NewFuncSym(path, name, makeFuncSig(t))
		obj := &objFunc{name: as, ref: newRef(t, ref)}
		s := sym8.Make(b.path, as, tast.SymFunc, obj, t, nil)
		pre := b.scope.Declare(s)
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
			return nil
		}
		return ref
	}

	// TODO: export these as generic function pointer symbols, and convert
	// them into G functions in g files, rather than inside here in the
	// compiler.
	o("PrintInt32", "printInt", types.NewVoidFunc(types.Int))
	o("PrintUint32", "printUint", types.NewVoidFunc(types.Uint))
	o("PrintChar", "printChar", types.NewVoidFunc(types.Int8))
	b.panicFunc = o("Panic", "panic", types.VoidFunc)

	bi := func(name string) {
		t := types.NewBuiltInFunc(name)
		obj := &objFunc{name: name, ref: newRef(t, nil)}
		s := sym8.Make(b.path, name, tast.SymFunc, obj, t, nil)
		pre := b.scope.Declare(s)
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
		}
	}

	bi("len")
	bi("make")

	c := func(name string, r *ref) {
		// TODO: declare these as typed consts
		obj := &objConst{name, r}
		s := sym8.Make(b.path, name, tast.SymConst, obj, r.Type(), nil)
		pre := b.scope.Declare(s)
		if pre != nil {
			b.Errorf(nil, "builtin symbol %s declare failed", name)
		}
	}

	c("true", refTrue)
	c("false", refFalse)
	c("nil", refNil)

	t := func(name string, t types.T) {
		s := sym8.Make(b.path, name, tast.SymType, nil, &types.Type{t}, nil)
		pre := b.scope.Declare(s)
		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("string", types.String)
	// t("ptr", &types.Pointer{types.Uint8})
	// t("float32", types.Float32)
	// t("string", types.String)
}