tisc

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

tisc.c (4395B)


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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SYMBOLS    1000
#define MAX_SYMBOL_LEN 100
#define MAX_LINE_LEN   100
#define MAX_INSTYPES   4

#define ADD  0
#define NAND 1
#define OR   2
#define CIN  2
#define LLI  3
#define LUI  7
#define EXT  11
#define CMP  15

int  validSymbols = 0;
int  addresses[MAX_SYMBOLS];
char symbols[MAX_SYMBOLS][MAX_SYMBOL_LEN];
char *formats[MAX_INSTYPES][8] =
{
	// 0 arg instructions
	{
		"push",
		"pop",
		"pcr",
		NULL
	},
	// 1 arg instructions
	{
		"lli",
		"lui",
		"jmp",
		"lb",
		"sb",
		"sp",
		".fill",
		NULL
	},
	// 2 arg instructions
	{
		"cmp",
		NULL
	},
	// 3 arg instructions
	{
		"add",
		"nand",
		"or",
		"cin",
		NULL
	}
};

/* char *parse -> parses file and sets label, opcode, and args
   accordingly */
char *parse(FILE *file, char *line, char **label, char **opcode,
            char **arg0, char **arg1, char **arg2)
{
	char *str, *first;
	str = fgets(line, MAX_LINE_LEN, file);
	if (str != NULL)
	{
		first = strtok(line, " \t\n");
		if (first == NULL || first[0] == '#')
		{
			return parse(file, line, label, opcode, arg0, arg1, arg2);
		}
		else if (first[strlen(first) - 1] == ':')
		{
			*label = first;
			*opcode = strtok(NULL, " \t\n");
			first[strlen(first) - 1] = '\0';
		}
		else
		{
			*label = NULL;
			*opcode = first;
		}
		*arg0 = strtok(NULL, " \t\n");
		*arg1 = strtok(NULL, " \t\n");
		*arg2 = strtok(NULL, " \t\n");
	}
	
	return(str);
}

int labelexists(const char *label)
{
	int i, status = 0;

	for (i = 0; i < validSymbols; i++)
		if (strcmp(symbols[i], label) == 0) status = 1;

	return status;
}

int validins(int op, int format, const char *opcode, char *arg[])
{
	int status = 0;
	if (strcmp(formats[format][op], opcode) == 0)
	{
		status = 1;
		if (!(format == 0 && arg[0] == NULL && arg[1] == NULL && arg[2] == NULL ||
		      format == 1 && arg[0] != NULL && arg[1] == NULL && arg[2] == NULL ||
		      format == 2 && arg[0] != NULL && arg[1] != NULL && arg[2] == NULL ||
		      format == 3 && arg[0] != NULL && arg[1] != NULL && arg[2] != NULL ))
		{
			status = 2;
		}
	}

	return status;
}

/* int preprocess -> 1st pass over file, links symbols to address
   and reports syntax errors, fails if returns -1 */
int preprocess(int address, char *label, char *opcode,
               char *arg0, char *arg1, char *arg2)
{
	int i,op;
	int status = 0;

	// arguement check
	for (i = 0; i < MAX_INSTYPES; i++)
	{
		for (op = 0; formats[i][op] != NULL; op++)
		{
			char *args[3] = {arg0, arg2, arg2};
			if (status == 0) status = validins(op, i, opcode, args);
		}
	}

	switch (status)
	{
		case 0:
			printf("Error:%i: instruction '%s' does not exist\n",address, opcode);
			status = 1;
		case 2:
			printf("Error:%i: not enough arguements for '%s'\n",address, opcode);
			status = 1;
	}

	//duplicate label check
	if (label != NULL)
	{
		if (labelexists(label) == 1)
		{
			printf("Error:%i: label exists '%s'\n",address, label);
		}
		else
		{
			strcpy(symbols[validSymbols], label);
			addresses[validSymbols] = address;
			validSymbols++;
		}
	}

	return status;
}
/* int process -> 2nd pass over file, instrutions are turned into
   machine code with symbols filled in as adresses, 
   fails if returns -1 */
int process(int address, FILE *outfile, char *label, char *opcode,
            char *arg0, char *arg1, char *arg2)
{
	return 0;
}

int main(int argc, char *argv[])
{
	if (argc != 3)
	{
		fprintf(stderr,
		"error useage: %s <assembly-code-file> <machine-code-file> \n", argv[0]);
		exit(1);
	}

	char *input,  *output;
	FILE *inputf, *outputf;
	char *labels, *opcodes, *args0, *args1, *args2;
	char lines[MAX_LINE_LEN + 1];
	int address = 0;

	input = argv[1];
	output = argv[2];
	inputf = fopen(input, "r");
	outputf = fopen(output, "w");

	if (inputf == NULL)
		fprintf(stderr, "Error opening file '%s'", input); exit(1);

	if (outputf == NULL)
		fprintf(stderr, "Error opening file '%s'", output); exit(1);

	while (parse(inputf, lines, &labels, &opcodes, &args0, &args1, &args2) != NULL)
	{
		if (preprocess(address, labels, opcodes, args0, args1, args2) == 0)
			exit(1);
		address++;
	}

	rewind(inputf);
	address = 0;

	while (parse(inputf, lines, &labels, &opcodes, &args0, &args1, &args2) != NULL)
	{
		if (process(address, outputf, labels, opcodes, args0, args1, args2) == 0)
			exit(1);
		address++;
	}

	fclose(inputf);
	fclose(outputf);

	return 1;
}