summary refs log tree commit diff
path: root/compiler.c
blob: 18983be2fa515730af38a45b9f594834d7c298f3 (plain)
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
/* Assembler for LC */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXLINELENGTH 1000
#define MAXNUMLABELS 65536
#define MAXLABELLENGTH 7 /* includes the null character termination */

#define ADD 0
#define NAND 1
#define LW 2
#define SW 3
#define BEQ 4
#define JALR 5
#define HALT 6
#define NOOP 7

/*
 * Read and parse a line of the assembly-language file.  Fields are returned
 * in label, opcode, arg0, arg1, arg2 (these strings must have memory already
 * allocated to them).
 *
 * Return values:
 *     0 if reached end of file
 *     1 if all went well
 *
 * exit(1) if line is too long.
 */
int
readAndParse(FILE *inFilePtr, char *label, char *opcode, char *arg0,
    char *arg1, char *arg2)
{
    char line[MAXLINELENGTH];
    char *ptr = line;

    /* delete prior values */
    label[0] = opcode[0] = arg0[0] = arg1[0] = arg2[0] = '\0';

    /* read the line from the assembly-language file */
    if (fgets(line, MAXLINELENGTH, inFilePtr) == NULL) {
	/* reached end of file */
        return(0);
    }

    /* check for line too long */
    if (strlen(line) == MAXLINELENGTH-1) {
	printf("error: line too long\n");
	exit(1);
    }

    /* is there a label? */
    ptr = line;
    if (sscanf(ptr, "%[^\t\n ]", label)) {
	/* successfully read label; advance pointer over the label */
        ptr += strlen(label);
    }

    /*
     * Parse the rest of the line.  Would be nice to have real regular
     * expressions, but scanf will suffice.
     */
    sscanf(ptr, "%*[\t\n\r ]%[^\t\n\r ]%*[\t\n\r ]%[^\t\n\r ]%*[\t\n\r ]%[^\t\n\r ]%*[\t\n\r ]%[^\t\n\r ]",
        opcode, arg0, arg1, arg2);
    return(1);
}

int
translateSymbol(char labelArray[MAXNUMLABELS][MAXLABELLENGTH],
    int labelAddress[MAXNUMLABELS], int numLabels, char *symbol)
{
    int i;

    /* search through address label table */
    for (i=0; i<numLabels && strcmp(symbol, labelArray[i]); i++) {
    }

    if (i>=numLabels) {
	printf("error: missing label %s\n", symbol);
	exit(1);
    }

    return(labelAddress[i]);
}

int
isNumber(char *string)
{
    /* return 1 if string is a number */
    int i;
    return( (sscanf(string, "%d", &i)) == 1);
}

/*
 * Test register argument; make sure it's in range and has no bad characters.
 */
void
testRegArg(char *arg)
{
    int num;
    char c;

    if (atoi(arg) < 0 || atoi(arg) > 7) {
	printf("error: register out of range\n");
	exit(2);
    }
    if (sscanf(arg, "%d%c", &num, &c) != 1) {
	printf("bad character in register argument\n");
	exit(2);
    }
}

/*
 * Test addressField argument.
 */
void
testAddrArg(char *arg)
{
    int num;
    char c;

    /* test numeric addressField */
    if (isNumber(arg)) {
	if (sscanf(arg, "%d%c", &num, &c) != 1) {
	    printf("bad character in addressField\n");
	    exit(2);
	}
    }
}

/*
 * main function
 */
int
main(int argc, char *argv[])
{
    char *inFileString, *outFileString;
    FILE *inFilePtr, *outFilePtr;
    int address;
    char label[MAXLINELENGTH], opcode[MAXLINELENGTH], arg0[MAXLINELENGTH],
	arg1[MAXLINELENGTH], arg2[MAXLINELENGTH], argTmp[MAXLINELENGTH];
    int i;
    int numLabels=0;
    int num;
    int addressField;

    char labelArray[MAXNUMLABELS][MAXLABELLENGTH];
    int labelAddress[MAXNUMLABELS];

    if (argc != 3) {
	printf("error: usage: %s <assembly-code-file> <machine-code-file>\n",
	    argv[0]);
	exit(1);
    }

    inFileString = argv[1];
    outFileString = argv[2];

    inFilePtr = fopen(inFileString, "r");
    if (inFilePtr == NULL) {
	printf("error in opening %s\n", inFileString);
	exit(1);
    }
    outFilePtr = fopen(outFileString, "w");
    if (outFilePtr == NULL) {
	printf("error in opening %s\n", outFileString);
	exit(1);
    }

    /* map symbols to addresses */

    /* assume address start at 0 */
    for (address=0; readAndParse(inFilePtr, label, opcode, arg0, arg1, arg2);
	    address++) {
	/*
	printf("%d: label=%s, opcode=%s, arg0=%s, arg1=%s, arg2=%s\n",
	    address, label, opcode, arg0, arg1, arg2);
	*/

	/* check for illegal opcode */
	if (strcmp(opcode, "add") && strcmp(opcode, "nand") &&
	        strcmp(opcode, "lw") && strcmp(opcode, "sw") &&
		strcmp(opcode, "beq") && strcmp(opcode, "jalr") &&
		strcmp(opcode, "halt") && strcmp(opcode, "noop") &&
		strcmp(opcode, ".fill") ) {
	    printf("error: unrecognized opcode %s at address %d\n", opcode,
		    address);
	    exit(1);
	}

	/* check register fields */
	if (!strcmp(opcode, "add") || !strcmp(opcode, "nand") ||
		!strcmp(opcode, "lw") || !strcmp(opcode, "sw") ||
		!strcmp(opcode, "beq") || !strcmp(opcode, "jalr")) {
	    testRegArg(arg0);
	    testRegArg(arg1);
	}
	if (!strcmp(opcode, "add") || !strcmp(opcode, "nand")) {
	    testRegArg(arg2);
	}

	/* check addressField */
	if (!strcmp(opcode, "lw") || !strcmp(opcode, "sw") ||
		!strcmp(opcode, "beq")) {
	    testAddrArg(arg2);
	}
	if (!strcmp(opcode, ".fill")) {
	    testAddrArg(arg0);
	}

	/* check for enough arguments */
	if ( (strcmp(opcode, "halt") && strcmp(opcode, "noop") &&
	      strcmp(opcode, ".fill")  && strcmp(opcode, "jalr")
	      && arg2[0]=='\0') ||
	     (!strcmp(opcode, "jalr") && arg1[0]=='\0') ||
	     (!strcmp(opcode, ".fill") && arg0[0]=='\0')) {
	    printf("error at address %d: not enough arguments\n", address);
	    exit(2);
	}

	if (label[0] != '\0') {
	    /* check for labels that are too long */
	    if (strlen(label) >= MAXLABELLENGTH) {
		printf("label too long\n");
		exit(2);
	    }

	    /* make sure label starts with letter */
	    if (! sscanf(label, "%[a-zA-Z]", argTmp) ) {
	        printf("label doesn't start with letter\n");
		exit(2);
	    }

	    /* make sure label consists of only letters and numbers */
	    sscanf(label, "%[a-zA-Z0-9]", argTmp);
	    if (strcmp(argTmp, label)) {
	        printf("label has character other than letters and numbers\n");
		exit(2);
	    }

	    /* look for duplicate label */
	    for (i=0; i<numLabels; i++) {
		if (!strcmp(label, labelArray[i])) {
		    printf("error: duplicate label %s at address %d\n",
			label, address);
		    exit(1);
		}
	    }
	    /* see if there are too many labels */
	    if (numLabels >= MAXNUMLABELS) {
		printf("error: too many labels (label=%s)\n", label);
		exit(2);
	    }

	    strcpy(labelArray[numLabels], label);
	    labelAddress[numLabels++] = address;
	}
    }

    for (i=0; i<numLabels; i++) {
	/* printf("%s = %d\n", labelArray[i], labelAddress[i]); */
    }

    /* now do second pass (print machine code, with symbols filled in as
	addresses) */
    rewind(inFilePtr);
    for (address=0; readAndParse(inFilePtr, label, opcode, arg0, arg1, arg2);
	    address++) {
	if (!strcmp(opcode, "add")) {
	    num = (ADD << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16)
		    | atoi(arg2);
	} else if (!strcmp(opcode, "nand")) {
	    num = (NAND << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16)
		    | atoi(arg2);
	} else if (!strcmp(opcode, "jalr")) {
	    num = (JALR << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16);
	} else if (!strcmp(opcode, "halt")) {
	    num = (HALT << 22);
	} else if (!strcmp(opcode, "noop")) {
	    num = (NOOP << 22);
	} else if (!strcmp(opcode, "lw") || !strcmp(opcode, "sw") ||
		   !strcmp(opcode, "beq")) {
	    /* if arg2 is symbolic, then translate into an address */
	    if (!isNumber(arg2)) {
		addressField = translateSymbol(labelArray, labelAddress,
					    numLabels, arg2);
		/*
		printf("%s being translated into %d\n", arg2, addressField);
		*/
		if (!strcmp(opcode, "beq")) {
		    addressField = addressField-address-1;
		}
	    } else {
		addressField = atoi(arg2);
	    }


	    if (addressField < -32768 || addressField > 32767) {
		printf("error: offset %d out of range\n", addressField);
		exit(1);
	    }

	    /* truncate the offset field, in case it's negative */
	    addressField = addressField & 0xFFFF;

	    if (!strcmp(opcode, "beq")) {
		num = (BEQ << 22) | (atoi(arg0) << 19) | (atoi(arg1) << 16)
		    | addressField;
	    } else {
		/* lw or sw */
		if (!strcmp(opcode, "lw")) {
		    num = (LW << 22) | (atoi(arg0) << 19) |
			    (atoi(arg1) << 16) | addressField;
		} else {
		    num = (SW << 22) | (atoi(arg0) << 19) |
			    (atoi(arg1) << 16) | addressField;
		}
	    }
	} else if (!strcmp(opcode, ".fill")) {
	    if (!isNumber(arg0)) {
		num = translateSymbol(labelArray, labelAddress, numLabels,
					arg0);
	    } else {
		num = atoi(arg0);
	    }
	}
	/* printf("(address %d): %d (hex 0x%x)\n", address, num, num); */
	fprintf(outFilePtr, "%d\n", num);
    }

    exit(0);
}