From 730544a6cefd67d7cc84d9f07a8c29ab2cbc58af Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Wed, 1 Nov 2017 22:56:40 -0500 Subject: [PATCH] Fix optimizer bug that could limit certain address calculations to a 32k or 64k range even when using the large memory model. This optimization could apply when indexing into an array whose elements are a power-of-2 size using a 16-bit index value. It is now only used when addressing arrays on the stack (which are necessarily smaller than 64k). The following program demonstrates the problem: #pragma optimize 1 #pragma memorymodel 1 long c[40000]; int main(void) { int i = 30000; c[30000] = 3; return c[i]; /* should return 3 */ } --- DAG.pas | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/DAG.pas b/DAG.pas index 8edb5fc..0747808 100644 --- a/DAG.pas +++ b/DAG.pas @@ -806,23 +806,24 @@ case op^.opcode of {check for optimizations of this node} if op^.right^.left^.opcode = pc_cnv then begin fromtype.i := (op^.right^.left^.q & $00F0) >> 4; if fromType.optype in [cgByte,cgUByte,cgWord,cgUWord] then - begin - if fromType.optype = cgByte then - op^.right^.left^.q := $02 - else if fromType.optype = cgUByte then - op^.right^.left^.q := $13 - else - op^.right^.left := op^.right^.left^.left; - with op^.right^.right^ do begin - lq := lval; - lval := 0; - q := long(lq).lsw; - optype := cgUWord; - end; {with} - op^.right^.opcode := pc_shl; - op^.opcode := pc_ixa; - PeepHoleOptimization(opv); - end; {if} + if op^.left^.opcode = pc_lda then + begin + if fromType.optype = cgByte then + op^.right^.left^.q := $02 + else if fromType.optype = cgUByte then + op^.right^.left^.q := $13 + else + op^.right^.left := op^.right^.left^.left; + with op^.right^.right^ do begin + lq := lval; + lval := 0; + q := long(lq).lsw; + optype := cgUWord; + end; {with} + op^.right^.opcode := pc_shl; + op^.opcode := pc_ixa; + PeepHoleOptimization(opv); + end; {if} end; {if} end {if} else if op^.right^.opcode = pc_cnv then begin