ANSI C conversion.

This commit is contained in:
carl 2019-05-11 01:15:51 +08:00
parent 970e7da896
commit 0301827482
3 changed files with 101 additions and 86 deletions

View file

@ -7,18 +7,17 @@ static char rcsid[] = "$Id$";
#endif #endif
/* This program converts either human-readable or compact EM /* This program converts either human-readable or compact EM
assembly code to calls of the procedure-interface. assembly code to calls of the procedure-interface.
It must be linked with two libraries: It must be linked with two libraries:
- a library to read EM code, according to read_em(3) - a library to read EM code, according to read_em(3)
- a library implementing the em_code(3) interface. - a library implementing the em_code(3) interface.
Thus, this program could serve as an EM_encoder, an Thus, this program could serve as an EM_encoder, an
EM_decoder, or some code generator, depending on how it is EM_decoder, or some code generator, depending on how it is
linked. linked.
*/ */
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include "system.h"
#include "print.h" #include "print.h"
#include "em_pseu.h" #include "em_pseu.h"
#include "em_mnem.h" #include "em_mnem.h"
@ -28,62 +27,69 @@ static char rcsid[] = "$Id$";
#include "em.h" #include "em.h"
#include "em_comp.h" #include "em_comp.h"
char *filename; /* Name of input file */ char *filename; /* Name of input file */
int errors; /* Number of errors */ int errors; /* Number of errors */
extern char *C_error; extern char *C_error;
extern int C_out(register struct e_instr *);
void error(const char *, ...); void error(const char *, ...);
void fatal(const char *, ...); void fatal(const char *, ...);
int int main(int argc, char **argv)
main(int argc, char **argv)
{ {
struct e_instr buf; struct e_instr buf;
register struct e_instr *p = &buf; register struct e_instr *p = &buf;
if (argc >= 2) { if (argc >= 2)
{
filename = argv[1]; filename = argv[1];
} }
else filename = 0; else
if (!EM_open(filename)) { filename = 0;
if (!EM_open(filename))
{
fatal(EM_error); fatal(EM_error);
} }
EM_getinstr(p); EM_getinstr(p);
C_init((arith) EM_wordsize, (arith) EM_pointersize); C_init((arith) EM_wordsize, (arith) EM_pointersize);
if (argc >= 3) { if (argc >= 3)
if (!C_open(argv[2])) { {
if (!C_open(argv[2]))
{
fatal("C_open failed"); fatal("C_open failed");
} }
} }
else if (!C_open( (char *) 0)) fatal("C_open failed"); else if (!C_open((char *) 0))
fatal("C_open failed");
C_magic(); C_magic();
while (p->em_type != EM_EOF) { while (p->em_type != EM_EOF)
if (p->em_type == EM_FATAL) { {
if (p->em_type == EM_FATAL)
{
fatal("%s", EM_error); fatal("%s", EM_error);
} }
if (EM_error) { if (EM_error)
{
error("%s", EM_error); error("%s", EM_error);
} }
if (p->em_type != EM_ERROR && !C_out(p)) { if (p->em_type != EM_ERROR && !C_out(p))
{
error("%s", C_error); error("%s", C_error);
} }
EM_getinstr(p); EM_getinstr(p);
} }
C_close(); C_close();
EM_close(); EM_close();
exit(errors ? 1 : 0); exit(errors ? EXIT_FAILURE : EXIT_SUCCESS);
} }
/* VARARGS */ /* VARARGS */
void void error(const char *s, ...)
error(const char *s, ...)
{ {
va_list ap; va_list ap;
va_start(ap, s); va_start(ap, s);
fprint(STDERR, fprint(STDERR, "%s, line %d: ", filename ? filename : "standard input",
"%s, line %d: ", EM_lineno);
filename ? filename : "standard input",
EM_lineno);
doprnt(STDERR, s, ap); doprnt(STDERR, s, ap);
fprint(STDERR, "\n"); fprint(STDERR, "\n");
errors++; errors++;
@ -91,12 +97,12 @@ error(const char *s, ...)
} }
/* VARARGS */ /* VARARGS */
void void fatal(const char *s, ...)
fatal(const char *s, ...)
{ {
va_list ap; va_list ap;
va_start(ap, s); va_start(ap, s);
if (C_busy()) C_close(); if (C_busy())
C_close();
error(s, ap); error(s, ap);
exit(1); exit(EXIT_FAILURE);
} }

View file

@ -3,10 +3,10 @@
.SH NAME .SH NAME
em_decode, em_encode \- compact to readable EM and v.v. em_decode, em_encode \- compact to readable EM and v.v.
.SH SYNOPSIS .SH SYNOPSIS
.B ~em/lib.bin/em_decode .B em_decode
[ inputfile [ outputfile ] ] [ inputfile [ outputfile ] ]
.br .br
.B ~em/lib.bin/em_encode .B em_encode
[ inputfile [ outputfile ] ] [ inputfile [ outputfile ] ]
.SH DESCRIPTION .SH DESCRIPTION
Most programs involved with the EM project only produce and accept Most programs involved with the EM project only produce and accept
@ -15,12 +15,12 @@ These files are only machine readable.
A description of this compact form can be found in [1]. A description of this compact form can be found in [1].
To inspect the code produced by compilers or to patch them for one reason To inspect the code produced by compilers or to patch them for one reason
or another, human readable assembly code is needed. or another, human readable assembly code is needed.
Em_decode produces human readable assembly code from the compact form. em_decode produces human readable assembly code from the compact form.
.PP .PP
Em_decode accepts the normal compact form in both optimized and em_decode accepts the normal compact form in both optimized and
unoptimized form. unoptimized form.
.PP .PP
Em_encode produces the compact form em_encode produces the compact form
out of these human readable assembly code. out of these human readable assembly code.
.PP .PP
The first argument is the input file. The first argument is the input file.

View file

@ -1,5 +1,5 @@
/* esize: prints info from e.out header /* esize: prints info from e.out header
*/ */
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@ -12,9 +12,9 @@ FILE *load_fp;
int eof; int eof;
/* Much of the code has been borrowed from the EM interpreter /* Much of the code has been borrowed from the EM interpreter
*/ */
typedef /* unsigned */ long ptr; /* pointer to EM adress */ typedef /* unsigned */long ptr; /* pointer to EM adress */
long magic; long magic;
long flags; long flags;
@ -33,51 +33,65 @@ long szdata;
long ptr7; long ptr7;
long ptr8; long ptr8;
main(argc, argv) /* Forward declarations. */
int argc; static void esize(char *);
char *argv[]; static int rd_open(char*);
static int rd_byte(void);
static long rd_int(long n);
static int rd_header(void);
static void rd_close(void);
#define rd_ptr() ((ptr) rd_int(psize))
int main(int argc, char *argv[])
{ {
printf("TPFCRE uref vers w/p text nproc szdata\n"); printf("TPFCRE uref vers w/p text nproc szdata\n");
if (argc == 1) { if (argc == 1)
{
esize("e.out"); esize("e.out");
} }
else { else
while (argc > 1) { {
while (argc > 1)
{
esize(argv[1]); esize(argv[1]);
argc--, argv++; argc--, argv++;
} }
} }
exit(0); exit(EXIT_SUCCESS);
} }
esize(fname) static void esize(char *fname)
char *fname;
{ {
eof = 0; eof = 0;
if (!rd_open(fname)) { if (!rd_open(fname))
{
printf("%s: cannot open\n", fname); printf("%s: cannot open\n", fname);
} }
else { else
if (!rd_header()) { {
if (!rd_header())
{
printf("%s: not EM object format\n", fname); printf("%s: not EM object format\n", fname);
} }
else { else
printf("%c", flags&0001 ? 'T' : '-'); {
printf("%c", flags&0002 ? 'P' : '-'); printf("%c", flags & 0001 ? 'T' : '-');
printf("%c", flags&0004 ? 'F' : '-'); printf("%c", flags & 0002 ? 'P' : '-');
printf("%c", flags&0010 ? 'C' : '-'); printf("%c", flags & 0004 ? 'F' : '-');
printf("%c", flags&0020 ? 'R' : '-'); printf("%c", flags & 0010 ? 'C' : '-');
printf("%c", flags&0040 ? 'E' : '-'); printf("%c", flags & 0020 ? 'R' : '-');
printf("%c", flags&0100 ? '?' : ' '); printf("%c", flags & 0040 ? 'E' : '-');
printf("%c", flags&0200 ? '?' : ' '); printf("%c", flags & 0100 ? '?' : ' ');
printf("%c", flags & 0200 ? '?' : ' ');
printf("%3ld ", uref); printf("%3ld ", uref);
printf("%3ld ", version); printf("%3ld ", version);
printf("%1ld/%1ld", wsize, psize); printf("%1ld/%1ld", wsize, psize);
printf("%c", int7 ? '?' : ' '); printf("%c", int7 ? '?' : ' ');
printf("%c", int8 ? '?' : ' '); printf("%c", int8 ? '?' : ' ');
printf("%5ld ", ntext); printf("%5ld ", ntext);
printf("%5ld ", nproc); printf("%5ld ", nproc);
printf("%6ld", szdata); printf("%6ld", szdata);
@ -91,15 +105,12 @@ esize(fname)
#define btol(a) ((long)(((long) (a)) & 0xFF)) #define btol(a) ((long)(((long) (a)) & 0xFF))
int static int rd_open(char* load_file)
rd_open(load_file)
char *load_file;
{ {
return (load_fp = fopen(load_file, "r")) != NULL; return (load_fp = fopen(load_file, "rb")) != NULL;
} }
int static int rd_byte(void)
rd_byte()
{ {
int i; int i;
@ -108,35 +119,33 @@ rd_byte()
return (i); return (i);
} }
long static long rd_int(long n)
rd_int(n)
long n;
{ {
long l; long l;
register int i; register int i;
l = btol(rd_byte()); l = btol(rd_byte());
for (i = 1; i < n; i++) for (i = 1; i < n; i++)
l = l | (btol(rd_byte()) << (long) (i*8)); l = l | (btol(rd_byte()) << (long) (i * 8));
return (l); return (l);
} }
#define rd_ptr() ((ptr) rd_int(psize))
int
rd_header() /* read e.out header information */
static int rd_header(void)
{ {
magic = rd_int(2L); magic = rd_int(2L);
if (magic != MAGIC || eof) if (magic != MAGIC || eof)
return 0; return 0;
flags = rd_int(2L); flags = rd_int(2L);
uref = rd_int(2L); uref = rd_int(2L);
version = rd_int(2L); version = rd_int(2L);
wsize = rd_int(2L); wsize = rd_int(2L);
psize = rd_int(2L); psize = rd_int(2L);
int7 = rd_int(2L); /* Entry 7 is unused */ int7 = rd_int(2L); /* Entry 7 is unused */
int8 = rd_int(2L); /* Entry 8 is unused */ int8 = rd_int(2L); /* Entry 8 is unused */
ntext = rd_ptr(); ntext = rd_ptr();
ndata = rd_ptr(); ndata = rd_ptr();
@ -144,13 +153,13 @@ rd_header()
entrypoint = rd_ptr(); entrypoint = rd_ptr();
nline = rd_ptr(); nline = rd_ptr();
szdata = rd_ptr(); szdata = rd_ptr();
ptr7 = rd_ptr(); /* entry 7 is unused */ ptr7 = rd_ptr(); /* entry 7 is unused */
ptr8 = rd_ptr(); /* entry 8 is unused */ ptr8 = rd_ptr(); /* entry 8 is unused */
return !eof; return !eof;
} }
rd_close() static void rd_close(void)
{ {
fclose(load_fp); fclose(load_fp);
} }