V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-stubs-arm.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_ARM
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/counters.h"
13 #include "src/double.h"
14 #include "src/frame-constants.h"
15 #include "src/frames.h"
16 #include "src/ic/ic.h"
17 #include "src/ic/stub-cache.h"
18 #include "src/isolate.h"
19 #include "src/macro-assembler.h"
20 #include "src/objects/api-callbacks.h"
21 #include "src/objects/regexp-match-info.h"
22 #include "src/regexp/jsregexp.h"
23 #include "src/regexp/regexp-macro-assembler.h"
24 #include "src/runtime/runtime.h"
25 
26 #include "src/arm/code-stubs-arm.h" // Cannot be the first include.
27 
28 namespace v8 {
29 namespace internal {
30 
31 #define __ ACCESS_MASM(masm)
32 
33 void JSEntryStub::Generate(MacroAssembler* masm) {
34  // r0: code entry
35  // r1: function
36  // r2: receiver
37  // r3: argc
38  // [sp+0]: argv
39 
40  Label invoke, handler_entry, exit;
41 
42  {
43  NoRootArrayScope no_root_array(masm);
44 
45  // Called from C, so do not pop argc and args on exit (preserve sp)
46  // No need to save register-passed args
47  // Save callee-saved registers (incl. cp and fp), sp, and lr
48  __ stm(db_w, sp, kCalleeSaved | lr.bit());
49 
50  // Save callee-saved vfp registers.
51  __ vstm(db_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
52  // Set up the reserved register for 0.0.
53  __ vmov(kDoubleRegZero, Double(0.0));
54 
55  __ InitializeRootRegister();
56  }
57 
58  // Get address of argv, see stm above.
59  // r0: code entry
60  // r1: function
61  // r2: receiver
62  // r3: argc
63 
64  // Set up argv in r4.
65  int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
66  offset_to_argv += kNumDoubleCalleeSaved * kDoubleSize;
67  __ ldr(r4, MemOperand(sp, offset_to_argv));
68 
69  // Push a frame with special values setup to mark it as an entry frame.
70  // r0: code entry
71  // r1: function
72  // r2: receiver
73  // r3: argc
74  // r4: argv
75  StackFrame::Type marker = type();
76  __ mov(r7, Operand(StackFrame::TypeToMarker(marker)));
77  __ mov(r6, Operand(StackFrame::TypeToMarker(marker)));
78  __ mov(r5, Operand(ExternalReference::Create(
79  IsolateAddressId::kCEntryFPAddress, isolate())));
80  __ ldr(r5, MemOperand(r5));
81  {
82  UseScratchRegisterScope temps(masm);
83  Register scratch = temps.Acquire();
84 
85  // Push a bad frame pointer to fail if it is used.
86  __ mov(scratch, Operand(-1));
87  __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() | scratch.bit());
88  }
89 
90  Register scratch = r6;
91 
92  // Set up frame pointer for the frame to be pushed.
93  __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
94 
95  // If this is the outermost JS call, set js_entry_sp value.
96  Label non_outermost_js;
97  ExternalReference js_entry_sp =
98  ExternalReference::Create(IsolateAddressId::kJSEntrySPAddress, isolate());
99  __ mov(r5, Operand(ExternalReference(js_entry_sp)));
100  __ ldr(scratch, MemOperand(r5));
101  __ cmp(scratch, Operand::Zero());
102  __ b(ne, &non_outermost_js);
103  __ str(fp, MemOperand(r5));
104  __ mov(scratch, Operand(StackFrame::OUTERMOST_JSENTRY_FRAME));
105  Label cont;
106  __ b(&cont);
107  __ bind(&non_outermost_js);
108  __ mov(scratch, Operand(StackFrame::INNER_JSENTRY_FRAME));
109  __ bind(&cont);
110  __ push(scratch);
111 
112  // Jump to a faked try block that does the invoke, with a faked catch
113  // block that sets the pending exception.
114  __ jmp(&invoke);
115 
116  // Block literal pool emission whilst taking the position of the handler
117  // entry. This avoids making the assumption that literal pools are always
118  // emitted after an instruction is emitted, rather than before.
119  {
120  Assembler::BlockConstPoolScope block_const_pool(masm);
121  __ bind(&handler_entry);
122  handler_offset_ = handler_entry.pos();
123  // Caught exception: Store result (exception) in the pending exception
124  // field in the JSEnv and return a failure sentinel. Coming in here the
125  // fp will be invalid because the PushStackHandler below sets it to 0 to
126  // signal the existence of the JSEntry frame.
127  __ mov(scratch,
128  Operand(ExternalReference::Create(
129  IsolateAddressId::kPendingExceptionAddress, isolate())));
130  }
131  __ str(r0, MemOperand(scratch));
132  __ LoadRoot(r0, RootIndex::kException);
133  __ b(&exit);
134 
135  // Invoke: Link this frame into the handler chain.
136  __ bind(&invoke);
137  // Must preserve r0-r4, r5-r6 are available.
138  __ PushStackHandler();
139  // If an exception not caught by another handler occurs, this handler
140  // returns control to the code after the bl(&invoke) above, which
141  // restores all kCalleeSaved registers (including cp and fp) to their
142  // saved values before returning a failure to C.
143 
144  // Invoke the function by calling through JS entry trampoline builtin.
145  // Notice that we cannot store a reference to the trampoline code directly in
146  // this stub, because runtime stubs are not traversed when doing GC.
147 
148  // Expected registers by Builtins::JSEntryTrampoline
149  // r0: code entry
150  // r1: function
151  // r2: receiver
152  // r3: argc
153  // r4: argv
154  __ Call(EntryTrampoline(), RelocInfo::CODE_TARGET);
155 
156  // Unlink this frame from the handler chain.
157  __ PopStackHandler();
158 
159  __ bind(&exit); // r0 holds result
160  // Check if the current stack frame is marked as the outermost JS frame.
161  Label non_outermost_js_2;
162  __ pop(r5);
163  __ cmp(r5, Operand(StackFrame::OUTERMOST_JSENTRY_FRAME));
164  __ b(ne, &non_outermost_js_2);
165  __ mov(r6, Operand::Zero());
166  __ mov(r5, Operand(ExternalReference(js_entry_sp)));
167  __ str(r6, MemOperand(r5));
168  __ bind(&non_outermost_js_2);
169 
170  // Restore the top frame descriptors from the stack.
171  __ pop(r3);
172  __ mov(scratch, Operand(ExternalReference::Create(
173  IsolateAddressId::kCEntryFPAddress, isolate())));
174  __ str(r3, MemOperand(scratch));
175 
176  // Reset the stack to the callee saved registers.
177  __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
178 
179  // Restore callee-saved registers and return.
180 #ifdef DEBUG
181  if (FLAG_debug_code) {
182  __ mov(lr, Operand(pc));
183  }
184 #endif
185 
186  // Restore callee-saved vfp registers.
187  __ vldm(ia_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
188 
189  __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
190 }
191 
192 void DirectCEntryStub::Generate(MacroAssembler* masm) {
193  // Place the return address on the stack, making the call
194  // GC safe. The RegExp backend also relies on this.
195  __ str(lr, MemOperand(sp, 0));
196  __ blx(ip); // Call the C++ function.
197  __ ldr(pc, MemOperand(sp, 0));
198 }
199 
200 
201 void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
202  Register target) {
203  if (FLAG_embedded_builtins) {
204  if (masm->root_array_available() &&
205  isolate()->ShouldLoadConstantsFromRootList()) {
206  // This is basically an inlined version of Call(Handle<Code>) that loads
207  // the code object into lr instead of ip.
208  __ Move(ip, target);
209  __ IndirectLoadConstant(lr, GetCode());
210  __ add(lr, lr, Operand(Code::kHeaderSize - kHeapObjectTag));
211  __ blx(lr);
212  return;
213  }
214  }
215  intptr_t code =
216  reinterpret_cast<intptr_t>(GetCode().location());
217  __ Move(ip, target);
218  __ mov(lr, Operand(code, RelocInfo::CODE_TARGET));
219  __ blx(lr); // Call the stub.
220 }
221 
222 
223 static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
224  return ref0.address() - ref1.address();
225 }
226 
227 
228 // Calls an API function. Allocates HandleScope, extracts returned value
229 // from handle and propagates exceptions. Restores context. stack_space
230 // - space to be unwound on exit (includes the call JS arguments space and
231 // the additional space allocated for the fast call).
232 static void CallApiFunctionAndReturn(MacroAssembler* masm,
233  Register function_address,
234  ExternalReference thunk_ref,
235  int stack_space,
236  MemOperand* stack_space_operand,
237  MemOperand return_value_operand) {
238  Isolate* isolate = masm->isolate();
239  ExternalReference next_address =
240  ExternalReference::handle_scope_next_address(isolate);
241  const int kNextOffset = 0;
242  const int kLimitOffset = AddressOffset(
243  ExternalReference::handle_scope_limit_address(isolate), next_address);
244  const int kLevelOffset = AddressOffset(
245  ExternalReference::handle_scope_level_address(isolate), next_address);
246 
247  DCHECK(function_address == r1 || function_address == r2);
248 
249  Label profiler_disabled;
250  Label end_profiler_check;
251  __ Move(r9, ExternalReference::is_profiling_address(isolate));
252  __ ldrb(r9, MemOperand(r9, 0));
253  __ cmp(r9, Operand(0));
254  __ b(eq, &profiler_disabled);
255 
256  // Additional parameter is the address of the actual callback.
257  __ Move(r3, thunk_ref);
258  __ jmp(&end_profiler_check);
259 
260  __ bind(&profiler_disabled);
261  __ Move(r3, function_address);
262  __ bind(&end_profiler_check);
263 
264  // Allocate HandleScope in callee-save registers.
265  __ Move(r9, next_address);
266  __ ldr(r4, MemOperand(r9, kNextOffset));
267  __ ldr(r5, MemOperand(r9, kLimitOffset));
268  __ ldr(r6, MemOperand(r9, kLevelOffset));
269  __ add(r6, r6, Operand(1));
270  __ str(r6, MemOperand(r9, kLevelOffset));
271 
272  if (FLAG_log_timer_events) {
273  FrameScope frame(masm, StackFrame::MANUAL);
274  __ PushSafepointRegisters();
275  __ PrepareCallCFunction(1);
276  __ Move(r0, ExternalReference::isolate_address(isolate));
277  __ CallCFunction(ExternalReference::log_enter_external_function(), 1);
278  __ PopSafepointRegisters();
279  }
280 
281  // Native call returns to the DirectCEntry stub which redirects to the
282  // return address pushed on stack (could have moved after GC).
283  // DirectCEntry stub itself is generated early and never moves.
284  DirectCEntryStub stub(isolate);
285  stub.GenerateCall(masm, r3);
286 
287  if (FLAG_log_timer_events) {
288  FrameScope frame(masm, StackFrame::MANUAL);
289  __ PushSafepointRegisters();
290  __ PrepareCallCFunction(1);
291  __ Move(r0, ExternalReference::isolate_address(isolate));
292  __ CallCFunction(ExternalReference::log_leave_external_function(), 1);
293  __ PopSafepointRegisters();
294  }
295 
296  Label promote_scheduled_exception;
297  Label delete_allocated_handles;
298  Label leave_exit_frame;
299  Label return_value_loaded;
300 
301  // load value from ReturnValue
302  __ ldr(r0, return_value_operand);
303  __ bind(&return_value_loaded);
304  // No more valid handles (the result handle was the last one). Restore
305  // previous handle scope.
306  __ str(r4, MemOperand(r9, kNextOffset));
307  if (__ emit_debug_code()) {
308  __ ldr(r1, MemOperand(r9, kLevelOffset));
309  __ cmp(r1, r6);
310  __ Check(eq, AbortReason::kUnexpectedLevelAfterReturnFromApiCall);
311  }
312  __ sub(r6, r6, Operand(1));
313  __ str(r6, MemOperand(r9, kLevelOffset));
314  __ ldr(r6, MemOperand(r9, kLimitOffset));
315  __ cmp(r5, r6);
316  __ b(ne, &delete_allocated_handles);
317 
318  // Leave the API exit frame.
319  __ bind(&leave_exit_frame);
320  // LeaveExitFrame expects unwind space to be in a register.
321  if (stack_space_operand != nullptr) {
322  __ ldr(r4, *stack_space_operand);
323  } else {
324  __ mov(r4, Operand(stack_space));
325  }
326  __ LeaveExitFrame(false, r4, stack_space_operand != nullptr);
327 
328  // Check if the function scheduled an exception.
329  __ LoadRoot(r4, RootIndex::kTheHoleValue);
330  __ Move(r6, ExternalReference::scheduled_exception_address(isolate));
331  __ ldr(r5, MemOperand(r6));
332  __ cmp(r4, r5);
333  __ b(ne, &promote_scheduled_exception);
334 
335  __ mov(pc, lr);
336 
337  // Re-throw by promoting a scheduled exception.
338  __ bind(&promote_scheduled_exception);
339  __ TailCallRuntime(Runtime::kPromoteScheduledException);
340 
341  // HandleScope limit has changed. Delete allocated extensions.
342  __ bind(&delete_allocated_handles);
343  __ str(r5, MemOperand(r9, kLimitOffset));
344  __ mov(r4, r0);
345  __ PrepareCallCFunction(1);
346  __ Move(r0, ExternalReference::isolate_address(isolate));
347  __ CallCFunction(ExternalReference::delete_handle_scope_extensions(), 1);
348  __ mov(r0, r4);
349  __ jmp(&leave_exit_frame);
350 }
351 
352 void CallApiCallbackStub::Generate(MacroAssembler* masm) {
353  // ----------- S t a t e -------------
354  // -- r4 : call_data
355  // -- r2 : holder
356  // -- r1 : api_function_address
357  // -- cp : context
358  // --
359  // -- sp[0] : last argument
360  // -- ...
361  // -- sp[(argc - 1) * 4] : first argument
362  // -- sp[argc * 4] : receiver
363  // -----------------------------------
364 
365  Register call_data = r4;
366  Register holder = r2;
367  Register api_function_address = r1;
368 
369  typedef FunctionCallbackArguments FCA;
370 
371  STATIC_ASSERT(FCA::kArgsLength == 6);
372  STATIC_ASSERT(FCA::kNewTargetIndex == 5);
373  STATIC_ASSERT(FCA::kDataIndex == 4);
374  STATIC_ASSERT(FCA::kReturnValueOffset == 3);
375  STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
376  STATIC_ASSERT(FCA::kIsolateIndex == 1);
377  STATIC_ASSERT(FCA::kHolderIndex == 0);
378 
379  // new target
380  __ PushRoot(RootIndex::kUndefinedValue);
381 
382  // call data
383  __ push(call_data);
384 
385  Register scratch0 = call_data;
386  Register scratch1 = r5;
387  __ LoadRoot(scratch0, RootIndex::kUndefinedValue);
388  // return value
389  __ push(scratch0);
390  // return value default
391  __ push(scratch0);
392  // isolate
393  __ Move(scratch1, ExternalReference::isolate_address(masm->isolate()));
394  __ push(scratch1);
395  // holder
396  __ push(holder);
397 
398  // Prepare arguments.
399  __ mov(scratch0, sp);
400 
401  // Allocate the v8::Arguments structure in the arguments' space since
402  // it's not controlled by GC.
403  const int kApiStackSpace = 3;
404 
405  FrameScope frame_scope(masm, StackFrame::MANUAL);
406  __ EnterExitFrame(false, kApiStackSpace);
407 
408  DCHECK(api_function_address != r0 && scratch0 != r0);
409  // r0 = FunctionCallbackInfo&
410  // Arguments is after the return address.
411  __ add(r0, sp, Operand(1 * kPointerSize));
412  // FunctionCallbackInfo::implicit_args_
413  __ str(scratch0, MemOperand(r0, 0 * kPointerSize));
414  // FunctionCallbackInfo::values_
415  __ add(scratch1, scratch0,
416  Operand((FCA::kArgsLength - 1 + argc()) * kPointerSize));
417  __ str(scratch1, MemOperand(r0, 1 * kPointerSize));
418  // FunctionCallbackInfo::length_ = argc
419  __ mov(scratch0, Operand(argc()));
420  __ str(scratch0, MemOperand(r0, 2 * kPointerSize));
421 
422  ExternalReference thunk_ref = ExternalReference::invoke_function_callback();
423 
424  AllowExternalCallThatCantCauseGC scope(masm);
425  // Stores return the first js argument
426  int return_value_offset = 2 + FCA::kReturnValueOffset;
427  MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
428  const int stack_space = argc() + FCA::kArgsLength + 1;
429  MemOperand* stack_space_operand = nullptr;
430 
431  CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, stack_space,
432  stack_space_operand, return_value_operand);
433 }
434 
435 
436 void CallApiGetterStub::Generate(MacroAssembler* masm) {
437  // Build v8::PropertyCallbackInfo::args_ array on the stack and push property
438  // name below the exit frame to make GC aware of them.
439  STATIC_ASSERT(PropertyCallbackArguments::kShouldThrowOnErrorIndex == 0);
440  STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 1);
441  STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 2);
442  STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 3);
443  STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 4);
444  STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 5);
445  STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 6);
446  STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 7);
447 
448  Register receiver = ApiGetterDescriptor::ReceiverRegister();
449  Register holder = ApiGetterDescriptor::HolderRegister();
450  Register callback = ApiGetterDescriptor::CallbackRegister();
451  Register scratch = r4;
452  DCHECK(!AreAliased(receiver, holder, callback, scratch));
453 
454  Register api_function_address = r2;
455 
456  __ push(receiver);
457  // Push data from AccessorInfo.
458  __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kDataOffset));
459  __ push(scratch);
460  __ LoadRoot(scratch, RootIndex::kUndefinedValue);
461  __ Push(scratch, scratch);
462  __ Move(scratch, ExternalReference::isolate_address(isolate()));
463  __ Push(scratch, holder);
464  __ Push(Smi::zero()); // should_throw_on_error -> false
465  __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kNameOffset));
466  __ push(scratch);
467  // v8::PropertyCallbackInfo::args_ array and name handle.
468  const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
469 
470  // Load address of v8::PropertyAccessorInfo::args_ array and name handle.
471  __ mov(r0, sp); // r0 = Handle<Name>
472  __ add(r1, r0, Operand(1 * kPointerSize)); // r1 = v8::PCI::args_
473 
474  const int kApiStackSpace = 1;
475  FrameScope frame_scope(masm, StackFrame::MANUAL);
476  __ EnterExitFrame(false, kApiStackSpace);
477 
478  // Create v8::PropertyCallbackInfo object on the stack and initialize
479  // it's args_ field.
480  __ str(r1, MemOperand(sp, 1 * kPointerSize));
481  __ add(r1, sp, Operand(1 * kPointerSize)); // r1 = v8::PropertyCallbackInfo&
482 
483  ExternalReference thunk_ref =
484  ExternalReference::invoke_accessor_getter_callback();
485 
486  __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kJsGetterOffset));
487  __ ldr(api_function_address,
488  FieldMemOperand(scratch, Foreign::kForeignAddressOffset));
489 
490  // +3 is to skip prolog, return address and name handle.
491  MemOperand return_value_operand(
492  fp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
493  CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
494  kStackUnwindSpace, nullptr, return_value_operand);
495 }
496 
497 #undef __
498 
499 } // namespace internal
500 } // namespace v8
501 
502 #endif // V8_TARGET_ARCH_ARM
Definition: libplatform.h:13