2018-09-02 16:57:25 +00:00
|
|
|
|
|
|
|
/* MIPS table for ACK target optimizer */
|
|
|
|
|
|
|
|
MAXOP 5;
|
|
|
|
LABEL_STARTER '.';
|
|
|
|
|
2019-10-24 18:08:52 +00:00
|
|
|
{
|
2019-10-25 19:52:09 +00:00
|
|
|
int plus(const char *, const char *, char *);
|
2019-10-24 18:08:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-02 16:57:25 +00:00
|
|
|
%%;
|
|
|
|
|
|
|
|
X, Y, Z { TRUE };
|
2018-09-19 22:39:13 +00:00
|
|
|
R { TRUE };
|
2018-09-02 16:57:25 +00:00
|
|
|
|
|
|
|
%%;
|
|
|
|
|
|
|
|
/* Whitespace is significant here! */
|
|
|
|
|
2018-09-19 22:39:13 +00:00
|
|
|
addiu R, R, X : addiu R, R, Y { plus(X, Y, Z) } -> addiu R, R, Z ;
|
|
|
|
|
|
|
|
addiu X, X, 0 -> ;
|
2018-09-02 16:57:25 +00:00
|
|
|
|
|
|
|
b X : nop : labdef X -> labdef X ;
|
|
|
|
|
|
|
|
%%;
|
|
|
|
|
2018-09-19 22:39:13 +00:00
|
|
|
/* Does it fit a signed 16-bit integer? */
|
|
|
|
static int fits16(long l) {
|
|
|
|
return l >= -32768 && l <= 32767;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Tries sum = a + b with signed 16-bit integers. */
|
2019-10-25 19:52:09 +00:00
|
|
|
int plus(const char *a, const char *b, char *sum)
|
2018-09-19 22:39:13 +00:00
|
|
|
{
|
|
|
|
long la, lb, lsum;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
la = strtol(a, &end, 10);
|
|
|
|
if (*a == '\0' || *end != '\0' || !fits16(la))
|
|
|
|
return 0;
|
|
|
|
lb = strtol(b, &end, 10);
|
|
|
|
if (*b == '\0' || *end != '\0' || !fits16(lb))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
lsum = la + lb;
|
|
|
|
if (!fits16(lsum))
|
|
|
|
return 0;
|
|
|
|
snprintf(sum, 7, "%ld", lsum);
|
|
|
|
return 1;
|
|
|
|
}
|