Skip to content

Commit 886e507

Browse files
committed
Refine built-in libc files and inliner tool
Since lib/c.h was introduced in the previous commit, both c.c and c.h contained duplicated definitions. This commit adjusts the files to remove the duplicated definitions by making c.c include c.h and reuse the necessary definitions, such as macros and forward declarations. The inliner tool is also enhanced to generate two functions, 'libc_decl()' and 'libc_impl()'. The compiler will use these functions to automatically generate the source of c.h and c.c for compiled programs when needed.
1 parent 5a8bb93 commit 886e507

File tree

5 files changed

+64
-62
lines changed

5 files changed

+64
-62
lines changed

Makefile

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ SRCDIR := $(shell find src -type d)
4444
LIBDIR := $(shell find lib -type d)
4545
LINK_MODES = static dynamic
4646

47-
BUILTIN_LIBC ?= c.c
47+
BUILTIN_LIBC_SOURCE ?= c.c
48+
BUILTIN_LIBC_HEADER := c.h
4849
STAGE0_FLAGS ?= --dump-ir
4950
STAGE1_FLAGS ?=
5051
ifeq ($(LINK_MODE),dynamic)
5152
ifeq ($(ARCH),riscv)
5253
# TODO: implement dynamic linking for RISC-V.
5354
$(error "Dynamic linking mode is not implemented for RISC-V")
5455
endif
55-
BUILTIN_LIBC := c.h
5656
STAGE0_FLAGS += --dynlink
5757
STAGE1_FLAGS += --dynlink
5858
endif
@@ -139,11 +139,12 @@ $(OUT)/norm-lf: tools/norm-lf.c
139139
$(VECHO) " CC+LD\t$@\n"
140140
$(Q)$(CC) $(CFLAGS) -o $@ $^
141141

142-
$(OUT)/libc.inc: $(OUT)/inliner $(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC)
142+
$(OUT)/libc.inc: $(OUT)/inliner $(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_SOURCE) $(LIBDIR)/$(BUILTIN_LIBC_HEADER)
143143
$(VECHO) " GEN\t$@\n"
144-
$(Q)$(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC) $(OUT)/c.normalized.c
145-
$(Q)$(OUT)/inliner $(OUT)/c.normalized.c $@
146-
$(Q)$(RM) $(OUT)/c.normalized.c
144+
$(Q)$(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_SOURCE) $(OUT)/c.normalized.c
145+
$(Q)$(OUT)/norm-lf $(LIBDIR)/$(BUILTIN_LIBC_HEADER) $(OUT)/c.normalized.h
146+
$(Q)$(OUT)/inliner $(OUT)/c.normalized.c $(OUT)/c.normalized.h $@
147+
$(Q)$(RM) $(OUT)/c.normalized.c $(OUT)/c.normalized.h
147148

148149
$(OUT)/inliner: tools/inliner.c
149150
$(VECHO) " CC+LD\t$@\n"

lib/c.c

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,9 @@
66
*/
77

88
/* minimal libc implementation */
9-
10-
#define NULL 0
11-
12-
#define bool _Bool
13-
#define true 1
14-
#define false 0
15-
16-
#define INT_MAX 0x7fffffff
17-
#define INT_MIN 0x80000000
18-
19-
#if defined(__arm__)
20-
#define __SIZEOF_POINTER__ 4
21-
#define __syscall_exit 1
22-
#define __syscall_read 3
23-
#define __syscall_write 4
24-
#define __syscall_close 6
25-
#define __syscall_open 5
26-
#define __syscall_mmap2 192
27-
#define __syscall_munmap 91
28-
29-
#elif defined(__riscv)
30-
#define __SIZEOF_POINTER__ 4
31-
#define __syscall_exit 93
32-
#define __syscall_read 63
33-
#define __syscall_write 64
34-
#define __syscall_close 57
35-
#define __syscall_open 1024
36-
#define __syscall_openat 56
37-
#define __syscall_mmap2 222
38-
#define __syscall_munmap 215
39-
40-
#else /* Only Arm32 and RV32 are supported */
41-
#error "Unsupported architecture"
42-
#endif
43-
9+
#include "c.h"
4410
#define INT_BUF_LEN 16
4511

46-
typedef int FILE;
47-
48-
/* va_list support for variadic functions */
49-
typedef int *va_list;
50-
51-
void abort(void);
52-
5312
int strlen(char *str)
5413
{
5514
/* process the string by checking 4 characters (a 32-bit word) at a time */
@@ -584,18 +543,11 @@ int fputc(int c, FILE *stream)
584543
return c;
585544
}
586545

587-
/* Non-portable: Assume page size is 4KiB */
588-
#define PAGESIZE 4096
589-
590546
#define CHUNK_SIZE_FREED_MASK 1
591547
#define CHUNK_SIZE_SZ_MASK 0xFFFFFFFE
592548
#define CHUNK_GET_SIZE(size) (size & CHUNK_SIZE_SZ_MASK)
593549
#define IS_CHUNK_GET_FREED(size) (size & CHUNK_SIZE_FREED_MASK)
594550

595-
/* Minimum alignment for all memory allocations. */
596-
#define MIN_ALIGNMENT 8
597-
#define ALIGN_UP(val, align) (((val) + (align) - 1) & ~((align) - 1))
598-
599551
typedef struct chunk {
600552
struct chunk *next, *prev;
601553
int size;

lib/c.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,41 @@
1414
#define true 1
1515
#define false 0
1616

17+
#define INT_MAX 0x7fffffff
18+
#define INT_MIN 0x80000000
19+
20+
#if defined(__arm__)
21+
#define __SIZEOF_POINTER__ 4
22+
#define __syscall_exit 1
23+
#define __syscall_read 3
24+
#define __syscall_write 4
25+
#define __syscall_close 6
26+
#define __syscall_open 5
27+
#define __syscall_mmap2 192
28+
#define __syscall_munmap 91
29+
30+
#elif defined(__riscv)
31+
#define __SIZEOF_POINTER__ 4
32+
#define __syscall_exit 93
33+
#define __syscall_read 63
34+
#define __syscall_write 64
35+
#define __syscall_close 57
36+
#define __syscall_open 1024
37+
#define __syscall_openat 56
38+
#define __syscall_mmap2 222
39+
#define __syscall_munmap 215
40+
41+
#else /* Only Arm32 and RV32 are supported */
42+
#error "Unsupported architecture"
43+
#endif
44+
45+
/* Non-portable: Assume page size is 4KiB */
46+
#define PAGESIZE 4096
47+
48+
/* Minimum alignment for all memory allocations. */
49+
#define MIN_ALIGNMENT 8
50+
#define ALIGN_UP(val, align) (((val) + (align) - 1) & ~((align) - 1))
51+
1752
/* va_list support for variadic functions */
1853
typedef int *va_list;
1954

src/main.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,11 @@ int main(int argc, char *argv[])
8686
global_init();
8787

8888
/* include libc */
89-
if (libc)
90-
libc_generate();
89+
if (libc) {
90+
libc_decl();
91+
if (!dynlink)
92+
libc_impl();
93+
}
9194

9295
/* load and parse source code into IR */
9396
parse(in);

tools/inliner.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,13 @@ void load_from(char *file)
138138
fclose(f);
139139
return;
140140
}
141-
write_line(buffer);
141+
142+
if (!strncmp(buffer, "#pragma once", 12))
143+
continue;
144+
else if (!strncmp(buffer, "#include \"c.h\"", 14))
145+
continue;
146+
else
147+
write_line(buffer);
142148
}
143149
fclose(f);
144150
}
@@ -153,8 +159,8 @@ void save_to(char *file)
153159

154160
int main(int argc, char *argv[])
155161
{
156-
if (argc <= 2) {
157-
printf("Usage: inliner <input.c> <output.inc>\n");
162+
if (argc <= 3) {
163+
printf("Usage: inliner <input.c> <input.h> <output.inc>\n");
158164
return -1;
159165
}
160166

@@ -176,10 +182,15 @@ int main(int argc, char *argv[])
176182
write_str(" strbuf_puts(SOURCE, src);\n");
177183
write_str("}\n");
178184

179-
write_str("void libc_generate() {\n");
185+
write_str("void libc_impl() {\n");
180186
load_from(argv[1]);
181187
write_str("}\n");
182-
save_to(argv[2]);
188+
189+
write_str("void libc_decl() {\n");
190+
load_from(argv[2]);
191+
write_str("}\n");
192+
193+
save_to(argv[3]);
183194
strbuf_free(SOURCE);
184195

185196
return 0;

0 commit comments

Comments
 (0)