Home | History | Annotate | Download | only in src
      1 // Copyright 2008 Google Inc.
      2 // Author: Lincoln Smith
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 
     16 #include <config.h>
     17 #include "instruction_map.h"
     18 #include <string.h>  // memset
     19 #include "addrcache.h"
     20 #include "vcdiff_defs.h"
     21 
     22 namespace open_vcdiff {
     23 
     24 // VCDiffInstructionMap members and methods
     25 
     26 VCDiffInstructionMap* VCDiffInstructionMap::default_instruction_map = NULL;
     27 
     28 VCDiffInstructionMap* VCDiffInstructionMap::GetDefaultInstructionMap() {
     29   if (!default_instruction_map) {
     30     default_instruction_map = new VCDiffInstructionMap(
     31         VCDiffCodeTableData::kDefaultCodeTableData,
     32         VCDiffAddressCache::DefaultLastMode());
     33   }
     34   return default_instruction_map;
     35 }
     36 
     37 static unsigned char FindMaxSize(
     38     const unsigned char size_array[VCDiffCodeTableData::kCodeTableSize]) {
     39   unsigned char max_size = size_array[0];
     40   for (int i = 1; i < VCDiffCodeTableData::kCodeTableSize; ++i) {
     41     if (size_array[i] > max_size) {
     42       max_size = size_array[i];
     43     }
     44   }
     45   return max_size;
     46 }
     47 
     48 static void ClearSizeOpcodeArray(int length, OpcodeOrNone* array) {
     49   for (int i = 0; i < length; ++i) {
     50     array[i] = kNoOpcode;
     51   }
     52 }
     53 
     54 static OpcodeOrNone* NewSizeOpcodeArray(int length) {
     55   OpcodeOrNone* array = new OpcodeOrNone[length];
     56   ClearSizeOpcodeArray(length, array);
     57   return array;
     58 }
     59 
     60 VCDiffInstructionMap::FirstInstructionMap::FirstInstructionMap(
     61     int num_insts_and_modes,
     62     int max_size_1)
     63     : num_instruction_type_modes_(num_insts_and_modes),
     64       max_size_1_(max_size_1) {
     65   first_opcodes_ = new OpcodeOrNone*[num_instruction_type_modes_];
     66   for (int i = 0; i < num_instruction_type_modes_; ++i) {
     67     // There must be at least (max_size_1_ + 1) elements in first_opcodes_
     68     // because the element first_opcodes[max_size_1_] will be referenced.
     69     first_opcodes_[i] = NewSizeOpcodeArray(max_size_1_ + 1);
     70   }
     71 }
     72 
     73 VCDiffInstructionMap::FirstInstructionMap::~FirstInstructionMap() {
     74   for (int i = 0; i < num_instruction_type_modes_; ++i) {
     75     delete[] first_opcodes_[i];
     76   }
     77   delete[] first_opcodes_;
     78 }
     79 
     80 VCDiffInstructionMap::SecondInstructionMap::SecondInstructionMap(
     81     int num_insts_and_modes,
     82     int max_size_2)
     83     : num_instruction_type_modes_(num_insts_and_modes),
     84       max_size_2_(max_size_2) {
     85   memset(second_opcodes_, 0, sizeof(second_opcodes_));
     86 }
     87 
     88 
     89 VCDiffInstructionMap::SecondInstructionMap::~SecondInstructionMap() {
     90   for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
     91     if (second_opcodes_[opcode] != NULL) {
     92       for (int inst_mode = 0;
     93            inst_mode < num_instruction_type_modes_;
     94            ++inst_mode) {
     95         // No need to check for NULL
     96         delete[] second_opcodes_[opcode][inst_mode];
     97       }
     98       delete[] second_opcodes_[opcode];
     99     }
    100   }
    101 }
    102 
    103 void VCDiffInstructionMap::SecondInstructionMap::Add(
    104     unsigned char first_opcode,
    105     unsigned char inst,
    106     unsigned char size,
    107     unsigned char mode,
    108     unsigned char second_opcode) {
    109   OpcodeOrNone**& inst_mode_array = second_opcodes_[first_opcode];
    110   if (!inst_mode_array) {
    111     inst_mode_array = new OpcodeOrNone*[num_instruction_type_modes_];
    112     memset(inst_mode_array,
    113            0,
    114            num_instruction_type_modes_ * sizeof(inst_mode_array[0]));
    115   }
    116   OpcodeOrNone*& size_array = inst_mode_array[inst + mode];
    117   if (!size_array) {
    118     // There must be at least (max_size_2_ + 1) elements in size_array
    119     // because the element size_array[max_size_2_] will be referenced.
    120     size_array = NewSizeOpcodeArray(max_size_2_ + 1);
    121   }
    122   if (size_array[size] == kNoOpcode) {
    123     size_array[size] = second_opcode;
    124   }
    125 }
    126 
    127 OpcodeOrNone VCDiffInstructionMap::SecondInstructionMap::Lookup(
    128     unsigned char first_opcode,
    129     unsigned char inst,
    130     unsigned char size,
    131     unsigned char mode) const {
    132   if (size > max_size_2_) {
    133     return kNoOpcode;
    134   }
    135   const OpcodeOrNone* const * const inst_mode_array =
    136     second_opcodes_[first_opcode];
    137   if (!inst_mode_array) {
    138     return kNoOpcode;
    139   }
    140   int inst_mode = (inst == VCD_COPY) ? (inst + mode) : inst;
    141   const OpcodeOrNone* const size_array = inst_mode_array[inst_mode];
    142   if (!size_array) {
    143     return kNoOpcode;
    144   }
    145   return size_array[size];
    146 }
    147 
    148 // Because a constructor should never fail, the caller must already
    149 // have run ValidateCodeTable() against the code table data.
    150 //
    151 VCDiffInstructionMap::VCDiffInstructionMap(
    152     const VCDiffCodeTableData& code_table_data,
    153     unsigned char max_mode)
    154     : first_instruction_map_(VCD_LAST_INSTRUCTION_TYPE + max_mode + 1,
    155                              FindMaxSize(code_table_data.size1)),
    156       second_instruction_map_(VCD_LAST_INSTRUCTION_TYPE + max_mode + 1,
    157                               FindMaxSize(code_table_data.size2)) {
    158   // First pass to fill up first_instruction_map_
    159   for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
    160     if (code_table_data.inst2[opcode] == VCD_NOOP) {
    161       // Single instruction.  If there is more than one opcode for the same
    162       // inst, mode, and size, then the lowest-numbered opcode will always
    163       // be used by the encoder, because of the descending loop.
    164       first_instruction_map_.Add(code_table_data.inst1[opcode],
    165                                  code_table_data.size1[opcode],
    166                                  code_table_data.mode1[opcode],
    167                                  opcode);
    168     } else if (code_table_data.inst1[opcode] == VCD_NOOP) {
    169       // An unusual case where inst1 == NOOP and inst2 == ADD, RUN, or COPY.
    170       // This is valid under the standard, but unlikely to be used.
    171       // Add it to the first instruction map as if inst1 and inst2 were swapped.
    172       first_instruction_map_.Add(code_table_data.inst2[opcode],
    173                                  code_table_data.size2[opcode],
    174                                  code_table_data.mode2[opcode],
    175                                  opcode);
    176     }
    177   }
    178   // Second pass to fill up second_instruction_map_ (depends on first pass)
    179   for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
    180     if ((code_table_data.inst1[opcode] != VCD_NOOP) &&
    181         (code_table_data.inst2[opcode] != VCD_NOOP)) {
    182       // Double instruction.  Find the corresponding single instruction opcode
    183       const OpcodeOrNone single_opcode =
    184           LookupFirstOpcode(code_table_data.inst1[opcode],
    185                             code_table_data.size1[opcode],
    186                             code_table_data.mode1[opcode]);
    187       if (single_opcode == kNoOpcode) continue;  // No single opcode found
    188       second_instruction_map_.Add(static_cast<unsigned char>(single_opcode),
    189                                   code_table_data.inst2[opcode],
    190                                   code_table_data.size2[opcode],
    191                                   code_table_data.mode2[opcode],
    192                                   opcode);
    193     }
    194   }
    195 }
    196 
    197 };  // namespace open_vcdiff
    198