forked from facebook/hhvm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-gen-cf.h
More file actions
233 lines (191 loc) · 7.41 KB
/
code-gen-cf.h
File metadata and controls
233 lines (191 loc) · 7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-2016 Facebook, Inc. (http://www.facebook.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
*/
#ifndef incl_HPHP_VM_CODE_GEN_CF_H_
#define incl_HPHP_VM_CODE_GEN_CF_H_
#include "hphp/runtime/vm/jit/vasm-gen.h"
#include "hphp/runtime/vm/jit/vasm-instr.h"
#include "hphp/runtime/vm/jit/vasm-reg.h"
namespace HPHP { namespace jit {
///////////////////////////////////////////////////////////////////////////////
namespace code_gen_detail {
///////////////////////////////////////////////////////////////////////////////
template <class Then>
void ifThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock) {
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {done, then}};
vcold = then;
thenBlock(vcold);
if (!vcold.closed()) vcold << jmp{done};
vmain = done;
}
template <class Then, class Else>
void ifThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock) {
auto elze = vmain.makeBlock();
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {elze, then}};
vmain = elze;
elseBlock(vmain);
if (!vmain.closed()) vmain << jmp{done};
vcold = then;
thenBlock(vcold);
if (!vcold.closed()) vcold << jmp{done};
vmain = done;
}
template <class Then, class Else>
Vreg cond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock) {
auto elze = vmain.makeBlock();
auto then = vcold.makeBlock();
auto done = vmain.makeBlock();
vmain << jcc{cc, sf, {elze, then}};
vmain = elze;
auto r1 = elseBlock(vmain);
vmain << phijmp{done, vmain.makeTuple({r1})};
vcold = then;
auto r2 = thenBlock(vcold);
vcold << phijmp{done, vcold.makeTuple({r2})};
vmain = done;
vmain << phidef{vmain.makeTuple({dst})};
return dst;
}
///////////////////////////////////////////////////////////////////////////////
}
///////////////////////////////////////////////////////////////////////////////
// Conditionals.
//
// Each conditional control-flow helper comes in three flavors:
// - Emit `thenBlock' to `vmain' (along with all other blocks).
// - Emit `thenBlock' to `vcold' (with all other blocks in `vmain').
// - Emit `thenBlock' to `unlikely ? vcold : vmain'.
/*
* Generate an if-then block construct.
*
* Tests `sf' for the branch condition `cc', and jumps to the code generated by
* the `thenBlock' lambda if the condition holds. A jmp past the construct is
* emitted if `thenBlock' is not terminated.
*
* `thenBlock' takes a single argument: the Vout to emit to.
*/
template <class Then>
void ifThen(Vout& vmain, ConditionCode cc, Vreg sf, Then thenBlock) {
code_gen_detail::ifThen(vmain, vmain, cc, sf, thenBlock);
}
template <class Then>
void unlikelyIfThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock) {
code_gen_detail::ifThen(vmain, vcold, cc, sf, thenBlock);
}
template <class Then>
void ifThen(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, bool unlikely) {
code_gen_detail::ifThen(vmain, unlikely ? vcold : vmain,
cc, sf, thenBlock);
}
/*
* Like the above flavors of ifThen(), except with a block label instead of a
* block-emitting lambda.
*/
inline void ifThen(Vout& v, ConditionCode cc, Vreg sf, Vlabel then) {
auto const done = v.makeBlock();
v << jcc{cc, sf, {done, then}};
v = done;
}
/*
* Generate an if-then-else block construct.
*
* Like ifThen(), except that in addition, we jump to the code generated by
* `elseBlock' if the condition does not hold.
*
* `elseBlock' takes the same arguments as `thenBlock', and we likewise close
* it with a jmp to past the construct if it is not terminated.
*/
template <class Then, class Else>
void ifThenElse(Vout& vmain, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock) {
code_gen_detail::ifThenElse(vmain, vmain, cc, sf, thenBlock, elseBlock);
}
template <class Then, class Else>
void unlikelyIfThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock) {
code_gen_detail::ifThenElse(vmain, vcold, cc, sf, thenBlock, elseBlock);
}
template <class Then, class Else>
void ifThenElse(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Then thenBlock, Else elseBlock, bool unlikely) {
code_gen_detail::ifThenElse(vmain, unlikely ? vcold : vmain,
cc, sf, thenBlock, elseBlock);
}
/*
* Generate an if-then-else block construct with a single dst.
*
* Like ifThenElse(), except that the blocks are expected to return a dst Vreg.
* The dsts are phi'd into `dst'.
*
* Returns `dst' unaltered, for convenience.
*/
template <class Then, class Else>
Vreg cond(Vout& vmain, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock) {
return code_gen_detail::cond(vmain, vmain, cc, sf, dst, thenBlock, elseBlock);
}
template <class Then, class Else>
Vreg unlikelyCond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock) {
return code_gen_detail::cond(vmain, vcold, cc, sf, dst, thenBlock, elseBlock);
}
template <class Then, class Else>
Vreg cond(Vout& vmain, Vout& vcold, ConditionCode cc, Vreg sf,
Vreg dst, Then thenBlock, Else elseBlock, bool unlikely) {
return code_gen_detail::cond(vmain, unlikely ? vcold : vmain,
cc, sf, dst, thenBlock, elseBlock);
}
///////////////////////////////////////////////////////////////////////////////
/*
* Generate a do-while loop.
*
* The `regs' list is the list of initial loop registers, which will be phi'd
* appropriately for the loop.
*
* `loopBlock' is the lambda responsible for generating the code. It takes
* both the input phidef and output phijcc loop registers as arguments, and
* should return a single SF Vreg to be tested against `cc'.
*/
template <class Loop>
void doWhile(Vout& v, ConditionCode cc,
const VregList& regs, Loop loopBlock) {
auto loop = v.makeBlock();
auto done = v.makeBlock();
auto const freshRegs = [&] {
auto copy = regs;
for (auto& reg : copy) reg = v.makeReg();
return copy;
};
auto in = freshRegs(), out = freshRegs();
v << phijmp{loop, v.makeTuple(regs)};
v = loop;
v << phidef{v.makeTuple(in)};
auto const sf = loopBlock(in, out);
v << phijcc{cc, sf, {done, loop}, v.makeTuple(out)};
v = done;
v << phidef{v.makeTuple(freshRegs())};
}
///////////////////////////////////////////////////////////////////////////////
}}
#endif