tealish icon indicating copy to clipboard operation
tealish copied to clipboard

Use frame pointer opcodes for functions - part 1

Open fergalwalsh opened this issue 3 years ago • 1 comments

AVM 8+ has support for frame pointers which allow for more efficient stack & slot usage for subroutines (functions in Tealish).

Tealish can generate Teal that uses proto at the start of function definitions instead of stores. frame_dig can be used to access the function args from the stack without having to store them in slots.

Teal example for reference:

pushbytes "ab"
pushbytes "cd"
// log(myfunc("ab", "cd"))
callsub myfunc
log
pushint 1
return


// myfunc(x: bytes, y: bytes) bytes:
myfunc:
// specify that our function takes 2 arguments and has one return value
proto 2 1 // internally puts the first 2 values from the stack into the frame
frame_dig -2 // first arg
frame_dig -1 // second arg
concat
retsub

fergalwalsh avatar Nov 28 '22 07:11 fergalwalsh

I now understand that we can use frame pointers with frame_dig & frame_bury for function locals too instead of scratch slots. This is will allow for safe recursive functions and negate the need to analyse the call graph to find a safe slot allocation. It is a more invasive change however so I suggest to tackle it as a separate issue after this one.

fergalwalsh avatar Dec 05 '22 14:12 fergalwalsh