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