From df42ce257f7611ce9f1dfbfa7a2543093c5546b3 Mon Sep 17 00:00:00 2001
From: Stephen Heumann <stephenheumann@gmail.com>
Date: Sat, 11 Nov 2017 23:18:16 -0600
Subject: [PATCH] Fix issue where certain address computations could be
 improperly restricted to a 32K or 64K range (even when using the large memory
 model).

This could occur with computations where multiple variables were added to a pointer.

The following program is an example that was miscompiled:

#pragma optimize 1
#pragma memorymodel 1

char c[80000];

int main(void) {
    unsigned i = 30000, j = 40000;
    c[70000] = 3;
    return *(c+i+j); /* should return 3 */
}
---
 DAG.pas | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/DAG.pas b/DAG.pas
index ad07cf1..2983bcf 100644
--- a/DAG.pas
+++ b/DAG.pas
@@ -1522,11 +1522,16 @@ case op^.opcode of			{check for optimizations of this node}
             end; {if}
          end {else if}
       else if op^.left^.opcode = pc_ixa then begin
-         op2 := op^.left;
-         op^.left := op^.left^.left;
-         op2^.left := op^.right;
-         op2^.opcode := pc_adi;
-         op^.right := op2;
+         if smallMemoryModel then
+            if op^.left^.left^.opcode in [pc_lao,pc_lda] then
+               if op^.left^.left^.q = 0 then begin
+                  op2 := op^.left;
+                  op^.left := op^.left^.left;
+                  op2^.left := op^.right;
+                  op2^.opcode := pc_adi;
+                  op^.right := op2;
+                  op^.optype := cgUWord;
+                  end; {if}
          end; {else if}
       end; {case pc_ixa}