Compare commits

...

61 Commits

Author SHA1 Message Date
53ec113f81 Added an extention to the relation 2022-04-23 23:08:12 +02:00
a7e5535dde Added comments, the continue worked 2022-04-23 23:07:52 +02:00
958dfe51fd added while in a while with some continue tests 2022-04-23 23:07:38 +02:00
75c8e5dc68 working while, not sure of the continue 2022-04-22 18:41:50 +02:00
77376dc1ef Added more debug vsl 2022-04-22 18:40:36 +02:00
57cc3f62fc added debug launcher 2022-04-22 18:39:58 +02:00
2a3c76cb6e Added some more test files 2022-04-22 00:03:10 +02:00
9787419800 Beginnings of if 2022-04-22 00:02:52 +02:00
419f753847 Somewhat working, pre leave rax on stack 2022-04-21 21:20:40 +02:00
ef76abe453 added clean 2022-04-21 21:17:54 +02:00
2c8c1cbe6a missing return 2022-04-21 21:17:49 +02:00
ea8beae4c5 added my code from ps5 2022-04-10 16:03:03 +02:00
c9eacbfdfc init ps6 2022-04-10 15:55:56 +02:00
2a9aa13058 added gdb to trask 2022-04-03 23:41:54 +02:00
56119f44f4 cant decide 2022-04-03 22:54:38 +02:00
79653a3b84 fixed typo 2022-04-03 22:52:21 +02:00
ece2d9b83a fixed a bug with removing a node it shouldn't 2022-04-03 22:42:13 +02:00
83479f6a2d Added some NULLs 2022-04-03 22:41:49 +02:00
454d642f80 Generates all the nesessary code for running
- can compile simple stuff
2022-04-03 22:41:37 +02:00
1594d045fd Debugger for vscode 2022-04-03 22:40:49 +02:00
250a517768 Cheatsheet 2022-04-03 22:40:40 +02:00
521e412db2 Added files to debug acoss ps 2022-04-03 22:40:33 +02:00
63a5efae3c more continious examples 2022-04-02 20:15:06 +02:00
3d68d7227c started function body 2022-04-02 20:14:57 +02:00
9583009760 added some more test files to vsl 2022-03-31 23:12:42 +02:00
b6a3b145e0 clean before recompile 2022-03-31 23:12:24 +02:00
36680f5c8d Finished two of the generator parts
- string table
-global vars

beginnings of function
2022-03-31 23:12:12 +02:00
7b0cf372c8 Added some outfiles 2022-03-31 23:11:17 +02:00
71f757ecdc Moved my files into project 2022-03-31 21:33:52 +02:00
9f31718671 added some presentations 2022-03-31 21:24:06 +02:00
d266ac400e Init ps5 2022-03-31 21:23:57 +02:00
7c3e5aaefa Added own submission 2022-03-19 13:41:37 +01:00
8dfa078c0f Added syntax highlighter 2022-03-19 13:41:29 +01:00
c857bb68e8 Fixed state 2022-03-18 22:49:07 +01:00
d042cd00fb Removed comment 2022-03-18 22:48:22 +01:00
8e8f509e44 Added a lot of documentation 2022-03-18 22:48:09 +01:00
4793e4e934 Changed the print of globals 2022-03-18 19:47:32 +01:00
a92ee8c187 Added pygments lexer 2022-03-18 19:47:18 +01:00
fab7d8916e Added lookup of vars, start commenting 2022-03-17 23:14:24 +01:00
398bdc9487 Fixed some errors in the vsl programs 2022-03-17 23:13:55 +01:00
e839190fbf Added strings, and cleaned up 2022-03-17 22:00:38 +01:00
d80961ec56 Start bind_names and declarations 2022-03-17 21:40:41 +01:00
06bca1fa00 Cleaned up ir.c 2022-03-17 21:40:18 +01:00
b56eb1a707 Added print and destroy symbol table, start bind_names 2022-03-16 22:28:17 +01:00
6b87321671 Removed helpers and moved to ir.c 2022-03-16 22:27:42 +01:00
78d4161e4d Added header for helpers 2022-03-16 22:26:25 +01:00
b0f3da6f0d Added default scope stack size 2022-03-16 22:25:32 +01:00
bef1e015f6 Added scanning of global variables 2022-03-15 22:29:45 +01:00
fb6c35523e Added some constant values for sizes 2022-03-15 22:29:29 +01:00
a0c695ef66 Removed unoptimized print 2022-03-15 22:29:14 +01:00
2734cdb176 fixed bug with being to aggressive
- Is now more flexible
- Added resolve constant relations
2022-03-13 20:20:35 +01:00
e29492f7f8 Cleaned up the ir.h 2022-03-13 20:19:53 +01:00
b2ba4768c6 added some constant relations 2022-03-13 20:19:39 +01:00
5d866e48a0 Added a pdf from the year 21 2022-03-13 20:19:29 +01:00
b8255e4e93 added some comments 2022-03-12 19:32:10 +01:00
c45131d029 print out what relation is used 2022-03-12 19:26:50 +01:00
a97b6602a0 fixed bug with being to agressive 2022-03-12 19:26:35 +01:00
fe49453d0a added var to function call 2022-03-12 19:22:14 +01:00
40c9090656 added flags to output 2022-03-12 19:22:05 +01:00
94d83fc59b fixed bug where the simplify is to aggressive 2022-03-12 19:21:53 +01:00
a5f6aaf1fa added my own from ps3 2022-03-12 19:21:38 +01:00
100 changed files with 8084 additions and 297 deletions

View File

@@ -154,8 +154,8 @@ prune_children(node_t **simplified, node_t *root)
switch (root->type)
{
case GLOBAL:
case ARGUMENT_LIST:
case PARAMETER_LIST:
//case ARGUMENT_LIST:
//case PARAMETER_LIST:
case STATEMENT:
case PRINT_ITEM:
case PRINT_STATEMENT:
@@ -287,7 +287,6 @@ flatten(node_t **simplified, node_t *root)
*simplified = result;
}
static void
simplify_tree ( node_t **simplified, node_t *root )
{
@@ -302,4 +301,4 @@ simplify_tree ( node_t **simplified, node_t *root )
flatten(&root, root);
*simplified = root;
}
}

Binary file not shown.

Binary file not shown.

View File

@@ -10,21 +10,42 @@ typedef struct n {
struct n **children;
} node_t;
// Export the initializer function, it is needed by the parser
void node_init (
node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
/**Export the initializer function, it is needed by the parser
* @param *nd node to initialize
* @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 {
SYM_GLOBAL_VAR, SYM_FUNCTION, SYM_PARAMETER, SYM_LOCAL_VAR
SYM_GLOBAL_VAR,
SYM_FUNCTION,
SYM_PARAMETER,
SYM_LOCAL_VAR
} symtype_t;
typedef struct s {
char *name;
symtype_t type;
node_t *node;
size_t seq;
size_t nparms;
tlhash_t *locals;
char* name;
symtype_t type;
node_t* node;
size_t seq;
size_t nparms;
tlhash_t* locals;
} symbol_t;
#endif
#define GLOBAL_BUCKET_SIZE 32
#define LOCAL_BUCKET_SIZE 16
#define DEFAULT_STRING_LIST_SIZE 8
#define DEFAULT_NO_SCOPES 1

View File

@@ -25,6 +25,7 @@ int yyerror ( const char *error );
/* These are defined in the parser generated by bison */
extern int yylineno;
extern int yylex ( void );
extern int yylex_destroy( void );
extern char yytext[];
/* Global state */

View File

@@ -1,76 +1,602 @@
#include <vslc.h>
#define ERRPRT(format, args...) {fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args);}
// Externally visible, for the generator
extern tlhash_t *global_names;
extern char **string_list;
extern size_t n_string_list, stringc;
// Implementation choices, only relevant internally
static void find_globals ( void );
/** @param function Function's symbol table entry
* @param root Function's root node */
// Functions from the skeleton
static uint64_t find_globals ( void );
static void bind_names ( symbol_t *function, node_t *root );
// Helper functions, see description in the definition
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
create_symbol_table ( void )
{
/* TODO: traverse the syntax tree and create the symbol table */
// ! Example code solely to demonstrate usage of tlhash. Make sure to remove
// ! or comment this out when implementing your solution.
// Initialize string array
n_string_list = DEFAULT_STRING_LIST_SIZE;
string_list = malloc(n_string_list * sizeof(char*));
stringc = 0;
// Initialize table
tlhash_t *my_table = (tlhash_t*)malloc(sizeof(tlhash_t));
tlhash_init(my_table, 64);
// Initialize scope array
no_scopes = DEFAULT_NO_SCOPES;
scopes = malloc(no_scopes * sizeof(tlhash_t));
cur_scope_depth = 0;
char *my_key0 = "key"; // Keep in mind that these are stack allocated for simplicity, yours should not
char *my_val0 = "valuable"; // Also, I'm using strings as values, you will be using symbol_t pointers
char *my_key1 = "another_key";
char *my_val1 = "more valuable";
// Insert some values to the table. Remember that the length of a string
// interpreted as an array is the string length plus one '\0' character
tlhash_insert(my_table, my_key0, strlen(my_key0)+1, my_val0);
tlhash_insert(my_table, my_key1, strlen(my_key1)+1, my_val1);
// Traverse the root node for globals
uint64_t no_globals = find_globals();
// Iterate keys and lookup their values
size_t size = tlhash_size(my_table);
char **keys = (char **)malloc(size);
tlhash_keys(my_table, keys);
char *val;
for (int i = 0; i < size; i++) {
tlhash_lookup(my_table, keys[i], strlen(keys[i])+1, &val);
printf("my_table[%s] = \"%s\"\n", keys[i], val);
// 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 allocated memory when done with the symbol table
tlhash_finalize(my_table);
free(my_table);
free(keys);
// Free the temp list
free(global_list);
}
/**
* Prints the symbol table and the string array
*
* Used in vslc.c
*/
void
print_symbol_table ( void )
{
/* TODO: output its contents */
/* 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
destroy_symbol_table ( void )
{
/* TODO: release memory allocated to the symbol table */
// 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);
// FREE SCOPES
// At the end of program, all scopes have to be popped
// Therefore only free the list
free(scopes);
// FREE GLOBAL NAMES
if (!global_names)
return;
// Fetch list of globals
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 );
// Destroy all global elements
for (uint64_t g = 0; g < no_globals; g++)
destroy_global(global_list[g]);
// Destory the global hash table
tlhash_finalize(global_names);
// Free the global hash table
free(global_names);
// Free the temp list
free(global_list);
}
void
/**
* Destroys the supplied global symbol by
* finalizing each of the local tables
*
* @param global pointer to the global symbol to be destroyed
*/
static void
destroy_global(symbol_t* global)
{
if (!global)
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);
}
/**
* 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 )
{
/* TODO: Populate symbol table with global variables and functions */
tlhash_init(global_names = malloc(sizeof(tlhash_t)), GLOBAL_BUCKET_SIZE);
uint64_t no_functions = 0, no_global_vars = 0;
node_t *global_list = root;
// Check if not nullptr
if (!global_list)
return 0;
symbol_t* global_symbol;
for (uint64_t global_i = 0; global_i < global_list->n_children; global_i++)
{
node_t *current_global = global_list->children[global_i];
switch (current_global->type)
{
case VARIABLE_LIST:
// Go through the variable list and get all the global variables
for (uint64_t var_i = 0; var_i < current_global->n_children; var_i++)
{
global_symbol = malloc(sizeof(symbol_t));
*global_symbol = (symbol_t){
.type = SYM_GLOBAL_VAR,
.name = current_global->children[var_i]->data,
.node = current_global->children[var_i],
.seq = 0,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_names, global_symbol);
no_global_vars++;
}
break;
case FUNCTION:
node_t *function = current_global;
// Function node allways have the same structure,
// [0] are the identifier
// [1] are the variable list, within a paramerer_list
// [2] are the actual block
if (!function->children[0])
break;
// Create the function symbol
global_symbol = malloc(sizeof(symbol_t));
*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++)
{
param_sym = malloc(sizeof(symbol_t));
*param_sym = (symbol_t){
.type = SYM_PARAMETER,
.name = param_list->children[param_i]->data,
.node = param_list->children[param_i],
.seq = param_i,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_symbol->locals, param_sym);
}
break;
}
}
return no_functions + no_global_vars;
}
/**
* Inserts a symbol into a hash table, key is defined in the name field in the symbol supplied.
*
* @param hash_table pointer to the hash table the symbol is inserted into
* @param symbol pointer to the symbol to be inserted
*/
void
bind_names ( symbol_t *function, node_t *root )
insert_symbol(tlhash_t *hash_table, symbol_t* symbol)
{
/* TODO: Bind names and string literals in local symbol table */
tlhash_insert(hash_table, symbol->name, strlen(symbol->name), symbol);
}
/**
* @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);
}
break;
// If STRING_DATA, collect the string and point the
// data in the corresponding node to the array index
case STRING_DATA:
collect_string(root);
break;
// If none of the above, go deeper if possible.
default:
for (uint64_t i = 0; i < root->n_children; i++)
bind_names(function, root->children[i]);
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
insert_local_to_func(symbol_t *function, symbol_t *local)
{
tlhash_insert(
function->locals, //! Insert local to the function var table
&local->seq, //! The key is a number, unique, strictly growing
sizeof(local->seq), //! Size of key
local //! The local symbol
);
}
/**
* Collects strings to the string array and
* 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)
{
n_string_list *= 2;
char **new_string_list = realloc(string_list, n_string_list * sizeof(char*));
if (!new_string_list)
{
ERRPRT("Could not realloc string list!\n");
exit(EXIT_FAILURE);
}
string_list = new_string_list;
}
}
/**
* 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;
}

View File

@@ -1,176 +1,303 @@
%{
#include <vslc.h>
#define N0C(n,t,d) do { \
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 )
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
%}
%left '|'
%left '^'
%left '&'
%define api.value.type {node_t}
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%token VAR NUMBER IDENTIFIER STRING
%left '|' '&' '^'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%right '~'
%expect 1
//%expect 1
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%token VAR NUMBER IDENTIFIER STRING
%nonassoc IF THEN
%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 :
global_list { N1C ( root, PROGRAM, NULL, $1 ); }
;
global_list :
global { N1C ( $$, GLOBAL_LIST, NULL, $1 ); }
| global_list global { N2C ( $$, GLOBAL_LIST, NULL, $1, $2 ); }
;
program:
global_list {
root = NODE(PROGRAM, NULL, 1, $1);
}
;
global_list:
global {
$$ = NODE(GLOBAL_LIST, NULL, 1, $1);
}
| global_list global {
$$ = NODE(GLOBAL_LIST, NULL, 2, $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 ); }
;
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
{ 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) ); }
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

View File

@@ -12,6 +12,10 @@ tree_print(node_t* root, stem head);
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 */
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, ...)
{
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++ )
nd->children[i] = va_arg ( child_list, node_t * );
va_end ( child_list );
return nd;
}
@@ -82,9 +89,10 @@ tree_print(node_t* root, stem head)
return;
}
printf("─%s", node_string[root->type]);
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION )
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION ||
root->type == RELATION)
printf("(%s)", (char *) root->data);
else if (root->type == NUMBER_DATA)
printf("(%ld)", *((int64_t *)root->data));
@@ -152,81 +160,237 @@ destroy_subtree ( node_t *discard )
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;
/* Simplify subtrees before examining this node */
for ( uint64_t i=0; i<root->n_children; i++ )
simplify_tree ( &root->children[i], root->children[i] );
/* Do this recursivly */
for (int i = 0; i < root->n_children; i++)
flatten(&root->children[i], root->children[i]);
node_t *discard, *result = root;
switch ( root->type )
node_t **new_children, *result = root;
switch (root->type)
{
/* Structures of purely syntactic function */
case PARAMETER_LIST: case ARGUMENT_LIST:
case STATEMENT: case PRINT_ITEM: case GLOBAL:
result = root->children[0];
node_finalize ( root );
case GLOBAL_LIST:
case STATEMENT_LIST:
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;
case PRINT_STATEMENT:
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);
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:
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;
node_finalize(root);
break;
}
*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)
{
case 1:
result = root->children[0];
if (root->data &&
result->type == NUMBER_DATA &&
result->data)
{
switch (*((char*)root->data))
{
case '-':
*((int64_t*)result->data) *= -1;
break;
case '~':
*((int64_t*)result->data) = ~*((int64_t*)result->data);
break;
}
}
node_finalize(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);
/* 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;
case EXPRESSION:
switch ( root->n_children )
{
case 1:
if ( root->children[0]->type == NUMBER_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) )
{
case '+': *x += *y; break;
case '-': *x -= *y; break;
case '*': *x *= *y; break;
case '/': *x /= *y; break;
}
node_finalize ( root->children[1] );
node_finalize ( root );
}
break;
}
}
break;
}
*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;
}

View File

@@ -6,19 +6,19 @@
/* Global state */
node_t *root; // Syntax tree
tlhash_t *global_names; // Symbol table
char **string_list; // List of strings in the source
size_t n_string_list = 8; // Initial string list capacity (grow on demand)
size_t stringc = 0; // Initial string count
node_t *root; // Syntax tree
tlhash_t *global_names; // Symbol table
char **string_list; // List of strings in the source
size_t n_string_list = 8; // Initial string list capacity (grow on demand)
size_t stringc = 0; // Initial string count
/* Command line option parsing for the main function */
static void options ( int argc, char **argv );
bool
print_full_tree = false,
print_simplified_tree = false,
print_full_tree = false,
print_simplified_tree = false,
print_symbol_table_contents = false,
new_print_style = true;
new_print_style = true;
/* Entry point */
@@ -27,7 +27,8 @@ main ( int argc, char **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 )
print_syntax_tree ();
@@ -35,7 +36,7 @@ main ( int argc, char **argv )
if ( print_simplified_tree )
print_syntax_tree ();
create_symbol_table (); // In ir.c
create_symbol_table (); // In ir.c
if ( print_symbol_table_contents )
print_symbol_table();

View File

@@ -9,8 +9,8 @@ ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES)
%.ast: %.vsl
$(VSLC) -t < $^ > $@
%.ast: %.vsl clean
$(VSLC) -T -s < $^ > $@
clean:
-rm -r */*.ast

View File

@@ -1,6 +1,6 @@
func main() begin
var a
var a, b
a := 1 + 2 + 4 + 5 + 6 + 7 + 8 + 9
b := (10 + 10 * 4) * (2 + 2 * (1 + 1)) / 10 + 2 * 5 + 6 / 3

View File

@@ -1,5 +1,5 @@
func my_fun(a, b, c, d, e, f, g, h) begin
func my_func(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := a + b + d
@@ -7,10 +7,22 @@ func my_fun(a, b, c, d, e, f, g, h) begin
if i = f then begin
print "hmmm"
end
if 1 = 1 then begin
print "true"
end
if 1 = 2 then begin
print "false"
end
if 1 < 2 then begin
print "true"
end
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)
n += my_func(1, 2, 3, 5, 8, 13, 21, w)
end

17
exercises/05/.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
# Buildfiles
*.o
vslc/src/vslc
parser.c
scanner.c
y.tab.h
# Submission
*.tar.xz
# VSL treefiles
*.ast
*.sast
*.sym
*.bin
*.s
*.S

49
exercises/05/.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,49 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch task",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/tasks/10_green_bottles",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
},
{
// for Linux
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/vslc/src/vslc",
"args": ["<", "${workspaceFolder}/vslc/vsl_programs/ps5-codegen1/lists.vsl"],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}

BIN
exercises/05/PS5.pdf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,12 @@
LEX=flex
YACC=bison
YFLAGS+=--defines=src/y.tab.h -o y.tab.c
CFLAGS+=-std=c99 -g -Isrc -Iinclude -D_POSIX_C_SOURCE=200809L -DYYSTYPE="node_t *"
src/vslc: src/vslc.c src/parser.o src/scanner.o src/nodetypes.o src/tree.o src/ir.o src/generator.o src/tlhash.c
src/y.tab.h: src/parser.c
src/scanner.c: src/y.tab.h src/scanner.l
clean:
-rm -f src/parser.c src/scanner.c src/*.tab.* src/*.o
purge: clean
-rm -f src/vslc

View File

@@ -0,0 +1,51 @@
#ifndef IR_H
#define IR_H
/* This is the tree node structure */
typedef struct n {
node_index_t type;
void *data;
struct s *entry;
uint64_t n_children;
struct n **children;
} node_t;
/**Export the initializer function, it is needed by the parser
* @param *nd node to initialize
* @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 {
SYM_GLOBAL_VAR,
SYM_FUNCTION,
SYM_PARAMETER,
SYM_LOCAL_VAR
} symtype_t;
typedef struct s {
char* name;
symtype_t type;
node_t* node;
size_t seq;
size_t nparms;
tlhash_t* locals;
} symbol_t;
#endif
#define GLOBAL_BUCKET_SIZE 32
#define LOCAL_BUCKET_SIZE 16
#define DEFAULT_STRING_LIST_SIZE 8
#define DEFAULT_NO_SCOPES 1

View File

@@ -0,0 +1,37 @@
#ifndef NODETYPES_H
#define NODETYPES_H
typedef enum {
PROGRAM,
GLOBAL_LIST,
GLOBAL,
STATEMENT_LIST,
PRINT_LIST,
EXPRESSION_LIST,
VARIABLE_LIST,
ARGUMENT_LIST,
PARAMETER_LIST,
DECLARATION_LIST,
FUNCTION,
STATEMENT,
BLOCK,
ASSIGNMENT_STATEMENT,
ADD_STATEMENT,
SUBTRACT_STATEMENT,
MULTIPLY_STATEMENT,
DIVIDE_STATEMENT,
RETURN_STATEMENT,
PRINT_STATEMENT,
NULL_STATEMENT,
IF_STATEMENT,
WHILE_STATEMENT,
EXPRESSION,
RELATION,
DECLARATION,
PRINT_ITEM,
IDENTIFIER_DATA,
NUMBER_DATA,
STRING_DATA
} node_index_t;
extern char *node_string[26];
#endif

View File

@@ -0,0 +1,28 @@
#ifndef TLHASH_H
#define TLHASH_H
#include <stddef.h>
typedef struct el {
void *key, *value;
size_t key_length;
struct el *next;
} tlhash_element_t;
typedef struct {
size_t n_buckets, size;
tlhash_element_t **buckets;
} tlhash_t;
int tlhash_init ( tlhash_t *tab, size_t n_buckets );
int tlhash_finalize ( tlhash_t *tab );
int tlhash_insert ( tlhash_t *tab, void *key, size_t keylen, void *val );
int tlhash_lookup ( tlhash_t *tab, void *key, size_t keylen, void **val );
int tlhash_remove ( tlhash_t *tab, void *key, size_t key_length );
size_t tlhash_size ( tlhash_t *tab );
void tlhash_keys ( tlhash_t *tab, void **keys );
void tlhash_values ( tlhash_t *tab, void **values );
#define TLHASH_SUCCESS 0 /* Success */
#define TLHASH_ENOMEM 1 /* No memory available */
#define TLHASH_ENOENT 2 /* No such table entry */
#define TLHASH_EEXIST 3 /* Table entry already exists */
#endif

View File

@@ -0,0 +1,51 @@
#ifndef VSLC_H
#define VSLC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
// Prototypes for the hash table functions
#include "tlhash.h"
// Numbers and names for the types of syntax tree nodes
#include "nodetypes.h"
// Definition of the tree node type
#include "ir.h"
// Token definitions and other things from bison, needs def. of node type
#include "y.tab.h"
/* This is generated from the bison grammar, calls on the flex specification */
int yyerror ( const char *error );
/* These are defined in the parser generated by bison */
extern int yylineno;
extern int yylex ( void );
extern int yylex_destroy( void );
extern char yytext[];
/* Global state */
extern node_t *root;
// Moving global defs to global header
extern tlhash_t *global_names; // Defined in ir.c, used by generator.c
extern char **string_list; // Defined in ir.c, used by generator.c
extern size_t stringc; // Defined in ir.c, used by generator.c
/* Global routines, called from main in vslc.c */
void simplify_syntax_tree ( void );
void print_syntax_tree ( void );
void destroy_syntax_tree ( void );
void create_symbol_table ( void );
void print_symbol_table ( void );
void destroy_symbol_table ( void );
void generate_program ( void );
#endif

View File

@@ -0,0 +1,612 @@
#include <vslc.h>
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
#define LABEL(label) printf("_%s:\n", (char*)label)
#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 );
/**Initializes program (already implemented) */
void generate_main ( symbol_t *first );
#define MIN(a,b) (((a)<(b)) ? (a):(b))
static const char *record[NO_REG_RECORD] = {
"%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
/**
* 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
generate_program ( void )
{
generate_stringtable();
generate_global_variables();
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
generate_stringtable ( void )
{
/* These can be used to emit numbers, strings and a run-time
* error msg. from main
*/
puts("# DATA SECTION");
puts(".data");
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
generate_global_variables ( void )
{
symbol_t **global_list;
uint64_t no_globals = fetch_symbols(global_names, &global_list);
puts("# GLOBAL VARIABLES");
puts(".bss");
puts(".align 8");
for (uint64_t g = 0; g < no_globals; g++) {
if (global_list[g]->type == SYM_GLOBAL_VAR)
printf(".%s:\n", global_list[g]->name);
}
putchar('\n');
free(global_list);
}
void
generate_function ( symbol_t *function )
{
// TODO: Generate code for declaring and entering function, then generate its body
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
generate_main ( symbol_t *first )
{
puts("###### Entry point for GAS #####");
puts ( ".globl main" );
puts ( ".section .text" );
puts ( "main:" );
puts ( "\tpushq %rbp" );
puts ( "\tmovq %rsp, %rbp" );
printf ( "\tsubq\t$1,%%rdi\n" );
printf ( "\tcmpq\t$%zu,%%rdi\n", first->nparms );
printf ( "\tjne \tABORT\n" );
printf ( "\tcmpq\t$0,%%rdi\n" );
printf ( "\tjz \tSKIP_ARGS\n" );
printf ( "\tmovq\t%%rdi,%%rcx\n" );
printf ( "\taddq\t$%zu, %%rsi\n", 8*first->nparms );
printf ( "PARSE_ARGV:\n" );
printf ( "\tpushq\t%%rcx\n" );
printf ( "\tpushq\t%%rsi\n" );
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
printf ( "\tmovq\t$0,%%rsi\n" );
printf ( "\tmovq\t$10,%%rdx\n" );
printf ( "\tcall\tstrtol\n" );
/* Now a new argument is an integer in rax */
printf ( "\tpopq\t%%rsi\n" );
printf ( "\tpopq\t%%rcx\n" );
printf ( "\tpushq\t%%rax\n" );
printf ( "\tsubq\t$8, %%rsi\n" );
printf ( "\tloop\tPARSE_ARGV\n" );
/* Now the arguments are in order on stack */
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
printf ( "\tpopq\t%s\n", record[arg] );
printf ( "SKIP_ARGS:\n" );
printf ( "\tcall\t_%s\n", first->name );
printf ( "\tjmp \tEND\n" );
printf ( "ABORT:\n" );
printf ( "\tmovq\t$.errout, %%rdi\n" );
printf ( "\tcall\tputs\n" );
printf ( "END:\n" );
puts ( "\tmovq \t%rax, %rdi" );
puts ( "\tcall \texit" );
puts("###### FUNCTIONS FROM VSL BELOW #####");
putchar('\n');
}
static uint64_t
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
{
uint64_t no_symbols = tlhash_size(symbol_table);
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
tlhash_values(symbol_table, (void **)*symbol_list );
return no_symbols;
}

602
exercises/05/vslc/src/ir.c Normal file
View File

@@ -0,0 +1,602 @@
#include <vslc.h>
#define ERRPRT(format, args...) {fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args);}
// Externally visible, for the generator
extern tlhash_t *global_names;
extern char **string_list;
extern size_t n_string_list, stringc;
// Functions from the skeleton
static uint64_t find_globals ( void );
static void bind_names ( symbol_t *function, node_t *root );
// Helper functions, see description in the definition
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
create_symbol_table ( void )
{
// Initialize string array
n_string_list = DEFAULT_STRING_LIST_SIZE;
string_list = malloc(n_string_list * sizeof(char*));
stringc = 0;
// 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
print_symbol_table ( void )
{
/* 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
destroy_symbol_table ( void )
{
// 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);
// FREE SCOPES
// At the end of program, all scopes have to be popped
// Therefore only free the list
free(scopes);
// FREE GLOBAL NAMES
if (!global_names)
return;
// Fetch list of globals
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 );
// Destroy all global elements
for (uint64_t g = 0; g < no_globals; g++)
destroy_global(global_list[g]);
// Destory the global hash table
tlhash_finalize(global_names);
// Free the global hash table
free(global_names);
// Free the temp list
free(global_list);
}
/**
* Destroys the supplied global symbol by
* finalizing each of the local tables
*
* @param global pointer to the global symbol to be destroyed
*/
static void
destroy_global(symbol_t* global)
{
if (!global)
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);
}
/**
* 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 )
{
tlhash_init(global_names = malloc(sizeof(tlhash_t)), GLOBAL_BUCKET_SIZE);
uint64_t no_functions = 0, no_global_vars = 0;
node_t *global_list = root;
// Check if not nullptr
if (!global_list)
return 0;
symbol_t* global_symbol;
for (uint64_t global_i = 0; global_i < global_list->n_children; global_i++)
{
node_t *current_global = global_list->children[global_i];
switch (current_global->type)
{
case VARIABLE_LIST:
// Go through the variable list and get all the global variables
for (uint64_t var_i = 0; var_i < current_global->n_children; var_i++)
{
global_symbol = malloc(sizeof(symbol_t));
*global_symbol = (symbol_t){
.type = SYM_GLOBAL_VAR,
.name = current_global->children[var_i]->data,
.node = current_global->children[var_i],
.seq = 0,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_names, global_symbol);
no_global_vars++;
}
break;
case FUNCTION:
node_t *function = current_global;
// Function node allways have the same structure,
// [0] are the identifier
// [1] are the variable list, within a paramerer_list
// [2] are the actual block
if (!function->children[0])
break;
// Create the function symbol
global_symbol = malloc(sizeof(symbol_t));
*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++)
{
param_sym = malloc(sizeof(symbol_t));
*param_sym = (symbol_t){
.type = SYM_PARAMETER,
.name = param_list->children[param_i]->data,
.node = param_list->children[param_i],
.seq = param_i,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_symbol->locals, param_sym);
}
break;
}
}
return no_functions + no_global_vars;
}
/**
* Inserts a symbol into a hash table, key is defined in the name field in the symbol supplied.
*
* @param hash_table pointer to the hash table the symbol is inserted into
* @param symbol pointer to the symbol to be inserted
*/
void
insert_symbol(tlhash_t *hash_table, symbol_t* symbol)
{
tlhash_insert(hash_table, symbol->name, strlen(symbol->name), symbol);
}
/**
* @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);
}
break;
// If STRING_DATA, collect the string and point the
// data in the corresponding node to the array index
case STRING_DATA:
collect_string(root);
break;
// If none of the above, go deeper if possible.
default:
for (uint64_t i = 0; i < root->n_children; i++)
bind_names(function, root->children[i]);
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
insert_local_to_func(symbol_t *function, symbol_t *local)
{
tlhash_insert(
function->locals, //! Insert local to the function var table
&local->seq, //! The key is a number, unique, strictly growing
sizeof(local->seq), //! Size of key
local //! The local symbol
);
}
/**
* Collects strings to the string array and
* 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)
{
n_string_list *= 2;
char **new_string_list = realloc(string_list, n_string_list * sizeof(char*));
if (!new_string_list)
{
ERRPRT("Could not realloc string list!\n");
exit(EXIT_FAILURE);
}
string_list = new_string_list;
}
}
/**
* 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;
}

View File

@@ -0,0 +1,34 @@
#define STRING(x) #x
char *node_string[30] = {
STRING(PROGRAM),
STRING(GLOBAL_LIST),
STRING(GLOBAL),
STRING(STATEMENT_LIST),
STRING(PRINT_LIST),
STRING(EXPRESSION_LIST),
STRING(VARIABLE_LIST),
STRING(ARGUMENT_LIST),
STRING(PARAMETER_LIST),
STRING(DECLARATION_LIST),
STRING(FUNCTION),
STRING(STATEMENT),
STRING(BLOCK),
STRING(ASSIGNMENT_STATEMENT),
STRING(ADD_STATEMENT),
STRING(SUBTRACT_STATEMENT),
STRING(MULTIPLY_STATEMENT),
STRING(DIVIDE_STATEMENT),
STRING(RETURN_STATEMENT),
STRING(PRINT_STATEMENT),
STRING(NULL_STATEMENT),
STRING(IF_STATEMENT),
STRING(WHILE_STATEMENT),
STRING(EXPRESSION),
STRING(RELATION),
STRING(DECLARATION),
STRING(PRINT_ITEM),
STRING(IDENTIFIER_DATA),
STRING(NUMBER_DATA),
STRING(STRING_DATA)
};
#undef STRING

View File

@@ -0,0 +1,308 @@
%{
#include <vslc.h>
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
%}
%define api.value.type {node_t}
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%token VAR NUMBER IDENTIFIER STRING
%left '|' '&' '^'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%right '~'
//%expect 1
%nonassoc IF THEN
%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:
global_list {
root = NODE(PROGRAM, NULL, 1, $1);
}
;
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
yyerror ( const char *error )
{
fprintf ( stderr, "%s on line %d\n", error, yylineno );
exit ( EXIT_FAILURE );
}

View File

@@ -0,0 +1,30 @@
%{
#include <vslc.h>
%}
%option noyywrap
%option array
%option yylineno
WHITESPACE [\ \t\v\r\n]
COMMENT \/\/[^\n]+
QUOTED \"([^\"\n]|\\\")*\"
%%
{WHITESPACE}+ { /* Eliminate whitespace */ }
{COMMENT} { /* Eliminate comments */ }
func { return FUNC; }
print { return PRINT; }
return { return RETURN; }
continue { return CONTINUE; }
if { return IF; }
then { return THEN; }
else { return ELSE; }
while { return WHILE; }
do { return DO; }
begin { return OPENBLOCK; }
end { return CLOSEBLOCK; }
var { return VAR; }
[0-9]+ { return NUMBER; }
[A-Za-z_][0-9A-Za-z_]* { return IDENTIFIER; }
{QUOTED} { return STRING; }
. { return yytext[0]; }
%%

View File

@@ -0,0 +1,281 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <tlhash.h>
/*********************************************************************
* Declarations of the utility functions for obtaining hashes, found *
* at the bottom of this file. *
*********************************************************************/
/* Little-endian, for x86-s */
#define CRC32_IEEE802_3 (0xedb88320)
static const uint32_t crc32_ieee802_3[256];
static const uint32_t *crc_table = (uint32_t *)crc32_ieee802_3;
static uint32_t crc32 ( void *input, size_t length );
/********************************
* External interface functions *
********************************/
/* Initializer
* Returns
* ENOMEM - if allocation of table entries fails.
*/
int
tlhash_init ( tlhash_t *tab, size_t n_buckets )
{
size_t i;
tab->n_buckets = n_buckets;
tab->size = 0;
tab->buckets = (tlhash_element_t **) calloc (
n_buckets, sizeof(tlhash_element_t *)
);
if ( tab->buckets == NULL )
return TLHASH_ENOMEM;
for ( i=0; i<n_buckets; i++ )
tab->buckets[i] = NULL;
return TLHASH_SUCCESS;
}
/* Finalizer
* Returns
* ENOENT - if there is no table to free.
*/
int
tlhash_finalize ( tlhash_t *tab )
{
size_t i;
if ( tab == NULL )
return TLHASH_ENOENT;
for ( i=0; i<tab->n_buckets; i++ )
{
tlhash_element_t *element = tab->buckets[i], *next;
while ( element != NULL )
{
next = element->next;
free ( element->key );
free ( element );
tab->size -= 1;
element = next;
}
}
free ( tab->buckets );
return TLHASH_SUCCESS;
}
/* Insert - find hash value, modulate over buckets, append to linked list
* Returns
* EEXIST - if an element is already indexed by this key
* ENOMEM - if allocation of element or key copy fails
*/
int
tlhash_insert (
tlhash_t *tab, void *key, size_t key_length, void *value
)
{
void *test_entry;
int test = tlhash_lookup ( tab, key, key_length, &test_entry );
if ( test != TLHASH_ENOENT )
return TLHASH_EEXIST;
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *element = malloc ( sizeof(tlhash_element_t) );
if ( element == NULL )
return TLHASH_ENOMEM;
void *key_copy = malloc ( key_length );
if ( key_copy == NULL )
{
free ( element );
return TLHASH_ENOMEM;
}
memcpy ( key_copy, key, key_length );
element->key = key_copy;
element->key_length = key_length;
element->value = value;
element->next = tab->buckets[bucket];
tab->buckets[bucket] = element;
tab->size += 1;
return TLHASH_SUCCESS;
}
/* Lookup - find hash value, modulate over buckets, search linked list
* Returns
* ENOENT - if no element is indexed by this key
*/
int
tlhash_lookup (
tlhash_t *tab, void *key, size_t key_length, void **value
)
{
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *el = tab->buckets[bucket];
*value = NULL;
while ( el != NULL )
{
if ( el->key_length == key_length && ! memcmp(el->key,key,key_length) )
{
*value = el->value;
break;
}
el = el->next;
}
if ( el != NULL )
return TLHASH_SUCCESS;
else
return TLHASH_ENOENT;
}
/* Removal - find hash value, modulate over buckets, delete entry
* Returns
* ENOENT - no such element to remove was found.
*/
int
tlhash_remove ( tlhash_t *tab, void *key, size_t key_length )
{
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *el = tab->buckets[bucket], *prev = NULL;
while ( el != NULL )
{
if ( el->key_length == key_length && ! memcmp(el->key,key,key_length) )
{
/* We have a match. */
if ( prev != NULL ) /* Remove from list if it's not the head */
prev->next = (void *)el->next;
else /* Substitute it if it IS the head */
tab->buckets[bucket] = el->next;
/* Free the container and key copy allocated by this lib */
free ( el->key );
free ( el );
break;
}
prev = el;
el = el->next;
}
if ( el == NULL )
return TLHASH_ENOENT;
else
{
tab->size -= 1;
return TLHASH_SUCCESS;
}
}
size_t
tlhash_size ( tlhash_t *tab )
{
return tab->size;
}
void
tlhash_keys ( tlhash_t *tab, void **keys )
{
size_t b, i = 0;
for ( b=0; b<tab->n_buckets; b++ )
{
tlhash_element_t *el = tab->buckets[b];
while ( el != NULL )
{
keys[i] = el->key;
i += 1;
el = el->next;
}
}
}
void
tlhash_values ( tlhash_t *tab, void **values )
{
size_t b, i = 0;
for ( b=0; b<tab->n_buckets; b++ )
{
tlhash_element_t *el = tab->buckets[b];
while ( el != NULL )
{
values[i] = el->value;
i += 1;
el = el->next;
}
}
}
/***************************************
* Hashing function and IEEE data blob *
***************************************/
static uint32_t
crc32 ( void *input, size_t length )
{
const uint8_t *data = (uint8_t *)input;
size_t i = 0;
uint32_t hash = 0xFFFFFFFF;
for ( i = 0; i<length; i++ )
hash = (hash>>8) ^ crc_table [ data[i] ^ (uint8_t)hash ];
return (hash^0xFFFFFFFF);
}
static const uint32_t
crc32_ieee802_3[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};

View File

@@ -0,0 +1,400 @@
#include <vslc.h>
static void node_print ( node_t *root, int nesting );
static void simplify_tree ( node_t **simplified, node_t *root );
static void node_finalize ( node_t *discard );
typedef struct stem_t *stem;
struct stem_t { const char *str; stem next; };
static void
tree_print(node_t* root, stem head);
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 */
void
destroy_syntax_tree ( void )
{
destroy_subtree ( root );
}
void
simplify_syntax_tree ( void )
{
simplify_tree ( &root, root );
}
extern bool new_print_style;
void
print_syntax_tree ( void )
{
if (new_print_style)
tree_print ( root, 0 );
// Old tree printing
else
node_print ( root, 0 );
}
// 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, ...)
{
va_list child_list;
*nd = (node_t) {
.type = type,
.data = data,
.entry = NULL,
.n_children = n_children,
.children = (node_t **) malloc ( n_children * sizeof(node_t *) )
};
va_start ( child_list, n_children );
for ( uint64_t i=0; i<n_children; i++ )
nd->children[i] = va_arg ( child_list, node_t * );
va_end ( child_list );
return nd;
}
static void
tree_print(node_t* root, stem head)
{
static const char *sdown = "", *slast = "", *snone = " ";
struct stem_t col = {0, 0}, *tail;
// Print stems of branches coming further down
for (tail = head; tail; tail = tail->next) {
if (!tail->next) {
if (!strcmp(sdown, tail->str))
printf("");
else
printf("%s", tail->str);
break;
}
printf("%s", tail->str);
}
if (root == NULL) {
// Secure against null pointers sent as root
printf("─(nil)\n");
return;
}
printf("─%s", node_string[root->type]);
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION ||
root->type == RELATION)
printf("(%s)", (char *) root->data);
else if (root->type == NUMBER_DATA)
printf("(%ld)", *((int64_t *)root->data));
putchar('\n');
if (!root->n_children) return;
if (tail && tail->str == slast)
tail->str = snone;
if (!tail) tail = head = &col;
else tail->next = &col;
for ( int64_t i=0; i < root->n_children; i++ ) {
col.str = root->n_children - i - 1 ? sdown : slast;
tree_print(root->children[i], head);
}
tail->next = 0;
}
/* Internal choices */
static void
node_print ( node_t *root, int nesting )
{
if ( root != NULL )
{
printf ( "%*c%s", nesting, ' ', node_string[root->type] );
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION )
printf ( "(%s)", (char *) root->data );
else if ( root->type == NUMBER_DATA )
printf ( "(%ld)", *((int64_t *)root->data) );
putchar ( '\n' );
for ( int64_t i=0; i<root->n_children; i++ )
node_print ( root->children[i], nesting+1 );
}
else
printf ( "%*c%p\n", nesting, ' ', root );
}
static void
node_finalize ( node_t *discard )
{
if ( discard != NULL )
{
free ( discard->data );
free ( discard->children );
free ( discard );
}
}
static void
destroy_subtree ( node_t *discard )
{
if ( discard != NULL )
{
for ( uint64_t i=0; i<discard->n_children; i++ )
destroy_subtree ( discard->children[i] );
node_finalize ( discard );
}
}
static void
flatten(node_t **simplified, node_t *root)
{
/* This will flatten left-expanded lists */
if (!root)
return;
/* Do this recursivly */
for (int i = 0; i < root->n_children; i++)
flatten(&root->children[i], root->children[i]);
node_t **new_children, *result = root;
switch (root->type)
{
case GLOBAL_LIST:
case STATEMENT_LIST:
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->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);
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:
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;
node_finalize(root);
break;
}
*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)
{
case 1:
result = root->children[0];
if (root->data &&
result->type == NUMBER_DATA &&
result->data)
{
switch (*((char*)root->data))
{
case '-':
*((int64_t*)result->data) *= -1;
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);
}
break;
}
*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;
}

View File

@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <vslc.h>
/* Global state */
node_t *root; // Syntax tree
tlhash_t *global_names; // Symbol table
char **string_list; // List of strings in the source
size_t n_string_list = 8; // Initial string list capacity (grow on demand)
size_t stringc = 0; // Initial string count
/* Command line option parsing for the main function */
static void options ( int argc, char **argv );
bool
print_full_tree = false,
print_simplified_tree = false,
print_symbol_table_contents = false,
print_generated_program = true,
new_print_style = true;
/* Entry point */
int
main ( int argc, char **argv )
{
options ( argc, argv );
yyparse(); // Generated from grammar/bison, constructs syntax tree
yylex_destroy(); // Free heap used by flex
if ( print_full_tree )
print_syntax_tree ();
simplify_syntax_tree (); // In tree.c
if ( print_simplified_tree )
print_syntax_tree ();
create_symbol_table (); // In ir.c
if ( print_symbol_table_contents )
print_symbol_table();
if ( print_generated_program )
generate_program (); // In generator.c
destroy_syntax_tree (); // In tree.c
destroy_symbol_table (); // In ir.c
}
static const char *usage =
"Command line options\n"
"\t-h\tOutput this text and halt\n"
"\t-t\tOutput the full syntax tree\n"
"\t-T\tOutput the simplified syntax tree\n"
"\t-s\tOutput the symbol table contents\n"
"\t-q\tQuiet: suppress output from the code generator\n"
"\t-u\tDo not use print style more like the tree command\n";
static void
options ( int argc, char **argv )
{
int o;
while ( (o=getopt(argc,argv,"htTsqu")) != -1 )
{
switch ( o )
{
case 'h':
printf ( "%s:\n%s", argv[0], usage );
exit ( EXIT_FAILURE );
break;
case 't': print_full_tree = true; break;
case 'T': print_simplified_tree = true; break;
case 's': print_symbol_table_contents = true; break;
case 'q': print_generated_program = false; break;
case 'u': new_print_style = false; break;
}
}
}

View File

@@ -0,0 +1,42 @@
VSLC := ../src/vslc
AS := gcc
# This updated makefile calls the relevant print for each assignment
# The added target compile will attempt to compile all vsl source files,
# while the compile-ps5 target attempts to compile a selection of sources
# that should work after ps5 is done.
# Call `vslc -h` to see the available flags, and call `vslc [flags] < file.vsl`
# to compile a single file.
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl))
PS3_EXAMPLES := $(patsubst ps3-simplify/%.vsl, ps3-simplify/%.sast, $(wildcard ps3-simplify/*.vsl))
PS4_EXAMPLES := $(patsubst ps4-symtab/%.vsl, ps4-symtab/%.sym, $(wildcard ps4-symtab/*.vsl))
PS5_EXAMPLES := $(patsubst ps5-codegen1/%.vsl, ps5-codegen1/%.S, $(wildcard ps5-codegen1/*.vsl))
PS5_OBJECTS := $(PS5_EXAMPLES:.S=.bin)
OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin)
all: clean $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES)
ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES)
ps5: $(PS5_EXAMPLES)
%.ast: %.vsl
$(VSLC) -t -q < $^ > $@ 2> $@
%.sast: %.vsl
$(VSLC) -T -q < $^ > $@ 2> $@
%.sym: %.vsl
$(VSLC) -s -q < $^ > $@ 2> $@
%.S: %.vsl
$(VSLC) < $^ > $@
# This target is only tested on x86-linux
%.bin: %.S
$(AS) -no-pie -o $@ $^
ps5-compile: $(PS5_OBJECTS)
compile: $(OBJECTS)
clean:
-rm -r */*.ast */*.sast */*.sym */*.bin */*.s */*.S

View File

@@ -0,0 +1,15 @@
// checking that comments are ignored
// This program checks the assignment operators
func main()
begin
var a
a := 3
a += 1
a /= 2
a *= 32
a -= 2
print a
return 0
end

View File

@@ -0,0 +1,10 @@
func add(a, b) begin
a := -a
return a + b
end
func main()
begin
print add(40, 2)
return 0
end

View File

@@ -0,0 +1,4 @@
func helloworld() begin
print "Hello, World!"
return 0
end

View File

@@ -0,0 +1,26 @@
func main()
begin
var a, b, c, d
c := 1
a := 3
b := a + c // 4
d := a * 100 + 50
print "a", a
print "b", b
print "c", c
print "d", d
if a = 14 then
print 1, "N", d / 5 + a, "RPR", a, "TERS "
else
print "COMP", c, "L", "ERS "
print b, "R", a, " "
if a < b then
if d > 42 then
print b, "W", d, "ME"
else
print "L", b, "M", c
// A dangling else, what could go wrong?
return 0
end

View File

@@ -0,0 +1,21 @@
var global_var
func my_func(param)
begin
var local_var, local_var2
local_var := 1
return local_var
end
var glob1, glob2
func main()
begin
var main_local_var
begin
var main_local_nested_var
main_local_nested_var := main_local_var + my_func(1)
end
return 0
end

View File

@@ -0,0 +1,11 @@
// check parsing of do-while loop
func main()
begin
var i
i := 2
while i < 42 do
i := i * i
print i
return 0
end

View File

@@ -0,0 +1,10 @@
func main() begin
var a, b
a := 1 + 2 + 4 + 5 + 6 + 7 + 8 + 9
b := (10 + 10 * 4) * (2 + 2 * (1 + 1)) / 10 + 2 * 5 + 6 / 3
if a = b then
print "The answer is", b
return 0
end

View File

@@ -0,0 +1,9 @@
func add(a, b, c) begin
return a + b
end
func main()
begin
print add(40, 2, 4)
return 0
end

View File

@@ -0,0 +1,12 @@
var global_var0, global_var1
func my_func(param0, param1) begin
var a
return 0
end
func main() begin
var a
print "a string"
return 0
end

View File

@@ -0,0 +1,22 @@
func my_func(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := a + ~(i + 4)
i := a + (b + d) * (3 + b)
if i = f then begin
print "hmmm"
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)
return 0
end

View File

@@ -0,0 +1,28 @@
// Checking symbol shadowing
var a
func shadow() begin
var a, b
a := 1
begin
var a
a := 2
b := 40
begin
var a
a := b + 2
print a, b
end
print a
begin
var b
b := 38
a := b + 3
print a, b
end
print a
end
print b
return 0
end

View File

@@ -0,0 +1,12 @@
var global_var0, global_var1
func my_func(param0, param1) begin
var a
return 0
end
func main() begin
var a
print "a string"
return 0
end

View File

@@ -0,0 +1,20 @@
func my_func(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := --i
i := a + b + d
if i = f then begin
print "hmmm"
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)
return 0
end

View File

@@ -0,0 +1,28 @@
// Checking symbol shadowing
var a
func shadow() begin
var a, b
a := 1
begin
var a
a := 2
b := 40
begin
var a
a := b + 2
print a, b
end
print a
begin
var b
b := 38
a := b + 3
print a, b
end
print a
end
print b
return 0
end

View File

@@ -0,0 +1,9 @@
func add(a, b, c) begin
return a + b
end
func main()
begin
print add(40, 2, 4)
return 0
end

View File

@@ -0,0 +1,12 @@
var global_var0, global_var1
func my_func(param0, param1) begin
var a
return 0
end
func main() begin
var a
print "a string"
return 0
end

View File

@@ -0,0 +1,4 @@
func helloworld() begin
print "Hello, World!"
return 0
end

View File

@@ -0,0 +1,22 @@
func my_func(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := a + ~(i + 4)
i := a + (b + d) * (3 + b)
if i = f then begin
print "hmmm"
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)
print n
return 0
end

View File

@@ -0,0 +1,30 @@
// This program tests activation records, function call and return
func funcall ()
begin
var x,y,z
x := 5
y := 10
print "Calling my_function with parameters", x, y
z := my_function ( x, y )
print "The returned result is", z
z := my_other_function ()
print "The other returned result is", z
return 0
end
func my_function ( s, t )
begin
var u
u := s*s + t*t
print "Parameter s is", s
print "Parameter t is", t
print "The sum of their squares is", u
return u
end
func my_other_function ()
begin
var x
x := 42
return x
end

View File

@@ -0,0 +1,28 @@
// Checking symbol shadowing
var a
func shadow() begin
var a, b
a := 1
begin
var a
a := 2
b := 40
begin
var a
a := b + 2
print a, b
end
print a
begin
var b
b := 38
a := b + 3
print a, b
end
print a
end
print b
return 0
end

17
exercises/06/.gitignore vendored Normal file
View File

@@ -0,0 +1,17 @@
# Buildfiles
*.o
vslc/src/vslc
parser.c
scanner.c
y.tab.h
# Submission
*.tar.xz
# VSL treefiles
*.ast
*.sast
*.sym
*.bin
*.s
*.S

28
exercises/06/.vscode/launch.json vendored Normal file
View 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
}
]
}
]
}

BIN
exercises/06/PS6.pdf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,12 @@
LEX=flex
YACC=bison
YFLAGS+=--defines=src/y.tab.h -o y.tab.c
CFLAGS+=-std=c99 -g -Isrc -Iinclude -D_POSIX_C_SOURCE=200809L -DYYSTYPE="node_t *"
src/vslc: src/vslc.c src/parser.o src/scanner.o src/nodetypes.o src/tree.o src/ir.o src/generator.o src/tlhash.c
src/y.tab.h: src/parser.c
src/scanner.c: src/y.tab.h src/scanner.l
clean:
-rm -f src/parser.c src/scanner.c src/*.tab.* src/*.o
purge: clean
-rm -f src/vslc

View File

@@ -0,0 +1,51 @@
#ifndef IR_H
#define IR_H
/* This is the tree node structure */
typedef struct n {
node_index_t type;
void *data;
struct s *entry;
uint64_t n_children;
struct n **children;
} node_t;
/**Export the initializer function, it is needed by the parser
* @param *nd node to initialize
* @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 {
SYM_GLOBAL_VAR,
SYM_FUNCTION,
SYM_PARAMETER,
SYM_LOCAL_VAR
} symtype_t;
typedef struct s {
char* name;
symtype_t type;
node_t* node;
size_t seq;
size_t nparms;
tlhash_t* locals;
} symbol_t;
#endif
#define GLOBAL_BUCKET_SIZE 32
#define LOCAL_BUCKET_SIZE 16
#define DEFAULT_STRING_LIST_SIZE 8
#define DEFAULT_NO_SCOPES 1

View File

@@ -0,0 +1,37 @@
#ifndef NODETYPES_H
#define NODETYPES_H
typedef enum {
PROGRAM,
GLOBAL_LIST,
GLOBAL,
STATEMENT_LIST,
PRINT_LIST,
EXPRESSION_LIST,
VARIABLE_LIST,
ARGUMENT_LIST,
PARAMETER_LIST,
DECLARATION_LIST,
FUNCTION,
STATEMENT,
BLOCK,
ASSIGNMENT_STATEMENT,
ADD_STATEMENT,
SUBTRACT_STATEMENT,
MULTIPLY_STATEMENT,
DIVIDE_STATEMENT,
RETURN_STATEMENT,
PRINT_STATEMENT,
NULL_STATEMENT,
IF_STATEMENT,
WHILE_STATEMENT,
EXPRESSION,
RELATION,
DECLARATION,
PRINT_ITEM,
IDENTIFIER_DATA,
NUMBER_DATA,
STRING_DATA
} node_index_t;
extern char *node_string[26];
#endif

View File

@@ -0,0 +1,28 @@
#ifndef TLHASH_H
#define TLHASH_H
#include <stddef.h>
typedef struct el {
void *key, *value;
size_t key_length;
struct el *next;
} tlhash_element_t;
typedef struct {
size_t n_buckets, size;
tlhash_element_t **buckets;
} tlhash_t;
int tlhash_init ( tlhash_t *tab, size_t n_buckets );
int tlhash_finalize ( tlhash_t *tab );
int tlhash_insert ( tlhash_t *tab, void *key, size_t keylen, void *val );
int tlhash_lookup ( tlhash_t *tab, void *key, size_t keylen, void **val );
int tlhash_remove ( tlhash_t *tab, void *key, size_t key_length );
size_t tlhash_size ( tlhash_t *tab );
void tlhash_keys ( tlhash_t *tab, void **keys );
void tlhash_values ( tlhash_t *tab, void **values );
#define TLHASH_SUCCESS 0 /* Success */
#define TLHASH_ENOMEM 1 /* No memory available */
#define TLHASH_ENOENT 2 /* No such table entry */
#define TLHASH_EEXIST 3 /* Table entry already exists */
#endif

View File

@@ -0,0 +1,51 @@
#ifndef VSLC_H
#define VSLC_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
// Prototypes for the hash table functions
#include "tlhash.h"
// Numbers and names for the types of syntax tree nodes
#include "nodetypes.h"
// Definition of the tree node type
#include "ir.h"
// Token definitions and other things from bison, needs def. of node type
#include "y.tab.h"
/* This is generated from the bison grammar, calls on the flex specification */
int yyerror ( const char *error );
/* These are defined in the parser generated by bison */
extern int yylineno;
extern int yylex ( void );
extern int yylex_destroy( void );
extern char yytext[];
/* Global state */
extern node_t *root;
// Moving global defs to global header
extern tlhash_t *global_names; // Defined in ir.c, used by generator.c
extern char **string_list; // Defined in ir.c, used by generator.c
extern size_t stringc; // Defined in ir.c, used by generator.c
/* Global routines, called from main in vslc.c */
void simplify_syntax_tree ( void );
void print_syntax_tree ( void );
void destroy_syntax_tree ( void );
void create_symbol_table ( void );
void print_symbol_table ( void );
void destroy_symbol_table ( void );
void generate_program ( void );
#endif

View File

@@ -0,0 +1,829 @@
#include <vslc.h>
#define ERRPRT(format, args...) fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args)
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
#define LABEL(label) printf("_%s:\n", (char*)label)
#define COMMENT(format, args...) printf("# "format"\n", ##args)
// 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 POP(param) printf("\tpopq\t%s\t\t\t\t# POP: %ld\n", #param, --stack_depth)
#define NO_REG_RECORD 6
// Keep track of sequence of stack depth, ifs and whiles
static uint64_t
stack_depth,
if_seq,
while_seq,
closest_while;
/**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 );
/**Initializes program (already implemented) */
void generate_main ( symbol_t *first );
#define MIN(a,b) (((a)<(b)) ? (a):(b))
static const char *record[NO_REG_RECORD] = {
"%rdi", "%rsi", "%rdx", "%rcx", "%r8", "%r9"
};
// 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);
/**
* 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);
/**
* 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);
/**
* Inserts a jump to the inner most while.
* This also works when multiple whiles are nested.
*
*/
static void solve_continue_statement();
/**
* 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
generate_program ( void )
{
// Generate the string table at the top
generate_stringtable();
// Fetch all the global elements (functions and global vars)
symbol_t **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;
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]);
// Then generate all the functions from vsl
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
generate_stringtable ( void )
{
/* These can be used to emit numbers, strings and a run-time
* error msg. from main
*/
puts("# DATA SECTION");
puts(".section .data");
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
generate_global_variables ( void )
{
symbol_t **global_list;
uint64_t no_globals = fetch_symbols(global_names, &global_list);
puts("# GLOBAL VARIABLES");
puts(".bss");
puts(".align 8");
for (uint64_t g = 0; g < no_globals; g++)
if (global_list[g]->type == SYM_GLOBAL_VAR)
printf(".%s:\n", global_list[g]->name);
putchar('\n');
free(global_list);
}
void
generate_function ( symbol_t *function )
{
// Keep track of the stack size in each of the functions
stack_depth = 0;
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
puts(".section .text");
printf(".global _%s\n", function->name);
LABEL(function->name);
PUSH(%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\t\t\t\t# PUSH: %ld\n",
record[arg],
++stack_depth
);
// How many local variables are inside function
uint64_t no_locals = function->locals->size - function->nparms;
// Make room for the local vars
while(no_locals--)
PUSH($0);
// IF the stack alignment is not 16 bytes,
// add one now as all local var also is 0
if (stack_depth % 2)
PUSH($0);
// Now the stack ptr should be 16 byte aligned.
// Then generate the body of the function
generate_node(function->node);
putchar('\n');
}
void
generate_node ( node_t *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:
// First solve the rhs
solve_expressions(node->children[1]);
// Then store in lhs
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:
generate_if_statement(node);
break;
case WHILE_STATEMENT:
generate_while_statement(node);
break;
case NULL_STATEMENT:
solve_continue_statement();
break;
case DECLARATION_LIST:
/* List of blocks we dont need to traverse */
break;
default:
// Otherwise, generate the traverse
for (int c = 0; c < node->n_children; c++)
generate_node(node->children[c]);
break;
}
}
// Generate the print node
void
generate_print(node_t* node)
{
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(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;
}
COMMENT("printf call");
ASM(call, printf);
}
// Adds a newline
ASM(movq, $'\n', %rdi);
ASM(call, putchar);
}
// This will put the value of var in node in dest
void
fetch_variable(node_t *node, const char* dest)
{
printf("\tmovq\t");
generate_var_ident(node);
printf(", %s\t\t# Fetched: %s and put in %s\n",
dest,
node->entry->name,
dest
);
}
// 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' 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
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
// no no no noooo, it should leave it in rax! As it does :))))
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");
break;
case NUMBER_DATA:
printf("\tmovq\t$%ld,%%rax\n",*(int64_t*)node->data);
break;
}
break;
case 1:
solve_expressions(node->children[0]);
switch (*(char*)node->data)
{
case '-':
ASM(negq, %rax);
break;
case '~':
ASM(notq, %rax);
break;
}
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]);
PUSH(%rax);
}
// Put rhs in %r10
POP(%r10);
// put lhs in %rax
POP(%rax);
// All operators below leaves result in %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;
}
break;
}
}
void
generate_function_call(node_t *node)
{
printf("# Function call\n");
bool isStack16ByteAligned = !(stack_depth % 2);
// If the stack is 16 byte alligned here, offset
// by 1 because call pushes return addr to stack
// Therefore after call, the stack is 16 byte aligned
if (isStack16ByteAligned)
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];
if (arg_list->n_children)
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++)
{
switch (arg_list->children[arg]->type)
{
case NUMBER_DATA:
printf("\tmovq\t$%ld, %s\n",
*(int64_t*)arg_list->children[arg]->data,
record[arg]
);
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]);
break;
}
}
if (arg_list->n_children > NO_REG_RECORD)
{
// If there is an odd number of args to push to stack, add 1
if (arg_list->n_children % 2)
PUSH($0);
for (int arg = arg_list->n_children - 1; arg >= NO_REG_RECORD; arg--)
{
switch (arg_list->children[arg]->type)
{
case NUMBER_DATA:
printf("\tpushq\t$%ld\t\t\t\t# PUSH: %ld\n",
*(int64_t*)arg_list->children[arg]->data,
++stack_depth
);
break;
// Arg can be an expression
case EXPRESSION:
solve_expressions(arg_list->children[arg]);
PUSH(%rax);
break;
default:
printf("\tpushq\t");
generate_var_ident(arg_list->children[arg]);
printf("\t\t\t\t# PUSH: %ld", ++stack_depth);
putchar('\n');
break;
}
}
}
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
// Aaaand pop the stack to return back to stack alignment
if (isStack16ByteAligned)
POP(%rcx);
printf("# End of function call\n");
}
void
generate_function_return(node_t *node)
{
solve_expressions(node->children[0]);
ASM(leave);
ASM(ret);
}
void
solve_statements(node_t *node, char *operator)
{
node->type = EXPRESSION;
node->data = strdup(operator);
solve_expressions(node);
writeback_variable(node->children[0], "%rax");
}
// Takes in a relation/number node and sets %rax to true if the statement is true
void
solve_relations(node_t *relation_root)
{
switch (relation_root->type)
{
case NUMBER_DATA:
// Numberdata is boring, just leave value in %rax, only 1 is interperted as true
solve_expressions(relation_root);
break;
case RELATION:
if (relation_root->n_children != 2)
{
ERRPRT("Relation requires two expressions, one lhs and one rhs\n");
exit(EXIT_FAILURE);
}
for (int i = 0; i < relation_root->n_children; i++)
{
solve_expressions(relation_root->children[i]);
PUSH(%rax);
}
POP(%r10);
POP(%rax);
ASM(cmp, %r10, %rax);
ASM(movq, $0, %rax);
switch (*(char*)relation_root->data)
{
case '=': ASM(sete, %al); break; // Set %al (0th byte of %rax) to 1 if lhs == rhs
case '>': ASM(setg, %al); break; // Set %al (0th byte of %rax) to 1 if lhs > rhs
case '<': ASM(setl, %al); break; // Set %al (0th byte of %rax) to 1 if lhs < rhs
}
break;
}
}
void
generate_if_statement(node_t *node)
{
uint64_t current_if_seq = if_seq++;
COMMENT("Begin IF %ld", current_if_seq);
// The realtion is allways in the first part of the IF
solve_relations(node->children[0]);
// 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("ELSE%03ld:\n", current_if_seq);
// Generate ELSE body
generate_node(node->children[2]);
}
COMMENT("End IF %ld", current_if_seq);
printf("ENDIF%03ld:\n", current_if_seq);
}
void
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
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
* main function (first, if no function is named main)
* @param first Symbol table entry of our main function */
void
generate_main ( symbol_t *first )
{
puts("###### Entry point for GAS #####");
puts ( ".globl main" );
puts ( ".section .text" );
puts ( "main:" );
puts ( "\tpushq %rbp" ); // Added this for stack alignment
puts ( "\tpushq %rbp" );
puts ( "\tmovq %rsp, %rbp" );
printf ( "\tsubq\t$1,%%rdi\n" );
printf ( "\tcmpq\t$%zu,%%rdi\n", first->nparms );
printf ( "\tjne \tABORT\n" );
printf ( "\tcmpq\t$0,%%rdi\n" );
printf ( "\tjz \tSKIP_ARGS\n" );
printf ( "\tmovq\t%%rdi,%%rcx\n" );
printf ( "\taddq\t$%zu, %%rsi\n", 8*first->nparms );
// Modification to mail call, remove this if no params supplied
if (first->nparms)
{
printf ( "PARSE_ARGV:\n" );
printf ( "\tpushq\t%%rcx\n" );
printf ( "\tpushq\t%%rsi\n" );
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
printf ( "\tmovq\t$0,%%rsi\n" );
printf ( "\tmovq\t$10,%%rdx\n" );
printf ( "\tcall\tstrtol\n" );
/* Now a new argument is an integer in rax */
printf ( "\tpopq\t%%rsi\n" );
printf ( "\tpopq\t%%rcx\n" );
printf ( "\tpushq\t%%rax\n" );
printf ( "\tsubq\t$8, %%rsi\n" );
printf ( "\tloop\tPARSE_ARGV\n" );
/* Now the arguments are in order on stack */
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
printf ( "\tpopq\t%s\n", record[arg] );
}
printf ( "SKIP_ARGS:\n" );
printf ( "\tcall\t_%s\n", first->name );
printf ( "\tjmp \tEND\n" );
printf ( "ABORT:\n" );
printf ( "\tmovq\t$.errout, %%rdi\n" );
printf ( "\tcall\tputs\n" );
printf ( "END:\n" );
puts ( "\tmovq \t%rax, %rdi" );
puts ( "\tcall \texit" );
puts("###### FUNCTIONS FROM VSL BELOW #####");
putchar('\n');
}

View File

@@ -0,0 +1,641 @@
#include <vslc.h>
#define ASM(opcode, args...) puts("\t"#opcode"\t"#args)
#define LABEL(label) printf("_%s:\n", (char*)label)
#define COMMENT(format, args...) printf("# "format"\n", ##args)
#define PUSH(param) printf("\tpushq\t%s\t\t\t\t# Scope depth: %ld\n", #param, ++scope_depth)
#define POP(param) printf("\tpopq\t%s\t\t\t\t# Scope depth: %ld\n", #param, --scope_depth)
#define NO_REG_RECORD 6
#define NO_CALLE_SAVED_REG 10
uint64_t scope_depth = 0;
/**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 );
/**Initializes program (already implemented) */
void generate_main ( symbol_t *first );
#define MIN(a,b) (((a)<(b)) ? (a):(b))
static const char *record[NO_REG_RECORD] = {
"%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
/**
* 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
generate_program ( void )
{
generate_stringtable();
generate_global_variables();
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
generate_stringtable ( void )
{
/* These can be used to emit numbers, strings and a run-time
* error msg. from main
*/
puts("# DATA SECTION");
puts(".section .data");
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
generate_global_variables ( void )
{
symbol_t **global_list;
uint64_t no_globals = fetch_symbols(global_names, &global_list);
puts("# GLOBAL VARIABLES");
puts(".bss");
puts(".align 8");
for (uint64_t g = 0; g < no_globals; g++) {
if (global_list[g]->type == SYM_GLOBAL_VAR)
printf(".%s:\n", global_list[g]->name);
}
putchar('\n');
free(global_list);
}
void
generate_function ( symbol_t *function )
{
// TODO: Generate code for declaring and entering function, then generate its body
printf("# func %s(nparams: %ld)\n", function->name, function->nparms);
puts(".section .text");
printf(".global _%s\n", function->name);
LABEL(function->name);
//ASM(pushq, %rbp);
PUSH(%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\t\t\t\t# Scope depth: %ld\n", record[arg], ++scope_depth);
// 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)
PUSH($0);
//ASM(pushq, $0);
// Make room for the local vars
while(no_locals--)
PUSH($0);
//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);
POP(%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);
POP(%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;
}
PUSH(%rax);
ASM(movq, $0, %rax);
COMMENT("Actual print. Stack size: %ld", scope_depth);
ASM(call, printf);
POP(%rax);
}
// Adds a newline
PUSH(%rax);
ASM(movq, $0, %rax);
ASM(movq, $'\n', %rdi);
ASM(call, putchar);
POP(%rax);
}
// 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");
PUSH(%rax);
//ASM(pushq, %rax);
break;
case NUMBER_DATA:
printf("\tmovq\t$%ld,%%rax\n",*(int64_t*)node->data);
PUSH(%rax);
//ASM(pushq, %rax);
break;
}
break;
case 1:
solve_expressions(node->children[0]);
//ASM(popq, %rax);
POP(%rax);
switch (*(char*)node->data)
{
case '-':
ASM(negq, %rax);
break;
case '~':
ASM(notq, %rax);
break;
}
//ASM(pushq, %rax);
PUSH(%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);
POP(%r10);
// put lhs in %rax
//ASM(popq, %rax);
POP(%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);
PUSH(%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\t\t\t\t# Scope depth: %ld\n",
*(int64_t*)arg_list->children[arg]->data,
++scope_depth
);
else
{
printf("\tpushq\t");
generate_var_ident(arg_list->children[arg]);
printf("\t\t\t\t# Scope depth: %ld", ++scope_depth);
putchar('\n');
}
}
if (arg_list->n_children % 2)
PUSH($0);
//ASM(pushq, $0);
}
printf("\tcall\t_%s\n", (char*)node->children[0]->data);
PUSH(%rax);
//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);
POP(%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);
POP(%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
generate_main ( symbol_t *first )
{
puts("###### Entry point for GAS #####");
puts ( ".globl main" );
puts ( ".section .text" );
puts ( "main:" );
puts ( "\tpushq %rbp" );
puts ( "\tmovq %rsp, %rbp" );
printf ( "\tsubq\t$1,%%rdi\n" );
printf ( "\tcmpq\t$%zu,%%rdi\n", first->nparms );
printf ( "\tjne \tABORT\n" );
printf ( "\tcmpq\t$0,%%rdi\n" );
printf ( "\tjz \tSKIP_ARGS\n" );
printf ( "\tmovq\t%%rdi,%%rcx\n" );
printf ( "\taddq\t$%zu, %%rsi\n", 8*first->nparms );
printf ( "PARSE_ARGV:\n" );
printf ( "\tpushq\t%%rcx\n" );
printf ( "\tpushq\t%%rsi\n" );
printf ( "\tmovq\t(%%rsi),%%rdi\n" );
printf ( "\tmovq\t$0,%%rsi\n" );
printf ( "\tmovq\t$10,%%rdx\n" );
printf ( "\tcall\tstrtol\n" );
/* Now a new argument is an integer in rax */
printf ( "\tpopq\t%%rsi\n" );
printf ( "\tpopq\t%%rcx\n" );
printf ( "\tpushq\t%%rax\n" );
printf ( "\tsubq\t$8, %%rsi\n" );
printf ( "\tloop\tPARSE_ARGV\n" );
/* Now the arguments are in order on stack */
for (int arg = 0; arg < MIN(6,first->nparms); arg++)
printf ( "\tpopq\t%s\n", record[arg] );
printf ( "SKIP_ARGS:\n" );
printf ( "\tcall\t_%s\n", first->name );
printf ( "\tjmp \tEND\n" );
printf ( "ABORT:\n" );
printf ( "\tmovq\t$.errout, %%rdi\n" );
printf ( "\tcall\tputs\n" );
printf ( "END:\n" );
puts ( "\tmovq \t%rax, %rdi" );
puts ( "\tcall \texit" );
puts("###### FUNCTIONS FROM VSL BELOW #####");
putchar('\n');
}
static uint64_t
fetch_symbols(tlhash_t* symbol_table, symbol_t*** symbol_list)
{
uint64_t no_symbols = tlhash_size(symbol_table);
*symbol_list = malloc(no_symbols * sizeof(symbol_t));
tlhash_values(symbol_table, (void **)*symbol_list );
return no_symbols;
}

602
exercises/06/vslc/src/ir.c Normal file
View File

@@ -0,0 +1,602 @@
#include <vslc.h>
#define ERRPRT(format, args...) {fprintf(stderr, "[ERROR] "); fprintf(stderr ,format, ##args);}
// Externally visible, for the generator
extern tlhash_t *global_names;
extern char **string_list;
extern size_t n_string_list, stringc;
// Functions from the skeleton
static uint64_t find_globals ( void );
static void bind_names ( symbol_t *function, node_t *root );
// Helper functions, see description in the definition
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
create_symbol_table ( void )
{
// Initialize string array
n_string_list = DEFAULT_STRING_LIST_SIZE;
string_list = malloc(n_string_list * sizeof(char*));
stringc = 0;
// 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
print_symbol_table ( void )
{
/* 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
destroy_symbol_table ( void )
{
// 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);
// FREE SCOPES
// At the end of program, all scopes have to be popped
// Therefore only free the list
free(scopes);
// FREE GLOBAL NAMES
if (!global_names)
return;
// Fetch list of globals
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 );
// Destroy all global elements
for (uint64_t g = 0; g < no_globals; g++)
destroy_global(global_list[g]);
// Destory the global hash table
tlhash_finalize(global_names);
// Free the global hash table
free(global_names);
// Free the temp list
free(global_list);
}
/**
* Destroys the supplied global symbol by
* finalizing each of the local tables
*
* @param global pointer to the global symbol to be destroyed
*/
static void
destroy_global(symbol_t* global)
{
if (!global)
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);
}
/**
* 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 )
{
tlhash_init(global_names = malloc(sizeof(tlhash_t)), GLOBAL_BUCKET_SIZE);
uint64_t no_functions = 0, no_global_vars = 0;
node_t *global_list = root;
// Check if not nullptr
if (!global_list)
return 0;
symbol_t* global_symbol;
for (uint64_t global_i = 0; global_i < global_list->n_children; global_i++)
{
node_t *current_global = global_list->children[global_i];
switch (current_global->type)
{
case VARIABLE_LIST:
// Go through the variable list and get all the global variables
for (uint64_t var_i = 0; var_i < current_global->n_children; var_i++)
{
global_symbol = malloc(sizeof(symbol_t));
*global_symbol = (symbol_t){
.type = SYM_GLOBAL_VAR,
.name = current_global->children[var_i]->data,
.node = current_global->children[var_i],
.seq = 0,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_names, global_symbol);
no_global_vars++;
}
break;
case FUNCTION:
node_t *function = current_global;
// Function node allways have the same structure,
// [0] are the identifier
// [1] are the variable list, within a paramerer_list
// [2] are the actual block
if (!function->children[0])
break;
// Create the function symbol
global_symbol = malloc(sizeof(symbol_t));
*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++)
{
param_sym = malloc(sizeof(symbol_t));
*param_sym = (symbol_t){
.type = SYM_PARAMETER,
.name = param_list->children[param_i]->data,
.node = param_list->children[param_i],
.seq = param_i,
.nparms = 0,
.locals = NULL
};
insert_symbol(global_symbol->locals, param_sym);
}
break;
}
}
return no_functions + no_global_vars;
}
/**
* Inserts a symbol into a hash table, key is defined in the name field in the symbol supplied.
*
* @param hash_table pointer to the hash table the symbol is inserted into
* @param symbol pointer to the symbol to be inserted
*/
void
insert_symbol(tlhash_t *hash_table, symbol_t* symbol)
{
tlhash_insert(hash_table, symbol->name, strlen(symbol->name), symbol);
}
/**
* @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);
}
break;
// If STRING_DATA, collect the string and point the
// data in the corresponding node to the array index
case STRING_DATA:
collect_string(root);
break;
// If none of the above, go deeper if possible.
default:
for (uint64_t i = 0; i < root->n_children; i++)
bind_names(function, root->children[i]);
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
insert_local_to_func(symbol_t *function, symbol_t *local)
{
tlhash_insert(
function->locals, //! Insert local to the function var table
&local->seq, //! The key is a number, unique, strictly growing
sizeof(local->seq), //! Size of key
local //! The local symbol
);
}
/**
* Collects strings to the string array and
* 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)
{
n_string_list *= 2;
char **new_string_list = realloc(string_list, n_string_list * sizeof(char*));
if (!new_string_list)
{
ERRPRT("Could not realloc string list!\n");
exit(EXIT_FAILURE);
}
string_list = new_string_list;
}
}
/**
* 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;
}

View File

@@ -0,0 +1,34 @@
#define STRING(x) #x
char *node_string[30] = {
STRING(PROGRAM),
STRING(GLOBAL_LIST),
STRING(GLOBAL),
STRING(STATEMENT_LIST),
STRING(PRINT_LIST),
STRING(EXPRESSION_LIST),
STRING(VARIABLE_LIST),
STRING(ARGUMENT_LIST),
STRING(PARAMETER_LIST),
STRING(DECLARATION_LIST),
STRING(FUNCTION),
STRING(STATEMENT),
STRING(BLOCK),
STRING(ASSIGNMENT_STATEMENT),
STRING(ADD_STATEMENT),
STRING(SUBTRACT_STATEMENT),
STRING(MULTIPLY_STATEMENT),
STRING(DIVIDE_STATEMENT),
STRING(RETURN_STATEMENT),
STRING(PRINT_STATEMENT),
STRING(NULL_STATEMENT),
STRING(IF_STATEMENT),
STRING(WHILE_STATEMENT),
STRING(EXPRESSION),
STRING(RELATION),
STRING(DECLARATION),
STRING(PRINT_ITEM),
STRING(IDENTIFIER_DATA),
STRING(NUMBER_DATA),
STRING(STRING_DATA)
};
#undef STRING

View File

@@ -0,0 +1,313 @@
%{
#include <vslc.h>
#define NODE(type, data, n_children, children...) node_init(malloc(sizeof(node_t)), type, data, n_children, ##children)
%}
%define api.value.type {node_t}
%token FUNC PRINT RETURN CONTINUE IF THEN ELSE WHILE DO OPENBLOCK CLOSEBLOCK
%token VAR NUMBER IDENTIFIER STRING
%left '|' '&' '^'
%left '+' '-'
%left '*' '/'
%nonassoc UMINUS
%right '~'
//%expect 1
%nonassoc IF THEN
%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:
global_list {
root = NODE(PROGRAM, NULL, 1, $1);
}
;
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);
}
/* 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 '|' 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
yyerror ( const char *error )
{
fprintf ( stderr, "%s on line %d\n", error, yylineno );
exit ( EXIT_FAILURE );
}

View File

@@ -0,0 +1,30 @@
%{
#include <vslc.h>
%}
%option noyywrap
%option array
%option yylineno
WHITESPACE [\ \t\v\r\n]
COMMENT \/\/[^\n]+
QUOTED \"([^\"\n]|\\\")*\"
%%
{WHITESPACE}+ { /* Eliminate whitespace */ }
{COMMENT} { /* Eliminate comments */ }
func { return FUNC; }
print { return PRINT; }
return { return RETURN; }
continue { return CONTINUE; }
if { return IF; }
then { return THEN; }
else { return ELSE; }
while { return WHILE; }
do { return DO; }
begin { return OPENBLOCK; }
end { return CLOSEBLOCK; }
var { return VAR; }
[0-9]+ { return NUMBER; }
[A-Za-z_][0-9A-Za-z_]* { return IDENTIFIER; }
{QUOTED} { return STRING; }
. { return yytext[0]; }
%%

View File

@@ -0,0 +1,281 @@
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <tlhash.h>
/*********************************************************************
* Declarations of the utility functions for obtaining hashes, found *
* at the bottom of this file. *
*********************************************************************/
/* Little-endian, for x86-s */
#define CRC32_IEEE802_3 (0xedb88320)
static const uint32_t crc32_ieee802_3[256];
static const uint32_t *crc_table = (uint32_t *)crc32_ieee802_3;
static uint32_t crc32 ( void *input, size_t length );
/********************************
* External interface functions *
********************************/
/* Initializer
* Returns
* ENOMEM - if allocation of table entries fails.
*/
int
tlhash_init ( tlhash_t *tab, size_t n_buckets )
{
size_t i;
tab->n_buckets = n_buckets;
tab->size = 0;
tab->buckets = (tlhash_element_t **) calloc (
n_buckets, sizeof(tlhash_element_t *)
);
if ( tab->buckets == NULL )
return TLHASH_ENOMEM;
for ( i=0; i<n_buckets; i++ )
tab->buckets[i] = NULL;
return TLHASH_SUCCESS;
}
/* Finalizer
* Returns
* ENOENT - if there is no table to free.
*/
int
tlhash_finalize ( tlhash_t *tab )
{
size_t i;
if ( tab == NULL )
return TLHASH_ENOENT;
for ( i=0; i<tab->n_buckets; i++ )
{
tlhash_element_t *element = tab->buckets[i], *next;
while ( element != NULL )
{
next = element->next;
free ( element->key );
free ( element );
tab->size -= 1;
element = next;
}
}
free ( tab->buckets );
return TLHASH_SUCCESS;
}
/* Insert - find hash value, modulate over buckets, append to linked list
* Returns
* EEXIST - if an element is already indexed by this key
* ENOMEM - if allocation of element or key copy fails
*/
int
tlhash_insert (
tlhash_t *tab, void *key, size_t key_length, void *value
)
{
void *test_entry;
int test = tlhash_lookup ( tab, key, key_length, &test_entry );
if ( test != TLHASH_ENOENT )
return TLHASH_EEXIST;
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *element = malloc ( sizeof(tlhash_element_t) );
if ( element == NULL )
return TLHASH_ENOMEM;
void *key_copy = malloc ( key_length );
if ( key_copy == NULL )
{
free ( element );
return TLHASH_ENOMEM;
}
memcpy ( key_copy, key, key_length );
element->key = key_copy;
element->key_length = key_length;
element->value = value;
element->next = tab->buckets[bucket];
tab->buckets[bucket] = element;
tab->size += 1;
return TLHASH_SUCCESS;
}
/* Lookup - find hash value, modulate over buckets, search linked list
* Returns
* ENOENT - if no element is indexed by this key
*/
int
tlhash_lookup (
tlhash_t *tab, void *key, size_t key_length, void **value
)
{
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *el = tab->buckets[bucket];
*value = NULL;
while ( el != NULL )
{
if ( el->key_length == key_length && ! memcmp(el->key,key,key_length) )
{
*value = el->value;
break;
}
el = el->next;
}
if ( el != NULL )
return TLHASH_SUCCESS;
else
return TLHASH_ENOENT;
}
/* Removal - find hash value, modulate over buckets, delete entry
* Returns
* ENOENT - no such element to remove was found.
*/
int
tlhash_remove ( tlhash_t *tab, void *key, size_t key_length )
{
uint32_t hash = crc32 ( key, key_length );
size_t bucket = hash % tab->n_buckets;
tlhash_element_t *el = tab->buckets[bucket], *prev = NULL;
while ( el != NULL )
{
if ( el->key_length == key_length && ! memcmp(el->key,key,key_length) )
{
/* We have a match. */
if ( prev != NULL ) /* Remove from list if it's not the head */
prev->next = (void *)el->next;
else /* Substitute it if it IS the head */
tab->buckets[bucket] = el->next;
/* Free the container and key copy allocated by this lib */
free ( el->key );
free ( el );
break;
}
prev = el;
el = el->next;
}
if ( el == NULL )
return TLHASH_ENOENT;
else
{
tab->size -= 1;
return TLHASH_SUCCESS;
}
}
size_t
tlhash_size ( tlhash_t *tab )
{
return tab->size;
}
void
tlhash_keys ( tlhash_t *tab, void **keys )
{
size_t b, i = 0;
for ( b=0; b<tab->n_buckets; b++ )
{
tlhash_element_t *el = tab->buckets[b];
while ( el != NULL )
{
keys[i] = el->key;
i += 1;
el = el->next;
}
}
}
void
tlhash_values ( tlhash_t *tab, void **values )
{
size_t b, i = 0;
for ( b=0; b<tab->n_buckets; b++ )
{
tlhash_element_t *el = tab->buckets[b];
while ( el != NULL )
{
values[i] = el->value;
i += 1;
el = el->next;
}
}
}
/***************************************
* Hashing function and IEEE data blob *
***************************************/
static uint32_t
crc32 ( void *input, size_t length )
{
const uint8_t *data = (uint8_t *)input;
size_t i = 0;
uint32_t hash = 0xFFFFFFFF;
for ( i = 0; i<length; i++ )
hash = (hash>>8) ^ crc_table [ data[i] ^ (uint8_t)hash ];
return (hash^0xFFFFFFFF);
}
static const uint32_t
crc32_ieee802_3[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};

View File

@@ -0,0 +1,400 @@
#include <vslc.h>
static void node_print ( node_t *root, int nesting );
static void simplify_tree ( node_t **simplified, node_t *root );
static void node_finalize ( node_t *discard );
typedef struct stem_t *stem;
struct stem_t { const char *str; stem next; };
static void
tree_print(node_t* root, stem head);
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 */
void
destroy_syntax_tree ( void )
{
destroy_subtree ( root );
}
void
simplify_syntax_tree ( void )
{
simplify_tree ( &root, root );
}
extern bool new_print_style;
void
print_syntax_tree ( void )
{
if (new_print_style)
tree_print ( root, 0 );
// Old tree printing
else
node_print ( root, 0 );
}
// 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, ...)
{
va_list child_list;
*nd = (node_t) {
.type = type,
.data = data,
.entry = NULL,
.n_children = n_children,
.children = (node_t **) malloc ( n_children * sizeof(node_t *) )
};
va_start ( child_list, n_children );
for ( uint64_t i=0; i<n_children; i++ )
nd->children[i] = va_arg ( child_list, node_t * );
va_end ( child_list );
return nd;
}
static void
tree_print(node_t* root, stem head)
{
static const char *sdown = "", *slast = "", *snone = " ";
struct stem_t col = {0, 0}, *tail;
// Print stems of branches coming further down
for (tail = head; tail; tail = tail->next) {
if (!tail->next) {
if (!strcmp(sdown, tail->str))
printf("");
else
printf("%s", tail->str);
break;
}
printf("%s", tail->str);
}
if (root == NULL) {
// Secure against null pointers sent as root
printf("─(nil)\n");
return;
}
printf("─%s", node_string[root->type]);
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION ||
root->type == RELATION)
printf("(%s)", (char *) root->data);
else if (root->type == NUMBER_DATA)
printf("(%ld)", *((int64_t *)root->data));
putchar('\n');
if (!root->n_children) return;
if (tail && tail->str == slast)
tail->str = snone;
if (!tail) tail = head = &col;
else tail->next = &col;
for ( int64_t i=0; i < root->n_children; i++ ) {
col.str = root->n_children - i - 1 ? sdown : slast;
tree_print(root->children[i], head);
}
tail->next = 0;
}
/* Internal choices */
static void
node_print ( node_t *root, int nesting )
{
if ( root != NULL )
{
printf ( "%*c%s", nesting, ' ', node_string[root->type] );
if ( root->type == IDENTIFIER_DATA ||
root->type == STRING_DATA ||
root->type == EXPRESSION )
printf ( "(%s)", (char *) root->data );
else if ( root->type == NUMBER_DATA )
printf ( "(%ld)", *((int64_t *)root->data) );
putchar ( '\n' );
for ( int64_t i=0; i<root->n_children; i++ )
node_print ( root->children[i], nesting+1 );
}
else
printf ( "%*c%p\n", nesting, ' ', root );
}
static void
node_finalize ( node_t *discard )
{
if ( discard != NULL )
{
free ( discard->data );
free ( discard->children );
free ( discard );
}
}
static void
destroy_subtree ( node_t *discard )
{
if ( discard != NULL )
{
for ( uint64_t i=0; i<discard->n_children; i++ )
destroy_subtree ( discard->children[i] );
node_finalize ( discard );
}
}
static void
flatten(node_t **simplified, node_t *root)
{
/* This will flatten left-expanded lists */
if (!root)
return;
/* Do this recursivly */
for (int i = 0; i < root->n_children; i++)
flatten(&root->children[i], root->children[i]);
node_t **new_children, *result = root;
switch (root->type)
{
case GLOBAL_LIST:
case STATEMENT_LIST:
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->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);
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:
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;
node_finalize(root);
break;
}
*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)
{
case 1:
result = root->children[0];
if (root->data &&
result->type == NUMBER_DATA &&
result->data)
{
switch (*((char*)root->data))
{
case '-':
*((int64_t*)result->data) *= -1;
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);
}
break;
}
*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;
}

View File

@@ -0,0 +1,83 @@
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <vslc.h>
/* Global state */
node_t *root; // Syntax tree
tlhash_t *global_names; // Symbol table
char **string_list; // List of strings in the source
size_t n_string_list = 8; // Initial string list capacity (grow on demand)
size_t stringc = 0; // Initial string count
/* Command line option parsing for the main function */
static void options ( int argc, char **argv );
bool
print_full_tree = false,
print_simplified_tree = false,
print_symbol_table_contents = false,
print_generated_program = true,
new_print_style = true;
/* Entry point */
int
main ( int argc, char **argv )
{
options ( argc, argv );
yyparse(); // Generated from grammar/bison, constructs syntax tree
yylex_destroy(); // Free heap used by flex
if ( print_full_tree )
print_syntax_tree ();
simplify_syntax_tree (); // In tree.c
if ( print_simplified_tree )
print_syntax_tree ();
create_symbol_table (); // In ir.c
if ( print_symbol_table_contents )
print_symbol_table();
if ( print_generated_program )
generate_program (); // In generator.c
destroy_syntax_tree (); // In tree.c
destroy_symbol_table (); // In ir.c
}
static const char *usage =
"Command line options\n"
"\t-h\tOutput this text and halt\n"
"\t-t\tOutput the full syntax tree\n"
"\t-T\tOutput the simplified syntax tree\n"
"\t-s\tOutput the symbol table contents\n"
"\t-q\tQuiet: suppress output from the code generator\n"
"\t-u\tDo not use print style more like the tree command\n";
static void
options ( int argc, char **argv )
{
int o;
while ( (o=getopt(argc,argv,"htTsqu")) != -1 )
{
switch ( o )
{
case 'h':
printf ( "%s:\n%s", argv[0], usage );
exit ( EXIT_FAILURE );
break;
case 't': print_full_tree = true; break;
case 'T': print_simplified_tree = true; break;
case 's': print_symbol_table_contents = true; break;
case 'q': print_generated_program = false; break;
case 'u': new_print_style = false; break;
}
}
}

View File

@@ -0,0 +1,45 @@
VSLC := ../src/vslc
AS := gcc
# This updated makefile calls the relevant print for each assignment
# The added target compile will attempt to compile all vsl source files,
# for ps5 and ps6. Other programs are mostly not interesting after the their
# targeted assignment.
# Call `vslc -h` to see the available flags, and call `vslc [flags] < file.vsl`
# to compile a single file.
PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl))
PS3_EXAMPLES := $(patsubst ps3-simplify/%.vsl, ps3-simplify/%.sast, $(wildcard ps3-simplify/*.vsl))
PS4_EXAMPLES := $(patsubst ps4-symtab/%.vsl, ps4-symtab/%.sym, $(wildcard ps4-symtab/*.vsl))
PS5_EXAMPLES := $(patsubst ps5-codegen1/%.vsl, ps5-codegen1/%.S, $(wildcard ps5-codegen1/*.vsl))
PS6_EXAMPLES := $(patsubst ps6-codegen2/%.vsl, ps6-codegen2/%.S, $(wildcard ps6-codegen2/*.vsl))
PS5_OBJECTS := $(PS5_EXAMPLES:.S=.bin)
PS6_OBJECTS := $(PS6_EXAMPLES:.S=.bin)
# OBJECTS := $(PS5_OBJECTS) $(PS4_EXAMPLES:.sym=.bin) $(PS3_EXAMPLES:.sast=.bin) $(PS2_EXAMPLES:.ast=.bin)
OBJECTS := $(PS5_OBJECTS) $(PS6_OBJECTS)
all: clean $(PS2_EXAMPLES) $(PS3_EXAMPLES) $(PS4_EXAMPLES) $(PS5_EXAMPLES) $(PS6_EXAMPLES)
ps2: $(PS2_EXAMPLES)
ps3: $(PS3_EXAMPLES)
ps4: $(PS4_EXAMPLES)
ps5: $(PS5_EXAMPLES)
%.ast: %.vsl
$(VSLC) -t -q < $^ > $@ 2> $@
%.sast: %.vsl
$(VSLC) -T -q < $^ > $@ 2> $@
%.sym: %.vsl
$(VSLC) -s -q < $^ > $@ 2> $@
%.S: %.vsl
$(VSLC) < $^ > $@
# This target is only tested on x86-linux
%.bin: %.S
$(AS) -no-pie -o $@ $^
ps5-compile: $(PS5_OBJECTS)
ps6-compile: $(PS6_OBJECTS)
compile: $(OBJECTS)
clean:
-rm -r */*.ast */*.sast */*.sym */*.bin */*.S

View File

@@ -0,0 +1,14 @@
// checking that comments are ignored
// This program checks the assignment operators
func main()
begin
var a
a := 3
a += 1
a /= 2
a *= 32
a -= 2
print a
end

View File

@@ -0,0 +1,8 @@
func add(a, b) begin
return a + b
end
func main()
begin
print add(40, 2)
end

View File

@@ -0,0 +1,3 @@
func main() begin
print "Hello, World!"
end

View File

@@ -0,0 +1,25 @@
func main()
begin
var a, b, c, d
c := 1
a := 3
b := a + c // 4
d := a * 100 + 50
print "a", a
print "b", b
print "c", c
print "d", d
if a = 14 then
print 1, "N", d / 5 + a, "RPR", a, "TERS "
else
print "COMP", c, "L", "ERS "
print b, "R", a, " "
if a < b then
if d > 42 then
print b, "W", d, "ME"
else
print "L", b, "M", c
// A dangling else, what could go wrong?
end

View File

@@ -0,0 +1,19 @@
var global_var
func my_func(param)
begin
var local_var, local_var2
local_var := 1
end
var glob1, glob2
func main()
begin
var main_local_var
begin
var main_local_nested_var
main_local_nested_var := main_local_var
end
end

View File

@@ -0,0 +1,10 @@
// check parsing of do-while loop
func main()
begin
var i
i := 2
while i < 9000 do
i := i * i
print i
end

View File

@@ -0,0 +1,9 @@
func main() begin
var a, b
a := 1 + 2 + 4 + 5 + 6 + 7 + 8 + 9
b := (10 + 10 * 4) * (2 + 2 * (1 + 1)) / 10 + 2 * 5 + 6 / 3
if a = b then
print "The answer is", b
end

View File

@@ -0,0 +1,20 @@
func euclid ( a, b )
begin
if a < 0 then a := -a
if b < 0 then b := -b
if gcd ( a, b ) > 1 then
print "Greatest common divisor of", a, "and", b, "is", gcd ( a, b )
else
print a, "and", b, "are relative primes"
return 0
end
func gcd( a, b )
begin
var g
if b > 0 then
g := gcd ( b, a - ((a/b)*b) )
else
g := a
return g
end

View File

@@ -0,0 +1,33 @@
func my_func(a, b, c, d, e, f, g, h) begin
var i, j, k, l, m
i := a + b + d
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

View 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

View File

@@ -0,0 +1,11 @@
var global_var0, global_var1
func my_func(param0, param1) begin
var a
return 0
end
func main() begin
var a
print "a string"
end

View File

@@ -0,0 +1,24 @@
func main() begin
var a, b
a := 1
begin
var a
a := 2
b := 40
begin
var a
a := b + 2
print a, b
end
print a
begin
var b
b := 38
a := b + 3
print a, b
end
print a
end
print b
end

View File

@@ -0,0 +1,10 @@
func add(a, b) begin
print "adding", a, "and", b
return a + b
end
func main()
begin
print 2 + add(40, 2) + 2
return 0
end

View File

@@ -0,0 +1,4 @@
func main() begin
print "Hello, World!"
return 0
end

View File

@@ -0,0 +1,30 @@
// This program tests activation records, function call and return
func funcall ()
begin
var x,y,z
x := 5
y := 10
print "Calling my_function with parameters", x, y
z := my_function ( x, y )
print "The returned result is", z
z := my_other_function ()
print "The other returned result is", z
return 0
end
func my_function ( s, t )
begin
var u
u := s*s + t*t
print "Parameter s is", s
print "Parameter t is", t
print "The sum of their squares is", u
return u
end
func my_other_function ()
begin
var x
x := 42
return x
end

View File

@@ -0,0 +1,25 @@
func main() begin
var a, b
a := 1
begin
var a
a := 2
b := 40
begin
var a
a := b + 2
print a, b
end
print a
begin
var b
b := 38
a := b + 3
print a, b
end
print a
end
print b
return 0
end

View File

@@ -0,0 +1,20 @@
func euclid ( a, b )
begin
if a < 0 then a := -a
if b < 0 then b := -b
if gcd ( a, b ) > 1 then
print "Greatest common divisor of", a, "and", b, "is", gcd ( a, b )
else
print a, "and", b, "are relative primes"
return 0
end
func gcd( a, b )
begin
var g
if b > 0 then
g := gcd ( b, a - ((a/b)*b) )
else
g := a
return g
end

View 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

View File

@@ -0,0 +1,21 @@
// Approximate square root by the Newton/Raphson method for f(x) = x^2 - n
// f(x) = x^2 - n = 0
// f'(x) = 2x
// x{n+1} = x{n} - (x^2-n) / 2x
func newton ( n )
begin
print "The square root of", n, "is", improve ( n, 1 )
return 0
end
func improve ( n, estimate )
begin
var next
next := estimate - ( (estimate * estimate - n) / ( 2 * estimate ) )
if next - estimate = 0 then
// Integer precision converges at smallest int greater than the square
return next-1
else
return improve ( n, next )
end

View File

@@ -0,0 +1,83 @@
// 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 "Loops done in test_while:", b
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, 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

23
exercises/vsl.py Normal file
View File

@@ -0,0 +1,23 @@
from pygments.lexer import RegexLexer, bygroups
from pygments.token import *
class VSLLexer(RegexLexer):
name = "VSL"
aliases = ["vsl"]
filenames = ["*.vsl"]
tokens = {
"root": [
(r"[\ \t\v\r\n]+", Whitespace),
(r"\/\/[^\n]+", Comment.Single),
(r"var", Keyword.Declaration),
(r"func|print|return|continue|if|then|else|while|do|begin|end", Keyword),
(r"\^|\||:|=|\+|-|\*|\/|<|>|&", Operator),
(r"[0-9]+", Number.Integer),
(r"([A-Za-z_][0-9A-Za-z_]*)([\ \t\v\r\n]*)(\()", bygroups(Name.Function, Whitespace, Punctuation)),
(r"\(|\)|\[|\]|{|}", Punctuation),
(r"[A-Za-z_][0-9A-Za-z_]*", Name.Variable),
(r"\"([^\"\n]|\\\")*\"", String),
(r".", Text),
]
}

Binary file not shown.

BIN
slides/fixed_points.pdf Normal file

Binary file not shown.

BIN
slides/summary_1.pdf Normal file

Binary file not shown.

BIN
slides/summary_2.pdf Normal file

Binary file not shown.

BIN
x64_cheatsheet.pdf Normal file

Binary file not shown.