Skip to content

Commit

Permalink
Adds removal of variables with fixed values.
Browse files Browse the repository at this point in the history
This is disabled by default, enable with "-O +fixed_vars".
  • Loading branch information
dmsc committed May 27, 2020
1 parent efed807 commit d29c247
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 7 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -286,6 +286,15 @@ The optimization sub-options are:
a variable initialized to the value. The initialization code is added
before any statement in the program, and tries to use the minimum number
of bytes posible.
- `fixed_vars`: This is the complement of the `const_replace` option, tries to
identify variables with a fixed value in the whole program and removes the
variable. Use this optimization when converting original basic listings, as
reversing the constant replacing gives a simpler listing and allows to apply
further optimizations. Note that currently this option can produce bad
results, as it does not follows the program flow and can't detect if a
variable is used before the first assignment, so it is not enabled by
default. You need to check each removed variable, as printed in the output
and in the comments in the resulting program.

Note that options can be changed at any place in the file, this is an example
of changing the parser mode in the middle of the file:
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Expand Up @@ -209,12 +209,12 @@ int main(int argc, char **argv)
{
// Not an optimize option, go back and
// assume "set all optimizations".
do_optimize = -1;
do_optimize = optimize_all();
optind --;
}
}
else
do_optimize = -1;
do_optimize = optimize_all();
break;
case 'n':
max_line_len = atoi(optarg);
Expand Down
18 changes: 18 additions & 0 deletions src/optimize.c
Expand Up @@ -40,10 +40,17 @@ enum optimize_levels optimize_option(const char *opt)
return OPT_LINE_NUM;
else if( 0 == strcmp(opt, "const_replace") )
return OPT_CONST_VARS;
else if( 0 == strcmp(opt, "fixed_vars") )
return OPT_FIXED_VARS;
else
return 0;
}

enum optimize_levels optimize_all(void)
{
return OPT_CONST_FOLD | OPT_NUMBER_TOK | OPT_COMMUTE | OPT_LINE_NUM | OPT_CONST_VARS;
}

int optimize_program(program *pgm, int level)
{
// Convert program to expression tree
Expand All @@ -60,6 +67,17 @@ int optimize_program(program *pgm, int level)
if( level & OPT_COMMUTE )
err |= opt_commute(ex);

if( level & OPT_FIXED_VARS )
{
err |= opt_replace_fixed_vars(ex);
// After fixed variable removal, redo constant propagation and try again
if( level & OPT_CONST_FOLD )
{
err |= opt_constprop(ex);
err |= opt_replace_fixed_vars(ex);
}
}

if( level & OPT_LINE_NUM )
err |= opt_remove_line_num(ex);

Expand Down
6 changes: 5 additions & 1 deletion src/optimize.h
Expand Up @@ -24,9 +24,13 @@ enum optimize_levels {
OPT_NUMBER_TOK = 2,
OPT_COMMUTE = 4,
OPT_LINE_NUM = 8,
OPT_CONST_VARS = 16
OPT_CONST_VARS = 16,
OPT_FIXED_VARS = 32
};

// Returns the "standard" optimizations
enum optimize_levels optimize_all(void);

// Returns the optimization option from the text
enum optimize_levels optimize_option(const char *txt);

Expand Down

0 comments on commit d29c247

Please sign in to comment.