summaryrefslogtreecommitdiffhomepage
path: root/main.c
diff options
context:
space:
mode:
authorJeff Heiges <jeff.heiges@colorado.edu>2025-03-05 18:33:53 -0700
committerJeff Heiges <jeff.heiges@colorado.edu>2025-03-05 18:33:53 -0700
commitc43cb5791de75b64cf1766ff6e9ca6f8aab90d49 (patch)
tree03cd8ec1ac04d4756d86e723358777c733567725 /main.c
RV32I Assembler
Diffstat (limited to 'main.c')
-rw-r--r--main.c391
1 files changed, 391 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..5460e27
--- /dev/null
+++ b/main.c
@@ -0,0 +1,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;
+}