Skip to content

Commit

Permalink
labels (a5): add labels to everywhere that might be jumped to to simp…
Browse files Browse the repository at this point in the history
…lify translation (needs more manual validation)
  • Loading branch information
SantriptaSharma committed Dec 6, 2023
1 parent a75c394 commit ab74829
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 15 deletions.
36 changes: 29 additions & 7 deletions 15_A5.y
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

%%
/* auxiliary symbols */
marker: { $$ = quads_size; } // just store the quad index of the next instruction on reduction
marker: { $$ = quads_size; Emit(JmpLabel()); } // just store the quad index of the next instruction on reduction
guard: { $$ = MakeList(quads_size); Emit(Jump(AImm(0))); } // emit an unconditional jump, and store a next list to be filled in by rules that use guards

/* expressions */
Expand Down Expand Up @@ -467,13 +467,16 @@ conditional_expression:

$$ = PURE_EXPR(GenTemp());
Backpatch($6, quads_size);
Emit(JmpLabel());
Emit(Mov(ASym($$), ASym($5)));
size_t q = quads_size;
Emit(Jump(AImm(0)));

Backpatch($10, quads_size);
Emit(JmpLabel());
Emit(Mov(ASym($$), ASym($9)));
quads[q].rd.imm = quads_size;
Emit(JmpLabel());
}

assignment_expression:
Expand Down Expand Up @@ -681,7 +684,7 @@ compound_statement:

block_item_list:
block_item
| block_item_list marker block_item { Backpatch($1, $2); $$ = $3; Backpatch($$, quads_size); }
| block_item_list marker block_item { Backpatch($1, $2); $$ = $3; Backpatch($$, quads_size); Emit(JmpLabel()); }

block_item:
declaration { $$ = NULL; }
Expand Down Expand Up @@ -716,12 +719,24 @@ selection_statement:
}

iteration_statement:
FOR '(' opt_expression ';' marker opt_expression ';' marker opt_expression guard ')' marker statement {
QuadList *tl = $6.has_expr ? $6.expr.truelist : NULL;
FOR '(' opt_expression ';' marker opt_expression {
if (!$6.has_expr) {
$6.has_expr = 1;
$6.expr = BOOL_EXPR(GenTemp(), MakeList(quads_size), NULL);
Emit(Jump(AImm(0)));
}
} ';' marker opt_expression guard ')' marker statement {
QuadList *tl = $6.expr.truelist;

if (tl == NULL) {
tl = MakeList(quads_size);
Emit(Jump(AImm(0)));
}

QuadList *fl = $6.has_expr ? $6.expr.falselist : NULL;

Backpatch(tl, $12); Backpatch($10, $5); Backpatch($13, $8);
Emit(Jump(AImm($8)));
Backpatch(tl, $13); Backpatch($11, $5); Backpatch($14, $9);
Emit(Jump(AImm($9)));
$$ = fl;
}

Expand All @@ -734,6 +749,8 @@ jump_statement:

$$ = NULL;

Emit(JmpLabel());

if ($2.has_expr == 1) {
Emit(Mov(((Addr){SYMBOL_A, .sym = SymLookup("__retval", 0)}), ASym($2.expr)));
}
Expand Down Expand Up @@ -788,9 +805,14 @@ function_definition:

if (quads[quads_size - 1].opcode != RET) {
Backpatch($4, quads_size);
Emit(JmpLabel());
Emit(Return(AImm(0)));
} else {
Backpatch($4, quads_size - 1);
if (quads[quads_size-2].opcode == MOV) {
Backpatch($4, quads_size - 3);
} else {
Backpatch($4, quads_size - 2);
}
}
}
%%
Expand Down
34 changes: 32 additions & 2 deletions 15_A5_translator.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include "15_A5_translator.h"
#include "compiler.h"

extern int yyparse();
extern void yyerror(char *s);
Expand All @@ -10,6 +11,8 @@ const unsigned int size_of_char = 1;
const unsigned int size_of_int = 4;
const unsigned int size_of_pointer = 4;

int label_count = 0;

int quads_size = 0, quads_capacity = 64;
Quad *quads;

Expand Down Expand Up @@ -231,14 +234,17 @@ void DisplayQuad(Quad q) {
DisplayAddr(q.rs);
break;
case FN_LABEL:
printf("%s:", q.rd.sym->name);
if (q.rd.kind == SYMBOL_A)
printf("%s:", q.rd.sym->name);
else
printf("_L_%d_:", q.rd.imm);
break;
}
}

void DisplayQuads() {
for (int i = 0; i < quads_size; i++) {
if (quads[i].opcode == FN_LABEL) printf("\n");
if (quads[i].opcode == FN_LABEL && quads[i].rd.kind == SYMBOL_A) printf("\n");
printf("%d: ", i);
DisplayQuad(quads[i]);
printf("\n");
Expand Down Expand Up @@ -716,6 +722,30 @@ int main() {
SymTableDispl(current_table);
DisplayQuads();

char valid = 1;

// Verify quads: the target of each jump is a label
for (int i = 0; i < quads_size; i++) {
Quad q = quads[i];
if (q.opcode == JMP || q.opcode == JIF || q.opcode == JNT
|| q.opcode == JLT || q.opcode == JGT || q.opcode == JEQ
|| q.opcode == JNE || q.opcode == JLE || q.opcode == JGE) {
int ind = q.rd.imm;
if (quads[ind].opcode != FN_LABEL) {
printf("On quad %d, jump target %d is not a label\n", i, ind);
valid = 0;
}
}
}

if (!valid) {
printf("Quads are invalid, aborting\n");
FreeTables();
FreeQuads();
DestroyLists();
return 1;
}

// TODO: write quads to file
// TODO: append input lib declarations to symbol table

Expand Down
3 changes: 3 additions & 0 deletions 15_A5_translator.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef struct _Addr {
};
} Addr;

extern int label_count;

// Used for true, false, and next lists within symbols
typedef struct _QuadList {
int quad_index;
Expand Down Expand Up @@ -78,6 +80,7 @@ void Emit(Quad q);
#define IndexWrite(dest, index, source) ((Quad){INDW, source, index, dest})
#define DerefWrite(dest, source) ((Quad){PTRW, dest, AImm(0), source})
#define FnLabel(label) ((Quad){FN_LABEL, AImm(0), AImm(0), label})
#define JmpLabel() ((Quad){FN_LABEL, AImm(0), AImm(0), AImm(label_count++)})

// Functions for printing a quad & the quads list
void DisplayQuad(Quad q);
Expand Down
5 changes: 5 additions & 0 deletions A4_tests/testsimple.nc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
int GLOB = 1;
char POPPING_thing = 'c';
char charithmetic = (POPPING_thing + 'd') - 'c';
char ithmetic2 = charithmetic - 'd';
int arithmetic = ithmetic2 + 5;
char d = +'c';
char f = -'f';

void printf(char *c, int argv);

Expand Down
9 changes: 5 additions & 4 deletions A5_Tests/test5.nc
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
int d = 99;

int * fun2(int b){
int * e = 0;
return e;
}

int fun(int c){
if(c%d == 0) return fun2(c);
else return fun(c+1);
}

int * fun2(int b){
int * e = 0;
return e;
}
int main(){
int a = 1;
int * ret = fun(a);
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ build: $(o)
test: build
./$(o) < $(testfile)

$(o): $(fname)_translator.o $(fname).tab.o $(lx).o
$(o): $(fname)_translator.o $(fname).tab.o $(lx).o compiler.o
$(CC) -o $@ $^ $(CFLAGS)

$(lx): lexmain.o $(lx).o
Expand Down
4 changes: 4 additions & 0 deletions compiler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <stdio.h>

#include "compiler.h"

9 changes: 9 additions & 0 deletions compiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _COMPILER_H_
#define _COMPILER_H_
#include "15_A5_translator.h"

extern int quads_size, quads_capacity;
extern Quad *quads;


#endif
2 changes: 1 addition & 1 deletion createa5.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cp tex_build/15_A5.pdf .
tar -c 15_A5_translator.c 15_A5_translator.h 15_A5.l 15_A5.nc 15_A5.y Makefile 15_A5.pdf 15_A5_quads* -f 15_A5.tar
tar -c 15_A5_translator.c 15_A5_translator.h compiler.c compiler.h 15_A5.l 15_A5.nc 15_A5.y Makefile 15_A5.pdf 15_A5_quads* -f 15_A5.tar
rm 15_A5.pdf

0 comments on commit ab74829

Please sign in to comment.