SuperH Registers Calling Convention

Ask anything your want about the 32X Mushroom programming.

Moderator: BigEvilCorporation

Post Reply
ob1
Very interested
Posts: 463
Joined: Wed Dec 06, 2006 9:01 am
Location: Aix-en-Provence, France

SuperH Registers Calling Convention

Post by ob1 » Wed Jul 04, 2012 7:29 am

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 :(

TapamN
Interested
Posts: 15
Joined: Mon Apr 25, 2011 1:05 am

Post by TapamN » Wed Jul 04, 2012 2:58 pm

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.

ElBarto
Very interested
Posts: 160
Joined: Wed Dec 13, 2006 10:29 am
Contact:

Post by ElBarto » Wed Jul 04, 2012 6:36 pm

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

Chilly Willy
Very interested
Posts: 2984
Joined: Fri Aug 17, 2007 9:33 pm

Post by Chilly Willy » Thu Jul 05, 2012 12:01 am

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.

ob1
Very interested
Posts: 463
Joined: Wed Dec 06, 2006 9:01 am
Location: Aix-en-Provence, France

Post by ob1 » Thu Jul 05, 2012 6:45 am


Post Reply