Skip to content

Commit

Permalink
better comments
Browse files Browse the repository at this point in the history
  • Loading branch information
christo committed Dec 19, 2024
1 parent 0d52e67 commit 96e6190
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions client/src/machine/asm/Disassembler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import * as R from "ramda";
import {LabelsComments} from "./asm.ts";
import {DisassemblyMeta} from "./DisassemblyMeta.ts";

/** Stateful translator of bytes to their parsed instruction line */
/**
* Stateful translator of bytes to their parsed instruction line
*/
class Disassembler {
originalIndex: number;
currentIndex: number;
Expand Down Expand Up @@ -45,8 +47,7 @@ class Disassembler {
* Starting from one offset, read count bytes at most. Only reads up to the end of the file.
* @param from must be 0+
* @param count must be 1+ (defaults to 1)
* @return the possibly empty actual bytes read.
* @private
* @return the possibly empty array of actual bytes read.
*/
readBytes(from: number, count = 1) {
const i1 = R.max(0, from);
Expand All @@ -55,11 +56,11 @@ class Disassembler {
}

/**
* Currently responsible for deciding which Instructionish should be constructed at the current index point
* Decides which {@link InstructionLike} should be constructed at the current index point
* and advances the index by the correct number of bytes.
*/
nextInstructionLine(): InstructionLike {
// some helper functions

const lc = this.mkPredefLabelsComments(this.currentAddress);

let instructionish: InstructionLike;
Expand Down Expand Up @@ -279,26 +280,32 @@ class Disassembler {
};

private maybeMkEdict(lc: LabelsComments) {
const edict = this.disMeta.getEdict(this.currentIndex);
if (edict !== undefined) {
return this.edictOrBust(edict, this.bytesLeftInFile(), lc);
// see if edict is declared for currentIndex
const declaredEdict: Edict<InstructionLike> | undefined = this.disMeta.getEdict(this.currentIndex);
if (declaredEdict !== undefined) {
// either create as specified or fallback/compromise if it won't fit
return this.edictOrBust(declaredEdict, this.bytesLeftInFile(), lc);
}
// no declared edict
return undefined;
}

private edictOrBust(edict: Edict<InstructionLike>, remainingBytes: number, lc: LabelsComments) {
// if the edict won't fit in the remaining bytes, just get its labels/comments and explain
/**
* If the given edict fits, create the
*/
private edictOrBust(edict: Edict<InstructionLike>, remainingBytes: number, lc: LabelsComments): InstructionLike | undefined {
// if we can't comply with edict in remaining bytes, make best effort
const edictWillFit = edict.length <= remainingBytes;
if (edictWillFit) {
this.currentIndex += edict.length;
return edict.create(this.fb);
} else {
// cannot comply
const elc = edict.create(this.fb).labelsComments;
const explainLc = elc.length() > 0 ? ` (preserving ${elc.length()} labels/comments)` : "";
lc.addComments(`End of file clashes with edict${explainLc}: '${edict.describe()}'`);
lc.merge(elc);
this.currentIndex += remainingBytes;
// fall through
}
return undefined;
}
Expand Down

0 comments on commit 96e6190

Please sign in to comment.