Makefile rewrite
This commit is contained in:
81
Makefile
81
Makefile
@ -1,39 +1,73 @@
|
||||
# ----- Definitions -----
|
||||
CC := gcc
|
||||
FLEX := flex
|
||||
BISON = bison
|
||||
|
||||
CFLAGS := -ggdb
|
||||
BISONFLAGS := -d
|
||||
|
||||
LEX := src/lexicalStructure.lex
|
||||
YACC := src/grammar.y
|
||||
EXE := alpha
|
||||
CFLAGS :=
|
||||
YACC := bison
|
||||
|
||||
OBJS := tmp/runner.o tmp/symbol_table.o tmp/grammar.tab.o tmp/lex.yy.o tmp/intermediate_code.o
|
||||
# tmp/intermediate_code.o codegen.o
|
||||
|
||||
TESTS-S1 := $(wildcard tests/sprint1/test/*.alpha)
|
||||
TESTS-S2 := $(wildcard tests/sprint2/test/*.alpha)
|
||||
TESTS-S3 := $(wildcard tests/sprint3/test/*.alpha)
|
||||
TESTS-S4 := $(wildcard tests/sprint4/test/*.alpha)
|
||||
# ----------
|
||||
|
||||
compiler: clean runner
|
||||
|
||||
tmp/grammar.tab.c: src/grammar.y
|
||||
|
||||
# ----- Targets -----
|
||||
all: compiler
|
||||
|
||||
compiler: clean tmp $(OBJS)
|
||||
$(CC) $(CFLAGS) -o $(EXE) $(OBJS)
|
||||
|
||||
clean:
|
||||
rm -f $(EXE)
|
||||
rm -rf out
|
||||
rm -rf tmp
|
||||
|
||||
tmp:
|
||||
mkdir -p tmp
|
||||
$(YACC) -d src/grammar.y
|
||||
mv grammar.tab.c tmp/
|
||||
mv grammar.tab.h tmp/
|
||||
|
||||
tmp/lex.yy.c: src/lexicalStructure.lex tmp/grammar.tab.c
|
||||
tmp/grammar.tab.c tmp/grammar.tab.h: $(YACC)
|
||||
$(BISON) $(BISONFLAGS) -o tmp/grammar.tab.c $(YACC)
|
||||
|
||||
tmp/lex.yy.c tmp/flex.h: $(LEX) tmp/grammar.tab.h
|
||||
$(FLEX) -o tmp/lex.yy.c $(LEX)
|
||||
mv flex.h tmp/
|
||||
mv flex.h tmp/flex.h
|
||||
# -----------
|
||||
|
||||
tmp/runner.o: src/runner.c src/runner.h tmp/flex.h
|
||||
$(CC) $(CFLAGS) -o tmp/runner.o -c src/runner.c
|
||||
|
||||
|
||||
# ----- Create Objs -----
|
||||
tmp/grammar.tab.o: tmp/grammar.tab.c
|
||||
$(CC) $(CFLAGS) -c tmp/grammar.tab.c -o tmp/grammar.tab.o
|
||||
|
||||
tmp/lex.yy.o: tmp/lex.yy.c
|
||||
$(CC) $(CFLAGS) -c tmp/lex.yy.c -o tmp/lex.yy.o
|
||||
|
||||
tmp/symbol_table.o: src/symbol_table.c src/symbol_table.h
|
||||
$(CC) $(CFLAGS) -o tmp/symbol_table.o -c src/symbol_table.c
|
||||
$(CC) $(CFLAGS) -c src/symbol_table.c -o tmp/symbol_table.o
|
||||
|
||||
runner: tmp/lex.yy.c tmp/runner.o tmp/symbol_table.o
|
||||
$(CC) $(CFLAGS) -o $(EXE) -g -ggdb tmp/runner.o tmp/grammar.tab.c tmp/lex.yy.c
|
||||
tmp/intermediate_code.o: src/intermediate_code.c src/intermediate_code.h
|
||||
$(CC) $(CFLAGS) -c src/intermediate_code.c -o tmp/intermediate_code.o
|
||||
|
||||
debug: CFLAGS += -DDEBUG=1
|
||||
debug: clean compiler
|
||||
# tmp/codegen.o: src/codegen.c src/codegen.h
|
||||
# $(CC) $(CFLAGS) -c src/codegen.c -o tmp/codegen.o
|
||||
|
||||
tmp/runner.o: src/runner.c src/runner.h tmp/flex.h tmp/grammar.tab.h
|
||||
$(CC) $(CFLAGS) -c src/runner.c -o tmp/runner.o
|
||||
# -----------
|
||||
|
||||
|
||||
|
||||
# ----- Tests -----
|
||||
test:
|
||||
chmod +x ./check.sh
|
||||
chmod +x ./test.sh
|
||||
@ -67,15 +101,6 @@ test-s4:
|
||||
./test.sh sp4
|
||||
./check.sh sp4
|
||||
|
||||
clean:
|
||||
rm -f *.o
|
||||
rm -f lex.yy.c
|
||||
rm -f $(EXE)
|
||||
rm -f flex.h
|
||||
rm -f *.tok
|
||||
rm -f grammar.tab.c
|
||||
rm -f grammar.tab.h
|
||||
rm -f *.st
|
||||
rm -rf out
|
||||
rm -rf tmp
|
||||
rm -f parser
|
||||
# Temprorary test ~ Scarlett
|
||||
test-make: clean tmp tmp/grammar.tab.c tmp/grammar.tab.h tmp/lex.yy.c tmp/flex.h tmp/grammar.tab.o tmp/lex.yy.o tmp/symbol_table.o tmp/runner.o
|
||||
# -----------
|
@ -1,10 +0,0 @@
|
||||
#ifndef GRAMMAR_H
|
||||
#define GRAMMAR_H
|
||||
|
||||
#include "../src/runner.h"
|
||||
|
||||
void yyerror(const char *err);
|
||||
int token_tracker;
|
||||
TableNode * tn;
|
||||
|
||||
#endif
|
@ -17,18 +17,21 @@
|
||||
// Please ask Scarlett if you are unsure of how to format something. Thanks! 😀
|
||||
|
||||
%{
|
||||
#include "../src/symbol_table.c"
|
||||
void yyerror(const char *err);
|
||||
#include "../src/symbol_table.h"
|
||||
extern FILE *asc_flag;
|
||||
extern bool tc_flag;
|
||||
int token_tracker;
|
||||
TableNode * tn;
|
||||
|
||||
typedef enum {
|
||||
ERROR_RUNTIME = 1,
|
||||
ERROR_SYNTAX = 2,
|
||||
ERROR_TYPE = 3,
|
||||
ERROR_UNDEFINED = 4
|
||||
} ErrorType;
|
||||
|
||||
int token_tracker;
|
||||
TableNode * tn;
|
||||
|
||||
void yyerror(const char *err);
|
||||
void throw_error(ErrorType error_type, const char *format, ...);
|
||||
%}
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
#include <stdio.h>
|
||||
#include "intermediate_code.h"
|
||||
|
||||
Instruction * begin;
|
||||
@ -9,11 +8,42 @@ char * temp = NULL;
|
||||
// TODO: this is here to bring your attention to the comment bellow.
|
||||
// check if start is NULL if it is assign it to the start globle variable
|
||||
// otherwise make it next of current and set cur to your instruction.
|
||||
TNodeOrConst * getOperand1(Instruction * i){
|
||||
return i->operand1;
|
||||
}
|
||||
|
||||
TNodeOrConst * tn_or_const(Op op, void * tnc) {
|
||||
TNodeOrConst * getOperand2(Instruction * i){
|
||||
return i->operand2;
|
||||
}
|
||||
|
||||
TableNode * get_result(Instruction * i){
|
||||
return i->result;
|
||||
}
|
||||
|
||||
Op getOp(Instruction * i){
|
||||
return i->opcode;
|
||||
}
|
||||
|
||||
int getLabel(Instruction * i){
|
||||
return i->label;
|
||||
}
|
||||
|
||||
int get_index(Instruction * i){
|
||||
return i->index;
|
||||
}
|
||||
|
||||
void set_label(Instruction * i, int label){
|
||||
i->label = label;
|
||||
}
|
||||
|
||||
bool isConst(TNodeOrConst * tnc) {
|
||||
return tnc->d != NODE;
|
||||
}
|
||||
|
||||
TNodeOrConst * tn_or_const(Discriminant d, void * tnc) {
|
||||
TNodeOrConst * count = calloc(1, sizeof(*count));
|
||||
count->d = op;
|
||||
switch (op) {
|
||||
count->d = d;
|
||||
switch (d) {
|
||||
case NODE:
|
||||
count->tnc_union->node = tnc;
|
||||
break;
|
||||
@ -52,10 +82,10 @@ static void emit_helper(void){
|
||||
void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2){
|
||||
emit_helper();
|
||||
current->opcode = op;
|
||||
// TODO: create temp and remove result from param list
|
||||
current->result = result;
|
||||
current->operand1 = arg1;
|
||||
current->operand2 = arg2;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
|
||||
@ -63,38 +93,162 @@ void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
|
||||
current->opcode = op;
|
||||
current->result = result;
|
||||
current->operand1 = arg;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_assignment(TableNode * target, TNodeOrConst * source){
|
||||
emit_helper();
|
||||
current->opcode = E_ASSIGN; // TODO: replace with move
|
||||
current->opcode = E_ASSIGN;
|
||||
current->result = target;
|
||||
current->operand1 = source;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_as_file(FILE * out_file, Instruction * instr_arr){
|
||||
if(instr_arr == NULL){
|
||||
return;
|
||||
char * get_string(TNodeOrConst * tc){
|
||||
char * s;
|
||||
switch (tc->d) {
|
||||
case NODE:
|
||||
return getName(tc->tnc_union->node);
|
||||
case ADDRESS:
|
||||
return strdup("null");
|
||||
case STRING:
|
||||
return tc->tnc_union->string;
|
||||
case INTEGER:
|
||||
s = calloc(10, sizeof(char));
|
||||
sprintf(s, "%d", tc->tnc_union->integer);
|
||||
return s;
|
||||
case CHARACTER:
|
||||
s = calloc(2, sizeof(char));
|
||||
sprintf(s, "%c", tc->tnc_union->character);
|
||||
return s;
|
||||
case BOOLEAN:
|
||||
if(tc->tnc_union->Boolean){
|
||||
return strdup("true");
|
||||
}
|
||||
return strdup("false");
|
||||
}
|
||||
|
||||
//fprintf(out_file,
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_as_file(FILE * out_file, Instruction * i){
|
||||
if(!i){
|
||||
return;
|
||||
}
|
||||
switch(i->opcode){
|
||||
case E_LABEL:
|
||||
// this is a terrible one to start with
|
||||
// fprintf(out_file, "%04.d: %d ", i->index, i->label);
|
||||
case E_ADD:
|
||||
fprintf(out_file, "%4.d: %s = %s + %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_SUB:
|
||||
fprintf(out_file, "%4.d: %s = %s - %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_MUL:
|
||||
fprintf(out_file, "%4.d: %s = %s * %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_DIV:
|
||||
fprintf(out_file, "%4.d: %s = %s / %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_MOD:
|
||||
fprintf(out_file, "%4.d: %s = %s %% %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_OR:
|
||||
fprintf(out_file, "%4.d: %s = %s | %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_AND:
|
||||
fprintf(out_file, "%4.d: %s = %s & %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_NEG:
|
||||
fprintf(out_file, "%4.d: %s = -%s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1));
|
||||
break;
|
||||
case E_NOT:
|
||||
fprintf(out_file, "%4.d: %s = !%s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand1));
|
||||
break;
|
||||
case E_ASSIGN:
|
||||
fprintf(out_file, "%4.d: %s = %s\n",
|
||||
i->index, getName(i->result),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
case E_GOTO:
|
||||
// are we ever going to use this?
|
||||
// yes we do look at bounds checking
|
||||
case E_IF_X_TRUE:
|
||||
fprintf(out_file, "%4.d: if %s goto %d\n",
|
||||
i->index, get_string(i->operand1),
|
||||
i->label);
|
||||
break;
|
||||
case E_IF_X_FALSE:
|
||||
fprintf(out_file, "%4.d: if %s false goto %d\n",
|
||||
i->index, get_string(i->operand1),
|
||||
i->label);
|
||||
break;
|
||||
case E_LESS_THAN:
|
||||
fprintf(out_file, "%4.d: if %s < %s goto %d\n",
|
||||
i->index, get_string(i->operand1),
|
||||
get_string(i->operand2), i->label);
|
||||
break;
|
||||
case E_EQUAL_TO:
|
||||
fprintf(out_file, "%4.d: if %s = %s goto %d\n",
|
||||
i->index, get_string(i->operand1),
|
||||
get_string(i->operand2), i->label);
|
||||
break;
|
||||
case E_CALL:
|
||||
fprintf(out_file, "%4.d: call %s %s\n",
|
||||
i->index, get_string(i->operand1),
|
||||
get_string(i->operand2));
|
||||
break;
|
||||
|
||||
case E_PARAM:
|
||||
fprintf(out_file, "%4.d: param %s \n",
|
||||
i->index, get_string(i->operand1));
|
||||
break;
|
||||
case E_RETURN:
|
||||
|
||||
case E_INDEX_COPY_RIGHT:
|
||||
case E_INDEX_COPY_LEFT:
|
||||
|
||||
case E_ADDRESS_OF:
|
||||
|
||||
case E_DEREF_RIGHT:
|
||||
case E_DEREF_LEFT:
|
||||
}
|
||||
|
||||
emit_as_file(out_file, i->next);
|
||||
}
|
||||
|
||||
void emit_label(int label){
|
||||
emit_helper();
|
||||
current->opcode = E_LABEL;
|
||||
current->label = label;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_jump(int label){
|
||||
emit_helper();
|
||||
current->opcode = E_GOTO;
|
||||
current->label = label;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_conditional_jump(Op condition, int label, ...){
|
||||
@ -112,7 +266,7 @@ void emit_conditional_jump(Op condition, int label, ...){
|
||||
n1 = va_arg(argptr, TNodeOrConst *);
|
||||
current->operand1 = n1;
|
||||
break;
|
||||
case E_LESSTHEN: case E_EQUALTO:
|
||||
case E_LESS_THAN: case E_EQUAL_TO:
|
||||
n1 = va_arg(argptr, TNodeOrConst *);
|
||||
n2 = va_arg(argptr, TNodeOrConst *);
|
||||
current->operand1 = n1;
|
||||
@ -120,7 +274,6 @@ void emit_conditional_jump(Op condition, int label, ...){
|
||||
break;
|
||||
}
|
||||
va_end(argptr);
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_function_start(int name){
|
||||
@ -128,14 +281,12 @@ void emit_function_start(int name){
|
||||
current->opcode = E_LABEL; // I think this is right TODO: ask
|
||||
current->label = name;
|
||||
// this is probabaly a func decleration
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_parameter(TNodeOrConst * param){
|
||||
emit_helper();
|
||||
current->opcode = E_PARAM;
|
||||
current->operand1 = param;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name){
|
||||
@ -144,37 +295,80 @@ void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name
|
||||
current->operand1 = tn_or_const(INTEGER, ¶m_count);
|
||||
current->operand2 = name;
|
||||
current->result = result;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_return(TNodeOrConst * value){
|
||||
emit_helper();
|
||||
current->opcode = E_RETURN;
|
||||
current->operand1 = value;
|
||||
return;
|
||||
}
|
||||
void emit_reserve(char* result, char* type_name, int size){
|
||||
emit_helper();
|
||||
return;
|
||||
|
||||
void emit_reserve(TableNode * result, TNodeOrConst * size){
|
||||
emit_parameter(size);
|
||||
emit_function_call(result, 1, tn_or_const(NODE, look_up(cur, "reserve")));
|
||||
}
|
||||
void emit_release(char* pointer){
|
||||
emit_helper();
|
||||
|
||||
void emit_release(TableNode * pointer){
|
||||
emit_parameter(tn_or_const(NODE, pointer));
|
||||
emit_function_call(pointer, 1, tn_or_const(NODE, look_up(cur, "release")));
|
||||
}
|
||||
|
||||
void emit_deref_right(){
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_deref_left(){
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_field_access(char* result, char* record, char* field){
|
||||
emit_helper();
|
||||
return;
|
||||
}
|
||||
void emit_array_access(char* result, char* array, char* index, char* dimension){
|
||||
emit_helper();
|
||||
return;
|
||||
}
|
||||
void emit_bounds_check(char* index, char* size, char* error_label){
|
||||
emit_helper();
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index){
|
||||
emit_helper();
|
||||
current->opcode;
|
||||
current->result = result;
|
||||
current->operand1 = array;
|
||||
current->operand2 = index;
|
||||
// TODO: Still don't know what to do with the dimentions
|
||||
}
|
||||
|
||||
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr){
|
||||
/*
|
||||
{[string: 5]
|
||||
.
|
||||
.
|
||||
s:= reserve s(5);
|
||||
s(0) := 'H';
|
||||
s(1) := 'e';
|
||||
.
|
||||
.
|
||||
s._0 num of dims Known at compile time
|
||||
s._1 size Known at run time
|
||||
s._1 int | 1 byte
|
||||
+-------+---+---+---+---+---+
|
||||
| 5 | H | e | l | l | o |
|
||||
+-------+---+---+---+---+---+
|
||||
size
|
||||
^
|
||||
|
|
||||
p
|
||||
s._0 ok
|
||||
s._1 ok
|
||||
s._2 not ok
|
||||
t_0 is index
|
||||
t_1 = *(int *)p = s._1
|
||||
if t_0 < 0 GOTO ERROR
|
||||
if t_0 < s._1 GOTO access array
|
||||
GOTO ERROR
|
||||
*/
|
||||
emit_conditional_jump(E_LESS_THAN, );
|
||||
emit_conditional_jump(E_LESS_THAN, );
|
||||
emit_jump();
|
||||
/* We need a label ERROR to jump to
|
||||
*/
|
||||
}
|
||||
|
||||
// * Implement temp variable generator function that produces unique names (t1, t2, etc.)
|
||||
char * temp_var_gen(){
|
||||
@ -189,4 +383,4 @@ char * label_gen(){
|
||||
sprintf(ret, "L_%d", label_count);
|
||||
label_count++;
|
||||
return ret;
|
||||
}
|
||||
}
|
@ -2,12 +2,15 @@
|
||||
// * Add Bison actions for arithmetic expressions:
|
||||
// - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3);
|
||||
// - Subtraction, multiplication, division, modulo
|
||||
#include "runner.h"
|
||||
#pragma once
|
||||
|
||||
#include "symbol_table.h"
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
// these are from page 364
|
||||
typedef enum {
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_ADD, // 1 from the list
|
||||
E_SUB, // 1
|
||||
E_MUL, // 1
|
||||
@ -22,16 +25,16 @@ typedef enum {
|
||||
E_COND_GOTO, // 5 I don't thik I need this because we could just follow the < or the = and just assume that it's a cond got
|
||||
E_IF_X_TRUE, // 5
|
||||
E_IF_X_FALSE, // 5
|
||||
E_LESSTHEN, // 6 rule 1 + 5
|
||||
E_EQUALTO, // 6 rule 1 + 5
|
||||
E_LESS_THAN, // 6 rule 1 + 5
|
||||
E_EQUAL_TO, // 6 rule 1 + 5
|
||||
E_CALL, // 7
|
||||
E_PARAM, // 7
|
||||
E_RETURN, // 7
|
||||
E_INDEX_COPY_RIGHT, // 8
|
||||
E_INDEX_COPY_LEFT, // 8
|
||||
E_ADDRESS_OF // 9
|
||||
/* for x = *y and *y = x we can just use index copy right and left with
|
||||
index 0*/
|
||||
E_INDEX_COPY_RIGHT, // 8 this is x = y[i]
|
||||
E_INDEX_COPY_LEFT, // 8 x[i] = y
|
||||
E_ADDRESS_OF, // 9 x = &y
|
||||
E_DEREF_RIGHT, // 9 x = *y
|
||||
E_DEREF_LEFT // 9 x* = y
|
||||
} Op;
|
||||
|
||||
typedef enum {
|
||||
@ -70,6 +73,20 @@ typedef struct Instruction {
|
||||
Instruction * next;
|
||||
} Instruction;
|
||||
|
||||
|
||||
typedef struct TFList {
|
||||
Instruction * i;
|
||||
TFList * next;
|
||||
} TFList;
|
||||
|
||||
TFList * make_list(Instruction * i);
|
||||
// - makelist(i) function to create instruction lists
|
||||
void merge(TFList * l1, TFList * l2);
|
||||
// - merge(p1,p2) function to concatenate lists
|
||||
void backpatch(TFList * l, int label);
|
||||
// - backpatch(p,i) function to fill in jump targets
|
||||
|
||||
|
||||
extern Instruction * begin;
|
||||
extern Instruction * current;
|
||||
int temp_count = 0;
|
||||
@ -77,34 +94,62 @@ int label_count = 0;
|
||||
bool code_gen = true;
|
||||
|
||||
|
||||
|
||||
TNodeOrConst * tn_or_const(Op op, void * tnc);
|
||||
|
||||
TNodeOrConst * tn_or_const(Discriminant , void * );
|
||||
void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2);
|
||||
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg);
|
||||
void emit_assignment(TableNode * target, TNodeOrConst * source);
|
||||
// TODO: Find out what these are suposed to do. Guess is create an entry in
|
||||
// the list of instructions. Guess is that its suposed to ret a struct ptr
|
||||
|
||||
// * Implement integer/boolean/character specific operation handling
|
||||
// TODO: Find out what this means.
|
||||
|
||||
// * Create output function to write instructions to file with line formatting
|
||||
void emit_as_file(FILE * out_file, Instruction * instr_arr);
|
||||
|
||||
// * Implement instruction array storage for backpatching
|
||||
|
||||
void emit_label(int label);
|
||||
void emit_jump(int label);
|
||||
|
||||
void emit_conditional_jump(Op condition, int label, ...);
|
||||
|
||||
void emit_function_start(int name);
|
||||
void emit_parameter(TNodeOrConst * param);
|
||||
void emit_function_call(TableNode * result, int param_count, TNodeOrConst * name);
|
||||
void emit_return(TNodeOrConst * value);
|
||||
void emit_reserve(char* result, char* type_name, int size);
|
||||
void emit_release(char* pointer);
|
||||
|
||||
void emit_reserve(TableNode * result, TNodeOrConst * size);
|
||||
void emit_release(TableNode * pointer);
|
||||
void emit_field_access(char* result, char* record, char* field);
|
||||
void emit_array_access(char* result, char* array, char* index, char* dimension);
|
||||
void emit_bounds_check(char* index, char* size, char* error_label);
|
||||
void emit_array_access(Op op, TableNode * result, TNodeOrConst * array, TNodeOrConst * index);
|
||||
void emit_bounds_check(TNodeOrConst * index, TNodeOrConst * arr, int error_label);
|
||||
|
||||
// * Implement instruction array storage for backpatching
|
||||
/*
|
||||
Track 2: Control Flow & Boolean Expressions
|
||||
* Implement backpatching infrastructure:
|
||||
* Create truelist and falselist attributes for Boolean expressions
|
||||
* Create control flow emission functions:
|
||||
* Add Bison actions for control structures:
|
||||
- if-then-else with backpatching
|
||||
- while loops with backpatching
|
||||
* Implement short-circuit Boolean operations (&&, ||, !)
|
||||
* Add marker (M) nonterminal for recording instruction positions
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
Track 3: Functions & Complex Types
|
||||
* Implement function-related emission:
|
||||
* Add Bison actions for the 'as' clause
|
||||
* Create memory layout calculation functions:
|
||||
- calculate_record_size(Record_Type* type) → returns bytes needed
|
||||
- calculate_array_size(Array_Type* type, int dimensions[]) → returns total bytes
|
||||
- calculate_field_offset(Record_Type* type, char* field_name) → returns offset
|
||||
* Add Bison actions for arrays and records
|
||||
*/
|
||||
|
||||
/*
|
||||
Track 4: Memory Access & Integration
|
||||
* Implement array and record access code:
|
||||
- emit_field_access(char* result, char* record, char* field)
|
||||
- emit_array_access(char* result, char* array, char* index, char* dimension)
|
||||
* Add array dimension access (a._1, a._2, etc.)
|
||||
* Implement bounds checking emission:
|
||||
- emit_bounds_check(char* index, char* size, char* error_label)
|
||||
* Create the code generation driver function
|
||||
* Implement common error handling
|
||||
* Document the complete intermediate instruction set
|
||||
* Build integration test suite covering all language features
|
||||
* Implement row-major/column-major array layout calculation
|
||||
*/
|
@ -1,33 +0,0 @@
|
||||
#ifndef LEXICALSTRUCTURE_H
|
||||
#define LEXICALSTRUCTURE_H
|
||||
|
||||
#include "../tmp/grammar.tab.h"
|
||||
#include "../src/symbol_table.h"
|
||||
|
||||
extern SymbolTable * cur;
|
||||
extern FILE* tok_flag;
|
||||
extern TableNode *funprime;
|
||||
extern TableNode *funtypeprime;
|
||||
extern TableNode *arrayprim;
|
||||
extern TableNode *recprime;
|
||||
extern TableNode *integ;
|
||||
extern TableNode *addr;
|
||||
extern TableNode *chara;
|
||||
extern TableNode *stri;
|
||||
extern TableNode *boo;
|
||||
extern TableNode *undefined;
|
||||
extern void incr(int lnum,int cnum, int tok);
|
||||
extern void print_tok(int tok);
|
||||
|
||||
extern int line_number;
|
||||
extern int column_number;
|
||||
extern int yycolumn;
|
||||
#define YY_USER_ACTION { \
|
||||
yylloc.first_line = yylineno; \
|
||||
yylloc.last_line = yylineno; \
|
||||
yylloc.first_column = yycolumn; \
|
||||
yylloc.last_column = yycolumn + yyleng - 1; \
|
||||
yycolumn += yyleng; \
|
||||
}
|
||||
|
||||
#endif
|
@ -1,6 +1,8 @@
|
||||
/* Runner File - Compiles alpha Compiler */
|
||||
/* The Translators - Spring 2025 */
|
||||
|
||||
#pragma once
|
||||
|
||||
#define ALPHA_OFFSET 6
|
||||
#define TOK_LEN 3
|
||||
#define ST_LEN 2
|
||||
|
@ -1072,7 +1072,6 @@ TableNode *look_up(SymbolTable *table, char *x) {
|
||||
}
|
||||
|
||||
int col_widths[5] = {30, 8, 8, 35, 35};
|
||||
void printline(FILE *file_ptr, bool b);
|
||||
void printline(FILE *file_ptr, bool b) {
|
||||
if (b) {
|
||||
fprintf(file_ptr, "oop\n");
|
||||
@ -1087,7 +1086,6 @@ void printline(FILE *file_ptr, bool b) {
|
||||
fprintf(file_ptr, "\n");
|
||||
}
|
||||
|
||||
void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5);
|
||||
void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5) {
|
||||
if (label3 == -100) {
|
||||
fprintf(file_ptr, "%-*s: %0*d : %*s :%-*s:%-*s\n",
|
||||
|
@ -1,3 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
@ -82,12 +84,20 @@ typedef enum {
|
||||
TYPE_PRIMITIVE_TYPE = 12
|
||||
} types;
|
||||
|
||||
void printdebug_impl(char *file, int line, const char *format, ...);
|
||||
#define printdebug(format, ...) \
|
||||
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
void printdebug_impl(char *file, int line, const char *format, ...);
|
||||
AdInfo *CreatePrimitiveInfo(int size);
|
||||
int getPrimSize(TableNode *definition);
|
||||
AdInfo *CreateArrayInfo(int dim, TableNode *type);
|
||||
int getNumArrDim(TableNode *definition);
|
||||
TableNode *getArrType(TableNode *definition);
|
||||
AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope);
|
||||
int getRecTotal(TableNode *node);
|
||||
TableNode *setRecOffsetInfo(SymbolTable *scope, TableNode *node);
|
||||
int *getRecOffsets(TableNode *node);
|
||||
int getRecLength(TableNode *definition);
|
||||
SymbolTable *getRecList(TableNode *definition);
|
||||
TableNode *setRecSize(TableNode *tn, int n);
|
||||
@ -103,19 +113,21 @@ TableNode *getReturn(TableNode *definition);
|
||||
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column);
|
||||
SymbolTable *init(SymbolTable *start);
|
||||
TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info);
|
||||
AdInfo *getAdInfo(TableNode *tn);
|
||||
int getAdInfoType(TableNode *tn);
|
||||
TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id,
|
||||
AdInfo *ad);
|
||||
TableNode *CreateEntry(SymbolTable *table, int tag, TableNode *typeOf, char *id, AdInfo *ad);
|
||||
TableNode *getTypeEntry(TableNode *tn);
|
||||
char *getType(TableNode *tn);
|
||||
char *getName(TableNode *tn);
|
||||
int getLine(SymbolTable *st);
|
||||
int getColumn(SymbolTable *st);
|
||||
TableNode *getTypeEntry(TableNode *tn);
|
||||
TableNode *addName(TableNode *tn, char *str);
|
||||
SymbolTable *setLineNumber(SymbolTable *st, int line);
|
||||
SymbolTable *setColumnNumber(SymbolTable *st, int column);
|
||||
TableNode *table_lookup(SymbolTable *table, char *x);
|
||||
TableNode *look_up(SymbolTable *table, char *x);
|
||||
void printline(FILE *file_ptr, bool b);
|
||||
void st_fprint(FILE *file_ptr, char *label1, int label2, int label3, char *label4, char *label5);
|
||||
void print_symbol_table(SymbolTable *table, FILE *file_ptr);
|
||||
SymbolTable *getAncestor(SymbolTable *table);
|
||||
SymbolTable *removeEntry(SymbolTable *scope, char *search);
|
||||
@ -126,11 +138,7 @@ SymbolTable *getFirstChild(ListOfTable *lt);
|
||||
ListOfTable *getRestOfChildren(ListOfTable *lt);
|
||||
TableNode *getFirstEntry(SymbolTable *st);
|
||||
TableNode *getNextEntry(TableNode *tn);
|
||||
|
||||
TableNode *printTableNode(TableNode *tn);
|
||||
void printdebug_impl(char *file, int line, const char *format, ...);
|
||||
#define printdebug(format, ...) \
|
||||
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
|
||||
|
||||
extern int yylex(void);
|
||||
extern char *yytext;
|
||||
|
29
test.alpha
29
test.alpha
@ -1,29 +0,0 @@
|
||||
|
||||
|
||||
|
||||
type a : 1 -> integers
|
||||
type t : integer -> a
|
||||
type r : integer -> integer
|
||||
|
||||
|
||||
|
||||
function foo : t
|
||||
function bar : r
|
||||
function entry :
|
||||
|
||||
bar(a) := {
|
||||
5 + bar(a - 1);
|
||||
return a * bar(a-1);
|
||||
}
|
||||
|
||||
foo(c) := {
|
||||
[a: arg]
|
||||
arg := reserve arg(c);
|
||||
return arg;
|
||||
}
|
||||
|
||||
entry(args) := {
|
||||
[a: b]
|
||||
b := foo(8);
|
||||
}
|
||||
|
Reference in New Issue
Block a user