ack/mach/mips/top/table
George Koehler 0576641cae Reduce clang warnings from top
Also add `static` and remove `register` in mach/proto/top/top.c.  A
static function is only in one file, so its function declaration may
go in that file, instead of a header file.
2019-10-30 18:36:55 -04:00

52 lines
1 KiB
Plaintext

/* MIPS table for ACK target optimizer */
MAXOP 5;
LABEL_STARTER '.';
{
int plus(const char *, const char *, char *);
}
%%;
X, Y, Z { TRUE };
R { TRUE };
%%;
/* Whitespace is significant here! */
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 ;
%%;
/* 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)
{
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;
}