V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-stubs-ia32.cc
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_IA32
6 
7 #include "src/api-arguments-inl.h"
8 #include "src/assembler-inl.h"
9 #include "src/base/bits.h"
10 #include "src/bootstrapper.h"
11 #include "src/code-stubs.h"
12 #include "src/frame-constants.h"
13 #include "src/frames.h"
14 #include "src/ic/ic.h"
15 #include "src/ic/stub-cache.h"
16 #include "src/isolate.h"
17 #include "src/macro-assembler.h"
18 #include "src/objects/api-callbacks.h"
19 #include "src/regexp/jsregexp.h"
20 #include "src/regexp/regexp-macro-assembler.h"
21 #include "src/runtime/runtime.h"
22 
23 namespace v8 {
24 namespace internal {
25 
26 #define __ ACCESS_MASM(masm)
27 
28 void JSEntryStub::Generate(MacroAssembler* masm) {
29  Label invoke, handler_entry, exit;
30  Label not_outermost_js, not_outermost_js_2;
31 
32  { // NOLINT. Scope block confuses linter.
33  NoRootArrayScope uninitialized_root_register(masm);
34 
35  // Set up frame.
36  __ push(ebp);
37  __ mov(ebp, esp);
38 
39  // Push marker in two places.
40  StackFrame::Type marker = type();
41  __ push(Immediate(StackFrame::TypeToMarker(marker))); // marker
42  ExternalReference context_address =
43  ExternalReference::Create(IsolateAddressId::kContextAddress, isolate());
44  __ push(Operand(context_address.address(),
45  RelocInfo::EXTERNAL_REFERENCE)); // context
46  // Save callee-saved registers (C calling conventions).
47  __ push(edi);
48  __ push(esi);
49  __ push(ebx);
50 
51  __ InitializeRootRegister();
52  }
53 
54  // Save copies of the top frame descriptor on the stack.
55  ExternalReference c_entry_fp =
56  ExternalReference::Create(IsolateAddressId::kCEntryFPAddress, isolate());
57  __ push(__ ExternalReferenceAsOperand(c_entry_fp, edi));
58 
59  // If this is the outermost JS call, set js_entry_sp value.
60  ExternalReference js_entry_sp =
61  ExternalReference::Create(IsolateAddressId::kJSEntrySPAddress, isolate());
62  __ cmp(__ ExternalReferenceAsOperand(js_entry_sp, edi), Immediate(0));
63  __ j(not_equal, &not_outermost_js, Label::kNear);
64  __ mov(__ ExternalReferenceAsOperand(js_entry_sp, edi), ebp);
65  __ push(Immediate(StackFrame::OUTERMOST_JSENTRY_FRAME));
66  __ jmp(&invoke, Label::kNear);
67  __ bind(&not_outermost_js);
68  __ push(Immediate(StackFrame::INNER_JSENTRY_FRAME));
69 
70  // Jump to a faked try block that does the invoke, with a faked catch
71  // block that sets the pending exception.
72  __ jmp(&invoke);
73  __ bind(&handler_entry);
74  handler_offset_ = handler_entry.pos();
75  // Caught exception: Store result (exception) in the pending exception
76  // field in the JSEnv and return a failure sentinel.
77  ExternalReference pending_exception = ExternalReference::Create(
78  IsolateAddressId::kPendingExceptionAddress, isolate());
79  __ mov(__ ExternalReferenceAsOperand(pending_exception, edi), eax);
80  __ mov(eax, Immediate(isolate()->factory()->exception()));
81  __ jmp(&exit);
82 
83  // Invoke: Link this frame into the handler chain.
84  __ bind(&invoke);
85  __ PushStackHandler(edi);
86 
87  // Invoke the function by calling through JS entry trampoline builtin and
88  // pop the faked function when we return. Notice that we cannot store a
89  // reference to the trampoline code directly in this stub, because the
90  // builtin stubs may not have been generated yet.
91  __ Call(EntryTrampoline(), RelocInfo::CODE_TARGET);
92 
93  // Unlink this frame from the handler chain.
94  __ PopStackHandler(edi);
95 
96  __ bind(&exit);
97 
98  // Check if the current stack frame is marked as the outermost JS frame.
99  __ pop(edi);
100  __ cmp(edi, Immediate(StackFrame::OUTERMOST_JSENTRY_FRAME));
101  __ j(not_equal, &not_outermost_js_2);
102  __ mov(__ ExternalReferenceAsOperand(js_entry_sp, edi), Immediate(0));
103  __ bind(&not_outermost_js_2);
104 
105  // Restore the top frame descriptor from the stack.
106  __ pop(__ ExternalReferenceAsOperand(c_entry_fp, edi));
107 
108  // Restore callee-saved registers (C calling conventions).
109  __ pop(ebx);
110  __ pop(esi);
111  __ pop(edi);
112  __ add(esp, Immediate(2 * kPointerSize)); // remove markers
113 
114  // Restore frame pointer and return.
115  __ pop(ebp);
116  __ ret(0);
117 }
118 
119 // Generates an Operand for saving parameters after PrepareCallApiFunction.
120 static Operand ApiParameterOperand(int index) {
121  return Operand(esp, index * kPointerSize);
122 }
123 
124 
125 // Prepares stack to put arguments (aligns and so on). Reserves
126 // space for return value if needed (assumes the return value is a handle).
127 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
128 // etc. Saves context (esi). If space was reserved for return value then
129 // stores the pointer to the reserved slot into esi.
130 static void PrepareCallApiFunction(MacroAssembler* masm, int argc,
131  Register scratch) {
132  __ EnterApiExitFrame(argc, scratch);
133  if (__ emit_debug_code()) {
134  __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
135  }
136 }
137 
138 // Calls an API function. Allocates HandleScope, extracts returned value
139 // from handle and propagates exceptions. Clobbers esi, edi and
140 // caller-save registers. Restores context. On return removes
141 // stack_space * kPointerSize (GCed).
142 static void CallApiFunctionAndReturn(MacroAssembler* masm,
143  Register function_address,
144  ExternalReference thunk_ref,
145  Operand thunk_last_arg, int stack_space,
146  Operand* stack_space_operand,
147  Operand return_value_operand) {
148  Isolate* isolate = masm->isolate();
149 
150  ExternalReference next_address =
151  ExternalReference::handle_scope_next_address(isolate);
152  ExternalReference limit_address =
153  ExternalReference::handle_scope_limit_address(isolate);
154  ExternalReference level_address =
155  ExternalReference::handle_scope_level_address(isolate);
156 
157  DCHECK(edx == function_address);
158  // Allocate HandleScope in callee-save registers.
159  __ add(__ ExternalReferenceAsOperand(level_address, esi), Immediate(1));
160  __ mov(esi, __ ExternalReferenceAsOperand(next_address, esi));
161  __ mov(edi, __ ExternalReferenceAsOperand(limit_address, edi));
162 
163  if (FLAG_log_timer_events) {
164  FrameScope frame(masm, StackFrame::MANUAL);
165  __ PushSafepointRegisters();
166  __ PrepareCallCFunction(1, eax);
167  __ Move(Operand(esp, 0),
168  Immediate(ExternalReference::isolate_address(isolate)));
169  __ CallCFunction(ExternalReference::log_enter_external_function(), 1);
170  __ PopSafepointRegisters();
171  }
172 
173 
174  Label profiler_disabled;
175  Label end_profiler_check;
176  __ Move(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
177  __ cmpb(Operand(eax, 0), Immediate(0));
178  __ j(zero, &profiler_disabled);
179 
180  // Additional parameter is the address of the actual getter function.
181  __ mov(thunk_last_arg, function_address);
182  // Call the api function.
183  __ Move(eax, Immediate(thunk_ref));
184  __ call(eax);
185  __ jmp(&end_profiler_check);
186 
187  __ bind(&profiler_disabled);
188  // Call the api function.
189  __ call(function_address);
190  __ bind(&end_profiler_check);
191 
192  if (FLAG_log_timer_events) {
193  FrameScope frame(masm, StackFrame::MANUAL);
194  __ PushSafepointRegisters();
195  __ PrepareCallCFunction(1, eax);
196  __ mov(eax, Immediate(ExternalReference::isolate_address(isolate)));
197  __ mov(Operand(esp, 0), eax);
198  __ CallCFunction(ExternalReference::log_leave_external_function(), 1);
199  __ PopSafepointRegisters();
200  }
201 
202  Label prologue;
203  // Load the value from ReturnValue
204  __ mov(eax, return_value_operand);
205 
206  Label promote_scheduled_exception;
207  Label delete_allocated_handles;
208  Label leave_exit_frame;
209 
210  __ bind(&prologue);
211  // No more valid handles (the result handle was the last one). Restore
212  // previous handle scope.
213  __ mov(__ ExternalReferenceAsOperand(next_address, ecx), esi);
214  __ sub(__ ExternalReferenceAsOperand(level_address, ecx), Immediate(1));
215  __ Assert(above_equal, AbortReason::kInvalidHandleScopeLevel);
216  __ cmp(edi, __ ExternalReferenceAsOperand(limit_address, ecx));
217  __ j(not_equal, &delete_allocated_handles);
218 
219  // Leave the API exit frame.
220  __ bind(&leave_exit_frame);
221  if (stack_space_operand != nullptr) {
222  __ mov(edx, *stack_space_operand);
223  }
224  __ LeaveApiExitFrame();
225 
226  // Check if the function scheduled an exception.
227  ExternalReference scheduled_exception_address =
228  ExternalReference::scheduled_exception_address(isolate);
229  __ mov(ecx, __ ExternalReferenceAsOperand(scheduled_exception_address, ecx));
230  __ CompareRoot(ecx, RootIndex::kTheHoleValue);
231  __ j(not_equal, &promote_scheduled_exception);
232 
233 #if DEBUG
234  // Check if the function returned a valid JavaScript value.
235  Label ok;
236  Register return_value = eax;
237  Register map = ecx;
238 
239  __ JumpIfSmi(return_value, &ok, Label::kNear);
240  __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
241 
242  __ CmpInstanceType(map, LAST_NAME_TYPE);
243  __ j(below_equal, &ok, Label::kNear);
244 
245  __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
246  __ j(above_equal, &ok, Label::kNear);
247 
248  __ CompareRoot(map, RootIndex::kHeapNumberMap);
249  __ j(equal, &ok, Label::kNear);
250 
251  __ CompareRoot(return_value, RootIndex::kUndefinedValue);
252  __ j(equal, &ok, Label::kNear);
253 
254  __ CompareRoot(return_value, RootIndex::kTrueValue);
255  __ j(equal, &ok, Label::kNear);
256 
257  __ CompareRoot(return_value, RootIndex::kFalseValue);
258  __ j(equal, &ok, Label::kNear);
259 
260  __ CompareRoot(return_value, RootIndex::kNullValue);
261  __ j(equal, &ok, Label::kNear);
262 
263  __ Abort(AbortReason::kAPICallReturnedInvalidObject);
264 
265  __ bind(&ok);
266 #endif
267 
268  if (stack_space_operand != nullptr) {
269  DCHECK_EQ(0, stack_space);
270  __ pop(ecx);
271  __ add(esp, edx);
272  __ jmp(ecx);
273  } else {
274  __ ret(stack_space * kPointerSize);
275  }
276 
277  // Re-throw by promoting a scheduled exception.
278  __ bind(&promote_scheduled_exception);
279  __ TailCallRuntime(Runtime::kPromoteScheduledException);
280 
281  // HandleScope limit has changed. Delete allocated extensions.
282  ExternalReference delete_extensions =
283  ExternalReference::delete_handle_scope_extensions();
284  __ bind(&delete_allocated_handles);
285  __ mov(__ ExternalReferenceAsOperand(limit_address, ecx), edi);
286  __ mov(edi, eax);
287  __ Move(eax, Immediate(ExternalReference::isolate_address(isolate)));
288  __ mov(Operand(esp, 0), eax);
289  __ Move(eax, Immediate(delete_extensions));
290  __ call(eax);
291  __ mov(eax, edi);
292  __ jmp(&leave_exit_frame);
293 }
294 
295 void CallApiCallbackStub::Generate(MacroAssembler* masm) {
296  // ----------- S t a t e -------------
297  // -- eax : call_data
298  // -- ecx : holder
299  // -- edx : api_function_address
300  // -- esi : context
301  // --
302  // -- esp[0] : return address
303  // -- esp[4] : last argument
304  // -- ...
305  // -- esp[argc * 4] : first argument
306  // -- esp[(argc + 1) * 4] : receiver
307  // -----------------------------------
308 
309  Register call_data = eax;
310  Register holder = ecx;
311  Register api_function_address = edx;
312  Register return_address = edi;
313 
314  typedef FunctionCallbackArguments FCA;
315 
316  STATIC_ASSERT(FCA::kArgsLength == 6);
317  STATIC_ASSERT(FCA::kNewTargetIndex == 5);
318  STATIC_ASSERT(FCA::kDataIndex == 4);
319  STATIC_ASSERT(FCA::kReturnValueOffset == 3);
320  STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
321  STATIC_ASSERT(FCA::kIsolateIndex == 1);
322  STATIC_ASSERT(FCA::kHolderIndex == 0);
323 
324  __ pop(return_address);
325 
326  // new target
327  __ PushRoot(RootIndex::kUndefinedValue);
328 
329  // call data
330  __ push(call_data);
331 
332  // return value
333  __ PushRoot(RootIndex::kUndefinedValue);
334  // return value default
335  __ PushRoot(RootIndex::kUndefinedValue);
336  // isolate
337  __ Push(Immediate(ExternalReference::isolate_address(isolate())));
338  // holder
339  __ push(holder);
340 
341  Register scratch = call_data;
342 
343  __ mov(scratch, esp);
344 
345  // push return address
346  __ push(return_address);
347 
348  // API function gets reference to the v8::Arguments. If CPU profiler
349  // is enabled wrapper function will be called and we need to pass
350  // address of the callback as additional parameter, always allocate
351  // space for it.
352  const int kApiArgc = 1 + 1;
353 
354  // Allocate the v8::Arguments structure in the arguments' space since
355  // it's not controlled by GC.
356  const int kApiStackSpace = 3;
357 
358  PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace, edi);
359 
360  // FunctionCallbackInfo::implicit_args_.
361  __ mov(ApiParameterOperand(2), scratch);
362  __ add(scratch, Immediate((argc() + FCA::kArgsLength - 1) * kPointerSize));
363  // FunctionCallbackInfo::values_.
364  __ mov(ApiParameterOperand(3), scratch);
365  // FunctionCallbackInfo::length_.
366  __ Move(ApiParameterOperand(4), Immediate(argc()));
367 
368  // v8::InvocationCallback's argument.
369  __ lea(scratch, ApiParameterOperand(2));
370  __ mov(ApiParameterOperand(0), scratch);
371 
372  ExternalReference thunk_ref = ExternalReference::invoke_function_callback();
373 
374  // Stores return the first js argument
375  int return_value_offset = 2 + FCA::kReturnValueOffset;
376  Operand return_value_operand(ebp, return_value_offset * kPointerSize);
377  const int stack_space = argc() + FCA::kArgsLength + 1;
378  Operand* stack_space_operand = nullptr;
379  CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
380  ApiParameterOperand(1), stack_space,
381  stack_space_operand, return_value_operand);
382 }
383 
384 
385 void CallApiGetterStub::Generate(MacroAssembler* masm) {
386  // Build v8::PropertyCallbackInfo::args_ array on the stack and push property
387  // name below the exit frame to make GC aware of them.
388  STATIC_ASSERT(PropertyCallbackArguments::kShouldThrowOnErrorIndex == 0);
389  STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 1);
390  STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 2);
391  STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 3);
392  STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 4);
393  STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 5);
394  STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 6);
395  STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 7);
396 
397  Register receiver = ApiGetterDescriptor::ReceiverRegister();
398  Register holder = ApiGetterDescriptor::HolderRegister();
399  Register callback = ApiGetterDescriptor::CallbackRegister();
400  Register scratch = edi;
401  DCHECK(!AreAliased(receiver, holder, callback, scratch));
402 
403  __ pop(scratch); // Pop return address to extend the frame.
404  __ push(receiver);
405  __ push(FieldOperand(callback, AccessorInfo::kDataOffset));
406  __ PushRoot(RootIndex::kUndefinedValue); // ReturnValue
407  // ReturnValue default value
408  __ PushRoot(RootIndex::kUndefinedValue);
409  __ Push(Immediate(ExternalReference::isolate_address(isolate())));
410  __ push(holder);
411  __ push(Immediate(Smi::zero())); // should_throw_on_error -> false
412  __ push(FieldOperand(callback, AccessorInfo::kNameOffset));
413  __ push(scratch); // Restore return address.
414 
415  // v8::PropertyCallbackInfo::args_ array and name handle.
416  const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
417 
418  // Allocate v8::PropertyCallbackInfo object, arguments for callback and
419  // space for optional callback address parameter (in case CPU profiler is
420  // active) in non-GCed stack space.
421  const int kApiArgc = 3 + 1;
422 
423  PrepareCallApiFunction(masm, kApiArgc, scratch);
424 
425  // Load address of v8::PropertyAccessorInfo::args_ array. The value in ebp
426  // here corresponds to esp + kPointersize before PrepareCallApiFunction.
427  __ lea(scratch, Operand(ebp, kPointerSize + 2 * kPointerSize));
428  // Create v8::PropertyCallbackInfo object on the stack and initialize
429  // it's args_ field.
430  Operand info_object = ApiParameterOperand(3);
431  __ mov(info_object, scratch);
432 
433  // Name as handle.
434  __ sub(scratch, Immediate(kPointerSize));
435  __ mov(ApiParameterOperand(0), scratch);
436  // Arguments pointer.
437  __ lea(scratch, info_object);
438  __ mov(ApiParameterOperand(1), scratch);
439  // Reserve space for optional callback address parameter.
440  Operand thunk_last_arg = ApiParameterOperand(2);
441 
442  ExternalReference thunk_ref =
443  ExternalReference::invoke_accessor_getter_callback();
444 
445  __ mov(scratch, FieldOperand(callback, AccessorInfo::kJsGetterOffset));
446  Register function_address = edx;
447  __ mov(function_address,
448  FieldOperand(scratch, Foreign::kForeignAddressOffset));
449  // +3 is to skip prolog, return address and name handle.
450  Operand return_value_operand(
451  ebp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
452  CallApiFunctionAndReturn(masm, function_address, thunk_ref, thunk_last_arg,
453  kStackUnwindSpace, nullptr, return_value_operand);
454 }
455 
456 #undef __
457 
458 } // namespace internal
459 } // namespace v8
460 
461 #endif // V8_TARGET_ARCH_IA32
Definition: libplatform.h:13