-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathc2mir.c
More file actions
14236 lines (13267 loc) · 524 KB
/
c2mir.c
File metadata and controls
14236 lines (13267 loc) · 524 KB
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
/* This file is a part of MIR project.
Copyright (C) 2018-2024 Vladimir Makarov <vmakarov.gcc@gmail.com>.
*/
/* C to MIR compiler. It is a four pass compiler:
o preprocessor pass generating tokens
o parsing pass generating AST
o context pass checking context constraints and augmenting AST
o generation pass producing MIR
The compiler implements C11 standard w/o C11 optional features:
atomic, complex, variable size arrays. */
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <setjmp.h>
#include <math.h>
#include <wchar.h>
#include "time.h"
#include "c2mir.h"
#if defined(__x86_64__) || defined(_M_AMD64)
#include "x86_64/cx86_64.h"
#elif defined(__aarch64__)
#include "aarch64/caarch64.h"
#elif defined(__PPC64__)
#include "ppc64/cppc64.h"
#elif defined(__s390x__)
#include "s390x/cs390x.h"
#elif defined(__riscv)
#include "riscv64/criscv64.h"
#else
#error "undefined or unsupported generation target for C"
#endif
#define SWAP(a1, a2, t) \
do { \
t = a1; \
a1 = a2; \
a2 = t; \
} while (0)
typedef enum {
C_alloc_error,
C_unfinished_comment,
C_out_of_range_number,
C_invalid_char_constant,
C_no_string_end,
C_invalid_str_constant,
C_invalid_char,
} C_error_code_t;
DEF_VARR (char);
#define pos_t c2mir_pos
static const pos_t no_pos = {NULL, -1, -1};
typedef struct c2m_ctx *c2m_ctx_t;
typedef struct stream {
FILE *f; /* the current file, NULL for top-level or string stream */
const char *fname; /* NULL only for preprocessor string stream */
int (*getc_func) (c2m_ctx_t); /* get function for top-level or string stream */
VARR (char) * ln; /* stream current line in reverse order */
pos_t pos; /* includes file name used for reports */
fpos_t fpos; /* file pos to resume file stream */
const char *start, *curr; /* non NULL only for string stream */
int ifs_length_at_stream_start; /* length of ifs at the stream start */
} *stream_t;
DEF_VARR (stream_t);
typedef const char *char_ptr_t;
DEF_VARR (char_ptr_t);
typedef void *void_ptr_t;
DEF_VARR (void_ptr_t);
typedef struct {
const char *s;
size_t len;
} str_t;
typedef struct {
str_t str;
size_t key, flags;
} tab_str_t;
DEF_HTAB (tab_str_t);
typedef struct token *token_t;
DEF_VARR (token_t);
typedef struct node *node_t;
enum symbol_mode { S_REGULAR, S_TAG, S_LABEL };
DEF_VARR (node_t);
typedef struct {
enum symbol_mode mode;
node_t id;
node_t scope;
node_t def_node, aux_node;
VARR (node_t) * defs;
} symbol_t;
DEF_HTAB (symbol_t);
struct init_object {
struct type *container_type;
int field_designator_p;
union {
mir_llong curr_index;
node_t curr_member;
} u;
};
typedef struct init_object init_object_t;
DEF_VARR (init_object_t);
typedef struct pre_ctx *pre_ctx_t;
typedef struct parse_ctx *parse_ctx_t;
typedef struct check_ctx *check_ctx_t;
typedef struct gen_ctx *gen_ctx_t;
DEF_VARR (pos_t);
struct c2m_ctx {
MIR_context_t ctx;
struct c2mir_options *options;
jmp_buf env; /* put it first as it might need 16-byte malloc allignment */
VARR (char_ptr_t) * headers;
VARR (char_ptr_t) * system_headers;
const char **header_dirs, **system_header_dirs;
void (*error_func) (c2m_ctx_t, C_error_code_t code, const char *message);
VARR (void_ptr_t) * reg_memory;
VARR (stream_t) * streams; /* stack of streams */
stream_t cs, eof_s; /* current stream and stream corresponding the last EOF */
HTAB (tab_str_t) * str_tab;
HTAB (tab_str_t) * str_key_tab;
str_t empty_str;
unsigned curr_uid;
int (*c_getc) (void *); /* c2mir interface get function */
void *c_getc_data;
unsigned n_errors, n_warnings;
VARR (char) * symbol_text, *temp_string;
VARR (token_t) * recorded_tokens, *buffered_tokens;
node_t top_scope;
HTAB (symbol_t) * symbol_tab;
VARR (pos_t) * node_positions;
VARR (node_t) * call_nodes;
VARR (node_t) * containing_anon_members;
VARR (init_object_t) * init_object_path;
char temp_str_buff[50];
struct pre_ctx *pre_ctx;
struct parse_ctx *parse_ctx;
struct check_ctx *check_ctx;
struct gen_ctx *gen_ctx;
};
typedef struct c2m_ctx *c2m_ctx_t;
#define c2m_options c2m_ctx->options
#define headers c2m_ctx->headers
#define system_headers c2m_ctx->system_headers
#define header_dirs c2m_ctx->header_dirs
#define system_header_dirs c2m_ctx->system_header_dirs
#define error_func c2m_ctx->error_func
#define reg_memory c2m_ctx->reg_memory
#define str_tab c2m_ctx->str_tab
#define streams c2m_ctx->streams
#define cs c2m_ctx->cs
#define eof_s c2m_ctx->eof_s
#define str_key_tab c2m_ctx->str_key_tab
#define empty_str c2m_ctx->empty_str
#define curr_uid c2m_ctx->curr_uid
#define c_getc c2m_ctx->c_getc
#define c_getc_data c2m_ctx->c_getc_data
#define n_errors c2m_ctx->n_errors
#define n_warnings c2m_ctx->n_warnings
#define symbol_text c2m_ctx->symbol_text
#define temp_string c2m_ctx->temp_string
#define recorded_tokens c2m_ctx->recorded_tokens
#define buffered_tokens c2m_ctx->buffered_tokens
#define top_scope c2m_ctx->top_scope
#define symbol_tab c2m_ctx->symbol_tab
#define node_positions c2m_ctx->node_positions
#define call_nodes c2m_ctx->call_nodes
#define containing_anon_members c2m_ctx->containing_anon_members
#define init_object_path c2m_ctx->init_object_path
#define temp_str_buff c2m_ctx->temp_str_buff
static inline c2m_ctx_t *c2m_ctx_loc (MIR_context_t ctx) {
return (c2m_ctx_t *) ((void **) ctx + 1);
}
static void alloc_error (c2m_ctx_t c2m_ctx, const char *message) {
error_func (c2m_ctx, C_alloc_error, message);
}
static const int max_nested_includes = 32;
#define MIR_VARR_ERROR alloc_error
#define MIR_HTAB_ERROR MIR_VARR_ERROR
#define FALSE 0
#define TRUE 1
#include "mir-varr.h"
#include "mir-dlist.h"
#include "mir-hash.h"
#include "mir-htab.h"
static mir_size_t round_size (mir_size_t size, mir_size_t round) {
return (size + round - 1) / round * round;
}
/* Some abbreviations: */
#define NL_HEAD(list) DLIST_HEAD (node_t, list)
#define NL_TAIL(list) DLIST_TAIL (node_t, list)
#define NL_LENGTH(list) DLIST_LENGTH (node_t, list)
#define NL_NEXT(el) DLIST_NEXT (node_t, el)
#define NL_PREV(el) DLIST_PREV (node_t, el)
#define NL_REMOVE(list, el) DLIST_REMOVE (node_t, list, el)
#define NL_APPEND(list, el) DLIST_APPEND (node_t, list, el)
#define NL_PREPEND(list, el) DLIST_PREPEND (node_t, list, el)
#define NL_EL(list, n) DLIST_EL (node_t, list, n)
enum basic_type {
TP_UNDEF,
TP_VOID,
/* Integer types: the first should be BOOL and the last should be
ULLONG. The order is important -- do not change it. */
TP_BOOL,
TP_CHAR,
TP_SCHAR,
TP_UCHAR,
TP_SHORT,
TP_USHORT,
TP_INT,
TP_UINT,
TP_LONG,
TP_ULONG,
TP_LLONG,
TP_ULLONG,
TP_FLOAT,
TP_DOUBLE,
TP_LDOUBLE,
};
struct type_qual {
unsigned int const_p : 1, restrict_p : 1, volatile_p : 1, atomic_p : 1; /* Type qualifiers */
};
static const struct type_qual zero_type_qual = {0, 0, 0, 0};
struct arr_type {
unsigned int static_p : 1;
struct type *el_type;
struct type_qual ind_type_qual;
node_t size;
};
struct func_type {
unsigned int dots_p : 1;
struct type *ret_type;
node_t param_list; /* w/o N_DOTS */
MIR_item_t proto_item;
};
enum type_mode {
TM_UNDEF,
TM_BASIC,
TM_ENUM,
TM_PTR,
TM_STRUCT,
TM_UNION,
TM_ARR,
TM_FUNC,
};
struct type {
node_t pos_node; /* set up and used only for checking type correctness */
struct type *arr_type; /* NULL or array type before its adjustment */
MIR_alias_t antialias; /* it can be non-zero only for pointers */
struct type_qual type_qual;
enum type_mode mode;
char func_type_before_adjustment_p;
char unnamed_anon_struct_union_member_type_p;
int align; /* type align, undefined if < 0 */
/* Raw type size (w/o alignment type itself requirement but with
element alignment requirements), undefined if mir_size_max. */
mir_size_t raw_size;
union {
enum basic_type basic_type; /* also integer type */
node_t tag_type; /* struct/union/enum */
struct type *ptr_type;
struct arr_type *arr_type;
struct func_type *func_type;
} u;
};
/*!*/ static struct type VOID_TYPE
= {.raw_size = MIR_SIZE_MAX, .align = -1, .mode = TM_BASIC, .u = {.basic_type = TP_VOID}};
static void set_type_layout (c2m_ctx_t c2m_ctx, struct type *type);
static mir_size_t raw_type_size (c2m_ctx_t c2m_ctx, struct type *type) {
if (type->raw_size == MIR_SIZE_MAX) set_type_layout (c2m_ctx, type);
if (n_errors != 0 && type->raw_size == MIR_SIZE_MAX) {
/* Use safe values for programs with errors: */
type->raw_size = 0;
type->align = 1;
}
assert (type->raw_size != MIR_SIZE_MAX);
return type->raw_size;
}
typedef struct {
const char *name, *content;
} string_include_t;
#if defined(__x86_64__) || defined(_M_AMD64)
#include "x86_64/cx86_64-code.c"
#elif defined(__aarch64__)
#include "aarch64/caarch64-code.c"
#elif defined(__PPC64__)
#include "ppc64/cppc64-code.c"
#elif defined(__s390x__)
#include "s390x/cs390x-code.c"
#elif defined(__riscv)
#include "riscv64/criscv64-code.c"
#else
#error "undefined or unsupported generation target for C"
#endif
static void *reg_malloc (c2m_ctx_t c2m_ctx, size_t s) {
void *mem = malloc (s);
if (mem == NULL) alloc_error (c2m_ctx, "no memory");
VARR_PUSH (void_ptr_t, reg_memory, mem);
return mem;
}
static void reg_memory_pop (c2m_ctx_t c2m_ctx, size_t mark) {
while (VARR_LENGTH (void_ptr_t, reg_memory) > mark) free (VARR_POP (void_ptr_t, reg_memory));
}
static size_t MIR_UNUSED reg_memory_mark (c2m_ctx_t c2m_ctx) {
return VARR_LENGTH (void_ptr_t, reg_memory);
}
static void reg_memory_finish (c2m_ctx_t c2m_ctx) {
reg_memory_pop (c2m_ctx, 0);
VARR_DESTROY (void_ptr_t, reg_memory);
}
static void reg_memory_init (c2m_ctx_t c2m_ctx) { VARR_CREATE (void_ptr_t, reg_memory, 4096); }
static int char_is_signed_p (void) { return MIR_CHAR_MAX == MIR_SCHAR_MAX; }
enum str_flag { FLAG_EXT = 1, FLAG_C89, FLAG_EXT89 };
static int str_eq (tab_str_t str1, tab_str_t str2, void *arg MIR_UNUSED) {
return str1.str.len == str2.str.len && memcmp (str1.str.s, str2.str.s, str1.str.len) == 0;
}
static htab_hash_t str_hash (tab_str_t str, void *arg MIR_UNUSED) {
return (htab_hash_t) mir_hash (str.str.s, str.str.len, 0x42);
}
static int str_key_eq (tab_str_t str1, tab_str_t str2, void *arg MIR_UNUSED) {
return str1.key == str2.key;
}
static htab_hash_t str_key_hash (tab_str_t str, void *arg MIR_UNUSED) {
return (htab_hash_t) mir_hash64 (str.key, 0x24);
}
static str_t uniq_cstr (c2m_ctx_t c2m_ctx, const char *str);
static void str_init (c2m_ctx_t c2m_ctx) {
HTAB_CREATE (tab_str_t, str_tab, 1000, str_hash, str_eq, NULL);
HTAB_CREATE (tab_str_t, str_key_tab, 200, str_key_hash, str_key_eq, NULL);
empty_str = uniq_cstr (c2m_ctx, "");
}
static int str_exists_p (c2m_ctx_t c2m_ctx, const char *s, size_t len, tab_str_t *tab_str) {
tab_str_t el, str;
str.str.s = s;
str.str.len = len;
if (!HTAB_DO (tab_str_t, str_tab, str, HTAB_FIND, el)) return FALSE;
*tab_str = el;
return TRUE;
}
static tab_str_t str_add (c2m_ctx_t c2m_ctx, const char *s, size_t len, size_t key, size_t flags,
int key_p) {
char *heap_s;
tab_str_t el, str;
if (str_exists_p (c2m_ctx, s, len, &el)) return el;
heap_s = reg_malloc (c2m_ctx, len);
memcpy (heap_s, s, len);
str.str.s = heap_s;
str.str.len = len;
str.key = key;
str.flags = flags;
HTAB_DO (tab_str_t, str_tab, str, HTAB_INSERT, el);
if (key_p) HTAB_DO (tab_str_t, str_key_tab, str, HTAB_INSERT, el);
return str;
}
static const char *str_find_by_key (c2m_ctx_t c2m_ctx, size_t key) {
tab_str_t el, str;
str.key = key;
if (!HTAB_DO (tab_str_t, str_key_tab, str, HTAB_FIND, el)) return NULL;
return el.str.s;
}
static void str_finish (c2m_ctx_t c2m_ctx) {
HTAB_DESTROY (tab_str_t, str_tab);
HTAB_DESTROY (tab_str_t, str_key_tab);
}
static void *c2mir_calloc (c2m_ctx_t c2m_ctx, size_t size) {
void *res = calloc (1, size);
if (res == NULL) (*MIR_get_error_func (c2m_ctx->ctx)) (MIR_alloc_error, "no memory");
return res;
}
void c2mir_init (MIR_context_t ctx) {
struct c2m_ctx **c2m_ctx_ptr = c2m_ctx_loc (ctx), *c2m_ctx;
*c2m_ctx_ptr = c2m_ctx = c2mir_calloc (NULL, sizeof (struct c2m_ctx));
c2m_ctx->ctx = ctx;
reg_memory_init (c2m_ctx);
str_init (c2m_ctx);
}
void c2mir_finish (MIR_context_t ctx) {
struct c2m_ctx **c2m_ctx_ptr = c2m_ctx_loc (ctx), *c2m_ctx = *c2m_ctx_ptr;
str_finish (c2m_ctx);
reg_memory_finish (c2m_ctx);
free (c2m_ctx);
*c2m_ctx_ptr = NULL;
}
/* New Page */
/* ------------------------- Parser Start ------------------------------ */
/* Parser is manually written parser with back-tracing to keep original
grammar close to C11 standard grammar as possible. It has a
rudimentary syntax error recovery based on stop symbols ';' and
'}'. The input is parse tokens and the output is the following AST
nodes (the AST root is transl_unit):
const : N_I | N_L | N_LL | N_U | N_UL | N_ULL | N_F | N_D | N_LD
| N_CH | N_CH16 | N_CH32 | N_STR | N_STR16 | N_STR32
expr : const | N_ID | N_LABEL_ADDR (N_ID) | N_ADD (expr)
| N_SUB (expr) | N_ADD (expr, expr) | N_SUB (expr, expr)
| N_MUL (expr, expr) | N_DIV (expr, expr) | N_MOD (expr, expr)
| N_LSH (expr, expr) | N_RSH (expr, expr)
| N_NOT (expr) | N_BITWISE_NOT (expr)
| N_INC (expr) | N_DEC (expr) | N_POST_INC (expr)| N_POST_DEC (expr)
| N_ALIGNOF (type_name?) | N_SIZEOF (type_name) | N_EXPR_SIZEOF (expr)
| N_CAST (type_name, expr) | N_COMMA (expr, expr) | N_ANDAND (expr, expr)
| N_OROR (expr, expr) | N_EQ (expr, expr) | N_NE (expr, expr)
| N_LT (expr, expr) | N_LE (expr, expr) | N_GT (expr, expr) | N_GE (expr, expr)
| N_AND (expr, expr) | N_OR (expr, expr) | N_XOR (expr, expr)
| N_ASSIGN (expr, expr) | N_ADD_ASSIGN (expr, expr) | N_SUB_ASSIGN (expr, expr)
| N_MUL_ASSIGN (expr, expr) | N_DIV_ASSIGN (expr, expr) | N_MOD_ASSIGN (expr, expr)
| N_LSH_ASSIGN (expr, expr) | N_RSH_ASSIGN (expr, expr)
| N_AND_ASSIGN (expr, expr) | N_OR_ASSIGN (expr, expr) | N_XOR_ASSIGN (expr, expr)
| N_DEREF (expr) | | N_ADDR (expr) | N_IND (expr, expr) | N_FIELD (expr, N_ID)
| N_DEREF_FIELD (expr, N_ID) | N_COND (expr, expr, expr)
| N_COMPOUND_LITERAL (type_name, initializer) | N_CALL (expr, N_LIST:(expr)*)
| N_GENERIC (expr, N_LIST:(N_GENERIC_ASSOC (type_name?, expr))+ )
| N_STMTEXPR (compound_stmt)
label: N_CASE(expr) | N_CASE(expr,expr) | N_DEFAULT | N_LABEL(N_ID)
stmt: compound_stmt | N_IF(N_LIST:(label)*, expr, stmt, stmt?)
| N_SWITCH(N_LIST:(label)*, expr, stmt) | (N_WHILE|N_DO) (N_LIST:(label)*, expr, stmt)
| N_FOR(N_LIST:(label)*,(N_LIST: declaration+ | expr)?, expr?, expr?, stmt)
| N_GOTO(N_LIST:(label)*, N_ID) | N_INDIRECT_GOTO(N_LIST:(label)*, expr)
| (N_CONTINUE|N_BREAK) (N_LIST:(label)*)
| N_RETURN(N_LIST:(label)*, expr?) | N_EXPR(N_LIST:(label)*, expr)
compound_stmt: N_BLOCK(N_LIST:(label)*, N_LIST:(declaration | stmt)*)
asm: N_ASM(N_STR | N_STR16 | N_STR32)
attr_arg: const | N_ID
attr: N_ATTR(N_ID, NLIST:(attr_arg)*)
attrs: N_LIST:(attrs)*
declaration: N_SPEC_DECL(N_SHARE(declaration_specs), declarator?, attrs?, asm?, initializer?)
| st_assert
st_assert: N_ST_ASSERT(const_expr, N_STR | N_STR16 | N_STR32)
declaration_specs: N_LIST:(align_spec|sc_spec|type_qual|func_spec|type_spec|attr)*
align_spec: N_ALIGNAS(type_name|const_expr)
sc_spec: N_TYPEDEF|N_EXTERN|N_STATIC|N_AUTO|N_REGISTER|N_THREAD_LOCAL
type_qual: N_CONST|N_RESTRICT|N_VOLATILE|N_ATOMIC
func_spec: N_INLINE|N_NO_RETURN
type_spec: N_VOID|N_CHAR|N_SHORT|N_INT|N_LONG|N_FLOAT|N_DOUBLE|N_SIGNED|N_UNSIGNED|N_BOOL
| (N_STRUCT|N_UNION) (N_ID?, struct_declaration_list?)
| N_ENUM(N_ID?, N_LIST?: N_ENUM_COST(N_ID, const_expr?)*) | typedef_name
struct_declaration_list: N_LIST: struct_declaration*
struct_declaration: st_assert | N_MEMBER(N_SHARE(spec_qual_list), declarator?, attrs?, const_expr?)
spec_qual_list: N_LIST:(type_qual|type_spec)*
declarator: the same as direct declarator
direct_declarator: N_DECL(N_ID,
N_LIST:(N_POINTER(type_qual_list) | N_FUNC(id_list|parameter_list)
| N_ARR(N_STATIC?, type_qual_list,
(assign_expr|N_STAR)?))*)
pointer: N_LIST: N_POINTER(type_qual_list)*
type_qual_list : N_LIST: type_qual*
parameter_type_list: N_LIST:(N_SPEC_DECL(declaration_specs, declarator, attrs?, ignore, ignore)
| N_TYPE(declaration_specs, abstract_declarator))+ [N_DOTS]
id_list: N_LIST: N_ID*
initializer: assign_expr | initialize_list
initializer_list: N_LIST: N_INIT(N_LIST:(const_expr | N_FIELD_ID (N_ID))* initializer)*
type_name: N_TYPE(spec_qual_list, abstract_declarator)
abstract_declarator: the same as abstract direct declarator
abstract_direct_declarator: N_DECL(ignore,
N_LIST:(N_POINTER(type_qual_list) | N_FUNC(parameter_list)
| N_ARR(N_STATIC?, type_qual_list,
(assign_expr|N_STAR)?))*)
typedef_name: N_ID
transl_unit: N_MODULE(N_LIST:(declaration
| N_FUNC_DEF(declaration_specs, declarator,
N_LIST: declaration*, compound_stmt))*)
Here ? means it can be N_IGNORE, * means 0 or more elements in the list, + means 1 or more.
*/
#define REP_SEP ,
#define T_EL(t) T_##t
typedef enum {
T_NUMBER = 256,
REP8 (T_EL, CH, STR, ID, ASSIGN, DIVOP, ADDOP, SH, CMP),
REP8 (T_EL, EQNE, ANDAND, OROR, INCDEC, ARROW, UNOP, DOTS, BOOL),
REP8 (T_EL, COMPLEX, ALIGNOF, ALIGNAS, ATOMIC, GENERIC, NO_RETURN, STATIC_ASSERT, THREAD_LOCAL),
REP8 (T_EL, THREAD, AUTO, BREAK, CASE, CHAR, CONST, CONTINUE, DEFAULT),
REP8 (T_EL, DO, DOUBLE, ELSE, ENUM, EXTERN, FLOAT, FOR, GOTO),
REP8 (T_EL, IF, INLINE, INT, LONG, REGISTER, RESTRICT, RETURN, SHORT),
REP8 (T_EL, SIGNED, SIZEOF, STATIC, STRUCT, SWITCH, TYPEDEF, TYPEOF, UNION),
REP5 (T_EL, UNSIGNED, VOID, VOLATILE, WHILE, EOFILE),
/* tokens existing in preprocessor only: */
T_HEADER, /* include header */
T_NO_MACRO_IDENT, /* ??? */
T_DBLNO, /* ## */
T_PLM,
T_RDBLNO, /* placemarker, ## in replacement list */
T_BOA, /* begin of argument */
T_EOA,
T_EOR, /* end of argument and macro replacement */
T_EOP, /* end of processing */
T_EOU, /* end of translation unit */
} token_code_t;
static token_code_t FIRST_KW = T_BOOL, LAST_KW = T_WHILE;
#define NODE_EL(n) N_##n
typedef enum {
REP8 (NODE_EL, IGNORE, I, L, LL, U, UL, ULL, F),
REP8 (NODE_EL, D, LD, CH, CH16, CH32, STR, STR16, STR32),
REP5 (NODE_EL, ID, COMMA, ANDAND, OROR, STMTEXPR),
REP8 (NODE_EL, EQ, NE, LT, LE, GT, GE, ASSIGN, BITWISE_NOT),
REP8 (NODE_EL, NOT, AND, AND_ASSIGN, OR, OR_ASSIGN, XOR, XOR_ASSIGN, LSH),
REP8 (NODE_EL, LSH_ASSIGN, RSH, RSH_ASSIGN, ADD, ADD_ASSIGN, SUB, SUB_ASSIGN, MUL),
REP8 (NODE_EL, MUL_ASSIGN, DIV, DIV_ASSIGN, MOD, MOD_ASSIGN, IND, FIELD, ADDR),
REP8 (NODE_EL, DEREF, DEREF_FIELD, COND, INC, DEC, POST_INC, POST_DEC, ALIGNOF),
REP8 (NODE_EL, SIZEOF, EXPR_SIZEOF, CAST, COMPOUND_LITERAL, CALL, GENERIC, GENERIC_ASSOC, IF),
REP8 (NODE_EL, SWITCH, WHILE, DO, FOR, GOTO, INDIRECT_GOTO, CONTINUE, BREAK),
REP8 (NODE_EL, RETURN, EXPR, BLOCK, CASE, DEFAULT, LABEL, LABEL_ADDR, LIST),
REP8 (NODE_EL, SPEC_DECL, SHARE, TYPEDEF, EXTERN, STATIC, AUTO, REGISTER, THREAD_LOCAL),
REP8 (NODE_EL, DECL, VOID, CHAR, SHORT, INT, LONG, FLOAT, DOUBLE),
REP8 (NODE_EL, SIGNED, UNSIGNED, BOOL, STRUCT, UNION, ENUM, ENUM_CONST, MEMBER),
REP8 (NODE_EL, CONST, RESTRICT, VOLATILE, ATOMIC, INLINE, NO_RETURN, ALIGNAS, FUNC),
REP8 (NODE_EL, STAR, POINTER, DOTS, ARR, INIT, FIELD_ID, TYPE, ST_ASSERT),
REP4 (NODE_EL, FUNC_DEF, MODULE, ASM, ATTR),
} node_code_t;
#undef REP_SEP
DEF_DLIST_LINK (node_t);
DEF_DLIST_TYPE (node_t);
struct node {
node_code_t code;
unsigned uid;
void *attr; /* used a scope for parser and as an attribute after */
DLIST_LINK (node_t) op_link;
union {
str_t s;
mir_char ch;
mir_long l;
mir_llong ll;
mir_ulong ul; /* includes CH16 and CH32 */
mir_ullong ull;
mir_float f;
mir_double d;
mir_ldouble ld;
DLIST (node_t) ops;
} u;
};
static pos_t get_node_pos (c2m_ctx_t c2m_ctx, node_t n) {
return VARR_GET (pos_t, node_positions, n->uid);
}
#define POS(n) get_node_pos (c2m_ctx, n)
static void set_node_pos (c2m_ctx_t c2m_ctx, node_t n, pos_t pos) {
while (n->uid >= VARR_LENGTH (pos_t, node_positions)) VARR_PUSH (pos_t, node_positions, no_pos);
VARR_SET (pos_t, node_positions, n->uid, pos);
}
DEF_DLIST_CODE (node_t, op_link);
struct token {
int code : 16; /* token_code_t and EOF */
int processed_p : 16;
pos_t pos;
node_code_t node_code;
node_t node;
const char *repr;
};
static node_t add_pos (c2m_ctx_t c2m_ctx, node_t n, pos_t p) {
if (POS (n).lno < 0) set_node_pos (c2m_ctx, n, p);
return n;
}
static node_t op_append (c2m_ctx_t c2m_ctx, node_t n, node_t op) {
NL_APPEND (n->u.ops, op);
return add_pos (c2m_ctx, n, POS (op));
}
static node_t op_prepend (c2m_ctx_t c2m_ctx, node_t n, node_t op) {
NL_PREPEND (n->u.ops, op);
return add_pos (c2m_ctx, n, POS (op));
}
static void op_flat_append (c2m_ctx_t c2m_ctx, node_t n, node_t op) {
if (op->code != N_LIST) {
op_append (c2m_ctx, n, op);
return;
}
for (node_t next_el, el = NL_HEAD (op->u.ops); el != NULL; el = next_el) {
next_el = NL_NEXT (el);
NL_REMOVE (op->u.ops, el);
op_append (c2m_ctx, n, el);
}
}
static node_t new_node (c2m_ctx_t c2m_ctx, node_code_t nc) {
node_t n = reg_malloc (c2m_ctx, sizeof (struct node));
n->code = nc;
n->uid = curr_uid++;
DLIST_INIT (node_t, n->u.ops);
n->attr = NULL;
set_node_pos (c2m_ctx, n, no_pos);
return n;
}
static node_t copy_node_with_pos (c2m_ctx_t c2m_ctx, node_t n, pos_t pos) {
node_t r = new_node (c2m_ctx, n->code);
set_node_pos (c2m_ctx, r, pos);
r->u = n->u;
return r;
}
static node_t copy_node (c2m_ctx_t c2m_ctx, node_t n) {
return copy_node_with_pos (c2m_ctx, n, POS (n));
}
static node_t new_pos_node (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p) {
return add_pos (c2m_ctx, new_node (c2m_ctx, nc), p);
}
static node_t new_node1 (c2m_ctx_t c2m_ctx, node_code_t nc, node_t op1) {
return op_append (c2m_ctx, new_node (c2m_ctx, nc), op1);
}
static node_t new_pos_node1 (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p, node_t op1) {
return add_pos (c2m_ctx, new_node1 (c2m_ctx, nc, op1), p);
}
static node_t new_node2 (c2m_ctx_t c2m_ctx, node_code_t nc, node_t op1, node_t op2) {
return op_append (c2m_ctx, new_node1 (c2m_ctx, nc, op1), op2);
}
static node_t new_pos_node2 (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p, node_t op1, node_t op2) {
return add_pos (c2m_ctx, new_node2 (c2m_ctx, nc, op1, op2), p);
}
static node_t new_node3 (c2m_ctx_t c2m_ctx, node_code_t nc, node_t op1, node_t op2, node_t op3) {
return op_append (c2m_ctx, new_node2 (c2m_ctx, nc, op1, op2), op3);
}
static node_t new_pos_node3 (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p, node_t op1, node_t op2,
node_t op3) {
return add_pos (c2m_ctx, new_node3 (c2m_ctx, nc, op1, op2, op3), p);
}
static node_t new_node4 (c2m_ctx_t c2m_ctx, node_code_t nc, node_t op1, node_t op2, node_t op3,
node_t op4) {
return op_append (c2m_ctx, new_node3 (c2m_ctx, nc, op1, op2, op3), op4);
}
static node_t new_pos_node4 (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p, node_t op1, node_t op2,
node_t op3, node_t op4) {
return add_pos (c2m_ctx, new_node4 (c2m_ctx, nc, op1, op2, op3, op4), p);
}
static node_t new_node5 (c2m_ctx_t c2m_ctx, node_code_t nc, node_t op1, node_t op2, node_t op3,
node_t op4, node_t op5) {
return op_append (c2m_ctx, new_node4 (c2m_ctx, nc, op1, op2, op3, op4), op5);
}
static node_t new_pos_node5 (c2m_ctx_t c2m_ctx, node_code_t nc, pos_t p, node_t op1, node_t op2,
node_t op3, node_t op4, node_t op5) {
return add_pos (c2m_ctx, new_node5 (c2m_ctx, nc, op1, op2, op3, op4, op5), p);
}
static node_t new_ch_node (c2m_ctx_t c2m_ctx, int ch, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_CH, p);
n->u.ch = ch;
return n;
}
static node_t new_ch16_node (c2m_ctx_t c2m_ctx, mir_ulong ch, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_CH16, p);
n->u.ul = ch;
return n;
}
static node_t new_ch32_node (c2m_ctx_t c2m_ctx, mir_ulong ch, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_CH32, p);
n->u.ul = ch;
return n;
}
static node_t new_i_node (c2m_ctx_t c2m_ctx, long l, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_I, p);
n->u.l = l;
return n;
}
static node_t new_l_node (c2m_ctx_t c2m_ctx, long l, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_L, p);
n->u.l = l;
return n;
}
static node_t new_ll_node (c2m_ctx_t c2m_ctx, long long ll, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_LL, p);
n->u.ll = ll;
return n;
}
static node_t new_u_node (c2m_ctx_t c2m_ctx, unsigned long ul, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_U, p);
n->u.ul = ul;
return n;
}
static node_t new_ul_node (c2m_ctx_t c2m_ctx, unsigned long ul, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_UL, p);
n->u.ul = ul;
return n;
}
static node_t new_ull_node (c2m_ctx_t c2m_ctx, unsigned long long ull, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_ULL, p);
n->u.ull = ull;
return n;
}
static node_t new_f_node (c2m_ctx_t c2m_ctx, float f, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_F, p);
n->u.f = f;
return n;
}
static node_t new_d_node (c2m_ctx_t c2m_ctx, double d, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_D, p);
n->u.d = d;
return n;
}
static node_t new_ld_node (c2m_ctx_t c2m_ctx, long double ld, pos_t p) {
node_t n = new_pos_node (c2m_ctx, N_LD, p);
n->u.ld = ld;
return n;
}
static node_t new_str_node (c2m_ctx_t c2m_ctx, node_code_t nc, str_t s, pos_t p) {
node_t n = new_pos_node (c2m_ctx, nc, p);
n->u.s = s;
return n;
}
static node_t get_op (node_t n, int nop) {
n = NL_HEAD (n->u.ops);
for (; nop > 0; nop--) n = NL_NEXT (n);
return n;
}
static str_t uniq_cstr (c2m_ctx_t c2m_ctx, const char *str) {
return str_add (c2m_ctx, str, strlen (str) + 1, T_STR, 0, FALSE).str;
}
static str_t uniq_str (c2m_ctx_t c2m_ctx, const char *str, size_t len) {
return str_add (c2m_ctx, str, len, T_STR, 0, FALSE).str;
}
static token_t new_token (c2m_ctx_t c2m_ctx, pos_t pos, const char *repr, int token_code,
node_code_t node_code) {
token_t token = reg_malloc (c2m_ctx, sizeof (struct token));
token->code = token_code;
token->processed_p = FALSE;
token->pos = pos;
token->repr = repr;
token->node_code = node_code;
token->node = NULL;
return token;
}
static token_t copy_token (c2m_ctx_t c2m_ctx, token_t t, pos_t pos) {
token_t token = new_token (c2m_ctx, pos, t->repr, t->code, t->node_code);
if (t->node != NULL) token->node = copy_node_with_pos (c2m_ctx, t->node, pos);
return token;
}
static token_t new_token_wo_uniq_repr (c2m_ctx_t c2m_ctx, pos_t pos, const char *repr,
int token_code, node_code_t node_code) {
return new_token (c2m_ctx, pos, uniq_cstr (c2m_ctx, repr).s, token_code, node_code);
}
static token_t new_node_token (c2m_ctx_t c2m_ctx, pos_t pos, const char *repr, int token_code,
node_t node) {
token_t token = new_token_wo_uniq_repr (c2m_ctx, pos, repr, token_code, N_IGNORE);
token->node = node;
return token;
}
static void print_pos (FILE *f, pos_t pos, int col_p) {
if (pos.lno < 0) return;
fprintf (f, "%s:%d", pos.fname, pos.lno);
if (col_p) fprintf (f, ":%d: ", pos.ln_pos);
}
static const char *get_token_name (c2m_ctx_t c2m_ctx, int token_code) {
const char *s;
switch (token_code) {
case T_NUMBER: return "number";
case T_CH: return "char constant";
case T_STR: return "string";
case T_ID: return "identifier";
case T_ASSIGN: return "assign op";
case T_DIVOP: return "/ or %";
case T_ADDOP: return "+ or -";
case T_SH: return "shift op";
case T_CMP: return "comparison op";
case T_EQNE: return "equality op";
case T_ANDAND: return "&&";
case T_OROR: return "||";
case T_INCDEC: return "++ or --";
case T_ARROW: return "->";
case T_UNOP: return "unary op";
case T_DOTS: return "...";
default:
if ((s = str_find_by_key (c2m_ctx, token_code)) != NULL) return s;
if (token_code < 256 && isprint (token_code))
sprintf (temp_str_buff, "%c", token_code);
else
sprintf (temp_str_buff, "%d", token_code);
return temp_str_buff;
}
}
static int log_message (c2m_ctx_t c2m_ctx, c2mir_log_type type, pos_t *pos, const char *message, va_list args) {
int logged = 0;
FILE *f;
if ((f = c2m_options->message_file) != NULL) {
if (pos && pos->lno >= 0) {
fprintf (f, "%s: %d:%d: ", pos->fname, pos->lno, pos->ln_pos);
}
switch (type) {
case log_warning: fprintf (f, "warning: "); break;
case log_error: fprintf (f, "error: "); break;
case log_fatal_error: fprintf (f, "fatal error: "); break;
}
vfprintf (f, message, args);
fprintf (f, "\n");
logged++;
}
if (c2m_options->console_log) {
c2m_options->console_log (c2m_options->console_instance, type, pos, message, args);
logged++;
}
return logged;
}
static void fatal_error (c2m_ctx_t c2m_ctx, C_error_code_t code MIR_UNUSED, const char *format, ...) {
va_list args;
va_start (args, format);
log_message (c2m_ctx, log_fatal_error, NULL, format, args);
va_end (args);
longjmp (c2m_ctx->env, 1);
}
static void syntax_error_core (c2m_ctx_t c2m_ctx, pos_t pos, const char *format, ...) {
n_errors++;
va_list args;
va_start (args, format);
log_message (c2m_ctx, log_syntax_error, &pos, format, args);
va_end (args);
}
static void error (c2m_ctx_t c2m_ctx, pos_t pos, const char *format, ...) {
n_errors++;
va_list args;
va_start (args, format);
log_message (c2m_ctx, log_error, &pos, format, args);
va_end (args);
}
static void warning (c2m_ctx_t c2m_ctx, pos_t pos, const char *format, ...) {
n_warnings++;
if (c2m_options->ignore_warnings_p) return;
va_list args;
va_start (args, format);
log_message (c2m_ctx, log_warning, &pos, format, args);
va_end (args);
}
static void verbose (c2m_ctx_t c2m_ctx, const char *format, ...) {
if (c2m_options->verbose_p) {
va_list args;
va_start (args, format);
log_message (c2m_ctx, log_verbose, NULL, format, args);
va_end (args);
}
}
#define TAB_STOP 8
static void init_streams (c2m_ctx_t c2m_ctx) {
cs = eof_s = NULL;
VARR_CREATE (stream_t, streams, 32);
}
static void free_stream (stream_t s) {
VARR_DESTROY (char, s->ln);
free (s);
}
static void finish_streams (c2m_ctx_t c2m_ctx) {
if (eof_s != NULL) free_stream (eof_s);
if (streams == NULL) return;
while (VARR_LENGTH (stream_t, streams) != 0) free_stream (VARR_POP (stream_t, streams));
VARR_DESTROY (stream_t, streams);
}
static stream_t new_stream (FILE *f, const char *fname, int (*getc_func) (c2m_ctx_t)) {
stream_t s = malloc (sizeof (struct stream));
VARR_CREATE (char, s->ln, 128);
s->f = f;
s->fname = s->pos.fname = fname;
s->pos.lno = 0;
s->pos.ln_pos = 0;
s->ifs_length_at_stream_start = 0;
s->start = s->curr = NULL;
s->getc_func = getc_func;
return s;
}
static void add_stream (c2m_ctx_t c2m_ctx, FILE *f, const char *fname,
int (*getc_func) (c2m_ctx_t)) {
assert (fname != NULL);
if (cs != NULL && cs->f != NULL && cs->f != stdin) {
fgetpos (cs->f, &cs->fpos);
fclose (cs->f);
cs->f = NULL;
}
cs = new_stream (f, fname, getc_func);
VARR_PUSH (stream_t, streams, cs);
}
static int str_getc (c2m_ctx_t c2m_ctx) {
if (*cs->curr == '\0') return EOF;
return *cs->curr++;
}
static void add_string_stream (c2m_ctx_t c2m_ctx, const char *pos_fname, const char *str) {
add_stream (c2m_ctx, NULL, pos_fname, str_getc);
cs->start = cs->curr = str;
}
static int string_stream_p (stream_t s) { return s->getc_func != NULL; }
static void change_stream_pos (c2m_ctx_t c2m_ctx, pos_t pos) { cs->pos = pos; }
static void remove_trigraphs (c2m_ctx_t c2m_ctx) {
int len = (int) VARR_LENGTH (char, cs->ln);
char *addr = VARR_ADDR (char, cs->ln);
int i, start, to, ch;