forked from tsungweiwu/homework6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeline.cc
More file actions
271 lines (236 loc) · 7.05 KB
/
Copy pathcodeline.cc
File metadata and controls
271 lines (236 loc) · 7.05 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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "codeline.h"
/***************************************************************************
*3456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789
* Class 'CodeLine' as a container for one line of code.
*
* Author/copyright: Duncan A. Buell. All rights reserved.
* Used with permission and modified by: Group 5:
Tsung Wei Wu, Sean Wiig, Samyu Comandur, Mark Mcmurtury, Mayank Patel.
* Date: 4 December 2018
**/
/***************************************************************************
* Constructor
**/
CodeLine::CodeLine() {
}
//CodeLine::CodeLine(Globals globals) {
// globals_ = globals;
//}
/***************************************************************************
* Destructor
**/
CodeLine::~CodeLine() {
}
/***************************************************************************
* Accessors and Mutators
**/
/***************************************************************************
* Accessor for the 'addr_'.
**/
string CodeLine::GetAddr() const {
return addr_;
}
/***************************************************************************
* Accessor for the 'code_'.
**/
string CodeLine::GetCode() const {
return code_;
}
/***************************************************************************
* Accessor for the 'comments_'.
**/
string CodeLine::GetComments() const {
return comments_;
}
/***************************************************************************
* Accessor for the 'error_messages_'.
**/
string CodeLine::GetErrorMessages() const {
return error_messages_;
}
/***************************************************************************
* Accessor for the 'hex_'.
**/
Hex CodeLine::GetHexObject() const {
return hex_;
}
/***************************************************************************
* Accessor for the 'label_'.
**/
string CodeLine::GetLabel() const {
return label_;
}
/***************************************************************************
* Accessor for the 'mnemonic_'.
**/
string CodeLine::GetMnemonic() const {
return mnemonic_;
}
int CodeLine::GetPC() const {
return pc_;
}
/***************************************************************************
* Accessor for the 'symoperand_'.
**/
string CodeLine::GetSymOperand() const {
return symoperand_;
}
/***************************************************************************
* Boolean indicator of the presence of a label.
**/
bool CodeLine::HasLabel() const {
bool returnValue = !(label_.empty() || label_ == "nulllabel");
return returnValue;
}
/***************************************************************************
* Boolean indicator of the presence of a symbolic operand.
**/
bool CodeLine::HasSymOperand() const {
bool returnValue = !(symoperand_.empty() || symoperand_ == "nullsymoperand");
return returnValue;
}
/***************************************************************************
* Accessor for 'is_all_comment_'.
**/
bool CodeLine::IsAllComment() const {
return is_all_comment_;
}
/***************************************************************************
* General functions.
**/
/***************************************************************************
* Function 'SetCodeLine'.
* Sets the values for a line of code that isn't all comments.
*
* Parameters:
* linecounter - the line counter for this line in source
* pc - the program counter if it is there
* label - the label if it is there
* mnemonic - the mnemonic of the opcode
* addr - the direct/indirect addresssing asterisk
* symoperand - the symbolic operand if it is there
* hexoperand - the hexoperand if it is there
* comments - comments if any
**/
void CodeLine::SetCodeLine(int linecounter, int pc, string label,
string mnemonic, string addr, string symoperand,
string hexoperand, string comments, string code) {
linecounter_ = linecounter;
pc_ = pc;
label_ = label;
mnemonic_ = mnemonic;
addr_ = addr;
symoperand_ = symoperand;
hex_ = Hex(hexoperand);
comments_ = comments;
code_ = code;
is_all_comment_ = false;
}
/***************************************************************************
* Function 'SetCommentsOnly'.
* Sets a line of code that is all comments.
*
* Parameters:
* line - the code line that is taken to be all comments
**/
void CodeLine::SetCommentsOnly(int linecounter, string line) {
comments_ = line; // assume asterisk was not stripped already
is_all_comment_ = true;
linecounter_ = linecounter;
}
/***************************************************************************
* Function 'SetErrorMessages'.
* Sets the 'error_messages_' variable for later printing.
*
* Parameters:
* messages - the string of messages
**/
void CodeLine::SetErrorMessages(string messages) {
error_messages_ = messages;
}
/***************************************************************************
* Function 'SetMachineCode'.
* Set the 'code_' for this line of code.
*
* Parameters:
* code - the code to set
**/
void CodeLine::SetMachineCode(string code) {
code_ = code;
}
/***************************************************************************
* Function 'SetPC'.
* Set the PC for this line of code.
*
* Parameters:
* what - the value to set as the PC
**/
void CodeLine::SetPC(int what) {
pc_ = what;
}
/***************************************************************************
* Function 'ToString'.
* This function formats a 'CodeLine' for prettyprinting.
*
* Returns:
* the prettyprint string for printing
**/
string CodeLine::ToString() const {
#ifdef EBUG
Utils::log_stream << "enter ToString" << endl;
#endif
string s = "";
s += Utils::Format(linecounter_, 5) + " ";
s += Utils::Format(pc_, 4) + " ";
s += DABnamespace::DecToBitString(pc_, 12) + " ";
if (code_ == "nullcode") {
s += Utils::Format("xxxx xxxx xxxx xxxx", 19);
} else {
s += Utils::Format(code_.substr(0,4), 4)
+ " " + Utils::Format(code_.substr(4,4), 4)
+ " " + Utils::Format(code_.substr(8,4), 4)
+ " " + Utils::Format(code_.substr(12,4), 4);
}
if (label_ == "nulllabel") {
s += " " + Utils::Format("...", 3);
} else {
s += " " + Utils::Format(label_, 3);
}
if (mnemonic_ == "nullmnemonic") {
s += " " + Utils::Format("...", 3);
} else {
s += " " + Utils::Format(mnemonic_, 6);
}
if (addr_ == "*") {
s += " " + Utils::Format("*", 1);
} else {
s += " " + Utils::Format(" ", 1);
}
if (symoperand_ == "nullsymoperand") {
s += " " + Utils::Format("...", 3);
} else {
s += " " + Utils::Format(symoperand_, 3);
}
if (hex_.IsNull()) {
s += " " + Utils::Format(".....", 5);
} else {
s += " " + hex_.ToString();
}
if (comments_ != "nullcomments") {
if (is_all_comment_) {
s = Utils::Format(linecounter_, 5) + " ";
s += Utils::Format(" ", 41);
s += " " + comments_;
} else {
s += " " + comments_;
}
}
if ((error_messages_.length() > 0) &&
(error_messages_ != "nullerrormessages")) {
s += error_messages_;
}
#ifdef EBUG
Utils::log_stream << "leave ToString" << endl;
#endif
return s;
}