Pristine Ack-5.5
[Ack-5.5.git] / mach / sparc / ce_cg / 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.3 1994/06/24 13:57:37 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 char *out_file;                 /* Name of output file */
30 int errors;                     /* Number of errors */
31 int debug;
32 int __gdb_flag;
33 extern char *C_error;
34
35 main(argc,argv)
36         char **argv;
37 {
38         struct e_instr buf;
39         register struct e_instr *p = &buf;
40         int ac;
41         char **av;
42
43         filename= 0;
44         out_file= 0;
45         debug= 0;
46         for (ac=argc-1,av=argv+1;ac;ac--,av++)
47         {
48                 if (av[0][0]=='-' && av[0][1]=='d' && av[0][2]==0)
49                 {
50                         debug= 1;
51                         continue;
52                 }
53                 if (! strcmp(av[0], "-gdb")) {
54                         __gdb_flag = 1;
55                         continue;
56                 }
57                 if (!filename)
58                 {
59                         if (strcmp(*av, "-"))
60                                 filename= *av;
61                 }
62                 else if (!out_file)
63                 {
64                         if (strcmp(*av, "-"))
65                                 out_file= *av;
66                 }
67                 else
68                         fatal ("too many arguments");
69         }
70         if (!EM_open(filename)) {
71                 fatal(EM_error);
72         }
73         EM_getinstr(p);
74         C_init((arith) EM_wordsize, (arith) EM_pointersize);
75         if (!C_open(out_file)) {
76                 fatal("C_open failed");
77         }
78         C_magic();
79         while (p->em_type != EM_EOF) {
80                 if (p->em_type == EM_FATAL) {
81                         fatal("%s", EM_error);
82                 }
83                 if (EM_error) {
84                         error("%s", EM_error);
85                 }
86                 if (p->em_type != EM_ERROR && !C_out(p)) {
87                         error("%s", C_error);
88                 }
89                 EM_getinstr(p);
90         }
91         C_close();
92         EM_close();
93         exit(errors);
94 }
95
96 /* VARARGS */
97 error(s,a1,a2,a3,a4)
98         char *s;
99 {
100         fprint(STDERR,
101                 "%s, line %d: ",
102                 filename ? filename : "standard input",
103                 EM_lineno);
104         fprint(STDERR,s,a1,a2,a3,a4);
105         fprint(STDERR, "\n");
106         errors++;
107 }
108
109 /* VARARGS */
110 fatal(s,a1,a2,a3,a4)
111         char *s;
112 {
113         if (C_busy()) C_close();
114         error(s,a1,a2,a3,a4);
115         exit(1);
116 }