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 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 (ri *indirectRetInfo) decode(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder, call llvm.Value) []llvm.Value { if ri.numResults == 1 { return []llvm.Value{builder.CreateLoad(ri.sretSlot, "")} } else { vals := make([]llvm.Value, ri.numResults) for i, _ := range vals { vals[i] = builder.CreateLoad(builder.CreateStructGEP(ri.sretSlot, i, ""), "") } return vals } }
func (ai *indirectArgInfo) decode(ctx llvm.Context, allocaBuilder llvm.Builder, builder llvm.Builder) llvm.Value { fn := builder.GetInsertBlock().Parent() return builder.CreateLoad(fn.Param(ai.argOffset), "") }