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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
|
static bool cgen_stmt(CGenerator *g, Statement *s);
#define CGEN_BLOCK_NOENTER 0x01 /* should cgen_block actually enter and exit the block? */
#define CGEN_BLOCK_NOBRACES 0x02 /* should it use braces? */
static bool cgen_block(CGenerator *g, Block *b, const char *ret_name, uint16_t flags);
static bool cgen_expr_pre(CGenerator *g, Expression *e);
static bool cgen_expr(CGenerator *g, Expression *e);
static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents, const char *prefix, Expression *to);
static bool cgen_type_pre(CGenerator *g, Type *t, Location where);
static bool cgen_type_post(CGenerator *g, Type *t, Location where);
static bool cgen_decl(CGenerator *g, Declaration *d);
static bool cgen_ret(CGenerator *g, Expression *ret);
static bool cgen_val(CGenerator *g, Value *v, Type *t, Location where);
static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where);
static void cgen_create(CGenerator *g, FILE *out, Identifiers *ids, Evaluator *ev) {
g->outc = out;
g->ident_counter = 1; /* some places use 0 to mean no id */
g->main_ident = ident_get(ids, "main");
g->evalr = ev;
g->will_indent = true;
g->indent_lvl = 0;
g->anon_fns = NULL;
g->idents = ids;
}
static bool cgen_block_enter(CGenerator *g, Block *b) {
g->block = b;
Statement *stmts;
if (b == NULL) {
stmts = g->file->stmts;
} else {
stmts = b->stmts;
}
if (b) g->indent_lvl++;
return block_enter(b, stmts, 0);
}
static void cgen_block_exit(CGenerator *g, Block *into) {
Block *b = g->block;
Statement *stmts;
if (b == NULL) {
stmts = g->file->stmts;
} else {
stmts = b->stmts;
}
block_exit(b, stmts);
if (b) g->indent_lvl--;
g->block = into;
}
static inline FILE *cgen_writing_to(CGenerator *g) {
return g->outc; /* for now */
}
/* indent iff needed */
static inline void cgen_indent(CGenerator *g) {
if (g->will_indent) {
for (int i = 0; i < g->indent_lvl; i++)
fprintf(cgen_writing_to(g), "\t");
g->will_indent = false;
}
}
static inline void cgen_write(CGenerator *g, const char *fmt, ...) {
va_list args;
cgen_indent(g);
va_start(args, fmt);
vfprintf(cgen_writing_to(g), fmt, args);
va_end(args);
}
static inline void cgen_nl(CGenerator *g) {
fprintf(cgen_writing_to(g), "\n");
g->will_indent = true;
}
static inline void cgen_writeln(CGenerator *g, const char *fmt, ...) {
va_list args;
cgen_indent(g);
va_start(args, fmt);
vfprintf(cgen_writing_to(g), fmt, args);
va_end(args);
cgen_nl(g);
}
static void cgen_ident_id(CGenerator *g, IdentID id) {
cgen_write(g, "a%lu_", (unsigned long)id);
}
/* should declaration be a direct function declaration C (as opposed to using a function pointer or not being a function) */
static bool cgen_fn_is_direct(CGenerator *g, Declaration *d) {
return g->block == NULL && (d->flags & DECL_FLAG_HAS_EXPR) && d->expr.kind == EXPR_FN && arr_len(d->idents) == 1;
}
static bool cgen_uses_ptr(Type *t) {
switch (t->kind) {
case TYPE_TUPLE:
case TYPE_STRUCT:
return true;
case TYPE_ARR: /* TODO: test me */
case TYPE_BUILTIN:
case TYPE_PTR:
case TYPE_FN:
case TYPE_SLICE:
case TYPE_VOID:
case TYPE_UNKNOWN:
case TYPE_TYPE:
return false;
case TYPE_USER:
return cgen_uses_ptr(type_user_underlying(t));
}
assert(0);
return false;
}
static void cgen_ident(CGenerator *g, Identifier i) {
if (i == g->main_ident) {
/* don't conflict with C's main! */
cgen_write(g, "main__");
} else {
cgen_indent(g);
fprint_ident(cgen_writing_to(g), i);
}
}
static char *cgen_ident_to_str(Identifier i) {
return ident_to_str(i);
}
/* buffer should be at least 32 bytes */
static inline void cgen_ident_id_to_str(char *buffer, IdentID id) {
snprintf(buffer, 32, "a%lu_", (unsigned long)id);
}
static inline Identifier cgen_ident_id_to_ident(CGenerator *g, IdentID id) {
char s[32];
cgen_ident_id_to_str(s, id);
return ident_get(g->idents, s);
}
static bool cgen_type_pre(CGenerator *g, Type *t, Location where) {
assert(t->flags & TYPE_FLAG_RESOLVED);
switch (t->kind) {
case TYPE_BUILTIN:
switch (t->builtin) {
case BUILTIN_I8: cgen_write(g, "i8"); break;
case BUILTIN_I16: cgen_write(g, "i16"); break;
case BUILTIN_I32: cgen_write(g, "i32"); break;
case BUILTIN_I64: cgen_write(g, "i64"); break;
case BUILTIN_U8: cgen_write(g, "u8"); break;
case BUILTIN_U16: cgen_write(g, "u16"); break;
case BUILTIN_U32: cgen_write(g, "u32"); break;
case BUILTIN_U64: cgen_write(g, "u64"); break;
case BUILTIN_CHAR: cgen_write(g, "char"); break;
case BUILTIN_BOOL: cgen_write(g, "bool"); break;
case BUILTIN_F32: cgen_write(g, "f32"); break;
case BUILTIN_F64: cgen_write(g, "f64"); break;
} break;
case TYPE_PTR:
if (!cgen_type_pre(g, t->ptr, where))
return false;
cgen_write(g, "(*");
break;
case TYPE_ARR:
if (!cgen_type_pre(g, t->arr.of, where))
return false;
cgen_write(g, "(");
break;
case TYPE_FN:
if (cgen_uses_ptr(&t->fn.types[0])) {
cgen_write(g, "void");
} else {
if (!cgen_type_pre(g, &t->fn.types[0], where))
return false;
}
cgen_write(g, " (*");
break;
case TYPE_SLICE:
cgen_write(g, "slice_");
break;
case TYPE_VOID: cgen_write(g, "void"); break;
case TYPE_UNKNOWN:
err_print(where, "Can't determine type.");
return false;
case TYPE_STRUCT:
cgen_write(g, "struct {");
g->indent_lvl++;
cgen_nl(g);
arr_foreach(t->struc.fields, Field, f) {
if (!cgen_type_pre(g, f->type, where)) return false;
cgen_write(g, " ");
cgen_ident(g, f->name);
if (!cgen_type_post(g, f->type, where)) return false;
cgen_write(g, ";");
cgen_nl(g);
}
g->indent_lvl--;
cgen_write(g, "}");
break;
case TYPE_TUPLE:
case TYPE_TYPE:
/* We should never try to generate this type */
assert(0);
return false;
case TYPE_USER: {
Type *this = t;
do {
Type *next = type_user_underlying(this);
if (next->kind == TYPE_STRUCT) {
/* use struct tag */
cgen_write(g, "struct ");
t = this;
break;
}
this = next;
} while (this->kind == TYPE_USER);
Declaration *d = t->user.decl;
int idx = t->user.index;
if (d->c.ids[idx])
cgen_ident_id(g, d->c.ids[idx]);
else
cgen_ident(g, d->idents[idx]);
} break;
}
return true;
}
static bool cgen_type_post(CGenerator *g, Type *t, Location where) {
switch (t->kind) {
case TYPE_PTR:
cgen_write(g, ")");
if (!cgen_type_post(g, t->ptr, where))
return false;
break;
case TYPE_ARR:
assert(t->flags & TYPE_FLAG_RESOLVED);
cgen_write(g, "[%lu])", (unsigned long)t->arr.n);
if (!cgen_type_post(g, t->arr.of, where))
return false;
break;
case TYPE_FN: {
bool out_param = cgen_uses_ptr(&t->fn.types[0]);
cgen_write(g, ")(");
for (size_t i = 1; i < arr_len(t->fn.types); i++) {
if (i != 1)
cgen_write(g, ", ");
if (!cgen_type_pre(g, &t->fn.types[i], where))
return false;
if (cgen_uses_ptr(&t->fn.types[i]))
cgen_write(g, "(*)");
if (!cgen_type_post(g, &t->fn.types[i], where))
return false;
}
if (out_param) {
Type *ret_type = &t->fn.types[0];
if (arr_len(t->fn.types) > 1)
cgen_write(g, ", ");
if (ret_type->kind == TYPE_TUPLE) {
arr_foreach(ret_type->tuple, Type, x) {
if (!cgen_type_pre(g, x, where))
return false;
cgen_write(g, "(*)");
if (!cgen_type_post(g, x, where))
return false;
if (x != arr_last(ret_type->tuple)) {
cgen_write(g, ", ");
}
}
} else {
if (!cgen_type_pre(g, ret_type, where))
return false;
cgen_write(g, "(*)");
if (!cgen_type_post(g, ret_type, where))
return false;
}
}
if (arr_len(t->fn.types) == 1 && !out_param)
cgen_write(g, "void");
cgen_write(g, ")");
if (!out_param)
if (!cgen_type_post(g, &t->fn.types[0], where))
return false;
} break;
case TYPE_BUILTIN:
case TYPE_VOID:
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_TYPE:
case TYPE_SLICE:
case TYPE_USER:
case TYPE_STRUCT:
break;
}
return true;
}
static inline void cgen_fn_name(CGenerator *g, FnExpr *f) {
if (f->c.name) {
cgen_ident(g, f->c.name);
} else {
cgen_ident_id(g, f->c.id);
}
}
static bool cgen_fn_header(CGenerator *g, FnExpr *f, Location where) {
bool out_param = cgen_uses_ptr(&f->ret_type);
bool any_params = false;
if (!f->c.name) /* anonymous fn */
cgen_write(g, "static ");
if (out_param) {
cgen_write(g, "void ");
} else {
if (!cgen_type_pre(g, &f->ret_type, where)) return false;
cgen_write(g, " ");
}
cgen_fn_name(g, f);
if (!out_param) {
if (!cgen_type_post(g, &f->ret_type, where)) return false;
}
cgen_write(g, "(");
arr_foreach(f->params, Declaration, d) {
long idx = 0;
arr_foreach(d->idents, Identifier, i) {
if (d != f->params || i != d->idents)
cgen_write(g, ", ");
Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx++] : &d->type;
any_params = true;
if (!cgen_type_pre(g, type, where))
return false;
cgen_write(g, " ");
cgen_ident(g, *i);
if (!cgen_type_post(g, type, where))
return false;
}
}
if (out_param) {
if (f->ret_type.kind == TYPE_TUPLE) {
/* multiple return variables */
for (size_t i = 0; i < arr_len(f->ret_type.tuple); i++) {
Type *x = &f->ret_type.tuple[i];
if (any_params || i > 0)
cgen_write(g, ", ");
if (!cgen_type_pre(g, x, where)) return false;
cgen_write(g, "(*ret%lu_)", (unsigned long)i);
if (!cgen_type_post(g, x, where)) return false;
}
} else {
if (any_params)
cgen_write(g, ", ");
if (!cgen_type_pre(g, &f->ret_type, where))
return false;
cgen_write(g, " (*ret_)");
if (!cgen_type_post(g, &f->ret_type, where))
return false;
}
}
if (!out_param && arr_len(f->params) == 0)
cgen_write(g, "void");
cgen_write(g, ")");
return true;
}
/*
Either set_expr or set_str should be NULL and either to_expr or to_str should be NULL
Also, set_str and/or to_str should be NULL
this will call cgen_expr_pre for set_expr and to_expr
*/
static bool cgen_set(CGenerator *g, Expression *set_expr, const char *set_str, Expression *to_expr,
const char *to_str) {
Type *type;
Location where;
if (set_expr) {
type = &set_expr->type;
where = set_expr->where;
if (!cgen_expr_pre(g, set_expr)) return false;
} else {
assert(to_expr);
type = &to_expr->type;
where = to_expr->where;
if (!cgen_expr_pre(g, to_expr)) return false;
}
type = type_inner(type);
switch (type->kind) {
case TYPE_BUILTIN:
case TYPE_FN:
case TYPE_PTR:
case TYPE_SLICE:
case TYPE_STRUCT:
case TYPE_UNKNOWN:
if (set_expr) {
if (!cgen_expr(g, set_expr)) return false;
} else {
cgen_write(g, set_str);
}
cgen_write(g, " = ");
if (to_expr) {
if (!cgen_expr(g, to_expr)) return false;
} else {
cgen_write(g, to_str);
}
cgen_write(g, ";");
break;
case TYPE_ARR:
cgen_write(g, "{");
cgen_nl(g);
cgen_write(g, "size_t i;");
if (!cgen_type_pre(g, type->arr.of, where)) return false;
cgen_write(g, "(*arr__in)");
if (!cgen_type_post(g, type->arr.of, where)) return false;
cgen_write(g, " = ");
if (to_expr) {
if (!cgen_expr(g, to_expr)) return false;
} else {
cgen_write(g, to_str);
}
cgen_write(g, "; ");
if (!cgen_type_pre(g, type->arr.of, where)) return false;
cgen_write(g, "(*arr__out)");
if (!cgen_type_post(g, type->arr.of, where)) return false;
cgen_write(g, " = ");
if (set_expr) {
if (!cgen_expr(g, set_expr)) return false;
} else {
cgen_write(g, set_str);
}
cgen_write(g, ";");
cgen_nl(g);
cgen_write(g, "for (i = 0; i < %lu; i++) arr__out[i] = arr__in[i];", (unsigned long)type->arr.n);
cgen_nl(g);
cgen_write(g, "}");
break;
case TYPE_TUPLE:
assert(set_expr);
assert(to_expr);
assert(set_expr->kind == EXPR_TUPLE);
if (!cgen_set_tuple(g, set_expr->tuple, NULL, NULL, to_expr))
return false;
break;
case TYPE_USER:
case TYPE_VOID:
case TYPE_TYPE:
assert(0);
return false;
}
return true;
}
/* one of exprs, idents, and prefix should be NULL. */
static bool cgen_set_tuple(CGenerator *g, Expression *exprs, Identifier *idents, const char *prefix, Expression *to) {
IdentID prefix_id; /* ID of prefix for block */
switch (to->kind) {
case EXPR_TUPLE:
/* e.g. a, b = 3, 5; */
for (size_t i = 0; i < arr_len(to->tuple); i++) {
char *s = NULL, buf[64];
Expression *e = NULL;
if (idents)
s = cgen_ident_to_str(idents[i]);
else if (exprs)
e = &exprs[i];
else {
snprintf(buf, sizeof buf, "(%s%lu_)", prefix, i);
s = buf;
}
if (!cgen_set(g, e, s, &to->tuple[i], NULL)) return false;
if (s != buf) free(s);
}
break;
case EXPR_CALL: {
/* e.g. a, b = fn_which_returns_tuple(); */
if (!cgen_expr(g, to->call.fn)) return false;
cgen_write(g, "(");
bool any_args = arr_len(to->call.arg_exprs) != 0;
arr_foreach(to->call.arg_exprs, Expression, arg) {
if (arg != to->call.arg_exprs)
cgen_write(g, ", ");
if (!cgen_expr(g, arg))
return false;
}
/* out params */
size_t len = exprs ? arr_len(exprs) : arr_len(idents);
for (unsigned long i = 0; i < (unsigned long)len; i++) {
if (any_args || i > 0)
cgen_write(g, ", ");
if (exprs) {
cgen_write(g, "&");
if (!cgen_expr(g, &exprs[i]))
return false;
} else if (idents) {
cgen_write(g, "&");
cgen_ident(g, idents[i]);
} else {
cgen_write(g, "&(%s%lu_)", prefix, i);
}
}
cgen_writeln(g, ");");
} break;
case EXPR_IF:
prefix_id = to->if_.c.id;
goto prefixed;
case EXPR_BLOCK:
prefix_id = to->block_ret_id;
goto prefixed;
case EXPR_WHILE:
prefix_id = to->while_.c.id;
goto prefixed;
case EXPR_EACH:
prefix_id = to->each.c.id;
goto prefixed;
prefixed:
for (unsigned long i = 0; i < (unsigned long)arr_len(to->type.tuple); i++) {
cgen_write(g, "(");
if (exprs) {
if (!cgen_expr(g, &exprs[i]))
return false;
} else if (idents) {
cgen_ident(g, idents[i]);
} else {
cgen_write(g, "%s%lu_", prefix, i);
}
cgen_write(g, ") = ");
cgen_ident_id(g, prefix_id);
cgen_write(g, "%lu_", i);
cgen_write(g, "; ");
}
break;
case EXPR_SLICE:
case EXPR_IDENT:
case EXPR_LITERAL_INT:
case EXPR_LITERAL_CHAR:
case EXPR_LITERAL_BOOL:
case EXPR_LITERAL_STR:
case EXPR_LITERAL_FLOAT:
case EXPR_UNARY_OP:
case EXPR_BINARY_OP:
case EXPR_FN:
case EXPR_CAST:
case EXPR_NEW:
case EXPR_C:
case EXPR_DSIZEOF:
case EXPR_DALIGNOF:
case EXPR_TYPE:
assert(0);
return false;
}
return true;
}
/* generates the C code for new'ing a slice of array type t (e.g. [5]int) and putting it in the given ident id. */
static bool cgen_new_slice(CGenerator *g, Type *t, IdentID id, Location where) {
Expression *n_expr = t->arr.n_expr;
assert(!(t->flags & TYPE_FLAG_RESOLVED)); /* we don't want this to be resolved, because the size might only be known at runtime. */
if (!cgen_expr_pre(g, n_expr)) return false;
cgen_write(g, "size_t s");
cgen_ident_id(g, id);
cgen_write(g, " = ");
if (!cgen_expr(g, n_expr)) return false;
cgen_write(g, "; slice_ ");
cgen_ident_id(g, id);
cgen_write(g, ";");
cgen_ident_id(g, id);
cgen_write(g, ".data = e__calloc(s");
cgen_ident_id(g, id);
cgen_write(g, ", sizeof(");
if (t->arr.of->kind == TYPE_ARR) {
cgen_write(g, "slice_");
} else {
if (!cgen_type_pre(g, t->arr.of, where))
return false;
if (!cgen_type_post(g, t->arr.of, where))
return false;
}
cgen_write(g, ")); ");
cgen_ident_id(g, id);
cgen_write(g, ".n = s");
cgen_ident_id(g, id);
cgen_write(g, ";");
if (t->arr.of->kind == TYPE_ARR) {
/* slice of slices. initialize the inner slices. */
IdentID child_id = g->ident_counter++;
cgen_write(g, "for (i64 i_ = 0; i_ < s");
cgen_ident_id(g, id);
cgen_write(g, "; i_++) {");
cgen_nl(g);
g->indent_lvl++;
if (!cgen_new_slice(g, t->arr.of, child_id, where))
return false;
cgen_write(g, " ((slice_*)");
cgen_ident_id(g, id);
cgen_write(g, ".data)[i_] = ");
cgen_ident_id(g, child_id);
cgen_write(g, ";");
cgen_nl(g);
g->indent_lvl--;
cgen_write(g, "}");
}
return true;
}
static bool cgen_expr_pre(CGenerator *g, Expression *e) {
IdentID id = 0;
char ret_name[64];
switch (e->kind) {
case EXPR_IF:
case EXPR_WHILE:
case EXPR_EACH:
case EXPR_BLOCK: {
id = g->ident_counter++;
cgen_ident_id_to_str(ret_name, id);
char *p = ret_name + strlen(ret_name);
if (e->type.kind != TYPE_VOID) {
if (e->type.kind == TYPE_TUPLE) {
for (unsigned long i = 0; i < arr_len(e->type.tuple); i++) {
sprintf(p, "%lu_", i);
if (!cgen_type_pre(g, &e->type.tuple[i], e->where)) return false;
cgen_write(g, " %s", ret_name);
if (!cgen_type_post(g, &e->type.tuple[i], e->where)) return false;
cgen_write(g, "; ");
}
} else {
cgen_type_pre(g, &e->type, e->where);
cgen_write(g, " %s", ret_name);
cgen_type_post(g, &e->type, e->where);
cgen_write(g, ";");
cgen_nl(g);
}
}
*p = 0; /* clear tuple suffixes */
} break;
default: break;
}
switch (e->kind) {
case EXPR_IF: {
IfExpr *curr = &e->if_;
curr->c.id = id;
while (1) {
if (curr->cond) {
cgen_write(g, "if (");
if (!cgen_expr(g, curr->cond))
return false;
cgen_write(g, ") ");
}
if (!cgen_block(g, &curr->body, ret_name, 0))
return false;
if (curr->next_elif) {
cgen_write(g, " else ");
curr = &curr->next_elif->if_;
} else break;
}
} break;
case EXPR_WHILE: {
WhileExpr *w = &e->while_;
w->c.id = id;
cgen_write(g, "while (");
if (w->cond) {
if (!cgen_expr(g, w->cond))
return false;
} else {
cgen_write(g, "true");
}
cgen_write(g, ") ");
if (!cgen_block(g, &w->body, ret_name, 0))
return false;
} break;
case EXPR_EACH: {
EachExpr *ea = &e->each;
int is_range = ea->flags & EACH_IS_RANGE;
if (is_range) {
if (!cgen_expr_pre(g, ea->range.from)) return false;
if (ea->range.to && !cgen_expr_pre(g, ea->range.to)) return false;
} else {
if (!cgen_expr_pre(g, ea->of)) return false;
}
ea->c.id = id;
if (!each_enter(e, 0)) return false;
cgen_write(g, "{");
if (is_range) {
/* set value to from */
if (ea->value) {
if (!cgen_type_pre(g, &ea->type, e->where)) return false;
cgen_write(g, " ");
cgen_ident(g, ea->value);
if (!cgen_type_post(g, &ea->type, e->where)) return false;
cgen_write(g, "; ");
Expression val_expr;
val_expr.flags = EXPR_FLAG_FOUND_TYPE;
val_expr.kind = EXPR_IDENT;
val_expr.ident = ea->value;
val_expr.type = ea->type;
if (!cgen_set(g, &val_expr, NULL, ea->range.from, NULL))
return false;
} else {
if (!cgen_type_pre(g, &ea->type, e->where)) return false;
cgen_write(g, " val_");
if (!cgen_type_post(g, &ea->type, e->where)) return false;
cgen_write(g, "; ");
if (!cgen_set(g, NULL, "val_", ea->range.from, NULL))
return false;
}
if (ea->range.to) {
/* pre generate to */
if (!cgen_type_pre(g, &ea->type, e->where)) return false;
cgen_write(g, " to_");
if (!cgen_type_post(g, &ea->type, e->where)) return false;
cgen_write(g, " = ");
if (!cgen_expr(g, ea->range.to))
return false;
cgen_write(g, "; ");
}
} else {
/* pre-generate of */
if (!cgen_type_pre(g, &ea->of->type, e->where))
return false;
cgen_write(g, " of_");
if (!cgen_type_post(g, &ea->of->type, e->where))
return false;
cgen_write(g, "; ");
if (!cgen_set(g, NULL, "of_", ea->of, NULL))
return false;
}
cgen_write(g, "for (");
if (ea->index || !is_range) {
cgen_write(g, "i64 ");
if (ea->index)
cgen_ident(g, ea->index);
else
cgen_write(g, "i_");
cgen_write(g, " = 0");
}
cgen_write(g, "; ");
if (!(is_range && !ea->range.to)) { /* if it's finite */
if (is_range) {
if (ea->value)
cgen_ident(g, ea->value);
else
cgen_write(g, "val_");
bool positive_step
= ea->range.stepval == NULL || val_is_nonnegative(ea->range.stepval, &ea->type);
cgen_write(g, " %c= to_", positive_step ? '<' : '>');
} else {
if (ea->index)
cgen_ident(g, ea->index);
else
cgen_write(g, "i_");
cgen_write(g, " < ");
switch (ea->of->type.kind) {
case TYPE_ARR:
cgen_write(g, "%lu", (unsigned long)ea->of->type.arr.n);
break;
case TYPE_SLICE:
cgen_write(g, "of_.n");
break;
default: assert(0); break;
}
}
}
cgen_write(g, "; ");
if (is_range) {
if (ea->value)
cgen_ident(g, ea->value);
else
cgen_write(g, "val_");
cgen_write(g, " += ");
if (ea->range.stepval) {
if (!cgen_val(g, ea->range.stepval, &ea->type, e->where))
return false;
} else {
cgen_write(g, "1");
}
if (ea->index) cgen_write(g, ", ");
}
if (ea->index || !is_range) {
if (ea->index)
cgen_ident(g, ea->index);
else
cgen_write(g, "i_");
cgen_write(g, "++");
}
cgen_write(g, ") {");
cgen_nl(g);
if (ea->value) {
if (!is_range) {
/* necessary for iterating over, e.g., an array of arrays */
if (!cgen_type_pre(g, &ea->type, e->where))
return false;
cgen_write(g, "(*p_)");
if (!cgen_type_post(g, &ea->type, e->where))
return false;
cgen_write(g, " = ");
if (ea->of->type.kind == TYPE_SLICE) {
cgen_write(g, "((");
if (!cgen_type_pre(g, &ea->type, e->where)) return false;
cgen_write(g, "(*)");
if (!cgen_type_post(g, &ea->type, e->where)) return false;
cgen_write(g, ")of_.data) + ");
if (ea->index)
cgen_ident(g, ea->index);
else
cgen_write(g, "i_");
} else {
cgen_write(g, "&of_[");
if (ea->index)
cgen_ident(g, ea->index);
else
cgen_write(g, "i_");
cgen_write(g, "]");
}
cgen_write(g, "; ");
if (!cgen_type_pre(g, &ea->type, e->where)) return false;
cgen_write(g, " ");
cgen_ident(g, ea->value);
if (!cgen_type_post(g, &ea->type, e->where)) return false;
cgen_write(g, "; ");
Expression set_expr;
set_expr.kind = EXPR_IDENT;
set_expr.ident = ea->value;
set_expr.type = ea->type;
set_expr.flags = EXPR_FLAG_FOUND_TYPE;
if (!cgen_set(g, &set_expr, NULL, NULL, "(*p_)"))
return false;
}
}
if (!cgen_block(g, &ea->body, ret_name, CGEN_BLOCK_NOBRACES))
return false;
cgen_write(g, "}}");
each_exit(e);
} break;
case EXPR_BLOCK:
e->block_ret_id = id;
if (!cgen_block(g, &e->block, ret_name, 0))
return false;
break;
case EXPR_CALL: {
if (!cgen_expr_pre(g, e->call.fn)) return false;
arr_foreach(e->call.arg_exprs, Expression, arg)
if (!cgen_expr_pre(g, arg)) return false;
if (cgen_uses_ptr(&e->type)) {
e->call.c.id = g->ident_counter++;
if (!cgen_type_pre(g, &e->type, e->where)) return false;
cgen_write(g, " ");
cgen_ident_id(g, e->call.c.id);
if (!cgen_type_post(g, &e->type, e->where)) return false;
cgen_write(g, ";"); cgen_nl(g);
if (!cgen_expr(g, e->call.fn)) return false;
cgen_write(g, "(");
bool any_args = false;
arr_foreach(e->call.arg_exprs, Expression, arg) {
any_args = true;
if (arg != e->call.arg_exprs)
cgen_write(g, ", ");
if (!cgen_expr(g, arg))
return false;
}
if (any_args) {
cgen_write(g, ", ");
}
cgen_write(g, "&");
cgen_ident_id(g, e->call.c.id);
cgen_write(g, ");");
cgen_nl(g);
}
} break;
case EXPR_UNARY_OP:
if (!cgen_expr_pre(g, e->unary.of)) return false;
break;
case EXPR_BINARY_OP:
if (!cgen_expr_pre(g, e->binary.lhs)) return false;
if (e->binary.op != BINARY_DOT)
if (!cgen_expr_pre(g, e->binary.rhs)) return false;
break;
case EXPR_CAST:
if (!cgen_expr_pre(g, e->cast.expr)) return false;
break;
case EXPR_SLICE: {
SliceExpr *s = &e->slice;
IdentID s_id = e->slice.c.id = g->ident_counter++;
IdentID from_id = g->ident_counter++;
if (!cgen_expr_pre(g, s->of))
return false;
if (s->from && !cgen_expr_pre(g, s->from))
return false;
if (s->to && !cgen_expr_pre(g, s->to))
return false;
cgen_write(g, "slice_ ");
cgen_ident_id(g, s_id);
cgen_write(g, "; { slice_ of__ = ");
if (!cgen_expr(g, s->of))
return false;
cgen_write(g, "; i64 ");
cgen_ident_id(g, from_id);
cgen_write(g, " = ");
if (s->from) {
if (!cgen_expr(g, s->from))
return false;
} else {
cgen_write(g, "0");
}
cgen_write(g, "; ");
cgen_ident_id(g, s_id);
cgen_write(g, ".data = (");
if (!cgen_type_pre(g, e->type.slice, e->where))
return false;
cgen_write(g, "(*)");
if (!cgen_type_post(g, e->type.slice, e->where))
return false;
cgen_write(g, ")(of__");
if (s->of->type.kind == TYPE_SLICE) {
cgen_write(g, ".data");
}
cgen_write(g, ") + ");
cgen_ident_id(g, from_id);
cgen_write(g, "; ");
cgen_ident_id(g, s_id);
cgen_write(g, ".n = ");
if (s->to) {
if (!cgen_expr(g, s->to))
return false;
} else {
cgen_write(g, "of__.n - 1");
}
cgen_write(g, " - ");
cgen_ident_id(g, from_id);
cgen_write(g, "; }");
cgen_nl(g);
} break;
case EXPR_NEW:
if (e->new.n && !cgen_expr_pre(g, e->new.n))
return false;
break;
case EXPR_LITERAL_INT:
case EXPR_LITERAL_FLOAT:
case EXPR_LITERAL_BOOL:
case EXPR_LITERAL_CHAR:
case EXPR_LITERAL_STR:
case EXPR_IDENT:
case EXPR_FN:
case EXPR_C:
case EXPR_DSIZEOF:
case EXPR_DALIGNOF:
case EXPR_TYPE:
break;
case EXPR_TUPLE:
arr_foreach(e->tuple, Expression, x)
if (!cgen_expr_pre(g, x)) return false;
}
return true;
}
static bool cgen_expr(CGenerator *g, Expression *e) {
switch (e->kind) {
case EXPR_LITERAL_FLOAT:
cgen_write(g, "%.16Lf", (long double)e->floatl); /* TODO(eventually): better precision? */
break;
case EXPR_LITERAL_INT:
cgen_write(g, UINTEGER_FMT, e->intl);
break;
case EXPR_LITERAL_STR: {
size_t c;
cgen_write(g, "mkslice_(\"");
for (c = 0; c < e->strl.len; c++) {
cgen_write(g, "\\x%x", e->strl.str[c]);
}
cgen_write(g, "\", %lu)", (unsigned long)e->strl.len);
} break;
case EXPR_LITERAL_BOOL:
cgen_write(g, e->booll ? "true" : "false");
break;
case EXPR_LITERAL_CHAR:
cgen_write(g, "((char)%d)", e->charl);
break;
case EXPR_IDENT:
cgen_ident(g, e->ident);
break;
case EXPR_BINARY_OP: {
const char *s = "";
bool handled = false;
switch (e->binary.op) {
case BINARY_SUB:
s = "-"; break;
case BINARY_ADD:
s = "+"; break;
case BINARY_MUL:
s = "*"; break;
case BINARY_DIV:
s = "/"; break;
case BINARY_SET:
if (!cgen_set(g, e->binary.lhs, NULL, e->binary.rhs, NULL)) return false;
handled = true;
break;
case BINARY_GT:
s = ">"; break;
case BINARY_LT:
s = "<"; break;
case BINARY_GE:
s = ">="; break;
case BINARY_LE:
s = "<="; break;
case BINARY_EQ:
s = "=="; break;
case BINARY_NE:
s = "!="; break;
case BINARY_AT_INDEX:
cgen_write(g, "(");
switch (e->binary.lhs->type.kind) {
case TYPE_ARR:
if (!cgen_expr(g, e->binary.lhs))
return false;
cgen_write(g, "[");
if (!cgen_expr(g, e->binary.rhs))
return false;
cgen_write(g, "]");
break;
case TYPE_SLICE:
cgen_write(g, "((");
if (!cgen_type_pre(g, &e->type, e->where))
return false;
cgen_write(g, "(*)");
if (!cgen_type_post(g, &e->type, e->where))
return false;
cgen_write(g, ")(");
if (!cgen_expr(g, e->binary.lhs))
return false;
cgen_write(g, ".data))[");
if (!cgen_expr(g, e->binary.rhs))
return false;
cgen_write(g, "]");
break;
default:
assert(0);
break;
}
cgen_write(g, ")");
handled = true;
break;
case BINARY_DOT: {
cgen_write(g, "(");
cgen_expr(g, e->binary.lhs);
bool is_ptr = type_inner(&e->binary.lhs->type)->kind == TYPE_PTR;
cgen_write(g, is_ptr ? "->" :".");
cgen_ident(g, e->binary.field->name);
cgen_write(g, ")");
handled = true;
} break;
}
if (handled) break;
cgen_write(g, "(");
if (!cgen_expr(g, e->binary.lhs))
return false;
cgen_write(g, "%s", s);
if (!cgen_expr(g, e->binary.rhs))
return false;
cgen_write(g, ")");
} break;
case EXPR_UNARY_OP: {
const char *s = "";
bool handled = false;
Type *of_type = &e->unary.of->type;
switch (e->unary.op) {
case UNARY_MINUS:
s = "-"; break;
case UNARY_DEREF:
s = "*"; break;
case UNARY_ADDRESS:
s = "&"; break;
case UNARY_NOT:
s = "!"; break;
case UNARY_DEL:
cgen_write(g, "free(");
if (!cgen_expr(g, e->unary.of))
return false;
if (of_type->kind == TYPE_SLICE)
cgen_write(g, ".data");
cgen_write(g, ")");
handled = true;
break;
case UNARY_LEN: {
bool is_ptr = of_type->kind == TYPE_PTR;
if (is_ptr) {
of_type = of_type->ptr;
}
switch (of_type->kind) {
case TYPE_SLICE:
if (!cgen_expr(g, e->unary.of))
return false;
cgen_write(g, "%sn", is_ptr ? "->" : ".");
break;
case TYPE_ARR:
cgen_write(g, "%lu", (unsigned long)of_type->arr.n);
break;
default: assert(0); break;
}
handled = true;
} break;
}
if (handled) break;
cgen_write(g, "(");
cgen_write(g, "%s", s);
if (!cgen_expr(g, e->unary.of))
return false;
cgen_write(g, ")");
} break;
case EXPR_NEW: {
if (e->new.n) {
cgen_write(g, "mkslice_(e__calloc(");
if (!cgen_expr(g, e->new.n)) return false;
cgen_write(g, ", (i64)sizeof(");
if (!cgen_type_pre(g, &e->new.type, e->where)) return false;
if (!cgen_type_post(g, &e->new.type, e->where)) return false;
cgen_write(g, ")), ");
if (!cgen_expr(g, e->new.n)) return false;
cgen_write(g, ")");
} else {
Type *t = &e->new.type;
cgen_write(g, "((");
if (!cgen_type_pre(g, &e->type, e->where))
return false;
if (!cgen_type_post(g, &e->type, e->where))
return false;
cgen_write(g, ")e__calloc(1, sizeof(");
if (!cgen_type_pre(g, t, e->where))
return false;
if (!cgen_type_post(g, t, e->where))
return false;
cgen_write(g, ")))");
}
} break;
case EXPR_IF:
if (e->type.kind != TYPE_VOID)
cgen_ident_id(g, e->if_.c.id);
break;
case EXPR_WHILE:
if (e->type.kind != TYPE_VOID)
cgen_ident_id(g, e->while_.c.id);
break;
case EXPR_BLOCK:
if (e->type.kind != TYPE_VOID)
cgen_ident_id(g, e->block_ret_id);
break;
case EXPR_EACH:
if (e->type.kind != TYPE_VOID)
cgen_ident_id(g, e->each.c.id);
break;
case EXPR_CALL:
if (cgen_uses_ptr(&e->type)) {
cgen_ident_id(g, e->call.c.id);
} else {
cgen_write(g, "(");
if (!cgen_expr(g, e->call.fn))
return false;
cgen_write(g, "(");
arr_foreach(e->call.arg_exprs, Expression, arg) {
if (arg != e->call.arg_exprs)
cgen_write(g, ", ");
if (!cgen_expr(g, arg))
return false;
}
cgen_write(g, "))");
}
break;
case EXPR_C: {
Value val;
if (!eval_expr(g->evalr, e->c.code, &val))
return false;
cgen_indent(g);
fwrite(val.slice.data, 1, (size_t)val.slice.n, cgen_writing_to(g));
} break;
case EXPR_DSIZEOF:
case EXPR_DALIGNOF: {
Value val;
if (!eval_expr(g->evalr, e, &val))
return false;
cgen_write(g, "%"PRId64, val.i64);
} break;
case EXPR_CAST: {
Type *from = &e->cast.expr->type;
Type *to = &e->cast.type;
if (from->kind == TYPE_USER || to->kind == TYPE_USER) {
/* don't need to cast; they're the same type in C */
if (!cgen_expr(g, e->cast.expr))
return false;
} else {
cgen_write(g, "((");
cgen_type_pre(g, to, e->where);
cgen_type_post(g, to, e->where);
cgen_write(g, ")(");
if (!cgen_expr(g, e->cast.expr))
return false;
cgen_write(g, ")");
if (from->kind == TYPE_SLICE /* casting from a slice to a non-slice */
&& to->kind != TYPE_SLICE)
cgen_write(g, ".data");
cgen_write(g, ")");
}
} break;
case EXPR_TUPLE:
/* the only time this should happen is if you're stating
a tuple, e.g. 3, 5;, but we've errored about that before
(the comma operator does not exist in toc!) */
case EXPR_TYPE:
assert(0);
break;
case EXPR_FN: {
if (g->block != NULL) {
Expression **eptr = arr_add(&g->anon_fns);
*eptr = e;
}
cgen_fn_name(g, &e->fn);
} break;
case EXPR_SLICE:
cgen_ident_id(g, e->slice.c.id);
break;
}
return true;
}
/*
ret_name = variable to store block return value in; NULL for none. NOTE:
functions always call with NULL as ret_name, even if they use out params, for now
at least.
*/
static bool cgen_block(CGenerator *g, Block *b, const char *ret_name, U16 flags) {
Block *prev = g->block;
if (!(flags & CGEN_BLOCK_NOBRACES))
cgen_write(g, "{");
if (!(flags & CGEN_BLOCK_NOENTER))
if (!cgen_block_enter(g, b))
return false;
cgen_nl(g);
arr_foreach(b->stmts, Statement, s)
if (!cgen_stmt(g, s))
return false;
if (b->ret_expr && ret_name) {
if (b->ret_expr->type.kind == TYPE_TUPLE) {
if (!cgen_set_tuple(g, NULL, NULL, ret_name, b->ret_expr))
return false;
} else {
if (!cgen_set(g, NULL, ret_name, b->ret_expr, NULL))
return false;
}
cgen_nl(g);
}
if (!(flags & CGEN_BLOCK_NOENTER))
cgen_block_exit(g, prev);
if (!(flags & CGEN_BLOCK_NOBRACES))
cgen_write(g, "}");
return true;
}
static void cgen_zero_value(CGenerator *g, Type *t) {
switch (t->kind) {
case TYPE_BUILTIN:
cgen_write(g, "0");
break;
case TYPE_PTR:
case TYPE_FN:
cgen_write(g, "NULL");
break;
case TYPE_SLICE:
cgen_write(g, "{NULL, 0}");
break;
case TYPE_ARR:
case TYPE_STRUCT:
cgen_write(g, "{0}");
break;
case TYPE_USER:
cgen_zero_value(g, type_inner(t));
break;
case TYPE_TYPE:
case TYPE_VOID:
case TYPE_UNKNOWN:
case TYPE_TUPLE:
assert(0);
break;
}
}
static bool cgen_fn(CGenerator *g, FnExpr *f, Location where) {
FnExpr *prev_fn = g->fn;
Block *prev_block = g->block;
fn_enter(f, 0);
if (!cgen_fn_header(g, f, where))
return false;
cgen_write(g, " ");
g->fn = f;
cgen_write(g, "{");
cgen_nl(g);
arr_foreach(f->ret_decls, Declaration, d) {
if (!cgen_decl(g, d))
return false;
}
if (!cgen_block_enter(g, &f->body)) return false;
if (!cgen_block(g, &f->body, NULL, CGEN_BLOCK_NOENTER | CGEN_BLOCK_NOBRACES))
return false;
if (f->ret_decls) {
if (cgen_uses_ptr(&f->ret_type)) {
} else {
cgen_write(g, "return ");
cgen_ident(g, f->ret_decls[0].idents[0]);
cgen_writeln(g, ";");
}
} else if (f->body.ret_expr) {
if (!cgen_ret(g, f->body.ret_expr)) return false;
}
cgen_block_exit(g, prev_block);
cgen_write(g, "}");
fn_exit(f);
cgen_nl(g);
g->fn = prev_fn;
cgen_nl(g);
cgen_nl(g);
return true;
}
static bool cgen_val_ptr_pre(CGenerator *g, void *v, Type *t, Location where) {
switch (t->kind) {
case TYPE_SLICE: {
Slice *s = (Slice *)v;
for (I64 i = 0; i < s->n; i++) {
if (!cgen_val_ptr_pre(g, (char *)s->data + (U64)i * compiler_sizeof(t->slice), t->slice, where))
return false;
}
if (!cgen_type_pre(g, t->slice, where)) return false;
cgen_write(g, "(d%p_[])", v); /* TODO: improve this somehow? */
if (!cgen_type_post(g, t->slice, where)) return false;
cgen_write(g, " = {");
for (I64 i = 0; i < s->n; i++) {
if (i) cgen_write(g, ", ");
if (!cgen_val_ptr(g, (char *)s->data + (U64)i * compiler_sizeof(t->slice), t->slice, where))
return false;
}
cgen_write(g, "};");
cgen_nl(g);
} break;
case TYPE_ARR:
for (size_t i = 0; i < t->arr.n; i++) {
if (!cgen_val_ptr_pre(g, (char *)*(void **)v + i * compiler_sizeof(t->arr.of), t->arr.of, where))
return false;
}
break;
case TYPE_USER:
if (!cgen_val_ptr_pre(g, v, type_inner(t), where))
return false;
break;
case TYPE_FN:
case TYPE_TYPE:
case TYPE_UNKNOWN:
case TYPE_TUPLE:
case TYPE_VOID:
case TYPE_BUILTIN:
case TYPE_PTR:
case TYPE_STRUCT:
break;
}
return true;
}
/* generate a value from a pointer */
static bool cgen_val_ptr(CGenerator *g, void *v, Type *t, Location where) {
switch (t->kind) {
case TYPE_TUPLE:
case TYPE_VOID:
case TYPE_TYPE:
assert(0);
return false;
case TYPE_UNKNOWN:
err_print(where, "Cannot determine type.");
return false;
case TYPE_ARR:
cgen_write(g, "{");
for (size_t i = 0; i < t->arr.n; i++) {
if (i) cgen_write(g, ", ");
if (!cgen_val_ptr(g, *(char **)v + i * compiler_sizeof(t->arr.of), t->arr.of, where))
return false;
}
cgen_write(g, "}");
break;
case TYPE_SLICE:
cgen_write(g, "{d%p_, %lu}", v, ((Slice *)v)->n);
break;
case TYPE_STRUCT:
err_print(where, "TODO");
/* TODO */
break;
case TYPE_FN:
cgen_fn_name(g, *(FnExpr **)v);
break;
case TYPE_PTR:
err_print(where, "Cannot bring compile time pointer to runtime.");
return false;
case TYPE_BUILTIN:
switch (t->builtin) {
case BUILTIN_I8: cgen_write(g, "%"PRId8, *(I8 *)v); break;
case BUILTIN_U8: cgen_write(g, "%"PRIu8, *(U8 *)v); break;
case BUILTIN_I16: cgen_write(g, "%"PRId16, *(I16 *)v); break;
case BUILTIN_U16: cgen_write(g, "%"PRIu16, *(U16 *)v); break;
case BUILTIN_I32: cgen_write(g, "%"PRId32, *(I32 *)v); break;
case BUILTIN_U32: cgen_write(g, "%"PRIu32, *(U32 *)v); break;
case BUILTIN_I64: cgen_write(g, "%"PRId64, *(I64 *)v); break;
case BUILTIN_U64: cgen_write(g, "%"PRIu64, *(U64 *)v); break;
case BUILTIN_F32: cgen_write(g, F32_FMT, *(F32 *)v); break;
case BUILTIN_F64: cgen_write(g, F64_FMT, *(F64 *)v); break;
case BUILTIN_CHAR: cgen_write(g, "\\x%02x", *(char *)v); break;
case BUILTIN_BOOL: cgen_write(g, "%s", *(bool *)v ? "true" : "false"); break;
}
break;
case TYPE_USER:
if (!cgen_val_ptr(g, v, type_inner(t), where))
return false;
break;
}
return true;
}
static bool cgen_val_pre(CGenerator *g, Value *v, Type *t, Location where) {
return cgen_val_ptr_pre(g, v, t, where);
}
/* generates a value fit for use as an initializer */
static bool cgen_val(CGenerator *g, Value *v, Type *t, Location where) {
/*
Because Value is a union, a pointer to v works as a pointer to any member.
As a result, this function is only needed for type checking.
*/
return cgen_val_ptr(g, v, t, where);
}
static bool cgen_decl(CGenerator *g, Declaration *d) {
int has_expr = d->flags & DECL_FLAG_HAS_EXPR;
bool is_tuple = d->type.kind == TYPE_TUPLE;
if (cgen_fn_is_direct(g, d)) {
if (!cgen_fn(g, &d->expr.fn, d->where))
return false;
} else if ((d->flags & DECL_FLAG_CONST) || g->block == NULL) {
/* declarations where we use a value */
for (size_t idx = 0; idx < arr_len(d->idents); idx++) {
Identifier i = d->idents[idx];
Type *type = is_tuple ? &d->type.tuple[idx] : &d->type;
Value *val = is_tuple ? &d->val.tuple[idx] : &d->val;
if (type->kind == TYPE_TYPE) {
/* mostly handled in typedefs_cgen, except for struct declarations */
if (val->type->kind == TYPE_STRUCT) {
cgen_write(g, "struct ");
if (g->block == NULL)
cgen_ident(g, i);
else
cgen_ident_id(g, d->c.ids[idx]);
cgen_write(g, "{");
cgen_nl(g);
g->indent_lvl++;
arr_foreach(val->type->struc.fields, Field, f) {
if (!cgen_type_pre(g, f->type, d->where)) return false;
cgen_write(g, " ");
cgen_ident(g, f->name);
if (!cgen_type_post(g, f->type, d->where)) return false;
cgen_write(g, ";");
cgen_nl(g);
}
g->indent_lvl--;
cgen_write(g, "};");
cgen_nl(g);
}
continue;
}
if (!cgen_val_pre(g, val, type, d->where))
return false;
if (g->block != NULL)
cgen_write(g, "static ");
if (!cgen_type_pre(g, type, d->where)) return false;
cgen_write(g, " ");
cgen_ident(g, i);
if (!cgen_type_post(g, type, d->where)) return false;
if (has_expr) {
cgen_write(g, " = ");
if (!cgen_val(g, val, type, d->where))
return false;
}
cgen_write(g, ";");
cgen_nl(g);
}
} else {
/* declarations where we use an expression */
for (size_t idx = 0; idx < arr_len(d->idents); idx++) {
Identifier i = d->idents[idx];
Type *type = d->type.kind == TYPE_TUPLE ? &d->type.tuple[idx] : &d->type;
if (!cgen_type_pre(g, type, d->where)) return false;
cgen_write(g, " ");
cgen_ident(g, i);
if (!cgen_type_post(g, type, d->where)) return false;
if (!has_expr) {
cgen_write(g, " = ");
cgen_zero_value(g, type);
}
cgen_write(g, "; ");
}
if (has_expr) {
if (d->expr.type.kind == TYPE_TUPLE) {
if (!cgen_set_tuple(g, NULL, d->idents, NULL, &d->expr)) return false;
} else {
cgen_write(g, "{");
cgen_nl(g);
if (!cgen_type_pre(g, &d->type, d->expr.where)) return false;
cgen_write(g, " expr__");
if (!cgen_type_post(g, &d->type, d->expr.where)) return false;
cgen_write(g, "; ");
if (!cgen_set(g, NULL, "expr__", &d->expr, NULL))
return false;
arr_foreach(d->idents, Identifier, i) {
Expression e;
e.flags = 0;
e.kind = EXPR_IDENT;
e.type = d->type;
e.ident = *i;
if (!cgen_set(g, &e, NULL, NULL, "expr__"))
return false;
}
cgen_write(g, "}");
}
}
cgen_nl(g);
}
return true;
}
static bool cgen_ret(CGenerator *g, Expression *ret) {
assert((g->fn->ret_type.kind == TYPE_VOID) == (ret == NULL));
if (!ret) {
cgen_write(g, "return");
} else if (cgen_uses_ptr(&g->fn->ret_type)) {
if (g->fn->ret_type.kind == TYPE_TUPLE) {
if (!cgen_set_tuple(g, NULL, NULL, "*ret", ret))
return false;
} else {
if (!cgen_set(g, NULL, "*ret_", ret, NULL)) return false;
}
cgen_write(g, "return");
} else {
if (!cgen_expr_pre(g, ret)) return false;
cgen_write(g, "return ");
if (!cgen_expr(g, ret)) return false;
}
cgen_write(g, ";");
cgen_nl(g);
return true;
}
static bool cgen_stmt(CGenerator *g, Statement *s) {
/*
TODO(eventually): optionally this:
*/
// cgen_write(g, "/* %s:%d */", s->where.ctx->filename, s->where.line);
/* (or even #line directives!) */
switch (s->kind) {
case STMT_DECL:
if (!cgen_decl(g, &s->decl)) return false;
break;
case STMT_EXPR:
if (!cgen_expr_pre(g, &s->expr)) return false;
if (!cgen_expr(g, &s->expr)) return false;
cgen_write(g, ";");
cgen_nl(g);
break;
case STMT_RET:
if (!cgen_ret(g, s->ret.flags & RET_HAS_EXPR ? &s->ret.expr : NULL))
return false;
break;
}
return true;
}
static bool cgen_file(CGenerator *g, ParsedFile *f) {
g->block = NULL;
g->file = f;
cgen_write(g, "#include <stdint.h>\n"
"#include <stdlib.h>\n"
"#include <stdio.h>\n"
"typedef int8_t i8;\n"
"typedef int16_t i16;\n"
"typedef int32_t i32;\n"
"typedef int64_t i64;\n"
"typedef uint8_t u8;\n"
"typedef uint16_t u16;\n"
"typedef uint32_t u32;\n"
"typedef uint64_t u64;\n"
"typedef float f32;\n"
"typedef double f64;\n"
"typedef u8 bool;\n"
"typedef struct { void *data; i64 n; } slice_;\n"
"static slice_ mkslice_(void *data, i64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n"
"static void *e__calloc(size_t n, size_t sz) { void *ret = calloc(n, sz); if (!ret) { fprintf(stderr, \"Out of memory.\\n\"); abort(); } return ret; }\n"
"#define false ((bool)0)\n"
"#define true ((bool)1)\n\n\n");
if (!typedefs_file(g, f))
return false;
if (!cgen_decls_file(g, f))
return false;
cgen_write(g, "/* code */\n");
cgen_write(g, "int main() {\n\tmain__();\n\treturn 0;\n}\n\n");
arr_foreach(f->stmts, Statement, s) {
if (!cgen_stmt(g, s))
return false;
}
typedef Expression *ExprPtr;
arr_foreach(g->anon_fns, ExprPtr, eptr) {
Expression *e = *eptr;
cgen_fn(g, &e->fn, e->where);
}
return true;
}
|