removed obsolete and buggy ARRAYINIT
[fw/sdcc] / src / ds390 / gen.c
index 88ae8de7587b30c574eb79604d73ae2869a8b4f1..c412390a5978e259ab811682b5918e080b9546ec 100644 (file)
 #include "SDCCglobl.h"
 #include "newalloc.h"
 
-#ifdef HAVE_SYS_ISA_DEFS_H
-#include <sys/isa_defs.h>
-#else
-#ifdef HAVE_MACHINE_ENDIAN_H
-#include <machine/endian.h>
-#else
-#ifdef HAVE_ENDIAN_H
-#include <endian.h>
-#else
-#if !defined(__BORLANDC__) && !defined(_MSC_VER) && !defined(__MINGW32__) && !defined(__CYGWIN__)
-#warning "Cannot determine ENDIANESS of this machine assuming LITTLE_ENDIAN"
-#warning "If you running sdcc on an INTEL 80x86 Platform you are okay"
-#endif
-#endif
-#endif
-#endif
-
 #define BETTER_LITERAL_SHIFT
 
 char *aopLiteral (value * val, int offset);
@@ -109,22 +92,9 @@ static void saveRBank (int, iCode *, bool);
                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
                          IC_RESULT(x)->aop->type == AOP_STK )
 
-/* #define MOVA(x) if (strcmp(x,"a") && strcmp(x,"acc")) emitcode("mov","a,%s",x); */
-#define MOVA(x) { \
-                char *_mova_tmp = strdup(x); \
-                 if (strcmp(_mova_tmp,"a") && strcmp(_mova_tmp,"acc")) \
-                 { \
-                    emitcode("mov","a,%s",_mova_tmp); \
-                 } \
-                 free(_mova_tmp); \
-                }
-#define MOVB(x) { char *_movb_tmp = strdup(x); \
-                 if (strcmp(_movb_tmp,"b")) \
-                 { \
-                    emitcode("mov","b,%s",_movb_tmp); \
-                 } \
-                 free(_movb_tmp); \
-                }
+#define MOVA(x) _movA(x)
+#define MOVB(x) _movB(x)
+                
 #define CLRC    emitcode("clr","c")
 #define SETC    emitcode("setb","c")
 
@@ -210,6 +180,30 @@ emitcode (char *inst, char *fmt,...)
     va_end (ap);
 }
 
+//
+// Move the passed value into A unless it is already there.
+// 
+static void
+_movA(const char *s)
+{
+    if (strcmp(s,"a") && strcmp(s,"acc"))
+    { 
+       emitcode("mov","a,%s",s);
+    } 
+}
+
+//
+// Move the passed value into B unless it is already there.
+// 
+static void
+_movB(const char *s)
+{
+    if (strcmp(s,"b"))
+    { 
+       emitcode("mov","b,%s",s);
+    } 
+}
+
 /*-----------------------------------------------------------------*/
 /* getFreePtr - returns r0 or r1 whichever is free or can be pushed */
 /*-----------------------------------------------------------------*/
@@ -524,14 +518,20 @@ aopForSym (iCode * ic, symbol * sym, bool result, bool useDP2)
        if (stack_val < 0 && stack_val > -5) { /* between -5 & -1 */
            if (useDP2) {
                if (options.model == MODEL_FLAT24)
-                   emitcode ("mov", "dpx1,#!constbyte", (options.stack_loc >> 16) & 0xff);
-               TR_DPTR("#2");
+               {
+                   emitcode ("mov", "dpx1,#!constbyte",
+                             (options.stack_loc >> 16) & 0xff);
+               }
                emitcode ("mov", "dph1,_bpx+1");
+               
                emitcode ("mov", "dpl1,_bpx");
                emitcode ("mov","dps,#1");
            } else {
                if (options.model == MODEL_FLAT24)
-                   emitcode ("mov", "dpx,#!constbyte", (options.stack_loc >> 16) & 0xff);
+               {
+                   emitcode ("mov", "dpx,#!constbyte",
+                             (options.stack_loc >> 16) & 0xff);
+               }
                emitcode ("mov", "dph,_bpx+1");
                emitcode ("mov", "dpl,_bpx");
            }
@@ -557,13 +557,18 @@ aopForSym (iCode * ic, symbol * sym, bool result, bool useDP2)
            emitcode ("subb","a,#!constbyte",(stack_val >> 8) & 0xff);
            if (useDP2) {
                if (options.model == MODEL_FLAT24)
-                   emitcode ("mov", "dpx1,#!constbyte", (options.stack_loc >> 16) & 0xff);
-               TR_DPTR("#2");
+               {
+                   emitcode ("mov", "dpx1,#!constbyte",
+                             (options.stack_loc >> 16) & 0xff);
+               }
                emitcode ("mov", "dph1,a");
                emitcode ("mov", "dpl1,b");
            } else {
                if (options.model == MODEL_FLAT24)
-                   emitcode ("mov", "dpx,#!constbyte", (options.stack_loc >> 16) & 0xff);
+               {
+                   emitcode ("mov", "dpx,#!constbyte",
+                             (options.stack_loc >> 16) & 0xff);
+               }
                emitcode ("mov", "dph,a");
                emitcode ("mov", "dpl,b");
            }
@@ -666,7 +671,7 @@ aopForRemat (symbol * sym)
                "(%s %c 0x%04x)",
                OP_SYMBOL (IC_LEFT (ic))->rname,
                val >= 0 ? '+' : '-',
-               abs (val) & 0xffff);
+               abs (val) & 0xffffff);
   }
   else 
   {
@@ -2040,11 +2045,14 @@ saveRegisters (iCode * lic)
 
   /* if the registers have been saved already then
      do nothing */
-  if (ic->regsSaved || IFFUNC_ISNAKED(OP_SYM_TYPE(IC_LEFT(ic)))) return ;
+  if (ic->regsSaved || 
+      (IS_SYMOP(IC_LEFT(ic)) && IFFUNC_ISNAKED(OP_SYM_TYPE(IC_LEFT(ic)))))
+    return ;
 
   /* special case if DPTR alive across a function call then must save it 
      even though callee saves */
-  if (IFFUNC_CALLEESAVES(OP_SYMBOL (IC_LEFT (ic))->type)) {
+  if (IS_SYMOP(IC_LEFT(ic)) &&
+      IFFUNC_CALLEESAVES(OP_SYMBOL (IC_LEFT (ic))->type)) {
       int i;
       rsave = newBitVect(ic->rMask->size);
       for (i = DPL_IDX ; i <= B_IDX ; i++ ) {
@@ -2513,6 +2521,18 @@ static void genSend(set *sendSet)
     }
 }
 
+static void
+adjustEsp(const char *reg)
+{
+    emitcode ("anl","%s,#3", reg);
+    if (TARGET_IS_DS400)
+    {
+       emitcode ("orl","%s,#!constbyte",
+                 reg,
+                 (options.stack_loc >> 8) & 0xff);
+    }
+}
+
 /*-----------------------------------------------------------------*/
 /* genCall - generates a call statement                            */
 /*-----------------------------------------------------------------*/
@@ -2638,7 +2658,7 @@ genCall (iCode * ic)
              emitcode ("subb","a,#!constbyte",ic->parmBytes & 0xff);
              emitcode ("mov","sp,a");
              emitcode ("mov","a,esp");
-             emitcode ("anl","a,#3");
+             adjustEsp("a");
              emitcode ("subb","a,#!constbyte",(ic->parmBytes >> 8) & 0xff);
              emitcode ("mov","esp,a");   
              UNPROTECT_SP;
@@ -2756,7 +2776,7 @@ genPcall (iCode * ic)
              emitcode ("subb","a,#!constbyte",ic->parmBytes & 0xff);
              emitcode ("mov","sp,a");
              emitcode ("mov","a,esp");
-             emitcode ("anl","a,#3");
+             adjustEsp("a");
              emitcode ("subb","a,#!constbyte",(ic->parmBytes >> 8) & 0xff);
              emitcode ("mov","esp,a");   
              UNPROTECT_SP;
@@ -3039,7 +3059,7 @@ genFunction (iCode * ic)
                     }
                }
            }
-           // jwk: this needs a closer look
+           // TODO: this needs a closer look
            SPEC_ISR_SAVED_BANKS(currFunc->etype) = banksToSave;
        }
     }
@@ -3083,7 +3103,7 @@ genFunction (iCode * ic)
          emitcode ("push","_bpx+1");
          emitcode ("mov","_bpx,%s",spname);
          emitcode ("mov","_bpx+1,esp");
-         emitcode ("anl","_bpx+1,#3");
+         adjustEsp("_bpx+1");
       } else {
          if (options.useXstack) {
              emitcode ("mov", "r0,%s", spname);
@@ -3112,7 +3132,7 @@ genFunction (iCode * ic)
              emitcode ("add","a,#!constbyte", ((short) sym->stack & 0xff));
              emitcode ("mov","sp,a");
              emitcode ("mov","a,esp");
-             emitcode ("anl","a,#3");
+             adjustEsp("a");
              emitcode ("addc","a,#!constbyte", (((short) sym->stack) >> 8) & 0xff);
              emitcode ("mov","esp,a");
              UNPROTECT_SP;
@@ -3262,7 +3282,6 @@ genEndFunction (iCode * ic)
             * Restore any register banks saved by genFunction
             * in reverse order.
             */
-         // jwk: this needs a closer look
            unsigned savedBanks = SPEC_ISR_SAVED_BANKS(currFunc->etype);
            int ix;
          
@@ -3816,11 +3835,21 @@ bool aopOp3(iCode * ic)
 //            AOP_IS_DPTRn(IC_LEFT(ic)) ? "true" : "false",
 //            AOP_IS_DPTRn(IC_RIGHT(ic)) ? "true" : "false",
 //            AOP_IS_DPTRn(IC_RESULT(ic)) ? "true" : "false");
-//      );      
-
+//      );
     
-    // Right uses DPTR unless left or result is an AOP_STR.
-    aopOp (IC_RIGHT(ic),ic,FALSE, AOP_IS_STR(IC_LEFT(ic)) || AOP_IS_STR(IC_RESULT(ic)));
+    // Right uses DPTR unless left or result is an AOP_STR; however,
+    // if right is an AOP_STR, it must use DPTR regardless.
+    if ((AOP_IS_STR(IC_LEFT(ic)) || AOP_IS_STR(IC_RESULT(ic)))
+     && !AOP_IS_STR(IC_RIGHT(ic)))
+    {
+       useDp2 = TRUE;
+    }
+    else
+    {
+       useDp2 = FALSE;
+    }
+       
+    aopOp (IC_RIGHT(ic),ic,FALSE, useDp2);
     
     // if the right used DPTR, left MUST use DPTR2.
     // if the right used DPTR2, left MUST use DPTR.
@@ -5392,7 +5421,7 @@ genCmp (operand * left, operand * right,
       AOP_TYPE (right) == AOP_CRY)
     {
       emitcode ("mov", "c,%s", AOP (right)->aopu.aop_dir);
-      emitcode ("anl", "c,/%s", AOP (left)->aopu.aop_dir);
+      emitcode ("anl", "c,%s", AOP (left)->aopu.aop_dir);
     }
   else
     {
@@ -7900,8 +7929,6 @@ shiftR2Left2Result (operand * left, int offl,
 }
 #endif
 
-#if 0
-//REMOVE ME!!!
 /*-----------------------------------------------------------------*/
 /* shiftLLeftOrResult - shift left one byte from left, or to result */
 /*-----------------------------------------------------------------*/
@@ -7918,7 +7945,6 @@ shiftLLeftOrResult (operand * left, int offl,
   /* back to result */
   aopPut (AOP (result), "a", offr);
 }
-#endif
 
 #if 0
 //REMOVE ME!!!
@@ -8453,8 +8479,6 @@ genrshTwo (operand * result, operand * left,
 }
 #endif
 
-#if 0
-//REMOVE ME!!!
 /*-----------------------------------------------------------------*/
 /* shiftRLong - shift right one long from left to result           */
 /* offl = LSB or MSB16                                             */
@@ -8470,7 +8494,7 @@ shiftRLong (operand * left, int offl,
     werror(E_INTERNAL_ERROR, __FILE__, __LINE__);
   }
 
-  MOVA (aopGet (AOP (left), MSB32, FALSE, NULL));
+  MOVA (aopGet (AOP (left), MSB32, FALSE, FALSE, NULL));
   
   if (offl==MSB16) {
     // shift is > 8
@@ -8478,7 +8502,7 @@ shiftRLong (operand * left, int offl,
       emitcode ("rlc", "a");
       emitcode ("subb", "a,acc");
       emitcode ("xch", "a,%s",
-               aopGet(AOP(left), MSB32, FALSE, DP2_RESULT_REG));
+               aopGet(AOP(left), MSB32, FALSE, FALSE, DP2_RESULT_REG));
     } else {
       aopPut (AOP(result), zero, MSB32);
     }
@@ -8494,34 +8518,31 @@ shiftRLong (operand * left, int offl,
 
   if (isSameRegs && offl==MSB16) {
     emitcode ("xch",
-             "a,%s",aopGet (AOP (left), MSB24, FALSE, DP2_RESULT_REG));
+             "a,%s",aopGet (AOP (left), MSB24, FALSE, FALSE, DP2_RESULT_REG));
   } else {
     aopPut (AOP (result), "a", MSB32);
-    MOVA (aopGet (AOP (left), MSB24, FALSE, NULL));
+    MOVA (aopGet (AOP (left), MSB24, FALSE, FALSE, NULL));
   }
 
   emitcode ("rrc", "a");
   if (isSameRegs && offl==1) {
     emitcode ("xch", "a,%s",
-             aopGet (AOP (left), MSB16, FALSE, DP2_RESULT_REG));
+             aopGet (AOP (left), MSB16, FALSE, FALSE, DP2_RESULT_REG));
   } else {
     aopPut (AOP (result), "a", MSB24);
-    MOVA (aopGet (AOP (left), MSB16, FALSE, NULL));
+    MOVA (aopGet (AOP (left), MSB16, FALSE, FALSE, NULL));
   }
   emitcode ("rrc", "a");
   aopPut (AOP (result), "a", MSB16 - offl);
 
   if (offl == LSB)
     {
-      MOVA (aopGet (AOP (left), LSB, FALSE, NULL));
+      MOVA (aopGet (AOP (left), LSB, FALSE, FALSE, NULL));
       emitcode ("rrc", "a");
       aopPut (AOP (result), "a", LSB);
     }
 }
-#endif
 
-#if 0
-//REMOVE ME!!!
 /*-----------------------------------------------------------------*/
 /* genrshFour - shift four byte by a known amount != 0             */
 /*-----------------------------------------------------------------*/
@@ -8529,22 +8550,24 @@ static void
 genrshFour (operand * result, operand * left,
            int shCount, int sign)
 {
-  D (emitcode (";", "genrshFour");
-    );
+  D (emitcode (";", "genrshFour"););
 
   /* if shifting more that 3 bytes */
   if (shCount >= 24)
     {
       shCount -= 24;
+      _startLazyDPSEvaluation();
       if (shCount)
        shiftR1Left2Result (left, MSB32, result, LSB, shCount, sign);
       else
        movLeft2Result (left, MSB32, result, LSB, sign);
       addSign (result, MSB16, sign);
+      _endLazyDPSEvaluation();
     }
   else if (shCount >= 16)
     {
       shCount -= 16;
+      _startLazyDPSEvaluation();
       if (shCount)
        shiftR2Left2Result (left, MSB24, result, LSB, shCount, sign);
       else
@@ -8553,12 +8576,16 @@ genrshFour (operand * result, operand * left,
          movLeft2Result (left, MSB32, result, MSB16, sign);
        }
       addSign (result, MSB24, sign);
+      _endLazyDPSEvaluation();
     }
   else if (shCount >= 8)
     {
       shCount -= 8;
+      _startLazyDPSEvaluation();
       if (shCount == 1)
-       shiftRLong (left, MSB16, result, sign);
+       {
+           shiftRLong (left, MSB16, result, sign);
+       }
       else if (shCount == 0)
        {
          movLeft2Result (left, MSB16, result, LSB, 0);
@@ -8574,9 +8601,11 @@ genrshFour (operand * result, operand * left,
          shiftR1Left2Result (left, MSB32, result, MSB24, shCount, sign);
          addSign (result, MSB32, sign);
        }
+       _endLazyDPSEvaluation();
     }
   else
-    {                          /* 1 <= shCount <= 7 */
+    {  
+       /* 1 <= shCount <= 7 */
       if (shCount <= 2)
        {
          shiftRLong (left, LSB, result, sign);
@@ -8591,7 +8620,6 @@ genrshFour (operand * result, operand * left,
        }
     }
 }
-#endif
 
 #ifdef BETTER_LITERAL_SHIFT
 /*-----------------------------------------------------------------*/
@@ -8615,7 +8643,8 @@ genRightShiftLiteral (operand * left,
   if ((shCount != 0)
    && (shCount < (size * 8))
    && (size != 1)
-   && (size != 2))
+   && (size != 2)
+   /* && (size != 4) in a minute... */)
   {
       D(emitcode (";", "genRightShiftLiteral wimping out"););  
       return FALSE;
@@ -8664,7 +8693,7 @@ genRightShiftLiteral (operand * left,
        case 2:
          genrshTwo (result, left, shCount, sign);
          break;
-#if 0
+#if 1
        case 4:
          genrshFour (result, left, shCount, sign);
          break;
@@ -9403,9 +9432,10 @@ genFarPointerGet (operand * left,
          _endLazyDPSEvaluation ();
       }
     pi->generated = 1;
-  } else if ((OP_SYMBOL(left)->ruonly || AOP_INDPTRn(left)) && 
+  } else if ((AOP_IS_STR(left) || AOP_INDPTRn(left)) && 
             AOP_SIZE(result) > 1 &&
-            (OP_SYMBOL (left)->liveTo > ic->seq || ic->depth)) {
+            IS_SYMOP(left) &&
+            (OP_SYMBOL(left)->liveTo > ic->seq || ic->depth)) {
       
       size = AOP_SIZE (result) - 1;
       if (AOP_INDPTRn(left)) {
@@ -10502,6 +10532,8 @@ genAddrOf (iCode * ic)
       /* if 10 bit stack */
       if (options.stack10bit) {
          char buff[10];
+         int  offset;
+         
          tsprintf(buff, sizeof(buff), 
                   "#!constbyte",(options.stack_loc >> 16) & 0xff);
          /* if it has an offset then we need to compute it */
@@ -10520,9 +10552,13 @@ genAddrOf (iCode * ic)
                                             ((char) sym->stack )) & 0xff);
              emitcode ("mov", "b,a");
              emitcode ("mov", "a,_bpx+1");
-             emitcode ("addc","a,#!constbyte", (((sym->stack < 0) ? 
-                                             ((short) (sym->stack - _G.nRegsSaved)) :
-                                             ((short) sym->stack )) >> 8) & 0xff);
+             
+             offset = (((sym->stack < 0) ? 
+                        ((short) (sym->stack - _G.nRegsSaved)) :
+                        ((short) sym->stack )) >> 8) & 0xff;
+         
+             emitcode ("addc","a,#!constbyte", offset);
+
              aopPut (AOP (IC_RESULT (ic)), "b", 0);
              aopPut (AOP (IC_RESULT (ic)), "a", 1);
              aopPut (AOP (IC_RESULT (ic)), buff, 2);
@@ -10569,13 +10605,13 @@ genAddrOf (iCode * ic)
       if (offset) {
          switch (offset) {
          case 1:
-             tsprintf(s, sizeof(s), "!his",sym->rname);
+             tsprintf(s, sizeof(s), "#!his",sym->rname);
              break;
          case 2:
-             tsprintf(s, sizeof(s), "!hihis",sym->rname);
+             tsprintf(s, sizeof(s), "#!hihis",sym->rname);
              break;
          case 3:
-             tsprintf(s, sizeof(s), "!hihihis",sym->rname);
+             tsprintf(s, sizeof(s), "#!hihihis",sym->rname);
              break;
          default: /* should not need this (just in case) */
              SNPRINTF (s, sizeof(s), "#(%s >> %d)",
@@ -10596,6 +10632,7 @@ release:
 
 }
 
+#if 0 // obsolete, and buggy for != xdata
 /*-----------------------------------------------------------------*/
 /* genArrayInit - generates code for address of                       */
 /*-----------------------------------------------------------------*/
@@ -10704,6 +10741,7 @@ genArrayInit (iCode * ic)
     
     freeAsmop (IC_LEFT(ic), NULL, ic, TRUE);
 }
+#endif
 
 /*-----------------------------------------------------------------*/
 /* genFarFarAssign - assignment when both are in far space         */
@@ -11153,7 +11191,7 @@ genCast (iCode * ic)
   /* also, if the source is a bit, we don't need to sign extend, because
    * it can't possibly have set the sign bit.
    */
-  if (SPEC_USIGN (rtype) || !IS_SPEC (rtype) || AOP_TYPE (right) == AOP_CRY)
+  if (!IS_SPEC (rtype) || SPEC_USIGN (rtype) || AOP_TYPE (right) == AOP_CRY)
     {
       while (size--)
        {
@@ -12700,13 +12738,13 @@ gen390Code (iCode * lic)
   }
 #if 1
   /* print the allocation information */
-  if (allocInfo)
+  if (allocInfo && currFunc)
     printAllocInfo (currFunc, codeOutFile);
 #endif
   /* if debug information required */
   if (options.debug && currFunc)
     {
-      cdbSymbol (currFunc, cdbFile, FALSE, TRUE);
+      debugFile->writeFunction(currFunc);
       _G.debugLine = 1;
       if (IS_STATIC (currFunc->etype))
        emitcode ("", "F%s$%s$0$0 ==.", moduleName, currFunc->name);
@@ -12935,9 +12973,11 @@ gen390Code (iCode * lic)
          else addSet (&_G.sendSet, ic);
          break;
 
+#if 0 // obsolete, and buggy for != xdata
        case ARRAYINIT:
            genArrayInit(ic);
            break;
+#endif
            
        default:
          ic = ic;