本文整理汇总了Golang中llvm/org/llvm/bindings/go/llvm.Value类的典型用法代码示例。如果您正苦于以下问题:Golang Value类的具体用法?Golang Value怎么用?Golang Value使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Value类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Gen
func (n *VarExpr) Gen(cg *CG) llvm.Value {
fun := cg.GetInsertBlock().Parent()
var oldBindingNames []string
var oldBindingValues []llvm.Value
for name, init := range n.Names {
var initVal llvm.Value
if init != nil {
init.Gen(cg)
if initVal.IsNil() {
return llvm.Value{}
}
} else {
initVal = llvm.ConstFloat(cg.FloatType(), 0)
}
alloca := createEntryBlockAlloca(fun, name)
cg.CreateStore(initVal, alloca)
oldBindingNames = append(oldBindingNames, name)
oldBindingValues = append(oldBindingValues, cg.NamedValues[name])
cg.NamedValues[name] = alloca
}
bodyVal := n.Body.Gen(cg)
if bodyVal.IsNil() {
return llvm.Value{}
}
for i, name := range oldBindingNames {
cg.NamedValues[name] = oldBindingValues[i]
}
return bodyVal
}
示例2: generateArithmeticBinaryExpr
func (c *Codegen) generateArithmeticBinaryExpr(left, right llvm.Value, op string) llvm.Value {
t := c.getUnderlyingType(left.Type())
switch op {
case "+":
if t == PRIMITIVE_TYPES["float"] {
return c.builder.CreateFAdd(left, right, "")
} else if t == PRIMITIVE_TYPES["int"] {
return c.builder.CreateAdd(left, right, "")
} else if t == c.templates["string"].Type {
return c.generateStringConcat(left, right)
}
case "-":
if t == PRIMITIVE_TYPES["float"] {
return c.builder.CreateFSub(left, right, "")
} else if t == PRIMITIVE_TYPES["int"] {
return c.builder.CreateSub(left, right, "")
}
case "*":
if t == PRIMITIVE_TYPES["float"] {
return c.builder.CreateFMul(left, right, "")
} else if t == PRIMITIVE_TYPES["int"] {
return c.builder.CreateMul(left, right, "")
}
case "/":
if t == PRIMITIVE_TYPES["float"] {
return c.builder.CreateFDiv(left, right, "")
} else if t == PRIMITIVE_TYPES["int"] {
return c.builder.CreateSDiv(left, right, "")
}
}
return null
}
示例3: 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)
}
示例4: generateVarDecl
func (c *Codegen) generateVarDecl(node *parser.VarDeclNode, global bool) {
t := c.getLLVMType(node.Type)
name := node.Name.Value
if c.scope.Declared(name) {
// Error name has already been declared
}
var alloc, val llvm.Value
if node.Value == nil {
if t.TypeKind() == llvm.PointerTypeKind {
val = c.convert(c.scope.GetValue("null"), t)
} else {
val = llvm.Undef(t)
}
} else {
val = c.convert(c.generateExpression(node.Value), t)
}
if !global {
alloc = c.builder.CreateAlloca(t, name)
c.builder.CreateStore(val, alloc)
} else {
alloc = llvm.AddGlobal(c.module, t, name)
alloc.SetInitializer(val)
}
c.scope.AddVariable(name, alloc)
}
示例5: directEncode
func directEncode(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, argTypes []llvm.Type, args []llvm.Value, val llvm.Value) {
valType := val.Type()
switch len(argTypes) {
case 0:
// do nothing
case 1:
if argTypes[0].C == valType.C {
args[0] = val
return
}
alloca := allocaBuilder.CreateAlloca(valType, "")
bitcast := builder.CreateBitCast(alloca, llvm.PointerType(argTypes[0], 0), "")
builder.CreateStore(val, alloca)
args[0] = builder.CreateLoad(bitcast, "")
case 2:
encodeType := llvm.StructType(argTypes, false)
alloca := allocaBuilder.CreateAlloca(valType, "")
bitcast := builder.CreateBitCast(alloca, llvm.PointerType(encodeType, 0), "")
builder.CreateStore(val, alloca)
args[0] = builder.CreateLoad(builder.CreateStructGEP(bitcast, 0, ""), "")
args[1] = builder.CreateLoad(builder.CreateStructGEP(bitcast, 1, ""), "")
default:
panic("unexpected argTypes size")
}
}
示例6: 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()
}
示例7: createZExtOrTrunc
func (fr *frame) createZExtOrTrunc(v llvm.Value, t llvm.Type, name string) llvm.Value {
switch n := v.Type().IntTypeWidth() - t.IntTypeWidth(); {
case n < 0:
v = fr.builder.CreateZExt(v, fr.target.IntPtrType(), name)
case n > 0:
v = fr.builder.CreateTrunc(v, fr.target.IntPtrType(), name)
}
return v
}
示例8: unbox
func (c *Codegen) unbox(val llvm.Value) llvm.Value {
t := c.getUnderlyingType(val.Type())
switch t {
case c.templates["string"].Type:
val = c.builder.CreateStructGEP(val, 0, "")
val = c.builder.CreateLoad(val, "")
}
return val
}
示例9: setBranchWeightMetadata
func (fr *frame) setBranchWeightMetadata(br llvm.Value, trueweight, falseweight uint64) {
mdprof := llvm.MDKindID("prof")
mdnode := llvm.MDNode([]llvm.Value{
llvm.MDString("branch_weights"),
llvm.ConstInt(llvm.Int32Type(), trueweight, false),
llvm.ConstInt(llvm.Int32Type(), falseweight, false),
})
br.SetMetadata(mdprof, mdnode)
}
示例10: genEnumLiteral
func (v *Codegen) genEnumLiteral(n *parser.EnumLiteral) llvm.Value {
enumType := n.Type.ActualType().(parser.EnumType)
enumLLVMType := v.typeToLLVMType(n.Type)
memberIdx := enumType.MemberIndex(n.Member)
member := enumType.Members[memberIdx]
if enumType.Simple {
return llvm.ConstInt(enumLLVMType, uint64(member.Tag), false)
}
// TODO: Handle other integer size, maybe dynamic depending on max value?
tagValue := llvm.ConstInt(llvm.IntType(32), uint64(member.Tag), false)
enumValue := llvm.Undef(enumLLVMType)
enumValue = v.builder().CreateInsertValue(enumValue, tagValue, 0, "")
memberLLVMType := v.typeToLLVMType(member.Type)
var memberValue llvm.Value
if n.TupleLiteral != nil {
memberValue = v.genTupleLiteral(n.TupleLiteral)
} else if n.CompositeLiteral != nil {
memberValue = v.genCompositeLiteral(n.CompositeLiteral)
}
if v.inFunction() {
alloc := v.builder().CreateAlloca(enumLLVMType, "")
tagGep := v.builder().CreateStructGEP(alloc, 0, "")
v.builder().CreateStore(tagValue, tagGep)
if !memberValue.IsNil() {
dataGep := v.builder().CreateStructGEP(alloc, 1, "")
dataGep = v.builder().CreateBitCast(dataGep, llvm.PointerType(memberLLVMType, 0), "")
v.builder().CreateStore(memberValue, dataGep)
}
return v.builder().CreateLoad(alloc, "")
} else {
panic("unimplemented: global enum literal")
}
}
示例11: hackDump
// hackDump returns the value dump as a string.
func hackDump(v llvm.Value) (string, error) {
// Open temp file.
// TODO: Use an in-memory file instead of /tmp/x.
fd, err := unix.Open("/tmp/x", unix.O_WRONLY|unix.O_TRUNC|unix.O_CREAT, 0644)
if err != nil {
return "", errutil.Err(err)
}
// Store original stderr.
stderr, err := unix.Dup(2)
if err != nil {
return "", errutil.Err(err)
}
// Capture stderr and redirect its output to the temp file.
err = unix.Dup2(fd, 2)
if err != nil {
return "", errutil.Err(err)
}
err = unix.Close(fd)
if err != nil {
return "", errutil.Err(err)
}
// Dump value.
v.Dump()
C.fflush_stderr()
// Restore stderr.
err = unix.Dup2(stderr, 2)
if err != nil {
return "", errutil.Err(err)
}
err = unix.Close(stderr)
if err != nil {
return "", errutil.Err(err)
}
// Return content of temp file.
buf, err := ioutil.ReadFile("/tmp/x")
if err != nil {
return "", errutil.Err(err)
}
return string(buf), nil
}
示例12: parseOperand
// parseOperand converts the provided LLVM IR operand into an equivalent Go AST
// expression node (a basic literal, a composite literal or an identifier).
//
// Syntax:
// i32 1
// %foo = ...
func parseOperand(op llvm.Value) (ast.Expr, error) {
// TODO: Support *BasicLit, *CompositeLit.
// Parse and validate tokens.
tokens, err := getTokens(op)
if err != nil {
return nil, err
}
if len(tokens) < 2 {
// TODO: Remove debug output.
op.Dump()
return nil, errutil.Newf("unable to parse operand; expected 2 >= tokens, got %d", len(tokens))
}
// TODO: Add support for operand of other types than int.
// TODO: Parse type.
// Create and return a constant operand.
// i32 42
if tokens[0].Kind == lltoken.Type {
switch tok := tokens[1]; tok.Kind {
case lltoken.Int:
return &ast.BasicLit{Kind: token.INT, Value: tok.Val}, nil
case lltoken.LocalVar:
return getIdent(tok)
default:
return nil, errutil.Newf("support for LLVM IR token kind %v not yet implemented", tok.Kind)
}
}
// Create and return a variable operand.
// %foo = ...
if tokens[1].Kind == lltoken.Equal {
switch tok := tokens[0]; tok.Kind {
case lltoken.LocalVar, lltoken.LocalID:
// %foo
// %42
return getIdent(tok)
default:
return nil, errutil.Newf("support for LLVM IR token kind %v not yet implemented", tok.Kind)
}
}
return nil, errutil.New("support for LLVM IR operand not yet implemented")
}
示例13: PushFunction
// PushFunction creates debug metadata for the specified function,
// and pushes it onto the scope stack.
func (d *DIBuilder) PushFunction(fnptr llvm.Value, sig *types.Signature, pos token.Pos) {
var diFile llvm.Metadata
var line int
if file := d.fset.File(pos); file != nil {
d.fnFile = file.Name()
diFile = d.getFile(file)
line = file.Line(pos)
}
d.fn = d.builder.CreateFunction(d.scope(), llvm.DIFunction{
Name: fnptr.Name(), // TODO(axw) unmangled name?
LinkageName: fnptr.Name(),
File: diFile,
Line: line,
Type: d.DIType(sig),
IsDefinition: true,
})
fnptr.SetSubprogram(d.fn)
}
示例14: parseBinOp
// parseBinOp converts the provided LLVM IR binary operation into an equivalent
// Go AST node (an assignment statement with a binary expression on the right-
// hand side).
//
// Syntax:
// <result> add <type> <op1>, <op2>
//
// References:
// http://llvm.org/docs/LangRef.html#binary-operations
func parseBinOp(inst llvm.Value, op token.Token) (ast.Stmt, error) {
x, err := parseOperand(inst.Operand(0))
if err != nil {
return nil, err
}
y, err := parseOperand(inst.Operand(1))
if err != nil {
return nil, err
}
result, err := getResult(inst)
if err != nil {
return nil, errutil.Err(err)
}
lhs := []ast.Expr{result}
rhs := []ast.Expr{&ast.BinaryExpr{X: x, Op: op, Y: y}}
// TODO: Use "=" instead of ":=" and let go-post and grind handle the ":=" to
// "=" propagation.
return &ast.AssignStmt{Lhs: lhs, Tok: token.DEFINE, Rhs: rhs}, nil
}
示例15: convert
func (c *Codegen) convert(val llvm.Value, t llvm.Type) llvm.Value {
if val.Type() == t {
return val
}
if val == c.scope.GetValue("null") {
log.Println("Null conversion")
return llvm.ConstPointerNull(t)
}
switch val.Type() {
case PRIMITIVE_TYPES["int"]:
if t == PRIMITIVE_TYPES["float"] {
return c.builder.CreateSIToFP(val, t, "")
}
}
return val
}