Skip to content

GCC Toolchain

Michael Grossniklaus edited this page Dec 18, 2020 · 2 revisions

Using the compiler with the GGC toolchain

The following example assumes that the Oberon LLVM frontend is used to compile the Sort.Mod module. Compilation from Oberon source code to a binary executable is accomplished in three steps. First the Oberon LLVM frontend translates the module into the LLVM IR (intermediate representation). Then the LLVM tools opt and llc are used to optimize and compile the LLVM IR, respectively. Finally, gcc is used to link and generate the executable.

  1. Invoke the Oberon LLVM frontend oberon-lang using the following command to translate module Sort.Mod to LLVM IR.

    > oberon-lang --filetype=ll Sort.Mod
    

    This will create a new file called Sort.ll containing the serialized LLVM IR in textual format.

  2. In the next step, the LLVM Optimizer and LLVM Static Compiler are used to optimize the LLVM IR and translate it into an assembly file.

    > opt -S -O3 Sort.ll | llc --relocation-model=pic -O3 -o Sort.s
    

    In the example above, optimization level O3 is applied, which performs a preconfigured sequence of LLVM’s optimization or analysis passes. For additional configuration options, please refer to the documentation of opt and llc linked above. In order to compile and link the generate assembly file with gcc, a position independent code file has to be created.

  3. The final step is to link and generate the executable binary from the Sort.s assembly file. In order to accomplish this, gcc can be used as shown below.

    > gcc Sort.s -o Sort
    

This creates the Sort executable binary that can be invoked from the command line.