OLD | NEW |
---|---|
(Empty) | |
1 //===- subzero/src/WasmTranslator.cpp - WASM to Subzero Translation -------===// | |
2 // | |
3 // The Subzero Code Generator | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
Jim Stichnoth
2016/04/04 21:26:51
Add the blurb about what this file does.
Probably
Eric Holk
2016/04/04 22:23:22
Done.
| |
9 | |
10 #include "llvm/Support/StreamingMemoryObject.h" | |
11 | |
12 #include "WasmTranslator.h" | |
13 | |
14 #include "src/wasm/module-decoder.h" | |
15 #include "src/wasm/wasm-opcodes.h" | |
16 | |
Jim Stichnoth
2016/04/04 21:26:52
Remove blank line?
Eric Holk
2016/04/04 22:23:23
Done.
| |
17 #include "src/zone.h" | |
18 | |
19 #include "IceGlobalInits.h" | |
Jim Stichnoth
2016/04/04 21:26:52
alphabetize these
Eric Holk
2016/04/04 22:23:22
Done.
| |
20 #include "IceCfgNode.h" | |
21 | |
22 using namespace std; | |
23 using namespace Ice; | |
24 using namespace v8; | |
25 using namespace v8::internal; | |
26 using namespace v8::internal::wasm; | |
27 using v8::internal::wasm::DecodeWasmModule; | |
28 | |
29 #include "src/wasm/ast-decoder-impl.h" | |
30 | |
31 #define LOG(Expr) log([&](Ostream & out) { Expr; }) | |
32 | |
33 Ice::Type toIceType(v8::internal::MachineType) { | |
Jim Stichnoth
2016/04/04 21:26:51
Should these be inside an anonymous namespace?
Eric Holk
2016/04/04 22:23:22
Done.
| |
34 // TODO(eholk): actually convert this. | |
35 return IceType_i32; | |
36 } | |
37 | |
38 Ice::Type toIceType(wasm::LocalType Type) { | |
39 switch (Type) { | |
40 default: | |
41 llvm::report_fatal_error("unexpected enum value"); | |
42 case MachineRepresentation::kNone: | |
43 llvm::report_fatal_error("kNone type not supported"); | |
44 case MachineRepresentation::kBit: | |
45 return IceType_i1; | |
46 case MachineRepresentation::kWord8: | |
47 return IceType_i8; | |
48 case MachineRepresentation::kWord16: | |
49 return IceType_i16; | |
50 case MachineRepresentation::kWord32: | |
51 return IceType_i32; | |
52 case MachineRepresentation::kWord64: | |
53 return IceType_i64; | |
54 case MachineRepresentation::kFloat32: | |
55 return IceType_f32; | |
56 case MachineRepresentation::kFloat64: | |
57 return IceType_f64; | |
58 case MachineRepresentation::kSimd128: | |
59 llvm::report_fatal_error("ambiguous SIMD type"); | |
60 case MachineRepresentation::kTagged: | |
61 llvm::report_fatal_error("kTagged type not supported"); | |
62 } | |
63 } | |
64 | |
65 /// This class wraps either an Operand or a CfgNode. | |
66 /// | |
67 /// Turbofan's sea of nodes representation only has nodes for values, control | |
68 /// flow, etc. In Subzero these concepts are all separate. This class lets V8's | |
69 /// Wasm decoder treat Subzero objects as though they are all the same. | |
70 class OperandNode { | |
71 static constexpr uintptr_t NODE_FLAG = 1; | |
72 static constexpr uintptr_t UNDEF_PTR = (uintptr_t)-1; | |
73 | |
74 uintptr_t Data = UNDEF_PTR; | |
75 | |
76 public: | |
77 explicit OperandNode() = default; | |
Jim Stichnoth
2016/04/04 21:26:52
You can remove "explicit" from a zero-argument met
Eric Holk
2016/04/04 22:23:23
Done.
| |
78 explicit OperandNode(Operand *Operand) | |
79 : Data(reinterpret_cast<uintptr_t>(Operand)) {} | |
80 explicit OperandNode(CfgNode *Node) | |
81 : Data(reinterpret_cast<uintptr_t>(Node) | NODE_FLAG) {} | |
82 explicit OperandNode(nullptr_t) : Data(UNDEF_PTR) {} | |
83 | |
84 operator Operand *() const { | |
Jim Stichnoth
2016/04/04 21:26:52
I'm not sure where operator() is actually used so
Eric Holk
2016/04/04 22:23:23
I think most every use of these operators has been
| |
85 if (UNDEF_PTR == Data) { | |
86 return nullptr; | |
87 } | |
88 assert(isOperand()); | |
89 return reinterpret_cast<Operand *>(Data); | |
90 } | |
91 | |
92 operator CfgNode *() const { | |
93 if (UNDEF_PTR == Data) { | |
94 return nullptr; | |
95 } | |
96 assert(isCfgNode()); | |
97 return reinterpret_cast<CfgNode *>(Data & ~NODE_FLAG); | |
98 } | |
99 | |
100 explicit operator bool() const { return (Data != UNDEF_PTR) && Data; } | |
101 bool operator==(const OperandNode &Rhs) const { | |
102 return (Data == Rhs.Data) || | |
103 (UNDEF_PTR == Data && (Rhs.Data == 0 || Rhs.Data == NODE_FLAG)) || | |
104 (UNDEF_PTR == Rhs.Data && (Data == 0 || Data == NODE_FLAG)); | |
105 } | |
106 bool operator!=(const OperandNode &Rhs) const { return !(*this == Rhs); } | |
107 | |
108 bool isOperand() const { return (Data != UNDEF_PTR) && !(Data & NODE_FLAG); } | |
109 bool isCfgNode() const { return (Data != UNDEF_PTR) && (Data & NODE_FLAG); } | |
110 | |
111 Operand *toOperand() const { return static_cast<Operand *>(*this); } | |
Jim Stichnoth
2016/04/04 21:26:52
Hmm, I expected toOperand() and toNode() to make i
Eric Holk
2016/04/04 22:23:23
I replaced the asserts in the cast operators to ch
| |
112 | |
113 CfgNode *toNode() const { return static_cast<CfgNode *>(*this); } | |
Jim Stichnoth
2016/04/04 21:26:51
Name this toCfgNode() for consistency?
Eric Holk
2016/04/04 22:23:22
Done.
| |
114 }; | |
115 | |
116 Ostream &operator<<(Ostream &Out, const OperandNode &Op) { | |
117 if (Op.isOperand()) { | |
118 Out << "(Operand*)" << Op.toOperand(); | |
119 } else if (Op.isCfgNode()) { | |
120 Out << "(CfgNode*)" << Op.toNode(); | |
121 } else { | |
122 Out << "nullptr"; | |
123 } | |
124 return Out; | |
125 } | |
126 | |
127 constexpr bool isComparison(wasm::WasmOpcode Opcode) { | |
128 switch (Opcode) { | |
129 case kExprI32Ne: | |
130 case kExprI64Ne: | |
131 case kExprI32Eq: | |
132 case kExprI64Eq: | |
133 case kExprI32LtS: | |
134 case kExprI64LtS: | |
135 case kExprI32LtU: | |
136 case kExprI64LtU: | |
137 case kExprI32GeS: | |
138 case kExprI64GeS: | |
139 case kExprI32GtS: | |
140 case kExprI64GtS: | |
141 case kExprI32GtU: | |
142 case kExprI64GtU: | |
143 return true; | |
144 default: | |
145 return false; | |
146 } | |
147 } | |
148 | |
149 class IceBuilder { | |
150 using Node = OperandNode; | |
151 | |
152 IceBuilder() = delete; | |
153 IceBuilder(const IceBuilder &) = delete; | |
154 IceBuilder &operator=(const IceBuilder &) = delete; | |
155 | |
156 public: | |
157 explicit IceBuilder(class Cfg *Func) | |
158 : Func(Func), Ctx(Func->getContext()), ControlPtr(nullptr) {} | |
159 | |
160 /// Allocates a buffer of Nodes for use by V8. | |
161 Node *Buffer(size_t Count) { | |
162 LOG(out << "Buffer(" << Count << ")\n"); | |
163 return Func->allocateArrayOf<Node>(Count); | |
164 } | |
165 | |
166 Node Error() { llvm::report_fatal_error("Error"); } | |
167 Node Start(unsigned Params) { | |
168 LOG(out << "Start(" << Params << ") = "); | |
169 auto *Entry = Func->makeNode(); | |
170 Func->setEntryNode(Entry); | |
171 LOG(out << Node(Entry) << "\n"); | |
172 return OperandNode(Entry); | |
173 } | |
174 Node Param(unsigned Index, wasm::LocalType Type) { | |
175 LOG(out << "Param(" << Index << ") = "); | |
176 auto *Arg = makeVariable(toIceType(Type)); | |
177 assert(Index == NextArg); | |
178 Func->addArg(Arg); | |
179 ++NextArg; | |
180 LOG(out << Node(Arg) << "\n"); | |
181 return OperandNode(Arg); | |
182 } | |
183 Node Loop(CfgNode *Entry) { | |
184 auto *Loop = Func->makeNode(); | |
185 LOG(out << "Loop(" << Entry << ") = " << Loop << "\n"); | |
186 Entry->appendInst(InstBr::create(Func, Loop)); | |
187 return OperandNode(Loop); | |
188 } | |
189 void Terminate(Node Effect, Node Control) { | |
190 // TODO(eholk): this is almost certainly wrong | |
191 LOG(out << "Terminate(" << Effect << ", " << Control << ")" | |
192 << "\n"); | |
193 } | |
194 Node Merge(unsigned Count, Node *Controls) { | |
195 LOG(out << "Merge(" << Count); | |
196 for (unsigned i = 0; i < Count; ++i) { | |
197 LOG(out << ", " << Controls[i]); | |
198 } | |
199 LOG(out << ") = "); | |
200 | |
201 auto *MergedNode = Func->makeNode(); | |
202 | |
203 for (unsigned i = 0; i < Count; ++i) { | |
204 CfgNode *Control = Controls[i]; | |
205 Control->appendInst(InstBr::create(Func, MergedNode)); | |
206 } | |
207 LOG(out << (OperandNode)MergedNode << "\n"); | |
208 return OperandNode(MergedNode); | |
209 } | |
210 Node Phi(wasm::LocalType Type, unsigned Count, Node *Vals, Node Control) { | |
211 LOG(out << "Phi(" << Count << ", " << Control); | |
212 for (int i = 0; i < Count; ++i) { | |
213 LOG(out << ", " << Vals[i]); | |
214 } | |
215 LOG(out << ") = "); | |
216 | |
217 const auto &InEdges = Control.toNode()->getInEdges(); | |
218 assert(Count == InEdges.size()); | |
219 | |
220 assert(Count > 0); | |
221 | |
222 auto *Dest = makeVariable(Vals[0].toOperand()->getType(), Control); | |
223 | |
224 // Multiply by 10 in case more things get added later. | |
225 | |
226 // TODO(eholk): find a better way besides multiplying by some arbitrary | |
227 // constant. | |
228 auto *Phi = InstPhi::create(Func, Count * 10, Dest); | |
229 for (int i = 0; i < Count; ++i) { | |
230 auto *Op = Vals[i].toOperand(); | |
231 assert(Op); | |
232 Phi->addArgument(Op, InEdges[i]); | |
233 } | |
234 setDefiningInst(Dest, Phi); | |
235 Control.toNode()->appendInst(Phi); | |
236 LOG(out << Node(Dest) << "\n"); | |
237 return OperandNode(Dest); | |
238 } | |
239 Node EffectPhi(unsigned Count, Node *Effects, Node Control) { | |
240 // TODO(eholk): this function is almost certainly wrong. | |
241 LOG(out << "EffectPhi(" << Count << ", " << Control << "):\n"); | |
242 for (unsigned i = 0; i < Count; ++i) { | |
243 LOG(out << " " << Effects[i] << "\n"); | |
244 } | |
245 return OperandNode(nullptr); | |
246 } | |
247 Node Int32Constant(int32_t Value) { | |
248 LOG(out << "Int32Constant(" << Value << ") = "); | |
249 auto *Const = Ctx->getConstantInt32(Value); | |
250 assert(Const); | |
251 assert(Control()); | |
252 LOG(out << Node(Const) << "\n"); | |
253 return OperandNode(Const); | |
254 } | |
255 Node Int64Constant(int64_t Value) { | |
256 LOG(out << "Int64Constant(" << Value << ") = "); | |
257 auto *Const = Ctx->getConstantInt64(Value); | |
258 assert(Const); | |
259 LOG(out << Node(Const) << "\n"); | |
260 return OperandNode(Const); | |
261 } | |
262 Node Float32Constant(float Value) { | |
263 LOG(out << "Float32Constant(" << Value << ") = "); | |
264 auto *Const = Ctx->getConstantFloat(Value); | |
265 assert(Const); | |
266 LOG(out << Node(Const) << "\n"); | |
267 return OperandNode(Const); | |
268 } | |
269 Node Float64Constant(double Value) { | |
270 LOG(out << "Float64Constant(" << Value << ") = "); | |
271 auto *Const = Ctx->getConstantDouble(Value); | |
272 assert(Const); | |
273 LOG(out << Node(Const) << "\n"); | |
274 return OperandNode(Const); | |
275 } | |
276 Node Binop(wasm::WasmOpcode Opcode, Node Left, Node Right) { | |
277 LOG(out << "Binop(" << WasmOpcodes::OpcodeName(Opcode) << ", " << Left | |
278 << ", " << Right << ") = "); | |
279 auto *Dest = makeVariable( | |
280 isComparison(Opcode) ? IceType_i1 : Left.toOperand()->getType()); | |
281 switch (Opcode) { | |
282 case kExprI32Add: | |
283 case kExprI64Add: | |
284 Control()->appendInst( | |
285 InstArithmetic::create(Func, InstArithmetic::Add, Dest, Left, Right)); | |
286 break; | |
287 case kExprI32Sub: | |
288 case kExprI64Sub: | |
289 Control()->appendInst( | |
290 InstArithmetic::create(Func, InstArithmetic::Sub, Dest, Left, Right)); | |
291 break; | |
292 case kExprI32Mul: | |
293 case kExprI64Mul: | |
294 Control()->appendInst( | |
295 InstArithmetic::create(Func, InstArithmetic::Mul, Dest, Left, Right)); | |
296 break; | |
297 case kExprI32DivU: | |
298 case kExprI64DivU: | |
299 Control()->appendInst(InstArithmetic::create(Func, InstArithmetic::Udiv, | |
300 Dest, Left, Right)); | |
301 break; | |
302 case kExprI32RemU: | |
303 case kExprI64RemU: | |
304 Control()->appendInst(InstArithmetic::create(Func, InstArithmetic::Urem, | |
305 Dest, Left, Right)); | |
306 break; | |
307 case kExprI32Ior: | |
308 case kExprI64Ior: | |
309 Control()->appendInst( | |
310 InstArithmetic::create(Func, InstArithmetic::Or, Dest, Left, Right)); | |
311 break; | |
312 case kExprI32Xor: | |
313 case kExprI64Xor: | |
314 Control()->appendInst( | |
315 InstArithmetic::create(Func, InstArithmetic::Xor, Dest, Left, Right)); | |
316 break; | |
317 case kExprI32Shl: | |
318 case kExprI64Shl: | |
319 Control()->appendInst( | |
320 InstArithmetic::create(Func, InstArithmetic::Shl, Dest, Left, Right)); | |
321 break; | |
322 case kExprI32ShrU: | |
323 case kExprI64ShrU: | |
324 case kExprI32ShrS: | |
325 case kExprI64ShrS: | |
326 Control()->appendInst(InstArithmetic::create(Func, InstArithmetic::Ashr, | |
327 Dest, Left, Right)); | |
328 break; | |
329 case kExprI32And: | |
330 case kExprI64And: | |
331 Control()->appendInst( | |
332 InstArithmetic::create(Func, InstArithmetic::And, Dest, Left, Right)); | |
333 break; | |
334 case kExprI32Ne: | |
335 case kExprI64Ne: | |
336 Control()->appendInst( | |
337 InstIcmp::create(Func, InstIcmp::Ne, Dest, Left, Right)); | |
338 break; | |
339 case kExprI32Eq: | |
340 case kExprI64Eq: | |
341 Control()->appendInst( | |
342 InstIcmp::create(Func, InstIcmp::Eq, Dest, Left, Right)); | |
343 break; | |
344 case kExprI32LtS: | |
345 case kExprI64LtS: | |
346 Control()->appendInst( | |
347 InstIcmp::create(Func, InstIcmp::Slt, Dest, Left, Right)); | |
348 break; | |
349 case kExprI32LtU: | |
350 case kExprI64LtU: | |
351 Control()->appendInst( | |
352 InstIcmp::create(Func, InstIcmp::Ult, Dest, Left, Right)); | |
353 break; | |
354 case kExprI32GeS: | |
355 case kExprI64GeS: | |
356 Control()->appendInst( | |
357 InstIcmp::create(Func, InstIcmp::Sge, Dest, Left, Right)); | |
358 case kExprI32GtS: | |
359 case kExprI64GtS: | |
360 Control()->appendInst( | |
361 InstIcmp::create(Func, InstIcmp::Sgt, Dest, Left, Right)); | |
362 break; | |
363 case kExprI32GtU: | |
364 case kExprI64GtU: | |
365 Control()->appendInst( | |
366 InstIcmp::create(Func, InstIcmp::Ugt, Dest, Left, Right)); | |
367 break; | |
368 default: | |
369 LOG(out << "Unknown binop: " << WasmOpcodes::OpcodeName(Opcode) << "\n"); | |
370 llvm::report_fatal_error("Uncovered or invalid binop."); | |
371 return OperandNode(nullptr); | |
372 } | |
373 LOG(out << Dest << "\n"); | |
374 return OperandNode(Dest); | |
375 } | |
376 Node Unop(wasm::WasmOpcode Opcode, Node Input) { | |
377 LOG(out << "Unop(" << WasmOpcodes::OpcodeName(Opcode) << ", " << Input | |
378 << ") = "); | |
379 Ice::Variable *Dest = nullptr; | |
380 switch (Opcode) { | |
381 case kExprF32Neg: { | |
382 Dest = makeVariable(IceType_f32); | |
383 Control()->appendInst(InstArithmetic::create( | |
384 Func, InstArithmetic::Fsub, Dest, Ctx->getConstantFloat(0), Input)); | |
385 break; | |
386 } | |
387 case kExprF64Neg: { | |
388 Dest = makeVariable(IceType_f64); | |
389 Control()->appendInst(InstArithmetic::create( | |
390 Func, InstArithmetic::Fsub, Dest, Ctx->getConstantDouble(0), Input)); | |
391 break; | |
392 } | |
393 case kExprI64UConvertI32: | |
394 Dest = makeVariable(IceType_i64); | |
395 Control()->appendInst( | |
396 InstCast::create(Func, InstCast::Zext, Dest, Input)); | |
397 break; | |
398 default: | |
399 LOG(out << "Unknown unop: " << WasmOpcodes::OpcodeName(Opcode) << "\n"); | |
400 llvm::report_fatal_error("Uncovered or invalid unop."); | |
401 return OperandNode(nullptr); | |
402 } | |
403 LOG(out << Dest << "\n"); | |
404 return OperandNode(Dest); | |
405 } | |
406 unsigned InputCount(CfgNode *Node) const { return Node->getInEdges().size(); } | |
407 bool IsPhiWithMerge(Node Phi, Node Merge) const { | |
408 LOG(out << "IsPhiWithMerge(" << Phi << ", " << Merge << ")" | |
409 << "\n"); | |
410 if (Phi && Phi.isOperand()) { | |
411 LOG(out << " ...is operand" | |
412 << "\n"); | |
413 if (auto *Inst = getDefiningInst(Phi)) { | |
414 LOG(out << " ...has defining instruction" | |
415 << "\n"); | |
416 LOG(out << getDefNode(Phi) << "\n"); | |
417 LOG(out << " ..." << (getDefNode(Phi) == Merge) << "\n"); | |
418 return getDefNode(Phi) == Merge; | |
419 } | |
420 } | |
421 return false; | |
422 } | |
423 void AppendToMerge(CfgNode *Merge, CfgNode *From) const { | |
424 From->appendInst(InstBr::create(Func, Merge)); | |
425 } | |
426 void AppendToPhi(Node Merge, Node Phi, Node From) { | |
427 LOG(out << "AppendToPhi(" << Merge << ", " << Phi << ", " << From << ")" | |
428 << "\n"); | |
429 auto *Inst = getDefiningInst(Phi); | |
430 Inst->addArgument(From, getDefNode(From)); | |
431 } | |
432 | |
433 //----------------------------------------------------------------------- | |
434 // Operations that read and/or write {control} and {effect}. | |
435 //----------------------------------------------------------------------- | |
436 Node Branch(Node Cond, Node *TrueNode, Node *FalseNode) { | |
437 // true_node and false_node appear to be out parameters. | |
438 LOG(out << "Branch(" << Cond << ", "); | |
439 | |
440 // save control here because true_node appears to alias control. | |
441 auto *Ctrl = Control(); | |
442 | |
443 *TrueNode = OperandNode(Func->makeNode()); | |
444 *FalseNode = OperandNode(Func->makeNode()); | |
445 | |
446 LOG(out << *TrueNode << ", " << *FalseNode << ")" | |
447 << "\n"); | |
448 | |
449 Ctrl->appendInst(InstBr::create(Func, Cond, *TrueNode, *FalseNode)); | |
450 return OperandNode(nullptr); | |
451 } | |
452 Node Switch(unsigned Count, Node Key) { llvm::report_fatal_error("Switch"); } | |
453 Node IfValue(int32_t Value, Node Sw) { llvm::report_fatal_error("IfValue"); } | |
454 Node IfDefault(Node Sw) { llvm::report_fatal_error("IfDefault"); } | |
455 Node Return(unsigned Count, Node *Vals) { | |
456 assert(1 >= Count); | |
457 LOG(out << "Return("); | |
458 if (Count > 0) | |
459 LOG(out << Vals[0]); | |
460 LOG(out << ")" | |
461 << "\n"); | |
462 auto *Instr = | |
463 1 == Count ? InstRet::create(Func, Vals[0]) : InstRet::create(Func); | |
464 Control()->appendInst(Instr); | |
465 Control()->setHasReturn(); | |
466 LOG(out << Node(nullptr) << "\n"); | |
467 return OperandNode(nullptr); | |
468 } | |
469 Node ReturnVoid() { | |
470 LOG(out << "ReturnVoid() = "); | |
471 auto *Instr = InstRet::create(Func); | |
472 Control()->appendInst(Instr); | |
473 Control()->setHasReturn(); | |
474 LOG(out << Node(nullptr) << "\n"); | |
475 return OperandNode(nullptr); | |
476 } | |
477 Node Unreachable() { | |
478 LOG(out << "Unreachable() = "); | |
479 auto *Instr = InstUnreachable::create(Func); | |
480 Control()->appendInst(Instr); | |
481 LOG(out << Node(nullptr) << "\n"); | |
482 return OperandNode(nullptr); | |
483 } | |
484 | |
485 Node CallDirect(uint32_t Index, Node *Args) { | |
486 LOG(out << "CallDirect(" << Index << ")" | |
487 << "\n"); | |
488 assert(Module->IsValidFunction(Index)); | |
489 const auto *Module = this->Module->module; | |
490 assert(Module); | |
491 const auto &Target = Module->functions[Index]; | |
492 const auto *Sig = Target.sig; | |
493 assert(Sig); | |
494 const auto NumArgs = Sig->parameter_count(); | |
495 LOG(out << " number of args: " << NumArgs << "\n"); | |
496 | |
497 const auto TargetName = | |
498 Ctx->getGlobalString(Module->GetName(Target.name_offset)); | |
499 LOG(out << " target name: " << TargetName << "\n"); | |
500 | |
501 assert(Sig->return_count() <= 1); | |
502 | |
503 auto *TargetOperand = Ctx->getConstantSym(0, TargetName); | |
504 | |
505 auto *Dest = Sig->return_count() > 0 | |
506 ? makeVariable(toIceType(Sig->GetReturn())) | |
507 : nullptr; | |
508 auto *Call = InstCall::create(Func, NumArgs, Dest, TargetOperand, | |
509 false /* HasTailCall */); | |
510 for (int i = 0; i < NumArgs; ++i) { | |
511 // The builder reserves the first argument for the code object. | |
512 LOG(out << " args[" << i << "] = " << Args[i + 1] << "\n"); | |
513 Call->addArg(Args[i + 1]); | |
514 } | |
515 | |
516 Control()->appendInst(Call); | |
517 LOG(out << "Call Result = " << Node(Dest) << "\n"); | |
518 return OperandNode(Dest); | |
519 } | |
520 Node CallImport(uint32_t Index, Node *Args) { | |
521 LOG(out << "CallImport(" << Index << ")" | |
522 << "\n"); | |
523 const auto *Module = this->Module->module; | |
524 assert(Module); | |
525 const auto *Sig = this->Module->GetImportSignature(Index); | |
526 assert(Sig); | |
527 const auto NumArgs = Sig->parameter_count(); | |
528 LOG(out << " number of args: " << NumArgs << "\n"); | |
529 | |
530 const auto &Target = Module->import_table[Index]; | |
531 const auto TargetName = | |
532 Ctx->getGlobalString(Module->GetName(Target.function_name_offset)); | |
533 LOG(out << " target name: " << TargetName << "\n"); | |
534 | |
535 assert(Sig->return_count() <= 1); | |
536 | |
537 auto *TargetOperand = Ctx->getConstantSym(0, TargetName); | |
538 | |
539 auto *Dest = Sig->return_count() > 0 | |
540 ? makeVariable(toIceType(Sig->GetReturn())) | |
541 : nullptr; | |
542 constexpr bool NoTailCall = false; | |
543 auto *Call = | |
544 InstCall::create(Func, NumArgs, Dest, TargetOperand, NoTailCall); | |
545 for (int i = 0; i < NumArgs; ++i) { | |
546 // The builder reserves the first argument for the code object. | |
547 LOG(out << " args[" << i << "] = " << Args[i + 1] << "\n"); | |
548 Call->addArg(Args[i + 1]); | |
549 } | |
550 | |
551 Control()->appendInst(Call); | |
552 LOG(out << "Call Result = " << Node(Dest) << "\n"); | |
553 return OperandNode(Dest); | |
554 } | |
555 Node CallIndirect(uint32_t Index, Node *Args) { | |
556 llvm::report_fatal_error("CallIndirect"); | |
557 } | |
558 Node Invert(Node Node) { llvm::report_fatal_error("Invert"); } | |
559 Node FunctionTable() { llvm::report_fatal_error("FunctionTable"); } | |
560 | |
561 //----------------------------------------------------------------------- | |
562 // Operations that concern the linear memory. | |
563 //----------------------------------------------------------------------- | |
564 Node MemSize(uint32_t Offset) { llvm::report_fatal_error("MemSize"); } | |
565 Node LoadGlobal(uint32_t Index) { llvm::report_fatal_error("LoadGlobal"); } | |
566 Node StoreGlobal(uint32_t Index, Node Val) { | |
567 llvm::report_fatal_error("StoreGlobal"); | |
568 } | |
569 Node LoadMem(wasm::LocalType Type, MachineType MemType, Node Index, | |
570 uint32_t Offset) { | |
571 LOG(out << "LoadMem(" << Index << "[" << Offset << "]) = "); | |
572 | |
573 // first, add the index and the offset together. | |
574 auto *OffsetConstant = Ctx->getConstantInt32(Offset); | |
575 auto *Addr = makeVariable(IceType_i32); | |
576 Control()->appendInst(InstArithmetic::create(Func, InstArithmetic::Add, | |
577 Addr, Index, OffsetConstant)); | |
578 | |
579 // then load the memory | |
580 auto *LoadResult = makeVariable(toIceType(MemType)); | |
581 Control()->appendInst(InstLoad::create(Func, LoadResult, Addr)); | |
582 | |
583 // and cast, if needed | |
584 Ice::Variable *Result = nullptr; | |
585 if (toIceType(Type) != toIceType(MemType)) { | |
586 Result = makeVariable(toIceType(Type)); | |
587 // TODO(eholk): handle signs correctly. | |
588 Control()->appendInst( | |
589 InstCast::create(Func, InstCast::Sext, Result, LoadResult)); | |
590 } else { | |
591 Result = LoadResult; | |
592 } | |
593 | |
594 LOG(out << Result << "\n"); | |
595 return OperandNode(Result); | |
596 } | |
597 void StoreMem(MachineType Type, Node Index, uint32_t Offset, Node Val) { | |
598 LOG(out << "StoreMem(" << Index << "[" << Offset << "] = " << Val << ")" | |
599 << "\n"); | |
600 | |
601 // TODO(eholk): surely there is a better way to do this. | |
602 | |
603 // first, add the index and the offset together. | |
604 auto *OffsetConstant = Ctx->getConstantInt32(Offset); | |
605 auto *Addr = makeVariable(IceType_i32); | |
606 Control()->appendInst(InstArithmetic::create(Func, InstArithmetic::Add, | |
607 Addr, Index, OffsetConstant)); | |
608 | |
609 // cast the value to the right type, if needed | |
610 Operand *StoreVal = nullptr; | |
611 if (toIceType(Type) != Val.toOperand()->getType()) { | |
612 auto *LocalStoreVal = makeVariable(toIceType(Type)); | |
613 Control()->appendInst( | |
614 InstCast::create(Func, InstCast::Trunc, LocalStoreVal, Val)); | |
615 StoreVal = LocalStoreVal; | |
616 } else { | |
617 StoreVal = Val; | |
618 } | |
619 | |
620 // then store the memory | |
621 Control()->appendInst(InstStore::create(Func, StoreVal, Addr)); | |
622 } | |
623 | |
624 static void PrintDebugName(Node node) { | |
625 llvm::report_fatal_error("PrintDebugName"); | |
626 } | |
627 | |
628 CfgNode *Control() { | |
629 return ControlPtr ? ControlPtr->toNode() : Func->getEntryNode(); | |
630 } | |
631 Node Effect() { return *EffectPtr; } | |
632 | |
633 void set_module(wasm::ModuleEnv *Module) { this->Module = Module; } | |
634 | |
635 void set_control_ptr(Node *Control) { this->ControlPtr = Control; } | |
636 | |
637 void set_effect_ptr(Node *Effect) { this->EffectPtr = Effect; } | |
638 | |
639 private: | |
640 wasm::ModuleEnv *Module; | |
641 Node *ControlPtr; | |
642 Node *EffectPtr; | |
643 | |
644 class Cfg *Func; | |
645 GlobalContext *Ctx; | |
646 | |
647 SizeT NextArg = 0; | |
648 | |
649 CfgUnorderedMap<Operand *, InstPhi *> PhiMap; | |
650 CfgUnorderedMap<Operand *, CfgNode *> DefNodeMap; | |
651 | |
652 InstPhi *getDefiningInst(Operand *Op) const { | |
653 const auto &Iter = PhiMap.find(Op); | |
654 if (Iter == PhiMap.end()) { | |
655 return nullptr; | |
656 } | |
657 return Iter->second; | |
658 } | |
659 | |
660 void setDefiningInst(Operand *Op, InstPhi *Phi) { | |
661 LOG(out << "\n== setDefiningInst(" << Op << ", " << Phi << ") ==\n"); | |
662 PhiMap.emplace(Op, Phi); | |
663 } | |
664 | |
665 Ice::Variable *makeVariable(Ice::Type Type) { | |
666 return makeVariable(Type, Control()); | |
667 } | |
668 | |
669 Ice::Variable *makeVariable(Ice::Type Type, CfgNode *DefNode) { | |
670 auto *Var = Func->makeVariable(Type); | |
671 DefNodeMap.emplace(Var, DefNode); | |
672 return Var; | |
673 } | |
674 | |
675 CfgNode *getDefNode(Operand *Op) const { | |
676 const auto &Iter = DefNodeMap.find(Op); | |
677 if (Iter == DefNodeMap.end()) { | |
678 return nullptr; | |
679 } | |
680 return Iter->second; | |
681 } | |
682 | |
683 template <typename F = std::function<void(Ostream &)>> void log(F Fn) const { | |
684 if (BuildDefs::dump() && (Ctx->getFlags().getVerbose() & IceV_Wasm)) { | |
685 Fn(Ctx->getStrDump()); | |
686 Ctx->getStrDump().flush(); | |
687 } | |
688 } | |
689 }; | |
690 | |
691 std::string fnNameFromId(uint32_t Id) { | |
692 return std::string("fn") + to_string(Id); | |
693 } | |
694 | |
695 std::unique_ptr<Cfg> WasmTranslator::translateFunction(Zone *Zone, | |
696 FunctionEnv *Env, | |
697 const byte *Base, | |
698 const byte *Start, | |
699 const byte *End) { | |
700 OstreamLocker L1(Ctx); | |
701 auto Func = Cfg::create(Ctx, getNextSequenceNumber()); | |
702 Ice::CfgLocalAllocatorScope L2(Func.get()); | |
703 | |
704 // TODO: parse the function signature... | |
705 | |
706 IceBuilder Builder(Func.get()); | |
707 LR_WasmDecoder<OperandNode, IceBuilder> Decoder(Zone, &Builder); | |
708 | |
709 LOG(out << Ctx->getFlags().getDefaultGlobalPrefix() << "\n"); | |
710 Decoder.Decode(Env, Base, Start, End); | |
711 | |
712 // We don't always know where the incoming branches are in phi nodes, so this | |
713 // function finds them. | |
714 Func->fixPhiNodes(); | |
715 | |
716 return Func; | |
717 } | |
718 | |
719 WasmTranslator::WasmTranslator(GlobalContext *Ctx) | |
720 : Translator(Ctx), BufferSize(24 << 10), Buffer(new uint8_t[24 << 10]) { | |
721 // TODO(eholk): compute the correct buffer size. This uses 24k by default, | |
722 // which has been big enough for testing but is not a general solution. | |
723 } | |
724 | |
725 void WasmTranslator::translate( | |
726 const std::string &IRFilename, | |
727 std::unique_ptr<llvm::DataStreamer> InputStream) { | |
728 LOG(out << "Initializing v8/wasm stuff..." | |
729 << "\n"); | |
730 Zone Zone; | |
731 ZoneScope _(&Zone); | |
732 | |
733 SizeT BytesRead = InputStream->GetBytes(Buffer.get(), BufferSize); | |
734 LOG(out << "Read " << BytesRead << " bytes" | |
735 << "\n"); | |
736 | |
737 LOG(out << "Decoding module " << IRFilename << "\n"); | |
738 | |
739 auto Result = DecodeWasmModule( | |
740 nullptr /* DecodeWasmModule ignores the Isolate | |
Jim Stichnoth
2016/04/04 21:26:51
Whatever *NoIsolate = nullptr;
auto Result = Dec
Eric Holk
2016/04/04 22:23:23
Done.
| |
741 * parameter, so we just pass a null | |
742 * one rather than go through the | |
743 * hullabaloo of making one. */, &Zone, Buffer.get(), | |
Jim Stichnoth
2016/04/04 21:26:52
80-col
Eric Holk
2016/04/04 22:23:23
Done.
| |
744 Buffer.get() + BytesRead, false, kWasmOrigin); | |
745 | |
746 auto Module = Result.val; | |
747 | |
748 LOG(out << "Module info:" | |
749 << "\n"); | |
750 LOG(out << " number of globals: " << Module->globals.size() << "\n"); | |
751 LOG(out << " number of signatures: " << Module->signatures.size() | |
752 << "\n"); | |
753 LOG(out << " number of functions: " << Module->functions.size() << "\n"); | |
754 LOG(out << " number of data_segments: " << Module->data_segments.size() | |
755 << "\n"); | |
756 LOG(out << " function table size: " << Module->function_table.size() | |
757 << "\n"); | |
758 | |
759 ModuleEnv ModuleEnv; | |
760 ModuleEnv.module = Module; | |
761 | |
762 LOG(out << "\n" | |
763 << "Function information:" | |
764 << "\n"); | |
765 for (const auto F : Module->functions) { | |
766 LOG(out << " " << F.name_offset << ": " << Module->GetName(F.name_offset)); | |
767 if (F.exported) | |
768 LOG(out << " export"); | |
769 if (F.external) | |
770 LOG(out << " extern"); | |
771 LOG(out << "\n"); | |
772 } | |
773 | |
774 FunctionEnv Fenv; | |
775 Fenv.module = &ModuleEnv; | |
776 | |
777 LOG(out << "Translating " << IRFilename << "\n"); | |
778 | |
779 // Translate each function. | |
780 uint32_t Id = 0; | |
781 for (const auto Fn : Module->functions) { | |
782 std::string NewName = fnNameFromId(Id++); | |
783 LOG(out << " " << Fn.name_offset << ": " << Module->GetName(Fn.name_offset) | |
784 << " -> " << NewName << "..."); | |
785 | |
786 Fenv.sig = Fn.sig; | |
787 Fenv.local_i32_count = Fn.local_i32_count; | |
788 Fenv.local_i64_count = Fn.local_i64_count; | |
789 Fenv.local_f32_count = Fn.local_f32_count; | |
790 Fenv.local_f64_count = Fn.local_f64_count; | |
791 Fenv.SumLocals(); | |
792 | |
793 auto Func = translateFunction(&Zone, &Fenv, Buffer.get(), | |
794 Buffer.get() + Fn.code_start_offset, | |
795 Buffer.get() + Fn.code_end_offset); | |
796 Func->setFunctionName(Ctx->getGlobalString(NewName)); | |
797 | |
798 Ctx->optQueueBlockingPush(makeUnique<CfgOptWorkItem>(std::move(Func))); | |
799 LOG(out << "done.\n"); | |
800 } | |
801 | |
802 return; | |
803 } | |
OLD | NEW |