From 867be799d274290f800f1402c7bb723eb3475c79 Mon Sep 17 00:00:00 2001 From: phobos2077 Date: Fri, 9 Jun 2023 00:54:45 +0200 Subject: [PATCH] Fix optimizer not treating CALL as variable use (valid case when variable contains procedure name) - closes #6 --- optimize.c | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/optimize.c b/optimize.c index f09a618..480e81f 100644 --- a/optimize.c +++ b/optimize.c @@ -83,6 +83,16 @@ typedef struct { int flags; } VarUsage; +static void MarkVariableRead(VarUsage* usage, int var, int currstatement, int whiledepth) { + usage[var].lastuse = currstatement; + if (whiledepth) usage[var].flags |= VU_LAST_READ_IN_WHILE; + else usage[var].flags &= ~VU_LAST_READ_IN_WHILE; + if (!usage[var].firstuse) { + usage[var].firstuse = currstatement; + if (whiledepth) usage[var].flags |= VU_FIRST_READ_IN_WHILE; + } +} + static void FindVarUsage(const Node* node, VarUsage* usage, int varCount) { int depth = 1, currstatement = 0, i = 0, whiledepth = 0, ifdepth = 0, var, expressiondepth; memset(usage, 0, varCount * sizeof(VarUsage)); @@ -129,13 +139,7 @@ static void FindVarUsage(const Node* node, VarUsage* usage, int varCount) { } else if (!pure && currstatement == usage[var].firstassign) usage[var].flags &= ~VU_FIRST_ASSIGN_IS_PURE; } if (node->token != T_ASSIGN) { - usage[var].lastuse = currstatement; - if (whiledepth) usage[var].flags |= VU_LAST_READ_IN_WHILE; - else usage[var].flags &= ~VU_LAST_READ_IN_WHILE; - if (!usage[var].firstuse) { - usage[var].firstuse = currstatement; - if (whiledepth) usage[var].flags |= VU_FIRST_READ_IN_WHILE; - } + MarkVariableRead(usage, var, currstatement, whiledepth); } } break; @@ -148,17 +152,16 @@ static void FindVarUsage(const Node* node, VarUsage* usage, int varCount) { case T_END_EXPRESSION: expressiondepth--; break; case T_SYMBOL: if ((var = LookupVariable(node)) != -1) { - usage[var].lastuse = currstatement; - if (whiledepth) usage[var].flags |= VU_LAST_READ_IN_WHILE; - else usage[var].flags &= ~VU_LAST_READ_IN_WHILE; - if (!usage[var].firstuse) { - usage[var].firstuse = currstatement; - if (whiledepth) usage[var].flags |= VU_FIRST_READ_IN_WHILE; - } + MarkVariableRead(usage, var, currstatement, whiledepth); } } } break; + case T_CALL: + if ((++node)->token == T_SYMBOL && ((var = LookupVariable(node)) != -1)) { + MarkVariableRead(usage, var, currstatement, whiledepth); + } + break; } } assert(node->token == T_END);