_include <string.h>
_include <ctype.h>
_include <signal.h>
+_include <unistd.h>
#else
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <stdarg.h>
#include <ctype.h>
#include <signal.h>
+#include <unistd.h>
#endif
#ifdef ASLD
#include "arch.h"
#endif
+#include "object.h"
#include "out.h"
#if DEBUG == 0
#define MACHREL_BWR (0)
#endif
#endif
-
-extern FILE *fopen(); /* some systems don't have this in stdio.h */
extern int curr_token;
/* forward function declarations */
+int yyparse();
+
+/* ========== comm4.c prototypes =========== */
+void stop(int signal);
+int main(int argc, char *argv[]);
+int pass_1(int argc, char *argv[]);
+void parse(char *s);
+void pass_23(int n);
+void newmodule(char *s);
+void setupoutput(void);
+void commfinish(void);
+
+/* ========== comm5.c prototypes =========== */
+int yylex(void);
+void putval(int c);
+int getval(int c);
+int nextchar(void);
+void readcode(int n);
+int induo(int c);
+int inident(int c);
+int innumber(int c);
+int instring(int termc);
+int inescape(void);
+int infbsym(char *p);
+int hash(char *p);
+item_t *item_search(char *p);
+void item_insert(item_t *ip, int h);
+item_t *item_alloc(int typ);
+item_t *fb_alloc(int lab);
+item_t *fb_shift(int lab);
#ifdef ASLD
-extern char *readident();
+char *readident(int c);
#endif
-extern char *remember();
-extern item_t *fb_shift();
-extern item_t *fb_alloc();
-extern item_t *item_alloc();
-extern item_t *item_search();
-extern valu_t load();
-extern FILE *ffcreat();
-extern FILE *fftemp();
+
+/* ========== comm6.c prototypes =========== */
+void newequate(item_t *ip, int typ);
+void newident(item_t *ip, int typ);
+void newlabel(item_t *ip);
+void newsect(item_t *ip);
+void newbase(valu_t base);
+void newcomm(item_t *ip, valu_t val);
+void switchsect(int newtyp);
+void align(valu_t bytes);
+long new_string(char *s);
+void newsymb(char *name, int type, int desc, valu_t valu);
+void new_common(item_t *ip);
+#ifdef RELOCATION
+void newrelo(int s, int n);
+#endif
+
+/* ========== comm7.c prototypes =========== */
+valu_t load(item_t *ip);
+int store(item_t *ip, valu_t val);
+char *remember(char *s);
+int combine(int typ1, int typ2, int op);
+int small(int fitsmall, int gain);
+void emit1(int arg);
+void emit2(int arg);
+void emit4(long arg);
+void emitx(valu_t val, int n);
+void emitstr(int zero);
+void ffreopen(char *s, FILE *f);
+FILE *ffcreat(char *s);
+FILE *fftemp(char *path, char *tail);
+void yyerror(char *str);
+int printx(int ndig, valu_t val);
+void nosect(void);
+void wr_fatal(void);
+void fatal(char *s, ...);
+void assert1(void);
+void serror(char *s, ...);
+void warning(char *s, ...);
+void diag(char *tail, char *s, ...);
+void nofit(void);
+void listline(int textline);
/* ========== Machine dependent C declarations ========== */
#include "y.tab.h"
item_t keytab[] = {
- 0, EXTERN, 0, ".define",
- 0, EXTERN, 0, ".extern",
- 0, DOT, 0, ".",
- 0, DATA, 1, ".data1",
- 0, DATA, 2, ".data2",
- 0, DATA, 4, ".data4",
- 0, ASCII, 0, ".ascii",
- 0, ASCII, 1, ".asciz",
- 0, ALIGN, 0, ".align",
- 0, ASSERT, 0, ".assert",
- 0, SPACE, 0, ".space",
- 0, COMMON, 0, ".comm",
- 0, SECTION, 0, ".sect",
- 0, BASE, 0, ".base",
- 0, SYMB, 0, ".symb",
- 0, SYMD, 0, ".symd",
- 0, LINE, 0, ".line",
- 0, FILe, 0, ".file",
+ { 0, EXTERN, 0, ".define" },
+ { 0, EXTERN, 0, ".extern" },
+ { 0, DOT, 0, "." },
+ { 0, DATA, 1, ".data1" },
+ { 0, DATA, 2, ".data2" },
+ { 0, DATA, 4, ".data4" },
+ { 0, ASCII, 0, ".ascii" },
+ { 0, ASCII, 1, ".asciz" },
+ { 0, ALIGN, 0, ".align" },
+ { 0, ASSERT, 0, ".assert" },
+ { 0, SPACE, 0, ".space" },
+ { 0, COMMON, 0, ".comm" },
+ { 0, SECTION, 0, ".sect" },
+ { 0, BASE, 0, ".base" },
+ { 0, SYMB, 0, ".symb" },
+ { 0, SYMD, 0, ".symd" },
+ { 0, LINE, 0, ".line" },
+ { 0, FILe, 0, ".file" },
#ifdef LISTING
- 0, LIST, 0, ".nolist",
- 0, LIST, 1, ".list",
+ { 0, LIST, 0, ".nolist" },
+ { 0, LIST, 1, ".list" },
#endif
#include "mach3.c"
- 0, 0, 0, 0
+ { 0, 0, 0, 0 }
};
* Johan Stevenson, Han Schaminee and Hans de Vries
* Philips S&I, T&M, PMDS, Eindhoven
*/
-
#include "comm0.h"
#include "comm1.h"
#include "y.tab.h"
/* ========== Machine independent C routines ========== */
-void stop() {
+void stop(int signal)
+{
#if DEBUG < 2
unlink(temppath);
#ifdef LISTING
exit(nerrors != 0);
}
-main(argc, argv)
-char **argv;
+int main(int argc, char *argv[])
{
- register char *p;
- register i;
+ char *p;
+ int i;
static char sigs[] = {
SIGHUP, SIGINT, SIGQUIT, SIGTERM, 0
};
}
progname = *argv++; argc--;
- for (p = sigs; i = *p++; )
+ for (p = sigs; (i = *p++); )
if (signal(i, SIG_IGN) != SIG_IGN)
signal(i, stop);
for (i = 0; i < argc; i++) {
#endif
pass_23(PASS_3);
wr_close();
- stop();
+ stop(0);
+ return 0;
}
/* ---------- pass 1: arguments, modules, archives ---------- */
-pass_1(argc, argv)
-char **argv;
+int pass_1(int argc, char *argv[])
{
- register char *p;
- register item_t *ip;
+ char *p;
+ item_t *ip;
#ifdef ASLD
char armagic[2];
#else
- register nfile = 0;
+ int nfile = 0;
#endif
#ifdef THREE_PASS
machfinish(PASS_1);
#ifdef ASLD
if (unresolved) {
- register int i;
+ int i;
nerrors++;
fflush(stdout);
fatal("no source file");
*/
#endif
+ return 0;
}
#ifdef ASLD
-archive() {
- register long offset;
+void archive()
+{
+ long offset;
struct ar_hdr header;
char getsize[AR_TOTAL];
archmode = 0;
}
-needed()
+int needed()
{
- register c, first;
- register item_t *ip;
- register need;
+ c, first;
+ item_t *ip;
+ int need;
#ifdef LISTING
- register save;
+ int save;
save = listflag; listflag = 0;
#endif
}
#endif /* ASLD */
-parse(s)
-char *s;
+void parse(char *s)
{
- register i;
- register item_t *ip;
- register char *p;
+ int i;
+ item_t *ip;
+ char *p;
for (p = s; *p; )
if (*p++ == '/')
}
}
-pass_23(n)
+void pass_23(int n)
{
- register i;
+ int i;
#ifdef ASLD
- register ADDR_T base = 0;
+ ADDR_T base = 0;
#endif
- register sect_t *sp;
+ sect_t *sp;
if (nerrors)
- stop();
+ stop(0);
pass = n;
#ifdef LISTING
listmode >>= 3;
machfinish(n);
}
-newmodule(s)
-char *s;
+void newmodule(char *s)
{
static char nmbuf[STRINGMAX];
#endif
}
-setupoutput()
+void setupoutput()
{
- register sect_t *sp;
- register long off;
+ sect_t *sp;
+ long off;
struct outsect outsect;
- register struct outsect *pos = &outsect;
+ struct outsect *pos = &outsect;
if (! wr_open(aoutpath)) {
fatal("can't create %s", aoutpath);
outhead.oh_nchar = off; /* see newsymb() */
}
-commfinish()
+void commfinish()
{
#ifndef ASLD
- register int i;
+ int i;
#endif
- register struct common_t *cp;
- register item_t *ip;
- register sect_t *sp;
- register valu_t addr;
+ struct common_t *cp;
+ item_t *ip;
+ sect_t *sp;
+ valu_t addr;
switchsect(S_UND);
/*
extern YYSTYPE yylval;
-yylex()
+int yylex()
{
- register c;
+ int c;
if (pass == PASS_1) {
/* scan the input file */
return(c);
}
-putval(c)
+void putval(int c)
{
- register valu_t v;
- register n = 0;
- register char *p = 0;
+ valu_t v;
+ int n = 0;
+ char *p = 0;
assert(c >= 256 && c < 256+128);
switch (c) {
putc(*p++, tempfile);
}
-getval(c)
+int getval(int c)
{
- register n = 0;
- register valu_t v;
- register char *p = 0;
+ int n = 0;
+ valu_t v;
+ char *p = 0;
switch (c) {
case CODE1:
/* ---------- lexical scan in pass 1 ---------- */
-nextchar()
+int nextchar()
{
- register c;
+ int c;
if (peekc != -1) {
c = peekc;
return(c);
}
-readcode(n)
+void readcode(int n)
{
- register c;
+ int c;
yylval.y_valu = 0;
do {
} while (--n);
}
-induo(c)
-register c;
+int induo(int c)
{
static short duo[] = {
('='<<8) | '=', OP_EQ,
('|'<<8) | '|', OP_OO,
('&'<<8) | '&', OP_AA,
};
- register short *p;
+ short *p;
c = (c<<8) | nextchar();
for (p = duo; *p; p++)
static char name[NAMEMAX+1];
-inident(c)
-register c;
+int inident(int c)
{
- register char *p = name;
- register item_t *ip;
- register n = NAMEMAX;
+ char *p = name;
+ item_t *ip;
+ int n = NAMEMAX;
do {
if (--n >= 0)
}
#ifdef ASLD
-char *
-readident(c)
-register c;
+char *readident(int c)
{
- register n = NAMEMAX;
- register char *p = name;
+ int n = NAMEMAX;
+ char *p = name;
do {
if (--n >= 0)
}
#endif
-innumber(c)
-register c;
+int innumber(int c)
{
- register char *p;
- register radix;
+ char *p;
+ int radix;
static char num[20+1];
p = num;
if (radix != 16 && (c == 'f' || c == 'b'))
return(infbsym(num));
yylval.y_valu = 0;
- while (c = *p++) {
+ while ( (c = *p++) ) {
if (c > '9')
c -= ('a' - '9' - 1);
c -= '0';
return(NUMBER);
}
-instring(termc)
+int instring(int termc)
{
- register char *p;
- register c;
+ char *p;
+ int c;
static int maxstring = 0;
if (! maxstring) {
return(STRING);
}
-inescape()
+int inescape()
{
- register c, j, r;
+ int c, j, r;
c = nextchar();
if (c >= '0' && c <= '7') {
return(c);
}
-infbsym(p)
-register char *p;
+int infbsym(char *p)
{
- register lab;
- register item_t *ip;
+ int lab;
+ item_t *ip;
lab = *p++ - '0';
if ((unsigned)lab < 10) {
return(FBSYM);
}
-hash(p)
-register char *p;
+int hash(char *p)
{
- register unsigned short h;
- register c;
+ unsigned short h;
+ int c;
h = 0;
- while (c = *p++) {
+ while ( (c = *p++) ) {
h <<= 2;
h += c;
}
return(h % H_SIZE);
}
-item_t *
-item_search(p)
-char *p;
+item_t *item_search(char *p)
{
- register h;
- register item_t *ip;
+ int h;
+ item_t *ip;
for (h = hash(p); h < H_TOTAL; h += H_SIZE) {
ip = hashtab[h];
return(ip);
}
-item_insert(ip, h)
-item_t *ip;
+void item_insert(item_t *ip, int h)
{
ip->i_next = hashtab[h];
hashtab[h] = ip;
}
-item_t *
-item_alloc(typ)
+item_t *item_alloc(int typ)
{
- register item_t *ip;
+ item_t *ip;
static nleft = 0;
static item_t *next;
return(ip);
}
-item_t *
-fb_alloc(lab)
-register lab;
+item_t *fb_alloc(int lab)
{
- register item_t *ip, *p;
+ item_t *ip, *p;
ip = item_alloc(S_UND);
p = fb_ptr[FB_TAIL+lab];
return(ip);
}
-item_t *
-fb_shift(lab)
-register lab;
+item_t *fb_shift(int lab)
{
- register item_t *ip;
+ item_t *ip;
ip = fb_ptr[FB_FORW+lab];
if (ip == 0)
+ {
if (pass == PASS_1)
+ {
ip = fb_alloc(lab);
+ }
else
+ {
ip = fb_ptr[FB_HEAD+lab];
+ }
+ }
fb_ptr[FB_BACK+lab] = ip;
fb_ptr[FB_FORW+lab] = ip->i_next;
return(ip);
#include "comm1.h"
#include "y.tab.h"
-newequate(ip, typ)
-register item_t *ip;
-register int typ;
+void newequate(item_t *ip, int typ)
{
typ &= ~S_EXT;
if (typ & S_COM)
newident(ip, typ);
}
-newident(ip, typ)
-register item_t *ip;
+void newident(item_t *ip, int typ)
{
- register flag;
+ int flag;
#ifdef GENLAB
static char genlab[] = GENLAB;
#endif /* GENLAB */
);
}
-newlabel(ip)
-register item_t *ip;
+void newlabel(item_t *ip)
{
#if DEBUG != 0
#ifdef THREE_PASS
- register ADDR_T oldval = ip->i_valu;
+ ADDR_T oldval = ip->i_valu;
#endif
#endif
#endif
}
-newsect(ip)
-register item_t *ip;
+void newsect(item_t *ip)
{
- register int typ;
- register sect_t *sp = NULL;
+ int typ;
+ sect_t *sp = NULL;
typ = ip->i_type & S_TYP;
if (typ == S_UND) {
}
/*ARGSUSED*/
-newbase(base)
-valu_t base;
+void newbase(valu_t base)
{
#ifdef ASLD
- register sect_t *sp;
+ sect_t *sp;
if ((sp = DOTSCT) == NULL)
nosect();
* - maximum length of .comm is recorded in i_valu during PASS_1
* - i_valu is used for relocation info during PASS_3
*/
-newcomm(ip, val)
-register item_t *ip;
-valu_t val;
+void newcomm(item_t *ip, valu_t val)
{
if (pass == PASS_1) {
if (DOTSCT == NULL)
}
}
-switchsect(newtyp)
-int newtyp;
+void switchsect(int newtyp)
{
- register sect_t *sp;
+ sect_t *sp;
- if (sp = DOTSCT)
+ if ( (sp = DOTSCT) )
sp->s_size = DOTVAL - sp->s_base;
if (newtyp == S_UND) {
DOTSCT = NULL;
DOTTYP = newtyp;
}
-align(bytes)
-valu_t bytes;
+void align(valu_t bytes)
{
- register valu_t gap;
- register sect_t *sp;
+ valu_t gap;
+ sect_t *sp;
if ((sp = DOTSCT) == NULL)
nosect();
if (bytes == 0)
bytes = ALIGNWORD;
if (sp->s_lign % bytes)
+ {
if (bytes % sp->s_lign)
+ {
serror("illegal alignment");
+ }
else
+ {
sp->s_lign = bytes;
+ }
+ }
if (pass == PASS_1)
+ {
/*
* be pessimistic: biggest gap possible
*/
gap = bytes - 1;
- else {
+ }
+ else
+ {
/*
* calculate gap correctly;
* will be the same in PASS_2 and PASS_3
}
#ifdef RELOCATION
-newrelo(s, n)
+void newrelo(int s, int n)
{
int iscomm;
struct outrelo outrelo;
s &= ~S_COM;
if ((n & RELPC) == 0 && ((s & ~S_VAR) == S_ABS))
return;
+
if ((n & RELPC) != 0 && s == DOTTYP
#ifndef ASLD
&& ! iscomm
#endif
)
return;
+
if (pass != PASS_3) {
outhead.oh_nrelo++;
return;
}
+
s &= ~S_VAR;
outrelo.or_type = (char)n;
outrelo.or_sect = (char)DOTTYP;
relonami = 0;
} else
#endif
- if (s < S_MIN) {
+ if (s < S_MIN)
+ {
assert(s == S_ABS);
/*
* use first non existing entry (argh)
*/
outrelo.or_nami = outhead.oh_nname;
- } else {
+ }
+ else
+ {
/*
* section symbols are at the end
*/
}
#endif
-long
-new_string(s)
- char *s;
+long new_string(char *s)
{
long r = 0;
return r;
}
-newsymb(name, type, desc, valu)
-register char *name;
-valu_t valu;
+void newsymb(char *name, int type, int desc, valu_t valu)
{
struct outname outname;
wr_name(&outname, 1);
}
-new_common(ip)
- item_t *ip;
+void new_common(item_t *ip)
{
- register struct common_t *cp;
+ struct common_t *cp;
static nleft = 0;
static struct common_t *next;
#include "comm1.h"
#include "y.tab.h"
-valu_t
-load(ip)
-register item_t *ip;
+valu_t load(item_t *ip)
{
#ifdef ASLD
- register typ;
+ int typ;
typ = ip->i_type & S_TYP;
if ((typ -= S_MIN) < 0) /* S_UND or S_ABS */
#endif
}
-store(ip, val)
-register item_t *ip;
-valu_t val;
+int store(item_t *ip, valu_t val)
{
#ifdef ASLD
- register typ;
+ int typ;
typ = ip->i_type & S_TYP;
if ((typ -= S_MIN) >= 0)
return(1);
}
-char *
-remember(s)
-register char *s;
+char *remember(char *s)
{
- register char *p;
- register n;
+ char *p;
+ int n;
static nleft = 0;
static char *next;
assert(nleft >= 0);
}
p = next;
- while (*p++ = *s++)
+ while ( (*p++ = *s++) )
;
s = next;
next = p;
return(s);
}
-combine(typ1, typ2, op)
-register typ1, typ2;
+int combine(int typ1, int typ2, int op)
{
switch (op) {
case '+':
}
#ifdef LISTING
-printx(ndig, val)
-valu_t val;
+int printx(int ndig, valu_t val)
{
static char buf[8];
- register char *p;
- register c, n;
+ char *p;
+ int c, n;
p = buf; n = ndig;
do {
val >>= 4;
} while (--n);
do {
- c = "0123456789ABCDEF"[*--p];
+ c = "0123456789ABCDEF"[*(unsigned char *)--p];
putchar(c);
} while (p > buf);
return(ndig);
#endif
#ifdef LISTING
-listline(textline)
+void listline(int textline)
{
- register c;
+ int c;
if ((listflag & 4) && (c = getc(listfile)) != '\n' && textline) {
if (listcolm >= 24)
#define PBITTABSZ 128
static char *pbittab[PBITTABSZ];
-small(fitsmall, gain)
+int small(int fitsmall, int gain)
{
- register bit;
- register char *p;
+ int bit;
+ char *p;
if (DOTSCT == NULL)
nosect();
return(*p & bit);
}
/*NOTREACHED*/
+ return 0;
}
#endif
/* ---------- output ---------- */
-emit1(arg)
+void emit1(int arg)
{
static int olddottyp = -1;
#ifdef LISTING
DOTVAL++;
}
-emit2(arg)
-int arg;
+void emit2(int arg)
{
#ifdef BYTES_REVERSED
emit1((arg>>8)); emit1(arg);
#endif
}
-emit4(arg)
-long arg;
+void emit4(long arg)
{
#ifdef WORDS_REVERSED
emit2((int)(arg>>16)); emit2((int)(arg));
#endif
}
-emitx(val, n)
-valu_t val;
-int n;
+void emitx(valu_t val, int n)
{
switch (n) {
case 1:
}
}
-emitstr(zero)
+void emitstr(int zero)
{
- register i;
- register char *p;
+ int i;
+ char *p;
p = stringbuf;
i = stringlen;
/* ---------- Error checked file I/O ---------- */
-ffreopen(s, f)
-char *s;
-FILE *f;
+void ffreopen(char *s, FILE *f)
{
if (freopen(s, "r", f) == NULL)
fatal("can't reopen %s", s);
}
-FILE *
-ffcreat(s)
-char *s;
+FILE *ffcreat(char *s)
{
FILE *f;
#endif
char *tmp_dir = TMPDIR;
-FILE *
-fftemp(path, tail)
-char *path, *tail;
+FILE *fftemp(char *path, char *tail)
{
- register char *dir;
+ char *dir;
if ((dir = getenv("TMPDIR")) == NULL)
dir = tmp_dir;
/* ---------- Error handling ---------- */
/*VARARGS*/
-yyerror(){} /* we will do our own error printing */
+void yyerror(char *str){} /* we will do our own error printing */
-nosect()
+/* VARARGS1 */
+static void vdiag(char *tail, char *s, va_list ap)
+{
+ fflush(stdout);
+ if (modulename)
+ fprintf(stderr, "\"%s\", line %ld: ", modulename, lineno);
+ else
+ fprintf(stderr, "%s: ", progname);
+ vfprintf(stderr, s, ap);
+ fprintf(stderr, "%s", tail);
+}
+
+void nosect()
{
fatal("no sections");
}
-wr_fatal()
+void wr_fatal()
{
fatal("write error");
}
/* VARARGS1 */
-fatal(s, a1, a2, a3, a4)
-char *s;
+void fatal(char *s, ...)
{
+ va_list ap;
nerrors++;
- diag(" (fatal)\n", s, a1, a2, a3, a4);
- stop();
+ va_start(ap, s);
+ vdiag(" (fatal)\n", s, ap);
+ va_end(ap);
+ stop(0);
}
#if DEBUG == 2
-assert2(file, line)
-char *file;
+void assert2(char *file, int line)
{
fatal("assertion failed (%s, %d)", file, line);
}
#endif
#if DEBUG == 1
-assert1()
+void assert1()
{
- diag(" (fatal)\n", "assertion failed");
+ vdiag(" (fatal)\n", "assertion failed");
abort();
}
#endif
/* VARARGS1 */
-serror(s, a1, a2, a3, a4)
-char *s;
+void serror(char *s, ...)
{
+ va_list ap;
nerrors++;
- diag("\n", s, a1, a2, a3, a4);
+ va_start(ap, s);
+ vdiag("\n", s, ap);
+ va_end(ap);
}
/* VARARGS1 */
-warning(s, a1, a2, a3, a4)
-char *s;
+void warning(char *s, ...)
{
- diag(" (warning)\n", s, a1, a2, a3, a4);
+ va_list ap;
+ va_start(ap, s);
+ vdiag(" (warning)\n", s, ap);
+ va_end(ap);
}
/* VARARGS1 */
-diag(tail, s, a1, a2, a3, a4)
-char *tail, *s;
+void diag(char *tail, char *s, ...)
{
- fflush(stdout);
- if (modulename)
- fprintf(stderr, "\"%s\", line %ld: ", modulename, lineno);
- else
- fprintf(stderr, "%s: ", progname);
- fprintf(stderr, s, a1, a2, a3, a4);
- fprintf(stderr, tail);
+ va_list ap;
+ va_start(ap, s);
+ vdiag(tail, s, ap);
+ va_end(ap);
}
-nofit()
+void nofit()
{
if (pass == PASS_3)
warning("too big");