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

Require LACE_STACK=1 to use non-standard 'stack' instruction set extension #49

Merged
merged 8 commits into from
Nov 29, 2024

Conversation

dxrcy
Copy link
Contributor

@dxrcy dxrcy commented Nov 27, 2024

PUSH, POP, CALL, and RET instructions will now cause both an assembler error and a runtime panic if used without enabling the stack extension.
The stack extension can be enabled by setting the environment variable LACE_STACK=1.

Example:

main
    call hw_sub
    halt

hw_sub
    lea r0 hw
    puts
    rets

hw .stringz "Hello from the stack\n"

If ran with lace run stack.asm, this program will fail to assemble, with the following error:

Error: parse::stack_extension_not_enabled

  × Non-standard 'push' instruction used without 'stack' extension enabled
   ╭─[2:5]
 1 │ main
 2 │     call hw_sub
   ·     ──┬─
   ·       ╰── non-standard instruction
 3 │     lea r0 sr
   ╰────
  help: this instruction requires the non-standard 'stack' extension
        run with `LACE_STACK=1` to enable
        note: this identifier cannot be used as a label

If ran with LACE_STACK=1 lace run stack.asm, the program will assemble and run correctly.

If a program assembled (with the stack extension enabled), is then separately ran (without the extension enabled), the runtime will print the following error, and immediately halt:

You called a reserved instruction.
Note: Run with `LACE_STACK=1` to enable stack features.
Halting...

@dxrcy dxrcy marked this pull request as ready for review November 27, 2024 05:57
@rozukke
Copy link
Owner

rozukke commented Nov 28, 2024

I think instead of doing this in the parser, everything can be done in the check_instruction fn in the lexer, where the effects would be contained in that function entirely (disregarding the env infra, and might have to change return type to Result for a few fns)

@dxrcy
Copy link
Contributor Author

dxrcy commented Nov 29, 2024

I think instead of doing this in the parser, everything can be done in the check_instruction fn in the lexer, where the effects would be contained in that function entirely (disregarding the env infra, and might have to change return type to Result for a few fns)

I was originally going to do this but I assumed making the ident and check_instruction methods fallible was not preferable. Changed now.

Copy link
Owner

@rozukke rozukke left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@rozukke rozukke merged commit c7a1853 into rozukke:main Nov 29, 2024
3 checks passed
@dxrcy dxrcy deleted the stack-env-var branch November 29, 2024 04:20
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