Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FunC incorrectly return from condition in inline functions #370

Closed
EmelyanenkoK opened this issue May 17, 2022 · 0 comments
Closed

FunC incorrectly return from condition in inline functions #370

EmelyanenkoK opened this issue May 17, 2022 · 0 comments

Comments

@EmelyanenkoK
Copy link
Member

The following code

int f1(a) inline {
  if (a > -1) {
    return 3;
  } 
  return 2;
}

int main() {
  return 1 + f1(2); 
}

will be compiled into

PROGRAM{
  DECLPROC f1
  DECLPROC main
  f1 PROC:<{
    //  a
    -1 GTINT	//  _2
    IFJMP:<{	// 
      3 PUSHINT	//  _3=3
    }>	// 
    2 PUSHINT	//  _4=2
  }>
  main PROC:<{
    // 
    2 PUSHINT	//  _1=2
    f1 INLINECALLDICT	//  _2
    INC	//  _3
  }>
}END>c

and upon execution
<b b> 0 "some.fif" include <s <b b> runvm 2drop swap drop
will return 3 instead of 4.

This bug happens due to the fact that funC compiler inserts IFJMP for ifs with returns (in f1 in example above) and being inlined IFJMP jumps out not from the function it caused (f1) but from the function which inlined function with if (main).

There are two approaches to combat this behavior:

  1. detect on funC level that function is inlined and do not use IFJMPs in that case
  2. redefine INLINECALLDICT such it will wrap inline functions into PUSHCONT { ... } EXECUTE (this will, however, consume excessive space even for the cases when it is not necessary).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@EmelyanenkoK and others