gcc, modify registers as function args

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

gcc, modify registers as function args

Post by lemonyii »

hi,
i'm really considering my message passing performance as i posted before. i want to use something like L4, and want to make it easier and faster than my before one. in my previous one, i do like this:

Code: Select all

MSG msg;
msg.dest = dest;
msg.arg[n] = val[n];
...
msg.send();
as we all know, we have to put args into regs in assembly before a syscall, so we have to save regs(i mean those used for msg), put args, syscall(which will save/restore regs again), get return val, restore regs. that's a lot of work considering its frequency. so i want gcc to put args into right regs. then what we have to do is just

Code: Select all

send(dest, arg0,...);
then steps become syscall, get ret val. maybe twice or more faster at the entry.
and what's more, my message is just 8bytes x 8 , so it would be great to use r8-r15 on x64.

my problem is, how can i modify the regs used for functions with gcc? e.g. func(a,b,c,d) will results in r8=a,r9=b,r10=c?
thanks!
Enjoy my life!------A fish with a tattooed retina
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: gcc, modify registers as function args

Post by JamesM »

Hi,

When asking "how can I force a compiler to do X?", try to rethink your question into more of the form "how can I write my code such that the compiler has all the information it needs to do X?".

In your case, "send" would normally call "syscall", which may be in a different compilation unit so it can't be inlined. If you change your send function to something like:

Code: Select all

inline void send(X dest) {
    asm volatile("syscall" : : "r8"(dest));
}
inline void send(X dest, X arg0) {
    asm volatile("syscall" : : "r8"(dest), "r9"(arg0));
}
... etc ...
You get the idea. Embed the syscall assembler statement directly in your function body, and most importantly, define it in a header file, so GCC has access to the function's definition wherever it is called. Only then will it be able to inline, then the register allocator will work to your advantage.

I noticed you were using C++, so I named the functions the same, relying on C++'s function overloading to make your life easier - if you're using C, you could name them something like "send0", "send1", etc.

James
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: gcc, modify registers as function args

Post by lemonyii »

yeah! good idea! thank you! trying...
When asking "how can I force a compiler to do X?", try to rethink your question into more of the form "how can I write my code such that the compiler has all the information it needs to do X?".
that's what a mistake i've made since the beginning!
Enjoy my life!------A fish with a tattooed retina
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: gcc, modify registers as function args

Post by Owen »

Another thing you might think of is your clobber list. For example, you might specify that a lot of registers are clobbered by the system call, in order to avoid the kernel having to save and restore them. Of course, here you are balancing the cost of userspace saving and restoring the registers vs the cost of the kernel doing the same. My general preference is to have the the ABI specified callee-clobber registers be marked as clobbered; then the kernel only saves and restores exactly the registers it requires to do its job.

Of course, the kernel must make sure to zero all registers it isn't restoring or returning data in, to avoid leaking state to userspace. However, this is likely faster than restoring the original values, because it doesn't need to touch memory and can be perfectly parallelized.
User avatar
lemonyii
Member
Member
Posts: 153
Joined: Thu Mar 25, 2010 11:28 pm
Location: China

Re: gcc, modify registers as function args

Post by lemonyii »

yeah. but will it be harder to keep track of it? e.g. if i found a little mistake laterly, and i have to change some code, then i may ignore something like saving a register? or when i have a "DIV" instruction, i may forget to save edx.
so, it's not safe enough for me, as i'm always careless. thank you all the same!
Enjoy my life!------A fish with a tattooed retina
Post Reply