+ int varNumber, expectedRefCount;
+ bool rc = FALSE;
+
+ if (sscanf (cmdLine, "%*[ \t%]%d %d", &varNumber, &expectedRefCount) == 2)
+ {
+ char *label = hTabItemWithKey (vars, varNumber);
+
+ if (label)
+ {
+ labelHashEntry *entry = getLabelRef (label, head);
+
+ if (entry)
+ {
+#if 0
+ /* 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);
+ }
+ }
+ else
+ {
+ 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);
+ }
+ 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
+** was found and not volatile, or var was a constant or CPU register).
+** Returns FALSE if the symbol was found and volatile, the symbol was
+** not found, or var was a indirect/pointer addressing mode.
+*/
+static bool
+notVolatileVariable(char *var, lineNode *currPl, lineNode *endPl)
+{
+ char symname[SDCC_NAME_MAX + 1];
+ char *p = symname;
+ char *vp = var;
+ lineNode *cl;
+ operand *op;
+ iCode *last_ic;
+
+ /* Can't tell if indirect accesses are volatile or not, so
+ ** assume they are, just to be safe.
+ */
+ if (TARGET_IS_MCS51 || TARGET_IS_DS390 || TARGET_IS_DS400)
+ {
+ if (*var=='@')
+ return FALSE;
+ }
+ if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
+ {
+ if (strstr(var,"(bc)"))
+ return FALSE;
+ if (strstr(var,"(de)"))
+ return FALSE;
+ if (strstr(var,"(hl)"))
+ return FALSE;
+ if (strstr(var,"(ix"))
+ return FALSE;
+ if (strstr(var,"(iy"))
+ return FALSE;
+ }
+
+ /* Extract a symbol name from the variable */
+ while (*vp && (*vp!='_'))
+ vp++;
+ while (*vp && (ISCHARALNUM(*vp) || *vp=='_'))
+ *p++ = *vp++;
+ *p='\0';
+
+ if (!symname[0])
+ {
+ /* Nothing resembling a symbol name was found, so it can't
+ be volatile
+ */
+ return TRUE;
+ }
+
+ last_ic = NULL;
+ for (cl = currPl; cl!=endPl->next; cl = cl->next)
+ {
+ if (cl->ic && (cl->ic!=last_ic))
+ {
+ last_ic = cl->ic;
+ switch (cl->ic->op)
+ {
+ case IFX:
+ op = IC_COND (cl->ic);
+ if (IS_SYMOP (op) &&
+ ( !strcmp(OP_SYMBOL (op)->rname, symname) ||
+ (OP_SYMBOL (op)->isspilt &&
+ SPIL_LOC (op) &&
+ !strcmp(SPIL_LOC (op)->rname, symname)) ))
+ {
+ return !op->isvolatile;
+ }
+ case JUMPTABLE:
+ op = IC_JTCOND (cl->ic);
+ if (IS_SYMOP (op) &&
+ ( !strcmp(OP_SYMBOL (op)->rname, symname) ||
+ (OP_SYMBOL (op)->isspilt &&
+ SPIL_LOC (op) &&
+ !strcmp(SPIL_LOC (op)->rname, symname)) ))
+ {
+ return !op->isvolatile;
+ }
+ default:
+ op = IC_LEFT (cl->ic);
+ if (IS_SYMOP (op) &&
+ ( !strcmp(OP_SYMBOL (op)->rname, symname) ||
+ (OP_SYMBOL (op)->isspilt &&
+ SPIL_LOC (op) &&
+ !strcmp(SPIL_LOC (op)->rname, symname)) ))
+ {
+ return !op->isvolatile;
+ }
+ op = IC_RIGHT (cl->ic);
+ if (IS_SYMOP (op) &&
+ ( !strcmp(OP_SYMBOL (op)->rname, symname) ||
+ (OP_SYMBOL (op)->isspilt &&
+ SPIL_LOC (op) &&
+ !strcmp(SPIL_LOC (op)->rname, symname)) ))
+ {
+ return !op->isvolatile;
+ }
+ op = IC_RESULT (cl->ic);
+ if (IS_SYMOP (op) &&
+ ( !strcmp(OP_SYMBOL (op)->rname, symname) ||
+ (OP_SYMBOL (op)->isspilt &&
+ SPIL_LOC (op) &&
+ !strcmp(SPIL_LOC (op)->rname, symname)) ))
+ {
+ return !op->isvolatile;
+ }
+ }
+ }
+ }
+
+ /* Couldn't find the symbol for some reason. Assume volatile. */
+ return FALSE;
+}
+
+/* notVolatile:
+ *
+ * This rule restriction has two different behaviours depending on
+ * the number of parameters given.
+ *
+ * if notVolatile (no parameters given)
+ * The rule is applied only if none of the iCodes originating
+ * the matched pattern reference a volatile operand.
+ *
+ * if notVolatile %1 ... (one or more parameters given)
+ * The rule is applied if the parameters are not expressions
+ * containing volatile symbols and are not pointer accesses.
+ *
+ */
+FBYNAME (notVolatile)
+{
+ int varNumber;
+ char *var;
+ bool notvol;
+ char *digitend;
+ lineNode *cl;
+ operand *op;
+
+ if (!cmdLine)
+ {
+ /* If no parameters given, just scan the iCodes for volatile operands */
+ for (cl = currPl; cl!=endPl->next; cl = cl->next)
+ {
+ if (cl->ic)
+ {
+ switch (cl->ic->op)
+ {
+ case IFX:
+ op = IC_COND (cl->ic);
+ if (IS_SYMOP (op) && op->isvolatile)
+ return FALSE;
+ case JUMPTABLE:
+ op = IC_JTCOND (cl->ic);
+ if (IS_SYMOP (op) && op->isvolatile)
+ return FALSE;
+ default:
+ op = IC_LEFT (cl->ic);
+ if (IS_SYMOP (op) && op->isvolatile)
+ return FALSE;
+ op = IC_RIGHT (cl->ic);
+ if (IS_SYMOP (op) && op->isvolatile)
+ return FALSE;
+ op = IC_RESULT (cl->ic);
+ if (IS_SYMOP (op) && op->isvolatile)
+ return FALSE;
+ }
+ }
+ }
+ return TRUE;
+ }
+
+ /* There were parameters; check the volatility of each */
+ while (*cmdLine && ISCHARSPACE(*cmdLine))
+ cmdLine++;
+ while (*cmdLine)
+ {
+ if (*cmdLine!='%')
+ goto error;
+ cmdLine++;
+ if (!ISCHARDIGIT(*cmdLine))
+ goto error;
+ varNumber = strtol(cmdLine, &digitend, 10);
+ cmdLine = digitend;
+ while (*cmdLine && ISCHARSPACE(*cmdLine))
+ cmdLine++;
+
+ var = hTabItemWithKey (vars, varNumber);
+
+ if (var)
+ {
+ notvol = notVolatileVariable (var, currPl, endPl);
+ if (!notvol)
+ return FALSE;
+ }
+ else
+ {
+ 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;
+ // bug 1739475, temp fix
+ if (op[0] == '@')
+ return operandBaseName(op+1);
+ }
+
+ return op;
+}
+
+/*-------------------------------------------------------------------*/
+/* operandsNotRelated - returns true if 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;
+}
+
+static const struct ftab
+{
+ char *fname;
+ int (*func) (hTab *, lineNode *, lineNode *, lineNode *, char *);
+}
+ftab[] = // sorted on the number of times used
+{ // in the peephole rules on 2007-10-29
+ {
+ "labelRefCount", labelRefCount //105
+ },
+ {
+ "notVolatile", notVolatile //85
+ },
+ {
+ "labelRefCountChange", labelRefCountChange //74
+ },
+ {
+ "labelInRange", labelInRange //37
+ },
+ {
+ "labelJTInRange", labelJTInRange //13
+ },
+ {
+ "operandsNotRelated", operandsNotRelated //9
+ },
+ {
+ "24bitMode", flat24bitMode //9
+ },
+ {
+ "operandsNotSame", operandsNotSame //8
+ },
+ {
+ "operandsNotSame3", operandsNotSame3
+ },
+ {
+ "operandsNotSame4", operandsNotSame4
+ },
+ {
+ "operandsNotSame5", operandsNotSame5
+ },
+ {
+ "operandsNotSame6", operandsNotSame6
+ },
+ {
+ "operandsNotSame7", operandsNotSame7
+ },
+ {
+ "operandsNotSame8", operandsNotSame8
+ },
+ {
+ "xramMovcOption", xramMovcOption
+ },
+ {
+ "portIsDS390", portIsDS390
+ },
+ {
+ "labelIsReturnOnly", labelIsReturnOnly
+ },
+ {
+ "labelIsUncondJump", labelIsUncondJump
+ },
+ {
+ "okToRemoveSLOC", okToRemoveSLOC
+ },
+ {
+ "deadMove", deadMove
+ },
+ {
+ "operandsLiteral", operandsLiteral
+ },
+ {
+ "useAcallAjmp", useAcallAjmp
+ },
+ {
+ "notUsed", notUsed
+ }
+};
+/*-----------------------------------------------------------------*/
+/* callFuncByName - calls a function as defined in the table */
+/*-----------------------------------------------------------------*/
+static int
+callFuncByName (char *fname,
+ hTab * vars,
+ lineNode * currPl, /* first source line matched */
+ lineNode * endPl, /* last source line matched */
+ lineNode * head)
+{
+ int i;
+ char *cmdCopy, *funcName, *funcArgs, *cmdTerm;
+ char c;
+ int rc;