flat assembler
Message board for the users of flat assembler.

flat assembler > Projects and Ideas > more interactive c compiler?

Goto page Previous  1, 2, 3
Author
Thread Post new topic Reply to topic
vivik



Joined: 29 Oct 2016
Posts: 405
Yet they have the concept of calling conventions, which is the same thing, just more limited.
Post 29 Apr 2018, 05:49
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 15910
Location: SDSS J140821.67+025733.2
vivik wrote:
Yet they have the concept of calling conventions, which is the same thing, just more limited.
Yes., some do expose the details. IIRC Java doesn't have them exposed. It is a bad failing of C (IMO) that is allows such things, breaking things when you actually try to use it across different platforms.
Post 29 Apr 2018, 06:16
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
crossplatform complicates things.
Post 29 Apr 2018, 07:49
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 302
Location: Belarus
vivik wrote:
Custom calling convention is needed for connecting assembly language and hll language properly. Just the need to push variables to stack (when you could just pass them in registers) kind of defeats the point of writing some function in assembly.

Pseudocode:

Code:
func do_something([eax] int par1, [ecx] int par2) : [eax] int res {
    res = par1 + par2
}    


If you specify that some registers are trashed by this function, they will be saved before entering the function and restored after this function (or maybe this register just wouldn't be used outside for some time, for 5 or more function calls in a row). If some registers are marked as preserved, they will be saved on entering the function (or somewhere in the middle), and restored at exiting it (or somewhere in the middle).

Ideally it will be the compiler who will decide a proper calling convention. But register allocation is a complex problem, probably only solvable by trial and error, and benchmarks.

A-a-and this is not custom calling convention. The example shows what is known as fastcall and is implemented like everywhere. I’m pretty sure Delphi uses it for internal subroutines by default, MSVC might do this as well.

Like you said, register allocation is quite complex. But compiler is just a program which is not wise by any means, only plain algorithms. Even if you got the means to specify custom registers to use it would most likely be compiled to additional mov’s at the beginning of a function to copy the values to the registers this particular compiler’s code generator prefers. No luck trying to win anything.
Post 29 Apr 2018, 16:36
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2292
Location: Usono (aka, USA)
This probably won't help much, but a guy on YouTube (Bisqwit) is a genius regarding things like this. His latest video is on a similar topic, if you're ultra curious.
Post 02 May 2018, 02:00
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
I'm a bit bitter that nobody donates anything. Why pay somebody to do something that he will do anyway.

@DimonSoft In fastcall you can use only 3 registers, while you potentially can use all 7 for passing data (esp still used for stack).

@rugxulo That's cool, I don't have to read gcc and clang sources now.
Post 10 May 2018, 17:01
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
hm...

Code:
GLOBAL(boolean)
jpeg_fill_bit_buffer (bitread_working_state *state,
                      register bit_buf_type get_buffer, register int bits_left,
                      int nbits)    


Look at "register" keyword. This does what I hope this does?
Post 14 May 2018, 06:36
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 302
Location: Belarus
vivik wrote:
@DimonSoft In fastcall you can use only 3 registers, while you potentially can use all 7 for passing data (esp still used for stack).

You wouldn’t be happy with that in general: the caller also needs to store its data somewhere between the calls. If it has to spit data from registers into memory to make use of your custom calling convention with 7 GPRs involved, you lose everything you might have won by using register-based calling convention.
Post 14 May 2018, 10:54
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
What if function returns 7 different values? This way, they will already be in registers ready to use, without any stack operations.
Post 14 May 2018, 13:41
View user's profile Send private message Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2292
Location: Usono (aka, USA)
vivik wrote:
Look at "register" keyword. This does what I hope this does?


Certainly you're aware of C's long-standing use of it. But modern compilers ignore the hint, figuring they know better. So it's mostly a no-op these days.
Post 15 May 2018, 01:01
View user's profile Send private message Visit poster's website Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
Code:
#include <stdio.h>

void test_reg(register int i) {
        i = 2;
}

int main() {
        int i = 1;
        printf("%i\n", i);
        test_reg(i);
        printf("%i\n", i);
        getchar();
}

//output:
//1
//1    


nevermind then, just brainfart
Post 15 May 2018, 15:32
View user's profile Send private message Reply with quote
Furs



Joined: 04 Mar 2016
Posts: 1201
BTW fastcall passes only 2 parameters in registers: ecx and edx. eax is not used and neither preserved.

The 3 parameters (eax, ecx, edx) is a GCC-specific custom calling convention (by custom I mean that it doesn't even have a name, you have to use regparm(3) attribute AFAIK, or it does it automatically if it determines all callers of the function (LTO or if it's with static linkage)).


But in respect to the topic, I don't think this is good idea for C. It's a language made to be compiled, not interpreted. Wink
Post 16 May 2018, 20:14
View user's profile Send private message Reply with quote
vivik



Joined: 29 Oct 2016
Posts: 405
edit and continue != interpreted. It merely redirects functions to newer versions of functions.
Post 16 May 2018, 20:56
View user's profile Send private message Reply with quote
DimonSoft



Joined: 03 Mar 2010
Posts: 302
Location: Belarus
Furs wrote:
BTW fastcall passes only 2 parameters in registers: ecx and edx. eax is not used and neither preserved.

The 3 parameters (eax, ecx, edx) is a GCC-specific custom calling convention (by custom I mean that it doesn't even have a name, you have to use regparm(3) attribute AFAIK, or it does it automatically if it determines all callers of the function (LTO or if it's with static linkage)).

fastcall is not a calling convention but a set of calling conventions that use registers for parameter passing, so it’s not really valid to talk about The Right Fastcall. Maybe the most common… But Delphi uses 3 registers for fastcall, so I’d say it’s not obvious which one is more common. It depends…
Post 18 May 2018, 09:22
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2, 3

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2018, Tomasz Grysztar.

Powered by rwasa.