6295c73c38c6e699b4219beb2e30e7c3169e1249
[mono.git] / docs / jit-thoughts
1 Just some thoughts for the JITer:
2
3 General issues:
4 ===============
5
6 We are designing a JIT compiler, so we have to consider two things:
7
8 - the quality of the generated code
9 - the time needed to generate that code
10
11 The current approach is to keep the JITer as simple as possible, and thus as
12 fast as possible. The generated code quality will suffer from that.
13
14 We do not map local variables to registers at the moment, and this makes the
15 whole JIT much easier, for example we do not need to identify basic block
16 boundaries or the lifetime of local variables, or select the variables which
17 are worth to put into a register.
18
19 Register allocation is thus done only inside the trees of the forest, and each
20 tree can use the full set of registers. We simply split a tree if we get out of
21 registers, for example the following tree:
22
23
24               add(R0)
25              /   \
26             /     \
27            a(R0)  add(R1)
28                  /   \
29                 /     \
30                b(R1)  add(R2)
31                      /   \
32                     /     \
33                    c(R2)   b(R3)
34
35 can be transformed to:
36
37
38        stloc(t1)         add(R0)
39          |              /   \
40          |             /     \
41         add(R0)       a(R0)  add(R1)
42        /   \                /   \
43       /     \              /     \
44      c(R0)   b(R1)        b(R1)  t1(R2)
45
46
47 Please notice that the split trees use less registers than the original
48 tree. 
49
50
51 Register Allocation:
52 ====================
53
54 With lcc you can assign a fixed register to a tree before register
55 allocation. For example this is needed by call, which return the value always
56 in EAX on x86. The current implementation works without such system, due to
57 special forest generation.
58
59
60 X86 Register Allocation:
61 ========================
62
63 We can use 8bit or 16bit registers on the x86. If we use that feature we have
64 more registers to allocate, which maybe prevents some register spills. We
65 currently ignore that ability and always allocate 32 bit registers, because I
66 think we would gain very little from that optimisation and it would complicate
67 the code.
68
69 Different Register Sets:
70 ========================
71
72 Most processors have more that one register set, at least one for floating
73 point values, and one for integers. Should we support architectures with more
74 that two sets? Does someone knows such an architecture?
75
76 64bit Integer Values:
77 =====================
78
79 I can imagine two different implementation. On possibility would be to treat
80 long (64bit) values simply like any other value type. This implies that we
81 call class methods for ALU operations like add or sub. Sure, this method will
82 be be a bit inefficient.
83
84 The more performant solution is to allocate two 32bit registers for each 64bit
85 value. We add a new non terminal to the monoburg grammar called long_reg. The
86 register allocation routines takes care of this non terminal and allocates two
87 registers for them.
88
89
90 Forest generation:
91 ==================
92
93 It seems that trees generated from the CIL language have some special
94 properties, i.e. the trees already represents basic blocks, so there can be no
95 branches to the inside of such a tree. All results of those trees are stored to
96 memory.
97
98 One idea was to drive the code generation directly from the CIL code, without
99 generating an intermediate forest of trees. I think this is not possible,
100 because you always have to gather some attributes and attach it to the
101 instruction (for example the register allocation info). So I thing generating a
102 tree is the right thing and that also works perfectly with monoburg. IMO we
103 would not get any benefit from trying to feed monoburg directly with CIL
104 instructions. 
105
106 DAG handling:
107 =============
108
109 Monoburg can't handle DAGs, instead we need real trees as input for
110 the code generator. So we have two problems:
111
112 1.) DUP instruction: This one is obvious - we need to store the value
113 into a temporary variable to solve the problem.
114
115 2.) function calls: Chapter 12.8, page 343 of "A retargetable C compiler"
116 explains that: "because listing a call node will give it a hidden reference
117 from the code list". I don't understand that (can someone explain that?), but
118 there is another reason to save return values to temporaries: Consider the
119 following code:
120
121 x = f(y) + g(z); // all functions return integers
122
123 We could generate such a tree for this expression: STLOC(ADD(CALL,CALL))
124
125 The problem is that both calls returns the value in the same register,
126 so it is non trivial to generate code for that tree. We must copy one
127 register into another one, which make register allocation more complex.
128 The easier solution is store the result of function calls to
129 temporaries. This leads to the following forest:
130
131 STLOC(CALL)
132 STLOC(CALL)
133 STLOC(ADD (LDLOC, LDLOC))
134
135 This is what lcc is doing, if I understood 12.8, page 342, 343?
136
137 Value Types:
138 ============
139
140 The only CLI instructions which can handle value types are loads and stores,
141 either to local variable, to the stack or to array elements. Value types with a
142 size smaller than sizeof(int) are handled like any other basic type. For other
143 value types we load the base address and emit block copies to store them. 
144