summaryrefslogtreecommitdiff
path: root/deps/v8/src/arm/virtual-frame-arm.h
blob: b6e794a5c06cb8b5f3623ca1ffddd991d1ac5199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright 2009 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_ARM_VIRTUAL_FRAME_ARM_H_
#define V8_ARM_VIRTUAL_FRAME_ARM_H_

#include "register-allocator.h"

namespace v8 {
namespace internal {

// This dummy class is only used to create invalid virtual frames.
extern class InvalidVirtualFrameInitializer {}* kInvalidVirtualFrameInitializer;


// -------------------------------------------------------------------------
// Virtual frames
//
// The virtual frame is an abstraction of the physical stack frame.  It
// encapsulates the parameters, frame-allocated locals, and the expression
// stack.  It supports push/pop operations on the expression stack, as well
// as random access to the expression stack elements, locals, and
// parameters.

class VirtualFrame : public ZoneObject {
 public:
  class RegisterAllocationScope;
  // A utility class to introduce a scope where the virtual frame is
  // expected to remain spilled.  The constructor spills the code
  // generator's current frame, and keeps it spilled.
  class SpilledScope BASE_EMBEDDED {
   public:
    explicit SpilledScope(VirtualFrame* frame)
      : old_is_spilled_(is_spilled_) {
      if (frame != NULL) {
        if (!is_spilled_) {
          frame->SpillAll();
        } else {
          frame->AssertIsSpilled();
        }
      }
      is_spilled_ = true;
    }
    ~SpilledScope() {
      is_spilled_ = old_is_spilled_;
    }
    static bool is_spilled() { return is_spilled_; }

   private:
    static bool is_spilled_;
    int old_is_spilled_;

    SpilledScope() { }

    friend class RegisterAllocationScope;
  };

  class RegisterAllocationScope BASE_EMBEDDED {
   public:
    // A utility class to introduce a scope where the virtual frame
    // is not spilled, ie. where register allocation occurs.  Eventually
    // when RegisterAllocationScope is ubiquitous it can be removed
    // along with the (by then unused) SpilledScope class.
    inline explicit RegisterAllocationScope(CodeGenerator* cgen);
    inline ~RegisterAllocationScope();

   private:
    CodeGenerator* cgen_;
    bool old_is_spilled_;

    RegisterAllocationScope() { }
  };

  // An illegal index into the virtual frame.
  static const int kIllegalIndex = -1;

  // Construct an initial virtual frame on entry to a JS function.
  inline VirtualFrame();

  // Construct an invalid virtual frame, used by JumpTargets.
  inline VirtualFrame(InvalidVirtualFrameInitializer* dummy);

  // Construct a virtual frame as a clone of an existing one.
  explicit inline VirtualFrame(VirtualFrame* original);

  inline CodeGenerator* cgen() const;
  inline MacroAssembler* masm();

  // The number of elements on the virtual frame.
  int element_count() const { return element_count_; }

  // The height of the virtual expression stack.
  inline int height() const;

  bool is_used(int num) {
    switch (num) {
      case 0: {  // r0.
        return kR0InUse[top_of_stack_state_];
      }
      case 1: {  // r1.
        return kR1InUse[top_of_stack_state_];
      }
      case 2:
      case 3:
      case 4:
      case 5:
      case 6: {  // r2 to r6.
        ASSERT(num - kFirstAllocatedRegister < kNumberOfAllocatedRegisters);
        ASSERT(num >= kFirstAllocatedRegister);
        if ((register_allocation_map_ &
             (1 << (num - kFirstAllocatedRegister))) == 0) {
          return false;
        } else {
          return true;
        }
      }
      default: {
        ASSERT(num < kFirstAllocatedRegister ||
               num >= kFirstAllocatedRegister + kNumberOfAllocatedRegisters);
        return false;
      }
    }
  }

  // Add extra in-memory elements to the top of the frame to match an actual
  // frame (eg, the frame after an exception handler is pushed).  No code is
  // emitted.
  void Adjust(int count);

  // Forget elements from the top of the frame to match an actual frame (eg,
  // the frame after a runtime call).  No code is emitted except to bring the
  // frame to a spilled state.
  void Forget(int count);

  // Spill all values from the frame to memory.
  void SpillAll();

  void AssertIsSpilled() const {
    ASSERT(top_of_stack_state_ == NO_TOS_REGISTERS);
    ASSERT(register_allocation_map_ == 0);
  }

  void AssertIsNotSpilled() {
    ASSERT(!SpilledScope::is_spilled());
  }

  // Spill all occurrences of a specific register from the frame.
  void Spill(Register reg) {
    UNIMPLEMENTED();
  }

  // Spill all occurrences of an arbitrary register if possible.  Return the
  // register spilled or no_reg if it was not possible to free any register
  // (ie, they all have frame-external references).  Unimplemented.
  Register SpillAnyRegister();

  // Make this virtual frame have a state identical to an expected virtual
  // frame.  As a side effect, code may be emitted to make this frame match
  // the expected one.
  void MergeTo(VirtualFrame* expected, Condition cond = al);
  void MergeTo(const VirtualFrame* expected, Condition cond = al);

  // Checks whether this frame can be branched to by the other frame.
  bool IsCompatibleWith(const VirtualFrame* other) const {
    return (tos_known_smi_map_ & (~other->tos_known_smi_map_)) == 0;
  }

  inline void ForgetTypeInfo() {
    tos_known_smi_map_ = 0;
  }

  // Detach a frame from its code generator, perhaps temporarily.  This
  // tells the register allocator that it is free to use frame-internal
  // registers.  Used when the code generator's frame is switched from this
  // one to NULL by an unconditional jump.
  void DetachFromCodeGenerator() {
  }

  // (Re)attach a frame to its code generator.  This informs the register
  // allocator that the frame-internal register references are active again.
  // Used when a code generator's frame is switched from NULL to this one by
  // binding a label.
  void AttachToCodeGenerator() {
  }

  // Emit code for the physical JS entry and exit frame sequences.  After
  // calling Enter, the virtual frame is ready for use; and after calling
  // Exit it should not be used.  Note that Enter does not allocate space in
  // the physical frame for storing frame-allocated locals.
  void Enter();
  void Exit();

  // Prepare for returning from the frame by elements in the virtual frame. This
  // avoids generating unnecessary merge code when jumping to the
  // shared return site. No spill code emitted. Value to return should be in r0.
  inline void PrepareForReturn();

  // Number of local variables after when we use a loop for allocating.
  static const int kLocalVarBound = 5;

  // Allocate and initialize the frame-allocated locals.
  void AllocateStackSlots();

  // The current top of the expression stack as an assembly operand.
  MemOperand Top() {
    AssertIsSpilled();
    return MemOperand(sp, 0);
  }

  // An element of the expression stack as an assembly operand.
  MemOperand ElementAt(int index) {
    int adjusted_index = index - kVirtualElements[top_of_stack_state_];
    ASSERT(adjusted_index >= 0);
    return MemOperand(sp, adjusted_index * kPointerSize);
  }

  bool KnownSmiAt(int index) {
    if (index >= kTOSKnownSmiMapSize) return false;
    return (tos_known_smi_map_ & (1 << index)) != 0;
  }

  // A frame-allocated local as an assembly operand.
  inline MemOperand LocalAt(int index);

  // Push the address of the receiver slot on the frame.
  void PushReceiverSlotAddress();

  // The function frame slot.
  MemOperand Function() { return MemOperand(fp, kFunctionOffset); }

  // The context frame slot.
  MemOperand Context() { return MemOperand(fp, kContextOffset); }

  // A parameter as an assembly operand.
  inline MemOperand ParameterAt(int index);

  // The receiver frame slot.
  inline MemOperand Receiver();

  // Push a try-catch or try-finally handler on top of the virtual frame.
  void PushTryHandler(HandlerType type);

  // Call stub given the number of arguments it expects on (and
  // removes from) the stack.
  inline void CallStub(CodeStub* stub, int arg_count);

  // Call JS function from top of the stack with arguments
  // taken from the stack.
  void CallJSFunction(int arg_count);

  // Call runtime given the number of arguments expected on (and
  // removed from) the stack.
  void CallRuntime(Runtime::Function* f, int arg_count);
  void CallRuntime(Runtime::FunctionId id, int arg_count);

#ifdef ENABLE_DEBUGGER_SUPPORT
  void DebugBreak();
#endif

  // Invoke builtin given the number of arguments it expects on (and
  // removes from) the stack.
  void InvokeBuiltin(Builtins::JavaScript id,
                     InvokeJSFlags flag,
                     int arg_count);

  // Call load IC. Receiver is on the stack and is consumed. Result is returned
  // in r0.
  void CallLoadIC(Handle<String> name, RelocInfo::Mode mode);

  // Call store IC. If the load is contextual, value is found on top of the
  // frame. If not, value and receiver are on the frame. Both are consumed.
  // Result is returned in r0.
  void CallStoreIC(Handle<String> name, bool is_contextual,
                   StrictModeFlag strict_mode);

  // Call keyed load IC. Key and receiver are on the stack. Both are consumed.
  // Result is returned in r0.
  void CallKeyedLoadIC();

  // Call keyed store IC. Value, key and receiver are on the stack. All three
  // are consumed. Result is returned in r0.
  void CallKeyedStoreIC();

  // Call into an IC stub given the number of arguments it removes
  // from the stack.  Register arguments to the IC stub are implicit,
  // and depend on the type of IC stub.
  void CallCodeObject(Handle<Code> ic,
                      RelocInfo::Mode rmode,
                      int dropped_args);

  // Drop a number of elements from the top of the expression stack.  May
  // emit code to affect the physical frame.  Does not clobber any registers
  // excepting possibly the stack pointer.
  void Drop(int count);

  // Drop one element.
  void Drop() { Drop(1); }

  // Pop an element from the top of the expression stack.  Discards
  // the result.
  void Pop();

  // Pop an element from the top of the expression stack.  The register
  // will be one normally used for the top of stack register allocation
  // so you can't hold on to it if you push on the stack.
  Register PopToRegister(Register but_not_to_this_one = no_reg);

  // Look at the top of the stack.  The register returned is aliased and
  // must be copied to a scratch register before modification.
  Register Peek();

  // Look at the value beneath the top of the stack.  The register returned is
  // aliased and must be copied to a scratch register before modification.
  Register Peek2();

  // Duplicate the top of stack.
  void Dup();

  // Duplicate the two elements on top of stack.
  void Dup2();

  // Flushes all registers, but it puts a copy of the top-of-stack in r0.
  void SpillAllButCopyTOSToR0();

  // Flushes all registers, but it puts a copy of the top-of-stack in r1.
  void SpillAllButCopyTOSToR1();

  // Flushes all registers, but it puts a copy of the top-of-stack in r1
  // and the next value on the stack in r0.
  void SpillAllButCopyTOSToR1R0();

  // Pop and save an element from the top of the expression stack and
  // emit a corresponding pop instruction.
  void EmitPop(Register reg);

  // Takes the top two elements and puts them in r0 (top element) and r1
  // (second element).
  void PopToR1R0();

  // Takes the top element and puts it in r1.
  void PopToR1();

  // Takes the top element and puts it in r0.
  void PopToR0();

  // Push an element on top of the expression stack and emit a
  // corresponding push instruction.
  void EmitPush(Register reg, TypeInfo type_info = TypeInfo::Unknown());
  void EmitPush(Operand operand, TypeInfo type_info = TypeInfo::Unknown());
  void EmitPush(MemOperand operand, TypeInfo type_info = TypeInfo::Unknown());
  void EmitPushRoot(Heap::RootListIndex index);

  // Overwrite the nth thing on the stack.  If the nth position is in a
  // register then this turns into a mov, otherwise an str.  Afterwards
  // you can still use the register even if it is a register that can be
  // used for TOS (r0 or r1).
  void SetElementAt(Register reg, int this_far_down);

  // Get a register which is free and which must be immediately used to
  // push on the top of the stack.
  Register GetTOSRegister();

  // Push multiple registers on the stack and the virtual frame
  // Register are selected by setting bit in src_regs and
  // are pushed in decreasing order: r15 .. r0.
  void EmitPushMultiple(int count, int src_regs);

  static Register scratch0() { return r7; }
  static Register scratch1() { return r9; }

 private:
  static const int kLocal0Offset = JavaScriptFrameConstants::kLocal0Offset;
  static const int kFunctionOffset = JavaScriptFrameConstants::kFunctionOffset;
  static const int kContextOffset = StandardFrameConstants::kContextOffset;

  static const int kHandlerSize = StackHandlerConstants::kSize / kPointerSize;
  static const int kPreallocatedElements = 5 + 8;  // 8 expression stack slots.

  // 5 states for the top of stack, which can be in memory or in r0 and r1.
  enum TopOfStack {
    NO_TOS_REGISTERS,
    R0_TOS,
    R1_TOS,
    R1_R0_TOS,
    R0_R1_TOS,
    TOS_STATES
  };

  static const int kMaxTOSRegisters = 2;

  static const bool kR0InUse[TOS_STATES];
  static const bool kR1InUse[TOS_STATES];
  static const int kVirtualElements[TOS_STATES];
  static const TopOfStack kStateAfterPop[TOS_STATES];
  static const TopOfStack kStateAfterPush[TOS_STATES];
  static const Register kTopRegister[TOS_STATES];
  static const Register kBottomRegister[TOS_STATES];

  // We allocate up to 5 locals in registers.
  static const int kNumberOfAllocatedRegisters = 5;
  // r2 to r6 are allocated to locals.
  static const int kFirstAllocatedRegister = 2;

  static const Register kAllocatedRegisters[kNumberOfAllocatedRegisters];

  static Register AllocatedRegister(int r) {
    ASSERT(r >= 0 && r < kNumberOfAllocatedRegisters);
    return kAllocatedRegisters[r];
  }

  // The number of elements on the stack frame.
  int element_count_;
  TopOfStack top_of_stack_state_:3;
  int register_allocation_map_:kNumberOfAllocatedRegisters;
  static const int kTOSKnownSmiMapSize = 4;
  unsigned tos_known_smi_map_:kTOSKnownSmiMapSize;

  // The index of the element that is at the processor's stack pointer
  // (the sp register).  For now since everything is in memory it is given
  // by the number of elements on the not-very-virtual stack frame.
  int stack_pointer() { return element_count_ - 1; }

  // The number of frame-allocated locals and parameters respectively.
  inline int parameter_count() const;
  inline int local_count() const;

  // The index of the element that is at the processor's frame pointer
  // (the fp register).  The parameters, receiver, function, and context
  // are below the frame pointer.
  inline int frame_pointer() const;

  // The index of the first parameter.  The receiver lies below the first
  // parameter.
  int param0_index() { return 1; }

  // The index of the context slot in the frame.  It is immediately
  // below the frame pointer.
  inline int context_index();

  // The index of the function slot in the frame.  It is below the frame
  // pointer and context slot.
  inline int function_index();

  // The index of the first local.  Between the frame pointer and the
  // locals lies the return address.
  inline int local0_index() const;

  // The index of the base of the expression stack.
  inline int expression_base_index() const;

  // Convert a frame index into a frame pointer relative offset into the
  // actual stack.
  inline int fp_relative(int index);

  // Spill all elements in registers. Spill the top spilled_args elements
  // on the frame.  Sync all other frame elements.
  // Then drop dropped_args elements from the virtual frame, to match
  // the effect of an upcoming call that will drop them from the stack.
  void PrepareForCall(int spilled_args, int dropped_args);

  // If all top-of-stack registers are in use then the lowest one is pushed
  // onto the physical stack and made free.
  void EnsureOneFreeTOSRegister();

  // Emit instructions to get the top of stack state from where we are to where
  // we want to be.
  void MergeTOSTo(TopOfStack expected_state, Condition cond = al);

  inline bool Equals(const VirtualFrame* other);

  inline void LowerHeight(int count) {
    element_count_ -= count;
    if (count >= kTOSKnownSmiMapSize) {
      tos_known_smi_map_ = 0;
    } else {
      tos_known_smi_map_ >>= count;
    }
  }

  inline void RaiseHeight(int count, unsigned known_smi_map = 0) {
    ASSERT(count >= 32 || known_smi_map < (1u << count));
    element_count_ += count;
    if (count >= kTOSKnownSmiMapSize) {
      tos_known_smi_map_ = known_smi_map;
    } else {
      tos_known_smi_map_ = ((tos_known_smi_map_ << count) | known_smi_map);
    }
  }

  friend class JumpTarget;
};


} }  // namespace v8::internal

#endif  // V8_ARM_VIRTUAL_FRAME_ARM_H_