Fix bugs to make code compile. Code now compiles

This commit is contained in:
sBubshait 2024-06-11 23:19:04 +01:00
parent 31c1ae90f7
commit 999f36facd
2 changed files with 60 additions and 110 deletions

View File

@ -1,49 +0,0 @@
#include <stdio.h>
typedef struct st st;
typedef struct {
const void* key;
void* value;
node* prev;
node* next;
} node;
struct st {
node* head;
node* tail;
};
// add new node to the end
void st_add(st table, void* key, void* value) {
node n = {key, value, table.tail};
if (table.head == NULL) {
table.head = &n;
table.tail = &n;
}
else {
(*(table.tail)).next = &n;
table.tail = &n;
}
}
// returns the pointer to key of the specified node, or null, if it does not exist
void* st_search(st table, void* key) {
return nodeSearch(table.head, key);
}
void* nodeSearch(node* n, void* key) {
if (n != NULL) {
if ((*n).key == key) {
return (*n).value;
}
else {
return nodeSearch((*n).next, key);
}
}
else {
return NULL;
}
}

View File

@ -1,14 +1,15 @@
#include "global.h"
#include "a64instruction/a64instruction.h"
#include "symboltable.c"
//generates assembled code based on two pass assembly method
#include <stdlib.h>
#include <limits.h>
// Generates assembled code based on the two-pass assembly method
word assembleBranch(a64inst_instruction *instr) {
word binInstr = 0;
binInstr += (5^28); //101 start of branch instr
switch (instr->data.BranchData.BranchType)
{
binInstr += (5 << 28); // 101 start of branch instr
switch (instr->data.BranchData.BranchType) {
case a64inst_UNCONDITIONAL:
// 000101
// 25-0: sign extended simm26
@ -20,13 +21,13 @@ word assembleBranch(a64inst_instruction *instr){
// 000000
// 9-5: address from register
// 0000
binInstr += ((instr->data.BranchData.processOpData.registerData.src)^5);
binInstr += ((instr->data.BranchData.processOpData.registerData.src) << 5);
break;
case a64inst_CONDITIONAL:
// 01010100
// 25-5: sign extended offset
// 4-0: 0{condition}
binInstr += ((instr->data.BranchData.processOpData.conditionalData.offset)^5);
binInstr += ((instr->data.BranchData.processOpData.conditionalData.offset) << 5);
binInstr += instr->data.BranchData.processOpData.conditionalData.cond;
break;
default:
@ -39,39 +40,39 @@ st* firstPass(a64inst_instruction instrs[], int numInstrs){
// TODO:
// -iterate over instructions, adding to symbol table
// create symbol table and map labels to addresses/lines
struct st table;
st *table = (st*)malloc(sizeof(st));
for (int i = 0; i < numInstrs; i++) {
// discuss defining a LABEL type
if (instrs[i].type == a64inst_LABEL) {
st_add(table, &(instrs[i].data.LabelData.label), &i);
st_add(*table, &(instrs[i].data.LabelData.label), &i);
}
}
return &table;
return table;
}
word dpi(a64inst_instruction cI) {
word out = 0;
a64inst_DPImmediateData data = cI.data.DPImmediateData;
// sf
out += data.regType*(2^31);
out += data.processOp*(2^29);
out += 2^28;
out += data.regType * (1 << 31);
out += data.processOp * (1 << 29);
out += 1 << 28;
// if arithmetic
if (data.DPIOpType == a64inst_DPI_ARITHM) {
out += 2^24;
out += 1 << 24;
// shift
if (data.processOpData.arithmData.shiftImmediate) {
out += 2^22;
out += 1 << 22;
}
out += data.processOpData.arithmData.immediate*(2^10);
out += data.processOpData.arithmData.src*(2^5);
out += data.processOpData.arithmData.immediate * (1 << 10);
out += data.processOpData.arithmData.src * (1 << 5);
}
// if wide move
else {
out += 5*(2^23);
out += 5 * (1 << 23);
// hw
out += data.processOpData.wideMovData.shiftScalar*(2^21);
out += data.processOpData.wideMovData.immediate*(2^5);
out += data.processOpData.wideMovData.shiftScalar * (1 << 21);
out += data.processOpData.wideMovData.immediate * (1 << 5);
}
// destination register
out += data.dest;
@ -84,7 +85,7 @@ word dpr(a64inst_instruction cI) {
// sf
int sf = data.regType;
// bits 27-25
out += 5*(2^25);
out += 5 * (1 << 25);
int m = data.DPROpType;
int opc = 0;
int opr = 0;
@ -120,11 +121,11 @@ word dpr(a64inst_instruction cI) {
rm += data.src1;
rn += data.src2;
rd += data.dest;
out += sf*(2^31);
out += opc * (2^29);
out += m* (2^28);
out += opr * (2^21);
out += rm * (2^16);
out += sf * (1 << 31);
out += opc * (1 << 29);
out += m * (1 << 28);
out += opr * (1 << 21);
out += rm * (1 << 16);
out += operand * 1024;
out += rn * 32;
out += rd;
@ -136,10 +137,9 @@ word sts(a64inst_instruction cI) {
word out = 0;
a64inst_SingleDataTransferData data2 = data.processOpData.singleDataTransferData;
// this deals with every bit in the 31-23 range apart from sf and U
out += (512+128+64+32)*(2^23);
out += (512 + 128 + 64 + 32U) * (1 << 23);
int sf = data.regType;
int u = 0;
int l = data2.transferType;
int offset = 0;
int xn = data2.base;
int rt = data.target;
@ -158,8 +158,8 @@ word sts(a64inst_instruction cI) {
offset = 1 + data2.addressingMode * 2 + data2.a64inst_addressingModeData.indexedOffset * 4;
break;
}
out += sf*(2^30);
out += u*(2^22);
out += sf * (1 << 30);
out += u * (1 << 22);
out += offset * 1024;
out += xn * 32;
out += rt;
@ -167,12 +167,12 @@ word sts(a64inst_instruction cI) {
}
word ldl(a64inst_instruction cI) {
word out = 3*(2^27);
word out = 3 * (1 << 27);
a64inst_SingleTransferData data = cI.data.SingleTransferData;
int sf = data.regType;
int simm19 = data.processOpData.loadLiteralData.offset;
int rt = data.target;
out += sf * (2^30);
out += sf * (1 << 30);
out += simm19 * 32;
out += rt;
return out;
@ -184,7 +184,6 @@ void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr
// with values from symbol table
// after a line has had all the values replaced, assemble it and append
int index = 0;
int lbl = 0;
for (int i = 0; i < numInstrs; i++) {
a64inst_instruction cI = instrs[i];
switch (cI.type) {
@ -209,11 +208,11 @@ void secondPass(a64inst_instruction instrs[], int numInstrs, st* table, word arr
index++;
break;
case a64inst_HALT:
arr[index] = 69*(2^25);
arr[index] = 69U * (1 << 25);
index++;
break;
case a64inst_LABEL:
lbl++;
// Labels are handled in the first pass and used for addressing.
break;
case a64inst_BRANCH:
arr[index] = assembleBranch(&cI);