This repository has been archived by the owner on Oct 10, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoPassLinker.java
398 lines (341 loc) · 11.7 KB
/
TwoPassLinker.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
import java.util.Scanner;
import java.io.*;
import java.util.*;
/*
* An implementation of Operating Systems Lab 1.
* Two Pass Linker
* @Author: Eric Fang ef1467
*/
public class TwoPassLinker {
static ArrayList tokens = new ArrayList<String>();
static ArrayList<String> totalUses = new ArrayList<String>();
static int modNum;
static int useCount;
static LinkedHashMap<String, ArrayList<Integer>> table = new LinkedHashMap<String, ArrayList<Integer>>(); // Hash Map for symbol table
static ArrayList<ArrayList<String>> lines = new ArrayList<ArrayList<String>>(); // Lines of lines
static ArrayList<Integer> baseAddress = new ArrayList<Integer>(); // Each module's base address
static ArrayList<ArrayList<Integer>> modAddress = new ArrayList<ArrayList<Integer>>(); // ArrayList of ArrayList in each module
static ArrayList<Integer> countNT = new ArrayList<Integer>(); // ArrayList for number of NT uses
// static HashMap<Integer, String> uses = new HashMap<Integer, String>(); // Hashmap for external symbol uses
static ArrayList<String[]> modUses = new ArrayList<String[]>(); // list of hashmaps
public static void main(String args[]) {
System.out.println("Please state the type of input:"); // First determine what type of input
System.out.println("1. File");
System.out.println("2. Typed");
Scanner kbScanner = new Scanner(System.in);
int inputType = kbScanner.nextInt(); // Get input type
kbScanner.nextLine();
if (inputType == 1) { // If file, then ask for filename and proceed to run linker
System.out.println("What is the file name?");
String inputName = kbScanner.nextLine();
parseScanner(newScanner(inputName));
firstPass();
secondPass();
kbScanner.close();
}
else if (inputType == 2) {
keyboardScanner();
firstPass();
secondPass();
System.exit(0);
}
else {
System.out.println("Error: Not a valid input!");
System.exit(0);
}
}
public static Scanner newScanner(String inputName) {
try {
Scanner inputScanner = new Scanner(new FileInputStream((inputName)));
return inputScanner;
}
catch(Exception ex) {
System.out.println("Error reading " + inputName);
System.exit(0);
}
return null;
}
public static void keyboardScanner() {
System.out.println("Please type the input. Type exit and enter twice to finish reading.");
Scanner inputScanner = new Scanner(System.in);
String[] readIn;
String current = "";
boolean on = true;
while (inputScanner.hasNext() && on) { // Read through input and split into token array
current = inputScanner.nextLine();
if(current.equals("exit")) {
on = false;
}
else {
readIn = current.split("\\s+");
for(int j = 0; j < readIn.length; j++) {
if(!(readIn[j]).equals("")) {
tokens.add(readIn[j]);
}
}
}
}
inputScanner.close();
}
public static void firstPass() { // Pass one determines the base address for each module and the absolute address for each external symbol, storing the latter in the symbol table it produces
modNum = Integer.parseInt((String)tokens.get(0)); // Number of modules
int curMod = 0;
int lineCount = 0;
int lineType = 1; // 0, 1, 2, or 3
for (int j = 1; j < tokens.size();) { // Start from second token
String temp = (String)tokens.get(j);
int first = Integer.parseInt(temp);
lines.add(new ArrayList<String>());
int lineSize = 0;
if ((lineCount % 3) == 0) { // if definition list
lineSize = 2 * first + 1;
if (lineSize == 0) {
lines.get(lineCount).add("0");
j++;
}
else {
while (lineSize > 0) { // add tokens to first arraylist in line arraylist
temp = (String)tokens.get(j);
lines.get(lineCount).add(temp);
lineSize--;
j++;
}
}
}
else if (((lineCount-1) % 3) == 0) { // if use list
lineSize = first;
if (lineSize == 0) {
lines.get(lineCount).add("0");
j++;
}
else {
while (lineSize > 0) { // line size is determined by first, and each unit is determined by when -1
temp = (String)tokens.get(j); // lineSize ≠ # of tokens
lines.get(lineCount).add(temp);
if (tokens.get(j).equals("-1")) {
lineSize--;
}
j++;
}
}
}
else if (((lineCount-2) % 3) == 0) { // if program text
lineSize = first + 1; // Total # of tokens in line
if (lineSize == 0) { // if 0, add 0 to the lines stack
lines.get(lineCount).add("0");
j++;
}
else {
while (lineSize > 0) { //add all tokens to arraylist
temp = (String)tokens.get(j);
lines.get(lineCount).add(temp);
lineSize--;
j++;
}
}
}
lineCount++;
}
int increment = 0;
for (int i = 0; i < lineCount; i++) { // Find base addresses
if (i == 0) {
baseAddress.add(increment);
}
else if ((i % 3) == 0) { // When reaching new module
increment += Integer.parseInt(lines.get(i-1).get(0));
baseAddress.add(increment);
}
}
for (int k = 0; k < lines.size(); k+=3) { // Find the absolute address for external symbol
int numOfSymbols = lines.get(k).size();
for (int z = 1; z < numOfSymbols; z+=2) {
ArrayList currentList = lines.get(k);
if (table.containsKey((String)currentList.get(z))) {
table.get((String)currentList.get(z)).add((baseAddress.get(curMod)) + (Integer.parseInt((String)(currentList.get(z+1)))));
}
// if ((baseAddress.get(curMod) + Integer.parseInt((String)(currentList.get(z+1)))) >= useCount) {
// ArrayList temp = new ArrayList<Integer>();
// temp.add(useCount-1);
// table.put((String)currentList.get(z), temp);
// }
else {
ArrayList temp = new ArrayList<Integer>();
temp.add((baseAddress.get(curMod)) + (Integer.parseInt((String)(currentList.get(z+1)))));
table.put((String)currentList.get(z), temp); //hash map adding
}
}
curMod++;
}
}
/* Pass two uses the base addresses and the symbol table computed in pass one to generate the actual output
by relocating relative addresses and resolving external references.*/
public static void secondPass () {
// ArrayList<Integer> tempAddress = new ArrayList<Integer>();
int curMod = 0;
for (int i = 2; i < lines.size(); i+=3) { // Add instructions to an arraylist with corresponding module numbers as indices
modAddress.add(new ArrayList<Integer>());
for (int j = 0; j < lines.get(i).size(); j++) {
if (j == 0) {
if (curMod == 0) {
countNT.add(0);
}
else {
countNT.add(Integer.parseInt(lines.get(i-3).get(j)) + countNT.get(curMod-1));
}
}
else {
modAddress.get(curMod).add(Integer.parseInt(lines.get(i).get(j)));
useCount++;
}
}
curMod++;
}
curMod = 0;
String s = "";
boolean[] duplicate = new boolean[useCount];
for (int i = 1; i < lines.size(); i+=3) { // Add uses of external symbols to a list with indices corresponding to module number
String[] uses = new String[modAddress.get(i/3).size()];
int index = 0;
for (int j = 1; j < lines.get(i).size(); j++) {
if (j == 1) {
s = lines.get(i).get(j);
}
else if (isInteger((String)(lines.get(i).get(j-1))) && (Integer.parseInt(lines.get(i).get(j-1)) == -1)) {
s = lines.get(i).get(j);
}
else if (isInteger(lines.get(i).get(j)) && Integer.parseInt(lines.get(i).get(j)) == -1) {
s = "";
}
else {
index = Integer.parseInt(lines.get(i).get(j));
if (uses[index] != null) {
duplicate[index] = true;
}
else {
duplicate[index] = false;
}
uses[index] = s;
}
}
modUses.add(uses);
curMod++;
}
for (int i = 0; i < modUses.size(); i++) {
for (int j = 0; j < modUses.get(i).length; j++) {
totalUses.add(modUses.get(i)[j]);
}
}
System.out.println("Symbol Table");
ArrayList<String> notUsed = new ArrayList<String>();
if (!table.isEmpty()) {
for (Map.Entry<String, ArrayList<Integer>> entry : table.entrySet()) {
if (entry.getValue().get(0) >= useCount) {
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(useCount-1);
table.put(entry.getKey(), temp);
System.out.println(entry.getKey()+"="+(useCount-1)+ " Error: Definition exceeds module size; last word in module used.");
}
else if (entry.getValue().size()>1) {
System.out.println(entry.getKey()+"="+entry.getValue().get(entry.getValue().size()-1) + " Error: This variable is multiply defined; last value used.");
}
else System.out.println(entry.getKey()+"="+entry.getValue().get(0));
if (!totalUses.contains(entry.getKey())) {
notUsed.add(entry.getKey());
}
}
}
ArrayList<Integer> resolvedAddress = new ArrayList<Integer>();
for (int i = 0; i < modAddress.size(); i++) {
int counter = 0;
for (int j = 0; j < modAddress.get(i).size(); j++) {
int code = modAddress.get(i).get(j) % 10;
int temp = 0;
switch (code) { // Assuming all instructions have either 1, 2, 3, 4 as rightmost digit
case 1:
temp = modAddress.get(i).get(j)/10; //unchanged
break;
case 2:
temp = modAddress.get(i).get(j)/10; //unchanged
break;
case 3:
curMod = modAddress.indexOf(modAddress.get(i));
int offset = countNT.get(curMod);
temp = modAddress.get(i).get(j)/10 + offset;
break;
default:
//TODO resolve for 4 temp = modAddress.get(i).get(j)
// int index = modUses.get(i).get(counter);
if (table.get(modUses.get(i)[j]) == null) {
temp = resolveAddress(modAddress.get(i).get(j)/10, 111);
}
else {
temp = resolveAddress(modAddress.get(i).get(j)/10, table.get(modUses.get(i)[j]).get(table.get(modUses.get(i)[j]).size()-1));
}
counter++;
// System.out.println("moduses" + modUses.get(0)[1]);
// for (int k = 0; i < modUses.size(); i++) {
// for (int l = 0; j < modUses.get(k).length; j++) {
// System.out.println(modUses.get(k)[l]);
// }
// }
}
resolvedAddress.add(temp);
}
}
System.out.println("\n" + "Memory Map");
for (int i = 0; i < resolvedAddress.size(); i++) {
if (!table.containsKey(totalUses.get(i)) && totalUses.get(i)!= null) {
System.out.println(i+": " + resolvedAddress.get(i) + " Error: "+ totalUses.get(i) + " is not defined; 111 used.");
}
else if (duplicate[i]) {
System.out.println(i+": " + resolvedAddress.get(i) + " Error: Multiple variables used in instruction; all but last ignored.");
}
else if (resolvedAddress.get(i)/1000 == 2 && resolvedAddress.get(i)%1000 > 300) {
System.out.println(i+": " + (resolvedAddress.get(i)/1000*1000+299) + " Error: Absolute address exceeds machine size; largest legal value used.");
}
else {
System.out.println(i+": " + resolvedAddress.get(i));
}
}
for (int i = 0; i < notUsed.size(); i++) {
System.out.println("Warning: " + notUsed.get(i) + " was defined but never used.");
}
}
public static boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
public static Integer resolveAddress(Integer Target, Integer Dummy) { // for resolving
int dummy = Dummy;
int temp = Dummy;
int numOfDigits = 0;
int target = Target;
target /= 1000;
target *= 1000;
target += temp;
return target;
}
public static void parseScanner(Scanner inputScanner) {
String[] readIn;
String current = "";
while (inputScanner.hasNext()) { // Read through input and split into token array
current = inputScanner.nextLine();
if(current.equals("exit")) {
inputScanner.close();
System.exit(0);
}
else {
readIn = current.split("\\s+");
for(int j = 0; j < readIn.length; j++) {
if(!(readIn[j]).equals("")) {
tokens.add(readIn[j]);
}
}
}
}
}
}