273
|
1
|
|
2
|
|
3 Compiler
|
|
4 ========
|
|
5
|
|
6 This chapter describes the design of the compiler.
|
|
7
|
|
8
|
|
9 Overview
|
|
10 --------
|
|
11
|
|
12 The compiler consists a frontend, mid-end and back-end. The frontend deals with
|
|
13 source file parsing and semantics checking. The mid-end performs optimizations.
|
|
14 This is optional. The back-end generates machine code. The front-end produces
|
|
15 intermediate code. This is a simple representation of the source. The back-end
|
|
16 can accept this kind of representation. This way the compiler is portable and
|
|
17 a front end can be constructed without having to do the rest.
|
|
18
|
|
19 .. graphviz::
|
|
20
|
|
21 digraph x {
|
|
22 rankdir="LR"
|
|
23 1 [label="c3 source file"]
|
|
24 10 [label="c3 front end" ]
|
|
25 11 [label="language X front end" ]
|
|
26 20 [label="mid end" ]
|
|
27 30 [label="back end for X86" ]
|
|
28 31 [label="back end for ARM" ]
|
|
29 40 [label="object file"]
|
|
30 1 -> 10
|
|
31 10 -> 20 [label="IR-code"]
|
|
32 11 -> 20 [label="IR-code"]
|
|
33 20 -> 30 [label="IR-code"]
|
|
34 20 -> 31 [label="IR-code"]
|
|
35 30 -> 40
|
|
36 }
|
|
37
|
299
|
38
|
273
|
39 IR-code
|
|
40 -------
|
299
|
41 The intermediate representation (IR) of a program de-couples the front end
|
|
42 from the backend of the compiler.
|
273
|
43
|
299
|
44 See ir for details about all the available instructions.
|
273
|
45
|
|
46
|
274
|
47 C3 Front-end
|
|
48 ------------
|
273
|
49
|
|
50 For the front-end a recursive descent parser is created for the c3 language.
|
|
51 This is a subset of the C language with some additional features.
|
|
52
|
274
|
53 .. graphviz::
|
|
54
|
|
55 digraph c3 {
|
|
56 1 [label="source text"]
|
|
57 10 [label="lexer" ]
|
|
58 20 [label="parser" ]
|
|
59 30 [label="semantic checks" ]
|
|
60 40 [label="code generation"]
|
|
61 99 [label="IR-code object"]
|
299
|
62 1 -> 10
|
|
63 10 -> 20
|
274
|
64 20 -> 30
|
299
|
65 30 -> 40 [label="AST tree"]
|
274
|
66 40 -> 99
|
|
67 }
|
|
68
|
|
69 .. autoclass:: c3.Builder
|
273
|
70
|
|
71 .. autoclass:: c3.Parser
|
|
72
|
|
73 .. autoclass:: c3.CodeGenerator
|
|
74
|
|
75 Back-end
|
|
76 --------
|
|
77
|
|
78 The back-end is more complicated. There are several steps to be taken here.
|
|
79
|
|
80 1. Instruction selection
|
|
81 2. register allocation
|
|
82 3. Peep hole optimization?
|
|
83 4. real code generation
|
|
84
|
297
|
85 .. automodule:: codegen
|
273
|
86 :members:
|
|
87
|
274
|
88 Instruction selection
|
|
89 ~~~~~~~~~~~~~~~~~~~~~
|
|
90
|
|
91 The instruction selection phase takes care of scheduling and instruction
|
|
92 selection. The output of this phase is a one frame per function with a flat
|
|
93 list of abstract machine instructions.
|
|
94
|
|
95 .. autoclass:: irmach.Frame
|
|
96
|
|
97 .. autoclass:: irmach.AbstractInstruction
|
|
98
|
|
99
|