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
|
/*
Odin Middle End Notation
"me" prefix means middle end
*/
struct meModule; // Build/Translation Unit
struct meValue;
struct meProcedure;
struct meBlock;
struct meInstruction;
struct meConstant;
struct meGlobalVariable;
struct meParameter;
enum meValueKind : u8 {
meValue_Invalid = 0,
meValue_Instruction,
meValue_ConstantValue,
meValue_Block,
meValue_Procedure,
meValue_GlobalVariable,
meValue_Parameter,
};
struct meValue {
meValueKind kind;
union {
meInstruction *instr;
meConstant *constant;
meBlock *block;
meProcedure *proc;
meGlobalVariable *global;
meParameter *param;
};
};
enum meOpKind : u8 {
meOp_Invalid = 0,
meOp_Unreachable,
meOp_Return,
meOp_Jump,
meOp_CondJump,
meOp_Switch,
meOp_Phi,
// Unary Operators
meOp_Neg,
meOp_LogicalNot,
meOp_BitwiseNot,
// Binary Arithmetic Operators
meOp_Add,
meOp_Sub,
meOp_Mul,
meOp_Div,
meOp_Rem,
// Binary Bitwise Operators
meOp_Shl,
meOp_LShr,
meOp_AShr,
meOp_And,
meOp_Or,
meOp_Xor,
// Memory Operators
meOp_Alloca,
meOp_Load,
meOp_Store,
meOp_UnalignedLoad,
meOp_UnalignedStore,
meOp_GetElementPtr,
meOp_PtrOffset,
meOp_PtrSub,
// Cast Operators
meOp_Cast,
meOp_Transmute,
// Binary Comparison Operators
meOp_Eq,
meOp_NotEq,
meOp_Lt,
meOp_LtEq,
meOp_Gt,
meOp_GtEq,
meOp_Min,
meOp_Max,
// Ternary Operators
meOp_Select,
// Other
meOp_Call,
meOp_BuiltinCall,
meOp_ExtractValue,
meOp_Swizzle,
meOp_Alias, // alias of another value
// Atomics
meOp_Fence,
meOp_AtomicXchg,
meOp_AtomicCmpXchg,
};
enum meInstructionFlags : u16 {
meInstructionFlag_Volatile = 1<<0,
meInstructionFlag_AtomicRMW = 1<<1,
meInstructionFlag_ForceInline = 1<<2,
meInstructionFlag_ForceNoInline = 1<<3,
meInstructionFlag_HasSideEffects = 1<<4,
};
enum meAtomicOrderingKind : u8 {
meAtomicOrdering_NotAtomic,
meAtomicOrdering_Unordered,
meAtomicOrdering_Monotonic,
meAtomicOrdering_Acquire,
meAtomicOrdering_Release,
meAtomicOrdering_AcquireRelease,
meAtomicOrdering_SequentiallyConsistent,
meAtomicOrdering_COUNT,
};
enum meLinkageKind : u8 {
meLinkage_Strong,
meLinkage_Weak,
meLinkage_Internal,
meLinkage_LinkOnce,
meLinkage_Export,
};
enum {me_INSTRUCTION_MAX_ARG_COUNT = 4};
struct meInstruction {
meOpKind op;
meAtomicOrderingKind atomic_ordering;
u16 flags;
u16 alignment;
u16 uses;
Type * type;
meProcedure *parent;
TokenPos pos;
meValue ops[me_INSTRUCTION_MAX_ARG_COUNT];
isize op_count;
Slice<meValue> *extra_ops; // non-null if used
};
struct meConstant {
ExactValue value;
Type *type;
};
struct meBlock {
meProcedure * parent;
String name;
Array<meInstruction *> instructions;
Scope * scope;
i32 scope_index;
Array<meBlock *> preds;
Array<meBlock *> succs;
};
struct meBranchBlocks {
Ast *label;
meBlock *break_;
meBlock *continue_;
};
struct meTargetList {
meTargetList *prev;
bool is_block;
meBlock *break_;
meBlock *continue_;
meBlock *fallthrough_;
};
enum meGlobalVariableFlags : u16 {
meGlobalVariableFlag_ThreadLocal_default = 1<<0,
meGlobalVariableFlag_ThreadLocal_localdynamic = 1<<1,
meGlobalVariableFlag_ThreadLocal_initialexec = 1<<2,
meGlobalVariableFlag_ThreadLocal_localexec = 1<<3,
};
struct meGlobalVariable {
meModule * module;
Entity * entity;
Type * type;
String name; // link name
meLinkageKind linkage;
u16 flags;
ExactValue value;
i32 uses;
DeclInfo * decl;
};
struct meParameter {
String name;
Entity * entity;
meProcedure *parent;
i32 uses;
};
enum meAddrKind : u32 {
meAddr_Default = 0,
meAddr_Map,
meAddr_Context,
meAddr_SoaVariable,
meAddr_RelativePointer,
meAddr_RelativeSlice,
meAddr_Swizzle,
meAddr_SwizzleLarge,
};
struct meAddr {
meAddrKind kind;
meValue addr;
union {
struct {
meValue key;
Type *type;
Type *result;
} map;
struct {
Selection sel;
} ctx;
struct {
meValue index;
Ast *index_expr;
} soa;
struct {
meValue index;
Ast *node;
} index_set;
struct {
bool deref;
} relative;
struct {
Type *type;
u8 count; // 2, 3, or 4 components
u8 indices[4];
} swizzle;
struct {
Type *type;
Slice<i32> indices;
} swizzle_large;
};
};
struct meContextData {
meAddr ctx;
i32 scope_index;
i32 uses;
};
enum meDeferKind {
meDefer_Node,
meDefer_Proc,
};
struct meDefer {
meDeferKind kind;
i32 scope_index;
i32 context_stack_count;
meBlock * block;
union {
Ast *stmt;
struct {
meValue deferred;
Array<meValue> result_as_args;
} proc;
};
};
enum meDeferExitKind {
meDeferExit_Default,
meDeferExit_Return,
meDeferExit_Branch,
};
enum meProcedureFlags : u32 {
meProcedureFlag_Foreign = 1<<1,
meProcedureFlag_Export = 1<<2,
meProcedureFlag_EntryPoint = 1<<3,
meProcedureFlag_Startup = 1<<4,
meProcedureFlag_Inline = 1<<5,
meProcedureFlag_NoInline = 1<<6,
meProcedureFlag_Cold = 1<<7,
meProcedureFlag_Hot = 1<<8,
meProcedureFlag_WithoutMemcpy = 1<<9,
};
struct meProcedure {
meModule * module;
Entity * entity;
Type * type;
String name; // link name
meLinkageKind linkage;
u32 flags;
i32 uses;
BuiltinProcId builtin_id;
u16 state_flags;
bool is_done;
meProcedure * parent;
Array<meProcedure *> children;
Ast *type_expr;
Ast *body;
u64 tags;
meAddr return_ptr;
Array<meDefer> defer_stmts;
Array<meBlock *> blocks;
meBlock * decl_block;
meBlock * entry_block;
meBlock * curr_block;
Array<meBranchBlocks> branch_blocks;
Scope *curr_scope;
i32 scope_index;
meTargetList *target_list;
Ast *curr_stmt;
Array<Scope *> scope_stack;
Array<meContextData> context_stack;
};
struct meModule { // Build/Translation Unit
CheckerInfo *info;
AstPackage *pkg; // associated
PtrMap<Entity *, meValue> values;
PtrMap<Entity *, meAddr> soa_values;
StringMap<meValue> members;
StringMap<meProcedure *> procedures;
PtrMap<meProcedure *, Entity *> procedure_values;
Array<meProcedure *> missing_procedures_to_check;
StringMap<meValue> const_strings;
PtrMap<Type *, meProcedure *> equal_procs;
PtrMap<Type *, meProcedure *> hasher_procs;
u32 nested_type_name_guid;
Array<meProcedure *> procedures_to_generate;
Array<String> foreign_library_paths;
meProcedure *curr_procedure;
StringMap<meAddr> objc_classes;
StringMap<meAddr> objc_selectors;
};
bool me_generate(Checker *c);
String me_get_entity_name(meModule *m, Entity *e, String default_name = {});
meProcedure *me_procedure_create(meModule *m, Entity *entity, bool ignore_body=false);
meValue me_value(meInstruction *instr);
meValue me_value(meConstant *constant);
meValue me_value(meBlock *block);
meValue me_value(meProcedure *proc);
meValue me_value(meGlobalVariable *global);
meValue me_value(meParameter *param);
void me_build_stmt(meProcedure *p, Ast *stmt);
meValue me_build_expr(meProcedure *p, Ast *expr);
meValue me_emit_conv(meProcedure *p, meValue value, Type *type);
|