Skip to content

Commit 72e5979

Browse files
AW-AlanWuicgmilk
andcommitted
Replace SOURCE with dynarr_t
Replaces the "strbuf_t *SOURCE" in "src/global.c" with "dynarr_t *". Co-authored-by: Jim Hsu <[email protected]>
1 parent 1f03cbc commit 72e5979

File tree

6 files changed

+56
-50
lines changed

6 files changed

+56
-50
lines changed

src/globals.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ arena_t *BLOCK_ARENA;
5858
/* BB_ARENA is responsible for basic_block_t / ph2_ir_t allocation */
5959
arena_t *BB_ARENA;
6060

61+
arena_t *SOURCE_ARENA;
62+
6163
int bb_label_idx = 0;
6264

6365
ph2_ir_t **PH2_IR_FLATTEN;
@@ -71,7 +73,8 @@ int elf_offset = 0;
7173

7274
regfile_t REGS[REG_CNT];
7375

74-
strbuf_t *SOURCE;
76+
dynarr_t *SOURCE;
77+
int source_idx = 0;
7578

7679
hashmap_t *INCLUSION_MAP;
7780

@@ -1177,10 +1180,14 @@ char dynarr_get_byte(dynarr_t *arr, int index)
11771180
printf("dynarr_get_byte: elem_size must be 1\n");
11781181
abort();
11791182
}
1183+
/* FIXME: shecc currently has Out-of-Bounds access on SOURCE bytes,
1184+
* so index check may be invalid */
1185+
/*
11801186
if (index < 0 || index >= arr->size) {
11811187
printf("index %d out of bounds (size=%d)\n", index, arr->size);
11821188
abort();
11831189
}
1190+
*/
11841191
char *ptr = arr->elements;
11851192
return ptr[index];
11861193
}
@@ -1310,8 +1317,9 @@ void global_init(void)
13101317
BLOCK_ARENA = arena_init(DEFAULT_ARENA_SIZE);
13111318
INSN_ARENA = arena_init(DEFAULT_ARENA_SIZE);
13121319
BB_ARENA = arena_init(DEFAULT_ARENA_SIZE);
1320+
SOURCE_ARENA = arena_init(MAX_SOURCE);
13131321
PH2_IR_FLATTEN = malloc(MAX_IR_INSTR * sizeof(ph2_ir_t *));
1314-
SOURCE = strbuf_create(MAX_SOURCE);
1322+
SOURCE = dynarr_init(SOURCE_ARENA, MAX_SOURCE, sizeof(char));
13151323
FUNC_MAP = hashmap_create(DEFAULT_FUNCS_SIZE);
13161324
INCLUSION_MAP = hashmap_create(DEFAULT_INCLUSIONS_SIZE);
13171325
ALIASES_MAP = hashmap_create(MAX_ALIASES);
@@ -1332,8 +1340,8 @@ void global_release(void)
13321340
arena_free(BLOCK_ARENA);
13331341
arena_free(INSN_ARENA);
13341342
arena_free(BB_ARENA);
1343+
arena_free(SOURCE_ARENA);
13351344
free(PH2_IR_FLATTEN);
1336-
strbuf_free(SOURCE);
13371345
hashmap_free(FUNC_MAP);
13381346
hashmap_free(INCLUSION_MAP);
13391347
hashmap_free(ALIASES_MAP);
@@ -1363,20 +1371,20 @@ void error(char *msg)
13631371
int offset, start_idx, i = 0;
13641372
char diagnostic[512 /* MAX_LINE_LEN * 2 */];
13651373

1366-
for (offset = SOURCE->size; offset >= 0 && SOURCE->elements[offset] != '\n';
1367-
offset--)
1374+
for (offset = source_idx;
1375+
offset >= 0 && dynarr_get_byte(SOURCE, offset) != '\n'; offset--)
13681376
;
13691377

13701378
start_idx = offset + 1;
13711379

1372-
for (offset = 0;
1373-
offset < MAX_SOURCE && SOURCE->elements[start_idx + offset] != '\n';
1380+
for (offset = 0; offset < MAX_SOURCE &&
1381+
dynarr_get_byte(SOURCE, start_idx + offset) != '\n';
13741382
offset++) {
1375-
diagnostic[i++] = SOURCE->elements[start_idx + offset];
1383+
diagnostic[i++] = dynarr_get_byte(SOURCE, start_idx + offset);
13761384
}
13771385
diagnostic[i++] = '\n';
13781386

1379-
for (offset = start_idx; offset < SOURCE->size; offset++) {
1387+
for (offset = start_idx; offset < source_idx; offset++) {
13801388
diagnostic[i++] = ' ';
13811389
}
13821390

@@ -1385,8 +1393,8 @@ void error(char *msg)
13851393
/* TODO: figure out the corresponding C source file path and report line
13861394
* number.
13871395
*/
1388-
printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg,
1389-
SOURCE->size, diagnostic);
1396+
printf("[Error]: %s\nOccurs at source location %d.\n%s\n", msg, source_idx,
1397+
diagnostic);
13901398
abort();
13911399
}
13921400

src/lexer.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@ void skip_whitespace(void)
6767
{
6868
while (true) {
6969
if (is_linebreak(next_char)) {
70-
SOURCE->size += 2;
71-
next_char = SOURCE->elements[SOURCE->size];
70+
source_idx += 2;
71+
next_char = dynarr_get_byte(SOURCE, source_idx);
7272
continue;
7373
}
7474
if (is_whitespace(next_char) ||
7575
(skip_newline && is_newline(next_char))) {
76-
SOURCE->size++;
77-
next_char = SOURCE->elements[SOURCE->size];
76+
next_char = dynarr_get_byte(SOURCE, ++source_idx);
7877
continue;
7978
}
8079
break;
@@ -83,16 +82,15 @@ void skip_whitespace(void)
8382

8483
char read_char(bool is_skip_space)
8584
{
86-
SOURCE->size++;
87-
next_char = SOURCE->elements[SOURCE->size];
85+
next_char = dynarr_get_byte(SOURCE, ++source_idx);
8886
if (is_skip_space)
8987
skip_whitespace();
9088
return next_char;
9189
}
9290

9391
char peek_char(int offset)
9492
{
95-
return SOURCE->elements[SOURCE->size + offset];
93+
return dynarr_get_byte(SOURCE, source_idx + offset);
9694
}
9795

9896
/* Lex next token and returns its token type. Parameter 'aliasing' is used for
@@ -514,8 +512,8 @@ token_t lex_token_internal(bool aliasing)
514512
*/
515513
if (next_char == '\n') {
516514
if (macro_return_idx) {
517-
SOURCE->size = macro_return_idx;
518-
next_char = SOURCE->elements[SOURCE->size];
515+
source_idx = macro_return_idx;
516+
next_char = dynarr_get_byte(SOURCE, source_idx);
519517
} else
520518
next_char = read_char(true);
521519
return lex_token_internal(aliasing);

src/parser.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ bool read_preproc_directive(void)
539539
if (lex_accept(T_elipsis))
540540
macro->is_variadic = true;
541541

542-
macro->start_source_idx = SOURCE->size;
542+
macro->start_source_idx = source_idx;
543543
skip_macro_body();
544544
} else {
545545
/* Empty alias, may be dummy alias serves as include guard */
@@ -1025,8 +1025,8 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
10251025
macro_t *mac = find_macro(token);
10261026

10271027
if (!strcmp(token, "__VA_ARGS__")) {
1028-
/* 'size' has pointed at the character after __VA_ARGS__ */
1029-
int remainder, t = SOURCE->size;
1028+
/* 'source_idx' has pointed at the character after __VA_ARGS__ */
1029+
int remainder, t = source_idx;
10301030
macro_t *macro = parent->macro;
10311031

10321032
if (!macro)
@@ -1036,13 +1036,13 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
10361036

10371037
remainder = macro->num_params - macro->num_param_defs;
10381038
for (int i = 0; i < remainder; i++) {
1039-
SOURCE->size = macro->params[macro->num_params - remainder + i];
1040-
next_char = SOURCE->elements[SOURCE->size];
1039+
source_idx = macro->params[macro->num_params - remainder + i];
1040+
next_char = dynarr_get_byte(SOURCE, source_idx);
10411041
next_token = lex_token();
10421042
read_expr(parent, bb);
10431043
}
1044-
SOURCE->size = t;
1045-
next_char = SOURCE->elements[SOURCE->size];
1044+
source_idx = t;
1045+
next_char = dynarr_get_byte(SOURCE, source_idx);
10461046
next_token = lex_token();
10471047
} else if (mac) {
10481048
if (parent->macro)
@@ -1052,18 +1052,18 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
10521052
mac->num_params = 0;
10531053
lex_expect(T_identifier);
10541054

1055-
/* 'size' has pointed at the first parameter */
1055+
/* 'source_idx' has pointed at the first parameter */
10561056
while (!lex_peek(T_close_bracket, NULL)) {
1057-
mac->params[mac->num_params++] = SOURCE->size;
1057+
mac->params[mac->num_params++] = source_idx;
10581058
do {
10591059
next_token = lex_token();
10601060
} while (next_token != T_comma &&
10611061
next_token != T_close_bracket);
10621062
}
1063-
/* move 'size' to the macro body */
1064-
macro_return_idx = SOURCE->size;
1065-
SOURCE->size = mac->start_source_idx;
1066-
next_char = SOURCE->elements[SOURCE->size];
1063+
/* move 'source_idx' to the macro body */
1064+
macro_return_idx = source_idx;
1065+
source_idx = mac->start_source_idx;
1066+
next_char = dynarr_get_byte(SOURCE, source_idx);
10671067
lex_expect(T_close_bracket);
10681068

10691069
skip_newline = 0;
@@ -1075,13 +1075,13 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
10751075
macro_return_idx = 0;
10761076
} else if (macro_param_idx) {
10771077
/* "expand" the argument from where it comes from */
1078-
int t = SOURCE->size;
1079-
SOURCE->size = macro_param_idx;
1080-
next_char = SOURCE->elements[SOURCE->size];
1078+
int t = source_idx;
1079+
source_idx = macro_param_idx;
1080+
next_char = dynarr_get_byte(SOURCE, source_idx);
10811081
next_token = lex_token();
10821082
read_expr(parent, bb);
1083-
SOURCE->size = t;
1084-
next_char = SOURCE->elements[SOURCE->size];
1083+
source_idx = t;
1084+
next_char = dynarr_get_byte(SOURCE, source_idx);
10851085
next_token = lex_token();
10861086
} else if (con) {
10871087
vd = require_var(parent);
@@ -2716,17 +2716,17 @@ basic_block_t *read_body_statement(block_t *parent, basic_block_t *bb)
27162716
mac->num_params = 0;
27172717
lex_expect(T_identifier);
27182718

2719-
/* 'size' has pointed at the first parameter */
2719+
/* 'source_idx' has pointed at the first parameter */
27202720
while (!lex_peek(T_close_bracket, NULL)) {
2721-
mac->params[mac->num_params++] = SOURCE->size;
2721+
mac->params[mac->num_params++] = source_idx;
27222722
do {
27232723
next_token = lex_token();
27242724
} while (next_token != T_comma && next_token != T_close_bracket);
27252725
}
2726-
/* move 'size' to the macro body */
2727-
macro_return_idx = SOURCE->size;
2728-
SOURCE->size = mac->start_source_idx;
2729-
next_char = SOURCE->elements[SOURCE->size];
2726+
/* move 'source_idx' to the macro body */
2727+
macro_return_idx = source_idx;
2728+
source_idx = mac->start_source_idx;
2729+
next_char = dynarr_get_byte(SOURCE, source_idx);
27302730
lex_expect(T_close_bracket);
27312731

27322732
skip_newline = 0;
@@ -3009,8 +3009,8 @@ void parse_internal(void)
30093009
func->bbs = arena_alloc(BB_ARENA, sizeof(basic_block_t));
30103010

30113011
/* lexer initialization */
3012-
SOURCE->size = 0;
3013-
next_char = SOURCE->elements[0];
3012+
source_idx = 0;
3013+
next_char = dynarr_get_byte(SOURCE, 0);
30143014
lex_expect(T_start);
30153015

30163016
do {
@@ -3051,7 +3051,7 @@ void load_source_file(char *file)
30513051
snprintf(path + c + 1, inclusion_path_len, "%s", buffer + 10);
30523052
load_source_file(path);
30533053
} else {
3054-
strbuf_puts(SOURCE, buffer);
3054+
dynarr_extend(SOURCE, buffer, strlen(buffer));
30553055
}
30563056
}
30573057

tests/snapshots/test_dynarr-arm.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/test_dynarr-riscv.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tools/inliner.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ int main(int argc, char *argv[])
167167
* __c("}\n");
168168
*/
169169
write_str("void __c(char *src) {\n");
170-
write_str(" strbuf_puts(SOURCE, src);\n");
170+
write_str(" dynarr_extend(SOURCE, src, strlen(src));\n");
171171
write_str("}\n");
172172

173173
write_str("void libc_generate() {\n");

0 commit comments

Comments
 (0)