96d68858edf5ba40a6badde659384eb13b07f200
[vlp.git] / src / int / util.c
1 /* Loglan82 Compiler&Interpreter
2      Copyright (C) 1981-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              You should have received a copy of the GNU General Public License
16              along with this program; if not, write to the Free Software
17              Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19  contacts:  Andrzej.Salwicki@univ-pau.fr
20
21 or             Andrzej Salwicki
22                 LITA   Departement d'Informatique
23                 Universite de Pau
24                 Avenue de l'Universite
25                 64000 Pau   FRANCE
26                  tel.  ++33 59923154    fax. ++33 59841696
27
28 =======================================================================
29 */
30
31 #include "depend.h"
32 #include "genint.h"
33 #include "int.h"
34 #include "process.h"
35 #include "intproto.h"
36
37
38 #include <time.h>
39
40 /* Utility routines */
41
42 /**
43  * Compute entier (floor)
44  */
45 word entier(double x)
46 {
47         word i;
48
49         if (x >= 0.0) {
50                 i = (word)x;
51                 return i;
52         } else {
53                 i = (word)(-x);
54                 i = -i;
55                 if ((double)i <= x)
56                         return i;
57                 else
58                         return i - 1;
59         }
60 }
61
62 /**
63  * shift x by n bits
64  */
65 word shift(word x, word n)
66 {
67         if (n == 0)
68                 return (x);
69         if (n > 0)
70                 return (x << n);
71         else
72                 return ((x >> -n) & ~(~(word)0 << (8 * sizeof(word) + n)));
73 }
74
75
76 /**
77  * Get ASCIIZ string from arrayof char
78  */
79 char *asciiz(virtaddr *virt)
80 {
81         word am;
82         int len, i;
83         char *cp;
84
85         if (member(virt, &am)) {
86                 /* length of the string */
87                 len = M[am] - 3;
88                 /* allocate buffer for the string */
89                 cp = ballocate(len + 1);
90
91                 if (cp == NULL)
92                         errsignal(RTEMEMOV);
93
94                 for (i = 0; i < len; i++)
95                         cp[i] = (char) M[am + 3 + i];
96
97                 /* terminate string with 0 byte */
98                 cp[len] = '\0';
99                 return cp;
100         }
101         else {
102                 /* reference to none */
103                 errsignal(RTEREFTN);
104         }
105 }
106
107 /**
108  * Add extension to a file name
109  */
110 void addext(char *fname, char *ext)
111 {
112         char *cp;
113
114         cp = fname;
115         while (*cp != '\0' && *cp != '.')
116                 cp++;
117         strcpy(cp, ext);
118 }
119
120 void usage()
121 {
122 }
123
124 /**
125  * Print error message and abort
126  */
127 void abend(char *msg)
128 {
129         fprintf(stderr, "Error: %s\n", msg);
130         exit(8);
131 }
132
133
134 /* Pseudo random number generator */
135
136 static int ranpat1 = 7, ranpat2 = 503, ranpat3 = 15661;
137
138 /**
139  * Initialize generator
140  */
141 void ranset()
142 {
143         long tim;
144
145         time(&tim);
146         ranpat1 = tim % 30269;
147         ranpat2 = tim % 30307;
148         ranpat3 = tim % 30323;
149 }
150
151 /**
152  * Produce next pseudo random number
153  */
154 double prandom()
155 {
156         int i;
157         double r;
158
159         ranpat1 = 171 * (ranpat1 % 177) - 2 * (ranpat1 / 177);
160         if (ranpat1 < 0)
161                 ranpat1 += 30269;
162
163         ranpat2 = 172 * (ranpat2 % 176) - 35 * (ranpat2 / 176);
164
165         if (ranpat2 < 0)
166                 ranpat2 += 30307;
167
168         ranpat3 = 170 * (ranpat3 % 178) - 63 * (ranpat3 / 178);
169
170         if (ranpat3 < 0)
171                 ranpat3 += 30323;
172
173         r = ranpat1 / 30269.0 + ranpat2 / 30307.0 + ranpat3 / 30323.0;
174         i = (int)r;
175         return r - i;
176 }
177
178 /**
179  * Copy a block of memory
180  */
181 void moveblock(char *from, char *to, word len)
182 {
183         while (len-- > 0) {
184                 *to++ = *from++;
185         }
186 }
187
188 /**************************************************************
189
190 #define LINE 10
191 void dump(word pix, word from, int len)
192 {
193         int i;
194         memory M;
195
196         M = process[pix].M;
197         while (len > 0) {
198                 printf("%6ld: ", (long) from);
199                 for (i = 0; i < LINE; i++)
200                         printf("%7ld", (long)M[from++]);
201                 putchar('\n');
202                 len -= LINE;
203         }
204 }
205  **************************************************************/
206