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_BYTECODES_H_ | 5 #ifndef V8_INTERPRETER_BYTECODES_H_ |
6 #define V8_INTERPRETER_BYTECODES_H_ | 6 #define V8_INTERPRETER_BYTECODES_H_ |
7 | 7 |
8 #include <iosfwd> | 8 #include <iosfwd> |
9 | 9 |
10 // Clients of this interface shouldn't depend on lots of interpreter internals. | 10 // Clients of this interface shouldn't depend on lots of interpreter internals. |
11 // Do not include anything from src/interpreter here! | 11 // Do not include anything from src/interpreter here! |
12 #include "src/utils.h" | 12 #include "src/utils.h" |
13 | 13 |
14 namespace v8 { | 14 namespace v8 { |
15 namespace internal { | 15 namespace internal { |
16 namespace interpreter { | 16 namespace interpreter { |
17 | 17 |
| 18 // The list of operands used by bytecodes. |
| 19 #define OPERAND_LIST(V) \ |
| 20 V(None) \ |
| 21 V(Imm0) \ |
| 22 V(Imm8) \ |
| 23 V(Smi) \ |
| 24 V(Reg) |
| 25 |
| 26 enum class Operand : int8_t { |
| 27 #define DECLARE_OPERAND(Name) k##Name, |
| 28 OPERAND_LIST(DECLARE_OPERAND) |
| 29 #undef DECLARE_OPERAND |
| 30 #define COUNT_OPERAND(x) +1 |
| 31 // The COUNT_OPERAND macro will turn this into kLast = -1 +1 +1... which will |
| 32 // evaluate to the same value as the last operand. |
| 33 kLast = -1 OPERAND_LIST(COUNT_OPERAND) |
| 34 #undef COUNT_OPERAND |
| 35 }; |
| 36 |
| 37 |
18 // The list of bytecodes which are interpreted by the interpreter. | 38 // The list of bytecodes which are interpreted by the interpreter. |
19 #define BYTECODE_LIST(V) \ | 39 #define BYTECODE_LIST(V) \ |
20 V(LoadLiteral0, 1) \ | 40 V(LoadSmi0) \ |
21 V(Return, 0) | 41 V(LoadSmi8) \ |
| 42 V(LoadSmi) \ |
| 43 V(Move) \ |
| 44 V(Add) \ |
| 45 V(Sub) \ |
| 46 V(Mul) \ |
| 47 V(Div) \ |
| 48 V(Mod) \ |
| 49 V(Return) |
| 50 |
22 | 51 |
23 enum class Bytecode : uint8_t { | 52 enum class Bytecode : uint8_t { |
24 #define DECLARE_BYTECODE(Name, _) k##Name, | 53 #define DECLARE_BYTECODE(Name) k##Name, |
25 BYTECODE_LIST(DECLARE_BYTECODE) | 54 BYTECODE_LIST(DECLARE_BYTECODE) |
26 #undef DECLARE_BYTECODE | 55 #undef DECLARE_BYTECODE |
27 #define COUNT_BYTECODE(x, _) +1 | 56 #define COUNT_BYTECODE(x) +1 |
28 // The COUNT_BYTECODE macro will turn this into kLast = -1 +1 +1... which will | 57 // The COUNT_BYTECODE macro will turn this into kLast = -1 +1 +1... which will |
29 // evaluate to the same value as the last real bytecode. | 58 // evaluate to the same value as the last real bytecode. |
30 kLast = -1 BYTECODE_LIST(COUNT_BYTECODE) | 59 kLast = -1 BYTECODE_LIST(COUNT_BYTECODE) |
31 #undef COUNT_BYTECODE | 60 #undef COUNT_BYTECODE |
32 }; | 61 }; |
33 | 62 |
| 63 |
34 class Bytecodes { | 64 class Bytecodes { |
35 public: | 65 public: |
36 // Returns string representation of |bytecode|. | 66 // Returns string representation of |bytecode|. |
37 static const char* ToString(Bytecode bytecode); | 67 static const char* ToString(Bytecode bytecode); |
38 | 68 |
39 // Returns the number of arguments expected by |bytecode|. | 69 // Converts raw byte value into Bytecode, checking validity in the process. |
40 static const int NumberOfArguments(Bytecode bytecode); | 70 static Bytecode FromByte(uint8_t value); |
| 71 |
| 72 // Returns the number of operands expected by |bytecode|. |
| 73 static const int NumberOfOperands(Bytecode bytecode); |
| 74 |
| 75 // Return the i-th operand of |bytecode|. |
| 76 static const Operand GetOperand(Bytecode bytecode, int i); |
| 77 |
| 78 // Return the size of |operand| in bytes. |
| 79 static const int GetOperandSize(Operand operand); |
41 | 80 |
42 // Returns the size of the bytecode including its arguments. | 81 // Returns the size of the bytecode including its arguments. |
43 static const int Size(Bytecode bytecode); | 82 static const int Size(Bytecode bytecode); |
44 | 83 |
45 // The maximum number of arguments across all bytecodes. | 84 // The maximum number of operands across all bytecodes. |
46 static const int kMaximumNumberOfArguments = 1; | 85 static const int MaximumNumberOfOperands(); |
47 | 86 |
48 // Maximum size of a bytecode and its arguments. | 87 // Maximum size of a bytecode and its arguments. |
49 static const int kMaximumSize = 1 + kMaximumNumberOfArguments; | 88 static const int MaximumSize(); |
50 | 89 |
51 private: | 90 private: |
52 DISALLOW_IMPLICIT_CONSTRUCTORS(Bytecodes); | 91 DISALLOW_IMPLICIT_CONSTRUCTORS(Bytecodes); |
53 }; | 92 }; |
54 | 93 |
55 std::ostream& operator<<(std::ostream& os, const Bytecode& bytecode); | 94 std::ostream& operator<<(std::ostream& os, const Bytecode& bytecode); |
56 | 95 |
57 } // namespace interpreter | 96 } // namespace interpreter |
58 } // namespace internal | 97 } // namespace internal |
59 } // namespace v8 | 98 } // namespace v8 |
60 | 99 |
61 #endif // V8_INTERPRETER_BYTECODES_H_ | 100 #endif // V8_INTERPRETER_BYTECODES_H_ |
OLD | NEW |