Page 1 of 1

SuperH Registers Calling Convention

Posted: Wed Jul 04, 2012 7:29 am
by ob1
A little bit of ASM-to-C (et vice-versa) with this compilation of documents I've done, maybe it helps.

http://www.valpocl.com/SuperVDP/superh_ ... etion.html

PS : pity I couldn't insert HTML :(

Posted: Wed Jul 04, 2012 2:58 pm
by TapamN
Here's a minor thing I've seen missing on all SuperH calling convention docs I've seen. How functions that return a struct work, in something like this:

Code: Select all

struct thingy { int a,b,c; };

struct thingy InitThingy(int val)
{
     struct thingy newthing = { val, 0, val*2 };
     return newthing;
}

int main()
{
     struct thingy foo = InitThingy(10);
     // ...
}
Main will pass the address of foo to InitThingy in register R1. The value of val will be passed in R4 like normal.

I checked this in a disassembly of GCC output when generating SH4 code. I don't know if different compilers handle this differently; I haven't seen this documented in anything official.

The GCC code I disassembled built newthing on the stack, then called memcpy to copy it over to foo, but it seems like it would be more efficient just to write to foo directly...

I can't remember if InitThingy would set R0 to anything special when it returns.

Posted: Wed Jul 04, 2012 6:36 pm
by ElBarto
Rule #1 in C when using struct : Never pass a struct to a function
Rule #2 in C when using struct : Never return a struct to from function

Posted: Thu Jul 05, 2012 12:01 am
by Chilly Willy
ElBarto wrote:Rule #1 in C when using struct : Never pass a struct to a function
Rule #2 in C when using struct : Never return a struct to from function
Yeah, it's not part of the C spec, so everyone does something different. It's my main complaint about libdragon (SDK for N64 homebrew) - he uses struct passing for a number of functions, particularly the controllers.

Posted: Thu Jul 05, 2012 6:45 am
by ob1