| 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/interpreter/bytecodes.h" | 11 #include "src/interpreter/bytecodes.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 | 15 |
| 16 class Isolate; | 16 class Isolate; |
| 17 | 17 |
| 18 namespace interpreter { | 18 namespace interpreter { |
| 19 | 19 |
| 20 class Register; |
| 21 |
| 20 class BytecodeArrayBuilder { | 22 class BytecodeArrayBuilder { |
| 21 public: | 23 public: |
| 22 explicit BytecodeArrayBuilder(Isolate* isolate); | 24 explicit BytecodeArrayBuilder(Isolate* isolate); |
| 23 Handle<BytecodeArray> ToBytecodeArray(); | 25 Handle<BytecodeArray> ToBytecodeArray(); |
| 24 | 26 |
| 25 // Set number of locals required for bytecode array. | 27 // Set number of locals required for bytecode array. |
| 26 void set_locals_count(int number_of_locals); | 28 void set_locals_count(int number_of_locals); |
| 27 int locals_count() const; | 29 int locals_count() const; |
| 28 | 30 |
| 29 // Constant loads to accumulator | 31 // Constant loads to accumulator. |
| 30 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); | 32 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
| 31 BytecodeArrayBuilder& LoadUndefined(); | 33 BytecodeArrayBuilder& LoadUndefined(); |
| 32 BytecodeArrayBuilder& LoadNull(); | 34 BytecodeArrayBuilder& LoadNull(); |
| 33 BytecodeArrayBuilder& LoadTheHole(); | 35 BytecodeArrayBuilder& LoadTheHole(); |
| 34 BytecodeArrayBuilder& LoadTrue(); | 36 BytecodeArrayBuilder& LoadTrue(); |
| 35 BytecodeArrayBuilder& LoadFalse(); | 37 BytecodeArrayBuilder& LoadFalse(); |
| 36 | 38 |
| 37 // Register-accumulator transfers | 39 // Register-accumulator transfers. |
| 38 BytecodeArrayBuilder& LoadAccumulatorWithRegister(int reg); | 40 BytecodeArrayBuilder& LoadAccumulatorWithRegister(Register reg); |
| 39 BytecodeArrayBuilder& StoreAccumulatorInRegister(int reg); | 41 BytecodeArrayBuilder& StoreAccumulatorInRegister(Register reg); |
| 40 | 42 |
| 41 // Operators | 43 // Operators. |
| 42 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, int reg); | 44 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, Register reg); |
| 43 | 45 |
| 44 // Flow Control | 46 // Flow Control. |
| 45 BytecodeArrayBuilder& Return(); | 47 BytecodeArrayBuilder& Return(); |
| 46 | 48 |
| 47 private: | 49 private: |
| 48 static Bytecode BytecodeForBinaryOperation(Token::Value op); | 50 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
| 49 | 51 |
| 50 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); | 52 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); |
| 51 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); | 53 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); |
| 52 void Output(Bytecode bytecode, uint8_t r0); | 54 void Output(Bytecode bytecode, uint8_t r0); |
| 53 void Output(Bytecode bytecode); | 55 void Output(Bytecode bytecode); |
| 54 | 56 |
| 55 bool OperandIsValid(Bytecode bytecode, int operand_index, | 57 bool OperandIsValid(Bytecode bytecode, int operand_index, |
| 56 uint8_t operand_value) const; | 58 uint8_t operand_value) const; |
| 57 | 59 |
| 58 int BorrowTemporaryRegister(); | 60 int BorrowTemporaryRegister(); |
| 59 void ReturnTemporaryRegister(int reg); | 61 void ReturnTemporaryRegister(int reg_index); |
| 60 | 62 |
| 61 Isolate* isolate_; | 63 Isolate* isolate_; |
| 62 std::vector<uint8_t> bytecodes_; | 64 std::vector<uint8_t> bytecodes_; |
| 63 bool bytecode_generated_; | 65 bool bytecode_generated_; |
| 64 | 66 |
| 65 int local_register_count_; | 67 int local_register_count_; |
| 66 int temporary_register_count_; | 68 int temporary_register_count_; |
| 67 int temporary_register_next_; | 69 int temporary_register_next_; |
| 68 | 70 |
| 69 friend class TemporaryRegisterScope; | 71 friend class TemporaryRegisterScope; |
| 70 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); | 72 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
| 71 }; | 73 }; |
| 72 | 74 |
| 75 // An interpreter register which is located in the function's regsiter file |
| 76 // in its stack-frame. |
| 77 class Register { |
| 78 public: |
| 79 static const int kMaxRegisterIndex = 128; |
| 80 |
| 81 explicit Register(int index) : index_(index) { |
| 82 DCHECK_GE(index_, 0); |
| 83 DCHECK_LE(index_, kMaxRegisterIndex); |
| 84 } |
| 85 |
| 86 int index() { return index_; } |
| 87 uint8_t ToOperand() { return static_cast<uint8_t>(-index_); } |
| 88 static Register FromOperand(uint8_t operand) { return Register(-operand); } |
| 89 |
| 90 private: |
| 91 void* operator new(size_t size); |
| 92 void operator delete(void* p); |
| 93 |
| 94 int index_; |
| 95 }; |
| 96 |
| 73 // A stack-allocated class than allows the instantiator to allocate | 97 // A stack-allocated class than allows the instantiator to allocate |
| 74 // temporary registers that are cleaned up when scope is closed. | 98 // temporary registers that are cleaned up when scope is closed. |
| 75 class TemporaryRegisterScope { | 99 class TemporaryRegisterScope { |
| 76 public: | 100 public: |
| 77 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); | 101 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
| 78 ~TemporaryRegisterScope(); | 102 ~TemporaryRegisterScope(); |
| 79 int NewRegister(); | 103 Register NewRegister(); |
| 80 | 104 |
| 81 private: | 105 private: |
| 82 void* operator new(size_t size); | 106 void* operator new(size_t size); |
| 83 void operator delete(void* p); | 107 void operator delete(void* p); |
| 84 | 108 |
| 85 BytecodeArrayBuilder* builder_; | 109 BytecodeArrayBuilder* builder_; |
| 86 int count_; | 110 int count_; |
| 87 int register_; | 111 int last_register_index_; |
| 88 | 112 |
| 89 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); | 113 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
| 90 }; | 114 }; |
| 91 | 115 |
| 92 | 116 |
| 93 } // namespace interpreter | 117 } // namespace interpreter |
| 94 } // namespace internal | 118 } // namespace internal |
| 95 } // namespace v8 | 119 } // namespace v8 |
| 96 | 120 |
| 97 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ | 121 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| OLD | NEW |