Fixed up s51 autodetect
[fw/sdcc] / src / pic / glue.c
index abd20a56c9949e9bfd2a6d378d25e0915db55f6e..35d24fa530828bab7091fcb978c7e0151f30173c 100644 (file)
 #include "../common.h"
 #include <time.h>
 #include "ralloc.h"
+#include "pcode.h"
 #include "newalloc.h"
 
+
 extern symbol *interrupts[256];
 void printIval (symbol *, sym_link *, initList *, FILE *);
 extern int noAlloc;
 extern set *publics;
-extern int maxInterrupts;
+extern unsigned maxInterrupts;
 extern int maxRegBank;
 extern symbol *mainf;
 extern char *VersionString;
@@ -71,41 +73,55 @@ aopLiteral (value * val, int offset)
      void printIvalPtr (symbol * sym, sym_link * type, initList * ilist, FILE * oFile)
 #endif
 
+/*-----------------------------------------------------------------*/
+/* Allocation macros that replace those in SDCCalloc.h             */
+/*   Why? I dunno. I ran across a bug with those macros that       */
+/*   I couldn't fix, but I could work around...                    */
+/*-----------------------------------------------------------------*/
+
+#define  _ALLOC(x,sz) if (!(x = calloc((sz),1) ))      \
+         {                                          \
+            werror(E_OUT_OF_MEM,__FILE__,(long) sz);\
+            exit (1);                               \
+         }
+
+#define _ALLOC_ATOMIC(x,y) if (!((x) = malloc(y)))   \
+         {                                               \
+            werror(E_OUT_OF_MEM,__FILE__,(long) y);     \
+            exit (1);                                    \
+         }
+
 
 /*-----------------------------------------------------------------*/
 /* aopLiteral - string from a literal value                        */
 /*-----------------------------------------------------------------*/
-     char *pic14aopLiteral (value * val, int offset)
+int pic14aopLiteral (value *val, int offset)
 {
-  char *rs;
-  union
-    {
-      float f;
-      unsigned char c[4];
-    }
-  fl;
+  union {
+    float f;
+    unsigned char c[4];
+  } fl;
 
   /* if it is a float then it gets tricky */
   /* otherwise it is fairly simple */
-  if (!IS_FLOAT (val->type))
-    {
-      unsigned long v = floatFromVal (val);
+  if (!IS_FLOAT(val->type)) {
+    unsigned long v = (unsigned long) floatFromVal(val);
 
-      v >>= (offset * 8);
-      sprintf (buffer, "0x%02x", ((char) v) & 0xff);
-      rs = Safe_calloc (1, strlen (buffer) + 1);
-      return strcpy (rs, buffer);
-    }
+    //v >>= (offset * 8);
+    return ( (v >> (offset * 8)) & 0xff);
+    //sprintf(buffer,"0x%02x",((char) v) & 0xff);
+    //_ALLOC_ATOMIC(rs,strlen(buffer)+1);
+    //return strcpy (rs,buffer);
+  }
 
   /* it is type float */
-  fl.f = (float) floatFromVal (val);
-#ifdef _BIG_ENDIAN
-  sprintf (buffer, "0x%02x", fl.c[3 - offset]);
+  fl.f = (float) floatFromVal(val);
+#ifdef _BIG_ENDIAN    
+  return fl.c[3-offset];
 #else
-  sprintf (buffer, "0x%02x", fl.c[offset]);
+  return fl.c[offset];
 #endif
-  rs = Safe_calloc (1, strlen (buffer) + 1);
-  return strcpy (rs, buffer);
+
 }
 
 
@@ -214,21 +230,21 @@ pic14emitRegularMap (memmap * map, bool addPublics, bool arFlag)
            }
          //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)
-       {
-         ast *ival = NULL;
-
-         if (IS_AGGREGATE (sym->type))
-           ival = initAggregates (sym, sym->ival, NULL);
-         else
-           ival = newNode ('=', newAst_VALUE (symbolVal (sym)),
-                    decorateType (resolveSymbols (list2expr (sym->ival))));
-         codeOutFile = statsg->oFile;
-         eBBlockFromiCode (iCodeFromAst (ival));
-         sym->ival = NULL;
+       
+       /* if it has a initial value then do it only if
+          it is a global variable */
+       if (sym->ival && sym->level == 0) {
+           ast *ival = NULL;
+           
+           if (IS_AGGREGATE (sym->type))
+               ival = initAggregates (sym, sym->ival, NULL);
+           else
+               ival = newNode ('=', newAst_VALUE(symbolVal (sym)),
+                               decorateType (resolveSymbols (list2expr (sym->ival))));
+           codeOutFile = statsg->oFile;
+           GcurMemmap = statsg;
+           eBBlockFromiCode (iCodeFromAst (ival));
+           sym->ival = NULL;
        }
     }
 }
@@ -790,7 +806,7 @@ pic14emitMaps ()
 static void
 pic14createInterruptVect (FILE * vFile)
 {
-  int i = 0;
+  unsigned i = 0;
   mainf = newSymbol ("main", 0);
   mainf->block = 0;
 
@@ -803,7 +819,7 @@ pic14createInterruptVect (FILE * vFile)
     }
 
   /* if the main is only a prototype ie. no body then do nothing */
-  if (!mainf->fbody)
+  if (!IFFUNC_HASBODY(mainf->type))
     {
       /* if ! compile only then main function should be present */
       if (!options.cc_only)
@@ -976,85 +992,146 @@ pic14emitOverlay (FILE * afile)
 /* glue - the final glue that hold the whole thing together        */
 /*-----------------------------------------------------------------*/
 void
-pic14glue ()
+picglue ()
 {
+
   FILE *vFile;
   FILE *asmFile;
-  FILE *ovrFile = tempfile ();
+  FILE *ovrFile = tempfile();
   int i;
+  set *s=NULL,*t=NULL;
+  char a=1,b=2,c=3;
+
+
+  addSet(&s,&a);
+  addSet(&s,&b);
+  addSet(&s,&c);
+
+  fprintf(stdout,"\n\n\n******************\n\n\n");
+  for(t=s; t; t=t->next) {
+    if(t->item) 
+      fprintf(stdout,"Set item %d\n",*(char *)t->item);
+  }
+
+  s =reverseSet(s);
+  for(t=s; t; t=t->next) {
+    if(t->item) 
+      fprintf(stdout,"Set item %d\n",*(char *)t->item);
+  }
+
+  addSetHead(&tmpfileSet,ovrFile);
+
+
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
+
+    pBlock *pb = newpCodeChain(NULL,'X',newpCodeCharP("; Starting pCode block"));
+    addpBlock(pb);
+
+    /* entry point @ start of CSEG */
+    addpCode2pBlock(pb,newpCodeLabelStr("__sdcc_program_startup"));
+    /* put in the call to main */
+    addpCode2pBlock(pb,newpCode(POC_CALL,newpCodeOp("_main",PO_STR)));
+
+    if (options.mainreturn) {
+
+      addpCode2pBlock(pb,newpCodeCharP(";\treturn from main will return to caller\n"));
+      addpCode2pBlock(pb,newpCode(POC_RETURN,NULL));
+
+    } else {
+
+      addpCode2pBlock(pb,newpCodeCharP(";\treturn from main will lock up\n"));
+      addpCode2pBlock(pb,newpCode(POC_GOTO,newpCodeOp("$",PO_STR)));
+
+    }
+  }
+
+
+  /* At this point we've got all the code in the form of pCode structures */
+  /* Now it needs to be rearranged into the order it should be placed in the */
+  /* code space */
+
+  movepBlock2Head(code->dbName);     // Last
+  movepBlock2Head('X');
+  movepBlock2Head(statsg->dbName);   // First
+
+
+  AnalyzepCode('*'); //code->dbName);
+  printCallTree(stderr);
+
+  //pCodePeepInit();
+
+  //OptimizepCode(code->dbName);
+
 
-  addSetHead (&tmpfileSet, ovrFile);
   /* print the global struct definitions */
   if (options.debug)
-    cdbStructBlock (0, cdbFile);
+    cdbStructBlock (0,cdbFile);
 
-  vFile = tempfile ();
+  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);
+  if (port->general.glue_up_main) {
+    /* create the interrupt vector table */
+    pic14createInterruptVect (vFile);
+  }
 
+  addSetHead(&tmpfileSet,vFile);
+    
   /* emit code for the all the variables declared */
   pic14emitMaps ();
   /* do the overlay segments */
-  pic14emitOverlay (ovrFile);
+  pic14emitOverlay(ovrFile);
 
-  /* now put it all together into the assembler file */
-  /* create the assembler file name */
 
-  if (!options.c1mode)
-    {
-      sprintf (buffer, srcFileName);
-      strcat (buffer, ".asm");
-    }
-  else
-    {
-      strcpy (buffer, options.out_name);
-    }
+  pcode_test();
 
-  if (!(asmFile = fopen (buffer, "w")))
-    {
-      werror (E_FILE_OPEN_ERR, buffer);
-      exit (1);
-    }
 
+  /* now put it all together into the assembler file */
+  /* create the assembler file name */
+    
+  if (!options.c1mode) {
+    sprintf (buffer, srcFileName);
+    strcat (buffer, ".asm");
+  }
+  else {
+    strcpy(buffer, options.out_name);
+  }
+
+  if (!(asmFile = fopen (buffer, "w"))) {
+    werror (E_FILE_OPEN_ERR, buffer);
+    exit (1);
+  }
+    
   /* initial comments */
   pic14initialComments (asmFile);
-
+    
   /* print module name */
   fprintf (asmFile, ";\t.module %s\n", moduleName);
-
+    
   /* Let the port generate any global directives, etc. */
   if (port->genAssemblerPreamble)
     {
-      port->genAssemblerPreamble (asmFile);
+      port->genAssemblerPreamble(asmFile);
     }
-
+    
   /* print the global variables in this module */
   pic14printPublics (asmFile);
-
+    
 
   /* copy the sfr segment */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; special function registers\n");
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, sfr->oFile);
-
+    
 
   /* Put all variables into a cblock */
-  fprintf (asmFile, "\n\n\tcblock  0x13\n\n");
+  fprintf (asmFile, "\n\n\tcblock  0x20\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);
+  }
 
-  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 */
@@ -1072,7 +1149,7 @@ pic14glue ()
   fprintf (asmFile, "; special function bits \n");
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, sfrbit->oFile);
-
+    
   /* copy the data segment */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; internal ram data\n");
@@ -1083,42 +1160,40 @@ pic14glue ()
   /* create the overlay segments */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; overlayable items in internal ram \n");
-  fprintf (asmFile, "%s", iComments2);
+  fprintf (asmFile, "%s", iComments2);    
   copyFile (asmFile, ovrFile);
 
   /* create the stack segment MOF */
-  if (mainf && mainf->fbody)
-    {
-      fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, "; Stack segment in internal ram \n");
-      fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
-              ";__start__stack:\n;\t.ds\t1\n\n");
-    }
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; Stack segment in internal ram \n");
+    fprintf (asmFile, "%s", iComments2);    
+    fprintf (asmFile, ";\t.area\tSSEG\t(DATA)\n"
+            ";__start__stack:\n;\t.ds\t1\n\n");
+  }
 
   /* create the idata segment */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; indirectly addressable internal ram data\n");
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, idata->oFile);
-
+    
   /* if external stack then reserve space of it */
-  if (mainf && mainf->fbody && options.useXstack)
-    {
-      fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, "; external stack \n");
-      fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, ";\t.area XSEG (XDATA)\n");    /* MOF */
-      fprintf (asmFile, ";\t.ds 256\n");
-    }
-
-
+  if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; external stack \n");
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile,";\t.area XSEG (XDATA)\n"); /* MOF */
+    fprintf (asmFile,";\t.ds 256\n");
+  }
+       
+       
   /* copy xtern ram data */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; external ram data\n");
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, xdata->oFile);
-
+    
 
   fprintf (asmFile, "\tendc\n");
 
@@ -1133,106 +1208,69 @@ pic14glue ()
   fprintf (asmFile, "\tORG 0\n");
 
   /* copy the interrupt vector table */
-  if (mainf && mainf->fbody)
-    {
-      fprintf (asmFile, "%s", iComments2);
-      fprintf (asmFile, "; interrupt vector \n");
-      fprintf (asmFile, "%s", iComments2);
-      copyFile (asmFile, vFile);
-    }
-
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
+    fprintf (asmFile, "%s", iComments2);
+    fprintf (asmFile, "; interrupt vector \n");
+    fprintf (asmFile, "%s", iComments2);
+    copyFile (asmFile, vFile);
+  }
+    
   /* copy global & static initialisations */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; global & static initialisations\n");
   fprintf (asmFile, "%s", iComments2);
-
-  /* Everywhere we generate a reference to the static_name area,
-   * (which is currently only here), we immediately follow it with a
+    
+  /* Everywhere we generate a reference to the static_name area, 
+   * (which is currently only here), we immediately follow it with a 
    * definition of the post_static_name area. This guarantees that
    * the post_static_name area will immediately follow the static_name
    * area.
    */
-  fprintf (asmFile, ";\t.area %s\n", port->mem.static_name);   /* MOF */
+  fprintf (asmFile, ";\t.area %s\n", port->mem.static_name); /* MOF */
   fprintf (asmFile, ";\t.area %s\n", port->mem.post_static_name);
   fprintf (asmFile, ";\t.area %s\n", port->mem.static_name);
-
-  if (mainf && mainf->fbody)
-    {
-      fprintf (asmFile, "__sdcc_gsinit_startup:\n");
-      /* if external stack is specified then the
-         higher order byte of the xdatalocation is
-         going into P2 and the lower order going into
-         spx */
-      if (options.useXstack)
-       {
-         fprintf (asmFile, ";\tmov\tP2,#0x%02x\n",
-                  (((unsigned int) options.xdata_loc) >> 8) & 0xff);
-         fprintf (asmFile, ";\tmov\t_spx,#0x%02x\n",
-                  (unsigned int) options.xdata_loc & 0xff);
-       }
-
-      /* initialise the stack pointer */
-      /* if the user specified a value then use it */
-      if (options.stack_loc)
-       fprintf (asmFile, ";\tmov\tsp,#%d\n", options.stack_loc);
-      else
-       /* no: we have to compute it */
-      if (!options.stackOnData && maxRegBank <= 3)
-       fprintf (asmFile, ";\tmov\tsp,#%d\n", ((maxRegBank + 1) * 8) - 1);
-      else
-       fprintf (asmFile, ";\tmov\tsp,#__start__stack\n");      /* MOF */
-
-      fprintf (asmFile, ";\tlcall\t__sdcc_external_startup\n");
-      fprintf (asmFile, ";\tmov\ta,dpl\n");
-      fprintf (asmFile, ";\tjz\t__sdcc_init_data\n");
-      fprintf (asmFile, ";\tljmp\t__sdcc_program_startup\n");
-      fprintf (asmFile, ";__sdcc_init_data:\n");
-
+    
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
+    fprintf (asmFile,"__sdcc_gsinit_startup:\n");
+    /* if external stack is specified then the
+       higher order byte of the xdatalocation is
+       going into P2 and the lower order going into
+       spx */
+    if (options.useXstack) {
+      fprintf(asmFile,";\tmov\tP2,#0x%02x\n",
+             (((unsigned int)options.xdata_loc) >> 8) & 0xff);
+      fprintf(asmFile,";\tmov\t_spx,#0x%02x\n",
+             (unsigned int)options.xdata_loc & 0xff);
     }
-  copyFile (asmFile, statsg->oFile);
 
-  if (port->general.glue_up_main && mainf && mainf->fbody)
+  }
+
+  if (port->general.glue_up_main && mainf && IFFUNC_HASBODY(mainf->type))
     {
       /* This code is generated in the post-static area.
        * This area is guaranteed to follow the static area
        * by the ugly shucking and jiving about 20 lines ago.
        */
-      fprintf (asmFile, ";\t.area %s\n", port->mem.post_static_name);
-      fprintf (asmFile, ";\tljmp\t__sdcc_program_startup\n");
+      fprintf(asmFile, ";\t.area %s\n", port->mem.post_static_name);
+      fprintf (asmFile,";\tljmp\t__sdcc_program_startup\n");
     }
-
+       
   /* copy over code */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; code\n");
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, ";\t.area %s\n", port->mem.code_name);
-  if (mainf && mainf->fbody)
-    {
-
-      /* entry point @ start of CSEG */
-      fprintf (asmFile, "__sdcc_program_startup:\n");
-
-      /* put in the call to main */
-      fprintf (asmFile, "\tcall\t_main\n");
-      if (options.mainreturn)
-       {
 
-         fprintf (asmFile, ";\treturn from main ; will return to caller\n");
-         fprintf (asmFile, "\treturn\n");
+  //copyFile (asmFile, code->oFile);
 
-       }
-      else
-       {
+  copypCode(asmFile, statsg->dbName);
+  copypCode(asmFile, 'X');
+  copypCode(asmFile, code->dbName);
 
-         fprintf (asmFile, ";\treturn from main will lock up\n");
-         fprintf (asmFile, "\tgoto\t$\n");
-       }
-    }
-  copyFile (asmFile, code->oFile);
 
-  fprintf (asmFile, "\tend\n");
+  fprintf (asmFile,"\tend\n");
 
   fclose (asmFile);
-  applyToSet (tmpfileSet, closeTmpFiles);
-  applyToSet (tmpfileNameSet, rmTmpFiles);
+  applyToSet(tmpfileSet,closeTmpFiles);
+  applyToSet(tmpfileNameSet, rmTmpFiles);
 }