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