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

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

Issue 1321663003: [Interpreter] Add support for loading literals from the constant pool. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@int_const_pool_1
Patch Set: Review comments Created 5 years, 3 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
« no previous file with comments | « src/interpreter/bytecode-array-builder.h ('k') | src/interpreter/bytecode-generator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "src/interpreter/bytecode-array-builder.h" 5 #include "src/interpreter/bytecode-array-builder.h"
6 6
7 namespace v8 { 7 namespace v8 {
8 namespace internal { 8 namespace internal {
9 namespace interpreter { 9 namespace interpreter {
10 10
11 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate) 11 BytecodeArrayBuilder::BytecodeArrayBuilder(Isolate* isolate, Zone* zone)
12 : isolate_(isolate), 12 : isolate_(isolate),
13 bytecodes_(zone),
13 bytecode_generated_(false), 14 bytecode_generated_(false),
15 constants_map_(isolate->heap(), zone),
16 constants_(zone),
14 parameter_count_(-1), 17 parameter_count_(-1),
15 local_register_count_(-1), 18 local_register_count_(-1),
16 temporary_register_count_(0), 19 temporary_register_count_(0),
17 temporary_register_next_(0) {} 20 temporary_register_next_(0) {}
18 21
19 22
20 void BytecodeArrayBuilder::set_locals_count(int number_of_locals) { 23 void BytecodeArrayBuilder::set_locals_count(int number_of_locals) {
21 local_register_count_ = number_of_locals; 24 local_register_count_ = number_of_locals;
22 temporary_register_next_ = local_register_count_; 25 temporary_register_next_ = local_register_count_;
23 } 26 }
(...skipping 17 matching lines...) Expand all
41 } 44 }
42 45
43 46
44 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() { 47 Handle<BytecodeArray> BytecodeArrayBuilder::ToBytecodeArray() {
45 DCHECK_EQ(bytecode_generated_, false); 48 DCHECK_EQ(bytecode_generated_, false);
46 DCHECK_GE(parameter_count_, 0); 49 DCHECK_GE(parameter_count_, 0);
47 DCHECK_GE(local_register_count_, 0); 50 DCHECK_GE(local_register_count_, 0);
48 int bytecode_size = static_cast<int>(bytecodes_.size()); 51 int bytecode_size = static_cast<int>(bytecodes_.size());
49 int register_count = local_register_count_ + temporary_register_count_; 52 int register_count = local_register_count_ + temporary_register_count_;
50 int frame_size = register_count * kPointerSize; 53 int frame_size = register_count * kPointerSize;
54
51 Factory* factory = isolate_->factory(); 55 Factory* factory = isolate_->factory();
56 int constants_count = static_cast<int>(constants_.size());
57 Handle<FixedArray> constant_pool =
58 factory->NewFixedArray(constants_count, TENURED);
59 for (int i = 0; i < constants_count; i++) {
60 constant_pool->set(i, *constants_[i]);
61 }
62
52 Handle<BytecodeArray> output = 63 Handle<BytecodeArray> output =
53 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size, 64 factory->NewBytecodeArray(bytecode_size, &bytecodes_.front(), frame_size,
54 parameter_count_, factory->empty_fixed_array()); 65 parameter_count_, constant_pool);
55 bytecode_generated_ = true; 66 bytecode_generated_ = true;
56 return output; 67 return output;
57 } 68 }
58 69
59 70
60 BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value binop, 71 BytecodeArrayBuilder& BytecodeArrayBuilder::BinaryOperation(Token::Value binop,
61 Register reg) { 72 Register reg) {
62 Output(BytecodeForBinaryOperation(binop), reg.ToOperand()); 73 Output(BytecodeForBinaryOperation(binop), reg.ToOperand());
63 return *this; 74 return *this;
64 } 75 }
65 76
66 77
67 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral( 78 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral(
68 v8::internal::Smi* smi) { 79 v8::internal::Smi* smi) {
69 int32_t raw_smi = smi->value(); 80 int32_t raw_smi = smi->value();
70 if (raw_smi == 0) { 81 if (raw_smi == 0) {
71 Output(Bytecode::kLdaZero); 82 Output(Bytecode::kLdaZero);
72 } else if (raw_smi >= -128 && raw_smi <= 127) { 83 } else if (raw_smi >= -128 && raw_smi <= 127) {
73 Output(Bytecode::kLdaSmi8, static_cast<uint8_t>(raw_smi)); 84 Output(Bytecode::kLdaSmi8, static_cast<uint8_t>(raw_smi));
74 } else { 85 } else {
75 // TODO(oth): Put Smi in constant pool. 86 LoadLiteral(Handle<Object>(smi, isolate_));
87 }
88 return *this;
89 }
90
91
92 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadLiteral(Handle<Object> object) {
93 size_t entry = GetConstantPoolEntry(object);
94 if (entry <= 255) {
95 Output(Bytecode::kLdaConstant, static_cast<uint8_t>(entry));
96 } else {
76 UNIMPLEMENTED(); 97 UNIMPLEMENTED();
77 } 98 }
78 return *this; 99 return *this;
79 } 100 }
80 101
81 102
82 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadUndefined() { 103 BytecodeArrayBuilder& BytecodeArrayBuilder::LoadUndefined() {
83 Output(Bytecode::kLdaUndefined); 104 Output(Bytecode::kLdaUndefined);
84 return *this; 105 return *this;
85 } 106 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 return *this; 143 return *this;
123 } 144 }
124 145
125 146
126 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() { 147 BytecodeArrayBuilder& BytecodeArrayBuilder::Return() {
127 Output(Bytecode::kReturn); 148 Output(Bytecode::kReturn);
128 return *this; 149 return *this;
129 } 150 }
130 151
131 152
153 size_t BytecodeArrayBuilder::GetConstantPoolEntry(Handle<Object> object) {
154 // These constants shouldn't be added to the constant pool, the should use
155 // specialzed bytecodes instead.
156 DCHECK(!object.is_identical_to(isolate_->factory()->undefined_value()));
157 DCHECK(!object.is_identical_to(isolate_->factory()->null_value()));
158 DCHECK(!object.is_identical_to(isolate_->factory()->the_hole_value()));
159 DCHECK(!object.is_identical_to(isolate_->factory()->true_value()));
160 DCHECK(!object.is_identical_to(isolate_->factory()->false_value()));
161
162 size_t* entry = constants_map_.Find(object);
163 if (!entry) {
164 entry = constants_map_.Get(object);
165 *entry = constants_.size();
166 constants_.push_back(object);
167 }
168 DCHECK(constants_[*entry].is_identical_to(object));
169 return *entry;
170 }
171
172
132 int BytecodeArrayBuilder::BorrowTemporaryRegister() { 173 int BytecodeArrayBuilder::BorrowTemporaryRegister() {
133 DCHECK_GE(local_register_count_, 0); 174 DCHECK_GE(local_register_count_, 0);
134 int temporary_reg_index = temporary_register_next_++; 175 int temporary_reg_index = temporary_register_next_++;
135 int count = temporary_register_next_ - local_register_count_; 176 int count = temporary_register_next_ - local_register_count_;
136 if (count > temporary_register_count_) { 177 if (count > temporary_register_count_) {
137 temporary_register_count_ = count; 178 temporary_register_count_ = count;
138 } 179 }
139 return temporary_reg_index; 180 return temporary_reg_index;
140 } 181 }
141 182
142 183
143 void BytecodeArrayBuilder::ReturnTemporaryRegister(int reg_index) { 184 void BytecodeArrayBuilder::ReturnTemporaryRegister(int reg_index) {
144 DCHECK_EQ(reg_index, temporary_register_next_ - 1); 185 DCHECK_EQ(reg_index, temporary_register_next_ - 1);
145 temporary_register_next_ = reg_index; 186 temporary_register_next_ = reg_index;
146 } 187 }
147 188
148 189
149 bool BytecodeArrayBuilder::OperandIsValid(Bytecode bytecode, int operand_index, 190 bool BytecodeArrayBuilder::OperandIsValid(Bytecode bytecode, int operand_index,
150 uint8_t operand_value) const { 191 uint8_t operand_value) const {
151 OperandType operand_type = Bytecodes::GetOperandType(bytecode, operand_index); 192 OperandType operand_type = Bytecodes::GetOperandType(bytecode, operand_index);
152 switch (operand_type) { 193 switch (operand_type) {
153 case OperandType::kNone: 194 case OperandType::kNone:
154 return false; 195 return false;
155 case OperandType::kImm8: 196 case OperandType::kImm8:
156 return true; 197 return true;
198 case OperandType::kIdx:
199 return operand_value < constants_.size();
157 case OperandType::kReg: { 200 case OperandType::kReg: {
158 int reg_index = Register::FromOperand(operand_value).index(); 201 int reg_index = Register::FromOperand(operand_value).index();
159 return (reg_index >= 0 && reg_index < temporary_register_next_) || 202 return (reg_index >= 0 && reg_index < temporary_register_next_) ||
160 (reg_index <= kLastParamRegisterIndex && 203 (reg_index <= kLastParamRegisterIndex &&
161 reg_index > kLastParamRegisterIndex - parameter_count_); 204 reg_index > kLastParamRegisterIndex - parameter_count_);
162 } 205 }
163 } 206 }
164 UNREACHABLE(); 207 UNREACHABLE();
165 return false; 208 return false;
166 } 209 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 280
238 Register TemporaryRegisterScope::NewRegister() { 281 Register TemporaryRegisterScope::NewRegister() {
239 count_++; 282 count_++;
240 last_register_index_ = builder_->BorrowTemporaryRegister(); 283 last_register_index_ = builder_->BorrowTemporaryRegister();
241 return Register(last_register_index_); 284 return Register(last_register_index_);
242 } 285 }
243 286
244 } // namespace interpreter 287 } // namespace interpreter
245 } // namespace internal 288 } // namespace internal
246 } // namespace v8 289 } // namespace v8
OLDNEW
« no previous file with comments | « src/interpreter/bytecode-array-builder.h ('k') | src/interpreter/bytecode-generator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698