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
|
#ifndef DUCC_AST_H
#define DUCC_AST_H
#include "std.h"
enum StorageClass {
StorageClass_unspecified,
StorageClass_auto,
StorageClass_constexpr,
StorageClass_extern,
StorageClass_register,
StorageClass_static,
StorageClass_thread_local,
StorageClass_typedef,
};
typedef enum StorageClass StorageClass;
enum TypeKind {
TypeKind_unknown,
TypeKind_void,
TypeKind_char,
TypeKind_schar,
TypeKind_uchar,
TypeKind_short,
TypeKind_ushort,
TypeKind_int,
TypeKind_uint,
TypeKind_long,
TypeKind_ulong,
TypeKind_llong,
TypeKind_ullong,
TypeKind_struct,
TypeKind_union,
TypeKind_enum,
TypeKind_ptr,
TypeKind_array,
};
typedef enum TypeKind TypeKind;
const char* type_kind_stringify(TypeKind k);
struct AstNode;
typedef struct AstNode AstNode;
struct TypeRef {
struct AstNode* defs;
size_t index;
};
typedef struct TypeRef TypeRef;
struct Type {
TypeKind kind;
StorageClass storage_class;
// Check `base` instead of `kind` to test if the type is an array or a pointer.
struct Type* base;
int array_size;
TypeRef ref;
};
typedef struct Type Type;
Type* type_new(TypeKind kind);
Type* type_new_ptr(Type* base);
Type* type_new_array(Type* elem, int size);
Type* type_new_static_string(int len);
Type* type_array_to_ptr(Type* ty);
BOOL type_is_unsized(Type* ty);
int type_sizeof_struct(Type* ty);
int type_sizeof_union(Type* ty);
int type_alignof_struct(Type* ty);
int type_alignof_union(Type* ty);
int type_offsetof(Type* ty, const char* name);
Type* type_member_typeof(Type* ty, const char* name);
int type_sizeof(Type* ty);
int type_alignof(Type* ty);
int to_aligned(int n, int a);
enum AstNodeKind {
AstNodeKind_unknown,
AstNodeKind_nop,
AstNodeKind_assign_expr,
AstNodeKind_binary_expr,
AstNodeKind_break_stmt,
AstNodeKind_cond_expr,
AstNodeKind_continue_stmt,
AstNodeKind_deref_expr,
AstNodeKind_do_while_stmt,
AstNodeKind_enum_def,
AstNodeKind_enum_member,
AstNodeKind_expr_stmt,
AstNodeKind_for_stmt,
AstNodeKind_func_call,
AstNodeKind_func_decl,
AstNodeKind_func_def,
AstNodeKind_gvar,
AstNodeKind_gvar_decl,
AstNodeKind_if_stmt,
AstNodeKind_int_expr,
AstNodeKind_list,
AstNodeKind_logical_expr,
AstNodeKind_lvar,
AstNodeKind_lvar_decl,
AstNodeKind_param,
AstNodeKind_ref_expr,
AstNodeKind_return_stmt,
AstNodeKind_str_expr,
AstNodeKind_struct_decl,
AstNodeKind_struct_def,
AstNodeKind_struct_member,
AstNodeKind_type,
AstNodeKind_typedef_decl,
AstNodeKind_unary_expr,
AstNodeKind_union_decl,
AstNodeKind_union_def,
AstNodeKind_union_member,
// Intermediate ASTs: they are used only in parsing, not for parse result.
AstNodeKind_declarator,
};
typedef enum AstNodeKind AstNodeKind;
#define node_items __n1
#define node_len __i1
#define node_cap __i2
#define node_expr __n1
#define node_lhs __n1
#define node_rhs __n2
#define node_operand __n1
#define node_cond __n1
#define node_init __n2
#define node_update __n3
#define node_then __n2
#define node_else __n3
#define node_body __n4
#define node_members __n1
#define node_params __n1
#define node_args __n1
#define node_int_value __i1
#define node_idx __i1
#define node_op __i1
#define node_stack_offset __i1
#define node_stack_size __i1
#define node_function_is_static __i2
struct AstNode {
AstNodeKind kind;
const char* name;
Type* ty;
struct AstNode* __n1;
struct AstNode* __n2;
struct AstNode* __n3;
struct AstNode* __n4;
int __i1;
int __i2;
};
struct Program {
AstNode* funcs;
AstNode* vars;
const char** str_literals;
};
typedef struct Program Program;
AstNode* ast_new(AstNodeKind kind);
AstNode* ast_new_list(int capacity);
void ast_append(AstNode* list, AstNode* item);
AstNode* ast_new_int(int v);
AstNode* ast_new_unary_expr(int op, AstNode* operand);
AstNode* ast_new_binary_expr(int op, AstNode* lhs, AstNode* rhs);
AstNode* ast_new_assign_expr(int op, AstNode* lhs, AstNode* rhs);
AstNode* ast_new_assign_add_expr(AstNode* lhs, AstNode* rhs);
AstNode* ast_new_assign_sub_expr(AstNode* lhs, AstNode* rhs);
AstNode* ast_new_ref_expr(AstNode* operand);
AstNode* ast_new_deref_expr(AstNode* operand);
AstNode* ast_new_member_access_expr(AstNode* obj, const char* name);
#endif
|