Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: decoding error with constants #58

Merged
merged 6 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/ui/Code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ function DECODE_INSTRUCTION(encodedInstruction: string): [any[]] {
BigInt(encodedInstruction),
);

if (
instruction.Opcode === Opcodes.NOp &&
instruction.PcUpdate === PcUpdates.Regular
) {
return [["", "", toSignedInteger(encodedInstruction)]];
}

const dst: string =
`${instruction.DstRegister} ${instruction.DstOffset === 0 ? "" : (instruction.DstOffset > 0 ? "+ " : "- ") + Math.abs(instruction.DstOffset)}`.trim();
const op0: string =
`${instruction.Op0Register} ${instruction.Op0Offset === 0 ? "" : (instruction.Op0Offset > 0 ? "+ " : "- ") + Math.abs(instruction.Op0Offset)}`.trim();
const op1: string =
`${instruction.Op1Register} ${instruction.Op1Offset === 0 ? "" : (instruction.Op1Offset > 0 ? "+ " : "- ") + Math.abs(instruction.Op1Offset)}`.trim();
`${instruction.Op1Register === Op1Src.Op0 ? `[${op0}]` : instruction.Op1Register} ${instruction.Op1Offset === 0 ? "" : (instruction.Op1Offset > 0 ? "+ " : "- ") + Math.abs(instruction.Op1Offset)}`.trim();

let op: string;
switch (instruction.ResLogic) {
Expand Down Expand Up @@ -59,12 +52,16 @@ function DECODE_INSTRUCTION(encodedInstruction: string): [any[]] {
} catch (error) {
Logger.log(error);
if (error instanceof NonZeroHighBitError) {
return [["", "", BigInt(encodedInstruction) - PRIME]];
return [["", "", toSignedInteger(encodedInstruction)]];
}
return [[""]];
}
}

function TO_SIGNED_INTEGER(encodedInstruction: string): [any[]] {
return [["", "", toSignedInteger(BigInt(encodedInstruction)).toString(10)]];
}

/**
* Provides custom function for bitwise 'and' for given two inputs.
*
Expand Down
27 changes: 19 additions & 8 deletions src/ui/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ function showPicker() {
function loadProgram(program: any) {
programSheet.getRange("A2:G").clearContent();
const bytecode: string[] = program.data;
programSheet
.getRange(`A2:B${bytecode.length + 1}`)
.setValues(
bytecode.map((instruction, i) => [
instruction,
`=DECODE_INSTRUCTION(A${i + 2})`,
]),
);
let isConstant: boolean = false;
for (var i = 0; i < bytecode.length; i++) {
programSheet
.getRange(`A${i + 2}:B${i + 2}`)
.setValues([
[
bytecode[i],
isConstant
? `=TO_SIGNED_INTEGER(A${i + 2})`
: `=DECODE_INSTRUCTION(A${i + 2})`,
],
]);
if (!isConstant) {
isConstant = size(decodeInstruction(BigInt(bytecode[i]))) == 2;
} else {
isConstant = false;
}
}
Comment on lines +46 to +62
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when possible, I prefer not to use "stateful" for loops, ie loops where an iteration depends on the previous ones.

Suggested change
for (var i = 0; i < bytecode.length; i++) {
programSheet
.getRange(`A${i + 2}:B${i + 2}`)
.setValues([
[
bytecode[i],
isConstant
? `=TO_SIGNED_INTEGER(A${i + 2})`
: `=DECODE_INSTRUCTION(A${i + 2})`,
],
]);
if (!isConstant) {
isConstant = size(decodeInstruction(BigInt(bytecode[i]))) == 2;
} else {
isConstant = false;
}
}
programSheet
.getRange(`A2:B${bytecode.length + 1}`)
.setValues(
bytecode.map((instruction, i) => [
instruction,
i > 0 && size(decodeInstruction(BigInt(bytecode[i - 1]))) == 2 ? `=TO_SIGNED_INTEGER(A${i + 2})` : `=DECODE_INSTRUCTION(A${i + 2})`,
]),
);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

of course we compute two times decodeInstruction here so it's probably less efficient.


runSheet.getRange("A1:O").clearContent();
runSheet
.getRange(`${pcColumn}1:${executionColumn}1`)
Expand Down
Loading