I merged IR-Linting to Dev

This commit is contained in:
Meyer Simon
2025-05-02 13:39:58 -04:00
12 changed files with 407 additions and 156 deletions

View File

@ -15,16 +15,17 @@ void S_Free(Stack *s){
free(s);
}
void S_Push(Stack * s, void *v) {
void S_Push(Stack * s, void *v, int i) {
__Node * n = calloc(1, sizeof(*n));
n->v = v;
n->next = s->n;
s->n = n;
s->w = i;
s->size = s->size + 1;
}
void * S_Pop(Stack *s) {
if (s->size == 0) {
if (s == NULL || S_IsEmpty(s)) {
return NULL;
}
__Node * node = s->n;
@ -37,22 +38,31 @@ void * S_Pop(Stack *s) {
void * S_Peek(Stack *s){
if (!S_IsEmpty(s)) {
if (s == NULL || S_IsEmpty(s)) {
return NULL;
}
return s->n->v;
}
bool S_IsEmpty(Stack *s){
if(!s->size) {
if(s == NULL || s->size == 0) {
return true;
}
return false;
}
int S_Size(Stack *s){
if (s == NULL || S_IsEmpty(s)) {
return 0;
}
return s->size;
}
void emit_backpatch(Stack * s, int l){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
i->label = l;
}
}
//_______________________________________________________________________
char * temp = NULL;
@ -64,11 +74,28 @@ char * temp = NULL;
otherwise make it next of current and set cur to your instruction.
*/
void emit_push_all(Stack * s){
for (Instruction * i = S_Pop(s); i; i = S_Pop(s)){
current->next = i;
i->prev = current;
i->index = current->index + 1;
current = i;
current->next = NULL;
}
}
void emit_detach(){
current = current->prev;
current->next = NULL;
}
void backpatch(Stack *s, int l){
while (!S_IsEmpty(s)){
Instruction * i = S_Pop(s);
set_label(i, l);
}
}
TNodeOrConst * getOperand1(Instruction * i){
return i->operand1;
}
@ -129,10 +156,6 @@ TNodeOrConst * tn_or_const(Discriminant d, void * tnc) {
}
static void emit_helper(void){
if (!ir_flag) {
begin = NULL;
return;
}
Instruction * inst = calloc(1, sizeof(*inst));
if(begin == NULL){
begin = current = inst;
@ -159,6 +182,12 @@ void emit_binary_op(
current->operand2 = arg2;
}
void emit_goto(int i){
emit_helper();
current->opcode = E_GOTO;
current->label = i;
}
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
emit_helper();
current->opcode = op;
@ -254,8 +283,8 @@ void emit_function_call(
){
emit_helper();
current->opcode = E_CALL;
current->operand1 = tn_or_const(INTEGER, &param_count);
current->operand2 = name;
current->operand1 = name;
current->operand2 = tn_or_const(INTEGER, &param_count);
current->result = result;
}
@ -472,7 +501,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break;
case E_IF_X_TRUE:
fprintf(out_file,
"%4.d: if %s goto %d\n",
"%4.d: if %s GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
@ -480,7 +509,7 @@ void emit_as_file(FILE * out_file, Instruction * i){
break;
case E_IF_X_FALSE:
fprintf(out_file,
"%4.d: if %s false goto %d\n",
"%4.d: if %s false GOTO %d\n",
i->index,
get_string(i->operand1),
i->label
@ -489,21 +518,21 @@ void emit_as_file(FILE * out_file, Instruction * i){
case E_LESS_THAN:
// this feels wrong I need to TODO: this
fprintf(out_file,
"%4.d: %s = %s < %s\n",
"%4.d: if ( %s < %s ) GOTO %d\n",
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
get_string(i->operand2),
i->label
);
break;
case E_EQUAL_TO:
// this feels wrong I need to TODO: this
fprintf(out_file,
"%4.d: %s = %s == %s\n",
"%4.d: if ( %s = %s ) GOTO %d\n",
i->index,
getName(i->result),
get_string(i->operand1),
get_string(i->operand2)
get_string(i->operand2),
i->label
);
break;
case E_CALL: