Chromium Code Reviews| 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 //===----------------------------------------------------------------------===// | |
| 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 | |
| 17 #include "src/zone.h" | |
| 18 | |
| 19 #include "IceGlobalInits.h" | |
| 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 Ice::Type toIceType(v8::internal::MachineType) { | |
| 32 // TODO(eholk): actually convert this. | |
| 33 return IceType_i32; | |
| 34 } | |
| 35 | |
| 36 Ice::Type toIceType(wasm::LocalType Type) { | |
| 37 | |
|
Jim Stichnoth
2016/04/01 01:46:45
remove leading blank line
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 38 switch (Type) { | |
| 39 default: | |
| 40 llvm_unreachable("unexpected enum value"); | |
|
Jim Stichnoth
2016/04/01 01:46:44
Favor llvm::report_fatal_error over llvm_unreachab
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 41 case MachineRepresentation::kNone: | |
| 42 llvm_unreachable("kNone type not supported"); | |
| 43 case MachineRepresentation::kBit: | |
| 44 return IceType_i1; | |
| 45 case MachineRepresentation::kWord8: | |
| 46 return IceType_i8; | |
| 47 case MachineRepresentation::kWord16: | |
| 48 return IceType_i16; | |
| 49 case MachineRepresentation::kWord32: | |
| 50 return IceType_i32; | |
| 51 case MachineRepresentation::kWord64: | |
| 52 return IceType_i64; | |
| 53 case MachineRepresentation::kFloat32: | |
| 54 return IceType_f32; | |
| 55 case MachineRepresentation::kFloat64: | |
| 56 return IceType_f64; | |
| 57 case MachineRepresentation::kSimd128: | |
| 58 llvm_unreachable("ambiguous SIMD type"); | |
| 59 case MachineRepresentation::kTagged: | |
| 60 llvm_unreachable("kTagged type not supported"); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 /** | |
|
Jim Stichnoth
2016/04/01 01:46:44
Subzero block commenting style is usually "//" pre
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 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 let's V8's | |
|
Jim Stichnoth
2016/04/01 01:46:45
lets
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 69 Wasm decoder treat Subzero objects as though they are all the same. | |
| 70 */ | |
| 71 class OperandNode { | |
| 72 static constexpr uintptr_t NODE_FLAG = 1; | |
| 73 static constexpr uintptr_t UNDEF_PTR = (uintptr_t)-1; | |
| 74 | |
| 75 public: | |
| 76 uintptr_t Data; | |
|
Jim Stichnoth
2016/04/01 01:46:45
Maybe use "uintptr_t Data = UNDEF_PTR;"? Then you
Eric Holk
2016/04/01 19:15:02
Done.
The compiler still wouldn't accept default
| |
| 77 | |
| 78 OperandNode() : Data(UNDEF_PTR) {} | |
| 79 OperandNode(Operand *Operand) : Data((uintptr_t)Operand) {} | |
|
Jim Stichnoth
2016/04/01 01:46:44
I think this sort of casting would be better off a
Jim Stichnoth
2016/04/01 01:46:45
mark these ctors explicit
Eric Holk
2016/04/01 19:15:02
Done.
This required a little bit more change on t
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 80 OperandNode(CfgNode *Node) : Data((uintptr_t)Node | NODE_FLAG) {} | |
| 81 OperandNode(nullptr_t) : Data(UNDEF_PTR) {} | |
| 82 | |
| 83 operator Operand *() const { | |
| 84 if (UNDEF_PTR == Data) { | |
| 85 return nullptr; | |
| 86 } | |
| 87 assert(isOperand()); | |
| 88 return reinterpret_cast<Operand *>(Data); | |
| 89 } | |
| 90 | |
| 91 operator CfgNode *() const { | |
| 92 if (UNDEF_PTR == Data) { | |
| 93 return nullptr; | |
| 94 } | |
| 95 assert(isCfgNode()); | |
| 96 return reinterpret_cast<CfgNode *>(Data & ~NODE_FLAG); | |
| 97 } | |
| 98 | |
| 99 explicit operator bool() const { return (Data != UNDEF_PTR) && Data; } | |
| 100 bool operator==(const OperandNode &Rhs) const { | |
| 101 return (Data == Rhs.Data) || | |
| 102 (UNDEF_PTR == Data && (Rhs.Data == 0 || Rhs.Data == NODE_FLAG)) || | |
| 103 (UNDEF_PTR == Rhs.Data && (Data == 0 || Data == NODE_FLAG)); | |
| 104 } | |
| 105 bool operator!=(const OperandNode &Rhs) const { return !(*this == Rhs); } | |
| 106 | |
| 107 bool isOperand() const { return !(Data & NODE_FLAG); } | |
| 108 bool isCfgNode() const { return Data & NODE_FLAG; } | |
|
Jim Stichnoth
2016/04/01 01:46:45
So UNDEF_PTR is considered to be a CfgNode and not
Eric Holk
2016/04/01 19:15:02
Good catch. Your suggestion was actually the inten
| |
| 109 }; | |
| 110 | |
| 111 Ostream &operator<<(Ostream &Out, const OperandNode &Op) { | |
| 112 if (Op.isOperand()) { | |
| 113 auto Oper = static_cast<Operand *>(Op); | |
|
Jim Stichnoth
2016/04/01 01:46:44
auto *
Jim Stichnoth
2016/04/01 01:46:45
There are an awful lot of static_casts in this fil
Eric Holk
2016/04/01 19:15:02
I think John suggested this too, and I've now seen
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 114 Out << "(Operand*)" << Oper; | |
| 115 } else if (Op.isCfgNode()) { | |
| 116 auto Node = static_cast<CfgNode *>(Op); | |
|
Jim Stichnoth
2016/04/01 01:46:44
auto *
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 117 Out << "(CfgNode*)" << Node; | |
| 118 } else { | |
| 119 Out << "nullptr"; | |
| 120 } | |
| 121 return Out; | |
| 122 } | |
| 123 | |
| 124 constexpr bool isComparison(wasm::WasmOpcode Opcode) { | |
| 125 switch (Opcode) { | |
| 126 case kExprI32Ne: | |
| 127 case kExprI64Ne: | |
| 128 case kExprI32Eq: | |
| 129 case kExprI64Eq: | |
| 130 case kExprI32LtS: | |
| 131 case kExprI64LtS: | |
| 132 case kExprI32LtU: | |
| 133 case kExprI64LtU: | |
| 134 case kExprI32GeS: | |
| 135 case kExprI64GeS: | |
| 136 case kExprI32GtS: | |
| 137 case kExprI64GtS: | |
| 138 case kExprI32GtU: | |
| 139 case kExprI64GtU: | |
| 140 return true; | |
| 141 default: | |
| 142 return false; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 #define LOG(Expr) log([&](Ostream & out) { Expr; }) | |
|
Jim Stichnoth
2016/04/01 01:46:44
I would move macro definitions like this closer to
Eric Holk
2016/04/01 19:15:01
Done.
| |
| 147 | |
| 148 class IceBuilder { | |
| 149 using Node = OperandNode; | |
| 150 | |
| 151 IceBuilder() = delete; | |
| 152 IceBuilder(const IceBuilder &) = delete; | |
| 153 IceBuilder &operator=(const IceBuilder &) = delete; | |
| 154 | |
| 155 public: | |
| 156 IceBuilder(class Cfg *Cfg) | |
|
Jim Stichnoth
2016/04/01 01:46:44
explicit
Jim Stichnoth
2016/04/01 01:46:44
Don't use a type name for a variable name. We use
Eric Holk
2016/04/01 19:15:01
Done.
Eric Holk
2016/04/01 19:15:01
Done.
| |
| 157 : Cfg(Cfg), Ctx(Cfg->getContext()), ControlPtr(nullptr) {} | |
| 158 | |
| 159 /// Allocates a buffer of Nodes for use by V8. | |
| 160 Node *Buffer(size_t Count) { | |
|
Jim Stichnoth
2016/04/01 01:46:44
All these method names should start with lowercase
Eric Holk
2016/04/01 19:15:02
Right, these are called by V8, so we have to follo
| |
| 161 LOG(out << "Buffer(" << Count << ")\n"); | |
| 162 return Cfg->allocateArrayOf<Node>(Count); | |
| 163 } | |
| 164 | |
| 165 Node Error() { llvm_unreachable("Error"); } | |
| 166 Node Start(unsigned params) { | |
|
Jim Stichnoth
2016/04/01 01:46:44
capitalize Params
Eric Holk
2016/04/01 19:15:01
Done.
| |
| 167 LOG(out << "Start(" << params << ") = "); | |
| 168 auto *Entry = Cfg->makeNode(); | |
| 169 Cfg->setEntryNode(Entry); | |
| 170 LOG(out << Node(Entry) << "\n"); | |
| 171 return Entry; | |
| 172 } | |
| 173 Node Param(unsigned Index, wasm::LocalType Type) { | |
| 174 LOG(out << "Param(" << Index << ") = "); | |
| 175 auto *Arg = makeVariable(toIceType(Type)); | |
| 176 assert(Index == NextArg); | |
| 177 Cfg->addArg(Arg); | |
| 178 NextArg++; | |
|
Jim Stichnoth
2016/04/01 01:46:44
I think it's best to just get used to pre-incremen
Eric Holk
2016/04/01 19:15:01
Done.
| |
| 179 LOG(out << Node(Arg) << "\n"); | |
| 180 return Arg; | |
| 181 } | |
| 182 CfgNode *Loop(CfgNode *Entry) { | |
| 183 auto *Loop = Cfg->makeNode(); | |
| 184 LOG(out << "Loop(" << Entry << ") = " << Loop << "\n"); | |
| 185 Entry->appendInst(InstBr::create(Cfg, Loop)); | |
| 186 return Loop; | |
| 187 } | |
| 188 void Terminate(Node Effect, Node Control) { | |
| 189 // TODO(eholk): this is almost certainly wrong | |
| 190 LOG(out << "Terminate(" << Effect << ", " << Control << ")" | |
| 191 << "\n"); | |
| 192 } | |
| 193 Node Merge(unsigned Count, Node *Controls) { | |
| 194 LOG(out << "Merge(" << Count); | |
| 195 for (unsigned i = 0; i < Count; ++i) { | |
| 196 LOG(out << ", " << Controls[i]); | |
| 197 } | |
| 198 LOG(out << ") = "); | |
| 199 | |
| 200 auto *MergedNode = Cfg->makeNode(); | |
| 201 | |
| 202 for (unsigned i = 0; i < Count; ++i) { | |
| 203 CfgNode *Control = Controls[i]; | |
| 204 Control->appendInst(InstBr::create(Cfg, MergedNode)); | |
| 205 } | |
| 206 LOG(out << (OperandNode)MergedNode << "\n"); | |
| 207 return MergedNode; | |
| 208 } | |
| 209 Node Phi(wasm::LocalType Type, unsigned Count, Node *Vals, Node Control) { | |
| 210 LOG(out << "Phi(" << Count << ", " << Control); | |
| 211 for (int i = 0; i < Count; ++i) { | |
| 212 LOG(out << ", " << Vals[i]); | |
| 213 } | |
| 214 LOG(out << ") = "); | |
| 215 | |
| 216 const auto InEdges = static_cast<CfgNode *>(Control)->getInEdges(); | |
|
Jim Stichnoth
2016/04/01 01:46:44
const auto & (I think)
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 217 assert(Count == InEdges.size()); | |
| 218 | |
| 219 assert(Count > 0); | |
| 220 | |
| 221 auto *Dest = | |
| 222 makeVariable(static_cast<Operand *>(Vals[0])->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. | |
|
Jim Stichnoth
2016/04/01 01:46:44
Yeah -- here's where you eventually might just hav
Eric Holk
2016/04/01 19:15:02
I was thinking about this some more, and it's poss
| |
| 228 auto *Phi = InstPhi::create(Cfg, Count * 10, Dest); | |
| 229 for (int i = 0; i < Count; ++i) { | |
| 230 auto *Op = static_cast<Operand *>(Vals[i]); | |
| 231 assert(Op); | |
| 232 Phi->addArgument(Op, InEdges[i]); | |
| 233 } | |
| 234 setDefiningInst(Dest, Phi); | |
| 235 static_cast<CfgNode *>(Control)->appendInst(Phi); | |
| 236 LOG(out << Node(Dest) << "\n"); | |
| 237 return 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 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 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 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 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 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(isComparison(Opcode) | |
|
Jim Stichnoth
2016/04/01 01:46:44
Just checking - does wasm allow variables and func
Eric Holk
2016/04/01 19:15:01
I think imports and exports have explicit names (a
| |
| 280 ? IceType_i1 | |
| 281 : (static_cast<Operand *>(Left)->getType())); | |
| 282 switch (Opcode) { | |
| 283 case kExprI32Add: | |
| 284 case kExprI64Add: | |
| 285 Control()->appendInst( | |
| 286 InstArithmetic::create(Cfg, InstArithmetic::Add, Dest, Left, Right)); | |
| 287 break; | |
| 288 case kExprI32Sub: | |
| 289 case kExprI64Sub: | |
| 290 Control()->appendInst( | |
| 291 InstArithmetic::create(Cfg, InstArithmetic::Sub, Dest, Left, Right)); | |
| 292 break; | |
| 293 case kExprI32Mul: | |
| 294 case kExprI64Mul: | |
| 295 Control()->appendInst( | |
| 296 InstArithmetic::create(Cfg, InstArithmetic::Mul, Dest, Left, Right)); | |
| 297 break; | |
| 298 case kExprI32DivU: | |
| 299 case kExprI64DivU: | |
| 300 Control()->appendInst( | |
| 301 InstArithmetic::create(Cfg, InstArithmetic::Udiv, Dest, Left, Right)); | |
| 302 break; | |
| 303 case kExprI32RemU: | |
| 304 case kExprI64RemU: | |
| 305 Control()->appendInst( | |
| 306 InstArithmetic::create(Cfg, InstArithmetic::Urem, Dest, Left, Right)); | |
| 307 break; | |
| 308 case kExprI32Ior: | |
| 309 case kExprI64Ior: | |
| 310 Control()->appendInst( | |
| 311 InstArithmetic::create(Cfg, InstArithmetic::Or, Dest, Left, Right)); | |
| 312 break; | |
| 313 case kExprI32Xor: | |
| 314 case kExprI64Xor: | |
| 315 Control()->appendInst( | |
| 316 InstArithmetic::create(Cfg, InstArithmetic::Xor, Dest, Left, Right)); | |
| 317 break; | |
| 318 case kExprI32Shl: | |
| 319 case kExprI64Shl: | |
| 320 Control()->appendInst( | |
| 321 InstArithmetic::create(Cfg, InstArithmetic::Shl, Dest, Left, Right)); | |
| 322 break; | |
| 323 case kExprI32ShrU: | |
| 324 case kExprI64ShrU: | |
| 325 case kExprI32ShrS: | |
| 326 case kExprI64ShrS: | |
| 327 Control()->appendInst( | |
| 328 InstArithmetic::create(Cfg, InstArithmetic::Ashr, Dest, Left, Right)); | |
| 329 break; | |
| 330 case kExprI32And: | |
| 331 case kExprI64And: | |
| 332 Control()->appendInst( | |
| 333 InstArithmetic::create(Cfg, InstArithmetic::And, Dest, Left, Right)); | |
| 334 break; | |
| 335 case kExprI32Ne: | |
| 336 case kExprI64Ne: | |
| 337 Control()->appendInst( | |
| 338 InstIcmp::create(Cfg, InstIcmp::Ne, Dest, Left, Right)); | |
| 339 break; | |
| 340 case kExprI32Eq: | |
| 341 case kExprI64Eq: | |
| 342 Control()->appendInst( | |
| 343 InstIcmp::create(Cfg, InstIcmp::Eq, Dest, Left, Right)); | |
| 344 break; | |
| 345 case kExprI32LtS: | |
| 346 case kExprI64LtS: | |
| 347 Control()->appendInst( | |
| 348 InstIcmp::create(Cfg, InstIcmp::Slt, Dest, Left, Right)); | |
| 349 break; | |
| 350 case kExprI32LtU: | |
| 351 case kExprI64LtU: | |
| 352 Control()->appendInst( | |
| 353 InstIcmp::create(Cfg, InstIcmp::Ult, Dest, Left, Right)); | |
| 354 break; | |
| 355 case kExprI32GeS: | |
| 356 case kExprI64GeS: | |
| 357 Control()->appendInst( | |
| 358 InstIcmp::create(Cfg, InstIcmp::Sge, Dest, Left, Right)); | |
| 359 case kExprI32GtS: | |
| 360 case kExprI64GtS: | |
| 361 Control()->appendInst( | |
| 362 InstIcmp::create(Cfg, InstIcmp::Sgt, Dest, Left, Right)); | |
| 363 break; | |
| 364 case kExprI32GtU: | |
| 365 case kExprI64GtU: | |
| 366 Control()->appendInst( | |
| 367 InstIcmp::create(Cfg, InstIcmp::Ugt, Dest, Left, Right)); | |
| 368 break; | |
| 369 default: | |
| 370 LOG(out << "Unknown binop: " << WasmOpcodes::OpcodeName(Opcode) << "\n"); | |
| 371 llvm_unreachable("Uncovered or invalid binop."); | |
| 372 return nullptr; | |
| 373 } | |
| 374 LOG(out << Dest << "\n"); | |
| 375 return Dest; | |
| 376 } | |
| 377 Node Unop(wasm::WasmOpcode Opcode, Node Input) { | |
| 378 LOG(out << "Unop(" << WasmOpcodes::OpcodeName(Opcode) << ", " << Input | |
| 379 << ") = "); | |
| 380 Ice::Variable *Dest = nullptr; | |
| 381 switch (Opcode) { | |
| 382 case kExprF32Neg: { | |
| 383 Dest = makeVariable(IceType_f32); | |
| 384 auto *Zero = makeVariable(IceType_f32); | |
|
Jim Stichnoth
2016/04/01 01:46:44
auto *_0 = Ctx->getConstantZero(IceType_f32);
Cont
Eric Holk
2016/04/01 19:15:02
Right, I don't know why I thought you had to do it
| |
| 385 Control()->appendInst( | |
| 386 InstAssign::create(Cfg, Zero, Ctx->getConstantFloat(0))); | |
| 387 Control()->appendInst( | |
| 388 InstArithmetic::create(Cfg, InstArithmetic::Fsub, Dest, Zero, Input)); | |
| 389 break; | |
| 390 } | |
| 391 case kExprF64Neg: { | |
| 392 Dest = makeVariable(IceType_f64); | |
| 393 auto *Zero = makeVariable(IceType_f64); | |
| 394 Control()->appendInst( | |
| 395 InstAssign::create(Cfg, Zero, Ctx->getConstantDouble(0))); | |
| 396 Control()->appendInst( | |
| 397 InstArithmetic::create(Cfg, InstArithmetic::Fsub, Dest, Zero, Input)); | |
| 398 break; | |
| 399 } | |
| 400 case kExprI64UConvertI32: | |
| 401 Dest = makeVariable(IceType_i64); | |
| 402 Control()->appendInst(InstCast::create(Cfg, InstCast::Zext, Dest, Input)); | |
| 403 break; | |
| 404 default: | |
| 405 LOG(out << "Unknown unop: " << WasmOpcodes::OpcodeName(Opcode) << "\n"); | |
| 406 llvm_unreachable("Uncovered or invalid unop."); | |
| 407 return nullptr; | |
| 408 } | |
| 409 LOG(out << Dest << "\n"); | |
| 410 return Dest; | |
| 411 } | |
| 412 unsigned InputCount(CfgNode *Node) { return Node->getInEdges().size(); } | |
| 413 bool IsPhiWithMerge(Node Phi, Node Merge) { | |
| 414 LOG(out << "IsPhiWithMerge(" << Phi << ", " << Merge << ")" | |
|
Jim Stichnoth
2016/04/01 01:46:44
Did you run "make format"? I'm surprised this sta
Eric Holk
2016/04/01 19:15:01
I may have changed this since the last time I ran
| |
| 415 << "\n"); | |
| 416 if (Phi && Phi.isOperand()) { | |
| 417 LOG(out << " ...is operand" | |
| 418 << "\n"); | |
| 419 if (auto *Inst = getDefiningInst(Phi)) { | |
| 420 LOG(out << " ...has defining instruction" | |
| 421 << "\n"); | |
| 422 LOG(out << getDefNode(Phi) << "\n"); | |
| 423 LOG(out << " ..." << (getDefNode(Phi) == Merge) << "\n"); | |
| 424 return getDefNode(Phi) == Merge; | |
| 425 } | |
| 426 } | |
| 427 return false; | |
| 428 } | |
| 429 void AppendToMerge(CfgNode *Merge, CfgNode *From) const { | |
| 430 From->appendInst(InstBr::create(Cfg, Merge)); | |
| 431 } | |
| 432 void AppendToPhi(Node Merge, Node Phi, Node From) { | |
| 433 LOG(out << "AppendToPhi(" << Merge << ", " << Phi << ", " << From << ")" | |
| 434 << "\n"); | |
| 435 auto *Inst = getDefiningInst(Phi); | |
| 436 Inst->addArgument(From, getDefNode(From)); | |
| 437 } | |
| 438 | |
| 439 //----------------------------------------------------------------------- | |
| 440 // Operations that read and/or write {control} and {effect}. | |
| 441 //----------------------------------------------------------------------- | |
| 442 nullptr_t Branch(Node Cond, Node *TrueNode, Node *FalseNode) { | |
| 443 // true_node and false_node appear to be out parameters. | |
| 444 LOG(out << "Branch(" << Cond << ", "); | |
| 445 | |
| 446 // save control here because true_node appears to alias control. | |
| 447 auto *Ctrl = Control(); | |
| 448 | |
| 449 *TrueNode = Cfg->makeNode(); | |
| 450 *FalseNode = Cfg->makeNode(); | |
| 451 | |
| 452 LOG(out << *TrueNode << ", " << *FalseNode << ")" | |
| 453 << "\n"); | |
| 454 | |
| 455 Ctrl->appendInst(InstBr::create(Cfg, Cond, *TrueNode, *FalseNode)); | |
| 456 return nullptr; | |
| 457 } | |
| 458 Node Switch(unsigned Count, Node Key) { llvm_unreachable("Switch"); } | |
| 459 Node IfValue(int32_t Value, Node Sw) { llvm_unreachable("IfValue"); } | |
| 460 Node IfDefault(Node Sw) { llvm_unreachable("IfDefault"); } | |
| 461 Node Return(unsigned Count, Node *Vals) { | |
| 462 assert(1 >= Count); | |
| 463 LOG(out << "Return("); | |
| 464 if (Count > 0) | |
| 465 LOG(out << Vals[0]); | |
| 466 LOG(out << ")" | |
| 467 << "\n"); | |
| 468 auto *Instr = | |
| 469 1 == Count ? InstRet::create(Cfg, Vals[0]) : InstRet::create(Cfg); | |
| 470 Control()->appendInst(Instr); | |
| 471 Control()->setHasReturn(); | |
| 472 LOG(out << Node(nullptr) << "\n"); | |
| 473 return nullptr; | |
| 474 } | |
| 475 Node ReturnVoid() { | |
| 476 LOG(out << "ReturnVoid() = "); | |
| 477 auto *Instr = InstRet::create(Cfg); | |
| 478 Control()->appendInst(Instr); | |
| 479 Control()->setHasReturn(); | |
| 480 LOG(out << Node(nullptr) << "\n"); | |
| 481 return nullptr; | |
| 482 } | |
| 483 Node Unreachable() { | |
| 484 LOG(out << "Unreachable() = "); | |
| 485 auto *Instr = InstUnreachable::create(Cfg); | |
| 486 Control()->appendInst(Instr); | |
| 487 LOG(out << Node(nullptr) << "\n"); | |
| 488 return nullptr; | |
| 489 } | |
| 490 | |
| 491 Node CallDirect(uint32_t Index, Node *Args) { | |
| 492 LOG(out << "CallDirect(" << Index << ")" | |
| 493 << "\n"); | |
| 494 assert(Module->IsValidFunction(Index)); | |
| 495 const auto *Module = this->Module->module; | |
| 496 assert(Module); | |
| 497 const auto &Target = Module->functions[Index]; | |
| 498 const auto *Sig = Target.sig; | |
| 499 assert(Sig); | |
| 500 const auto NumArgs = Sig->parameter_count(); | |
| 501 LOG(out << " number of args: " << NumArgs << "\n"); | |
| 502 | |
| 503 const auto TargetName = Ctx->getGlobalString( | |
| 504 Module->GetName(Target.name_offset)); | |
| 505 LOG(out << " target name: " << TargetName << "\n"); | |
| 506 | |
| 507 assert(Sig->return_count() <= 1); | |
| 508 | |
| 509 auto *TargetOperand = Ctx->getConstantSym(0, TargetName); | |
| 510 | |
| 511 auto *Dest = Sig->return_count() > 0 | |
| 512 ? makeVariable(toIceType(Sig->GetReturn())) | |
| 513 : nullptr; | |
| 514 auto *Call = InstCall::create(Cfg, NumArgs, Dest, TargetOperand, | |
| 515 false /* HasTailCall */); | |
| 516 for (int i = 0; i < NumArgs; ++i) { | |
| 517 // The builder reserves the first argument for the code object. | |
| 518 LOG(out << " args[" << i << "] = " << Args[i + 1] << "\n"); | |
| 519 Call->addArg(Args[i + 1]); | |
| 520 } | |
| 521 | |
| 522 Control()->appendInst(Call); | |
| 523 LOG(out << "Call Result = " << Node(Dest) << "\n"); | |
| 524 return Dest; | |
| 525 } | |
| 526 Node CallImport(uint32_t Index, Node *Args) { | |
| 527 LOG(out << "CallImport(" << Index << ")" | |
| 528 << "\n"); | |
| 529 // assert(Module->IsValidFunction(index)); | |
|
Jim Stichnoth
2016/04/01 01:46:44
remove this?
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 530 const auto *Module = this->Module->module; | |
| 531 assert(Module); | |
| 532 const auto *Sig = this->Module->GetImportSignature(Index); | |
| 533 assert(Sig); | |
| 534 const auto NumArgs = Sig->parameter_count(); | |
| 535 LOG(out << " number of args: " << NumArgs << "\n"); | |
| 536 | |
| 537 const auto &Target = Module->import_table[Index]; | |
| 538 const auto TargetName = Ctx->getGlobalString( | |
| 539 Module->GetName(Target.function_name_offset)); | |
| 540 LOG(out << " target name: " << TargetName << "\n"); | |
| 541 | |
| 542 assert(Sig->return_count() <= 1); | |
| 543 | |
| 544 auto *TargetOperand = Ctx->getConstantSym(0, TargetName); | |
| 545 | |
| 546 auto *Dest = Sig->return_count() > 0 | |
| 547 ? makeVariable(toIceType(Sig->GetReturn())) | |
| 548 : nullptr; | |
| 549 auto *Call = InstCall::create(Cfg, NumArgs, Dest, TargetOperand, | |
|
Jim Stichnoth
2016/04/01 01:46:45
Subzero tends to document constant arguments like:
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 550 false /* HasTailCall */); | |
| 551 for (int i = 0; i < NumArgs; ++i) { | |
| 552 // The builder reserves the first argument for the code object. | |
| 553 LOG(out << " args[" << i << "] = " << Args[i + 1] << "\n"); | |
| 554 Call->addArg(Args[i + 1]); | |
| 555 } | |
| 556 | |
| 557 Control()->appendInst(Call); | |
| 558 LOG(out << "Call Result = " << Node(Dest) << "\n"); | |
| 559 return Dest; | |
| 560 } | |
| 561 Node CallIndirect(uint32_t Index, Node *Args) { | |
| 562 llvm_unreachable("CallIndirect"); | |
| 563 } | |
| 564 Node Invert(Node Node) { llvm_unreachable("Invert"); } | |
| 565 Node FunctionTable() { llvm_unreachable("FunctionTable"); } | |
| 566 | |
| 567 //----------------------------------------------------------------------- | |
| 568 // Operations that concern the linear memory. | |
| 569 //----------------------------------------------------------------------- | |
| 570 Node MemSize(uint32_t Offset) { llvm_unreachable("MemSize"); } | |
| 571 Node LoadGlobal(uint32_t Index) { llvm_unreachable("LoadGlobal"); } | |
| 572 Node StoreGlobal(uint32_t Index, Node Val) { | |
| 573 llvm_unreachable("StoreGlobal"); | |
| 574 } | |
| 575 Node LoadMem(wasm::LocalType Type, MachineType MemType, Node Index, | |
| 576 uint32_t Offset) { | |
| 577 LOG(out << "LoadMem(" << Index << "[" << Offset << "]) = "); | |
| 578 | |
| 579 // TODO(eholk): surely there is a better way to do this. | |
|
Jim Stichnoth
2016/04/01 01:46:44
Probably not - LLVM/PNaCl representation is pretty
Eric Holk
2016/04/01 19:15:02
Okay, I'll remove the comment.
| |
| 580 | |
| 581 // first, add the index and the offset together. | |
| 582 auto *OffsetConstant = Ctx->getConstantInt32(Offset); | |
| 583 auto *Addr = makeVariable(IceType_i32); | |
| 584 Control()->appendInst(InstArithmetic::create(Cfg, InstArithmetic::Add, Addr, | |
| 585 Index, OffsetConstant)); | |
| 586 | |
| 587 // then load the memory | |
| 588 auto *LoadResult = makeVariable(toIceType(MemType)); | |
| 589 Control()->appendInst(InstLoad::create(Cfg, LoadResult, Addr)); | |
| 590 | |
| 591 // and cast, if needed | |
| 592 Ice::Variable *Result = nullptr; | |
| 593 if (toIceType(Type) != toIceType(MemType)) { | |
| 594 Result = makeVariable(toIceType(Type)); | |
| 595 // TODO(eholk): handle signs correctly. | |
| 596 Control()->appendInst( | |
| 597 InstCast::create(Cfg, InstCast::Sext, Result, LoadResult)); | |
| 598 } else { | |
| 599 Result = LoadResult; | |
| 600 } | |
| 601 | |
| 602 LOG(out << Result << "\n"); | |
| 603 return Result; | |
| 604 } | |
| 605 void StoreMem(MachineType Type, Node Index, uint32_t Offset, Node Val) { | |
| 606 LOG(out << "StoreMem(" << Index << "[" << Offset << "] = " << Val << ")" | |
| 607 << "\n"); | |
| 608 | |
| 609 // TODO(eholk): surely there is a better way to do this. | |
| 610 | |
| 611 // first, add the index and the offset together. | |
| 612 auto *OffsetConstant = Ctx->getConstantInt32(Offset); | |
| 613 auto *Addr = makeVariable(IceType_i32); | |
| 614 Control()->appendInst(InstArithmetic::create(Cfg, InstArithmetic::Add, Addr, | |
| 615 Index, OffsetConstant)); | |
| 616 | |
| 617 // cast the value to the right type, if needed | |
| 618 Operand *StoreVal = nullptr; | |
| 619 if (toIceType(Type) != static_cast<Operand *>(Val)->getType()) { | |
| 620 auto *LocalStoreVal = makeVariable(toIceType(Type)); | |
| 621 Control()->appendInst( | |
| 622 InstCast::create(Cfg, InstCast::Trunc, LocalStoreVal, Val)); | |
| 623 StoreVal = LocalStoreVal; | |
| 624 } else { | |
| 625 StoreVal = Val; | |
| 626 } | |
| 627 | |
| 628 // then store the memory | |
| 629 Control()->appendInst(InstStore::create(Cfg, StoreVal, Addr)); | |
| 630 } | |
| 631 | |
| 632 static void PrintDebugName(Node node) { llvm_unreachable("PrintDebugName"); } | |
| 633 | |
| 634 CfgNode *Control() { | |
| 635 return ControlPtr ? static_cast<CfgNode *>(*ControlPtr) | |
| 636 : Cfg->getEntryNode(); | |
| 637 } | |
| 638 Node Effect() { return *EffectPtr; } | |
| 639 | |
| 640 void set_module(wasm::ModuleEnv *Module) { this->Module = Module; } | |
|
Jim Stichnoth
2016/04/01 01:46:45
The LLVM/Subzero naming convention would be more l
Eric Holk
2016/04/01 19:15:01
This is another function called by V8 so we have t
| |
| 641 | |
| 642 void set_control_ptr(Node *Control) { this->ControlPtr = Control; } | |
| 643 | |
| 644 void set_effect_ptr(Node *Effect) { this->EffectPtr = Effect; } | |
| 645 | |
| 646 private: | |
| 647 wasm::ModuleEnv *Module; | |
| 648 Node *ControlPtr; | |
| 649 Node *EffectPtr; | |
| 650 | |
| 651 class Cfg *Cfg; | |
| 652 GlobalContext *Ctx; | |
| 653 | |
| 654 SizeT NextArg = 0; | |
| 655 | |
| 656 CfgUnorderedMap<Operand *, InstPhi *> PhiMap; | |
| 657 CfgUnorderedMap<Operand *, CfgNode *> DefNodeMap; | |
| 658 | |
| 659 InstPhi *getDefiningInst(Operand *Op) const { | |
| 660 const auto &Iter = PhiMap.find(Op); | |
| 661 if (Iter == PhiMap.end()) { | |
| 662 return nullptr; | |
| 663 } | |
| 664 return Iter->second; | |
| 665 } | |
| 666 | |
| 667 void setDefiningInst(Operand *Op, InstPhi *Phi) { | |
| 668 LOG(out << "\n== setDefiningInst(" << Op << ", " << Phi << ") ==\n"); | |
| 669 PhiMap.emplace(Op, Phi); | |
| 670 } | |
| 671 | |
| 672 Ice::Variable *makeVariable(Ice::Type Type) { | |
| 673 return makeVariable(Type, Control()); | |
| 674 } | |
| 675 | |
| 676 Ice::Variable *makeVariable(Ice::Type Type, CfgNode *DefNode) { | |
| 677 auto *Var = Cfg->makeVariable(Type); | |
| 678 DefNodeMap.emplace(Var, DefNode); | |
| 679 return Var; | |
| 680 } | |
| 681 | |
| 682 CfgNode *getDefNode(Operand *Op) const { | |
| 683 const auto &Iter = DefNodeMap.find(Op); | |
| 684 if (Iter == DefNodeMap.end()) { | |
| 685 return nullptr; | |
| 686 } | |
| 687 return Iter->second; | |
| 688 } | |
| 689 | |
| 690 template <typename F = std::function<void(Ostream &)>> void log(F Fn) { | |
| 691 if (BuildDefs::dump() && (Ctx->getFlags().getVerbose() & IceV_Wasm)) { | |
| 692 OstreamLocker L(Ctx); | |
|
Jim Stichnoth
2016/04/01 01:46:45
This and the LOG() macro are clever.
The followin
Eric Holk
2016/04/01 19:15:01
Hopefully that's a good clever :)
| |
| 693 Fn(Ctx->getStrDump()); | |
| 694 Ctx->getStrDump().flush(); | |
| 695 } | |
| 696 } | |
| 697 }; | |
| 698 | |
| 699 std::string fnNameFromId(uint32_t Id) { | |
| 700 return std::string("fn") + to_string(Id); | |
| 701 } | |
| 702 | |
| 703 std::unique_ptr<Cfg> WasmTranslator::translateFunction(Zone *Zone, | |
| 704 FunctionEnv *Env, | |
| 705 const byte *Base, | |
| 706 const byte *Start, | |
| 707 const byte *End) { | |
| 708 auto Cfg = Cfg::create(Ctx, getNextSequenceNumber()); | |
| 709 Ice::CfgLocalAllocatorScope _(Cfg.get()); | |
| 710 | |
| 711 // TODO: parse the function signature... | |
| 712 | |
| 713 IceBuilder Builder(Cfg.get()); | |
| 714 LR_WasmDecoder<OperandNode, IceBuilder> Decoder(Zone, &Builder); | |
| 715 | |
| 716 LOG(out << Ctx->getFlags().getDefaultGlobalPrefix() << "\n"); | |
| 717 Decoder.Decode(Env, Base, Start, End); | |
| 718 | |
| 719 // We don't always know where the incoming branches are in phi nodes, so this | |
| 720 // function finds them. | |
| 721 Cfg->fixPhiNodes(); | |
| 722 | |
| 723 return Cfg; | |
| 724 } | |
| 725 | |
| 726 WasmTranslator::WasmTranslator(GlobalContext *Ctx) : Translator(Ctx) { | |
| 727 // TODO(eholk): compute the correct buffer size. | |
| 728 BufferSize = 24 << 10; // 24k, bigger than the test cases I have so far. | |
| 729 Buffer = new uint8_t[BufferSize]; | |
| 730 } | |
| 731 | |
| 732 WasmTranslator::~WasmTranslator() { | |
| 733 if (Buffer) { | |
| 734 delete[] Buffer; | |
|
Jim Stichnoth
2016/04/01 01:46:44
Can Buffer be a unique_ptr? Then you don't need t
Eric Holk
2016/04/01 19:15:02
I like that much better.
Done.
| |
| 735 } | |
| 736 } | |
| 737 | |
| 738 void WasmTranslator::translate( | |
| 739 const std::string &IRFilename, | |
| 740 std::unique_ptr<llvm::DataStreamer> InputStream) { | |
| 741 LOG(out << "Initializing v8/wasm stuff..." | |
| 742 << "\n"); | |
| 743 Zone Zone; | |
| 744 ZoneScope _(&Zone); | |
| 745 | |
| 746 SizeT BytesRead = InputStream->GetBytes(Buffer, BufferSize); | |
| 747 LOG(out << "Read " << BytesRead << " bytes" | |
| 748 << "\n"); | |
| 749 | |
| 750 LOG(out << "Decoding module " << IRFilename << "\n"); | |
| 751 | |
| 752 auto Result = | |
| 753 DecodeWasmModule(nullptr /* DecodeWasmModule ignores the Isolate | |
| 754 * parameter, so we just pass a null | |
| 755 * one rather than go through the | |
| 756 * hullabaloo of making one. */, &Zone, | |
| 757 Buffer, Buffer + BytesRead, false, kWasmOrigin); | |
| 758 | |
| 759 auto Module = Result.val; | |
| 760 | |
| 761 LOG(out << "Module info:" | |
| 762 << "\n"); | |
| 763 LOG(out << " number of globals: " << Module->globals.size() << "\n"); | |
| 764 LOG(out << " number of signatures: " << Module->signatures.size() | |
| 765 << "\n"); | |
| 766 LOG(out << " number of functions: " << Module->functions.size() << "\n"); | |
| 767 LOG(out << " number of data_segments: " << Module->data_segments.size() | |
| 768 << "\n"); | |
| 769 LOG(out << " function table size: " << Module->function_table.size() | |
| 770 << "\n"); | |
| 771 | |
| 772 ModuleEnv ModuleEnv; | |
| 773 ModuleEnv.module = Module; | |
| 774 | |
| 775 LOG(out << "\n" | |
| 776 << "Function information:" | |
| 777 << "\n"); | |
| 778 for (const auto F : Module->functions) { | |
| 779 LOG(out << " " << F.name_offset << ": " << Module->GetName(F.name_offset)); | |
| 780 if (F.exported) | |
| 781 LOG(out << " export"); | |
| 782 if (F.external) | |
| 783 LOG(out << " extern"); | |
| 784 LOG(out << "\n"); | |
| 785 } | |
| 786 | |
| 787 FunctionEnv Fenv; | |
| 788 Fenv.module = &ModuleEnv; | |
| 789 | |
| 790 LOG(out << "Translating " << IRFilename << "\n"); | |
| 791 | |
| 792 // Translate each function. | |
| 793 uint32_t Id = 0; | |
| 794 for (const auto Fn : Module->functions) { | |
| 795 std::string NewName = fnNameFromId(Id++); | |
| 796 LOG(out << " " << Fn.name_offset << ": " << Module->GetName(Fn.name_offset) | |
| 797 << " -> " << NewName << "..."); | |
| 798 | |
| 799 Fenv.sig = Fn.sig; | |
| 800 Fenv.local_i32_count = Fn.local_i32_count; | |
| 801 Fenv.local_i64_count = Fn.local_i64_count; | |
| 802 Fenv.local_f32_count = Fn.local_f32_count; | |
| 803 Fenv.local_f64_count = Fn.local_f64_count; | |
| 804 Fenv.SumLocals(); | |
| 805 | |
| 806 auto Cfg = | |
| 807 translateFunction(&Zone, &Fenv, Buffer, Buffer + Fn.code_start_offset, | |
| 808 Buffer + Fn.code_end_offset); | |
| 809 Cfg->setFunctionName(Ctx->getGlobalString(NewName)); | |
| 810 | |
| 811 Ctx->optQueueBlockingPush(std::move(Cfg)); | |
| 812 LOG(out << "done." | |
|
Jim Stichnoth
2016/04/01 01:46:45
Do one of these instead:
out << "done.\n"
out
Eric Holk
2016/04/01 19:15:02
Done.
| |
| 813 << "\n"); | |
| 814 } | |
| 815 | |
| 816 return; | |
| 817 } | |
| OLD | NEW |