Project

General

Profile

Concerning the "copy" instruction

Added by Rochus Keller about 1 year ago

During the migration of the standard library, I encountered a problem that I can't solve.

I had only used the "copy" instruction in one of the ~200 test cases so far, and there the result of the instruction had no influence on the reported result. Only now have I come across a case where a struct is returned as a function result, for which two copy operations are required. I had to finish implementing and debugging the feature first anyway, but it's still not working properly.

Enclosed is the code example "struct_return.c" (together with the code and the other files required for reproduction), which presents the problem in a minimal context.

The expected output is

compiling struct_return.c
quot=33 rem=44
quot=33 rem=44
0

but I still get

compiling struct_return.c
quot=33 rem=44
quot=0 rem=0
1

and don't know what I should change to make it work. I also verified in the documentation that the third op is indeed the number of "octets" (not the number of "values" as in fill).

I didn't look at the generated machine code yet.


Replies (4)

RE: Concerning the "copy" instruction - Added by Florian Negele about 1 year ago

The copy inside the callee seems to pickup the wrong destination address. The first operand of copy ptr $fp + 8, ptr $fp - 8, ptr 8 should probably read ptr [$fp + 8] since you want the destination address that was pushed by the caller rather than the address of that pointer itself.

RE: Concerning the "copy" instruction - Added by Rochus Keller about 1 year ago

Wow, that indeed was the issue! Thanks so much for pointing out. I starred at this code last night and just didn't see it (getting old).

RE: Concerning the "copy" instruction - Added by Florian Negele about 1 year ago

Since you are already pushing a destination address, why not push the address of the actually assigned variable such that the copy after returning is not necessary? Also, since the callee returns a local variable, you could internally reassign its address to the destination address such that even the copy before returning becomes obsolete:

https://sandbox.openbrace.org/?source=ob&target=code&input=eJwdjTEOwjAUQ-f-U3iEgYo5UQdorKoSJJGVIHEALhA69PikTPbw_PxMoT6I8vlu3qy8MyFMEOekcMHusMbChQJj6ERWmhmqiIzT2UHeXjeh_dudyxptaOMON-HqbRBLVUTz1ufIXXDk8TbaD3dwHgE

RE: Concerning the "copy" instruction - Added by Rochus Keller about 1 year ago

Thanks for the hint. WIll look at it tonight.

    (1-4/4)