Pristine Ack-5.5
[Ack-5.5.git] / util / misc / 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.11 1994/06/24 10:35: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 extern char *C_error;
31
32 main(argc,argv)
33         char **argv;
34 {
35         struct e_instr buf;
36         register struct e_instr *p = &buf;
37
38         if (argc >= 2) {
39                 filename = argv[1];
40         }
41         else    filename = 0;
42         if (!EM_open(filename)) {
43                 fatal(EM_error);
44         }
45         EM_getinstr(p);
46         C_init((arith) EM_wordsize, (arith) EM_pointersize);
47         if (argc >= 3) {
48                 if (!C_open(argv[2])) {
49                         fatal("C_open failed");
50                 }
51         }
52         else    if (!C_open( (char *) 0)) fatal("C_open failed");
53         C_magic();
54         while (p->em_type != EM_EOF) {
55                 if (p->em_type == EM_FATAL) {
56                         fatal("%s", EM_error);
57                 }
58                 if (EM_error) {
59                         error("%s", EM_error);
60                 }
61                 if (p->em_type != EM_ERROR && !C_out(p)) {
62                         error("%s", C_error);
63                 }
64                 EM_getinstr(p);
65         }
66         C_close();
67         EM_close();
68         exit(errors);
69 }
70
71 /* VARARGS */
72 error(s,a1,a2,a3,a4)
73         char *s;
74 {
75         fprint(STDERR,
76                 "%s, line %d: ",
77                 filename ? filename : "standard input",
78                 EM_lineno);
79         fprint(STDERR,s,a1,a2,a3,a4);
80         fprint(STDERR, "\n");
81         errors++;
82 }
83
84 /* VARARGS */
85 fatal(s,a1,a2,a3,a4)
86         char *s;
87 {
88         if (C_busy()) C_close();
89         error(s,a1,a2,a3,a4);
90         exit(1);
91 }