1// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_REGEXP_ASSEMBLER_BYTECODE_H_
6#define RUNTIME_VM_REGEXP_ASSEMBLER_BYTECODE_H_
7
8#include "vm/object.h"
9#include "vm/regexp_assembler.h"
10
11namespace dart {
12
13class BytecodeRegExpMacroAssembler : public RegExpMacroAssembler {
14 public:
15 // Create an assembler. Instructions and relocation information are emitted
16 // into a buffer, with the instructions starting from the beginning and the
17 // relocation information starting from the end of the buffer. See CodeDesc
18 // for a detailed comment on the layout (globals.h).
19 //
20 // If the provided buffer is NULL, the assembler allocates and grows its own
21 // buffer, and buffer_size determines the initial buffer size. The buffer is
22 // owned by the assembler and deallocated upon destruction of the assembler.
23 //
24 // If the provided buffer is not NULL, the assembler uses the provided buffer
25 // for code generation and assumes its size to be buffer_size. If the buffer
26 // is too small, a fatal error occurs. No deallocation of the buffer is done
27 // upon destruction of the assembler.
28 BytecodeRegExpMacroAssembler(ZoneGrowableArray<uint8_t>* buffer, Zone* zone);
29 virtual ~BytecodeRegExpMacroAssembler();
30
31 // The byte-code interpreter checks on each push anyway.
32 virtual intptr_t stack_limit_slack() { return 1; }
33 virtual bool CanReadUnaligned() { return false; }
34 virtual void BindBlock(BlockLabel* label);
35 virtual void AdvanceCurrentPosition(intptr_t by); // Signed cp change.
36 virtual void PopCurrentPosition();
37 virtual void PushCurrentPosition();
38 virtual void Backtrack();
39 virtual void GoTo(BlockLabel* label);
40 virtual void PushBacktrack(BlockLabel* label);
41 virtual bool Succeed();
42 virtual void Fail();
43 virtual void PopRegister(intptr_t register_index);
44 virtual void PushRegister(intptr_t register_index);
45 virtual void AdvanceRegister(intptr_t reg, intptr_t by); // r[reg] += by.
46 virtual void SetCurrentPositionFromEnd(intptr_t by);
47 virtual void SetRegister(intptr_t register_index, intptr_t to);
48 virtual void WriteCurrentPositionToRegister(intptr_t reg, intptr_t cp_offset);
49 virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to);
50 virtual void ReadCurrentPositionFromRegister(intptr_t reg);
51 virtual void WriteStackPointerToRegister(intptr_t reg);
52 virtual void ReadStackPointerFromRegister(intptr_t reg);
53 virtual void LoadCurrentCharacter(intptr_t cp_offset,
54 BlockLabel* on_end_of_input,
55 bool check_bounds = true,
56 intptr_t characters = 1);
57 virtual void CheckCharacter(unsigned c, BlockLabel* on_equal);
58 virtual void CheckCharacterAfterAnd(unsigned c,
59 unsigned mask,
60 BlockLabel* on_equal);
61 virtual void CheckCharacterGT(uint16_t limit, BlockLabel* on_greater);
62 virtual void CheckCharacterLT(uint16_t limit, BlockLabel* on_less);
63 virtual void CheckGreedyLoop(BlockLabel* on_tos_equals_current_position);
64 virtual void CheckAtStart(BlockLabel* on_at_start);
65 virtual void CheckNotAtStart(intptr_t cp_offset, BlockLabel* on_not_at_start);
66 virtual void CheckNotCharacter(unsigned c, BlockLabel* on_not_equal);
67 virtual void CheckNotCharacterAfterAnd(unsigned c,
68 unsigned mask,
69 BlockLabel* on_not_equal);
70 virtual void CheckNotCharacterAfterMinusAnd(uint16_t c,
71 uint16_t minus,
72 uint16_t mask,
73 BlockLabel* on_not_equal);
74 virtual void CheckCharacterInRange(uint16_t from,
75 uint16_t to,
76 BlockLabel* on_in_range);
77 virtual void CheckCharacterNotInRange(uint16_t from,
78 uint16_t to,
79 BlockLabel* on_not_in_range);
80 virtual void CheckBitInTable(const TypedData& table, BlockLabel* on_bit_set);
81 virtual void CheckNotBackReference(intptr_t start_reg,
82 bool read_backward,
83 BlockLabel* on_no_match);
84 virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg,
85 bool read_backward,
86 bool unicode,
87 BlockLabel* on_no_match);
88 virtual void IfRegisterLT(intptr_t register_index,
89 intptr_t comparand,
90 BlockLabel* if_lt);
91 virtual void IfRegisterGE(intptr_t register_index,
92 intptr_t comparand,
93 BlockLabel* if_ge);
94 virtual void IfRegisterEqPos(intptr_t register_index, BlockLabel* if_eq);
95
96 virtual IrregexpImplementation Implementation();
97 // virtual Handle<HeapObject> GetCode(Handle<String> source);
98 TypedDataPtr GetBytecode();
99
100 // New
101 virtual bool IsClosed() const {
102 // Added by Dart for the IR version. Bytecode version should never need an
103 // extra goto.
104 return true;
105 }
106 virtual void Print(const char* str) { UNIMPLEMENTED(); }
107 virtual void PrintBlocks() { UNIMPLEMENTED(); }
108 /////
109
110 static InstancePtr Interpret(const RegExp& regexp,
111 const String& str,
112 const Smi& start_index,
113 bool is_sticky,
114 Zone* zone);
115
116 private:
117 void Expand();
118 // Code and bitmap emission.
119 inline void EmitOrLink(BlockLabel* label);
120 inline void Emit32(uint32_t x);
121 inline void Emit16(uint32_t x);
122 inline void Emit8(uint32_t x);
123 inline void Emit(uint32_t bc, uint32_t arg);
124 // Bytecode buffer.
125 intptr_t length();
126
127 // The buffer into which code and relocation info are generated.
128 ZoneGrowableArray<uint8_t>* buffer_;
129
130 // The program counter.
131 intptr_t pc_;
132
133 BlockLabel backtrack_;
134
135 intptr_t advance_current_start_;
136 intptr_t advance_current_offset_;
137 intptr_t advance_current_end_;
138
139 static const int kInvalidPC = -1;
140
141 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeRegExpMacroAssembler);
142};
143
144} // namespace dart
145
146#endif // RUNTIME_VM_REGEXP_ASSEMBLER_BYTECODE_H_
147