Compare commits

...

4 Commits

Author SHA1 Message Date
Øyvind Skaaden 2734cdb176 fixed bug with being to aggressive
- Is now more flexible
- Added resolve constant relations
2022-03-13 20:20:35 +01:00
Øyvind Skaaden e29492f7f8 Cleaned up the ir.h 2022-03-13 20:19:53 +01:00
Øyvind Skaaden b2ba4768c6 added some constant relations 2022-03-13 20:19:39 +01:00
Øyvind Skaaden 5d866e48a0 Added a pdf from the year 21 2022-03-13 20:19:29 +01:00
4 changed files with 137 additions and 33 deletions

Binary file not shown.

View File

@ -20,11 +20,18 @@ typedef struct n {
* @return Pointer to the initialized node
* */
node_t* node_init (
node_t *nd, node_index_t type, void *data, uint64_t n_children, ...
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 {

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
@ -154,18 +158,71 @@ destroy_subtree ( node_t *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:
@ -189,6 +246,10 @@ 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;
@ -239,6 +300,7 @@ resolve_constant_expressions(node_t **simplified, node_t *root)
switch (*(char*)root->data)
{
/* Assignments */
case '|': *lhs |= *rhs; break;
case '^': *lhs ^= *rhs; break;
case '&': *lhs &= *rhs; break;
@ -258,57 +320,80 @@ resolve_constant_expressions(node_t **simplified, node_t *root)
*simplified = result;
}
static void
flatten(node_t **simplified, node_t *root)
resolve_constant_relations( node_t** simplified, node_t* root)
{
/* This will flatten left-expanded lists */
if (!root)
return;
node_t **new_children, *result = root;
switch (root->type)
/* 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)
{
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;
/* 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;
for (int i = 0; i < root->n_children; i++)
simplify_tree(&root->children[i], root->children[i]);
//for (int i = 0; i < root->n_children; i++)
// simplify_tree(&root->children[i], root->children[i]);
/*
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);
flatten(&root, root);
// The following is experimental, will resolve the constant relations
resolve_constant_relations(&root, root);
*simplified = root;
}

View File

@ -7,6 +7,18 @@ 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