-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPSSymbol.java
More file actions
175 lines (153 loc) · 5.5 KB
/
SPSSymbol.java
File metadata and controls
175 lines (153 loc) · 5.5 KB
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
package SPS1620;
/*
* SPSSymbolTable.java - symbol table support routines
*
* IBM 1620 Jr Project, Computer History Museum, 2017-2024
*
* To recreate the experience (visual, auditory, tactile, visceral) of running historic software on a 1960s-era computer.
*
* Dave Babcock - project lead, software, library
* John M. Bohn Jr. - software
* David Brock - CHM sponsor
* Steve Casner - hardware, software
* Joe Fredrick - hardware, firmware
* Len Shustek - CHM advisor
* Dag Spicer - CHM advisor
* David Wise - IBM 1620 expert
*/
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;
public class SPSSymbol {
// Data
public int Address;
public SPSStatement.StatementType Type;
public int Defined;
public LinkedList<Integer> References;
private static Map<String, SPSSymbol> symbolTable = new TreeMap<String, SPSSymbol>();
// Constructor
public SPSSymbol(int address, SPSStatement.StatementType type, int defined) {
this.Address = address;
this.Type = type;
this.Defined = defined;
this.References = null;
}
// Methods
public static void Add(String symbol, int address, int line) {
if (SPSUtility.IsEmpty(symbol)) return;
if (!SPSUtility.IsValidLabel(symbol)) {
SPSOutput.ReportError("invalid label (" + symbol + ")");
return;
}
String symbol2;
if ((SPSData.symbolHead != ' ') && (symbol.length() < 6)) {
symbol2 = SPSData.symbolHead + "$" + symbol;
} else {
symbol2 = symbol;
}
if (!symbolTable.containsKey(symbol2)) {
symbolTable.put(symbol2, new SPSSymbol(address, SPSData.inputStatement.Type, line));
} else {
SPSOutput.ReportError("duplicate label (" + symbol2 + ")");
}
}
public static void Verify(String symbol, int line) {
if (SPSUtility.IsEmpty(symbol)) return;
if (!SPSUtility.IsValidSymbol(symbol)) {
SPSOutput.ReportError("invalid label (" + symbol + ")");
return;
}
String symbol2;
if ((SPSData.symbolHead != ' ') && (symbol.length() < 6)) {
symbol2 = SPSData.symbolHead + "$" + symbol;
} else {
symbol2 = symbol;
}
if (symbolTable.containsKey(symbol2) && (symbolTable.get(symbol2).Defined != line)) {
SPSOutput.ReportError("duplicate label (" + symbol2 + ")");
}
}
public static int Lookup(String symbol, int line, boolean check) {
if (SPSUtility.IsEmpty(symbol)) return 0;
String symbol2;
int pos = symbol.indexOf('$');
if (pos == -1) {
if (!SPSUtility.IsValidSymbol(symbol)) {
SPSOutput.ReportError("invalid symbol (" + symbol + ")");
return 0;
}
if ((SPSData.symbolHead != ' ') && (symbol.length() < 6)) {
symbol2 = SPSData.symbolHead + "$" + symbol;
} else {
symbol2 = symbol;
}
} else if (pos == 0) {
symbol2 = symbol.substring(1);
} else if (pos == 1) {
if (!SPSUtility.IsValidHead(symbol.charAt(0))) {
SPSOutput.ReportError("invalid symbol head (" + symbol.charAt(0) + ")");
return 0;
}
String temp = symbol.substring(2);
if (!SPSUtility.IsValidSymbol(temp)) {
SPSOutput.ReportError("invalid symbol (" + temp + ")");
return 0;
}
symbol2 = symbol;
} else {
SPSOutput.ReportError("invalid symbol head (" + symbol.substring(0, pos) + ")");
return 0;
}
if (symbolTable.containsKey(symbol2)) {
SPSSymbol sym = symbolTable.get(symbol2);
if (check && (sym.Defined > line)) {
SPSOutput.ReportError("undefined [forward reference] symbol (" + symbol2 + ")");
return 0;
}
if (sym.References == null) sym.References = new LinkedList<Integer>();
if (!sym.References.contains(line)) {
sym.References.add(line);
}
return sym.Address;
} else {
SPSOutput.ReportError("undefined symbol (" + symbol2 + ")");
return 0;
}
}
public static void Print() {
if (SPSData.lstFile == null) return;
SPSData.lstWriter.write("\f\n Symbol Cross-Reference Table\n");
SPSData.lstWriter.write(" ============================\n");
if (SPSData.sourceMultipleFiles) {
SPSData.lstWriter.write("\n\n Id Source File\n");
SPSData.lstWriter.write(" -- --------------------------------------------------\n");
for (int i = 0; i < SPSData.sourceFileCount; ++i) {
SPSData.lstWriter.format(" %2d %s\n", (i + 1), SPSData.sourceFilenames[i]);
}
}
SPSData.lstWriter.write("\n\n Symbol Addr. Type Defined References\n");
SPSData.lstWriter.write(" ------- ----- ------ ------- -------------------------------------------------------------\n");
Object[] keys = symbolTable.keySet().toArray();
for (int i = 0; i < keys.length; ++i) {
SPSSymbol sym = symbolTable.get(keys[i]);
if (sym.Type == SPSStatement.StatementType.INSTRUCTION) {
SPSData.lstWriter.format(" %-7s %05d <inst> %7s", keys[i], sym.Address, SPSOutput.LineNumber(sym.Defined));
} else {
SPSData.lstWriter.format(" %-7s %05d %-6s %7s", keys[i], sym.Address, sym.Type, SPSOutput.LineNumber(sym.Defined));
}
if (sym.References != null) {
Collections.sort(sym.References);
int cnt = 0;
for (int line: sym.References) {
if (++cnt == SPSData.SIZE_XREF_LINE) {
SPSData.lstWriter.write("\n ");
cnt = 1;
}
SPSData.lstWriter.format(" %7s", SPSOutput.LineNumber(line));
}
}
SPSData.lstWriter.write("\n");
}
}
}