I could imagine it wouldn't be too hard to implement functions querying their parent's stack context, by adding a hidden parameter to these functions, which is always set to ebp when it's called, so the function could access to the locals.
It would need the compiler to accept a QueryContext flag behind a function definition (like it currently accepts the NoDebug or "Win32" flag) which would cause the compiler to keep in mind that the function has an additional hidden parameter, so that any access to the parameters using dword[ebp+offset] would result in dword[ebp+offset+4] to make sure that the hidden parameter isn't accessed instead of a normal one. If now any part of code inside the functions parent function calls this function, the compiler would add a push ebp statement just before the call. If the called function now needs to access to its parent's locals it just could do something like this:
mov eax,dword[ebp+8] ;load the hidden parameter
mov ebx,dword[eax-4] ;access the first local variable
So
Strict
Framework brl.blitz
Func1
Function Func1 ( )
Local Local1 = 5
Local Local2 = 7
Func2
Return Local1
Function Func2 ( ) QueryContext
Func3
Local1 :+ Local2
EndFunction
Function Func3 ( ) QueryContext
Local2 :+ 3
EndFunction
EndFunctioncould be compiled to
format MS COFF ;Strict
extrn ___bb_blitz_blitz ;Framework brl.blitz
public __bb_main
public _bb_Func1
section "code" code
__bb_main:
push ebp
mov ebp,esp
push ebx
cmp dword [_17],0
je _18
mov eax,0
pop ebx
mov esp,ebp
pop ebp
ret
_18:
mov dword [_17],1
call ___bb_blitz_blitz
call _bb_Func1 ;Func1
mov ebx,0
_3:
mov eax,ebx
pop ebx
mov esp,ebp
pop ebp
ret
_bb_Func1: ;Function Func1 ( )
push ebp
mov ebp,esp
sub esp,8
push ebx
mov dword [ebp-4],0
mov dword [ebp-8],0
mov dword [ebp-4],5 ;Local Local1 = 5
mov dword [ebp-8],7 ;Local Local2 = 7
push ebp
call _1 ;Func2
add esp,4
mov ebx,dword [ebp-4]
_5:
mov eax,ebx
pop ebx
mov esp,ebp
pop ebp
ret
_1: ;Function Func2 ( ) QueryContext
push ebp
mov ebp,esp
sub esp,4
push ebx
mov eax,dword [ebp+8]
mov dword [ebp-4],eax
push dword [ebp-4]
call _2 ;Func3
add esp,4
mov ecx,dword [ebp-4]
mov eax,dword [ebp-4]
mov edx,dword [eax+-4]
mov eax,dword [ebp-4]
add edx,dword [eax+-8] ;Local1 :+ Local2
mov dword [ecx+-4],edx
mov ebx,0
_8:
mov eax,ebx
pop ebx
mov esp,ebp
pop ebp
ret ;EndFunction
_2: ;Function Func3 ( ) QueryContext
push ebp
mov ebp,esp
sub esp,4
push ebx
mov eax,dword [ebp+8]
mov dword [ebp-4],eax
mov edx,dword [ebp-4]
mov eax,dword [ebp-4]
mov eax,dword [eax+-8]
add eax,3 ;Local2 :+ 3
mov dword [edx+-8],eax
mov ebx,0
_11:
mov eax,ebx
pop ebx
mov esp,ebp
pop ebp
ret ;EndFunction;EndFunction
section "data" data writeable align 8
align 4
_17:
dd 0
So it would be possible to add something like Grey Alien asked for.