예제 #1
0
        public SymbolTable(SymbolTable parentTable, Scope scope)
        {
            m_parentTable = parentTable;

            switch (m_scope = scope)
            {
            case Scope.Global:
                StaticSet  = new HashSet <StaticSymbol>();
                InitSymbol = ArgsSymbol = null;
                break;

            case Scope.Struct:
            case Scope.Union:
                m_currentOffset = 0;
                break;

            case Scope.Function:
                m_currentOffset = FunctionHeaderSize;
                break;

            case Scope.Block:
                m_currentOffset = m_parentTable.m_currentOffset;
                break;
            }
        }
예제 #2
0
        public static void CompileSourceFile(FileInfo file)
        {
            FileInfo     sourceFile   = new FileInfo(file.FullName + ".c");
            Preprocessor preprocessor = new Preprocessor(sourceFile);

            GenerateIncludeFile(file, preprocessor.IncludeSet);

            FileInfo     pFile   = new FileInfo(file.FullName + ".p");
            StreamWriter pWriter = new StreamWriter(pFile.FullName);

            pWriter.Write(preprocessor.PreprocessedCode);
            pWriter.Close();

            byte[] byteArray =
                Encoding.ASCII.GetBytes(preprocessor.PreprocessedCode);
            MemoryStream memoryStream = new MemoryStream(byteArray);

            CCompiler_Main.Scanner scanner =
                new CCompiler_Main.Scanner(memoryStream);

            try {
                SymbolTable.CurrentTable = new SymbolTable(null, Scope.Global);
                //CCompiler_Main.Scanner.Path = sourceFile;
                CCompiler_Main.Scanner.Line = 2000;
                CCompiler_Main.Parser parser = new CCompiler_Main.Parser(scanner);
                Assert.Error(parser.Parse(), Message.Syntax_error);
            }
            catch (IOException ioException) {
                Assert.Error(false, ioException.StackTrace, Message.Syntax_error);
            }

            if (Start.Linux)
            {
                ISet <string> totalExternSet = new HashSet <string>();

                foreach (StaticSymbol staticSymbol in SymbolTable.StaticSet)
                {
                    StaticSymbolLinux staticSymbolLinux =
                        (StaticSymbolLinux)staticSymbol;
                    totalExternSet.UnionWith(staticSymbolLinux.ExternSet);
                }

                foreach (StaticSymbol staticSymbol in SymbolTable.StaticSet)
                {
                    totalExternSet.Remove(staticSymbol.UniqueName);
                }

                FileInfo assemblyFile = new FileInfo(file.FullName + ".asm");
                File.Delete(assemblyFile.FullName);
                StreamWriter streamWriter = new StreamWriter(assemblyFile.FullName);

                foreach (StaticSymbol staticSymbol in SymbolTable.StaticSet)
                {
                    if (!staticSymbol.UniqueName.Contains(Symbol.SeparatorId) &&
                        !staticSymbol.UniqueName.Contains(Symbol.NumberId))
                    {
                        streamWriter.WriteLine("\tglobal " + staticSymbol.UniqueName);
                    }
                }
                streamWriter.WriteLine();

                foreach (string externName in totalExternSet)
                {
                    streamWriter.WriteLine("\textern " + externName);
                }

                if (SymbolTable.InitSymbol != null)
                {
                    streamWriter.WriteLine("\tglobal _start");
                    streamWriter.WriteLine("\tglobal " + Linker.StackStart);
                }
                else
                {
                    streamWriter.WriteLine("\textern " + Linker.StackStart);
                }
                streamWriter.WriteLine();

                foreach (StaticSymbol staticSymbol in SymbolTable.StaticSet)
                {
                    StaticSymbolLinux staticSymbolLinux =
                        (StaticSymbolLinux)staticSymbol;

                    streamWriter.WriteLine();
                    foreach (string line in staticSymbolLinux.TextList)
                    {
                        streamWriter.WriteLine(line);
                    }
                }

                if (SymbolTable.InitSymbol != null)
                {
                    streamWriter.WriteLine();
                    streamWriter.WriteLine("section .data");
                    streamWriter.WriteLine(Linker.StackStart + ":\ttimes 1048576 db 0");
                }

                streamWriter.Close();
            }

            if (Start.Windows)
            {
                FileInfo     objectFile   = new FileInfo(file.FullName + ".obj");
                BinaryWriter binaryWriter =
                    new BinaryWriter(File.Open(objectFile.FullName, FileMode.Create));

                binaryWriter.Write(SymbolTable.StaticSet.Count);
                foreach (StaticSymbol staticSymbol in SymbolTable.StaticSet)
                {
                    staticSymbol.Write(binaryWriter);
                }

                binaryWriter.Close();
            }
        }