-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConverter.cpp
More file actions
143 lines (125 loc) · 3.97 KB
/
Converter.cpp
File metadata and controls
143 lines (125 loc) · 3.97 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
//
// Created by mirro on 2019/09/16.
//
#include <iostream>
#include <fstream>
#include "Converter.h"
#include "Structures/instruction.h"
#include "Object_.h"
// #define ON_DEBUG
#ifdef ON_DEBUG
#define DEBUG_OUT(message, target) cout << "debug | " << message << target << endl
#else
#define DEBUG_OUT(message, target)
#endif
void Converter::IRtoWC(string const& file_name) {
// read `.ir` file
string ir = string(file_name);
ir += ".ir";
ifstream fin;
fin.open(ir.c_str(), ios::in | ios::binary);
if (!fin) {
cout << "error : file '" << ir.c_str() << "' didn't open" << endl;
exit(1);
}
// TODO read
fin.close();
// TODO convert
// write `.wc` file
string wc = string(file_name);
wc += ".wc";
ofstream fout;
fout.open(wc.c_str(), ios::out | ios::binary | ios::trunc);
if (!fout) {
cout << "error : file '" << wc.c_str() << "' didn't open" << endl;
exit(1);
}
// TODO write
fout.close();
}
VirtualMachine* Converter::IRtoMemory(string const& file_name) {
// read `.ir` file
string ir = string(file_name);
ir += ".ir";
ifstream fin;
fin.open(ir.c_str(), ios::in | ios::binary);
if (!fin) {
cout << "error : file '" << ir.c_str() << "' didn't open" << endl;
exit(1);
}
// TODO read
fin.close();
return new VirtualMachine(nullptr, nullptr, nullptr, nullptr);
}
VirtualMachine* Converter::WCtoMemory(string const& file_name) {
input.clear();
position = 0;
string wc = string(file_name);
wc += ".wc";
ifstream fin;
fin.open(wc.c_str(), ios::in | ios::binary);
if (!fin) {
cout << "error | file `"<< wc.c_str() <<"` didn't open" << endl;
exit(1);
}
while (!fin.eof()) {
uint8_t byte = 0;
fin.read((char*) &byte, sizeof(uint8_t));
input.push_back(byte);
}
fin.close();
uint32_t magic = *read_int();
DEBUG_OUT("magic : ", magic);
if (magic != 0xdeadbeef) {
cout << "error | magic is invalid" << endl;
}
uint32_t entry_point = *read_int();
DEBUG_OUT("entry point : ", entry_point);
uint32_t registers_size = *read_int();
DEBUG_OUT("registers size : ", registers_size);
auto* registers = new Object_*[registers_size];
uint32_t references_size = *read_int();
DEBUG_OUT("references size : ", references_size);
uint32_t functions_size = *read_int();
DEBUG_OUT("functions size : ", functions_size);
auto* functions = new uint32_t[functions_size];
functions = (uint32_t*) read(sizeof(uint32_t) * functions_size);
int32_t instructions_size = *read_int();
DEBUG_OUT("instructions size : ", instructions_size);
auto* instructions = new instruction[instructions_size];
instructions = (instruction*) read(sizeof(instruction) * instructions_size);
uint32_t constant_pool_size = *read_int();
DEBUG_OUT("constant pool size : ", constant_pool_size);
auto* constant_pool = new Object_*[constant_pool_size];
for (uint32_t i = 0; i < constant_pool_size; i++) {
uint32_t tag = *read_int();
uint32_t size;
switch (tag) {
case 0x0: // int
constant_pool[i] = (Object_ *) *read_int();
break;
case 0x1: // string
size = *read_int();
constant_pool[i] = (Object_ *) read(size);
break;
default:
cout << "error | illegal constant type `" << tag << "`" << endl;
exit(1);
}
}
return new VirtualMachine(registers, functions, instructions, constant_pool);
}
uint8_t* Converter::read(const int32_t length) {
auto bytes = new uint8_t[length];
for (int i = 0; i < length; i++) {
bytes[i] = input[position + i];
}
position += length;
return bytes;
}
uint32_t* Converter::read_int() {
auto bytes = read(4);
DEBUG_OUT("read_int called and returned : ", *(uint32_t*)bytes);
return (uint32_t*)bytes;
}
#undef DEBUG_OUT