예제 #1
0
        public void WriteStrings()
        {
            // build the list of unique strings
            var strings = new SortedList <string, bool>();

            foreach (var offset in mPatcher.Offsets)
            {
                if (!strings.ContainsKey(offset.Key))
                {
                    strings.Add(offset.Key, false);
                }
            }

            // write the string table
            OffsetTable patcher = new OffsetTable(mWriter);

            foreach (string s in strings.Keys)
            {
                mPatcher.DefineOffset(s);
                mWriter.Write(Encoding.UTF8.GetBytes(s));
                mWriter.Write(0);
            }

            mPatcher.PatchOffsets();
        }
예제 #2
0
 private BytecodeGenerator(Compiler compiler, BinaryWriter writer,
                           OffsetTable functionPatcher, StringTable stringTable)
 {
     mCompiler        = compiler;
     mWriter          = writer;
     mFunctionPatcher = functionPatcher;
     mStrings         = stringTable;
     mJumpTable       = new JumpTable(this);
 }
예제 #3
0
        public void Save(Stream outputStream)
        {
            // save the file
            var writer      = new BinaryWriter(outputStream, Encoding.UTF8);
            var funcPatcher = new OffsetTable(writer);
            var strings     = new StringTable(writer);
            var exportTable = new OffsetTable(writer);

            // magic number
            writer.Write(Encoding.ASCII.GetBytes(new char[] { 'p', 'i', 'e', '!' }));

            // version
            writer.Write(new byte[] { 0, 0, 0, 0 });

            // export table
            // number of exported functions
            //### bob: hack temp. exports all functions
            int numFunctions = mCompiler.Functions.Functions.Count();

            writer.Write(numFunctions);
            foreach (Function function in mCompiler.Functions.Functions)
            {
                string uniqueName = function.UniqueName();

                strings.InsertOffset(uniqueName);
                exportTable.InsertOffset(uniqueName);
            }

            // code section
            foreach (Function function in mCompiler.Functions.Functions)
            {
                string uniqueName = function.UniqueName();

                exportTable.DefineOffset(uniqueName);
                BytecodeGenerator.Generate(mCompiler, writer, funcPatcher, strings, function);
            }

            // now wire up all of the function offsets to each other
            funcPatcher.PatchOffsets();
            exportTable.PatchOffsets();

            // strings
            strings.WriteStrings();
        }
예제 #4
0
        public void Save(Stream outputStream)
        {
            // save the file
            var writer = new BinaryWriter(outputStream, Encoding.UTF8);
            var funcPatcher = new OffsetTable(writer);
            var strings = new StringTable(writer);
            var exportTable = new OffsetTable(writer);

            // magic number
            writer.Write(Encoding.ASCII.GetBytes(new char[] { 'p', 'i', 'e', '!' }));

            // version
            writer.Write(new byte[] { 0, 0, 0, 0 });

            // export table
            // number of exported functions
            //### bob: hack temp. exports all functions
            int numFunctions = mCompiler.Functions.Functions.Count();
            writer.Write(numFunctions);
            foreach (Function function in mCompiler.Functions.Functions)
            {
                string uniqueName = function.UniqueName();

                strings.InsertOffset(uniqueName);
                exportTable.InsertOffset(uniqueName);
            }

            // code section
            foreach (Function function in mCompiler.Functions.Functions)
            {
                string uniqueName = function.UniqueName();

                exportTable.DefineOffset(uniqueName);
                BytecodeGenerator.Generate(mCompiler, writer, funcPatcher, strings, function);
            }

            // now wire up all of the function offsets to each other
            funcPatcher.PatchOffsets();
            exportTable.PatchOffsets();

            // strings
            strings.WriteStrings();
        }
예제 #5
0
        public void WriteStrings()
        {
            // build the list of unique strings
            var strings = new SortedList<string, bool>();
            foreach (var offset in mPatcher.Offsets)
            {
                if (!strings.ContainsKey(offset.Key)) strings.Add(offset.Key, false);
            }

            // write the string table
            OffsetTable patcher = new OffsetTable(mWriter);
            foreach (string s in strings.Keys)
            {
                mPatcher.DefineOffset(s);
                mWriter.Write(Encoding.UTF8.GetBytes(s));
                mWriter.Write(0);
            }

            mPatcher.PatchOffsets();
        }
예제 #6
0
        public static void Generate(Compiler compiler, BinaryWriter writer,
                                    OffsetTable functionPatcher, StringTable stringTable, Function function)
        {
            BytecodeGenerator generator = new BytecodeGenerator(compiler,
                                                                writer, functionPatcher, stringTable);

            functionPatcher.DefineOffset(function.UniqueName());

            //### bob: hack temp
            if (compiler.FunctionStarted != null)
            {
                compiler.FunctionStarted(function.UniqueName(), writer.BaseStream.Position);
            }

            writer.Write(function.NumLocals);

            function.Body.Bound.Accept(generator);

            generator.TranslateTailCall();
            generator.Write(OpCode.Return);
        }
예제 #7
0
 public StringTable(BinaryWriter writer)
 {
     mWriter  = writer;
     mPatcher = new OffsetTable(mWriter);
 }
예제 #8
0
 public StringTable(BinaryWriter writer)
 {
     mWriter = writer;
     mPatcher = new OffsetTable(mWriter);
 }