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 "src/isolate-inl.h" | 7 #include "src/isolate-inl.h" |
8 | 8 |
9 #include "src/base/platform/platform.h" | 9 #include "src/base/platform/platform.h" |
10 | 10 |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 Node* exception = builder_->String(kTrapMessages[reason]); | 190 Node* exception = builder_->String(kTrapMessages[reason]); |
191 Node* end; | 191 Node* end; |
192 Node** control_ptr = builder_->control_; | 192 Node** control_ptr = builder_->control_; |
193 Node** effect_ptr = builder_->effect_; | 193 Node** effect_ptr = builder_->effect_; |
194 wasm::ModuleEnv* module = builder_->module_; | 194 wasm::ModuleEnv* module = builder_->module_; |
195 *control_ptr = traps_[reason] = | 195 *control_ptr = traps_[reason] = |
196 graph()->NewNode(common()->Merge(1), *control_ptr); | 196 graph()->NewNode(common()->Merge(1), *control_ptr); |
197 *effect_ptr = effects_[reason] = | 197 *effect_ptr = effects_[reason] = |
198 graph()->NewNode(common()->EffectPhi(1), *effect_ptr, *control_ptr); | 198 graph()->NewNode(common()->EffectPhi(1), *effect_ptr, *control_ptr); |
199 | 199 |
200 if (module && !module->context.is_null()) { | 200 if (module && !module->instance->context.is_null()) { |
201 // Use the module context to call the runtime to throw an exception. | 201 // Use the module context to call the runtime to throw an exception. |
202 Runtime::FunctionId f = Runtime::kThrow; | 202 Runtime::FunctionId f = Runtime::kThrow; |
203 const Runtime::Function* fun = Runtime::FunctionForId(f); | 203 const Runtime::Function* fun = Runtime::FunctionForId(f); |
204 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( | 204 CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( |
205 jsgraph()->zone(), f, fun->nargs, Operator::kNoProperties, | 205 jsgraph()->zone(), f, fun->nargs, Operator::kNoProperties, |
206 CallDescriptor::kNoFlags); | 206 CallDescriptor::kNoFlags); |
207 Node* inputs[] = { | 207 Node* inputs[] = { |
208 jsgraph()->CEntryStubConstant(fun->result_size), // C entry | 208 jsgraph()->CEntryStubConstant(fun->result_size), // C entry |
209 exception, // exception | 209 exception, // exception |
210 jsgraph()->ExternalConstant( | 210 jsgraph()->ExternalConstant( |
211 ExternalReference(f, jsgraph()->isolate())), // ref | 211 ExternalReference(f, jsgraph()->isolate())), // ref |
212 jsgraph()->Int32Constant(fun->nargs), // arity | 212 jsgraph()->Int32Constant(fun->nargs), // arity |
213 jsgraph()->Constant(module->context), // context | 213 jsgraph()->Constant(module->instance->context), // context |
214 *effect_ptr, | 214 *effect_ptr, |
215 *control_ptr}; | 215 *control_ptr}; |
216 | 216 |
217 Node* node = graph()->NewNode( | 217 Node* node = graph()->NewNode( |
218 common()->Call(desc), static_cast<int>(arraysize(inputs)), inputs); | 218 common()->Call(desc), static_cast<int>(arraysize(inputs)), inputs); |
219 *control_ptr = node; | 219 *control_ptr = node; |
220 *effect_ptr = node; | 220 *effect_ptr = node; |
221 } | 221 } |
222 if (false) { | 222 if (false) { |
223 // End the control flow with a throw | 223 // End the control flow with a throw |
(...skipping 1470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1694 Node* val = | 1694 Node* val = |
1695 FromJS(call, context, | 1695 FromJS(call, context, |
1696 sig->return_count() == 0 ? wasm::kAstStmt : sig->GetReturn()); | 1696 sig->return_count() == 0 ? wasm::kAstStmt : sig->GetReturn()); |
1697 Node* ret = graph()->NewNode(jsgraph()->common()->Return(), val, call, start); | 1697 Node* ret = graph()->NewNode(jsgraph()->common()->Return(), val, call, start); |
1698 | 1698 |
1699 MergeControlToEnd(jsgraph(), ret); | 1699 MergeControlToEnd(jsgraph(), ret); |
1700 } | 1700 } |
1701 | 1701 |
1702 | 1702 |
1703 Node* WasmGraphBuilder::MemBuffer(uint32_t offset) { | 1703 Node* WasmGraphBuilder::MemBuffer(uint32_t offset) { |
| 1704 DCHECK(module_ && module_->instance); |
1704 if (offset == 0) { | 1705 if (offset == 0) { |
1705 if (!mem_buffer_) | 1706 if (!mem_buffer_) { |
1706 mem_buffer_ = jsgraph()->IntPtrConstant(module_->mem_start); | 1707 mem_buffer_ = jsgraph()->IntPtrConstant( |
| 1708 reinterpret_cast<uintptr_t>(module_->instance->mem_start)); |
| 1709 } |
1707 return mem_buffer_; | 1710 return mem_buffer_; |
1708 } else { | 1711 } else { |
1709 return jsgraph()->IntPtrConstant(module_->mem_start + offset); | 1712 return jsgraph()->IntPtrConstant( |
| 1713 reinterpret_cast<uintptr_t>(module_->instance->mem_start + offset)); |
1710 } | 1714 } |
1711 } | 1715 } |
1712 | 1716 |
1713 | 1717 |
1714 Node* WasmGraphBuilder::MemSize(uint32_t offset) { | 1718 Node* WasmGraphBuilder::MemSize(uint32_t offset) { |
1715 int32_t size = static_cast<int>(module_->mem_end - module_->mem_start); | 1719 DCHECK(module_ && module_->instance); |
| 1720 uint32_t size = static_cast<uint32_t>(module_->instance->mem_size); |
1716 if (offset == 0) { | 1721 if (offset == 0) { |
1717 if (!mem_size_) mem_size_ = jsgraph()->Int32Constant(size); | 1722 if (!mem_size_) mem_size_ = jsgraph()->Int32Constant(size); |
1718 return mem_size_; | 1723 return mem_size_; |
1719 } else { | 1724 } else { |
1720 return jsgraph()->Int32Constant(size + offset); | 1725 return jsgraph()->Int32Constant(size + offset); |
1721 } | 1726 } |
1722 } | 1727 } |
1723 | 1728 |
1724 | 1729 |
1725 Node* WasmGraphBuilder::FunctionTable() { | 1730 Node* WasmGraphBuilder::FunctionTable() { |
| 1731 DCHECK(module_ && module_->instance && |
| 1732 !module_->instance->function_table.is_null()); |
1726 if (!function_table_) { | 1733 if (!function_table_) { |
1727 DCHECK(!module_->function_table.is_null()); | 1734 function_table_ = jsgraph()->Constant(module_->instance->function_table); |
1728 function_table_ = jsgraph()->Constant(module_->function_table); | |
1729 } | 1735 } |
1730 return function_table_; | 1736 return function_table_; |
1731 } | 1737 } |
1732 | 1738 |
1733 | 1739 |
1734 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { | 1740 Node* WasmGraphBuilder::LoadGlobal(uint32_t index) { |
| 1741 DCHECK(module_ && module_->instance && module_->instance->globals_start); |
1735 MachineType mem_type = module_->GetGlobalType(index); | 1742 MachineType mem_type = module_->GetGlobalType(index); |
1736 Node* addr = jsgraph()->IntPtrConstant( | 1743 Node* addr = jsgraph()->IntPtrConstant( |
1737 module_->globals_area + module_->module->globals->at(index).offset); | 1744 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
| 1745 module_->module->globals->at(index).offset)); |
1738 const Operator* op = jsgraph()->machine()->Load(mem_type); | 1746 const Operator* op = jsgraph()->machine()->Load(mem_type); |
1739 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, | 1747 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), *effect_, |
1740 *control_); | 1748 *control_); |
1741 *effect_ = node; | 1749 *effect_ = node; |
1742 return node; | 1750 return node; |
1743 } | 1751 } |
1744 | 1752 |
1745 | 1753 |
1746 Node* WasmGraphBuilder::StoreGlobal(uint32_t index, Node* val) { | 1754 Node* WasmGraphBuilder::StoreGlobal(uint32_t index, Node* val) { |
| 1755 DCHECK(module_ && module_->instance && module_->instance->globals_start); |
1747 MachineType mem_type = module_->GetGlobalType(index); | 1756 MachineType mem_type = module_->GetGlobalType(index); |
1748 Node* addr = jsgraph()->IntPtrConstant( | 1757 Node* addr = jsgraph()->IntPtrConstant( |
1749 module_->globals_area + module_->module->globals->at(index).offset); | 1758 reinterpret_cast<uintptr_t>(module_->instance->globals_start + |
| 1759 module_->module->globals->at(index).offset)); |
1750 const Operator* op = jsgraph()->machine()->Store( | 1760 const Operator* op = jsgraph()->machine()->Store( |
1751 StoreRepresentation(mem_type.representation(), kNoWriteBarrier)); | 1761 StoreRepresentation(mem_type.representation(), kNoWriteBarrier)); |
1752 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), val, | 1762 Node* node = graph()->NewNode(op, addr, jsgraph()->Int32Constant(0), val, |
1753 *effect_, *control_); | 1763 *effect_, *control_); |
1754 *effect_ = node; | 1764 *effect_ = node; |
1755 return node; | 1765 return node; |
1756 } | 1766 } |
1757 | 1767 |
1758 | 1768 |
1759 void WasmGraphBuilder::BoundsCheckMem(MachineType memtype, Node* index, | 1769 void WasmGraphBuilder::BoundsCheckMem(MachineType memtype, Node* index, |
1760 uint32_t offset) { | 1770 uint32_t offset) { |
1761 // TODO(turbofan): fold bounds checks for constant indexes. | 1771 // TODO(turbofan): fold bounds checks for constant indexes. |
1762 CHECK_GE(module_->mem_end, module_->mem_start); | 1772 DCHECK(module_ && module_->instance); |
1763 ptrdiff_t size = module_->mem_end - module_->mem_start; | 1773 size_t size = module_->instance->mem_size; |
1764 byte memsize = wasm::WasmOpcodes::MemSize(memtype); | 1774 byte memsize = wasm::WasmOpcodes::MemSize(memtype); |
1765 Node* cond; | 1775 Node* cond; |
1766 if (static_cast<ptrdiff_t>(offset) >= size || | 1776 if (offset >= size || (static_cast<uint64_t>(offset) + memsize) > size) { |
1767 static_cast<ptrdiff_t>(offset + memsize) > size) { | |
1768 // The access will always throw. | 1777 // The access will always throw. |
1769 cond = jsgraph()->Int32Constant(0); | 1778 cond = jsgraph()->Int32Constant(0); |
1770 } else { | 1779 } else { |
1771 // Check against the limit. | 1780 // Check against the limit. |
1772 size_t limit = size - offset - memsize; | 1781 size_t limit = size - offset - memsize; |
1773 CHECK(limit <= kMaxUInt32); | 1782 CHECK(limit <= kMaxUInt32); |
1774 cond = graph()->NewNode( | 1783 cond = graph()->NewNode( |
1775 jsgraph()->machine()->Uint32LessThanOrEqual(), index, | 1784 jsgraph()->machine()->Uint32LessThanOrEqual(), index, |
1776 jsgraph()->Int32Constant(static_cast<uint32_t>(limit))); | 1785 jsgraph()->Int32Constant(static_cast<uint32_t>(limit))); |
1777 } | 1786 } |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2092 code->Disassemble(buffer.start(), os); | 2101 code->Disassemble(buffer.start(), os); |
2093 } | 2102 } |
2094 #endif | 2103 #endif |
2095 return code; | 2104 return code; |
2096 } | 2105 } |
2097 | 2106 |
2098 | 2107 |
2099 } // namespace compiler | 2108 } // namespace compiler |
2100 } // namespace internal | 2109 } // namespace internal |
2101 } // namespace v8 | 2110 } // namespace v8 |
OLD | NEW |