Allow native code peephole opt to be used when stack repair is enabled.

I think the reason this was originally disallowed is that the old code sequence for stack repair code (in ORCA/C 2.1.0) ended with TYA. If this was followed by STA dp or STA abs, the native code peephole optimizer (prior to commit 7364e2d2d3) would have turned the combination into a STY instruction. That is invalid if the value in A is needed. This could come up, e.g., when assigning the return value from a function to two different variables.

This is no longer an issue, because the current code sequence for stack repair code no longer ends in TYA and is not susceptible to the same kind of invalid optimization. So it is no longer necessary to disable the native code peephole optimizer when using stack repair code (either for all calls or just varargs calls).
This commit is contained in:
Stephen Heumann 2022-12-10 20:32:53 -06:00
parent 7364e2d2d3
commit 32975b720f
7 changed files with 5 additions and 9 deletions

View File

@ -363,7 +363,6 @@ var
symLength: integer; {length of debug symbol table}
toolParms: boolean; {generate tool format parameters?}
volatile: boolean; {has a volatile qualifier been used?}
hasVarargsCall: boolean; {does current function call any varargs fns?}
{desk accessory variables}
{------------------------}

View File

@ -3463,8 +3463,6 @@ var
Gen1tName(pc_cup, ord(hasVarargs and strictVararg),
UsualUnaryConversions, fname);
end; {else}
if hasVarargs then
hasVarargsCall := true;
end {if}
else
GenTool(pc_tl1, ftype^.toolNum, long(ftype^.ftype^.size).lsw,

View File

@ -7623,7 +7623,7 @@ while bk <> nil do begin
end; {while}
bk := bk^.next;
end; {while}
if saveStack or checkStack or (strictVararg and hasVarargsCall) then begin
if saveStack or checkStack then begin
stackLoc := minSize;
minSize := minSize + 2;
localSize := localSize + 2;

View File

@ -1963,7 +1963,7 @@ var
begin {GenNative}
{ writeln('GenNative: ',p_opcode:4, ', mode=', ord(p_mode):1,
' operand=', p_operand:1); {debug}
if npeephole and not (strictVararg and hasVarargsCall) then begin
if npeephole then begin
if (nnextspot = 1) and not (p_opcode in nleadOpcodes) then begin
if p_opcode <> d_end then
if registers then

View File

@ -4035,7 +4035,6 @@ if isFunction then begin
end; {if}
Gen2Name(dc_str, segType, 0, fName);
doingMain := variable^.name^ = 'main';
hasVarargsCall := false;
firstCompoundStatement := true;
Gen0 (dc_pin);
if not isAsm then

View File

@ -3542,8 +3542,6 @@ if ch in ['a','d','e','i','l','p','u','w'] then begin
loopOptimizations := odd(val >> 5);
strictVararg := not odd(val >> 6);
fastMath := odd(val >> 7);
if saveStack then
npeepHole := false;
if token.kind <> eolsy then
Error(11);
end {else if}

View File

@ -1460,7 +1460,9 @@ Contrary to all common sense, the ANSI standard says this statement is legal IF
Beginning with ORCA/C 2.1, this statement will work. Note that in keeping with the ANSI standard, this call and others like it only work if the function is properly defined with a prototyped variable argument parameter list.
There are two undesirable side effects, though. First, all function calls to a variable argument function are surrounded by extra stack repair code, even if you set optimization bit 3. (This bit turns off stack repair code.) This increases code size and slows a program down. Sometimes these changes are noticeable, or even dramatic. Second, native code peephole optimization is always disabled when stack repair code is in use, so you loose another optimization if you do not use this one.
There is an undesirable side effect, though. All function calls to a variable argument function are surrounded by extra stack repair code, even if you set optimization bit 3. (This bit turns off stack repair code.) This increases code size and slows a program down. Sometimes these changes are noticeable, or even dramatic.
Prior to ORCA/C 2.2.0 B7, native code peephole optimization was always disabled when stack repair code was in use, so you would also lose another optimization if you did not use this one. As of ORCA/C 2.2.0 B7, this is no longer the case: native code peephole optimization can be used without disabling stack repair code.
Turning this optimization on means ORCA/C is no longer strictly in compliance with the ANSI standard. For strict compliance, you should leave stack repair code on for variable argument functions. You can add any or all of the other optimizations and remain ANSI compliant, so this pragma will work with all ANSI C programs: