summaryrefslogtreecommitdiffhomepage
path: root/main.c
blob: 5460e2706d991bceaba01d7c95ef58fab0e9bf4e (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
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdint.h>
#include<assert.h>
//#define TEST
char *word(char *read, char low, char high, size_t *offset, size_t *lnum, char *contents, size_t size) {
	char *ofstart = read;
	while(isspace(*read) || *read == ',') {
		if(*(read)=='\n') {
			lnum += 1;
		}
		if(*read=='#') {
			while(*read != '\n') {
				read += 1;
			}
			lnum += 1;
		}
		read += 1;
	}
	char *start = read;
	while(*read >= low && *read <= high) {
		read += 1;
	}

	char *inst = malloc(read - start + 3);
	memcpy(inst+1, start, read - start);
	*inst=' ';
	*(inst+(read-start)+1)=' ';
	*(inst+(read-start)+2) = '\0';
	for(size_t i = 0; i < read-start; i++) {
		*(inst + i) = tolower(*(inst + i));
	}
	*offset = read-ofstart;
	return inst;
}

uint32_t reg(char *read, size_t *offset, size_t *lnum) {
	char *string = word(read, '0', 'z', offset, lnum);
	char *registers = " zero ra sp gp tp t0 t1 t2 s0 fp s1 a0 a1 a2 a3 a4 a5 a6 a7 s2 s3 s4 s5 s6 s7 s8 s9 s10 s11 t3 t4 t5 t6 ";
	char *loc;
	assert((loc = strstr(registers, string)) != NULL);
	free(string);
	switch(loc - registers) {
		case 0: return 0;
		case 5: return 1;
		case 8: return 2;
		case 11: return 3;
		case 14: return 4;
		case 17: return 5;
		case 20: return 6;
		case 23: return 7;
		case 26: return 8;
		case 29: return 8;
		case 32: return 9;
		case 35: return 10;
		case 38: return 11;
		case 41: return 12;
		case 44: return 13;
		case 47: return 14;
		case 50: return 15;
		case 53: return 16;
		case 56: return 17;
		case 59: return 18;
		case 62: return 19;
		case 65: return 20;
		case 68: return 21;
		case 71: return 22;
		case 74: return 23;
		case 77: return 24;
		case 80: return 25;
		case 83: return 26;
		case 87: return 27;
		case 91: return 28;
		case 94: return 29;
		case 97: return 30;
		case 100: return 31;
		default: return -1;
	}
	return -1;
}

uint32_t rfunct3(size_t offset) {
	if(offset < 8) { return 0;	}
	else if(offset < 12) { return 1; }
	else if(offset < 16) { return 2; }
	else if(offset < 21) { return 3; }
	else if(offset < 25) { return 4; }
	else if(offset < 33) {return 5; }
	else if(offset < 36) {return 6;}
	return 7;
}
uint32_t ifunct3(size_t offset) {
	if(offset==40) {return 0;}
	if(offset==45) {return 2;}
	if(offset==50) {return 3;}
	if(offset==56) {return 4;}
	if(offset==61) {return 6;}
	if(offset==65) {return 7;}
	exit(1);
}
uint32_t lfunct3(size_t offset) {
	if(offset==95) {return 0;}
	if(offset==98) {return 1;}
	if(offset==101){return 2;}
	if(offset==104){return 4;}
	if(offset==108){return 5;}
	exit(1);
}
uint32_t sfunct3(size_t offset) {
	if(offset==112){return 0;}
	if(offset==115){return 1;}
	if(offset==118){return 2;}
	exit(1);
}
uint32_t bfunct3(size_t offset) {
	if(offset==121){return 0;}
	if(offset==125){return 1;}
	if(offset==129){return 4;}
	if(offset==133){return 5;}
	if(offset==137){return 6;}
	if(offset==142){return 7;}
	exit(1);
}
uint32_t efunct3(size_t offset) {
	if(offset==147){return 0;}
	if(offset==153){return 1;}
	exit(1);
}
long int lsearch(size_t *haystack, size_t size, size_t needle) {
	for(long int i=0; i<size; i++) {
		if(*(haystack+i)==needle) {
			return i;
		}
	}
	return -1;
}

uint32_t points(char *read, size_t *wordsize, dict *points, size_t *lnum) {
	char *string = word(read, '0', 'z', wordsize, lnum);
	assert(*(read+wordsize)==':');
	size_t loc;
	assert((loc = strstr(points->str, string)) != NULL);
	long int ret = lsearch(points->ints, points->ilen, loc);
	assert(ret != -1);
	return ret;
}
uint32_t immediate(char *read, size_t *wordsize, dict *points, size_t *lnum) {
	char *string = word(read, '-', '9', wordsize, lnum);
	if(strlen(string) == 0) {
		free(string);
		return points(read, wordsize, points, lnum);
	}
	char *a = malloc(strlen(string)-2);
	memcpy(a, string+1, strlen(string)-2);
	*(a+strlen(string)-2)='\0';
	uint32_t ret = atoi(a);
	free(a);
	free(string);
	return ret;
}
typedef struct dict {
	char *str;
	size_t cap;
	size_t *ints;
	size_t ilen;
} dict;

void assembler(char *contents, size_t size) {
	char *instructions = " add sub sll slt sltu xor srl sra or and addi slti sltiu xori ori andi slli srli srai auipc lui lb lh lw lbu lhu sb sh sw beq bne blt bge bltu bgeu ecall ebreak jal jalr ";
	char *read = contents;
	size_t wordsize;
	size_t colons = 0, lines = 0;
	for (int i=0; read[i]; i++){
  	colons += (read[i] == ':');
		lines += (read[i] == '\n');
	}
	uint32_t insts[lines];
	dict *points = malloc(sizeof(dict) * colons);
	points->str = malloc(colons * 20);
	points->cap = colons * 20;
	points->ints = malloc(sizeof(size_t) * lines);
	points->ilen = lines;
	size_t lnum = 0;

	while(read < contents + size) {
		while(*read > 'z' || *read < 'A') {
			if(read==contents+size){
				free(contents);
				return;
			}
			if(*read == '#') {
				while(*read != '\n') {
					read += 1;
				}
				lnum += 1;
			}
			read += 1;
		}
		char *inst = word(read, '0', 'z', &wordsize);
		read += wordsize;

		char *loc;
		if((loc = strstr(instructions, inst)) != NULL) {
			size_t offset = loc - instructions;
			uint32_t machine_code;

			if(offset < 40) {
				// add sub sll sltu 
#if !defined(TEST)
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t funct3 = rfunct3(offset);
				uint32_t rs1 = reg(read, &wordsize);
				read += wordsize;
				uint32_t rs2 = reg(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += 12 << 2;
				machine_code += rd << 7;
				machine_code += funct3 << 12;
				machine_code += rs1 << 15;
				machine_code += rs2 << 20;
				
				if(offset == 4 || offset == 29) {
					machine_code += 1 << 30;
				}

				
				// R instruction
			}
			else if(offset < 85) {
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t funct3 = ifunct3(offset);
				uint32_t rs1 = reg(read, &wordsize);
				read += wordsize;
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += 12 << 2;
				machine_code += rd << 7;
				machine_code += funct3 << 12;
				machine_code += rs1 << 15;
				machine_code += (imm & 0xFFF) << 20;

				// I instruction
			}
			else if (offset < 95) {
				// U instruction
				// auipc, lui
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t funct3 = 5;
				if(offset == 85) {
					funct3 = 13;
				}
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;
				
				machine_code += 3;
				machine_code += funct3 << 2;
				machine_code += rd << 7;
				machine_code += imm & 0xFFFFF000;

			}
			else if (offset < 112) {
				// L instruction
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t funct3 = lfunct3(offset);
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;
				uint32_t rs1 = reg(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += rd << 7;
				machine_code += funct3 << 12;
				machine_code += rs1 << 15;
				machine_code += (imm & 0xFFF) << 20;

			}
			else if (offset < 121) {
				uint32_t funct3 = sfunct3(offset);
				uint32_t rs2 = reg(read, &wordsize);
				read += wordsize;
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;
				uint32_t imm1 = imm & 0x1F;
				uint32_t imm2 = (imm >> 5) & 0x7F;
				uint32_t rs1 = reg(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += 8 << 2;
				machine_code += imm1 << 7;
				machine_code += funct3 << 12;
				machine_code += rs1 << 15;
				machine_code += rs2 << 20;
				machine_code += imm2 << 25;

				// S instruction
			}
			else if (offset < 147) {
				uint32_t rs1 = reg(read, &offset);
				read += offset;
				uint32_t rs2 = reg(read, &offset);
				read += offset;
				uint32_t imm = immediate(read, &wordsize);
				read += offset;
				uint32_t imm1 = (imm & 0x1E) + ((imm >> 11) & 1);
				uint32_t imm2 = (imm & 0x7E0) + ((imm >> 12) &1);
				uint32_t funct3 = bfunct3(offset);

				machine_code += 3;
				machine_code += 24 << 2;
				machine_code += imm1 << 7;
				machine_code += funct3 << 12;
				machine_code += rs1 << 15;
				machine_code += rs2 << 20;
				machine_code += imm2 << 25;
				// B instruction
			}
			// fence
			else if (offset < 160){//?) {
				// EC instruction
				uint32_t funct3 = efunct3(offset);
				machine_code += 115;
				machine_code += funct3 << 20;
				
			}
			else if(offset < 164){
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += 27 << 2;
				machine_code += rd << 7;
				machine_code += ((imm >> 12) & 0xFF) << 12;
				machine_code += ((imm >> 11) & 0x1) << 20;
				machine_code += ((imm >> 1) & 0x3FF) << 21;
				machine_code += ((imm >> 20) & 0x1) << 31;
			}
			else {
				uint32_t rd = reg(read, &wordsize);
				read += wordsize;
				uint32_t rs1 = reg(read, &wordsize);
				read += wordsize;
				uint32_t imm = immediate(read, &wordsize);
				read += wordsize;

				machine_code += 3;
				machine_code += 25 << 2;
				machine_code += rd << 7;
				machine_code += rs1 << 15;
				machine_code += (imm & 0x7FF) << 20;
#endif
			}
			printf("%u\n", machine_code);
		}
		else {
			printf("%s\n", inst);
		}
		free(inst);
	}
	free(contents);
}

int main(int argc, char *argv[]) {
	if(argc < 2) {
		printf("Need file as input\n");
		return 0;
	}

	FILE *in;
	in = fopen(argv[1], "r");
	fseek(in, 0, SEEK_END);
	size_t size = ftell(in);
	fseek(in, 0, SEEK_SET);
	char *contents = malloc(size+1);
	fread(contents, 1, size, in);
	fclose(in);
	assembler(contents, size);
	return 0;
}