Re: question about linker

Liste des GroupesRevenir à cl c  
Sujet : Re: question about linker
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 27. Nov 2024, 16:12:39
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vi7ct8$28ej$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : Mozilla Thunderbird
On 27/11/2024 13:27, Thiago Adams wrote:
On 27/11/2024 09:57, Thiago Adams wrote:
On 27/11/2024 09:38, Thiago Adams wrote:
On 27/11/2024 09:10, Bart wrote:
On 27/11/2024 11:57, Bart wrote:
On 27/11/2024 01:52, Thiago Adams wrote:
>
I also use ILs for my compilers, but I write my own backends. I've worked on two diifferent kinds. One looks like a HLL, and only exists for my language. So this original source:
>
>
>
I was wondering if is possible to write C programs without struct/union?
>
I did this experiment.
>
struct X {
     int a, b;
};
>
void F1() {
     struct X x;
     x.a = 1;
     x.b = 2;
     printf("%d, %d", x.a, x.b);
}
>
The equivalent C89 program in a subset without structs count be
>
#define M(T, obj, OFF) *((T*)(((char*)&(obj)) + (OFF)))
>
void F2() {
     char x[8];
     M(int, x, 0 /*offset of a*/) = 1;
     M(int, x, 4 /*offset of b*/) = 2;
     printf("\n");
     printf("%d, %d", M(int, x, 0), M(int, x, 4));
}
>
The char array represents the struct X memory, then we  have to find the offset of the members and cast to their types.
>
>
Does your IL have structs?
No. It has a 'block' type which defines a fixed-length memory block. So your struct ST type below I think would be represented as the type 'mem:824', as it is 824 bytes.
(This works fine for WinABI. But for SYS V ABI, that has a much more complex set of rules where struct passing may depend on the types of the members. They may be split up amongst different registers.
I'm not too worried about that however; it will only apply to structs passed by value across an FFI, and most external libraries don't pass by-value structs. There will also be workarounds.)

The QBE IL has aggregates types. I think this removes the front end calculate the the offsets.
>
https://c9x.me/compile/doc/il.html#Aggregate-Types
>
>
>
>
 I tried this sample with clang and -S -emit-llvm to see if it generates structs. The answer is yes.
 https://llvm.org/docs/LangRef.html#getelementptr-instruction
 struct RT {
   char A;
   int B[10][20];
   char C;
};
struct ST {
   int X;
   double Y;
   struct RT Z;
};
 int *foo(struct ST *s) {
   return &s[1].Z.B[5][13];
}
 The LLVM code generated by Clang is approximately:
 %struct.RT = type { i8, [10 x [20 x i32]], i8 }
%struct.ST = type { i32, double, %struct.RT }
 define ptr @foo(ptr %s) {
entry:
   %arrayidx = getelementptr inbounds %struct.ST, ptr %s, i64 1, i32 2, i32 1, i64 5, i64 13
   ret ptr %arrayidx
}
This example is misleading. That's the output from using -O3. Unoptimised LLVM output is this:
---------------------------------
define dso_local ptr @foo(ptr noundef %0) #0 !dbg !10 {
   %2 = alloca ptr, align 8
   store ptr %0, ptr %2, align 8
     #dbg_declare(ptr %2, !34, !DIExpression(), !35)
   %3 = load ptr, ptr %2, align 8, !dbg !36
   %4 = getelementptr inbounds %struct.ST, ptr %3, i64 1, !dbg !36
   %5 = getelementptr inbounds nuw %struct.ST, ptr %4, i32 0, i32 2, !dbg !37
   %6 = getelementptr inbounds nuw %struct.RT, ptr %5, i32 0, i32 1, !dbg !38
   %7 = getelementptr inbounds [10 x [20 x i32]], ptr %6, i64 0, i64 5, !dbg !36
   %8 = getelementptr inbounds [20 x i32], ptr %7, i64 0, i64 13, !dbg !36
   ret ptr %8, !dbg !39
}
---------------------------------
If you are writing the IR code, then it will be up to you to combine that chain of constant offsets into a single offset. Othewise it will still be you needing to do so the other side of the IR!
(I don't know if the reduction above is done pre-LLVM or by LLVM.
In my IL, it will generate multiple instructions, and there will be a reduction pass, to combine instructions where possible. That's a WIP, but such examples like yours are incredibly rare in my code-base, while the speed-up achieved is likely to be minor. Modern CPUs are good at running poor code fast. Mostly this just makes code more compact.
My IL for your example (I translated to my language) starts off as this:
---------------------
  proc t.foo:
     param    u64       s
     rettype  u64
     load     u64       s
     load     i64       1
     addpx    mem:824 /824/-824      # /scale factor /extra byte offset
     load     i64       20
     addpx    u64 /1
     load     i64       5
     addpx    mem:80 /80
     load     i64       13
     addpx    i32 /4
     jumpret  u64       #1
  #1:
     retfn    u64
  endproc
---------------------
The reductions could also be applied during codegen to native code. But as it is, no reductions are done, and the body of the function generates this x64 code:
     mov   rax,  [rbp + `t.foo.s]  # or mov rax, rcx with reg allocator
     lea   rax,  [rax+20]
     lea   rax,  [rax+400]
     lea   rax,  [rax+52]
Here the reduction could also be done with a peephole optimiser to combined the three LEAs into one instruction. With 's' in a register, probably the optimum code here would be one LEA instruction.

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