Skip to content

Commit

Permalink
Handle CF_BOOL_NEGATE in simplify_cfg (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 26, 2023
1 parent d62daf8 commit 4e2e78c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/simplify_cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,19 @@ static void update_statuses_with_instruction(const CfGraph *cfg, enum VarStatus
statuses[find_var_index(cfg, ins->operands[0])] = VS_UNPREDICTABLE;
statuses[destidx] = VS_DEFINED;
break;
case CF_BOOL_NEGATE:
switch(statuses[find_var_index(cfg, ins->operands[0])]) {
case VS_TRUE:
statuses[destidx] = VS_FALSE;
break;
case VS_FALSE:
statuses[destidx] = VS_TRUE;
break;
default:
statuses[destidx] = VS_DEFINED;
break;
}
break;
case CF_CONSTANT:
if (ins->data.constant.kind == CONSTANT_BOOL)
statuses[destidx] = ins->data.constant.data.boolean ? VS_TRUE : VS_FALSE;
Expand Down
3 changes: 1 addition & 2 deletions tests/other_errors/assert_fail.jou
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ def main() -> int:
assert lol and wat
assert lol and not wat # Output: Assertion 'lol and not wat' failed in file "tests/other_errors/assert_fail.jou", line 5.

# TODO: Compiler should be clever enough to not warn about missing return statement, but it isn't.
return 0
# No need for a "return 0" because compiler knows we will never get here.
3 changes: 1 addition & 2 deletions tests/other_errors/assert_fail_multiline.jou
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ def main() -> int:
and not wat
)

# TODO: Compiler should be clever enough to not warn about missing return statement, but it isn't.
return 0
# No need for a "return 0" because compiler knows we will never get here.
10 changes: 4 additions & 6 deletions tests/should_succeed/and_or_not.jou
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,10 @@ def main() -> int:
printf("%d", False or False or False)
printf("\n")

# TODO: presumably these should emit warnings too

# Output: Precedence and 0001
printf("Precedence and ")
printf("%d", not True and not True)
printf("%d", not True and not False)
printf("%d", not True and not True) # Warning: this code will never run
printf("%d", not True and not False) # Warning: this code will never run
printf("%d", not False and not True)
printf("%d", not False and not False)
printf("\n")
Expand All @@ -73,8 +71,8 @@ def main() -> int:
printf("Precedence or ")
printf("%d", not True or not True)
printf("%d", not True or not False)
printf("%d", not False or not True)
printf("%d", not False or not False)
printf("%d", not False or not True) # Warning: this code will never run
printf("%d", not False or not False) # Warning: this code will never run
printf("\n")

# Output: Side effects and aAbBcd
Expand Down
5 changes: 5 additions & 0 deletions tests/should_succeed/unreachable_warning.jou
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ def lots_of_unreachable_code() -> None:
puts("blah")
i = i+1

def with_not() -> None:
if not True:
puts("Hello") # Warning: this code will never run

def main() -> int:
after_return()
with_not()
# Can't run infinite loops (test script redirects output to file)
return 0

0 comments on commit 4e2e78c

Please sign in to comment.