Clang-format before editing.

This commit is contained in:
David Given 2018-09-10 22:37:04 +02:00
parent 6275896a11
commit 976fa0efca

View file

@ -8,37 +8,37 @@
* miscellaneous * miscellaneous
*/ */
#include "comm0.h" #include "comm0.h"
#include "comm1.h" #include "comm1.h"
#include "y.tab.h" #include "y.tab.h"
#include <stdarg.h> #include <stdarg.h>
#include <object.h> #include <object.h>
valu_t valu_t load(const item_t* ip)
load(const item_t *ip)
{ {
#ifdef ASLD #ifdef ASLD
int typ; int typ;
typ = ip->i_type & S_TYP; typ = ip->i_type & S_TYP;
if ((typ -= S_MIN) < 0) /* S_UND or S_ABS */ if ((typ -= S_MIN) < 0) /* S_UND or S_ABS */
return(ip->i_valu); return (ip->i_valu);
return(ip->i_valu + sect[typ].s_base); return (ip->i_valu + sect[typ].s_base);
#else #else
if ((ip->i_type & S_TYP) == S_UND || (ip->i_type & S_COM)) { if ((ip->i_type & S_TYP) == S_UND || (ip->i_type & S_COM))
if (pass == PASS_3) { {
if (pass == PASS_3)
{
if (relonami != 0) if (relonami != 0)
serror("relocation error (relonami=%d, type=%08x)", relonami, ip->i_type); serror("relocation error (relonami=%d, type=%08x)", relonami, ip->i_type);
relonami = ip->i_valu+1; relonami = ip->i_valu + 1;
} }
return(0); return (0);
} }
return(ip->i_valu); return (ip->i_valu);
#endif #endif
} }
int int store(item_t* ip, valu_t val)
store(item_t *ip, valu_t val)
{ {
#ifdef ASLD #ifdef ASLD
int typ; int typ;
@ -48,27 +48,27 @@ store(item_t *ip, valu_t val)
val -= sect[typ].s_base; val -= sect[typ].s_base;
#else #else
if ((ip->i_type & S_TYP) == S_UND) if ((ip->i_type & S_TYP) == S_UND)
return(0); return (0);
#endif #endif
assert(pass != PASS_3 || (ip->i_type & S_VAR) || ip->i_valu == val); assert(pass != PASS_3 || (ip->i_type & S_VAR) || ip->i_valu == val);
ip->i_valu = val; ip->i_valu = val;
return(1); return (1);
} }
char * char* remember(char* s)
remember(char *s)
{ {
char *p; char* p;
int n; int n;
static int nleft = 0; static int nleft = 0;
static char *next; static char* next;
p = s; p = s;
n = 0; n = 0;
do do
n++; n++;
while (*p++); while (*p++);
if ((nleft -= n) < 0) { if ((nleft -= n) < 0)
{
next = malloc(MEMINCR); next = malloc(MEMINCR);
if (next == 0) if (next == 0)
fatal("out of memory"); fatal("out of memory");
@ -80,84 +80,86 @@ remember(char *s)
; ;
s = next; s = next;
next = p; next = p;
return(s); return (s);
} }
int int combine(int typ1, int typ2, int op)
combine(int typ1, int typ2, int op)
{ {
switch (op) { switch (op)
case '+': {
if (typ1 == S_ABS) case '+':
return(typ2); if (typ1 == S_ABS)
if (typ2 == S_ABS) return (typ2);
return(typ1); if (typ2 == S_ABS)
break; return (typ1);
case '-': break;
if (typ2 == S_ABS) case '-':
return(typ1); if (typ2 == S_ABS)
if ((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND) return (typ1);
return(S_ABS|S_VAR); if ((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND)
break; return (S_ABS | S_VAR);
case '>': break;
if (typ1 == S_ABS && typ2 == S_ABS) case '>':
return(S_ABS); if (typ1 == S_ABS && typ2 == S_ABS)
if ( return (S_ABS);
((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND) if (((typ1 & ~S_DOT) == (typ2 & ~S_DOT) && typ1 != S_UND) || (typ1 == S_ABS)
|| (typ1 == S_ABS) || (typ2 == S_ABS))
|| (typ2 == S_ABS) return (S_ABS | S_VAR);
) break;
return(S_ABS|S_VAR); default:
break; if (typ1 == S_ABS && typ2 == S_ABS)
default: return (S_ABS);
if (typ1 == S_ABS && typ2 == S_ABS) break;
return(S_ABS);
break;
} }
if (pass != PASS_1) if (pass != PASS_1)
serror("illegal operator"); serror("illegal operator");
return(S_UND); return (S_UND);
} }
#ifdef LISTING #ifdef LISTING
int int printx(int ndig, valu_t val)
printx(int ndig, valu_t val)
{ {
static char buf[8]; static char buf[8];
char *p; char* p;
int c, n; int c, n;
p = buf; n = ndig; p = buf;
do { n = ndig;
*p++ = (int) val & 017; do
{
*p++ = (int)val & 017;
val >>= 4; val >>= 4;
} while (--n); } while (--n);
do { do
{
c = "0123456789ABCDEF"[*--p]; c = "0123456789ABCDEF"[*--p];
putchar(c); putchar(c);
} while (p > buf); } while (p > buf);
return(ndig); return (ndig);
} }
void void listline(int textline)
listline(int textline)
{ {
int c; int c;
if ((listflag & 4) && (c = getc(listfile)) != '\n' && textline) { if ((listflag & 4) && (c = getc(listfile)) != '\n' && textline)
{
if (listcolm >= 24) if (listcolm >= 24)
printf(" \\\n\t\t\t"); printf(" \\\n\t\t\t");
else else
do { do
{
putchar('\t'); putchar('\t');
listcolm += 8; listcolm += 8;
} while (listcolm < 24); } while (listcolm < 24);
do { do
{
assert(c != EOF); assert(c != EOF);
putchar(c); putchar(c);
} while ((c = getc(listfile)) != '\n'); } while ((c = getc(listfile)) != '\n');
} }
if (listflag & 7) { if (listflag & 7)
{
putchar('\n'); putchar('\n');
fflush(stdout); fflush(stdout);
} }
@ -170,35 +172,40 @@ listline(int textline)
/* ---------- code optimization ---------- */ /* ---------- code optimization ---------- */
#ifdef THREE_PASS #ifdef THREE_PASS
#define PBITTABSZ 128 #define PBITTABSZ 128
static char *pbittab[PBITTABSZ]; static char* pbittab[PBITTABSZ];
int int small(int fitsmall, int gain)
small(int fitsmall, int gain)
{ {
int bit; int bit;
char *p; char* p;
if (DOTSCT == NULL) if (DOTSCT == NULL)
nosect(); nosect();
if (bflag) if (bflag)
return(0); return (0);
if (nbits == BITCHUNK) { if (nbits == BITCHUNK)
{
bitindex++; bitindex++;
nbits = 0; nbits = 0;
if (bitindex == PBITTABSZ) { if (bitindex == PBITTABSZ)
{
static int w_given; static int w_given;
if (pass == PASS_1 && ! w_given) { if (pass == PASS_1 && !w_given)
{
w_given = 1; w_given = 1;
warning("bit table overflow"); warning("bit table overflow");
} }
return(0); return (0);
} }
if (pbittab[bitindex] == 0 && pass == PASS_1) { if (pbittab[bitindex] == 0 && pass == PASS_1)
if ((pbittab[bitindex] = calloc(MEMINCR, 1)) == 0) { {
if ((pbittab[bitindex] = calloc(MEMINCR, 1)) == 0)
{
static int w2_given; static int w2_given;
if (!w2_given) { if (!w2_given)
{
w2_given = 1; w2_given = 1;
warning("out of space for bit table"); warning("out of space for bit table");
} }
@ -207,21 +214,23 @@ small(int fitsmall, int gain)
if (pbittab[bitindex] == 0) if (pbittab[bitindex] == 0)
return (0); return (0);
} }
bit = 1 << (nbits&7); bit = 1 << (nbits & 7);
p = pbittab[bitindex]+(nbits>>3); p = pbittab[bitindex] + (nbits >> 3);
nbits++; nbits++;
switch (pass) { switch (pass)
case PASS_1: {
return(0); case PASS_1:
case PASS_2: return (0);
if (fitsmall) { case PASS_2:
DOTGAIN += gain; if (fitsmall)
*p |= bit; {
} DOTGAIN += gain;
return(fitsmall); *p |= bit;
case PASS_3: }
assert(fitsmall || (*p & bit) == 0); return (fitsmall);
return(*p & bit); case PASS_3:
assert(fitsmall || (*p & bit) == 0);
return (*p & bit);
} }
/*NOTREACHED*/ /*NOTREACHED*/
} }
@ -229,13 +238,14 @@ small(int fitsmall, int gain)
/* ---------- output ---------- */ /* ---------- output ---------- */
void void emit1(int arg)
emit1(int arg)
{ {
static int olddottyp = -1; static int olddottyp = -1;
#ifdef LISTING #ifdef LISTING
if (listeoln) { if (listeoln)
if (listflag & 1) { {
if (listflag & 1)
{
listcolm += printx(VALWIDTH, (valu_t)DOTVAL); listcolm += printx(VALWIDTH, (valu_t)DOTVAL);
listcolm++; listcolm++;
putchar(' '); putchar(' ');
@ -243,81 +253,90 @@ emit1(int arg)
listeoln = 0; listeoln = 0;
} }
if (listflag & 2) if (listflag & 2)
listcolm += printx(2, (valu_t) arg); listcolm += printx(2, (valu_t)arg);
#endif #endif
switch (pass) { switch (pass)
case PASS_1: {
if (DOTSCT == NULL) case PASS_1:
nosect(); if (DOTSCT == NULL)
/* no break */ nosect();
case PASS_2: /* no break */
DOTSCT->s_zero = 0; case PASS_2:
break; DOTSCT->s_zero = 0;
case PASS_3: break;
if (DOTTYP != olddottyp) { case PASS_3:
wr_outsect(DOTTYP-S_MIN); if (DOTTYP != olddottyp)
olddottyp = DOTTYP; {
} wr_outsect(DOTTYP - S_MIN);
while (DOTSCT->s_zero) { olddottyp = DOTTYP;
wr_putc(0); }
DOTSCT->s_zero--; while (DOTSCT->s_zero)
} {
wr_putc(arg); wr_putc(0);
break; DOTSCT->s_zero--;
}
wr_putc(arg);
break;
} }
DOTVAL++; DOTVAL++;
} }
void void emit2(int arg)
emit2(int arg)
{ {
#ifdef BYTES_REVERSED #ifdef BYTES_REVERSED
emit1((arg>>8)); emit1(arg); emit1((arg >> 8));
emit1(arg);
#else #else
emit1(arg); emit1((arg>>8)); emit1(arg);
emit1((arg >> 8));
#endif #endif
} }
void void emit4(long arg)
emit4(long arg)
{ {
#ifdef WORDS_REVERSED #ifdef WORDS_REVERSED
emit2((int)(arg>>16)); emit2((int)(arg)); emit2((int)(arg >> 16));
emit2((int)(arg));
#else #else
emit2((int)(arg)); emit2((int)(arg>>16)); emit2((int)(arg));
emit2((int)(arg >> 16));
#endif #endif
} }
void void emitx(valu_t val, int n)
emitx(valu_t val, int n)
{ {
switch (n) { switch (n)
case RELO1: {
emit1((int)val); break; case RELO1:
case RELO2: emit1((int)val);
break;
case RELO2:
#ifdef BYTES_REVERSED #ifdef BYTES_REVERSED
emit1(((int)val>>8)); emit1((int)val); emit1(((int)val >> 8));
emit1((int)val);
#else #else
emit1((int)val); emit1(((int)val>>8)); emit1((int)val);
emit1(((int)val >> 8));
#endif #endif
break; break;
case RELO4: case RELO4:
#ifdef WORDS_REVERSED #ifdef WORDS_REVERSED
emit2((int)(val>>16)); emit2((int)(val)); emit2((int)(val >> 16));
emit2((int)(val));
#else #else
emit2((int)(val)); emit2((int)(val>>16)); emit2((int)(val));
emit2((int)(val >> 16));
#endif #endif
break; break;
default: default:
assert(0); assert(0);
} }
} }
void void emitstr(int zero)
emitstr(int zero)
{ {
int i; int i;
char *p; char* p;
p = stringbuf; p = stringbuf;
i = stringlen; i = stringlen;
@ -329,55 +348,50 @@ emitstr(int zero)
/* ---------- Error checked file I/O ---------- */ /* ---------- Error checked file I/O ---------- */
void void ffreopen(char* s, FILE* f)
ffreopen(char *s, FILE *f)
{ {
if (freopen(s, "r", f) == NULL) if (freopen(s, "r", f) == NULL)
fatal("can't reopen %s", s); fatal("can't reopen %s", s);
} }
FILE * FILE* ffcreat(char* s)
ffcreat(char *s)
{ {
FILE *f; FILE* f;
if ((f = fopen(s, "w")) == NULL) if ((f = fopen(s, "w")) == NULL)
fatal("can't create %s", s); fatal("can't create %s", s);
return(f); return (f);
} }
#ifndef TMPDIR #ifndef TMPDIR
#define TMPDIR "/tmp" #define TMPDIR "/tmp"
#endif #endif
char *tmp_dir = TMPDIR; char* tmp_dir = TMPDIR;
FILE * FILE* fftemp(char* path, char* tail)
fftemp(char *path, char *tail)
{ {
char *dir; char* dir;
if ((dir = getenv("TMPDIR")) == NULL) if ((dir = getenv("TMPDIR")) == NULL)
dir = tmp_dir; dir = tmp_dir;
sprintf(path, "%s/%s", dir, tail); sprintf(path, "%s/%s", dir, tail);
close(mkstemp(path)); close(mkstemp(path));
return(ffcreat(path)); return (ffcreat(path));
} }
/* ---------- Error handling ---------- */ /* ---------- Error handling ---------- */
/* ARGSUSED */ /* ARGSUSED */
void void yyerror(const char* message)
yyerror(const char *message) {
{} /* we will do our own error printing */ } /* we will do our own error printing */
void void nosect(void)
nosect(void)
{ {
fatal("no sections"); fatal("no sections");
} }
void void wr_fatal(void)
wr_fatal(void)
{ {
fatal("write error"); fatal("write error");
} }
@ -432,8 +446,7 @@ void warning(const char* s, ...)
va_end(ap); va_end(ap);
} }
void void nofit(void)
nofit(void)
{ {
if (pass == PASS_3) if (pass == PASS_3)
warning("too big"); warning("too big");