mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 23:13:40 +00:00
Function pointers in global variables
This commit is contained in:
+10
@@ -755,6 +755,16 @@ func (c *Compiler) getInterpretedValue(value Value) (llvm.Value, error) {
|
|||||||
case *ConstValue:
|
case *ConstValue:
|
||||||
return c.parseConst(value.Expr)
|
return c.parseConst(value.Expr)
|
||||||
|
|
||||||
|
case *FunctionValue:
|
||||||
|
if value.Elem == nil {
|
||||||
|
llvmType, err := c.getLLVMType(value.Type)
|
||||||
|
if err != nil {
|
||||||
|
return llvm.Value{}, err
|
||||||
|
}
|
||||||
|
return getZeroValue(llvmType)
|
||||||
|
}
|
||||||
|
return c.ir.GetFunction(value.Elem).llvmFn, nil
|
||||||
|
|
||||||
case *GlobalValue:
|
case *GlobalValue:
|
||||||
zero := llvm.ConstInt(llvm.Int32Type(), 0, false)
|
zero := llvm.ConstInt(llvm.Int32Type(), 0, false)
|
||||||
ptr := llvm.ConstInBoundsGEP(value.Global.llvmGlobal, []llvm.Value{zero})
|
ptr := llvm.ConstInBoundsGEP(value.Global.llvmGlobal, []llvm.Value{zero})
|
||||||
|
|||||||
+9
-1
@@ -188,6 +188,8 @@ func (p *Program) getValue(value ssa.Value, locals map[ssa.Value]Value) (Value,
|
|||||||
switch value := value.(type) {
|
switch value := value.(type) {
|
||||||
case *ssa.Const:
|
case *ssa.Const:
|
||||||
return &ConstValue{value}, nil
|
return &ConstValue{value}, nil
|
||||||
|
case *ssa.Function:
|
||||||
|
return &FunctionValue{value.Type(), value}, nil
|
||||||
case *ssa.Global:
|
case *ssa.Global:
|
||||||
if strings.HasPrefix(value.Name(), "__cgofn__cgo_") || strings.HasPrefix(value.Name(), "_cgo_") {
|
if strings.HasPrefix(value.Name(), "__cgofn__cgo_") || strings.HasPrefix(value.Name(), "_cgo_") {
|
||||||
// Ignore CGo global variables which we don't use.
|
// Ignore CGo global variables which we don't use.
|
||||||
@@ -202,7 +204,6 @@ func (p *Program) getValue(value ssa.Value, locals map[ssa.Value]Value) (Value,
|
|||||||
g.initializer = value
|
g.initializer = value
|
||||||
}
|
}
|
||||||
return &GlobalValue{g}, nil
|
return &GlobalValue{g}, nil
|
||||||
//return &PointerValue{&g.initializer}, nil
|
|
||||||
default:
|
default:
|
||||||
if local, ok := locals[value]; ok {
|
if local, ok := locals[value]; ok {
|
||||||
return local, nil
|
return local, nil
|
||||||
@@ -226,6 +227,8 @@ func (p *Program) getZeroValue(t types.Type) (Value, error) {
|
|||||||
return &ArrayValue{typ.Elem(), elems}, nil
|
return &ArrayValue{typ.Elem(), elems}, nil
|
||||||
case *types.Basic:
|
case *types.Basic:
|
||||||
return &ZeroBasicValue{typ}, nil
|
return &ZeroBasicValue{typ}, nil
|
||||||
|
case *types.Signature:
|
||||||
|
return &FunctionValue{typ, nil}, nil
|
||||||
case *types.Interface:
|
case *types.Interface:
|
||||||
return &InterfaceValue{typ, nil}, nil
|
return &InterfaceValue{typ, nil}, nil
|
||||||
case *types.Map:
|
case *types.Map:
|
||||||
@@ -265,6 +268,11 @@ type PointerValue struct {
|
|||||||
Elem *Value
|
Elem *Value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type FunctionValue struct {
|
||||||
|
Type types.Type
|
||||||
|
Elem *ssa.Function
|
||||||
|
}
|
||||||
|
|
||||||
type InterfaceValue struct {
|
type InterfaceValue struct {
|
||||||
Type types.Type
|
Type types.Type
|
||||||
Elem Value
|
Elem Value
|
||||||
|
|||||||
Reference in New Issue
Block a user