From 73d7f2aa18807e3fbca99b2c214bda48250a555b Mon Sep 17 00:00:00 2001
From: nickysn <nickysn@gmail.com>
Date: Sun, 8 Jun 2014 21:14:57 +0000
Subject: [PATCH] * let the compiler generate the stack segment in i8086 near
 data memory models   as well. Even though, in these models, the stack is
 dynamically allocated   (because it goes on top of the heap, but the heap is
 variable size), there are   still benefits:   1) the program will run on a
 larger stack during initialization, before the      actual stack (and heap)
 are ready   2) in cases, when the system is extremely low on memory, DOS will
 reject to      load the program if there's not enough memory for the stack
 the program      requires. This way the startup code can be further
 simplified by omitting      the 'not enough memory' check in the future (when
 we add the minimum heap      size to the executable reserved space as well).

git-svn-id: trunk@27903 -
---
 compiler/i8086/n8086util.pas | 4 ++--
 compiler/x86/agx86nsm.pas    | 7 ++-----
 rtl/msdos/prt0comn.asm       | 7 ++++---
 3 files changed, 8 insertions(+), 10 deletions(-)

diff --git a/compiler/i8086/n8086util.pas b/compiler/i8086/n8086util.pas
index a8b83bbcd3..da19bfebf9 100644
--- a/compiler/i8086/n8086util.pas
+++ b/compiler/i8086/n8086util.pas
@@ -47,8 +47,7 @@ implementation
   class procedure ti8086nodeutils.InsertMemorySizes;
     begin
       inherited;
-      if current_settings.x86memorymodel in x86_far_data_models then
-        InsertStackSegment;
+      InsertStackSegment;
     end;
 
 
@@ -73,6 +72,7 @@ implementation
           dec(stacksizeleft,stackblock);
           inc(i);
         end;
+      current_asmdata.asmlists[al_globals].concat(tai_symbol.Createname_global('___stacktop',AT_DATA,0));
     end;
 
 
diff --git a/compiler/x86/agx86nsm.pas b/compiler/x86/agx86nsm.pas
index 0d32d96a40..35c1054a37 100644
--- a/compiler/x86/agx86nsm.pas
+++ b/compiler/x86/agx86nsm.pas
@@ -1226,16 +1226,13 @@ interface
       AsmWriteLn('SECTION .fpc');
       { WLINK requires class=bss in order to leave the BSS section out of the executable }
       AsmWriteLn('SECTION .bss class=bss');
-      if current_settings.x86memorymodel in x86_far_data_models then
+      if current_settings.x86memorymodel<>mm_tiny then
         AsmWriteLn('SECTION stack stack class=stack align=16');
       { group these sections in the same segment }
       if current_settings.x86memorymodel=mm_tiny then
         AsmWriteLn('GROUP dgroup text rodata data fpc bss')
       else
-        if current_settings.x86memorymodel in x86_far_data_models then
-          AsmWriteLn('GROUP dgroup rodata data fpc bss stack')
-        else
-          AsmWriteLn('GROUP dgroup rodata data fpc bss');
+        AsmWriteLn('GROUP dgroup rodata data fpc bss stack');
       if paratargetdbg in [dbg_dwarf2,dbg_dwarf3,dbg_dwarf4] then
         begin
           AsmWriteLn('SECTION .debug_frame  use32 class=DWARF');
diff --git a/rtl/msdos/prt0comn.asm b/rtl/msdos/prt0comn.asm
index c5fb06b773..c968eb5a16 100644
--- a/rtl/msdos/prt0comn.asm
+++ b/rtl/msdos/prt0comn.asm
@@ -56,6 +56,9 @@
 %ifdef __FAR_DATA__
         extern ___stack
 %endif
+%ifdef __NEAR_DATA__
+        extern ___stacktop
+%endif
 
         extern __SaveInt00
 
@@ -72,7 +75,7 @@
     %ifdef __NEAR_DATA__
         ; init the stack
         mov ss, bx
-        mov sp, stacktop
+        mov sp, ___stacktop wrt dgroup
     %endif
 %endif
 
@@ -475,8 +478,6 @@ __nullarea:
 
     %ifdef __NEAR_DATA__
         segment stack stack class=stack
-        resb 256
-        stacktop:
     %else
         segment data
         ; add reference to the beginning of stack, so the object module,