-
Notifications
You must be signed in to change notification settings - Fork 0
Description
We need to decide upon how we intend to allow jumping and conditional jumping/branching.
For reference, this document contains the instructions available on ARM: https://static.docs.arm.com/100898/0100/the_a64_Instruction_set_100898_0100.pdf
And this on x86: https://en.wikipedia.org/wiki/X86_instruction_listings
We obviously need a way to refer to somewhere to jump to; I think a label directive is an effective way to do this. It does't add syntax, and could just have the form:
label example
add something something_else
...
jump example
Here, jump example would jump to the add instruction.
We also need conditional branching. There are A LOT of conditions that these instruction sets support. With jump if 0, we can effectively implement everything else I think? Jump if equal is sub then jump if 0; jump if positive can be done with a bit mask then jump if 0; not equal would just jump over a jump if they are equal, etc. Then overflow checking can be done my comparing the operands before hand. Obviously none of these are as efficient as using the instructions, however it is an easy place to start and get things working - then we can add the different type of conditional jumps later.
So I propose that we just implement:
jumpz x example
which would jump to example if x is 0 for now.
Labels should be scoped, as otherwise you'd have to track which label names you've used which would get really messy.
Perhaps having an if directive could make writing code a lot nicer.
Consider the example where you want do thing_x if the condition is met, or thing_y if not.
You might have to write:
... something so that result will be 0 iff the condition is met.
jumpz result true
thing_y
jump aftercond
label true
thing_x
label aftercond
For this, the labels true and aftercond have to be used, which means that if this pattern becomes common, the programmer will have to be careful not to reuse labels in the same block - which is just messy!
To solve this, we could have some if ... else ... directive which has the form of this pattern. The syntax for this could be:
if result {
thing_y
} else {
thing_x
}
The above is also much more readable.