Skip to content

Commit 94f345c

Browse files
authored
Merge pull request #253 from sysprog21/compound-literal
Consolidates initial compound literal improvements
2 parents 0bd9bd9 + 8fd1859 commit 94f345c

File tree

6 files changed

+1592
-474
lines changed

6 files changed

+1592
-474
lines changed

src/arm-codegen.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
136136
void cfg_flatten(void)
137137
{
138138
func_t *func = find_func("__syscall");
139-
func->bbs->elf_offset = 44; /* offset of start + exit in codegen */
139+
func->bbs->elf_offset = 48; /* offset of start + branch + exit in codegen */
140140

141-
elf_offset = 80; /* offset of start + exit + syscall in codegen */
141+
elf_offset = 84; /* offset of start + branch + exit + syscall in codegen */
142142
GLOBAL_FUNC->bbs->elf_offset = elf_offset;
143143

144144
for (ph2_ir_t *ph2_ir = GLOBAL_FUNC->bbs->ph2_ir_list.head; ph2_ir;
@@ -457,6 +457,8 @@ void code_generate(void)
457457
emit(__sub_r(__AL, __sp, __sp, __r8));
458458
emit(__mov_r(__AL, __r12, __sp));
459459
emit(__bl(__AL, GLOBAL_FUNC->bbs->elf_offset - elf_code->size));
460+
/* After global init, jump to main preparation */
461+
emit(__b(__AL, 56)); /* PC+8: skip exit (24) + syscall (36) + ret (4) - 8 */
460462

461463
/* exit */
462464
emit(__movw(__AL, __r8, GLOBAL_FUNC->stack_size));
@@ -490,7 +492,7 @@ void code_generate(void)
490492
emit(__add_i(__AL, __r1, __r8, 4));
491493
emit(__bl(__AL, MAIN_BB->elf_offset - elf_code->size));
492494

493-
/* exit with main's return value */
495+
/* exit with main's return value - r0 already has the return value */
494496
emit(__mov_i(__AL, __r7, 1));
495497
emit(__svc());
496498

src/elf.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ void elf_write_str(strbuf_t *elf_array, char *vals)
2121
* If necessary, use elf_write_byte() to append the null character
2222
* after calling elf_write_str().
2323
*/
24+
if (!elf_array || !vals)
25+
return;
2426
strbuf_puts(elf_array, vals);
2527
}
2628

2729
void elf_write_byte(strbuf_t *elf_array, int val)
2830
{
31+
if (!elf_array)
32+
return;
2933
strbuf_putc(elf_array, val);
3034
}
3135

@@ -36,19 +40,29 @@ char e_extract_byte(int v, int b)
3640

3741
void elf_write_int(strbuf_t *elf_array, int val)
3842
{
43+
if (!elf_array)
44+
return;
3945
for (int i = 0; i < 4; i++)
4046
strbuf_putc(elf_array, e_extract_byte(val, i));
4147
}
4248

4349
void elf_write_blk(strbuf_t *elf_array, void *blk, int sz)
4450
{
51+
if (!elf_array || !blk || sz <= 0)
52+
return;
4553
char *ptr = blk;
4654
for (int i = 0; i < sz; i++)
4755
strbuf_putc(elf_array, ptr[i]);
4856
}
4957

5058
void elf_generate_header(void)
5159
{
60+
/* Check for null pointers to prevent crashes */
61+
if (!elf_code || !elf_data || !elf_symtab || !elf_strtab || !elf_header) {
62+
error("ELF buffers not initialized");
63+
return;
64+
}
65+
5266
elf32_hdr_t hdr;
5367
/*
5468
* The following table explains the meaning of each field in the
@@ -175,6 +189,12 @@ void elf_generate_header(void)
175189

176190
void elf_generate_sections(void)
177191
{
192+
/* Check for null pointers to prevent crashes */
193+
if (!elf_symtab || !elf_strtab || !elf_section) {
194+
error("ELF section buffers not initialized");
195+
return;
196+
}
197+
178198
/* symtab section */
179199
for (int b = 0; b < elf_symtab->size; b++)
180200
elf_write_byte(elf_section, elf_symtab->elements[b]);
@@ -312,6 +332,12 @@ void elf_generate_sections(void)
312332

313333
void elf_align(void)
314334
{
335+
/* Check for null pointers to prevent crashes */
336+
if (!elf_data || !elf_symtab || !elf_strtab) {
337+
error("ELF buffers not initialized for alignment");
338+
return;
339+
}
340+
315341
while (elf_data->size & 3)
316342
elf_write_byte(elf_data, 0);
317343

@@ -324,6 +350,12 @@ void elf_align(void)
324350

325351
void elf_add_symbol(char *symbol, int pc)
326352
{
353+
/* Check for null pointers to prevent crashes */
354+
if (!symbol || !elf_symtab || !elf_strtab) {
355+
error("Invalid parameters for elf_add_symbol");
356+
return;
357+
}
358+
327359
elf_write_int(elf_symtab, elf_strtab->size);
328360
elf_write_int(elf_symtab, pc);
329361
elf_write_int(elf_symtab, 0);
@@ -344,6 +376,11 @@ void elf_generate(char *outfile)
344376
outfile = "a.out";
345377

346378
FILE *fp = fopen(outfile, "wb");
379+
if (!fp) {
380+
error("Unable to open output file for writing");
381+
return;
382+
}
383+
347384
for (int i = 0; i < elf_header->size; i++)
348385
fputc(elf_header->elements[i], fp);
349386
for (int i = 0; i < elf_code->size; i++)

src/lexer.c

Lines changed: 41 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#define NUM_DIRECTIVES 11
1515
#define NUM_KEYWORDS 16
1616

17+
/* Token mapping structure for elegant initialization */
18+
typedef struct {
19+
char *name;
20+
token_t token;
21+
} token_mapping_t;
22+
1723
/* Preprocessor directive hash table using existing shecc hashmap */
1824
hashmap_t *DIRECTIVE_MAP = NULL;
1925
/* C keywords hash table */
@@ -29,41 +35,25 @@ void lex_init_directives()
2935

3036
DIRECTIVE_MAP = hashmap_create(16); /* Small capacity for directives */
3137

32-
/* Initialization using indexed for-loop */
38+
/* Initialization using struct compound literals for elegance */
3339
directive_tokens_storage =
3440
arena_alloc(GENERAL_ARENA, NUM_DIRECTIVES * sizeof(token_t));
3541

36-
char *names[NUM_DIRECTIVES];
37-
token_t token_values[NUM_DIRECTIVES];
38-
39-
/* Populate arrays using index-based assignments for compatibility */
40-
names[0] = "#define";
41-
token_values[0] = T_cppd_define;
42-
names[1] = "#elif";
43-
token_values[1] = T_cppd_elif;
44-
names[2] = "#else";
45-
token_values[2] = T_cppd_else;
46-
names[3] = "#endif";
47-
token_values[3] = T_cppd_endif;
48-
names[4] = "#error";
49-
token_values[4] = T_cppd_error;
50-
names[5] = "#if";
51-
token_values[5] = T_cppd_if;
52-
names[6] = "#ifdef";
53-
token_values[6] = T_cppd_ifdef;
54-
names[7] = "#ifndef";
55-
token_values[7] = T_cppd_ifndef;
56-
names[8] = "#include";
57-
token_values[8] = T_cppd_include;
58-
names[9] = "#pragma";
59-
token_values[9] = T_cppd_pragma;
60-
names[10] = "#undef";
61-
token_values[10] = T_cppd_undef;
42+
/* Use array compound literal for directive mappings */
43+
token_mapping_t directives[] = {
44+
{"#define", T_cppd_define}, {"#elif", T_cppd_elif},
45+
{"#else", T_cppd_else}, {"#endif", T_cppd_endif},
46+
{"#error", T_cppd_error}, {"#if", T_cppd_if},
47+
{"#ifdef", T_cppd_ifdef}, {"#ifndef", T_cppd_ifndef},
48+
{"#include", T_cppd_include}, {"#pragma", T_cppd_pragma},
49+
{"#undef", T_cppd_undef},
50+
};
6251

6352
/* hashmap insertion */
6453
for (int i = 0; i < NUM_DIRECTIVES; i++) {
65-
directive_tokens_storage[i] = token_values[i];
66-
hashmap_put(DIRECTIVE_MAP, names[i], &directive_tokens_storage[i]);
54+
directive_tokens_storage[i] = directives[i].token;
55+
hashmap_put(DIRECTIVE_MAP, directives[i].name,
56+
&directive_tokens_storage[i]);
6757
}
6858
}
6959

@@ -74,51 +64,34 @@ void lex_init_keywords()
7464

7565
KEYWORD_MAP = hashmap_create(32); /* Capacity for keywords */
7666

77-
/* Initialization using indexed for-loop */
67+
/* Initialization using struct compound literals for elegance */
7868
keyword_tokens_storage =
7969
arena_alloc(GENERAL_ARENA, NUM_KEYWORDS * sizeof(token_t));
8070

81-
char *names[NUM_KEYWORDS];
82-
token_t token_values[NUM_KEYWORDS];
83-
84-
/* Populate arrays using index-based assignments for compatibility */
85-
names[0] = "if";
86-
token_values[0] = T_if;
87-
names[1] = "while";
88-
token_values[1] = T_while;
89-
names[2] = "for";
90-
token_values[2] = T_for;
91-
names[3] = "do";
92-
token_values[3] = T_do;
93-
names[4] = "else";
94-
token_values[4] = T_else;
95-
names[5] = "return";
96-
token_values[5] = T_return;
97-
names[6] = "typedef";
98-
token_values[6] = T_typedef;
99-
names[7] = "enum";
100-
token_values[7] = T_enum;
101-
names[8] = "struct";
102-
token_values[8] = T_struct;
103-
names[9] = "sizeof";
104-
token_values[9] = T_sizeof;
105-
names[10] = "switch";
106-
token_values[10] = T_switch;
107-
names[11] = "case";
108-
token_values[11] = T_case;
109-
names[12] = "break";
110-
token_values[12] = T_break;
111-
names[13] = "default";
112-
token_values[13] = T_default;
113-
names[14] = "continue";
114-
token_values[14] = T_continue;
115-
names[15] = "union";
116-
token_values[15] = T_union;
71+
/* Use array compound literal for keyword mappings */
72+
token_mapping_t keywords[] = {
73+
{"if", T_if},
74+
{"while", T_while},
75+
{"for", T_for},
76+
{"do", T_do},
77+
{"else", T_else},
78+
{"return", T_return},
79+
{"typedef", T_typedef},
80+
{"enum", T_enum},
81+
{"struct", T_struct},
82+
{"sizeof", T_sizeof},
83+
{"switch", T_switch},
84+
{"case", T_case},
85+
{"break", T_break},
86+
{"default", T_default},
87+
{"continue", T_continue},
88+
{"union", T_union},
89+
};
11790

11891
/* hashmap insertion */
11992
for (int i = 0; i < NUM_KEYWORDS; i++) {
120-
keyword_tokens_storage[i] = token_values[i];
121-
hashmap_put(KEYWORD_MAP, names[i], &keyword_tokens_storage[i]);
93+
keyword_tokens_storage[i] = keywords[i].token;
94+
hashmap_put(KEYWORD_MAP, keywords[i].name, &keyword_tokens_storage[i]);
12295
}
12396
}
12497

0 commit comments

Comments
 (0)