Pristine Ack-5.5
[Ack-5.5.git] / modules / src / em_code / convert.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 #ifndef NORCSID
6 static char rcsid[] = "$Id: convert.c,v 1.2 1994/06/24 11:09:42 ceriel Exp $";
7 #endif
8
9 /*      This program converts either human-readable or compact EM
10         assembly code to calls of the procedure-interface.
11         It must be linked with two libraries:
12         - a library to read EM code, according to read_em(3)
13         - a library implementing the em_code(3) interface.
14         Thus, this program could serve as an EM_encoder, an
15         EM_decoder, or some code generator, depending on how it is
16         linked.
17 */
18
19 #include        <system.h>
20 #include        <em_pseu.h>
21 #include        <em_mnem.h>
22 #include        <em_spec.h>
23 #include        <em_flag.h>
24 #include        <em_ptyp.h>
25 #include        <em.h>
26 #include        <em_comp.h>
27
28 char *filename;                 /* Name of input file */
29 int errors;                     /* Number of errors */
30
31 main(argc,argv)
32         char **argv;
33 {
34         struct e_instr buf;
35         register struct e_instr *p = &buf;
36
37         if (argc >= 2) {
38                 filename = argv[1];
39         }
40         else    filename = 0;
41         if (!EM_open(filename)) {
42                 fatal(EM_error);
43         }
44         EM_getinstr(p);
45         C_init((arith) EM_wordsize, (arith) EM_pointersize);
46         if (argc >= 3) {
47                 if (!C_open(argv[2])) {
48                         fatal("C_open failed");
49                 }
50         }
51         else    if (!C_open( (char *) 0)) fatal("C_open failed");
52         C_magic();
53         while (p->em_type != EM_EOF) {
54                 if (p->em_type == EM_FATAL) {
55                         fatal("%s", EM_error);
56                 }
57                 if (EM_error) {
58                         error("%s", EM_error);
59                 }
60                 if (p->em_type != EM_ERROR) {
61                         EM_mkcalls(p);
62                 }
63                 EM_getinstr(p);
64         }
65         C_close();
66         EM_close();
67         exit(errors);
68 }
69
70 /* VARARGS */
71 error(s,a1,a2,a3,a4)
72         char *s;
73 {
74         fprint(STDERR,
75                 "%s, line %d: ",
76                 filename ? filename : "standard input",
77                 EM_lineno);
78         fprint(STDERR,s,a1,a2,a3,a4);
79         fprint(STDERR, "\n");
80         errors++;
81 }
82
83 /* VARARGS */
84 fatal(s,a1,a2,a3,a4)
85         char *s;
86 {
87         error(s,a1,a2,a3,a4);
88         exit(1);
89 }