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