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

Side by Side Diff: src/wasm/ast-decoder.cc

Issue 2135693002: [wasm] Add a BytecodeIterator and use in non-performance-critical situations. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Add C++ iterator Created 4 years, 5 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
OLDNEW
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 1446 matching lines...) Expand 10 before | Expand all | Expand 10 after
1457 1457
1458 bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start, 1458 bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start,
1459 const byte* end) { 1459 const byte* end) {
1460 base::AccountingAllocator allocator; 1460 base::AccountingAllocator allocator;
1461 Zone tmp(&allocator); 1461 Zone tmp(&allocator);
1462 FunctionBody body = {nullptr, nullptr, nullptr, start, end}; 1462 FunctionBody body = {nullptr, nullptr, nullptr, start, end};
1463 WasmFullDecoder decoder(&tmp, nullptr, body); 1463 WasmFullDecoder decoder(&tmp, nullptr, body);
1464 return decoder.DecodeLocalDecls(decls); 1464 return decoder.DecodeLocalDecls(decls);
1465 } 1465 }
1466 1466
1467 BytecodeIterator::BytecodeIterator(const byte* start, const byte* end,
1468 AstLocalDecls* decls)
1469 : Decoder(start, end) {
1470 if (decls != nullptr) {
1471 if (DecodeLocalDecls(*decls, start, end)) {
1472 pc_ += decls->decls_encoded_size;
1473 if (pc_ > end_) pc_ = end_;
1474 }
1475 }
1476 }
1477
1467 DecodeResult VerifyWasmCode(base::AccountingAllocator* allocator, 1478 DecodeResult VerifyWasmCode(base::AccountingAllocator* allocator,
1468 FunctionBody& body) { 1479 FunctionBody& body) {
1469 Zone zone(allocator); 1480 Zone zone(allocator);
1470 WasmFullDecoder decoder(&zone, nullptr, body); 1481 WasmFullDecoder decoder(&zone, nullptr, body);
1471 decoder.Decode(); 1482 decoder.Decode();
1472 return decoder.toResult<DecodeStruct*>(nullptr); 1483 return decoder.toResult<DecodeStruct*>(nullptr);
1473 } 1484 }
1474 1485
1475 DecodeResult BuildTFGraph(base::AccountingAllocator* allocator, 1486 DecodeResult BuildTFGraph(base::AccountingAllocator* allocator,
1476 TFBuilder* builder, FunctionBody& body) { 1487 TFBuilder* builder, FunctionBody& body) {
(...skipping 27 matching lines...) Expand all
1504 int line_nr = 0; 1515 int line_nr = 0;
1505 1516
1506 // Print the function signature. 1517 // Print the function signature.
1507 if (body.sig) { 1518 if (body.sig) {
1508 os << "// signature: " << *body.sig << std::endl; 1519 os << "// signature: " << *body.sig << std::endl;
1509 ++line_nr; 1520 ++line_nr;
1510 } 1521 }
1511 1522
1512 // Print the local declarations. 1523 // Print the local declarations.
1513 AstLocalDecls decls(&zone); 1524 AstLocalDecls decls(&zone);
1514 decoder.DecodeLocalDecls(decls); 1525 BytecodeIterator i(body.start, body.end, &decls);
1515 const byte* pc = decoder.pc(); 1526 if (body.start != i.pc()) {
1516 if (body.start != decoder.pc()) {
1517 os << "// locals: "; 1527 os << "// locals: ";
1518 for (auto p : decls.local_types) { 1528 for (auto p : decls.local_types) {
1519 LocalType type = p.first; 1529 LocalType type = p.first;
1520 uint32_t count = p.second; 1530 uint32_t count = p.second;
1521 os << " " << count << " " << WasmOpcodes::TypeName(type); 1531 os << " " << count << " " << WasmOpcodes::TypeName(type);
1522 } 1532 }
1523 os << std::endl; 1533 os << std::endl;
1524 ++line_nr; 1534 ++line_nr;
1525 1535
1526 for (const byte* locals = body.start; locals < pc; locals++) { 1536 for (const byte* locals = body.start; locals < i.pc(); locals++) {
1527 os << (locals == body.start ? "0x" : " 0x") << AsHex(*locals, 2) << ","; 1537 os << (locals == body.start ? "0x" : " 0x") << AsHex(*locals, 2) << ",";
1528 } 1538 }
1529 os << std::endl; 1539 os << std::endl;
1530 ++line_nr; 1540 ++line_nr;
1531 } 1541 }
1532 1542
1533 os << "// body: " << std::endl; 1543 os << "// body: " << std::endl;
1534 ++line_nr; 1544 ++line_nr;
1535 unsigned control_depth = 0; 1545 unsigned control_depth = 0;
1536 while (pc < body.end) { 1546 for (; i.has_next(); i.next()) {
1537 unsigned length = decoder.OpcodeLength(pc); 1547 unsigned length = decoder.OpcodeLength(i.pc());
1538 1548
1539 WasmOpcode opcode = static_cast<WasmOpcode>(*pc); 1549 WasmOpcode opcode = i.current();
1540 if (opcode == kExprElse) control_depth--; 1550 if (opcode == kExprElse) control_depth--;
1541 1551
1542 int num_whitespaces = control_depth < 32 ? 2 * control_depth : 64; 1552 int num_whitespaces = control_depth < 32 ? 2 * control_depth : 64;
1543 if (offset_table) { 1553 if (offset_table) {
1544 offset_table->push_back( 1554 offset_table->push_back(
1545 std::make_tuple(pc - body.start, line_nr, num_whitespaces)); 1555 std::make_tuple(i.pc_offset(), line_nr, num_whitespaces));
1546 } 1556 }
1547 1557
1548 // 64 whitespaces 1558 // 64 whitespaces
1549 const char* padding = 1559 const char* padding =
1550 " "; 1560 " ";
1551 os.write(padding, num_whitespaces); 1561 os.write(padding, num_whitespaces);
1552 os << "k" << WasmOpcodes::OpcodeName(opcode) << ","; 1562 os << "k" << WasmOpcodes::OpcodeName(opcode) << ",";
1553 1563
1554 for (size_t i = 1; i < length; ++i) { 1564 for (size_t j = 1; j < length; ++j) {
1555 os << " " << AsHex(pc[i], 2) << ","; 1565 os << " " << AsHex(i.pc()[j], 2) << ",";
1556 } 1566 }
1557 1567
1558 switch (opcode) { 1568 switch (opcode) {
1559 case kExprIf: 1569 case kExprIf:
1560 case kExprElse: 1570 case kExprElse:
1561 case kExprLoop: 1571 case kExprLoop:
1562 case kExprBlock: 1572 case kExprBlock:
1563 os << " // @" << static_cast<int>(pc - body.start); 1573 os << " // @" << i.pc_offset();
1564 control_depth++; 1574 control_depth++;
1565 break; 1575 break;
1566 case kExprEnd: 1576 case kExprEnd:
1567 os << " // @" << static_cast<int>(pc - body.start); 1577 os << " // @" << i.pc_offset();
1568 control_depth--; 1578 control_depth--;
1569 break; 1579 break;
1570 case kExprBr: { 1580 case kExprBr: {
1571 BreakDepthOperand operand(&decoder, pc); 1581 BreakDepthOperand operand(&i, i.pc());
1572 os << " // arity=" << operand.arity << " depth=" << operand.depth; 1582 os << " // arity=" << operand.arity << " depth=" << operand.depth;
1573 break; 1583 break;
1574 } 1584 }
1575 case kExprBrIf: { 1585 case kExprBrIf: {
1576 BreakDepthOperand operand(&decoder, pc); 1586 BreakDepthOperand operand(&i, i.pc());
1577 os << " // arity=" << operand.arity << " depth" << operand.depth; 1587 os << " // arity=" << operand.arity << " depth" << operand.depth;
1578 break; 1588 break;
1579 } 1589 }
1580 case kExprBrTable: { 1590 case kExprBrTable: {
1581 BranchTableOperand operand(&decoder, pc); 1591 BranchTableOperand operand(&i, i.pc());
1582 os << " // arity=" << operand.arity 1592 os << " // arity=" << operand.arity
1583 << " entries=" << operand.table_count; 1593 << " entries=" << operand.table_count;
1584 break; 1594 break;
1585 } 1595 }
1586 case kExprCallIndirect: { 1596 case kExprCallIndirect: {
1587 CallIndirectOperand operand(&decoder, pc); 1597 CallIndirectOperand operand(&i, i.pc());
1588 if (decoder.Complete(pc, operand)) { 1598 if (decoder.Complete(i.pc(), operand)) {
1589 os << " // sig #" << operand.index << ": " << *operand.sig; 1599 os << " // sig #" << operand.index << ": " << *operand.sig;
1590 } else { 1600 } else {
1591 os << " // arity=" << operand.arity << " sig #" << operand.index; 1601 os << " // arity=" << operand.arity << " sig #" << operand.index;
1592 } 1602 }
1593 break; 1603 break;
1594 } 1604 }
1595 case kExprCallImport: { 1605 case kExprCallImport: {
1596 CallImportOperand operand(&decoder, pc); 1606 CallImportOperand operand(&i, i.pc());
1597 if (decoder.Complete(pc, operand)) { 1607 if (decoder.Complete(i.pc(), operand)) {
1598 os << " // import #" << operand.index << ": " << *operand.sig; 1608 os << " // import #" << operand.index << ": " << *operand.sig;
1599 } else { 1609 } else {
1600 os << " // arity=" << operand.arity << " import #" << operand.index; 1610 os << " // arity=" << operand.arity << " import #" << operand.index;
1601 } 1611 }
1602 break; 1612 break;
1603 } 1613 }
1604 case kExprCallFunction: { 1614 case kExprCallFunction: {
1605 CallFunctionOperand operand(&decoder, pc); 1615 CallFunctionOperand operand(&i, i.pc());
1606 if (decoder.Complete(pc, operand)) { 1616 if (decoder.Complete(i.pc(), operand)) {
1607 os << " // function #" << operand.index << ": " << *operand.sig; 1617 os << " // function #" << operand.index << ": " << *operand.sig;
1608 } else { 1618 } else {
1609 os << " // arity=" << operand.arity << " function #" << operand.index; 1619 os << " // arity=" << operand.arity << " function #" << operand.index;
1610 } 1620 }
1611 break; 1621 break;
1612 } 1622 }
1613 case kExprReturn: { 1623 case kExprReturn: {
1614 ReturnArityOperand operand(&decoder, pc); 1624 ReturnArityOperand operand(&i, i.pc());
1615 os << " // arity=" << operand.arity; 1625 os << " // arity=" << operand.arity;
1616 break; 1626 break;
1617 } 1627 }
1618 default: 1628 default:
1619 break; 1629 break;
1620 } 1630 }
1621
1622 pc += length;
1623 os << std::endl; 1631 os << std::endl;
1624 ++line_nr; 1632 ++line_nr;
1625 } 1633 }
1626 1634
1627 return decoder.ok(); 1635 return decoder.ok();
1628 } 1636 }
1629 1637
1630 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, 1638 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals,
1631 const byte* start, const byte* end) { 1639 const byte* start, const byte* end) {
1632 FunctionBody body = {nullptr, nullptr, nullptr, start, end}; 1640 FunctionBody body = {nullptr, nullptr, nullptr, start, end};
1633 WasmFullDecoder decoder(zone, nullptr, body); 1641 WasmFullDecoder decoder(zone, nullptr, body);
1634 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals); 1642 return decoder.AnalyzeLoopAssignmentForTesting(start, num_locals);
1635 } 1643 }
1636 1644
1637 } // namespace wasm 1645 } // namespace wasm
1638 } // namespace internal 1646 } // namespace internal
1639 } // namespace v8 1647 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698