#include <config.h>
#include "instruction_map.h"
#include <string.h>
#include "addrcache.h"
#include "vcdiff_defs.h"
namespace open_vcdiff {
VCDiffInstructionMap* VCDiffInstructionMap::default_instruction_map = NULL;
VCDiffInstructionMap* VCDiffInstructionMap::GetDefaultInstructionMap() {
if (!default_instruction_map) {
default_instruction_map = new VCDiffInstructionMap(
VCDiffCodeTableData::kDefaultCodeTableData,
VCDiffAddressCache::DefaultLastMode());
}
return default_instruction_map;
}
static unsigned char FindMaxSize(
const unsigned char size_array[VCDiffCodeTableData::kCodeTableSize]) {
unsigned char max_size = size_array[0];
for (int i = 1; i < VCDiffCodeTableData::kCodeTableSize; ++i) {
if (size_array[i] > max_size) {
max_size = size_array[i];
}
}
return max_size;
}
static void ClearSizeOpcodeArray(int length, OpcodeOrNone* array) {
for (int i = 0; i < length; ++i) {
array[i] = kNoOpcode;
}
}
static OpcodeOrNone* NewSizeOpcodeArray(int length) {
OpcodeOrNone* array = new OpcodeOrNone[length];
ClearSizeOpcodeArray(length, array);
return array;
}
VCDiffInstructionMap::FirstInstructionMap::FirstInstructionMap(
int num_insts_and_modes,
int max_size_1)
: num_instruction_type_modes_(num_insts_and_modes),
max_size_1_(max_size_1) {
first_opcodes_ = new OpcodeOrNone*[num_instruction_type_modes_];
for (int i = 0; i < num_instruction_type_modes_; ++i) {
first_opcodes_[i] = NewSizeOpcodeArray(max_size_1_ + 1);
}
}
VCDiffInstructionMap::FirstInstructionMap::~FirstInstructionMap() {
for (int i = 0; i < num_instruction_type_modes_; ++i) {
delete[] first_opcodes_[i];
}
delete[] first_opcodes_;
}
VCDiffInstructionMap::SecondInstructionMap::SecondInstructionMap(
int num_insts_and_modes,
int max_size_2)
: num_instruction_type_modes_(num_insts_and_modes),
max_size_2_(max_size_2) {
memset(second_opcodes_, 0, sizeof(second_opcodes_));
}
VCDiffInstructionMap::SecondInstructionMap::~SecondInstructionMap() {
for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
if (second_opcodes_[opcode] != NULL) {
for (int inst_mode = 0;
inst_mode < num_instruction_type_modes_;
++inst_mode) {
delete[] second_opcodes_[opcode][inst_mode];
}
delete[] second_opcodes_[opcode];
}
}
}
void VCDiffInstructionMap::SecondInstructionMap::Add(
unsigned char first_opcode,
unsigned char inst,
unsigned char size,
unsigned char mode,
unsigned char second_opcode) {
OpcodeOrNone**& inst_mode_array = second_opcodes_[first_opcode];
if (!inst_mode_array) {
inst_mode_array = new OpcodeOrNone*[num_instruction_type_modes_];
memset(inst_mode_array,
0,
num_instruction_type_modes_ * sizeof(inst_mode_array[0]));
}
OpcodeOrNone*& size_array = inst_mode_array[inst + mode];
if (!size_array) {
size_array = NewSizeOpcodeArray(max_size_2_ + 1);
}
if (size_array[size] == kNoOpcode) {
size_array[size] = second_opcode;
}
}
OpcodeOrNone VCDiffInstructionMap::SecondInstructionMap::Lookup(
unsigned char first_opcode,
unsigned char inst,
unsigned char size,
unsigned char mode) const {
if (size > max_size_2_) {
return kNoOpcode;
}
const OpcodeOrNone* const * const inst_mode_array =
second_opcodes_[first_opcode];
if (!inst_mode_array) {
return kNoOpcode;
}
int inst_mode = (inst == VCD_COPY) ? (inst + mode) : inst;
const OpcodeOrNone* const size_array = inst_mode_array[inst_mode];
if (!size_array) {
return kNoOpcode;
}
return size_array[size];
}
VCDiffInstructionMap::VCDiffInstructionMap(
const VCDiffCodeTableData& code_table_data,
unsigned char max_mode)
: first_instruction_map_(VCD_LAST_INSTRUCTION_TYPE + max_mode + 1,
FindMaxSize(code_table_data.size1)),
second_instruction_map_(VCD_LAST_INSTRUCTION_TYPE + max_mode + 1,
FindMaxSize(code_table_data.size2)) {
for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
if (code_table_data.inst2[opcode] == VCD_NOOP) {
first_instruction_map_.Add(code_table_data.inst1[opcode],
code_table_data.size1[opcode],
code_table_data.mode1[opcode],
static_cast<unsigned char>(opcode));
} else if (code_table_data.inst1[opcode] == VCD_NOOP) {
first_instruction_map_.Add(code_table_data.inst2[opcode],
code_table_data.size2[opcode],
code_table_data.mode2[opcode],
static_cast<unsigned char>(opcode));
}
}
for (int opcode = 0; opcode < VCDiffCodeTableData::kCodeTableSize; ++opcode) {
if ((code_table_data.inst1[opcode] != VCD_NOOP) &&
(code_table_data.inst2[opcode] != VCD_NOOP)) {
const OpcodeOrNone single_opcode =
LookupFirstOpcode(code_table_data.inst1[opcode],
code_table_data.size1[opcode],
code_table_data.mode1[opcode]);
if (single_opcode == kNoOpcode) continue; second_instruction_map_.Add(static_cast<unsigned char>(single_opcode),
code_table_data.inst2[opcode],
code_table_data.size2[opcode],
code_table_data.mode2[opcode],
static_cast<unsigned char>(opcode));
}
}
}
};