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/assembler-inl.h" | 9 #include "src/assembler-inl.h" |
10 #include "src/base/platform/elapsed-timer.h" | 10 #include "src/base/platform/elapsed-timer.h" |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, | 104 Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
105 Node** parameters, int parameter_count, | 105 Node** parameters, int parameter_count, |
106 Node** effect_ptr, Node* control) { | 106 Node** effect_ptr, Node* control) { |
107 return BuildCallToRuntimeWithContext(f, jsgraph, jsgraph->NoContextConstant(), | 107 return BuildCallToRuntimeWithContext(f, jsgraph, jsgraph->NoContextConstant(), |
108 parameters, parameter_count, effect_ptr, | 108 parameters, parameter_count, effect_ptr, |
109 control); | 109 control); |
110 } | 110 } |
111 | 111 |
112 } // namespace | 112 } // namespace |
113 | 113 |
| 114 // TODO(eholk): Support trap handlers on other platforms. |
| 115 #if V8_TARGET_ARCH_X64 && V8_OS_LINUX |
| 116 const bool kTrapHandlerSupported = true; |
| 117 #else |
| 118 const bool kTrapHandlerSupported = false; |
| 119 #endif |
| 120 |
114 // A helper that handles building graph fragments for trapping. | 121 // A helper that handles building graph fragments for trapping. |
115 // To avoid generating a ton of redundant code that just calls the runtime | 122 // To avoid generating a ton of redundant code that just calls the runtime |
116 // to trap, we generate a per-trap-reason block of code that all trap sites | 123 // to trap, we generate a per-trap-reason block of code that all trap sites |
117 // in this function will branch to. | 124 // in this function will branch to. |
118 class WasmTrapHelper : public ZoneObject { | 125 class WasmTrapHelper : public ZoneObject { |
119 public: | 126 public: |
120 explicit WasmTrapHelper(WasmGraphBuilder* builder) | 127 explicit WasmTrapHelper(WasmGraphBuilder* builder) |
121 : builder_(builder), | 128 : builder_(builder), |
122 jsgraph_(builder->jsgraph()), | 129 jsgraph_(builder->jsgraph()), |
123 graph_(builder->jsgraph() ? builder->jsgraph()->graph() : nullptr) {} | 130 graph_(builder->jsgraph() ? builder->jsgraph()->graph() : nullptr) {} |
(...skipping 2669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2793 int pos = 0; | 2800 int pos = 0; |
2794 args[pos++] = HeapConstant(wasm_code); | 2801 args[pos++] = HeapConstant(wasm_code); |
2795 | 2802 |
2796 // Convert JS parameters to WASM numbers. | 2803 // Convert JS parameters to WASM numbers. |
2797 for (int i = 0; i < wasm_count; ++i) { | 2804 for (int i = 0; i < wasm_count; ++i) { |
2798 Node* param = Param(i + 1); | 2805 Node* param = Param(i + 1); |
2799 Node* wasm_param = FromJS(param, context, sig->GetParam(i)); | 2806 Node* wasm_param = FromJS(param, context, sig->GetParam(i)); |
2800 args[pos++] = wasm_param; | 2807 args[pos++] = wasm_param; |
2801 } | 2808 } |
2802 | 2809 |
2803 // Set the ThreadInWasm flag before we do the actual call. | |
2804 if (trap_handler::UseTrapHandler()) { | |
2805 // TODO(eholk): Set the flag directly without a runtime call. We should be | |
2806 // able to store directly to a location in the isolate (later TLS) that sets | |
2807 // the g_thread_in_wasm_code flag. | |
2808 BuildCallToRuntime(Runtime::kSetThreadInWasm, jsgraph(), nullptr, 0, | |
2809 effect_, *control_); | |
2810 } | |
2811 | |
2812 args[pos++] = *effect_; | 2810 args[pos++] = *effect_; |
2813 args[pos++] = *control_; | 2811 args[pos++] = *control_; |
2814 | 2812 |
2815 // Call the WASM code. | 2813 // Call the WASM code. |
2816 CallDescriptor* desc = | 2814 CallDescriptor* desc = |
2817 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); | 2815 wasm::ModuleEnv::GetWasmCallDescriptor(jsgraph()->zone(), sig); |
2818 | 2816 |
2819 Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), count, args); | 2817 Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), count, args); |
2820 *effect_ = call; | 2818 *effect_ = call; |
2821 | |
2822 // Clear the ThreadInWasmFlag | |
2823 if (trap_handler::UseTrapHandler()) { | |
2824 // TODO(eholk): Set the flag directly without a runtime call. We should be | |
2825 // able to store directly to a location in the isolate (later TLS) that sets | |
2826 // the g_thread_in_wasm_code flag. | |
2827 BuildCallToRuntime(Runtime::kClearThreadInWasm, jsgraph(), nullptr, 0, | |
2828 effect_, *control_); | |
2829 } | |
2830 | |
2831 Node* retval = call; | 2819 Node* retval = call; |
2832 Node* jsval = ToJS( | 2820 Node* jsval = ToJS( |
2833 retval, sig->return_count() == 0 ? wasm::kWasmStmt : sig->GetReturn()); | 2821 retval, sig->return_count() == 0 ? wasm::kWasmStmt : sig->GetReturn()); |
2834 Return(jsval); | 2822 Return(jsval); |
2835 } | 2823 } |
2836 | 2824 |
2837 int WasmGraphBuilder::AddParameterNodes(Node** args, int pos, int param_count, | 2825 int WasmGraphBuilder::AddParameterNodes(Node** args, int pos, int param_count, |
2838 wasm::FunctionSig* sig) { | 2826 wasm::FunctionSig* sig) { |
2839 // Convert WASM numbers to JS values. | 2827 // Convert WASM numbers to JS values. |
2840 int param_index = 0; | 2828 int param_index = 0; |
(...skipping 26 matching lines...) Expand all Loading... |
2867 jsgraph(), context, nullptr, 0, | 2855 jsgraph(), context, nullptr, 0, |
2868 effect_, *control_)); | 2856 effect_, *control_)); |
2869 return; | 2857 return; |
2870 } | 2858 } |
2871 | 2859 |
2872 Node** args = Buffer(wasm_count + 7); | 2860 Node** args = Buffer(wasm_count + 7); |
2873 | 2861 |
2874 Node* call; | 2862 Node* call; |
2875 bool direct_call = false; | 2863 bool direct_call = false; |
2876 | 2864 |
2877 if (trap_handler::UseTrapHandler()) { | |
2878 BuildCallToRuntime(Runtime::kClearThreadInWasm, jsgraph(), nullptr, 0, | |
2879 effect_, *control_); | |
2880 } | |
2881 | |
2882 if (target->IsJSFunction()) { | 2865 if (target->IsJSFunction()) { |
2883 Handle<JSFunction> function = Handle<JSFunction>::cast(target); | 2866 Handle<JSFunction> function = Handle<JSFunction>::cast(target); |
2884 if (function->shared()->internal_formal_parameter_count() == wasm_count) { | 2867 if (function->shared()->internal_formal_parameter_count() == wasm_count) { |
2885 direct_call = true; | 2868 direct_call = true; |
2886 int pos = 0; | 2869 int pos = 0; |
2887 args[pos++] = jsgraph()->Constant(target); // target callable. | 2870 args[pos++] = jsgraph()->Constant(target); // target callable. |
2888 // Receiver. | 2871 // Receiver. |
2889 if (is_sloppy(function->shared()->language_mode()) && | 2872 if (is_sloppy(function->shared()->language_mode()) && |
2890 !function->shared()->native()) { | 2873 !function->shared()->native()) { |
2891 args[pos++] = | 2874 args[pos++] = |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2936 args[pos++] = HeapConstant(isolate->native_context()); | 2919 args[pos++] = HeapConstant(isolate->native_context()); |
2937 args[pos++] = *effect_; | 2920 args[pos++] = *effect_; |
2938 args[pos++] = *control_; | 2921 args[pos++] = *control_; |
2939 | 2922 |
2940 call = graph()->NewNode(jsgraph()->common()->Call(desc), pos, args); | 2923 call = graph()->NewNode(jsgraph()->common()->Call(desc), pos, args); |
2941 } | 2924 } |
2942 | 2925 |
2943 *effect_ = call; | 2926 *effect_ = call; |
2944 SetSourcePosition(call, 0); | 2927 SetSourcePosition(call, 0); |
2945 | 2928 |
2946 if (trap_handler::UseTrapHandler()) { | |
2947 BuildCallToRuntime(Runtime::kSetThreadInWasm, jsgraph(), nullptr, 0, | |
2948 effect_, *control_); | |
2949 } | |
2950 | |
2951 // Convert the return value back. | 2929 // Convert the return value back. |
2952 Node* i32_zero = jsgraph()->Int32Constant(0); | 2930 Node* i32_zero = jsgraph()->Int32Constant(0); |
2953 Node* val = sig->return_count() == 0 | 2931 Node* val = sig->return_count() == 0 |
2954 ? i32_zero | 2932 ? i32_zero |
2955 : FromJS(call, HeapConstant(isolate->native_context()), | 2933 : FromJS(call, HeapConstant(isolate->native_context()), |
2956 sig->GetReturn()); | 2934 sig->GetReturn()); |
2957 Return(val); | 2935 Return(val); |
2958 } | 2936 } |
2959 | 2937 |
2960 void WasmGraphBuilder::BuildWasmInterpreterEntry( | 2938 void WasmGraphBuilder::BuildWasmInterpreterEntry( |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3210 trap_->AddTrapIfFalse(wasm::kTrapMemOutOfBounds, cond, position); | 3188 trap_->AddTrapIfFalse(wasm::kTrapMemOutOfBounds, cond, position); |
3211 } | 3189 } |
3212 | 3190 |
3213 Node* WasmGraphBuilder::LoadMem(wasm::ValueType type, MachineType memtype, | 3191 Node* WasmGraphBuilder::LoadMem(wasm::ValueType type, MachineType memtype, |
3214 Node* index, uint32_t offset, | 3192 Node* index, uint32_t offset, |
3215 uint32_t alignment, | 3193 uint32_t alignment, |
3216 wasm::WasmCodePosition position) { | 3194 wasm::WasmCodePosition position) { |
3217 Node* load; | 3195 Node* load; |
3218 | 3196 |
3219 // WASM semantics throw on OOB. Introduce explicit bounds check. | 3197 // WASM semantics throw on OOB. Introduce explicit bounds check. |
3220 if (!FLAG_wasm_trap_handler || !V8_TRAP_HANDLER_SUPPORTED) { | 3198 if (!FLAG_wasm_trap_handler || !kTrapHandlerSupported) { |
3221 BoundsCheckMem(memtype, index, offset, position); | 3199 BoundsCheckMem(memtype, index, offset, position); |
3222 } | 3200 } |
3223 bool aligned = static_cast<int>(alignment) >= | 3201 bool aligned = static_cast<int>(alignment) >= |
3224 ElementSizeLog2Of(memtype.representation()); | 3202 ElementSizeLog2Of(memtype.representation()); |
3225 | 3203 |
3226 if (aligned || | 3204 if (aligned || |
3227 jsgraph()->machine()->UnalignedLoadSupported(memtype, alignment)) { | 3205 jsgraph()->machine()->UnalignedLoadSupported(memtype, alignment)) { |
3228 if (FLAG_wasm_trap_handler && V8_TRAP_HANDLER_SUPPORTED) { | 3206 if (FLAG_wasm_trap_handler && kTrapHandlerSupported) { |
3229 DCHECK(FLAG_wasm_guard_pages); | 3207 DCHECK(FLAG_wasm_guard_pages); |
3230 Node* position_node = jsgraph()->Int32Constant(position); | 3208 Node* position_node = jsgraph()->Int32Constant(position); |
3231 load = graph()->NewNode(jsgraph()->machine()->ProtectedLoad(memtype), | 3209 load = graph()->NewNode(jsgraph()->machine()->ProtectedLoad(memtype), |
3232 MemBuffer(offset), index, position_node, *effect_, | 3210 MemBuffer(offset), index, position_node, *effect_, |
3233 *control_); | 3211 *control_); |
3234 } else { | 3212 } else { |
3235 load = graph()->NewNode(jsgraph()->machine()->Load(memtype), | 3213 load = graph()->NewNode(jsgraph()->machine()->Load(memtype), |
3236 MemBuffer(offset), index, *effect_, *control_); | 3214 MemBuffer(offset), index, *effect_, *control_); |
3237 } | 3215 } |
3238 } else { | 3216 } else { |
3239 // TODO(eholk): Support unaligned loads with trap handlers. | 3217 // TODO(eholk): Support unaligned loads with trap handlers. |
3240 DCHECK(!FLAG_wasm_trap_handler || !V8_TRAP_HANDLER_SUPPORTED); | 3218 DCHECK(!FLAG_wasm_trap_handler || !kTrapHandlerSupported); |
3241 load = graph()->NewNode(jsgraph()->machine()->UnalignedLoad(memtype), | 3219 load = graph()->NewNode(jsgraph()->machine()->UnalignedLoad(memtype), |
3242 MemBuffer(offset), index, *effect_, *control_); | 3220 MemBuffer(offset), index, *effect_, *control_); |
3243 } | 3221 } |
3244 | 3222 |
3245 *effect_ = load; | 3223 *effect_ = load; |
3246 | 3224 |
3247 #if defined(V8_TARGET_BIG_ENDIAN) | 3225 #if defined(V8_TARGET_BIG_ENDIAN) |
3248 load = BuildChangeEndianness(load, memtype, type); | 3226 load = BuildChangeEndianness(load, memtype, type); |
3249 #endif | 3227 #endif |
3250 | 3228 |
(...skipping 13 matching lines...) Expand all Loading... |
3264 return load; | 3242 return load; |
3265 } | 3243 } |
3266 | 3244 |
3267 | 3245 |
3268 Node* WasmGraphBuilder::StoreMem(MachineType memtype, Node* index, | 3246 Node* WasmGraphBuilder::StoreMem(MachineType memtype, Node* index, |
3269 uint32_t offset, uint32_t alignment, Node* val, | 3247 uint32_t offset, uint32_t alignment, Node* val, |
3270 wasm::WasmCodePosition position) { | 3248 wasm::WasmCodePosition position) { |
3271 Node* store; | 3249 Node* store; |
3272 | 3250 |
3273 // WASM semantics throw on OOB. Introduce explicit bounds check. | 3251 // WASM semantics throw on OOB. Introduce explicit bounds check. |
3274 if (!FLAG_wasm_trap_handler || !V8_TRAP_HANDLER_SUPPORTED) { | 3252 if (!FLAG_wasm_trap_handler || !kTrapHandlerSupported) { |
3275 BoundsCheckMem(memtype, index, offset, position); | 3253 BoundsCheckMem(memtype, index, offset, position); |
3276 } | 3254 } |
3277 StoreRepresentation rep(memtype.representation(), kNoWriteBarrier); | 3255 StoreRepresentation rep(memtype.representation(), kNoWriteBarrier); |
3278 | 3256 |
3279 bool aligned = static_cast<int>(alignment) >= | 3257 bool aligned = static_cast<int>(alignment) >= |
3280 ElementSizeLog2Of(memtype.representation()); | 3258 ElementSizeLog2Of(memtype.representation()); |
3281 | 3259 |
3282 #if defined(V8_TARGET_BIG_ENDIAN) | 3260 #if defined(V8_TARGET_BIG_ENDIAN) |
3283 val = BuildChangeEndianness(val, memtype); | 3261 val = BuildChangeEndianness(val, memtype); |
3284 #endif | 3262 #endif |
3285 | 3263 |
3286 if (aligned || | 3264 if (aligned || |
3287 jsgraph()->machine()->UnalignedStoreSupported(memtype, alignment)) { | 3265 jsgraph()->machine()->UnalignedStoreSupported(memtype, alignment)) { |
3288 if (FLAG_wasm_trap_handler && V8_TRAP_HANDLER_SUPPORTED) { | 3266 if (FLAG_wasm_trap_handler && kTrapHandlerSupported) { |
3289 Node* position_node = jsgraph()->Int32Constant(position); | 3267 Node* position_node = jsgraph()->Int32Constant(position); |
3290 store = graph()->NewNode( | 3268 store = graph()->NewNode( |
3291 jsgraph()->machine()->ProtectedStore(memtype.representation()), | 3269 jsgraph()->machine()->ProtectedStore(memtype.representation()), |
3292 MemBuffer(offset), index, val, position_node, *effect_, *control_); | 3270 MemBuffer(offset), index, val, position_node, *effect_, *control_); |
3293 } else { | 3271 } else { |
3294 StoreRepresentation rep(memtype.representation(), kNoWriteBarrier); | 3272 StoreRepresentation rep(memtype.representation(), kNoWriteBarrier); |
3295 store = | 3273 store = |
3296 graph()->NewNode(jsgraph()->machine()->Store(rep), MemBuffer(offset), | 3274 graph()->NewNode(jsgraph()->machine()->Store(rep), MemBuffer(offset), |
3297 index, val, *effect_, *control_); | 3275 index, val, *effect_, *control_); |
3298 } | 3276 } |
3299 } else { | 3277 } else { |
3300 // TODO(eholk): Support unaligned stores with trap handlers. | 3278 // TODO(eholk): Support unaligned stores with trap handlers. |
3301 DCHECK(!FLAG_wasm_trap_handler || !V8_TRAP_HANDLER_SUPPORTED); | 3279 DCHECK(!FLAG_wasm_trap_handler || !kTrapHandlerSupported); |
3302 UnalignedStoreRepresentation rep(memtype.representation()); | 3280 UnalignedStoreRepresentation rep(memtype.representation()); |
3303 store = | 3281 store = |
3304 graph()->NewNode(jsgraph()->machine()->UnalignedStore(rep), | 3282 graph()->NewNode(jsgraph()->machine()->UnalignedStore(rep), |
3305 MemBuffer(offset), index, val, *effect_, *control_); | 3283 MemBuffer(offset), index, val, *effect_, *control_); |
3306 } | 3284 } |
3307 | 3285 |
3308 *effect_ = store; | 3286 *effect_ = store; |
3309 | 3287 |
3310 return store; | 3288 return store; |
3311 } | 3289 } |
(...skipping 945 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4257 wasm::ErrorThrower* thrower, Isolate* isolate, | 4235 wasm::ErrorThrower* thrower, Isolate* isolate, |
4258 wasm::ModuleBytesEnv* module_env, const wasm::WasmFunction* function) { | 4236 wasm::ModuleBytesEnv* module_env, const wasm::WasmFunction* function) { |
4259 WasmCompilationUnit unit(isolate, module_env, function); | 4237 WasmCompilationUnit unit(isolate, module_env, function); |
4260 unit.ExecuteCompilation(); | 4238 unit.ExecuteCompilation(); |
4261 return unit.FinishCompilation(thrower); | 4239 return unit.FinishCompilation(thrower); |
4262 } | 4240 } |
4263 | 4241 |
4264 } // namespace compiler | 4242 } // namespace compiler |
4265 } // namespace internal | 4243 } // namespace internal |
4266 } // namespace v8 | 4244 } // namespace v8 |
OLD | NEW |