tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Decoder-vixl.h (10624B)


      1 // Copyright 2014, VIXL authors
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are met:
      6 //
      7 //   * Redistributions of source code must retain the above copyright notice,
      8 //     this list of conditions and the following disclaimer.
      9 //   * Redistributions in binary form must reproduce the above copyright notice,
     10 //     this list of conditions and the following disclaimer in the documentation
     11 //     and/or other materials provided with the distribution.
     12 //   * Neither the name of ARM Limited nor the names of its contributors may be
     13 //     used to endorse or promote products derived from this software without
     14 //     specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
     17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
     20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 
     27 #ifndef VIXL_A64_DECODER_A64_H_
     28 #define VIXL_A64_DECODER_A64_H_
     29 
     30 #include "mozilla/Vector.h"
     31 
     32 #include "jit/arm64/vixl/Globals-vixl.h"
     33 #include "jit/arm64/vixl/Instructions-vixl.h"
     34 #include "js/AllocPolicy.h"
     35 
     36 
     37 // List macro containing all visitors needed by the decoder class.
     38 
     39 #define VISITOR_LIST_THAT_RETURN(V)     \
     40  V(PCRelAddressing)                    \
     41  V(AddSubImmediate)                    \
     42  V(MaxMinImmediate)                    \
     43  V(LogicalImmediate)                   \
     44  V(MoveWideImmediate)                  \
     45  V(AtomicMemory)                       \
     46  V(Bitfield)                           \
     47  V(Extract)                            \
     48  V(UnconditionalBranch)                \
     49  V(UnconditionalBranchToRegister)      \
     50  V(CompareBranch)                      \
     51  V(TestBranch)                         \
     52  V(ConditionalBranch)                  \
     53  V(System)                             \
     54  V(Exception)                          \
     55  V(LoadStorePairPostIndex)             \
     56  V(LoadStorePairOffset)                \
     57  V(LoadStorePairPreIndex)              \
     58  V(LoadStorePairNonTemporal)           \
     59  V(LoadLiteral)                        \
     60  V(LoadStoreUnscaledOffset)            \
     61  V(LoadStorePostIndex)                 \
     62  V(LoadStorePreIndex)                  \
     63  V(LoadStoreRegisterOffset)            \
     64  V(LoadStoreUnsignedOffset)            \
     65  V(LoadStoreExclusive)                 \
     66  V(LogicalShifted)                     \
     67  V(AddSubShifted)                      \
     68  V(AddSubExtended)                     \
     69  V(AddSubWithCarry)                    \
     70  V(ConditionalCompareRegister)         \
     71  V(ConditionalCompareImmediate)        \
     72  V(ConditionalSelect)                  \
     73  V(DataProcessing1Source)              \
     74  V(DataProcessing2Source)              \
     75  V(DataProcessing3Source)              \
     76  V(FPCompare)                          \
     77  V(FPConditionalCompare)               \
     78  V(FPConditionalSelect)                \
     79  V(FPImmediate)                        \
     80  V(FPDataProcessing1Source)            \
     81  V(FPDataProcessing2Source)            \
     82  V(FPDataProcessing3Source)            \
     83  V(FPIntegerConvert)                   \
     84  V(FPFixedPointConvert)                \
     85  V(Crypto2RegSHA)                      \
     86  V(Crypto3RegSHA)                      \
     87  V(CryptoAES)                          \
     88  V(NEON2RegMisc)                       \
     89  V(NEON3Different)                     \
     90  V(NEON3Same)                          \
     91  V(NEONAcrossLanes)                    \
     92  V(NEONByIndexedElement)               \
     93  V(NEONCopy)                           \
     94  V(NEONExtract)                        \
     95  V(NEONLoadStoreMultiStruct)           \
     96  V(NEONLoadStoreMultiStructPostIndex)  \
     97  V(NEONLoadStoreSingleStruct)          \
     98  V(NEONLoadStoreSingleStructPostIndex) \
     99  V(NEONModifiedImmediate)              \
    100  V(NEONScalar2RegMisc)                 \
    101  V(NEONScalar3Diff)                    \
    102  V(NEONScalar3Same)                    \
    103  V(NEONScalarByIndexedElement)         \
    104  V(NEONScalarCopy)                     \
    105  V(NEONScalarPairwise)                 \
    106  V(NEONScalarShiftImmediate)           \
    107  V(NEONShiftImmediate)                 \
    108  V(NEONTable)                          \
    109  V(NEONPerm)
    110 
    111 #define VISITOR_LIST_THAT_DONT_RETURN(V)  \
    112  V(Unallocated)                          \
    113  V(Unimplemented)                        \
    114 
    115 #define VISITOR_LIST(V)             \
    116  VISITOR_LIST_THAT_RETURN(V)       \
    117  VISITOR_LIST_THAT_DONT_RETURN(V)  \
    118 
    119 namespace vixl {
    120 
    121 // The Visitor interface. Disassembler and simulator (and other tools)
    122 // must provide implementations for all of these functions.
    123 class DecoderVisitor {
    124 public:
    125  enum VisitorConstness {
    126    kConstVisitor,
    127    kNonConstVisitor
    128  };
    129  explicit DecoderVisitor(VisitorConstness constness = kConstVisitor)
    130      : constness_(constness) {}
    131 
    132  virtual ~DecoderVisitor() {}
    133 
    134  #define DECLARE(A) virtual void Visit##A(const Instruction* instr) = 0;
    135  VISITOR_LIST(DECLARE)
    136  #undef DECLARE
    137 
    138  bool IsConstVisitor() const { return constness_ == kConstVisitor; }
    139  Instruction* MutableInstruction(const Instruction* instr) {
    140    VIXL_ASSERT(!IsConstVisitor());
    141    return const_cast<Instruction*>(instr);
    142  }
    143 
    144 private:
    145  const VisitorConstness constness_;
    146 };
    147 
    148 
    149 class Decoder {
    150 public:
    151  Decoder() {}
    152 
    153  // Top-level wrappers around the actual decoding function.
    154  void Decode(const Instruction* instr) {
    155 #ifdef DEBUG
    156    for (auto visitor : visitors_) {
    157      VIXL_ASSERT(visitor->IsConstVisitor());
    158    }
    159 #endif
    160    DecodeInstruction(instr);
    161  }
    162  void Decode(Instruction* instr) {
    163    DecodeInstruction(const_cast<const Instruction*>(instr));
    164  }
    165 
    166  // Register a new visitor class with the decoder.
    167  // Decode() will call the corresponding visitor method from all registered
    168  // visitor classes when decoding reaches the leaf node of the instruction
    169  // decode tree.
    170  // Visitors are called in order.
    171  // A visitor can be registered multiple times.
    172  //
    173  //   d.AppendVisitor(V1);
    174  //   d.AppendVisitor(V2);
    175  //   d.PrependVisitor(V2);
    176  //   d.AppendVisitor(V3);
    177  //
    178  //   d.Decode(i);
    179  //
    180  // will call in order visitor methods in V2, V1, V2, V3.
    181  void AppendVisitor(DecoderVisitor* visitor);
    182  void PrependVisitor(DecoderVisitor* visitor);
    183  // These helpers register `new_visitor` before or after the first instance of
    184  // `registered_visiter` in the list.
    185  // So if
    186  //   V1, V2, V1, V2
    187  // are registered in this order in the decoder, calls to
    188  //   d.InsertVisitorAfter(V3, V1);
    189  //   d.InsertVisitorBefore(V4, V2);
    190  // will yield the order
    191  //   V1, V3, V4, V2, V1, V2
    192  //
    193  // For more complex modifications of the order of registered visitors, one can
    194  // directly access and modify the list of visitors via the `visitors()'
    195  // accessor.
    196  void InsertVisitorBefore(DecoderVisitor* new_visitor,
    197                           DecoderVisitor* registered_visitor);
    198  void InsertVisitorAfter(DecoderVisitor* new_visitor,
    199                          DecoderVisitor* registered_visitor);
    200 
    201  // Remove all instances of a previously registered visitor class from the list
    202  // of visitors stored by the decoder.
    203  void RemoveVisitor(DecoderVisitor* visitor);
    204 
    205  #define DECLARE(A) void Visit##A(const Instruction* instr);
    206  VISITOR_LIST(DECLARE)
    207  #undef DECLARE
    208 
    209 
    210 private:
    211  // Decodes an instruction and calls the visitor functions registered with the
    212  // Decoder class.
    213  void DecodeInstruction(const Instruction* instr);
    214 
    215  // Decode the PC relative addressing instruction, and call the corresponding
    216  // visitors.
    217  // On entry, instruction bits 27:24 = 0x0.
    218  void DecodePCRelAddressing(const Instruction* instr);
    219 
    220  // Decode the add/subtract immediate instruction, and call the correspoding
    221  // visitors.
    222  // On entry, instruction bits 27:24 = 0x1.
    223  void DecodeAddSubImmediate(const Instruction* instr);
    224 
    225  // Decode the branch, system command, and exception generation parts of
    226  // the instruction tree, and call the corresponding visitors.
    227  // On entry, instruction bits 27:24 = {0x4, 0x5, 0x6, 0x7}.
    228  void DecodeBranchSystemException(const Instruction* instr);
    229 
    230  // Decode the load and store parts of the instruction tree, and call
    231  // the corresponding visitors.
    232  // On entry, instruction bits 27:24 = {0x8, 0x9, 0xC, 0xD}.
    233  void DecodeLoadStore(const Instruction* instr);
    234 
    235  // Decode the logical immediate and move wide immediate parts of the
    236  // instruction tree, and call the corresponding visitors.
    237  // On entry, instruction bits 27:24 = 0x2.
    238  void DecodeLogical(const Instruction* instr);
    239 
    240  // Decode the bitfield and extraction parts of the instruction tree,
    241  // and call the corresponding visitors.
    242  // On entry, instruction bits 27:24 = 0x3.
    243  void DecodeBitfieldExtract(const Instruction* instr);
    244 
    245  // Decode the data processing parts of the instruction tree, and call the
    246  // corresponding visitors.
    247  // On entry, instruction bits 27:24 = {0x1, 0xA, 0xB}.
    248  void DecodeDataProcessing(const Instruction* instr);
    249 
    250  // Decode the floating point parts of the instruction tree, and call the
    251  // corresponding visitors.
    252  // On entry, instruction bits 27:24 = {0xE, 0xF}.
    253  void DecodeFP(const Instruction* instr);
    254 
    255  // Decode the Advanced SIMD (NEON) load/store part of the instruction tree,
    256  // and call the corresponding visitors.
    257  // On entry, instruction bits 29:25 = 0x6.
    258  void DecodeNEONLoadStore(const Instruction* instr);
    259 
    260  // Decode the Advanced SIMD (NEON) vector data processing part of the
    261  // instruction tree, and call the corresponding visitors.
    262  // On entry, instruction bits 28:25 = 0x7.
    263  void DecodeNEONVectorDataProcessing(const Instruction* instr);
    264 
    265  // Decode the Advanced SIMD (NEON) scalar data processing part of the
    266  // instruction tree, and call the corresponding visitors.
    267  // On entry, instruction bits 28:25 = 0xF.
    268  void DecodeNEONScalarDataProcessing(const Instruction* instr);
    269 
    270 private:
    271  // Visitors are registered in a list.
    272  mozilla::Vector<DecoderVisitor*, 8, js::SystemAllocPolicy> visitors_;
    273 };
    274 
    275 }  // namespace vixl
    276 
    277 #endif  // VIXL_A64_DECODER_A64_H_