blob: d303910f16ad065ff39c8a689c7a45ee73ad9f23 [file] [log] [blame]
ager@chromium.orga74f0da2008-12-03 16:05:52 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef REGEXP_MACRO_ASSEMBLER_IA32_H_
29#define REGEXP_MACRO_ASSEMBLER_IA32_H_
30
31namespace v8 { namespace internal {
32
33class RegExpMacroAssemblerIA32: public RegExpMacroAssembler {
34 public:
35 // Type of input string to generate code for.
36 enum Mode {ASCII = 1, UC16 = 2};
37
38 RegExpMacroAssemblerIA32(Mode mode, int registers_to_save);
39 virtual ~RegExpMacroAssemblerIA32();
40 virtual void AdvanceCurrentPosition(int by);
41 virtual void AdvanceRegister(int reg, int by);
42 virtual void Backtrack();
43 virtual void Bind(Label* label);
44 virtual void CheckBitmap(uc16 start, Label* bitmap, Label* on_zero);
45 virtual void CheckCharacter(uc16 c, Label* on_equal);
46 virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
47 virtual void CheckCharacterLT(uc16 limit, Label* on_less);
48 virtual void CheckCharacters(Vector<const uc16> str,
49 int cp_offset,
50 Label* on_failure);
51 virtual void CheckCurrentPosition(int register_index, Label* on_equal);
52 virtual void CheckNotAtStart(Label* on_not_at_start);
53 virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
54 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
55 Label* on_no_match);
56 virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal);
57 virtual void CheckNotCharacter(uc16 c, Label* on_not_equal);
58 virtual void CheckNotCharacterAfterOr(uc16 c, uc16 mask, Label* on_not_equal);
59 virtual void CheckNotCharacterAfterMinusOr(uc16 c,
60 uc16 mask,
61 Label* on_not_equal);
62 virtual void DispatchByteMap(uc16 start,
63 Label* byte_map,
64 const Vector<Label*>& destinations);
65 virtual void DispatchHalfNibbleMap(uc16 start,
66 Label* half_nibble_map,
67 const Vector<Label*>& destinations);
68 virtual void DispatchHighByteMap(byte start,
69 Label* byte_map,
70 const Vector<Label*>& destinations);
71 virtual void EmitOrLink(Label* label);
72 virtual void Fail();
73 virtual Handle<Object> GetCode();
74 virtual void GoTo(Label* label);
75 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
76 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
77 virtual IrregexpImplementation Implementation();
78 virtual void LoadCurrentCharacter(int cp_offset, Label* on_end_of_input);
79 virtual void PopCurrentPosition();
80 virtual void PopRegister(int register_index);
81 virtual void PushBacktrack(Label* label);
82 virtual void PushCurrentPosition();
83 virtual void PushRegister(int register_index);
84 virtual void ReadCurrentPositionFromRegister(int reg);
85 virtual void ReadStackPointerFromRegister(int reg);
86 virtual void SetRegister(int register_index, int to);
87 virtual void Succeed();
88 virtual void WriteCurrentPositionToRegister(int reg);
89 virtual void WriteStackPointerToRegister(int reg);
90
91 template <typename T>
92 static inline bool Execute(Code* code,
93 T** input,
94 int start_offset,
95 int end_offset,
96 int* output,
97 bool at_start) {
98 typedef bool (*matcher)(T**, int, int, int*, int);
99 matcher matcher_func = FUNCTION_CAST<matcher>(code->entry());
100 int at_start_val = at_start ? 1 : 0;
101 return matcher_func(input, start_offset, end_offset, output, at_start_val);
102 }
103
104 private:
105 // Offsets from ebp of arguments to function.
106 static const int kBackup_ebx = sizeof(uint32_t);
107 static const int kBackup_edi = kBackup_ebx + sizeof(uint32_t);
108 static const int kBackup_esi = kBackup_edi + sizeof(uint32_t);
109 static const int kReturn_eip = kBackup_esi + sizeof(uint32_t);
110 static const int kInputBuffer = kReturn_eip + sizeof(uint32_t);
111 static const int kInputStartOffset = kInputBuffer + sizeof(uint32_t);
112 static const int kInputEndOffset = kInputStartOffset + sizeof(uint32_t);
113 static const int kRegisterOutput = kInputEndOffset + sizeof(uint32_t);
114 static const int kAtStart = kRegisterOutput + sizeof(uint32_t);
115
116 // Initial size of code buffer.
117 static const size_t kRegExpCodeSize = 1024;
118 // Initial size of constant buffers allocated during compilation.
119 static const int kRegExpConstantsSize = 256;
120 // Only unroll loops up to this length.
121 static const int kMaxInlineStringTests = 8;
122 // Special "character" marking end of input.
123 static const uint32_t kEndOfInput = ~0;
124
125 // The ebp-relative location of a regexp register.
126 Operand register_location(int register_index);
127
128 // The register containing the current character after LoadCurrentCharacter.
129 Register current_character();
130
131 // Byte size of chars in the string to match (decided by the Mode argument)
132 size_t char_size();
133
134 // Equivalent to a conditional branch to the label, unless the label
135 // is NULL, in which case it is a conditional Backtrack.
136 void BranchOrBacktrack(Condition condition, Label* to);
137
138 // Read a character from input at the given offset from the current
139 // position.
140 void LoadCurrentCharToRegister(int cp_offset);
141
142 // Load the address of a "constant buffer" (a slice of a byte array)
143 // into a register. The address is computed from the ByteArray* address
144 // and an offset. Uses no extra registers.
145 void LoadConstantBufferAddress(Register reg, ArraySlice* buffer);
146
147 // Adds code that checks whether preemption has been requested
148 // (and checks if we have hit the stack limit too).
149 void CheckStackLimit();
150
151 MacroAssembler* masm_;
152 // Constant buffer provider. Allocates external storage for storing
153 // constants.
154 ByteArrayProvider constants_;
155 // Which mode to generate code for (ASCII or UTF16).
156 Mode mode_;
157 // One greater than maximal register index actually used.
158 int num_registers_;
159 // Number of registers to output at the end (the saved registers
160 // are always 0..num_saved_registers_-1)
161 int num_saved_registers_;
162 // Labels used internally.
163 Label entry_label_;
164 Label start_label_;
165 Label success_label_;
166 Label exit_label_;
167 // Handle used to represent the generated code object itself.
168 Handle<Object> self_;
169};
170
171}} // namespace v8::internal
172
173#endif /* REGEXP_MACRO_ASSEMBLER_IA32_H_ */