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
|
package svm;
import java.util.HashMap;
public class SVMVisitorImpl extends SVMBaseVisitor<Void> {
public AssemblyClass[] code = new AssemblyClass[ExecuteVM.CODESIZE];
private int i = 0;
private HashMap<String,Integer> labelAdd = new HashMap<String,Integer>();
// definisce a quale linea di codice corrisponde una etichetta
private HashMap<Integer,String> labelRef = new HashMap<Integer,String>();
// definisce l'insieme di linee di codice che contengono una certa etichetta
public Void visitAssembly(SVMParser.AssemblyContext ctx) {
visitChildren(ctx);
//printlabeladd() ; // checks
//printlabelref() ; // checks
for (Integer refAdd : labelRef.keySet()) {
int tmp = refAdd ;
String s = labelRef.get(refAdd) ;
if (code[tmp] == null) {
code[tmp] = new AssemblyClass(labelAdd.get(s), null, null, null);
} else {
code[tmp].setArg1(labelAdd.get(s).toString());
}
}
return null;
}
public void printlabeladd() {
for (String m : labelAdd.keySet()) {
System.out.println("chiave: " + m + " valore: " + labelAdd.get(m)) ;
}
}
public void printlabelref() {
for (Integer m : labelRef.keySet()) {
System.out.println("indirizzo: " + m + " etichetta: " + labelRef.get(m)) ;
}
}
public Void visitInstruction(SVMParser.InstructionContext ctx) {
switch (ctx.getStart().getType()) {
case SVMLexer.LOAD:
code[i] = new AssemblyClass(SVMParser.LOAD, ctx.REG(0).toString(), ctx.NUMBER().toString(),ctx.REG(1).toString());
i = i+1 ;
break;
case SVMLexer.STORE:
code[i] = new AssemblyClass(SVMParser.STORE, ctx.REG(0).toString(), ctx.NUMBER().toString(),ctx.REG(1).toString());
i = i+1 ;
break;
case SVMLexer.STOREI:
code[i] = new AssemblyClass(SVMParser.STOREI, ctx.REG(0).toString(), ctx.NUMBER().toString(), null);
i = i+1 ;
break;
case SVMLexer.MOVE:
code[i] = new AssemblyClass(SVMParser.MOVE, ctx.REG(0).toString(), ctx.REG(1).toString(), null);
i = i+1 ;
break;
case SVMLexer.ADD:
code[i] = new AssemblyClass(SVMParser.ADD, ctx.REG(0).toString(), ctx.REG(1).toString(), null);
i = i+1 ;
break;
case SVMLexer.ADDI:
code[i] = new AssemblyClass(SVMParser.ADDI, ctx.REG(0).toString(), ctx.NUMBER().toString(), null);
i = i+1 ;
break;
case SVMLexer.SUB:
code[i] = new AssemblyClass(SVMParser.SUB, ctx.REG(0).toString(), ctx.REG(1).toString(), null);
i = i+1 ;
break;
case SVMLexer.SUBI:
code[i] = new AssemblyClass(SVMParser.SUBI, ctx.REG(0).toString(), ctx.NUMBER().toString(), null);
i = i+1 ;
break;
case SVMLexer.MUL:
code[i] = new AssemblyClass(SVMParser.MUL, ctx.REG(0).toString(), ctx.REG(1).toString(), null);
i = i+1 ;
break;
case SVMLexer.MULI:
code[i] = new AssemblyClass(SVMParser.MULI, ctx.REG(0).toString(), ctx.NUMBER().toString(), null);
i = i+1 ;
break;
case SVMLexer.DIV:
code[i] = new AssemblyClass(SVMParser.DIV, ctx.REG(0).toString(), ctx.REG(1).toString(), null);
i = i+1 ;
break;
case SVMLexer.DIVI:
code[i] = new AssemblyClass(SVMParser.DIVI, ctx.REG(0).toString(), ctx.NUMBER().toString(), null);
i = i+1 ;
break;
case SVMLexer.PUSH:
if (ctx.n != null) {
code[i] = new AssemblyClass(SVMParser.PUSH, ctx.n.getText(), null, null);
} else {
code[i] = new AssemblyClass(SVMParser.PUSH, ctx.l.getText(), null, null);
labelRef.put(i, ctx.l.getText());
}
i = i+1 ;
break;
case SVMLexer.PUSHR:
code[i] = new AssemblyClass(SVMParser.PUSHR, ctx.REG(0).toString(), null, null);
i = i+1 ;
break;
case SVMLexer.POP:
code[i] = new AssemblyClass(SVMParser.POP, null, null, null);
i = i+1 ;
break;
case SVMLexer.POPR:
code[i] = new AssemblyClass(SVMParser.POPR, ctx.REG(0).toString(), null, null);
i = i+1 ;
break;
case SVMLexer.LABEL:
labelAdd.put(ctx.l.getText(),i);
break;
case SVMLexer.BRANCH:
code[i] = new AssemblyClass(SVMParser.BRANCH, ctx.LABEL().toString(), null, null);
i = i+1 ;
labelRef.put(i, (ctx.LABEL() != null ? ctx.LABEL().toString() : null));
i = i+1 ;
break;
case SVMLexer.BRANCHEQ:
code[i] = new AssemblyClass(SVMParser.BRANCHEQ, ctx.REG(0).toString(), ctx.REG(1).toString(), ctx.LABEL().toString());
i = i+1 ;
labelRef.put(i, (ctx.LABEL() != null ? ctx.LABEL().toString() : null));
i = i+1 ;
break;
case SVMLexer.BRANCHLESSEQ:
code[i] = new AssemblyClass(SVMParser.BRANCHLESSEQ, ctx.REG(0).toString(), ctx.REG(1).toString(), ctx.LABEL().toString());
i = i+1 ;
labelRef.put(i, (ctx.LABEL() != null ? ctx.LABEL().toString() : null));
i = i+1 ;
break;
case SVMLexer.JUMPSUB:
code[i] = new AssemblyClass(SVMParser.JUMPSUB, ctx.LABEL().toString(), null, null);
labelRef.put(i, ctx.LABEL().toString() );
i = i+1 ;
break;
case SVMLexer.RETURNSUB:
code[i] = new AssemblyClass(SVMParser.RETURNSUB, ctx.REG(0).toString(), null, null);
i = i+1 ;
break;
case SVMLexer.HALT:
code[i] = new AssemblyClass(SVMParser.HALT, null, null, null);
i = i+1 ;
break;
default:
break; // Invalid instruction
}
return null;
}
}
|