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#ifndef V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_
6#define V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_
7
8#ifdef V8_INTERPRETED_REGEXP
9
10#include "src/regexp/regexp-macro-assembler.h"
11
12namespace v8 {
13namespace internal {
14
15// A light-weight assembler for the Irregexp byte code.
16class RegExpMacroAssemblerIrregexp: public RegExpMacroAssembler {
17 public:
18  // Create an assembler. Instructions and relocation information are emitted
19  // into a buffer, with the instructions starting from the beginning and the
20  // relocation information starting from the end of the buffer. See CodeDesc
21  // for a detailed comment on the layout (globals.h).
22  //
23  // If the provided buffer is NULL, the assembler allocates and grows its own
24  // buffer, and buffer_size determines the initial buffer size. The buffer is
25  // owned by the assembler and deallocated upon destruction of the assembler.
26  //
27  // If the provided buffer is not NULL, the assembler uses the provided buffer
28  // for code generation and assumes its size to be buffer_size. If the buffer
29  // is too small, a fatal error occurs. No deallocation of the buffer is done
30  // upon destruction of the assembler.
31  RegExpMacroAssemblerIrregexp(Isolate* isolate, Vector<byte> buffer,
32                               Zone* zone);
33  virtual ~RegExpMacroAssemblerIrregexp();
34  // The byte-code interpreter checks on each push anyway.
35  virtual int stack_limit_slack() { return 1; }
36  virtual bool CanReadUnaligned() { return false; }
37  virtual void Bind(Label* label);
38  virtual void AdvanceCurrentPosition(int by);  // Signed cp change.
39  virtual void PopCurrentPosition();
40  virtual void PushCurrentPosition();
41  virtual void Backtrack();
42  virtual void GoTo(Label* label);
43  virtual void PushBacktrack(Label* label);
44  virtual bool Succeed();
45  virtual void Fail();
46  virtual void PopRegister(int register_index);
47  virtual void PushRegister(int register_index,
48                            StackCheckFlag check_stack_limit);
49  virtual void AdvanceRegister(int reg, int by);  // r[reg] += by.
50  virtual void SetCurrentPositionFromEnd(int by);
51  virtual void SetRegister(int register_index, int to);
52  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
53  virtual void ClearRegisters(int reg_from, int reg_to);
54  virtual void ReadCurrentPositionFromRegister(int reg);
55  virtual void WriteStackPointerToRegister(int reg);
56  virtual void ReadStackPointerFromRegister(int reg);
57  virtual void LoadCurrentCharacter(int cp_offset,
58                                    Label* on_end_of_input,
59                                    bool check_bounds = true,
60                                    int characters = 1);
61  virtual void CheckCharacter(unsigned c, Label* on_equal);
62  virtual void CheckCharacterAfterAnd(unsigned c,
63                                      unsigned mask,
64                                      Label* on_equal);
65  virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
66  virtual void CheckCharacterLT(uc16 limit, Label* on_less);
67  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
68  virtual void CheckAtStart(Label* on_at_start);
69  virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start);
70  virtual void CheckNotCharacter(unsigned c, Label* on_not_equal);
71  virtual void CheckNotCharacterAfterAnd(unsigned c,
72                                         unsigned mask,
73                                         Label* on_not_equal);
74  virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
75                                              uc16 minus,
76                                              uc16 mask,
77                                              Label* on_not_equal);
78  virtual void CheckCharacterInRange(uc16 from,
79                                     uc16 to,
80                                     Label* on_in_range);
81  virtual void CheckCharacterNotInRange(uc16 from,
82                                        uc16 to,
83                                        Label* on_not_in_range);
84  virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
85  virtual void CheckNotBackReference(int start_reg, bool read_backward,
86                                     Label* on_no_match);
87  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
88                                               bool read_backward, bool unicode,
89                                               Label* on_no_match);
90  virtual void IfRegisterLT(int register_index, int comparand, Label* if_lt);
91  virtual void IfRegisterGE(int register_index, int comparand, Label* if_ge);
92  virtual void IfRegisterEqPos(int register_index, Label* if_eq);
93
94  virtual IrregexpImplementation Implementation();
95  virtual Handle<HeapObject> GetCode(Handle<String> source);
96
97 private:
98  void Expand();
99  // Code and bitmap emission.
100  inline void EmitOrLink(Label* label);
101  inline void Emit32(uint32_t x);
102  inline void Emit16(uint32_t x);
103  inline void Emit8(uint32_t x);
104  inline void Emit(uint32_t bc, uint32_t arg);
105  // Bytecode buffer.
106  int length();
107  void Copy(Address a);
108
109  // The buffer into which code and relocation info are generated.
110  Vector<byte> buffer_;
111  // The program counter.
112  int pc_;
113  // True if the assembler owns the buffer, false if buffer is external.
114  bool own_buffer_;
115  Label backtrack_;
116
117  int advance_current_start_;
118  int advance_current_offset_;
119  int advance_current_end_;
120
121  Isolate* isolate_;
122
123  static const int kInvalidPC = -1;
124
125  DISALLOW_IMPLICIT_CONSTRUCTORS(RegExpMacroAssemblerIrregexp);
126};
127
128}  // namespace internal
129}  // namespace v8
130
131#endif  // V8_INTERPRETED_REGEXP
132
133#endif  // V8_REGEXP_REGEXP_MACRO_ASSEMBLER_IRREGEXP_H_
134