本文整理汇总了Golang中github.com/axw/gollvm/llvm.ConstAllOnes函数的典型用法代码示例。如果您正苦于以下问题:Golang ConstAllOnes函数的具体用法?Golang ConstAllOnes怎么用?Golang ConstAllOnes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ConstAllOnes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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])
}
示例2: convertI2V
// convertI2V converts an interface to a value.
func (v *LLVMValue) convertI2V(typ types.Type) (result, success Value) {
builder := v.compiler.builder
predicate := v.interfaceTypeEquals(typ).LLVMValue()
// If result is zero, then we've got a match.
end := llvm.InsertBasicBlock(builder.GetInsertBlock(), "end")
end.MoveAfter(builder.GetInsertBlock())
nonmatch := llvm.InsertBasicBlock(end, "nonmatch")
match := llvm.InsertBasicBlock(nonmatch, "match")
builder.CreateCondBr(predicate, match, nonmatch)
builder.SetInsertPointAtEnd(match)
matchResultValue := v.loadI2V(typ).LLVMValue()
builder.CreateBr(end)
builder.SetInsertPointAtEnd(nonmatch)
nonmatchResultValue := llvm.ConstNull(matchResultValue.Type())
builder.CreateBr(end)
builder.SetInsertPointAtEnd(end)
successValue := builder.CreatePHI(llvm.Int1Type(), "")
resultValue := builder.CreatePHI(matchResultValue.Type(), "")
successValues := []llvm.Value{llvm.ConstAllOnes(llvm.Int1Type()), llvm.ConstNull(llvm.Int1Type())}
successBlocks := []llvm.BasicBlock{match, nonmatch}
successValue.AddIncoming(successValues, successBlocks)
success = v.compiler.NewValue(successValue, types.Typ[types.Bool])
resultValues := []llvm.Value{matchResultValue, nonmatchResultValue}
resultBlocks := []llvm.BasicBlock{match, nonmatch}
resultValue.AddIncoming(resultValues, resultBlocks)
result = v.compiler.NewValue(resultValue, typ)
return result, success
}
示例3: UnaryOp
func (v *LLVMValue) UnaryOp(op token.Token) Value {
b := v.compiler.builder
switch op {
case token.SUB:
var value llvm.Value
isfp := types.Identical(types.Underlying(v.typ), types.Float32) ||
types.Identical(types.Underlying(v.typ), types.Float64)
if isfp {
zero := llvm.ConstNull(v.compiler.types.ToLLVM(v.Type()))
value = b.CreateFSub(zero, v.LLVMValue(), "")
} else {
value = b.CreateNeg(v.LLVMValue(), "")
}
return v.compiler.NewLLVMValue(value, v.typ)
case token.ADD:
return v // No-op
case token.AND:
return v.pointer
case token.NOT:
value := b.CreateNot(v.LLVMValue(), "")
return v.compiler.NewLLVMValue(value, v.typ)
case token.XOR:
lhs := v.LLVMValue()
rhs := llvm.ConstAllOnes(lhs.Type())
value := b.CreateXor(lhs, rhs, "")
return v.compiler.NewLLVMValue(value, v.typ)
default:
panic("Unhandled operator: ") // + expr.Op)
}
panic("unreachable")
}
示例4: UnaryOp
func (v *LLVMValue) UnaryOp(op token.Token) Value {
b := v.compiler.builder
switch op {
case token.SUB:
var value llvm.Value
if isFloat(v.typ) {
zero := llvm.ConstNull(v.compiler.types.ToLLVM(v.Type()))
value = b.CreateFSub(zero, v.LLVMValue(), "")
} else {
value = b.CreateNeg(v.LLVMValue(), "")
}
return v.compiler.NewValue(value, v.typ)
case token.ADD:
return v // No-op
case token.NOT:
value := b.CreateNot(v.LLVMValue(), "")
return v.compiler.NewValue(value, v.typ)
case token.XOR:
lhs := v.LLVMValue()
rhs := llvm.ConstAllOnes(lhs.Type())
value := b.CreateXor(lhs, rhs, "")
return v.compiler.NewValue(value, v.typ)
default:
panic(fmt.Sprintf("Unhandled operator: %s", op))
}
panic("unreachable")
}
示例5: compileLogicalOp
// Binary logical operators are handled specially, outside of the Value
// type, because of the need to perform lazy evaluation.
//
// Binary logical operators are implemented using a Phi node, which takes
// on the appropriate value depending on which basic blocks branch to it.
func (c *compiler) compileLogicalOp(op token.Token, lhs Value, rhsFunc func() Value) Value {
lhsBlock := c.builder.GetInsertBlock()
resultBlock := llvm.AddBasicBlock(lhsBlock.Parent(), "")
resultBlock.MoveAfter(lhsBlock)
rhsBlock := llvm.InsertBasicBlock(resultBlock, "")
falseBlock := llvm.InsertBasicBlock(resultBlock, "")
if op == token.LOR {
c.builder.CreateCondBr(lhs.LLVMValue(), resultBlock, rhsBlock)
} else {
c.builder.CreateCondBr(lhs.LLVMValue(), rhsBlock, falseBlock)
}
c.builder.SetInsertPointAtEnd(rhsBlock)
rhs := rhsFunc()
rhsBlock = c.builder.GetInsertBlock() // rhsFunc may create blocks
c.builder.CreateCondBr(rhs.LLVMValue(), resultBlock, falseBlock)
c.builder.SetInsertPointAtEnd(falseBlock)
c.builder.CreateBr(resultBlock)
c.builder.SetInsertPointAtEnd(resultBlock)
result := c.builder.CreatePHI(llvm.Int1Type(), "")
trueValue := llvm.ConstAllOnes(llvm.Int1Type())
falseValue := llvm.ConstNull(llvm.Int1Type())
var values []llvm.Value
var blocks []llvm.BasicBlock
if op == token.LOR {
values = []llvm.Value{trueValue, trueValue, falseValue}
blocks = []llvm.BasicBlock{lhsBlock, rhsBlock, falseBlock}
} else {
values = []llvm.Value{trueValue, falseValue}
blocks = []llvm.BasicBlock{rhsBlock, falseBlock}
}
result.AddIncoming(values, blocks)
return c.NewLLVMValue(result, types.Bool)
}
示例6: slice
func (c *compiler) slice(x, low, high *LLVMValue) *LLVMValue {
if low != nil {
low = low.Convert(types.Typ[types.Int]).(*LLVMValue)
} else {
low = c.NewValue(llvm.ConstNull(c.types.inttype), types.Typ[types.Int])
}
if high != nil {
high = high.Convert(types.Typ[types.Int]).(*LLVMValue)
} else {
// all bits set is -1
high = c.NewValue(llvm.ConstAllOnes(c.types.inttype), types.Typ[types.Int])
}
switch typ := x.Type().Underlying().(type) {
case *types.Pointer: // *array
sliceslice := c.runtime.sliceslice.LLVMValue()
i8slice := sliceslice.Type().ElementType().ReturnType()
sliceValue := llvm.Undef(i8slice) // temporary slice
arraytyp := typ.Elem().Underlying().(*types.Array)
arrayptr := x.LLVMValue()
arrayptr = c.builder.CreateBitCast(arrayptr, i8slice.StructElementTypes()[0], "")
arraylen := llvm.ConstInt(c.llvmtypes.inttype, uint64(arraytyp.Len()), false)
sliceValue = c.builder.CreateInsertValue(sliceValue, arrayptr, 0, "")
sliceValue = c.builder.CreateInsertValue(sliceValue, arraylen, 1, "")
sliceValue = c.builder.CreateInsertValue(sliceValue, arraylen, 2, "")
sliceTyp := types.NewSlice(arraytyp.Elem())
runtimeTyp := c.types.ToRuntime(sliceTyp)
runtimeTyp = c.builder.CreatePtrToInt(runtimeTyp, c.target.IntPtrType(), "")
args := []llvm.Value{runtimeTyp, sliceValue, low.LLVMValue(), high.LLVMValue()}
result := c.builder.CreateCall(sliceslice, args, "")
llvmSliceTyp := c.types.ToLLVM(sliceTyp)
return c.NewValue(c.coerceSlice(result, llvmSliceTyp), sliceTyp)
case *types.Slice:
sliceslice := c.runtime.sliceslice.LLVMValue()
i8slice := sliceslice.Type().ElementType().ReturnType()
sliceValue := x.LLVMValue()
sliceTyp := sliceValue.Type()
sliceValue = c.coerceSlice(sliceValue, i8slice)
runtimeTyp := c.types.ToRuntime(x.Type())
runtimeTyp = c.builder.CreatePtrToInt(runtimeTyp, c.target.IntPtrType(), "")
args := []llvm.Value{runtimeTyp, sliceValue, low.LLVMValue(), high.LLVMValue()}
result := c.builder.CreateCall(sliceslice, args, "")
return c.NewValue(c.coerceSlice(result, sliceTyp), x.Type())
case *types.Basic:
stringslice := c.runtime.stringslice.LLVMValue()
llv := x.LLVMValue()
args := []llvm.Value{
c.coerceString(llv, stringslice.Type().ElementType().ParamTypes()[0]),
low.LLVMValue(),
high.LLVMValue(),
}
result := c.builder.CreateCall(stringslice, args, "")
return c.NewValue(c.coerceString(result, llv.Type()), x.Type())
default:
panic("unimplemented")
}
panic("unreachable")
}
示例7: boolLLVMValue
func boolLLVMValue(v bool) (lv llvm.Value) {
if v {
lv = llvm.ConstAllOnes(llvm.Int1Type())
} else {
lv = llvm.ConstNull(llvm.Int1Type())
}
return lv
}
示例8: VisitSliceExpr
func (c *compiler) VisitSliceExpr(expr *ast.SliceExpr) Value {
// expr.X, expr.Low, expr.High
value := c.VisitExpr(expr.X)
var low, high llvm.Value
if expr.Low != nil {
low = c.VisitExpr(expr.Low).Convert(types.Int32).LLVMValue()
} else {
low = llvm.ConstNull(llvm.Int32Type())
}
if expr.High != nil {
high = c.VisitExpr(expr.High).Convert(types.Int32).LLVMValue()
} else {
high = llvm.ConstAllOnes(llvm.Int32Type()) // -1
}
if _, ok := types.Underlying(value.Type()).(*types.Pointer); ok {
value = value.(*LLVMValue).makePointee()
}
switch typ := types.Underlying(value.Type()).(type) {
case *types.Array:
sliceslice := c.NamedFunction("runtime.sliceslice", "func f(t uintptr, s slice, low, high int32) slice")
i8slice := sliceslice.Type().ElementType().ReturnType()
sliceValue := llvm.Undef(i8slice) // temporary slice
arrayptr := value.(*LLVMValue).pointer.LLVMValue()
arrayptr = c.builder.CreateBitCast(arrayptr, i8slice.StructElementTypes()[0], "")
arraylen := llvm.ConstInt(llvm.Int32Type(), typ.Len, false)
sliceValue = c.builder.CreateInsertValue(sliceValue, arrayptr, 0, "")
sliceValue = c.builder.CreateInsertValue(sliceValue, arraylen, 1, "")
sliceValue = c.builder.CreateInsertValue(sliceValue, arraylen, 2, "")
sliceTyp := &types.Slice{Elt: typ.Elt}
runtimeTyp := c.types.ToRuntime(sliceTyp)
runtimeTyp = c.builder.CreatePtrToInt(runtimeTyp, c.target.IntPtrType(), "")
args := []llvm.Value{runtimeTyp, sliceValue, low, high}
result := c.builder.CreateCall(sliceslice, args, "")
llvmSliceTyp := c.types.ToLLVM(sliceTyp)
return c.NewLLVMValue(c.coerceSlice(result, llvmSliceTyp), sliceTyp)
case *types.Slice:
sliceslice := c.NamedFunction("runtime.sliceslice", "func f(t uintptr, s slice, low, high int32) slice")
i8slice := sliceslice.Type().ElementType().ReturnType()
sliceValue := value.LLVMValue()
sliceTyp := sliceValue.Type()
sliceValue = c.coerceSlice(sliceValue, i8slice)
runtimeTyp := c.types.ToRuntime(value.Type())
runtimeTyp = c.builder.CreatePtrToInt(runtimeTyp, c.target.IntPtrType(), "")
args := []llvm.Value{runtimeTyp, sliceValue, low, high}
result := c.builder.CreateCall(sliceslice, args, "")
return c.NewLLVMValue(c.coerceSlice(result, sliceTyp), value.Type())
case *types.Name: // String
stringslice := c.NamedFunction("runtime.stringslice", "func f(a string, low, high int32) string")
args := []llvm.Value{value.LLVMValue(), low, high}
result := c.builder.CreateCall(stringslice, args, "")
return c.NewLLVMValue(result, value.Type())
default:
panic("unimplemented")
}
panic("unreachable")
}
示例9: LLVMValue
func (v ConstValue) LLVMValue() llvm.Value {
typ := types.Underlying(v.Type())
switch typ {
case types.Int, types.Uint:
return llvm.ConstInt(llvm.Int32Type(), uint64(v.Int64()), true)
// TODO 32/64bit (probably wait for gc)
//int_val := v.Val.(*big.Int)
//if int_val.Cmp(maxBigInt32) > 0 || int_val.Cmp(minBigInt32) < 0 {
// panic(fmt.Sprint("const ", int_val, " overflows int"))
//}
//return llvm.ConstInt(v.compiler.target.IntPtrType(), uint64(v.Int64()), true)
case types.Uint:
return llvm.ConstInt(llvm.Int32Type(), uint64(v.Int64()), false)
case types.Int8:
return llvm.ConstInt(llvm.Int8Type(), uint64(v.Int64()), true)
case types.Uint8, types.Byte:
return llvm.ConstInt(llvm.Int8Type(), uint64(v.Int64()), false)
case types.Int16:
return llvm.ConstInt(llvm.Int16Type(), uint64(v.Int64()), true)
case types.Uint16:
return llvm.ConstInt(llvm.Int16Type(), uint64(v.Int64()), false)
case types.Int32, types.Rune:
return llvm.ConstInt(llvm.Int32Type(), uint64(v.Int64()), true)
case types.Uint32:
return llvm.ConstInt(llvm.Int32Type(), uint64(v.Int64()), false)
case types.Int64:
return llvm.ConstInt(llvm.Int64Type(), uint64(v.Int64()), true)
case types.Uint64:
return llvm.ConstInt(llvm.Int64Type(), uint64(v.Int64()), true)
case types.Float32:
return llvm.ConstFloat(llvm.FloatType(), float64(v.Float64()))
case types.Float64:
return llvm.ConstFloat(llvm.DoubleType(), float64(v.Float64()))
case types.UnsafePointer, types.Uintptr:
inttype := v.compiler.target.IntPtrType()
return llvm.ConstInt(inttype, uint64(v.Int64()), false)
case types.String:
strval := (v.Val).(string)
ptr := v.compiler.builder.CreateGlobalStringPtr(strval, "")
len_ := llvm.ConstInt(llvm.Int32Type(), uint64(len(strval)), false)
return llvm.ConstStruct([]llvm.Value{ptr, len_}, false)
case types.Bool:
if v := v.Val.(bool); v {
return llvm.ConstAllOnes(llvm.Int1Type())
}
return llvm.ConstNull(llvm.Int1Type())
}
panic(fmt.Errorf("Unhandled type: %v", typ)) //v.typ.Kind))
}
示例10: shift
func (lhs *LLVMValue) shift(rhs *LLVMValue, op token.Token) *LLVMValue {
rhs = rhs.Convert(lhs.Type()).(*LLVMValue)
lhsval := lhs.LLVMValue()
bits := rhs.LLVMValue()
unsigned := isUnsigned(lhs.Type())
if !bits.IsAConstant().IsNil() {
if bits.ZExtValue() >= uint64(lhsval.Type().IntTypeWidth()) {
var fix llvm.Value
if unsigned || op == token.SHL {
fix = llvm.ConstNull(lhsval.Type())
} else {
fix = llvm.ConstAllOnes(lhsval.Type())
}
return lhs.compiler.NewValue(fix, lhs.typ)
}
}
b := lhs.compiler.builder
var result llvm.Value
if op == token.SHL {
result = b.CreateShl(lhsval, bits, "")
} else {
if unsigned {
result = b.CreateLShr(lhsval, bits, "")
} else {
result = b.CreateAShr(lhsval, bits, "")
}
}
if bits.IsAConstant().IsNil() {
// Shifting >= the width of the lhs
// yields undefined behaviour, so we
// must generate runtime branching logic.
width := llvm.ConstInt(bits.Type(), uint64(lhsval.Type().IntTypeWidth()), false)
less := b.CreateICmp(llvm.IntULT, bits, width, "")
var fix llvm.Value
if unsigned || op == token.SHL {
fix = llvm.ConstNull(lhsval.Type())
} else {
fix = llvm.ConstAllOnes(lhsval.Type())
}
result = b.CreateSelect(less, result, fix, "")
}
return lhs.compiler.NewValue(result, lhs.typ)
}
示例11: UnaryOp
func (v *LLVMValue) UnaryOp(op token.Token) Value {
b := v.compiler.builder
switch op {
case token.SUB:
var value llvm.Value
if isFloat(v.typ) {
zero := llvm.ConstNull(v.compiler.types.ToLLVM(v.Type()))
value = b.CreateFSub(zero, v.LLVMValue(), "")
} else {
value = b.CreateNeg(v.LLVMValue(), "")
}
return v.compiler.NewValue(value, v.typ)
case token.ADD:
return v // No-op
case token.AND:
if typ, ok := v.typ.Underlying().(*types.Pointer); ok {
if typ.Elem().Underlying() == typ {
// Taking the address of a recursive pointer
// yields a value with the same type.
value := v.pointer.value
basetyp := value.Type().ElementType()
value = b.CreateBitCast(value, basetyp, "")
return v.compiler.NewValue(value, v.typ)
}
}
return v.pointer
case token.NOT:
value := b.CreateNot(v.LLVMValue(), "")
return v.compiler.NewValue(value, v.typ)
case token.XOR:
lhs := v.LLVMValue()
rhs := llvm.ConstAllOnes(lhs.Type())
value := b.CreateXor(lhs, rhs, "")
return v.compiler.NewValue(value, v.typ)
case token.ARROW:
value, _ := v.chanRecv(false)
return value
default:
panic(fmt.Sprintf("Unhandled operator: %s", op))
}
panic("unreachable")
}
示例12: convertI2V
// convertI2V converts an interface to a value.
func (v *LLVMValue) convertI2V(typ types.Type) (result, success Value) {
typptrType := llvm.PointerType(llvm.Int8Type(), 0)
runtimeType := v.compiler.types.ToRuntime(typ)
runtimeType = llvm.ConstBitCast(runtimeType, typptrType)
vval := v.LLVMValue()
builder := v.compiler.builder
ifaceType := builder.CreateExtractValue(vval, 0, "")
diff := builder.CreatePtrDiff(runtimeType, ifaceType, "")
zero := llvm.ConstNull(diff.Type())
predicate := builder.CreateICmp(llvm.IntEQ, diff, zero, "")
// If result is zero, then we've got a match.
end := llvm.InsertBasicBlock(builder.GetInsertBlock(), "end")
end.MoveAfter(builder.GetInsertBlock())
nonmatch := llvm.InsertBasicBlock(end, "nonmatch")
match := llvm.InsertBasicBlock(nonmatch, "match")
builder.CreateCondBr(predicate, match, nonmatch)
builder.SetInsertPointAtEnd(match)
matchResultValue := v.loadI2V(typ).LLVMValue()
builder.CreateBr(end)
builder.SetInsertPointAtEnd(nonmatch)
nonmatchResultValue := llvm.ConstNull(matchResultValue.Type())
builder.CreateBr(end)
builder.SetInsertPointAtEnd(end)
successValue := builder.CreatePHI(llvm.Int1Type(), "")
resultValue := builder.CreatePHI(matchResultValue.Type(), "")
successValues := []llvm.Value{llvm.ConstAllOnes(llvm.Int1Type()), llvm.ConstNull(llvm.Int1Type())}
successBlocks := []llvm.BasicBlock{match, nonmatch}
successValue.AddIncoming(successValues, successBlocks)
success = v.compiler.NewLLVMValue(successValue, types.Bool)
resultValues := []llvm.Value{matchResultValue, nonmatchResultValue}
resultBlocks := []llvm.BasicBlock{match, nonmatch}
resultValue.AddIncoming(resultValues, resultBlocks)
result = v.compiler.NewLLVMValue(resultValue, typ)
return result, success
}
示例13: mdNode
func (d *SubprogramDescriptor) mdNode(info *DebugInfo) llvm.Value {
return llvm.MDNode([]llvm.Value{
llvm.ConstInt(llvm.Int32Type(), llvm.LLVMDebugVersion+uint64(d.Tag()), false),
FileDescriptor(d.File).path(),
info.MDNode(d.Context),
llvm.MDString(d.Name),
llvm.MDString(d.DisplayName),
llvm.MDString(""), // mips linkage name
llvm.ConstInt(llvm.Int32Type(), uint64(d.Line), false),
info.MDNode(d.Type),
llvm.ConstNull(llvm.Int1Type()), // not static
llvm.ConstAllOnes(llvm.Int1Type()), // locally defined (not extern)
llvm.ConstNull(llvm.Int32Type()), // virtuality
llvm.ConstNull(llvm.Int32Type()), // index into a virtual function
info.MDNode(nil), // basetype containing the vtable pointer
llvm.ConstInt(llvm.Int32Type(), 0, false), // flags
llvm.ConstNull(llvm.Int1Type()), // not optimised
d.Function,
info.MDNode(nil), // Template parameters
info.MDNode(nil), // function declaration descriptor
llvm.MDNode(nil), // function variables
llvm.ConstInt(llvm.Int32Type(), uint64(d.ScopeLine), false), // Line number where the scope of the subprogram begins
})
}
示例14: NewConstValue
func (c *compiler) NewConstValue(v exact.Value, typ types.Type) *LLVMValue {
switch {
case v.Kind() == exact.Unknown:
// TODO nil literals should be represented more appropriately once the exact-package supports it.
llvmtyp := c.types.ToLLVM(typ)
return c.NewValue(llvm.ConstNull(llvmtyp), typ)
case isString(typ):
if isUntyped(typ) {
typ = types.Typ[types.String]
}
llvmtyp := c.types.ToLLVM(typ)
strval := exact.StringVal(v)
strlen := len(strval)
i8ptr := llvm.PointerType(llvm.Int8Type(), 0)
var ptr llvm.Value
if strlen > 0 {
init := llvm.ConstString(strval, false)
ptr = llvm.AddGlobal(c.module.Module, init.Type(), "")
ptr.SetInitializer(init)
ptr = llvm.ConstBitCast(ptr, i8ptr)
} else {
ptr = llvm.ConstNull(i8ptr)
}
len_ := llvm.ConstInt(c.types.inttype, uint64(strlen), false)
llvmvalue := llvm.Undef(llvmtyp)
llvmvalue = llvm.ConstInsertValue(llvmvalue, ptr, []uint32{0})
llvmvalue = llvm.ConstInsertValue(llvmvalue, len_, []uint32{1})
return c.NewValue(llvmvalue, typ)
case isInteger(typ):
if isUntyped(typ) {
typ = types.Typ[types.Int]
}
llvmtyp := c.types.ToLLVM(typ)
var llvmvalue llvm.Value
if isUnsigned(typ) {
v, _ := exact.Uint64Val(v)
llvmvalue = llvm.ConstInt(llvmtyp, v, false)
} else {
v, _ := exact.Int64Val(v)
llvmvalue = llvm.ConstInt(llvmtyp, uint64(v), true)
}
return c.NewValue(llvmvalue, typ)
case isBoolean(typ):
if isUntyped(typ) {
typ = types.Typ[types.Bool]
}
var llvmvalue llvm.Value
if exact.BoolVal(v) {
llvmvalue = llvm.ConstAllOnes(llvm.Int1Type())
} else {
llvmvalue = llvm.ConstNull(llvm.Int1Type())
}
return c.NewValue(llvmvalue, typ)
case isFloat(typ):
if isUntyped(typ) {
typ = types.Typ[types.Float64]
}
llvmtyp := c.types.ToLLVM(typ)
floatval, _ := exact.Float64Val(v)
llvmvalue := llvm.ConstFloat(llvmtyp, floatval)
return c.NewValue(llvmvalue, typ)
case typ == types.Typ[types.UnsafePointer]:
llvmtyp := c.types.ToLLVM(typ)
v, _ := exact.Uint64Val(v)
llvmvalue := llvm.ConstInt(llvmtyp, v, false)
return c.NewValue(llvmvalue, typ)
case isComplex(typ):
if isUntyped(typ) {
typ = types.Typ[types.Complex128]
}
llvmtyp := c.types.ToLLVM(typ)
floattyp := llvmtyp.StructElementTypes()[0]
llvmvalue := llvm.ConstNull(llvmtyp)
realv := exact.Real(v)
imagv := exact.Imag(v)
realfloatval, _ := exact.Float64Val(realv)
imagfloatval, _ := exact.Float64Val(imagv)
llvmre := llvm.ConstFloat(floattyp, realfloatval)
llvmim := llvm.ConstFloat(floattyp, imagfloatval)
llvmvalue = llvm.ConstInsertValue(llvmvalue, llvmre, []uint32{0})
llvmvalue = llvm.ConstInsertValue(llvmvalue, llvmim, []uint32{1})
return c.NewValue(llvmvalue, typ)
}
// Special case for string -> [](byte|rune)
if u, ok := typ.Underlying().(*types.Slice); ok && isInteger(u.Elem()) {
if v.Kind() == exact.String {
strval := c.NewConstValue(v, types.Typ[types.String])
return strval.Convert(typ).(*LLVMValue)
}
}
panic(fmt.Sprintf("unhandled: t=%s(%T), v=%v(%T)", c.types.TypeString(typ), typ, v, v))
}
示例15: BinaryOp
//.........这里部分代码省略.........
case isFloat(lhs.typ):
result = b.CreateFDiv(lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateSDiv(lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateUDiv(lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, lhs.typ)
case token.REM:
switch {
case isFloat(lhs.typ):
result = b.CreateFRem(lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateSRem(lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateURem(lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, lhs.typ)
case token.ADD:
if isFloat(lhs.typ) {
result = b.CreateFAdd(lhs.LLVMValue(), rhs.LLVMValue(), "")
} else {
result = b.CreateAdd(lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, lhs.typ)
case token.SUB:
if isFloat(lhs.typ) {
result = b.CreateFSub(lhs.LLVMValue(), rhs.LLVMValue(), "")
} else {
result = b.CreateSub(lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, lhs.typ)
case token.SHL, token.SHR:
return lhs.shift(rhs, op)
case token.EQL:
if isFloat(lhs.typ) {
result = b.CreateFCmp(llvm.FloatOEQ, lhs.LLVMValue(), rhs.LLVMValue(), "")
} else {
result = b.CreateICmp(llvm.IntEQ, lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, types.Typ[types.Bool])
case token.LSS:
switch {
case isFloat(lhs.typ):
result = b.CreateFCmp(llvm.FloatOLT, lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateICmp(llvm.IntSLT, lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateICmp(llvm.IntULT, lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, types.Typ[types.Bool])
case token.LEQ:
switch {
case isFloat(lhs.typ):
result = b.CreateFCmp(llvm.FloatOLE, lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateICmp(llvm.IntSLE, lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateICmp(llvm.IntULE, lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, types.Typ[types.Bool])
case token.GTR:
switch {
case isFloat(lhs.typ):
result = b.CreateFCmp(llvm.FloatOGT, lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateICmp(llvm.IntSGT, lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateICmp(llvm.IntUGT, lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, types.Typ[types.Bool])
case token.GEQ:
switch {
case isFloat(lhs.typ):
result = b.CreateFCmp(llvm.FloatOGE, lhs.LLVMValue(), rhs.LLVMValue(), "")
case !isUnsigned(lhs.typ):
result = b.CreateICmp(llvm.IntSGE, lhs.LLVMValue(), rhs.LLVMValue(), "")
default:
result = b.CreateICmp(llvm.IntUGE, lhs.LLVMValue(), rhs.LLVMValue(), "")
}
return lhs.compiler.NewValue(result, types.Typ[types.Bool])
case token.AND: // a & b
result = b.CreateAnd(lhs.LLVMValue(), rhs.LLVMValue(), "")
return lhs.compiler.NewValue(result, lhs.typ)
case token.AND_NOT: // a &^ b
rhsval := rhs.LLVMValue()
rhsval = b.CreateXor(rhsval, llvm.ConstAllOnes(rhsval.Type()), "")
result = b.CreateAnd(lhs.LLVMValue(), rhsval, "")
return lhs.compiler.NewValue(result, lhs.typ)
case token.OR: // a | b
result = b.CreateOr(lhs.LLVMValue(), rhs.LLVMValue(), "")
return lhs.compiler.NewValue(result, lhs.typ)
case token.XOR: // a ^ b
result = b.CreateXor(lhs.LLVMValue(), rhs.LLVMValue(), "")
return lhs.compiler.NewValue(result, lhs.typ)
default:
panic(fmt.Sprint("Unimplemented operator: ", op))
}
panic("unreachable")
}