本文整理汇总了Golang中llvm/org/llvm/bindings/go/llvm.AddBasicBlock函数的典型用法代码示例。如果您正苦于以下问题:Golang AddBasicBlock函数的具体用法?Golang AddBasicBlock怎么用?Golang AddBasicBlock使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了AddBasicBlock函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: generateLoop
func (c *Codegen) generateLoop(node *parser.LoopStmtNode) (ret bool) {
currFunc := c.module.NamedFunction(c.currFunc)
if node.Init != nil {
c.generateVarDecl(node.Init, false)
}
entry := llvm.AddBasicBlock(currFunc, "")
body := llvm.AddBasicBlock(currFunc, "")
exit := llvm.AddBasicBlock(currFunc, "")
c.builder.CreateBr(entry)
c.builder.SetInsertPoint(entry, entry.LastInstruction())
cond := c.generateExpression(node.Cond)
c.builder.CreateCondBr(cond, body, exit)
c.builder.SetInsertPoint(body, body.LastInstruction())
if ret = c.generateBlock(node.Body); !ret {
if node.Post != nil {
switch t := node.Post.(type) {
case *parser.AssignStmtNode:
c.generateAssign(t)
case *parser.CallStmtNode:
c.generateCall(t.Call, null)
}
}
c.builder.CreateBr(entry)
}
c.builder.SetInsertPoint(exit, exit.LastInstruction())
return
}
示例2: genLoopStat
func (v *Codegen) genLoopStat(n *parser.LoopStat) {
switch n.LoopType {
case parser.LOOP_TYPE_INFINITE:
loopBlock := llvm.AddBasicBlock(v.currentFunction, "")
v.builder.CreateBr(loopBlock)
v.builder.SetInsertPointAtEnd(loopBlock)
v.genBlock(n.Body)
v.builder.CreateBr(loopBlock)
afterBlock := llvm.AddBasicBlock(v.currentFunction, "")
v.builder.SetInsertPointAtEnd(afterBlock)
case parser.LOOP_TYPE_CONDITIONAL:
evalBlock := llvm.AddBasicBlock(v.currentFunction, "")
v.builder.CreateBr(evalBlock)
loopBlock := llvm.AddBasicBlock(v.currentFunction, "")
afterBlock := llvm.AddBasicBlock(v.currentFunction, "")
v.builder.SetInsertPointAtEnd(evalBlock)
cond := v.genExpr(n.Condition)
v.builder.CreateCondBr(cond, loopBlock, afterBlock)
v.builder.SetInsertPointAtEnd(loopBlock)
v.genBlock(n.Body)
v.builder.CreateBr(evalBlock)
v.builder.SetInsertPointAtEnd(afterBlock)
default:
panic("invalid loop type")
}
}
示例3: genLogicalBinop
func (v *Codegen) genLogicalBinop(n *parser.BinaryExpr) llvm.Value {
and := n.Op == parser.BINOP_LOG_AND
next := llvm.AddBasicBlock(v.currentLLVMFunction(), "and_next")
exit := llvm.AddBasicBlock(v.currentLLVMFunction(), "and_exit")
b1 := v.genExpr(n.Lhand)
first := v.builder().GetInsertBlock()
if and {
v.builder().CreateCondBr(b1, next, exit)
} else {
v.builder().CreateCondBr(b1, exit, next)
}
v.builder().SetInsertPointAtEnd(next)
b2 := v.genExpr(n.Rhand)
next = v.builder().GetInsertBlock()
v.builder().CreateBr(exit)
v.builder().SetInsertPointAtEnd(exit)
phi := v.builder().CreatePHI(b2.Type(), "and_phi")
var testIncVal uint64
if and {
testIncVal = 0
} else {
testIncVal = 1
}
phi.AddIncoming([]llvm.Value{llvm.ConstInt(llvm.IntType(1), testIncVal, false), b2}, []llvm.BasicBlock{first, next})
return phi
}
示例4: condBrRuntimeError
func (fr *frame) condBrRuntimeError(cond llvm.Value, errcode uint64) {
if cond.IsNull() {
return
}
errorbb := fr.runtimeErrorBlocks[errcode]
newbb := errorbb.C == nil
if newbb {
errorbb = llvm.AddBasicBlock(fr.function, "")
fr.runtimeErrorBlocks[errcode] = errorbb
}
contbb := llvm.AddBasicBlock(fr.function, "")
br := fr.builder.CreateCondBr(cond, errorbb, contbb)
fr.setBranchWeightMetadata(br, 1, 1000)
if newbb {
fr.builder.SetInsertPointAtEnd(errorbb)
fr.runtime.runtimeError.call(fr, llvm.ConstInt(llvm.Int32Type(), errcode, false))
fr.builder.CreateUnreachable()
}
fr.builder.SetInsertPointAtEnd(contbb)
}
示例5: genBoundsCheck
func (v *Codegen) genBoundsCheck(limit llvm.Value, index llvm.Value, indexType parser.Type) {
segvBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "boundscheck_segv")
endBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "boundscheck_end")
upperCheckBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "boundscheck_upper_block")
tooLow := v.builder().CreateICmp(llvm.IntSGT, llvm.ConstInt(index.Type(), 0, false), index, "boundscheck_lower")
v.builder().CreateCondBr(tooLow, segvBlock, upperCheckBlock)
v.builder().SetInsertPointAtEnd(upperCheckBlock)
// make sure limit and index have same width
castedLimit := limit
castedIndex := index
if index.Type().IntTypeWidth() < limit.Type().IntTypeWidth() {
if indexType.IsSigned() {
castedIndex = v.builder().CreateSExt(index, limit.Type(), "")
} else {
castedIndex = v.builder().CreateZExt(index, limit.Type(), "")
}
} else if index.Type().IntTypeWidth() > limit.Type().IntTypeWidth() {
castedLimit = v.builder().CreateZExt(limit, index.Type(), "")
}
tooHigh := v.builder().CreateICmp(llvm.IntSLE, castedLimit, castedIndex, "boundscheck_upper")
v.builder().CreateCondBr(tooHigh, segvBlock, endBlock)
v.builder().SetInsertPointAtEnd(segvBlock)
v.genRaiseSegfault()
v.builder().CreateUnreachable()
v.builder().SetInsertPointAtEnd(endBlock)
}
示例6: emitInitPrologue
// emitInitPrologue emits the init-specific function prologue (guard check and
// initialization of dependent packages under the llgo native ABI), and returns
// the basic block into which the GC registration call should be emitted.
func (fr *frame) emitInitPrologue() llvm.BasicBlock {
if fr.GccgoABI {
return fr.builder.GetInsertBlock()
}
initGuard := llvm.AddGlobal(fr.module.Module, llvm.Int1Type(), "init$guard")
initGuard.SetLinkage(llvm.InternalLinkage)
initGuard.SetInitializer(llvm.ConstNull(llvm.Int1Type()))
returnBlock := llvm.AddBasicBlock(fr.function, "")
initBlock := llvm.AddBasicBlock(fr.function, "")
initGuardVal := fr.builder.CreateLoad(initGuard, "")
fr.builder.CreateCondBr(initGuardVal, returnBlock, initBlock)
fr.builder.SetInsertPointAtEnd(returnBlock)
fr.builder.CreateRetVoid()
fr.builder.SetInsertPointAtEnd(initBlock)
fr.builder.CreateStore(llvm.ConstInt(llvm.Int1Type(), 1, false), initGuard)
int8ptr := llvm.PointerType(fr.types.ctx.Int8Type(), 0)
ftyp := llvm.FunctionType(llvm.VoidType(), []llvm.Type{int8ptr}, false)
for _, pkg := range fr.pkg.Object.Imports() {
initname := ManglePackagePath(pkg.Path()) + "..import"
initfn := fr.module.Module.NamedFunction(initname)
if initfn.IsNil() {
initfn = llvm.AddFunction(fr.module.Module, initname, ftyp)
}
args := []llvm.Value{llvm.Undef(int8ptr)}
fr.builder.CreateCall(initfn, args, "")
}
return initBlock
}
示例7: callRecover
func (fr *frame) callRecover(isDeferredRecover bool) *govalue {
startbb := fr.builder.GetInsertBlock()
recoverbb := llvm.AddBasicBlock(fr.function, "")
contbb := llvm.AddBasicBlock(fr.function, "")
canRecover := fr.builder.CreateTrunc(fr.canRecover, llvm.Int1Type(), "")
fr.builder.CreateCondBr(canRecover, recoverbb, contbb)
fr.builder.SetInsertPointAtEnd(recoverbb)
var recovered llvm.Value
if isDeferredRecover {
recovered = fr.runtime.deferredRecover.call(fr)[0]
} else {
recovered = fr.runtime.recover.call(fr)[0]
}
recoverbb = fr.builder.GetInsertBlock()
fr.builder.CreateBr(contbb)
fr.builder.SetInsertPointAtEnd(contbb)
eface := types.NewInterface(nil, nil)
llv := fr.builder.CreatePHI(fr.types.ToLLVM(eface), "")
llv.AddIncoming(
[]llvm.Value{llvm.ConstNull(llv.Type()), recovered},
[]llvm.BasicBlock{startbb, recoverbb},
)
return newValue(llv, eface)
}
示例8: runDefers
// Runs defers. If a defer panics, check for recovers in later defers.
func (fr *frame) runDefers() {
loopbb := llvm.AddBasicBlock(fr.function, "")
fr.builder.CreateBr(loopbb)
retrylpad := llvm.AddBasicBlock(fr.function, "")
fr.builder.SetInsertPointAtEnd(retrylpad)
fr.createLandingPad(false)
fr.runtime.checkDefer.callOnly(fr, fr.frameptr)
fr.builder.CreateBr(loopbb)
fr.builder.SetInsertPointAtEnd(loopbb)
fr.runtime.undefer.invoke(fr, retrylpad, fr.frameptr)
}
示例9: genIfStat
func (v *Codegen) genIfStat(n *parser.IfStat) {
// Warning to all who tread here:
// This function is complicated, but theoretically it should never need to
// be changed again. God help the soul who has to edit this.
if !v.inFunction() {
panic("tried to gen if stat not in function")
}
statTerm := semantic.IsNodeTerminating(n)
var end llvm.BasicBlock
if !statTerm {
end = llvm.AddBasicBlock(v.currentLLVMFunction(), "end")
}
for i, expr := range n.Exprs {
cond := v.genExpr(expr)
ifTrue := llvm.AddBasicBlock(v.currentLLVMFunction(), "if_true")
ifFalse := llvm.AddBasicBlock(v.currentLLVMFunction(), "if_false")
v.builder().CreateCondBr(cond, ifTrue, ifFalse)
v.builder().SetInsertPointAtEnd(ifTrue)
v.genBlock(n.Bodies[i])
if !statTerm && !n.Bodies[i].IsTerminating && !isBreakOrNext(n.Bodies[i].LastNode()) {
v.builder().CreateBr(end)
}
v.builder().SetInsertPointAtEnd(ifFalse)
if !statTerm {
end.MoveAfter(ifFalse)
}
}
if n.Else != nil {
v.genBlock(n.Else)
}
if !statTerm && (n.Else == nil || (!n.Else.IsTerminating && !isBreakOrNext(n.Else.LastNode()))) {
v.builder().CreateBr(end)
}
if !statTerm {
v.builder().SetInsertPointAtEnd(end)
}
}
示例10: createInitMainFunction
func (c *compiler) createInitMainFunction(mainPkg *ssa.Package) {
int8ptr := llvm.PointerType(c.types.ctx.Int8Type(), 0)
ftyp := llvm.FunctionType(llvm.VoidType(), []llvm.Type{int8ptr}, false)
initMain := llvm.AddFunction(c.module.Module, "__go_init_main", ftyp)
c.addCommonFunctionAttrs(initMain)
entry := llvm.AddBasicBlock(initMain, "entry")
builder := llvm.GlobalContext().NewBuilder()
defer builder.Dispose()
builder.SetInsertPointAtEnd(entry)
args := []llvm.Value{llvm.Undef(int8ptr)}
if !c.GccgoABI {
initfn := c.module.Module.NamedFunction("main..import")
if !initfn.IsNil() {
builder.CreateCall(initfn, args, "")
}
builder.CreateRetVoid()
return
}
initdata := c.buildPackageInitData(mainPkg)
for _, init := range initdata.Inits {
initfn := c.module.Module.NamedFunction(init.InitFunc)
if initfn.IsNil() {
initfn = llvm.AddFunction(c.module.Module, init.InitFunc, ftyp)
}
builder.CreateCall(initfn, args, "")
}
builder.CreateRetVoid()
}
示例11: stdString
func (c *Codegen) stdString() {
tmpl := &Template{
Type: llvm.GlobalContext().StructCreateNamed("string"),
Variables: map[string]int{},
}
c.templates["string"] = tmpl
vars := []llvm.Type{
llvm.PointerType(PRIMITIVE_TYPES["char"], 0),
PRIMITIVE_TYPES["int"],
PRIMITIVE_TYPES["int"],
}
tmpl.Type.StructSetBody(vars, false)
lenFuncType := llvm.FunctionType(PRIMITIVE_TYPES["int"], []llvm.Type{llvm.PointerType(tmpl.Type, 0)}, false)
lenFunc := llvm.AddFunction(c.module, "-string-len", lenFuncType)
lenFunc.Param(0).SetName("this")
block := llvm.AddBasicBlock(c.module.NamedFunction("-string-len"), "entry")
c.functions["-string-len"] = block
c.currFunc = "-string-len"
c.builder.SetInsertPoint(block, block.LastInstruction())
ret := c.builder.CreateStructGEP(c.getCurrParam("this"), 1, "")
ret = c.builder.CreateLoad(ret, "")
ret = c.builder.CreateSub(ret, llvm.ConstInt(PRIMITIVE_TYPES["int"], 1, false), "")
c.builder.CreateRet(ret)
printFuncType := llvm.FunctionType(PRIMITIVE_TYPES["int"], []llvm.Type{
llvm.PointerType(PRIMITIVE_TYPES["char"], 0),
}, true)
llvm.AddFunction(c.module, "printf", printFuncType)
}
示例12: genFunctionDecl
func (v *Codegen) genFunctionDecl(n *parser.FunctionDecl) llvm.Value {
var res llvm.Value
mangledName := n.Function.MangledName(parser.MANGLE_ARK_UNSTABLE)
function := v.curFile.Module.NamedFunction(mangledName)
if function.IsNil() {
//v.err("genning function `%s` doesn't exist in module", n.Function.Name)
// hmmmm seems we just ignore this here
} else {
if !n.Prototype {
block := llvm.AddBasicBlock(function, "entry")
v.builder.SetInsertPointAtEnd(block)
for i, par := range n.Function.Parameters {
alloc := v.builder.CreateAlloca(v.typeToLLVMType(par.Variable.Type), par.Variable.MangledName(parser.MANGLE_ARK_UNSTABLE))
v.variableLookup[par.Variable] = alloc
v.builder.CreateStore(function.Params()[i], alloc)
}
v.inFunction = true
v.currentFunction = function
v.genBlock(n.Function.Body)
v.inFunction = false
}
}
return res
}
示例13: genFunctionBody
func (v *Codegen) genFunctionBody(fn *parser.Function, llvmFn llvm.Value) {
block := llvm.AddBasicBlock(llvmFn, "entry")
v.pushFunction(fn)
v.builders[v.currentFunction()] = llvm.NewBuilder()
v.builder().SetInsertPointAtEnd(block)
pars := fn.Parameters
if fn.Type.Receiver != nil {
newPars := make([]*parser.VariableDecl, len(pars)+1)
newPars[0] = fn.Receiver
copy(newPars[1:], pars)
pars = newPars
}
for i, par := range pars {
alloc := v.builder().CreateAlloca(v.typeToLLVMType(par.Variable.Type), par.Variable.Name)
v.variableLookup[par.Variable] = alloc
v.builder().CreateStore(llvmFn.Params()[i], alloc)
}
v.genBlock(fn.Body)
v.builder().Dispose()
delete(v.builders, v.currentFunction())
delete(v.curLoopExits, v.currentFunction())
delete(v.curLoopNexts, v.currentFunction())
v.popFunction()
}
示例14: genLoopStat
func (v *Codegen) genLoopStat(n *parser.LoopStat) {
curfn := v.currentFunction()
afterBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "loop_exit")
v.curLoopExits[curfn] = append(v.curLoopExits[curfn], afterBlock)
switch n.LoopType {
case parser.LOOP_TYPE_INFINITE:
loopBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "loop_body")
v.curLoopNexts[curfn] = append(v.curLoopNexts[curfn], loopBlock)
v.builder().CreateBr(loopBlock)
v.builder().SetInsertPointAtEnd(loopBlock)
v.genBlock(n.Body)
if !isBreakOrNext(n.Body.LastNode()) {
v.builder().CreateBr(loopBlock)
}
v.builder().SetInsertPointAtEnd(afterBlock)
case parser.LOOP_TYPE_CONDITIONAL:
evalBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "loop_condeval")
v.builder().CreateBr(evalBlock)
v.curLoopNexts[curfn] = append(v.curLoopNexts[curfn], evalBlock)
loopBlock := llvm.AddBasicBlock(v.currentLLVMFunction(), "loop_body")
v.builder().SetInsertPointAtEnd(evalBlock)
cond := v.genExpr(n.Condition)
v.builder().CreateCondBr(cond, loopBlock, afterBlock)
v.builder().SetInsertPointAtEnd(loopBlock)
v.genBlock(n.Body)
if !isBreakOrNext(n.Body.LastNode()) {
v.builder().CreateBr(evalBlock)
}
v.builder().SetInsertPointAtEnd(afterBlock)
default:
panic("invalid loop type")
}
v.curLoopExits[curfn] = v.curLoopExits[curfn][:len(v.curLoopExits[curfn])-1]
v.curLoopNexts[curfn] = v.curLoopNexts[curfn][:len(v.curLoopNexts[curfn])-1]
}
示例15: genIfStat
func (v *Codegen) genIfStat(n *parser.IfStat) {
if !v.inFunction {
panic("tried to gen if stat not in function")
}
end := llvm.AddBasicBlock(v.currentFunction, "")
for i, expr := range n.Exprs {
cond := v.genExpr(expr)
ifTrue := llvm.AddBasicBlock(v.currentFunction, "")
ifFalse := llvm.AddBasicBlock(v.currentFunction, "")
v.builder.CreateCondBr(cond, ifTrue, ifFalse)
v.builder.SetInsertPointAtEnd(ifTrue)
for _, node := range n.Bodies[i].Nodes {
v.genNode(node)
}
if len(n.Bodies[i].Nodes) == 0 {
v.builder.CreateBr(end)
} else {
switch n.Bodies[i].Nodes[len(n.Bodies[i].Nodes)-1].(type) {
case *parser.ReturnStat: // do nothing
default:
v.builder.CreateBr(end)
}
}
v.builder.SetInsertPointAtEnd(ifFalse)
end.MoveAfter(ifFalse)
}
if n.Else != nil {
for _, node := range n.Else.Nodes {
v.genNode(node)
}
}
v.builder.CreateBr(end)
v.builder.SetInsertPointAtEnd(end)
}