xc-basic3 icon indicating copy to clipboard operation
xc-basic3 copied to clipboard

Feature Request: Add @ operator to SUB and FUNCTION arguments

Open orlof opened this issue 2 years ago • 5 comments

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.

orlof avatar Sep 03 '23 19:09 orlof

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

JJFlash-IT avatar Sep 03 '23 21:09 JJFlash-IT

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?

orlof avatar Sep 04 '23 04:09 orlof

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.

JJFlash-IT avatar Sep 04 '23 06:09 JJFlash-IT

I think there are multiple possible improvements here:

  • Allow allocating STATIC SUB/FUNCTION parameters on ZP, e.g. SUB example (param AS BYTE FAST) STATIC - note this won't reuse an existing address but assign a new one
  • Allow declaring an entire SUB/FUNCTION as FAST, meaning that everything (parameters, local vars and the return value) is allocated on ZP, e.g. SUB example (param AS BYTE) STATIC FAST
  • Allow explicit memory address for SUB/FUNCTION parameters, e.g. SUB example (param AS BYTE @$FF) STATIC - note that the address can be a 16-bit value as well

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 ()

neilsf avatar Sep 04 '23 07:09 neilsf

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.

JJFlash-IT avatar Sep 04 '23 14:09 JJFlash-IT