C & Compilation Flashcards
What are the common languages of choice for low level programming
+ C due to the amount of control given to the programmer
+ Python because its easier for a programmer to understand and write code quickly
+ Rust because safety go burrr
What do *.h
files contain
The declarations for functions and variables which a given c
file exposes
What number can be special in some scenarios in c, what are some of these scenarios
0:
+ Any value which is not 0 is true
+ \0
is used to mark the end of strings
+ A pointer to address 0 is NULL and invalid
When is cross compilation used
When compilation on the target device is impossible or impracticable, for example:
+ For the first compiler on new hardware or a new OS
+ For low capability targets such as the Pico’s
What are the 4 stages of the compiler driver (to produce an object or ELF file from a C file)
Preprocessor
Compiler
Assembler
Linker
For cross-compilation we use:
Cross-Compiler
Cross-Assembler
Cross-Linker
What is the job of the preprocess, what files does it consider
The preprocessor takes *.c
files, expands macros and replaces include directives with the code blocks they reference. This produces a .i
file
What is the job of the compiler in the compile driver process
The compiler takes preprocessed code, parses it and optimises it.
To what form does the compiler (as a step) parse the code it is given
The compiler parses code to an “Intermediate Representation”, this is a strongly typed, assembly like, RISC instruction set which is abstract from details of source code and the target machine
What does the compiler (as a step) do once it has parsed code to an Intermediate Representation (IR)
It parses it to an optimiser such as the LLVM optimiser which apply’s a set of known optimisations (such as reversing the direction of for loops) to it
Why is it useful to parse C code to an Intermediate Representation before optimising it
Because it allows the same optimiser to be used for multiple languages
What does the assembler convert its input to
The assembler takes the intermediate representation of the program and converts it to a relocatable file such as .o
. or .elf
What are the 4 most important sections of a relocatable file (i.e. *.o
or *.elf
)
.text
.rodata
.data
.bss
What does the .text
section of a relocatable file contain, and where is this placed when the file is loaded
.text
contains instructions and gets placed into flash memory when the program is loaded
What does the .rodata
section of a relocatable file contain and where is this placed when the file is loaded
.rodata
contains read only data and is placed into flash memory when the program is loaded
What does the .data
section of a relocatable file conatain and where is this placed when the file is loaded
.data
contains Initialised global variables which are placed into flash to be copied into RAM when the program is loaded