-
Notifications
You must be signed in to change notification settings - Fork 1
/
compileOISC.java
executable file
·927 lines (791 loc) · 30.5 KB
/
compileOISC.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
import java.io.*;
import java.util.*;
import java.util.regex.Pattern;
/**
* Converts a file written in Paksoy Kader OISC Programming Language (PKOPL) into
* binary code that runs on our OISC chip. The output files are readable by the VHDL
* compiler.
* <br>
* Run using: <tt>java compileOISC <source text file> [output file]</tt>
* <br>If output file is not specified name "compiled.mif" is assumed
* <br><br>
*
* <h3>PKOPL Syntax and Operators</h3>
* PKOPL is a simple programming language allows variable definitions. It uses only
* a single instruction in implementing its capabilities. It is a good idea to perform
* all operations on variables since almost all instructions only take memory addresses
* as arguments. In fact, the only instruction that takes immediate values is DEF (define).
*
* <h4>Operators</h4>
* <ul>
* <li> <tt>JMP m:</tt> jump to memory loca tion m
* <li> <tt>JMPI a:</tt> jump to memory location stored in address a (m(a))
* <li> <tt>SUB a b c:</tt> m(c) = m(b) - m(a)
* <li> <tt>ADD a b c:</tt> m(c) = m(a) + m(b)
* <li> <tt>DIV a b c:</tt> m(c) = m(a) / m(b)
* <li> <tt>MUL a b c:</tt> m(c) = m(a) * m(b)
* <li> <tt>IFGT a b c:</tt> IF a>b THEN JMP c
* <li> <tt>IFLE a b c:</tt> IF a<=b THEN JMP c
* <li> <tt>DEF x <i>constant</i>:</tt> define variable 'x' to be <i>constant</i>
* <li> <tt>MOV a b:</tt> move m(a) to m(b)
* </ul>
* <h4>Syntax</h4>
* <b>General</b>
* <br>
* Variable definitions need to be placed in the beginning of a file.
* <br>Only one operation is read from each line, everything else is ignored. So
* comments can be placed on lines after all operands.
* <br> Variables values are parsed as 8-bit two's complement binary integers.
* These values are only used for generating the initial RAM content. In the compiler
* the variables are parsed as the memory address reserved to them. To create a pointer
* define a variable x, then define another variable y as x. This will use the address
* of x as the value for y.
* <br><i>Example:</i>
* <br><tt>DEF A 10</tt> -Address FE is allocated to A, contents of the address will be
* initialized with 0A (number 10)
* <br><tt>DEF B A</tt> -Address FD is allocated to B, contents of the address will be
* initialized with FE (address of A)
* <br>Addresses are 8-bit unsigned binary integers.
* <br>
* <b>Constant expressions:</b>
* <ul>
* <li> Hex form: preceed by '$' <i>ex:</i> $F0C1
* <li> Binary form: preceed by '#'. <i>ex:</i> #1000100
* <li> Decimal form: any operand consisting only of numbers. <i>ex:</i> 1231
* </ul>
*
* <br>
* <i>
* <br>Multiply and divide OISC algorithms based on code at
* http://www.cse.psu.edu/~cg331/samp/OISC/macros
* <br>Part of project OISCcompiler
* <br>Created on May 11, 2005, using eclipse 3.1
* <br>Written in Java 5
* </i>
*
* @author Paksoy Kader
*/
public class compileOISC {
private int heapPt, pCount;
/**Definitions end flag*/
private boolean defEnd;
/**Variable lookup table*/
private Hashtable<String,variable> variables;
/**
* theInstruction is the normal instruction that uses direct addressing for all
* parameters
* <br>loadInstruction take a parameter as immediate value and stores it at b,
* jumps to c if b<=0
* <br>cr is the line terminator for this platform, courtesy of
* http://www.javapractices.com/Topic42.cjp
*/
public static String theInstruction = "0", cr=System.getProperty("line.separator"),
loadInstruction = "1";
/**maximum number of variables allowed*/
private static int maxVar = 100;
/**heapPtStart is the lowest point of the heap*/
private static Integer heapPtStart = 249;
/**Reserved memory addresses for registers*/
public static String negOneRegister="11111110",tempAReg="11111101",tempBReg="11111100",
tempCReg="11111011",zeroRegister="11111010";
/**Constructor initializes local variables*/
compileOISC() {
heapPt = heapPtStart;
pCount = 0;
defEnd = false;
variables = new Hashtable<String,variable>();
}
/**Private variable class represents variables*/
private class variable {
private int memLoc,value;
private String memLocBin, valueBin, name;
variable(String nname, int mem, int val) {
value = val;
memLoc = mem;
memLocBin = decToBin(mem);
name = nname;
}
public String getMemLocBin() {
return memLocBin;
}
public int getMemLoc() {
return memLoc;
}
public void setMemLoc(int newloc) {
memLoc = newloc;
memLocBin = decToBin(newloc);
}
public int getValue() {
return value;
}
public void setValue(byte newval) {
value = newval;
}
public String getName() {
return name;
}
public void setName(String nname){
name = nname;
}
}
/**
* Expand hex representation to binary
* @param hex number in hex form, should be 2 chars
* @return number in 8-bit binary form
*/
public static String hexToBin(String hex) {
char[] hexRep = hex.toCharArray();
int numchars = hexRep.length;
//check input length
if (numchars>2) {
System.out.println("compileOISC:hexToBin:hex representation too long, " +
"high bits will be discarded. Input: \""+hex+"\".");
numchars=2;
}
else if (numchars<2)
System.out.println("compileOISC:hexToBin:hex representation too short, " +
"leading 0's will be added. Input: \""+hex+"\".");
StringBuffer binaryRep = new StringBuffer();
for (int i=0;i<numchars;i++) {
//First convert hex representation to integer and then retreive in
//binary form
String singlehex = Integer.toBinaryString(
Integer.parseInt(((Character) hexRep[i]).toString(),16));
//fill in missing leading zero's
for (int j=singlehex.length();j<4;j++)
singlehex = "0"+singlehex;
//append to result string
binaryRep.append(singlehex);
}
for (int i=numchars;i<2;i++)
binaryRep.insert(0,"0000");
return binaryRep.toString();
}
/**Convert integer to its 8bit unsigned binary representation*/
public static String decToBin (int dec) {
//low bound
if (dec<=0)
return "00000000";
//upper bound
else if (dec>=255)
return "11111111";
else {
//generate binary string
String ret = Integer.toBinaryString(dec);
int retlen = ret.length();
//fill in preceding zeros if necessary
for (int i=retlen;i<8;i++)
ret = "0" + ret;
//discard unnecessary preceding zeros
ret = ret.substring((ret.length()-8));
return ret;
}
}
/**
* Make given binary number 8 bits long, by either padding with leading
* or truncating high bits.
*
* @param bin input binary number
* @return 8-bit long binary number
*/
public static String binToBin(String bin) {
String ret = bin;
//fill in preceeding zeros
for (int i=bin.length();i<8;i++)
ret = "0" + ret;
//discard unnecessary preceding zeros
ret = ret.substring((ret.length()-8));
return ret;
}
/**Compiles given source code file, and writes the result to
* given target file
* @param inputFile file that contains source
* @param targetFile file to write to
*/
public static void compile(String inputFile, String targetFile) {
System.out.println("Reading source from file: "+inputFile);
int counter = 0;
//Scanner reads input file
Scanner source = null;
try {
source = new Scanner(new FileInputStream(inputFile));
} catch (FileNotFoundException e) {
System.out.println("Cannot find file: "+inputFile);
System.exit(0);
}
//create instance of compiler class to start reading
compileOISC compiler = new compileOISC();
//file write library usage code taken from:
//http://www.javapractices.com/Topic42.cjp
Writer output = null;
try {
StringBuffer sourceoutput = new StringBuffer();
output = new BufferedWriter( new FileWriter(targetFile) );
//write mif headers
output.write("DEPTH = 256;");
output.write(cr);
output.write("WIDTH = 25;"+cr+
"ADDRESS_RADIX = BIN;"+cr+
"DATA_RADIX = BIN;"+cr+
"CONTENT"+cr+
"BEGIN"+cr);
//load useful values
sourceoutput.append(compiler.loadRefVals());
//Read all lines
while (source.hasNextLine())
sourceoutput.append(compiler.compileLine(source.nextLine(),
counter++));
//write result
output.write(sourceoutput.toString());
//fill lines upto 256
for (int i = compiler.getPCount();i<256;i++)
output.write(decToBin(i)+" : 0000000000000000000000000 ;"+cr);
//write end mark
output.write("END;"+cr);
} catch (IOException e) {
System.out.println("compileOISC:compile:error when writing to " +
"file.");
System.exit(0);
}
finally {
//flush and close both "output" and its underlying FileWriter
if (output != null)
try {
output.close();
} catch (IOException e) {
System.out.println("compileOISC:compile:error whilr closing " +
"output stream.");
System.exit(0);
}
}
System.out.println("Finished compiling file "+inputFile+" "+
counter+" lines read.");
}
/**Accessor fir program counter*/
public int getPCount(){
return pCount;
}
/**Compiles a given line of source code.
* <br>Each line can only contain one operator. Operands are extracted
* and passed onto seperatemethods that handle different operations, where
* they are parsed further.
*
* @param input string to compile
* @return String assembly code for this line
*/
private String compileLine(String input, int linenum) {
Scanner linereader = new Scanner(input);
//check if line empty
if (!linereader.hasNext())
return "";
String oper = linereader.next();
StringBuffer ret = new StringBuffer();
//parse operator
try {
if (oper.equals("DEF"))
ret.append(define(linereader.next(),
Byte.parseByte(linereader.next())));
else if (oper.equals("JMP"))
ret.append(jump(linereader.next()));
else if (oper.equals("ADD"))
ret.append(add(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("SUB"))
ret.append(sub(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("DIV"))
ret.append(divide(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("MUL"))
ret.append(multiply(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("IFGT"))
ret.append(ifgt(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("IFLE"))
ret.append(ifle(linereader.next(),linereader.next(),
linereader.next()));
else if (oper.equals("MOV"))
ret.append(move(linereader.next(),linereader.next()));
//parse failed
else {
System.out.println("compileOISC:compileLine:Cannot parse operator \""
+oper+"\" on line " +linenum+", skipping line.");
return "";
}
} catch (NoSuchElementException e) {
System.out.println("compileOISC:compileLine:need more operands for " +
oper+ " on line "+linenum+".");
}
return ret.toString();
}
/**
* Creates a variable and reserves it space in the heap,
* if maximum number of variables is exceeded displays error message.
*
* @param varname name of variable to create
* @param val value to initially assign to variable
* @return instruction to initialize mem loc with val
*/
private String define(String varname, int val) {
if (defEnd) {
System.out.println("compileOISC:define:" +
"can only define variables at beginning of file.");
return "";
}
//check if variable name is only composed of numbers
else if (Pattern.matches("\\d+",varname)) {
System.out.println("compileOISC:define:" +
"invalid variable name "+varname+" must also contain non-numeric " +
"characters.");
return "";
}
//check if variable already exists
variable oldvar = variables.get(varname);
if (oldvar==null) {
//check if number of max variables is exceeded
if ((heapPtStart-heapPt)>=maxVar) {
System.out.println("compileOISC:define:out of heap space," +
" maximum number of variables exceeded. cannot define" +
" new variable "+varname+".");
return "";
}
//create new variable object
variable nvar = new variable(varname,heapPt,val);
String ret = loadABC(intToBin(val),parseOperand(heapPt)).
toString();
//put new variable in hashtable
variables.put(varname,nvar);
//move heapPt down
heapPt--;
return ret;
}
//if variable already exists redefine
if (varname.equals("ioPort")) {
System.out.println("compileOISC:define:" +
"ioPort is a reserved variable name.");
return "";
}
variables.put(varname,new variable(varname,oldvar.getMemLoc(),
val));
return loadABC(intToBin(val),oldvar.getMemLocBin()).
toString();
}
/**Load useful values*/
private StringBuffer loadRefVals() {
StringBuffer ret = new StringBuffer();
ret.append(loadABC("00000000",zeroRegister));
ret.append(loadABC("11111111",negOneRegister));
ret.append(loadABC("00000000",tempAReg));
ret.append(loadABC("00000000",tempBReg));
ret.append(loadABC("00000000",tempCReg));
return ret;
}
/**
* Generates assembly code that adds two variables and stores
* @param a first variable
* @param b second variable
* @param c dest variable
* @return source code that does c = a + b
*/
private StringBuffer add(String a, String b, String c) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip line
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
//generate assembly code
//combine variables in temporary register
ret.append(insABC(operA,zeroRegister));
ret.append(insABC(operB,zeroRegister));
//clear destination and store
ret.append(insABC(operC,operC));
ret.append(insABC(zeroRegister,operC));
//clear zero register
ret.append(insABC(zeroRegister,zeroRegister));
return ret;
}
/**
* Generates assembly code that stores the result of
* subtracting a from b, to b
* @param a first variable
* @param b second variable
* @param c destination var
* @return source code that does c = b - a
*/
private StringBuffer sub(String a, String b, String c) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
//generate assembly code
ret.append(clearReg(tempAReg));
ret.append(clearReg(tempBReg));
//ta = -a
ret.append(insABC(operA,tempAReg));
//tb = -b
ret.append(insABC(operB,tempBReg));
//ta = b-a
ret.append(insABC(tempBReg,tempAReg));
ret.append(insABC(operC,operC));
ret.append(clearReg(tempBReg));
ret.append(insABC(tempAReg,tempBReg));
ret.append(insABC(tempBReg,operC));
return ret;
}
/**
* Generates assembly code that jumps to a
*
* @param a jump loacta?on
* @return jump to a
*/
private StringBuffer jump(String a) {
if (!defEnd)
defEnd = true;
String operA = parseOperand(a);
//if operand doesn't parse skip line
if (operA==null)
return new StringBuffer();
pCount++;
return insABC(zeroRegister,zeroRegister,operA);
}
/**
* Jump indirect: jump to location in m(a)
*
* @param a pointer location
* @return jump to m(a)
*/
private StringBuffer jumpi(String a) {
if (!defEnd)
defEnd = true;
String operA = parseOperand(a);
//if operand doesn't parse skip line
if (operA==null)
return new StringBuffer();
StringBuffer ret = new StringBuffer();
//zr=-m(a)
ret.append(insABC(operA,zeroRegister));
//tempa=0
ret.append(insABC(tempAReg,tempAReg));
//tempa=m(a)
ret.append(insABC(zeroRegister,tempAReg));
//zr=0, jump to m(a)
pCount++;
ret.append(insABC(zeroRegister,zeroRegister,
tempAReg));
return ret;
}
/**
* m(b) = m(a)
* @param a mem loc a
* @param b mem loc b
* @return assembly code that does m(b) = m(a)
*/
private StringBuffer move(String a, String b) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null))
return new StringBuffer();
//generate assembly code
//clear destination
ret.append(insABC(operB,operB));
ret.append(insABC(operA,zeroRegister));
ret.append(insABC(zeroRegister,operB));
//clear zeroRegister
ret.append(insABC(zeroRegister,zeroRegister));
return ret;
}
/**
* Integer division b by a and store result in b
*
* @param a operand a
* @param b operand b
* @param c destination addr c
* @return m(c) = m(b) / m(a)
*/
private StringBuffer divide(String a,String b,String c) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
//Clear tempA
ret.append(clearReg(tempAReg));
ret.append(clearReg(tempBReg));
ret.append(clearReg(tempCReg));
//load temp regs
//tempb = b
ret.append(insABC(operB,tempAReg));
ret.append(insABC(tempAReg,tempBReg));
//tempc = a
ret.append(clearReg(tempAReg));
ret.append(insABC(operA,tempAReg));
ret.append(insABC(tempAReg,tempCReg));
ret.append(clearReg(operC));
//c++
ret.append(insABC(negOneRegister,operC));
pCount++;
ret.append(insABC(tempCReg,tempBReg,
parseOperand(pCount+1)));
pCount++;
ret.append(insABC(zeroRegister,zeroRegister,
parseOperand(pCount-3)));
return ret;
}
/**
* Multiply a by b and store in c
*
* @param a operand a
* @param b operand b
* @param c destination addr c
*
* @return m(c) = m(b) * m(a)
*/
private StringBuffer multiply(String a,String b,String c) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
//Clear registers
ret.append(clearReg(tempAReg));
ret.append(clearReg(tempBReg));
ret.append(clearReg(tempCReg));
//tempb = b
ret.append(insABC(operB,tempAReg));
ret.append(insABC(tempAReg,tempBReg));
//tempc= 1
ret.append(insABC(negOneRegister,tempCReg));
//tempa = -a
ret.append(clearReg(tempAReg));
ret.append(insABC(operA,tempAReg));
//clear destination
ret.append(clearReg(operC));
//sub -a from dest and b--, when b<=0 esc
ret.append(insABC(tempAReg,operC));
pCount++;
ret.append(insABC(tempCReg,tempBReg,
parseOperand(pCount+1)));
//loop back
pCount++;
ret.append(insABC(zeroRegister,zeroRegister,
parseOperand(pCount-3)));
return ret;
}
/**
* Jump to c if m(a)<=m(b)
*
* @param a operand a
* @param b operand b
* @param c jump location
* @return jmp c if m(a)<=m(b)
*/
private StringBuffer ifle(String a,String b,String c) {
if (!defEnd)
defEnd = true;
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
StringBuffer ret = new StringBuffer();
//tempAReg = 0
ret.append(insABC(tempAReg,tempAReg));
//tempAREg = -a
ret.append(insABC(operA,tempAReg));
//tempBReg = 0
ret.append(insABC(tempBReg,tempBReg));
//tempBReg = a
ret.append(insABC(tempAReg,tempBReg));
pCount++;
//tempBReg = a-b, if tempBReg>=0 jmp
ret.append(insABC(operB,tempBReg,
operC));
return ret;
}
/**
* Jump to c if m(a)>m(b)
*
* @param a operand
* @param b operand
* @param c jump location
* @return branching code
*/
private StringBuffer ifgt(String a, String b, String c) {
if (!defEnd)
defEnd = true;
StringBuffer ret = new StringBuffer();
String operA = parseOperand(a);
String operB = parseOperand(b);
String operC = parseOperand(c);
//if parse fails for any of the operands skip this line
//of source
if ((operA==null)||(operB==null)||(operC==null))
return new StringBuffer();
//tempA = 0
ret.append(insABC(tempAReg,tempAReg));
//tempA = -A
ret.append(insABC(operA,tempAReg));
//tempB = 0
ret.append(insABC(tempBReg,tempBReg));
//tempB = -B
ret.append(insABC(operB,tempBReg));
pCount++;
//tempA = B-A, if tempA<=0 jmp c
ret.append(insABC(tempBReg,tempAReg,
operC));
return ret;
}
/**
* If operand is a number, assumes decimal representation<br>
* If constant prefix exists, operand ?s parsed accordingly
* If neither is true, operand is treated as variable
*
* @param operand raw operand
* @return 8-bit binary address
*/
private String parseOperand(String operand) {
//check if operand needs to be treated as decimal
if (Pattern.matches("\\d+",operand))
return decToBin(Integer.parseInt(operand));
//check binary prefix
else if (operand.startsWith("#"))
return binToBin(operand.substring(1));
//check hex prefix
else if (operand.startsWith("$"))
return hexToBin(operand.substring(1));
//lookup variable from table
variable var = variables.get(operand);
//check if variable exists
if (var==null) {
System.out.println("compileOISC:parseOperand:" +
"cannot find variable: "+operand);
return null;
}
//return address assigned to variable
return var.getMemLocBin();
}
/**parseOperand wrapper for handling integers directly*/
private String parseOperand(int operand) {
return decToBin(operand);
}
/**Convert byte value to two's complement binary*/
public static String intToBin(int operand) {
//check bounds
if (operand>=127)
return "01111111";
else if (operand<=-127)
return "10000000";
else if (operand==0)
return "00000000";
else {
String binRep = "";
//if number is positive just create binary representation
//and pad with leading zeros
if (operand>0) {
binRep = Integer.toBinaryString(operand);
for (int i=binRep.length();i<8;i++)
binRep = "0"+binRep;
}
else {
int diff = operand + 128,i=64;
while (diff!=0) {
if (diff>=i) {
diff -= i;
binRep = binRep+"1";
}
else
binRep = binRep+"0";
i = i/2;
}
binRep = "1"+binRep;
}
return binRep;
}
}
/**Makes a single theInstruction using given parsed operands*/
private StringBuffer insABC(String a, String b, String c) {
StringBuffer ret = new StringBuffer();
ret.append(decToBin(pCount-1)+" : ");
ret.append(theInstruction);
ret.append(a);
ret.append(b);
ret.append(c);
ret.append(" ;"+cr);
return ret;
}
/**Wrapper for insABC that assumes jump to next line*/
private StringBuffer insABC(String a, String b) {
pCount++;
return insABC(a,b,parseOperand(pCount));
}
/**Makes a single loadInstruction using given parsed operands*/
private StringBuffer loadABC(String a,String b,String c) {
StringBuffer ret = new StringBuffer();
//memory address
ret.append(decToBin(pCount-1)+" : ");
ret.append(loadInstruction);
ret.append(a);
ret.append(b);
ret.append(c);
ret.append(" ;"+cr);
return ret;
}
/**Wrapper for loadABC that assumes jump to next line*/
private StringBuffer loadABC(String a, String b) {
pCount++;
return loadABC(a,b,parseOperand(pCount));
}
/**Returns code that clears given register*/
private StringBuffer clearReg(String a) {
return insABC(a,a);
}
/**Main method handles command line input*/
public static void main(String[] args) {
try {
String sourceFile = args[0];
String targetFile;
if (args.length>1) {
targetFile = args[1];
System.out.println("Using output file: "+targetFile);
}
else {
targetFile = "compiled.mif";
System.out.println("Using default output file: "+targetFile);
}
//Compile
compileOISC.compile(sourceFile,targetFile);
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Invalid input, you need to specify source file.");
System.exit(0);
}
}
}