Pristine Ack-5.5
[Ack-5.5.git] / util / ego / ic / ic_io.c
1 /* $Id: ic_io.c,v 1.8 1994/06/24 10:24:12 ceriel Exp $ */
2 /*
3  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
4  * See the copyright notice in the ACK home directory, in the file "Copyright".
5  */
6 /*  I N T E R M E D I A T E   C O D E
7  *
8  *  I C _ I O . C
9  */
10
11
12
13 #include <stdio.h>
14 #include <em_pseu.h>
15 #include <em_spec.h>
16 #include <arch.h>
17 #include "../share/types.h"
18 #include "../share/debug.h"
19 #include "ic.h"
20 #include "ic_lookup.h"
21 #include "../share/alloc.h"
22 #include "ic_io.h"
23
24
25 STATIC short libstate;
26 STATIC long  bytecnt;
27
28 STATIC FILE *infile;  /* The current EM input file */
29
30 STATIC int readbyte()
31 {
32         if (libstate == ARCHIVE && bytecnt-- == 0L) {
33                 /* If we're reading from an archive file, we'll
34                  * have to count the number of characters read,
35                  * to know where the current module ends.
36                  */
37                 return EOF;
38         }
39         return getc(infile);
40 }
41
42
43
44
45 short readshort() {
46         register int l_byte, h_byte;
47
48         l_byte = readbyte();
49         h_byte = readbyte();
50         if ( h_byte>=128 ) h_byte -= 256 ;
51         return l_byte | (h_byte*256) ;
52 }
53
54 #ifdef LONGOFF
55 offset readoffset() {
56         register long l;
57         register int h_byte;
58
59         l = readbyte();
60         l |= ((unsigned) readbyte())*256 ;
61         l |= readbyte()*256L*256L ;
62         h_byte = readbyte() ;
63         if ( h_byte>=128 ) h_byte -= 256 ;
64         return l | (h_byte*256L*256*256L) ;
65 }
66 #endif
67
68
69 short get_int() {
70
71         switch(table2()) {
72         default: error("int expected");
73         case CSTX1:
74                 return(tabval);
75         }
76 }
77
78 char readchar()
79 {
80         return(readbyte());
81 }
82
83
84
85 offset get_off() {
86
87         switch (table2()) {
88         default: error("offset expected");
89         case CSTX1:
90                 return((offset) tabval);
91 #ifdef LONGOFF
92         case CSTX2:
93                 return(tabval2);
94 #endif
95         }
96 }
97
98 STATIC make_string(n) int n; {
99         
100         sprintf(string,".%u",n);
101 }
102
103 STATIC inident() {
104         register n;
105         register char *p = string;
106         register c;
107
108         n = get_int();
109         while (n--) {
110                 c = readbyte();
111                 if (p<&string[IDL])
112                         *p++ = c;
113         }
114         *p++ = 0;
115 }
116
117 int table3(n) int n; {
118
119         switch (n) {
120         case sp_ilb1:   tabval = readbyte(); return(ILBX);
121         case sp_ilb2:   tabval = readshort(); return(ILBX);
122         case sp_dlb1:   make_string(readbyte()); return(DLBX);
123         case sp_dlb2:   make_string(readshort()); return(DLBX);
124         case sp_dnam:   inident(); return(DLBX);
125         case sp_pnam:   inident(); return(n);
126         case sp_cst2:   tabval = readshort(); return(CSTX1);
127 #ifdef LONGOFF
128         case sp_cst4:   tabval2 = readoffset(); return(CSTX2);
129 #endif
130         case sp_doff:   if (table2()!=DLBX) error("symbol expected");
131                         switch(table2()) {
132                         default:        error("offset expected");
133                         case CSTX1:             return(VALX1);
134 #ifdef LONGOFF
135                         case CSTX2:             return(VALX2);
136 #endif
137                         }
138         default:        return(n);
139         }
140 }
141
142 int table1() {
143         register n;
144
145         n = readbyte();
146         if (n == EOF)
147                 return(ATEOF);
148         if ((n <= sp_lmnem) && (n >= sp_fmnem)) {
149                 tabval = n;
150                 return(INST);
151         }
152         if ((n <= sp_lpseu) && (n >= sp_fpseu)) {
153                 tabval = n;
154                 return(PSEU);
155         }
156         if ((n < sp_filb0 + sp_nilb0) && (n >= sp_filb0)) {
157                 tabval = n - sp_filb0;
158                 return(ILBX);
159         }
160         return(table3(n));
161 }
162
163 int table2() {
164         register n;
165
166         n = readbyte();
167         if ((n < sp_fcst0 + sp_ncst0) && (n >= sp_fcst0)) {
168                 tabval = n - sp_zcst0;
169                 return(CSTX1);
170         }
171         return(table3(n));
172 }
173
174
175
176
177 file_init(f,state,length)
178         FILE *f;
179         short state;
180         long  length;
181 {
182         short n;
183
184         infile = f;
185         libstate = state;
186         bytecnt = length;
187         linecount = 0;
188         n = readshort();
189         if (n != (short) sp_magic) {
190                 error("wrong magic number: %d", n);
191         }
192 }
193
194
195
196 arch_init(arch)
197         FILE *arch;
198 {
199         short n;
200
201         infile = arch;
202         n = readshort();
203         if (n != (short)ARMAG && n != (short)AALMAG) {
204                 error("wrong archive magic number: %d",n);
205         }
206 }