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

Refactor Instruction type, catch more errors during transpilation #305

Merged
merged 4 commits into from
Jan 9, 2025

Conversation

poszu
Copy link
Collaborator

@poszu poszu commented Jan 8, 2025

Following up after #304, closes #18.

Refactored Instruction type to an enum with each instruction variant having exactly the operands it needs. Invalid instructions (wrong operand types, registers out of range etc.) are now caught during transpilation instead of runtime and generally, more invariants are guaranteed to be upheld.

@poszu poszu force-pushed the refactor-vm-instructions branch from ccd05c5 to 6116e23 Compare January 8, 2025 16:19
@poszu poszu force-pushed the refactor-vm-instructions branch from d93f649 to c404c64 Compare January 8, 2025 16:29
pub global_clk: u64,

/// The clock increments by 4 (possibly more in syscalls) for each instruction that has been
/// The gas counter increments by 4 (possibly more in syscalls) for each instruction that has been
Copy link
Collaborator

Choose a reason for hiding this comment

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

In case of clock I would understand that as instructions can take multiple clock ticks, but why we are keeping the same behavior for gas counter?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's the opposite in our situation. The clock (global_clk) ticks once per executed instruction. The gas counter, on the other hand, measures how much "work" has been done already and counts 4 per instruction + an extra cost of some syscalls (these are not "instructions").

Copy link
Collaborator

Choose a reason for hiding this comment

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

But why the cost of each instruction is 4 gas points? What is the reason to keep gas cost == ((number of executed instructions) >> 2) + sum(cost of called syscalls)?
Why it is 4 instead of (what seems to be more intuitive) 1, or instead of any randomly chosen other number?
That leads to the situation where syscall can cost less than any other instruction.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's an arbitrary number. The exact number doesn't matter; what matters is the cost of each instruction/syscall relative to each of the others. The value 4 seemed reasonable to me; it allows a little bit of flexibility if a "cheaper" instruction type were ever introduced in the future.

@poszu poszu force-pushed the refactor-vm-instructions branch from 9812c8d to 7c76746 Compare January 9, 2025 09:14
@poszu poszu force-pushed the refactor-vm-instructions branch from 7c76746 to 5f8631e Compare January 9, 2025 09:17
@poszu poszu enabled auto-merge January 9, 2025 09:37
@poszu poszu added this pull request to the merge queue Jan 9, 2025
Merged via the queue into main with commit 911b18b Jan 9, 2025
11 checks passed
@poszu poszu deleted the refactor-vm-instructions branch January 9, 2025 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Limit VM registers to 16 for RV32E
3 participants