-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeNode.h
591 lines (491 loc) · 10.5 KB
/
CodeNode.h
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
#pragma once
#include "SyntaxNode.h"
using Kano_Char = uint8_t;
using Kano_Int = int64_t;
using Kano_Real = double;
using Kano_Bool = bool;
enum Code_Type_Kind
{
CODE_TYPE_NULL,
CODE_TYPE_CHARACTER,
CODE_TYPE_INTEGER,
CODE_TYPE_REAL,
CODE_TYPE_BOOL,
CODE_TYPE_POINTER,
CODE_TYPE_PROCEDURE,
CODE_TYPE_STRUCT,
CODE_TYPE_ARRAY_VIEW,
CODE_TYPE_STATIC_ARRAY,
_CODE_TYPE_COUNT
};
struct Code_Type
{
Code_Type_Kind kind = CODE_TYPE_NULL;
uint32_t runtime_size = 0;
uint32_t alignment = 0;
};
struct Code_Type_Character : public Code_Type
{
Code_Type_Character()
{
kind = CODE_TYPE_CHARACTER;
runtime_size = sizeof(Kano_Char);
alignment = sizeof(Kano_Char);
}
};
struct Code_Type_Integer : public Code_Type
{
Code_Type_Integer()
{
kind = CODE_TYPE_INTEGER;
runtime_size = sizeof(Kano_Int);
alignment = sizeof(Kano_Int);
}
};
struct Code_Type_Real : public Code_Type
{
Code_Type_Real()
{
kind = CODE_TYPE_REAL;
runtime_size = sizeof(Kano_Real);
alignment = sizeof(Kano_Real);
}
};
struct Code_Type_Bool : public Code_Type
{
Code_Type_Bool()
{
kind = CODE_TYPE_BOOL;
runtime_size = sizeof(Kano_Bool);
alignment = sizeof(Kano_Bool);
}
};
struct Code_Type_Pointer : public Code_Type
{
Code_Type_Pointer()
{
kind = CODE_TYPE_POINTER;
runtime_size = sizeof(void *);
alignment = sizeof(void *);
}
Code_Type *base_type = nullptr;
};
typedef void (*CCall)(struct Interpreter *interp);
struct Code_Value_Procedure
{
struct Code_Node_Block *block;
CCall ccall;
};
struct Code_Type_Procedure : public Code_Type
{
Code_Type_Procedure()
{
kind = CODE_TYPE_PROCEDURE;
runtime_size = sizeof(Code_Value_Procedure);
alignment = sizeof(Code_Value_Procedure);
}
String name = "anonymous";
Code_Type **arguments = nullptr;
int64_t argument_count = 0;
bool is_variadic = false;
Code_Type * return_type = nullptr;
};
struct Code_Type_Struct : public Code_Type
{
Code_Type_Struct()
{
kind = CODE_TYPE_STRUCT;
}
struct Member
{
String name;
Code_Type *type;
uint64_t offset;
};
String name;
int64_t member_count;
Member * members;
uint64_t id;
};
struct Code_Type_Array_View : public Code_Type
{
Code_Type_Array_View()
{
kind = CODE_TYPE_ARRAY_VIEW;
runtime_size = sizeof(Array_View<void *>);
alignment = sizeof(int64_t);
}
Code_Type *element_type = nullptr;
};
struct Code_Type_Static_Array : public Code_Type
{
Code_Type_Static_Array()
{
kind = CODE_TYPE_STATIC_ARRAY;
}
Code_Type *element_type = nullptr;
uint32_t element_count = 0;
};
//
//
//
struct Symbol_Address
{
enum Kind
{
STACK = 0,
GLOBAL = 1,
CODE,
CCALL,
};
Kind kind;
union {
uint64_t offset;
struct Code_Node_Block *code;
CCall ccall;
};
};
inline Symbol_Address symbol_address_offset(uint32_t offset, Symbol_Address::Kind kind)
{
Symbol_Address address;
address.kind = kind;
address.offset = offset;
return address;
}
inline Symbol_Address symbol_address_code(struct Code_Node_Block *block)
{
Symbol_Address code;
code.kind = Symbol_Address::CODE;
code.code = block;
return code;
}
struct Symbol
{
String name;
Code_Type * type = nullptr;
Symbol_Address address = {Symbol_Address::CODE, 0};
uint32_t flags = 0;
Syntax_Location location = {};
};
constexpr uint32_t SYMBOL_INDEX_BUCKET_SIZE = 16;
constexpr uint32_t SYMBOL_INDEX_MASK = SYMBOL_INDEX_BUCKET_SIZE - 1;
constexpr uint32_t SYMBOL_INDEX_SHIFT = 4;
constexpr uint32_t SYMBOL_TABLE_BUCKET_COUNT = 64;
constexpr uint32_t HASH_SEED = 0x2564;
struct Symbol_Index
{
uint32_t hash[SYMBOL_INDEX_BUCKET_SIZE] = {};
uint32_t index[SYMBOL_INDEX_BUCKET_SIZE] = {};
Symbol_Index *next = nullptr;
};
struct Symbol_Lookup
{
Symbol_Index buckets[SYMBOL_TABLE_BUCKET_COUNT];
};
struct Symbol_Table {
Table<String, Symbol *> map;
Symbol_Table *parent = nullptr;
};
/*
struct Symbol_Table
{
Symbol_Lookup lookup;
Array<Symbol *> buffer;
Symbol_Table * parent = nullptr;
};
*/
//
//
//
enum Code_Node_Kind
{
CODE_NODE_NULL,
CODE_NODE_LITERAL,
CODE_NODE_ADDRESS,
CODE_NODE_TYPE_CAST,
CODE_NODE_UNARY_OPERATOR,
CODE_NODE_BINARY_OPERATOR,
CODE_NODE_EXPRESSION,
CODE_NODE_ASSIGNMENT,
CODE_NODE_RETURN,
CODE_NODE_BREAK,
CODE_NODE_CONTINUE,
CODE_NODE_STATEMENT,
CODE_NODE_PROCEDURE_CALL,
CODE_NODE_OFFSET,
CODE_NODE_SUBSCRIPT,
CODE_NODE_IF,
CODE_NODE_FOR,
CODE_NODE_WHILE,
CODE_NODE_DO,
CODE_NODE_BLOCK,
_CODE_NODE_COUNT,
};
struct Code_Node
{
Code_Node_Kind kind = CODE_NODE_NULL;
uint32_t flags = 0;
Code_Type * type = nullptr;
};
struct Code_Value_Integer
{
Kano_Int value;
};
struct Code_Value_Real
{
Kano_Real value;
};
struct Code_Value_Bool
{
Kano_Bool value;
};
struct Code_Value_Pointer
{
uint8_t *value;
};
struct Code_Value_String
{
String value;
};
union Code_Value {
Code_Value_String string = {};
Code_Value_Integer integer;
Code_Value_Real real;
Code_Value_Bool boolean;
Code_Value_Pointer pointer;
Code_Value_Procedure procedure;
Code_Value(){};
};
struct Code_Node_Literal : public Code_Node
{
Code_Node_Literal()
{
kind = CODE_NODE_LITERAL;
}
Code_Value data;
};
struct Code_Node_Address : public Code_Node
{
Code_Node_Address()
{
kind = CODE_NODE_ADDRESS;
}
// If subscript is null, then address is valid
struct Code_Node_Subscript *subscript = nullptr;
const Symbol_Address *address = nullptr;
uint64_t offset = 0;
};
struct Code_Node_Type_Cast : public Code_Node
{
Code_Node_Type_Cast()
{
kind = CODE_NODE_TYPE_CAST;
}
struct Code_Node_Expression *child = nullptr;
bool implicit = false;
};
enum Unary_Operator_Kind
{
UNARY_OPERATOR_PLUS,
UNARY_OPERATOR_MINUS,
UNARY_OPERATOR_BITWISE_NOT,
UNARY_OPERATOR_LOGICAL_NOT,
UNARY_OPERATOR_POINTER_TO,
UNARY_OPERATOR_DEREFERENCE,
_UNARY_OPERATOR_COUNT
};
struct Unary_Operator
{
Code_Type *parameter;
Code_Type *output;
};
struct Code_Node_Unary_Operator : public Code_Node
{
Code_Node_Unary_Operator()
{
kind = CODE_NODE_UNARY_OPERATOR;
}
Unary_Operator_Kind op_kind;
Code_Node * child = nullptr;
};
enum Binary_Operator_Kind
{
BINARY_OPERATOR_ADDITION,
BINARY_OPERATOR_SUBTRACTION,
BINARY_OPERATOR_MULTIPLICATION,
BINARY_OPERATOR_DIVISION,
BINARY_OPERATOR_REMAINDER,
BINARY_OPERATOR_BITWISE_SHIFT_RIGHT,
BINARY_OPERATOR_BITWISE_SHIFT_LEFT,
BINARY_OPERATOR_BITWISE_AND,
BINARY_OPERATOR_BITWISE_XOR,
BINARY_OPERATOR_BITWISE_OR,
BINARY_OPERATOR_RELATIONAL_GREATER,
BINARY_OPERATOR_RELATIONAL_LESS,
BINARY_OPERATOR_RELATIONAL_GREATER_EQUAL,
BINARY_OPERATOR_RELATIONAL_LESS_EQUAL,
BINARY_OPERATOR_COMPARE_EQUAL,
BINARY_OPERATOR_COMPARE_NOT_EQUAL,
BINARY_OPERATOR_COMPOUND_ADDITION,
BINARY_OPERATOR_COMPOUND_SUBTRACTION,
BINARY_OPERATOR_COMPOUND_MULTIPLICATION,
BINARY_OPERATOR_COMPOUND_DIVISION,
BINARY_OPERATOR_COMPOUND_REMAINDER,
BINARY_OPERATOR_COMPOUND_BITWISE_SHIFT_RIGHT,
BINARY_OPERATOR_COMPOUND_BITWISE_SHIFT_LEFT,
BINARY_OPERATOR_COMPOUND_BITWISE_AND,
BINARY_OPERATOR_COMPOUND_BITWISE_XOR,
BINARY_OPERATOR_COMPOUND_BITWISE_OR,
BINARY_OPERATOR_LOGICAL_AND,
BINARY_OPERATOR_LOGICAL_OR,
_BINARY_OPERATOR_COUNT
};
struct Binary_Operator
{
Code_Type *parameters[2];
Code_Type *output;
bool compound = false;
};
struct Code_Node_Binary_Operator : public Code_Node
{
Code_Node_Binary_Operator()
{
kind = CODE_NODE_BINARY_OPERATOR;
}
Binary_Operator_Kind op_kind;
Code_Node * left = nullptr;
Code_Node * right = nullptr;
};
struct Code_Node_Expression : public Code_Node
{
Code_Node_Expression()
{
kind = CODE_NODE_EXPRESSION;
}
Code_Node *child = nullptr;
};
struct Code_Node_Assignment : public Code_Node
{
Code_Node_Assignment()
{
kind = CODE_NODE_ASSIGNMENT;
}
Code_Node_Expression *destination = nullptr;
Code_Node_Expression *value = nullptr;
};
struct Code_Node_Return : public Code_Node
{
Code_Node_Return()
{
kind = CODE_NODE_RETURN;
}
Code_Node *expression = nullptr;
};
struct Code_Node_Break : public Code_Node
{
Code_Node_Break()
{
kind = CODE_NODE_BREAK;
}
};
struct Code_Node_Continue : public Code_Node
{
Code_Node_Continue()
{
kind = CODE_NODE_CONTINUE;
}
};
struct Code_Node_Statement : public Code_Node
{
Code_Node_Statement()
{
kind = CODE_NODE_STATEMENT;
}
uint64_t source_row = -1;
Code_Node * node = nullptr;
Code_Node_Statement *next = nullptr;
Symbol_Table *symbol_table = nullptr;
};
struct Code_Node_Procedure_Call : public Code_Node
{
Code_Node_Procedure_Call()
{
kind = CODE_NODE_PROCEDURE_CALL;
}
Code_Node_Expression * procedure = nullptr;
int64_t parameter_count = 0;
Code_Node_Expression **parameters = nullptr;
int64_t variadic_count = 0;
Code_Node_Expression **variadics = nullptr;
Code_Type_Procedure *procedure_type = nullptr;
uint64_t stack_top = 0;
};
struct Code_Node_Subscript : public Code_Node
{
Code_Node_Subscript()
{
kind = CODE_NODE_SUBSCRIPT;
}
Code_Node_Expression *expression = nullptr;
Code_Node_Expression *subscript = nullptr;
};
struct Code_Node_Offset : public Code_Node
{
Code_Node_Offset()
{
kind = CODE_NODE_OFFSET;
}
Code_Node_Expression *expression = nullptr;
uint64_t offset = 0;
};
struct Code_Node_If : public Code_Node
{
Code_Node_If()
{
kind = CODE_NODE_IF;
}
Code_Node_Expression *condition = nullptr;
Code_Node_Statement * true_statement = nullptr;
Code_Node_Statement * false_statement = nullptr;
};
struct Code_Node_For : public Code_Node
{
Code_Node_For()
{
kind = CODE_NODE_FOR;
}
Code_Node_Statement *initialization = nullptr;
Code_Node_Statement *condition = nullptr;
Code_Node_Statement *increment = nullptr;
Code_Node_Statement * body = nullptr;
Symbol_Table symbols;
};
struct Code_Node_While : public Code_Node
{
Code_Node_While()
{
kind = CODE_NODE_WHILE;
}
Code_Node_Statement *condition = nullptr;
Code_Node_Statement *body = nullptr;
};
struct Code_Node_Do : public Code_Node
{
Code_Node_Do()
{
kind = CODE_NODE_DO;
}
Code_Node_Statement *body = nullptr;
Code_Node_Statement *condition = nullptr;
};
struct Code_Node_Block : public Code_Node
{
Code_Node_Block()
{
kind = CODE_NODE_BLOCK;
}
Code_Node_Statement *statement_head = nullptr;
int64_t statement_count = 0;
Symbol_Table symbols;
int64_t procedure_source_row = -1;
};