Added upstream from http://ftp.icm.edu.pl/pub/loglan/
[loglan.git] / sources / gen / genio.c
1 /*     Loglan82 Compiler&Interpreter
2      Copyright (C) 1993 Institute of Informatics, University of Warsaw
3      Copyright (C)  1993, 1994 LITA, Pau
4      
5      This program is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published by
7      the Free Software Foundation; either version 2 of the License, or
8      (at your option) any later version.
9      
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14      
15
16 =======================================================================
17 */
18
19 #include "glodefs.h" 
20
21 #ifndef NO_PROTOTYPES
22
23 static void fputaddr(FILE *,address);
24 static int fgetint(FILE *);
25
26 #else
27
28 static void fputaddr();
29 static int fgetint();
30
31 #endif
32
33 int wordswritten = 0 ;
34
35 void setfiles(name) char *name;{
36  char work[80];
37  
38  strcpy(work, name) ;
39  strcat(work, ".lcd") ;
40  lfile = fopen(work, "rb");
41  if(lfile == NULL) 
42  { puts(strcat("Couldn't open file ", work)) ;
43    exit(8) ; 
44  }
45  strcpy(work, name) ;
46  strcat(work, ".pcd") ;
47  pfile = fopen(work, "wb") ;
48  
49  strcpy(work, name) ;
50  strcat(work, ".ccd") ;
51  cfile = fopen(work, "wb") ;
52  if(cfile == NULL) 
53  { 
54    printf("Couldn't open file %s for writing\n", work);
55    exit(8) ; 
56  }
57 } /* setfiles */
58  
59
60
61 int next(){
62    int i;
63
64    if(!fread((char *)&i,sizeof(i),1,lfile)){
65       printf("Unexpected EOF ! Aborting...\n");
66       exit(8);
67    }
68    return i;
69 } /* next */
70
71 static int fgetint(f) FILE * f;{
72    int c ;
73    fread((char *)&c, sizeof(int), 1, f) ;
74    return c ;
75 }
76
77 static void fputaddr(f,v) FILE *f; address v;{
78    fwrite((char *)&v, sizeof(address), 1, f);
79 }
80  
81    /* Buffered version - not finished   
82  static    address   buf[BUFSIZE] ;
83  static  address * bufp = buf;
84  static  int       n = 0;
85  
86  if(n==BUFSIZE)   */
87  
88 void putstrings()
89 {
90  int  cc, i, ch2 ;
91  int c1, c2, start, nr=1 ;
92  
93      cc = fgetint(lfile);                    /* character count */
94
95      while (cc != ENDOFSTRINGS)
96      {
97       m[ fre ] = cc;
98        fre ++ ;
99        start=fre;
100
101        for (i = 1; i <= (cc / CHARSINAD + 1) ; i++ )
102           /* extra 0 byte after string */
103        {
104
105 #if SMALL || HUGE
106         ch2 = fgetint(lfile) ;
107         m[ fre ] = ch2;
108 #elif LARGE
109         c1 = fgetint(lfile) ;
110         c2 = fgetint(lfile) ;
111         m[ fre ] = ( (long)c1 ) | ( ((long)c2) << 16 ) ;
112 #endif
113
114          fre++ ;
115        }
116
117 #if (TALK >= 2)
118        if(fre!=start+1)
119          printf("string %d = %s\n", nr++, (char *)(&m[start]));
120 #endif
121
122        cc = fgetint(lfile) ;
123      }
124    } /* putstrings */
125
126
127 void initiate()
128    /* read ipmem and some variables from the blank common of the compiler */
129 {
130   int tarr[302];
131 #ifdef DUMP
132   int  k;
133 #endif
134   fread((char *)tarr, sizeof(int), 302, lfile) ;
135   
136
137      lmem   = tarr[278] ;
138      lpmem  = tarr[279] ;
139      irecn  = tarr[280] ;
140      nrint  = tarr[285] ;
141      nrre   = tarr[286] ;
142      nrbool = tarr[287] ;
143      nrchr  = tarr[288] ;
144      nrcor  = tarr[289] ;
145      nrproc = tarr[290] ;
146      nrtext = tarr[291] ;
147      nblus  = tarr[296] ;
148      addrnone = tarr[299] ;   /*cdsw&ail */
149
150 #if (TALK >= 2)
151      printf("Initiate\n lmem = %d, lpmem = %d, irecn = %d\n",
152             lmem, lpmem, irecn);
153      printf(" nrint = %d\n", nrint);
154      printf(" nrre  = %d\n", nrre );
155      printf(" nrbool= %d\n", nrbool);
156      printf(" nrchr = %d\n", nrchr);
157      printf(" nrcor = %d\n", nrcor);
158      printf(" nrproc= %d\n", nrproc);
159      printf(" nrtext= %d\n", nrtext);
160      printf(" nblus = %d\n", nblus);
161      printf(" addrnone = %d\n", addrnone);
162 #endif
163
164      ipmem = (int *) calloc(lmem + 1, sizeof(int));
165      fread( (char *)(ipmem + 1), sizeof(int), irecn, lfile);
166    
167 #ifdef DUMP
168    for (k = 1; k <= irecn; k++)
169    {
170      printf("%8d",ipmem[k]);
171      if (k % 16 == 0) printf("\n");
172    }
173    exit(0);
174 #endif
175
176  }
177
178
179 void putreals()
180  /* WRITE REAL CONSTANTS TO MEMORY */
181 {
182   int i  ;
183   union 
184   {
185     float      r ;
186     struct { int int1, int2 ; } p;
187   }  trick1 ;
188
189   union 
190   {
191     real   r ;
192     struct { address int1, int2 ; } p;
193   }  trick2 ;
194
195   i = lpmem + 1 ;
196   while (i <= irecn)
197   {
198 #ifdef HUGE
199 #if (TALK >=2 )
200     printf("real %10d:%18f\n",i,*(float *)&(ipmem[i]));
201 #endif
202     m[ fre++ ] = ipmem[ i++ ] ; 
203 #else
204     trick1.p.int1 = ipmem[ i++ ] ; 
205     trick1.p.int2 = ipmem[ i++ ] ;
206     trick2.r = trick1.r;
207 #if (TALK >=2 )
208     printf("real %10d:%18f\n",i,trick1.r);
209 #endif
210     m[ fre++ ] = trick2.p.int1;
211     m[ fre++ ] = trick2.p.int2;
212 #endif
213   }
214 }/* putreals */
215
216
217
218
219 void generror(err)
220 errtype err;
221 {
222      switch (err)
223      {
224       case    TMPROT  : printf(" TOO MANY PROTOTYPES");
225               break ;
226
227       case     TLDESCR : printf(" DESCRIPTIONS TOO LONG");
228               break ;
229
230       case        MEMOVF  : printf(" MEMORY OVERFLOW ");
231               break ;
232
233       case        TMTEMP  : printf(" TOO MANY TEMPORARY VARIABLES NEEDED");
234               break ;
235
236       case        STSEQTL : printf(" STATEMENT SEQUENCE TOO LONG");
237               break ;
238
239       case        NOTIMPL : printf(" FUNCTION NOT IMPLEMENTED");
240               break ;
241
242       case        OBJTOLG : printf(" OBJECT TOO LONG");
243               break ;
244
245       case        PROCLTL : printf(" PROCEDURE LIST TOO LONG ") ;
246               break ;
247      } /* switch */
248
249      exit(4);
250
251    } /* generror */
252
253
254
255
256 void out(){
257    /* PUT THE CONTENTS OF M^[  0 .. fre-1  ] ON THE FILE "CFILE" */
258    /* 'BASE$ IS UPDATED AND 'fre' IS RESET TO ZERO              */
259
260    address n,w;
261
262 #if (TALK > 2)     
263    printf("Writing %4d words of code\n", fre) ;
264 #endif
265
266    for (n=0; n <= fre-1; n++ ){
267       w = m[ n ];
268       fputaddr(cfile, w);
269       wordswritten ++ ;
270    }
271
272    base += fre;
273 #if (TALK > 3)
274    printf("base = %d\n", base) ;
275    printf("fre = %d\n", fre) ;
276 #endif
277    fre = 0;
278 }
279
280
281 void outprot()
282 {
283   protaddr  n ;
284   address   lp ;
285
286   for (n = MAINBLOCK; n <= lastprot; n++)
287     fwrite( (char *)(prototype[ n ]), sizeof(*(prototype[0])), 1, pfile);
288
289   fputaddr(cfile,ipradr);
290   fputaddr(cfile,temporary);
291   fputaddr(cfile,strings);
292   lp = lastprot;
293   fputaddr(cfile, lp);
294   fputaddr(cfile,base);
295 }
296
297
298 int apet(ip)           /* STRONGLY MACHINE DEPENDENT */
299 int ip ;
300 {
301    return  ( iand( ishft( ipmem [ ip ],-14 ), 3 ) + 1 );
302 }
303
304