private void PatchJump(int offset) { // We adjust by two for the jump offset int jump = _Chunk.SizeCode - offset - 2; if (jump > ushort.MaxValue) { throw new CompilerException(Tokens.Peek(), "Too much code to jump over."); } _Chunk.WriteCodeAt(offset, (byte)((jump >> 8) & 0xff)); _Chunk.WriteCodeAt(offset + 1, (byte)(jump & 0xff)); }
private static void DoFixups(GearsChunk chunk, int origin, Func <GearsValue, int> makeConstant, Func <string, int> makeConstant2, List <LoxCompiler> fns) { foreach (LoxCompiler fn in fns) { int codeBase = chunk.SizeCode; chunk.WriteCode(fn._Chunk._Code, fn._Chunk._Lines, fn._Chunk.SizeCode); chunk.WriteCodeAt(origin + fn._OriginAddress, (byte)(codeBase >> 8)); chunk.WriteCodeAt(origin + fn._OriginAddress + 1, (byte)(codeBase & 0xff)); foreach (LoxCompilerFixup fixup in fn._FixupConstants) { GearsValue value = fn._Chunk.ReadConstantValue(fixup.Value); int constantFixup = makeConstant(value); // as fixup chunk.WriteCodeAt(codeBase + fixup.Address, (byte)(constantFixup >> 8)); chunk.WriteCodeAt(codeBase + fixup.Address + 1, (byte)(constantFixup & 0xff)); } foreach (LoxCompilerFixup fixup in fn._FixupStrings) { string value = fn._Chunk.Strings.ReadStringConstant(fixup.Value); int constantFixup = makeConstant2(value); // as fixup chunk.WriteCodeAt(codeBase + fixup.Address, (byte)(constantFixup >> 8)); chunk.WriteCodeAt(codeBase + fixup.Address + 1, (byte)(constantFixup & 0xff)); } DoFixups(chunk, codeBase, makeConstant, makeConstant2, fn._FixupFns); chunk.Compress(); } }