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: abstract stack pushing/popping in compiler 'GenerateTargetTraversal' #218

Open
mr-zwets opened this issue Oct 14, 2024 · 0 comments
Labels
cashc-compiler Relates to the cashc compiler

Comments

@mr-zwets
Copy link
Member

A good refactor would be to abstract stack pushing/popping in compiler 'GenerateTargetTraversal.ts'

from June 2023 discussion:

I agree it's confusing. It might be good to abstract away the stack pushing/popping at some point actually, since it's been the cause of a few bugs in the past 😅

This whole piece of code is very very bad and I would hopefully not write it the same way if I were to do it again lmao. But hopefully this gives some insight into what is actually happening here and what you need to change. By the end of your execution you need to end up with '(value)' on top of the stack, and the rest of the stack being unaffected.

So to recap: this.stack keeps track of what the stack looks like if the compiled bytecode were to be executed up until then. this.pushToStack() pushes to this stack, and this.popFromStack() pops from it.

In the example above, we start with this stack:
1:

When we execute this.emit(hexToBin('a914')), this pushes a value to the stack, so we also call this.pushToStack('(value)') to account for that.

1:
2:

Then we call this.visit(node.parameters[0]), the end result of this "visit" will be a new value on the stack (this is pushed in the call to this.visit().

1: node.parameters[0]
2:
3:

Then we execute this.emit(Op.OP_CAT), this concats the top 2 stack elements together. Because I am lazy I don't call this.popFromStack() here. So we need to remember to call pop 1x later on.

1: + node.parameters[0]
2:

Then we execute this.emit(hexToBin('87')), pushing a value to the stack. We don't call pushToStack, which cancels out last time we didn't call pop.

1:
2: + node.parameters[0]
3:

Then we execute this.emit(Op.OP_CAT), concatenating again. We don't call pop, so we need to remember to call pop 1x later on.

1: + node.parameters[0] +
2:

Then we call this.popFromStack(2) (equivalent to 2x this.popFromStack()) and later outside the big if-statement we call this.pushToStack('(value)'). The first pop is to compensate for the one we skipped earlier, the second pop+push is to replace whatever was on top of the stack with '(value)'.

1:
2:

@mr-zwets mr-zwets added the cashc-compiler Relates to the cashc compiler label Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cashc-compiler Relates to the cashc compiler
Projects
None yet
Development

No branches or pull requests

1 participant