1// Copyright 2014 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#ifndef V8_REGEXP_PPC_REGEXP_MACRO_ASSEMBLER_PPC_H_
6#define V8_REGEXP_PPC_REGEXP_MACRO_ASSEMBLER_PPC_H_
7
8#include "src/codegen/macro-assembler.h"
9#include "src/regexp/regexp-macro-assembler.h"
10
11namespace v8 {
12namespace internal {
13
14class V8_EXPORT_PRIVATE RegExpMacroAssemblerPPC
15    : public NativeRegExpMacroAssembler {
16 public:
17  RegExpMacroAssemblerPPC(Isolate* isolate, Zone* zone, Mode mode,
18                          int registers_to_save);
19  ~RegExpMacroAssemblerPPC() override;
20  int stack_limit_slack() override;
21  void AdvanceCurrentPosition(int by) override;
22  void AdvanceRegister(int reg, int by) override;
23  void Backtrack() override;
24  void Bind(Label* label) override;
25  void CheckAtStart(int cp_offset, Label* on_at_start) override;
26  void CheckCharacter(unsigned c, Label* on_equal) override;
27  void CheckCharacterAfterAnd(unsigned c, unsigned mask,
28                              Label* on_equal) override;
29  void CheckCharacterGT(base::uc16 limit, Label* on_greater) override;
30  void CheckCharacterLT(base::uc16 limit, Label* on_less) override;
31
32  // A "greedy loop" is a loop that is both greedy and with a simple
33  // body. It has a particularly simple implementation.
34  void CheckGreedyLoop(Label* on_tos_equals_current_position) override;
35  void CheckNotAtStart(int cp_offset, Label* on_not_at_start) override;
36  void CheckNotBackReference(int start_reg, bool read_backward,
37                             Label* on_no_match) override;
38  void CheckNotBackReferenceIgnoreCase(int start_reg, bool read_backward,
39                                       bool unicode,
40                                       Label* on_no_match) override;
41  void CheckNotCharacter(unsigned c, Label* on_not_equal) override;
42  void CheckNotCharacterAfterAnd(unsigned c, unsigned mask,
43                                 Label* on_not_equal) override;
44  void CheckNotCharacterAfterMinusAnd(base::uc16 c, base::uc16 minus,
45                                      base::uc16 mask,
46                                      Label* on_not_equal) override;
47  void CheckCharacterInRange(base::uc16 from, base::uc16 to,
48                             Label* on_in_range) override;
49  void CheckCharacterNotInRange(base::uc16 from, base::uc16 to,
50                                Label* on_not_in_range) override;
51  bool CheckCharacterInRangeArray(const ZoneList<CharacterRange>* ranges,
52                                  Label* on_in_range) override;
53  bool CheckCharacterNotInRangeArray(const ZoneList<CharacterRange>* ranges,
54                                     Label* on_not_in_range) override;
55  void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) override;
56
57  // Checks whether the given offset from the current position is before
58  // the end of the string.
59  void CheckPosition(int cp_offset, Label* on_outside_input) override;
60  bool CheckSpecialCharacterClass(StandardCharacterSet type,
61                                  Label* on_no_match) override;
62  void Fail() override;
63  Handle<HeapObject> GetCode(Handle<String> source) override;
64  void GoTo(Label* label) override;
65  void IfRegisterGE(int reg, int comparand, Label* if_ge) override;
66  void IfRegisterLT(int reg, int comparand, Label* if_lt) override;
67  void IfRegisterEqPos(int reg, Label* if_eq) override;
68  IrregexpImplementation Implementation() override;
69  void LoadCurrentCharacterUnchecked(int cp_offset,
70                                     int character_count) override;
71  void PopCurrentPosition() override;
72  void PopRegister(int register_index) override;
73  void PushBacktrack(Label* label) override;
74  void PushCurrentPosition() override;
75  void PushRegister(int register_index,
76                    StackCheckFlag check_stack_limit) override;
77  void ReadCurrentPositionFromRegister(int reg) override;
78  void ReadStackPointerFromRegister(int reg) override;
79  void SetCurrentPositionFromEnd(int by) override;
80  void SetRegister(int register_index, int to) override;
81  bool Succeed() override;
82  void WriteCurrentPositionToRegister(int reg, int cp_offset) override;
83  void ClearRegisters(int reg_from, int reg_to) override;
84  void WriteStackPointerToRegister(int reg) override;
85
86  // Called from RegExp if the stack-guard is triggered.
87  // If the code object is relocated, the return address is fixed before
88  // returning.
89  // {raw_code} is an Address because this is called via ExternalReference.
90  static int CheckStackGuardState(Address* return_address, Address raw_code,
91                                  Address re_frame);
92
93 private:
94  // Offsets from frame_pointer() of function parameters and stored registers.
95  static const int kFramePointer = 0;
96
97  // Above the frame pointer - Stored registers and stack passed parameters.
98  static const int kStoredRegisters = kFramePointer;
99  // Return address (stored from link register, read into pc on return).
100  static const int kReturnAddress = kStoredRegisters + 7 * kSystemPointerSize;
101  static const int kCallerFrame = kReturnAddress + kSystemPointerSize;
102
103  // Below the frame pointer.
104  // Register parameters stored by setup code.
105  static const int kIsolate = kFramePointer - kSystemPointerSize;
106  static const int kDirectCall = kIsolate - kSystemPointerSize;
107  static const int kNumOutputRegisters = kDirectCall - kSystemPointerSize;
108  static const int kRegisterOutput = kNumOutputRegisters - kSystemPointerSize;
109  static const int kInputEnd = kRegisterOutput - kSystemPointerSize;
110  static const int kInputStart = kInputEnd - kSystemPointerSize;
111  static const int kStartIndex = kInputStart - kSystemPointerSize;
112  static const int kInputString = kStartIndex - kSystemPointerSize;
113  // When adding local variables remember to push space for them in
114  // the frame in GetCode.
115  static const int kSuccessfulCaptures = kInputString - kSystemPointerSize;
116  static const int kStringStartMinusOne =
117      kSuccessfulCaptures - kSystemPointerSize;
118  static const int kBacktrackCount = kStringStartMinusOne - kSystemPointerSize;
119  // Stores the initial value of the regexp stack pointer in a
120  // position-independent representation (in case the regexp stack grows and
121  // thus moves).
122  static const int kRegExpStackBasePointer =
123      kBacktrackCount - kSystemPointerSize;
124
125  // First register address. Following registers are below it on the stack.
126  static const int kRegisterZero = kRegExpStackBasePointer - kSystemPointerSize;
127
128  // Initial size of code buffer.
129  static const int kRegExpCodeSize = 1024;
130
131  // Check whether preemption has been requested.
132  void CheckPreemption();
133
134  // Check whether we are exceeding the stack limit on the backtrack stack.
135  void CheckStackLimit();
136
137  void CallCheckStackGuardState(Register scratch);
138  void CallIsCharacterInRangeArray(const ZoneList<CharacterRange>* ranges);
139
140  // The ebp-relative location of a regexp register.
141  MemOperand register_location(int register_index);
142
143  // Register holding the current input position as negative offset from
144  // the end of the string.
145  static constexpr Register current_input_offset() { return r27; }
146
147  // The register containing the current character after LoadCurrentCharacter.
148  static constexpr Register current_character() { return r28; }
149
150  // Register holding address of the end of the input string.
151  static constexpr Register end_of_input_address() { return r30; }
152
153  // Register holding the frame address. Local variables, parameters and
154  // regexp registers are addressed relative to this.
155  static constexpr Register frame_pointer() { return fp; }
156
157  // The register containing the backtrack stack top. Provides a meaningful
158  // name to the register.
159  static constexpr Register backtrack_stackpointer() { return r29; }
160
161  // Register holding pointer to the current code object.
162  static constexpr Register code_pointer() { return r26; }
163
164  // Byte size of chars in the string to match (decided by the Mode argument)
165  inline int char_size() const { return static_cast<int>(mode_); }
166
167  // Equivalent to a conditional branch to the label, unless the label
168  // is nullptr, in which case it is a conditional Backtrack.
169  void BranchOrBacktrack(Condition condition, Label* to, CRegister cr = cr7);
170
171  // Call and return internally in the generated code in a way that
172  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
173  inline void SafeCall(Label* to, Condition cond = al, CRegister cr = cr7);
174  inline void SafeReturn();
175  inline void SafeCallTarget(Label* name);
176
177  // Pushes the value of a register on the backtrack stack. Decrements the
178  // stack pointer by a word size and stores the register's value there.
179  inline void Push(Register source);
180
181  // Pops a value from the backtrack stack. Reads the word at the stack pointer
182  // and increments it by a word size.
183  inline void Pop(Register target);
184
185  void LoadRegExpStackPointerFromMemory(Register dst);
186  void StoreRegExpStackPointerToMemory(Register src, Register scratch);
187  void PushRegExpBasePointer(Register stack_pointer, Register scratch);
188  void PopRegExpBasePointer(Register stack_pointer_out, Register scratch);
189
190  Isolate* isolate() const { return masm_->isolate(); }
191
192  const std::unique_ptr<MacroAssembler> masm_;
193  const NoRootArrayScope no_root_array_scope_;
194
195  // Which mode to generate code for (Latin1 or UC16).
196  const Mode mode_;
197
198  // One greater than maximal register index actually used.
199  int num_registers_;
200
201  // Number of registers to output at the end (the saved registers
202  // are always 0..num_saved_registers_-1)
203  const int num_saved_registers_;
204
205  // Labels used internally.
206  Label entry_label_;
207  Label start_label_;
208  Label success_label_;
209  Label backtrack_label_;
210  Label exit_label_;
211  Label check_preempt_label_;
212  Label stack_overflow_label_;
213  Label internal_failure_label_;
214  Label fallback_label_;
215};
216
217// Set of non-volatile registers saved/restored by generated regexp code.
218const RegList kRegExpCalleeSaved = {r25, r26, r27, r28, r29, r30, fp};
219
220}  // namespace internal
221}  // namespace v8
222
223#endif  // V8_REGEXP_PPC_REGEXP_MACRO_ASSEMBLER_PPC_H_
224