OLD | NEW |
| (Empty) |
1 // Copyright 2006-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 // A light-weight assembler for the Irregexp byte code. | |
29 | |
30 #ifndef V8_ASSEMBLER_IRREGEXP_H_ | |
31 #define V8_ASSEMBLER_IRREGEXP_H_ | |
32 | |
33 namespace v8 { namespace internal { | |
34 | |
35 | |
36 class IrregexpAssembler { | |
37 public: | |
38 // Create an assembler. Instructions and relocation information are emitted | |
39 // into a buffer, with the instructions starting from the beginning and the | |
40 // relocation information starting from the end of the buffer. See CodeDesc | |
41 // for a detailed comment on the layout (globals.h). | |
42 // | |
43 // If the provided buffer is NULL, the assembler allocates and grows its own | |
44 // buffer, and buffer_size determines the initial buffer size. The buffer is | |
45 // owned by the assembler and deallocated upon destruction of the assembler. | |
46 // | |
47 // If the provided buffer is not NULL, the assembler uses the provided buffer | |
48 // for code generation and assumes its size to be buffer_size. If the buffer | |
49 // is too small, a fatal error occurs. No deallocation of the buffer is done | |
50 // upon destruction of the assembler. | |
51 explicit IrregexpAssembler(Vector<byte>); | |
52 ~IrregexpAssembler(); | |
53 | |
54 // CP = current position in source. | |
55 // BT = backtrack label. | |
56 | |
57 // Stack. | |
58 void PushCurrentPosition(int cp_offset = 0); | |
59 void PushBacktrack(Label* l); | |
60 void PushRegister(int index); | |
61 void WriteCurrentPositionToRegister(int index, int cp_offset = 0); | |
62 void ReadCurrentPositionFromRegister(int index); | |
63 void WriteStackPointerToRegister(int index); | |
64 void ReadStackPointerFromRegister(int index); | |
65 void SetRegister(int index, int value); | |
66 void AdvanceRegister(int index, int by); | |
67 | |
68 void PopCurrentPosition(); | |
69 void PopBacktrack(); | |
70 void PopRegister(int index); | |
71 | |
72 void Fail(); | |
73 void Succeed(); | |
74 | |
75 // This instruction will cause a fatal VM error if hit. | |
76 void Break(); | |
77 | |
78 // Binds an unbound label L to the current code posn. | |
79 void Bind(Label* l); | |
80 | |
81 void AdvanceCP(int by); | |
82 | |
83 void GoTo(Label* l); | |
84 | |
85 // Loads current char into a machine register. Jumps to the label if we | |
86 // reached the end of the subject string. Fall through otherwise. | |
87 void LoadCurrentChar(int cp_offset, Label* on_end); | |
88 | |
89 // Checks current char register against a singleton. | |
90 void CheckCharacter(uc16 c, Label* on_match); | |
91 void CheckNotCharacter(uc16 c, Label* on_mismatch); | |
92 void OrThenCheckNotCharacter(uc16 c, uc16 mask, Label* on_mismatch); | |
93 void MinusOrThenCheckNotCharacter(uc16 c, uc16 mask, Label* on_mismatch); | |
94 | |
95 // Used to check current char register against a range. | |
96 void CheckCharacterLT(uc16 limit, Label* on_less); | |
97 void CheckCharacterGT(uc16 limit, Label* on_greater); | |
98 | |
99 // Checks current position for a match against a previous capture. Advances | |
100 // current position by the length of the capture iff it matches. The capture | |
101 // is stored in a given register and the register after. If a register | |
102 // contains -1 then the other register must always contain -1 and the | |
103 // on_mismatch label will never be called. | |
104 void CheckNotBackReference(int capture_index, Label* on_mismatch); | |
105 void CheckNotBackReferenceNoCase(int capture_index, Label* on_mismatch); | |
106 void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal); | |
107 | |
108 // Checks a register for strictly-less-than or greater-than-or-equal. | |
109 void CheckRegisterLT(int reg_index, uint16_t vs, Label* on_less_than); | |
110 void CheckRegisterGE(int reg_index, uint16_t vs, Label* on_greater_equal); | |
111 | |
112 // Subtracts a 16 bit value from the current character, uses the result to | |
113 // look up in a bit array, uses the result of that to decide whether to fall | |
114 // though (on 1) or jump to the on_zero label (on 0). | |
115 void LookupMap1(uc16 start, Label* bit_map, Label* on_zero); | |
116 | |
117 // Subtracts a 16 bit value from the current character, uses the result to | |
118 // look up in a 2-bit array, uses the result of that to look up in a label | |
119 // table and jumps to the label. | |
120 void LookupMap2(uc16 start, | |
121 Label* half_nibble_map, | |
122 const Vector<Label*>& table); | |
123 | |
124 // Subtracts a 16 bit value from the current character, uses the result to | |
125 // look up in a byte array, uses the result of that to look up in a label | |
126 // array and jumps to the label. | |
127 void LookupMap8(uc16 start, Label* byte_map, const Vector<Label*>& table); | |
128 | |
129 // Takes the high byte of the current character, uses the result to | |
130 // look up in a byte array, uses the result of that to look up in a label | |
131 // array and jumps to the label. | |
132 void LookupHighMap8(byte start, Label* byte_map, const Vector<Label*>& table); | |
133 | |
134 // Code and bitmap emission. | |
135 inline void Emit32(uint32_t x); | |
136 inline void Emit16(uint32_t x); | |
137 inline void Emit(uint32_t x); | |
138 | |
139 // Bytecode buffer. | |
140 int length(); | |
141 void Copy(Address a); | |
142 | |
143 inline void EmitOrLink(Label* l); | |
144 private: | |
145 inline void CheckRegister(int byte_code, | |
146 int reg_index, | |
147 uint16_t vs, | |
148 Label* on_true); | |
149 void Expand(); | |
150 | |
151 // The buffer into which code and relocation info are generated. | |
152 Vector<byte> buffer_; | |
153 // The program counter. | |
154 int pc_; | |
155 // True if the assembler owns the buffer, false if buffer is external. | |
156 bool own_buffer_; | |
157 | |
158 DISALLOW_IMPLICIT_CONSTRUCTORS(IrregexpAssembler); | |
159 }; | |
160 | |
161 | |
162 } } // namespace v8::internal | |
163 | |
164 #endif // V8_ASSEMBLER_IRREGEXP_H_ | |
OLD | NEW |