OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/wasm/wasm-text.h" | 5 #include "src/wasm/wasm-text.h" |
6 | 6 |
7 #include "src/ostreams.h" | 7 #include "src/ostreams.h" |
8 #include "src/vector.h" | 8 #include "src/vector.h" |
9 #include "src/wasm/ast-decoder.h" | 9 #include "src/wasm/ast-decoder.h" |
10 #include "src/wasm/wasm-module.h" | 10 #include "src/wasm/wasm-module.h" |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 bool valid_char = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || | 121 bool valid_char = (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || |
122 (c >= 'A' && c <= 'Z') || strchr(special_chars, c); | 122 (c >= 'A' && c <= 'Z') || strchr(special_chars, c); |
123 if (!valid_char) return false; | 123 if (!valid_char) return false; |
124 } | 124 } |
125 return true; | 125 return true; |
126 } | 126 } |
127 | 127 |
128 } // namespace | 128 } // namespace |
129 | 129 |
130 void wasm::PrintWasmText( | 130 void wasm::PrintWasmText( |
131 const WasmModule *module, uint32_t func_index, std::ostream &os, | 131 const WasmModule *module, const ModuleWireBytes &wire_bytes, |
| 132 uint32_t func_index, std::ostream &os, |
132 std::vector<std::tuple<uint32_t, int, int>> *offset_table) { | 133 std::vector<std::tuple<uint32_t, int, int>> *offset_table) { |
133 DCHECK_NOT_NULL(module); | 134 DCHECK_NOT_NULL(module); |
134 DCHECK_GT(module->functions.size(), func_index); | 135 DCHECK_GT(module->functions.size(), func_index); |
135 const WasmFunction *fun = &module->functions[func_index]; | 136 const WasmFunction *fun = &module->functions[func_index]; |
136 | 137 |
137 AccountingAllocator allocator; | 138 AccountingAllocator allocator; |
138 Zone zone(&allocator, ZONE_NAME); | 139 Zone zone(&allocator, ZONE_NAME); |
139 int line_nr = 0; | 140 int line_nr = 0; |
140 int control_depth = 0; | 141 int control_depth = 0; |
141 | 142 |
142 // Print the function signature. | 143 // Print the function signature. |
143 os << "func"; | 144 os << "func"; |
144 Vector<const char> fun_name( | 145 WasmName fun_name = wire_bytes.GetNameOrNull(fun); |
145 reinterpret_cast<const char *>(module->module_start + fun->name_offset), | |
146 fun->name_length); | |
147 if (IsValidFunctionName(fun_name)) { | 146 if (IsValidFunctionName(fun_name)) { |
148 os << " $"; | 147 os << " $"; |
149 os.write(fun_name.start(), fun_name.length()); | 148 os.write(fun_name.start(), fun_name.length()); |
150 } | 149 } |
151 size_t param_count = fun->sig->parameter_count(); | 150 size_t param_count = fun->sig->parameter_count(); |
152 if (param_count) { | 151 if (param_count) { |
153 os << " (param"; | 152 os << " (param"; |
154 for (size_t i = 0; i < param_count; ++i) | 153 for (size_t i = 0; i < param_count; ++i) |
155 os << ' ' << WasmOpcodes::TypeName(fun->sig->GetParam(i)); | 154 os << ' ' << WasmOpcodes::TypeName(fun->sig->GetParam(i)); |
156 os << ')'; | 155 os << ')'; |
157 } | 156 } |
158 size_t return_count = fun->sig->return_count(); | 157 size_t return_count = fun->sig->return_count(); |
159 if (return_count) { | 158 if (return_count) { |
160 os << " (result"; | 159 os << " (result"; |
161 for (size_t i = 0; i < return_count; ++i) | 160 for (size_t i = 0; i < return_count; ++i) |
162 os << ' ' << WasmOpcodes::TypeName(fun->sig->GetReturn(i)); | 161 os << ' ' << WasmOpcodes::TypeName(fun->sig->GetReturn(i)); |
163 os << ')'; | 162 os << ')'; |
164 } | 163 } |
165 os << "\n"; | 164 os << "\n"; |
166 ++line_nr; | 165 ++line_nr; |
167 | 166 |
168 // Print the local declarations. | 167 // Print the local declarations. |
169 AstLocalDecls decls(&zone); | 168 AstLocalDecls decls(&zone); |
170 const byte *code_start = module->module_start + fun->code_start_offset; | 169 Vector<const byte> func_bytes = wire_bytes.module_bytes.SubVector( |
171 const byte *code_end = module->module_start + fun->code_end_offset; | 170 fun->code_start_offset, fun->code_end_offset); |
172 BytecodeIterator i(code_start, code_end, &decls); | 171 BytecodeIterator i(func_bytes.begin(), func_bytes.end(), &decls); |
173 DCHECK_LT(code_start, i.pc()); | 172 DCHECK_LT(func_bytes.begin(), i.pc()); |
174 if (!decls.local_types.empty()) { | 173 if (!decls.local_types.empty()) { |
175 os << "(local"; | 174 os << "(local"; |
176 for (auto p : decls.local_types) { | 175 for (auto p : decls.local_types) { |
177 for (unsigned i = 0; i < p.second; ++i) | 176 for (unsigned i = 0; i < p.second; ++i) |
178 os << ' ' << WasmOpcodes::TypeName(p.first); | 177 os << ' ' << WasmOpcodes::TypeName(p.first); |
179 } | 178 } |
180 os << ")\n"; | 179 os << ")\n"; |
181 ++line_nr; | 180 ++line_nr; |
182 } | 181 } |
183 | 182 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 default: | 301 default: |
303 UNREACHABLE(); | 302 UNREACHABLE(); |
304 break; | 303 break; |
305 } | 304 } |
306 os << '\n'; | 305 os << '\n'; |
307 ++line_nr; | 306 ++line_nr; |
308 } | 307 } |
309 DCHECK_EQ(0, control_depth); | 308 DCHECK_EQ(0, control_depth); |
310 DCHECK(i.ok()); | 309 DCHECK(i.ok()); |
311 } | 310 } |
OLD | NEW |