-
Notifications
You must be signed in to change notification settings - Fork 56
/
Parser.java
1185 lines (1012 loc) · 42.5 KB
/
Parser.java
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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Parser.java
*
* Parses a MIPS64 source code and fills the symbol table and the memory.
*
* (c) 2006 mancausoft, Vanni
*
* This file is part of the EduMIPS64 project, and is released under the GNU
* General Public License.
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/** Parses a MIPS64 source code and fills the symbol table and the memory.
* @author mancausoft, Vanni
*/
package org.edumips64.core.parser;
import org.edumips64.core.Converter;
import org.edumips64.core.FCSRRegister;
import org.edumips64.core.IrregularStringOfBitsException;
import org.edumips64.core.IrregularStringOfHexException;
import org.edumips64.core.IrregularWriteOperationException;
import org.edumips64.core.Memory;
import org.edumips64.core.MemoryElement;
import org.edumips64.core.MemoryElementNotFoundException;
import org.edumips64.core.NotAlignException;
import org.edumips64.core.SameLabelsException;
import org.edumips64.core.StringFormatException;
import org.edumips64.core.SymbolTable;
import org.edumips64.core.SymbolTableOverflowException;
import org.edumips64.core.fpu.FPInstructionUtils;
import org.edumips64.core.fpu.FPOverflowException;
import org.edumips64.core.fpu.FPUnderflowException;
import org.edumips64.core.is.Instruction;
import org.edumips64.core.is.InstructionBuilder;
import org.edumips64.core.is.ParsedInstructionMetadata;
import org.edumips64.utils.io.FileUtils;
import org.edumips64.utils.io.ReadException;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.logging.Logger;
class VoidJump {
Instruction instr;
String label;
int row;
int column;
int instrCount;
String line;
boolean isBranch = false;
}
public class Parser {
/** Instance variables */
private static final Logger logger = Logger.getLogger(Parser.class.getName());
private final Memory mem;
private enum AliasRegister
{zero, at, v0, v1, a0, a1, a2, a3, t0, t1, t2, t3, t4, t5, t6, t7, s0, s1, s2, s3, s4, s5, s6, s7, t8, t9, k0, k1, gp, sp, fp, ra}
private static final String deprecateInstruction[] = {"BNEZ", "BEQZ", "HALT", "DADDUI", "DMULU", "L.D", "S.D"};
private ParserMultiException error;
/** Base basePath to use for further #include directives. */
private String basePath;
private int numError;
private enum FileSection {NONE, DATA, TEXT}
private FileSection section;
/** File to be parsed
*/
private int memoryCount;
private String filename;
private SymbolTable symTab;
private FileUtils fileUtils;
private InstructionBuilder instructionBuilder;
private FPInstructionUtils fpInstructionUtils;
/** Accessible only because of unit tests.
* The Parser needs an FCSR register only because the core FP functions of the simulator
* are coupled too tightly with the actual implementation of the FPU, and therefore they assume
* that there is an FCSR register and use it to decide whether to throw an exception or not.
*
* The Parser uses the FP functions to parse, and as a result it needs to have an FCSR. Not ideal. */
private FCSRRegister fcsr;
public FCSRRegister getFCSR() {
return fcsr;
}
/** Public methods */
public Parser(FileUtils utils, SymbolTable symTab, Memory memory, InstructionBuilder instructionBuilder) {
this.symTab = symTab;
this.fileUtils = utils;
this.mem = memory;
this.instructionBuilder = instructionBuilder;
this.fcsr = new FCSRRegister();
fcsr.setFPExceptions(FCSRRegister.FPExceptions.INVALID_OPERATION, true);
fcsr.setFPExceptions(FCSRRegister.FPExceptions.OVERFLOW, true);
fcsr.setFPExceptions(FCSRRegister.FPExceptions.UNDERFLOW, true);
fcsr.setFPExceptions(FCSRRegister.FPExceptions.DIVIDE_BY_ZERO, true);
fpInstructionUtils = new FPInstructionUtils(this.fcsr);
}
/** Loading from File
* @param filename A String with the system-dependent file name. It should be an absolute file name.
* @throws SecurityException if a security manager exists and its checkRead method denies read access to the file.
* @returns A string containing the code parsed.
*/
public void parse(String filename) throws ParserMultiException, ReadException {
logger.info("About to parse " + filename);
this.filename = filename;
basePath = fileUtils.GetBasePath(filename);
String code = preprocessor(filename);
doParsing(code);
logger.info(filename + " correctly parsed.");
}
/** Replace all Tabulator with space
* @param text the string to replace
* @return a new String
*/
public static String replaceTab(String text) {
return text.replace("\t", " ");
}
/** Private methods */
private String fileToString(String filename) throws ReadException {
return fileUtils.ReadFile(filename);
}
private void checkLoop(String data, Stack<String> included) throws ParserMultiException, ReadException {
int i = 0;
do {
i = data.indexOf("#include ", i);
if (i != -1) {
int end = data.indexOf("\n", i);
if (end == -1) {
end = data.length();
}
int a = included.search(data.substring(i + 9, end).trim());
if (a != -1) {
error = new ParserMultiException();
error.add("INCLUDE_LOOP", 0, 0, "#include " + data.substring(i + 9, end).trim());
throw error;
}
String filename = data.substring(i + 9, end).split(";") [0].trim();
if (!fileUtils.isAbsolute(filename)) {
filename = basePath + filename;
}
String filetmp = fileToString(filename);
checkLoop(filetmp , included);
i ++;
}
} while (i != -1);
}
/** Process the #include (Syntax #include file.ext )
*/
private String preprocessor(String filename) throws ParserMultiException, ReadException {
String filetmp;
filetmp = fileToString(filename);
int i = 0;
//check loop
Stack<String> included = new Stack<>();
included.push(this.filename);
checkLoop(filetmp, included);
// include
do {
i = filetmp.indexOf("#include ", i);
if (i != -1) {
int end = filetmp.indexOf("\n", i);
if (end == -1) {
end = filetmp.length();
}
logger.info("Open by #include: " + filetmp.substring(i + 9, end).trim());
String includedFilename = filetmp.substring(i + 9, end).split(";") [0].trim();
if (!fileUtils.isAbsolute(includedFilename)) {
includedFilename = basePath + includedFilename;
}
String fileContents = fileToString(includedFilename);
filetmp = filetmp.substring(0, i) + fileContents + filetmp.substring(end);
}
} while (i != -1);
return filetmp;
}
public void doParsing(String code) throws ParserMultiException {
boolean isFirstOutOfInstructionMemory = true;
boolean isFirstOutOfMemory = true;
boolean halt = false;
int row = 0;
numError = 0;
int numWarning = 0;
int instrCount = -4; // Hack fituso by Andrea
error = new ParserMultiException();
ParserMultiWarningException warning = new ParserMultiWarningException();
LinkedList<VoidJump> voidJump = new LinkedList<>();
memoryCount = 0;
String lastLabel = "";
code = code.replaceAll("\r\n", "\n");
for (String line : code.split("\n")) {
row++;
logger.info("-- Processing line " + row);
for (int column = 0; column < line.length(); column++) {
if (line.charAt(column) == ';') { //comments
break;
}
if (line.charAt(column) == ' ' || line.charAt(column) == '\t') {
continue;
}
int tab = line.indexOf('\t', column);
int space = line.indexOf(' ', column);
if (tab == -1) {
tab = line.length();
}
if (space == -1) {
space = line.length();
}
int end = Math.min(tab, space) - 1;
String instr = line.substring(column, end + 1);
String parameters;
try {
if (line.charAt(column) == '.') {
logger.info("Processing " + instr);
if (instr.compareToIgnoreCase(".DATA") == 0) {
section = FileSection.DATA;
} else if (instr.compareToIgnoreCase(".TEXT") == 0 || instr.compareToIgnoreCase(".CODE") == 0) {
section = FileSection.TEXT;
} else {
String name = instr.substring(1); // The name, without the dot.
if (section != FileSection.DATA) {
numError++;
error.add(name.toUpperCase() + "INCODE", row, column + 1, line);
column = line.length();
continue;
}
try {
if (!((instr.compareToIgnoreCase(".ASCII") == 0) || instr.compareToIgnoreCase(".ASCIIZ") == 0)) {
// We don't want strings to be uppercase, do we?
parameters = cleanFormat(line.substring(end + 2));
parameters = parameters.toUpperCase();
parameters = parameters.split(";") [0];
logger.info("parameters: " + parameters);
} else {
parameters = line.substring(end + 2);
}
parameters = parameters.split(";") [0].trim();
logger.info("parameters: " + parameters);
} catch (StringIndexOutOfBoundsException e) {
numWarning++;
warning.add("VALUE_MISS", row, column + 1, line);
error.addWarning("VALUE_MISS", row, column + 1, line);
memoryCount++;
column = line.length();
continue;
}
MemoryElement tmpMem;
tmpMem = mem.getCellByIndex(memoryCount);
logger.info("line: " + line);
String[] comment = (line.substring(column)).split(";", 2);
if (comment.length == 2) {
logger.info("found comments: " + comment[1]);
tmpMem.setComment(comment[1]);
}
tmpMem.setCode(comment[0]);
if (instr.compareToIgnoreCase(".ASCII") == 0 || instr.compareToIgnoreCase(".ASCIIZ") == 0) {
logger.info(".ascii(z): parameters = " + parameters);
boolean auto_terminate = false;
if (instr.compareToIgnoreCase(".ASCIIZ") == 0) {
auto_terminate = true;
}
try {
List<String> pList = splitStringParameters(parameters, auto_terminate);
for (String current_string : pList) {
logger.info("Current string: [" + current_string + "]");
logger.info(".ascii(z): requested new memory cell (" + memoryCount + ")");
tmpMem = mem.getCellByIndex(memoryCount);
memoryCount++;
int posInWord = 0;
// TODO: Controllo sui parametri (es. virgolette?)
int num = current_string.length();
boolean escape = false;
boolean placeholder = false;
int escaped = 0; // to avoid escape sequences to count as two bytes
for (int tmpi = 0; tmpi < num; tmpi++) {
if ((tmpi - escaped) % 8 == 0 && (tmpi - escaped) != 0 && !escape) {
logger.info(".ascii(z): requested new memory cell (" + memoryCount + ")");
tmpMem = mem.getCellByIndex(memoryCount);
memoryCount++;
posInWord = 0;
}
char c = current_string.charAt(tmpi);
int to_write = (int) c;
logger.info("Char: " + c + " (" + to_write + ") [" + Integer.toHexString(to_write) + "]");
if (escape) {
switch (c) {
case '0':
to_write = 0;
break;
case 'n':
to_write = 10;
break;
case 't':
to_write = 9;
break;
case '\\':
to_write = 92;
break;
case '"':
to_write = 34;
break;
default:
throw new StringFormatException();
}
logger.info("(escaped to [" + Integer.toHexString(to_write) + "])");
escape = false;
c = 0; // to avoid re-entering the escape if branch.
}
if (placeholder) {
if (c != '%' && c != 's' && c != 'd' && c != 'i') {
logger.info("Invalid placeholder: %" + c);
// Invalid placeholder
throw new StringFormatException();
}
placeholder = false;
} else if (c == '%') {
logger.info("Expecting on next step a valid placeholder...");
placeholder = true;
}
if (c == '\\') {
escape = true;
escaped++;
continue;
}
tmpMem.writeByte(to_write, posInWord++);
}
}
} catch (StringFormatException ex) {
logger.info("Badly formed string list");
numError++;
// TODO: more descriptive error message
error.add("INVALIDVALUE", row, 0, line);
}
end = line.length();
} else if (instr.compareToIgnoreCase(".SPACE") == 0) {
int posInWord = 0; //position of byte to write into a doubleword
memoryCount++;
try {
if (Converter.isHexNumber(parameters)) {
parameters = Converter.hexToLong(parameters);
}
if (Converter.isInteger(parameters)) {
int num = Integer.parseInt(parameters);
for (int tmpi = 0; tmpi < num; tmpi++) {
if (tmpi % 8 == 0 && tmpi != 0) {
tmpMem = mem.getCellByIndex(memoryCount);
memoryCount++;
posInWord = 0;
}
tmpMem.writeByte(0, posInWord++);
}
} else {
throw new NumberFormatException();
}
} catch (NumberFormatException ex) {
numError++;
error.add("INVALIDVALUE", row, column + 1, line);
continue;
} catch (IrregularStringOfHexException ex) {
numError++;
error.add("INVALIDVALUE", row, column + 1, line);
continue;
}
posInWord ++;
end = line.length();
} else if (instr.compareToIgnoreCase(".WORD") == 0 || instr.compareToIgnoreCase(".WORD64") == 0) {
logger.info("pamword: " + parameters);
writeIntegerInMemory(row, column, line, parameters, 64, "WORD");
end = line.length();
} else if (instr.compareToIgnoreCase(".WORD32") == 0) {
writeIntegerInMemory(row, column, line, parameters, 32, "WORD32");
end = line.length();
} else if (instr.compareToIgnoreCase(".BYTE") == 0) {
writeIntegerInMemory(row, column, line, parameters, 8, "BYTE");
end = line.length();
} else if (instr.compareToIgnoreCase(".WORD16") == 0) {
writeIntegerInMemory(row, column, line, parameters, 16 , "WORD16");
end = line.length();
} else if (instr.compareToIgnoreCase(".DOUBLE") == 0) {
writeDoubleInMemory(row, column, line, parameters);
end = line.length();
} else {
numError++;
error.add("INVALIDCODEFORDATA", row, column + 1, line);
column = line.length();
continue;
}
}
} else if (line.charAt(end) == ':') {
String label = line.substring(column, end);
logger.info("Processing label " + label);
if (section == FileSection.DATA) {
logger.info("in .data section");
try {
symTab.setCellLabel(memoryCount * 8, label);
} catch (SameLabelsException e) {
// TODO: errore del parser
logger.warning("Label " + label + " is already assigned");
}
} else if (section == FileSection.TEXT) {
logger.info("in .text section");
lastLabel = label;
}
logger.info("done");
} else {
if (section != FileSection.TEXT) {
numError++;
error.add("INVALIDCODEFORDATA", row, column + 1, line);
column = line.length();
continue;
} else if (section == FileSection.TEXT) {
boolean doPack = true;
end++;
Instruction tmpInst;
// Check for halt-like instructions
String temp = cleanFormat(line.substring(column)).toUpperCase();
if (temp.contains("HALT") || temp.contains("SYSCALL 0") || temp.contains("TRAP 0")) {
halt = true;
}
//timmy
for (int timmy = 0; timmy < deprecateInstruction.length; timmy++) {
if (deprecateInstruction[timmy].toUpperCase().equals(line.substring(column, end).toUpperCase())) {
warning.add("WINMIPS64_NOT_MIPS64", row, column + 1, line);
error.addWarning("WINMIPS64_NOT_MIPS64", row, column + 1, line);
numWarning++;
}
}
// Parsing the instruction name to get the type of parameters to expect.
ParsedInstructionMetadata meta = new ParsedInstructionMetadata(row, instrCount+4);
tmpInst = instructionBuilder.buildInstruction(line.substring(column, end).toUpperCase(), meta);
if (tmpInst == null) {
numError++;
error.add("INVALIDCODE", row, column + 1, line);
column = line.length();
continue;
}
// Syntax of the instruction. A string in a form like %R,%R,%I.
// See @{link Instruction#getSyntax getSyntax} for a list of
// symbols to specify the types of parameters.
String syntax = tmpInst.getSyntax();
instrCount += 4;
if (syntax.compareTo("") != 0 && (line.length() < end + 1)) {
numError++;
error.add("UNKNOWNSYNTAX", row, end, line);
column = line.length();
continue;
}
// Parse parameters if needed.
if (syntax.compareTo("") != 0) {
String param = cleanFormat(line.substring(end + 1));
param = param.toUpperCase();
param = param.split(";") [0].trim();
logger.info("param: " + param);
int paramStart = 0;
for (int syntaxIterator = 0; syntaxIterator < syntax.length(); syntaxIterator++) {
if (syntax.charAt(syntaxIterator) == '%') {
syntaxIterator++; // Skip over the %.
var type = syntax.charAt(syntaxIterator);
// If there is another separator (e.g., a comma or a parenthesis)
// save it in nextToken so we can try to find it later.
Character nextToken = ' ';
if (syntaxIterator < syntax.length() - 1) {
nextToken = syntax.charAt(syntaxIterator+1);
}
// Find the end of the parameter value, and as part of that validate
// that the correct separator is present.
int paramEnd;
if (syntaxIterator != syntax.length() - 1) {
paramEnd = param.indexOf(nextToken, paramStart);
} else {
paramEnd = param.length();
}
if (paramEnd == -1) {
numError++;
error.add("SEPARATORMISS", row, paramStart, line);
column = line.length();
tmpInst.getParams().add(0);
continue;
}
var paramValue = param.substring(paramStart, paramEnd);
// %R: General Purpose Register.
if (type == 'R') {
int reg;
if ((reg = isRegister(paramValue.trim())) >= 0) {
tmpInst.getParams().add(reg);
paramStart = paramEnd + 1;
} else {
numError++;
error.add("INVALIDREGISTER", row, line.indexOf(paramValue) + 1, line);
tmpInst.getParams().add(0);
column = line.length();
continue;
}
// %F: Floating Point Register.
} else if (type == 'F') {
int reg;
if ((reg = isRegisterFP(paramValue.trim())) >= 0) {
tmpInst.getParams().add(reg);
paramStart = paramEnd + 1;
} else {
numError++;
error.add("INVALIDREGISTER", row, line.indexOf(paramValue) + 1, line);
tmpInst.getParams().add(0);
column = line.length();
continue;
}
// %I: 16-bit signed immediate.
// Also allows a memory label as a parameter -- the address will be used as immediate value.
} else if (type == 'I') {
long immediateValue = 0;
String errorMessage = "";
try {
immediateValue = Converter.parseImmediate(paramValue);
} catch (NumberFormatException e) {
// Invalid number, try to parse it as a memory label.
MemoryElement tmpMem;
try {
tmpMem = symTab.getCell(paramValue.trim());
immediateValue = tmpMem.getAddress();
} catch (MemoryElementNotFoundException ex) {
errorMessage = "INVALIDIMMEDIATE";
}
}
if (errorMessage.isEmpty() && (immediateValue < -32768 || immediateValue > 32767)) {
errorMessage = "IMMEDIATE_TOO_LARGE";
immediateValue = 0;
}
// Casting to int is safe because we know the value is between -32768 and 32767.
tmpInst.getParams().add((int)immediateValue);
if (!errorMessage.isEmpty()) {
numError++;
error.add(errorMessage, row, line.indexOf(paramValue) + 1, line);
column = line.length();
continue;
}
// %U: 5-bit unsigned immediate.
// %C: 3-bit unsigned immediate.
} else if (type == 'U' || type == 'C') {
// The code for %U and %C is the same, only the upper bound changes (and the overflow error message).
long maxValue = type == 'U' ? 31 : 7;
String overflowErrorMessage = type == 'U' ? "5BIT_IMMEDIATE_TOO_LARGE" : "3BIT_IMMEDIATE_TOO_LARGE";
long immediateValue = 0;
String errorMessage = ""; // Will be populated with the error message string if there's an error.
try {
immediateValue = Converter.parseImmediate(paramValue);
if (immediateValue < 0) {
errorMessage = "VALUEISNOTUNSIGNED";
} else if (immediateValue > maxValue) {
errorMessage = overflowErrorMessage;
immediateValue = 0;
} else {
paramStart = paramEnd + 1;
}
} catch (NumberFormatException e) {
errorMessage = "INVALIDIMMEDIATE";
}
// Casting to int is safe because we know the value is between 0 and 31.
tmpInst.getParams().add((int)immediateValue);
if (!errorMessage.isEmpty()) {
numError++;
error.add(errorMessage, row, line.indexOf(paramValue) + 1, line);
column = line.length();
continue;
}
// %L: Memory Label.
} else if (type == 'L') {
try {
MemoryElement tmpMem;
if (paramValue.equals("")) {
tmpInst.getParams().add(0);
} else if (Converter.isInteger(paramValue.trim())) {
int tmp = Integer.parseInt(paramValue.trim());
if (tmp < Memory.MIN_OFFSET_BYTES || tmp > Memory.MAX_OFFSET_BYTES) {
numError++;
String er = "LABELADDRESSINVALID";
error.add(er, row, line.indexOf(paramValue) + 1, line);
column = line.length();
paramStart = paramEnd + 1;
tmpInst.getParams().add(0);
continue;
}
tmpInst.getParams().add(tmp);
} else {
tmpMem = symTab.getCell(paramValue.trim());
tmpInst.getParams().add(tmpMem.getAddress());
}
paramStart = paramEnd + 1;
} catch (MemoryElementNotFoundException e) {
numError++;
error.add("LABELNOTFOUND", row, line.indexOf(paramValue) + 1, line);
column = line.length();
paramStart = paramEnd + 1;
tmpInst.getParams().add(0);
continue;
}
// %E: Program label used for Jump instructions.
} else if (type == 'E') {
String label = paramValue.trim();
Integer labelAddr = symTab.getInstructionAddress(label);
logger.info("Label " + label + " at address " + labelAddr);
if (labelAddr != null) {
tmpInst.getParams().add(labelAddr);
} else {
VoidJump tmpVoid = new VoidJump();
tmpVoid.instr = tmpInst;
tmpVoid.row = row;
tmpVoid.line = line;
tmpVoid.column = paramStart;
tmpVoid.label = label;
voidJump.add(tmpVoid);
doPack = false;
}
// %B: Program label used for Branch instructions.
} else if (type == 'B') {
Integer labelAddr = symTab.getInstructionAddress(paramValue.trim());
if (labelAddr != null) {
labelAddr -= instrCount + 4;
tmpInst.getParams().add(labelAddr);
} else {
VoidJump tmpVoid = new VoidJump();
tmpVoid.instr = tmpInst;
tmpVoid.row = row;
tmpVoid.line = line;
tmpVoid.column = paramStart;
tmpVoid.label = paramValue;
tmpVoid.instrCount = instrCount;
tmpVoid.isBranch = true;
voidJump.add(tmpVoid);
doPack = false;
}
} else {
numError++;
error.add("UNKNOWNSYNTAX", row, 1, line);
column = line.length();
tmpInst.getParams().add(0);
continue;
}
// Ugly hack. The code used to rely on "peeking" behavior inside the
// parsing of every parameter to actually bring forward the index variable
// within each iteration. Since we removed the peeking, we still need to move
// it forward, waiting for a better version of this code that doesn't need
// this index munging.
if (syntaxIterator != syntax.length() - 1) {
syntaxIterator++;
}
} else {
if (syntax.charAt(syntaxIterator) != param.charAt(paramStart++)) {
numError++;
error.add("UNKNOWNSYNTAX", row, 1, line);
column = line.length();
tmpInst.getParams().add(0);
continue;
}
}
}
if (column == line.length()) {
continue;
}
}
try {
if (doPack) {
tmpInst.pack();
}
} catch (IrregularStringOfBitsException e) {
logger.severe("Irregular string of bits: " + e.getMessage());
}
logger.info("row: " + line);
String comment[] = line.split(";", 2);
tmpInst.setFullName(replaceTab(comment[0].substring(column)));
tmpInst.setFullName(replaceTab(comment[0].substring(column)));
tmpInst.setFullName(replaceTab(comment[0].substring(column)));
tmpInst.setFullName(replaceTab(comment[0].substring(column)));
if (comment.length == 2) {
tmpInst.setComment(comment[1]);
}
try {
mem.addInstruction(tmpInst, instrCount);
if (lastLabel != null && !lastLabel.equals("")) {
logger.info("About to add label: " + lastLabel);
symTab.setInstructionLabel(instrCount, lastLabel.toUpperCase());
}
} catch (SymbolTableOverflowException ex) {
if (isFirstOutOfInstructionMemory) { //is first out of memory?
isFirstOutOfInstructionMemory = false;
numError++;
error.add("OUTOFINSTRUCTIONMEMORY", row, column + 1, line);
column = line.length();
continue;
}
} catch (SameLabelsException ex) {
numError++;
error.add("SAMELABEL", row, 1, line);
column = line.length();
}
// Il finally e' totalmente inutile, ma Ú bello utilizzarlo per la
// prima volta in un programma ;)
finally {
lastLabel = "";
}
end = line.length();
}
}
column = end;
} catch (MemoryElementNotFoundException ex) {
// Even if we don't report the error, advance i to prevent other errors from
// being generated on this line.
column = line.length();
if (isFirstOutOfMemory) { //is first out of memory?
isFirstOutOfMemory = false;
numError++;
error.add("OUTOFMEMORY_PARSER", row, column + 1, line);
continue;
}
} catch (IrregularWriteOperationException ex) {
numError++;
error.add("INVALIDVALUE", row, column + 1, line);
break;
}
}
}
for (int i = 0; i < voidJump.size(); i++) {
Integer labelAddr = symTab.getInstructionAddress(voidJump.get(i).label.trim());
if (labelAddr != null) {
if (voidJump.get(i).isBranch) {
labelAddr -= voidJump.get(i).instrCount + 4;
}
voidJump.get(i).instr.getParams().add(labelAddr);
try {
voidJump.get(i).instr.pack();
} catch (IrregularStringOfBitsException ex) {
logger.severe("Irregular string of bits: " + ex.getMessage());
}
} else {
numError++;
error.add("LABELNOTFOUND", voidJump.get(i).row, voidJump.get(i).column , voidJump.get(i).line);
continue;
}
}
if (!halt) { //if Halt is not present in code
numWarning++;
warning.add("HALT_NOT_PRESENT", row, 0, "");
error.addWarning("HALT_NOT_PRESENT", row, 0, "");
try {
logger.warning("No terminating instruction detected, adding one.");
ParsedInstructionMetadata meta = new ParsedInstructionMetadata(row, instrCount+4);
Instruction tmpInst = instructionBuilder.buildInstruction("SYSCALL", meta);
tmpInst.getParams().add(0);
tmpInst.setFullName("SYSCALL 0");
try {
tmpInst.pack();
} catch (IrregularStringOfBitsException ex) {
logger.severe("Irregular string of bits: " + ex.getMessage());
}
mem.addInstruction(tmpInst, (instrCount + 4));
symTab.setInstructionLabel((instrCount + 4), "");
} catch (SymbolTableOverflowException ex) {
if (isFirstOutOfInstructionMemory) { //is first out of memory?
numError++;
error.add("OUTOFINSTRUCTIONMEMORY", row, 0, "Halt");
}
} catch (SameLabelsException ex) {
logger.severe("Same labels: " + ex);
} // impossible
}
if (numError > 0) {
throw error;
} else if (numWarning > 0) {
throw warning;
}
}
/** Clean multiple tab or spaces in a badly formatted String.
* @param s the badly formatted String
* @return the cleaned String
*/
private String cleanFormat(String s) {
if (s == null) {
logger.warning("Null string passed to cleanFormat, returning an empty string.");
return "";
}
if (s.length() > 0 && s.charAt(0) != ';' && s.charAt(0) != '\n') {
s = s.trim();
s = s.replace("\t", " ");
while (s.contains(" ")) {
s = s.replace(" ", " ");
}
s = s.replace(", ", ",");
s = s.replace(" ,", ",");
if (s.length() > 0) {
return s;
}
}
return "";
}
/** Check if is a valid string for a register
* @param reg the string to validate
* @return -1 if reg isn't a valid register, else a number of register
*/
private int isRegister(String reg) {
try {
int num;
if (reg.charAt(0) == 'r' || reg.charAt(0) == 'R' || reg.charAt(0) == '$') //ci sono altri modi di scrivere un registro???
if (Converter.isInteger(reg.substring(1))) {
num = Integer.parseInt(reg.substring(1));
if (num < 32 && num >= 0) {
return num;
}
}
if (reg.charAt(0) == '$' && (num = isAlias(reg.substring(1))) != -1) {
return num;
}
} catch (Exception ignored) {}
return -1;
}
/** Check if is a valid string for a floating point register
* @param reg the string to validate
* @return -1 if reg isn't a valid register, else a number of register
*/
private int isRegisterFP(String reg) {
try {
int num;
if (reg.charAt(0) == 'f' || reg.charAt(0) == 'F' || reg.charAt(0) == '$')
if (Converter.isInteger(reg.substring(1))) {
num = Integer.parseInt(reg.substring(1));
if (num < 32 && num >= 0) {
return num;
}
}
// TODO: is it correct to use those aliases for FP registers?
if (reg.charAt(0) == '$' && (num = isAlias(reg.substring(1))) != -1) {
return num;
}
} catch (Exception ignored) {}
return -1;
}
/** Check if the parameter is a valid string for an alias-register
* @param reg the string to validate
* @return -1 if reg isn't a valid alias-register, else a number of
register
*/
int isAlias(String reg) {
for (AliasRegister x : AliasRegister.values()) {
if (reg.equalsIgnoreCase(x.name())) {
return x.ordinal();