1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
/*
Copyright (C) 2019, 2020 Leo Tenenbaum.
This file is part of toc. toc is distributed under version 3 of the GNU General Public License, without any warranty whatsoever.
You should have received a copy of the GNU General Public License along with toc. If not, see <https://www.gnu.org/licenses/>.
*/
static void cgen_sdecls_stmt(CGenerator *g, Statement *s);
static void cgen_sdecls_decl(CGenerator *g, Declaration *d);
static void cgen_sdecls_expr(CGenerator *g, Expression *e);
static void cgen_decls_stmt(CGenerator *g, Statement *s);
static void cgen_decls_block(CGenerator *g, Block *b);
static void cgen_decls_decl(CGenerator *g, Declaration *d);
static void cgen_sdecls_block(CGenerator *g, Block *b) {
Block *prev_block = g->block;
g->block = b;
b->c.break_lbl = 0;
b->c.cont_lbl = 0;
arr_foreach(b->stmts, Statement, s)
cgen_sdecls_stmt(g, s);
if (b->ret_expr)
cgen_sdecls_expr(g, b->ret_expr);
g->block = prev_block;
}
/* i is the name for this type, NULL if not available */
static void cgen_sdecls_type(CGenerator *g, Type *type) {
if (type->kind == TYPE_STRUCT) {
StructDef *sdef = type->struc;
/* we'll actually define the struct later; here we can just declare it */
if ((sdef->flags & STRUCT_DEF_RESOLVED) && !(sdef->flags & STRUCT_DEF_CGEN_DECLARED)) {
cgen_write(g, "struct ");
if (!sdef->name) {
sdef->c.id = ++g->ident_counter;
}
cgen_struct_name(g, sdef);
cgen_write(g, ";");
cgen_nl(g);
/* we don't need to set the STRUCT_DEF_CGEN_DECLARED flag; that's done in cgen_recurse_subtypes */
}
}
cgen_recurse_subtypes(g, type, cgen_sdecls_type, cgen_sdecls_decl, cgen_sdecls_block, STRUCT_DEF_CGEN_DECLARED);
}
static char *cgen_nms_prefix_part(CGenerator *g, Namespace *n) {
char *s;
if (n->associated_ident) {
size_t ident_len = n->associated_ident->len;
s = malloc(ident_len + 3);
memcpy(s, n->associated_ident->str, ident_len);
s[ident_len] = '_';
s[ident_len+1] = '_';
s[ident_len+2] = '\0';
} else {
s = calloc(CGEN_IDENT_ID_STR_SIZE + 3, 1);
cgen_ident_id_to_str(s, ++g->ident_counter);
size_t len = strlen(s);
s[len] = '_';
s[len+1] = '_';
s[len+2] = '\0';
}
return s;
}
static void cgen_sdecls_expr(CGenerator *g, Expression *e) {
switch (e->kind) {
case EXPR_FN:
/* needs to go before decls_cgen.c... */
e->fn->c.id = ++g->ident_counter;
break;
case EXPR_TYPE:
cgen_sdecls_type(g, e->typeval);
break;
case EXPR_NMS: {
char *prefix_part = cgen_nms_prefix_part(g, e->nms);
size_t prefix_part_len = strlen(prefix_part);
const char *prev_prefix = g->nms_prefixes ? arr_last(g->nms_prefixes)
: "";
size_t prev_prefix_len = strlen(prev_prefix);
char *new_prefix = cgen_malloc(g, prev_prefix_len + prefix_part_len + 1);
memcpy(new_prefix, prev_prefix, prev_prefix_len);
memcpy(new_prefix + prev_prefix_len, prefix_part, prefix_part_len);
free(prefix_part);
arr_add(g->nms_prefixes, new_prefix);
new_prefix[prev_prefix_len + prefix_part_len] = 0;
e->nms->c.prefix = new_prefix;
} break;
default: break;
}
if (e->kind != EXPR_IDENT) {
cgen_recurse_subexprs(g, e, cgen_sdecls_expr, cgen_sdecls_block, cgen_sdecls_decl, cgen_sdecls_type);
}
if (e->kind == EXPR_NMS) {
arr_remove_last(g->nms_prefixes);
}
}
static void cgen_sdecls_decl(CGenerator *g, Declaration *d) {
cgen_sdecls_type(g, &d->type);
if (cgen_fn_is_direct(g, d)) {
d->expr.fn->c.name = d->idents[0];
}
for (int idx = 0; idx < (int)arr_len(d->idents); ++idx) {
Type *type = decl_type_at_index(d, idx);
if (type_is_builtin(type, BUILTIN_TYPE) && !(d->flags & DECL_IS_PARAM)) {
Value *val = decl_val_at_index(d, idx);
cgen_sdecls_type(g, val->type);
}
}
if (d->flags & DECL_HAS_EXPR) {
cgen_sdecls_expr(g, &d->expr);
if (d->flags & DECL_EXPORT) {
if (d->expr.kind == EXPR_FN)
d->expr.fn->flags |= FN_EXPR_EXPORT;
}
}
}
static void cgen_sdecls_stmt(CGenerator *g, Statement *s) {
switch (s->kind) {
case STMT_DECL:
cgen_sdecls_decl(g, s->decl);
break;
case STMT_EXPR:
cgen_sdecls_expr(g, s->expr);
break;
case STMT_RET: {
Return *r = s->ret;
if (r->flags & RET_HAS_EXPR)
cgen_sdecls_expr(g, &r->expr);
} break;
case STMT_BREAK:
if (!s->referring_to->c.break_lbl) {
s->referring_to->c.break_lbl = ++g->lbl_counter;
}
break;
case STMT_CONT:
if (!s->referring_to->c.cont_lbl) {
s->referring_to->c.cont_lbl = ++g->lbl_counter;
}
break;
case STMT_MESSAGE:
break;
case STMT_DEFER:
cgen_sdecls_stmt(g, s->defer);
break;
case STMT_USE:
cgen_sdecls_expr(g, &s->use->expr);
break;
case STMT_INLINE_BLOCK:
arr_foreach(s->inline_block, Statement, sub)
cgen_sdecls_stmt(g, sub);
break;
case STMT_INCLUDE:
assert(0);
break;
}
}
static void cgen_sdecls_file(CGenerator *g, ParsedFile *f) {
arr_foreach(f->stmts, Statement, s) {
cgen_sdecls_stmt(g, s);
}
}
static void cgen_decls_type(CGenerator *g, Type *type) {
/* this check needs to go before cgen_recurse_subtypes, because that sets the STRUCT_DEF_CGEN_DEFINED flag */
bool need_to_generate_struct_definition = type->kind == TYPE_STRUCT && !(type->struc->flags & STRUCT_DEF_CGEN_DEFINED)
&& !struct_is_template(type->struc);
/*
this has to go BEFORE defining the struct, so that if it has struct fields, they're defined before it is,
because
struct Foo;
struct Bar {
struct Foo f;
}
struct Foo {
int x;
}
does not work in C
*/
cgen_recurse_subtypes(g, type, cgen_decls_type, cgen_decls_decl, cgen_decls_block, STRUCT_DEF_CGEN_DEFINED);
if (need_to_generate_struct_definition) {
StructDef *sdef = type->struc;
/* generate struct definition */
cgen_write(g, "struct ");
cgen_struct_name(g, sdef);
cgen_write(g, "{");
cgen_nl(g);
++g->indent_lvl;
arr_foreach(sdef->fields, Field, f) {
cgen_type_pre(g, f->type);
cgen_write(g, " ");
cgen_ident_simple(g, f->name);
cgen_type_post(g, f->type);
cgen_write(g, ";");
cgen_nl(g);
}
--g->indent_lvl;
cgen_write(g, "};");
cgen_nl(g);
/* we don't need to set the STRUCT_DEF_CGEN_DEFINED flag; that's done in cgen_recurse_subtypes */
}
}
static void cgen_single_fn_decl(CGenerator *g, FnExpr *f, U64 which_are_const) {
if (cgen_should_gen_fn(f)) {
cgen_fn_header(g, f, which_are_const);
cgen_write(g, ";");
cgen_nl(g);
}
}
static void cgen_decls_fn_instances(CGenerator *g, FnExpr *f) {
Instance **data = f->instances->data;
for (U64 i = 0; i < f->instances->cap; ++i) {
if (f->instances->occupied[i]) {
if (cgen_should_gen_fn((*data)->fn)) {
(*data)->fn->c.name = f->c.name;
(*data)->fn->c.id = f->c.id;
cgen_single_fn_decl(g, (*data)->fn, (*data)->val.tuple[0].u64);
cgen_write(g, ";");
cgen_nl(g);
}
}
++data;
}
}
static void cgen_fn_decl(CGenerator *g, FnExpr *f, Type *t) {
if (f->flags & FN_EXPR_FOREIGN) {
/* foreign function declaration */
cgen_write(g, "extern ");
Type *fn_types = t->fn.types;
const char *foreign_name = f->foreign.name;
CType *ctypes = f->foreign.ctypes;
if (ctypes[0].kind == CTYPE_NONE) {
cgen_type_pre(g, &fn_types[0]);
} else {
cgen_ctype(g, &ctypes[0]);
}
cgen_write(g, " %s", foreign_name);
cgen_write(g, "(");
for (size_t i = 1; i < arr_len(fn_types); ++i) {
if (i > 1)
cgen_write(g, ", ");
CType *csub = &ctypes[i];
if (csub->kind == CTYPE_NONE) {
Type *sub = &fn_types[i];
cgen_type_pre(g, sub);
cgen_type_post(g, sub);
} else {
cgen_ctype(g, csub);
}
}
cgen_write(g, ")");
if (ctypes[0].kind == CTYPE_NONE)
cgen_type_post(g, &fn_types[0]);
cgen_write(g, ";");
if (!f->c.name || !ident_eq_str(f->c.name, foreign_name) || g->nms != NULL) {
cgen_write(g, "static ");
if (ctypes[0].kind == CTYPE_NONE) {
cgen_type_pre(g, &fn_types[0]);
} else {
cgen_ctype(g, &ctypes[0]);
}
cgen_write(g, " (* const ");
cgen_fn_name(g, f);
cgen_write(g, ")(");
for (size_t i = 1; i < arr_len(fn_types); ++i) {
if (i > 1)
cgen_write(g, ", ");
CType *csub = &ctypes[i];
if (csub->kind == CTYPE_NONE) {
Type *sub = &fn_types[i];
cgen_type_pre(g, sub);
cgen_type_post(g, sub);
} else {
cgen_ctype(g, csub);
}
}
cgen_write(g, ")");
if (ctypes[0].kind == CTYPE_NONE)
cgen_type_post(g, &fn_types[0]);
cgen_write(g, "= %s;", foreign_name);
}
cgen_nl(g);
return;
}
if (fn_has_instances(f)) {
cgen_decls_fn_instances(g, f);
} else {
cgen_single_fn_decl(g, f, 0);
}
}
static void cgen_decls_expr(CGenerator *g, Expression *e) {
assert(e->flags & EXPR_FOUND_TYPE);
cgen_recurse_subexprs(g, e, cgen_decls_expr, cgen_decls_block, cgen_decls_decl, cgen_decls_type);
switch (e->kind) {
case EXPR_FN: {
FnExpr *f = e->fn;
f->c.name = NULL;
if (!f->c.id)
f->c.id = ++g->ident_counter;
cgen_fn_decl(g, e->fn, &e->type);
} break;
case EXPR_TYPE: {
Type *type = e->typeval;
cgen_decls_type(g, type);
} break;
case EXPR_BINARY_OP: {
Type *lhs_type = &e->binary.lhs->type;
if (lhs_type->kind == TYPE_PTR)
lhs_type = lhs_type->ptr;
} break;
default:
break;
}
}
static void cgen_decls_block(CGenerator *g, Block *b) {
Block *prev_block = g->block;
g->block = b;
arr_foreach(b->stmts, Statement, s)
cgen_decls_stmt(g, s);
if (b->ret_expr) cgen_decls_expr(g, b->ret_expr);
g->block = prev_block;
}
static void cgen_decls_decl(CGenerator *g, Declaration *d) {
cgen_decls_type(g, &d->type);
if (cgen_fn_is_direct(g, d)) {
cgen_fn_decl(g, d->expr.fn, &d->expr.type);
cgen_recurse_subexprs(g, (&d->expr), cgen_decls_expr, cgen_decls_block, cgen_decls_decl, cgen_decls_type);
} else {
if (d->flags & DECL_HAS_EXPR) {
cgen_decls_expr(g, &d->expr);
}
if (!g->block || g->block->kind == BLOCK_NMS) {
/* global variables */
for (int i = 0, n_idents = (int)arr_len(d->idents); i < n_idents; ++i) {
Identifier ident = d->idents[i];
Type *type = decl_type_at_index(d, i);
Value *val = NULL;
if (d->flags & DECL_HAS_EXPR)
assert(d->flags & DECL_FOUND_VAL);
if (d->flags & DECL_FOUND_VAL)
val = decl_val_at_index(d, i);
if (!type_is_compileonly(type)) {
if (val) cgen_val_pre(g, val, type);
if (!(d->flags & DECL_EXPORT))
cgen_write(g, "static ");
cgen_type_pre(g, type);
cgen_write(g, " ");
cgen_ident(g, ident);
cgen_type_post(g, type);
if (val) {
cgen_write(g, " = ");
cgen_val(g, val, type);
} else {
cgen_write(g, " = ");
cgen_zero_value(g, type);
}
cgen_write(g, ";");
cgen_nl(g);
}
}
}
}
}
static void cgen_decls_stmt(CGenerator *g, Statement *s) {
switch (s->kind) {
case STMT_DECL:
cgen_decls_decl(g, s->decl);
break;
case STMT_EXPR:
cgen_decls_expr(g, s->expr);
break;
case STMT_RET: {
Return *r = s->ret;
if (r->flags & RET_HAS_EXPR)
cgen_decls_expr(g, &r->expr);
} break;
case STMT_BREAK:
case STMT_CONT:
case STMT_MESSAGE:
break;
case STMT_DEFER:
cgen_decls_stmt(g, s->defer);
break;
case STMT_USE:
cgen_sdecls_expr(g, &s->use->expr);
break;
case STMT_INLINE_BLOCK:
arr_foreach(s->inline_block, Statement, sub)
cgen_decls_stmt(g, sub);
break;
case STMT_INCLUDE:
assert(0);
break;
}
}
static void cgen_decls_file(CGenerator *g, ParsedFile *f) {
cgen_write(g, "/* declarations */\n");
arr_foreach(f->stmts, Statement, s) {
cgen_decls_stmt(g, s);
}
}
|