Exemple #1
0
        public void CreateDebugLocation()
        {
            string        fileName  = Path.GetFileName("DIBuilder.c");
            string        directory = Path.GetDirectoryName(".");
            LLVMModuleRef module    = LLVMModuleRef.CreateWithName("netscripten");

            module.Target = "asmjs-unknown-emscripten";
            var             dIBuilder    = module.CreateDIBuilder();
            var             builder      = module.Context.CreateBuilder();
            LLVMMetadataRef fileMetadata = dIBuilder.CreateFile(fileName, directory);

            LLVMMetadataRef compileUnitMetadata = dIBuilder.CreateCompileUnit(
                LLVMDWARFSourceLanguage.LLVMDWARFSourceLanguageC,
                fileMetadata, "ILC", 0 /* Optimized */, String.Empty, 1, String.Empty,
                LLVMDWARFEmissionKind.LLVMDWARFEmissionFull, 0, 0, 0);

            module.AddNamedMetadataOperand("llvm.dbg.cu", compileUnitMetadata);

            LLVMMetadataRef functionMetaType = dIBuilder.CreateSubroutineType(fileMetadata,
                                                                              ReadOnlySpan <LLVMMetadataRef> .Empty, LLVMDIFlags.LLVMDIFlagZero);

            uint lineNumber    = 1;
            var  debugFunction = dIBuilder.CreateFunction(fileMetadata, "CreateDebugLocation", "CreateDebugLocation",
                                                          fileMetadata,
                                                          lineNumber, functionMetaType, 1, 1, lineNumber, 0, 0);
            LLVMMetadataRef currentLine =
                module.Context.CreateDebugLocation(lineNumber, 0, debugFunction, default(LLVMMetadataRef));

            LLVMTypeRef[] FooParamTys = { LLVMTypeRef.Int64, LLVMTypeRef.Int64, };
            LLVMTypeRef   FooFuncTy   = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int64, FooParamTys);
            LLVMValueRef  FooFunction = module.AddFunction("foo", FooFuncTy);

            var funcBlock = module.Context.AppendBasicBlock(FooFunction, "foo");

            builder.PositionAtEnd(funcBlock);
            builder.BuildRet(LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, 0));
            builder.CurrentDebugLocation = module.Context.MetadataAsValue(currentLine);
            var dwarfVersion = LLVMValueRef.CreateMDNode(new[]
            {
                LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 2), module.Context.GetMDString("Dwarf Version", 13),
                LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 4)
            });
            var dwarfSchemaVersion = LLVMValueRef.CreateMDNode(new[]
            {
                LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 2),
                module.Context.GetMDString("Debug Info Version", 18),
                LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 3)
            });

            module.AddNamedMetadataOperand("llvm.module.flags", dwarfVersion);
            module.AddNamedMetadataOperand("llvm.module.flags", dwarfSchemaVersion);
            dIBuilder.DIBuilderFinalize();

            module.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out string message);

            Assert.AreEqual("", message);
        }
Exemple #2
0
        public static LLVMValueRef AddFunction(this LLVMModuleRef module, LLVMTypeRef returnType, string name, LLVMTypeRef[] parameterTypes, Action <LLVMValueRef, LLVMBuilderRef> action)
        {
            var type    = LLVMTypeRef.CreateFunction(returnType, parameterTypes);
            var func    = module.AddFunction(name, type);
            var block   = func.AppendBasicBlock(string.Empty);
            var builder = module.Context.CreateBuilder();

            builder.PositionAtEnd(block);
            action(func, builder);
            return(func);
        }
        static void BuildFilterFunclet(LLVMModuleRef module, LLVMTypeRef[] funcletArgTypes)
        {
            LlvmFilterFunclet = module.AddFunction("LlvmFilterFunclet", LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32, funcletArgTypes, false));
            var            block          = LlvmFilterFunclet.AppendBasicBlock("Filter");
            LLVMBuilderRef funcletBuilder = Context.CreateBuilder();

            funcletBuilder.PositionAtEnd(block);

            LLVMValueRef filterResult = funcletBuilder.BuildCall(LlvmFilterFunclet.GetParam(0), new LLVMValueRef[] { LlvmFilterFunclet.GetParam(1) }, "callFilter");

            funcletBuilder.BuildRet(filterResult);
            funcletBuilder.Dispose();
        }
Exemple #4
0
        public void EnsureLinkage(ref Function function)
        {
            if (function.Fragment.Module == this)
            {
                return;
            }

            if (_foreignFunctions.TryGetValue(function, out var fn))
            {
                function = fn;
                return;
            }

            fn = new Function(function, LlvmModule.AddFunction(function.GetMangledName(), function.Type.LlvmType));
            _foreignFunctions.Add(function, fn);
            function = fn;
        }
Exemple #5
0
        private SNode Visit(SNode node)
        {
            LLVMTypeRef[] paramType = { };
            var           funcType  = LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, paramType);

            func  = mod.AddFunction("main", funcType);
            entry = func.AppendBasicBlock("entry");
            var body = func.AppendBasicBlock("body");

            blockStack.Add(entry);
            blockStack.Add(body);
            symbolTables.Add(new LLVMSymbolTable(symbolTables.Last()));
            builder.PositionAtEnd(body);
            Visit(node.CompoundStatement);
            builder.BuildRetVoid();
            builder.PositionAtEnd(entry);
            builder.BuildBr(body);
            return(node);
        }
Exemple #6
0
        static void BuildCatchFunclet(LLVMModuleRef module, string funcletName, LLVMTypeRef[] funcletArgTypes, bool passGenericContext = false)
        {
            LlvmCatchFunclet = module.AddFunction(funcletName, LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32,
                                                                                          funcletArgTypes, false));
            var            block          = LlvmCatchFunclet.AppendBasicBlock("Catch");
            LLVMBuilderRef funcletBuilder = Context.CreateBuilder();

            funcletBuilder.PositionAtEnd(block);

            List <LLVMValueRef> llvmArgs = new List <LLVMValueRef>();

            llvmArgs.Add(LlvmCatchFunclet.GetParam(2));
            if (passGenericContext)
            {
                llvmArgs.Add(LlvmCatchFunclet.GetParam(3));
            }
            LLVMValueRef leaveToILOffset = funcletBuilder.BuildCall(LlvmCatchFunclet.GetParam(1), llvmArgs.ToArray(), string.Empty);

            funcletBuilder.BuildRet(leaveToILOffset);
            funcletBuilder.Dispose();
        }
Exemple #7
0
        static void BuildFilterFunclet(LLVMModuleRef module, string funcletName, LLVMTypeRef[] funcletArgTypes, bool passGenericContext = false)
        {
            LlvmFilterFunclet = module.AddFunction(funcletName, LLVMTypeRef.CreateFunction(LLVMTypeRef.Int32,
                                                                                           funcletArgTypes, false));
            var            block          = LlvmFilterFunclet.AppendBasicBlock("Filter");
            LLVMBuilderRef funcletBuilder = Context.CreateBuilder();

            funcletBuilder.PositionAtEnd(block);

            List <LLVMValueRef> llvmArgs = new List <LLVMValueRef>();

            llvmArgs.Add(LlvmFilterFunclet.GetParam(2)); //shadow stack
            llvmArgs.Add(LlvmFilterFunclet.GetParam(0)); // exception object
            if (passGenericContext)
            {
                llvmArgs.Add(LlvmFilterFunclet.GetParam(3));
            }
            LLVMValueRef filterResult = funcletBuilder.BuildCall(LlvmFilterFunclet.GetParam(1), llvmArgs.ToArray(), string.Empty);

            funcletBuilder.BuildRet(filterResult);
            funcletBuilder.Dispose();
        }
Exemple #8
0
        static void BuildFinallyFunclet(LLVMModuleRef module)
        {
            LlvmFinallyFunclet = module.AddFunction("LlvmFinallyFunclet", LLVMTypeRef.CreateFunction(LLVMTypeRef.Void,
                                                                                                     new LLVMTypeRef[]
            {
                LLVMTypeRef.CreatePointer(LLVMTypeRef.CreateFunction(LLVMTypeRef.Void, new LLVMTypeRef[] { LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0) }, false), 0), // finallyHandler
                LLVMTypeRef.CreatePointer(LLVMTypeRef.Int8, 0),                                                                                                          // shadow stack
            }, false));
            var            block          = LlvmFinallyFunclet.AppendBasicBlock("GenericFinally");
            LLVMBuilderRef funcletBuilder = Context.CreateBuilder();

            funcletBuilder.PositionAtEnd(block);

            var finallyFunclet  = LlvmFinallyFunclet.GetParam(0);
            var castShadowStack = LlvmFinallyFunclet.GetParam(1);

            List <LLVMValueRef> llvmArgs = new List <LLVMValueRef>();

            llvmArgs.Add(castShadowStack);

            funcletBuilder.BuildCall(finallyFunclet, llvmArgs.ToArray(), string.Empty);
            funcletBuilder.BuildRetVoid();
            funcletBuilder.Dispose();
        }
Exemple #9
0
 public Value AddFunction(string name, Type functionTy) => _module.AddFunction(name, functionTy);