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/compiler/wasm-compiler.h" | 5 #include "src/compiler/wasm-compiler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
10 | 10 |
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 | 291 |
292 WasmGraphBuilder::WasmGraphBuilder( | 292 WasmGraphBuilder::WasmGraphBuilder( |
293 Zone* zone, JSGraph* jsgraph, wasm::FunctionSig* function_signature, | 293 Zone* zone, JSGraph* jsgraph, wasm::FunctionSig* function_signature, |
294 compiler::SourcePositionTable* source_position_table) | 294 compiler::SourcePositionTable* source_position_table) |
295 : zone_(zone), | 295 : zone_(zone), |
296 jsgraph_(jsgraph), | 296 jsgraph_(jsgraph), |
297 module_(nullptr), | 297 module_(nullptr), |
298 mem_buffer_(nullptr), | 298 mem_buffer_(nullptr), |
299 mem_size_(nullptr), | 299 mem_size_(nullptr), |
300 function_tables_(zone), | 300 function_tables_(zone), |
301 function_table_sizes_(zone), | |
301 control_(nullptr), | 302 control_(nullptr), |
302 effect_(nullptr), | 303 effect_(nullptr), |
303 cur_buffer_(def_buffer_), | 304 cur_buffer_(def_buffer_), |
304 cur_bufsize_(kDefaultBufferSize), | 305 cur_bufsize_(kDefaultBufferSize), |
305 trap_(new (zone) WasmTrapHelper(this)), | 306 trap_(new (zone) WasmTrapHelper(this)), |
306 function_signature_(function_signature), | 307 function_signature_(function_signature), |
307 source_position_table_(source_position_table) { | 308 source_position_table_(source_position_table) { |
308 DCHECK_NOT_NULL(jsgraph_); | 309 DCHECK_NOT_NULL(jsgraph_); |
309 } | 310 } |
310 | 311 |
(...skipping 1394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1705 *effect_, *control_); | 1706 *effect_, *control_); |
1706 *effect_ = load; | 1707 *effect_ = load; |
1707 return load; | 1708 return load; |
1708 } | 1709 } |
1709 | 1710 |
1710 Node* WasmGraphBuilder::GrowMemory(Node* input) { | 1711 Node* WasmGraphBuilder::GrowMemory(Node* input) { |
1711 Diamond check_input_range( | 1712 Diamond check_input_range( |
1712 graph(), jsgraph()->common(), | 1713 graph(), jsgraph()->common(), |
1713 graph()->NewNode( | 1714 graph()->NewNode( |
1714 jsgraph()->machine()->Uint32LessThanOrEqual(), input, | 1715 jsgraph()->machine()->Uint32LessThanOrEqual(), input, |
1715 jsgraph()->Uint32Constant(wasm::WasmModule::kMaxMemPages)), | 1716 jsgraph()->Uint32Constant(wasm::WasmModule::kV8MaxPages)), |
1716 BranchHint::kTrue); | 1717 BranchHint::kTrue); |
1717 | 1718 |
1718 check_input_range.Chain(*control_); | 1719 check_input_range.Chain(*control_); |
1719 | 1720 |
1720 Runtime::FunctionId function_id = Runtime::kWasmGrowMemory; | 1721 Runtime::FunctionId function_id = Runtime::kWasmGrowMemory; |
1721 const Runtime::Function* function = Runtime::FunctionForId(function_id); | 1722 const Runtime::Function* function = Runtime::FunctionForId(function_id); |
1722 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( | 1723 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( |
1723 jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow, | 1724 jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow, |
1724 CallDescriptor::kNoFlags); | 1725 CallDescriptor::kNoFlags); |
1725 wasm::ModuleEnv* module = module_; | 1726 wasm::ModuleEnv* module = module_; |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2124 | 2125 |
2125 // Add code object as constant. | 2126 // Add code object as constant. |
2126 Handle<Code> code = module_->GetFunctionCode(index); | 2127 Handle<Code> code = module_->GetFunctionCode(index); |
2127 DCHECK(!code.is_null()); | 2128 DCHECK(!code.is_null()); |
2128 args[0] = HeapConstant(code); | 2129 args[0] = HeapConstant(code); |
2129 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); | 2130 wasm::FunctionSig* sig = module_->GetFunctionSignature(index); |
2130 | 2131 |
2131 return BuildWasmCall(sig, args, rets, position); | 2132 return BuildWasmCall(sig, args, rets, position); |
2132 } | 2133 } |
2133 | 2134 |
2134 Node* WasmGraphBuilder::CallIndirect(uint32_t index, Node** args, Node*** rets, | 2135 Node* WasmGraphBuilder::CallIndirect(uint32_t sig_index, Node** args, |
2136 Node*** rets, | |
2135 wasm::WasmCodePosition position) { | 2137 wasm::WasmCodePosition position) { |
2136 DCHECK_NOT_NULL(args[0]); | 2138 DCHECK_NOT_NULL(args[0]); |
2137 DCHECK(module_ && module_->instance); | 2139 DCHECK(module_ && module_->instance); |
2138 | 2140 |
2139 MachineOperatorBuilder* machine = jsgraph()->machine(); | 2141 // Assume only one table for now. |
2142 uint32_t table_index = 0; | |
2143 wasm::FunctionSig* sig = module_->GetSignature(sig_index); | |
2140 | 2144 |
2141 // Compute the code object by loading it from the function table. | 2145 if (!module_->IsValidTable(table_index)) { |
2142 Node* key = args[0]; | |
2143 | |
2144 // Assume only one table for now. | |
2145 DCHECK_LE(module_->instance->function_tables.size(), 1u); | |
2146 // Bounds check the index. | |
2147 uint32_t table_size = | |
2148 module_->IsValidTable(0) ? module_->GetTable(0)->max_size : 0; | |
2149 wasm::FunctionSig* sig = module_->GetSignature(index); | |
2150 if (table_size > 0) { | |
2151 // Bounds check against the table size. | |
2152 Node* size = Uint32Constant(table_size); | |
2153 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); | |
2154 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); | |
2155 } else { | |
2156 // No function table. Generate a trap and return a constant. | 2146 // No function table. Generate a trap and return a constant. |
2157 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); | 2147 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, Int32Constant(0), position); |
2158 (*rets) = Buffer(sig->return_count()); | 2148 (*rets) = Buffer(sig->return_count()); |
2159 for (size_t i = 0; i < sig->return_count(); i++) { | 2149 for (size_t i = 0; i < sig->return_count(); i++) { |
2160 (*rets)[i] = trap_->GetTrapValue(sig->GetReturn(i)); | 2150 (*rets)[i] = trap_->GetTrapValue(sig->GetReturn(i)); |
2161 } | 2151 } |
2162 return trap_->GetTrapValue(sig); | 2152 return trap_->GetTrapValue(sig); |
2163 } | 2153 } |
2164 Node* table = FunctionTable(0); | 2154 |
2155 EnsureFunctionTableNodes(); | |
bradnelson
2016/10/28 16:59:01
Don't care for Ensure, but not sure a good alterna
titzer
2016/10/28 17:29:08
Just to save nodes, since I assume most functions
| |
2156 MachineOperatorBuilder* machine = jsgraph()->machine(); | |
2157 Node* key = args[0]; | |
2158 | |
2159 // Bounds check against the table size. | |
2160 Node* size = function_table_sizes_[table_index]; | |
2161 Node* in_bounds = graph()->NewNode(machine->Uint32LessThan(), key, size); | |
2162 trap_->AddTrapIfFalse(wasm::kTrapFuncInvalid, in_bounds, position); | |
2163 Node* table = function_tables_[table_index]; | |
2165 | 2164 |
2166 // Load signature from the table and check. | 2165 // Load signature from the table and check. |
2167 // The table is a FixedArray; signatures are encoded as SMIs. | 2166 // The table is a FixedArray; signatures are encoded as SMIs. |
2168 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] | 2167 // [sig1, sig2, sig3, ...., code1, code2, code3 ...] |
2169 ElementAccess access = AccessBuilder::ForFixedArrayElement(); | 2168 ElementAccess access = AccessBuilder::ForFixedArrayElement(); |
2170 const int fixed_offset = access.header_size - access.tag(); | 2169 const int fixed_offset = access.header_size - access.tag(); |
2171 { | 2170 { |
2172 Node* load_sig = graph()->NewNode( | 2171 Node* load_sig = graph()->NewNode( |
2173 machine->Load(MachineType::AnyTagged()), table, | 2172 machine->Load(MachineType::AnyTagged()), table, |
2174 graph()->NewNode(machine->Int32Add(), | 2173 graph()->NewNode(machine->Int32Add(), |
2175 graph()->NewNode(machine->Word32Shl(), key, | 2174 graph()->NewNode(machine->Word32Shl(), key, |
2176 Int32Constant(kPointerSizeLog2)), | 2175 Int32Constant(kPointerSizeLog2)), |
2177 Int32Constant(fixed_offset)), | 2176 Int32Constant(fixed_offset)), |
2178 *effect_, *control_); | 2177 *effect_, *control_); |
2179 auto map = const_cast<wasm::SignatureMap&>( | 2178 auto map = const_cast<wasm::SignatureMap&>( |
2180 module_->module->function_tables[0].map); | 2179 module_->module->function_tables[0].map); |
2181 Node* sig_match = graph()->NewNode( | 2180 Node* sig_match = graph()->NewNode( |
2182 machine->WordEqual(), load_sig, | 2181 machine->WordEqual(), load_sig, |
2183 jsgraph()->SmiConstant(static_cast<int>(map.FindOrInsert(sig)))); | 2182 jsgraph()->SmiConstant(static_cast<int>(map.FindOrInsert(sig)))); |
2184 trap_->AddTrapIfFalse(wasm::kTrapFuncSigMismatch, sig_match, position); | 2183 trap_->AddTrapIfFalse(wasm::kTrapFuncSigMismatch, sig_match, position); |
2185 } | 2184 } |
2186 | 2185 |
2187 // Load code object from the table. | 2186 // Load code object from the table. |
2187 uint32_t table_size = module_->module->function_tables[table_index].min_size; | |
2188 uint32_t offset = fixed_offset + kPointerSize * table_size; | 2188 uint32_t offset = fixed_offset + kPointerSize * table_size; |
2189 Node* load_code = graph()->NewNode( | 2189 Node* load_code = graph()->NewNode( |
2190 machine->Load(MachineType::AnyTagged()), table, | 2190 machine->Load(MachineType::AnyTagged()), table, |
2191 graph()->NewNode(machine->Int32Add(), | 2191 graph()->NewNode(machine->Int32Add(), |
2192 graph()->NewNode(machine->Word32Shl(), key, | 2192 graph()->NewNode(machine->Word32Shl(), key, |
2193 Int32Constant(kPointerSizeLog2)), | 2193 Int32Constant(kPointerSizeLog2)), |
2194 Uint32Constant(offset)), | 2194 Uint32Constant(offset)), |
2195 *effect_, *control_); | 2195 *effect_, *control_); |
2196 | 2196 |
2197 args[0] = load_code; | 2197 args[0] = load_code; |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2824 if (!mem_size_) | 2824 if (!mem_size_) |
2825 mem_size_ = jsgraph()->RelocatableInt32Constant( | 2825 mem_size_ = jsgraph()->RelocatableInt32Constant( |
2826 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2826 size, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2827 return mem_size_; | 2827 return mem_size_; |
2828 } else { | 2828 } else { |
2829 return jsgraph()->RelocatableInt32Constant( | 2829 return jsgraph()->RelocatableInt32Constant( |
2830 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); | 2830 size + offset, RelocInfo::WASM_MEMORY_SIZE_REFERENCE); |
2831 } | 2831 } |
2832 } | 2832 } |
2833 | 2833 |
2834 Node* WasmGraphBuilder::FunctionTable(uint32_t index) { | 2834 void WasmGraphBuilder::EnsureFunctionTableNodes() { |
2835 DCHECK(module_ && module_->instance && | 2835 if (function_tables_.size() > 0) return; |
2836 index < module_->instance->function_tables.size()); | 2836 for (size_t i = 0; i < module_->instance->function_tables.size(); ++i) { |
2837 if (!function_tables_.size()) { | 2837 auto handle = module_->instance->function_tables[i]; |
2838 for (size_t i = 0; i < module_->instance->function_tables.size(); ++i) { | 2838 DCHECK(!handle.is_null()); |
2839 DCHECK(!module_->instance->function_tables[i].is_null()); | 2839 function_tables_.push_back(HeapConstant(handle)); |
2840 function_tables_.push_back( | 2840 uint32_t table_size = module_->module->function_tables[i].min_size; |
2841 HeapConstant(module_->instance->function_tables[i])); | 2841 function_table_sizes_.push_back(Uint32Constant(table_size)); |
2842 } | |
2843 } | 2842 } |
2844 return function_tables_[index]; | |
2845 } | 2843 } |
2846 | 2844 |
2847 Node* WasmGraphBuilder::GetGlobal(uint32_t index) { | 2845 Node* WasmGraphBuilder::GetGlobal(uint32_t index) { |
2848 MachineType mem_type = | 2846 MachineType mem_type = |
2849 wasm::WasmOpcodes::MachineTypeFor(module_->GetGlobalType(index)); | 2847 wasm::WasmOpcodes::MachineTypeFor(module_->GetGlobalType(index)); |
2850 Node* addr = jsgraph()->RelocatableIntPtrConstant( | 2848 Node* addr = jsgraph()->RelocatableIntPtrConstant( |
2851 reinterpret_cast<uintptr_t>(module_->instance->globals_start + | 2849 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
2852 module_->module->globals[index].offset), | 2850 module_->module->globals[index].offset), |
2853 RelocInfo::WASM_GLOBAL_REFERENCE); | 2851 RelocInfo::WASM_GLOBAL_REFERENCE); |
2854 const Operator* op = jsgraph()->machine()->Load(mem_type); | 2852 const Operator* op = jsgraph()->machine()->Load(mem_type); |
(...skipping 604 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3459 function_->code_start_offset), | 3457 function_->code_start_offset), |
3460 compile_ms); | 3458 compile_ms); |
3461 } | 3459 } |
3462 | 3460 |
3463 return code; | 3461 return code; |
3464 } | 3462 } |
3465 | 3463 |
3466 } // namespace compiler | 3464 } // namespace compiler |
3467 } // namespace internal | 3465 } // namespace internal |
3468 } // namespace v8 | 3466 } // namespace v8 |
OLD | NEW |