1994-06-24 14:02:31 +00:00
|
|
|
/* $Id$ */
|
1987-03-09 19:15:41 +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".
|
|
|
|
*/
|
1985-04-12 16:56:43 +00:00
|
|
|
/* @(#)comm7.c 1.10 */
|
|
|
|
/*
|
|
|
|
* miscellaneous
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "comm0.h"
|
|
|
|
#include "comm1.h"
|
|
|
|
#include "y.tab.h"
|
2017-11-11 04:30:46 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <object.h>
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
valu_t
|
2017-11-11 04:30:46 +00:00
|
|
|
load(const item_t *ip)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
#ifdef ASLD
|
2017-11-11 04:30:46 +00:00
|
|
|
int typ;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
typ = ip->i_type & S_TYP;
|
|
|
|
if ((typ -= S_MIN) < 0) /* S_UND or S_ABS */
|
|
|
|
return(ip->i_valu);
|
|
|
|
return(ip->i_valu + sect[typ].s_base);
|
|
|
|
#else
|
|
|
|
if ((ip->i_type & S_TYP) == S_UND || (ip->i_type & S_COM)) {
|
|
|
|
if (pass == PASS_3) {
|
|
|
|
if (relonami != 0)
|
2015-03-22 23:07:59 +00:00
|
|
|
serror("relocation error (relonami=%d, type=%08x)", relonami, ip->i_type);
|
1985-04-12 16:56:43 +00:00
|
|
|
relonami = ip->i_valu+1;
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
return(ip->i_valu);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
int
|
|
|
|
store(item_t *ip, valu_t val)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
#ifdef ASLD
|
2017-11-11 04:30:46 +00:00
|
|
|
int typ;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
typ = ip->i_type & S_TYP;
|
|
|
|
if ((typ -= S_MIN) >= 0)
|
|
|
|
val -= sect[typ].s_base;
|
|
|
|
#else
|
|
|
|
if ((ip->i_type & S_TYP) == S_UND)
|
|
|
|
return(0);
|
|
|
|
#endif
|
|
|
|
assert(pass != PASS_3 || (ip->i_type & S_VAR) || ip->i_valu == val);
|
|
|
|
ip->i_valu = val;
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2017-11-11 04:30:46 +00:00
|
|
|
remember(char *s)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2017-11-11 04:30:46 +00:00
|
|
|
char *p;
|
|
|
|
int n;
|
|
|
|
static int nleft = 0;
|
1985-04-12 16:56:43 +00:00
|
|
|
static char *next;
|
|
|
|
|
|
|
|
p = s;
|
|
|
|
n = 0;
|
|
|
|
do
|
|
|
|
n++;
|
|
|
|
while (*p++);
|
|
|
|
if ((nleft -= n) < 0) {
|
1987-08-06 13:04:20 +00:00
|
|
|
next = malloc(MEMINCR);
|
|
|
|
if (next == 0)
|
1985-04-12 16:56:43 +00:00
|
|
|
fatal("out of memory");
|
|
|
|
nleft = (MEMINCR / sizeof(char)) - n;
|
|
|
|
assert(nleft >= 0);
|
|
|
|
}
|
|
|
|
p = next;
|
|
|
|
while (*p++ = *s++)
|
|
|
|
;
|
|
|
|
s = next;
|
|
|
|
next = p;
|
|
|
|
return(s);
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
int
|
|
|
|
combine(int typ1, int typ2, int op)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case '+':
|
|
|
|
if (typ1 == S_ABS)
|
|
|
|
return(typ2);
|
|
|
|
if (typ2 == S_ABS)
|
|
|
|
return(typ1);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
if (typ2 == S_ABS)
|
|
|
|
return(typ1);
|
|
|
|
if ((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND)
|
|
|
|
return(S_ABS|S_VAR);
|
|
|
|
break;
|
|
|
|
case '>':
|
|
|
|
if (typ1 == S_ABS && typ2 == S_ABS)
|
|
|
|
return(S_ABS);
|
|
|
|
if (
|
|
|
|
((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND)
|
|
|
|
|| (typ1 == S_ABS)
|
|
|
|
|| (typ2 == S_ABS)
|
|
|
|
)
|
|
|
|
return(S_ABS|S_VAR);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (typ1 == S_ABS && typ2 == S_ABS)
|
|
|
|
return(S_ABS);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pass != PASS_1)
|
|
|
|
serror("illegal operator");
|
|
|
|
return(S_UND);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef LISTING
|
2017-11-11 04:30:46 +00:00
|
|
|
int
|
|
|
|
printx(int ndig, valu_t val)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
static char buf[8];
|
2017-11-11 04:30:46 +00:00
|
|
|
char *p;
|
|
|
|
int c, n;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
p = buf; n = ndig;
|
|
|
|
do {
|
|
|
|
*p++ = (int) val & 017;
|
|
|
|
val >>= 4;
|
|
|
|
} while (--n);
|
|
|
|
do {
|
|
|
|
c = "0123456789ABCDEF"[*--p];
|
|
|
|
putchar(c);
|
|
|
|
} while (p > buf);
|
|
|
|
return(ndig);
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
listline(int textline)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2017-11-11 04:30:46 +00:00
|
|
|
int c;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
if ((listflag & 4) && (c = getc(listfile)) != '\n' && textline) {
|
|
|
|
if (listcolm >= 24)
|
|
|
|
printf(" \\\n\t\t\t");
|
|
|
|
else
|
|
|
|
do {
|
|
|
|
putchar('\t');
|
|
|
|
listcolm += 8;
|
|
|
|
} while (listcolm < 24);
|
|
|
|
do {
|
|
|
|
assert(c != EOF);
|
|
|
|
putchar(c);
|
|
|
|
} while ((c = getc(listfile)) != '\n');
|
|
|
|
}
|
1988-06-16 09:57:59 +00:00
|
|
|
if (listflag & 7) {
|
1985-04-12 16:56:43 +00:00
|
|
|
putchar('\n');
|
1988-06-16 09:57:59 +00:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
1985-04-12 16:56:43 +00:00
|
|
|
listeoln = 1;
|
|
|
|
listcolm = 0;
|
|
|
|
listflag = listtemp;
|
|
|
|
}
|
1991-12-17 15:05:43 +00:00
|
|
|
#endif /* LISTING */
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
/* ---------- code optimization ---------- */
|
|
|
|
|
|
|
|
#ifdef THREE_PASS
|
1988-06-16 13:24:10 +00:00
|
|
|
#define PBITTABSZ 128
|
|
|
|
static char *pbittab[PBITTABSZ];
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
int
|
|
|
|
small(int fitsmall, int gain)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2017-11-11 04:30:46 +00:00
|
|
|
int bit;
|
|
|
|
char *p;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
if (DOTSCT == NULL)
|
|
|
|
nosect();
|
|
|
|
if (bflag)
|
|
|
|
return(0);
|
1988-06-16 13:24:10 +00:00
|
|
|
if (nbits == BITCHUNK) {
|
|
|
|
bitindex++;
|
|
|
|
nbits = 0;
|
|
|
|
if (bitindex == PBITTABSZ) {
|
|
|
|
static int w_given;
|
|
|
|
if (pass == PASS_1 && ! w_given) {
|
|
|
|
w_given = 1;
|
|
|
|
warning("bit table overflow");
|
|
|
|
}
|
|
|
|
return(0);
|
1988-06-13 18:01:47 +00:00
|
|
|
}
|
1988-06-16 13:24:10 +00:00
|
|
|
if (pbittab[bitindex] == 0 && pass == PASS_1) {
|
1989-03-01 10:00:37 +00:00
|
|
|
if ((pbittab[bitindex] = calloc(MEMINCR, 1)) == 0) {
|
1988-06-16 13:24:10 +00:00
|
|
|
static int w2_given;
|
|
|
|
|
|
|
|
if (!w2_given) {
|
|
|
|
w2_given = 1;
|
|
|
|
warning("out of space for bit table");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pbittab[bitindex] == 0)
|
|
|
|
return (0);
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
1988-06-16 13:24:10 +00:00
|
|
|
bit = 1 << (nbits&7);
|
|
|
|
p = pbittab[bitindex]+(nbits>>3);
|
1988-06-13 17:57:39 +00:00
|
|
|
nbits++;
|
1985-04-12 16:56:43 +00:00
|
|
|
switch (pass) {
|
|
|
|
case PASS_1:
|
|
|
|
return(0);
|
|
|
|
case PASS_2:
|
|
|
|
if (fitsmall) {
|
|
|
|
DOTGAIN += gain;
|
|
|
|
*p |= bit;
|
|
|
|
}
|
|
|
|
return(fitsmall);
|
|
|
|
case PASS_3:
|
|
|
|
assert(fitsmall || (*p & bit) == 0);
|
|
|
|
return(*p & bit);
|
|
|
|
}
|
1986-12-01 15:41:29 +00:00
|
|
|
/*NOTREACHED*/
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ---------- output ---------- */
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
emit1(int arg)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
1991-01-11 14:20:59 +00:00
|
|
|
static int olddottyp = -1;
|
1985-04-12 16:56:43 +00:00
|
|
|
#ifdef LISTING
|
|
|
|
if (listeoln) {
|
|
|
|
if (listflag & 1) {
|
1989-01-19 16:41:55 +00:00
|
|
|
listcolm += printx(VALWIDTH, (valu_t)DOTVAL);
|
1985-04-12 16:56:43 +00:00
|
|
|
listcolm++;
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
listeoln = 0;
|
|
|
|
}
|
|
|
|
if (listflag & 2)
|
|
|
|
listcolm += printx(2, (valu_t) arg);
|
|
|
|
#endif
|
|
|
|
switch (pass) {
|
|
|
|
case PASS_1:
|
|
|
|
if (DOTSCT == NULL)
|
|
|
|
nosect();
|
|
|
|
/* no break */
|
|
|
|
case PASS_2:
|
|
|
|
DOTSCT->s_zero = 0;
|
|
|
|
break;
|
|
|
|
case PASS_3:
|
1991-01-11 14:20:59 +00:00
|
|
|
if (DOTTYP != olddottyp) {
|
|
|
|
wr_outsect(DOTTYP-S_MIN);
|
|
|
|
olddottyp = DOTTYP;
|
|
|
|
}
|
1985-04-12 16:56:43 +00:00
|
|
|
while (DOTSCT->s_zero) {
|
1986-12-01 15:41:29 +00:00
|
|
|
wr_putc(0);
|
1985-04-12 16:56:43 +00:00
|
|
|
DOTSCT->s_zero--;
|
|
|
|
}
|
1986-12-01 15:41:29 +00:00
|
|
|
wr_putc(arg);
|
1985-04-12 16:56:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
DOTVAL++;
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
emit2(int arg)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
#ifdef BYTES_REVERSED
|
1989-01-19 13:48:09 +00:00
|
|
|
emit1((arg>>8)); emit1(arg);
|
1985-04-12 16:56:43 +00:00
|
|
|
#else
|
1989-01-19 13:48:09 +00:00
|
|
|
emit1(arg); emit1((arg>>8));
|
1985-04-12 16:56:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
emit4(long arg)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
#ifdef WORDS_REVERSED
|
1989-01-19 16:41:55 +00:00
|
|
|
emit2((int)(arg>>16)); emit2((int)(arg));
|
1985-04-12 16:56:43 +00:00
|
|
|
#else
|
1989-01-19 16:41:55 +00:00
|
|
|
emit2((int)(arg)); emit2((int)(arg>>16));
|
1985-04-12 16:56:43 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
emitx(valu_t val, int n)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
switch (n) {
|
2013-05-29 13:11:04 +00:00
|
|
|
case RELO1:
|
1989-01-19 13:48:09 +00:00
|
|
|
emit1((int)val); break;
|
2013-05-29 13:11:04 +00:00
|
|
|
case RELO2:
|
1991-01-11 14:20:59 +00:00
|
|
|
#ifdef BYTES_REVERSED
|
|
|
|
emit1(((int)val>>8)); emit1((int)val);
|
|
|
|
#else
|
|
|
|
emit1((int)val); emit1(((int)val>>8));
|
|
|
|
#endif
|
|
|
|
break;
|
2013-05-29 13:11:04 +00:00
|
|
|
case RELO4:
|
1991-01-11 14:20:59 +00:00
|
|
|
#ifdef WORDS_REVERSED
|
|
|
|
emit2((int)(val>>16)); emit2((int)(val));
|
|
|
|
#else
|
|
|
|
emit2((int)(val)); emit2((int)(val>>16));
|
|
|
|
#endif
|
|
|
|
break;
|
1985-04-12 16:56:43 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
emitstr(int zero)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2017-11-11 04:30:46 +00:00
|
|
|
int i;
|
|
|
|
char *p;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
p = stringbuf;
|
1990-08-01 14:30:45 +00:00
|
|
|
i = stringlen;
|
1985-04-12 16:56:43 +00:00
|
|
|
while (--i >= 0)
|
|
|
|
emit1(*p++);
|
|
|
|
if (zero)
|
|
|
|
emit1(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------- Error checked file I/O ---------- */
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
ffreopen(char *s, FILE *f)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
if (freopen(s, "r", f) == NULL)
|
|
|
|
fatal("can't reopen %s", s);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *
|
2017-11-11 04:30:46 +00:00
|
|
|
ffcreat(char *s)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if ((f = fopen(s, "w")) == NULL)
|
|
|
|
fatal("can't create %s", s);
|
|
|
|
return(f);
|
|
|
|
}
|
|
|
|
|
1988-06-16 09:57:59 +00:00
|
|
|
#ifndef TMPDIR
|
|
|
|
#define TMPDIR "/tmp"
|
|
|
|
#endif
|
|
|
|
char *tmp_dir = TMPDIR;
|
|
|
|
|
1985-04-12 16:56:43 +00:00
|
|
|
FILE *
|
2017-11-11 04:30:46 +00:00
|
|
|
fftemp(char *path, char *tail)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2017-11-11 04:30:46 +00:00
|
|
|
char *dir;
|
1985-04-12 16:56:43 +00:00
|
|
|
|
|
|
|
if ((dir = getenv("TMPDIR")) == NULL)
|
1988-06-16 09:57:59 +00:00
|
|
|
dir = tmp_dir;
|
1985-04-12 16:56:43 +00:00
|
|
|
sprintf(path, "%s/%s", dir, tail);
|
2017-08-06 11:22:05 +00:00
|
|
|
close(mkstemp(path));
|
|
|
|
return(ffcreat(path));
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ---------- Error handling ---------- */
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
/* ARGSUSED */
|
|
|
|
void
|
|
|
|
yyerror(const char *message)
|
|
|
|
{} /* we will do our own error printing */
|
1985-04-12 16:56:43 +00:00
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
nosect(void)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
fatal("no sections");
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
wr_fatal(void)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
fatal("write error");
|
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
static void diag(const char* tail, const char* s, va_list ap)
|
2015-03-22 23:07:59 +00:00
|
|
|
{
|
|
|
|
fflush(stdout);
|
|
|
|
if (modulename)
|
|
|
|
fprintf(stderr, "\"%s\", line %ld: ", modulename, lineno);
|
|
|
|
else
|
|
|
|
fprintf(stderr, "%s: ", progname);
|
|
|
|
vfprintf(stderr, s, ap);
|
|
|
|
fprintf(stderr, "%s", tail);
|
|
|
|
}
|
|
|
|
|
1985-04-12 16:56:43 +00:00
|
|
|
/* VARARGS1 */
|
2015-03-22 23:07:59 +00:00
|
|
|
void fatal(const char* s, ...)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2015-03-22 23:07:59 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
|
1985-04-12 16:56:43 +00:00
|
|
|
nerrors++;
|
2015-03-22 23:07:59 +00:00
|
|
|
diag(" (fatal)\n", s, ap);
|
1985-04-12 16:56:43 +00:00
|
|
|
stop();
|
2015-03-22 23:07:59 +00:00
|
|
|
|
|
|
|
va_end(ap);
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
/* VARARGS1 */
|
2015-03-22 23:07:59 +00:00
|
|
|
void serror(const char* s, ...)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2015-03-22 23:07:59 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
|
1985-04-12 16:56:43 +00:00
|
|
|
nerrors++;
|
2015-03-22 23:07:59 +00:00
|
|
|
diag("\n", s, ap);
|
|
|
|
stop();
|
1985-04-12 16:56:43 +00:00
|
|
|
|
2015-03-22 23:07:59 +00:00
|
|
|
va_end(ap);
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS1 */
|
2015-03-22 23:07:59 +00:00
|
|
|
void warning(const char* s, ...)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
2015-03-22 23:07:59 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, s);
|
|
|
|
|
|
|
|
nerrors++;
|
|
|
|
diag(" (warning)\n", s, ap);
|
|
|
|
stop();
|
|
|
|
|
|
|
|
va_end(ap);
|
1985-04-12 16:56:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-11 04:30:46 +00:00
|
|
|
void
|
|
|
|
nofit(void)
|
1985-04-12 16:56:43 +00:00
|
|
|
{
|
|
|
|
if (pass == PASS_3)
|
|
|
|
warning("too big");
|
|
|
|
}
|