Fix null pointer deref that caused build failures on Solaris
[fw/sdcc] / src / ds390 / gen.c
index 88ae8de7587b30c574eb79604d73ae2869a8b4f1..d417b07fc9afe1de751f7ed6112b74f7b547de3f 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 */
 /*-----------------------------------------------------------------*/
@@ -666,7 +660,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 +2034,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++ ) {
@@ -3039,7 +3036,7 @@ genFunction (iCode * ic)
                     }
                }
            }
-           // jwk: this needs a closer look
+           // TODO: this needs a closer look
            SPEC_ISR_SAVED_BANKS(currFunc->etype) = banksToSave;
        }
     }
@@ -3262,7 +3259,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;
          
@@ -5392,7 +5388,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
     {
@@ -9403,9 +9399,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)) {
@@ -10569,13 +10566,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)",
@@ -12700,13 +12697,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);