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 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
147 // http://sources.redhat.com/cgi-bin/cvsweb.cgi/libc/sysdeps/generic/?cvsroot=glibc
149 typedef unsigned int op_t;
152 void *memset (void *dstpp, int c, size_t len) {
153 long long int dstp = (long long int) dstpp;
160 cccc = (unsigned char) c;
164 /* Do the shift in two steps to avoid warning if long has 32 bits. */
165 cccc |= (cccc << 16) << 16;
167 /* There are at least some bytes to set.
168 No need to test for LEN == 0 in this alignment loop. */
169 while (dstp % OPSIZ != 0)
171 ((unsigned char *) dstp)[0] = c;
176 /* Write 8 `op_t' per iteration until less than 8 `op_t' remain. */
177 xlen = len / (OPSIZ * 8);
180 ((op_t *) dstp)[0] = cccc;
181 ((op_t *) dstp)[1] = cccc;
182 ((op_t *) dstp)[2] = cccc;
183 ((op_t *) dstp)[3] = cccc;
184 ((op_t *) dstp)[4] = cccc;
185 ((op_t *) dstp)[5] = cccc;
186 ((op_t *) dstp)[6] = cccc;
187 ((op_t *) dstp)[7] = cccc;
193 /* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
197 ((op_t *) dstp)[0] = cccc;
204 /* Write the last few bytes. */
207 ((unsigned char *) dstp)[0] = c;
215 void *memcpy(void *dstpp, const void *srcpp, size_t len) {
216 char *dstp = (char*)dstpp;
217 char *srcp = (char*) srcpp;
220 for (i = 0; i < len; ++i)
226 void *calloc(size_t nelem, size_t elsize) {
227 void *Result = malloc(nelem*elsize);
228 return memset(Result, 0, nelem*elsize);
232 //===----------------------------------------------------------------------===//
234 //===----------------------------------------------------------------------===//