ton-assembly
Version:
TON assembler and disassembler
102 lines • 3.26 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeBuilder = exports.MAX_CELL_BITS = void 0;
const core_1 = require("@ton/core");
exports.MAX_CELL_BITS = 1023;
/**
* Extended Builder class that stores additional debug information.
*/
class CodeBuilder extends core_1.Builder {
isDictionaryCell;
offset;
instructions = [];
subMappings = [];
dictionaryInfo = [];
debugSectionIds = [];
constructor(isDictionaryCell = false, offset = 0) {
super();
this.isDictionaryCell = isDictionaryCell;
this.offset = offset;
}
storeInstructionPrefix(value, bits, instr) {
this.instructions.push({ instr, offset: this.bits, debugSections: [...this.debugSectionIds] });
this.debugSectionIds = [];
return super.storeUint(value, bits);
}
addImplicitRet() {
const lastInstruction = this.instructions.at(-1);
if (!lastInstruction)
return;
// This implicit RET instruction is used as an anchor for all trailing DEBUGMARK instructions.
this.instructions.push({
instr: {
$: "RET",
loc: lastInstruction.instr.loc,
},
offset: lastInstruction.offset,
debugSections: [...this.debugSectionIds],
});
}
build() {
const cell = this.asCell();
return [
cell,
{
cell: cell.hash().toString("hex"),
instructions: this.instructions,
subMappings: this.subMappings,
dictionaryInfo: this.dictionaryInfo,
},
];
}
clearDebugSectionIds() {
this.debugSectionIds = [];
return this;
}
startDebugSection(id) {
this.debugSectionIds.push(id);
return this;
}
pushMappings(...mappings) {
this.subMappings.push(...mappings);
return this;
}
pushInstructions(...instructions) {
this.instructions.push(...instructions);
return this;
}
getDictionaryInfo() {
return this.dictionaryInfo;
}
pushDictionaryInfo(...info) {
this.dictionaryInfo.push(...info);
return this;
}
storeRefWithMapping([cell, mapping]) {
this.subMappings.push(mapping);
return super.storeRef(cell);
}
storeDictionaryDirect(dict) {
dict.storeDirect(this);
return this;
}
canFit(bits) {
const maxBits = exports.MAX_CELL_BITS - (this.isDictionaryCell ? this.offset : 0);
return this.bits + bits <= maxBits;
}
reinitFrom(other) {
// @ts-expect-error hack
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._bits = other._bits;
// @ts-expect-error hack
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
this._refs = other._refs;
this.instructions.push(...other.instructions);
this.subMappings.push(...other.subMappings);
this.dictionaryInfo.push(...other.dictionaryInfo);
this.debugSectionIds = [...other.debugSectionIds];
return this;
}
}
exports.CodeBuilder = CodeBuilder;
//# sourceMappingURL=builder.js.map