Fixed the hairy inline 'Y' bug in SDCC.y
[fw/sdcc] / src / SDCCBBlock.c
index 84c1ff56a126c93fafc5f5fc247570fc6624ea73..2059b3d7a622a76d49d0a8b531e0b694126aca5e 100644 (file)
 -------------------------------------------------------------------------*/
 
 #include "common.h"
+#include "newalloc.h"
 
 int eBBNum = 0;
 set *graphEdges = NULL ;  /* list of edges in this flow graph */
-extern int labelKey ;
 
 /*-----------------------------------------------------------------*/
 /* printEntryLabel - prints entry label of a ebblock               */
@@ -47,7 +47,7 @@ eBBlock *neweBBlock ()
 {
     eBBlock *ebb;
 
-    ALLOC(ebb,sizeof(eBBlock));   
+    ebb = Safe_calloc(sizeof(eBBlock));   
     return ebb ;
 }
 
@@ -58,13 +58,77 @@ edge *newEdge (eBBlock *from, eBBlock *to)
 {
     edge *ep ;
 
-    ALLOC(ep,sizeof(edge));     
+    ep = Safe_calloc(sizeof(edge));     
     
     ep->from = from;
     ep->to = to;
     return ep;
 }
 
+/*-----------------------------------------------------------------*/
+/* dumpLiveRanges - dump liverange information into a file         */
+/*-----------------------------------------------------------------*/
+void dumpLiveRanges (char *ext,hTab *liveRanges)
+{
+    FILE *file;
+    symbol *sym;
+    int k;
+
+    if (ext) {
+       /* create the file name */
+       strcpy(buffer,srcFileName);
+       strcat(buffer,ext);
+       
+       if (!(file = fopen(buffer,"a+"))) {
+           werror(E_FILE_OPEN_ERR,buffer);
+           exit(1);
+       }
+    } else 
+       file = stdout;
+    
+    for (sym = hTabFirstItem(liveRanges,&k); sym ; 
+        sym = hTabNextItem(liveRanges,&k)) {
+
+       fprintf (file,"%s [k%d lr%d:%d so:%d]{ re%d rm%d}",
+                (sym->rname[0] ? sym->rname : sym->name), 
+                sym->key,
+                sym->liveFrom,sym->liveTo,
+                sym->stack,
+                sym->isreqv,sym->remat
+                );
+       
+       fprintf(file,"{"); printTypeChain(sym->type,file); 
+       if (sym->usl.spillLoc) {
+           fprintf(file,"}{ sir@ %s",sym->usl.spillLoc->rname);
+       }
+       fprintf(file,"}");
+       
+       /* if assigned to registers */
+       if (sym->nRegs) {
+           if (sym->isspilt) {
+               if (!sym->remat)
+                   if (sym->usl.spillLoc)
+                       fprintf(file,"[%s]",(sym->usl.spillLoc->rname[0] ?
+                                            sym->usl.spillLoc->rname :
+                                            sym->usl.spillLoc->name));
+                   else
+                       fprintf(file,"[err]");
+               else
+                   fprintf(file,"[remat]");
+           }
+           else {
+               int i;
+               fprintf(file,"[");
+               for(i=0;i<sym->nRegs;i++)
+                   fprintf(file,"%s ", port->getRegName(sym->regs[i]));
+               fprintf(file,"]");
+           }
+       }
+       fprintf(file,"\n");
+    }
+       
+    fclose(file);
+}
 /*-----------------------------------------------------------------*/
 /* dumpEbbsToFileExt - writeall the basic blocks to a file         */
 /*-----------------------------------------------------------------*/
@@ -73,14 +137,17 @@ void dumpEbbsToFileExt (char *ext,eBBlock **ebbs, int count)
     FILE *of;
     int i;
 
-    /* create the file name */
-    strcpy(buffer,srcFileName);
-    strcat(buffer,ext);
-
-    if (!(of = fopen(buffer,"a+"))) {
-       werror(E_FILE_OPEN_ERR,buffer);
-       exit(1);
-    }
+    if (ext) {
+       /* create the file name */
+       strcpy(buffer,srcFileName);
+       strcat(buffer,ext);
+       
+       if (!(of = fopen(buffer,"a+"))) {
+           werror(E_FILE_OPEN_ERR,buffer);
+           exit(1);
+       }
+    } else
+       of = stdout;
 
     for (i=0; i < count ; i++ ) {
        fprintf(of,"\n----------------------------------------------------------------\n");
@@ -238,7 +305,9 @@ void addiCodeToeBBlock ( eBBlock *ebp, iCode *ic , iCode *ip)
 
     /* if the last instruction is a goto */
     /* we add it just before the goto    */
-    if ( ebp->ech->op == GOTO || ebp->ech->op == JUMPTABLE) {
+    if ( ebp->ech->op == GOTO || ebp->ech->op == JUMPTABLE
+      || ebp->ech->op == RETURN) 
+    {
        ic->lineno = ebp->ech->lineno;
        ic->prev = ebp->ech->prev;       
        ebp->ech->prev = ic;
@@ -317,7 +386,8 @@ eBBlock **iCodeBreakDown (iCode *ic, int *count)
     *count = 0 ;
 
     /* allocate for the first entry */
-    ALLOC(ebbs,sizeof(eBBlock **));
+
+    ebbs = Safe_calloc(sizeof(eBBlock **));
        
     while (loop) {       
 
@@ -332,12 +402,13 @@ eBBlock **iCodeBreakDown (iCode *ic, int *count)
        /* put it in the array */
        ebbs[(*count)++] = ebb ;
        
-       /* allocate for the next one */
-       if (!(ebbs = GC_realloc(ebbs,(*count + 1)*sizeof(eBBlock **)))) {
-           werror(E_OUT_OF_MEM,__FILE__,(*count + 1)*sizeof(eBBlock **)); 
-           exit (1);
-       }
-       
+       /* allocate for the next one. Remember to clear the new */
+       /*  pointer at the end, that was created by realloc. */
+
+       ebbs = Safe_realloc(ebbs,(*count + 1)*sizeof(eBBlock **)) ;
+
+       ebbs[*count] = 0;
+
        /* if this one ends in a goto or a conditional */
        /* branch then check if the block it is going  */
        /* to already exists, if yes then this could   */
@@ -373,11 +444,12 @@ eBBlock **iCodeBreakDown (iCode *ic, int *count)
                }
                
                (*count)++ ;
+               
                /* if we have stopped at the block , allocate for an extra one */
-               if (!(ebbs = GC_realloc(ebbs,(*count + 1)*sizeof(eBBlock **)))) {
-                   werror(E_OUT_OF_MEM,__FILE__,(*count + 1)*sizeof(eBBlock **)); 
-                   exit (1);
-               }
+
+               ebbs = Safe_realloc(ebbs,(*count + 1)*sizeof(eBBlock **)) ;
+
+               ebbs[*count] = 0;
                
                /* then move the block down one count */  
                pBlock = ebbs[j = i];