scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 8c6c81f5f752c9829fc14197577e94ce6a84a2a6
parent 62018c6c313a4b831976d23b71ed0e017cade6a2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sat, 20 Aug 2022 20:04:32 +0200

Merge remote-tracking branch 'origin/master'

Diffstat:
Minclude/assert.h | 1+
Ainclude/bits/amd64/arch/float.h | 31+++++++++++++++++++++++++++++++
Minclude/bits/darwin/amd64/arch/stdint.h | 8++++++++
Minclude/bits/dragonfly/amd64/arch/stdint.h | 8++++++++
Minclude/bits/i386/arch/stdint.h | 8++++++++
Minclude/bits/linux/amd64/arch/stdint.h | 8++++++++
Minclude/bits/linux/arm/arch/stdint.h | 8++++++++
Minclude/bits/linux/arm64/arch/stdint.h | 8++++++++
Minclude/bits/linux/ppc/arch/stdint.h | 8++++++++
Minclude/bits/netbsd/amd64/arch/stdint.h | 8++++++++
Minclude/bits/openbsd/amd64/arch/stdint.h | 8++++++++
Minclude/bits/z80/arch/stdint.h | 8++++++++
Minclude/float.h | 2+-
Msrc/libc/assert/__assert.c | 5+++--
Msrc/libc/assert/assert.c | 3++-
Msrc/libc/ctype/isdigit.c | 1+
Msrc/libc/ctype/islower.c | 1+
Msrc/libc/ctype/isspace.c | 1+
Msrc/libc/ctype/toupper.c | 1+
Msrc/libc/stdio/fgets.c | 2+-
Msrc/libc/stdio/fputs.c | 4+++-
Msrc/libc/stdio/getc.c | 2+-
Msrc/libc/stdio/putc.c | 2+-
Msrc/libc/stdio/vfprintf.c | 30+++++++++++++++---------------
Msrc/libc/stdlib/_dtoi.c | 2++
Msrc/libc/stdlib/abs.c | 1+
Msrc/libc/stdlib/calloc.c | 1+
Msrc/libc/stdlib/labs.c | 1+
Msrc/libc/stdlib/llabs.c | 1+
Msrc/libc/stdlib/malloc.c | 4++--
Msrc/libc/stdlib/rand.c | 12+++++++++---
31 files changed, 160 insertions(+), 28 deletions(-)

diff --git a/include/assert.h b/include/assert.h @@ -1,4 +1,5 @@ extern void __assert(char *, char *, long); +extern void assert(int); #undef assert #ifndef NDEBUG diff --git a/include/bits/amd64/arch/float.h b/include/bits/amd64/arch/float.h @@ -0,0 +1,31 @@ +#define FLT_RADIX 2 +#define FLT_MANT_DIG 24 +#define FLT_DIG 6 +#define FLT_ROUNDS 1 +#define FLT_EPSILON 1.19209290e-07F +#define FLT_MIN_EXP (-125) +#define FLT_MIN 1.17549435e-38F +#define FLT_MIN_10_EXP (-37) +#define FLT_MAX_EXP 128 +#define FLT_MAX 3.40282347e+38F +#define FLT_MAX_10_EXP 38 + +#define DBL_MANT_DIG 53 +#define DBL_DIG 15 +#define DBL_EPSILON 2.2204460492503131e-16 +#define DBL_MIN_EXP (-1021) +#define DBL_MIN 2.2250738585072014e-308 +#define DBL_MIN_10_EXP (-307) +#define DBL_MAX_EXP 1024 +#define DBL_MAX 1.7976931348623157e+308 +#define DBL_MAX_10_EXP 308 + +#define LDBL_MANT_DIG 64 +#define LDBL_DIG 18 +#define LDBL_EPSILON 1.08420217248550443401e-19L +#define LDBL_MIN_EXP (-16381) +#define LDBL_MIN 3.36210314311209350626e-4932L +#define LDBL_MIN_10_EXP (-4931) +#define LDBL_MAX_EXP 16384 +#define LDBL_MAX 1.18973149535723176502e+4932L +#define LDBL_MAX_10_EXP 4932 diff --git a/include/bits/darwin/amd64/arch/stdint.h b/include/bits/darwin/amd64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/dragonfly/amd64/arch/stdint.h b/include/bits/dragonfly/amd64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/i386/arch/stdint.h b/include/bits/i386/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/linux/amd64/arch/stdint.h b/include/bits/linux/amd64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/linux/arm/arch/stdint.h b/include/bits/linux/arm/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX - 1) #define UINT8_MAX 0xFF diff --git a/include/bits/linux/arm64/arch/stdint.h b/include/bits/linux/arm64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX - 1) #define UINT8_MAX 0xFFU diff --git a/include/bits/linux/ppc/arch/stdint.h b/include/bits/linux/ppc/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX - 1) #define UINT8_MAX 0xFF diff --git a/include/bits/netbsd/amd64/arch/stdint.h b/include/bits/netbsd/amd64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/openbsd/amd64/arch/stdint.h b/include/bits/openbsd/amd64/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## U + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/bits/z80/arch/stdint.h b/include/bits/z80/arch/stdint.h @@ -1,3 +1,11 @@ +#define INT8_C(c) c +#define INT16_C(c) c +#define INT32_C(c) c ## L + +#define UINT8_C(c) c +#define UINT16_C(c) c +#define UINT32_C(c) c ## UL + #define INT8_MAX 0x7F #define INT8_MIN (-INT8_MAX-1) #define UINT8_MAX 0xFF diff --git a/include/float.h b/include/float.h @@ -1,6 +1,6 @@ #ifndef _FLOAT_H #define _FLOAT_H -#error float.h is not supported yet +#include <arch/float.h> #endif diff --git a/src/libc/assert/__assert.c b/src/libc/assert/__assert.c @@ -1,8 +1,9 @@ #include <assert.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> -void __assert(char *exp, char *file, long line) +void +__assert(char *exp, char *file, long line) { fprintf(stderr, "%s:%ld: assertion failed '%s'\n", file, line, exp); abort(); diff --git a/src/libc/assert/assert.c b/src/libc/assert/assert.c @@ -1,6 +1,7 @@ #include <assert.h> -#include <stdlib.h> #include <stdio.h> +#include <stdlib.h> + #undef assert void diff --git a/src/libc/ctype/isdigit.c b/src/libc/ctype/isdigit.c @@ -1,4 +1,5 @@ #include <ctype.h> + #undef isdigit int diff --git a/src/libc/ctype/islower.c b/src/libc/ctype/islower.c @@ -1,4 +1,5 @@ #include <ctype.h> + #undef islower int diff --git a/src/libc/ctype/isspace.c b/src/libc/ctype/isspace.c @@ -1,4 +1,5 @@ #include <ctype.h> + #undef isspace int diff --git a/src/libc/ctype/toupper.c b/src/libc/ctype/toupper.c @@ -1,4 +1,5 @@ #include <ctype.h> + #undef toupper int diff --git a/src/libc/stdio/fgets.c b/src/libc/stdio/fgets.c @@ -11,7 +11,7 @@ fgets(char *restrict s, int n, FILE *restrict fp) for (--n; n > 0; --n) { if ((ch = getc(fp)) == EOF) break; - if ((*t++ = ch) == '\n') + if ((*t++ = ch) == '\n') break; } if (ch == EOF && s == t) diff --git a/src/libc/stdio/fputs.c b/src/libc/stdio/fputs.c @@ -3,10 +3,12 @@ #undef fputs int -fputs(const char * restrict bp, FILE * restrict fp) +fputs(const char *restrict bp, FILE *restrict fp) { int r, ch; + r = 0; + while ((ch = *bp++) != '\0') r = putc(ch, fp); diff --git a/src/libc/stdio/getc.c b/src/libc/stdio/getc.c @@ -5,5 +5,5 @@ int getc(FILE *fp) { - return (fp->rp >= fp->wp) ? __getc(fp) : *fp->rp++; + return (fp->rp >= fp->wp) ? __getc(fp) : *fp->rp++; } diff --git a/src/libc/stdio/putc.c b/src/libc/stdio/putc.c @@ -5,5 +5,5 @@ int putc(int ch, FILE *fp) { - return (fp->wp >= fp->rp) ? __putc(ch,fp) : (*fp->wp++ = ch); + return (fp->wp >= fp->rp) ? __putc(ch, fp) : (*fp->wp++ = ch); } diff --git a/src/libc/stdio/vfprintf.c b/src/libc/stdio/vfprintf.c @@ -151,15 +151,16 @@ static size_t strout(char *s, size_t len, int width, int fill, FILE *restrict fp) { int left = 0, adjust, ch, prefix; - size_t cnt = 0; + size_t cnt; if (width < 0) { left = 1; width = -width; } - adjust = (len > 0 && len < width) ? width - len : 0; + adjust = len < width ? width - len : 0; cnt = adjust + len; + if (left) adjust = -adjust; @@ -179,13 +180,8 @@ strout(char *s, size_t len, int width, int fill, FILE *restrict fp) for ( ; adjust > 0; adjust--) putc(fill, fp); - if (len == -1) { - for (cnt = 0; ch = *s++; ++cnt) - putc(ch, fp); - } else { - while (len-- > 0 && (ch = *s++)) - putc(ch, fp); - } + for ( ; len-- > 0 && (ch = *s) != '\0'; ++s) + putc(ch, fp); for ( ; adjust < 0; adjust++) putc(' ', fp); @@ -240,11 +236,8 @@ flags: for (n = 0; isdigit(ch = *fmt); fmt++) n = n * 10 + ch - '0'; } - if (n < 0) - n = 0; - if (n > MAXPREC) - n = MAXPREC; - conv.prec = n; + if (n >= 0) + conv.prec = n; goto flags; case '*': width = va_arg(va2, int); @@ -323,8 +316,11 @@ flags: conv.base = 8; flags |= UNSIGNED; numeric: - if (conv.prec != -1) + if (conv.prec != -1) { + if (conv.prec > MAXPREC) + conv.prec = MAXPREC; fill = ' '; + } s = numtostr(getnum(&va2, flags, &conv.sign), flags, &conv, @@ -347,6 +343,10 @@ flags: /* len = wcsnlen(ws, conv.prec); */ goto wstrout; } else { + if (conv.prec != -1) + len = conv.prec; + else + len = SIZE_MAX; s = va_arg(va2, char *); if ((len = strlen(s)) > conv.prec) len = conv.prec; diff --git a/src/libc/stdlib/_dtoi.c b/src/libc/stdlib/_dtoi.c @@ -2,6 +2,8 @@ #include <limits.h> #include <string.h> +#include "../libc.h" + int _dtoi(char c) { diff --git a/src/libc/stdlib/abs.c b/src/libc/stdlib/abs.c @@ -1,4 +1,5 @@ #include <stdlib.h> + #undef abs int diff --git a/src/libc/stdlib/calloc.c b/src/libc/stdlib/calloc.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <string.h> + #undef calloc void * diff --git a/src/libc/stdlib/labs.c b/src/libc/stdlib/labs.c @@ -1,4 +1,5 @@ #include <stdlib.h> + #undef labs long diff --git a/src/libc/stdlib/llabs.c b/src/libc/stdlib/llabs.c @@ -1,4 +1,5 @@ #include <stdlib.h> + #undef llabs long long diff --git a/src/libc/stdlib/malloc.c b/src/libc/stdlib/malloc.c @@ -117,11 +117,11 @@ morecore(size_t nunits) /* * Run over the list of free blocks trying to find a block - * big enough for nbytes. If the block fit perfectly with + * big enough for nbytes. If the block fits perfectly with * the required size then we only have to unlink * the block. Otherwise we have to split the block and * return the right part. If we run over the full list - * without a fit then we have to require more memory + * without a fit then we have to acquire more memory * * ______________________________________ * ___________./______________________________________\_____ diff --git a/src/libc/stdlib/rand.c b/src/libc/stdlib/rand.c @@ -1,6 +1,7 @@ #include <stdlib.h> -#undef rand + #undef srand +#undef rand static unsigned long next; @@ -11,8 +12,13 @@ srand(unsigned seed) } int -rand(void) /* RAND_MAX assumed to be 32767. */ +rand(void) { + /* + * next is just an arbitrary number + * used to generate a random number + * which is <= RAND_MAX + */ next = next * 1103515245 + 12345; - return (unsigned)(next/65536) % 32768; + return (unsigned)(next/65536) % (RAND_MAX + 1); }