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(); }
private BytecodeGenerator(Compiler compiler, BinaryWriter writer, OffsetTable functionPatcher, StringTable stringTable) { mCompiler = compiler; mWriter = writer; mFunctionPatcher = functionPatcher; mStrings = stringTable; mJumpTable = new JumpTable(this); }
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 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(); }
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); }
public StringTable(BinaryWriter writer) { mWriter = writer; mPatcher = new OffsetTable(mWriter); }