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

Stack depth management for CALL blocks #403

Merged
merged 6 commits into from
Sep 24, 2022
Merged

Stack depth management for CALL blocks #403

merged 6 commits into from
Sep 24, 2022

Conversation

bobbinth
Copy link
Contributor

@bobbinth bobbinth commented Sep 17, 2022

This PR implements management of stack depth for CALL code blocks. The rules are as follows:

  • When a CALL operation is executed, stack depth is set to 16 (the overflow table stays unchanged). This means that the callee function has access only the top 16 stack items of the caller.
  • When a CALL block is ended, stack depth is set to its pre-call value. We also need to ensure that the callee did not leave any extra items in the overflow table. To do this, we enforce that stack depth must be exactly 16 when a CALL block ends.

This PR includes the following:

  • Refactoring of operand stack modules to streamline interfaces, rename constants (most notable MIN_STACK_DEPTH -> STACK_TOP_SIZE), and fixing a couple of trace generation bugs related to population of stack helper registers.
  • Implementing stack.start_context() and stack.restore_context() methods to facilitate starting/ending of execution contexts within a stack.
    • As an outcome of this, instantiation of the Process struct for test purposes was updated to ensure that during tests user operations are never executed at clk=0.
  • Decoder was updated to enter/exit execution contexts in operand stack when a CALL block starts/ends execution. Also, components related to block stack table management were updated.

This PR does not include updates to the docs describing changes to constraints etc.

@bobbinth bobbinth mentioned this pull request Sep 17, 2022
26 tasks
@bobbinth bobbinth force-pushed the mem-context branch 2 times, most recently from f6a1c1f to 31f825e Compare September 20, 2022 04:00
Base automatically changed from mem-context to next September 20, 2022 04:27
@bobbinth bobbinth force-pushed the stack-refactor branch 4 times, most recently from 41ddd96 to 9ef0c77 Compare September 21, 2022 06:37
@bobbinth bobbinth marked this pull request as ready for review September 21, 2022 06:54
@bobbinth bobbinth requested a review from grjte September 21, 2022 06:54
Copy link
Contributor

@grjte grjte left a comment

Choose a reason for hiding this comment

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

Looks good! I left a few small things inline related to clarity

core/src/lib.rs Show resolved Hide resolved
processor/src/decoder/mod.rs Outdated Show resolved Hide resolved
processor/src/decoder/block_stack.rs Outdated Show resolved Hide resolved
processor/src/decoder/aux_hints.rs Outdated Show resolved Hide resolved
processor/src/decoder/tests.rs Show resolved Hide resolved
processor/src/stack/overflow.rs Outdated Show resolved Hide resolved
Comment on lines +108 to +113
// if last_row_addr is ZERO, any rows in the overflow table are not accessible from the
// current context. Thus, we should not be able to remove them.
debug_assert_ne!(
self.last_row_addr, ZERO,
"overflow table is empty in the current context"
);
Copy link
Contributor

Choose a reason for hiding this comment

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

given that removing an overflow row when the table is actually empty will cause a failure, I wonder if this should be a regular assert instead of debug_assert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should actually never happen under normal execution because we call Overflow::pop() only from Stack::shift_left() function. And there, we remove an item from the overflow table only if the stack depth is > 16.

processor/src/stack/overflow.rs Outdated Show resolved Hide resolved
processor/src/stack/trace.rs Outdated Show resolved Hide resolved
processor/src/stack/tests.rs Outdated Show resolved Hide resolved
@bobbinth bobbinth merged commit d07ebba into next Sep 24, 2022
@bobbinth bobbinth deleted the stack-refactor branch September 24, 2022 07:08
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.

2 participants