OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2012 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. |
| 5 * Copyright 2012, Google Inc. |
| 6 */ |
| 7 |
| 8 #ifndef NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_MIPS_MODEL_H |
| 9 #define NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_MIPS_MODEL_H |
| 10 |
| 11 /* |
| 12 * Models instructions and decode results. |
| 13 * |
| 14 * TODO(petarj): This file and the classes have been based on the same named |
| 15 * module within validator_arm folder. The code has been reduced and changed |
| 16 * slightly to accommodate some style issues. There still may be room for |
| 17 * improvement as we have not investigated performance impact of this classes. |
| 18 * Thus, this should be done later and the code should be revisited. |
| 19 */ |
| 20 |
| 21 #include <stdint.h> |
| 22 |
| 23 namespace nacl_mips_dec { |
| 24 |
| 25 /* |
| 26 * A value class that names a single register. We could use a typedef for this, |
| 27 * but it introduces some ambiguity problems because of the implicit conversion |
| 28 * to/from int. |
| 29 * |
| 30 * May be implicitly converted to a single-entry RegisterList (see below). |
| 31 */ |
| 32 class Register { |
| 33 public: |
| 34 explicit inline Register(uint32_t); |
| 35 |
| 36 /* |
| 37 * Produces the bitmask used to represent this register. |
| 38 */ |
| 39 inline uint32_t Bitmask() const; |
| 40 |
| 41 inline bool Equals(const Register &) const; |
| 42 |
| 43 private: |
| 44 uint32_t _number; |
| 45 }; |
| 46 |
| 47 const Register kRegisterNone(0); |
| 48 |
| 49 // Registers with special meaning in our model: |
| 50 const Register kRegisterStack(29); |
| 51 const Register kRegisterJumpMask(14); // $t6 = holds mask for jr. |
| 52 const Register kRegisterLoadStoreMask(15); // $t7 = holds mask for load/store. |
| 53 const Register kRegisterTls(24); // $t8 = holds tls index. |
| 54 |
| 55 const Register kRegisterZero(0); |
| 56 |
| 57 /* |
| 58 * A collection of Registers. Used to describe the side effects of operations. |
| 59 * |
| 60 * Note that this is technically a set, not a list -- but RegisterSet is a |
| 61 * well-known term that means something else. |
| 62 */ |
| 63 class RegisterList { |
| 64 public: |
| 65 /* |
| 66 * Produces a RegisterList that contains the registers specified in the |
| 67 * given bitmask. To indicate rN, the bitmask must include (1 << N). |
| 68 */ |
| 69 explicit inline RegisterList(uint32_t bitmask); |
| 70 |
| 71 /* |
| 72 * Produces a RegisterList containing a single register. |
| 73 * |
| 74 * Note that this is an implicit constructor. This is okay in this case |
| 75 * because |
| 76 * - It converts between two types that we control, |
| 77 * - It converts at most one step (no implicit conversions to Register), |
| 78 * - It inlines to a single machine instruction, |
| 79 * - The readability gain in inst_classes.cc is large. |
| 80 */ |
| 81 inline RegisterList(const Register r); |
| 82 |
| 83 /* |
| 84 * Checks whether this list contains the given register. |
| 85 */ |
| 86 inline bool operator[](const Register) const; |
| 87 |
| 88 // Checks whether this list contains all the registers in the operand. |
| 89 inline bool ContainsAll(RegisterList) const; |
| 90 |
| 91 // Checks whether this list contains any of the registers in the operand. |
| 92 inline bool ContainsAny(RegisterList) const; |
| 93 |
| 94 inline const RegisterList operator&(const RegisterList) const; |
| 95 |
| 96 private: |
| 97 uint32_t _bits; |
| 98 }; |
| 99 |
| 100 /* |
| 101 * A list containing every possible register, even some we don't define. |
| 102 * Used exclusively as a bogus scary return value for forbidden instructions. |
| 103 */ |
| 104 static const RegisterList kRegisterListEverything = RegisterList(-1); |
| 105 |
| 106 static uint32_t const kReservedRegsBitmask = kRegisterTls.Bitmask() |
| 107 + kRegisterJumpMask.Bitmask() |
| 108 + kRegisterLoadStoreMask.Bitmask(); |
| 109 static RegisterList const kRegListReserved = RegisterList(kReservedRegsBitmask); |
| 110 |
| 111 /* |
| 112 * A 32-bit Mips instruction of unspecified type. |
| 113 * |
| 114 * This class is designed for efficiency: |
| 115 * - Its public methods for bitfield extraction are short and inline. |
| 116 * - It has no vtable, so on 32-bit platforms it's exactly the size of the |
| 117 * instruction it models. |
| 118 */ |
| 119 class Instruction { |
| 120 public: |
| 121 explicit inline Instruction(uint32_t bits); |
| 122 |
| 123 /* |
| 124 * Extracts a range of contiguous bits, right-justifies it, and returns it. |
| 125 * Note that the range follows hardware convention, with the high bit first. |
| 126 */ |
| 127 inline uint32_t Bits(int hi, int lo) const; |
| 128 |
| 129 /* |
| 130 * A convenience method that's exactly equivalent to |
| 131 * Register(instruction.bits(hi, lo)) |
| 132 * |
| 133 * This sequence is quite common in inst_classes.cc. |
| 134 */ |
| 135 inline const Register Reg(int hi, int lo) const; |
| 136 |
| 137 /* |
| 138 * Extracts a single bit (0 - 31). |
| 139 */ |
| 140 inline bool Bit(int index) const; |
| 141 |
| 142 /* |
| 143 * Returns an integer equivalent of this instruction, masked by the given |
| 144 * mask. Used during decoding to test bitfields. |
| 145 */ |
| 146 inline uint32_t operator&(uint32_t) const; |
| 147 |
| 148 private: |
| 149 uint32_t _bits; |
| 150 }; |
| 151 |
| 152 uint32_t const kInstrSize = 4; |
| 153 uint32_t const kInstrAlign = 0xFFFFFFFC; |
| 154 uint32_t const kBundleAlign = 0xFFFFFFF0; |
| 155 |
| 156 } // namespace |
| 157 |
| 158 // Definitions for our inlined functions. |
| 159 #include "native_client/src/trusted/validator_mips/model-inl.h" |
| 160 |
| 161 #endif // NATIVE_CLIENT_SRC_TRUSTED_VALIDATOR_MIPS_MODEL_H |
OLD | NEW |