* Makefile.in, configure.in, configure,
[fw/sdcc] / src / pic / pcodepeep.c
index 902178d0fb05a9e609b51a307b4cc743faae8f6d..129158937e6f6e782c6f669e0a0e5dc533c6afe0 100644 (file)
    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 -------------------------------------------------------------------------*/
 
-#include <stdio.h>
-#include <stdlib.h>
-
-#include "common.h"   // Include everything in the SDCC src directory
-#include "newalloc.h"
-//#define PCODE_DEBUG
 #include "pcode.h"
 #include "pcodeflow.h"
 #include "ralloc.h"
 
-#if defined(__BORLANDC__) || defined(_MSC_VER)
-#define STRCASECMP stricmp
-#else
-#define STRCASECMP strcasecmp
-#endif
-
-pCodeOp *popCopyGPR2Bit(pCodeOpReg *pc, int bitval);
-pCodeOp *popRegFromString(char *str, int size, int offset);
-
-pCodeOp *newpCodeOpWild(int id, pCodeWildBlock *pcwb, pCodeOp *subtype);
-pCode *newpCodeWild(int pCodeID, pCodeOp *optional_operand, pCodeOp *optional_label);
-pCode * findNextInstruction(pCode *pc);
-int getpCode(char *mnem,int dest);
-int getpCodePeepCommand(char *cmd);
-void pBlockMergeLabels(pBlock *pb);
-char *pCode2str(char *str, int size, pCode *pc);
-char *get_op( pCodeOp *pcop,char *buf,int buf_size);
-
-extern pCodeInstruction *pic14Mnemonics[];
-extern pCode * findPrevInstruction(pCode *pci);
-
+//#define PCODE_DEBUG
 
 #define IS_PCCOMMENT(x) ( x && (x->type==PC_COMMENT))
 
-/****************************************************************/
-/*
-* rootRules - defined in SDCCpeep.c
-*  This is a pointer to the (parsed) peephole rules that are
-* defined in peep.def.
-*/
-
-//extern peepRule *rootRules;
-
-
-
 
 /****************************************************************/
 /****************************************************************/
@@ -88,32 +51,6 @@ typedef struct pCodePeepSnippets
 
 static pCodePeepSnippets  *peepSnippets=NULL;
 
-/****************************************************************/
-/*                                                              */
-/* curPeep                                                      */
-/*                                                              */
-/****************************************************************/
-
-//static pCodePeep          *curPeep=NULL;
-
-/****************************************************************/
-/*                                                              */
-/* curBlock                                                     */
-/*                                                              */
-/****************************************************************/
-
-//static pBlock             *curBlock=NULL;
-
-
-/****************************************************************/
-/*                                                              */
-/* max wild cards in a peep rule                                */
-/*                                                              */
-/****************************************************************/
-
-//static int                sMaxWildVar   = 0;
-//static int                sMaxWildMnem  = 0;
-
 
 typedef struct pCodeToken 
 {
@@ -126,8 +63,8 @@ typedef struct pCodeToken
        
 } pCodeToken;
 
-pCodeToken tokArr[50];
-unsigned   tokIdx=0;
+static pCodeToken tokArr[50];
+static unsigned   tokIdx=0;
 
 
 typedef enum  {
@@ -151,9 +88,7 @@ typedef struct parsedPattern {
 } parsedPattern;
 
 #define MAX_PARSEDPATARR 50
-parsedPattern parsedPatArr[MAX_PARSEDPATARR];
-unsigned int parsedPatIdx=0;
-
+static parsedPattern parsedPatArr[MAX_PARSEDPATARR];
 
 typedef enum {
        PCP_LABEL=1,
@@ -180,7 +115,7 @@ typedef struct pcPattern {
        void * (*f) (void *,pCodeWildBlock *);
 } pcPattern;
 
-pcPattern pcpArr[] = {
+static pcPattern pcpArr[] = {
        {PCP_LABEL,     pcpat_label,      NULL},
        {PCP_WILDSTR,   pcpat_wildString, NULL},
        {PCP_STR,       pcpat_string,     NULL},
@@ -228,7 +163,7 @@ static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb);
 
-pcPattern altArr[] = {
+static pcPattern altArr[] = {
        {ALT_LABEL,        alt_label,  cvt_altpat_label},
        {ALT_COMMENT,      alt_comment,cvt_altpat_comment},
        {ALT_MNEM3,        alt_mnem3,  cvt_altpat_mnem3},
@@ -259,7 +194,7 @@ static int cvt_extract_destination(parsedPattern *pp)
                
                // just check first letter for now
                
-               if(toupper(*pp->pct[0].tok.s) == 'F')
+               if(toupper((unsigned char)*pp->pct[0].tok.s) == 'F')
                        return 1;
                
        } else if (pp->pct[0].tt == PCT_NUMBER) {
@@ -290,15 +225,15 @@ static pCodeOp *cvt_extract_status(char *reg, char *bit)
        
        if(len == 1) {
                // check C,Z
-               if(toupper(*bit) == 'C')
-                       return PCOP(popCopyGPR2Bit(&pc_status,PIC_C_BIT));
-               if(toupper(*bit) == 'Z')
-                       return PCOP(popCopyGPR2Bit(&pc_status,PIC_Z_BIT));
+               if(toupper((unsigned char)*bit) == 'C')
+                       return PCOP(popCopyGPR2Bit(PCOP(&pc_status),PIC_C_BIT));
+               if(toupper((unsigned char)*bit) == 'Z')
+                       return PCOP(popCopyGPR2Bit(PCOP(&pc_status),PIC_Z_BIT));
        }
        
        // Check DC
-       if(len ==2 && toupper(bit[0]) == 'D' && toupper(bit[1]) == 'C')
-               return PCOP(popCopyGPR2Bit(&pc_status,PIC_DC_BIT));
+       if(len ==2 && toupper((unsigned char)bit[0]) == 'D' && toupper((unsigned char)bit[1]) == 'C')
+               return PCOP(popCopyGPR2Bit(PCOP(&pc_status),PIC_DC_BIT));
        
        return NULL;
        
@@ -392,10 +327,6 @@ static void * cvt_altpat_mnem0a(void *pp, pCodeWildBlock *pcwb)
        DFPRINTF((stderr,"altpat_mnem0a wild mnem # %d\n",  p[0].pct[1].tok.n));
        
        /* Save the index of the maximum wildcard mnemonic */
-       
-       //if(p[0].pct[1].tok.n > sMaxWildVar)
-       //  sMaxWildMnem = p[0].pct[1].tok.n;
-       
        if(p[0].pct[1].tok.n > pcwb->nwildpCodes)
                pcwb->nwildpCodes = p[0].pct[1].tok.n;
        
@@ -503,9 +434,6 @@ static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb)
                newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
        
        /* Save the index of the maximum wildcard variable */
-       //if(p[1].pct[1].tok.n > sMaxWildVar)
-       //  sMaxWildVar = p[1].pct[1].tok.n;
-       
        if(p[1].pct[1].tok.n > pcwb->nvars)
                pcwb->nvars = p[1].pct[1].tok.n;
        
@@ -644,9 +572,6 @@ static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
                newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
        
        /* Save the index of the maximum wildcard variable */
-       //if(p[1].pct[1].tok.n > sMaxWildVar)
-       //  sMaxWildVar = p[1].pct[1].tok.n;
-       
        if(p[1].pct[1].tok.n > pcwb->nvars)
                pcwb->nvars = p[1].pct[1].tok.n;
        
@@ -745,15 +670,15 @@ static void tokenizeLineNode(char *ln)
        
        while(*ln) {
                
-               if(isspace(*ln)) {
+               if(isspace((unsigned char)*ln)) {
                        // add a SPACE token and eat the extra spaces.
                        tokArr[tokIdx++].tt = PCT_SPACE;
-                       while (isspace (*ln))
+                       while (isspace ((unsigned char)*ln))
                                ln++;
                        continue;
                }
                
-               if(isdigit(*ln)) {
+               if(isdigit((unsigned char)*ln)) {
                        
                        tokArr[tokIdx].tt = PCT_NUMBER;
                        tokArr[tokIdx++].tok.n = strtol(ln, &ln, 0);
@@ -786,11 +711,11 @@ static void tokenizeLineNode(char *ln)
                        
                        
                default:
-                       if(isalpha(*ln) || (*ln == '_') ) {
+                       if(isalpha((unsigned char)*ln) || (*ln == '_') ) {
                                char buffer[50];
                                int i=0;
                                
-                               while( (isalpha(*ln)  ||  isdigit(*ln) || (*ln == '_')) && i<49)
+                               while( (isalpha((unsigned char)*ln) || isdigit((unsigned char)*ln) || (*ln == '_')) && i<49)
                                        buffer[i++] = *ln++;
                                
                                ln--;
@@ -826,7 +751,7 @@ static void tokenizeLineNode(char *ln)
 
 
 
-void dump1Token(pCodeTokens tt)
+static void dump1Token(pCodeTokens tt)
 {
        
        switch(tt) {
@@ -871,7 +796,7 @@ void dump1Token(pCodeTokens tt)
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
 
-int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
+static int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
 {
        int i=0;
        
@@ -904,7 +829,7 @@ int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
 
-int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
+static int altComparePattern(char *pct, parsedPattern *pat, int max_tokens)
 {
        int i=0;
        
@@ -939,7 +864,7 @@ int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
 
-int advTokIdx(int *v, int amt)
+static int advTokIdx(int *v, int amt)
 {
        
        if((unsigned) (*v + amt) > tokIdx)
@@ -968,7 +893,7 @@ int advTokIdx(int *v, int amt)
 /* pcode.                                                          */
 /*-----------------------------------------------------------------*/
 
-int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
+static int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
 {
        pCode *pc;
        int error = 0;
@@ -1151,8 +1076,6 @@ int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
                                //  pc->print(stderr,pc);
                                //if(pc && pc->destruct) pc->destruct(pc); dumps core?
                                
-                               //if(curBlock && pc)
-                               //addpCode2pBlock(curBlock, pc);
                                if(pc) {
                                        if (pcret) {
                                                *pcret = pc;
@@ -1190,7 +1113,7 @@ int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
 /*-----------------------------------------------------------------*/
 /*                                                                 */
 /*-----------------------------------------------------------------*/
-void  peepRuleBlock2pCodeBlock(  lineNode *ln, pCodeWildBlock *pcwb)
+static void peepRuleBlock2pCodeBlock(lineNode *ln, pCodeWildBlock *pcwb)
 {
        
        if(!ln)
@@ -1214,27 +1137,6 @@ void  peepRuleBlock2pCodeBlock(  lineNode *ln, pCodeWildBlock *pcwb)
        }
 }
 
-/*-----------------------------------------------------------------*/
-/*                                                                 */
-/*-----------------------------------------------------------------*/
-pCode *AssembleLine(char *line)
-{
-       pCode *pc=NULL;
-       
-       if(!line || !*line) {
-               fprintf(stderr,"WARNING returning NULL in AssembleLine\n");
-               return NULL;
-       }
-       
-       tokenizeLineNode(line);
-
-       if(parseTokens(NULL,&pc))
-               fprintf(stderr, "WARNING: unable to assemble line:\n%s\n",line);
-       
-       return pc;
-       
-}
-
 /*-----------------------------------------------------------------*/
 /* peepRuleCondition                                               */
 /*-----------------------------------------------------------------*/
@@ -1254,8 +1156,7 @@ static void   peepRuleCondition(char *cond, pCodePeep *pcp)
        
 }
 
-
-void initpCodeWildBlock(pCodeWildBlock *pcwb)
+static void initpCodeWildBlock(pCodeWildBlock *pcwb)
 {
        
        //  pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
@@ -1273,7 +1174,7 @@ void initpCodeWildBlock(pCodeWildBlock *pcwb)
        
 }
 
-void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
+static void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
 {
        
        if(!pcwb)
@@ -1290,7 +1191,7 @@ void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
        
 }
 
-void initpCodePeep(pCodePeep *pcp)
+static void initpCodePeep(pCodePeep *pcp)
 {
        
        //  pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
@@ -1317,9 +1218,7 @@ void initpCodePeep(pCodePeep *pcp)
 /* taking raw text to produce machine code, it produces pCode.     */
 /*                                                                 */
 /*-----------------------------------------------------------------*/
-extern void pic14initpCodePeepCommands(void);
-
-void  peepRules2pCode(peepRule *rules)
+void peepRules2pCode(peepRule *rules)
 {
        peepRule *pr;
        
@@ -1352,23 +1251,12 @@ void  peepRules2pCode(peepRule *rules)
                //DFPRINTF((stderr,"finished target, here it is in pcode form:\n"));
                //printpBlock(stderr, currentRule->target.pb);
                
-               //DFPRINTF((stderr,"target with labels merged:\n"));
-               //pBlockMergeLabels(curBlock);
                pBlockMergeLabels(currentRule->target.pb);
                //printpBlock(stderr, currentRule->replace.pb);
                
-               //#ifdef PCODE_DEBUG
-               //    printpBlock(stderr, curBlock);
-               //#endif
-               //DFPRINTF((stderr,"\nReplaced by:\n"));
-               
-               
                /* Convert the replace block */
                peepRuleBlock2pCodeBlock(pr->replace, &currentRule->replace);
                
-               //DFPRINTF((stderr,"finished replace block, here it is in pcode form:\n"));
-               //printpBlock(stderr, curBlock);
-               
                //DFPRINTF((stderr,"replace with labels merged:\n"));
                
                pBlockMergeLabels(currentRule->replace.pb);
@@ -1402,16 +1290,6 @@ void  peepRules2pCode(peepRule *rules)
        
 }
 
-void printpCodeString(FILE *of, pCode *pc, int max)
-{
-       int i=0;
-       
-       while(pc && (i++<max)) {
-               pc->print(of,pc);
-               pc = pc->next;
-       }
-}
-
 /*-----------------------------------------------------------------*/
 /* _DLL * DLL_append                                               */
 /*                                                                 */ 
@@ -1491,7 +1369,7 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond, int contIfSkip)
                        if(PCI(pc)->inCond & cond) {
                                if (contIfSkip) {
                                        /* If previous instruction is a skip then continue search as condiction is not certain */
-                                       pCode *pcp = findPrevInstruction(pc);
+                                       pCode *pcp = findPrevInstruction(pc->prev);
                                        if (pcp && !isPCI_SKIP(pcp)) {
                                                return 1;
                                        }
@@ -1502,7 +1380,7 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond, int contIfSkip)
                        if(PCI(pc)->outCond & cond) {
                                if (contIfSkip) {
                                        /* If previous instruction is a skip then continue search as condiction is not certain */
-                                       pCode *pcp = findPrevInstruction(pc);
+                                       pCode *pcp = findPrevInstruction(pc->prev);
                                        if (pcp && !isPCI_SKIP(pcp)) {
                                                return -1;
                                        }
@@ -1523,7 +1401,7 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond, int contIfSkip)
 *
 * Compare two pCodeOp's and return 1 if they're the same
 *-----------------------------------------------------------------*/
-int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
+static int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
 {
        char b[50], *n2;
        
@@ -1576,7 +1454,7 @@ int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
        return 1;
 }
 
-int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
+static int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 {
        int labindex;
        
@@ -1659,7 +1537,7 @@ int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 /*                                                                 */
 /*-----------------------------------------------------------------*/
 
-int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
+static int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 {
        int index;   // index into wild card arrays
        
@@ -1882,7 +1760,7 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
-void pCodePeepClrVars(pCodePeep *pcp)
+static void pCodePeepClrVars(pCodePeep *pcp)
 {
        
        int i;
@@ -2100,9 +1978,10 @@ int pCodePeepMatchRule(pCode *pc)
                        /* We have just replaced the inefficient code with the rule.
                        * Now, we need to re-add the C-source symbols if there are any */
                        pc = pcprev;
-                       while(pc_cline ) {
+                       while(pc && pc_cline ) {
                                
                                pc =  findNextInstruction(pc->next);
+                               if (!pc) break;
                                PCI(pc)->cline = pc_cline;
                                pc_cline = PCCS(pc_cline->pc.next);
                                
@@ -2111,13 +1990,14 @@ int pCodePeepMatchRule(pCode *pc)
                        /* Copy C code comments to new code. */
                        pc = pcprev->next;
                        if (pc) {
-                               for (; pcout!=pcin; pcout=pcout->next) {
+                               for (; pc && pcout!=pcin; pcout=pcout->next) {
                                        if (pcout->type==PC_OPCODE && PCI(pcout)->cline) {
                                                while (pc->type!=PC_OPCODE || PCI(pc)->cline) {
                                                        pc = pc->next;
                                                        if (!pc)
                                                                break;
                                                }
+                                               if (!pc) break;
                                                PCI(pc)->cline = PCI(pcout)->cline;
                                        }
                                }