2022-04-10 15:55:56 +02:00
|
|
|
#include <vslc.h>
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
#define ERRPRT(format, args...) fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args)
|
|
|
|
|
2022-04-21 21:20:40 +02:00
|
|
|
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
|
|
|
|
#define LABEL(label) printf("_%s:\n", (char*)label)
|
|
|
|
#define COMMENT(format, args...) printf("# "format"\n", ##args)
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
#define PUSH(param) printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n", #param, ++stack_depth)
|
|
|
|
#define POP(param) printf("\tpopq\t%s\t\t\t\t# POP: %ld\n", #param, --stack_depth)
|
2022-04-10 16:03:03 +02:00
|
|
|
|
|
|
|
#define NO_REG_RECORD 6
|
2022-04-22 00:02:52 +02:00
|
|
|
//#define NO_CALLE_SAVED_REG 10
|
2022-04-10 16:03:03 +02:00
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
// Keep track of sequence of stack depth, ifs and whiles
|
|
|
|
static uint64_t
|
|
|
|
stack_depth,
|
|
|
|
if_seq,
|
2022-04-22 18:41:50 +02:00
|
|
|
while_seq,
|
|
|
|
closest_while;
|
2022-04-21 21:20:40 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Generate table of strings in a rodata section. */
|
2022-04-10 15:55:56 +02:00
|
|
|
void generate_stringtable ( void );
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Declare global variables in a bss section */
|
2022-04-10 15:55:56 +02:00
|
|
|
void generate_global_variables ( void );
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Generate function entry code
|
|
|
|
* @param function symbol table entry of function */
|
2022-04-10 15:55:56 +02:00
|
|
|
void generate_function ( symbol_t *function );
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Generate code for a node in the AST, to be called recursively from
|
|
|
|
* generate_function
|
|
|
|
* @param node root node of current code block */
|
2022-04-10 15:55:56 +02:00
|
|
|
static void generate_node ( node_t *node );
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Initializes program (already implemented) */
|
2022-04-10 15:55:56 +02:00
|
|
|
void generate_main ( symbol_t *first );
|
|
|
|
|
|
|
|
#define MIN(a,b) (((a)<(b)) ? (a):(b))
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
static const char *record[NO_REG_RECORD] = {
|
2022-04-10 15:55:56 +02:00
|
|
|
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
|
|
|
|
};
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
//static const char *calle_saved_reg[NO_CALLE_SAVED_REG] = {
|
|
|
|
// "%rax", "%rcx", "%rdx", "%rdi", "%rsi", "%rsp", "%r8", "%r9", "%r10", "%r11"
|
|
|
|
//};
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// Helper funcs for generating different nodes
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates assembly for printing
|
|
|
|
*
|
|
|
|
* @param node print statement node with children to print
|
|
|
|
*/
|
|
|
|
static void generate_print(node_t* node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate identfier for a variable in memory
|
|
|
|
*
|
|
|
|
* @param node identifier we want the addres of
|
|
|
|
*/
|
|
|
|
static void generate_var_ident(node_t *node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main function to calculate and solve the expressions.
|
|
|
|
* Based on a stack machine. Result is stored on stack.
|
|
|
|
*
|
|
|
|
* @param node root node for expression
|
|
|
|
*/
|
|
|
|
static void solve_expressions(node_t *node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a funciton call
|
|
|
|
*
|
|
|
|
* @param node root node for function
|
|
|
|
*/
|
|
|
|
static void generate_function_call(node_t *node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates the return part of a function
|
|
|
|
*
|
|
|
|
* @param node node containing the return statement
|
|
|
|
*/
|
|
|
|
static void generate_function_return(node_t *node);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used for calculating and evaluating the add/sub/mul/div statements.
|
|
|
|
* Turns the statement into an expression, adds the result of rhs to lhs
|
|
|
|
* and stores the value back to the indentifier
|
|
|
|
*
|
|
|
|
* @param node node to the statement
|
|
|
|
* @param operator one of the following +, -, *, /
|
|
|
|
*/
|
|
|
|
static void solve_statements(node_t *node, char *operator);
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
static void generate_if_statement(node_t *node);
|
|
|
|
|
|
|
|
static void generate_while_statement(node_t *node);
|
|
|
|
|
|
|
|
static void solve_continue_statement();
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
/**
|
|
|
|
* Generate assembly to fetch a variable on stack
|
|
|
|
*
|
|
|
|
* @param node node to the variable to be fetched
|
|
|
|
* @param dest where to put the value
|
|
|
|
*/
|
|
|
|
static void fetch_variable(node_t *node, const char* dest);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Same as fetch_variable, but stores it back to memory.
|
|
|
|
*
|
|
|
|
* @param node variable to be stored
|
|
|
|
* @param src from where should the data come from
|
|
|
|
*/
|
|
|
|
static void writeback_variable(node_t *node, char* src);
|
|
|
|
|
|
|
|
// Helper func for fetching all symbols in a table
|
|
|
|
static uint64_t fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
generate_program ( void )
|
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_stringtable();
|
|
|
|
generate_global_variables();
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
symbol_t **global_list;
|
|
|
|
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
bool main_generated = false;
|
|
|
|
uint64_t seq0_index = -1;
|
|
|
|
for (uint64_t g = 0; g < no_globals; g++)
|
|
|
|
{
|
|
|
|
if (global_list[g]->type != SYM_FUNCTION)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// If the name of the function is main
|
|
|
|
if (!strcmp(global_list[g]->name, "main"))
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_main(global_list[g]);
|
|
|
|
main_generated = true;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
if (!global_list[g]->seq)
|
|
|
|
seq0_index = g;
|
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// If no main was found, use the first function instead.
|
|
|
|
// That means the function with seq = 0
|
|
|
|
if (!main_generated)
|
|
|
|
generate_main(global_list[seq0_index]);
|
|
|
|
|
|
|
|
for (uint64_t g = 0; g < no_globals; g++)
|
|
|
|
{
|
|
|
|
if (global_list[g]->type == SYM_FUNCTION)
|
|
|
|
generate_function(global_list[g]);
|
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
free(global_list);
|
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
generate_stringtable ( void )
|
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
/* These can be used to emit numbers, strings and a run-time
|
|
|
|
* error msg. from main
|
|
|
|
*/
|
|
|
|
puts("# DATA SECTION");
|
2022-04-21 21:20:40 +02:00
|
|
|
puts(".section .data");
|
2022-04-10 16:03:03 +02:00
|
|
|
puts(".intout:\t.asciz \"\%ld \"");
|
|
|
|
puts(".strout:\t.asciz \"\%s \"");
|
|
|
|
puts(".errout:\t.asciz \"Wrong number of arguments\"");
|
|
|
|
|
|
|
|
for (uint64_t s = 0; s < stringc; s++)
|
|
|
|
{
|
|
|
|
printf(".STR%03ld:\t.asciz %s\n", s, string_list[s]);
|
|
|
|
}
|
|
|
|
putchar('\n');
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
generate_global_variables ( void )
|
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
symbol_t **global_list;
|
|
|
|
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
|
|
|
|
|
|
|
puts("# GLOBAL VARIABLES");
|
|
|
|
puts(".bss");
|
|
|
|
puts(".align 8");
|
|
|
|
|
|
|
|
for (uint64_t g = 0; g < no_globals; g++) {
|
|
|
|
if (global_list[g]->type == SYM_GLOBAL_VAR)
|
|
|
|
printf(".%s:\n", global_list[g]->name);
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
2022-04-10 16:03:03 +02:00
|
|
|
putchar('\n');
|
|
|
|
free(global_list);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
void
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_function ( symbol_t *function )
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
// TODO: Generate code for declaring and entering function, then generate its body
|
2022-04-22 00:02:52 +02:00
|
|
|
stack_depth = 0;
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
|
2022-04-21 21:20:40 +02:00
|
|
|
puts(".section .text");
|
2022-04-10 16:03:03 +02:00
|
|
|
printf(".global _%s\n", function->name);
|
|
|
|
LABEL(function->name);
|
2022-04-21 21:20:40 +02:00
|
|
|
PUSH(%rbp);
|
2022-04-22 00:02:52 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
ASM(movq, %rsp, %rbp);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// Push params to stack
|
|
|
|
for (int arg = 0; arg < MIN(NO_REG_RECORD,function->nparms); arg++)
|
2022-04-22 00:02:52 +02:00
|
|
|
{
|
|
|
|
printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n",
|
|
|
|
record[arg],
|
|
|
|
++stack_depth
|
|
|
|
);
|
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// How many local variables are inside function
|
|
|
|
uint64_t no_locals = function->locals->size - function->nparms;
|
2022-04-22 00:02:52 +02:00
|
|
|
//stack_depth += no_locals;
|
|
|
|
|
|
|
|
// Make room for the local vars
|
|
|
|
while(no_locals--)
|
|
|
|
PUSH($0);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// IF the stack alignment is not 16 bytes,
|
|
|
|
// add one now as all local var also is 0
|
2022-04-22 00:02:52 +02:00
|
|
|
if (stack_depth % 2)
|
2022-04-21 21:20:40 +02:00
|
|
|
PUSH($0);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// Now the stack ptr should be 16 byte aligned.
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_node(function->node);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
putchar('\n');
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
generate_node ( node_t *node)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
// TODO: Generate code corresponding to node
|
|
|
|
|
|
|
|
// All statements have the same structure.
|
|
|
|
// [0] is the lhs, needs to be identifier, parser ensures this
|
|
|
|
// [1] is thr rhs
|
|
|
|
switch (node->type)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
case ASSIGNMENT_STATEMENT:
|
2022-04-22 18:41:50 +02:00
|
|
|
// First solve the rhs
|
2022-04-10 16:03:03 +02:00
|
|
|
solve_expressions(node->children[1]);
|
2022-04-22 18:41:50 +02:00
|
|
|
// Then store in lhs
|
2022-04-10 16:03:03 +02:00
|
|
|
writeback_variable(node->children[0], "%rax");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ADD_STATEMENT:
|
|
|
|
// The following way is the naive way of doing an assignment
|
|
|
|
/*
|
|
|
|
fetch_variable(node->children[0], "%rax");
|
|
|
|
ASM(pushq, %rax);
|
|
|
|
solve_expressions(node->children[1]);
|
|
|
|
ASM(popq, %r10);
|
|
|
|
ASM(popq, %rax);
|
|
|
|
ASM(addq, %r10, %rax);
|
|
|
|
writeback_variable(node->children[0], "%rax");
|
|
|
|
*/
|
|
|
|
/* The thing is that add/sub/mul/div assignments
|
|
|
|
have the same structure as expressions.
|
|
|
|
We can therefore just say that the assignment is
|
|
|
|
an expression, but remembering to do the writeback afterwards.
|
|
|
|
*/
|
|
|
|
puts("# Add statement");
|
|
|
|
solve_statements(node, "+");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SUBTRACT_STATEMENT:
|
|
|
|
puts("# Subtract statement");
|
|
|
|
solve_statements(node, "-");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MULTIPLY_STATEMENT:
|
|
|
|
puts("# Multiply statement");
|
|
|
|
solve_statements(node, "*");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DIVIDE_STATEMENT:
|
|
|
|
puts("# Divide statement");
|
|
|
|
solve_statements(node, "/");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PRINT_STATEMENT:
|
|
|
|
puts("# Print statement");
|
|
|
|
generate_print(node);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RETURN_STATEMENT:
|
|
|
|
puts("# Return statement");
|
|
|
|
generate_function_return(node);
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case IF_STATEMENT:
|
2022-04-22 00:02:52 +02:00
|
|
|
generate_if_statement(node);
|
|
|
|
break;
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
case WHILE_STATEMENT:
|
2022-04-22 18:41:50 +02:00
|
|
|
generate_while_statement(node);
|
2022-04-10 16:03:03 +02:00
|
|
|
break;
|
|
|
|
case NULL_STATEMENT:
|
2022-04-22 18:41:50 +02:00
|
|
|
solve_continue_statement();
|
2022-04-10 16:03:03 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case DECLARATION_LIST:
|
|
|
|
/* List of blocks we dont need to traverse */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
for (int c = 0; c < node->n_children; c++)
|
|
|
|
generate_node(node->children[c]);
|
|
|
|
break;
|
|
|
|
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
generate_print(node_t* node)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
// Push rdi and rsi to stack incase there are data in them
|
|
|
|
for (uint64_t p = 0; p < node->n_children; p++)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
node_t *curr_print = node->children[p];
|
|
|
|
|
|
|
|
switch (curr_print->type)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
case EXPRESSION:
|
|
|
|
solve_expressions(curr_print);
|
|
|
|
ASM(movq, $.intout, %rdi);
|
|
|
|
ASM(movq, %rax, %rsi);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STRING_DATA:
|
|
|
|
ASM(movq, $.strout, %rdi);
|
|
|
|
printf("\tmovq\t$.STR%03ld, %%rsi\n", *(uint64_t*)curr_print->data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IDENTIFIER_DATA:
|
|
|
|
ASM(movq, $.intout, %rdi);
|
|
|
|
fetch_variable(curr_print, "%rsi");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
2022-04-22 00:02:52 +02:00
|
|
|
//ASM(movq, $0, %rax);
|
|
|
|
COMMENT("printf call");
|
2022-04-10 16:03:03 +02:00
|
|
|
ASM(call, printf);
|
2022-04-21 21:20:40 +02:00
|
|
|
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
2022-04-10 16:03:03 +02:00
|
|
|
// Adds a newline
|
|
|
|
ASM(movq, $'\n', %rdi);
|
2022-04-22 00:02:52 +02:00
|
|
|
//ASM(movq, $0, %rax);
|
2022-04-10 16:03:03 +02:00
|
|
|
ASM(call, putchar);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This will put the value of var in node in dest
|
|
|
|
void
|
|
|
|
fetch_variable(node_t *node, const char* dest)
|
|
|
|
{
|
|
|
|
printf("\tmovq\t");
|
|
|
|
generate_var_ident(node);
|
|
|
|
printf(", %s\t\t# Fetched: %s\n", dest, node->entry->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This will put the value in dest to the var in node
|
|
|
|
void
|
|
|
|
writeback_variable(node_t *node, char* src)
|
|
|
|
{
|
|
|
|
printf("\tmovq\t%s,", src);
|
|
|
|
generate_var_ident(node);
|
|
|
|
printf("\t\t# Writeback: %s\n", node->entry->name);
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
generate_var_ident(node_t *node)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
symbol_t *ident_sym = node->entry;
|
|
|
|
switch (ident_sym->type)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
case SYM_GLOBAL_VAR:
|
|
|
|
printf("$.%s", ident_sym->name);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SYM_PARAMETER:
|
|
|
|
// If it is a paramter is one of the first 6, seacrch below bp
|
|
|
|
if (ident_sym->seq < 6)
|
|
|
|
printf("%ld(%%rbp)", -8 * (ident_sym->seq + 1));
|
|
|
|
else
|
|
|
|
// This requires that the parameters on
|
|
|
|
// stack is in reversed order... easier to implement
|
|
|
|
printf("%ld(%%rbp)", 8 * (ident_sym->seq - 6 + 1 ));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SYM_LOCAL_VAR:
|
|
|
|
printf("%ld(%%rbp)", -8 * (ident_sym->seq + 1));
|
|
|
|
break;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
// This should allways push the result to stack
|
2022-04-22 00:02:52 +02:00
|
|
|
// no no no no, it should leave it in rax
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
solve_expressions(node_t *node)
|
|
|
|
{
|
|
|
|
if (node->data)
|
|
|
|
{ // Check if the expression is a function call
|
|
|
|
bool is_function_call = !strcmp(node->data, "function_call");
|
|
|
|
if (is_function_call)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_function_call(node);
|
|
|
|
return;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
switch (node->n_children)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
case 0:
|
|
|
|
switch (node->type)
|
|
|
|
{
|
|
|
|
case IDENTIFIER_DATA:
|
|
|
|
fetch_variable(node, "%rax");
|
2022-04-10 15:55:56 +02:00
|
|
|
break;
|
2022-04-10 16:03:03 +02:00
|
|
|
case NUMBER_DATA:
|
|
|
|
printf("\tmovq\t$%ld,%%rax\n",*(int64_t*)node->data);
|
2022-04-10 15:55:56 +02:00
|
|
|
break;
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
solve_expressions(node->children[0]);
|
|
|
|
|
|
|
|
switch (*(char*)node->data)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
ASM(negq, %rax);
|
2022-04-10 15:55:56 +02:00
|
|
|
break;
|
2022-04-10 16:03:03 +02:00
|
|
|
case '~':
|
|
|
|
ASM(notq, %rax);
|
2022-04-10 15:55:56 +02:00
|
|
|
break;
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
|
|
|
|
// First fetch lhs of expr and then rhs
|
|
|
|
// Push results on stack
|
|
|
|
for (int i = 0; i < 2; i++)
|
2022-04-22 00:02:52 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
solve_expressions(node->children[i]);
|
2022-04-22 00:02:52 +02:00
|
|
|
PUSH(%rax);
|
|
|
|
}
|
2022-04-10 16:03:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Put rhs in %r10
|
2022-04-21 21:20:40 +02:00
|
|
|
POP(%r10);
|
2022-04-10 16:03:03 +02:00
|
|
|
// put lhs in %rax
|
2022-04-21 21:20:40 +02:00
|
|
|
POP(%rax);
|
2022-04-10 16:03:03 +02:00
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
// All operators below leaves result in %rax
|
2022-04-10 16:03:03 +02:00
|
|
|
switch (*(char*)node->data)
|
|
|
|
{
|
|
|
|
/* Assignments */
|
|
|
|
case '|': ASM(orq, %r10, %rax); break; // Bitwise or of %rax and %r10
|
|
|
|
case '^': ASM(xorq, %r10, %rax); break; // Bitwise xor of %rax and %r10
|
|
|
|
case '&': ASM(andq, %r10, %rax); break; // Bitwise and of %rax and %r10
|
|
|
|
case '+': ASM(addq, %r10, %rax); break; // Add %rax and %r10
|
|
|
|
case '-': ASM(subq, %r10, %rax); break; // Subtract %r10 from %rax
|
|
|
|
case '*': ASM(imulq, %r10); break; // Mulitply %rax with %r10
|
|
|
|
case '/':
|
|
|
|
ASM(cqto); // Convert rax to octaword, %rdx:%rax
|
|
|
|
ASM(idivq, %r10); // Divide %rdx:%rax by %r10
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
generate_function_call(node_t *node)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
printf("# Function call\n");
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
bool isStack16ByteAligned = !(stack_depth % 2);
|
|
|
|
|
|
|
|
// If the stack is 16 byte alligned here, offset
|
|
|
|
// by 1 because call pushes return addr to stack
|
|
|
|
if (isStack16ByteAligned)
|
|
|
|
PUSH($0);
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
node_t *arg_list = node->children[1];
|
|
|
|
if (arg_list->n_children)
|
|
|
|
arg_list = arg_list->children[0];
|
|
|
|
|
|
|
|
for (int arg = 0; arg < MIN(NO_REG_RECORD, arg_list->n_children); arg++)
|
|
|
|
{
|
2022-04-22 18:41:50 +02:00
|
|
|
switch (arg_list->children[arg]->type)
|
|
|
|
{
|
|
|
|
case NUMBER_DATA:
|
2022-04-10 16:03:03 +02:00
|
|
|
printf("\tmovq\t$%ld, %s\n",
|
|
|
|
*(int64_t*)arg_list->children[arg]->data,
|
|
|
|
record[arg]
|
|
|
|
);
|
2022-04-22 18:41:50 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case EXPRESSION:
|
|
|
|
solve_expressions(arg_list->children[arg]);
|
|
|
|
printf("\tmovq\t%%rax, %s\n",record[arg]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-04-10 16:03:03 +02:00
|
|
|
fetch_variable(arg_list->children[arg], record[arg]);
|
2022-04-22 18:41:50 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (arg_list->n_children > NO_REG_RECORD)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-22 00:02:52 +02:00
|
|
|
// If there is an odd number of args to push to stack, add 1
|
|
|
|
if (arg_list->n_children % 2)
|
|
|
|
PUSH($0);
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-22 18:41:50 +02:00
|
|
|
switch (arg_list->children[arg]->type)
|
|
|
|
{
|
|
|
|
case NUMBER_DATA:
|
2022-04-22 00:02:52 +02:00
|
|
|
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
|
2022-04-21 21:20:40 +02:00
|
|
|
*(int64_t*)arg_list->children[arg]->data,
|
2022-04-22 00:02:52 +02:00
|
|
|
++stack_depth
|
2022-04-10 16:03:03 +02:00
|
|
|
);
|
2022-04-22 18:41:50 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
// Arg can be an expression
|
|
|
|
case EXPRESSION:
|
|
|
|
solve_expressions(arg_list->children[arg]);
|
|
|
|
PUSH(%rax);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2022-04-10 16:03:03 +02:00
|
|
|
printf("\tpushq\t");
|
|
|
|
generate_var_ident(arg_list->children[arg]);
|
2022-04-22 00:02:52 +02:00
|
|
|
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
|
2022-04-10 16:03:03 +02:00
|
|
|
putchar('\n');
|
2022-04-22 18:41:50 +02:00
|
|
|
break;
|
2022-04-10 16:03:03 +02:00
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
2022-04-22 18:41:50 +02:00
|
|
|
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
// Aaaand pop the stack to return back to stack alignment
|
|
|
|
if (isStack16ByteAligned)
|
|
|
|
POP(%rcx);
|
2022-04-22 18:41:50 +02:00
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
printf("# End of function call\n");
|
2022-04-10 15:55:56 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
generate_function_return(node_t *node)
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
solve_expressions(node->children[0]);
|
|
|
|
ASM(leave);
|
|
|
|
ASM(ret);
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
void
|
|
|
|
solve_statements(node_t *node, char *operator)
|
|
|
|
{
|
|
|
|
node->type = EXPRESSION;
|
|
|
|
node->data = strdup(operator);
|
|
|
|
|
|
|
|
solve_expressions(node);
|
|
|
|
|
|
|
|
writeback_variable(node->children[0], "%rax");
|
|
|
|
}
|
2022-04-10 15:55:56 +02:00
|
|
|
|
2022-04-22 18:41:50 +02:00
|
|
|
// Takes in a relation/number node and sets %rax to true if the statement is true
|
2022-04-22 00:02:52 +02:00
|
|
|
void
|
2022-04-22 18:41:50 +02:00
|
|
|
solve_relations(node_t *relation_root)
|
2022-04-22 00:02:52 +02:00
|
|
|
{
|
|
|
|
switch (relation_root->type)
|
|
|
|
{
|
|
|
|
case NUMBER_DATA:
|
|
|
|
solve_expressions(relation_root);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RELATION:
|
|
|
|
if (relation_root->n_children != 2)
|
|
|
|
{
|
|
|
|
ERRPRT("Relation requires two expressions, one lhs and one rhs\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < relation_root->n_children; i++)
|
|
|
|
{
|
|
|
|
solve_expressions(relation_root->children[i]);
|
|
|
|
PUSH(%rax);
|
|
|
|
}
|
|
|
|
|
|
|
|
POP(%r10);
|
|
|
|
POP(%rax);
|
|
|
|
|
|
|
|
ASM(cmp, %r10, %rax);
|
2022-04-22 18:41:50 +02:00
|
|
|
ASM(movq, $0, %rax);
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
switch (*(char*)relation_root->data)
|
|
|
|
{
|
|
|
|
case '=':
|
2022-04-22 18:41:50 +02:00
|
|
|
ASM(sete, %al);
|
2022-04-22 00:02:52 +02:00
|
|
|
break;
|
|
|
|
case '>':
|
2022-04-22 18:41:50 +02:00
|
|
|
ASM(setg, %al);
|
2022-04-22 00:02:52 +02:00
|
|
|
break;
|
|
|
|
case '<':
|
2022-04-22 18:41:50 +02:00
|
|
|
ASM(setl, %al);
|
2022-04-22 00:02:52 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2022-04-22 18:41:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
generate_if_statement(node_t *node)
|
|
|
|
{
|
|
|
|
uint64_t current_if_seq = if_seq++;
|
|
|
|
|
|
|
|
COMMENT("Begin IF %ld", current_if_seq);
|
|
|
|
|
|
|
|
// The realtion is allways in the first part of the IF
|
|
|
|
solve_relations(node->children[0]);
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
ASM(cmp, $1, %rax);
|
|
|
|
printf("\tjne\t%s%03ld\n", (node->n_children > 2) ? "ELSE" : "ENDIF", current_if_seq);
|
2022-04-22 18:41:50 +02:00
|
|
|
generate_node(node->children[1]);
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
if (node->n_children > 2) {
|
2022-04-22 18:41:50 +02:00
|
|
|
printf("\tjmp \tENDIF%03ld\n", current_if_seq);
|
2022-04-22 00:02:52 +02:00
|
|
|
printf("ELSE%03ld:\n", current_if_seq);
|
|
|
|
|
2022-04-22 18:41:50 +02:00
|
|
|
generate_node(node->children[2]);
|
|
|
|
}
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
COMMENT("End IF %ld", current_if_seq);
|
2022-04-22 18:41:50 +02:00
|
|
|
printf("ENDIF%03ld:\n", current_if_seq);
|
2022-04-22 00:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
generate_while_statement(node_t *node)
|
|
|
|
{
|
2022-04-22 18:41:50 +02:00
|
|
|
uint64_t current_while_seq = while_seq++;
|
2022-04-22 00:02:52 +02:00
|
|
|
|
2022-04-22 18:41:50 +02:00
|
|
|
uint64_t prev_closest_while = closest_while;
|
|
|
|
closest_while = current_while_seq;
|
|
|
|
|
|
|
|
COMMENT("Begin WHILE %ld", current_while_seq);
|
|
|
|
printf("WHILE%03ld:\n", current_while_seq);
|
|
|
|
|
|
|
|
// Relation is allways the first entry in a while
|
|
|
|
solve_relations(node->children[0]);
|
|
|
|
|
|
|
|
ASM(cmp, $1, %rax);
|
|
|
|
|
|
|
|
printf("\tjne\tENDWHILE%03ld\n", current_while_seq);
|
|
|
|
|
|
|
|
generate_node(node->children[1]);
|
|
|
|
closest_while = prev_closest_while;
|
|
|
|
|
|
|
|
printf("\tjmp \tWHILE%03ld\n", current_while_seq);
|
|
|
|
printf("ENDWHILE%03ld:\n", current_while_seq);
|
|
|
|
|
2022-04-22 00:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
solve_continue_statement()
|
|
|
|
{
|
2022-04-22 18:41:50 +02:00
|
|
|
COMMENT("Continue to WHILE%03ld", closest_while);
|
|
|
|
printf("\tjmp \tWHILE%03ld\n", closest_while);
|
2022-04-22 00:02:52 +02:00
|
|
|
}
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
/**Generates the main function with argument parsing and calling of our
|
|
|
|
* main function (first, if no function is named main)
|
|
|
|
* @param first Symbol table entry of our main function */
|
2022-04-10 15:55:56 +02:00
|
|
|
void
|
2022-04-10 16:03:03 +02:00
|
|
|
generate_main ( symbol_t *first )
|
2022-04-10 15:55:56 +02:00
|
|
|
{
|
2022-04-10 16:03:03 +02:00
|
|
|
puts("###### Entry point for GAS #####");
|
|
|
|
puts ( ".globl main" );
|
|
|
|
puts ( ".section .text" );
|
|
|
|
puts ( "main:" );
|
2022-04-22 00:02:52 +02:00
|
|
|
puts ( "\tpushq %rbp" ); // Added this for stack alignment
|
2022-04-10 15:55:56 +02:00
|
|
|
puts ( "\tpushq %rbp" );
|
|
|
|
puts ( "\tmovq %rsp, %rbp" );
|
|
|
|
|
2022-04-10 16:03:03 +02:00
|
|
|
printf ( "\tsubq\t$1,%%rdi\n" );
|
|
|
|
printf ( "\tcmpq\t$%zu,%%rdi\n", first->nparms );
|
|
|
|
printf ( "\tjne \tABORT\n" );
|
|
|
|
printf ( "\tcmpq\t$0,%%rdi\n" );
|
|
|
|
printf ( "\tjz \tSKIP_ARGS\n" );
|
|
|
|
|
|
|
|
printf ( "\tmovq\t%%rdi,%%rcx\n" );
|
|
|
|
printf ( "\taddq\t$%zu, %%rsi\n", 8*first->nparms );
|
2022-04-22 00:02:52 +02:00
|
|
|
|
|
|
|
// Modification to mail call, remove this if no params supplied
|
|
|
|
if (first->nparms)
|
|
|
|
{
|
|
|
|
printf ( "PARSE_ARGV:\n" );
|
|
|
|
printf ( "\tpushq\t%%rcx\n" );
|
|
|
|
printf ( "\tpushq\t%%rsi\n" );
|
|
|
|
|
|
|
|
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
|
|
|
|
printf ( "\tmovq\t$0,%%rsi\n" );
|
|
|
|
printf ( "\tmovq\t$10,%%rdx\n" );
|
|
|
|
printf ( "\tcall\tstrtol\n" );
|
|
|
|
|
|
|
|
/* Now a new argument is an integer in rax */
|
|
|
|
|
|
|
|
printf ( "\tpopq\t%%rsi\n" );
|
|
|
|
printf ( "\tpopq\t%%rcx\n" );
|
|
|
|
printf ( "\tpushq\t%%rax\n" );
|
|
|
|
printf ( "\tsubq\t$8, %%rsi\n" );
|
|
|
|
printf ( "\tloop\tPARSE_ARGV\n" );
|
|
|
|
|
|
|
|
/* Now the arguments are in order on stack */
|
|
|
|
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
|
|
|
|
printf ( "\tpopq\t%s\n", record[arg] );
|
|
|
|
|
|
|
|
}
|
2022-04-10 16:03:03 +02:00
|
|
|
|
|
|
|
printf ( "SKIP_ARGS:\n" );
|
|
|
|
printf ( "\tcall\t_%s\n", first->name );
|
|
|
|
printf ( "\tjmp \tEND\n" );
|
|
|
|
printf ( "ABORT:\n" );
|
|
|
|
printf ( "\tmovq\t$.errout, %%rdi\n" );
|
|
|
|
printf ( "\tcall\tputs\n" );
|
|
|
|
|
|
|
|
printf ( "END:\n" );
|
|
|
|
puts ( "\tmovq \t%rax, %rdi" );
|
|
|
|
puts ( "\tcall \texit" );
|
|
|
|
puts("###### FUNCTIONS FROM VSL BELOW #####");
|
|
|
|
putchar('\n');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t
|
|
|
|
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
|
|
|
|
{
|
|
|
|
uint64_t no_symbols = tlhash_size(symbol_table);
|
|
|
|
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
|
|
|
|
tlhash_values(symbol_table, (void **)*symbol_list );
|
|
|
|
|
|
|
|
return no_symbols;
|
2022-04-10 15:55:56 +02:00
|
|
|
}
|