func (ri *directRetInfo) encode(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, vals []llvm.Value) { if len(ri.retTypes) == 0 { builder.CreateRetVoid() return } var val llvm.Value switch ri.numResults { case 1: val = vals[0] default: val = llvm.Undef(ri.resultsType) for i, v := range vals { val = builder.CreateInsertValue(val, v, i, "") } } args := make([]llvm.Value, len(ri.retTypes)) directEncode(ctx, allocaBuilder, builder, ri.retTypes, args, val) var retval llvm.Value switch len(ri.retTypes) { case 1: retval = args[0] default: retval = llvm.Undef(ctx.StructType(ri.retTypes, false)) for i, a := range args { retval = builder.CreateInsertValue(retval, a, i, "") } } builder.CreateRet(retval) }
func (t structBType) ToLLVM(c llvm.Context) llvm.Type { var lfields []llvm.Type for _, f := range t.fields { lfields = append(lfields, f.ToLLVM(c)) } return c.StructType(lfields, false) }
func (t floatBType) ToLLVM(c llvm.Context) llvm.Type { if t.isDouble { return c.DoubleType() } else { return c.FloatType() } }
func directDecode(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, valType llvm.Type, args []llvm.Value) llvm.Value { var alloca llvm.Value switch len(args) { case 0: return llvm.ConstNull(ctx.StructType(nil, false)) case 1: if args[0].Type().C == valType.C { return args[0] } alloca = allocaBuilder.CreateAlloca(valType, "") bitcast := builder.CreateBitCast(alloca, llvm.PointerType(args[0].Type(), 0), "") builder.CreateStore(args[0], bitcast) case 2: alloca = allocaBuilder.CreateAlloca(valType, "") var argTypes []llvm.Type for _, a := range args { argTypes = append(argTypes, a.Type()) } encodeType := ctx.StructType(argTypes, false) bitcast := builder.CreateBitCast(alloca, llvm.PointerType(encodeType, 0), "") builder.CreateStore(args[0], builder.CreateStructGEP(bitcast, 0, "")) builder.CreateStore(args[1], builder.CreateStructGEP(bitcast, 1, "")) default: panic("unexpected argTypes size") } return builder.CreateLoad(alloca, "") }
func (fi *functionTypeInfo) call(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, callee llvm.Value, chain llvm.Value, args []llvm.Value) []llvm.Value { callArgs := make([]llvm.Value, len(fi.argAttrs)) if chain.C == nil { chain = llvm.Undef(llvm.PointerType(ctx.Int8Type(), 0)) } callArgs[fi.chainIndex] = chain for i, a := range args { fi.argInfos[i].encode(ctx, allocaBuilder, builder, callArgs, a) } fi.retInf.prepare(ctx, allocaBuilder, callArgs) typedCallee := builder.CreateBitCast(callee, llvm.PointerType(fi.functionType, 0), "") call := builder.CreateCall(typedCallee, callArgs, "") call.AddInstrAttribute(0, fi.retAttr) for i, a := range fi.argAttrs { call.AddInstrAttribute(i+1, a) } return fi.retInf.decode(ctx, allocaBuilder, builder, call) }
func (t intBType) ToLLVM(c llvm.Context) llvm.Type { return c.IntType(t.width * 8) }
func (t ptrBType) ToLLVM(c llvm.Context) llvm.Type { return llvm.PointerType(c.Int8Type(), 0) }