From 1502e481880284e160e76e79cbf7ff35a0f95ea6 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Tue, 11 Jul 2017 21:15:57 -0500 Subject: [PATCH] Fix bug causing incorrect code generation in programs that use the 'volatile' keyword. This bug could both cause accesses to volatile variables to be omitted, and also cause other expressions to be erroneously optimized out in certain circumstances. As an example, both the access of x and the call to bar() would be erroneously removed in the following program: #pragma optimize 1 volatile int x; int bar(void); void foo(void) { if(x) ; if(bar()) ; } Note that this patch disables even more optimizations than previously if the 'volatile' keyword is used anywhere in a translation unit. This is necessary for correctness given the current design of ORCA/C, but it means that care should be taken to avoid unnecessary use of 'volatile'. --- DAG.pas | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/DAG.pas b/DAG.pas index 634ffba..7f41db6 100644 --- a/DAG.pas +++ b/DAG.pas @@ -479,13 +479,19 @@ var result: boolean; {temp result} begin {SideEffects} - if (op = nil) or volatile then - SideEffects := false + if op = nil then begin + if volatile then + SideEffects := true + else + SideEffects := false + end {if} else if op^.opcode in [pc_mov,pc_cbf,pc_cop,pc_cpi,pc_cpo,pc_gil,pc_gli,pc_gdl, pc_gld,pc_iil,pc_ili,pc_idl,pc_ild,pc_lil,pc_lli,pc_ldl, pc_lld,pc_sbf,pc_sro,pc_sto,pc_str,pc_cui,pc_cup,pc_tl1] then SideEffects := true + else if op^.opcode = pc_ldc then + SideEffects := false else SideEffects := SideEffects(op^.left) or SideEffects(op^.right); end; {SideEffects}