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