Genericize the ReversePostOrderIterator.
[oota-llvm.git] / tools / lli / RuntimeLib.lc
1 //===-- RuntimeLib.lc - LLVM Standard C Runtime Library -----------*- C -*-===//
2 // 
3 // This file contains definitions of C functions that are useful to get LLVM
4 // programs up and running.  This library of functions is automatically linked
5 // into programs loaded into LLI.
6 //
7 // This file is compiled by the LLVM port of GCC to get LLVM code.
8 //
9 // A lot of this code is ripped gratuitously from glibc and libiberty.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include <stdlib.h>
14
15 // Prototypes for functions exported by LLI directly.
16 void exit(int Code);
17 int putchar(int);
18 void *malloc(unsigned);
19 void free(void *);
20
21 #define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n')
22 #define isdigit(x) ((x) >= '0' && (x) <= '9')
23 #define isupper(x) ((x) >= 'A' && (x) <= 'Z')
24 #define islower(x) ((x) >= 'a' && (x) <= 'z')
25 #define isalpha(x) (isupper(x) || islower(x))
26
27 // The puts() function writes the string pointed to by s, followed by a 
28 // NEWLINE character, to the standard output stream stdout. On success the 
29 // number of characters written is returned; otherwise they return EOF.
30 //
31 int puts(const char *S) {
32   const char *Str = S;
33   while (*Str) putchar(*Str++);
34   putchar('\n');
35   return Str+1-S;
36 }
37
38
39 #ifndef ULONG_MAX
40 #define ULONG_MAX       ((unsigned long)(~0L))          /* 0xFFFFFFFF */
41 #endif
42
43 #ifndef LONG_MAX
44 #define LONG_MAX        ((long)(ULONG_MAX >> 1))        /* 0x7FFFFFFF */
45 #endif
46
47 #ifndef LONG_MIN
48 #define LONG_MIN        ((long)(~LONG_MAX))             /* 0x80000000 */
49 #endif
50
51 /*
52  * Convert a string to a long integer.
53  *
54  * Ignores `locale' stuff.  Assumes that the upper and lower case
55  * alphabets and digits are each contiguous.
56  */
57 long strtol(const char *nptr, char **endptr, int base) {
58         register const char *s = nptr;
59         register unsigned long acc;
60         register int c;
61         register unsigned long cutoff;
62         register int neg = 0, any, cutlim;
63
64         /*
65          * Skip white space and pick up leading +/- sign if any.
66          * If base is 0, allow 0x for hex and 0 for octal, else
67          * assume decimal; if base is already 16, allow 0x.
68          */
69         do {
70                 c = *s++;
71         } while (isspace(c));
72         if (c == '-') {
73                 neg = 1;
74                 c = *s++;
75         } else if (c == '+')
76                 c = *s++;
77         if ((base == 0 || base == 16) &&
78             c == '0' && (*s == 'x' || *s == 'X')) {
79                 c = s[1];
80                 s += 2;
81                 base = 16;
82         }
83         if (base == 0)
84                 base = c == '0' ? 8 : 10;
85
86         /*
87          * Compute the cutoff value between legal numbers and illegal
88          * numbers.  That is the largest legal value, divided by the
89          * base.  An input number that is greater than this value, if
90          * followed by a legal input character, is too big.  One that
91          * is equal to this value may be valid or not; the limit
92          * between valid and invalid numbers is then based on the last
93          * digit.  For instance, if the range for longs is
94          * [-2147483648..2147483647] and the input base is 10,
95          * cutoff will be set to 214748364 and cutlim to either
96          * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
97          * a value > 214748364, or equal but the next digit is > 7 (or 8),
98          * the number is too big, and we will return a range error.
99          *
100          * Set any if any `digits' consumed; make it negative to indicate
101          * overflow.
102          */
103         cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
104         cutlim = cutoff % (unsigned long)base;
105         cutoff /= (unsigned long)base;
106         for (acc = 0, any = 0;; c = *s++) {
107                 if (isdigit(c))
108                         c -= '0';
109                 else if (isalpha(c))
110                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
111                 else
112                         break;
113                 if (c >= base)
114                         break;
115                 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
116                         any = -1;
117                 else {
118                         any = 1;
119                         acc *= base;
120                         acc += c;
121                 }
122         }
123         if (any < 0) {
124                 acc = neg ? LONG_MIN : LONG_MAX;
125         } else if (neg)
126                 acc = -acc;
127         if (endptr != 0)
128                 *endptr = (char *) (any ? s - 1 : nptr);
129         return (acc);
130 }
131
132
133 /* Convert a string to an int.  */
134 int atoi(const char *nptr) {
135   return (int)strtol(nptr, 0, 10);
136 }
137
138 /* Convert a string to a long int.  */
139 long int atol(const char *nptr) {
140   return strtol(nptr, 0, 10);
141 }
142
143
144 unsigned strlen(const char *Str) {
145   int Count = 0;
146   while (*Str) { ++Count; ++Str; }
147   return Count;
148 }
149
150 char *strdup(const char *str) {
151   int Len = strlen(str);
152   char *Result = (char*)malloc((Len+1)*sizeof(char));
153   memcpy(Result, str, Len+1);
154   return Result;
155 }
156
157
158 /* Compare S1 and S2, returning less than, equal to or
159    greater than zero if S1 is lexicographically less than,
160    equal to or greater than S2.  */
161 int strcmp (const char *p1, const char *p2) {
162   register const unsigned char *s1 = (const unsigned char *) p1;
163   register const unsigned char *s2 = (const unsigned char *) p2;
164   unsigned char c1, c2;
165
166   do
167     {
168       c1 = (unsigned char) *s1++;
169       c2 = (unsigned char) *s2++;
170       if (c1 == '\0')
171         return c1 - c2;
172     }
173   while (c1 == c2);
174
175   return c1 - c2;
176 }
177
178
179 //===----------------------------------------------------------------------===//
180 // memory stuff...
181 //===----------------------------------------------------------------------===//
182 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
183
184 typedef unsigned int op_t;
185 #define OPSIZ 4
186
187 void *memset (void *dstpp, int c, size_t len) {
188   long long int dstp = (long long int) dstpp;
189
190   if (len >= 8)
191     {
192       size_t xlen;
193       op_t cccc;
194
195       cccc = (unsigned char) c;
196       cccc |= cccc << 8;
197       cccc |= cccc << 16;
198       if (OPSIZ > 4)
199         /* Do the shift in two steps to avoid warning if long has 32 bits.  */
200         cccc |= (cccc << 16) << 16;
201
202       /* There are at least some bytes to set.
203          No need to test for LEN == 0 in this alignment loop.  */
204       while (dstp % OPSIZ != 0)
205         {
206           ((unsigned char *) dstp)[0] = c;
207           dstp += 1;
208           len -= 1;
209         }
210
211       /* Write 8 `op_t' per iteration until less than 8 `op_t' remain.  */
212       xlen = len / (OPSIZ * 8);
213       while (xlen > 0)
214         {
215           ((op_t *) dstp)[0] = cccc;
216           ((op_t *) dstp)[1] = cccc;
217           ((op_t *) dstp)[2] = cccc;
218           ((op_t *) dstp)[3] = cccc;
219           ((op_t *) dstp)[4] = cccc;
220           ((op_t *) dstp)[5] = cccc;
221           ((op_t *) dstp)[6] = cccc;
222           ((op_t *) dstp)[7] = cccc;
223           dstp += 8 * OPSIZ;
224           xlen -= 1;
225         }
226       len %= OPSIZ * 8;
227
228       /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain.  */
229       xlen = len / OPSIZ;
230       while (xlen > 0)
231         {
232           ((op_t *) dstp)[0] = cccc;
233           dstp += OPSIZ;
234           xlen -= 1;
235         }
236       len %= OPSIZ;
237     }
238
239   /* Write the last few bytes.  */
240   while (len > 0)
241     {
242       ((unsigned char *) dstp)[0] = c;
243       dstp += 1;
244       len -= 1;
245     }
246
247   return dstpp;
248 }
249
250 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
251   char *dstp = (char*)dstpp;
252   char *srcp = (char*) srcpp;
253   unsigned i;
254
255   for (i = 0; i < len; ++i)
256     dstp[i] = srcp[i];
257
258   return dstpp;
259 }
260
261 void *calloc(size_t nelem, size_t elsize) {
262   void *Result = malloc(nelem*elsize);
263   return memset(Result, 0, nelem*elsize);
264 }
265
266
267 //===----------------------------------------------------------------------===//
268 // libm stuff...
269 //===----------------------------------------------------------------------===//
270