1 //===-- RuntimeLib.lc - LLVM Standard C Runtime Library -----------*- C -*-===//
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.
7 // This file is compiled by the LLVM port of GCC to get LLVM code.
9 // A lot of this code is ripped gratuitously from glibc and libiberty.
11 //===----------------------------------------------------------------------===//
15 // Prototypes for functions exported by LLI directly.
18 void *malloc(unsigned);
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))
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.
31 int puts(const char *S) {
33 while (*Str) putchar(*Str++);
40 #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
44 #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
48 #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
52 * Convert a string to a long integer.
54 * Ignores `locale' stuff. Assumes that the upper and lower case
55 * alphabets and digits are each contiguous.
57 long strtol(const char *nptr, char **endptr, int base) {
58 register const char *s = nptr;
59 register unsigned long acc;
61 register unsigned long cutoff;
62 register int neg = 0, any, cutlim;
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.
77 if ((base == 0 || base == 16) &&
78 c == '0' && (*s == 'x' || *s == 'X')) {
84 base = c == '0' ? 8 : 10;
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.
100 * Set any if any `digits' consumed; make it negative to indicate
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++) {
110 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
115 if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
124 acc = neg ? LONG_MIN : LONG_MAX;
128 *endptr = (char *) (any ? s - 1 : nptr);
133 /* Convert a string to an int. */
134 int atoi(const char *nptr) {
135 return (int)strtol(nptr, 0, 10);
138 /* Convert a string to a long int. */
139 long int atol(const char *nptr) {
140 return strtol(nptr, 0, 10);
144 unsigned strlen(const char *Str) {
146 while (*Str) { ++Count; ++Str; }
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);
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;
168 c1 = (unsigned char) *s1++;
169 c2 = (unsigned char) *s2++;
179 //===----------------------------------------------------------------------===//
181 //===----------------------------------------------------------------------===//
182 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
184 typedef unsigned int op_t;
187 void *memset (void *dstpp, int c, size_t len) {
188 long long int dstp = (long long int) dstpp;
195 cccc = (unsigned char) c;
199 /* Do the shift in two steps to avoid warning if long has 32 bits. */
200 cccc |= (cccc << 16) << 16;
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)
206 ((unsigned char *) dstp)[0] = c;
211 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
212 xlen = len / (OPSIZ * 8);
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;
228 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
232 ((op_t *) dstp)[0] = cccc;
239 /* Write the last few bytes. */
242 ((unsigned char *) dstp)[0] = c;
250 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
251 char *dstp = (char*)dstpp;
252 char *srcp = (char*) srcpp;
255 for (i = 0; i < len; ++i)
261 void *calloc(size_t nelem, size_t elsize) {
262 void *Result = malloc(nelem*elsize);
263 return memset(Result, 0, nelem*elsize);
267 //===----------------------------------------------------------------------===//
269 //===----------------------------------------------------------------------===//