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

std function call destroys the stack #4

Open
shhyou opened this issue Oct 24, 2015 · 3 comments
Open

std function call destroys the stack #4

shhyou opened this issue Oct 24, 2015 · 3 comments

Comments

@shhyou
Copy link
Contributor

shhyou commented Oct 24, 2015

In computation, intermediate results are kept on the stack, yet standard function calls assume space for arguments be reserved on the stack.

puts(printf("OK"))
; => OK2

However,

puts(1 +  printf("OK"))
; => ?
@jserv
Copy link
Contributor

jserv commented Oct 25, 2015

At present, rubi lacks of appropriate calling convention. @suhorng, would you like to revise the existing design? I assume the compatibility of Ruby.

@shhyou
Copy link
Contributor Author

shhyou commented Oct 29, 2015

Actually, that Rubi pushes its argument from left to right is fine and consistent. It's the intermix of standard library function calls (back into C) that mess up the stack. I'm not sure to what degree are we to refine Rubi prototype? The simplest way would be excluding standard function calls from expressions and regarding them as statements in Rubi syntax. Otherwise, we could change std function calls to

+ | sub esp, sizeof(int32_t)*5
  do {
    relExpr();
    | mov [esp + a], eax
    a += 4;
  } while (skip(','));
  | call dword [esi + offset]
+ | add esp, sizeof(int32_t)*5

Adapting Rubi functions to C calling conventions would have similar effect. Their argument counts are known anyway.

I can't think of a simple way to support variadic functions though. Either swaping arguments on the stack after all of them have been evaluated or counting argument number prior to code gen would be a little mess.

@jserv
Copy link
Contributor

jserv commented Oct 29, 2015

@suhorng :
For former Ruby, there is a package named after ffi to perform C calling convention over existing libraries. However, we don't really need similar mechanisms for rubi since only essential function calls should be supported at the moment. I would defer to your proposed prototype changes for further compatibility.

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