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
What is the job of the linker
The linker takes Relocatable files and combines them to produce new Relocatable Files, merging the sections of the files
What does the linker bottleneck refer to
The inker bottleneck refers to the fact compilation can be done in parallel, while linking cannot and MUST be done sequentially
What operation does a loader perform
A loader accepts a relocatable file and places the sections into physical memory
When a c program is loaded into memory, where are global and static variables placed (for one example memory location
Global and static variables are placed after the text segment and before the heap
They are split up into those which are initialized and those which are not
What is the heap used for
The heap is the segment in memory which can be dynamically allocated, this can be accessed with commands such as malloc
, realloc
and free
What is the stack used for
The stack is used to store stack frames, these are created whenever a function is called and removed when the function is left, they contain local variables
How could the amount of memory available to a program be estimated
By creating a new item on the stack (new local var) s
, and a new item on the heap (i.e. with malloc
) h
and taking s* - h*