Beispiel #1
0
        public CodeGenVisitor(AST.Module module)
        {
            builder = LLVM.CreateBuilder();

            LLVM.InitializeX86Target();
            LLVM.InitializeX86TargetInfo();
            LLVM.InitializeX86TargetMC();

            new TypeVisitor(module);
            module.Accept(this);

            LLVM.DisposeBuilder(builder);
        }
Beispiel #2
0
        /// <summary>
        /// Creates all sub-instances for a module
        /// </summary>
        /// <param name="state">The state to use</param>
        /// <param name="module">The parent module</param>
        /// <returns></returns>
        private Instance.Module CreateAndRegisterInstance(ValidationState state, AST.Module module)
        {
            var modinstance      = new Instance.Module(module);
            var parentCollection = modinstance.Instances;

            using (var scope = state.StartScope(module, modinstance))
            {
                // TODO: Create module instances here
                foreach (var imp in module.Imports)
                {
                }

                foreach (var decl in module.Declarations)
                {
                    if (decl is AST.ConstantDeclaration cdecl)
                    {
                        var cref = new Instance.ConstantReference(cdecl);
                        scope.TryAddSymbol(cref.Name, cref, cdecl.Name);
                        parentCollection.Add(cref);
                    }
                    else if (decl is AST.EnumDeclaration edecl)
                    {
                        var e = new Instance.EnumTypeReference(edecl);
                        scope.TryAddSymbol(e.Name, e, edecl.Name);
                        using (state.StartScope(e))
                            CreateAndRegisterInstance(state, e);
                        parentCollection.Add(e);
                    }
                    else if (decl is AST.FunctionDefinition fdecl)
                    {
                        scope.TryAddSymbol(fdecl.Name.Name, fdecl, fdecl.Name);
                    }
                }

                // The entry-level network is not user-instantiated
                if (module == state.TopLevel.Module)
                {
                    modinstance.Instances.Add(
                        state.TopLevel.NetworkInstance =
                            CreateAndRegisterInstance(state, state.TopLevel.NetworkDeclaration, state.TopLevel.SourceNetwork)
                        );
                }
            }

            return(modinstance);
        }
Beispiel #3
0
        public void Visit(AST.Module module)
        {
            moduleLLVM = LLVM.ModuleCreateWithName(module.Name);
            var triple = Marshal.PtrToStringAnsi(LLVM.GetDefaultTargetTriple());

            LLVM.SetTarget(moduleLLVM, triple);

            builtIns = new BuiltInsLLVM(builder, moduleLLVM);

            mainFuncLLVM = LLVM.AddFunction(moduleLLVM, "main", LLVM.FunctionType(LLVM.Int32Type(), new LLVMTypeRef[] { }, false));

            var varBlock  = LLVM.AppendBasicBlock(mainFuncLLVM, "var");
            var codeBlock = LLVM.AppendBasicBlock(mainFuncLLVM, "begin");

            varBlocks.Push(varBlock);
            codeBlocks.Push(codeBlock);

            foreach (var statement in module.Statements)
            {
                statement.Accept(this);
                if (returns)
                {
                    break;
                }
            }
            LLVM.PositionBuilderAtEnd(builder, varBlock);
            LLVM.BuildBr(builder, codeBlock);

            if (!returns)
            {
                LLVM.PositionBuilderAtEnd(builder, codeBlocks.Peek());
                LLVM.BuildRet(builder, LLVM.ConstInt(LLVM.Int32Type(), 0, false));
            }

            codeBlocks.Pop();
            varBlocks.Pop();

            LLVM.WriteBitcodeToFile(moduleLLVM, $"{module.Name}.bc");
            LLVM.PrintModuleToFile(moduleLLVM, $"{module.Name}.ll", out var _);
        }
Beispiel #4
0
 /// <summary>
 /// Constructs a new module instance
 /// </summary>
 /// <param name="source">The module to instantiate</param>
 public Module(AST.Module source)
 {
     ModuleDefinition = source ?? throw new ArgumentNullException(nameof(source));
 }