Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(647)

Side by Side Diff: src/interpreter/bytecode-array-builder.h

Issue 1343363002: [Interpreter] Basic flow control. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebase to fix patch failure with git cl try. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 bool HasExplicitReturn();
40
41 Register Parameter(int parameter_index); 39 Register Parameter(int parameter_index);
42 40
43 // Constant loads to accumulator. 41 // Constant loads to accumulator.
44 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); 42 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value);
45 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object); 43 BytecodeArrayBuilder& LoadLiteral(Handle<Object> object);
46 BytecodeArrayBuilder& LoadUndefined(); 44 BytecodeArrayBuilder& LoadUndefined();
47 BytecodeArrayBuilder& LoadNull(); 45 BytecodeArrayBuilder& LoadNull();
48 BytecodeArrayBuilder& LoadTheHole(); 46 BytecodeArrayBuilder& LoadTheHole();
49 BytecodeArrayBuilder& LoadTrue(); 47 BytecodeArrayBuilder& LoadTrue();
50 BytecodeArrayBuilder& LoadFalse(); 48 BytecodeArrayBuilder& LoadFalse();
(...skipping 16 matching lines...) Expand all
67 int feedback_slot, 65 int feedback_slot,
68 LanguageMode language_mode); 66 LanguageMode language_mode);
69 67
70 // Call a JS function. The JSFunction or Callable to be called should be in 68 // 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 69 // |callable|, the receiver should be in |receiver| and all subsequent
72 // arguments should be in registers <receiver + 1> to 70 // arguments should be in registers <receiver + 1> to
73 // <receiver + 1 + arg_count>. 71 // <receiver + 1 + arg_count>.
74 BytecodeArrayBuilder& Call(Register callable, Register receiver, 72 BytecodeArrayBuilder& Call(Register callable, Register receiver,
75 size_t arg_count); 73 size_t arg_count);
76 74
77 // Operators. 75 // Operators (register == lhs, accumulator = rhs).
78 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); 76 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg);
79 77
78 // Tests.
79 BytecodeArrayBuilder& CompareOperation(Token::Value op, Register reg,
80 LanguageMode language_mode);
81
82 // Casts
83 BytecodeArrayBuilder& CastAccumulatorToBoolean();
84
80 // Flow Control. 85 // Flow Control.
86 BytecodeArrayBuilder& Bind(BytecodeLabel* label);
87 BytecodeArrayBuilder& Jump(BytecodeLabel* label);
88 BytecodeArrayBuilder& JumpIfTrue(BytecodeLabel* label);
89 BytecodeArrayBuilder& JumpIfFalse(BytecodeLabel* label);
81 BytecodeArrayBuilder& Return(); 90 BytecodeArrayBuilder& Return();
82 91
92 BytecodeArrayBuilder& EnterBlock();
93 BytecodeArrayBuilder& LeaveBlock();
94
83 private: 95 private:
96 ZoneVector<uint8_t>* bytecodes() { return &bytecodes_; }
97 const ZoneVector<uint8_t>* bytecodes() const { return &bytecodes_; }
98 Isolate* isolate() const { return isolate_; }
99
84 static Bytecode BytecodeForBinaryOperation(Token::Value op); 100 static Bytecode BytecodeForBinaryOperation(Token::Value op);
85 static bool FitsInByteOperand(int value); 101 static Bytecode BytecodeForCompareOperation(Token::Value op);
86 static bool FitsInByteOperand(size_t value); 102 static bool FitsInIdxOperand(int value);
103 static bool FitsInIdxOperand(size_t value);
104 static bool FitsInImm8Operand(int value);
105 static bool IsJumpWithSmi8Operand(Bytecode jump_bytecode);
106 static Bytecode GetJumpWithConstantOperand(Bytecode jump_with_smi8_operand);
87 107
88 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); 108 template <size_t N>
89 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); 109 void Output(uint8_t(&bytes)[N]);
90 void Output(Bytecode bytecode, uint8_t r0); 110 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1,
111 uint8_t operand2);
112 void Output(Bytecode bytecode, uint8_t operand0, uint8_t operand1);
113 void Output(Bytecode bytecode, uint8_t operand0);
91 void Output(Bytecode bytecode); 114 void Output(Bytecode bytecode);
115 void PatchJump(const ZoneVector<uint8_t>::iterator& jump_target,
116 ZoneVector<uint8_t>::iterator jump_location);
117 void OutputJump(Bytecode jump_bytecode,
118 const ZoneVector<uint8_t>::iterator& jump_target);
119 BytecodeArrayBuilder& Jump(Bytecode jump_bytecode, BytecodeLabel* label);
120 void EnsureReturn();
rmcilroy 2015/09/23 14:00:28 nit - newline above EnsureReturn();
oth 2015/09/24 11:15:27 Done.
92 121
93 bool OperandIsValid(Bytecode bytecode, int operand_index, 122 bool OperandIsValid(Bytecode bytecode, int operand_index,
94 uint8_t operand_value) const; 123 uint8_t operand_value) const;
124 bool LastBytecodeInSameBlock() const;
95 125
96 size_t GetConstantPoolEntry(Handle<Object> object); 126 size_t GetConstantPoolEntry(Handle<Object> object);
97 127
128 // Scope helpers used by TemporaryRegisterScope
98 int BorrowTemporaryRegister(); 129 int BorrowTemporaryRegister();
99 void ReturnTemporaryRegister(int reg_index); 130 void ReturnTemporaryRegister(int reg_index);
100 131
101 Isolate* isolate_; 132 Isolate* isolate_;
102 ZoneVector<uint8_t> bytecodes_; 133 ZoneVector<uint8_t> bytecodes_;
103 bool bytecode_generated_; 134 bool bytecode_generated_;
135 size_t last_block_end_;
136 size_t last_bytecode_start_;
104 137
105 IdentityMap<size_t> constants_map_; 138 IdentityMap<size_t> constants_map_;
106 ZoneVector<Handle<Object>> constants_; 139 ZoneVector<Handle<Object>> constants_;
107 140
108 int parameter_count_; 141 int parameter_count_;
109 int local_register_count_; 142 int local_register_count_;
110 int temporary_register_count_; 143 int temporary_register_count_;
111 int temporary_register_next_; 144 int temporary_register_next_;
112 145
113 friend class TemporaryRegisterScope; 146 friend class TemporaryRegisterScope;
114 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); 147 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder);
115 }; 148 };
116 149
150
151 // A label representing a branch target in a bytecode array. When a
152 // label is bound, it represents a known position in the bytecode
153 // array. For labels that are forward references there can be at most
154 // one reference whilst it is unbound.
155 class BytecodeLabel final {
156 public:
157 BytecodeLabel() : bound_(false), offset_(kInvalidOffset) {}
158 ~BytecodeLabel() { DCHECK(bound_ && offset_ != kInvalidOffset); }
159
160 private:
161 static const size_t kInvalidOffset = static_cast<size_t>(-1);
162
163 INLINE(void bind_to(size_t offset)) {
164 DCHECK(!bound_ && offset != kInvalidOffset);
165 offset_ = offset;
166 bound_ = true;
167 }
168 INLINE(void set_referrer(size_t offset)) {
169 DCHECK(!bound_ && offset != kInvalidOffset);
170 offset_ = offset;
171 }
172 INLINE(size_t offset() const) { return offset_; }
173 INLINE(bool is_bound() const) { return bound_; }
174 INLINE(bool is_forward_target() const) {
175 return offset() != kInvalidOffset && !is_bound();
176 }
177
178 // There are three states for a label:
179 // bound_ offset_
180 // UNSET false kInvalidOffset
181 // FORWARD_TARGET false Offset of referring jump
182 // BACKWARD_TARGET true Offset of label in bytecode array when bound
183 bool bound_;
184 size_t offset_;
185
186 friend class BytecodeArrayBuilder;
187 DISALLOW_COPY_AND_ASSIGN(BytecodeLabel);
188 };
189
190
117 // A stack-allocated class than allows the instantiator to allocate 191 // A stack-allocated class than allows the instantiator to allocate
118 // temporary registers that are cleaned up when scope is closed. 192 // temporary registers that are cleaned up when scope is closed.
119 class TemporaryRegisterScope { 193 class TemporaryRegisterScope {
120 public: 194 public:
121 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); 195 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder);
122 ~TemporaryRegisterScope(); 196 ~TemporaryRegisterScope();
123 Register NewRegister(); 197 Register NewRegister();
124 198
125 private: 199 private:
126 void* operator new(size_t size); 200 void* operator new(size_t size);
127 void operator delete(void* p); 201 void operator delete(void* p);
128 202
129 BytecodeArrayBuilder* builder_; 203 BytecodeArrayBuilder* builder_;
130 int count_; 204 int count_;
131 int last_register_index_; 205 int last_register_index_;
132 206
133 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); 207 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope);
134 }; 208 };
135 209
136 210
137 } // namespace interpreter 211 } // namespace interpreter
138 } // namespace internal 212 } // namespace internal
139 } // namespace v8 213 } // namespace v8
140 214
141 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ 215 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698