Skip to content

Commit 48ef5d1

Browse files
committed
Logic cleanup & add preprocessing CI test
1 parent 76284bb commit 48ef5d1

File tree

4 files changed

+47
-15
lines changed

4 files changed

+47
-15
lines changed

.github/workflows/main.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,44 @@ jobs:
5656
make check-sanitizer || exit 1
5757
make check || exit 1
5858
59+
preprocessor-host:
60+
runs-on: ubuntu-24.04
61+
strategy:
62+
matrix:
63+
compiler: [gcc, clang]
64+
architecture: [arm, riscv]
65+
steps:
66+
- name: Checkout code
67+
uses: actions/checkout@v4
68+
- name: Download dependencies
69+
run: |
70+
sudo apt-get update -q -y
71+
sudo apt-get install -q -y graphviz jq
72+
sudo apt-get install -q -y qemu-user
73+
sudo apt-get install -q -y build-essential
74+
- name: Configurate config
75+
env:
76+
CC: ${{ matrix.compiler }}
77+
run: |
78+
make distclean config ARCH=${{ matrix.architecture }}
79+
- name: Preprocess stage 1 source code
80+
run: |
81+
make out/shecc
82+
./out/shecc -E src/main.c > ./out/out.c
83+
- name: Build stage 1 artifact
84+
run: |
85+
./out/shecc --no-libc -o out/shecc-stage1.elf ./out/out.c
86+
- name: Preprocess stage 2 source code
87+
run: |
88+
make out/shecc
89+
./out/shecc-stage1.elf -E src/main.c > ./out/out.c
90+
- name: Build stage 2 artifact
91+
run: |
92+
./out/shecc-stage1.elf --no-libc -o out/shecc-stage2.elf ./out/out.c
93+
- name: Test stage 2 artifact
94+
run: |
95+
make check-stage2 || exit 1
96+
5997
coding-style:
6098
runs-on: ubuntu-24.04
6199
steps:

src/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#define SMALL_ARENA_SIZE 65536 /* 64 KiB - for small allocations */
5757
#define LARGE_ARENA_SIZE 524288 /* 512 KiB - for instruction arena */
5858
#define DEFAULT_FUNCS_SIZE 64
59+
#define DEFAULT_SRC_FILE_COUNT 8
5960

6061
/* Arena compaction bitmask flags for selective memory reclamation */
6162
#define COMPACT_ARENA_BLOCK 0x01 /* BLOCK_ARENA - variables/blocks */

src/globals.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ char *intern_string(char *str);
1818

1919
/* Lexer */
2020
token_t *cur_token;
21+
/* TOKEN_CACHE maps filename to the corresponding computed token stream */
22+
hashmap_t *TOKEN_CACHE;
23+
strbuf_t *LIBC_SRC;
2124

2225
/* Global objects */
2326

@@ -71,8 +74,6 @@ int elf_offset = 0;
7174

7275
regfile_t REGS[REG_CNT];
7376

74-
strbuf_t *SOURCE;
75-
7677
hashmap_t *INCLUSION_MAP;
7778

7879
/* ELF sections */
@@ -1223,7 +1224,8 @@ void global_init(void)
12231224
arena_alloc(GENERAL_ARENA, sizeof(string_literal_pool_t));
12241225
string_literal_pool->literals = hashmap_create(256);
12251226

1226-
SRC_FILE_MAP = hashmap_create(8);
1227+
TOKEN_CACHE = hashmap_create(DEFAULT_SRC_FILE_COUNT);
1228+
SRC_FILE_MAP = hashmap_create(DEFAULT_SRC_FILE_COUNT);
12271229
FUNC_MAP = hashmap_create(DEFAULT_FUNCS_SIZE);
12281230
CONSTANTS_MAP = hashmap_create(MAX_CONSTANTS);
12291231

@@ -1351,7 +1353,7 @@ void global_release(void)
13511353
arena_free(TOKEN_ARENA);
13521354
arena_free(GENERAL_ARENA); /* free TYPES and PH2_IR_FLATTEN */
13531355

1354-
strbuf_free(SOURCE);
1356+
strbuf_free(LIBC_SRC);
13551357
strbuf_free(elf_code);
13561358
strbuf_free(elf_data);
13571359
strbuf_free(elf_rodata);
@@ -1360,6 +1362,7 @@ void global_release(void)
13601362
strbuf_free(elf_strtab);
13611363
strbuf_free(elf_section);
13621364

1365+
hashmap_free(TOKEN_CACHE);
13631366
hashmap_free(SRC_FILE_MAP);
13641367
hashmap_free(FUNC_MAP);
13651368
hashmap_free(CONSTANTS_MAP);
@@ -1694,7 +1697,7 @@ void error_at(char *msg, source_location_t *loc)
16941697
/* FIXME: This function has been deprecated, use `error_at` instead. */
16951698
void error(char *msg)
16961699
{
1697-
printf("[Error]: %s\nOccurs at source location %d.\n", msg, SOURCE->size);
1700+
printf("[Error]: %s.\n");
16981701
abort();
16991702
}
17001703

src/lexer.c

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ hashmap_t *KEYWORD_MAP = NULL;
2727
token_kind_t *directive_tokens_storage = NULL;
2828
token_kind_t *keyword_tokens_storage = NULL;
2929

30-
/* TOKEN_CACHE maps filename to the corresponding computed token stream */
31-
hashmap_t *TOKEN_CACHE = NULL;
32-
strbuf_t *LIBC_SRC;
33-
3430
void lex_init_directives()
3531
{
3632
if (DIRECTIVE_MAP)
@@ -934,9 +930,6 @@ token_stream_t *lex_token_by_file(char *filename)
934930
source_location_t loc = {0, 1, 1, 1, filename};
935931
strbuf_t *buf;
936932

937-
if (!TOKEN_CACHE)
938-
TOKEN_CACHE = hashmap_create(8);
939-
940933
tks = hashmap_get(TOKEN_CACHE, filename);
941934

942935
/* Already cached, just return the computed token stream */
@@ -1000,9 +993,6 @@ token_stream_t *include_libc()
1000993
if (tks)
1001994
return tks;
1002995

1003-
if (!TOKEN_CACHE)
1004-
TOKEN_CACHE = hashmap_create(8);
1005-
1006996
if (!hashmap_contains(SRC_FILE_MAP, filename))
1007997
hashmap_put(SRC_FILE_MAP, filename, LIBC_SRC);
1008998

0 commit comments

Comments
 (0)