Debian Codegraph @

asm.cpp

raw symbols SCIP
1 // File: asm.cpp
2 /*
3 Pep8-1 is a virtual machine for writing machine language and assembly
4 language programs.
5
6 Copyright (C) 2009 J. Stanley Warford, Pepperdine University
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 #include "asm.h"
22 #include "argument.h"
23 #include "code.h"
24
25 // Regular expressions for lexical analysis
26 QRegExp Asm::rxAddrMode("^((,)(\\s*)(i|d|x|n|s(?![fx])|sx(?![f])|sf|sxf){1}){1}");
27 QRegExp Asm::rxCharConst("^((\')(?![\'])(([^\'\\\\]){1}|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))(\'))");
28 QRegExp Asm::rxComment("^((;{1})(.)*)");
29 QRegExp Asm::rxDecConst("^((([+|-]{0,1})([0-9]+))|^(([1-9])([0-9]*)))");
30 QRegExp Asm::rxDotCommand("^((.)(([A-Z|a-z]{1})(\\w)*))");
31 QRegExp Asm::rxHexConst("^((0(?![x|X]))|((0)([x|X])([0-9|A-F|a-f])+)|((0)([0-9]+)))");
32 QRegExp Asm::rxIdentifier("^((([A-Z|a-z|_]{1})(\\w*))(:){0,1})");
33 QRegExp Asm::rxStringConst("^((\")((([^\"\\\\])|((\\\\)([\'|b|f|n|r|t|v|\"|\\\\]))|((\\\\)(([x|X])([0-9|A-F|a-f]{2}))))*)(\"))");
34
35 // Regular expressions for trace tag analysis
36 QRegExp Asm::rxFormatTag("(#((1c)|(1d)|(1h)|(2d)|(2h))((\\d)+a)?(\\s|$))");
37 QRegExp Asm::rxSymbolTag("#([a-zA-Z][a-zA-Z0-9]{0,7})");
38 QRegExp Asm::rxArrayMultiplier("((\\d)+)a");
39
40 bool Asm::getToken(QString &sourceLine, ELexicalToken &token, QString &tokenString)
41 {
42
43
44 sourceLine = sourceLine.trimmed();
45 if (sourceLine.length() == 0) {
46 token = LT_EMPTY;
47 tokenString = "";
48 return true;
49 }
50 QChar firstChar = sourceLine[0];
51 rxAddrMode.setCaseSensitivity (Qt::CaseInsensitive); // Make rxAddrMode not case sensitive
52 if (firstChar == ',') {
53 if (rxAddrMode.indexIn(sourceLine) == -1) {
54 tokenString = ";ERROR: Malformed addressing mode.";
55 return false;
56 }
57 token = LT_ADDRESSING_MODE;
58 tokenString = rxAddrMode.capturedTexts()[0];
59 sourceLine.remove(0, tokenString.length());
60 return true;
61 }
62 if (firstChar == '\'') {
63 if (rxCharConst.indexIn(sourceLine) == -1) {
64 tokenString = ";ERROR: Malformed character constant.";
65 return false;
66 }
67 token = LT_CHAR_CONSTANT;
68 tokenString = rxCharConst.capturedTexts()[0];
69 sourceLine.remove(0, tokenString.length());
70 return true;
71 }
72 if (firstChar == ';') {
73 if (rxComment.indexIn(sourceLine) == -1) {
74 // This error should not occur, as any characters are allowed in a comment.
75 tokenString = ";ERROR: Malformed comment";
76 return false;
77 }
78 token = LT_COMMENT;
79 tokenString = rxComment.capturedTexts()[0];
80 sourceLine.remove(0, tokenString.length());
81 return true;
82 }
83 if (startsWithHexPrefix(sourceLine)) {
84 if (rxHexConst.indexIn(sourceLine) == -1) {
85 tokenString = ";ERROR: Malformed hex constant.";
86 return false;
87 }
88 token = LT_HEX_CONSTANT;
89 tokenString = rxHexConst.capturedTexts()[0];
90 sourceLine.remove(0, tokenString.length());
91 return true;
92 }
93 if ((firstChar.isDigit() || firstChar == '+' || firstChar == '-')) {
94 if (rxDecConst.indexIn(sourceLine) == -1) {
95 tokenString = ";ERROR: Malformed decimal constant.";
96 return false;
97 }
98 token = LT_DEC_CONSTANT;
99 tokenString = rxDecConst.capturedTexts()[0];
100 sourceLine.remove(0, tokenString.length());
101 return true;
102 }
103 if (firstChar == '.') {
104 if (rxDotCommand.indexIn(sourceLine) == -1) {
105 tokenString = ";ERROR: Malformed dot command.";
106 return false;
107 }
108 token = LT_DOT_COMMAND;
109 tokenString = rxDotCommand.capturedTexts()[0];
110 sourceLine.remove(0, tokenString.length());
111 return true;
112 }
113 if (firstChar.isLetter() || firstChar == '_') {
114 if (rxIdentifier.indexIn(sourceLine) == -1) {
115 // This error should not occur, as one-character identifiers are valid.
116 tokenString = ";ERROR: Malformed identifier.";
117 return false;
118 }
119 tokenString = rxIdentifier.capturedTexts()[0];
120 token = tokenString.endsWith(':') ? LT_SYMBOL_DEF : LT_IDENTIFIER;
121 sourceLine.remove(0, tokenString.length());
122 return true;
123 }
124 if (firstChar == '\"') {
125 if (rxStringConst.indexIn(sourceLine) == -1) {
126 tokenString = ";ERROR: Malformed string constant.";
127 return false;
128 }
129 token = LT_STRING_CONSTANT;
130 tokenString = rxStringConst.capturedTexts()[0];
131 sourceLine.remove(0, tokenString.length());
132 return true;
133 }
134 tokenString = ";ERROR: Syntax error.";
135 return false;
136 }
137
138 QList<QString> Asm::listOfReferencedSymbols;
139 QList<int> Asm::listOfReferencedSymbolLineNums;
140
141
142 bool Asm::startsWithHexPrefix(QString str)
143 {
144 if (str.length() < 2) return false;
145 if (str[0] != '0') return false;
146 if (str[1] == 'x' || str[1] == 'X') return true;
147 return false;
148 }
149
150 Enu::EAddrMode Asm::stringToAddrMode(QString str)
151 {
152 str.remove(0, 1); // Remove the comma.
153 str = str.trimmed().toUpper();
154 if (str == "I") return Enu::I;
155 if (str == "D") return Enu::D;
156 if (str == "N") return Enu::N;
157 if (str == "S") return Enu::S;
158 if (str == "SF") return Enu::SF;
159 if (str == "X") return Enu::X;
160 if (str == "SX") return Enu::SX;
161 if (str == "SXF") return Enu::SXF;
162 return Enu::NONE;
163 }
164
165 int Asm::charStringToInt(QString str)
166 {
167 str.remove(0, 1); // Remove the leftmost single quote.
168 str.chop(1); // Remove the rightmost single quote.
169 int value;
170 Asm::unquotedStringToInt(str, value);
171 return value;
172 }
173
174 int Asm::string2ArgumentToInt(QString str) {
175 int valueA, valueB;
176 str.remove(0, 1); // Remove the leftmost double quote.
177 str.chop(1); // Remove the rightmost double quote.
178 Asm::unquotedStringToInt(str, valueA);
179 if (str.length() == 0) {
180 return valueA;
181 }
182 else {
183 Asm::unquotedStringToInt(str, valueB);
184 valueA = 256 * valueA + valueB;
185 if (valueA < 0) {
186 valueA += 65536; // Stored as two-byte unsigned.
187 }
188 return valueA;
189 }
190 }
191
192 void Asm::unquotedStringToInt(QString &str, int &value)
193 {
194 QString s;
195 if (str.startsWith("\\x") || str.startsWith("\\X")) {
196 str.remove(0, 2); // Remove the leading \x or \X
197 s = str.left(2);
198 str.remove(0, 2); // Get the two-char hex number
199 bool ok;
200 value = s.toInt(&ok, 16);
201 }
202 else if (str.startsWith("\\")) {
203 str.remove(0, 1); // Remove the leading bash
204 s = str.left(1);
205 str.remove(0,1);
206 if (s == "b") { // backspace
207 value = 8;
208 }
209 else if (s == "f") { // form feed
210 value = 12;
211 }
212 else if (s == "n") { // line feed (new line)
213 value = 10;
214 }
215 else if (s == "r") { // carriage return
216 value = 13;
217 }
218 else if (s == "t") { // horizontal tab
219 value = 9;
220 }
221 else if (s == "v") { // vertical tab
222 value = 11;
223 }
224 else {
225 value = QChar(s[0]).toLatin1();
226 }
227 }
228 else {
229 s = str.left(1);
230 str.remove(0, 1);
231 value = QChar(s[0]).toLatin1();
232 }
233 value += value < 0 ? 256 : 0;
234 }
235
236 int Asm::byteStringLength(QString str)
237 {
238 str.remove(0, 1); // Remove the leftmost double quote.
239 str.chop(1); // Remove the rightmost double quote.
240 int length = 0;
241 while (str.length() > 0) {
242 if (str.startsWith("\\x") || str.startsWith("\\X")) {
243 str.remove(0, 4); // Remove the \xFF
244 }
245 else if (str.startsWith("\\")) {
246 str.remove(0, 2); // Remove the quoted character
247 }
248 else {
249 str.remove(0, 1); // Remove the single character
250 }
251 length++;
252 }
253 return length;
254 }
255
256 Enu::ESymbolFormat Asm::formatTagType(QString formatTag) {
257 if (formatTag.startsWith("#1c")) return Enu::F_1C;
258 if (formatTag.startsWith("#1d")) return Enu::F_1D;
259 if (formatTag.startsWith("#2d")) return Enu::F_2D;
260 if (formatTag.startsWith("#1h")) return Enu::F_1H;
261 if (formatTag.startsWith("#2h")) return Enu::F_2H;
262 return Enu::F_NONE; // Should not occur
263 }
264
265 int Asm::tagNumBytes(Enu::ESymbolFormat symbolFormat) {
266 switch (symbolFormat) {
267 case Enu::F_1C: return 1;
268 case Enu::F_1D: return 1;
269 case Enu::F_2D: return 2;
270 case Enu::F_1H: return 1;
271 case Enu::F_2H: return 2;
272 case Enu::F_NONE: return 0;
273 default: return -1; // Should not occur.
274 }
275 }
276
277 int Asm::formatMultiplier(QString formatTag) {
278 int pos = Asm::rxArrayMultiplier.indexIn(formatTag);
279 if (pos > -1) {
280 QString multiplierTag = Asm::rxArrayMultiplier.cap(0);
281 multiplierTag.chop(1); // Remove the last character 'a' from the array tag.
282 return multiplierTag.toInt();
283 }
284 else {
285 return 1;
286 }
287 }
288
289 bool Asm::processSourceLine(QString sourceLine, int lineNum, Code *&code, QString &errorString, bool &dotEndDetected)
290 {
291 Asm::ELexicalToken token; // Passed to getToken.
292 QString tokenString; // Passed to getToken.
293 QString localSymbolDef = ""; // Saves symbol definition for processing in the following state.
294 Enu::EMnemonic localEnumMnemonic; // Key to Pep:: table lookups.
295
296 // The concrete code objects asssigned to code.
297 UnaryInstruction *unaryInstruction = NULL;
298 NonUnaryInstruction *nonUnaryInstruction = NULL;
299 DotAddrss *dotAddrss = NULL;
300 DotAscii *dotAscii = NULL;
301 DotBlock *dotBlock = NULL;
302 DotBurn *dotBurn = NULL;
303 DotByte *dotByte = NULL;
304 DotEnd *dotEnd = NULL;
305 DotEquate *dotEquate = NULL;
306 DotWord *dotWord = NULL;
307 CommentOnly *commentOnly = NULL;
308 BlankLine *blankLine = NULL;
309
310 dotEndDetected = false;
311 Asm::ParseState state = Asm::PS_START;
312 do {
313 if (!getToken(sourceLine, token, tokenString)) {
314 errorString = tokenString;
315 return false;
316 }
317 switch (state) {
318 case Asm::PS_START:
319 if (token == Asm::LT_IDENTIFIER) {
320 if (Pep::mnemonToEnumMap.contains(tokenString.toUpper())) {
321 localEnumMnemonic = Pep::mnemonToEnumMap.value(tokenString.toUpper());
322 if (Pep::isUnaryMap.value(localEnumMnemonic)) {
323 unaryInstruction = new UnaryInstruction;
324 unaryInstruction->symbolDef = "";
325 unaryInstruction->mnemonic = localEnumMnemonic;
326 code = unaryInstruction;
327 code->memAddress = Pep::byteCount;
328 Pep::byteCount += 1; // One byte generated for unary instruction.
329 state = Asm::PS_CLOSE;
330 }
331 else {
332 nonUnaryInstruction = new NonUnaryInstruction;
333 nonUnaryInstruction->symbolDef = "";
334 nonUnaryInstruction->mnemonic = localEnumMnemonic;
335 code = nonUnaryInstruction;
336 code->memAddress = Pep::byteCount;
337 Pep::byteCount += 3; // Three bytes generated for nonunary instruction.
338 state = Asm::PS_INSTRUCTION;
339 }
340 }
341 else {
342 errorString = ";ERROR: Invalid mnemonic.";
343 return false;
344 }
345 }
346 else if (token == Asm::LT_DOT_COMMAND) {
347 tokenString.remove(0, 1); // Remove the period
348 tokenString = tokenString.toUpper();
349 if (tokenString == "ADDRSS") {
350 dotAddrss = new DotAddrss;
351 dotAddrss->symbolDef = "";
352 code = dotAddrss;
353 code->memAddress = Pep::byteCount;
354 state = Asm::PS_DOT_ADDRSS;
355 }
356 else if (tokenString == "ASCII") {
357 dotAscii = new DotAscii;
358 dotAscii->symbolDef = "";
359 code = dotAscii;
360 code->memAddress = Pep::byteCount;
361 state = Asm::PS_DOT_ASCII;
362 }
363 else if (tokenString == "BLOCK") {
364 dotBlock = new DotBlock;
365 dotBlock->symbolDef = "";
366 code = dotBlock;
367 code->memAddress = Pep::byteCount;
368 state = Asm::PS_DOT_BLOCK;
369 }
370 else if (tokenString == "BURN") {
371 dotBurn = new DotBurn;
372 dotBurn->symbolDef = "";
373 code = dotBurn;
374 code->memAddress = Pep::byteCount;
375 state = Asm::PS_DOT_BURN;
376 }
377 else if (tokenString == "BYTE") {
378 dotByte = new DotByte;
379 dotByte->symbolDef = "";
380 code = dotByte;
381 code->memAddress = Pep::byteCount;
382 state = Asm::PS_DOT_BYTE;
383 }
384 else if (tokenString == "END") {
385 dotEnd = new DotEnd;
386 dotEnd->symbolDef = "";
387 code = dotEnd;
388 code->memAddress = Pep::byteCount;
389 dotEndDetected = true;
390 state = Asm::PS_DOT_END;
391 }
392 else if (tokenString == "EQUATE") {
393 dotEquate = new DotEquate;
394 dotEquate->symbolDef = "";
395 code = dotEquate;
396 code->memAddress = Pep::byteCount;
397 state = Asm::PS_DOT_EQUATE;
398 }
399 else if (tokenString == "WORD") {
400 dotWord = new DotWord;
401 dotWord->symbolDef = "";
402 code = dotWord;
403 code->memAddress = Pep::byteCount;
404 state = Asm::PS_DOT_WORD;
405 }
406 else {
407 errorString = ";ERROR: Invalid dot command.";
408 return false;
409 }
410 }
411 else if (token == Asm::LT_SYMBOL_DEF) {
412 tokenString.chop(1); // Remove the colon
413 if (tokenString.length() > 8) {
414 errorString = ";ERROR: Symbol " + tokenString + " cannot have more than eight characters.";
415 return false;
416 }
417 if (Pep::symbolTable.contains(tokenString)) {
418 errorString = ";ERROR: Symbol " + tokenString + " was previously defined.";
419 return false;
420 }
421 localSymbolDef = tokenString;
422 Pep::symbolTable.insert(localSymbolDef, Pep::byteCount);
423 Pep::adjustSymbolValueForBurn.insert(localSymbolDef, true);
424 state = Asm::PS_SYMBOL_DEF;
425 }
426 else if (token == Asm::LT_COMMENT) {
427 commentOnly = new CommentOnly;
428 commentOnly->comment = tokenString;
429 code = commentOnly;
430 code->memAddress = Pep::byteCount;
431 state = Asm::PS_COMMENT;
432 }
433 else if (token == Asm::LT_EMPTY) {
434 blankLine = new BlankLine;
435 code = blankLine;
436 code->memAddress = Pep::byteCount;
437 code->sourceCodeLine = lineNum;
438 state = Asm::PS_FINISH;
439 }
440 else {
441 errorString = ";ERROR: Line must start with symbol definition, mnemonic, dot command, or comment.";
442 return false;
443 }
444 break;
445
446 case Asm::PS_SYMBOL_DEF:
447 if (token == Asm::LT_IDENTIFIER){
448 if (Pep::mnemonToEnumMap.contains(tokenString.toUpper())) {
449 localEnumMnemonic = Pep::mnemonToEnumMap.value(tokenString.toUpper());
450 if (Pep::isUnaryMap.value(localEnumMnemonic)) {
451 unaryInstruction = new UnaryInstruction;
452 unaryInstruction->symbolDef = localSymbolDef;
453 unaryInstruction->mnemonic = localEnumMnemonic;
454 code = unaryInstruction;
455 code->memAddress = Pep::byteCount;
456 Pep::byteCount += 1; // One byte generated for unary instruction.
457 state = Asm::PS_CLOSE;
458 }
459 else {
460 nonUnaryInstruction = new NonUnaryInstruction;
461 nonUnaryInstruction->symbolDef = localSymbolDef;
462 nonUnaryInstruction->mnemonic = localEnumMnemonic;
463 code = nonUnaryInstruction;
464 code->memAddress = Pep::byteCount;
465 Pep::byteCount += 3; // Three bytes generated for unary instruction.
466 state = Asm::PS_INSTRUCTION;
467 }
468 }
469 else {
470 errorString = ";ERROR: Invalid mnemonic.";
471 return false;
472 }
473 }
474 else if (token == Asm::LT_DOT_COMMAND) {
475 tokenString.remove(0, 1); // Remove the period
476 tokenString = tokenString.toUpper();
477 if (tokenString == "ADDRSS") {
478 dotAddrss = new DotAddrss;
479 dotAddrss->symbolDef = localSymbolDef;
480 code = dotAddrss;
481 code->memAddress = Pep::byteCount;
482 state = Asm::PS_DOT_ADDRSS;
483 }
484 else if (tokenString == "ASCII") {
485 dotAscii = new DotAscii;
486 dotAscii->symbolDef = localSymbolDef;
487 code = dotAscii;
488 code->memAddress = Pep::byteCount;
489 state = Asm::PS_DOT_ASCII;
490 }
491 else if (tokenString == "BLOCK") {
492 dotBlock = new DotBlock;
493 dotBlock->symbolDef = localSymbolDef;
494 code = dotBlock;
495 code->memAddress = Pep::byteCount;
496 state = Asm::PS_DOT_BLOCK;
497 }
498 else if (tokenString == "BURN") {
499 dotBurn = new DotBurn;
500 dotBurn->symbolDef = localSymbolDef;
501 code = dotBurn;
502 code->memAddress = Pep::byteCount;
503 state = Asm::PS_DOT_BURN;
504 }
505 else if (tokenString == "BYTE") {
506 dotByte = new DotByte;
507 dotByte->symbolDef = localSymbolDef;
508 code = dotByte;
509 code->memAddress = Pep::byteCount;
510 state = Asm::PS_DOT_BYTE;
511 }
512 else if (tokenString == "END") {
513 dotEnd = new DotEnd;
514 dotEnd->symbolDef = localSymbolDef;
515 code = dotEnd;
516 code->memAddress = Pep::byteCount;
517 dotEndDetected = true;
518 state = Asm::PS_DOT_END;
519 }
520 else if (tokenString == "EQUATE") {
521 dotEquate = new DotEquate;
522 dotEquate->symbolDef = localSymbolDef;
523 code = dotEquate;
524 code->memAddress = Pep::byteCount;
525 state = Asm::PS_DOT_EQUATE;
526 }
527 else if (tokenString == "WORD") {
528 dotWord = new DotWord;
529 dotWord->symbolDef = localSymbolDef;
530 code = dotWord;
531 code->memAddress = Pep::byteCount;
532 state = Asm::PS_DOT_WORD;
533 }
534 else {
535 errorString = ";ERROR: Invalid dot command.";
536 return false;
537 }
538 }
539 else {
540 errorString = ";ERROR: Must have mnemonic or dot command after symbol definition.";
541 return false;
542 }
543 break;
544
545 case Asm::PS_INSTRUCTION:
546 if (token == Asm::LT_IDENTIFIER) {
547 if (tokenString.length() > 8) {
548 errorString = ";ERROR: Symbol " + tokenString + " cannot have more than eight characters.";
549 return false;
550 }
551 nonUnaryInstruction->argument = new SymbolRefArgument(tokenString);
552 Asm::listOfReferencedSymbols.append(tokenString);
553 Asm::listOfReferencedSymbolLineNums.append(lineNum);
554 state = Asm::PS_ADDRESSING_MODE;
555 }
556 else if (token == Asm::LT_STRING_CONSTANT) {
557 if (Asm::byteStringLength(tokenString) > 2) {
558 errorString = ";ERROR: String operands must have length at most two.";
559 return false;
560 }
561 nonUnaryInstruction->argument = new StringArgument(tokenString);
562 state = Asm::PS_ADDRESSING_MODE;
563 }
564 else if (token == Asm::LT_HEX_CONSTANT) {
565 tokenString.remove(0, 2); // Remove "0x" prefix.
566 bool ok;
567 int value = tokenString.toInt(&ok, 16);
568 if (value < 65536) {
569 nonUnaryInstruction->argument = new HexArgument(value);
570 state = Asm::PS_ADDRESSING_MODE;
571 }
572 else {
573 errorString = ";ERROR: Hexidecimal constant is out of range (0x0000..0xFFFF).";
574 return false;
575 }
576 }
577 else if (token == Asm::LT_DEC_CONSTANT) {
578 bool ok;
579 int value = tokenString.toInt(&ok, 10);
580 if ((-32768 <= value) && (value <= 65535)) {
581 if (value < 0) {
582 value += 65536; // Stored as two-byte unsigned.
583 nonUnaryInstruction->argument = new DecArgument(value);
584 }
585 else {
586 nonUnaryInstruction->argument = new UnsignedDecArgument(value);
587 }
588 state = Asm::PS_ADDRESSING_MODE;
589 }
590 else {
591 errorString = ";ERROR: Decimal constant is out of range (-32768..65535).";
592 return false;
593 }
594 }
595 else if (token == Asm::LT_CHAR_CONSTANT) {
596 nonUnaryInstruction->argument = new CharArgument(tokenString);
597 state = Asm::PS_ADDRESSING_MODE;
598 }
599 else {
600 errorString = ";ERROR: Operand specifier expected after mnemonic.";
601 return false;
602 }
603 break;
604
605 case Asm::PS_ADDRESSING_MODE:
606 if (token == Asm::LT_ADDRESSING_MODE) {
607 Enu::EAddrMode addrMode = Asm::stringToAddrMode(tokenString);
608 if ((addrMode & Pep::addrModesMap.value(localEnumMnemonic)) == 0) { // Nested parens required.
609 errorString = ";ERROR: Illegal addressing mode for this instruction.";
610 return false;
611 }
612 nonUnaryInstruction->addressingMode = addrMode;
613 state = Asm::PS_CLOSE;
614 }
615 else if (Pep::addrModeRequiredMap.value(localEnumMnemonic)) {
616 errorString = ";ERROR: Addressing mode required for this instruction.";
617 return false;
618 }
619 else { // Must be branch type instruction with no addressing mode. Assign default addressing mode.
620 nonUnaryInstruction->addressingMode = Enu::I;
621 if (token == Asm::LT_COMMENT) {
622 code->comment = tokenString;
623 state = Asm::PS_COMMENT;
624 }
625 else if (token == Asm::LT_EMPTY) {
626 code->sourceCodeLine = lineNum;
627 state = Asm::PS_FINISH;
628 }
629 else {
630 errorString = ";ERROR: Comment expected following instruction.";
631 return false;
632 }
633 }
634 break;
635
636 case Asm::PS_DOT_ADDRSS:
637 if (token == Asm::LT_IDENTIFIER) {
638 if (tokenString.length() > 8) {
639 errorString = ";ERROR: Symbol " + tokenString + " cannot have more than eight characters.";
640 return false;
641 }
642 dotAddrss->argument = new SymbolRefArgument(tokenString);
643 Asm::listOfReferencedSymbols.append(tokenString);
644 Asm::listOfReferencedSymbolLineNums.append(lineNum);
645 Pep::byteCount += 2;
646 state = Asm::PS_CLOSE;
647 }
648 else {
649 errorString = ";ERROR: .ADDRSS requires a symbol argument.";
650 return false;
651 }
652 break;
653
654 case Asm::PS_DOT_ASCII:
655 if (token == Asm::LT_STRING_CONSTANT) {
656 dotAscii->argument = new StringArgument(tokenString);
657 Pep::byteCount += Asm::byteStringLength(tokenString);
658 state = Asm::PS_CLOSE;
659 }
660 else {
661 errorString = ";ERROR: .ASCII requires a string constant argument.";
662 return false;
663 }
664 break;
665
666 case Asm::PS_DOT_BLOCK:
667 if (token == Asm::LT_DEC_CONSTANT) {
668 bool ok;
669 int value = tokenString.toInt(&ok, 10);
670 if ((0 <= value) && (value <= 65535)) {
671 if (value < 0) {
672 value += 65536; // Stored as two-byte unsigned.
673 dotBlock->argument = new DecArgument(value);
674 }
675 else {
676 dotBlock->argument = new UnsignedDecArgument(value);
677 }
678 Pep::byteCount += value;
679 state = Asm::PS_CLOSE;
680 }
681 else {
682 errorString = ";ERROR: Decimal constant is out of range (0..65535).";
683 return false;
684 }
685 }
686 else if (token == Asm::LT_HEX_CONSTANT) {
687 tokenString.remove(0, 2); // Remove "0x" prefix.
688 bool ok;
689 int value = tokenString.toInt(&ok, 16);
690 if (value < 65536) {
691 dotBlock->argument = new HexArgument(value);
692 Pep::byteCount += value;
693 state = Asm::PS_CLOSE;
694 }
695 else {
696 errorString = ";ERROR: Hexidecimal constant is out of range (0x0000..0xFFFF).";
697 return false;
698 }
699 }
700 else {
701 errorString = ";ERROR: .BLOCK requires a decimal or hex constant argument.";
702 return false;
703 }
704 break;
705
706 case Asm::PS_DOT_BURN:
707 if (token == Asm::LT_HEX_CONSTANT) {
708 tokenString.remove(0, 2); // Remove "0x" prefix.
709 bool ok;
710 int value = tokenString.toInt(&ok, 16);
711 if (value < 65536) {
712 dotBurn->argument = new HexArgument(value);
713 Pep::burnCount++;
714 Pep::dotBurnArgument = value;
715 Pep::romStartAddress = Pep::byteCount;
716 state = Asm::PS_CLOSE;
717 }
718 else {
719 errorString = ";ERROR: Hexidecimal constant is out of range (0x0000..0xFFFF).";
720 return false;
721 }
722 }
723 else {
724 errorString = ";ERROR: .BURN requires a hex constant argument.";
725 return false;
726 }
727 break;
728
729 case Asm::PS_DOT_BYTE:
730 if (token == Asm::LT_CHAR_CONSTANT) {
731 dotByte->argument = new CharArgument(tokenString);
732 Pep::byteCount += 1;
733 state = Asm::PS_CLOSE;
734 }
735 else if (token == Asm::LT_DEC_CONSTANT) {
736 bool ok;
737 int value = tokenString.toInt(&ok, 10);
738 if ((-128 <= value) && (value <= 255)) {
739 if (value < 0) {
740 value += 256; // value stored as one-byte unsigned.
741 }
742 dotByte->argument = new DecArgument(value);
743 Pep::byteCount += 1;
744 state = Asm::PS_CLOSE;
745 }
746 else {
747 errorString = ";ERROR: Decimal constant is out of byte range (-128..255).";
748 return false;
749 }
750 }
751 else if (token == Asm::LT_HEX_CONSTANT) {
752 tokenString.remove(0, 2); // Remove "0x" prefix.
753 bool ok;
754 int value = tokenString.toInt(&ok, 16);
755 if (value < 256) {
756 dotByte->argument = new HexArgument(value);
757 Pep::byteCount += 1;
758 state = Asm::PS_CLOSE;
759 }
760 else {
761 errorString = ";ERROR: Hex constant is out of byte range (0x00..0xFF).";
762 return false;
763 }
764 }
765 else if (token == Asm::LT_STRING_CONSTANT) {
766 if (Asm::byteStringLength(tokenString) > 1) {
767 errorString = ";ERROR: .BYTE string operands must have length one.";
768 return false;
769 }
770 dotByte->argument = new StringArgument(tokenString);
771 Pep::byteCount += 1;
772 state = Asm::PS_CLOSE;
773 }
774 else {
775 errorString = ";ERROR: .BYTE requires a char, dec, hex, or string constant argument.";
776 return false;
777 }
778 break;
779
780 case Asm::PS_DOT_END:
781 if (token == Asm::LT_COMMENT) {
782 dotEnd->comment = tokenString;
783 code->sourceCodeLine = lineNum;
784 state = Asm::PS_FINISH;
785 }
786 else if (token == Asm::LT_EMPTY) {
787 dotEnd->comment = "";
788 code->sourceCodeLine = lineNum;
789 state = Asm::PS_FINISH;
790 }
791 else {
792 errorString = ";ERROR: Only a comment can follow .END.";
793 return false;
794 }
795 break;
796
797 case Asm::PS_DOT_EQUATE:
798 if (dotEquate->symbolDef == "") {
799 errorString = ";ERROR: .EQUATE must have a symbol definition.";
800 return false;
801 }
802 else if (token == Asm::LT_DEC_CONSTANT) {
803 bool ok;
804 int value = tokenString.toInt(&ok, 10);
805 if ((-32768 <= value) && (value <= 65535)) {
806
807 if (value < 0) {
808 value += 65536; // Stored as two-byte unsigned.
809 dotEquate->argument = new DecArgument(value);
810 }
811 else {
812 dotEquate->argument = new UnsignedDecArgument(value);
813 }
814 Pep::symbolTable.insert(dotEquate->symbolDef, value);
815 Pep::adjustSymbolValueForBurn.insert(dotEquate->symbolDef, false);
816 state = Asm::PS_CLOSE;
817 }
818 else {
819 errorString = ";ERROR: Decimal constant is out of range (-32768..65535).";
820 return false;
821 }
822 }
823 else if (token == Asm::LT_HEX_CONSTANT) {
824 tokenString.remove(0, 2); // Remove "0x" prefix.
825 bool ok;
826 int value = tokenString.toInt(&ok, 16);
827 if (value < 65536) {
828 dotEquate->argument = new HexArgument(value);
829 Pep::symbolTable.insert(dotEquate->symbolDef, value);
830 Pep::adjustSymbolValueForBurn.insert(dotEquate->symbolDef, false);
831 state = Asm::PS_CLOSE;
832 }
833 else {
834 errorString = ";ERROR: Hexidecimal constant is out of range (0x0000..0xFFFF).";
835 return false;
836 }
837 }
838 else if (token == Asm::LT_STRING_CONSTANT) {
839 if (Asm::byteStringLength(tokenString) > 2) {
840 errorString = ";ERROR: .EQUATE string operand must have length at most two.";
841 return false;
842 }
843 dotEquate->argument = new StringArgument(tokenString);
844 Pep::symbolTable.insert(dotEquate->symbolDef, Asm::string2ArgumentToInt(tokenString));
845 Pep::adjustSymbolValueForBurn.insert(dotEquate->symbolDef, false);
846 state = Asm::PS_CLOSE;
847 }
848 else {
849 errorString = ";ERROR: .EQUATE requires a dec, hex, or string constant argument.";
850 return false;
851 }
852 break;
853
854 case Asm::PS_DOT_WORD:
855 if (token == Asm::LT_CHAR_CONSTANT) {
856 dotWord->argument = new CharArgument(tokenString);
857 Pep::byteCount += 2;
858 state = Asm::PS_CLOSE;
859 }
860 else if (token == Asm::LT_DEC_CONSTANT) {
861 bool ok;
862 int value = tokenString.toInt(&ok, 10);
863 if ((-32768 <= value) && (value < 65536)) {
864
865 if (value < 0) {
866 value += 65536; // Stored as two-byte unsigned.
867 dotWord->argument = new DecArgument(value);
868 }
869 else {
870 dotWord->argument = new UnsignedDecArgument(value);
871 }
872 Pep::byteCount += 2;
873 state = Asm::PS_CLOSE;
874 }
875 else {
876 errorString = ";ERROR: Decimal constant is out of range (-32768..65535).";
877 return false;
878 }
879 }
880 else if (token == Asm::LT_HEX_CONSTANT) {
881 tokenString.remove(0, 2); // Remove "0x" prefix.
882 bool ok;
883 int value = tokenString.toInt(&ok, 16);
884 if (value < 65536) {
885 dotWord->argument = new HexArgument(value);
886 Pep::byteCount += 2;
887 state = Asm::PS_CLOSE;
888 }
889 else {
890 errorString = ";ERROR: Hexidecimal constant is out of range (0x0000..0xFFFF).";
891 return false;
892 }
893 }
894 else if (token == Asm::LT_STRING_CONSTANT) {
895 if (Asm::byteStringLength(tokenString) > 2) {
896 errorString = ";ERROR: .WORD string operands must have length at most two.";
897 return false;
898 }
899 dotWord->argument = new StringArgument(tokenString);
900 Pep::byteCount += 2;
901 state = Asm::PS_CLOSE;
902 }
903 else {
904 errorString = ";ERROR: .WORD requires a char, dec, hex, or string constant argument.";
905 return false;
906 }
907 break;
908
909 case Asm::PS_CLOSE:
910 if (token == Asm::LT_EMPTY) {
911 code->sourceCodeLine = lineNum;
912 state = Asm::PS_FINISH;
913 }
914 else if (token == Asm::LT_COMMENT) {
915 code->comment = tokenString;
916 state = Asm::PS_COMMENT;
917 }
918 else {
919 errorString = ";ERROR: Comment expected following instruction.";
920 return false;
921 }
922 break;
923
924 case Asm::PS_COMMENT:
925 if (token == Asm::LT_EMPTY) {
926 code->sourceCodeLine = lineNum;
927 state = Asm::PS_FINISH;
928 }
929 else {
930 // This error should not occur, as all characters are allowed in comments.
931 errorString = ";ERROR: Problem detected after comment.";
932 return false;
933 }
934 break;
935
936 default:
937 break;
938 }
939 }
940 while (state != Asm::PS_FINISH);
941 return true;
942 }