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();
        }
        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);
        }
Beispiel #3
0
 public JumpTable(BytecodeGenerator bytecode)
 {
     mBytecode = bytecode;
 }