+FBYNAME (operandsNotSame)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+
+ if (strcmp (op1, op2) == 0)
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame3- check if any pair of %1,%2,%3 are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame3)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op2, op3) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame4- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame4)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+ char *op4 = hTabItemWithKey (vars, 4);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op1, op4) == 0) ||
+ (strcmp (op2, op3) == 0) ||
+ (strcmp (op2, op4) == 0) ||
+ (strcmp (op3, op4) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame5- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame5)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+ char *op4 = hTabItemWithKey (vars, 4);
+ char *op5 = hTabItemWithKey (vars, 5);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op1, op4) == 0) ||
+ (strcmp (op1, op5) == 0) ||
+ (strcmp (op2, op3) == 0) ||
+ (strcmp (op2, op4) == 0) ||
+ (strcmp (op2, op5) == 0) ||
+ (strcmp (op3, op4) == 0) ||
+ (strcmp (op3, op5) == 0) ||
+ (strcmp (op4, op5) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame6- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame6)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+ char *op4 = hTabItemWithKey (vars, 4);
+ char *op5 = hTabItemWithKey (vars, 5);
+ char *op6 = hTabItemWithKey (vars, 6);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op1, op4) == 0) ||
+ (strcmp (op1, op5) == 0) ||
+ (strcmp (op1, op6) == 0) ||
+ (strcmp (op2, op3) == 0) ||
+ (strcmp (op2, op4) == 0) ||
+ (strcmp (op2, op5) == 0) ||
+ (strcmp (op2, op6) == 0) ||
+ (strcmp (op3, op4) == 0) ||
+ (strcmp (op3, op5) == 0) ||
+ (strcmp (op3, op6) == 0) ||
+ (strcmp (op4, op5) == 0) ||
+ (strcmp (op4, op6) == 0) ||
+ (strcmp (op5, op6) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame7- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame7)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+ char *op4 = hTabItemWithKey (vars, 4);
+ char *op5 = hTabItemWithKey (vars, 5);
+ char *op6 = hTabItemWithKey (vars, 6);
+ char *op7 = hTabItemWithKey (vars, 7);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op1, op4) == 0) ||
+ (strcmp (op1, op5) == 0) ||
+ (strcmp (op1, op6) == 0) ||
+ (strcmp (op1, op7) == 0) ||
+ (strcmp (op2, op3) == 0) ||
+ (strcmp (op2, op4) == 0) ||
+ (strcmp (op2, op5) == 0) ||
+ (strcmp (op2, op6) == 0) ||
+ (strcmp (op2, op7) == 0) ||
+ (strcmp (op3, op4) == 0) ||
+ (strcmp (op3, op5) == 0) ||
+ (strcmp (op3, op6) == 0) ||
+ (strcmp (op3, op7) == 0) ||
+ (strcmp (op4, op5) == 0) ||
+ (strcmp (op4, op6) == 0) ||
+ (strcmp (op4, op7) == 0) ||
+ (strcmp (op5, op6) == 0) ||
+ (strcmp (op5, op7) == 0) ||
+ (strcmp (op6, op7) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame8- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame8)
+{
+ char *op1 = hTabItemWithKey (vars, 1);
+ char *op2 = hTabItemWithKey (vars, 2);
+ char *op3 = hTabItemWithKey (vars, 3);
+ char *op4 = hTabItemWithKey (vars, 4);
+ char *op5 = hTabItemWithKey (vars, 5);
+ char *op6 = hTabItemWithKey (vars, 6);
+ char *op7 = hTabItemWithKey (vars, 7);
+ char *op8 = hTabItemWithKey (vars, 8);
+
+ if ( (strcmp (op1, op2) == 0) ||
+ (strcmp (op1, op3) == 0) ||
+ (strcmp (op1, op4) == 0) ||
+ (strcmp (op1, op5) == 0) ||
+ (strcmp (op1, op6) == 0) ||
+ (strcmp (op1, op7) == 0) ||
+ (strcmp (op1, op8) == 0) ||
+ (strcmp (op2, op3) == 0) ||
+ (strcmp (op2, op4) == 0) ||
+ (strcmp (op2, op5) == 0) ||
+ (strcmp (op2, op6) == 0) ||
+ (strcmp (op2, op7) == 0) ||
+ (strcmp (op2, op8) == 0) ||
+ (strcmp (op3, op4) == 0) ||
+ (strcmp (op3, op5) == 0) ||
+ (strcmp (op3, op6) == 0) ||
+ (strcmp (op3, op7) == 0) ||
+ (strcmp (op3, op8) == 0) ||
+ (strcmp (op4, op5) == 0) ||
+ (strcmp (op4, op6) == 0) ||
+ (strcmp (op4, op7) == 0) ||
+ (strcmp (op4, op8) == 0) ||
+ (strcmp (op5, op6) == 0) ||
+ (strcmp (op5, op7) == 0) ||
+ (strcmp (op5, op8) == 0) ||
+ (strcmp (op6, op7) == 0) ||
+ (strcmp (op6, op8) == 0) ||
+ (strcmp (op7, op8) == 0) )
+ return FALSE;
+ else
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* labelHashEntry- searches for a label in the list labelHash */
+/* Builds labelHash, if it does not yet exist. */
+/* Returns the labelHashEntry or NULL */
+/*-----------------------------------------------------------------*/
+labelHashEntry *
+getLabelRef (const char *label, lineNode *head)
+{
+ labelHashEntry *entry;
+
+ /* If we don't have the label hash table yet, build it. */
+ if (!labelHash)
+ {
+ buildLabelRefCountHash (head);
+ }
+
+ entry = hTabFirstItemWK (labelHash, hashSymbolName (label));
+
+ while (entry)
+ {
+ if (!strcmp (label, entry->name))
+ {
+ break;
+ }
+ entry = hTabNextItemWK (labelHash);
+ }
+ return entry;
+}
+
+/* labelRefCount:
+
+ * takes two parameters: a variable (bound to a label name)
+ * and an expected reference count.
+ *
+ * Returns TRUE if that label is defined and referenced exactly
+ * the given number of times.
+ */
+FBYNAME (labelRefCount)
+{
+ 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)