+/*-----------------------------------------------------------------*/
+/* 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<count; i++)
+ {
+ /* assumes that the %5 pattern variable has the first ljmp label */
+ lbl = hTabItemWithKey (vars, 5+i);
+ if (!lbl)
+ return FALSE;
+
+ dist = pcDistance (currPl, lbl, FALSE);
+
+ /* three terms used to calculate allowable distance */
+// printf("\nlabel %s %i dist %i cdist 0x%02x 0x%02x\n", lbl, i, dist, dist -(count-i-1)-(7+3*i), 127+(count-i-1)+(7+3*i) - dist);
+ if (!dist ||
+ dist > 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 */
+/*-----------------------------------------------------------------*/
+FBYNAME (labelIsReturnOnly)
+{
+ /* assumes that %5 pattern variable has the label name */
+ 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->isComment && pl->isLabel)
+ {
+ 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 && ISCHARSPACE(*p); p++)
+ ;
+
+ retInst = "ret";
+ if (TARGET_IS_HC08)
+ retInst = "rts";
+ if (strcmp(p, retInst) == 0)
+ return TRUE;
+ return FALSE;
+}
+
+/*-----------------------------------------------------------------*/
+/* labelIsUncondJump - Check if label %5 is followed by an */
+/* unconditional jump and put the destination of that jump in %6 */
+/*-----------------------------------------------------------------*/
+FBYNAME (labelIsUncondJump)
+{
+ /* assumes that %5 pattern variable has the label name */
+ const char *label;
+ char *p, *q;
+ const lineNode *pl;
+ int len;
+ char * jpInst = NULL;
+
+ /* 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->isComment && pl->isLabel)
+ {
+ 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)
+ return FALSE; /* next line not valid */
+ p = pl->line;
+ while (*p && ISCHARSPACE(*p))
+ p++;
+
+ if (TARGET_MCS51_LIKE)
+ jpInst = "ljmp";
+ if (TARGET_IS_HC08)
+ jpInst = "jmp";
+ if (TARGET_Z80_LIKE)
+ jpInst = "jp";
+ len = strlen(jpInst);
+ if (strncmp(p, jpInst, len) != 0)
+ return FALSE; /* next line is no jump */
+ p += len;
+ while (*p && ISCHARSPACE(*p))
+ p++;
+
+ q = p;
+ while (*q && *q!=';')
+ q++;
+ while (q>p && ISCHARSPACE(*q))
+ q--;
+ len = q-p;
+ if (len == 0)
+ return FALSE; /* no destination? */
+ if (TARGET_Z80_LIKE)
+ {
+ while (q>p && *q!=',')
+ q--;
+ if (*q==',')
+ return FALSE; /* conditional jump */
+ }
+ if (strcmp(p, q) == 0)
+ return FALSE; /* labels are equal */
+ /* now put the destination in %6 */
+ bindVar (6, &p, &vars);
+ return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* okToRemoveSLOC - Check if label %1 is a SLOC and not other */
+/* usage of it in the code depends on a value from this section */
+/*-----------------------------------------------------------------*/
+FBYNAME (okToRemoveSLOC)
+{
+ const lineNode *pl;
+ const char *sloc, *p;
+ int dummy1, dummy2, dummy3;
+
+ /* assumes that %1 as the SLOC name */
+ sloc = hTabItemWithKey (vars, 1);
+ if (sloc == NULL) return FALSE;
+ p = strstr(sloc, "sloc");
+ if (p == NULL) return FALSE;
+ p += 4;
+ if (sscanf(p, "%d_%d_%d", &dummy1, &dummy2, &dummy3) != 3) return FALSE;
+ /*TODO: ultra-paranoid: get funtion name from "head" and check that */
+ /* the sloc name begins with that. Probably not really necessary */
+
+ /* 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;
+ }
+ /* 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;
+ }
+ return TRUE; /* safe for a peephole to remove it :) */
+}
+
+/*-----------------------------------------------------------------*/
+/* deadMove - Check, if a pop/push pair can be removed */
+/*-----------------------------------------------------------------*/
+FBYNAME (deadMove)
+{
+ const char *reg = hTabItemWithKey (vars, 1);
+
+ if (port->peep.deadMove)
+ return port->peep.deadMove (reg, currPl, head);
+
+ fprintf (stderr, "Function deadMove not initialized in port structure\n");
+ return FALSE;
+}
+