* sim/ucsim/cmd.src/cmdutil.cc: NUL device is detected as CG_FILE type
[fw/sdcc] / src / avr / gen.c
index faf4c36ca613bbdfabcbfecfed91ca40400a12eb..7c17501518f6b2e52645cd68e34217f0b4197c5f 100644 (file)
@@ -1,5 +1,5 @@
 /*-------------------------------------------------------------------------
-  avrgen.c - source file for code generation for ATMEL AVR
+  gen.c - source file for code generation for ATMEL AVR
 
   Written By -  Sandeep Dutta . sandeep.dutta@usa.net (2000)
 
 #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
-
 #include "common.h"
 #include "SDCCpeeph.h"
 #include "ralloc.h"
@@ -68,20 +51,7 @@ static char *spname;
 char *fReturnAVR[] = { "r16", "r17", "r18", "r19" };
 unsigned fAVRReturnSize = 4;   /* shared with ralloc.c */
 char **fAVRReturn = fReturnAVR;
-static char *larray[4] = { "lo8", "hi8", "hlo8", "hhi8" };
-
-#if 0
-// PENDING: Unused
-static short rbank = -1;
-static char *tscr[4] = { "r0", "r1", "r24", "r25" };
-static unsigned char SLMask[] = { 0xFF, 0xFE, 0xFC, 0xF8, 0xF0,
-       0xE0, 0xC0, 0x80, 0x00
-};
-static unsigned char SRMask[] = { 0xFF, 0x7F, 0x3F, 0x1F, 0x0F,
-       0x07, 0x03, 0x01, 0x00
-};
-
-#endif
+static char *larray[4] = { ">", "<", "hlo8", "hhi8" };
 
 static struct {
        short xPushed;
@@ -95,7 +65,7 @@ static struct {
 
 extern int avr_ptrRegReq;
 extern int avr_nRegs;
-extern FILE *codeOutFile;
+extern struct dbuf_s *codeOutBuf;
 #define RESULTONSTACK(x) \
                          (IC_RESULT(x) && IC_RESULT(x)->aop && \
                          IC_RESULT(x)->aop->type == AOP_STK )
@@ -279,7 +249,7 @@ emitcode (char *inst, char *fmt, ...)
        else
                vsprintf (lb, fmt, ap);
 
-       while (isspace (*lbp))
+       while (isspace ((unsigned char)*lbp))
                lbp++;
 
        if (lbp && *lbp)
@@ -291,6 +261,18 @@ emitcode (char *inst, char *fmt, ...)
        va_end (ap);
 }
 
+/*-----------------------------------------------------------------*/
+/* avr_emitDebuggerSymbol - associate the current code location  */
+/*   with a debugger symbol                                        */
+/*-----------------------------------------------------------------*/
+void
+avr_emitDebuggerSymbol (char * debugSym)
+{
+  _G.debugLine = 1;
+  emitcode ("", "%s ==.", debugSym);
+  _G.debugLine = 0;
+}
+
 /*-----------------------------------------------------------------*/
 /* hasInc - operand is incremented before any other use            */
 /*-----------------------------------------------------------------*/
@@ -303,6 +285,7 @@ hasInc (operand *op, iCode *ic)
        int isize ;
   
        if (IS_BITVAR(retype)||!IS_PTR(type)) return NULL;
+       if (IS_AGGREGATE(type->next)) return NULL;
        isize = getSize(type->next);
        while (lic) {
                /* if operand of the form op = op + <sizeof *op> */
@@ -313,7 +296,7 @@ hasInc (operand *op, iCode *ic)
                        return lic;
                }
                /* if the operand used or deffed */
-               if (bitVectBitValue(OP_USES(op),lic->key) || ((unsigned) lic->defKey == op->key)) {
+               if (bitVectBitValue(OP_USES(op),lic->key) || (lic->defKey == op->key)) {
                        return NULL;
                }
                lic = lic->next;
@@ -699,6 +682,22 @@ isRegPair (asmop * aop)
        return 0;
 }
 
+/*-----------------------------------------------------------------*/
+/* allHigh - all registers are high registers                      */
+/*-----------------------------------------------------------------*/
+static int allHigh (asmop * aop)
+{
+       int i;
+       
+       if (aop->type == AOP_X || aop->type == AOP_Z)
+               return 1;
+       if (aop->type != AOP_REG)
+               return 0;
+       for (i=0; i < aop->size ; i++ ) 
+               if (aop->aopu.aop_reg[i]->rIdx < R16_IDX) return 0;
+       return 1;
+}
+
 /*-----------------------------------------------------------------*/
 /* aopOp - allocates an asmop for an operand  :                    */
 /*-----------------------------------------------------------------*/
@@ -759,6 +758,8 @@ aopOp (operand * op, iCode * ic, bool result)
           b) has a spill location */
        if (sym->isspilt || sym->nRegs == 0) {
 
+               asmop *oldAsmOp = NULL;
+
                /* rematerialize it NOW */
                if (sym->remat) {
                        sym->aop = op->aop = aop = aopForRemat (sym);
@@ -780,8 +781,17 @@ aopOp (operand * op, iCode * ic, bool result)
                }
 
                /* else spill location  */
+               if (sym->usl.spillLoc && getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
+                   /* force a new aop if sizes differ */
+                   oldAsmOp = sym->usl.spillLoc->aop;
+                   sym->usl.spillLoc->aop = NULL;
+               }
                sym->aop = op->aop = aop =
                        aopForSym (ic, sym->usl.spillLoc, result);
+               if (getSize(sym->type) != getSize(sym->usl.spillLoc->type)) {
+                       /* Don't reuse the new aop, go with the last one */
+                       sym->usl.spillLoc->aop = oldAsmOp;
+               }
                aop->size = getSize (sym->type);
                return;
        }
@@ -1038,7 +1048,7 @@ aopPut (asmop * aop, char *s, int offset)
                break;
 
        case AOP_REG:
-               if (toupper (*s) != 'R') {
+               if (toupper ((unsigned char)*s) != 'R') {
                        if (s == zero) {
                                emitcode ("clr", "%s",
                                          aop->aopu.aop_reg[offset]->name);
@@ -1106,7 +1116,7 @@ aopPut (asmop * aop, char *s, int offset)
 
        case AOP_CRY:
                /* if used only for a condition code check */
-               assert (toupper (*s) == 'R');
+               assert (toupper ((unsigned char)*s) == 'R');
                if (offset == 0) {
                        emitcode ("xrl", "r0,r0");
                        emitcode ("cpi", "%s,0", s);
@@ -1542,7 +1552,7 @@ static void
 genCall (iCode * ic)
 {
 
-       /* if send set is not empty the assign */
+       /* if send set is not empty then assign */
        if (_G.sendSet) {
                iCode *sic;
                int rnum = 16;
@@ -1697,12 +1707,6 @@ resultRemat (iCode * ic)
        return 0;
 }
 
-#if defined(__BORLANDC__) || defined(_MSC_VER)
-#define STRCASECMP stricmp
-#else
-#define STRCASECMP strcasecmp
-#endif
-
 /*-----------------------------------------------------------------*/
 /* genFunction - generated code for function entry                 */
 /*-----------------------------------------------------------------*/
@@ -1710,7 +1714,7 @@ static void
 genFunction (iCode * ic)
 {
        symbol *sym;
-       sym_link *fetype;
+       sym_link *ftype;
        int i = 0;
 
        _G.nRegsSaved = 0;
@@ -1721,13 +1725,13 @@ genFunction (iCode * ic)
        emitcode (";", "-----------------------------------------");
 
        emitcode ("", "%s:", sym->rname);
-       fetype = getSpec (operandType (IC_LEFT (ic)));
+       ftype = operandType (IC_LEFT (ic));
 
        /* if critical function then turn interrupts off */
-       if (SPEC_CRTCL (fetype))
+       if (IFFUNC_ISCRITICAL (ftype))
                emitcode ("cli", "");
 
-       if (IS_ISR (sym->etype)) {
+       if (IFFUNC_ISISR (sym->type)) {
        }
 
        /* save the preserved registers that are used in this function */
@@ -1821,10 +1825,14 @@ genEndFunction (iCode * ic)
                }
        }
 
-       if (SPEC_CRTCL (sym->etype))
+       if (IFFUNC_ISCRITICAL (sym->type))
                emitcode ("sti", "");
 
-       if (IS_ISR (sym->etype)) {
+       if (options.debug && currFunc) {
+               debugFile->writeEndFunction (currFunc, ic, 1);
+       }
+
+       if (IFFUNC_ISISR (sym->type)) {
                emitcode ("rti", "");
        }
        else {
@@ -2110,7 +2118,7 @@ genMinusDec (iCode * ic)
 
        /* if the sizes are greater than 2 or they are not the same regs
           then we cannot */
-       if (!sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RIGHT (ic))))
+       if (!sameRegs (AOP (IC_LEFT (ic)), AOP (IC_RESULT (ic))))
                return FALSE;
 
        /* so we know LEFT & RESULT in the same registers and add
@@ -2313,7 +2321,7 @@ genMult (iCode * ic)
        }
 
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 
       release:
        freeAsmop (left, NULL, ic, (RESULTONSTACK (ic) ? FALSE : TRUE));
@@ -2328,7 +2336,7 @@ static void
 genDiv (iCode * ic)
 {
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2338,7 +2346,7 @@ static void
 genMod (iCode * ic)
 {
        /* should have been converted to function call */
-       assert (1);
+       assert (0);
 
 }
 
@@ -2371,7 +2379,7 @@ revavrcnd (int type)
                if (rar[i].rtype == type)
                        return rar[i].type;
        }
-       assert (1);             /* cannot happen */
+       assert (0);             /* cannot happen */
        return 0;               /* makes the compiler happy */
 }
 
@@ -2496,7 +2504,7 @@ static void
 genCmpGt (iCode * ic, iCode * ifx)
 {
        /* should have transformed by the parser */
-       assert (1);
+       assert (0);
 }
 
 /*-----------------------------------------------------------------*/
@@ -2672,7 +2680,7 @@ genBitWise (iCode * ic, iCode * ifx, int bitop)
                                (int) floatFromVal (AOP (right)->aopu.
                                                    aop_lit);
                        int p2 = powof2 (lit);
-                       if (bitop == AVR_AND && p2) {   /* right side is a power of 2 */
+                       if (bitop == AVR_AND && (p2 >= 0)) {    /* right side is a power of 2 */
                                l = aopGet (AOP (left), p2 / 8);
                                if (IC_TRUE (ifx)) {
                                        emitcode ("sbrc", "%s,%d", l,
@@ -4580,6 +4588,10 @@ genPointerSet (iCode * ic, iCode *pi)
        case GPOINTER:
                genGenPointerSet (right, result, ic, pi);
                break;
+
+       default:
+         werror (E_INTERNAL_ERROR, __FILE__, __LINE__, 
+                 "genPointerSet: illegal pointer type");
        }
 
 }
@@ -4616,23 +4628,17 @@ genIfx (iCode * ic, iCode * popIc)
 
        lbl = newiTempLabel(NULL);
        if (IC_TRUE(ic)) {
-               if (tob)
-                       emitcode ("breq","L%05d",lbl->key);
-               else
-                       emitcode ("brne","L%05d",lbl->key);
+               emitcode ("breq","L%05d",lbl->key);
                emitcode ("jmp","L%05d",IC_TRUE(ic)->key);
                emitcode ("","L%05d:",lbl->key);
        } else {
-               if (tob)
-                       emitcode ("brne","L%05d",lbl->key);
-               else
-                       emitcode ("breq","L%05d",lbl->key);
+               emitcode ("brne","L%05d",lbl->key);
                emitcode ("jmp","L%05d",IC_FALSE(ic)->key);
                emitcode ("","L%05d:",lbl->key);
        }
        ic->generated = 1;
 }
-/* here */
+
 /*-----------------------------------------------------------------*/
 /* genAddrOf - generates code for address of                       */
 /*-----------------------------------------------------------------*/
@@ -4650,15 +4656,44 @@ genAddrOf (iCode * ic)
        if (sym->onStack) {
                /* if it has an offset then we need to compute it */
                if (sym->stack) {
-#if 0
-                       if (AOP_ISHIGHREG(AOP(
-                                 ((char) sym->stack & 0xff));
-#endif
-                       aopPut (AOP (IC_RESULT (ic)), "a", 0);
+                       if (allHigh(AOP(IC_RESULT(ic)))) {
+                               if (isRegPair (AOP(IC_RESULT(ic)))) {
+                                       emitcode ("movw","%s,r28",aopGet(AOP(IC_RESULT(ic)),0));                                        
+                               } else {
+                                       emitcode ("mov","%s,r28",aopGet(AOP(IC_RESULT(ic)),0));
+                                       emitcode ("mov","%s,r29",aopGet(AOP(IC_RESULT(ic)),1));                                 
+                               }
+                               if (sym->stack < 0) {
+                                       emitcode("subi","%s,<(%d)",aopGet(AOP(IC_RESULT(ic)),0),-sym->stack);
+                                       emitcode("sbci","%s,>(%d)",aopGet(AOP(IC_RESULT(ic)),1),-sym->stack);
+                               } else {
+                                       emitcode("subi","%s,<(-%d)",aopGet(AOP(IC_RESULT(ic)),0),sym->stack);
+                                       emitcode("sbci","%s,>(-%d)",aopGet(AOP(IC_RESULT(ic)),1),sym->stack);
+                               }
+                       } else {
+                               emitcode("movw","r24,r28");
+                               if (sym->stack > -63 && sym->stack < 63) {
+                                       if (sym->stack < 0)
+                                               emitcode("sbiw","r24,%d",-sym->stack);
+                                       else
+                                               emitcode("sbiw","r24,%d",sym->stack);
+                               } else {                                        
+                                       if (sym->stack < 0) {
+                                               emitcode("subi","r24,<(%d)",-sym->stack);
+                                               emitcode("sbci","r25,>(%d)",-sym->stack);
+                                       } else {
+                                               emitcode("subi","r24,<(-%d)",sym->stack);
+                                               emitcode("sbci","r25,>(-%d)",sym->stack);
+                                       }
+                               }
+                               
+                               aopPut(AOP(IC_RESULT(ic)),"r24",0);
+                               aopPut(AOP(IC_RESULT(ic)),"r25",1);
+                       }
                }
                else {
-                       /* we can just move _bp */
-                       aopPut (AOP (IC_RESULT (ic)), "_bp", 0);
+                       aopPut(AOP(IC_RESULT(ic)),"r28",0);
+                       aopPut(AOP(IC_RESULT(ic)),"r29",1);
                }
                /* fill the result with zero */
                size = AOP_SIZE (IC_RESULT (ic)) - 2;
@@ -4673,13 +4708,13 @@ genAddrOf (iCode * ic)
        /* object not on stack then we need the name */
        size = AOP_SIZE (IC_RESULT (ic));
        offset = 0;
-
+       assert(size<=2);
        while (size--) {
                char s[SDCC_NAME_MAX];
                if (offset)
-                       sprintf (s, "(%s >> %d)", sym->rname, offset * 8);
+                       sprintf (s, ">(%s)", sym->rname);
                else
-                       sprintf (s, "%s", sym->rname);
+                       sprintf (s, "<(%s)", sym->rname);
                aopPut (AOP (IC_RESULT (ic)), s, offset++);
        }
 
@@ -4910,8 +4945,6 @@ genCast (iCode * ic)
 
                /* pointer to generic pointer */
                if (IS_GENPTR (ctype)) {
-                       char *l = zero;
-
                        if (IS_PTR (type))
                                p_type = DCL_TYPE (type);
                        else {
@@ -4927,30 +4960,22 @@ genCast (iCode * ic)
                                        aopGet (AOP (right), offset), offset);
                                offset++;
                        }
-                       /* the last byte depending on type */
-                       switch (p_type) {
-                       case IPOINTER:
-                       case POINTER:
-                               l = zero;
-                               break;
-                       case FPOINTER:
-                               l = one;
-                               break;
-                       case CPOINTER:
-                               l = "0x02";
-                               break;
-                       case PPOINTER:
-                               l = "0x03";
-                               break;
-
-                       default:
-                               /* this should never happen */
-                               werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
-                                       "got unknown pointer type");
-                               exit (1);
+                   
+                   /* the last byte depending on type */
+                   {
+                       int gpVal = pointerTypeToGPByte(p_type, NULL, NULL);
+                       char gpValStr[10];
+                       
+                       if (gpVal == -1)
+                       {
+                           // pointerTypeToGPByte will have bitched.
+                           exit(1);
                        }
-                       aopPut (AOP (result), l, GPTRSIZE - 1);
-                       goto release;
+                       
+                       sprintf(gpValStr, "#0x%x", gpVal);
+                       aopPut (AOP (result), gpValStr, GPTRSIZE - 1);
+                   }               
+                   goto release;
                }
 
                /* just copy the pointers */
@@ -5078,6 +5103,18 @@ genReceive (iCode * ic)
        freeAsmop (IC_RESULT (ic), NULL, ic, TRUE);
 }
 
+/*-----------------------------------------------------------------*/
+/* genDummyRead - generate code for dummy read of volatiles        */
+/*-----------------------------------------------------------------*/
+static void
+genDummyRead (iCode * ic)
+{
+  emitcode (";     genDummyRead","");
+  emitcode (";     not implemented","");
+
+  ic = ic;
+}
+
 /*-----------------------------------------------------------------*/
 /* gen51Code - generate code for 8051 based controllers            */
 /*-----------------------------------------------------------------*/
@@ -5091,14 +5128,10 @@ genAVRCode (iCode * lic)
        recvCnt = 0;
        /* print the allocation information */
        if (allocInfo)
-               printAllocInfo (currFunc, codeOutFile);
+               printAllocInfo (currFunc, codeOutBuf);
        /* if debug information required */
-       /*     if (options.debug && currFunc) { */
-       if (currFunc) {
-               cdbSymbol (currFunc, cdbFile, FALSE, TRUE);
-               _G.debugLine = 1;
-/*             emitcode ("", ".type %s,@function", currFunc->name); */
-               _G.debugLine = 0;
+       if (options.debug && currFunc) {
+               debugFile->writeFunction (currFunc, lic);
        }
        /* stack pointer name */
        spname = "sp";
@@ -5108,11 +5141,7 @@ genAVRCode (iCode * lic)
 
                if (cln != ic->lineno) {
                        if (options.debug) {
-                               _G.debugLine = 1;
-                               emitcode ("", "C$%s$%d$%d$%d ==.",
-                                         FileBaseName (ic->filename),
-                                         ic->lineno, ic->level, ic->block);
-                               _G.debugLine = 0;
+                               debugFile->writeCLine (ic);
                        }
                        emitcode (";", "%s %d", ic->filename, ic->lineno);
                        cln = ic->lineno;
@@ -5309,6 +5338,10 @@ genAVRCode (iCode * lic)
                        addSet (&_G.sendSet, ic);
                        break;
 
+               case DUMMY_READ_VOLATILE:
+                       genDummyRead (ic);
+                       break;
+
                default:
                        ic = ic;
                }
@@ -5321,6 +5354,6 @@ genAVRCode (iCode * lic)
                peepHole (&lineHead);
 
        /* now do the actual printing */
-       printLine (lineHead, codeOutFile);
+       printLine (lineHead, codeOutBuf);
        return;
 }