OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "src/ast.h" | 10 #include "src/ast.h" |
11 #include "src/identity-map.h" | 11 #include "src/identity-map.h" |
12 #include "src/interpreter/bytecodes.h" | 12 #include "src/interpreter/bytecodes.h" |
13 #include "src/zone.h" | 13 #include "src/zone.h" |
14 #include "src/zone-containers.h" | 14 #include "src/zone-containers.h" |
15 | 15 |
16 namespace v8 { | 16 namespace v8 { |
17 namespace internal { | 17 namespace internal { |
18 | 18 |
19 class Isolate; | 19 class Isolate; |
20 | 20 |
21 namespace interpreter { | 21 namespace interpreter { |
22 | 22 |
23 class BytecodeLabel; | |
23 class Register; | 24 class Register; |
24 | 25 |
25 class BytecodeArrayBuilder { | 26 class BytecodeArrayBuilder { |
26 public: | 27 public: |
27 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); | 28 BytecodeArrayBuilder(Isolate* isolate, Zone* zone); |
28 Handle<BytecodeArray> ToBytecodeArray(); | 29 Handle<BytecodeArray> ToBytecodeArray(); |
29 | 30 |
30 // Set number of parameters expected by function. | 31 // Set number of parameters expected by function. |
31 void set_parameter_count(int number_of_params); | 32 void set_parameter_count(int number_of_params); |
32 int parameter_count() const; | 33 int parameter_count() const; |
33 | 34 |
34 // Set number of locals required for bytecode array. | 35 // Set number of locals required for bytecode array. |
35 void set_locals_count(int number_of_locals); | 36 void set_locals_count(int number_of_locals); |
36 int locals_count() const; | 37 int locals_count() const; |
37 | 38 |
38 // Returns true if the bytecode has an explicit return at the end. | 39 // Returns true if the bytecode has an explicit return at the end. |
39 bool HasExplicitReturn(); | 40 bool HasExplicitReturn(); |
40 | 41 |
41 Register Parameter(int parameter_index); | 42 Register Parameter(int parameter_index); |
42 | 43 |
43 // Constant loads to accumulator. | 44 // Constant loads to accumulator. |
44 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); | 45 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
45 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); | 46 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); |
47 BytecodeArrayBuilder& LoadLiteral(int value); | |
rmcilroy
2015/09/18 10:42:23
I would prefer not to add this since it's not clea
oth
2015/09/23 10:46:55
Done. The intent was to enable writing slightly cl
| |
48 BytecodeArrayBuilder& LoadLiteral(double value); | |
46 BytecodeArrayBuilder& LoadUndefined(); | 49 BytecodeArrayBuilder& LoadUndefined(); |
47 BytecodeArrayBuilder& LoadNull(); | 50 BytecodeArrayBuilder& LoadNull(); |
48 BytecodeArrayBuilder& LoadTheHole(); | 51 BytecodeArrayBuilder& LoadTheHole(); |
49 BytecodeArrayBuilder& LoadTrue(); | 52 BytecodeArrayBuilder& LoadTrue(); |
50 BytecodeArrayBuilder& LoadFalse(); | 53 BytecodeArrayBuilder& LoadFalse(); |
51 | 54 |
52 // Register-accumulator transfers. | 55 // Register-accumulator transfers. |
53 BytecodeArrayBuilder& LoadAccumulatorWithRegister(Register reg); | 56 BytecodeArrayBuilder& LoadAccumulatorWithRegister(Register reg); |
54 BytecodeArrayBuilder& StoreAccumulatorInRegister(Register reg); | 57 BytecodeArrayBuilder& StoreAccumulatorInRegister(Register reg); |
55 | 58 |
(...skipping 11 matching lines...) Expand all Loading... | |
67 int feedback_slot, | 70 int feedback_slot, |
68 LanguageMode language_mode); | 71 LanguageMode language_mode); |
69 | 72 |
70 // Call a JS function. The JSFunction or Callable to be called should be in | 73 // Call a JS function. The JSFunction or Callable to be called should be in |
71 // |callable|, the receiver should be in |receiver| and all subsequent | 74 // |callable|, the receiver should be in |receiver| and all subsequent |
72 // arguments should be in registers <receiver + 1> to | 75 // arguments should be in registers <receiver + 1> to |
73 // <receiver + 1 + arg_count>. | 76 // <receiver + 1 + arg_count>. |
74 BytecodeArrayBuilder& Call(Register callable, Register receiver, | 77 BytecodeArrayBuilder& Call(Register callable, Register receiver, |
75 size_t arg_count); | 78 size_t arg_count); |
76 | 79 |
77 // Operators. | 80 // Operators (register == lhs, accumulator = rhs). |
78 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); | 81 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); |
82 BytecodeArrayBuilder& Add(Register reg); | |
83 BytecodeArrayBuilder& Subtract(Register reg); | |
84 BytecodeArrayBuilder& Multiply(Register reg); | |
85 BytecodeArrayBuilder& Divide(Register reg); | |
86 BytecodeArrayBuilder& Modulo(Register reg); | |
87 | |
88 // Tests. | |
89 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg); | |
79 | 90 |
80 // Flow Control. | 91 // Flow Control. |
92 BytecodeArrayBuilder& Bind(BytecodeLabel* label); | |
93 BytecodeArrayBuilder& Jump(BytecodeLabel* label); | |
94 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label); | |
95 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label); | |
81 BytecodeArrayBuilder& Return(); | 96 BytecodeArrayBuilder& Return(); |
82 | 97 |
83 private: | 98 private: |
84 static Bytecode BytecodeForBinaryOperation(Token::Value op); | 99 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
85 static bool FitsInByteOperand(int value); | 100 static Bytecode BytecodeForCompareOperation(Token::Value op); |
86 static bool FitsInByteOperand(size_t value); | 101 static bool FitsInIdxOperand(int value); |
102 static bool FitsInIdxOperand(size_t value); | |
103 static bool FitsInImm8Operand(int value); | |
104 static bool IsJumpWithSmi8Operand(Bytecode jump_bytecode); | |
105 static Bytecode GetJumpWithConstantOperand(Bytecode jump_with_smi8_operand); | |
87 | 106 |
88 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); | 107 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; } |
89 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); | 108 const ZoneVector<uint8_t>* bytecodes() const { return &bytecodes_; } |
90 void Output(Bytecode bytecode, uint8_t r0); | 109 Isolate* isolate() const { return isolate_; } |
110 | |
111 template <size_t N> | |
112 void RawOutputAt(ZoneVector<uint8_t>::iterator pos, uint8_t(&bytes)[N]); | |
113 void Output(const ZoneVector<uint8_t>::iterator& pos, Bytecode bytecode, | |
114 uint8_t operand); | |
115 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1, | |
116 uint8_t operand2); | |
117 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1); | |
118 void Output(Bytecode bytecode, uint8_t operand0); | |
91 void Output(Bytecode bytecode); | 119 void Output(Bytecode bytecode); |
120 void OutputJump(Bytecode jump_bytecode, | |
121 const ZoneVector<uint8_t>::iterator& jump_location, | |
122 const ZoneVector<uint8_t>::iterator& jump_target); | |
123 BytecodeArrayBuilder& Jump(Bytecode jump_bytecode, BytecodeLabel* label); | |
92 | 124 |
93 bool OperandIsValid(Bytecode bytecode, int operand_index, | 125 bool OperandIsValid(Bytecode bytecode, int operand_index, |
94 uint8_t operand_value) const; | 126 uint8_t operand_value) const; |
95 | 127 |
96 size_t GetConstantPoolEntry(Handle<Object> object); | 128 size_t GetConstantPoolEntry(Handle<Object> object); |
97 | 129 |
98 int BorrowTemporaryRegister(); | 130 int BorrowTemporaryRegister(); |
99 void ReturnTemporaryRegister(int reg_index); | 131 void ReturnTemporaryRegister(int reg_index); |
100 | 132 |
101 Isolate* isolate_; | 133 Isolate* isolate_; |
102 ZoneVector<uint8_t> bytecodes_; | 134 ZoneVector<uint8_t> bytecodes_; |
103 bool bytecode_generated_; | 135 bool bytecode_generated_; |
104 | 136 |
105 IdentityMap<size_t> constants_map_; | 137 IdentityMap<size_t> constants_map_; |
106 ZoneVector<Handle<Object>> constants_; | 138 ZoneVector<Handle<Object>> constants_; |
139 ZoneSet<BytecodeLabel*> forward_labels_; | |
140 ZoneSet<BytecodeLabel*> bound_labels_; | |
107 | 141 |
108 int parameter_count_; | 142 int parameter_count_; |
109 int local_register_count_; | 143 int local_register_count_; |
110 int temporary_register_count_; | 144 int temporary_register_count_; |
111 int temporary_register_next_; | 145 int temporary_register_next_; |
112 | 146 |
113 friend class TemporaryRegisterScope; | 147 friend class TemporaryRegisterScope; |
114 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 148 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
115 }; | 149 }; |
116 | 150 |
151 | |
152 // A label representing a branch target in a bytecode array. When a | |
153 // label is bound, it represents a known position in the bytecode | |
154 // array. For labels that are forward references there can be at most | |
155 // one reference whilst it is unbound. | |
156 class BytecodeLabel { | |
157 public: | |
158 static const size_t kUnbound = 0; | |
159 BytecodeLabel() : offset_(kUnbound) {} | |
160 | |
161 private: | |
162 // For forward references |offset_| is the offset of the branch that | |
163 // refers to this label. For bound references it is the offset of | |
164 // the label itself in the bytecode array. | |
165 size_t offset_; | |
166 | |
167 INLINE(void bind_to(size_t offset)) { offset_ = offset; } | |
168 INLINE(size_t offset() const) { return offset_; } | |
169 | |
170 friend class BytecodeArrayBuilder; | |
171 | |
172 DISALLOW_COPY_AND_ASSIGN(BytecodeLabel); | |
173 }; | |
174 | |
175 | |
117 // A stack-allocated class than allows the instantiator to allocate | 176 // A stack-allocated class than allows the instantiator to allocate |
118 // temporary registers that are cleaned up when scope is closed. | 177 // temporary registers that are cleaned up when scope is closed. |
119 class TemporaryRegisterScope { | 178 class TemporaryRegisterScope { |
120 public: | 179 public: |
121 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 180 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
122 ~TemporaryRegisterScope(); | 181 ~TemporaryRegisterScope(); |
123 Register NewRegister(); | 182 Register NewRegister(); |
124 | 183 |
125 private: | 184 private: |
126 void* operator new(size_t size); | 185 void* operator new(size_t size); |
127 void operator delete(void* p); | 186 void operator delete(void* p); |
128 | 187 |
129 BytecodeArrayBuilder* builder_; | 188 BytecodeArrayBuilder* builder_; |
130 int count_; | 189 int count_; |
131 int last_register_index_; | 190 int last_register_index_; |
132 | 191 |
133 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 192 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
134 }; | 193 }; |
135 | 194 |
136 | 195 |
137 } // namespace interpreter | 196 } // namespace interpreter |
138 } // namespace internal | 197 } // namespace internal |
139 } // namespace v8 | 198 } // namespace v8 |
140 | 199 |
141 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 200 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
OLD | NEW |