-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.cpp
More file actions
225 lines (204 loc) · 7.91 KB
/
Copy pathexecutor.cpp
File metadata and controls
225 lines (204 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//
// Created by michal on 09.09.17.
//
#include <algorithm>
#include <memory>
#include <cstring>
#include <thread>
#include "executor.h"
#include "function.h"
#include "thread_info.h"
std::vector<std::shared_ptr<Function>> functions;
std::vector<std::thread> threads;
const static int ORDER = 7;
void Executor::process_instruction() {
int64_t constant_arg = 0;
Label *label = nullptr;
std::vector<std::shared_ptr<Argument>> arguments;
find_instruction();
auto params_number = instruction.param_list.length();
for (int i = 0; i < params_number; i++) {
if (instruction.param_list[i] == 'R') {
find_argument();
if (parameters[i].size() == 4) {
auto argument = create_register_argument(parameters[i]);
arguments.push_back(std::move(argument));
}
if (parameters[i].size() == 6) {
auto argument = create_memory_argument(parameters[i]);
arguments.push_back(std::move(argument));
}
}
if (instruction.param_list[i] == 'C') {
find_const_value();
constant_arg = (int64_t) std::stoull(parameters[i], nullptr, 2);
}
if (instruction.param_list[i] == 'L') {
find_label_address();
auto label_addr = (uint32_t) std::stol(parameters[i], nullptr, 2);
label = new Label(label_addr, get_actual_bit());
}
}
if (functions.empty())
std::cout << "Instruction: " << instruction.name << " at: " << std::dec << actual_byte * 8 + actual_bit
<< std::endl;
execution(constant_arg, *label, arguments);
for (std::shared_ptr<Argument> &argument : arguments) {
if (argument.get()->arg_type == RegisterType) {
if (functions.empty())
std::cout << "Reg ID: " << argument.get()->reg_id << " value: " << argument.get()->value << std::endl;
vm_registers[argument.get()->reg_id].get()->update_register_value(argument.get()->value);
}
if (argument.get()->arg_type == MemoryType) {
if (functions.empty())
std::cout << "Memory offset: " << argument.get()->offset << " data type: " << argument.get()->data_type
<< " value: " << argument.get()->value << std::endl;
memory.update(argument.get()->value, (uint64_t) argument.get()->offset, argument.get()->data_type);
}
}
if (label != nullptr) {
bool if_jump = label->get_if_jump();
if (if_jump) {
if (functions.empty())
std::cout << "Jump address: " << std::dec << label->get_jump_address() << std::endl;
set_actual_bit(label->get_jump_address());
}
bool if_function = label->get_if_function();
if (if_function) {
std::shared_ptr<Function> function{
new Function(*this, label->get_jump_address(), static_cast<uint32_t>(label->get_call_address()))};
functions.push_back(function);
function->set_executed_bit(label->get_jump_address());
do {
function->process_function();
} while (!function->check_function_end());
set_actual_bit(label->get_call_address());
functions.pop_back();
instruction.if_return = false;
}
bool if_thread = label->get_if_thread();
if (if_thread) {
Executor *subexecutor = new Executor(this->buffer_ref, this->vm_registers, this->memory);
subexecutor->set_actual_bit(label->get_jump_address());
ThreadInfo thread_info(std::move(*subexecutor), static_cast<uint32_t>(label->get_jump_address()));
thread_info.set_executed_bit(label->get_jump_address());
threads_number++;
threads.emplace_back(std::move(thread_info));
auto thread_id = threads.back().get_id();
vm_registers[label->get_thread_id_reg()].get()->value = static_cast<int64_t>(hasher(thread_id));
}
delete label;
}
if (instruction.if_return) {
functions.back()->set_function_end(true);
}
if (instruction.if_join) {
int64_t hash_thread_id = vm_registers[arguments.back()->reg_id]->value;
for (std::thread &thread : threads) {
if (hasher(threads.back().get_id()) == hash_thread_id) {
// threads.back().join();
while(1) {
}
}
}
}
}
void Executor::execution(int64_t constant_arg, Label &label, std::vector<std::shared_ptr<Argument>> arguments) {
instruction.implementation(constant_arg, label, std::move(arguments));
}
int Executor::find_instruction() {
bool found = false;
std::string opcode;
std::vector<Instruction>::iterator instruction_iterator;
auto run_instruction = [&opcode](
const Instruction &ins) {
return ins.opcode == opcode;
};
do {
int bit = read_bit();
opcode.push_back((char) (bit + '0'));
instruction_iterator = std::find_if(std::begin(instructions), std::end(instructions), run_instruction);
found = (instruction_iterator != std::end(instructions));
} while (!found);
instruction = *instruction_iterator;
return 0;
}
std::shared_ptr<Argument> Executor::create_register_argument(std::string &raw_parameter) {
std::string raw_reg_id = raw_parameter;
int vec_id = reg_id_to_vector_id(raw_reg_id);
std::shared_ptr<Argument> argument{new Argument(vec_id, vm_registers[vec_id].get()->value)};
return argument;
}
std::shared_ptr<Argument> Executor::create_memory_argument(std::string &raw_parameter) {
std::string raw_data_type = std::string(raw_parameter.begin(), raw_parameter.begin() + 2);
std::string raw_reg_id = std::string(raw_parameter.begin() + 2, raw_parameter.begin() + 6);
DataType data_type = (DataType) id_to_data_type(raw_data_type);
int reg_id = reg_id_to_vector_id(raw_reg_id);
auto reg_value = (int64_t) vm_registers[reg_id].get()->value;
std::vector<unsigned char> memory_argument = memory.access_data(reg_value, data_type);
int64_t memory_value = memory.memory_to_int(memory_argument);
std::shared_ptr<Argument> argument{new Argument(data_type, reg_value, memory_value)};
return argument;
}
void Executor::find_label_address() {
std::string label_address;
for (int i = 0; i < 32; i++) {
int bit = read_bit();
label_address.push_back((char) (bit + '0'));
}
std::reverse(label_address.begin(), label_address.end());
parameters.push_back(label_address);
}
void Executor::find_const_value() {
std::string const_value;
for (int i = 0; i < 64; i++) {
int bit = read_bit();
const_value.push_back((char) (bit + '0'));
}
std::reverse(const_value.begin(), const_value.end());
parameters.push_back(const_value);
}
void Executor::find_argument() {
int bit = read_bit();
std::string register_index;
if (bit == 0) {
for (int i = 0; i < 4; i++) {
bit = read_bit();
register_index.push_back((char) (bit + '0'));
}
} else if (bit == 1) {
for (int i = 0; i < 6; i++) {
bit = read_bit();
register_index.push_back((char) (bit + '0'));
}
}
parameters.push_back(register_index);
}
/*
* Reads actual value of bit and moves to next
*/
int Executor::read_bit() {
int shift_value = ORDER - actual_bit;
int mask = 1 << shift_value;
int masked_byte = buffer_ref[actual_byte] & mask;
int bit = masked_byte >> shift_value;
if (actual_bit != 7) {
actual_bit++;
} else {
actual_bit = 0;
actual_byte++;
}
return bit;
}
void Executor::set_actual_bit(int current_bit) {
actual_byte = current_bit / 8;
actual_bit = current_bit % 8;
}
int Executor::get_actual_bit() {
return 8 * actual_byte + actual_bit;
}
void Executor::reset() {
actual_bit = 0;
actual_byte = 0;
parameters.clear();
}