Example #1
0
        private static BitcodeModule CreateModule(Context ctx, TargetMachine machine, int magicNumber, string modulename = "test", string functionname = "main")
        {
            var module = ctx.CreateBitcodeModule(modulename);

            module.Layout = machine.TargetData;
            IrFunction main       = module.CreateFunction(functionname, ctx.GetFunctionType(ctx.Int32Type));
            BasicBlock entryBlock = main.AppendBasicBlock("entry");
            var        bldr       = new InstructionBuilder(entryBlock);

            bldr.Return(ctx.CreateConstant(magicNumber));
            return(module);
        }
Example #2
0
        private static void CreateDoCopyFunctionBody(BitcodeModule module
                                                     , IrFunction doCopyFunc
                                                     , IStructType foo
                                                     , GlobalVariable bar
                                                     , GlobalVariable baz
                                                     , IrFunction 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");

            Debug.Assert(doCopyFunc.DISubProgram != null, "Expected non null subProgram");

            // 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(module.Layout.CallFrameAlignmentOf(foo));

                instBuilder.SetDebugLocation(25, 11, doCopyFunc.DISubProgram);
                var bitCastDst = instBuilder.BitCast(dstAddr, bytePtrType);
                var bitCastSrc = instBuilder.BitCast(bar, bytePtrType);

                instBuilder.MemCpy(bitCastDst
                                   , bitCastSrc
                                   , module.Context.CreateConstant(module.Layout.ByteSizeOf(foo))
                                   , false
                                   );

                instBuilder.SetDebugLocation(25, 5, doCopyFunc.DISubProgram)
                .Call(copyFunc, dstAddr, baz);
            }
            else
            {
                instBuilder.SetDebugLocation(25, 5, doCopyFunc.DISubProgram)
                .Call(copyFunc, bar, baz)
                .AddAttributes(FunctionAttributeIndex.Parameter0, copyFunc.Parameters[0].Attributes);
            }

            instBuilder.SetDebugLocation(26, 1, doCopyFunc.DISubProgram)
            .Return( );
        }
Example #3
0
        private static void CreateCopyFunctionBody(BitcodeModule module
                                                   , IrFunction copyFunc
                                                   , DIFile diFile
                                                   , ITypeRef foo
                                                   , DebugPointerType fooPtr
                                                   , DIType constFooType
                                                   )
        {
            Debug.Assert(copyFunc.DISubProgram != null, "Expected function with a valid debug subprogram");
            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 = module.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.SetDebugLocation(15, 6, copyFunc.DISubProgram)
                            .Load(fooPtr, dstAddr)
                            .Alignment(ptrAlign);

            instBuilder.SetDebugLocation(15, 13, copyFunc.DISubProgram);
            var dstPtr = instBuilder.BitCast(loadedDst, module.Context.Int8Type.CreatePointerType( ));
            var srcPtr = instBuilder.BitCast(copyFunc.Parameters[0], module.Context.Int8Type.CreatePointerType( ));

            uint pointerSize = module.Layout.IntPtrType(module.Context).IntegerBitWidth;

            instBuilder.MemCpy(dstPtr
                               , srcPtr
                               , module.Context.CreateConstant(pointerSize, module.Layout.ByteSizeOf(foo), false)
                               , false
                               );
            instBuilder.SetDebugLocation(16, 1, copyFunc.DISubProgram)
            .Return( );
        }