-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression_table.cpp
176 lines (166 loc) · 6.38 KB
/
expression_table.cpp
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
#include "expression_table.h"
#include "entries.h"
#include "assembler.h"
#include "const_table.h"
#include <iostream>
#include <iomanip>
using namespace std;
ExpressionTableEntry* ExpressionTable::get(string& name) {
for (int i = 0; i < table.size(); i++) {
if (table[i]->name == name) return table[i];
}
return nullptr;
}
void ExpressionTable::put(ExpressionTableEntry* entry) {
table.push_back(entry);
}
void ExpressionTable::resolve() {
vector<int> entries_to_remove;
while (true) {
int changes_cntr = 0;
for (int i = 0; i < table.size(); i++) {
ExpressionTableEntry* entry = table[i];
if (!entry->op1_resolved) {
//da li je u tableli konstanti
ConstTableEntry* const_tab_entry = my_asm->const_table->get(entry->op1);
SymTableEntry* sym_tab_entry = my_asm->sym_table->getByName(entry->op1);
ExpressionTableEntry* exp_tab_entry = my_asm->expression_table->get(entry->op1);
if (const_tab_entry) {
entry->value_to_write += const_tab_entry->value;
if (entry->op2_resolved && entry->refered_symbol_id == 0) {
//u tabelu kontatni ide
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
entry->op1_resolved = true; changes_cntr++;
}
//da li je u tabli simbola
else if (sym_tab_entry) {
int sym_for_reloc = sym_tab_entry->scope == SymTable::LOCAL ? sym_tab_entry->section->id : sym_tab_entry->id;
entry->value_to_write += sym_tab_entry->scope == SymTable::LOCAL ? sym_tab_entry->offset : 0;
if (entry->refered_symbol_id == sym_for_reloc) {
//ide u tabelu konstanti
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
else if (entry->refered_symbol_id == 0) entry->refered_symbol_id = sym_for_reloc;
else {
exit(50);
}
entry->op1_resolved = true; changes_cntr++;
}
//da li je u tabli izraza
else if (exp_tab_entry && exp_tab_entry->op1_resolved && exp_tab_entry->op2_resolved) {
entry->value_to_write += exp_tab_entry->value_to_write;
if (entry->refered_symbol_id == 0 && exp_tab_entry->refered_symbol_id > 0)
entry->refered_symbol_id = exp_tab_entry->refered_symbol_id;
else if (entry->refered_symbol_id == exp_tab_entry->refered_symbol_id) {
//u tabelu konstanti
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
else {
exit(50);
}
entry->op1_resolved = true; changes_cntr++;
}
}
if (!entry->op2_resolved) {
//da li je u tableli konstanti
ConstTableEntry* const_tab_entry = my_asm->const_table->get(entry->op2);
SymTableEntry* sym_tab_entry = my_asm->sym_table->getByName(entry->op2);
ExpressionTableEntry* exp_tab_entry = my_asm->expression_table->get(entry->op2);
if (const_tab_entry) {
int16_t value = entry->operation == ExpressionTable::ADD ? const_tab_entry->value : -const_tab_entry->value;
entry->value_to_write += value;
if (entry->op1_resolved && entry->refered_symbol_id == 0) {
//u tabelu kontatni ide
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
entry->op2_resolved = true; changes_cntr++;
}
//da li je u tabli simbola
else if (sym_tab_entry) {
int sym_for_reloc = sym_tab_entry->scope == SymTable::LOCAL ? sym_tab_entry->section->id : sym_tab_entry->id;
int16_t value = entry->operation == sym_tab_entry->scope == SymTable::LOCAL ? sym_tab_entry->offset : 0;
value = entry->operation == ExpressionTable::ADD ? value : -value;
entry->value_to_write += value;
if (entry->refered_symbol_id == sym_for_reloc) {
//ide u tabelu konstanti
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
else if (entry->refered_symbol_id == 0) entry->refered_symbol_id = sym_for_reloc;
else {
exit(50);
}
entry->op2_resolved = true; changes_cntr++;
}
//da li je u tabli izraza
else if (exp_tab_entry && exp_tab_entry->op1_resolved && exp_tab_entry->op2_resolved) {
int16_t value = entry->operation ==
ExpressionTable::ADD ? exp_tab_entry->value_to_write : -exp_tab_entry->value_to_write;
entry->value_to_write += value;
if (entry->refered_symbol_id == 0 && exp_tab_entry->refered_symbol_id > 0)
entry->refered_symbol_id = exp_tab_entry->refered_symbol_id;
else if (entry->refered_symbol_id == exp_tab_entry->refered_symbol_id) {
//u tabelu konstanti
if (entry->refered_symbol_id > 0 && ((exp_tab_entry->reloc_neg && entry->operation == SUB) ||
(!exp_tab_entry->reloc_neg && entry->operation == ADD)))
exit(50);
my_asm->const_table->put(entry->name, entry->value_to_write);
entries_to_remove.push_back(i);
}
else {
exit(50);
}
entry->op2_resolved = true; changes_cntr++;
}
}
}
if (changes_cntr == 0) break;
}
//proveravanje da li su svi simboli razreseni
int unresolved = 0;
for (int i = 0; i < table.size(); i++) {
ExpressionTableEntry* entry = table[i];
//ako je se ne zna gde je simbol koji se referise, odstampa ju se informacije
if (!entry->op1_resolved) {
cout << "[" << my_asm->line_counter << "] " << "Nerazresen simbol: " << entry->op1 << endl;
unresolved++;
}
if (!entry->op2_resolved) {
cout << "[" << my_asm->line_counter << "] " << "Nerazresen simbol: " << entry->op2 << endl;
unresolved++;
}
}
if (unresolved > 0) exit(1000);
//izbacivanje ulaza koji su konstante
for (int i = 0; i < entries_to_remove.size(); i++) {
int min = entries_to_remove[i];
for (int j = i + 1; j < entries_to_remove.size(); j++) {
if (entries_to_remove[i] > entries_to_remove[j]) {
int temp = entries_to_remove[i];
entries_to_remove[i] = entries_to_remove[j];
entries_to_remove[j] = temp;
}
}
}
for (int i = entries_to_remove.size() - 1; i >= 0; i--) {
table.erase(table.begin() + entries_to_remove[i]);
}
}
ostream& operator<< (ostream& out, ExpressionTable& tab) {
for (int i = 0; i < tab.table.size(); i++) {
ExpressionTableEntry* entry = tab.table[i];
out << setw(15) << entry->name
<< setw(15) << entry->op1
<< setw(15) << entry->op1_resolved
<< setw(15) << entry->op2
<< setw(15) << entry->op2_resolved
<< setw(15) << entry->refered_symbol_id
<< setw(15) << entry->value_to_write << endl;
}
return out;
}