annotate doc/compiler.rst @ 299:674789d9ff37

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