Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: src/WasmTranslator.cpp

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

Powered by Google App Engine
This is Rietveld 408576698