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