| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/signature.h" | 5 #include "src/signature.h" |
| 6 | 6 |
| 7 #include "src/bit-vector.h" | 7 #include "src/bit-vector.h" |
| 8 #include "src/flags.h" | 8 #include "src/flags.h" |
| 9 #include "src/handles.h" | 9 #include "src/handles.h" |
| 10 #include "src/zone-containers.h" | 10 #include "src/zone-containers.h" |
| (...skipping 1506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1517 int OpcodeLength(const byte* pc, const byte* end) { | 1517 int OpcodeLength(const byte* pc, const byte* end) { |
| 1518 WasmDecoder decoder(nullptr, nullptr, pc, end); | 1518 WasmDecoder decoder(nullptr, nullptr, pc, end); |
| 1519 return decoder.OpcodeLength(pc); | 1519 return decoder.OpcodeLength(pc); |
| 1520 } | 1520 } |
| 1521 | 1521 |
| 1522 int OpcodeArity(const byte* pc, const byte* end) { | 1522 int OpcodeArity(const byte* pc, const byte* end) { |
| 1523 WasmDecoder decoder(nullptr, nullptr, pc, end); | 1523 WasmDecoder decoder(nullptr, nullptr, pc, end); |
| 1524 return decoder.OpcodeArity(pc); | 1524 return decoder.OpcodeArity(pc); |
| 1525 } | 1525 } |
| 1526 | 1526 |
| 1527 void PrintAstForDebugging(const byte* start, const byte* end) { |
| 1528 FunctionBody body = {nullptr, nullptr, start, start, end}; |
| 1529 base::AccountingAllocator allocator; |
| 1530 PrintAst(&allocator, body); |
| 1531 } |
| 1532 |
| 1527 void PrintAst(base::AccountingAllocator* allocator, FunctionBody& body) { | 1533 void PrintAst(base::AccountingAllocator* allocator, FunctionBody& body) { |
| 1528 Zone zone(allocator); | 1534 Zone zone(allocator); |
| 1529 SR_WasmDecoder decoder(&zone, nullptr, body); | 1535 SR_WasmDecoder decoder(&zone, nullptr, body); |
| 1530 | 1536 |
| 1531 OFStream os(stdout); | 1537 OFStream os(stdout); |
| 1532 | 1538 |
| 1533 // Print the function signature. | 1539 // Print the function signature. |
| 1534 if (body.sig) { | 1540 if (body.sig) { |
| 1535 os << "// signature: " << *body.sig << std::endl; | 1541 os << "// signature: " << *body.sig << std::endl; |
| 1536 } | 1542 } |
| 1537 | 1543 |
| 1538 // Print the local declarations. | 1544 // Print the local declarations. |
| 1539 AstLocalDecls decls(&zone); | 1545 AstLocalDecls decls(&zone); |
| 1540 decoder.DecodeLocalDecls(decls); | 1546 decoder.DecodeLocalDecls(decls); |
| 1541 const byte* pc = decoder.pc(); | 1547 const byte* pc = decoder.pc(); |
| 1542 if (body.start != decoder.pc()) { | 1548 if (body.start != decoder.pc()) { |
| 1543 printf("// locals:"); | 1549 os << "// locals: "; |
| 1544 for (auto p : decls.local_types) { | 1550 for (auto p : decls.local_types) { |
| 1545 LocalType type = p.first; | 1551 LocalType type = p.first; |
| 1546 uint32_t count = p.second; | 1552 uint32_t count = p.second; |
| 1547 os << " " << count << " " << WasmOpcodes::TypeName(type); | 1553 os << " " << count << " " << WasmOpcodes::TypeName(type); |
| 1548 } | 1554 } |
| 1549 os << std::endl; | 1555 os << std::endl; |
| 1550 | 1556 |
| 1551 for (const byte* locals = body.start; locals < pc; locals++) { | 1557 for (const byte* locals = body.start; locals < pc; locals++) { |
| 1552 printf(" 0x%02x,", *locals); | 1558 printf(" 0x%02x,", *locals); |
| 1553 } | 1559 } |
| 1554 printf("\n"); | 1560 os << std::endl; |
| 1555 } | 1561 } |
| 1556 | 1562 |
| 1557 printf("// body: \n"); | 1563 os << "// body: \n"; |
| 1558 std::vector<int> arity_stack; | 1564 int control_depth = 0; |
| 1559 while (pc < body.end) { | 1565 while (pc < body.end) { |
| 1560 int arity = decoder.OpcodeArity(pc); | |
| 1561 size_t length = decoder.OpcodeLength(pc); | 1566 size_t length = decoder.OpcodeLength(pc); |
| 1562 | 1567 |
| 1563 for (auto arity : arity_stack) { | 1568 WasmOpcode opcode = static_cast<WasmOpcode>(*pc); |
| 1564 printf(" "); | 1569 if (opcode == kExprElse) control_depth--; |
| 1565 USE(arity); | |
| 1566 } | |
| 1567 | 1570 |
| 1568 WasmOpcode opcode = static_cast<WasmOpcode>(*pc); | 1571 for (int i = 0; i < control_depth && i < 32; i++) printf(" "); |
| 1569 printf("k%s,", WasmOpcodes::OpcodeName(opcode)); | 1572 printf("k%s,", WasmOpcodes::OpcodeName(opcode)); |
| 1570 | 1573 |
| 1571 for (size_t i = 1; i < length; i++) { | 1574 for (size_t i = 1; i < length; i++) { |
| 1572 printf(" 0x%02x,", pc[i]); | 1575 printf(" 0x%02x,", pc[i]); |
| 1573 } | 1576 } |
| 1574 | 1577 |
| 1575 if (body.module) { | 1578 switch (opcode) { |
| 1576 switch (opcode) { | 1579 case kExprIf: |
| 1577 case kExprCallIndirect: { | 1580 case kExprElse: |
| 1578 CallIndirectOperand operand(&decoder, pc); | 1581 case kExprLoop: |
| 1579 if (decoder.Validate(pc, operand)) { | 1582 case kExprBlock: |
| 1580 os << " // sig #" << operand.index << ": " << *operand.sig; | 1583 os << " // @" << static_cast<int>(pc - body.start); |
| 1581 } | 1584 control_depth++; |
| 1582 break; | 1585 break; |
| 1586 case kExprEnd: |
| 1587 os << " // @" << static_cast<int>(pc - body.start); |
| 1588 control_depth--; |
| 1589 break; |
| 1590 case kExprBr: { |
| 1591 BreakDepthOperand operand(&decoder, pc); |
| 1592 os << " // arity=" << operand.arity << " depth=" << operand.depth; |
| 1593 break; |
| 1594 } |
| 1595 case kExprBrIf: { |
| 1596 BreakDepthOperand operand(&decoder, pc); |
| 1597 os << " // arity=" << operand.arity << " depth" << operand.depth; |
| 1598 break; |
| 1599 } |
| 1600 case kExprBrTable: { |
| 1601 BranchTableOperand operand(&decoder, pc); |
| 1602 os << " // arity=" << operand.arity |
| 1603 << " entries=" << operand.table_count; |
| 1604 break; |
| 1605 } |
| 1606 case kExprCallIndirect: { |
| 1607 CallIndirectOperand operand(&decoder, pc); |
| 1608 if (decoder.Validate(pc, operand)) { |
| 1609 os << " // sig #" << operand.index << ": " << *operand.sig; |
| 1610 } else { |
| 1611 os << " // arity=" << operand.arity << " sig #" << operand.index; |
| 1583 } | 1612 } |
| 1584 case kExprCallImport: { | 1613 break; |
| 1585 CallImportOperand operand(&decoder, pc); | 1614 } |
| 1586 if (decoder.Validate(pc, operand)) { | 1615 case kExprCallImport: { |
| 1587 os << " // import #" << operand.index << ": " << *operand.sig; | 1616 CallImportOperand operand(&decoder, pc); |
| 1588 } | 1617 if (decoder.Validate(pc, operand)) { |
| 1589 break; | 1618 os << " // import #" << operand.index << ": " << *operand.sig; |
| 1619 } else { |
| 1620 os << " // arity=" << operand.arity << " import #" << operand.index; |
| 1590 } | 1621 } |
| 1591 case kExprCallFunction: { | 1622 break; |
| 1592 CallFunctionOperand operand(&decoder, pc); | 1623 } |
| 1593 if (decoder.Validate(pc, operand)) { | 1624 case kExprCallFunction: { |
| 1594 os << " // function #" << operand.index << ": " << *operand.sig; | 1625 CallFunctionOperand operand(&decoder, pc); |
| 1595 } | 1626 if (decoder.Validate(pc, operand)) { |
| 1596 break; | 1627 os << " // function #" << operand.index << ": " << *operand.sig; |
| 1628 } else { |
| 1629 os << " // arity=" << operand.arity << " function #" << operand.index; |
| 1597 } | 1630 } |
| 1598 default: | 1631 break; |
| 1599 break; | |
| 1600 } | 1632 } |
| 1601 } | 1633 case kExprReturn: { |
| 1634 ReturnArityOperand operand(&decoder, pc); |
| 1635 os << " // arity=" << operand.arity; |
| 1636 break; |
| 1637 } |
| 1638 default: |
| 1639 break; |
| 1640 } |
| 1602 | 1641 |
| 1603 pc += length; | 1642 pc += length; |
| 1604 printf("\n"); | 1643 os << std::endl; |
| 1605 | |
| 1606 arity_stack.push_back(arity); | |
| 1607 while (arity_stack.back() == 0) { | |
| 1608 arity_stack.pop_back(); | |
| 1609 if (arity_stack.empty()) break; | |
| 1610 arity_stack.back()--; | |
| 1611 } | |
| 1612 } | 1644 } |
| 1613 } | 1645 } |
| 1614 | 1646 |
| 1615 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, | 1647 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, |
| 1616 const byte* start, const byte* end) { | 1648 const byte* start, const byte* end) { |
| 1617 FunctionBody body = {nullptr, nullptr, nullptr, start, end}; | 1649 FunctionBody body = {nullptr, nullptr, nullptr, start, end}; |
| 1618 SR_WasmDecoder decoder(zone, nullptr, body); | 1650 SR_WasmDecoder decoder(zone, nullptr, body); |
| 1619 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); | 1651 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); |
| 1620 } | 1652 } |
| 1621 | 1653 |
| 1622 } // namespace wasm | 1654 } // namespace wasm |
| 1623 } // namespace internal | 1655 } // namespace internal |
| 1624 } // namespace v8 | 1656 } // namespace v8 |
| OLD | NEW |