-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathooasm.cc
183 lines (142 loc) · 5.6 KB
/
ooasm.cc
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
#include "instruction.h"
#include "ooasm.h"
namespace ooasm {
class Data : public Instruction {
public:
Data(ID::id_t _id, std::unique_ptr<Num> _value)
: id(_id), value(std::move(_value)) {}
void execute(ProcessorAbstract &, Memory &) const override {}
void declare(Memory &memory) const override {
memory.add_variable(id.get(), value->get(memory));
}
private:
ID id;
const std::unique_ptr<Num> value;
};
class Mov : public Instruction {
public:
Mov(std::unique_ptr<LValue> _dst, std::unique_ptr<RValue> _src)
: dst(std::move(_dst)), src(std::move(_src)) {}
void execute(ProcessorAbstract &, Memory &memory) const override {
dst->set(memory, src->get(memory));
}
private:
const std::unique_ptr<LValue> dst;
const std::unique_ptr<RValue> src;
};
// Abstract class for handling any arithmetic operation performed in ooasm.
// Deriving classes should overwrite the method <function> to choose performed operation.
class ArithmeticOperation : public Instruction {
public:
void execute(ProcessorAbstract &processorAbstract, Memory &memory) const override {
word_t res = function(arg1->get(memory), arg2->get(memory));
set_flags(res, processorAbstract);
set_value(res, memory);
}
virtual ~ArithmeticOperation() = default;
protected:
ArithmeticOperation(std::unique_ptr<LValue> _arg1, std::unique_ptr<RValue> _arg2)
: arg1(std::move(_arg1)), arg2(std::move(_arg2)) {}
private:
const std::unique_ptr<LValue> arg1;
const std::unique_ptr<RValue> arg2;
void set_value(word_t res, Memory &memory) const {
arg1->set(memory, res);
}
static void set_flags(word_t res, ProcessorAbstract &processorAbstract) {
processorAbstract.setSF(res < 0);
processorAbstract.setZF(res == 0);
}
// Function to be applied on given values.
[[nodiscard]] virtual word_t function(word_t a1, word_t a2) const = 0;
};
class Add : public ArithmeticOperation {
public:
Add(std::unique_ptr<LValue> _arg1, std::unique_ptr<RValue> _arg2)
: ArithmeticOperation(std::move(_arg1), std::move(_arg2)) {}
private:
[[nodiscard]] word_t function(word_t a1, word_t a2) const override {
return a1 + a2;
}
};
class Sub : public ArithmeticOperation {
public:
Sub(std::unique_ptr<LValue> _arg1, std::unique_ptr<RValue> _arg2)
: ArithmeticOperation(std::move(_arg1), std::move(_arg2)) {}
private:
[[nodiscard]] word_t function(word_t a1, word_t a2) const override {
return a1 - a2;
}
};
// Base class for setting one at given position.
// Deriving classes should override should_set function to choose when 1 should be set.
class One : public Instruction {
public:
explicit One(std::unique_ptr<LValue> _lValue) : lValue(std::move(_lValue)) {}
void execute(ProcessorAbstract &processorAbstract, Memory &memory) const override {
if (should_set(processorAbstract)) {
lValue->set(memory, 1);
}
}
~One() override = default;
protected:
[[nodiscard]] virtual bool should_set(const ProcessorAbstract &) const {
return true;
}
private:
std::unique_ptr<LValue> lValue;
};
class OneZ : public One {
public:
explicit OneZ(std::unique_ptr<LValue> _lValue) : One(std::move(_lValue)) {}
protected:
[[nodiscard]] bool should_set(const ProcessorAbstract &processorAbstract) const override {
return processorAbstract.getZF();
}
};
class OneS : public One {
public:
explicit OneS(std::unique_ptr<LValue> _lValue) : One(std::move(_lValue)) {}
protected:
[[nodiscard]] bool should_set(const ProcessorAbstract &processorAbstract) const override {
return processorAbstract.getSF();
}
};
}
using namespace ooasm;
std::unique_ptr<Num> num(word_t word) {
return std::make_unique<Num>(word);
}
std::unique_ptr<Mem> mem(std::unique_ptr<RValue> addr) {
return std::make_unique<Mem>(std::move(addr));
}
std::unique_ptr<LEA> lea(ID::id_t id) {
return std::make_unique<LEA>(id);
}
std::shared_ptr<Instruction> data(ID::id_t id, std::unique_ptr<Num> value) {
return std::make_shared<Data>(id, std::move(value));
}
std::shared_ptr<Instruction> mov(std::unique_ptr<LValue> dst, std::unique_ptr<RValue> src) {
return std::make_shared<Mov>(std::move(dst), std::move(src));
}
std::shared_ptr<Instruction> add(std::unique_ptr<LValue> arg1, std::unique_ptr<RValue> arg2) {
return std::make_shared<Add>(std::move(arg1), std::move(arg2));
}
std::shared_ptr<Instruction> sub(std::unique_ptr<LValue> arg1, std::unique_ptr<RValue> arg2) {
return std::make_shared<Sub>(std::move(arg1), std::move(arg2));
}
std::shared_ptr<Instruction> inc(std::unique_ptr<LValue> arg) {
return std::make_shared<Add>(std::move(arg), num(1));
}
std::shared_ptr<Instruction> dec(std::unique_ptr<LValue> arg) {
return std::make_shared<Sub>(std::move(arg), num(1));
}
std::shared_ptr<Instruction> one(std::unique_ptr<LValue> arg) {
return std::make_shared<One>(std::move(arg));
}
std::shared_ptr<Instruction> onez(std::unique_ptr<LValue> arg) {
return std::make_shared<OneZ>(std::move(arg));
}
std::shared_ptr<Instruction> ones(std::unique_ptr<LValue> arg) {
return std::make_shared<OneS>(std::move(arg));
}