Compare commits
6 Commits
2a3c76cb6e
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 53ec113f81 | |||
| a7e5535dde | |||
| 958dfe51fd | |||
| 75c8e5dc68 | |||
| 77376dc1ef | |||
| 57cc3f62fc |
28
exercises/06/.vscode/launch.json
vendored
Normal file
28
exercises/06/.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
// for Linux
|
||||||
|
"name": "(gdb) Launch euclid codegen 2",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceRoot}/vslc/src/vslc",
|
||||||
|
"args": ["<", "${workspaceFolder}/vslc/vsl_programs/ps6-codegen2/euclid.vsl"],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": true,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -6,17 +6,18 @@
|
|||||||
#define LABEL(label) printf("_%s:\n", (char*)label)
|
#define LABEL(label) printf("_%s:\n", (char*)label)
|
||||||
#define COMMENT(format, args...) printf("# "format"\n", ##args)
|
#define COMMENT(format, args...) printf("# "format"\n", ##args)
|
||||||
|
|
||||||
|
// The PUSH and POP macros also increments/decrements the stack_depth to keep track of the stack
|
||||||
#define PUSH(param) printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n", #param, ++stack_depth)
|
#define PUSH(param) printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n", #param, ++stack_depth)
|
||||||
#define POP(param) printf("\tpopq\t%s\t\t\t\t# POP: %ld\n", #param, --stack_depth)
|
#define POP(param) printf("\tpopq\t%s\t\t\t\t# POP: %ld\n", #param, --stack_depth)
|
||||||
|
|
||||||
#define NO_REG_RECORD 6
|
#define NO_REG_RECORD 6
|
||||||
//#define NO_CALLE_SAVED_REG 10
|
|
||||||
|
|
||||||
// Keep track of sequence of stack depth, ifs and whiles
|
// Keep track of sequence of stack depth, ifs and whiles
|
||||||
static uint64_t
|
static uint64_t
|
||||||
stack_depth,
|
stack_depth,
|
||||||
if_seq,
|
if_seq,
|
||||||
while_seq;
|
while_seq,
|
||||||
|
closest_while;
|
||||||
|
|
||||||
/**Generate table of strings in a rodata section. */
|
/**Generate table of strings in a rodata section. */
|
||||||
void generate_stringtable ( void );
|
void generate_stringtable ( void );
|
||||||
@@ -38,10 +39,6 @@ static const char *record[NO_REG_RECORD] = {
|
|||||||
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
|
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
|
||||||
};
|
};
|
||||||
|
|
||||||
//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
|
// Helper funcs for generating different nodes
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -90,11 +87,38 @@ static void generate_function_return(node_t *node);
|
|||||||
*/
|
*/
|
||||||
static void solve_statements(node_t *node, char *operator);
|
static void solve_statements(node_t *node, char *operator);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Solves a relation and leaves the result (1 or 0, True or False) in %rax
|
||||||
|
* If the relation node is just a number, just put the number in %rax.
|
||||||
|
* This is used with a cmp instruction to check if the relation is true.
|
||||||
|
*
|
||||||
|
* This makes it possible to compute the constant relations.
|
||||||
|
*
|
||||||
|
* @param node pointer to the node the suspected relation is in
|
||||||
|
*/
|
||||||
|
static void solve_relations(node_t *node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Solves a node that is an if statement
|
||||||
|
* Uses the solve realtions function to solve the relations
|
||||||
|
*
|
||||||
|
* @param node pointer to the if node
|
||||||
|
*/
|
||||||
static void generate_if_statement(node_t *node);
|
static void generate_if_statement(node_t *node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Solves a node that is an while statement
|
||||||
|
* Uses the solve realtions function to solve the relations
|
||||||
|
*
|
||||||
|
* @param node pointer to the while node
|
||||||
|
*/
|
||||||
static void generate_while_statement(node_t *node);
|
static void generate_while_statement(node_t *node);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inserts a jump to the inner most while.
|
||||||
|
* This also works when multiple whiles are nested.
|
||||||
|
*
|
||||||
|
*/
|
||||||
static void solve_continue_statement();
|
static void solve_continue_statement();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -119,12 +143,23 @@ static uint64_t fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list);
|
|||||||
void
|
void
|
||||||
generate_program ( void )
|
generate_program ( void )
|
||||||
{
|
{
|
||||||
|
// Generate the string table at the top
|
||||||
generate_stringtable();
|
generate_stringtable();
|
||||||
generate_global_variables();
|
|
||||||
|
|
||||||
|
// Fetch all the global elements (functions and global vars)
|
||||||
symbol_t **global_list;
|
symbol_t **global_list;
|
||||||
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
uint64_t no_globals = fetch_symbols(global_names, &global_list);
|
||||||
|
|
||||||
|
// Find the number of actual global variables
|
||||||
|
uint64_t no_global_vars = 0;
|
||||||
|
for (uint64_t g = 0; g < no_globals; g++)
|
||||||
|
if (global_list[g]->type == SYM_GLOBAL_VAR) no_global_vars++;
|
||||||
|
|
||||||
|
// Generate globbal variables if there are any
|
||||||
|
if (no_global_vars)
|
||||||
|
generate_global_variables();
|
||||||
|
|
||||||
|
// Find the function called main and keep track of if it found an generated
|
||||||
bool main_generated = false;
|
bool main_generated = false;
|
||||||
uint64_t seq0_index = -1;
|
uint64_t seq0_index = -1;
|
||||||
for (uint64_t g = 0; g < no_globals; g++)
|
for (uint64_t g = 0; g < no_globals; g++)
|
||||||
@@ -148,6 +183,7 @@ generate_program ( void )
|
|||||||
if (!main_generated)
|
if (!main_generated)
|
||||||
generate_main(global_list[seq0_index]);
|
generate_main(global_list[seq0_index]);
|
||||||
|
|
||||||
|
// Then generate all the functions from vsl
|
||||||
for (uint64_t g = 0; g < no_globals; g++)
|
for (uint64_t g = 0; g < no_globals; g++)
|
||||||
{
|
{
|
||||||
if (global_list[g]->type == SYM_FUNCTION)
|
if (global_list[g]->type == SYM_FUNCTION)
|
||||||
@@ -186,10 +222,10 @@ generate_global_variables ( void )
|
|||||||
puts(".bss");
|
puts(".bss");
|
||||||
puts(".align 8");
|
puts(".align 8");
|
||||||
|
|
||||||
for (uint64_t g = 0; g < no_globals; g++) {
|
for (uint64_t g = 0; g < no_globals; g++)
|
||||||
if (global_list[g]->type == SYM_GLOBAL_VAR)
|
if (global_list[g]->type == SYM_GLOBAL_VAR)
|
||||||
printf(".%s:\n", global_list[g]->name);
|
printf(".%s:\n", global_list[g]->name);
|
||||||
}
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
free(global_list);
|
free(global_list);
|
||||||
|
|
||||||
@@ -198,7 +234,7 @@ generate_global_variables ( void )
|
|||||||
void
|
void
|
||||||
generate_function ( symbol_t *function )
|
generate_function ( symbol_t *function )
|
||||||
{
|
{
|
||||||
// TODO: Generate code for declaring and entering function, then generate its body
|
// Keep track of the stack size in each of the functions
|
||||||
stack_depth = 0;
|
stack_depth = 0;
|
||||||
|
|
||||||
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
|
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
|
||||||
@@ -211,16 +247,13 @@ generate_function ( symbol_t *function )
|
|||||||
|
|
||||||
// Push params to stack
|
// Push params to stack
|
||||||
for (int arg = 0; arg < MIN(NO_REG_RECORD,function->nparms); arg++)
|
for (int arg = 0; arg < MIN(NO_REG_RECORD,function->nparms); arg++)
|
||||||
{
|
|
||||||
printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n",
|
printf("\tpushq\t%s\t\t\t\t# PUSH: %ld\n",
|
||||||
record[arg],
|
record[arg],
|
||||||
++stack_depth
|
++stack_depth
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
// How many local variables are inside function
|
// How many local variables are inside function
|
||||||
uint64_t no_locals = function->locals->size - function->nparms;
|
uint64_t no_locals = function->locals->size - function->nparms;
|
||||||
//stack_depth += no_locals;
|
|
||||||
|
|
||||||
// Make room for the local vars
|
// Make room for the local vars
|
||||||
while(no_locals--)
|
while(no_locals--)
|
||||||
@@ -234,6 +267,7 @@ generate_function ( symbol_t *function )
|
|||||||
|
|
||||||
// Now the stack ptr should be 16 byte aligned.
|
// Now the stack ptr should be 16 byte aligned.
|
||||||
|
|
||||||
|
// Then generate the body of the function
|
||||||
generate_node(function->node);
|
generate_node(function->node);
|
||||||
|
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
@@ -243,17 +277,15 @@ generate_function ( symbol_t *function )
|
|||||||
void
|
void
|
||||||
generate_node ( node_t *node)
|
generate_node ( node_t *node)
|
||||||
{
|
{
|
||||||
// TODO: Generate code corresponding to node
|
|
||||||
|
|
||||||
// All statements have the same structure.
|
// All statements have the same structure.
|
||||||
// [0] is the lhs, needs to be identifier, parser ensures this
|
// [0] is the lhs, needs to be identifier, parser ensures this
|
||||||
// [1] is thr rhs
|
// [1] is thr rhs
|
||||||
switch (node->type)
|
switch (node->type)
|
||||||
{
|
{
|
||||||
case ASSIGNMENT_STATEMENT:
|
case ASSIGNMENT_STATEMENT:
|
||||||
|
// First solve the rhs
|
||||||
solve_expressions(node->children[1]);
|
solve_expressions(node->children[1]);
|
||||||
//ASM(popq, %rax);
|
// Then store in lhs
|
||||||
//POP(%rax);
|
|
||||||
writeback_variable(node->children[0], "%rax");
|
writeback_variable(node->children[0], "%rax");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -302,25 +334,24 @@ generate_node ( node_t *node)
|
|||||||
generate_function_return(node);
|
generate_function_return(node);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
case IF_STATEMENT:
|
case IF_STATEMENT:
|
||||||
generate_if_statement(node);
|
generate_if_statement(node);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WHILE_STATEMENT:
|
case WHILE_STATEMENT:
|
||||||
/* DO NOTHING YET */
|
generate_while_statement(node);
|
||||||
break;
|
|
||||||
case NULL_STATEMENT:
|
|
||||||
/* USED IN WHILE */
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case NULL_STATEMENT:
|
||||||
|
solve_continue_statement();
|
||||||
|
break;
|
||||||
|
|
||||||
case DECLARATION_LIST:
|
case DECLARATION_LIST:
|
||||||
/* List of blocks we dont need to traverse */
|
/* List of blocks we dont need to traverse */
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
// Otherwise, generate the traverse
|
||||||
for (int c = 0; c < node->n_children; c++)
|
for (int c = 0; c < node->n_children; c++)
|
||||||
generate_node(node->children[c]);
|
generate_node(node->children[c]);
|
||||||
break;
|
break;
|
||||||
@@ -329,10 +360,11 @@ generate_node ( node_t *node)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Generate the print node
|
||||||
void
|
void
|
||||||
generate_print(node_t* node)
|
generate_print(node_t* node)
|
||||||
{
|
{
|
||||||
// Push rdi and rsi to stack incase there are data in them
|
|
||||||
for (uint64_t p = 0; p < node->n_children; p++)
|
for (uint64_t p = 0; p < node->n_children; p++)
|
||||||
{
|
{
|
||||||
node_t *curr_print = node->children[p];
|
node_t *curr_print = node->children[p];
|
||||||
@@ -357,14 +389,13 @@ generate_print(node_t* node)
|
|||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//ASM(movq, $0, %rax);
|
|
||||||
COMMENT("printf call");
|
COMMENT("printf call");
|
||||||
ASM(call, printf);
|
ASM(call, printf);
|
||||||
|
|
||||||
}
|
}
|
||||||
// Adds a newline
|
// Adds a newline
|
||||||
ASM(movq, $'\n', %rdi);
|
ASM(movq, $'\n', %rdi);
|
||||||
//ASM(movq, $0, %rax);
|
|
||||||
ASM(call, putchar);
|
ASM(call, putchar);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -375,7 +406,11 @@ fetch_variable(node_t *node, const char* dest)
|
|||||||
{
|
{
|
||||||
printf("\tmovq\t");
|
printf("\tmovq\t");
|
||||||
generate_var_ident(node);
|
generate_var_ident(node);
|
||||||
printf(", %s\t\t# Fetched: %s\n", dest, node->entry->name);
|
printf(", %s\t\t# Fetched: %s and put in %s\n",
|
||||||
|
dest,
|
||||||
|
node->entry->name,
|
||||||
|
dest
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This will put the value in dest to the var in node
|
// This will put the value in dest to the var in node
|
||||||
@@ -384,10 +419,16 @@ writeback_variable(node_t *node, char* src)
|
|||||||
{
|
{
|
||||||
printf("\tmovq\t%s,", src);
|
printf("\tmovq\t%s,", src);
|
||||||
generate_var_ident(node);
|
generate_var_ident(node);
|
||||||
printf("\t\t# Writeback: %s\n", node->entry->name);
|
printf("\t\t# Writeback '%s' from %s\n",
|
||||||
|
node->entry->name,
|
||||||
|
src
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate variable identifier,
|
||||||
|
// if local var -> find offset from sb
|
||||||
|
// if parameter -> find offset from sb
|
||||||
|
// if global -> insert global tag
|
||||||
void
|
void
|
||||||
generate_var_ident(node_t *node)
|
generate_var_ident(node_t *node)
|
||||||
{
|
{
|
||||||
@@ -404,7 +445,7 @@ generate_var_ident(node_t *node)
|
|||||||
printf("%ld(%%rbp)", -8 * (ident_sym->seq + 1));
|
printf("%ld(%%rbp)", -8 * (ident_sym->seq + 1));
|
||||||
else
|
else
|
||||||
// This requires that the parameters on
|
// This requires that the parameters on
|
||||||
// stack is in reversed order... easier to implement
|
// stack is in reversed order. easier to implement...
|
||||||
printf("%ld(%%rbp)", 8 * (ident_sym->seq - 6 + 1 ));
|
printf("%ld(%%rbp)", 8 * (ident_sym->seq - 6 + 1 ));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -416,7 +457,7 @@ generate_var_ident(node_t *node)
|
|||||||
|
|
||||||
|
|
||||||
// This should allways push the result to stack
|
// This should allways push the result to stack
|
||||||
// no no no no, it should leave it in rax
|
// no no no noooo, it should leave it in rax! As it does :))))
|
||||||
void
|
void
|
||||||
solve_expressions(node_t *node)
|
solve_expressions(node_t *node)
|
||||||
{
|
{
|
||||||
@@ -444,6 +485,7 @@ solve_expressions(node_t *node)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 1:
|
case 1:
|
||||||
solve_expressions(node->children[0]);
|
solve_expressions(node->children[0]);
|
||||||
|
|
||||||
@@ -502,22 +544,37 @@ generate_function_call(node_t *node)
|
|||||||
|
|
||||||
// If the stack is 16 byte alligned here, offset
|
// If the stack is 16 byte alligned here, offset
|
||||||
// by 1 because call pushes return addr to stack
|
// by 1 because call pushes return addr to stack
|
||||||
|
// Therefore after call, the stack is 16 byte aligned
|
||||||
if (isStack16ByteAligned)
|
if (isStack16ByteAligned)
|
||||||
PUSH($0);
|
PUSH($0);
|
||||||
|
|
||||||
|
// Arg list is allways the second children, lies within a parameter list
|
||||||
|
// Type of this is therefore PARAMETER_LIST
|
||||||
node_t *arg_list = node->children[1];
|
node_t *arg_list = node->children[1];
|
||||||
if (arg_list->n_children)
|
if (arg_list->n_children)
|
||||||
arg_list = arg_list->children[0];
|
arg_list = arg_list->children[0]; // This is the acutal parameter list
|
||||||
|
|
||||||
for (int arg = 0; arg < MIN(NO_REG_RECORD, arg_list->n_children); arg++)
|
for (int arg = 0; arg < MIN(NO_REG_RECORD, arg_list->n_children); arg++)
|
||||||
{
|
{
|
||||||
if (arg_list->children[arg]->type == NUMBER_DATA)
|
switch (arg_list->children[arg]->type)
|
||||||
|
{
|
||||||
|
case NUMBER_DATA:
|
||||||
printf("\tmovq\t$%ld, %s\n",
|
printf("\tmovq\t$%ld, %s\n",
|
||||||
*(int64_t*)arg_list->children[arg]->data,
|
*(int64_t*)arg_list->children[arg]->data,
|
||||||
record[arg]
|
record[arg]
|
||||||
);
|
);
|
||||||
else
|
break;
|
||||||
|
|
||||||
|
case EXPRESSION:
|
||||||
|
solve_expressions(arg_list->children[arg]);
|
||||||
|
printf("\tmovq\t%%rax, %s\n",record[arg]);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
fetch_variable(arg_list->children[arg], record[arg]);
|
fetch_variable(arg_list->children[arg], record[arg]);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arg_list->n_children > NO_REG_RECORD)
|
if (arg_list->n_children > NO_REG_RECORD)
|
||||||
@@ -528,19 +585,30 @@ generate_function_call(node_t *node)
|
|||||||
|
|
||||||
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
|
||||||
{
|
{
|
||||||
if (arg_list->children[arg]->type == NUMBER_DATA)
|
switch (arg_list->children[arg]->type)
|
||||||
|
{
|
||||||
|
case NUMBER_DATA:
|
||||||
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
|
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
|
||||||
*(int64_t*)arg_list->children[arg]->data,
|
*(int64_t*)arg_list->children[arg]->data,
|
||||||
++stack_depth
|
++stack_depth
|
||||||
);
|
);
|
||||||
else
|
break;
|
||||||
{
|
|
||||||
|
// Arg can be an expression
|
||||||
|
case EXPRESSION:
|
||||||
|
solve_expressions(arg_list->children[arg]);
|
||||||
|
PUSH(%rax);
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
printf("\tpushq\t");
|
printf("\tpushq\t");
|
||||||
generate_var_ident(arg_list->children[arg]);
|
generate_var_ident(arg_list->children[arg]);
|
||||||
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
|
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
|
||||||
@@ -548,6 +616,7 @@ generate_function_call(node_t *node)
|
|||||||
// Aaaand pop the stack to return back to stack alignment
|
// Aaaand pop the stack to return back to stack alignment
|
||||||
if (isStack16ByteAligned)
|
if (isStack16ByteAligned)
|
||||||
POP(%rcx);
|
POP(%rcx);
|
||||||
|
|
||||||
printf("# End of function call\n");
|
printf("# End of function call\n");
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -571,18 +640,14 @@ solve_statements(node_t *node, char *operator)
|
|||||||
writeback_variable(node->children[0], "%rax");
|
writeback_variable(node->children[0], "%rax");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Takes in a relation/number node and sets %rax to true if the statement is true
|
||||||
void
|
void
|
||||||
generate_if_statement(node_t *node)
|
solve_relations(node_t *relation_root)
|
||||||
{
|
{
|
||||||
uint64_t current_if_seq = if_seq++;
|
|
||||||
|
|
||||||
COMMENT("Begin IF %ld", current_if_seq);
|
|
||||||
|
|
||||||
node_t *relation_root = node->children[0];
|
|
||||||
|
|
||||||
switch (relation_root->type)
|
switch (relation_root->type)
|
||||||
{
|
{
|
||||||
case NUMBER_DATA:
|
case NUMBER_DATA:
|
||||||
|
// Numberdata is boring, just leave value in %rax, only 1 is interperted as true
|
||||||
solve_expressions(relation_root);
|
solve_expressions(relation_root);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -603,47 +668,98 @@ generate_if_statement(node_t *node)
|
|||||||
POP(%rax);
|
POP(%rax);
|
||||||
|
|
||||||
ASM(cmp, %r10, %rax);
|
ASM(cmp, %r10, %rax);
|
||||||
|
ASM(movq, $0, %rax);
|
||||||
|
|
||||||
switch (*(char*)relation_root->data)
|
switch (*(char*)relation_root->data)
|
||||||
{
|
{
|
||||||
case '=':
|
case '=': ASM(sete, %al); break; // Set %al (0th byte of %rax) to 1 if lhs == rhs
|
||||||
ASM(sete, %rax);
|
case '>': ASM(setg, %al); break; // Set %al (0th byte of %rax) to 1 if lhs > rhs
|
||||||
break;
|
case '<': ASM(setl, %al); break; // Set %al (0th byte of %rax) to 1 if lhs < rhs
|
||||||
case '>':
|
|
||||||
ASM(setg, %rax);
|
|
||||||
break;
|
|
||||||
case '<':
|
|
||||||
ASM(setl, %rax);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ASM(cmp, $1, %rax);
|
void
|
||||||
printf("\tjne\t%s%03ld\n", (node->n_children > 2) ? "ELSE" : "ENDIF", current_if_seq);
|
generate_if_statement(node_t *node)
|
||||||
solve_expressions(node->children[1]);
|
{
|
||||||
|
uint64_t current_if_seq = if_seq++;
|
||||||
|
|
||||||
if (node->n_children > 2) {
|
COMMENT("Begin IF %ld", current_if_seq);
|
||||||
|
|
||||||
|
// The realtion is allways in the first part of the IF
|
||||||
|
solve_relations(node->children[0]);
|
||||||
|
|
||||||
|
// Compare to 0 (False)
|
||||||
|
ASM(cmp, $0, %rax);
|
||||||
|
// If False, jump to either ELSE or ENDIF based on no children in if
|
||||||
|
printf("\tje \t%s%03ld\n", (node->n_children > 2) ? "ELSE" : "ENDIF", current_if_seq);
|
||||||
|
|
||||||
|
// Then generate body
|
||||||
|
generate_node(node->children[1]);
|
||||||
|
|
||||||
|
// If else block aswell, add that
|
||||||
|
if (node->n_children > 2)
|
||||||
|
{
|
||||||
printf("\tjmp \tENDIF%03ld\n", current_if_seq);
|
printf("\tjmp \tENDIF%03ld\n", current_if_seq);
|
||||||
printf("ELSE%03ld:\n", current_if_seq);
|
printf("ELSE%03ld:\n", current_if_seq);
|
||||||
solve_expressions(node->children[2]);
|
|
||||||
|
// Generate ELSE body
|
||||||
|
generate_node(node->children[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("ENDIF%03ld:\n", current_if_seq);
|
|
||||||
|
|
||||||
COMMENT("End IF %ld", current_if_seq);
|
COMMENT("End IF %ld", current_if_seq);
|
||||||
|
printf("ENDIF%03ld:\n", current_if_seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
generate_while_statement(node_t *node)
|
generate_while_statement(node_t *node)
|
||||||
{
|
{
|
||||||
|
// Keep local var of which WHILE this is
|
||||||
|
uint64_t current_while_seq = while_seq++;
|
||||||
|
|
||||||
|
// Also keep the previous closest while
|
||||||
|
// in case a continue happens after
|
||||||
|
// this (and this is inside an another while loop)
|
||||||
|
uint64_t prev_closest_while = closest_while;
|
||||||
|
closest_while = current_while_seq;
|
||||||
|
|
||||||
|
COMMENT("Begin WHILE %ld", current_while_seq);
|
||||||
|
printf("WHILE%03ld:\n", current_while_seq);
|
||||||
|
|
||||||
|
// Relation is allways the first entry in a while
|
||||||
|
solve_relations(node->children[0]);
|
||||||
|
|
||||||
|
// Compare to 0 (False)
|
||||||
|
ASM(cmp, $0, %rax);
|
||||||
|
// If false, then exit while
|
||||||
|
printf("\tje \tENDWHILE%03ld\n", current_while_seq);
|
||||||
|
|
||||||
|
// Generate body
|
||||||
|
generate_node(node->children[1]);
|
||||||
|
|
||||||
|
// Restore the previous while
|
||||||
|
closest_while = prev_closest_while;
|
||||||
|
|
||||||
|
printf("\tjmp \tWHILE%03ld\n", current_while_seq);
|
||||||
|
printf("ENDWHILE%03ld:\n", current_while_seq);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
solve_continue_statement()
|
solve_continue_statement()
|
||||||
{
|
{
|
||||||
|
COMMENT("Continue to WHILE%03ld", closest_while);
|
||||||
|
printf("\tjmp \tWHILE%03ld\n", closest_while);
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint64_t
|
||||||
|
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
|
||||||
|
{
|
||||||
|
uint64_t no_symbols = tlhash_size(symbol_table);
|
||||||
|
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
|
||||||
|
tlhash_values(symbol_table, (void **)*symbol_list );
|
||||||
|
|
||||||
|
return no_symbols;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**Generates the main function with argument parsing and calling of our
|
/**Generates the main function with argument parsing and calling of our
|
||||||
@@ -710,12 +826,4 @@ generate_main ( symbol_t *first )
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t
|
|
||||||
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
|
|
||||||
{
|
|
||||||
uint64_t no_symbols = tlhash_size(symbol_table);
|
|
||||||
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
|
|
||||||
tlhash_values(symbol_table, (void **)*symbol_list );
|
|
||||||
|
|
||||||
return no_symbols;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -219,6 +219,11 @@ relation:
|
|||||||
| expression '>' expression {
|
| expression '>' expression {
|
||||||
$$ = NODE(RELATION, strdup(">"), 2, $1, $3);
|
$$ = NODE(RELATION, strdup(">"), 2, $1, $3);
|
||||||
}
|
}
|
||||||
|
/* This can actually be extented with the following (with some minor tweaks to the generator.c)
|
||||||
|
| expression {
|
||||||
|
$$ = NODE(RELATION, NULL, 1, $1);
|
||||||
|
}
|
||||||
|
That will allow to have "if 1 then" or "while 1 do" */
|
||||||
;
|
;
|
||||||
|
|
||||||
expression:
|
expression:
|
||||||
|
|||||||
@@ -28,4 +28,6 @@ func main() begin
|
|||||||
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
||||||
print "True!"
|
print "True!"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
end
|
end
|
||||||
57
exercises/06/vslc/vsl_programs/ps3-simplify/while_test.vsl
Normal file
57
exercises/06/vslc/vsl_programs/ps3-simplify/while_test.vsl
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// This program is a simple test of while loops, counting down from 19 to 0
|
||||||
|
// and skipping 10 (if continue is implemented)
|
||||||
|
|
||||||
|
func while_test ()
|
||||||
|
begin
|
||||||
|
var a, b
|
||||||
|
a := 20
|
||||||
|
|
||||||
|
b := test_while()
|
||||||
|
|
||||||
|
print a
|
||||||
|
|
||||||
|
|
||||||
|
if a > 0 then print "foobar"
|
||||||
|
while a > 0 do
|
||||||
|
begin
|
||||||
|
if a = 10 then
|
||||||
|
begin
|
||||||
|
a -= 1
|
||||||
|
print "Skip..."
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
else
|
||||||
|
a -= 1
|
||||||
|
print a
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
func test_while()
|
||||||
|
begin
|
||||||
|
var n, m
|
||||||
|
|
||||||
|
n := 4
|
||||||
|
m := 21
|
||||||
|
|
||||||
|
while n > 0 do
|
||||||
|
begin
|
||||||
|
n -= 1
|
||||||
|
if n = 2 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
while m > 0 do
|
||||||
|
begin
|
||||||
|
m -= 1
|
||||||
|
|
||||||
|
if m = 10 then
|
||||||
|
continue
|
||||||
|
|
||||||
|
print n, m
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
|
end
|
||||||
35
exercises/06/vslc/vsl_programs/ps6-codegen2/lists.vsl
Normal file
35
exercises/06/vslc/vsl_programs/ps6-codegen2/lists.vsl
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
|
||||||
|
func my_func(a, b, c, d, e, f, g, h) begin
|
||||||
|
var i, j, k, l, m
|
||||||
|
|
||||||
|
i := a + b + d
|
||||||
|
|
||||||
|
print i, f
|
||||||
|
|
||||||
|
if i = f then begin
|
||||||
|
print "hmmm"
|
||||||
|
end
|
||||||
|
|
||||||
|
if 1 < 2 then begin
|
||||||
|
print "Whaa"
|
||||||
|
end
|
||||||
|
else begin
|
||||||
|
print "Whooo"
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return i
|
||||||
|
end
|
||||||
|
|
||||||
|
func main() begin
|
||||||
|
var n, o, p, q, r, s, t, u, v, w
|
||||||
|
n := 5
|
||||||
|
n += my_func(1, 2, 3, 5, 8, 13, 21, 34)
|
||||||
|
|
||||||
|
if my_func(1, 2, 3, 5, 8, 13, 21, 34) > 3 then begin
|
||||||
|
print "True!"
|
||||||
|
end
|
||||||
|
|
||||||
|
return 0
|
||||||
|
end
|
||||||
@@ -3,9 +3,16 @@
|
|||||||
|
|
||||||
func while_test ()
|
func while_test ()
|
||||||
begin
|
begin
|
||||||
var a
|
var a, b
|
||||||
a := 20
|
a := 20
|
||||||
|
|
||||||
|
b := test_while()
|
||||||
|
|
||||||
|
print "Loops done in test_while:", b
|
||||||
|
|
||||||
print a
|
print a
|
||||||
|
|
||||||
|
|
||||||
if a > 0 then print "foobar"
|
if a > 0 then print "foobar"
|
||||||
while a > 0 do
|
while a > 0 do
|
||||||
begin
|
begin
|
||||||
@@ -21,3 +28,56 @@ begin
|
|||||||
end
|
end
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
func test_while()
|
||||||
|
begin
|
||||||
|
var n, m, loops_done
|
||||||
|
|
||||||
|
m := 10
|
||||||
|
n := 5
|
||||||
|
|
||||||
|
loops_done := 0
|
||||||
|
|
||||||
|
|
||||||
|
while m > 0 do
|
||||||
|
begin
|
||||||
|
|
||||||
|
if m = 7 then
|
||||||
|
begin
|
||||||
|
m -= 1
|
||||||
|
print "Skipping loop 7"
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
|
n := 5
|
||||||
|
while n > 0 do
|
||||||
|
begin
|
||||||
|
|
||||||
|
|
||||||
|
if n = 3 then
|
||||||
|
begin
|
||||||
|
n -= 1
|
||||||
|
print "-------Skip 3 in inner loop"
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
loops_done += 1
|
||||||
|
print "loop:", loops_done, "Values:", m, n
|
||||||
|
n -= 1
|
||||||
|
end
|
||||||
|
|
||||||
|
if m = 5 then
|
||||||
|
begin
|
||||||
|
m -= 1
|
||||||
|
print "<<<<<<Skip after exit of inner while"
|
||||||
|
continue
|
||||||
|
end
|
||||||
|
|
||||||
|
print "######## Loop", m, "done"
|
||||||
|
m -= 1
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
return loops_done
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user