* src/SDCCast.c (processParams): added new type flow and restructured
[fw/sdcc] / src / pic / glue.c
index 2a74a1a32c81f51a809cf123eb374a69dcdead6b..d1f271717f98e2d94880c1402f19de6fbb689ba0 100644 (file)
@@ -58,6 +58,7 @@ extern DEFSETFUNC (rmTmpFiles);
 
 extern void AnalyzeBanking (void);
 extern void copyFile (FILE * dest, FILE * src);
+extern void ReuseReg(void);
 extern void InlinepCode(void);
 extern void writeUsedRegs(FILE *);
 
@@ -68,6 +69,8 @@ extern void printChar (FILE * ofile, char *s, int plen);
 void  pCodeInitRegisters(void);
 int getConfigWord(int address);
 
+char *udata_section_name=0;            // FIXME Temporary fix to change udata section name -- VR
+
 /*-----------------------------------------------------------------*/
 /* aopLiteral - string from a literal value                        */
 /*-----------------------------------------------------------------*/
@@ -104,16 +107,16 @@ static void
 pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
 {
   symbol *sym;
-  int i, size, bitvars = 0;;
+  int bitvars = 0;;
 
+  /* print the area name */
   if (addPublics)
     fprintf (map->oFile, ";\t.area\t%s\n", map->sname);
 
-  /* print the area name */
   for (sym = setFirstItem (map->syms); sym;
        sym = setNextItem (map->syms)) {
 
-    printf("%s\n",sym->name);
+    //printf("%s\n",sym->name);
 
     /* if extern then add it into the extern list */
     if (IS_EXTERN (sym->etype)) {
@@ -137,6 +140,9 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
        !IS_STATIC (sym->etype))
       addSetHead (&publics, sym);
 
+    // PIC code allocates its own registers - so ignore parameter variable generated by processFuncArgs()
+    if (sym->_isparm)
+      continue;
     /* if extern then do nothing or is a function
        then do nothing */
     if (IS_FUNC (sym->type))
@@ -183,16 +189,20 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
           {
             fprintf (map->oFile, "%s\tres\t%d\n", sym->rname,getSize (sym->type) & 0xffff);
             /*
-            if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1)
-              {
-                for (i = 1; i < size; i++)
-                  fprintf (map->oFile, "\t%s_%d\n", sym->rname, i);
-              }
+            {
+              int i, size;
+
+              if ((size = (unsigned int) getSize (sym->type) & 0xffff) > 1)
+                {
+                  for (i = 1; i < size; i++)
+                    fprintf (map->oFile, "\t%s_%d\n", sym->rname, i);
+                }
+            }
             */
           }
         //fprintf (map->oFile, "\t.ds\t0x%04x\n", (unsigned int)getSize (sym->type) & 0xffff);
        }
-       
+
     /* if it has a initial value then do it only if
        it is a global variable */
     if (sym->ival && sym->level == 0) {
@@ -202,7 +212,7 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
        ival = initAggregates (sym, sym->ival, NULL);
       else
        ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
-                       decorateType (resolveSymbols (list2expr (sym->ival))));
+                       decorateType (resolveSymbols (list2expr (sym->ival)), RESULT_CHECK));
       codeOutFile = statsg->oFile;
       GcurMemmap = statsg;
       eBBlockFromiCode (iCodeFromAst (ival));
@@ -276,7 +286,7 @@ printIvalChar (sym_link * type, initList * ilist, pBlock *pb, char *s)
   if(!pb)
     return 0;
 
-  fprintf(stderr, "%s\n",__FUNCTION__);
+  //fprintf(stderr, "%s\n",__FUNCTION__);
   if (!s)
     {
 
@@ -303,7 +313,7 @@ printIvalChar (sym_link * type, initList * ilist, pBlock *pb, char *s)
   else {
     //printChar (oFile, s, strlen (s) + 1);
 
-    for(remain=0; remain<strlen(s); remain++) {
+    for(remain=0; remain<(int)strlen(s); remain++) {
       addpCode2pBlock(pb,newpCode(POC_RETLW,newpCodeOpLit(s[remain])));
       //fprintf(stderr,"0x%02x ",s[remain]);
     }
@@ -439,9 +449,11 @@ pic14emitStaticSeg (memmap * map)
   for (sym = setFirstItem (map->syms); sym;
        sym = setNextItem (map->syms))
     {
-      /* if it is "extern" then do nothing */
-      if (IS_EXTERN (sym->etype))
-       continue;
+    /* if extern then add it into the extern list */
+    if (IS_EXTERN (sym->etype)) {
+      addSetHead (&externs, sym);
+      continue;
+    }
 
       /* if it is not static add it to the public
          table */
@@ -488,7 +500,7 @@ pic14emitStaticSeg (memmap * map)
 
              fprintf (code->oFile, "%s:\n", sym->rname);
              noAlloc++;
-             resolveIvalSym (sym->ival);
+             resolveIvalSym (sym->ival, sym->type);
              //printIval (sym, sym->type, sym->ival, code->oFile);
              pb = newpCodeChain(NULL, 'P',newpCodeCharP("; Starting pCode block for Ival"));
              addpBlock(pb);
@@ -549,7 +561,8 @@ pic14createInterruptVect (FILE * vFile)
   if (!(mainf = findSymWithLevel (SymbolTab, mainf)))
     {
       if (!options.cc_only)
-       werror (E_NO_MAIN);
+//     werror (E_NO_MAIN);
+      fprintf(stderr,"WARNING: function 'main' undefined\n");
       return;
     }
 
@@ -558,7 +571,8 @@ pic14createInterruptVect (FILE * vFile)
     {
       /* if ! compile only then main function should be present */
       if (!options.cc_only)
-       werror (E_NO_MAIN);
+//     werror (E_NO_MAIN);
+      fprintf(stderr,"WARNING: function 'main' undefined\n");
       return;
     }
 
@@ -568,14 +582,11 @@ pic14createInterruptVect (FILE * vFile)
   fprintf (vFile, "\t__config 0x%x\n", getConfigWord(0x2007));
 
   fprintf (vFile, "%s", iComments2);
-  fprintf (vFile, "; reset and interrupt vectors \n");
+  fprintf (vFile, "; reset vector \n");
   fprintf (vFile, "%s", iComments2);
-  fprintf (vFile, "STARTUP\t%s\n", CODE_NAME);
+  fprintf (vFile, "STARTUP\t%s\n", CODE_NAME); // Lkr file should place section STARTUP at address 0x0
+  fprintf (vFile, "\tnop\n"); /* first location for used by incircuit debugger */
   fprintf (vFile, "\tgoto\t__sdcc_gsinit_startup\n");
-  fprintf (vFile, "\tnop\n");
-  fprintf (vFile, "\tnop\n");
-  fprintf (vFile, "\tgoto\t__sdcc_interrupt\n");
-
 }
 
 
@@ -622,10 +633,15 @@ pic14printPublics (FILE * afile)
 
   for (sym = setFirstItem (publics); sym;
        sym = setNextItem (publics)) {
-    if(!IS_BITFIELD(sym->type) && ((IS_FUNC(sym->type) || sym->allocreq)))
-      fprintf (afile, "\tglobal %s\n", sym->rname);
-    else
-      fprintf (afile, ";\tglobal %s\n", sym->rname);
+
+    if(!IS_BITFIELD(sym->type) && ((IS_FUNC(sym->type) || sym->allocreq))) {
+      if (!IS_BITVAR(sym->type))
+        fprintf (afile, "\tglobal %s\n", sym->rname);
+    } else {
+               /* Absolute variables are defines in the asm file as equates and thus can not be made global. */
+         if (!SPEC_ABSA (sym->etype))
+             fprintf (afile, "\tglobal %s\n", sym->rname);
+       }
   }
 }
 
@@ -637,8 +653,13 @@ pic14emitOverlay (FILE * afile)
 {
   set *ovrset;
 
-  if (!elementsInSet (ovrSetSets))
-    fprintf (afile, "\t%s\n", port->mem.overlay_name);
+/*  if (!elementsInSet (ovrSetSets))*/
+
+  /* the hack below, fixes translates for devices which
+   * only have udata_shr memory */
+  fprintf (afile, "%s\t%s\n",
+       (elementsInSet(ovrSetSets)?"":";"),
+       port->mem.overlay_name);
 
   /* for each of the sets in the overlay segment do */
   for (ovrset = setFirstItem (ovrSetSets); ovrset;
@@ -738,7 +759,7 @@ pic14emitOverlay (FILE * afile)
 void
 picglue ()
 {
-
+  char udata_name[80];
   FILE *vFile;
   FILE *asmFile;
   FILE *ovrFile = tempfile();
@@ -785,11 +806,6 @@ picglue ()
     cdbStructBlock (0);
 
   vFile = tempfile();
-  /* PENDING: this isnt the best place but it will do */
-  if (port->general.glue_up_main) {
-    /* create the interrupt vector table */
-    pic14createInterruptVect (vFile);
-  }
 
   addSetHead(&tmpfileSet,vFile);
     
@@ -798,12 +814,15 @@ picglue ()
   /* do the overlay segments */
   pic14emitOverlay(ovrFile);
 
+  /* PENDING: this isnt the best place but it will do */
+  if (port->general.glue_up_main) {
+    /* create the interrupt vector table */
+    pic14createInterruptVect (vFile);
+  }
 
   AnalyzepCode('*');
 
-  //#ifdef PCODE_DEBUG
-  //printCallTree(stderr);
-  //#endif
+  ReuseReg(); // ReuseReg where call tree permits
 
   InlinepCode();
 
@@ -854,10 +873,16 @@ picglue ()
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, sfr->oFile);
 
+  
+  if (udata_section_name) {
+    sprintf(udata_name,"%s",udata_section_name);
+  } else {
+    sprintf(udata_name,"data_%s",moduleName);
+  }
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; udata\n");
   fprintf (asmFile, "%s", iComments2);
-  fprintf (asmFile, "\tudata\n");
+  fprintf (asmFile, "%s\tudata\n", udata_name);
   copyFile (asmFile, data->oFile);
 
   /* Put all variables into a cblock */
@@ -915,11 +940,15 @@ picglue ()
     copyFile (asmFile, vFile);
     
     fprintf (asmFile, "%s", iComments2);
-    fprintf (asmFile, "; initialization and interrupt code \n");
+    fprintf (asmFile, "; interrupt and initialization code\n");
     fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "code_init\t%s\t0x4\n", CODE_NAME); // Note - for mplink may have to enlarge section vectors in .lnk file
+
+    /* interrupt service routine */
+    fprintf (asmFile, "__sdcc_interrupt:\n");
+    copypCode(asmFile, 'I');
 
     /* initialize data memory */
-    fprintf (asmFile, "code_init\t%s\t0x10\n", CODE_NAME);
     fprintf (asmFile,"__sdcc_gsinit_startup:\n");
     /* FIXME: This is temporary.  The idata section should be used.  If 
        not, we could add a special feature to the linker.  This will 
@@ -927,11 +956,6 @@ picglue ()
     copypCode(asmFile, statsg->dbName);
     fprintf (asmFile,"\tpagesel _main\n");
     fprintf (asmFile,"\tgoto _main\n");
-
-    /* interrupt service routine */
-    fprintf (asmFile, "__sdcc_interrupt:\n");
-    copypCode(asmFile, 'I');
-    fprintf (asmFile,"\tgoto $\n");
   }
 
 #if 0