Optimize generated code for some indexing ops in large memory model.

This generates slightly better code for indexing a global/static char array with a signed 16-bit index and a positive offset, e.g. a[i+1].

Here is an example that is affected:

#pragma memorymodel 1
#pragma optimize -1
char a[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int main(int argc, char *argv[]) {
        return a[argc+2];
}
This commit is contained in:
Stephen Heumann 2024-03-04 19:37:52 -06:00
parent 995885540b
commit 60b472a99e

12
Gen.pas
View File

@ -3617,11 +3617,17 @@ else begin
else begin
if op^.left^.opcode = pc_lao then begin
GenTree(op^.right);
if signed then
GenImplied(m_tay);
GenNative(m_ldx_imm, immediate, op^.left^.q, op^.left^.lab, shift16);
if signed then begin
GenImpliedForFlags(m_tay);
lab2 := GenLabel;
GenNative(m_bpl, relative, lab2, nil, 0);
GenImplied(m_dex);
GenLab(lab2);
signed := false;
end; {if}
GenImplied(m_clc);
GenNative(m_adc_imm, immediate, op^.left^.q, op^.left^.lab, 0);
GenNative(m_ldx_imm, immediate, op^.left^.q, op^.left^.lab, shift16);
end {if}
else begin
gLong.preference := onStack;