> I guess that one could put the return value as the first one on the stack
> frame, so there is no copy involved. It seems to me that this is safe to do
> when one uses the special variable result. It depends on the calling
> convention, but I think it should be doable?
This is close to how most calling conventions return results larger than a
pointer. For many calling conventions, the caller of the function allocates the
storage for the result (usually from the stack) then passes a pointer to that
memory as a hidden argument to the called function. The called function then
writes to that memory upon returning.
Basically, it's like this:
type LargeObj = object
a: array[0..10, int]
b: string
proc foo(): LargeObj =
result.a[0] = 1
result.b = "hello"
return result
# The above becomes this when compiled:
proc foo(result: ptr LargeObj) =
result.a[0] = 1
result.b = "hello"
return