PIC Port - Indirection works better. instrumented pic/ralloc.c to dump
[fw/sdcc] / src / pic / glue.c
index 59d53c9e8e7ba40cce5ae6b5df3fd29f8b3e9512..bead6f1c82eb27ffb594fe957b2fb203a48bc401 100644 (file)
 
 #include "../common.h"
 #include <time.h>
-
+#include "ralloc.h"
 
 extern symbol *interrupts[256];
-void printIval (symbol *, link *, initList *, FILE *);
+void printIval (symbol *, sym_link *, initList *, FILE *);
 extern int noAlloc;
 extern set *publics;
 extern int maxInterrupts;
@@ -58,15 +58,15 @@ extern void printChar (FILE * ofile, char *s, int plen);
 char *aopLiteral (value *val, int offset)
 static void emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 value *initPointer (initList *ilist)
-void printIvalType (link * type, initList * ilist, FILE * oFile)
-void printIvalStruct (symbol * sym,link * type,
+void printIvalType (sym_link * type, initList * ilist, FILE * oFile)
+void printIvalStruct (symbol * sym,sym_link * type,
                      initList * ilist, FILE * oFile)
-int printIvalChar (link * type, initList * ilist, FILE * oFile, char *s)
-void printIvalArray (symbol * sym, link * type, initList * ilist,
+int printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
+void printIvalArray (symbol * sym, sym_link * type, initList * ilist,
                     FILE * oFile)
-void printIvalFuncPtr (link * type, initList * ilist, FILE * oFile)
-int printIvalCharPtr (symbol * sym, link * type, value * val, FILE * oFile)
-void printIvalPtr (symbol * sym, link * type, initList * ilist, FILE * oFile)
+void printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
+int printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
+void printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 #endif
 
 
@@ -110,7 +110,7 @@ char *pic14aopLiteral (value *val, int offset)
 static void pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 {
     symbol *sym;
-    int i,size;
+    int i,size,bitvars=0;;
 
     if (addPublics)
       fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
@@ -173,13 +173,30 @@ static void pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
        }
        else {
            /* allocate space */
-         //if (options.debug || sym->level == 0)
-         //fprintf(map->oFile,"==.\n");
+
+         /* If this is a bit variable, then allocate storage after 8 bits have been declared */
+         /* unlike the 8051, the pic does not have a separate bit area. So we emulate bit ram */
+         /* by grouping the bits together into groups of 8 and storing them in the normal ram.*/
+         if(IS_BITVAR(sym->etype)) {
+           if((bitvars % 8) == 0) {
+             fprintf (map->oFile, "  cblock\n");
+             fprintf (map->oFile, "\tbitfield%d\n", bitvars);
+             fprintf (map->oFile, "  endc\n");
+           }
+
+           fprintf (map->oFile, "%s\tEQU\t( (bitfield%d<<3)+%d)\n",
+                    sym->rname,
+                    bitvars & 0xfff8,
+                    bitvars & 0x0007);
+             
+           bitvars++;
+         } else {
            fprintf (map->oFile, "\t%s\n", sym->rname);
            if( (size = (unsigned int)getSize (sym->type) & 0xffff)>1) {
              for(i=1; i<size; i++)
-               fprintf (map->oFile, "\t%s_%d\n", sym->rname,size);
+               fprintf (map->oFile, "\t%s_%d\n", sym->rname,i);
            }
+         }
              //fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff);
        }
        
@@ -311,7 +328,7 @@ void printChar (FILE * ofile, char *s, int plen)
 /*-----------------------------------------------------------------*/
 /* printIvalType - generates ival for int/char                     */
 /*-----------------------------------------------------------------*/
-void printIvalType (link * type, initList * ilist, FILE * oFile)
+void printIvalType (sym_link * type, initList * ilist, FILE * oFile)
 {
     value *val;
     
@@ -353,7 +370,7 @@ void printIvalType (link * type, initList * ilist, FILE * oFile)
 /*-----------------------------------------------------------------*/
 /* printIvalStruct - generates initial value for structures        */
 /*-----------------------------------------------------------------*/
-void printIvalStruct (symbol * sym,link * type,
+void printIvalStruct (symbol * sym,sym_link * type,
                      initList * ilist, FILE * oFile)
 {
     symbol *sflds;
@@ -376,7 +393,7 @@ void printIvalStruct (symbol * sym,link * type,
 /*-----------------------------------------------------------------*/
 /* printIvalChar - generates initital value for character array    */
 /*-----------------------------------------------------------------*/
-int printIvalChar (link * type, initList * ilist, FILE * oFile, char *s)
+int printIvalChar (sym_link * type, initList * ilist, FILE * oFile, char *s)
 {
     value *val;
     int remain;
@@ -412,7 +429,7 @@ int printIvalChar (link * type, initList * ilist, FILE * oFile, char *s)
 /*-----------------------------------------------------------------*/
 /* printIvalArray - generates code for array initialization        */
 /*-----------------------------------------------------------------*/
-void printIvalArray (symbol * sym, link * type, initList * ilist,
+void printIvalArray (symbol * sym, sym_link * type, initList * ilist,
                     FILE * oFile)
 {
     initList *iloop;
@@ -463,7 +480,7 @@ void printIvalArray (symbol * sym, link * type, initList * ilist,
 /*-----------------------------------------------------------------*/
 /* printIvalFuncPtr - generate initial value for function pointers */
 /*-----------------------------------------------------------------*/
-void printIvalFuncPtr (link * type, initList * ilist, FILE * oFile)
+void printIvalFuncPtr (sym_link * type, initList * ilist, FILE * oFile)
 {
     value *val;
     int dLvl = 0;
@@ -495,7 +512,7 @@ void printIvalFuncPtr (link * type, initList * ilist, FILE * oFile)
 /*-----------------------------------------------------------------*/
 /* printIvalCharPtr - generates initial values for character pointers */
 /*-----------------------------------------------------------------*/
-int printIvalCharPtr (symbol * sym, link * type, value * val, FILE * oFile)
+int printIvalCharPtr (symbol * sym, sym_link * type, value * val, FILE * oFile)
 {
     int size = 0;
     
@@ -523,7 +540,7 @@ int printIvalCharPtr (symbol * sym, link * type, value * val, FILE * oFile)
 /*-----------------------------------------------------------------*/
 /* printIvalPtr - generates initial value for pointers             */
 /*-----------------------------------------------------------------*/
-void printIvalPtr (symbol * sym, link * type, initList * ilist, FILE * oFile)
+void printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 {
     value *val;
     
@@ -586,7 +603,7 @@ void printIvalPtr (symbol * sym, link * type, initList * ilist, FILE * oFile)
 /*-----------------------------------------------------------------*/
 /* printIval - generates code for initial value                    */
 /*-----------------------------------------------------------------*/
-void printIval (symbol * sym, link * type, initList * ilist, FILE * oFile)
+void printIval (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 {
     if (!ilist)
        return;    
@@ -895,7 +912,8 @@ void pic14glue ()
     FILE *vFile;
     FILE *asmFile;
     FILE *ovrFile = tempfile();
-    
+    int i;
+
     addSetHead(&tmpfileSet,ovrFile);
     /* print the global struct definitions */
     if (options.debug)
@@ -957,6 +975,23 @@ void pic14glue ()
     /* Put all variables into a cblock */
     fprintf (asmFile, "\n\n\tcblock  0x13\n\n");
 
+    for(i=0; i<pic14_nRegs; i++) {
+      if(regspic14[i].wasUsed && (regspic14[i].offset>=0x0c) )
+       fprintf (asmFile, "\t%s\n",regspic14[i].name);
+    }
+    //fprintf (asmFile, "\tr0x0C\n");
+    //fprintf (asmFile, "\tr0x0D\n");
+
+    /* For now, create a "dpl" and a "dph" in the register space */
+    /* of the pic so that we can use the same calling mechanism */
+    /* as the 8051 port */
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; dpl and dph to emulate the 8051 calling mechanism \n");
+    fprintf (asmFile, "%s", iComments2);
+
+    fprintf (asmFile, "\tdph\n");
+
+
 
     /* copy the sbit segment */
     fprintf (asmFile, "%s", iComments2);
@@ -977,12 +1012,6 @@ void pic14glue ()
     fprintf (asmFile, "%s", iComments2);    
     copyFile (asmFile, ovrFile);
 
-    /* copy the bit segment */
-    fprintf (asmFile, "%s", iComments2);
-    fprintf (asmFile, "; bit data\n");
-    fprintf (asmFile, "%s", iComments2);
-    copyFile (asmFile, bit->oFile);
-
     /* create the stack segment MOF */
     if (mainf && mainf->fbody) {
        fprintf (asmFile, "%s", iComments2);
@@ -1016,6 +1045,15 @@ void pic14glue ()
     
 
     fprintf (asmFile, "\tendc\n");
+
+
+    /* copy the bit segment */
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; bit data\n");
+    fprintf (asmFile, "%s", iComments2);
+    copyFile (asmFile, bit->oFile);
+
+
     fprintf (asmFile, "\tORG 0\n");
 
     /* copy the interrupt vector table */