'How to compile GHCs generates C Code with GCC

I want to generate C code from a Haskell program and compile the generated C code down to assembly using GCC. Therefor I build my own unregistered version of GHC and invoked it with -C on my Haskell source file. This generated a .hc file, which is just regular C code.

Now I want to compile this C file with GCC. Therefor the include path has to be set to GHCs include folder, so GCC can find various headers. Here's how I invoked GCC (I renamed the generated hello.hc file to hello.c):

gcc -I /usr/local/lib/ghc-9.5.20220505/rts/include/ hello.c

But I get the following errors:

hello.C:7:5: error: uninitialized ‘const rFD_bytes’ [-fpermissive]
    7 | IB_(rFD_bytes);
      |     ^~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:264:37: note: in definition of macro ‘IB_’
  264 | #define IB_(X)    static const char X[]
      |                                     ^
hello.C:7:5: error: storage size of ‘rFD_bytes’ isn’t known
    7 | IB_(rFD_bytes);
      |     ^~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:264:37: note: in definition of macro ‘IB_’
  264 | #define IB_(X)    static const char X[]
      |                                     ^
hello.C:12:5: error: uninitialized ‘const rFB_bytes’ [-fpermissive]
   12 | IB_(rFB_bytes);
      |     ^~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:264:37: note: in definition of macro ‘IB_’
  264 | #define IB_(X)    static const char X[]
      |                                     ^
hello.C:12:5: error: storage size of ‘rFB_bytes’ isn’t known
   12 | IB_(rFB_bytes);
      |     ^~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:264:37: note: in definition of macro ‘IB_’
  264 | #define IB_(X)    static const char X[]
      |                                     ^
hello.C: In function ‘void* (* (* sIx_entry())())()’:
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:522:49: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
  522 | #define FunReg ((StgFunTable *)((void *)BaseReg - STG_FIELD_OFFSET(struct PartCapability_, r)))
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:328:39: note: in definition of macro ‘JMP_’
  328 | #define JMP_(cont) return((StgFunPtr)(cont))
      |                                       ^~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:525:36: note: in expansion of macro ‘FunReg’
  525 | #define stg_gc_enter_1            (FunReg->stgGCEnter1)
      |                                    ^~~~~~
hello.C:39:6: note: in expansion of macro ‘stg_gc_enter_1’
   39 | JMP_(stg_gc_enter_1);
      |      ^~~~~~~~~~~~~~
hello.C: At global scope:
hello.C:49:6: error: uninitialized ‘const sIx_info’ [-fpermissive]
   49 | IRO_(sIx_info);
      |      ^~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:273:46: note: in definition of macro ‘IRO_’
  273 | #define IRO_(X)   static const StgWordArray (X)
      |                                              ^
hello.C:49:6: error: storage size of ‘sIx_info’ isn’t known
   49 | IRO_(sIx_info);
      |      ^~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:273:46: note: in definition of macro ‘IRO_’
  273 | #define IRO_(X)   static const StgWordArray (X)
      |                                              ^
hello.C:55:5: error: storage size of ‘sIx_closure’ isn’t known
   55 | IC_(sIx_closure);
      |     ^~~~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:267:46: note: in definition of macro ‘IC_’
  267 | #define IC_(X)    static       StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
      |                                              ^
hello.C:64:6: error: storage size of ‘_uIR_srt’ isn’t known
   64 | IRW_(_uIR_srt);
      |      ^~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:270:46: note: in definition of macro ‘IRW_’
  270 | #define IRW_(X)   static       StgWordArray (X)
      |                                              ^
hello.C: In function ‘void* (* (* Main_main_entry())())()’:
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:522:49: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
  522 | #define FunReg ((StgFunTable *)((void *)BaseReg - STG_FIELD_OFFSET(struct PartCapability_, r)))
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:328:39: note: in definition of macro ‘JMP_’
  328 | #define JMP_(cont) return((StgFunPtr)(cont))
      |                                       ^~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:525:36: note: in expansion of macro ‘FunReg’
  525 | #define stg_gc_enter_1            (FunReg->stgGCEnter1)
      |                                    ^~~~~~
hello.C:90:6: note: in expansion of macro ‘stg_gc_enter_1’
   90 | JMP_(stg_gc_enter_1);
      |      ^~~~~~~~~~~~~~
hello.C: At global scope:
hello.C:115:6: error: storage size of ‘_uJ1_srt’ isn’t known
  115 | IRW_(_uJ1_srt);
      |      ^~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:270:46: note: in definition of macro ‘IRW_’
  270 | #define IRW_(X)   static       StgWordArray (X)
      |                                              ^
hello.C: In function ‘void* (* (* ZCMain_main_entry())())()’:
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:522:49: warning: pointer of type ‘void *’ used in arithmetic [-Wpointer-arith]
  522 | #define FunReg ((StgFunTable *)((void *)BaseReg - STG_FIELD_OFFSET(struct PartCapability_, r)))
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:328:39: note: in definition of macro ‘JMP_’
  328 | #define JMP_(cont) return((StgFunPtr)(cont))
      |                                       ^~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/stg/Regs.h:525:36: note: in expansion of macro ‘FunReg’
  525 | #define stg_gc_enter_1            (FunReg->stgGCEnter1)
      |                                    ^~~~~~
hello.C:141:6: note: in expansion of macro ‘stg_gc_enter_1’
  141 | JMP_(stg_gc_enter_1);
      |      ^~~~~~~~~~~~~~
hello.C: At global scope:
hello.C:166:5: error: storage size of ‘rFC_closure’ isn’t known
  166 | IC_(rFC_closure);
      |     ^~~~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:267:46: note: in definition of macro ‘IC_’
  267 | #define IC_(X)    static       StgWordArray (X) GNU_ATTRIBUTE(aligned (8))
      |                                              ^
hello.C:175:5: error: storage size of ‘rFE_closure’ isn’t known
  175 | IC_(rFE_closure);
      |     ^~~~~~~~~~~
/usr/local/lib/ghc-9.5.20220505/rts/include/Stg.h:267:46: note: in definition of macro ‘IC_’
  267 | #define IC_(X)    static       StgWordArray (X) GNU_ATTRIBUTE(aligned (8))

Does anyone know, how GHC internally compiles C code, or how I have to call GCC on it?

ghc


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source