1 #include "exec_trail.hpp"
2 
3 #include <iostream>
4 using namespace std;
5 
6 /*** COpcodeMap implementation ***/
7 
COpcodeMap()8 COpcodeMap::COpcodeMap() {
9 	makeopmap();
10 }
11 
opmap(field_t field) const12 unsigned COpcodeMap::opmap(field_t field) const {
13 	return _opmap[field];
14 }
15 
mapop(OPCODE op,OPTYPE type)16 void COpcodeMap::mapop(OPCODE op,OPTYPE type) {
17 	for(unsigned i=0; i<MODIFIER_LAST; i++) {
18 		_opmap[_OP(op,(MODIFIER)i)] = type;
19 	}
20 }
21 
mapop(OPCODE op,MODIFIER mod,OPTYPE type)22 void COpcodeMap::mapop(OPCODE op,MODIFIER mod,OPTYPE type) {
23 	_opmap[_OP(op,mod)] = type;
24 }
25 
makeopmap()26 void COpcodeMap::makeopmap() {
27 	mapop(DAT,OTHER);
28 	mapop(SPL,SPLIT);
29 	mapop(MOV,MATH);
30 	mapop(DJN,BRANCH);
31 	mapop(ADD,MATH);
32 	mapop(JMZ,BRANCH);
33 	mapop(SUB,MATH);
34 	mapop(SEQ,BRANCH);
35 	mapop(SNE,BRANCH);
36 	mapop(SLT,BRANCH);
37 	mapop(JMN,BRANCH);
38 	mapop(JMP,BRANCH);
39 	mapop(NOP,OTHER);
40 	mapop(MUL,MATH);
41 	mapop(MODM,MATH);
42 	mapop(DIV,MATH);
43 	mapop(LDP,OTHER);
44 	mapop(STP,OTHER);
45 	mapop(MOV,mI,MOVI); // special case
46 }
47 
48 /*** CExecTrail implementation ***/
49 
CExecTrail(COpcodeMap * map)50 CExecTrail::CExecTrail(COpcodeMap *map): _map(map) {
51 	_exec_count = new dist_t*[_map->segments()];
52 	for(unsigned seg=0; seg<_map->segments(); seg++) {
53 		_exec_count[seg] = new dist_t[_map->optypes()];
54 		for(unsigned op=0; op<_map->optypes(); op++) {
55 			_exec_count[seg][op] = ZERO;
56 		}
57 	}
58 }
59 
~CExecTrail()60 CExecTrail::~CExecTrail() {
61 	for(unsigned seg=0; seg<_map->segments(); seg++) {
62 		delete[] _exec_count[seg];
63 	}
64 	delete[] _exec_count;
65 }
66 
clear()67 void CExecTrail::clear() {
68 	for(unsigned seg=0; seg<_map->segments(); seg++) {
69 		for(unsigned op=0; op<_map->optypes(); op++) {
70 			_exec_count[seg][op] = ZERO;
71 		}
72 	}
73 }
74 
set(unsigned const segment,CExecTrail::dist_t table[MAX_OPCODES])75 void CExecTrail::set(unsigned const segment,CExecTrail::dist_t table[MAX_OPCODES]) { // table[MAX_OPCODES]
76 	clear();
77 	for(unsigned op=0; op<_map->optypes(); op++) {
78 		_exec_count[segment][op] = ZERO;
79 	}
80 	for(field_t op=0; op<MAX_OPCODES; op++) {
81 		_exec_count[segment][_map->opmap(op)] += table[op];
82 	}
83 }
84 
distance_squared(CExecTrail const * other) const85 CExecTrail::dist_t CExecTrail::distance_squared(CExecTrail const *other) const {
86 	dist_t ret = ZERO;
87 	for(unsigned seg=0; seg<_map->segments(); seg++) {
88 		for(unsigned op=0; op<_map->optypes(); op++) {
89 			dist_t d = _exec_count[seg][op] - other->_exec_count[seg][op];
90 			ret += (d * d);
91 		}
92 	}
93 	return ret;
94 }
95 
max_distance_from() const96 CExecTrail::dist_t CExecTrail::max_distance_from() const {
97 	dist_t ret = ZERO;
98 	for(unsigned seg=0; seg<_map->segments(); seg++) {
99 		for(unsigned op=0; op<_map->optypes(); op++) {
100 			dist_t d = _exec_count[seg][op];
101 			if(d < (_map->segsize() >> 1)) // invert?
102 				d = _map->segsize() - d;
103 			ret += (d * d);
104 		}
105 	}
106 	return ret;
107 }
108 
load_from_file(const char * filename)109 bool CExecTrail::load_from_file(const char *filename) {  // returns success
110 	_filename = filename;
111 	bool ret = false;
112 	clear();
113 	dist_t entry[MAX_OPCODES];
114 	// load the file..
115 	FILE *f = fopen(filename,"rb");
116 	if(f) {
117 		for(unsigned seg=0; seg<_map->segments(); seg++) {
118 			if(1 == fread(entry,sizeof(entry),1,f)) {
119 				set(seg,entry);
120 				ret = true;
121 			} else {
122 				cerr << "could not read from \"" << filename << "\" when loading the exec trail" << endl;
123 				ret = false;
124 				break;
125 			}
126 		}
127 		// done
128 		fclose(f);
129 	} else {
130 		cerr << "could not open \"" << filename << "\" to load the exec trail" << endl;
131 	}
132 	return ret;
133 }
134 
135