corth-lang / Corth

A self-hosted stack based language like Forth
MIT License
7 stars 1 forks source link

Register allocation/reservation system #39

Open HuseyinSimsek7904 opened 7 months ago

HuseyinSimsek7904 commented 7 months ago

In C, register keyword can be used to hint to the compiler that the variable is used very heavily and should be stored in a register instead of local memory for optimization reasons. Same can be done in Corth. Instead of using a local memory, some heavily used variables can be directly stored in register. However, this requires several rewrites as there is currently no way of reserving or allocating registers.

Possible syntax:

// This is a very typical 'for-like' loop in Corth.
0 while dup len @64 < do let i in
  i src array64.get i dst array64.set
i end inc end drop

// Changing i into a register would optimize the program a lot.
register i int in
  0 set i
  while get i len @64 < do
    get i src array64.get get i dst array64.set
  get i inc set i end
  // It is very easy to detect 'get i inc set i' pattern and replace it with register increment instruction which would simplify 
  // it to only 1 CPU cycle.
end