added my code from ps5
parent
c9eacbfdfc
commit
ea8beae4c5
|
@ -10,13 +10,28 @@ typedef struct n {
|
||||||
struct n **children;
|
struct n **children;
|
||||||
} node_t;
|
} node_t;
|
||||||
|
|
||||||
// Export the initializer function, it is needed by the parser
|
/**Export the initializer function, it is needed by the parser
|
||||||
void node_init (
|
* @param *nd node to initialize
|
||||||
node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
|
* @param type type of node (see nodetype.h)
|
||||||
|
* @param *data associated data. Declared void to allow any type
|
||||||
|
* @param n_children number of children
|
||||||
|
* @param ... variable argument list of child nodes (node_t *)
|
||||||
|
*
|
||||||
|
* @return Pointer to the initialized node
|
||||||
|
* */
|
||||||
|
node_t* node_init (
|
||||||
|
node_t* nd,
|
||||||
|
node_index_t type,
|
||||||
|
void* data,
|
||||||
|
uint64_t n_children,
|
||||||
|
...
|
||||||
);
|
);
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
SYM_GLOBAL_VAR, SYM_FUNCTION, SYM_PARAMETER, SYM_LOCAL_VAR
|
SYM_GLOBAL_VAR,
|
||||||
|
SYM_FUNCTION,
|
||||||
|
SYM_PARAMETER,
|
||||||
|
SYM_LOCAL_VAR
|
||||||
} symtype_t;
|
} symtype_t;
|
||||||
|
|
||||||
typedef struct s {
|
typedef struct s {
|
||||||
|
@ -28,3 +43,9 @@ typedef struct s {
|
||||||
tlhash_t* locals;
|
tlhash_t* locals;
|
||||||
} symbol_t;
|
} symbol_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define GLOBAL_BUCKET_SIZE 32
|
||||||
|
#define LOCAL_BUCKET_SIZE 16
|
||||||
|
|
||||||
|
#define DEFAULT_STRING_LIST_SIZE 8
|
||||||
|
#define DEFAULT_NO_SCOPES 1
|
||||||
|
|
|
@ -25,6 +25,7 @@ int yyerror ( const char *error );
|
||||||
/* These are defined in the parser generated by bison */
|
/* These are defined in the parser generated by bison */
|
||||||
extern int yylineno;
|
extern int yylineno;
|
||||||
extern int yylex ( void );
|
extern int yylex ( void );
|
||||||
|
extern int yylex_destroy( void );
|
||||||
extern char yytext[];
|
extern char yytext[];
|
||||||
|
|
||||||
/* Global state */
|
/* Global state */
|
||||||
|
|
|
@ -1,84 +1,556 @@
|
||||||
#include <vslc.h>
|
#include <vslc.h>
|
||||||
|
|
||||||
void generate_stringtable ( void );
|
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
|
||||||
void generate_global_variables ( void );
|
#define LABEL(label) printf("_%s:\n", (char*)label)
|
||||||
void generate_function ( symbol_t *function );
|
|
||||||
|
|
||||||
|
#define NO_REG_RECORD 6
|
||||||
|
#define NO_CALLE_SAVED_REG 10
|
||||||
|
|
||||||
|
/**Generate table of strings in a rodata section. */
|
||||||
|
void generate_stringtable ( void );
|
||||||
|
/**Declare global variables in a bss section */
|
||||||
|
void generate_global_variables ( void );
|
||||||
|
/**Generate function entry code
|
||||||
|
* @param function symbol table entry of function */
|
||||||
|
void generate_function ( symbol_t *function );
|
||||||
|
/**Generate code for a node in the AST, to be called recursively from
|
||||||
|
* generate_function
|
||||||
|
* @param node root node of current code block */
|
||||||
static void generate_node ( node_t *node );
|
static void generate_node ( node_t *node );
|
||||||
|
/**Initializes program (already implemented) */
|
||||||
void generate_main ( symbol_t *first );
|
void generate_main ( symbol_t *first );
|
||||||
static void generate_function_call ( node_t *call );
|
|
||||||
|
|
||||||
#define MIN(a,b) (((a)<(b)) ? (a):(b))
|
#define MIN(a,b) (((a)<(b)) ? (a):(b))
|
||||||
|
|
||||||
static const char *record[6] = {
|
static const char *record[NO_REG_RECORD] = {
|
||||||
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
|
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
|
||||||
};
|
};
|
||||||
|
|
||||||
static symbol_t *current_function = NULL;
|
static const char *calle_saved_reg[NO_CALLE_SAVED_REG] = {
|
||||||
|
"%rax", "%rcx", "%rdx", "%rdi", "%rsi", "%rsp", "%r8", "%r9", "%r10", "%r11"
|
||||||
|
};
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
|
||||||
void
|
void
|
||||||
generate_program ( void )
|
generate_program ( void )
|
||||||
{
|
{
|
||||||
|
|
||||||
size_t n_globals = tlhash_size(global_names);
|
|
||||||
symbol_t *global_list[n_globals];
|
|
||||||
tlhash_values ( global_names, (void **)&global_list );
|
|
||||||
|
|
||||||
symbol_t *first_function;
|
|
||||||
for ( size_t i=0; i<tlhash_size(global_names); i++ )
|
|
||||||
if ( global_list[i]->type == SYM_FUNCTION )
|
|
||||||
{
|
|
||||||
// Allows the use of main as name to override entry point
|
|
||||||
if (!strcmp(global_list[i]->name, "main")) {
|
|
||||||
first_function = global_list[i];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (global_list[i]->seq == 0) {
|
|
||||||
first_function = global_list[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
generate_stringtable();
|
generate_stringtable();
|
||||||
generate_global_variables();
|
generate_global_variables();
|
||||||
generate_main ( first_function );
|
|
||||||
for ( size_t i=0; i<tlhash_size(global_names); i++ )
|
|
||||||
if ( global_list[i]->type == SYM_FUNCTION )
|
|
||||||
generate_function ( global_list[i] );
|
|
||||||
|
|
||||||
|
symbol_t **global_list;
|
||||||
|
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
||||||
|
|
||||||
|
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"))
|
||||||
|
{
|
||||||
|
generate_main(global_list[g]);
|
||||||
|
main_generated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!global_list[g]->seq)
|
||||||
|
seq0_index = g;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(global_list);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
generate_stringtable ( void )
|
generate_stringtable ( void )
|
||||||
{
|
{
|
||||||
puts ( ".section .rodata" );
|
/* These can be used to emit numbers, strings and a run-time
|
||||||
puts ( ".intout: .string \"\%ld \"" );
|
* error msg. from main
|
||||||
puts ( ".strout: .string \"\%s \"" );
|
*/
|
||||||
puts ( ".errout: .string \"Wrong number of arguments\"" );
|
puts("# DATA SECTION");
|
||||||
for ( size_t s=0; s<stringc; s++ )
|
puts(".data");
|
||||||
printf ( ".STR%zu: .string %s\n", s, string_list[s] );
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
generate_global_variables ( void )
|
generate_global_variables ( void )
|
||||||
{
|
{
|
||||||
puts ( ".section .data" );
|
symbol_t **global_list;
|
||||||
size_t nsyms = tlhash_size ( global_names );
|
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
||||||
symbol_t *syms[nsyms];
|
|
||||||
tlhash_values ( global_names, (void **)&syms );
|
puts("# GLOBAL VARIABLES");
|
||||||
for ( size_t n=0; n<nsyms; n++ )
|
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);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
free(global_list);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_function ( symbol_t *function )
|
||||||
{
|
{
|
||||||
if ( syms[n]->type == SYM_GLOBAL_VAR )
|
// TODO: Generate code for declaring and entering function, then generate its body
|
||||||
printf ( "._%s: .zero 8\n", syms[n]->name );
|
|
||||||
|
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
|
||||||
|
puts(".text");
|
||||||
|
printf(".global _%s\n", function->name);
|
||||||
|
LABEL(function->name);
|
||||||
|
ASM(pushq, %rbp);
|
||||||
|
ASM(movq, %rsp, %rbp);
|
||||||
|
|
||||||
|
// Push params to stack
|
||||||
|
for (int arg = 0; arg < MIN(NO_REG_RECORD,function->nparms); arg++)
|
||||||
|
printf("\tpushq\t%s\n", record[arg] );
|
||||||
|
|
||||||
|
// How many local variables are inside function
|
||||||
|
uint64_t no_locals = function->locals->size - function->nparms;
|
||||||
|
|
||||||
|
// IF the stack alignment is not 16 bytes,
|
||||||
|
// add one now as all local var also is 0
|
||||||
|
if ((MIN(6,function->nparms) + no_locals) % 2)
|
||||||
|
ASM(pushq, $0);
|
||||||
|
|
||||||
|
// Make room for the local vars
|
||||||
|
while(no_locals--)
|
||||||
|
ASM(pushq, $0);
|
||||||
|
|
||||||
|
// Now the stack ptr should be 16 byte aligned.
|
||||||
|
|
||||||
|
generate_node(function->node);
|
||||||
|
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_node ( node_t *node)
|
||||||
|
{
|
||||||
|
// 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)
|
||||||
|
{
|
||||||
|
case ASSIGNMENT_STATEMENT:
|
||||||
|
solve_expressions(node->children[1]);
|
||||||
|
ASM(popq, %rax);
|
||||||
|
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:
|
||||||
|
case WHILE_STATEMENT:
|
||||||
|
/* DO NOTHING YET */
|
||||||
|
break;
|
||||||
|
case NULL_STATEMENT:
|
||||||
|
/* USED IN WHILE/IF */
|
||||||
|
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;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_print(node_t* node)
|
||||||
|
{
|
||||||
|
// Push rdi and rsi to stack incase there are data in them
|
||||||
|
//ASM(pushq, %rdi);
|
||||||
|
//ASM(pushq, %rsi);
|
||||||
|
for (uint64_t p = 0; p < node->n_children; p++)
|
||||||
|
{
|
||||||
|
node_t *curr_print = node->children[p];
|
||||||
|
|
||||||
|
switch (curr_print->type)
|
||||||
|
{
|
||||||
|
case EXPRESSION:
|
||||||
|
solve_expressions(curr_print);
|
||||||
|
ASM(popq, %rax);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
ASM(call, printf);
|
||||||
|
}
|
||||||
|
// Adds a newline
|
||||||
|
ASM(movq, $'\n', %rdi);
|
||||||
|
ASM(call, putchar);
|
||||||
|
|
||||||
|
//ASM(popq, %rsi);
|
||||||
|
//ASM(popq, %rdi);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_var_ident(node_t *node)
|
||||||
|
{
|
||||||
|
symbol_t *ident_sym = node->entry;
|
||||||
|
switch (ident_sym->type)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// This should allways push the result to stack
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
generate_function_call(node);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
switch (node->n_children)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
switch (node->type)
|
||||||
|
{
|
||||||
|
case IDENTIFIER_DATA:
|
||||||
|
fetch_variable(node, "%rax");
|
||||||
|
ASM(pushq, %rax);
|
||||||
|
break;
|
||||||
|
case NUMBER_DATA:
|
||||||
|
printf("\tmovq\t$%ld,%%rax\n",*(int64_t*)node->data);
|
||||||
|
ASM(pushq, %rax);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
solve_expressions(node->children[0]);
|
||||||
|
ASM(popq, %rax);
|
||||||
|
|
||||||
|
switch (*(char*)node->data)
|
||||||
|
{
|
||||||
|
case '-':
|
||||||
|
ASM(negq, %rax);
|
||||||
|
break;
|
||||||
|
case '~':
|
||||||
|
ASM(notq, %rax);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ASM(pushq, %rax);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
|
||||||
|
// First fetch lhs of expr and then rhs
|
||||||
|
// Push results on stack
|
||||||
|
for (int i = 0; i < 2; i++)
|
||||||
|
solve_expressions(node->children[i]);
|
||||||
|
|
||||||
|
|
||||||
|
// Put rhs in %r10
|
||||||
|
ASM(popq, %r10);
|
||||||
|
// put lhs in %rax
|
||||||
|
ASM(popq, %rax);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Push result to stack.
|
||||||
|
ASM(pushq, %rax);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_function_call(node_t *node)
|
||||||
|
{
|
||||||
|
printf("# Function call\n");
|
||||||
|
|
||||||
|
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++)
|
||||||
|
{
|
||||||
|
if (arg_list->children[arg]->type == NUMBER_DATA)
|
||||||
|
printf("\tmovq\t$%ld, %s\n",
|
||||||
|
*(int64_t*)arg_list->children[arg]->data,
|
||||||
|
record[arg]
|
||||||
|
);
|
||||||
|
else
|
||||||
|
fetch_variable(arg_list->children[arg], record[arg]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arg_list->n_children > NO_REG_RECORD)
|
||||||
|
{
|
||||||
|
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
||||||
|
{
|
||||||
|
if (arg_list->children[arg]->type == NUMBER_DATA)
|
||||||
|
printf("\tpushq\t$%ld\n",
|
||||||
|
*(int64_t*)arg_list->children[arg]->data
|
||||||
|
);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("\tpushq\t");
|
||||||
|
generate_var_ident(arg_list->children[arg]);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (arg_list->n_children % 2)
|
||||||
|
ASM(pushq, $0);
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
||||||
|
ASM(pushq, %rax);
|
||||||
|
printf("# End of function call\n");
|
||||||
|
|
||||||
|
/*
|
||||||
|
for (int reg = 0; reg < NO_CALLE_SAVED_REG; reg++)
|
||||||
|
printf("\tpushq\t%s \t\t# Pushing %s to stack\n",
|
||||||
|
calle_saved_reg[reg],
|
||||||
|
calle_saved_reg[reg]
|
||||||
|
);
|
||||||
|
|
||||||
|
for (int reg = NO_CALLE_SAVED_REG; reg > 0; reg--)
|
||||||
|
printf("\tpopq\t%s \t\t# Poping %s from stack\n",
|
||||||
|
calle_saved_reg[reg],
|
||||||
|
calle_saved_reg[reg]
|
||||||
|
);*/
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
generate_function_return(node_t *node)
|
||||||
|
{
|
||||||
|
solve_expressions(node->children[0]);
|
||||||
|
ASM(popq, %rax);
|
||||||
|
ASM(leave);
|
||||||
|
ASM(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
solve_statements(node_t *node, char *operator)
|
||||||
|
{
|
||||||
|
node->type = EXPRESSION;
|
||||||
|
node->data = strdup(operator);
|
||||||
|
|
||||||
|
solve_expressions(node);
|
||||||
|
|
||||||
|
ASM(popq, %rax);
|
||||||
|
writeback_variable(node->children[0], "%rax");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**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 */
|
||||||
void
|
void
|
||||||
generate_main ( symbol_t *first )
|
generate_main ( symbol_t *first )
|
||||||
{
|
{
|
||||||
|
puts("###### Entry point for GAS #####");
|
||||||
puts ( ".globl main" );
|
puts ( ".globl main" );
|
||||||
puts ( ".section .text" );
|
puts ( ".section .text" );
|
||||||
puts ( "main:" );
|
puts ( "main:" );
|
||||||
|
@ -92,10 +564,10 @@ generate_main ( symbol_t *first )
|
||||||
printf ( "\tjz \tSKIP_ARGS\n" );
|
printf ( "\tjz \tSKIP_ARGS\n" );
|
||||||
|
|
||||||
printf ( "\tmovq\t%%rdi,%%rcx\n" );
|
printf ( "\tmovq\t%%rdi,%%rcx\n" );
|
||||||
printf ( "\taddq $%zu, %%rsi\n", 8*first->nparms );
|
printf ( "\taddq\t$%zu, %%rsi\n", 8*first->nparms );
|
||||||
printf ( "PARSE_ARGV:\n" );
|
printf ( "PARSE_ARGV:\n" );
|
||||||
printf ( "\tpushq %%rcx\n" );
|
printf ( "\tpushq\t%%rcx\n" );
|
||||||
printf ( "\tpushq %%rsi\n" );
|
printf ( "\tpushq\t%%rsi\n" );
|
||||||
|
|
||||||
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
|
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
|
||||||
printf ( "\tmovq\t$0,%%rsi\n" );
|
printf ( "\tmovq\t$0,%%rsi\n" );
|
||||||
|
@ -104,11 +576,11 @@ generate_main ( symbol_t *first )
|
||||||
|
|
||||||
/* Now a new argument is an integer in rax */
|
/* Now a new argument is an integer in rax */
|
||||||
|
|
||||||
printf ( "\tpopq %%rsi\n" );
|
printf ( "\tpopq\t%%rsi\n" );
|
||||||
printf ( "\tpopq %%rcx\n" );
|
printf ( "\tpopq\t%%rcx\n" );
|
||||||
printf ( "\tpushq %%rax\n" );
|
printf ( "\tpushq\t%%rax\n" );
|
||||||
printf ( "\tsubq $8, %%rsi\n" );
|
printf ( "\tsubq\t$8, %%rsi\n" );
|
||||||
printf ( "\tloop PARSE_ARGV\n" );
|
printf ( "\tloop\tPARSE_ARGV\n" );
|
||||||
|
|
||||||
/* Now the arguments are in order on stack */
|
/* Now the arguments are in order on stack */
|
||||||
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
|
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
|
||||||
|
@ -119,336 +591,22 @@ generate_main ( symbol_t *first )
|
||||||
printf ( "\tjmp \tEND\n" );
|
printf ( "\tjmp \tEND\n" );
|
||||||
printf ( "ABORT:\n" );
|
printf ( "ABORT:\n" );
|
||||||
printf ( "\tmovq\t$.errout, %%rdi\n" );
|
printf ( "\tmovq\t$.errout, %%rdi\n" );
|
||||||
printf ( "\tcall puts\n" );
|
printf ( "\tcall\tputs\n" );
|
||||||
|
|
||||||
printf ( "END:\n" );
|
printf ( "END:\n" );
|
||||||
puts ( "\tmovq %rax, %rdi" );
|
puts ( "\tmovq \t%rax, %rdi" );
|
||||||
puts ( "\tcall exit" );
|
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 );
|
||||||
|
|
||||||
static void
|
return no_symbols;
|
||||||
generate_identifier ( node_t *ident )
|
|
||||||
{
|
|
||||||
symbol_t *symbol = ident->entry;
|
|
||||||
int64_t argument_offset;
|
|
||||||
switch ( symbol->type )
|
|
||||||
{
|
|
||||||
case SYM_GLOBAL_VAR:
|
|
||||||
/* Global variables called by name */
|
|
||||||
printf ( "._%s", symbol->name );
|
|
||||||
break;
|
|
||||||
case SYM_PARAMETER:
|
|
||||||
if ( symbol->seq > 5 )
|
|
||||||
/* Extra parameters pushed in decreasing order */
|
|
||||||
printf ( "%ld(%%rbp)", 8+8*(symbol->seq-5) );
|
|
||||||
else
|
|
||||||
/* First six parameters directly after base poiter */
|
|
||||||
printf ( "%ld(%%rbp)", -8*(symbol->seq+1) );
|
|
||||||
break;
|
|
||||||
case SYM_LOCAL_VAR:
|
|
||||||
/* Local variables places after parameters in stack */
|
|
||||||
argument_offset = -8*MIN(6,current_function->nparms);
|
|
||||||
printf ( "%ld(%%rbp)", -8*(symbol->seq+1) + argument_offset );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_expression ( node_t *expr )
|
|
||||||
{
|
|
||||||
if ( expr->type == IDENTIFIER_DATA )
|
|
||||||
{
|
|
||||||
printf ( "\tmovq\t" );
|
|
||||||
generate_identifier ( expr );
|
|
||||||
printf ( ", %%rax\n" );
|
|
||||||
}
|
|
||||||
else if ( expr->type == NUMBER_DATA )
|
|
||||||
{
|
|
||||||
printf ( "\tmovq\t$%ld, %%rax\n", *(int64_t *)expr->data );
|
|
||||||
}
|
|
||||||
else if ( expr->n_children == 1 )
|
|
||||||
{
|
|
||||||
switch ( *((char*)(expr->data)) )
|
|
||||||
{
|
|
||||||
case '-':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tnegq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
case '~':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tnotq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ( expr->n_children == 2 )
|
|
||||||
{
|
|
||||||
if ( expr->data != NULL )
|
|
||||||
{
|
|
||||||
switch ( *((char *)expr->data) )
|
|
||||||
{
|
|
||||||
case '+':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\taddq\t%%rax, (%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
case '-':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\tsubq\t%%rax, (%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
case '*':
|
|
||||||
printf ( "\tpushq\t%%rdx\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tmulq\t(%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rdx\n" );
|
|
||||||
printf ( "\tpopq\t%%rdx\n" );
|
|
||||||
break;
|
|
||||||
case '/':
|
|
||||||
printf ( "\tpushq\t%%rdx\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tcqo\n" );
|
|
||||||
printf ( "\tidivq\t(%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rdx\n" );
|
|
||||||
printf ( "\tpopq\t%%rdx\n" );
|
|
||||||
break;
|
|
||||||
case '|':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\torq\t%%rax, (%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
case '^':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\txorq\t%%rax, (%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
case '&':
|
|
||||||
generate_expression ( expr->children[0] );
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
generate_expression ( expr->children[1] );
|
|
||||||
printf ( "\tandq\t%%rax, (%%rsp)\n" );
|
|
||||||
printf ( "\tpopq\t%%rax\n" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
generate_function_call ( expr );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_function_call ( node_t *call )
|
|
||||||
{
|
|
||||||
/* Check function call */
|
|
||||||
size_t n_arguments = 0;
|
|
||||||
if ( call->children[1] != NULL )
|
|
||||||
n_arguments = call->children[1]->n_children;
|
|
||||||
symbol_t *function = call->children[0]->entry;
|
|
||||||
if ( n_arguments != function->nparms )
|
|
||||||
{
|
|
||||||
fprintf ( stderr,
|
|
||||||
"Function %s has %zu parameters, called with %zu arguments\n",
|
|
||||||
(char *) call->children[0]->data,
|
|
||||||
(size_t) call->children[0]->entry->nparms,
|
|
||||||
n_arguments
|
|
||||||
);
|
|
||||||
exit ( EXIT_FAILURE );
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Generate function call: */
|
|
||||||
|
|
||||||
/* Push all the arguments */
|
|
||||||
node_t *arglist = call->children[1];
|
|
||||||
if ( arglist != NULL )
|
|
||||||
{
|
|
||||||
for ( size_t p=arglist->n_children; p>0; p-- )
|
|
||||||
{
|
|
||||||
generate_expression ( arglist->children[(p-1)] );
|
|
||||||
if ( (p-1)>5 )
|
|
||||||
printf ( "\tpushq\t%%rax\n" );
|
|
||||||
else
|
|
||||||
printf ( "\tmovq\t%%rax, %s\n", record[(p-1)] );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* Call the function */
|
|
||||||
printf ( "\tcall _%s\n", (char *)call->children[0]->data );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_assignment_statement ( node_t *statement )
|
|
||||||
{
|
|
||||||
switch ( statement->type )
|
|
||||||
{
|
|
||||||
case ASSIGNMENT_STATEMENT:
|
|
||||||
generate_expression ( statement->children[1] );
|
|
||||||
printf ( "\tmovq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
break;
|
|
||||||
case ADD_STATEMENT:
|
|
||||||
generate_expression ( statement->children[1] );
|
|
||||||
printf ( "\taddq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
break;
|
|
||||||
case SUBTRACT_STATEMENT:
|
|
||||||
generate_expression ( statement->children[1] );
|
|
||||||
printf ( "\tsubq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
break;
|
|
||||||
case MULTIPLY_STATEMENT:
|
|
||||||
generate_expression ( statement->children[1] );
|
|
||||||
printf ( "\tmulq\t " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
printf ( "\tmovq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
break;
|
|
||||||
case DIVIDE_STATEMENT:
|
|
||||||
generate_expression ( statement->children[1] );
|
|
||||||
printf ( "\txchgq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
printf ( "\tcqo\n" );
|
|
||||||
printf ( "\tidivq\t" );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
printf ( "\txchgq\t%%rax, " );
|
|
||||||
generate_identifier ( statement->children[0] );
|
|
||||||
printf ( "\n" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_print_statement ( node_t *statement )
|
|
||||||
{
|
|
||||||
for ( size_t i=0; i<statement->n_children; i++ )
|
|
||||||
{
|
|
||||||
node_t *item = statement->children[i];
|
|
||||||
switch ( item->type )
|
|
||||||
{
|
|
||||||
case STRING_DATA:
|
|
||||||
printf ( "\tmovq\t$.STR%zu, %%rsi\n", *((size_t *)item->data) );
|
|
||||||
printf ( "\tmovq\t$.strout, %%rdi\n" );
|
|
||||||
break;
|
|
||||||
case NUMBER_DATA:
|
|
||||||
printf ("\tmovq\t$%ld, %%rsi\n", *((int64_t *)item->data) );
|
|
||||||
printf ( "\tmovq\t$.intout, %%rdi\n" );
|
|
||||||
break;
|
|
||||||
case IDENTIFIER_DATA:
|
|
||||||
printf ( "\tmovq\t" );
|
|
||||||
generate_identifier ( item );
|
|
||||||
printf ( ", %%rsi\n" );
|
|
||||||
printf ( "\tmovq\t$.intout, %%rdi\n" );
|
|
||||||
break;
|
|
||||||
case EXPRESSION:
|
|
||||||
generate_expression ( item );
|
|
||||||
printf ( "\tmovq\t%%rax, %%rsi\n" );
|
|
||||||
printf ( "\tmovq\t$.intout, %%rdi\n" );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
puts ( "\tmovq\t$0, %rax\n" // Clear rax to indicate not to use SSE instructions
|
|
||||||
"\tcall\tprintf" );
|
|
||||||
}
|
|
||||||
printf ( "\tmovq\t$0x0A, %%rdi\n" ); // Finish statement by inserting a newline
|
|
||||||
puts ( "\tcall\tputchar" );
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_if_statement ( node_t *statement )
|
|
||||||
{
|
|
||||||
// TODO: Handle if statement
|
|
||||||
// statement->nodetype = IF_STATEMENT
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_while_statement ( node_t *statement )
|
|
||||||
{
|
|
||||||
// TODO: Handle while statement
|
|
||||||
// statement->nodetype = WHILE_STATEMENT
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
generate_node ( node_t *node )
|
|
||||||
{
|
|
||||||
switch (node->type)
|
|
||||||
{
|
|
||||||
case PRINT_STATEMENT:
|
|
||||||
generate_print_statement ( node );
|
|
||||||
break;
|
|
||||||
case ASSIGNMENT_STATEMENT:
|
|
||||||
case ADD_STATEMENT:
|
|
||||||
case SUBTRACT_STATEMENT:
|
|
||||||
case MULTIPLY_STATEMENT:
|
|
||||||
case DIVIDE_STATEMENT:
|
|
||||||
generate_assignment_statement ( node );
|
|
||||||
break;
|
|
||||||
case RETURN_STATEMENT:
|
|
||||||
generate_expression ( node->children[0] );
|
|
||||||
printf ( "\tleave\n" );
|
|
||||||
printf ( "\tret\n" );
|
|
||||||
break;
|
|
||||||
case IF_STATEMENT:
|
|
||||||
// TODO: Implement
|
|
||||||
break;
|
|
||||||
case WHILE_STATEMENT:
|
|
||||||
// TODO: Implement
|
|
||||||
break;
|
|
||||||
case NULL_STATEMENT:
|
|
||||||
// TODO: Implement
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
for ( size_t i=0; i<node->n_children; i++ )
|
|
||||||
generate_node ( node->children[i] );
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void
|
|
||||||
generate_function ( symbol_t *function )
|
|
||||||
{
|
|
||||||
current_function = function;
|
|
||||||
printf ( "_%s:\n", function->name );
|
|
||||||
puts ( "\tpushq %rbp" );
|
|
||||||
puts ( "\tmovq %rsp, %rbp" );
|
|
||||||
|
|
||||||
/* Save arguments in local stack frame */
|
|
||||||
for ( size_t arg=1; arg<=MIN(6,function->nparms); arg++ )
|
|
||||||
printf ( "\tpushq\t%s\n", record[arg-1] );
|
|
||||||
/* Make space for locals in local stack frame */
|
|
||||||
size_t local_vars = tlhash_size(function->locals) - function->nparms;
|
|
||||||
if ( local_vars > 0 )
|
|
||||||
printf ( "\tsubq $%zu, %%rsp\n", 8*local_vars );
|
|
||||||
if ( (tlhash_size(function->locals)&1) == 1 )
|
|
||||||
puts ( "\tpushq\t$0 /* Stack padding for 16-byte alignment */" );
|
|
||||||
generate_node ( function->node );
|
|
||||||
printf( "\tmovq\t%%rbp, %%rsp\n" // movq %rbp, %rsp // restore stack pointer
|
|
||||||
"\tmovq\t$0, %%rax\n" // movq $0, %rax // return 0 if nothing else
|
|
||||||
"\tpopq\t%%rbp\n" // popq %rbp // restore base pointer
|
|
||||||
"\tret\n"); // ret
|
|
||||||
current_function = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,336 +1,602 @@
|
||||||
#include <vslc.h>
|
#include <vslc.h>
|
||||||
|
|
||||||
|
#define ERRPRT(format, args...) {fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args);}
|
||||||
|
|
||||||
|
|
||||||
// Externally visible, for the generator
|
// Externally visible, for the generator
|
||||||
extern tlhash_t *global_names;
|
extern tlhash_t *global_names;
|
||||||
extern char **string_list;
|
extern char **string_list;
|
||||||
extern size_t n_string_list, stringc;
|
extern size_t n_string_list, stringc;
|
||||||
|
|
||||||
|
// Functions from the skeleton
|
||||||
|
|
||||||
// Implementation choices, only relevant internally
|
static uint64_t find_globals ( void );
|
||||||
static void find_globals ( void );
|
|
||||||
static void bind_names ( symbol_t *function, node_t *root );
|
static void bind_names ( symbol_t *function, node_t *root );
|
||||||
static void print_symbols ( tlhash_t *table );
|
|
||||||
static void destroy_symtab ( void );
|
|
||||||
|
|
||||||
// Internal details of name resolution
|
// Helper functions, see description in the definition
|
||||||
static size_t n_scopes = 1, scope_depth = 0;
|
|
||||||
static tlhash_t **scopes = NULL;
|
|
||||||
|
|
||||||
/* External interface */
|
static void print_global_tree(symbol_t* global);
|
||||||
|
static void print_string_list(void);
|
||||||
|
static void destroy_global(symbol_t* global);
|
||||||
|
static void push_scope(void);
|
||||||
|
static void pop_scope(void);
|
||||||
|
static void insert_symbol(tlhash_t *hash_table, symbol_t* symbol);
|
||||||
|
static void insert_local_to_scope(symbol_t *local);
|
||||||
|
static void insert_local_to_func(symbol_t *function, symbol_t *root);
|
||||||
|
static void insert_local_var(symbol_t *function, node_t *root);
|
||||||
|
static void collect_string(node_t *root);
|
||||||
|
static symbol_t* lookup_var(symbol_t *function, char* var);
|
||||||
|
|
||||||
|
// Local "global" variables
|
||||||
|
static const char *symbol_names[4] = {
|
||||||
|
"GLOBAL_VAR",
|
||||||
|
"FUNCTION",
|
||||||
|
"PARAMETER",
|
||||||
|
"LOCAL_VAR"
|
||||||
|
};
|
||||||
|
static uint64_t no_scopes, cur_scope_depth;
|
||||||
|
static tlhash_t **scopes;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gather information and create a symbol table.
|
||||||
|
*
|
||||||
|
* Used in vslc.c
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
create_symbol_table ( void )
|
create_symbol_table ( void )
|
||||||
{
|
{
|
||||||
find_globals();
|
|
||||||
size_t n_globals = tlhash_size ( global_names );
|
// Initialize string array
|
||||||
symbol_t *global_list[n_globals];
|
n_string_list = DEFAULT_STRING_LIST_SIZE;
|
||||||
tlhash_values ( global_names, (void **)&global_list );
|
string_list = malloc(n_string_list * sizeof(char*));
|
||||||
for ( size_t i=0; i<n_globals; i++ )
|
stringc = 0;
|
||||||
if ( global_list[i]->type == SYM_FUNCTION )
|
|
||||||
bind_names ( global_list[i], global_list[i]->node );
|
// Initialize scope array
|
||||||
|
no_scopes = DEFAULT_NO_SCOPES;
|
||||||
|
scopes = malloc(no_scopes * sizeof(tlhash_t));
|
||||||
|
cur_scope_depth = 0;
|
||||||
|
|
||||||
|
// Traverse the root node for globals
|
||||||
|
uint64_t no_globals = find_globals();
|
||||||
|
|
||||||
|
// Prepare a temp list of globals and fetch all globals
|
||||||
|
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
|
||||||
|
tlhash_values(global_names, (void **)global_list );
|
||||||
|
|
||||||
|
/* Iterate over the temporary list, bind names in each function */
|
||||||
|
for (uint64_t g = 0; g < no_globals; g++ )
|
||||||
|
{
|
||||||
|
if (global_list[g]->type == SYM_FUNCTION)
|
||||||
|
bind_names(global_list[g], global_list[g]->node);
|
||||||
|
}
|
||||||
|
// Free the temp list
|
||||||
|
free(global_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the symbol table and the string array
|
||||||
|
*
|
||||||
|
* Used in vslc.c
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
print_symbol_table ( void )
|
print_symbol_table ( void )
|
||||||
{
|
{
|
||||||
print_symbols ( global_names );
|
/* Get the number of symbols, size up a temporary list and fill it */
|
||||||
|
uint64_t no_globals = tlhash_size(global_names);
|
||||||
|
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
|
||||||
|
tlhash_values(global_names, (void **)global_list );
|
||||||
|
|
||||||
|
/* Iterate over the temporary list, printing entries */
|
||||||
|
for (uint64_t g = 0; g < no_globals; g++ )
|
||||||
|
// Print the tree structure for each global
|
||||||
|
print_global_tree(global_list[g]);
|
||||||
|
|
||||||
|
free(global_list);
|
||||||
|
|
||||||
|
// Print strings
|
||||||
|
print_string_list();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prins the tree of a global
|
||||||
|
*
|
||||||
|
* @param global pointer to the global to be printed
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
print_global_tree(symbol_t* global)
|
||||||
|
{
|
||||||
|
// Check if null ptr
|
||||||
|
if (!global)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Print global root
|
||||||
|
printf("─%s: %-16s [nparams=%2ld, seq=%2ld, node=%p]\n",
|
||||||
|
symbol_names[global->type],
|
||||||
|
global->name,
|
||||||
|
global->nparms,
|
||||||
|
global->seq,
|
||||||
|
global->node
|
||||||
|
);
|
||||||
|
|
||||||
|
// If the global does not have params or locals, return
|
||||||
|
if (!global->nparms && !global->locals)
|
||||||
|
{putchar('\n');return;}
|
||||||
|
|
||||||
|
// Need to fetch the whole size, since nparams
|
||||||
|
// only count the params, not all locals
|
||||||
|
uint64_t no_locals = tlhash_size(global->locals);
|
||||||
|
symbol_t **locals_list = malloc(no_locals * sizeof(symbol_t));
|
||||||
|
tlhash_values(global->locals, (void **)locals_list );
|
||||||
|
// Go through all locals
|
||||||
|
for (int l = 0; l < no_locals; l++)
|
||||||
|
{ // Do some simple sorting, so seq num is in order
|
||||||
|
for (int ll = 0; ll < no_locals; ll++)
|
||||||
|
{
|
||||||
|
if (locals_list[ll]->seq == l)
|
||||||
|
{
|
||||||
|
printf(" %s─[%s]: %-22s\t[seq=%2ld, node=%p]\n",
|
||||||
|
(l < (no_locals - 1)) ? "├" : "└",
|
||||||
|
symbol_names[locals_list[ll]->type],
|
||||||
|
locals_list[ll]->name,
|
||||||
|
locals_list[ll]->seq,
|
||||||
|
locals_list[ll]->node
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
free(locals_list);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Prints the array of strings
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
print_string_list(void)
|
||||||
|
{ // Print out all the collected strings
|
||||||
|
printf("─STRINGS [%ld]\n", stringc);
|
||||||
|
for (uint64_t i = 0; i < stringc; i++)
|
||||||
|
printf(" %s─[%ld]: %s\n",
|
||||||
|
(i < (stringc - 1)) ? "├" : "└",
|
||||||
|
i,
|
||||||
|
string_list[i]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys all the dynamicly allocated memory and all the hash tables.
|
||||||
|
* Frees up the array of strings as well.
|
||||||
|
*
|
||||||
|
* Used in vslc.c
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
destroy_symbol_table ( void )
|
destroy_symbol_table ( void )
|
||||||
{
|
{
|
||||||
destroy_symtab();
|
// FREE STRINGS
|
||||||
}
|
// Free all strings that are kept in the array
|
||||||
|
for (uint64_t c = 0; c < stringc; c++)
|
||||||
|
free(string_list[c]);
|
||||||
|
// Free the actual list
|
||||||
|
free(string_list);
|
||||||
|
|
||||||
/* Internal matters */
|
// FREE SCOPES
|
||||||
|
// At the end of program, all scopes have to be popped
|
||||||
|
// Therefore only free the list
|
||||||
|
free(scopes);
|
||||||
|
|
||||||
|
// FREE GLOBAL NAMES
|
||||||
static void
|
if (!global_names)
|
||||||
print_symbols ( tlhash_t *table )
|
|
||||||
{
|
|
||||||
if ( table == NULL )
|
|
||||||
return;
|
return;
|
||||||
size_t n_entries = tlhash_size(table);
|
|
||||||
symbol_t *entry_list[n_entries];
|
// Fetch list of globals
|
||||||
tlhash_values ( table, (void **)&entry_list );
|
uint64_t no_globals = tlhash_size(global_names);
|
||||||
for ( size_t e=0; e<n_entries; e++ )
|
symbol_t **global_list = malloc(no_globals * sizeof(symbol_t));
|
||||||
{
|
tlhash_values(global_names, (void **)global_list );
|
||||||
switch ( entry_list[e]->type )
|
|
||||||
{
|
// Destroy all global elements
|
||||||
case SYM_FUNCTION:
|
for (uint64_t g = 0; g < no_globals; g++)
|
||||||
fprintf ( stderr, "function: %s\n", entry_list[e]->name );
|
destroy_global(global_list[g]);
|
||||||
if ( entry_list[e]->type == SYM_FUNCTION )
|
|
||||||
print_symbols ( entry_list[e]->locals );
|
// Destory the global hash table
|
||||||
break;
|
tlhash_finalize(global_names);
|
||||||
case SYM_GLOBAL_VAR:
|
// Free the global hash table
|
||||||
fprintf ( stderr, "global var: %s\n", entry_list[e]->name );
|
free(global_names);
|
||||||
break;
|
|
||||||
case SYM_PARAMETER:
|
// Free the temp list
|
||||||
fprintf ( stderr, "parameter: %s\n", entry_list[e]->name );
|
free(global_list);
|
||||||
break;
|
|
||||||
case SYM_LOCAL_VAR:
|
|
||||||
fprintf ( stderr, "local var: %s\n", entry_list[e]->name );
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
/* This should never happen if all symbols have correct type */
|
|
||||||
fprintf ( stderr,
|
|
||||||
"** Unknown symbol: %s\n", entry_list[e]->name
|
|
||||||
);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destroys the supplied global symbol by
|
||||||
|
* finalizing each of the local tables
|
||||||
|
*
|
||||||
|
* @param global pointer to the global symbol to be destroyed
|
||||||
|
*/
|
||||||
static void
|
static void
|
||||||
add_global ( symbol_t *symbol )
|
destroy_global(symbol_t* global)
|
||||||
{
|
{
|
||||||
tlhash_insert (
|
if (!global)
|
||||||
global_names, symbol->name, strlen(symbol->name), symbol
|
return;
|
||||||
);
|
if (!global->locals)
|
||||||
|
{
|
||||||
|
free(global);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint64_t no_locals = tlhash_size(global->locals);
|
||||||
|
symbol_t **locals_list = malloc(no_locals * sizeof(symbol_t));
|
||||||
|
tlhash_values(global->locals, (void **)locals_list );
|
||||||
|
|
||||||
|
for (int l = 0; l < no_locals; l++)
|
||||||
|
free(locals_list[l]);
|
||||||
|
|
||||||
|
tlhash_finalize(global->locals);
|
||||||
|
free(global->locals);
|
||||||
|
free(global);
|
||||||
|
|
||||||
|
free(locals_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
/**
|
||||||
|
* Goes trough the root node and finds all global variables and functions
|
||||||
|
*
|
||||||
|
* @return Returns the number of globals found (functions + variables)
|
||||||
|
*/
|
||||||
|
static uint64_t
|
||||||
find_globals ( void )
|
find_globals ( void )
|
||||||
{
|
{
|
||||||
global_names = malloc ( sizeof(tlhash_t) );
|
tlhash_init(global_names = malloc(sizeof(tlhash_t)), GLOBAL_BUCKET_SIZE);
|
||||||
tlhash_init ( global_names, 32 );
|
|
||||||
string_list = malloc ( n_string_list * sizeof(char * ) );
|
|
||||||
size_t n_functions = 0;
|
|
||||||
|
|
||||||
node_t *global_list = root->children[0];
|
uint64_t no_functions = 0, no_global_vars = 0;
|
||||||
for ( uint64_t g=0; g<global_list->n_children; g++ )
|
node_t *global_list = root;
|
||||||
{
|
|
||||||
node_t *global = global_list->children[g], *namelist;
|
|
||||||
symbol_t *symbol;
|
|
||||||
switch ( global->type )
|
|
||||||
{
|
|
||||||
case FUNCTION:
|
|
||||||
symbol = malloc ( sizeof(symbol_t) );
|
|
||||||
*symbol = (symbol_t) {
|
|
||||||
.type = SYM_FUNCTION,
|
|
||||||
.name = global->children[0]->data,
|
|
||||||
.node = global->children[2],
|
|
||||||
.seq = n_functions,
|
|
||||||
.nparms = 0,
|
|
||||||
.locals = malloc ( sizeof(tlhash_t) )
|
|
||||||
};
|
|
||||||
n_functions++;
|
|
||||||
|
|
||||||
tlhash_init ( symbol->locals, 32 );
|
// Check if not nullptr
|
||||||
if ( global->children[1] != NULL )
|
if (!global_list)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
symbol_t* global_symbol;
|
||||||
|
for (uint64_t global_i = 0; global_i < global_list->n_children; global_i++)
|
||||||
{
|
{
|
||||||
symbol->nparms = global->children[1]->n_children;
|
node_t *current_global = global_list->children[global_i];
|
||||||
for ( int p=0; p<symbol->nparms; p++ )
|
switch (current_global->type)
|
||||||
{
|
{
|
||||||
node_t *param = global->children[1]->children[p];
|
case VARIABLE_LIST:
|
||||||
symbol_t *psym = malloc ( sizeof(symbol_t) );
|
|
||||||
*psym = (symbol_t) {
|
// Go through the variable list and get all the global variables
|
||||||
.type = SYM_PARAMETER,
|
for (uint64_t var_i = 0; var_i < current_global->n_children; var_i++)
|
||||||
.name = param->data,
|
|
||||||
.node = NULL,
|
|
||||||
.seq = p,
|
|
||||||
.nparms = 0,
|
|
||||||
.locals = NULL
|
|
||||||
};
|
|
||||||
tlhash_insert (
|
|
||||||
symbol->locals, psym->name, strlen(psym->name), psym
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
add_global ( symbol );
|
|
||||||
break;
|
|
||||||
case DECLARATION:
|
|
||||||
namelist = global->children[0];
|
|
||||||
for ( uint64_t d=0; d<namelist->n_children; d++ )
|
|
||||||
{
|
{
|
||||||
symbol = malloc ( sizeof(symbol_t) );
|
global_symbol = malloc(sizeof(symbol_t));
|
||||||
*symbol = (symbol_t) {
|
*global_symbol = (symbol_t){
|
||||||
.type = SYM_GLOBAL_VAR,
|
.type = SYM_GLOBAL_VAR,
|
||||||
.name = namelist->children[d]->data,
|
.name = current_global->children[var_i]->data,
|
||||||
.node = NULL,
|
.node = current_global->children[var_i],
|
||||||
.seq = 0,
|
.seq = 0,
|
||||||
.nparms = 0,
|
.nparms = 0,
|
||||||
.locals = NULL
|
.locals = NULL
|
||||||
};
|
};
|
||||||
add_global(symbol);
|
insert_symbol(global_names, global_symbol);
|
||||||
|
no_global_vars++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
case FUNCTION:
|
||||||
}
|
node_t *function = current_global;
|
||||||
}
|
|
||||||
|
|
||||||
|
// Function node allways have the same structure,
|
||||||
static void
|
// [0] are the identifier
|
||||||
push_scope ( void )
|
// [1] are the variable list, within a paramerer_list
|
||||||
{
|
// [2] are the actual block
|
||||||
if ( scopes == NULL )
|
if (!function->children[0])
|
||||||
scopes = malloc ( n_scopes * sizeof(tlhash_t *) );
|
|
||||||
tlhash_t *new_scope = malloc ( sizeof(tlhash_t) );
|
|
||||||
tlhash_init ( new_scope, 32 );
|
|
||||||
scopes[scope_depth] = new_scope;
|
|
||||||
|
|
||||||
scope_depth += 1;
|
|
||||||
if ( scope_depth >= n_scopes )
|
|
||||||
{
|
|
||||||
n_scopes *= 2;
|
|
||||||
scopes = realloc ( scopes, n_scopes*sizeof(tlhash_t **) );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
add_local ( symbol_t *local )
|
|
||||||
{
|
|
||||||
tlhash_insert (
|
|
||||||
scopes[scope_depth-1],local->name,strlen(local->name),local
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static symbol_t *
|
|
||||||
lookup_local ( char *name )
|
|
||||||
{
|
|
||||||
symbol_t *result = NULL;
|
|
||||||
size_t depth = scope_depth;
|
|
||||||
while ( result == NULL && depth > 0 )
|
|
||||||
{
|
|
||||||
depth -= 1;
|
|
||||||
tlhash_lookup ( scopes[depth], name, strlen(name), (void **)&result );
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
pop_scope ( void )
|
|
||||||
{
|
|
||||||
scope_depth -= 1;
|
|
||||||
tlhash_finalize ( scopes[scope_depth] );
|
|
||||||
free ( scopes[scope_depth] );
|
|
||||||
scopes[scope_depth] = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
add_string ( node_t *string )
|
|
||||||
{
|
|
||||||
string_list[stringc] = string->data;
|
|
||||||
string->data = malloc ( sizeof(size_t) );
|
|
||||||
*((size_t *)string->data) = stringc;
|
|
||||||
stringc++;
|
|
||||||
if ( stringc >= n_string_list )
|
|
||||||
{
|
|
||||||
n_string_list *= 2;
|
|
||||||
string_list = realloc ( string_list, n_string_list * sizeof(char *) );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
bind_names ( symbol_t *function, node_t *root )
|
|
||||||
{
|
|
||||||
if ( root == NULL )
|
|
||||||
return;
|
|
||||||
else switch ( root->type )
|
|
||||||
{
|
|
||||||
node_t *namelist;
|
|
||||||
symbol_t *entry;
|
|
||||||
|
|
||||||
case BLOCK:
|
|
||||||
push_scope();
|
|
||||||
for ( size_t c=0; c<root->n_children; c++ )
|
|
||||||
bind_names ( function, root->children[c] );
|
|
||||||
pop_scope();
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DECLARATION:
|
// Create the function symbol
|
||||||
namelist = root->children[0];
|
global_symbol = malloc(sizeof(symbol_t));
|
||||||
for ( uint64_t d=0; d<namelist->n_children; d++ )
|
*global_symbol = (symbol_t){
|
||||||
|
.type = SYM_FUNCTION,
|
||||||
|
.name = current_global->children[0]->data,
|
||||||
|
.node = current_global->children[2],
|
||||||
|
.seq = no_functions++,
|
||||||
|
.nparms = 0,
|
||||||
|
.locals = malloc(sizeof(tlhash_t))
|
||||||
|
};
|
||||||
|
|
||||||
|
// Initialize the local variable table
|
||||||
|
tlhash_init(global_symbol->locals, LOCAL_BUCKET_SIZE);
|
||||||
|
|
||||||
|
// Insert the pointer to the newly created symbol
|
||||||
|
insert_symbol(global_names, global_symbol);
|
||||||
|
|
||||||
|
// If there are no parameters in function, break.
|
||||||
|
if (!current_global->children[1]->n_children)
|
||||||
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
// Find all params and insert into hash table in global_symbol
|
||||||
|
symbol_t *param_sym;
|
||||||
|
node_t *param_list = current_global->children[1]->children[0];
|
||||||
|
global_symbol->nparms = param_list->n_children;
|
||||||
|
|
||||||
|
for (uint64_t param_i = 0; param_i < param_list->n_children; param_i++)
|
||||||
{
|
{
|
||||||
node_t *varname = namelist->children[d];
|
param_sym = malloc(sizeof(symbol_t));
|
||||||
size_t local_num =
|
*param_sym = (symbol_t){
|
||||||
tlhash_size(function->locals) - function->nparms;
|
.type = SYM_PARAMETER,
|
||||||
symbol_t *symbol = malloc ( sizeof(symbol_t) );
|
.name = param_list->children[param_i]->data,
|
||||||
*symbol = (symbol_t) {
|
.node = param_list->children[param_i],
|
||||||
.type = SYM_LOCAL_VAR,
|
.seq = param_i,
|
||||||
.name = varname->data,
|
|
||||||
.node = NULL,
|
|
||||||
.seq = local_num,
|
|
||||||
.nparms = 0,
|
.nparms = 0,
|
||||||
.locals = NULL
|
.locals = NULL
|
||||||
};
|
};
|
||||||
tlhash_insert (
|
|
||||||
function->locals, &local_num, sizeof(size_t), symbol
|
insert_symbol(global_symbol->locals, param_sym);
|
||||||
);
|
|
||||||
add_local ( symbol );
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return no_functions + no_global_vars;
|
||||||
|
}
|
||||||
|
|
||||||
case IDENTIFIER_DATA:
|
|
||||||
entry = lookup_local ( root->data );
|
/**
|
||||||
if ( entry == NULL )
|
* Inserts a symbol into a hash table, key is defined in the name field in the symbol supplied.
|
||||||
tlhash_lookup (
|
*
|
||||||
function->locals, root->data,
|
* @param hash_table pointer to the hash table the symbol is inserted into
|
||||||
strlen(root->data), (void**)&entry
|
* @param symbol pointer to the symbol to be inserted
|
||||||
);
|
*/
|
||||||
if ( entry == NULL )
|
void
|
||||||
tlhash_lookup (
|
insert_symbol(tlhash_t *hash_table, symbol_t* symbol)
|
||||||
global_names,root->data,strlen(root->data),(void**)&entry
|
|
||||||
);
|
|
||||||
if ( entry == NULL )
|
|
||||||
{
|
{
|
||||||
fprintf ( stderr, "Identifier '%s' does not exist in scope\n",
|
tlhash_insert(hash_table, symbol->name, strlen(symbol->name), symbol);
|
||||||
(char *)root->data
|
}
|
||||||
);
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Traverse a node root, and find all variables and strings
|
||||||
|
*
|
||||||
|
* @param function pointer to the current function
|
||||||
|
* @param root pointer to the root node
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
bind_names ( symbol_t *function, node_t *root )
|
||||||
|
{ // NULL check
|
||||||
|
if (!function)
|
||||||
|
return;
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Can't declare variables inside switch unless
|
||||||
|
// it is in a new scope
|
||||||
|
node_t *declarations;
|
||||||
|
// We want do top to bottom traverse, so do not
|
||||||
|
// call recusivly unless we need to go deeper
|
||||||
|
switch (root->type)
|
||||||
|
{
|
||||||
|
// If new BLOCK start, push the scope and recurse from here.
|
||||||
|
case BLOCK:
|
||||||
|
push_scope();
|
||||||
|
for (uint64_t i = 0; i < root->n_children; i++)
|
||||||
|
bind_names(function, root->children[i]);
|
||||||
|
pop_scope();
|
||||||
|
break;
|
||||||
|
|
||||||
|
// If DECLARATION_LIST, find all the identifiers
|
||||||
|
// and insert local into scope and function
|
||||||
|
case DECLARATION_LIST:
|
||||||
|
if (!root->children[0])
|
||||||
|
break;
|
||||||
|
|
||||||
|
declarations = root->children[0];
|
||||||
|
for (uint64_t i = 0; i < declarations->n_children; i++)
|
||||||
|
// Insert each of the local variables in the declaration
|
||||||
|
insert_local_var(function, declarations->children[i]);
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
// If IDENTIFIER_DATA, look up the identifier in all the scopes.
|
||||||
|
// If not found (NULL), crash the compiler with a somewhat helpful message.
|
||||||
|
case IDENTIFIER_DATA:
|
||||||
|
if (!root->data)
|
||||||
|
break;
|
||||||
|
|
||||||
|
if (!(root->entry = lookup_var(function, root->data)))
|
||||||
|
{
|
||||||
|
ERRPRT("Could not find %s in scope!\n", (char*)root->data)
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
root->entry = entry;
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If STRING_DATA, collect the string and point the
|
||||||
|
// data in the corresponding node to the array index
|
||||||
case STRING_DATA:
|
case STRING_DATA:
|
||||||
add_string ( root );
|
collect_string(root);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
// If none of the above, go deeper if possible.
|
||||||
default:
|
default:
|
||||||
for ( size_t c=0; c<root->n_children; c++ )
|
for (uint64_t i = 0; i < root->n_children; i++)
|
||||||
bind_names ( function, root->children[c] );
|
bind_names(function, root->children[i]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pushes a new hash table to the scope stack.
|
||||||
|
*
|
||||||
|
* Increases the size of the stack if too small.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
push_scope(void)
|
||||||
|
{
|
||||||
|
// Allocate memory for the hash table and initialize
|
||||||
|
scopes[cur_scope_depth] = malloc(sizeof(tlhash_t));
|
||||||
|
tlhash_init(scopes[cur_scope_depth++], LOCAL_BUCKET_SIZE);
|
||||||
|
|
||||||
|
// Grow the amount of scopes if not enough
|
||||||
|
if (cur_scope_depth >= no_scopes)
|
||||||
|
{
|
||||||
|
no_scopes *= 2;
|
||||||
|
tlhash_t **new_scopes = realloc(scopes, no_scopes * sizeof(tlhash_t));
|
||||||
|
if (!new_scopes)
|
||||||
|
{
|
||||||
|
ERRPRT("Could not realloc scopes!\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
scopes = new_scopes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pops the dynamicy allocated hash table for the current scope depth
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
pop_scope(void)
|
||||||
|
{
|
||||||
|
tlhash_finalize(scopes[--cur_scope_depth]);
|
||||||
|
free(scopes[cur_scope_depth]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Allocates and inserts a local symbol into
|
||||||
|
* the scope stack and into the function
|
||||||
|
*
|
||||||
|
* @param function pointer to the current function
|
||||||
|
* @param root pointer to the root node for the symbol
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
insert_local_var(symbol_t *function, node_t *root)
|
||||||
|
{ // Null ptr check
|
||||||
|
if (!root->data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Get the sequence num, is the size
|
||||||
|
size_t sequence = tlhash_size(function->locals);
|
||||||
|
|
||||||
|
symbol_t *variable = malloc(sizeof(symbol_t));
|
||||||
|
*variable = (symbol_t){
|
||||||
|
.type = SYM_LOCAL_VAR,
|
||||||
|
.name = root->data,
|
||||||
|
.node = root,
|
||||||
|
.seq = sequence, //! Use sequence as name in var list of function, strictly growing
|
||||||
|
.nparms = 0,
|
||||||
|
.locals = NULL
|
||||||
|
};
|
||||||
|
insert_local_to_scope(variable);
|
||||||
|
insert_local_to_func(function, variable);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a symbol to the top most scope in stack
|
||||||
|
*
|
||||||
|
* @param local pointer to the local to be inserted
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
insert_local_to_scope(symbol_t *local)
|
||||||
|
{
|
||||||
|
insert_symbol(scopes[cur_scope_depth - 1], local);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Insert local symbol to the functions table of local variables
|
||||||
|
* uses the seq num as key as this is strictly growing
|
||||||
|
*
|
||||||
|
* @param function pointer to the function to insert the symbol
|
||||||
|
* @param local pointer to the symbol to be inserted in the table
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
destroy_symtab ( void )
|
insert_local_to_func(symbol_t *function, symbol_t *local)
|
||||||
{
|
{
|
||||||
for ( size_t i=0; i<stringc; i++ )
|
tlhash_insert(
|
||||||
free ( string_list[i] );
|
function->locals, //! Insert local to the function var table
|
||||||
free ( string_list );
|
&local->seq, //! The key is a number, unique, strictly growing
|
||||||
|
sizeof(local->seq), //! Size of key
|
||||||
|
local //! The local symbol
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
size_t n_globals = tlhash_size ( global_names );
|
|
||||||
symbol_t *global_list[n_globals];
|
/**
|
||||||
tlhash_values ( global_names, (void **)&global_list );
|
* Collects strings to the string array and
|
||||||
for ( size_t g=0; g<n_globals; g++ )
|
* points the data in the associated node
|
||||||
|
* to the array position
|
||||||
|
*
|
||||||
|
* @param root pointer to the root node of the string
|
||||||
|
*/
|
||||||
|
static void
|
||||||
|
collect_string(node_t *root)
|
||||||
|
{ // Null ptr check
|
||||||
|
if (!root->data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Get the string and allocate room for array index of string
|
||||||
|
string_list[stringc] = root->data;
|
||||||
|
root->data = malloc(sizeof(size_t));
|
||||||
|
// Set the data ptr
|
||||||
|
*((size_t*)root->data) = stringc++;
|
||||||
|
|
||||||
|
// Grow string array if nessecary
|
||||||
|
if (stringc >= n_string_list)
|
||||||
{
|
{
|
||||||
symbol_t *glob = global_list[g];
|
n_string_list *= 2;
|
||||||
if ( glob->locals != NULL )
|
char **new_string_list = realloc(string_list, n_string_list * sizeof(char*));
|
||||||
|
if (!new_string_list)
|
||||||
{
|
{
|
||||||
size_t n_locals = tlhash_size ( glob->locals );
|
ERRPRT("Could not realloc string list!\n");
|
||||||
symbol_t *locals[n_locals];
|
exit(EXIT_FAILURE);
|
||||||
tlhash_values ( glob->locals, (void **)&locals );
|
|
||||||
for ( size_t l=0; l<n_locals; l++ )
|
|
||||||
free ( locals[l] );
|
|
||||||
tlhash_finalize ( glob->locals );
|
|
||||||
free ( glob->locals );
|
|
||||||
}
|
}
|
||||||
free ( glob );
|
string_list = new_string_list;
|
||||||
}
|
}
|
||||||
tlhash_finalize ( global_names );
|
}
|
||||||
free ( global_names );
|
|
||||||
free ( scopes );
|
|
||||||
|
/**
|
||||||
|
* Looks up a variable identifier in all the scopes.
|
||||||
|
* Start with the scopes, then the parameters and
|
||||||
|
* the the globals
|
||||||
|
*
|
||||||
|
* @param function pointer to the function
|
||||||
|
* @param var identifier to the variable
|
||||||
|
* @return Returns the pointer to the "closest" matched identifier. NULL if not found.
|
||||||
|
*/
|
||||||
|
static symbol_t*
|
||||||
|
lookup_var(symbol_t *function, char* var)
|
||||||
|
{
|
||||||
|
// Symbol to store the stymbol to be found
|
||||||
|
symbol_t* symbol = NULL;
|
||||||
|
// Result stores the result of the hash lookups
|
||||||
|
int result;
|
||||||
|
|
||||||
|
// Try the local scopes first
|
||||||
|
for (int64_t d = cur_scope_depth - 1; d >= 0; d--)
|
||||||
|
{
|
||||||
|
result = tlhash_lookup(scopes[d], var, strlen(var), (void**)&symbol);
|
||||||
|
if (result == TLHASH_SUCCESS)
|
||||||
|
return symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then move to parameters
|
||||||
|
result = tlhash_lookup(function->locals, var, strlen(var), (void**)&symbol);
|
||||||
|
if (result == TLHASH_SUCCESS)
|
||||||
|
return symbol;
|
||||||
|
|
||||||
|
// Last try global parameters
|
||||||
|
result = tlhash_lookup(global_names, var, strlen(var), (void**)&symbol);
|
||||||
|
if (result == TLHASH_SUCCESS)
|
||||||
|
return symbol;
|
||||||
|
|
||||||
|
// If nothing is found, return NULL
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,176 +1,303 @@
|
||||||
%{
|
%{
|
||||||
#include <vslc.h>
|
#include <vslc.h>
|
||||||
|
|
||||||
#define N0C(n,t,d) do { \
|
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
|
||||||
node_init ( n = malloc(sizeof(node_t)), t, d, 0 ); \
|
|
||||||
} while ( false )
|
|
||||||
#define N1C(n,t,d,a) do { \
|
|
||||||
node_init ( n = malloc(sizeof(node_t)), t, d, 1, a ); \
|
|
||||||
} while ( false )
|
|
||||||
#define N2C(n,t,d,a,b) do { \
|
|
||||||
node_init ( n = malloc(sizeof(node_t)), t, d, 2, a, b ); \
|
|
||||||
} while ( false )
|
|
||||||
#define N3C(n,t,d,a,b,c) do { \
|
|
||||||
node_init ( n = malloc(sizeof(node_t)), t, d, 3, a, b, c ); \
|
|
||||||
} while ( false )
|
|
||||||
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%left '|'
|
%define api.value.type {node_t}
|
||||||
%left '^'
|
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
|
||||||
%left '&'
|
%token VAR NUMBER IDENTIFIER STRING
|
||||||
|
|
||||||
|
%left '|' '&' '^'
|
||||||
%left '+' '-'
|
%left '+' '-'
|
||||||
%left '*' '/'
|
%left '*' '/'
|
||||||
%nonassoc UMINUS
|
%nonassoc UMINUS
|
||||||
%right '~'
|
%right '~'
|
||||||
%expect 1
|
//%expect 1
|
||||||
|
|
||||||
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
|
%nonassoc IF THEN
|
||||||
%token VAR NUMBER IDENTIFIER STRING
|
%nonassoc ELSE
|
||||||
|
|
||||||
|
/* Tried fixing vscode complaining about the type for the non-terminals, didn't work
|
||||||
|
%union {
|
||||||
|
node_t* node;
|
||||||
|
}
|
||||||
|
|
||||||
|
%type <node> global_list global
|
||||||
|
%type <node> statement_list print_list expression_list variable_list argument_list parameter_list declaration_list
|
||||||
|
%type <node> function statement block
|
||||||
|
%type <node> assignment_statement return_statement print_statement null_statement if_statement while_statement
|
||||||
|
%type <node> relation expression declaration print_item identifier number string
|
||||||
|
*/
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
program:
|
program:
|
||||||
global_list { N1C ( root, PROGRAM, NULL, $1 ); }
|
global_list {
|
||||||
;
|
root = NODE(PROGRAM, NULL, 1, $1);
|
||||||
global_list :
|
|
||||||
global { N1C ( $$, GLOBAL_LIST, NULL, $1 ); }
|
|
||||||
| global_list global { N2C ( $$, GLOBAL_LIST, NULL, $1, $2 ); }
|
|
||||||
;
|
|
||||||
global:
|
|
||||||
function { N1C ( $$, GLOBAL, NULL, $1 ); }
|
|
||||||
| declaration { N1C ( $$, GLOBAL, NULL, $1 ); }
|
|
||||||
;
|
|
||||||
statement_list :
|
|
||||||
statement { N1C ( $$, STATEMENT_LIST, NULL, $1 ); }
|
|
||||||
| statement_list statement { N2C ( $$, STATEMENT_LIST, NULL, $1, $2 ); }
|
|
||||||
;
|
|
||||||
print_list :
|
|
||||||
print_item { N1C ( $$, PRINT_LIST, NULL, $1 ); }
|
|
||||||
| print_list ',' print_item { N2C ( $$, PRINT_LIST, NULL, $1, $3 ); }
|
|
||||||
;
|
|
||||||
expression_list :
|
|
||||||
expression { N1C ( $$, EXPRESSION_LIST, NULL, $1 ); }
|
|
||||||
| expression_list ',' expression { N2C($$, EXPRESSION_LIST, NULL, $1, $3); }
|
|
||||||
;
|
|
||||||
variable_list :
|
|
||||||
identifier { N1C ( $$, VARIABLE_LIST, NULL, $1 ); }
|
|
||||||
| variable_list ',' identifier { N2C ( $$, VARIABLE_LIST, NULL, $1, $3 ); }
|
|
||||||
;
|
|
||||||
argument_list :
|
|
||||||
expression_list { N1C ( $$, ARGUMENT_LIST, NULL, $1 ); }
|
|
||||||
| /* epsilon */ { $$ = NULL; }
|
|
||||||
;
|
|
||||||
parameter_list :
|
|
||||||
variable_list { N1C ( $$, PARAMETER_LIST, NULL, $1 ); }
|
|
||||||
| /* epsilon */ { $$ = NULL; }
|
|
||||||
;
|
|
||||||
declaration_list :
|
|
||||||
declaration { N1C ( $$, DECLARATION_LIST, NULL, $1 ); }
|
|
||||||
| declaration_list declaration { N2C ($$, DECLARATION_LIST, NULL, $1, $2); }
|
|
||||||
;
|
|
||||||
function :
|
|
||||||
FUNC identifier '(' parameter_list ')' statement
|
|
||||||
{ N3C ( $$, FUNCTION, NULL, $2, $4, $6 ); }
|
|
||||||
;
|
|
||||||
statement :
|
|
||||||
assignment_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| return_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| print_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| if_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| while_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| null_statement { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
| block { N1C ( $$, STATEMENT, NULL, $1 ); }
|
|
||||||
;
|
|
||||||
block :
|
|
||||||
OPENBLOCK declaration_list statement_list CLOSEBLOCK
|
|
||||||
{ N2C ($$, BLOCK, NULL, $2, $3); }
|
|
||||||
| OPENBLOCK statement_list CLOSEBLOCK { N1C ($$, BLOCK, NULL, $2 ); }
|
|
||||||
;
|
|
||||||
assignment_statement :
|
|
||||||
identifier ':' '=' expression
|
|
||||||
{ N2C ( $$, ASSIGNMENT_STATEMENT, NULL, $1, $4 ); }
|
|
||||||
| identifier '+' '=' expression
|
|
||||||
{ N2C ( $$, ADD_STATEMENT, NULL, $1, $4 ); }
|
|
||||||
| identifier '-' '=' expression
|
|
||||||
{ N2C ( $$, SUBTRACT_STATEMENT, NULL, $1, $4 ); }
|
|
||||||
| identifier '*' '=' expression
|
|
||||||
{ N2C ( $$, MULTIPLY_STATEMENT, NULL, $1, $4 ); }
|
|
||||||
| identifier '/' '=' expression
|
|
||||||
{ N2C ( $$, DIVIDE_STATEMENT, NULL, $1, $4 ); }
|
|
||||||
;
|
|
||||||
return_statement :
|
|
||||||
RETURN expression
|
|
||||||
{ N1C ( $$, RETURN_STATEMENT, NULL, $2 ); }
|
|
||||||
;
|
|
||||||
print_statement :
|
|
||||||
PRINT print_list
|
|
||||||
{ N1C ( $$, PRINT_STATEMENT, NULL, $2 ); }
|
|
||||||
;
|
|
||||||
null_statement :
|
|
||||||
CONTINUE
|
|
||||||
{ N0C ( $$, NULL_STATEMENT, NULL ); }
|
|
||||||
;
|
|
||||||
if_statement :
|
|
||||||
IF relation THEN statement
|
|
||||||
{ N2C ( $$, IF_STATEMENT, NULL, $2, $4 ); }
|
|
||||||
| IF relation THEN statement ELSE statement
|
|
||||||
{ N3C ( $$, IF_STATEMENT, NULL, $2, $4, $6 ); }
|
|
||||||
;
|
|
||||||
while_statement :
|
|
||||||
WHILE relation DO statement
|
|
||||||
{ N2C ( $$, WHILE_STATEMENT, NULL, $2, $4 ); }
|
|
||||||
;
|
|
||||||
relation:
|
|
||||||
expression '=' expression
|
|
||||||
{ N2C ( $$, RELATION, strdup("="), $1, $3 ); }
|
|
||||||
| expression '<' expression
|
|
||||||
{ N2C ( $$, RELATION, strdup("<"), $1, $3 ); }
|
|
||||||
| expression '>' expression
|
|
||||||
{ N2C ( $$, RELATION, strdup(">"), $1, $3 ); }
|
|
||||||
;
|
|
||||||
expression :
|
|
||||||
expression '|' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("|"), $1, $3 ); }
|
|
||||||
| expression '^' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("^"), $1, $3 ); }
|
|
||||||
| expression '&' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("&"), $1, $3 ); }
|
|
||||||
| expression '+' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("+"), $1, $3 ); }
|
|
||||||
| expression '-' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("-"), $1, $3 ); }
|
|
||||||
| expression '*' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("*"), $1, $3 ); }
|
|
||||||
| expression '/' expression
|
|
||||||
{ N2C ( $$, EXPRESSION, strdup("/"), $1, $3 ); }
|
|
||||||
| '-' expression %prec UMINUS
|
|
||||||
{ N1C ( $$, EXPRESSION, strdup("-"), $2 ); }
|
|
||||||
| '~' expression %prec UMINUS
|
|
||||||
{ N1C ( $$, EXPRESSION, strdup("~"), $2 ); }
|
|
||||||
| '(' expression ')' { $$ = $2; }
|
|
||||||
| number { N1C ( $$, EXPRESSION, NULL, $1 ); }
|
|
||||||
| identifier
|
|
||||||
{ N1C ( $$, EXPRESSION, NULL, $1 ); }
|
|
||||||
| identifier '(' argument_list ')'
|
|
||||||
{ N2C ( $$, EXPRESSION, NULL, $1, $3 ); }
|
|
||||||
;
|
|
||||||
declaration :
|
|
||||||
VAR variable_list { N1C ( $$, DECLARATION, NULL, $2 ); }
|
|
||||||
;
|
|
||||||
print_item :
|
|
||||||
expression
|
|
||||||
{ N1C ( $$, PRINT_ITEM, NULL, $1 ); }
|
|
||||||
| string
|
|
||||||
{ N1C ( $$, PRINT_ITEM, NULL, $1 ); }
|
|
||||||
;
|
|
||||||
identifier: IDENTIFIER { N0C($$, IDENTIFIER_DATA, strdup(yytext) ); }
|
|
||||||
number: NUMBER
|
|
||||||
{
|
|
||||||
int64_t *value = malloc ( sizeof(int64_t) );
|
|
||||||
*value = strtol ( yytext, NULL, 10 );
|
|
||||||
N0C($$, NUMBER_DATA, value );
|
|
||||||
}
|
}
|
||||||
string: STRING { N0C($$, STRING_DATA, strdup(yytext) ); }
|
;
|
||||||
|
|
||||||
|
global_list:
|
||||||
|
global {
|
||||||
|
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| global_list global {
|
||||||
|
$$ = NODE(GLOBAL_LIST, NULL, 2, $1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
global:
|
||||||
|
function {
|
||||||
|
$$ = NODE(GLOBAL, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| declaration {
|
||||||
|
$$ = NODE(GLOBAL, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
statement_list:
|
||||||
|
statement {
|
||||||
|
$$ = NODE(STATEMENT_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| statement_list statement {
|
||||||
|
$$ = NODE(STATEMENT_LIST, NULL, 2, $1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
print_list:
|
||||||
|
print_item {
|
||||||
|
$$ = NODE(PRINT_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| print_list ',' print_item {
|
||||||
|
$$ = NODE(PRINT_LIST, NULL, 2, $1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
expression_list:
|
||||||
|
expression {
|
||||||
|
$$ = NODE(EXPRESSION_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| expression_list ',' expression {
|
||||||
|
$$ = NODE(EXPRESSION_LIST, NULL, 2, $1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
variable_list:
|
||||||
|
identifier {
|
||||||
|
$$ = NODE(VARIABLE_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| variable_list ',' identifier {
|
||||||
|
$$ = NODE(VARIABLE_LIST, NULL, 2, $1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
argument_list:
|
||||||
|
expression_list {
|
||||||
|
$$ = NODE(ARGUMENT_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| /* epsilon */ {
|
||||||
|
$$ = NODE(ARGUMENT_LIST, NULL, 0);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
parameter_list:
|
||||||
|
variable_list {
|
||||||
|
$$ = NODE(PARAMETER_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| /* epsilon */ {
|
||||||
|
$$ = NODE(PARAMETER_LIST, NULL, 0);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
declaration_list:
|
||||||
|
declaration {
|
||||||
|
$$ = NODE(DECLARATION_LIST, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| declaration_list declaration {
|
||||||
|
$$ = NODE(DECLARATION_LIST, NULL, 2, $1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
function:
|
||||||
|
FUNC identifier '(' parameter_list ')' statement {
|
||||||
|
$$ = NODE(FUNCTION, NULL, 3, $2, $4, $6);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
statement:
|
||||||
|
assignment_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| return_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| print_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| if_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| while_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| null_statement {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| block {
|
||||||
|
$$ = NODE(STATEMENT, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
block:
|
||||||
|
OPENBLOCK declaration_list statement_list CLOSEBLOCK {
|
||||||
|
$$ = NODE(BLOCK, NULL, 2, $2, $3);
|
||||||
|
}
|
||||||
|
| OPENBLOCK statement_list CLOSEBLOCK {
|
||||||
|
$$ = NODE(BLOCK, NULL, 1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
assignment_statement:
|
||||||
|
identifier ':' '=' expression {
|
||||||
|
$$ = NODE(ASSIGNMENT_STATEMENT, NULL, 2, $1, $4);
|
||||||
|
}
|
||||||
|
| identifier '+' '=' expression {
|
||||||
|
$$ = NODE(ADD_STATEMENT, NULL, 2, $1, $4);
|
||||||
|
}
|
||||||
|
| identifier '-' '=' expression {
|
||||||
|
$$ = NODE(SUBTRACT_STATEMENT, NULL, 2, $1, $4);
|
||||||
|
}
|
||||||
|
| identifier '*' '=' expression {
|
||||||
|
$$ = NODE(MULTIPLY_STATEMENT, NULL, 2, $1, $4);
|
||||||
|
}
|
||||||
|
| identifier '/' '=' expression {
|
||||||
|
$$ = NODE(DIVIDE_STATEMENT, NULL, 2, $1, $4);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
return_statement:
|
||||||
|
RETURN expression {
|
||||||
|
$$ = NODE(RETURN_STATEMENT, NULL, 1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
print_statement:
|
||||||
|
PRINT print_list {
|
||||||
|
$$ = NODE(PRINT_STATEMENT, NULL, 1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
null_statement:
|
||||||
|
CONTINUE {
|
||||||
|
$$ = NODE(NULL_STATEMENT, NULL, 0);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
if_statement:
|
||||||
|
IF relation THEN statement {
|
||||||
|
$$ = NODE(IF_STATEMENT, NULL, 2, $2, $4);
|
||||||
|
}
|
||||||
|
| IF relation THEN statement ELSE statement {
|
||||||
|
$$ = NODE(IF_STATEMENT, NULL, 3, $2, $4, $6);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
while_statement:
|
||||||
|
WHILE relation DO statement {
|
||||||
|
$$ = NODE(WHILE_STATEMENT, NULL, 2, $2, $4);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
relation:
|
||||||
|
expression '=' expression {
|
||||||
|
$$ = NODE(RELATION, strdup("="), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '<' expression {
|
||||||
|
$$ = NODE(RELATION, strdup("<"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '>' expression {
|
||||||
|
$$ = NODE(RELATION, strdup(">"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
expression:
|
||||||
|
expression '|' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("|"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '^' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("^"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '&' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("&"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '+' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("+"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '-' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("-"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '*' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("*"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| expression '/' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("/"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
| '-' expression %prec UMINUS {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("-"), 1, $2);
|
||||||
|
}
|
||||||
|
| '~' expression {
|
||||||
|
$$ = NODE(EXPRESSION, strdup("~"), 1, $2);
|
||||||
|
}
|
||||||
|
| '(' expression ')' {
|
||||||
|
$$ = NODE(EXPRESSION, NULL /*strdup("group")*/, 1, $2);
|
||||||
|
}
|
||||||
|
| number {
|
||||||
|
$$ = NODE(EXPRESSION, NULL /*strdup("number")*/, 1, $1);
|
||||||
|
}
|
||||||
|
| identifier {
|
||||||
|
$$ = NODE(EXPRESSION, NULL /*strdup("identifier")*/, 1, $1);
|
||||||
|
}
|
||||||
|
| identifier '(' argument_list ')' {
|
||||||
|
$$ = NODE(EXPRESSION, /*NULL*/ strdup("function_call"), 2, $1, $3);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
declaration:
|
||||||
|
VAR variable_list {
|
||||||
|
$$ = NODE(DECLARATION, NULL, 1, $2);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
print_item:
|
||||||
|
expression {
|
||||||
|
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
| string {
|
||||||
|
$$ = NODE(PRINT_ITEM, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
identifier:
|
||||||
|
IDENTIFIER {
|
||||||
|
$$ = NODE(IDENTIFIER_DATA, strdup(yytext), 0); // Zero children
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
number:
|
||||||
|
NUMBER {
|
||||||
|
uint64_t* p_number = malloc(sizeof(uint64_t));
|
||||||
|
*p_number = strtol(yytext, NULL, 10);
|
||||||
|
$$ = NODE(NUMBER_DATA, p_number, 0); // Zero children
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
|
string:
|
||||||
|
STRING {
|
||||||
|
$$ = NODE(STRING_DATA, strdup(yytext), 0); // Zero children
|
||||||
|
}
|
||||||
|
;
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -12,6 +12,10 @@ tree_print(node_t* root, stem head);
|
||||||
|
|
||||||
static void destroy_subtree ( node_t *discard );
|
static void destroy_subtree ( node_t *discard );
|
||||||
|
|
||||||
|
static void prune_children(node_t **simplified, node_t *root);
|
||||||
|
static void resolve_constant_expressions(node_t **simplified, node_t *root);
|
||||||
|
static void flatten(node_t **simplified, node_t *root);
|
||||||
|
|
||||||
|
|
||||||
/* External interface */
|
/* External interface */
|
||||||
void
|
void
|
||||||
|
@ -40,7 +44,8 @@ print_syntax_tree ( void )
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
// Changed so it returns the pointer to the new node, can be used as before, but makes the parser file cleaner
|
||||||
|
node_t*
|
||||||
node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
|
node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
|
||||||
{
|
{
|
||||||
va_list child_list;
|
va_list child_list;
|
||||||
|
@ -55,6 +60,8 @@ node_init (node_t *nd, node_index_t type, void *data, uint64_t n_children, ...)
|
||||||
for ( uint64_t i=0; i<n_children; i++ )
|
for ( uint64_t i=0; i<n_children; i++ )
|
||||||
nd->children[i] = va_arg ( child_list, node_t * );
|
nd->children[i] = va_arg ( child_list, node_t * );
|
||||||
va_end ( child_list );
|
va_end ( child_list );
|
||||||
|
|
||||||
|
return nd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,7 +91,8 @@ tree_print(node_t* root, stem head)
|
||||||
printf("─%s", node_string[root->type]);
|
printf("─%s", node_string[root->type]);
|
||||||
if ( root->type == IDENTIFIER_DATA ||
|
if ( root->type == IDENTIFIER_DATA ||
|
||||||
root->type == STRING_DATA ||
|
root->type == STRING_DATA ||
|
||||||
root->type == EXPRESSION )
|
root->type == EXPRESSION ||
|
||||||
|
root->type == RELATION)
|
||||||
printf("(%s)", (char *) root->data);
|
printf("(%s)", (char *) root->data);
|
||||||
else if (root->type == NUMBER_DATA)
|
else if (root->type == NUMBER_DATA)
|
||||||
printf("(%ld)", *((int64_t *)root->data));
|
printf("(%ld)", *((int64_t *)root->data));
|
||||||
|
@ -152,81 +160,241 @@ destroy_subtree ( node_t *discard )
|
||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
simplify_tree ( node_t **simplified, node_t *root )
|
flatten(node_t **simplified, node_t *root)
|
||||||
{
|
{
|
||||||
if ( root == NULL )
|
/* This will flatten left-expanded lists */
|
||||||
|
if (!root)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
/* Simplify subtrees before examining this node */
|
/* Do this recursivly */
|
||||||
for ( uint64_t i=0; i<root->n_children; i++ )
|
for (int i = 0; i < root->n_children; i++)
|
||||||
simplify_tree ( &root->children[i], root->children[i] );
|
flatten(&root->children[i], root->children[i]);
|
||||||
|
|
||||||
node_t *discard, *result = root;
|
node_t **new_children, *result = root;
|
||||||
switch (root->type)
|
switch (root->type)
|
||||||
{
|
{
|
||||||
/* Structures of purely syntactic function */
|
case GLOBAL_LIST:
|
||||||
case PARAMETER_LIST: case ARGUMENT_LIST:
|
case STATEMENT_LIST:
|
||||||
case STATEMENT: case PRINT_ITEM: case GLOBAL:
|
case PRINT_LIST:
|
||||||
|
case EXPRESSION_LIST:
|
||||||
|
case VARIABLE_LIST:
|
||||||
|
case DECLARATION_LIST:
|
||||||
|
// Check if node have more than two children
|
||||||
|
if (root->n_children < 2)
|
||||||
|
break;
|
||||||
|
|
||||||
result = root->children[0];
|
result = root->children[0];
|
||||||
|
result->n_children++;
|
||||||
|
|
||||||
|
// Realloc the array of children to the new size
|
||||||
|
if (!(new_children = realloc(result->children, result->n_children * sizeof(node_t*))))
|
||||||
|
break;
|
||||||
|
// if successs, insert the new array
|
||||||
|
result->children = new_children;
|
||||||
|
|
||||||
|
// Insert child at the end
|
||||||
|
result->children[result->n_children - 1] = root->children[1];
|
||||||
|
|
||||||
node_finalize(root);
|
node_finalize(root);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
*simplified = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
prune_children(node_t **simplified, node_t *root)
|
||||||
|
{
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Do this recursivly */
|
||||||
|
for (int i = 0; i < root->n_children; i++)
|
||||||
|
prune_children(&root->children[i], root->children[i]);
|
||||||
|
|
||||||
|
node_t *result = root;
|
||||||
|
switch (root->type)
|
||||||
|
{
|
||||||
|
case PROGRAM:
|
||||||
|
case GLOBAL:
|
||||||
|
//case ARGUMENT_LIST: // For this to work, need to change order of operations
|
||||||
|
//case PARAMETER_LIST: // For this to work, need to change order of operations
|
||||||
|
//case VARIABLE_LIST:
|
||||||
|
//case EXPRESSION_LIST:
|
||||||
|
case DECLARATION:
|
||||||
|
case STATEMENT:
|
||||||
|
case PRINT_ITEM:
|
||||||
case PRINT_STATEMENT:
|
case PRINT_STATEMENT:
|
||||||
result = root->children[0];
|
result = root->children[0];
|
||||||
|
|
||||||
|
// The print_statement only contains a print_list, still need a print_statement.
|
||||||
|
if (root->type == PRINT_STATEMENT)
|
||||||
result->type = PRINT_STATEMENT;
|
result->type = PRINT_STATEMENT;
|
||||||
|
|
||||||
node_finalize(root);
|
node_finalize(root);
|
||||||
/* Flatten lists:
|
|
||||||
* Take left child, append right child, substitute left for root.
|
|
||||||
*/
|
|
||||||
case STATEMENT_LIST: case DECLARATION_LIST: case GLOBAL_LIST:
|
|
||||||
case PRINT_LIST: case EXPRESSION_LIST: case VARIABLE_LIST:
|
|
||||||
if ( root->n_children >= 2 )
|
|
||||||
{
|
|
||||||
result = root->children[0];
|
|
||||||
result->n_children += 1;
|
|
||||||
result->children = realloc (
|
|
||||||
result->children, result->n_children * sizeof(node_t *)
|
|
||||||
);
|
|
||||||
result->children[result->n_children-1] = root->children[1];
|
|
||||||
node_finalize ( root );
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case EXPRESSION:
|
}
|
||||||
|
|
||||||
|
*simplified = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
resolve_constant_expressions(node_t **simplified, node_t *root)
|
||||||
|
{
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Do this recursivly */
|
||||||
|
for (int i = 0; i < root->n_children; i++)
|
||||||
|
resolve_constant_expressions(&root->children[i], root->children[i]);
|
||||||
|
|
||||||
|
if (root->type != EXPRESSION)
|
||||||
|
return;
|
||||||
|
|
||||||
|
node_t *result = root;
|
||||||
|
|
||||||
switch (root->n_children)
|
switch (root->n_children)
|
||||||
{
|
{
|
||||||
case 1:
|
case 1:
|
||||||
if ( root->children[0]->type == NUMBER_DATA )
|
result = root->children[0];
|
||||||
|
|
||||||
|
if (root->data &&
|
||||||
|
result->type == NUMBER_DATA &&
|
||||||
|
result->data)
|
||||||
{
|
{
|
||||||
result = root->children[0];
|
|
||||||
if ( root->data != NULL )
|
|
||||||
*((int64_t *)result->data) *= -1;
|
|
||||||
node_finalize (root);
|
|
||||||
}
|
|
||||||
else if ( root->data == NULL )
|
|
||||||
{
|
|
||||||
result = root->children[0];
|
|
||||||
node_finalize (root);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
if ( root->children[0]->type == NUMBER_DATA &&
|
|
||||||
root->children[1]->type == NUMBER_DATA
|
|
||||||
) {
|
|
||||||
result = root->children[0];
|
|
||||||
int64_t
|
|
||||||
*x = result->data,
|
|
||||||
*y = root->children[1]->data;
|
|
||||||
switch (*((char*)root->data))
|
switch (*((char*)root->data))
|
||||||
{
|
{
|
||||||
case '+': *x += *y; break;
|
case '-':
|
||||||
case '-': *x -= *y; break;
|
*((int64_t*)result->data) *= -1;
|
||||||
case '*': *x *= *y; break;
|
break;
|
||||||
case '/': *x /= *y; break;
|
case '~':
|
||||||
|
*((int64_t*)result->data) = ~*((int64_t*)result->data);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
node_finalize(root);
|
||||||
|
}
|
||||||
|
else if (!root->data)
|
||||||
|
node_finalize(root);
|
||||||
|
else
|
||||||
|
result = root;
|
||||||
|
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 2:
|
||||||
|
// Both children needs to be numbers to resolve constants
|
||||||
|
if (root->children[0]->type == NUMBER_DATA &&
|
||||||
|
root->children[1]->type == NUMBER_DATA)
|
||||||
|
{
|
||||||
|
// Check if children does not contain null pointers
|
||||||
|
if (!root->children[0]->data)
|
||||||
|
break;
|
||||||
|
if (!root->children[1]->data)
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Check if data field is not null pointer
|
||||||
|
if (!root->data)
|
||||||
|
break;
|
||||||
|
|
||||||
|
result = root->children[0];
|
||||||
|
int64_t
|
||||||
|
*lhs = result->data,
|
||||||
|
*rhs = root->children[1]->data;
|
||||||
|
|
||||||
|
switch (*(char*)root->data)
|
||||||
|
{
|
||||||
|
/* Assignments */
|
||||||
|
case '|': *lhs |= *rhs; break;
|
||||||
|
case '^': *lhs ^= *rhs; break;
|
||||||
|
case '&': *lhs &= *rhs; break;
|
||||||
|
case '+': *lhs += *rhs; break;
|
||||||
|
case '-': *lhs -= *rhs; break;
|
||||||
|
case '*': *lhs *= *rhs; break;
|
||||||
|
case '/': *lhs /= *rhs; break;
|
||||||
|
}
|
||||||
|
|
||||||
node_finalize(root->children[1]);
|
node_finalize(root->children[1]);
|
||||||
node_finalize(root);
|
node_finalize(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*simplified = result;
|
*simplified = result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
resolve_constant_relations( node_t** simplified, node_t* root)
|
||||||
|
{
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/* Do this recursivly */
|
||||||
|
for (int i = 0; i < root->n_children; i++)
|
||||||
|
resolve_constant_relations(&root->children[i], root->children[i]);
|
||||||
|
|
||||||
|
if (root->type != RELATION)//|| root->type != RELATION)
|
||||||
|
return;
|
||||||
|
|
||||||
|
node_t *result = root;
|
||||||
|
|
||||||
|
if (root->n_children != 2)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Both children must be constant numbers
|
||||||
|
if (root->children[0]->type != NUMBER_DATA ||
|
||||||
|
root->children[1]->type != NUMBER_DATA)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Check if children does not contain null pointers
|
||||||
|
if (!root->children[0]->data)
|
||||||
|
return;
|
||||||
|
if (!root->children[1]->data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Check if data field is not null pointer
|
||||||
|
if (!root->data)
|
||||||
|
return;
|
||||||
|
|
||||||
|
result = root->children[0];
|
||||||
|
int64_t
|
||||||
|
*lhs = result->data,
|
||||||
|
*rhs = root->children[1]->data;
|
||||||
|
|
||||||
|
switch (*(char*)root->data)
|
||||||
|
{
|
||||||
|
/* Relations */
|
||||||
|
case '=': *lhs = (*lhs == *rhs); break;
|
||||||
|
case '<': *lhs = (*lhs < *rhs); break;
|
||||||
|
case '>': *lhs = (*lhs > *rhs); break;
|
||||||
|
}
|
||||||
|
|
||||||
|
node_finalize(root->children[1]);
|
||||||
|
node_finalize(root);
|
||||||
|
|
||||||
|
*simplified = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
simplify_tree ( node_t **simplified, node_t *root )
|
||||||
|
{
|
||||||
|
if (!root)
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
Each of the functions do their operations recursivly.
|
||||||
|
This opens up for a lot more flexibility, like removing
|
||||||
|
variable list after it is flatten
|
||||||
|
*/
|
||||||
|
flatten(&root, root);
|
||||||
|
prune_children(&root, root);
|
||||||
|
resolve_constant_expressions(&root, root);
|
||||||
|
|
||||||
|
// The following is experimental, will resolve the constant relations
|
||||||
|
resolve_constant_relations(&root, root);
|
||||||
|
|
||||||
|
*simplified = root;
|
||||||
|
}
|
||||||
|
|
|
@ -29,6 +29,7 @@ main ( int argc, char **argv )
|
||||||
options ( argc, argv );
|
options ( argc, argv );
|
||||||
|
|
||||||
yyparse(); // Generated from grammar/bison, constructs syntax tree
|
yyparse(); // Generated from grammar/bison, constructs syntax tree
|
||||||
|
yylex_destroy(); // Free heap used by flex
|
||||||
|
|
||||||
if ( print_full_tree )
|
if ( print_full_tree )
|
||||||
print_syntax_tree ();
|
print_syntax_tree ();
|
||||||
|
|
Loading…
Reference in New Issue