*** empty log message ***
[fw/sdcc] / src / pic / glue.c
index 77f6cc68591e7ba1f91148f514096a8abcd15c55..1baf9327a39e6ab7b5b229445d2fec9494ff134d 100644 (file)
@@ -33,7 +33,7 @@ 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;
@@ -105,7 +105,7 @@ int pic14aopLiteral (value *val, int offset)
   /* if it is a float then it gets tricky */
   /* otherwise it is fairly simple */
   if (!IS_FLOAT(val->type)) {
-    unsigned long v = floatFromVal(val);
+    unsigned long v = (unsigned long) floatFromVal(val);
 
     //v >>= (offset * 8);
     return ( (v >> (offset * 8)) & 0xff);
@@ -806,7 +806,7 @@ pic14emitMaps ()
 static void
 pic14createInterruptVect (FILE * vFile)
 {
-  int i = 0;
+  unsigned i = 0;
   mainf = newSymbol ("main", 0);
   mainf->block = 0;
 
@@ -819,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)
@@ -992,15 +992,74 @@ pic14emitOverlay (FILE * afile)
 /* glue - the final glue that hold the whole thing together        */
 /*-----------------------------------------------------------------*/
 void
-pic14glue ()
+picglue ()
 {
 
   FILE *vFile;
   FILE *asmFile;
   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);
+
+  DFPRINTF((stderr,"\n\n\n******************\n\n\n"));
+  for(t=s; t; t=t->next) {
+    if(t->item) 
+      DFPRINTF((stderr,"Set item %d\n",*(char *)t->item));
+  }
+
+  s =reverseSet(s);
+  for(t=s; t; t=t->next) {
+    if(t->item) 
+      DFPRINTF((stderr,"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);
+#ifdef PCODE_DEBUG
+  printCallTree(stderr);
+#endif
+
   /* print the global struct definitions */
   if (options.debug)
     cdbStructBlock (0,cdbFile);
@@ -1063,14 +1122,13 @@ pic14glue ()
     
 
   /* 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);
   }
-  //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 */
@@ -1079,9 +1137,6 @@ pic14glue ()
   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);
@@ -1095,6 +1150,8 @@ pic14glue ()
   fprintf (asmFile, "%s", iComments2);
   copyFile (asmFile, data->oFile);
 
+  fprintf (asmFile, "\tendc\n");
+
 
   /* create the overlay segments */
   fprintf (asmFile, "%s", iComments2);
@@ -1103,7 +1160,7 @@ pic14glue ()
   copyFile (asmFile, ovrFile);
 
   /* create the stack segment MOF */
-  if (mainf && mainf->fbody) {
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; Stack segment in internal ram \n");
     fprintf (asmFile, "%s", iComments2);    
@@ -1118,7 +1175,7 @@ pic14glue ()
   copyFile (asmFile, idata->oFile);
     
   /* if external stack then reserve space of it */
-  if (mainf && mainf->fbody && options.useXstack ) {
+  if (mainf && IFFUNC_HASBODY(mainf->type) && options.useXstack ) {
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; external stack \n");
     fprintf (asmFile, "%s", iComments2);
@@ -1134,9 +1191,6 @@ pic14glue ()
   copyFile (asmFile, xdata->oFile);
     
 
-  fprintf (asmFile, "\tendc\n");
-
-
   /* copy the bit segment */
   fprintf (asmFile, "%s", iComments2);
   fprintf (asmFile, "; bit data\n");
@@ -1147,7 +1201,7 @@ pic14glue ()
   fprintf (asmFile, "\tORG 0\n");
 
   /* copy the interrupt vector table */
-  if (mainf && mainf->fbody) {
+  if (mainf && IFFUNC_HASBODY(mainf->type)) {
     fprintf (asmFile, "%s", iComments2);
     fprintf (asmFile, "; interrupt vector \n");
     fprintf (asmFile, "%s", iComments2);
@@ -1169,7 +1223,7 @@ pic14glue ()
   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) {
+  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
@@ -1182,28 +1236,9 @@ pic14glue ()
              (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");
-       
   }
-  //copyFile (asmFile, statsg->oFile);
-  copypCode(asmFile, statsg->dbName);
 
-  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
@@ -1218,33 +1253,13 @@ pic14glue ()
   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");
-
-    } else {
-                  
-      fprintf(asmFile,";\treturn from main will lock up\n");
-      fprintf(asmFile,"\tgoto\t$\n");
-    }
-  }
-
-  AnalyzepCode(code->dbName);
-  pCodePeepInit();
-
-  OptimizepCode(code->dbName);
   //copyFile (asmFile, code->oFile);
+
+  copypCode(asmFile, statsg->dbName);
+  copypCode(asmFile, 'X');
   copypCode(asmFile, code->dbName);
 
-  printCallTree(stderr);
 
   fprintf (asmFile,"\tend\n");