Index: src/compiler/wasm-compiler.cc |
diff --git a/src/compiler/wasm-compiler.cc b/src/compiler/wasm-compiler.cc |
index 5e6a900ee093cb05236994d19c29bb556890f777..c2c61a0df142e2209b2dde8ae48e7777abd951ff 100644 |
--- a/src/compiler/wasm-compiler.cc |
+++ b/src/compiler/wasm-compiler.cc |
@@ -67,10 +67,12 @@ void MergeControlToEnd(JSGraph* jsgraph, Node* node) { |
} |
} |
-Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
- Handle<Context> context, Node** parameters, |
- int parameter_count, Node** effect_ptr, |
- Node* control) { |
+// Do only call this function for code which is not reused across |
titzer
2017/02/24 09:06:06
s/Do only/Only/
Clemens Hammacher
2017/02/24 10:00:40
Done.
|
+// instantiations, as we do not patch the embedded context. |
+Node* BuildCallToRuntimeWithContext(Runtime::FunctionId f, JSGraph* jsgraph, |
+ Node* context, Node** parameters, |
+ int parameter_count, Node** effect_ptr, |
+ Node* control) { |
const Runtime::Function* fun = Runtime::FunctionForId(f); |
CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( |
jsgraph->zone(), f, fun->nargs, Operator::kNoProperties, |
@@ -78,7 +80,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
// CEntryStubConstant nodes have to be created and cached in the main |
// thread. At the moment this is only done for CEntryStubConstant(1). |
DCHECK_EQ(1, fun->result_size); |
- // At the moment we only allow 2 parameters. If more parameters are needed, |
+ // At the moment we only allow 3 parameters. If more parameters are needed, |
// increase this constant accordingly. |
static const int kMaxParams = 3; |
DCHECK_GE(kMaxParams, parameter_count); |
@@ -91,9 +93,7 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
inputs[count++] = jsgraph->ExternalConstant( |
ExternalReference(f, jsgraph->isolate())); // ref |
inputs[count++] = jsgraph->Int32Constant(fun->nargs); // arity |
- inputs[count++] = context.is_null() |
- ? jsgraph->SmiConstant(0) |
- : jsgraph->HeapConstant(context); // context |
+ inputs[count++] = context; // context |
inputs[count++] = *effect_ptr; |
inputs[count++] = control; |
@@ -103,6 +103,14 @@ Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
return node; |
} |
+Node* BuildCallToRuntime(Runtime::FunctionId f, JSGraph* jsgraph, |
+ Node** parameters, int parameter_count, |
+ Node** effect_ptr, Node* control) { |
+ return BuildCallToRuntimeWithContext(f, jsgraph, jsgraph->NoContextConstant(), |
+ parameters, parameter_count, effect_ptr, |
+ control); |
+} |
+ |
} // namespace |
// TODO(eholk): Support trap handlers on other platforms. |
@@ -329,8 +337,7 @@ class WasmTrapHelper : public ZoneObject { |
if (module && !module->instance->context.is_null()) { |
Node* parameters[] = {trap_reason_smi, // message id |
trap_position_smi}; // byte position |
- BuildCallToRuntime(Runtime::kThrowWasmError, jsgraph(), |
- Handle<Context>::null(), parameters, |
+ BuildCallToRuntime(Runtime::kThrowWasmError, jsgraph(), parameters, |
arraysize(parameters), effect_ptr, *control_ptr); |
} |
if (false) { |
@@ -501,7 +508,7 @@ void WasmGraphBuilder::StackCheck(wasm::WasmCodePosition position, |
CallDescriptor::kNoFlags, Operator::kNoProperties); |
Node* stub_code = jsgraph()->HeapConstant(code); |
- Node* context = jsgraph()->SmiConstant(0); |
+ Node* context = jsgraph()->NoContextConstant(); |
Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), stub_code, |
context, *effect, stack_check.if_false); |
@@ -1792,30 +1799,18 @@ Node* WasmGraphBuilder::GrowMemory(Node* input) { |
check_input_range.Chain(*control_); |
- Runtime::FunctionId function_id = Runtime::kWasmGrowMemory; |
- const Runtime::Function* function = Runtime::FunctionForId(function_id); |
- CallDescriptor* desc = Linkage::GetRuntimeCallDescriptor( |
- jsgraph()->zone(), function_id, function->nargs, Operator::kNoThrow, |
- CallDescriptor::kNoFlags); |
- wasm::ModuleEnv* module = module_; |
- input = BuildChangeUint32ToSmi(input); |
- Node* inputs[] = { |
- jsgraph()->CEntryStubConstant(function->result_size), input, // C entry |
- jsgraph()->ExternalConstant( |
- ExternalReference(function_id, jsgraph()->isolate())), // ref |
- jsgraph()->Int32Constant(function->nargs), // arity |
- jsgraph()->HeapConstant(module->instance->context), // context |
- *effect_, |
- check_input_range.if_true}; |
- Node* call = graph()->NewNode(jsgraph()->common()->Call(desc), |
- static_cast<int>(arraysize(inputs)), inputs); |
+ Node* parameters[] = {BuildChangeUint32ToSmi(input)}; |
+ Node* old_effect = *effect_; |
+ Node* call = BuildCallToRuntime(Runtime::kWasmGrowMemory, jsgraph(), |
+ parameters, arraysize(parameters), effect_, |
+ check_input_range.if_true); |
Node* result = BuildChangeSmiToInt32(call); |
result = check_input_range.Phi(MachineRepresentation::kWord32, result, |
jsgraph()->Int32Constant(-1)); |
- *effect_ = graph()->NewNode(jsgraph()->common()->EffectPhi(2), call, *effect_, |
- check_input_range.merge); |
+ *effect_ = graph()->NewNode(jsgraph()->common()->EffectPhi(2), call, |
+ old_effect, check_input_range.merge); |
*control_ = check_input_range.merge; |
return result; |
} |
@@ -1838,8 +1833,7 @@ Node* WasmGraphBuilder::Throw(Node* input) { |
graph()->NewNode(machine->Word32And(), input, Int32Constant(0xFFFFu))); |
Node* parameters[] = {lower, upper}; // thrown value |
- return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), |
- module_->instance->context, parameters, |
+ return BuildCallToRuntime(Runtime::kWasmThrow, jsgraph(), parameters, |
arraysize(parameters), effect_, *control_); |
} |
@@ -1849,8 +1843,7 @@ Node* WasmGraphBuilder::Catch(Node* input, wasm::WasmCodePosition position) { |
Node* parameters[] = {input}; // caught value |
Node* value = |
BuildCallToRuntime(Runtime::kWasmGetCaughtExceptionValue, jsgraph(), |
- module_->instance->context, parameters, |
- arraysize(parameters), effect_, *control_); |
+ parameters, arraysize(parameters), effect_, *control_); |
Node* is_smi; |
Node* is_heap; |
@@ -2771,12 +2764,18 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code, |
*control_ = start; |
*effect_ = start; |
+ // Create the context parameter |
+ Node* context = graph()->NewNode( |
+ jsgraph()->common()->Parameter( |
+ Linkage::GetJSCallContextParamIndex(wasm_count + 1), "%context"), |
+ graph()->start()); |
+ |
if (!HasJSCompatibleSignature(sig_)) { |
- // Throw a TypeError. The native context is good enough here because we |
- // only throw a TypeError. |
- BuildCallToRuntime(Runtime::kWasmThrowTypeError, jsgraph(), |
- jsgraph()->isolate()->native_context(), nullptr, 0, |
- effect_, *control_); |
+ // Throw a TypeError. Use the context of the calling javascript function |
+ // (passed as a parameter), such that the generated code is context |
+ // independent. |
+ BuildCallToRuntimeWithContext(Runtime::kWasmThrowTypeError, jsgraph(), |
+ context, nullptr, 0, effect_, *control_); |
// Add a dummy call to the wasm function so that the generated wrapper |
// contains a reference to the wrapped wasm function. Without this reference |
@@ -2795,12 +2794,6 @@ void WasmGraphBuilder::BuildJSToWasmWrapper(Handle<Code> wasm_code, |
return; |
} |
- // Create the context parameter |
- Node* context = graph()->NewNode( |
- jsgraph()->common()->Parameter( |
- Linkage::GetJSCallContextParamIndex(wasm_count + 1), "%context"), |
- graph()->start()); |
- |
int pos = 0; |
args[pos++] = HeapConstant(wasm_code); |
@@ -2851,11 +2844,13 @@ void WasmGraphBuilder::BuildWasmToJSWrapper(Handle<JSReceiver> target, |
*control_ = start; |
if (!HasJSCompatibleSignature(sig_)) { |
- // Throw a TypeError. The native context is good enough here because we |
- // only throw a TypeError. |
- Return(BuildCallToRuntime(Runtime::kWasmThrowTypeError, jsgraph(), |
- jsgraph()->isolate()->native_context(), nullptr, |
- 0, effect_, *control_)); |
+ // Throw a TypeError. Embedding the context is ok here, since this code is |
+ // regenerated at instantiation time. |
+ Node* context = |
+ jsgraph()->HeapConstant(jsgraph()->isolate()->native_context()); |
+ Return(BuildCallToRuntimeWithContext(Runtime::kWasmThrowTypeError, |
+ jsgraph(), context, nullptr, 0, |
+ effect_, *control_)); |
return; |
} |
@@ -3007,8 +3002,7 @@ void WasmGraphBuilder::BuildWasmInterpreterEntry( |
jsgraph()->SmiConstant(function_index), // function index |
arg_buffer, // argument buffer |
}; |
- BuildCallToRuntime(Runtime::kWasmRunInterpreter, jsgraph(), |
- instance->compiled_module()->native_context(), parameters, |
+ BuildCallToRuntime(Runtime::kWasmRunInterpreter, jsgraph(), parameters, |
arraysize(parameters), effect_, *control_); |
// Read back the return value. |