本文整理匯總了Golang中github.com/axw/gollvm/llvm.AddGlobal函數的典型用法代碼示例。如果您正苦於以下問題:Golang AddGlobal函數的具體用法?Golang AddGlobal怎麽用?Golang AddGlobal使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了AddGlobal函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: nameRuntimeType
func (tm *TypeMap) nameRuntimeType(n *types.Named) (global, ptr llvm.Value) {
name := typeString(n)
path := "runtime"
if pkg := n.Obj().Pkg(); pkg != nil {
path = pkg.Path()
}
if path != tm.pkgpath {
// We're not compiling the package from whence the type came,
// so we'll just create a pointer to it here.
global := llvm.AddGlobal(tm.module, tm.runtime.rtype.llvm, typeSymbol(name))
global.SetInitializer(llvm.ConstNull(tm.runtime.rtype.llvm))
global.SetLinkage(llvm.CommonLinkage)
return global, global
}
// If the underlying type is Basic, then we always create
// a new global. Otherwise, we clone the value returned
// from toRuntime in case it is cached and reused.
underlying := n.Underlying()
if basic, ok := underlying.(*types.Basic); ok {
global, ptr = tm.basicRuntimeType(basic, true)
global.SetName(typeSymbol(name))
} else {
global, ptr = tm.toRuntime(underlying)
clone := llvm.AddGlobal(tm.module, global.Type().ElementType(), typeSymbol(name))
clone.SetInitializer(global.Initializer())
global = clone
ptr = llvm.ConstBitCast(global, llvm.PointerType(tm.runtime.rtype.llvm, 0))
}
global.SetLinkage(llvm.ExternalLinkage)
// Locate the rtype.
underlyingRuntimeType := global.Initializer()
rtype := underlyingRuntimeType
if rtype.Type() != tm.runtime.rtype.llvm {
rtype = llvm.ConstExtractValue(rtype, []uint32{0})
}
// Insert the uncommon type.
uncommonTypeInit := tm.uncommonType(n, nil)
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
rtype = llvm.ConstInsertValue(rtype, uncommonType, []uint32{9})
// Replace the rtype's string representation with the one from
// uncommonType. XXX should we have the package name prepended? Probably.
namePtr := llvm.ConstExtractValue(uncommonTypeInit, []uint32{0})
rtype = llvm.ConstInsertValue(rtype, namePtr, []uint32{8})
// Update the global's initialiser. Note that we take a copy
// of the underlying type; we're not updating a shared type.
if underlyingRuntimeType.Type() != tm.runtime.rtype.llvm {
underlyingRuntimeType = llvm.ConstInsertValue(underlyingRuntimeType, rtype, []uint32{0})
} else {
underlyingRuntimeType = rtype
}
global.SetInitializer(underlyingRuntimeType)
return global, ptr
}
示例2: nameRuntimeType
func (tm *TypeMap) nameRuntimeType(n *types.Named) (global, ptr llvm.Value) {
var path string
if data, ok := tm.functions.objectdata[n.Obj()]; ok {
path = pkgpath(data.Package)
}
if path == "" {
// Set to "runtime", so the builtin types have a home.
path = "runtime"
}
globalname := "__llgo.type." + path + "." + n.Obj().Name()
if path != tm.pkgpath {
// We're not compiling the package from whence the type came,
// so we'll just create a pointer to it here.
global := llvm.AddGlobal(tm.module, tm.runtimeType, globalname)
global.SetInitializer(llvm.ConstNull(tm.runtimeType))
global.SetLinkage(llvm.CommonLinkage)
return global, global
}
underlying := n.Underlying()
if name, ok := underlying.(*types.Named); ok {
underlying = name.Underlying()
}
global, ptr = tm.toRuntime(underlying)
// Locate the rtype.
underlyingRuntimeType := global.Initializer()
rtype := underlyingRuntimeType
if rtype.Type() != tm.runtimeType {
rtype = llvm.ConstExtractValue(rtype, []uint32{0})
}
// Insert the uncommon type.
uncommonTypeInit := tm.uncommonType(n, false)
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
rtype = llvm.ConstInsertValue(rtype, uncommonType, []uint32{9})
// Replace the rtype's string representation with the one from
// uncommonType. XXX should we have the package name prepended? Probably.
namePtr := llvm.ConstExtractValue(uncommonTypeInit, []uint32{0})
rtype = llvm.ConstInsertValue(rtype, namePtr, []uint32{8})
// Update the global's initialiser. Note that we take a copy
// of the underlying type; we're not updating a shared type.
if underlyingRuntimeType.Type() != tm.runtimeType {
underlyingRuntimeType = llvm.ConstInsertValue(underlyingRuntimeType, rtype, []uint32{0})
} else {
underlyingRuntimeType = rtype
}
global.SetName(globalname)
global.SetInitializer(underlyingRuntimeType)
return global, ptr
}
示例3: globalStringPtr
// globalStringPtr returns a *string with the specified value.
func (tm *TypeMap) globalStringPtr(value string) llvm.Value {
strval := llvm.ConstString(value, false)
strglobal := llvm.AddGlobal(tm.module, strval.Type(), "")
strglobal.SetInitializer(strval)
strglobal = llvm.ConstBitCast(strglobal, llvm.PointerType(llvm.Int8Type(), 0))
strlen := llvm.ConstInt(tm.inttype, uint64(len(value)), false)
str := llvm.ConstStruct([]llvm.Value{strglobal, strlen}, false)
g := llvm.AddGlobal(tm.module, str.Type(), "")
g.SetInitializer(str)
return g
}
示例4: pointerRuntimeType
func (tm *TypeMap) pointerRuntimeType(p *types.Pointer) (global, ptr llvm.Value) {
// Is the base type a named type from another package? If so, we'll
// create a reference to the externally defined symbol.
var globalname string
if n, ok := p.Base.(*types.Name); ok {
pkgpath := n.Package
if pkgpath == "" {
pkgpath = "runtime"
}
globalname = "__llgo.type.*" + n.String()
if pkgpath != tm.pkgpath {
global := llvm.AddGlobal(tm.module, tm.runtimeType, globalname)
global.SetInitializer(llvm.ConstNull(tm.runtimeType))
global.SetLinkage(llvm.CommonLinkage)
return global, global
}
}
commonType := tm.makeCommonType(p, reflect.Ptr)
if n, ok := p.Base.(*types.Name); ok {
uncommonTypeInit := tm.uncommonType(n, true)
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
commonType = llvm.ConstInsertValue(commonType, uncommonType, []uint32{9})
}
baseTypeGlobal, baseTypePtr := tm.toRuntime(p.Base)
ptrType := llvm.ConstNull(tm.runtimePtrType)
ptrType = llvm.ConstInsertValue(ptrType, commonType, []uint32{0})
ptrType = llvm.ConstInsertValue(ptrType, baseTypePtr, []uint32{1})
global, ptr = tm.makeRuntimeTypeGlobal(ptrType)
global.SetName(globalname)
// Set ptrToThis in the base type's commonType.
baseRuntimeType := baseTypeGlobal.Initializer()
baseType := llvm.ConstExtractValue(baseRuntimeType, []uint32{1})
if baseType.Type() == tm.runtimeCommonType {
baseType = llvm.ConstInsertValue(baseType, ptr, []uint32{10})
} else {
commonType := llvm.ConstExtractValue(baseType, []uint32{0})
commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{10})
baseType = llvm.ConstInsertValue(baseType, commonType, []uint32{0})
}
baseRuntimeType = llvm.ConstInsertValue(baseRuntimeType, baseType, []uint32{1})
baseTypeGlobal.SetInitializer(baseRuntimeType)
return global, ptr
}
示例5: basicRuntimeType
// basicRuntimeType creates the runtime type structure for
// a basic type. If underlying is true, then a new global
// is always created.
func (tm *TypeMap) basicRuntimeType(b *types.Basic, underlying bool) (global, ptr llvm.Value) {
b = types.Typ[b.Kind()] // unalias
var name string
if !underlying {
name = typeString(b)
if tm.pkgpath != "runtime" {
global := llvm.AddGlobal(tm.module, tm.runtime.rtype.llvm, typeSymbol(name))
global.SetInitializer(llvm.ConstNull(tm.runtime.rtype.llvm))
global.SetLinkage(llvm.CommonLinkage)
return global, global
}
}
rtype := tm.makeRtype(b, basicReflectKinds[b.Kind()])
global, ptr = tm.makeRuntimeTypeGlobal(rtype, name)
global.SetLinkage(llvm.ExternalLinkage)
if !underlying {
switch b.Kind() {
case types.Int32:
llvm.AddAlias(tm.module, global.Type(), global, typeSymbol("rune"))
case types.Uint8:
llvm.AddAlias(tm.module, global.Type(), global, typeSymbol("byte"))
}
}
return global, ptr
}
示例6: makeRuntimeTypeGlobal
func (tm *TypeMap) makeRuntimeTypeGlobal(v llvm.Value, name string) (global, ptr llvm.Value) {
global = llvm.AddGlobal(tm.module, v.Type(), typeSymbol(name))
global.SetInitializer(v)
global.SetLinkage(llvm.LinkOnceAnyLinkage)
ptr = llvm.ConstBitCast(global, llvm.PointerType(tm.runtime.rtype.llvm, 0))
return global, ptr
}
示例7: nameRuntimeType
func (tm *TypeMap) nameRuntimeType(n *types.Name) (global, ptr llvm.Value) {
global, ptr = tm.makeRuntimeType(n.Underlying)
globalInit := global.Initializer()
// Locate the common type.
underlyingRuntimeType := llvm.ConstExtractValue(globalInit, []uint32{1})
commonType := underlyingRuntimeType
if _, ok := n.Underlying.(*types.Basic); !ok {
commonType = llvm.ConstExtractValue(commonType, []uint32{0})
}
// Insert the uncommon type.
uncommonTypeInit := llvm.ConstNull(tm.runtimeUncommonType)
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
commonType = llvm.ConstInsertValue(commonType, uncommonType, []uint32{8})
// Update the global's initialiser.
if _, ok := n.Underlying.(*types.Basic); !ok {
underlyingRuntimeType = llvm.ConstInsertValue(underlyingRuntimeType, commonType, []uint32{0})
} else {
underlyingRuntimeType = commonType
}
globalInit = llvm.ConstInsertValue(globalInit, underlyingRuntimeType, []uint32{1})
global.SetName("__llgo.reflect." + n.Obj.Name)
return global, ptr
}
示例8: mapLookup
// mapLookup searches a map for a specified key, returning a pointer to the
// memory location for the value. If insert is given as true, and the key
// does not exist in the map, it will be added with an uninitialised value.
func (c *compiler) mapLookup(m *LLVMValue, key Value, insert bool) (elem *LLVMValue, notnull *LLVMValue) {
mapType := m.Type().Underlying().(*types.Map)
maplookup := c.NamedFunction("runtime.maplookup", "func f(t, m, k uintptr, insert bool) uintptr")
ptrType := c.target.IntPtrType()
args := make([]llvm.Value, 4)
args[0] = llvm.ConstPtrToInt(c.types.ToRuntime(mapType), ptrType)
args[1] = c.builder.CreatePtrToInt(m.LLVMValue(), ptrType, "")
if insert {
args[3] = llvm.ConstAllOnes(llvm.Int1Type())
} else {
args[3] = llvm.ConstNull(llvm.Int1Type())
}
if lv, islv := key.(*LLVMValue); islv && lv.pointer != nil {
args[2] = c.builder.CreatePtrToInt(lv.pointer.LLVMValue(), ptrType, "")
}
if args[2].IsNil() {
stackval := c.builder.CreateAlloca(c.types.ToLLVM(key.Type()), "")
c.builder.CreateStore(key.LLVMValue(), stackval)
args[2] = c.builder.CreatePtrToInt(stackval, ptrType, "")
}
eltPtrType := types.NewPointer(mapType.Elem())
llvmtyp := c.types.ToLLVM(eltPtrType)
zeroglobal := llvm.AddGlobal(c.module.Module, llvmtyp.ElementType(), "")
zeroglobal.SetInitializer(llvm.ConstNull(llvmtyp.ElementType()))
result := c.builder.CreateCall(maplookup, args, "")
result = c.builder.CreateIntToPtr(result, llvmtyp, "")
notnull_ := c.builder.CreateIsNotNull(result, "")
result = c.builder.CreateSelect(notnull_, result, zeroglobal, "")
value := c.NewValue(result, eltPtrType)
return value.makePointee(), c.NewValue(notnull_, types.Typ[types.Bool])
}
示例9: mapInsert
// mapInsert inserts a key into a map, returning a pointer to the memory
// location for the value.
func (c *compiler) mapInsert(m *LLVMValue, key Value) *LLVMValue {
mapType := m.Type().(*types.Map)
mapinsert := c.module.Module.NamedFunction("runtime.mapinsert")
ptrType := c.target.IntPtrType()
if mapinsert.IsNil() {
// params: dynamic type, mapptr, keyptr
paramTypes := []llvm.Type{ptrType, ptrType, ptrType}
funcType := llvm.FunctionType(ptrType, paramTypes, false)
mapinsert = llvm.AddFunction(c.module.Module, "runtime.mapinsert", funcType)
}
args := make([]llvm.Value, 3)
args[0] = llvm.ConstPtrToInt(c.types.ToRuntime(m.Type()), ptrType)
args[1] = c.builder.CreatePtrToInt(m.pointer.LLVMValue(), ptrType, "")
if lv, islv := key.(*LLVMValue); islv && lv.pointer != nil {
args[2] = c.builder.CreatePtrToInt(lv.pointer.LLVMValue(), ptrType, "")
}
if args[2].IsNil() {
// Create global constant, so we can take its address.
global := llvm.AddGlobal(c.module.Module, c.types.ToLLVM(key.Type()), "")
global.SetGlobalConstant(true)
global.SetInitializer(key.LLVMValue())
args[2] = c.builder.CreatePtrToInt(global, ptrType, "")
}
eltPtrType := &types.Pointer{Base: mapType.Elt}
result := c.builder.CreateCall(mapinsert, args, "")
result = c.builder.CreateIntToPtr(result, c.types.ToLLVM(eltPtrType), "")
value := c.NewLLVMValue(result, eltPtrType)
return value.makePointee()
}
示例10: nameRuntimeType
func (tm *TypeMap) nameRuntimeType(n *types.Name) llvm.Value {
underlyingRuntimeType := tm.ToRuntime(n.Underlying).Initializer()
result := llvm.AddGlobal(tm.module, underlyingRuntimeType.Type(), "")
result.SetName("__llgo.reflect." + n.Obj.Name)
elementTypes := tm.runtimeCommonType.StructElementTypes()
ptr := llvm.ConstBitCast(result, elementTypes[9])
commonType := llvm.ConstInsertValue(underlyingRuntimeType, ptr, []uint32{9})
uncommonTypeInit := llvm.ConstNull(tm.runtimeUncommonType) // TODO
uncommonType := llvm.AddGlobal(tm.module, uncommonTypeInit.Type(), "")
uncommonType.SetInitializer(uncommonTypeInit)
commonType = llvm.ConstInsertValue(commonType, uncommonType, []uint32{8})
// TODO set string, uncommonType.
result.SetInitializer(commonType)
return result
}
示例11: basicRuntimeType
func (tm *TypeMap) basicRuntimeType(b *types.Basic) llvm.Value {
commonType := tm.makeCommonType(b, reflect.Kind(b.Kind))
result := llvm.AddGlobal(tm.module, commonType.Type(), "")
elementTypes := tm.runtimeCommonType.StructElementTypes()
ptr := llvm.ConstBitCast(result, elementTypes[9])
commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{9})
result.SetInitializer(commonType)
return result
}
示例12: Resolve
func (c *compiler) Resolve(ident *ast.Ident) Value {
obj := c.typeinfo.Objects[ident]
data := c.objectdata[obj]
if data.Value != nil {
return data.Value
}
var value *LLVMValue
switch obj := obj.(type) {
case *types.Func:
value = c.makeFunc(ident, obj.Type().(*types.Signature))
case *synthFunc:
value = c.makeFunc(ident, obj.Type().(*types.Signature))
case *types.Var:
if data.Ident.Obj != nil {
switch decl := data.Ident.Obj.Decl.(type) {
case *ast.ValueSpec:
c.VisitValueSpec(decl)
case *ast.Field:
// No-op. Fields will be yielded for function
// arg/recv/ret. We update the .Data field of the
// object when we enter the function definition.
if data.Value == nil {
panic("expected object value")
}
}
}
// If it's an external variable, we'll need to create a global
// value reference here. It may be possible for multiple objects
// to refer to the same variable.
value = data.Value
if value == nil {
module := c.module.Module
t := obj.Type()
name := obj.Pkg().Path() + "." + obj.Name()
g := module.NamedGlobal(name)
if g.IsNil() {
g = llvm.AddGlobal(module, c.types.ToLLVM(t), name)
}
value = c.NewValue(g, types.NewPointer(t)).makePointee()
}
case *types.Const:
value = c.NewConstValue(obj.Val(), obj.Type())
default:
panic(fmt.Sprintf("unreachable (%T)", obj))
}
data.Value = value
return value
}
示例13: mapRuntimeType
func (tm *TypeMap) mapRuntimeType(m *types.Map) llvm.Value {
result := llvm.AddGlobal(tm.module, tm.runtimeMapType, "")
elementTypes := tm.runtimeCommonType.StructElementTypes()
ptr := llvm.ConstBitCast(result, elementTypes[9])
commonType := tm.makeCommonType(m, reflect.Map)
commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{9})
init := llvm.ConstNull(tm.runtimeMapType)
init = llvm.ConstInsertValue(init, commonType, []uint32{0})
result.SetInitializer(init)
// TODO set key, elem
return result
}
示例14: structRuntimeType
func (tm *TypeMap) structRuntimeType(s *types.Struct) llvm.Value {
result := llvm.AddGlobal(tm.module, tm.runtimeStructType, "")
elementTypes := tm.runtimeCommonType.StructElementTypes()
ptr := llvm.ConstBitCast(result, elementTypes[9])
commonType := tm.makeCommonType(s, reflect.Struct)
commonType = llvm.ConstInsertValue(commonType, ptr, []uint32{9})
init := llvm.ConstNull(tm.runtimeStructType)
init = llvm.ConstInsertValue(init, commonType, []uint32{0})
result.SetInitializer(init)
// TODO set fields
//panic("unimplemented")
return result
}
示例15: makeRuntimeTypeGlobal
func (tm *TypeMap) makeRuntimeTypeGlobal(v llvm.Value) (global, ptr llvm.Value) {
// Each runtime type is preceded by an interface{}.
initType := llvm.StructType([]llvm.Type{tm.runtimeType, v.Type()}, false)
global = llvm.AddGlobal(tm.module, initType, "")
ptr = llvm.ConstBitCast(global, llvm.PointerType(tm.runtimeType, 0))
// interface{} containing v's *commonType representation.
runtimeTypeValue := llvm.Undef(tm.runtimeType)
zero := llvm.ConstNull(llvm.Int32Type())
one := llvm.ConstInt(llvm.Int32Type(), 1, false)
i8ptr := llvm.PointerType(llvm.Int8Type(), 0)
if tm.commonTypePtrRuntimeType.IsNil() {
// Create a dummy pointer value, which we'll update straight after
// defining the runtime type info for commonType.
tm.commonTypePtrRuntimeType = llvm.Undef(i8ptr)
commonTypePtr := &types.Pointer{Base: tm.commonType}
commonTypeGlobal, commonTypeRuntimeType := tm.makeRuntimeType(tm.commonType)
tm.types[tm.commonType.String()] = runtimeTypeInfo{commonTypeGlobal, commonTypeRuntimeType}
commonTypePtrGlobal, commonTypePtrRuntimeType := tm.makeRuntimeType(commonTypePtr)
tm.types[commonTypePtr.String()] = runtimeTypeInfo{commonTypePtrGlobal, commonTypePtrRuntimeType}
tm.commonTypePtrRuntimeType = llvm.ConstBitCast(commonTypePtrRuntimeType, i8ptr)
if tm.pkgpath == tm.commonType.Package {
// Update the interace{} header of the commonType/*commonType
// runtime types we just created.
for _, g := range [...]llvm.Value{commonTypeGlobal, commonTypePtrGlobal} {
init := g.Initializer()
typptr := tm.commonTypePtrRuntimeType
runtimeTypeValue := llvm.ConstExtractValue(init, []uint32{0})
runtimeTypeValue = llvm.ConstInsertValue(runtimeTypeValue, typptr, []uint32{0})
init = llvm.ConstInsertValue(init, runtimeTypeValue, []uint32{0})
g.SetInitializer(init)
}
}
}
commonTypePtr := llvm.ConstGEP(global, []llvm.Value{zero, one})
commonTypePtr = llvm.ConstBitCast(commonTypePtr, i8ptr)
runtimeTypeValue = llvm.ConstInsertValue(runtimeTypeValue, tm.commonTypePtrRuntimeType, []uint32{0})
runtimeTypeValue = llvm.ConstInsertValue(runtimeTypeValue, commonTypePtr, []uint32{1})
init := llvm.Undef(initType)
init = llvm.ConstInsertValue(init, runtimeTypeValue, []uint32{0})
init = llvm.ConstInsertValue(init, v, []uint32{1})
global.SetInitializer(init)
return global, ptr
}