Re: question about linker

Liste des GroupesRevenir à cl c  
Sujet : Re: question about linker
De : cr88192 (at) *nospam* gmail.com (BGB)
Groupes : comp.lang.c
Date : 02. Dec 2024, 19:50:47
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vikvij$3go5h$1@dont-email.me>
References : 1 2 3 4 5 6
User-Agent : Mozilla Thunderbird
On 11/27/2024 4:52 AM, Thiago Adams wrote:
On 27/11/2024 06:53, David Brown wrote:
On 26/11/2024 20:42, Thiago Adams wrote:
On 26/11/2024 16:25, Bart wrote:
On 26/11/2024 19:11, Thiago Adams wrote:
On 26/11/2024 15:35, Thiago Adams wrote:
>
>
Then adding:
>
int strcmp();
>
int main() {
     strcmp("a", "b");
}
>
it works in C99 / C11
>
I think in C23 empty parameter list means no args, while in the previous versions (void) means no args.
>
Considering that in previous versions of C we could call a function without its signature I think the compiler only needs the caller side. (of course I am not considering programmer mistakes)
>
So, I think one extra simplification for small compilers is to ignore function parameters.
>
I don't think so. But you are welcome to look at godbolt.org and see for yourself. Try this for example:
>
>
>
Yes..I realized now I am wrong. Considering function calls uses registers I think the old C model works only when passing everything on stack.
>
>
>
No, it should work for other calling conventions too.  Passing everything on the stack has not been common practice for many decades, for most processor architectures.
>
What you have to consider here is the "default argument promotions".  If a function is defined to take a "double", and you call it using an int expression without using a function prototype, the result is UB (in a real and practical sense, not just hypothetically).  It doesn't matter if arguments are passed on the stack or registers, or if you have 32-bit or 64-bit or any other size of cpu (I don't know why Bart thinks that matters).  It is still a disaster.
>
If you want to write or generate code that calls a function, you need to know /exactly/ what type the parameters are.  And you need to call it with parameters of those types.  You can do that by having a function prototype and letting the compiler make the appropriate implicit conversions (assuming they are allowed by the language), or you can manually add any required conversions (such as casts) before the call, or you can rely on the default argument promotions if you know the result will be the correct type.
>
 Thanks for the comments. Very useful.
  
There is - to my knowledge - never a good reason for omitting a function prototype.  Implicit function declaration was IMHO one of the biggest design flaws in pre-standard C, and allowing it to continue in C90 after prototypes were added to the language, was a serious mistake.  Compilers should complain loudly if you try to call a function without a prototype declaration.  (I believe Bart's compiler treats it as a fatal error - it is a non-conformity of which I approve.)  And finally in C23 - some thirty years late - the standard finally requires proper prototypes.
>
I am not all sure why you are generating code C90 code here.  I don't think anyone much cares about using strict C90 other than a couple of people in this newsgroup.
 The objective is to leave all complications to a front end, and write a simpler C89 code that is used by the backend.
 The advantage, comparing with a custom IL, is that the code will works in any C compiler.
 
Another option could be:
   Front-end processes whatever language, producing an IR;
   IR happens to support C as one of the targets.
If you want to produce C as output, then the IL/IR stage can be hidden within the compiler (though, less useful in my case, as C was the input language, and the target was to produce machine code).
The choice of IR design is an open issue.
For backend code generation, it is usually better to have some sort of Three-Address-Code, with SSA (Static Single Assignment) being a popular variant (my compiler uses a sort of "pseudo SSA" in the backend, *1).
*1: There is a split between "variable identity" and "sequence number", where all assignments to a given named variable (or temporary) have the same identity, but a different sequence number, where the sequence number distinguishes different "versions" of the variable (and phi operations are semi-implicit).
Conceptually, a phi operations would exist every time a variable is referenced within a basic block without first having been assigned within that basic block, with a list of all sequence numbers for the variable which "flow into" this basic block.
In BGBCC, I ended up with a Stack-IL for the main IL stage:
   I did this first;
     So, inertia.
   Easier to produce from a language frontend;
   Easier to serialize and reload;
     SSA IR's are generally more complicated to work with here.
   Fairly easy to convert into SSA form on decoding;
     Backend works using 3AC/SSA.
   ...
Unlike free-form stack-languages (like Forth), generally in such a stack IR, the stack is always empty when branching or landing on a label (the .NET IL has a similar restriction). The stack doesn't actually push "values" so much as "the identity of the variable in question".
Stack manipulation operations like swapping stack items are essentially free.
Though, operations like DUP have two variants:
   One duplicates the identity of the variable on the stack;
   The other pushes a temporary holding a copy of the value.
Typically, stack operations do not encode type, as type information is carried along with the stack items (when converted to 3AC, each 3AC operator does have an associated type though).
One partial downside (vs a fully 3AC IL), is that the stack may impose extra awkwardness in some cases, and optimizations like "eliminating common sub-expressions" are harder to express in the front-end as then it needs to manage temporary values to some extent anyways (partially diminishing the simplicity advantage of a Stack-IL for the front-end).
Still better at the IL stage to use/ if-goto and labels as the primary abstraction, as structured constructs (like for or while loops) would make things much more of a pain.
...
There are a few differences, from JVM or .NET:
Function calls also involve pushing a "mark" onto the stack, which expresses the total argument count;
Arguments are evaluated/pushed in right-to-left order (partly for consistency with MS-DOS era compilers).
My existing format is also kind of crufty, but inertia has kept it as it is (and, in my case, is also mostly used for static libraries).
...
If designing an IR, it could possibly also make sense to make a simplified BASIC-like language:
   c = a + b
   c = func ( a , b )
   ...
While still leaving things like type-propagation and sequence numbering as internal (regenerated when the IR is loaded), in contrast to something like LLVM IR (which would mildly simplify the IR parser at the cost of making everything else more complicated).
Also, ideally, the binary representation of the IR should be defined in terms of a representation of the constructs in the IR, and *not* some scheme for binary serializing all of the structures that make up the IR internally.
Maybe also resist the temptation to be overly concerned with space used for the IR. Can probably use bytes and VLN, but avoid making the encoding overly complicated.
Say:
If c=a+b takes 4 or 5 bytes to encode when it could have been bit-twiddled down to 3 bytes, maybe better to stay with 4 or 5, say:
   Opcode C A B
   Opcode SubOp C A B
   ADD    C A B
   SUB    C A B
   ...
   CALL   C N Arg0 Arg1 .. ArgN-1
   LABEL  LBLID
   GOTO   LBLID
   BEQ    LBLID A B  //if(a==b)goto LBLID;
   ...
With each using a VLN scheme, say:
   00000000..0000007F: 1 byte
   00000080..00003FFF: 2 bytes
   00004000..001FFFFF: 3 bytes
   ...
Though, since opcodes are usually small numbers, one can maybe indulge:
   0000..00EF: 1 byte
   00F0..0FFF: 2 byte
   1000..7FFF: 3 byte
Increasing the range of single-byte opcodes at the expense of larger valued numbers (where, 2-byte encodings overlapping with the 1-byte range encode the 3 byte range; and/or not bother, reasoning that one is not going to have that many opcodes).
And, for signed values, say:
   uvl=(val<<1)^(val>>63);  // fold sign into LSB
   ...
Variables are encoded as an index into an array of variables, then, say:
   VAR    A T    //Creates local variable A with a type given in T
   VARI   A T V  //creates and initializes a variable
   LITI   A T V  //declare a local constant or literal value.
   VARG   A T GV //declare a reference to a Global Variable (Index|Name)
   ARG    A T V  //declare a function argument (V = index in arg list).
V is the value given as a VLN
   How V is interpreted depends on the type.
GV Global Variable
T, Type:
Types and string literals could be given as an offset into a strings table. Within a function, the variables list would be declared before the first non-variable in the function.
...

Date Sujet#  Auteur
26 Nov 24 * question about linker382Thiago Adams
26 Nov 24 +* Re: question about linker16Thiago Adams
26 Nov 24 i`* Re: question about linker15Bart
26 Nov 24 i `* Re: question about linker14Thiago Adams
27 Nov 24 i  +* Re: question about linker2BGB
27 Nov 24 i  i`- Re: question about linker1Bart
27 Nov 24 i  +* Re: question about linker5David Brown
27 Nov 24 i  i`* Re: question about linker4Thiago Adams
27 Nov 24 i  i +* Re: question about linker2David Brown
27 Nov 24 i  i i`- Re: question about linker1Thiago Adams
2 Dec 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker6Michael S
27 Nov 24 i   `* Re: question about linker5Thiago Adams
27 Nov 24 i    `* Re: question about linker4Michael S
27 Nov 24 i     +- Re: question about linker1David Brown
28 Nov 24 i     +- Re: question about linker1Tim Rentsch
2 Dec 24 i     `- Re: question about linker1BGB
26 Nov 24 +* Re: question about linker20Bart
26 Nov 24 i`* Re: question about linker19Thiago Adams
26 Nov 24 i `* Re: question about linker18Bart
27 Nov 24 i  +* Re: question about linker3BGB
27 Nov 24 i  i`* Re: question about linker2fir
27 Nov 24 i  i `- Re: question about linker1BGB
27 Nov 24 i  `* Re: question about linker14Bart
27 Nov 24 i   +* Re: question about linker12Thiago Adams
27 Nov 24 i   i+- Re: question about linker1Thiago Adams
27 Nov 24 i   i`* Re: question about linker10Bart
27 Nov 24 i   i +* Re: question about linker6Bart
27 Nov 24 i   i i`* Re: question about linker5Thiago Adams
27 Nov 24 i   i i +* Re: question about linker3Thiago Adams
27 Nov 24 i   i i i`* Re: question about linker2Thiago Adams
27 Nov 24 i   i i i `- Re: question about linker1Bart
27 Nov 24 i   i i `- Re: question about linker1Bart
27 Nov 24 i   i `* Re: question about linker3Thiago Adams
27 Nov 24 i   i  `* Re: question about linker2Bart
27 Nov 24 i   i   `- Re: question about linker1Thiago Adams
28 Nov 24 i   `- Re: question about linker1Tim Rentsch
27 Nov 24 `* Re: question about linker345Waldek Hebisch
27 Nov 24  `* Re: question about linker344Thiago Adams
28 Nov 24   `* Re: question about linker343Keith Thompson
28 Nov 24    `* Re: question about linker342Thiago Adams
28 Nov 24     +* Re: question about linker337Bart
28 Nov 24     i`* Re: question about linker336Keith Thompson
29 Nov 24     i `* Re: question about linker335Bart
29 Nov 24     i  `* Re: question about linker334Keith Thompson
29 Nov 24     i   `* Re: question about linker333Bart
29 Nov 24     i    +* Re: question about linker3Keith Thompson
29 Nov 24     i    i`* Re: question about linker2Bart
29 Nov 24     i    i `- Re: question about linker1Keith Thompson
29 Nov 24     i    `* Re: question about linker329David Brown
29 Nov 24     i     `* Re: question about linker328Bart
29 Nov 24     i      +- Re: question about linker1Ike Naar
29 Nov 24     i      +* Re: question about linker325Michael S
29 Nov 24     i      i+* Re: question about linker322Bart
29 Nov 24     i      ii`* Re: question about linker321Michael S
29 Nov 24     i      ii +* Re: question about linker319David Brown
29 Nov 24     i      ii i`* Re: question about linker318Bart
29 Nov 24     i      ii i +* Re: question about linker164Keith Thompson
29 Nov 24     i      ii i i`* Re: question about linker163Bart
30 Nov 24     i      ii i i `* Re: question about linker162Keith Thompson
30 Nov 24     i      ii i i  +* Re: question about linker95Waldek Hebisch
30 Nov 24     i      ii i i  i+- Re: question about linker1Keith Thompson
30 Nov 24     i      ii i i  i+* Re: question about linker3James Kuyper
30 Nov 24     i      ii i i  ii`* Re: question about linker2Michael S
3 Dec 24     i      ii i i  ii `- Re: question about linker1Tim Rentsch
1 Dec 24     i      ii i i  i`* Re: question about linker90David Brown
1 Dec 24     i      ii i i  i +* Re: question about linker88Bart
1 Dec 24     i      ii i i  i i`* Re: question about linker87David Brown
1 Dec 24     i      ii i i  i i `* Re: question about linker86Bart
2 Dec 24     i      ii i i  i i  `* Re: question about linker85David Brown
2 Dec 24     i      ii i i  i i   `* Re: question about linker84Bart
2 Dec 24     i      ii i i  i i    +* Re: question about linker78David Brown
2 Dec 24     i      ii i i  i i    i+* Re: question about linker72Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii+* Re: question about linker70Bart
2 Dec 24     i      ii i i  i i    iii+* Re: question about linker68David Brown
2 Dec 24     i      ii i i  i i    iiii`* Re: question about linker67Bart
3 Dec 24     i      ii i i  i i    iiii `* Re: question about linker66David Brown
3 Dec 24     i      ii i i  i i    iiii  +* Re: question about linker53Bart
3 Dec 24     i      ii i i  i i    iiii  i`* Re: question about linker52David Brown
3 Dec 24     i      ii i i  i i    iiii  i `* Re: question about linker51Bart
4 Dec 24     i      ii i i  i i    iiii  i  `* Re: question about linker50David Brown
4 Dec 24     i      ii i i  i i    iiii  i   `* Re: question about linker49Bart
4 Dec 24     i      ii i i  i i    iiii  i    `* Re: question about linker48David Brown
4 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker24Bart
5 Dec 24     i      ii i i  i i    iiii  i     i`* Re: question about linker23David Brown
5 Dec 24     i      ii i i  i i    iiii  i     i +- Re: question about linker1Janis Papanagnou
5 Dec 24     i      ii i i  i i    iiii  i     i `* Re: question about linker21Bart
6 Dec 24     i      ii i i  i i    iiii  i     i  `* Re: question about linker20David Brown
6 Dec 24     i      ii i i  i i    iiii  i     i   `* Re: question about linker19Bart
6 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker5Ike Naar
6 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i+- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker2Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `- Re: question about linker1Keith Thompson
7 Dec 24     i      ii i i  i i    iiii  i     i    +* Re: question about linker10David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i`* Re: question about linker9Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i `* Re: question about linker8David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i  `* Re: question about linker7Bart
7 Dec 24     i      ii i i  i i    iiii  i     i    i   `* Re: question about linker6David Brown
7 Dec 24     i      ii i i  i i    iiii  i     i    i    `* Re: question about linker5Bart
8 Dec 24     i      ii i i  i i    iiii  i     i    i     +* Re: question about linker3Ben Bacarisse
8 Dec 24     i      ii i i  i i    iiii  i     i    i     `- Re: question about linker1David Brown
8 Dec 24     i      ii i i  i i    iiii  i     i    `* Re: question about linker3Waldek Hebisch
5 Dec 24     i      ii i i  i i    iiii  i     +* Re: question about linker15Waldek Hebisch
11 Dec 24     i      ii i i  i i    iiii  i     `* Re: question about linker8James Kuyper
3 Dec 24     i      ii i i  i i    iiii  `* Re: question about linker12Bart
3 Dec 24     i      ii i i  i i    iii`- Re: question about linker1Janis Papanagnou
2 Dec 24     i      ii i i  i i    ii`- Re: question about linker1Bart
2 Dec 24     i      ii i i  i i    i`* Re: question about linker5Bart
4 Dec 24     i      ii i i  i i    `* Re: question about linker5Waldek Hebisch
1 Dec 24     i      ii i i  i `- Re: question about linker1Janis Papanagnou
30 Nov 24     i      ii i i  +* Re: question about linker44Bart
30 Nov 24     i      ii i i  +- Re: question about linker1Janis Papanagnou
1 Dec 24     i      ii i i  `* Re: question about linker21David Brown
30 Nov 24     i      ii i `* Re: question about linker153David Brown
5 Dec 24     i      ii `- Re: question about linker1Tim Rentsch
30 Nov 24     i      i`* Re: question about linker2Tim Rentsch
29 Nov 24     i      `- Re: question about linker1David Brown
28 Nov 24     `* Re: question about linker4Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal