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
|
/*
* Copyright (c) 2015 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#ifndef ZIG_ANALYZE_HPP
#define ZIG_ANALYZE_HPP
#include "codegen.hpp"
#include "hash_map.hpp"
#include "zig_llvm.hpp"
#include "errmsg.hpp"
struct FnTableEntry;
struct BlockContext;
struct TypeTableEntry;
struct TypeTableEntryPointer {
TypeTableEntry *pointer_child;
bool pointer_is_const;
};
struct TypeTableEntryInt {
bool is_signed;
};
struct TypeTableEntryArray {
TypeTableEntry *child_type;
uint64_t len;
};
struct TypeStructField {
Buf *name;
TypeTableEntry *type_entry;
};
struct TypeTableEntryStruct {
bool is_packed;
int field_count;
TypeStructField *fields;
};
enum TypeTableEntryId {
TypeTableEntryIdInvalid,
TypeTableEntryIdVoid,
TypeTableEntryIdBool,
TypeTableEntryIdUnreachable,
TypeTableEntryIdInt,
TypeTableEntryIdFloat,
TypeTableEntryIdPointer,
TypeTableEntryIdArray,
TypeTableEntryIdStruct,
};
struct TypeTableEntry {
TypeTableEntryId id;
LLVMTypeRef type_ref;
LLVMZigDIType *di_type;
uint64_t size_in_bits;
uint64_t align_in_bits;
Buf name;
union {
TypeTableEntryPointer pointer;
TypeTableEntryInt integral;
TypeTableEntryArray array;
TypeTableEntryStruct structure;
} data;
// use these fields to make sure we don't duplicate type table entries for the same type
TypeTableEntry *pointer_const_parent;
TypeTableEntry *pointer_mut_parent;
HashMap<int, TypeTableEntry *, int_hash, int_eq> arrays_by_size;
};
struct ImportTableEntry {
AstNode *root;
Buf *path; // relative to root_source_dir
LLVMZigDIFile *di_file;
Buf *source_code;
ZigList<int> *line_offsets;
// reminder: hash tables must be initialized before use
HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
};
struct LabelTableEntry {
AstNode *label_node;
LLVMBasicBlockRef basic_block;
bool used;
bool entered_from_fallthrough;
};
enum FnAttrId {
FnAttrIdNaked,
FnAttrIdAlwaysInline,
};
struct FnTableEntry {
LLVMValueRef fn_value;
AstNode *proto_node;
AstNode *fn_def_node;
bool is_extern;
bool internal_linkage;
unsigned calling_convention;
ImportTableEntry *import_entry;
ZigList<FnAttrId> fn_attr_list;
// reminder: hash tables must be initialized before use
HashMap<Buf *, LabelTableEntry *, buf_hash, buf_eql_buf> label_table;
};
struct CodeGen {
LLVMModuleRef module;
ZigList<ErrorMsg*> errors;
LLVMBuilderRef builder;
LLVMZigDIBuilder *dbuilder;
LLVMZigDICompileUnit *compile_unit;
ZigList<Buf *> lib_search_paths;
// reminder: hash tables must be initialized before use
HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
HashMap<Buf *, bool, buf_hash, buf_eql_buf> link_table;
HashMap<Buf *, ImportTableEntry *, buf_hash, buf_eql_buf> import_table;
struct {
TypeTableEntry *entry_bool;
TypeTableEntry *entry_u8;
TypeTableEntry *entry_i32;
TypeTableEntry *entry_isize;
TypeTableEntry *entry_usize;
TypeTableEntry *entry_f32;
TypeTableEntry *entry_c_string_literal;
TypeTableEntry *entry_string;
TypeTableEntry *entry_void;
TypeTableEntry *entry_unreachable;
TypeTableEntry *entry_invalid;
} builtin_types;
LLVMTargetDataRef target_data_ref;
unsigned pointer_size_bytes;
bool is_static;
bool strip_debug_symbols;
bool insert_bootstrap_code;
CodeGenBuildType build_type;
LLVMTargetMachineRef target_machine;
bool is_native_target;
Buf *root_source_dir;
Buf *root_out_name;
// The function definitions this module includes. There must be a corresponding
// fn_protos entry.
ZigList<FnTableEntry *> fn_defs;
// The function prototypes this module includes. In the case of external declarations,
// there will not be a corresponding fn_defs entry.
ZigList<FnTableEntry *> fn_protos;
OutType out_type;
FnTableEntry *cur_fn;
LLVMBasicBlockRef cur_basic_block;
BlockContext *cur_block_context;
bool c_stdint_used;
AstNode *root_export_decl;
int version_major;
int version_minor;
int version_patch;
bool verbose;
ErrColor err_color;
ImportTableEntry *root_import;
};
struct LocalVariableTableEntry {
Buf name;
TypeTableEntry *type;
LLVMValueRef value_ref;
bool is_const;
bool is_ptr; // if true, value_ref is a pointer
AstNode *decl_node;
LLVMZigDILocalVariable *di_loc_var;
int arg_index;
};
struct BlockContext {
AstNode *node; // either NodeTypeFnDef or NodeTypeBlock
BlockContext *root; // always points to the BlockContext with the NodeTypeFnDef
BlockContext *parent; // nullptr when this is the root
HashMap<Buf *, LocalVariableTableEntry *, buf_hash, buf_eql_buf> variable_table;
ZigList<AstNode *> cast_expr_alloca_list;
LLVMZigDIScope *di_scope;
};
struct TypeNode {
TypeTableEntry *entry;
};
struct FnProtoNode {
FnTableEntry *fn_table_entry;
};
struct FnDefNode {
TypeTableEntry *implicit_return_type;
BlockContext *block_context;
bool skip;
// Required to be a pre-order traversal of the AST. (parents must come before children)
ZigList<BlockContext *> all_block_contexts;
};
struct ExprNode {
TypeTableEntry *type_entry;
// the context in which this expression is evaluated.
// for blocks, this points to the containing scope, not the block's own scope for its children.
BlockContext *block_context;
};
struct AssignNode {
LocalVariableTableEntry *var_entry;
};
struct BlockNode {
BlockContext *block_context;
};
struct StructDeclNode {
TypeTableEntry *type_entry;
};
struct FieldAccessNode {
int field_index;
TypeStructField *type_struct_field;
};
enum CastOp {
CastOpPtrToInt,
CastOpIntWidenOrShorten,
CastOpArrayToString,
};
struct CastNode {
CastOp op;
// if op is CastOpArrayToString, this will be a pointer to
// the string struct on the stack
LLVMValueRef ptr;
};
struct CodeGenNode {
union {
TypeNode type_node; // for NodeTypeType
FnDefNode fn_def_node; // for NodeTypeFnDef
FnProtoNode fn_proto_node; // for NodeTypeFnProto
LabelTableEntry *label_entry; // for NodeTypeGoto and NodeTypeLabel
AssignNode assign_node; // for NodeTypeBinOpExpr where op is BinOpTypeAssign
BlockNode block_node; // for NodeTypeBlock
StructDeclNode struct_decl_node; // for NodeTypeStructDecl
FieldAccessNode field_access_node; // for NodeTypeFieldAccessExpr
CastNode cast_node; // for NodeTypeCastExpr
} data;
ExprNode expr_node; // for all the expression nodes
};
static inline Buf *hack_get_fn_call_name(CodeGen *g, AstNode *node) {
// Assume that the expression evaluates to a simple name and return the buf
// TODO after type checking works we should be able to remove this hack
assert(node->type == NodeTypeSymbol);
return &node->data.symbol;
}
void semantic_analyze(CodeGen *g);
void add_node_error(CodeGen *g, AstNode *node, Buf *msg);
TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
LocalVariableTableEntry *find_local_variable(BlockContext *context, Buf *name);
#endif
|