OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| 6 #define V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "src/ast.h" |
| 11 #include "src/interpreter/bytecodes.h" |
| 12 |
| 13 namespace v8 { |
| 14 namespace internal { |
| 15 |
| 16 class Isolate; |
| 17 |
| 18 namespace interpreter { |
| 19 |
| 20 class BytecodeArrayBuilder { |
| 21 public: |
| 22 explicit BytecodeArrayBuilder(Isolate* isolate); |
| 23 Handle<BytecodeArray> ToBytecodeArray(); |
| 24 |
| 25 // Set number of locals required for bytecode array. |
| 26 void set_locals_count(int number_of_locals); |
| 27 int locals_count() const; |
| 28 |
| 29 // Constant loads to accumulator |
| 30 BytecodeArrayBuilder& LoadLiteral(v8::internal::Smi* value); |
| 31 BytecodeArrayBuilder& LoadUndefined(); |
| 32 BytecodeArrayBuilder& LoadNull(); |
| 33 BytecodeArrayBuilder& LoadTheHole(); |
| 34 BytecodeArrayBuilder& LoadTrue(); |
| 35 BytecodeArrayBuilder& LoadFalse(); |
| 36 |
| 37 // Register-accumulator transfers |
| 38 BytecodeArrayBuilder& LoadAccumulatorWithRegister(int reg); |
| 39 BytecodeArrayBuilder& StoreAccumulatorInRegister(int reg); |
| 40 |
| 41 // Operators |
| 42 BytecodeArrayBuilder& BinaryOperation(Token::Value binop, int reg); |
| 43 |
| 44 // Flow Control |
| 45 BytecodeArrayBuilder& Return(); |
| 46 |
| 47 private: |
| 48 static Bytecode BytecodeForBinaryOperation(Token::Value op); |
| 49 |
| 50 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1, uint8_t r2); |
| 51 void Output(Bytecode bytecode, uint8_t r0, uint8_t r1); |
| 52 void Output(Bytecode bytecode, uint8_t r0); |
| 53 void Output(Bytecode bytecode); |
| 54 |
| 55 bool OperandIsValid(Bytecode bytecode, int operand_index, |
| 56 uint8_t operand_value) const; |
| 57 |
| 58 int BorrowTemporaryRegister(); |
| 59 void ReturnTemporaryRegister(int reg); |
| 60 |
| 61 Isolate* isolate_; |
| 62 std::vector<uint8_t> bytecodes_; |
| 63 bool bytecode_generated_; |
| 64 |
| 65 int local_register_count_; |
| 66 int temporary_register_count_; |
| 67 int temporary_register_next_; |
| 68 |
| 69 friend class TemporaryRegisterScope; |
| 70 DISALLOW_IMPLICIT_CONSTRUCTORS(BytecodeArrayBuilder); |
| 71 }; |
| 72 |
| 73 // A stack-allocated class than allows the instantiator to allocate |
| 74 // temporary registers that are cleaned up when scope is closed. |
| 75 class TemporaryRegisterScope { |
| 76 public: |
| 77 explicit TemporaryRegisterScope(BytecodeArrayBuilder* builder); |
| 78 ~TemporaryRegisterScope(); |
| 79 int NewRegister(); |
| 80 |
| 81 private: |
| 82 void* operator new(size_t size); |
| 83 void operator delete(void* p); |
| 84 |
| 85 BytecodeArrayBuilder* builder_; |
| 86 int count_; |
| 87 int register_; |
| 88 |
| 89 DISALLOW_COPY_AND_ASSIGN(TemporaryRegisterScope); |
| 90 }; |
| 91 |
| 92 |
| 93 } // namespace interpreter |
| 94 } // namespace internal |
| 95 } // namespace v8 |
| 96 |
| 97 #endif // V8_INTERPRETER_BYTECODE_ARRAY_BUILDER_H_ |
OLD | NEW |