deleted extra files
This commit is contained in:
44
cg.s
44
cg.s
@ -1,44 +0,0 @@
|
||||
movl $3, 0(%rbp)
|
||||
movl $2, 1(%rbp)
|
||||
movl $8, 2(%rbp)
|
||||
movl 1(%rbp), %eax
|
||||
subl 2(%rbp), %eax
|
||||
movl %eax, 3(%rbp)
|
||||
movl 0(%rbp), %eax
|
||||
addl 0(%rbp), %eax
|
||||
movl %eax, 4(%rbp)
|
||||
movl $3, 5(%rbp)
|
||||
movl $2, 6(%rbp)
|
||||
movl $8, 7(%rbp)
|
||||
movl 6(%rbp), %eax
|
||||
cltd
|
||||
idivl 7(%rbp)
|
||||
movl %eax, 8(%rbp)
|
||||
movl 5(%rbp), %eax
|
||||
subl 8(%rbp), %eax
|
||||
movl %eax, 9(%rbp)
|
||||
movl $2, 10(%rbp)
|
||||
movl $8, 11(%rbp)
|
||||
movl $3, 12(%rbp)
|
||||
movl $2, 13(%rbp)
|
||||
movl 12(%rbp), %eax
|
||||
subl 13(%rbp), %eax
|
||||
movl %eax, 14(%rbp)
|
||||
movl $8, 15(%rbp)
|
||||
movl 14(%rbp), %eax
|
||||
cltd
|
||||
idivl 15(%rbp)
|
||||
movl %edx, 16(%rbp)
|
||||
movl $3, 17(%rbp)
|
||||
movl $2, 18(%rbp)
|
||||
movl 17(%rbp), %eax
|
||||
cltd
|
||||
idivl 18(%rbp)
|
||||
movl %edx, 19(%rbp)
|
||||
movl $8, 20(%rbp)
|
||||
movl 19(%rbp), %eax
|
||||
subl 20(%rbp), %eax
|
||||
movl %eax, 21(%rbp)
|
||||
movl $3, 22(%rbp)
|
||||
movl $8, 23(%rbp)
|
||||
movl $0, 24(%rbp)
|
@ -1,188 +0,0 @@
|
||||
|
||||
|
||||
|
||||
// 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 * tn_or_const(Op op, void * tnc) {
|
||||
TNodeOrConst * count = calloc(1, sizeof(*count));
|
||||
count->d = op;
|
||||
switch (op) {
|
||||
case NODE:
|
||||
count->tnc_union->node = tnc;
|
||||
break;
|
||||
case ADDRESS:
|
||||
count->tnc_union->address = tnc;
|
||||
break;
|
||||
case STRING:
|
||||
count->tnc_union->string = tnc;
|
||||
break;
|
||||
case INTEGER:
|
||||
count->tnc_union->integer = *(int*)tnc;
|
||||
break;
|
||||
case CHARACTER:
|
||||
count->tnc_union->character = *(char*)tnc;
|
||||
break;
|
||||
case BOOLEAN:
|
||||
count->tnc_union->Boolean = *(bool*)tnc;
|
||||
break;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static void emit_helper(void){
|
||||
Instruction * inst = calloc(1, sizeof(*inst));
|
||||
if(begin == NULL){
|
||||
begin = current = inst;
|
||||
current->index = 1;
|
||||
} else {
|
||||
current->next = inst;
|
||||
inst->prev = current;
|
||||
inst->index = current->index++;
|
||||
current = inst;
|
||||
}
|
||||
}
|
||||
|
||||
void emit_binary_op(Op op, TableNode * result, TNodeOrConst * arg1, TNodeOrConst * arg2){
|
||||
emit_helper();
|
||||
current->opcode = op;
|
||||
current->result = result;
|
||||
current->operand1 = arg1;
|
||||
current->operand2 = arg2;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_unary_op(Op op, TableNode * result, TNodeOrConst * arg){
|
||||
emit_helper();
|
||||
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->result = target;
|
||||
current->operand1 = source;
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_as_file(FILE * out_file, Instruction * instr_arr){
|
||||
if(instr_arr == NULL){
|
||||
return;
|
||||
}
|
||||
|
||||
//fprintf(out_file,
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
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, ...){
|
||||
// when this instruction is a conditional jump then the imput looks like (Op, int, TNodeOrConst *).
|
||||
// when the inst is a cond with a Relational operation then the input looks like (Op, int, TNodeOrConst *, TNodeOrConst *)
|
||||
emit_helper();
|
||||
va_list argptr;
|
||||
va_start(argptr, label);
|
||||
current->opcode = condition;
|
||||
current->label = label;
|
||||
TNodeOrConst * n1;
|
||||
TNodeOrConst * n2;
|
||||
switch (condition) {
|
||||
case E_IF_X_TRUE: case E_IF_X_FALSE:
|
||||
n1 = va_arg(argptr, TNodeOrConst *);
|
||||
current->operand1 = n1;
|
||||
break;
|
||||
case E_LESSTHEN: case E_EQUALTO:
|
||||
n1 = va_arg(argptr, TNodeOrConst *);
|
||||
n2 = va_arg(argptr, TNodeOrConst *);
|
||||
current->operand1 = n1;
|
||||
current->operand2 = n2;
|
||||
break;
|
||||
}
|
||||
va_end(argptr);
|
||||
return;
|
||||
}
|
||||
|
||||
void emit_function_start(int name){
|
||||
emit_helper();
|
||||
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){
|
||||
emit_helper();
|
||||
current->opcode = E_CALL;
|
||||
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_release(char* pointer){
|
||||
emit_helper();
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
// * Implement temp variable generator function that produces unique names (t1, t2, etc.)
|
||||
char * temp_var_gen(){
|
||||
char * ret = calloc(9, sizeof(*ret));
|
||||
sprintf(ret, "$t%d", temp_count);
|
||||
temp_count++;
|
||||
return ret;
|
||||
}
|
||||
|
||||
char * label_gen(){
|
||||
char * ret = calloc( 9, sizeof(*ret));
|
||||
sprintf(ret, "L_%d", label_count);
|
||||
label_count++;
|
||||
return ret;
|
||||
}
|
@ -1,109 +0,0 @@
|
||||
// Track 1: Core Infrastructure & Basic Expressions
|
||||
// * Add Bison actions for arithmetic expressions:
|
||||
// - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3);
|
||||
// - Subtraction, multiplication, division, modulo
|
||||
|
||||
|
||||
// these are from page 364
|
||||
typedef enum {
|
||||
E_LABEL = 10000, // this is not in the book
|
||||
E_ADD, // 1 from the list
|
||||
E_SUB, // 1
|
||||
E_MUL, // 1
|
||||
E_DIV, // 1
|
||||
E_MOD, // 1
|
||||
E_OR, // 1
|
||||
E_AND, // 1
|
||||
E_NEG, // 2
|
||||
E_NOT, // 2
|
||||
E_ASSIGN, // 3
|
||||
E_GOTO, // 4
|
||||
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_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*/
|
||||
} Op;
|
||||
|
||||
typedef enum {
|
||||
NODE = 11000, // TableNode
|
||||
INTEGER, // int
|
||||
STRING, // char *
|
||||
CHARACTER, // char
|
||||
ADDRESS, // void *
|
||||
BOOLEAN // bool
|
||||
} Discriminant;
|
||||
|
||||
typedef union {
|
||||
TableNode * node;
|
||||
int integer;
|
||||
char * string;
|
||||
char character;
|
||||
void * address;
|
||||
bool Boolean;
|
||||
} TNConstUnion;
|
||||
|
||||
typedef struct {
|
||||
Discriminant d;
|
||||
TNConstUnion * tnc_union;
|
||||
} TNodeOrConst;
|
||||
|
||||
typedef struct Instruction Instruction;
|
||||
typedef struct Instruction {
|
||||
Op opcode;
|
||||
TableNode * result;
|
||||
TNodeOrConst * operand1;
|
||||
TNodeOrConst * operand2;
|
||||
int label;
|
||||
int index;
|
||||
|
||||
Instruction * prev;
|
||||
Instruction * next;
|
||||
} Instruction;
|
||||
|
||||
extern Instruction * begin;
|
||||
extern Instruction * current;
|
||||
int temp_count = 0;
|
||||
int label_count = 0;
|
||||
bool code_gen = true;
|
||||
|
||||
|
||||
|
||||
TNodeOrConst * tn_or_const(Op op, void * tnc);
|
||||
|
||||
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_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);
|
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