ack/mach/mips/top/table

52 lines
1 KiB
Plaintext
Raw Normal View History

/* MIPS table for ACK target optimizer */
MAXOP 5;
LABEL_STARTER '.';
{
int plus(const char *, const char *, char *);
}
%%;
X, Y, Z { TRUE };
2018-09-19 22:39:13 +00:00
R { TRUE };
%%;
/* 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 -> ;
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. */
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;
}