ack/modules/src/read_em/reade.c

663 lines
11 KiB
C
Raw Normal View History

1994-06-24 11:31:16 +00:00
/* $Id$ */
1987-03-09 15:15:03 +00:00
/*
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
* See the copyright notice in the ACK home directory, in the file "Copyright".
*/
1990-08-01 14:30:45 +00:00
/* This file is ment to be included in the file read_em.c.
1987-01-06 11:05:35 +00:00
It contains the part that takes care of the reading of human readable
EM-code.
*/
#include <ctype.h>
/* #define XXX_YYY /* only for early debugging */
#ifdef XXX_YYY
1987-03-12 22:24:19 +00:00
#define out(str) (sys_write(STDOUT, str, strlen(str)))
1987-01-06 11:05:35 +00:00
#else
#define out(s)
#endif
#define fit16i(x) ((x) >= -32768L && (x) <= 32767L)
#define HSIZE 256 /* Size of hashtable for mnemonics */
static int hashtab[HSIZE]; /* The hashtable for mnemonics */
static int argnum; /* Number of arguments */
#define COMMENTSTARTER ';'
/* inithash, pre_hash, hash: Simple hashtable mechanism
*/
PRIVATE int
hash(s)
register char *s;
{
register int h = 0;
while (*s) {
h <<= 1;
h += *s++;
}
return h;
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
pre_hash(i, s)
char *s;
{
register int h;
assert(i != 0);
h = hash(s);
for (;;) {
h++;
if (h >= HSIZE) h %= HSIZE;
if (hashtab[h] == 0) {
hashtab[h] = i;
return;
}
}
/*NOTREACHED*/
}
extern char em_mnem[][4];
extern char em_pseu[][4];
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
inithash()
{
register int i;
/* Enter instructions ... */
for (i = sp_fmnem; i <= sp_lmnem; i++) {
pre_hash(i, em_mnem[i - sp_fmnem]);
}
/* and pseudos ... */
for (i = sp_fpseu; i <= sp_lpseu; i++) {
pre_hash(i, em_pseu[i - sp_fpseu]);
}
}
/* nospace: skip until we find a non-space character. Also skip
comments.
*/
PRIVATE int
nospace()
{
register int c;
do c = getbyte();
while (isspace(c) && c != '\n');
if (c == COMMENTSTARTER) {
do c = getbyte();
while (c != '\n' && c != EOF);
}
return c;
}
/* syntax: Put an error message in EM_error and skip to the end of the line
*/
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
syntax(s)
char *s;
{
register int c;
xerror(s);
state = 0;
while ((c = getbyte()) != '\n' && c != EOF) /* nothing */ ;
ungetbyte(c);
}
/* checkeol: check that we have a complete line (except maybe for spaces)
*/
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
checkeol()
{
if (nospace() != '\n') {
syntax("end of line expected");
1987-03-12 22:24:19 +00:00
nospace();
1987-01-06 11:05:35 +00:00
}
}
/* getescape: read a '\' escape sequence
*/
PRIVATE int
getescape()
{
register int c, j, r;
if ((c = getbyte()) >= '0' && c <= '7') { /* numeric escape */
r = c - '0';
for (j = 0; j < 2; j++) {
if ((c = getbyte()) < '0' || c > '7') {
ungetbyte(c);
return r;
}
r <<= 3;
r += c - '0';
}
return r;
}
switch(c) {
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
}
return c;
}
/* getname: Read a string of characters representing an identifier
*/
PRIVATE struct string *
getname()
{
register char *p;
register struct string *s;
register int c;
1987-06-30 12:55:30 +00:00
s = &string;
1987-01-06 11:05:35 +00:00
p = s->str;
1990-08-01 14:30:45 +00:00
if (!p) {
s->maxlen = 256;
s->str = p = Malloc(256);
}
1987-01-06 11:05:35 +00:00
c = getbyte();
if (!(isalpha(c) || c == '_')) {
ungetbyte(c);
syntax("Letter expected");
return s;
}
while (isalnum(c) || c == '_') {
1990-08-01 14:30:45 +00:00
if (p >= &(s->str[s->maxlen])) {
int df = p - s->str;
s->str = Realloc(s->str, (s->maxlen += 256));
p = s->str + df;
}
*p++ = c;
1987-01-06 11:05:35 +00:00
c = getbyte();
}
ungetbyte(c);
*p = '\0';
s->length = p - s->str;
return s;
}
/* getstring: read a string of characters between quotes
*/
PRIVATE struct string *
getstring()
{
register char *p;
struct string *s;
register int c;
static int termc;
1987-06-30 12:55:30 +00:00
s = &string;
1987-01-06 11:05:35 +00:00
p = s->str;
1990-08-01 14:30:45 +00:00
if (!p) {
s->maxlen = 256;
s->str = p = Malloc(256);
1987-01-06 11:05:35 +00:00
}
1990-08-01 14:30:45 +00:00
termc = getbyte();
/* assert(termc == '"' || termc == '\''); */
/* This assertion does not work. Compiler error messages.
The trouble lies in the ", it terminates the string
created in the assertion macro
*/
1987-01-06 11:05:35 +00:00
for (;;) {
if ((c = getbyte()) == '\n' || c == EOF) {
ungetbyte(c);
syntax("non-terminated string");
break;
}
if (c == termc) {
if (termc == '"') *p++ = '\0';
break;
}
if (c == '\\') c = getescape();
1990-08-01 14:30:45 +00:00
if (p >= &(s->str[s->maxlen])) {
int df = p - s->str;
s->str = Realloc(s->str, (s->maxlen += 256));
p = s->str + df;
1987-01-06 11:05:35 +00:00
}
*p++ = c;
}
*p = '\0';
s->length = p - s->str;
return s;
}
1993-11-10 12:09:49 +00:00
PRIVATE void gettyp();
1987-01-06 11:05:35 +00:00
PRIVATE int
offsetted(argtyp, ap)
arith *ap;
{
register int c;
if ((c = nospace()) == '+' || c == '-') {
1987-06-30 12:55:30 +00:00
struct e_arg dummy;
gettyp(cst_ptyp, &dummy);
if (c == '-') *ap = -(dummy.ema_cst);
else *ap = dummy.ema_cst;
1987-01-06 11:05:35 +00:00
return sp_doff;
}
else *ap = 0;
ungetbyte(c);
return argtyp;
}
PRIVATE int
getnumber(c, ap)
register int c;
1987-06-30 12:55:30 +00:00
register struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
1990-08-01 14:30:45 +00:00
char str[256 + 1];
1987-06-30 12:55:30 +00:00
register char *p = str;
1987-01-06 11:05:35 +00:00
int n;
int expsign;
long str2long();
1987-06-30 18:59:51 +00:00
ap->ema_argtype = cst_ptyp;
1987-01-06 11:05:35 +00:00
expsign = 0;
if (c == '+' || c == '-') {
if (c == '-') *p++ = c;
c = getbyte();
}
if (! isdigit(c)) {
ungetbyte(c);
syntax("digit expected");
return sp_cst4;
}
n = sp_cst4;
for (;;) {
1990-08-01 14:30:45 +00:00
if (p >= &(str[256])) {
1987-01-06 11:05:35 +00:00
syntax("number too long");
return sp_cst4;
}
*p++ = c;
if ((c = getbyte()) == '.' || c == 'e' || c == 'E') {
expsign = c != '.';
n = sp_fcon;
continue;
}
if (expsign) {
expsign = 0;
if (c == '+' || c == '-') continue;
}
if (! isdigit(c)) break;
}
ungetbyte(c);
*p = '\0';
c = nospace();
if (n == sp_fcon && c != 'F') {
ungetbyte(c);
syntax("'F' expected");
return n;
}
if (c == 'I' || c == 'U' || c == 'F') {
1987-06-30 12:55:30 +00:00
struct e_arg dummy;
strcpy(string.str, str);
ap->ema_string = string.str;
gettyp(cst_ptyp, &dummy);
1987-06-30 18:59:51 +00:00
ap->ema_szoroff = dummy.ema_cst;
1987-01-06 11:05:35 +00:00
switch(c) {
case 'I':
1987-06-30 18:59:51 +00:00
ap->ema_argtype = ico_ptyp;
1987-01-06 11:05:35 +00:00
return sp_icon;
case 'U':
1987-06-30 18:59:51 +00:00
ap->ema_argtype = uco_ptyp;
1987-01-06 11:05:35 +00:00
return sp_ucon;
case 'F':
1987-06-30 18:59:51 +00:00
ap->ema_argtype = fco_ptyp;
1987-01-06 11:05:35 +00:00
return sp_fcon;
}
assert(0);
}
ungetbyte(c);
1987-06-30 12:55:30 +00:00
ap->ema_cst = (arith) str2long(str, 10);
1987-01-06 11:05:35 +00:00
return sp_cst4;
}
PRIVATE int getexpr();
PRIVATE int
getfactor(c, ap)
register int c;
1987-06-30 12:55:30 +00:00
register struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
if (c == '(') {
if (getexpr(nospace(), ap) != sp_cst4) {
syntax("expression expected");
}
else if ((c = nospace()) != ')') {
ungetbyte(c);
syntax("')' expected");
}
return sp_cst4;
}
return getnumber(c, ap);
}
PRIVATE int
getterm(c, ap)
register int c;
1987-06-30 12:55:30 +00:00
register struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
arith left;
if ((c = getfactor(c, ap)) != sp_cst4) return c;
for (;;) {
if ((c = nospace()) != '*' && c != '/' && c != '%') {
ungetbyte(c);
break;
}
1987-06-30 12:55:30 +00:00
left = ap->ema_cst;
1987-01-06 11:05:35 +00:00
if (getfactor(nospace(), ap) != sp_cst4) {
syntax("factor expected");
break;
}
1987-06-30 12:55:30 +00:00
if (c == '*') ap->ema_cst *= left;
else if (c == '/') ap->ema_cst = left / ap->ema_cst;
else ap->ema_cst = left % ap->ema_cst;
1987-01-06 11:05:35 +00:00
}
return sp_cst4;
}
PRIVATE int
getexpr(c, ap)
register int c;
1987-06-30 12:55:30 +00:00
register struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
arith left;
if ((c = getterm(c, ap)) != sp_cst4) return c;
for (;;) {
if ((c = nospace()) != '+' && c != '-') {
ungetbyte(c);
break;
}
1987-06-30 12:55:30 +00:00
left = ap->ema_cst;
1987-01-06 11:05:35 +00:00
if (getterm(nospace(), ap) != sp_cst4) {
syntax("term expected");
break;
}
1987-06-30 12:55:30 +00:00
if (c == '+') ap->ema_cst += left;
else ap->ema_cst = left - ap->ema_cst;
1987-01-06 11:05:35 +00:00
}
return sp_cst4;
}
PRIVATE int
get15u()
{
1987-06-30 12:55:30 +00:00
struct e_arg dummy;
1987-01-06 11:05:35 +00:00
1987-06-30 12:55:30 +00:00
if (getnumber(getbyte(), &dummy) != sp_cst4) {
1987-01-06 11:05:35 +00:00
syntax("integer expected");
}
1987-06-30 12:55:30 +00:00
else check((dummy.ema_cst & ~077777) == 0);
return (int) (dummy.ema_cst);
1987-01-06 11:05:35 +00:00
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-06-30 12:55:30 +00:00
gettyp(typset, ap)
register struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
register int c, t;
register int argtyp;
if ((c = nospace()) == '\n') {
ungetbyte(c);
out("newline\n");
argtyp = sp_cend;
}
else if (isdigit(c) || c == '+' || c == '-' || c == '(') {
out("expr\n");
argtyp = getexpr(c, ap);
1987-06-30 12:55:30 +00:00
if (argtyp == sp_cst4 && fit16i(ap->ema_cst)) argtyp = sp_cst2;
1987-01-06 11:05:35 +00:00
}
else if (isalpha(c) || c == '_') {
out("name\n");
ungetbyte(c);
1987-06-30 12:55:30 +00:00
ap->ema_dnam = getname()->str;
1987-06-30 18:59:51 +00:00
ap->ema_argtype = sof_ptyp;
argtyp = offsetted(sp_dnam, &(ap->ema_szoroff));
1987-01-06 11:05:35 +00:00
}
else if (c == '.') {
out(".label\n");
1987-06-30 12:55:30 +00:00
ap->ema_dlb = get15u();
1987-06-30 18:59:51 +00:00
ap->ema_argtype = nof_ptyp;
argtyp = offsetted(sp_dlb2, &(ap->ema_szoroff));
1987-01-06 11:05:35 +00:00
}
else if (c == '*') {
out("*label\n");
1987-06-30 12:55:30 +00:00
ap->ema_ilb = get15u();
1987-06-30 18:59:51 +00:00
ap->ema_argtype = ilb_ptyp;
1987-01-06 11:05:35 +00:00
argtyp = sp_ilb2;
}
else if (c == '$') {
out("$name\n");
1987-06-30 12:55:30 +00:00
ap->ema_pnam = getname()->str;
1987-06-30 18:59:51 +00:00
ap->ema_argtype = pro_ptyp;
1987-01-06 11:05:35 +00:00
argtyp = sp_pnam;
}
else if (c == '"' || c == '\'') {
register struct string *s;
out("string\n");
ungetbyte(c);
s = getstring(0);
1987-06-30 12:55:30 +00:00
ap->ema_string = s->str;
1987-06-30 18:59:51 +00:00
ap->ema_szoroff = s->length;
ap->ema_argtype = str_ptyp;
1987-01-06 11:05:35 +00:00
argtyp = sp_scon;
}
else if (c == '?') {
out("?\n");
argtyp = sp_cend;
1987-06-30 18:59:51 +00:00
ap->ema_argtype = 0;
1987-01-06 11:05:35 +00:00
}
else {
/* c != '\n', so "ungetbyte" not neccesary */
syntax("operand expected");
1987-06-30 12:55:30 +00:00
return;
1987-01-06 11:05:35 +00:00
}
t = argtyp - sp_fspec;
assert(t >= 0 && t < 16);
if ((typset & (1 << t)) == 0) {
syntax("Bad argument type");
1987-06-30 12:55:30 +00:00
return;
1987-01-06 11:05:35 +00:00
}
1987-06-30 12:55:30 +00:00
if (argtyp == sp_cend) {
1987-06-30 18:59:51 +00:00
ap->ema_argtype = 0;
1987-06-30 12:55:30 +00:00
}
1987-01-06 11:05:35 +00:00
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-06-30 12:55:30 +00:00
getarg(typset, ap)
struct e_arg *ap;
1987-01-06 11:05:35 +00:00
{
register int c;
if (argnum != 1) {
if ((c = nospace()) != ',') {
if (c != '\n') {
syntax("comma expected");
1993-11-10 12:09:49 +00:00
return;
1987-01-06 11:05:35 +00:00
}
ungetbyte(c);
}
}
argnum++;
1993-11-10 12:09:49 +00:00
gettyp(typset, ap);
1987-01-06 11:05:35 +00:00
}
/* getmnem: We found the start of either an instruction or a pseudo.
get the rest of it
*/
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
getmnem(c, p)
register struct e_instr *p;
{
register int h;
int i;
register struct string *s;
ungetbyte(c);
s = getname();
h = hash(s->str);
for (;;) {
h++;
if (h >= HSIZE) h %= HSIZE;
if ((i = hashtab[h]) == 0) {
syntax("bad mnemonic");
return;
}
else if (i <= sp_lmnem) {
assert(i >= sp_fmnem);
if (strcmp(s->str, em_mnem[i - sp_fmnem]) != 0) {
continue;
}
p->em_type = EM_MNEM;
p->em_opcode = i;
break;
}
assert(i <= sp_lpseu && i >= sp_fpseu);
if (strcmp(s->str, em_pseu[i - sp_fpseu]) != 0) {
continue;
}
if (i == ps_mes) {
p->em_type = EM_STARTMES;
break;
}
p->em_opcode = i;
p->em_type = EM_PSEU;
break;
}
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
line_line()
{
1990-08-01 14:30:45 +00:00
static char filebuf[256 + 1];
1987-01-06 11:05:35 +00:00
char *btscpy();
1987-06-30 12:55:30 +00:00
struct e_arg dummy;
1987-01-06 11:05:35 +00:00
1987-06-30 12:55:30 +00:00
gettyp(ptyp(sp_cst2), &dummy);
EM_lineno = dummy.ema_cst;
gettyp(str_ptyp, &dummy);
1987-06-30 18:59:51 +00:00
btscpy(filebuf, dummy.ema_string, (int) dummy.ema_szoroff);
1987-01-06 11:05:35 +00:00
EM_filename = filebuf;
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-01-06 11:05:35 +00:00
getlabel(c, p)
register struct e_instr *p;
{
ungetbyte(c);
1987-06-30 12:55:30 +00:00
gettyp(lab_ptyp|ptyp(sp_cst2), &(p->em_arg));
switch(p->em_argtype) {
1987-01-06 11:05:35 +00:00
case cst_ptyp:
p->em_type = EM_DEFILB;
p->em_argtype = ilb_ptyp;
1987-06-30 12:55:30 +00:00
p->em_ilb = p->em_cst;
1987-01-06 11:05:35 +00:00
break;
case sof_ptyp:
p->em_type = EM_DEFDNAM;
break;
case nof_ptyp:
p->em_type = EM_DEFDLB;
break;
}
checkeol();
}
1993-11-10 12:09:49 +00:00
PRIVATE void
1987-06-30 12:55:30 +00:00
gethead(p)
1987-08-06 14:21:01 +00:00
register struct e_instr *p;
1987-01-06 11:05:35 +00:00
{
1987-05-18 13:34:06 +00:00
register int c;
1987-01-06 11:05:35 +00:00
argnum = 1;
for (;;) {
EM_lineno++;
1987-02-06 10:27:57 +00:00
c = getbyte();
if (c == COMMENTSTARTER) {
do c = getbyte();
while (c != '\n' && c != EOF);
}
1987-06-30 12:55:30 +00:00
if (c == EOF) {
p->em_type = EM_EOF;
return;
}
1987-01-06 11:05:35 +00:00
if (c == '\n') continue;
if (isspace(c)) {
c = nospace();
if (isalpha(c) || c == '_') {
getmnem(c, p);
1987-06-30 12:55:30 +00:00
return;
1987-01-06 11:05:35 +00:00
}
ungetbyte(c);
}
else if (c == '#') line_line();
else {
getlabel(c, p);
1987-06-30 12:55:30 +00:00
return;
1987-01-06 11:05:35 +00:00
}
checkeol();
1987-08-06 14:21:01 +00:00
if (p->em_type == EM_ERROR || p->em_type == EM_FATAL) return;
1987-01-06 11:05:35 +00:00
}
/*NOTREACHED*/
}