0

I am developing a naive programming language, effectively a C code generator, and for simplification, I want to represent all functions as with no return type in C, with the return type being passed as the first parameter, if any.

My question is whether this could present a compatibility or performance issue, I am not really sure what exactly is the difference, but common logic suggests that there is not much of a difference from the function assigning the returned value to the return address vs doing that manually and the address is a parameter. But maybe there is some additional work being done? Maybe different optimizations kick in?

  • Compatibility issue with *what*? – Kerrek SB Aug 08 '13 at 22:49
  • @KerrekSB - implementing certain features like different calling conventions, common optimizations and so on... Language and compiler features using no return function format. –  Aug 08 '13 at 22:52
  • Not sure if it is a duplicate, but a [similar question discusses return value vs parameter](http://stackoverflow.com/questions/5212794/pass-reference-to-output-location-vs-using-return) – Mark Wilkins Aug 08 '13 at 22:52
  • @MarkWilkins - so maybe return value optimization is under question? Considering I am keeping track of allocating the necessary temporaries that are targeted by the return address assignment. –  Aug 08 '13 at 22:54
  • @user2341104: That's how I interpreted it. In practice, though, I doubt there are very many situations where you would notice a difference. – Mark Wilkins Aug 08 '13 at 22:59

1 Answers1

1

For complex return types you're probably right, that there is no difference.

For simple return types, the compiler will return the result in a register, e.g. for

int foo(...);

but if you pass a pointer to result storage, like this:

void foo (int* result, ...);

it can't (at least not in the general case).

But let me suggest to worry about optimization later and ignore the possible performance impact. If you're not an experienced language designer, you'll have other problems to worry about. And later, dependent on you're design, you'll have other opportunities to optimization. In example: If you generate all C code into one file, you could let the c compiler do aggressive inlining - which AFAICS will remove the perfomance difference we just have been talking about.

M.E.L.
  • 613
  • 3
  • 8
  • The general idea is to JIT and dynamically load compiled code, but before that there is a stack based bytecode interpreter, which doesn't use registers, e.g. to represent the dynamic portion of the language. –  Aug 08 '13 at 22:57