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(); } }
internal int WriteConstantValue(GearsValue value) { CheckGrowConstantCapacity(1); int index = SizeConstant; _Constants[index] = (ulong)value; SizeConstant += 1; return(index); }
internal GearsValue ReadConstantValue(int offset) { if (offset < 0 || offset > SizeConstant) { return(-1); // todo: runtime error } GearsValue value = _Constants[offset]; return(value); }
/// <summary> /// Adds the given value to the chunk's constant table. /// Returns the index of that constant in the constant table. /// </summary> private int MakeValueConstant(GearsValue value) { for (int i = 0; i < _Chunk.SizeConstant; i++) { if ((ulong)_Chunk.ReadConstantValue(i) == (ulong)value) { return(i); } } int index = _Chunk.WriteConstantValue(value); if (index > short.MaxValue) { throw new CompilerException(Tokens.Peek(), "Too many constants in one chunk."); } return(index); }
public override void SetField(ulong name, GearsValue value) => _Fields.Set(name, value);
public override bool TryGetField(ulong name, out GearsValue value) => _Fields.TryGet(name, out value);
public abstract void SetField(ulong name, GearsValue value);
public abstract bool TryGetField(ulong name, out GearsValue value);
public GearsObjBoundMethod(GearsValue receiver, GearsObjFunction method) { Receiver = receiver; Method = method; }
public override bool TryGetField(ulong name, out GearsValue value) { return(_Wrapper.TryGetField(_Context, WrappedObject, name, out value)); }
public override void SetField(ulong name, GearsValue value) { _Wrapper.SetField(_Context, WrappedObject, name, value); }
internal string ReadConstantValueAsBitStr(int offset) { GearsValue value = ReadConstantValue(offset); return(BitString.GetBitStr((ulong)value)); }