-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbaseline.cpp
More file actions
92 lines (89 loc) · 1.91 KB
/
baseline.cpp
File metadata and controls
92 lines (89 loc) · 1.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
#include "insn.h"
#include <ctime>
#include <vector>
#include <iostream>
#include <fstream>
int *sp, stack[100], result, iserr;
clock_t st, ed;
unsigned char *pc;
std::string input_path = "bytecode/bytecode.txt";
void exec(std::vector<unsigned char> bytecode)
{
pc = &bytecode[0];
sp = &stack[0];
st = clock();
while (true)
{
switch (*pc++)
{
case Insn::NOP:
break;
case Insn::INT0:
*sp++ = 0;
break;
case Insn::INT1:
*sp++ = 1;
break;
case Insn::INT2:
*sp++ = 2;
break;
case Insn::INT3:
*sp++ = 3;
break;
case Insn::INT4:
*sp++ = 4;
break;
case Insn::INT5:
*sp++ = 5;
break;
case Insn::INT6:
*sp++ = 6;
break;
case Insn::INT7:
*sp++ = 7;
break;
case Insn::INT8:
*sp++ = 8;
break;
case Insn::ADD:
sp[-2] += sp[-1];
sp--;
break;
case Insn::SUB:
sp[-2] -= sp[-1];
sp--;
break;
case Insn::MUL:
sp[-2] *= sp[-1];
sp--;
break;
case Insn::DIV:
sp[-2] /= sp[-1];
sp--;
break;
case Insn::RETURN:
result = sp[-1];
iserr = false;
return;
default:
return;
}
}
}
int main(int argc, char *argv[])
{
std::ifstream infile(input_path);
if (!infile)
{
std::cerr << "No input file.\n";
return 1;
}
std::vector<unsigned char> bytecode;
std::string line;
while (std::getline(infile, line))
bytecode.push_back(std::stoi(line));
infile.close();
exec(bytecode);
ed = clock();
std::cout << ed - st << std::endl;
}