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
|
|
65 .. autoclass:: c3.Builder
|
273
|
66
|
|
67 .. autoclass:: c3.Parser
|
|
68
|
|
69 .. autoclass:: c3.CodeGenerator
|
|
70
|
|
71 Back-end
|
|
72 --------
|
|
73
|
|
74 The back-end is more complicated. There are several steps to be taken here.
|
|
75
|
|
76 1. Instruction selection
|
|
77 2. register allocation
|
|
78 3. Peep hole optimization?
|
|
79 4. real code generation
|
|
80
|
297
|
81 .. automodule:: codegen
|
273
|
82 :members:
|
|
83
|
274
|
84 Instruction selection
|
|
85 ~~~~~~~~~~~~~~~~~~~~~
|
|
86
|
|
87 The instruction selection phase takes care of scheduling and instruction
|
|
88 selection. The output of this phase is a one frame per function with a flat
|
|
89 list of abstract machine instructions.
|
|
90
|
|
91 .. autoclass:: irmach.Frame
|
|
92
|
|
93 .. autoclass:: irmach.AbstractInstruction
|
|
94
|
|
95
|