Added upstream version.
[vlp.git] / 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 word entier(x)                          /* Compute entier (floor) */
44 double x;
45 {
46     word i;
47
48     if (x >= 0.0)
49     {
50         i = (word)x;
51         return(i);
52     }
53     else
54     {
55         i = (word)(-x);
56         i = -i;
57         if ((double)i <= x) return(i);  else return(i-1);
58     }
59 } /* end entier */
60
61
62 word shift(x, n)                        /* shift x by n bits */
63 word x, n;
64 {
65     if (n == 0) return (x);
66     if (n > 0) return (x << n);
67     else return ( (x >> -n) & ~(~(word)0 << (8*sizeof(word)+n)) );
68 } /* end shift */
69
70
71 char *asciiz(virt)                    /* Get ASCIIZ string from arrayof char */
72 virtaddr *virt;
73 {
74     word am;
75     int len, i;
76     char *cp;
77
78     if (member(virt, &am))
79     {
80         len = M[ am ]-3;                /* length of the string */
81         cp = ballocate(len+1);          /* allocate buffer for the string */
82         if (cp == NULL) errsignal(RTEMEMOV);
83         for (i = 0;  i < len;  i++) cp[ i ] = (char) M[ am+3+i ];
84         cp[ len ] = '\0';               /* terminate string with 0 byte */
85         return (cp);
86     }
87     else errsignal(RTEREFTN);           /* reference to none */
88 } /* end asciiz */
89
90
91 void addext(fname, ext)                 /* Add extension to a file name */
92 char *fname, *ext;
93 {
94     char *cp;
95
96     cp = fname;
97     while (*cp != '\0' && *cp != '.') cp++;
98     strcpy(cp, ext);
99 } /* end addext */
100
101
102 void usage()
103 {
104 }
105
106
107 void abend(msg)                         /* Print error message and abort */
108 char *msg;
109 {
110     fprintf(stderr, "Error: %s\n", msg);
111     exit(8);
112 } /* end abend */
113
114
115 /* Pseudo random number generator */
116
117 static int ranpat1 = 7, ranpat2 = 503, ranpat3 = 15661;
118
119 void ranset()                           /* Initialize generator */
120 {
121     long tim;
122
123     time(&tim);
124     ranpat1 = tim % 30269;
125     ranpat2 = tim % 30307;
126     ranpat3 = tim % 30323;
127 } /* end ranset */
128
129
130 double prandom()                                /* Produce next pseudo random number */
131 {
132     int i;
133     double r;
134
135     ranpat1 = 171*(ranpat1 % 177)- 2*(ranpat1 / 177);
136     if (ranpat1 < 0) ranpat1 += 30269;
137     ranpat2 = 172*(ranpat2 % 176)-35*(ranpat2 / 176);
138     if (ranpat2 < 0) ranpat2 += 30307;
139     ranpat3 = 170*(ranpat3 % 178)-63*(ranpat3 / 178);
140     if (ranpat3 < 0) ranpat3 += 30323;
141     r = ranpat1/30269.0 + ranpat2/30307.0 + ranpat3/30323.0;
142     i = (int)r;
143     return (r-i);
144 }
145
146
147 void moveblock(from, to, len)           /* Copy a block of memory */
148 char *from, *to;
149 word len;
150 {
151     while (len-- > 0) *to++ = *from++;
152 } /* end moveblock */
153
154
155 /**************************************************************
156
157 #define LINE    10
158 void dump(pix, from, len)
159 word pix, from;
160 int len;
161 {
162     int i;
163     memory M;
164
165     M = process[ pix ].M;
166     while (len > 0)
167     {
168         printf("%6ld: ", (long) from);
169         for (i = 0; i < LINE; i++) printf("%7ld", (long)M[from++]);
170         putchar('\n');
171         len -= LINE;
172     }
173 }
174
175  **************************************************************/
176