neilsf / xc-basic3

A BASIC cross compiler for MOS 6502-based machines
MIT License
45 stars 5 forks source link

Feature Request: Add @ operator to SUB and FUNCTION arguments #243

Open orlof opened 1 year ago

orlof commented 1 year ago

Example:

SUB SomeTimeCriticalRoutine(Value AS BYTE @30) STATIC
  'Some fine code
END SUB

This would allow reuse of ZP addresses in multiple time critical subroutines.

JJFlash-IT commented 1 year ago

This would be quite useful even with normal addresses, so that one would be able to reuse memory among different subs and functions while keeping the speed of STATIC procedures

orlof commented 1 year ago

Even fancier way would be to allow renaming e.g.

DIM zp AS BYTE FAST
SUB SomeTimeCriticalRoutine(Value AS BYTE @zp) STATIC
  'Some fine code
END SUB

...but that might be too far from Basic's traditions?

JJFlash-IT commented 1 year ago

I fear that, most of all, it would allow for too messy code. Since parameter (argument) memory locations are bound to be rewritten with each sub/function call, I guess it would be fine to re-use memory, but I wouldn't mess up global (if not shared!) variables.

neilsf commented 1 year ago

I think there are multiple possible improvements here:

Note that all the above could work together with STATIC only as dynamic SUBS/FUNCTIONS always pass parameters and the return value using the Stack.

Even fancier way would be to allow renaming e.g.

Or you can simply use global variables to share values:

DIM zp AS BYTE FAST
SUB SomeTimeCriticalRoutine() STATIC
  'You can use zp here as it is global
END SUB
zp = 1 ' Assign value instead of passing parameter value
CALL SomeTimeCriticalRoutine ()
JJFlash-IT commented 1 year ago

Basically, argument/parameter variables would be at the same level of variables declared with DIM, which you can declare as FAST OR at a specific address. That I would like :) Yes, the compiler could give a warning and ignore the FAST or specific-address directives, or stop with an error if the sub/function is not declared STATIC.