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") } }
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, args []llvm.Value) []llvm.Value { callArgs := make([]llvm.Value, len(fi.argAttrs)) 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 (fi *functionTypeInfo) invoke(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, callee llvm.Value, chain llvm.Value, args []llvm.Value, cont, lpad llvm.BasicBlock) []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.CreateInvoke(typedCallee, callArgs, cont, lpad, "") call.AddInstrAttribute(0, fi.retAttr) for i, a := range fi.argAttrs { call.AddInstrAttribute(i+1, a) } builder.SetInsertPointAtEnd(cont) return fi.retInf.decode(ctx, allocaBuilder, builder, call) }