Skip to content

Commit

Permalink
Add stdarg.h, stdbool.h, stddef.h, stdalign.h and float.h
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent 7746e4e commit 7cbfd11
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ stage2/%.o: chibicc self.py %.c

stage2/test/%.exe: stage2/chibicc test/%.c
mkdir -p stage2/test
./stage2/chibicc -Itest -c -o stage2/test/$*.o test/$*.c
./stage2/chibicc -Iinclude -Itest -c -o stage2/test/$*.o test/$*.c
$(CC) -o $@ stage2/test/$*.o -xc test/common

test-stage2: $(TESTS:test/%=stage2/test/%)
Expand Down
42 changes: 42 additions & 0 deletions include/float.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef __STDFLOAT_H
#define __STDFLOAT_H

#define DECIMAL_DIG 21
#define FLT_EVAL_METHOD 0 // C11 5.2.4.2.2p9
#define FLT_RADIX 2
#define FLT_ROUNDS 1 // C11 5.2.4.2.2p8: to nearest

#define FLT_DIG 6
#define FLT_EPSILON 0x1p-23
#define FLT_MANT_DIG 24
#define FLT_MAX 0x1.fffffep+127
#define FLT_MAX_10_EXP 38
#define FLT_MAX_EXP 128
#define FLT_MIN 0x1p-126
#define FLT_MIN_10_EXP -37
#define FLT_MIN_EXP -125
#define FLT_TRUE_MIN 0x1p-149

#define DBL_DIG 15
#define DBL_EPSILON 0x1p-52
#define DBL_MANT_DIG 53
#define DBL_MAX 0x1.fffffffffffffp+1023
#define DBL_MAX_10_EXP 308
#define DBL_MAX_EXP 1024
#define DBL_MIN 0x1p-1022
#define DBL_MIN_10_EXP -307
#define DBL_MIN_EXP -1021
#define DBL_TRUE_MIN 0x0.0000000000001p-1022

#define LDBL_DIG 15
#define LDBL_EPSILON 0x1p-52
#define LDBL_MANT_DIG 53
#define LDBL_MAX 0x1.fffffffffffffp+1023
#define LDBL_MAX_10_EXP 308
#define LDBL_MAX_EXP 1024
#define LDBL_MIN 0x1p-1022
#define LDBL_MIN_10_EXP -307
#define LDBL_MIN_EXP -1021
#define LDBL_TRUE_MIN 0x0.0000000000001p-1022

#endif
9 changes: 9 additions & 0 deletions include/stdalign.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __STDALIGN_H
#define __STDALIGN_H

#define alignas _Alignas
#define alignof _Alignof
#define __alignas_is_defined 1
#define __alignof_is_defined 1

#endif
21 changes: 21 additions & 0 deletions include/stdarg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __STDARG_H
#define __STDARG_H

typedef struct {
unsigned int gp_offset;
unsigned int fp_offset;
void *overflow_arg_area;
void *reg_save_area;
} __va_elem;

typedef __va_elem va_list[1];

#define va_start(ap, last) \
do { *(ap) = *(__va_elem *)__va_area__; } while (0)

#define va_end(ap)

#define __GNUC_VA_LIST 1
typedef va_list __gnuc_va_list;

#endif
9 changes: 9 additions & 0 deletions include/stdbool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef __STDBOOL_H
#define __STDBOOL_H

#define bool _Bool
#define true 1
#define false 0
#define __bool_true_false_are_defined 1

#endif
11 changes: 11 additions & 0 deletions include/stddef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef __STDDEF_H
#define __STDDEF_H

#define NULL ((void *)0)

typedef unsigned long size_t;
typedef long ptrdiff_t;
typedef unsigned int wchar_t;
typedef long max_align_t;

#endif
6 changes: 6 additions & 0 deletions include/stdnoreturn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __STDNORETURN_H
#define __STDNORETURN_H

#define noreturn _Noreturn

#endif
10 changes: 10 additions & 0 deletions self.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@
int stat(char *pathname, struct stat *statbuf);
int stat(char *pathname, struct stat *statbuf);
char *dirname(char *path);
char *basename(char *path);
char *strrchr(char *s, int c);
int unlink(char *pathname);
int mkstemp(char *template);
int close(int fd);
int fork(void);
int execvp(char *file, char **argv);
void _exit(int code);
int wait(int *wstatus);
int atexit(void (*)(void));
""")

for path in sys.argv[1:]:
Expand Down
12 changes: 12 additions & 0 deletions test/stdhdr.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "test.h"
#include <float.h>
#include <stdalign.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdnoreturn.h>

int main() {
printf("OK\n");
return 0;
}

0 comments on commit 7cbfd11

Please sign in to comment.