X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2FSDCCpeeph.c;h=c38627dc2a7e76bfb81779ad823aead57d3c2198;hb=bb939d05cb3f6e3e8f8d5cb10a63b8d48e07a085;hp=a470b6d9bfdc61007902188f21225be5bb2e9a18;hpb=10cd3fdad4b5b2b6bec63afc7f059f4cdfcbcaf6;p=fw%2Fsdcc diff --git a/src/SDCCpeeph.c b/src/SDCCpeeph.c index a470b6d9..c38627dc 100644 --- a/src/SDCCpeeph.c +++ b/src/SDCCpeeph.c @@ -25,6 +25,10 @@ #include "common.h" +#define ISCHARDIGIT(c) isdigit((unsigned char)c) +#define ISCHARSPACE(c) isspace((unsigned char)c) +#define ISCHARALNUM(c) isalnum((unsigned char)c) + static peepRule *rootRules = NULL; static peepRule *currRule = NULL; @@ -51,7 +55,7 @@ static bool matchLine (char *, char *, hTab **); bool isLabelDefinition (const char *line, const char **start, int *len); #define FBYNAME(x) int x (hTab *vars, lineNode *currPl, lineNode *endPl, \ - lineNode *head, const char *cmdLine) + lineNode *head, char *cmdLine) #if !OPT_DISABLE_PIC void peepRules2pCode(peepRule *); @@ -64,136 +68,8 @@ void pic16_peepRules2pCode(peepRule *); /*-----------------------------------------------------------------*/ /* pcDistance - afinds a label back ward or forward */ /*-----------------------------------------------------------------*/ -int -mcs51_instruction_size(const char *inst) -{ - char *op, op1[256], op2[256]; - int opsize; - const char *p; - - while (*inst && isspace(*inst)) inst++; - - #define ISINST(s) (strncmp(inst, (s), sizeof(s)-1) == 0) - - /* Based on the current (2003-08-22) code generation for the - small library, the top instruction probability is: - - 57% mov/movx/movc - 6% push - 6% pop - 4% inc - 4% lcall - 4% add - 3% clr - 2% subb - */ - /* mov, push, & pop are the 69% of the cases. Check them first! */ - if (ISINST("mov")) - { - if (*(inst+3)=='x') return 1; /* movx */ - if (*(inst+3)=='c') return 1; /* movc */ - goto checkoperands; /* mov */ - } - if (ISINST("push")) return 2; - if (ISINST("pop")) return 2; - - if (ISINST("lcall")) return 3; - if (ISINST("ret")) return 1; - if (ISINST("ljmp")) return 3; - if (ISINST("sjmp")) return 2; - if (ISINST("rlc")) return 1; - if (ISINST("rrc")) return 1; - if (ISINST("rl")) return 1; - if (ISINST("rr")) return 1; - if (ISINST("swap")) return 1; - if (ISINST("jc")) return 2; - if (ISINST("jnc")) return 2; - if (ISINST("jb")) return 3; - if (ISINST("jnb")) return 3; - if (ISINST("jbc")) return 3; - if (ISINST("jmp")) return 1; // always jmp @a+dptr - if (ISINST("jz")) return 2; - if (ISINST("jnz")) return 2; - if (ISINST("cjne")) return 3; - if (ISINST("mul")) return 1; - if (ISINST("div")) return 1; - if (ISINST("da")) return 1; - if (ISINST("xchd")) return 1; - if (ISINST("reti")) return 1; - if (ISINST("nop")) return 1; - if (ISINST("acall")) return 1; - if (ISINST("ajmp")) return 2; - -checkoperands: - p = inst; - while (*p && isalnum(*p)) p++; - for (op = op1, opsize=0; *p && *p != ',' && opsize < sizeof(op1); p++) { - if (!isspace(*p)) *op++ = *p, opsize++; - } - *op = '\0'; - if (*p == ',') p++; - for (op = op2, opsize=0; *p && *p != ',' && opsize < sizeof(op2); p++) { - if (!isspace(*p)) *op++ = *p, opsize++; - } - *op = '\0'; - - #define IS_A(s) (*(s) == 'a' && *(s+1) == '\0') - #define IS_C(s) (*(s) == 'c' && *(s+1) == '\0') - #define IS_Rn(s) (*(s) == 'r' && *(s+1) >= '0' && *(s+1) <= '7') - #define IS_atRi(s) (*(s) == '@' && *(s+1) == 'r') - - if (ISINST("mov")) { - if (IS_C(op1) || IS_C(op2)) return 2; - if (IS_A(op1)) { - if (IS_Rn(op2) || IS_atRi(op2)) return 1; - return 2; - } - if (IS_Rn(op1) || IS_atRi(op1)) { - if (IS_A(op2)) return 1; - return 2; - } - if (strcmp(op1, "dptr") == 0) return 3; - if (IS_A(op2) || IS_Rn(op2) || IS_atRi(op2)) return 2; - return 3; - } - if (ISINST("add") || ISINST("addc") || ISINST("subb") || ISINST("xch")) { - if (IS_Rn(op2) || IS_atRi(op2)) return 1; - return 2; - } - if (ISINST("inc") || ISINST("dec")) { - if (IS_A(op1) || IS_Rn(op1) || IS_atRi(op1)) return 1; - if (strcmp(op1, "dptr") == 0) return 1; - return 2; - } - if (ISINST("anl") || ISINST("orl") || ISINST("xrl")) { - if (IS_C(op1)) return 2; - if (IS_A(op1)) { - if (IS_Rn(op2) || IS_atRi(op2)) return 1; - return 2; - } else { - if (IS_A(op2)) return 2; - return 3; - } - } - if (ISINST("clr") || ISINST("setb") || ISINST("cpl")) { - if (IS_A(op1) || IS_C(op1)) return 1; - return 2; - } - if (ISINST("djnz")) { - if (IS_Rn(op1)) return 2; - return 3; - } - - if (*inst == 'a' && *(inst+1) == 'r' && *(inst+2) >= '0' && *(inst+2) <= '7' && op1[0] == '=') { - /* ignore ar0 = 0x00 type definitions */ - return 0; - } - - fprintf(stderr, "Warning, peephole unrecognized instruction: %s\n", inst); - return 3; -} -int +int pcDistance (lineNode * cpos, char *lbl, bool back) { lineNode *pl = cpos; @@ -205,45 +81,45 @@ pcDistance (lineNode * cpos, char *lbl, bool back) { if (pl->line && - *pl->line != ';' && - pl->line[strlen (pl->line) - 1] != ':' && - !pl->isDebug) { - if (TARGET_IS_MCS51) { - dist += mcs51_instruction_size(pl->line); - } else { - dist += 3; - } - } + *pl->line != ';' && + pl->line[strlen (pl->line) - 1] != ':' && + !pl->isDebug) { + if (port->peep.getSize) { + dist += port->peep.getSize(pl); + } else { + dist += 3; + } + } if (strncmp (pl->line, buff, strlen (buff)) == 0) - return dist; + return dist; if (back) - pl = pl->prev; + pl = pl->prev; else - pl = pl->next; + pl = pl->next; } return 0; } /*-----------------------------------------------------------------*/ -/* flat24bitModeAndPortDS390 - */ +/* flat24bitModeAndPortDS390 - */ /*-----------------------------------------------------------------*/ FBYNAME (flat24bitModeAndPortDS390) { return (((strcmp(port->target,"ds390") == 0) || - (strcmp(port->target,"ds400") == 0)) && - (options.model == MODEL_FLAT24)); + (strcmp(port->target,"ds400") == 0)) && + (options.model == MODEL_FLAT24)); } /*-----------------------------------------------------------------*/ -/* portIsDS390 - return true if port is DS390 */ +/* portIsDS390 - return true if port is DS390 */ /*-----------------------------------------------------------------*/ FBYNAME (portIsDS390) { return ((strcmp(port->target,"ds390") == 0) || - (strcmp(port->target,"ds400") == 0)); + (strcmp(port->target,"ds400") == 0)); } /*-----------------------------------------------------------------*/ @@ -279,6 +155,10 @@ FBYNAME (labelInRange) if (!lbl) return FALSE; + /* Don't optimize jumps in a jump table; a more generic test */ + if (currPl->ic && currPl->ic->op == JUMPTABLE) + return FALSE; + /* if the previous two instructions are "ljmp"s then don't do it since it can be part of a jump table */ if (currPl->prev && currPl->prev->prev && @@ -293,7 +173,7 @@ FBYNAME (labelInRange) for a relative jump. we could get more precise this will suffice for now since it catches > 90% cases */ dist = (pcDistance (currPl, lbl, TRUE) + - pcDistance (currPl, lbl, FALSE)); + pcDistance (currPl, lbl, FALSE)); /* changed to 127, now that pcDistance return actual number of bytes */ if (!dist || dist > 127) @@ -302,6 +182,51 @@ FBYNAME (labelInRange) return TRUE; } + +/*-----------------------------------------------------------------*/ +/* labelJTInRange - will check to see if label %5 and up are */ +/* within range. */ +/* Specifically meant to optimize long (3-byte) jumps to short */ +/* (2-byte) jumps in jumptables */ +/*-----------------------------------------------------------------*/ +FBYNAME (labelJTInRange) +{ + char *lbl; + int dist, count, i; + + if (!getenv("SDCC_SJMP_JUMPTABLE")) + return FALSE; + + /* Only optimize within a jump table */ + if (currPl->ic && currPl->ic->op != JUMPTABLE) + return FALSE; + + count = elementsInSet( IC_JTLABELS (currPl->ic) ); + + /* check all labels (this is needed if the case statements are unsorted) */ + for (i=0; i 127+ /* range of sjmp */ + (7+3*i)+ /* offset between this jump and currPl, + should use pcDistance instead? */ + (count-i-1) /* if peephole applies distance is shortened */ + ) + return FALSE; + } + return TRUE; +} + + /*-----------------------------------------------------------------*/ /* labelIsReturnOnly - Check if label %5 is followed by RET */ /*-----------------------------------------------------------------*/ @@ -311,30 +236,41 @@ FBYNAME (labelIsReturnOnly) const char *label, *p; const lineNode *pl; int len; + char * retInst; + + /* Don't optimize jumps in a jump table; a more generic test */ + if (currPl->ic && currPl->ic->op == JUMPTABLE) + return FALSE; label = hTabItemWithKey (vars, 5); if (!label) return FALSE; len = strlen(label); for(pl = currPl; pl; pl = pl->next) { - if (pl->line && !pl->isDebug && - pl->line[strlen(pl->line)-1] == ':') { - if (strncmp(pl->line, label, len) == 0) break; /* Found Label */ - if (strlen(pl->line) != 7 || !isdigit(*(pl->line)) || - !isdigit(*(pl->line+1)) || !isdigit(*(pl->line+2)) || - !isdigit(*(pl->line+3)) || !isdigit(*(pl->line+4)) || - *(pl->line+5) != '$') { - return FALSE; /* non-local label encountered */ - } - } + if (pl->line && !pl->isDebug && !pl->isComment && + pl->line[strlen(pl->line)-1] == ':') { + if (strncmp(pl->line, label, len) == 0) break; /* Found Label */ + if (strlen(pl->line) != 7 || !ISCHARDIGIT(*(pl->line)) || + !ISCHARDIGIT(*(pl->line+1)) || !ISCHARDIGIT(*(pl->line+2)) || + !ISCHARDIGIT(*(pl->line+3)) || !ISCHARDIGIT(*(pl->line+4)) || + *(pl->line+5) != '$') { + return FALSE; /* non-local label encountered */ + } + } } if (!pl) return FALSE; /* did not find the label */ pl = pl->next; + while (pl && (pl->isDebug || pl->isComment)) + pl = pl->next; if (!pl || !pl->line || pl->isDebug) return FALSE; /* next line not valid */ p = pl->line; - for (p = pl->line; *p && isspace(*p); p++) - ; - if (strcmp(p, "ret") == 0) return TRUE; + for (p = pl->line; *p && ISCHARSPACE(*p); p++) + ; + + retInst = "ret"; + if (TARGET_IS_HC08) + retInst = "rts"; + if (strcmp(p, retInst) == 0) return TRUE; return FALSE; } @@ -361,15 +297,15 @@ FBYNAME (okToRemoveSLOC) /* Look for any occurance of this SLOC before the peephole match */ for (pl = currPl->prev; pl; pl = pl->prev) { - if (pl->line && !pl->isDebug && !pl->isComment - && *pl->line != ';' && strstr(pl->line, sloc)) - return FALSE; + if (pl->line && !pl->isDebug && !pl->isComment + && *pl->line != ';' && strstr(pl->line, sloc)) + return FALSE; } /* Look for any occurance of this SLOC after the peephole match */ for (pl = endPl->next; pl; pl = pl->next) { - if (pl->line && !pl->isDebug && !pl->isComment - && *pl->line != ';' && strstr(pl->line, sloc)) - return FALSE; + if (pl->line && !pl->isDebug && !pl->isComment + && *pl->line != ';' && strstr(pl->line, sloc)) + return FALSE; } return TRUE; /* safe for a peephole to remove it :) */ } @@ -597,53 +533,131 @@ FBYNAME (labelRefCount) char *label = hTabItemWithKey (vars, varNumber); if (label) - { - labelHashEntry *entry; - - entry = hTabFirstItemWK (labelHash, hashSymbolName (label)); - - while (entry) - { - if (!strcmp (label, entry->name)) - { - break; - } - entry = hTabNextItemWK (labelHash); - } - if (entry) - { + { + labelHashEntry *entry; + + entry = hTabFirstItemWK (labelHash, hashSymbolName (label)); + + while (entry) + { + if (!strcmp (label, entry->name)) + { + break; + } + entry = hTabNextItemWK (labelHash); + } + if (entry) + { #if 0 - /* debug spew. */ - fprintf (stderr, "labelRefCount: %s has refCount %d, want %d\n", - label, entry->refCount, expectedRefCount); + /* debug spew. */ + fprintf (stderr, "labelRefCount: %s has refCount %d, want %d\n", + label, entry->refCount, expectedRefCount); #endif - rc = (expectedRefCount == entry->refCount); - } - else - { - fprintf (stderr, "*** internal error: no label has entry for" - " %s in labelRefCount peephole.\n", - label); - } - } + rc = (expectedRefCount == entry->refCount); + } + else + { + fprintf (stderr, "*** internal error: no label has entry for" + " %s in labelRefCount peephole.\n", + label); + } + } else - { - fprintf (stderr, "*** internal error: var %d not bound" - " in peephole labelRefCount rule.\n", - varNumber); - } + { + fprintf (stderr, "*** internal error: var %d not bound" + " in peephole labelRefCount rule.\n", + varNumber); + } } else { fprintf (stderr, - "*** internal error: labelRefCount peephole restriction" - " malformed: %s\n", cmdLine); + "*** internal error: labelRefCount peephole restriction" + " malformed: %s\n", cmdLine); } return rc; } + +/* labelRefCountChange: + * takes two parameters: a variable (bound to a label name) + * and a signed int for changing the reference count. + * + * Please note, this function is not a conditional. It unconditionally + * changes the label. It should be passed as the 'last' function + * so it only is applied if all other conditions have been met. + * + * should always return TRUE + */ +FBYNAME (labelRefCountChange) +{ + int varNumber, RefCountDelta; + bool rc = FALSE; + + /* If we don't have the label hash table yet, build it. */ + if (!labelHash) + { + buildLabelRefCountHash (head); + } + + if (sscanf (cmdLine, "%*[ \t%]%d %i", &varNumber, &RefCountDelta) == 2) + { + char *label = hTabItemWithKey (vars, varNumber); + + if (label) + { + labelHashEntry *entry; + + entry = hTabFirstItemWK (labelHash, hashSymbolName (label)); + + while (entry) + { + if (!strcmp (label, entry->name)) + { + break; + } + entry = hTabNextItemWK (labelHash); + } + if (entry) + { + if (0 <= entry->refCount + RefCountDelta) + { + entry->refCount += RefCountDelta; + rc = TRUE; + } + else + { + fprintf (stderr, "*** internal error: label %s may not get" + " negative refCount in %s peephole.\n", + label, __FUNCTION__); + } + } + else + { + fprintf (stderr, "*** internal error: no label has entry for" + " %s in %s peephole.\n", + label, __FUNCTION__); + } + } + else + { + fprintf (stderr, "*** internal error: var %d not bound" + " in peephole %s rule.\n", + varNumber, __FUNCTION__); + } + } + else + { + fprintf (stderr, + "*** internal error: labelRefCount peephole restriction" + " malformed: %s\n", cmdLine); + } + return rc; +} + + /* Within the context of the lines currPl through endPl, determine ** if the variable var contains a symbol that is volatile. Returns ** TRUE only if it is certain that this was not volatile (the symbol @@ -686,7 +700,7 @@ notVolatileVariable(char *var, lineNode *currPl, lineNode *endPl) /* Extract a symbol name from the variable */ while (*vp && (*vp!='_')) vp++; - while (*vp && (isalnum(*vp) || *vp=='_')) + while (*vp && (ISCHARALNUM(*vp) || *vp=='_')) *p++ = *vp++; *p='\0'; @@ -727,7 +741,7 @@ notVolatileVariable(char *var, lineNode *currPl, lineNode *endPl) } } } - + /* Couldn't find the symbol for some reason. Assume volatile. */ return FALSE; } @@ -789,18 +803,18 @@ FBYNAME (notVolatile) } /* There were parameters; check the volatility of each */ - while (*cmdLine && isspace(*cmdLine)) + while (*cmdLine && ISCHARSPACE(*cmdLine)) cmdLine++; while (*cmdLine) { if (*cmdLine!='%') goto error; cmdLine++; - if (!isdigit(*cmdLine)) + if (!ISCHARDIGIT(*cmdLine)) goto error; varNumber = strtol(cmdLine, &digitend, 10); cmdLine = digitend; - while (*cmdLine && isspace(*cmdLine)) + while (*cmdLine && ISCHARSPACE(*cmdLine)) cmdLine++; var = hTabItemWithKey (vars, varNumber); @@ -812,39 +826,194 @@ FBYNAME (notVolatile) return FALSE; } else - { - fprintf (stderr, "*** internal error: var %d not bound" - " in peephole notVolatile rule.\n", - varNumber); - return FALSE; - } + { + fprintf (stderr, "*** internal error: var %d not bound" + " in peephole notVolatile rule.\n", + varNumber); + return FALSE; + } } return TRUE; - - + + error: fprintf (stderr, "*** internal error: notVolatile peephole restriction" " malformed: %s\n", cmdLine); return FALSE; } - + + +/*------------------------------------------------------------------*/ +/* setFromConditionArgs - parse a peephole condition's arguments */ +/* to produce a set of strings, one per argument. Variables %x will */ +/* be replaced with their values. String literals (in single quotes)*/ +/* are accepted and return in unquoted form. */ +/*------------------------------------------------------------------*/ +static set * +setFromConditionArgs (char *cmdLine, hTab * vars) +{ + int varNumber; + char *var; + char *digitend; + set *operands = NULL; + + if (!cmdLine) + return NULL; + + while (*cmdLine && ISCHARSPACE(*cmdLine)) + cmdLine++; + + while (*cmdLine) + { + if (*cmdLine == '%') + { + cmdLine++; + if (!ISCHARDIGIT(*cmdLine)) + goto error; + varNumber = strtol(cmdLine, &digitend, 10); + cmdLine = digitend; + + var = hTabItemWithKey (vars, varNumber); + + if (var) + { + addSetHead (&operands, var); + } + else + goto error; + } + else if (*cmdLine == '\'' ) + { + char quote = *cmdLine; + + var = ++cmdLine; + while (*cmdLine && *cmdLine != quote) + cmdLine++; + if (*cmdLine == quote) + *cmdLine++ = '\0'; + else + goto error; + addSetHead (&operands, var); + } + else + goto error; + + while (*cmdLine && ISCHARSPACE(*cmdLine)) + cmdLine++; + } + + return operands; + +error: + deleteSet (&operands); + return NULL; +} + +static const char * +operandBaseName (const char *op) +{ + if (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_DS400) + { + if (!strcmp (op, "acc") || !strncmp (op, "acc.", 4)) + return "a"; + if (!strncmp (op, "ar", 2) && ISCHARDIGIT(*(op+2)) && !*(op+3)) + return op+1; + } + + return op; +} + + +/*-------------------------------------------------------------------*/ +/* operandsNotRelated - returns true of the condition's operands are */ +/* not related (taking into account register name aliases). N-way */ +/* comparison performed between all operands. */ +/*-------------------------------------------------------------------*/ +FBYNAME (operandsNotRelated) +{ + set *operands; + const char *op1, *op2; + + operands = setFromConditionArgs (cmdLine, vars); + + if (!operands) + { + fprintf (stderr, + "*** internal error: operandsNotRelated peephole restriction" + " malformed: %s\n", cmdLine); + return FALSE; + } + + while ((op1 = setFirstItem (operands))) + { + deleteSetItem (&operands, (void*)op1); + op1 = operandBaseName (op1); + + for (op2 = setFirstItem (operands); op2; op2 = setNextItem (operands)) + { + op2 = operandBaseName (op2); + if (strcmp (op1, op2) == 0) + { + deleteSet (&operands); + return FALSE; + } + } + } + + deleteSet (&operands); + return TRUE; +} + + +/*-------------------------------------------------------------------*/ +/* operandsLiteral - returns true of the condition's operands are */ +/* literals. */ +/*-------------------------------------------------------------------*/ +FBYNAME (operandsLiteral) +{ + set *operands; + const char *op; + + operands = setFromConditionArgs (cmdLine, vars); + + if (!operands) + { + fprintf (stderr, + "*** internal error: operandsLiteral peephole restriction" + " malformed: %s\n", cmdLine); + return FALSE; + } + + for (op = setFirstItem (operands); op; op = setNextItem (operands)) + { + if (!isdigit(*op)) + { + deleteSet (&operands); + return FALSE; + } + } + + deleteSet (&operands); + return TRUE; +} + /*-----------------------------------------------------------------*/ /* callFuncByName - calls a function as defined in the table */ /*-----------------------------------------------------------------*/ -int +int callFuncByName (char *fname, - hTab * vars, - lineNode * currPl, - lineNode * endPl, - lineNode * head) + hTab * vars, + lineNode * currPl, + lineNode * endPl, + lineNode * head) { struct ftab { char *fname; - int (*func) (hTab *, lineNode *, lineNode *, lineNode *, const char *); + int (*func) (hTab *, lineNode *, lineNode *, lineNode *, char *); } ftab[] = { @@ -852,6 +1021,10 @@ callFuncByName (char *fname, "labelInRange", labelInRange } , + { + "labelJTInRange", labelJTInRange + } + , { "operandsNotSame", operandsNotSame } @@ -879,7 +1052,7 @@ callFuncByName (char *fname, { "operandsNotSame8", operandsNotSame8 } - , + , { "24bitMode", flat24bitMode } @@ -906,53 +1079,109 @@ callFuncByName (char *fname, }, { "notVolatile", notVolatile + }, + { + "operandsNotRelated", operandsNotRelated + }, + { + "operandsLiteral", operandsLiteral + }, + { + "labelRefCountChange", labelRefCountChange } }; - int i; - char *cmdCopy, *funcName, *funcArgs; - int rc = -1; - - /* Isolate the function name part (we are passed the full condition - * string including arguments) + int i; + char *cmdCopy, *funcName, *funcArgs, *cmdTerm; + char c; + int rc; + + /* Isolate the function name part (we are passed the full condition + * string including arguments) */ - cmdCopy = Safe_strdup(fname); - funcName = strtok(cmdCopy, " \t"); - funcArgs = strtok(NULL, ""); + cmdTerm = cmdCopy = Safe_strdup(fname); - for (i = 0; i < ((sizeof (ftab)) / (sizeof (struct ftab))); i++) - { - if (strcmp (ftab[i].fname, funcName) == 0) - { - rc = (*ftab[i].func) (vars, currPl, endPl, head, - funcArgs); - } - } - - if (rc == -1) + do { - fprintf (stderr, - "could not find named function \"%s\" in " - "peephole function table\n", - funcName); - // If the function couldn't be found, let's assume it's - // a bad rule and refuse it. - rc = FALSE; + funcArgs = funcName = cmdTerm; + while ((c = *funcArgs) && c != ' ' && c != '\t' && c != '(') + funcArgs++; + *funcArgs = '\0'; /* terminate the function name */ + if (c) + funcArgs++; + + /* Find the start of the arguments */ + if (c == ' ' || c == '\t') + while ((c = *funcArgs) && (c == ' ' || c == '\t')) + funcArgs++; + + /* If the arguments started with an opening parenthesis, */ + /* use the closing parenthesis for the end of the */ + /* arguments and look for the start of another condition */ + /* that can optionally follow. If there was no opening */ + /* parethesis, then everything that follows are arguments */ + /* and there can be no additional conditions. */ + if (c == '(') + { + cmdTerm = funcArgs; + while ((c = *cmdTerm) && c != ')') + cmdTerm++; + *cmdTerm = '\0'; /* terminate the arguments */ + if (c == ')') + { + cmdTerm++; + while ((c = *cmdTerm) && (c == ' ' || c == '\t' || c == ',')) + cmdTerm++; + if (!*cmdTerm) + cmdTerm = NULL; + } + else + cmdTerm = NULL; /* closing parenthesis missing */ + } + else + cmdTerm = NULL; + + if (!*funcArgs) + funcArgs = NULL; + + rc = -1; + for (i = 0; i < ((sizeof (ftab)) / (sizeof (struct ftab))); i++) + { + if (strcmp (ftab[i].fname, funcName) == 0) + { + rc = (*ftab[i].func) (vars, currPl, endPl, head, + funcArgs); + break; + } + } + + if (rc == -1) + { + fprintf (stderr, + "could not find named function \"%s\" in " + "peephole function table\n", + funcName); + // If the function couldn't be found, let's assume it's + // a bad rule and refuse it. + rc = FALSE; + break; + } } + while (rc && cmdTerm); + + Safe_free(cmdCopy); - Safe_free(cmdCopy); - return rc; } /*-----------------------------------------------------------------*/ /* printLine - prints a line chain into a given file */ /*-----------------------------------------------------------------*/ -void +void printLine (lineNode * head, FILE * of) { iCode *last_ic = NULL; bool debug_iCode_tracking = (getenv("DEBUG_ICODE_TRACKING")!=NULL); - + if (!of) of = stdout; @@ -970,18 +1199,18 @@ printLine (lineNode * head, FILE * of) fprintf (of, "; iCode lost\n"); } } - + /* don't indent comments & labels */ if (head->line && - (*head->line == ';' || - head->line[strlen (head->line) - 1] == ':')) { - fprintf (of, "%s\n", head->line); + (*head->line == ';' || + head->line[strlen (head->line) - 1] == ':')) { + fprintf (of, "%s\n", head->line); } else { - if (head->isInline && *head->line=='#') { - // comment out preprocessor directives in inline asm - fprintf (of, ";"); - } - fprintf (of, "\t%s\n", head->line); + if (head->isInline && *head->line=='#') { + // comment out preprocessor directives in inline asm + fprintf (of, ";"); + } + fprintf (of, "\t%s\n", head->line); } head = head->next; } @@ -992,9 +1221,9 @@ printLine (lineNode * head, FILE * of) /*-----------------------------------------------------------------*/ peepRule * newPeepRule (lineNode * match, - lineNode * replace, - char *cond, - int restart) + lineNode * replace, + char *cond, + int restart) { peepRule *pr; @@ -1053,7 +1282,7 @@ connectLine (lineNode * pl1, lineNode * pl2) return pl2; } -#define SKIP_SPACE(x,y) { while (*x && (isspace(*x) || *x == '\n')) x++; \ +#define SKIP_SPACE(x,y) { while (*x && (ISCHARSPACE(*x) || *x == '\n')) x++; \ if (!*x) { fprintf(stderr,y); return ; } } #define EXPECT_STR(x,y,z) { while (*x && strncmp(x,y,strlen(y))) x++ ; \ @@ -1064,11 +1293,12 @@ connectLine (lineNode * pl1, lineNode * pl2) /*-----------------------------------------------------------------*/ /* getPeepLine - parses the peep lines */ /*-----------------------------------------------------------------*/ -static void +static void getPeepLine (lineNode ** head, char **bpp) { char lines[MAX_PATTERN_LEN]; char *lp; + int isComment; lineNode *currL = NULL; char *bp = *bpp; @@ -1076,41 +1306,45 @@ getPeepLine (lineNode ** head, char **bpp) { if (!*bp) - { - fprintf (stderr, "unexpected end of match pattern\n"); - return; - } + { + fprintf (stderr, "unexpected end of match pattern\n"); + return; + } if (*bp == '\n') - { - bp++; - while (isspace (*bp) || - *bp == '\n') - bp++; - } + { + bp++; + while (ISCHARSPACE (*bp) || + *bp == '\n') + bp++; + } if (*bp == '}') - { - bp++; - break; - } + { + bp++; + break; + } /* read till end of line */ lp = lines; while ((*bp != '\n' && *bp != '}') && *bp) - *lp++ = *bp++; - + *lp++ = *bp++; *lp = '\0'; - if (!currL) - *head = currL = newLineNode (lines); - else - currL = connectLine (currL, newLineNode (lines)); lp = lines; - while (*lp && isspace(*lp)) + while (*lp && ISCHARSPACE(*lp)) lp++; - if (*lp==';') - currL->isComment = 1; + isComment = (*lp == ';'); + + if (!isComment || (isComment && !options.noPeepComments)) + { + if (!currL) + *head = currL = newLineNode (lines); + else + currL = connectLine (currL, newLineNode (lines)); + currL->isComment = isComment; + } + } *bpp = bp; @@ -1119,12 +1353,12 @@ getPeepLine (lineNode ** head, char **bpp) /*-----------------------------------------------------------------*/ /* readRules - reads the rules from a string buffer */ /*-----------------------------------------------------------------*/ -static void +static void readRules (char *bp) { char restart = 0; char lines[MAX_PATTERN_LEN]; - char *lp; + char *lp, *rp; lineNode *match; lineNode *replace; lineNode *currL = NULL; @@ -1144,7 +1378,7 @@ top: /* then look for either "restart" or '{' */ while (strncmp (bp, "restart", 7) && - *bp != '{' && bp) + *bp != '{' && bp) bp++; /* not found */ @@ -1158,7 +1392,7 @@ top: if (*bp == '{') bp++; else - { /* must be restart */ + { /* must be restart */ restart++; bp += strlen ("restart"); /* look for '{' */ @@ -1180,36 +1414,65 @@ top: EXPECT_CHR (bp, '{', "expected '{'\n"); bp++; + /* save char position (needed for generating error msg) */ + rp = bp; + SKIP_SPACE (bp, "unexpected end of rule\n"); getPeepLine (&replace, &bp); /* look for a 'if' */ - while ((isspace (*bp) || *bp == '\n') && *bp) + while ((ISCHARSPACE (*bp) || *bp == '\n') && *bp) bp++; if (strncmp (bp, "if", 2) == 0) { bp += 2; - while ((isspace (*bp) || *bp == '\n') && *bp) - bp++; + while ((ISCHARSPACE (*bp) || *bp == '\n') && *bp) + bp++; if (!*bp) - { - fprintf (stderr, "expected condition name\n"); - return; - } + { + fprintf (stderr, "expected condition name\n"); + return; + } /* look for the condition */ lp = lines; while (*bp && (*bp != '\n')) - { - *lp++ = *bp++; - } + { + *lp++ = *bp++; + } *lp = '\0'; newPeepRule (match, replace, lines, restart); } else - newPeepRule (match, replace, NULL, restart); + { + if (*bp && strncmp (bp, "replace", 7)) + { + /* not the start of a new peeprule, so "if" should be here */ + + char strbuff[1000]; + char *cp; + + /* go to the start of the line following "{" of the "by" token */ + while (*rp && (*rp == '\n')) + rp++; + + /* copy text of rule starting with line after "by {" */ + cp = strbuff; + while (*rp && (rp < bp) && ((cp - strbuff) < sizeof(strbuff))) + *cp++ = *rp++; + + /* and now the rest of the line */ + while (*rp && (*rp != '\n') && ((cp - strbuff) < sizeof(strbuff))) + *cp++ = *rp++; + + *cp = '\0'; + fprintf (stderr, "%s\nexpected '} if ...'\n", strbuff); + return; + } + newPeepRule (match, replace, NULL, restart); + } goto top; } @@ -1217,12 +1480,12 @@ top: /*-----------------------------------------------------------------*/ /* keyForVar - returns the numeric key for a var */ /*-----------------------------------------------------------------*/ -static int +static int keyForVar (char *d) { int i = 0; - while (isdigit (*d)) + while (ISCHARDIGIT (*d)) { i *= 10; i += (*d++ - '0'); @@ -1234,7 +1497,7 @@ keyForVar (char *d) /*-----------------------------------------------------------------*/ /* bindVar - binds a value to a variable in the given hashtable */ /*-----------------------------------------------------------------*/ -static void +static void bindVar (int key, char **s, hTab ** vtab) { char vval[MAX_PATTERN_LEN]; @@ -1245,30 +1508,30 @@ bindVar (int key, char **s, hTab ** vtab) vvx = *s; /* the value is ended by a ',' or space or newline or null or ) */ while (*vvx && - *vvx != ',' && - !isspace (*vvx) && - *vvx != '\n' && - *vvx != ':' && - *vvx != ')') + *vvx != ',' && + !ISCHARSPACE (*vvx) && + *vvx != '\n' && + *vvx != ':' && + *vvx != ')') { char ubb = 0; /* if we find a '(' then we need to balance it */ if (*vvx == '(') - { - ubb++; - while (ubb) - { - *vv++ = *vvx++; - if (*vvx == '(') - ubb++; - if (*vvx == ')') - ubb--; - } - // include the trailing ')' - *vv++ = *vvx++; - } + { + ubb++; + while (ubb) + { + *vv++ = *vvx++; + if (*vvx == '(') + ubb++; + if (*vvx == ')') + ubb--; + } + // include the trailing ')' + *vv++ = *vvx++; + } else - *vv++ = *vvx++; + *vv++ = *vvx++; } *s = vvx; *vv = '\0'; @@ -1281,7 +1544,7 @@ bindVar (int key, char **s, hTab ** vtab) /*-----------------------------------------------------------------*/ /* matchLine - matches one line */ /*-----------------------------------------------------------------*/ -static bool +static bool matchLine (char *s, char *d, hTab ** vars) { @@ -1292,56 +1555,56 @@ matchLine (char *s, char *d, hTab ** vars) { /* skip white space in both */ - while (isspace (*s)) - s++; - while (isspace (*d)) - d++; + while (ISCHARSPACE (*s)) + s++; + while (ISCHARSPACE (*d)) + d++; /* if the destination is a var */ - if (*d == '%' && isdigit (*(d + 1)) && vars) - { - char *v = hTabItemWithKey (*vars, keyForVar (d + 1)); - /* if the variable is already bound - then it MUST match with dest */ - if (v) - { - while (*v) - if (*v++ != *s++) - return FALSE; - } - else - /* variable not bound we need to - bind it */ - bindVar (keyForVar (d + 1), &s, vars); - - /* in either case go past the variable */ - d++; - while (isdigit (*d)) - d++; - - while (isspace (*s)) - s++; - while (isspace (*d)) - d++; - } + if (*d == '%' && ISCHARDIGIT (*(d + 1)) && vars) + { + char *v = hTabItemWithKey (*vars, keyForVar (d + 1)); + /* if the variable is already bound + then it MUST match with dest */ + if (v) + { + while (*v) + if (*v++ != *s++) + return FALSE; + } + else + /* variable not bound we need to + bind it */ + bindVar (keyForVar (d + 1), &s, vars); + + /* in either case go past the variable */ + d++; + while (ISCHARDIGIT (*d)) + d++; + + while (ISCHARSPACE (*s)) + s++; + while (ISCHARSPACE (*d)) + d++; + } /* they should be an exact match other wise */ if (*s && *d) - { - if (*s++ != *d++) - return FALSE; - } + { + if (*s++ != *d++) + return FALSE; + } } /* get rid of the trailing spaces in both source & destination */ if (*s) - while (isspace (*s)) + while (ISCHARSPACE (*s)) s++; if (*d) - while (isspace (*d)) + while (ISCHARSPACE (*d)) d++; /* after all this if only one of them @@ -1355,16 +1618,16 @@ matchLine (char *s, char *d, hTab ** vars) /*-----------------------------------------------------------------*/ /* matchRule - matches a all the rule lines */ /*-----------------------------------------------------------------*/ -static bool +static bool matchRule (lineNode * pl, - lineNode ** mtail, - peepRule * pr, - lineNode * head) + lineNode ** mtail, + peepRule * pr, + lineNode * head) { - lineNode *spl; /* source pl */ - lineNode *rpl; /* rule peep line */ + lineNode *spl; /* source pl */ + lineNode *rpl; /* rule peep line */ -/* setToNull((void **) &pr->vars); */ +/* setToNull((void *) &pr->vars); */ /* pr->vars = newHashTable(100); */ /* for all the lines defined in the rule */ @@ -1377,18 +1640,18 @@ matchRule (lineNode * pl, comment line don't process or the source line contains == . debugger information skip it */ if (spl->line && - (*spl->line == ';' || spl->isDebug)) - { - spl = spl->next; - continue; - } + (*spl->line == ';' || spl->isDebug)) + { + spl = spl->next; + continue; + } if (!matchLine (spl->line, rpl->line, &pr->vars)) - return FALSE; + return FALSE; rpl = rpl->next; if (rpl) - spl = spl->next; + spl = spl->next; } /* if rules ended */ @@ -1396,20 +1659,20 @@ matchRule (lineNode * pl, { /* if this rule has additional conditions */ if (pr->cond) - { - if (callFuncByName (pr->cond, pr->vars, pl, spl, head)) - { - *mtail = spl; - return TRUE; - } - else - return FALSE; - } + { + if (callFuncByName (pr->cond, pr->vars, pl, spl, head)) + { + *mtail = spl; + return TRUE; + } + else + return FALSE; + } else - { - *mtail = spl; - return TRUE; - } + { + *mtail = spl; + return TRUE; + } } else return FALSE; @@ -1419,8 +1682,8 @@ static void reassociate_ic_down (lineNode *shead, lineNode *stail, lineNode *rhead, lineNode *rtail) { - lineNode *csl; /* current source line */ - lineNode *crl; /* current replacement line */ + lineNode *csl; /* current source line */ + lineNode *crl; /* current replacement line */ csl = shead; crl = rhead; @@ -1451,8 +1714,8 @@ static void reassociate_ic_up (lineNode *shead, lineNode *stail, lineNode *rhead, lineNode *rtail) { - lineNode *csl; /* current source line */ - lineNode *crl; /* current replacement line */ + lineNode *csl; /* current source line */ + lineNode *crl; /* current replacement line */ csl = stail; crl = rtail; @@ -1486,11 +1749,11 @@ static void reassociate_ic (lineNode *shead, lineNode *stail, lineNode *rhead, lineNode *rtail) { - lineNode *csl; /* current source line */ - lineNode *crl; /* current replacement line */ + lineNode *csl; /* current source line */ + lineNode *crl; /* current replacement line */ bool single_iCode; iCode *ic; - + /* Check to see if all the source lines (excluding comments) came ** for the same iCode */ @@ -1516,11 +1779,11 @@ reassociate_ic (lineNode *shead, lineNode *stail, crl->ic = ic; return; } - + single_iCode = FALSE; break; } - + /* If all of the source lines came from the same iCode, then so have ** all of the replacement lines too. */ @@ -1530,7 +1793,7 @@ reassociate_ic (lineNode *shead, lineNode *stail, crl->ic = ic; return; } - + /* The source lines span iCodes, so we may end up with replacement ** lines that we don't know which iCode(s) to associate with. Do the ** best we can by using the following strategies: @@ -1547,7 +1810,7 @@ reassociate_ic (lineNode *shead, lineNode *stail, /* Strategy #1: Start at the top and scan down for matches */ reassociate_ic_down(shead,stail,rhead,rtail); - + /* Strategy #2: Start at the bottom and scan up for matches */ reassociate_ic_up(shead,stail,rhead,rtail); @@ -1559,7 +1822,7 @@ reassociate_ic (lineNode *shead, lineNode *stail, { const char *labelStart; int labelLength; - + /* skip over any comments */ while (csl!=stail->next && csl->isComment) csl = csl->next; @@ -1587,7 +1850,7 @@ reassociate_ic (lineNode *shead, lineNode *stail, } csl = csl->next; } - + /* Try to assign a meaningful iCode to any comment that is missing one. Since they are comments, it's ok to make mistakes; we are just trying to improve continuity to simplify other tests. @@ -1601,11 +1864,11 @@ reassociate_ic (lineNode *shead, lineNode *stail, } } - + /*-----------------------------------------------------------------*/ /* replaceRule - does replacement of a matching pattern */ /*-----------------------------------------------------------------*/ -static void +static void replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr) { lineNode *cl = NULL; @@ -1620,12 +1883,12 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr) for (cl = *shead; cl != stail; cl = cl->next) { if (cl->line && (*cl->line == ';' || cl->isDebug)) - { - pl = (pl ? connectLine (pl, newLineNode (cl->line)) : - (comment = newLineNode (cl->line))); - pl->isDebug = cl->isDebug; - pl->isComment = cl->isComment || (*cl->line == ';'); - } + { + pl = (pl ? connectLine (pl, newLineNode (cl->line)) : + (comment = newLineNode (cl->line))); + pl->isDebug = cl->isDebug; + pl->isComment = cl->isComment || (*cl->line == ';'); + } } cl = NULL; @@ -1639,34 +1902,34 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr) l = pl->line; while (*l) - { - /* if the line contains a variable */ - if (*l == '%' && isdigit (*(l + 1))) - { - v = hTabItemWithKey (pr->vars, keyForVar (l + 1)); - if (!v) - { - fprintf (stderr, "used unbound variable in replacement\n"); - l++; - continue; - } - while (*v) { - *lbp++ = *v++; - } - l++; - while (isdigit (*l)) { - l++; - } - continue; - } - *lbp++ = *l++; - } + { + /* if the line contains a variable */ + if (*l == '%' && ISCHARDIGIT (*(l + 1))) + { + v = hTabItemWithKey (pr->vars, keyForVar (l + 1)); + if (!v) + { + fprintf (stderr, "used unbound variable in replacement\n"); + l++; + continue; + } + while (*v) { + *lbp++ = *v++; + } + l++; + while (ISCHARDIGIT (*l)) { + l++; + } + continue; + } + *lbp++ = *l++; + } *lbp = '\0'; if (cl) - cl = connectLine (cl, newLineNode (lb)); + cl = connectLine (cl, newLineNode (lb)); else - lhead = cl = newLineNode (lb); + lhead = cl = newLineNode (lb); cl->isComment = pl->isComment; } @@ -1675,30 +1938,42 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr) { lineNode *lc = comment; while (lc->next) - lc = lc->next; + lc = lc->next; lc->next = lhead; if (lhead) - lhead->prev = lc; + lhead->prev = lc; lhead = comment; } - /* determine which iCodes the replacment lines relate to */ - reassociate_ic(*shead,stail,lhead,cl); - - /* now we need to connect / replace the original chain */ - /* if there is a prev then change it */ - if ((*shead)->prev) + if (lhead) { - (*shead)->prev->next = lhead; - lhead->prev = (*shead)->prev; + /* determine which iCodes the replacment lines relate to */ + reassociate_ic(*shead,stail,lhead,cl); + + /* now we need to connect / replace the original chain */ + /* if there is a prev then change it */ + if ((*shead)->prev) + { + (*shead)->prev->next = lhead; + lhead->prev = (*shead)->prev; + } + *shead = lhead; + /* now for the tail */ + if (stail && stail->next) + { + stail->next->prev = cl; + if (cl) + cl->next = stail->next; + } } - *shead = lhead; - /* now for the tail */ - if (stail && stail->next) + else { - stail->next->prev = cl; - if (cl) - cl->next = stail->next; + /* the replacement is empty - delete the source lines */ + if ((*shead)->prev) + (*shead)->prev->next = stail->next; + if (stail->next) + stail->next->prev = (*shead)->prev; + *shead = stail->next; } } @@ -1707,7 +1982,7 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr) * If so, start will point to the start of the label name, * and len will be it's length. */ -bool +bool isLabelDefinition (const char *line, const char **start, int *len) { const char *cp = line; @@ -1717,7 +1992,7 @@ isLabelDefinition (const char *line, const char **start, int *len) * (alnum | $ | _ ) followed by a colon. */ - while (*cp && isspace (*cp)) + while (*cp && ISCHARSPACE (*cp)) { cp++; } @@ -1729,7 +2004,7 @@ isLabelDefinition (const char *line, const char **start, int *len) *start = cp; - while (isalnum (*cp) || (*cp == '$') || (*cp == '_')) + while (ISCHARALNUM (*cp) || (*cp == '$') || (*cp == '_')) { cp++; } @@ -1744,7 +2019,7 @@ isLabelDefinition (const char *line, const char **start, int *len) } /* Quick & dirty string hash function. */ -static int +static int hashSymbolName (const char *name) { int hash = 0; @@ -1766,7 +2041,7 @@ hashSymbolName (const char *name) /* Build a hash of all labels in the passed set of lines * and how many times they are referenced. */ -static void +static void buildLabelRefCountHash (lineNode * head) { lineNode *line; @@ -1783,18 +2058,24 @@ buildLabelRefCountHash (lineNode * head) while (line) { if (isLabelDefinition (line->line, &label, &labelLen) - && labelLen <= SDCC_NAME_MAX) - { - labelHashEntry *entry; + && labelLen <= SDCC_NAME_MAX) + { + labelHashEntry *entry; + + entry = traceAlloc (&_G.labels, Safe_alloc(sizeof (labelHashEntry))); - entry = traceAlloc (&_G.labels, Safe_alloc(sizeof (labelHashEntry))); + memcpy (entry->name, label, labelLen); + entry->name[labelLen] = 0; + entry->refCount = -1; - memcpy (entry->name, label, labelLen); - entry->name[labelLen] = 0; - entry->refCount = -1; + /* Assume function entry points are referenced somewhere, */ + /* even if we can't find a reference (might be from outside */ + /* the function) */ + if (line->ic && (line->ic->op == FUNCTION)) + entry->refCount++; - hTabAddItem (&labelHash, hashSymbolName (entry->name), entry); - } + hTabAddItem (&labelHash, hashSymbolName (entry->name), entry); + } line = line->next; } @@ -1805,20 +2086,20 @@ buildLabelRefCountHash (lineNode * head) while (line) { for (i = 0; i < HTAB_SIZE; i++) - { - labelHashEntry *thisEntry; - - thisEntry = hTabFirstItemWK (labelHash, i); - - while (thisEntry) - { - if (strstr (line->line, thisEntry->name)) - { - thisEntry->refCount++; - } - thisEntry = hTabNextItemWK (labelHash); - } - } + { + labelHashEntry *thisEntry; + + thisEntry = hTabFirstItemWK (labelHash, i); + + while (thisEntry) + { + if (strstr (line->line, thisEntry->name)) + { + thisEntry->refCount++; + } + thisEntry = hTabNextItemWK (labelHash); + } + } line = line->next; } @@ -1831,11 +2112,11 @@ buildLabelRefCountHash (lineNode * head) thisEntry = hTabFirstItemWK (labelHash, i); while (thisEntry) - { - fprintf (stderr, "label: %s ref %d\n", - thisEntry->name, thisEntry->refCount); - thisEntry = hTabNextItemWK (labelHash); - } + { + fprintf (stderr, "label: %s ref %d\n", + thisEntry->name, thisEntry->refCount); + thisEntry = hTabNextItemWK (labelHash); + } } #endif } @@ -1852,13 +2133,13 @@ buildLabelRefCountHash (lineNode * head) matchLine Where is stuff allocated? - + */ /*-----------------------------------------------------------------*/ /* peepHole - matches & substitutes rules */ /*-----------------------------------------------------------------*/ -void +void peepHole (lineNode ** pls) { lineNode *spl; @@ -1891,21 +2172,21 @@ peepHole (lineNode ** pls) ** or comment */ if (spl->isDebug || spl->isComment || *(spl->line)==';') continue; - + mtail = NULL; /* Tidy up any data stored in the hTab */ - + /* if it matches */ if (matchRule (spl, &mtail, pr, *pls)) { - + /* then replace */ if (spl == *pls) replaceRule (pls, mtail, pr); else replaceRule (&spl, mtail, pr); - + /* if restart rule type then start at the top again */ if (pr->restart) @@ -1913,14 +2194,14 @@ peepHole (lineNode ** pls) restart = TRUE; } } - + if (pr->vars) { hTabDeleteAll (pr->vars); Safe_free (pr->vars); pr->vars = NULL; } - + freeTrace (&_G.values); } } @@ -1959,20 +2240,20 @@ readFileIntoBuffer (char *fname) /* if we maxed out our local buffer */ if (nch >= (MAX_PATTERN_LEN - 2)) - { - lb[nch] = '\0'; - /* copy it into allocated buffer */ - if (rs) - { - rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1); - strncatz (rs, lb, strlen (rs) + strlen (lb) + 1); - } - else - { - rs = Safe_strdup (lb); - } - nch = 0; - } + { + lb[nch] = '\0'; + /* copy it into allocated buffer */ + if (rs) + { + rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1); + strncatz (rs, lb, strlen (rs) + strlen (lb) + 1); + } + else + { + rs = Safe_strdup (lb); + } + nch = 0; + } } /* if some charaters left over */ @@ -1981,14 +2262,14 @@ readFileIntoBuffer (char *fname) lb[nch] = '\0'; /* copy it into allocated buffer */ if (rs) - { - rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1); - strncatz (rs, lb, strlen (rs) + strlen (lb) + 1); - } + { + rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1); + strncatz (rs, lb, strlen (rs) + strlen (lb) + 1); + } else - { - rs = Safe_strdup (lb); - } + { + rs = Safe_strdup (lb); + } } return rs; } @@ -1996,19 +2277,24 @@ readFileIntoBuffer (char *fname) /*-----------------------------------------------------------------*/ /* initPeepHole - initialises the peep hole optimizer stuff */ /*-----------------------------------------------------------------*/ -void +void initPeepHole () { char *s; /* read in the default rules */ - readRules (port->peep.default_rules); + if (!options.nopeep) + { + readRules (port->peep.default_rules); + } /* if we have any additional file read it too */ if (options.peep_file) { readRules (s = readFileIntoBuffer (options.peep_file)); - setToNull ((void **) &s); + setToNull ((void *) &s); + /* override nopeep setting, default rules have not been read */ + options.nopeep = 0; } @@ -2016,8 +2302,8 @@ initPeepHole () /* Convert the peep rules into pcode. NOTE: this is only support in the PIC port (at the moment) */ - if (TARGET_IS_PIC) - peepRules2pCode(rootRules); + if (TARGET_IS_PIC) + peepRules2pCode(rootRules); #endif #if !OPT_DISABLE_PIC16 @@ -2025,8 +2311,8 @@ initPeepHole () NOTE: this is only support in the PIC port (at the moment) and the PIC16 port (VR 030601) */ - if (TARGET_IS_PIC16) - pic16_peepRules2pCode(rootRules); + if (TARGET_IS_PIC16) + pic16_peepRules2pCode(rootRules); #endif