private static void CreateDoCopyFunctionBody(BitcodeModule module , DataLayout layout , Function doCopyFunc , IStructType foo , GlobalVariable bar , GlobalVariable baz , Function copyFunc ) { var bytePtrType = module.Context.Int8Type.CreatePointerType( ); // create block for the function body, only need one for this simple sample var blk = doCopyFunc.AppendBasicBlock("entry"); // create instruction builder to build the body var instBuilder = new InstructionBuilder(blk); bool param0ByVal = copyFunc.Attributes[FunctionAttributeIndex.Parameter0].Contains(AttributeKind.ByVal); if (!param0ByVal) { // create a temp local copy of the global structure var dstAddr = instBuilder.Alloca(foo) .RegisterName("agg.tmp") .Alignment(layout.CallFrameAlignmentOf(foo)); var bitCastDst = instBuilder.BitCast(dstAddr, bytePtrType) .SetDebugLocation(25, 11, doCopyFunc.DISubProgram); var bitCastSrc = instBuilder.BitCast(bar, bytePtrType) .SetDebugLocation(25, 11, doCopyFunc.DISubProgram); instBuilder.MemCpy(module , bitCastDst , bitCastSrc , module.Context.CreateConstant(layout.ByteSizeOf(foo)) , ( int )layout.CallFrameAlignmentOf(foo) , false ).SetDebugLocation(25, 11, doCopyFunc.DISubProgram); instBuilder.Call(copyFunc, dstAddr, baz) .SetDebugLocation(25, 5, doCopyFunc.DISubProgram); } else { instBuilder.Call(copyFunc, bar, baz) .SetDebugLocation(25, 5, doCopyFunc.DISubProgram) .AddAttributes(FunctionAttributeIndex.Parameter0, copyFunc.Parameters[0].Attributes); } instBuilder.Return( ) .SetDebugLocation(26, 1, doCopyFunc.DISubProgram); }
private static void CreateCopyFunctionBody(BitcodeModule module , DataLayout layout , Function copyFunc , DIFile diFile , ITypeRef foo , DebugPointerType fooPtr , DIType constFooType ) { var diBuilder = module.DIBuilder; copyFunc.Parameters[0].Name = "src"; copyFunc.Parameters[1].Name = "pDst"; // create block for the function body, only need one for this simple sample var blk = copyFunc.AppendBasicBlock("entry"); // create instruction builder to build the body var instBuilder = new InstructionBuilder(blk); // create debug info locals for the arguments // NOTE: Debug parameter indices are 1 based! var paramSrc = diBuilder.CreateArgument(copyFunc.DISubProgram, "src", diFile, 11, constFooType, false, 0, 1); var paramDst = diBuilder.CreateArgument(copyFunc.DISubProgram, "pDst", diFile, 12, fooPtr.DIType, false, 0, 2); uint ptrAlign = layout.CallFrameAlignmentOf(fooPtr); // create Locals // NOTE: There's no debug location attached to these instructions. // The debug info will come from the declare intrinsic below. var dstAddr = instBuilder.Alloca(fooPtr) .RegisterName("pDst.addr") .Alignment(ptrAlign); bool param0ByVal = copyFunc.Attributes[FunctionAttributeIndex.Parameter0].Contains(AttributeKind.ByVal); if (param0ByVal) { diBuilder.InsertDeclare(copyFunc.Parameters[0] , paramSrc , new DILocation(module.Context, 11, 43, copyFunc.DISubProgram) , blk ); } instBuilder.Store(copyFunc.Parameters[1], dstAddr) .Alignment(ptrAlign); // insert declare pseudo instruction to attach debug info to the local declarations diBuilder.InsertDeclare(dstAddr, paramDst, new DILocation(module.Context, 12, 38, copyFunc.DISubProgram), blk); if (!param0ByVal) { // since the function's LLVM signature uses a pointer, which is copied locally // inform the debugger to treat it as the value by dereferencing the pointer diBuilder.InsertDeclare(copyFunc.Parameters[0] , paramSrc , diBuilder.CreateExpression(ExpressionOp.deref) , new DILocation(module.Context, 11, 43, copyFunc.DISubProgram) , blk ); } var loadedDst = instBuilder.Load(dstAddr) .Alignment(ptrAlign) .SetDebugLocation(15, 6, copyFunc.DISubProgram); var dstPtr = instBuilder.BitCast(loadedDst, module.Context.Int8Type.CreatePointerType( )) .SetDebugLocation(15, 13, copyFunc.DISubProgram); var srcPtr = instBuilder.BitCast(copyFunc.Parameters[0], module.Context.Int8Type.CreatePointerType( )) .SetDebugLocation(15, 13, copyFunc.DISubProgram); uint pointerSize = layout.IntPtrType(module.Context).IntegerBitWidth; instBuilder.MemCpy(module , dstPtr , srcPtr , module.Context.CreateConstant(pointerSize, layout.ByteSizeOf(foo), false) , ( int )layout.AbiAlignmentOf(foo) , false ).SetDebugLocation(15, 13, copyFunc.DISubProgram); instBuilder.Return( ) .SetDebugLocation(16, 1, copyFunc.DISubProgram); }