Skip to content

Latest commit

 

History

History
1861 lines (1654 loc) · 58.8 KB

mojoshader_compiler.c

File metadata and controls

1861 lines (1654 loc) · 58.8 KB
 
Feb 19, 2010
Feb 19, 2010
1
2
3
4
5
6
7
8
9
/**
* MojoShader; generate shader programs from bytecode of compiled
* Direct3D shaders.
*
* Please see the file LICENSE.txt in the source's root directory.
*
* This file written by Ryan C. Gordon.
*/
Feb 28, 2009
Feb 28, 2009
10
11
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
Aug 23, 2009
Aug 23, 2009
13
14
15
16
#if DEBUG_COMPILER_PARSER
#define LEMON_SUPPORT_TRACING 1
#endif
Feb 9, 2010
Feb 9, 2010
17
typedef union TokenData
Aug 23, 2009
Aug 23, 2009
18
{
Feb 9, 2010
Feb 9, 2010
19
20
21
int64 i64;
double dbl;
const char *string;
Aug 23, 2009
Aug 23, 2009
22
23
} TokenData;
Feb 9, 2010
Feb 9, 2010
24
25
26
27
28
29
typedef struct StringBucket
{
char *string;
struct StringBucket *next;
} StringBucket;
Feb 28, 2009
Feb 28, 2009
30
Feb 19, 2010
Feb 19, 2010
31
// Structures that make up the parse tree...
Feb 9, 2010
Feb 9, 2010
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
typedef enum Operator
{
OP_START_RANGE_UNARY,
OP_POSTINCREMENT,
OP_POSTDECREMENT,
OP_PREINCREMENT,
OP_PREDECREMENT,
OP_NEGATE,
OP_COMPLEMENT,
OP_NOT,
OP_END_RANGE_UNARY,
OP_START_RANGE_BINARY,
OP_DEREF_ARRAY,
OP_CALLFUNC,
OP_DEREF_STRUCT,
OP_COMMA,
OP_MULTIPLY,
OP_DIVIDE,
OP_MODULO,
OP_ADD,
OP_SUBTRACT,
OP_LSHIFT,
OP_RSHIFT,
OP_LESSTHAN,
OP_GREATERTHAN,
OP_LESSTHANOREQUAL,
OP_GREATERTHANOREQUAL,
OP_EQUAL,
OP_NOTEQUAL,
OP_BINARYAND,
OP_BINARYXOR,
OP_BINARYOR,
OP_LOGICALAND,
OP_LOGICALOR,
OP_ASSIGN,
OP_MULASSIGN,
OP_DIVASSIGN,
OP_MODASSIGN,
OP_ADDASSIGN,
OP_SUBASSIGN,
OP_LSHIFTASSIGN,
OP_RSHIFTASSIGN,
OP_ANDASSIGN,
OP_XORASSIGN,
OP_ORASSIGN,
OP_END_RANGE_BINARY,
OP_START_RANGE_TERNARY,
OP_CONDITIONAL,
OP_END_RANGE_TERNARY,
OP_START_RANGE_DATA,
OP_IDENTIFIER,
OP_INT_LITERAL,
OP_FLOAT_LITERAL,
OP_STRING_LITERAL,
OP_END_RANGE_DATA,
Feb 20, 2010
Feb 20, 2010
91
92
93
OP_CONSTRUCTOR,
OP_CAST
Feb 9, 2010
Feb 9, 2010
94
95
} Operator;
Feb 19, 2010
Feb 19, 2010
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
typedef enum VariableAttributes
{
VARATTR_EXTERN = (1 << 0),
VARATTR_NOINTERPOLATION = (1 << 1),
VARATTR_SHARED = (1 << 2),
VARATTR_STATIC = (1 << 3),
VARATTR_UNIFORM = (1 << 4),
VARATTR_VOLATILE = (1 << 5),
VARATTR_CONST = (1 << 6),
VARATTR_ROWMAJOR = (1 << 7),
VARATTR_COLUMNMAJOR = (1 << 8)
} VariableAttributes;
typedef enum IfAttributes
{
IFATTR_NONE,
IFATTR_BRANCH,
IFATTR_FLATTEN,
IFATTR_IFALL,
IFATTR_IFANY,
IFATTR_PREDICATE,
IFATTR_PREDICATEBLOCK,
} IfAttributes;
typedef enum SwitchAttributes
{
SWITCHATTR_NONE,
SWITCHATTR_FLATTEN,
SWITCHATTR_BRANCH,
SWITCHATTR_FORCECASE,
SWITCHATTR_CALL
} SwitchAttributes;
Feb 9, 2010
Feb 9, 2010
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
static inline int operator_is_unary(const Operator op)
{
return ((op > OP_START_RANGE_UNARY) && (op < OP_END_RANGE_UNARY));
} // operator_is_unary
static inline int operator_is_binary(const Operator op)
{
return ((op > OP_START_RANGE_BINARY) && (op < OP_END_RANGE_BINARY));
} // operator_is_binary
static inline int operator_is_ternary(const Operator op)
{
return ((op > OP_START_RANGE_TERNARY) && (op < OP_END_RANGE_TERNARY));
} // operator_is_ternary
typedef struct Expression
{
Operator op; // operator
} Expression;
typedef struct ExpressionUnary
{
Operator op; // operator
Expression *operand;
} ExpressionUnary;
typedef struct ExpressionBinary
{
Operator op; // operator
Expression *left;
Expression *right;
} ExpressionBinary;
typedef struct ExpressionTernary
{
Operator op; // operator
Expression *left;
Expression *center;
Expression *right;
} ExpressionTernary;
typedef struct ExpressionIdentifier
{
Operator op; // Always OP_IDENTIFIER
const char *identifier;
} ExpressionIdentifier;
typedef struct ExpressionIntLiteral
{
Operator op; // Always OP_INT_LITERAL
int64 value;
} ExpressionIntLiteral;
typedef struct ExpressionFloatLiteral
{
Operator op; // Always OP_FLOAT_LITERAL
double value;
} ExpressionFloatLiteral;
typedef struct ExpressionStringLiteral
{
Operator op; // Always OP_STRING_LITERAL
const char *string;
} ExpressionStringLiteral;
Feb 20, 2010
Feb 20, 2010
195
196
197
198
199
200
201
202
203
204
205
206
207
208
typedef struct ExpressionConstructor
{
Operator op; // Always OP_CONSTRUCTOR
const char *datatype;
Expression *args;
} ExpressionConstructor;
typedef struct ExpressionCast
{
Operator op; // Always OP_CAST
const char *datatype;
Expression *operand;
} ExpressionCast;
Feb 19, 2010
Feb 19, 2010
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
typedef enum CompilationUnitType
{
COMPUNITTYPE_FUNCTION, // function declaration or definition
COMPUNITTYPE_TYPEDEF, // typedef or struct
COMPUNITTYPE_STRUCT, // global struct
COMPUNITTYPE_VARIABLE // global variable
} CompilationUnitType;
typedef struct CompilationUnit
{
CompilationUnitType type;
struct CompilationUnit *next;
} CompilationUnit;
typedef enum FunctionStorageClass
{
FNSTORECLS_NONE,
FNSTORECLS_INLINE
} FunctionStorageClass;
typedef enum InputModifier
{
INPUTMOD_NONE,
INPUTMOD_IN,
INPUTMOD_OUT,
INPUTMOD_INOUT,
INPUTMOD_UNIFORM
} InputModifier;
typedef enum InterpolationModifier
{
INTERPMOD_NONE,
INTERPMOD_LINEAR,
INTERPMOD_CENTROID,
INTERPMOD_NOINTERPOLATION,
INTERPMOD_NOPERSPECTIVE,
INTERPMOD_SAMPLE
} InterpolationModifier;
typedef struct FunctionArguments
{
InputModifier input_modifier;
const char *datatype;
const char *identifier;
const char *semantic;
InterpolationModifier interpolation_modifier;
Expression *initializer;
struct FunctionArguments *next;
} FunctionArguments;
typedef struct FunctionSignature
{
const char *datatype;
const char *identifier;
FunctionArguments *args;
FunctionStorageClass storage_class;
const char *semantic;
} FunctionSignature;
typedef struct ScalarOrArray
{
const char *identifier;
int isarray;
Expression *dimension;
} ScalarOrArray;
typedef struct Annotations
{
const char *datatype;
Expression *initializer;
struct Annotations *next;
} Annotations;
typedef struct PackOffset
{
const char *ident1; // !!! FIXME: rename this.
const char *ident2;
} PackOffset;
typedef struct VariableLowLevel
{
PackOffset *packoffset;
const char *register_name;
} VariableLowLevel;
typedef struct StructMembers
{
const char *datatype;
const char *semantic;
ScalarOrArray *details;
InterpolationModifier interpolation_mod;
struct StructMembers *next;
} StructMembers;
typedef struct StructDeclaration
{
const char *name;
StructMembers *members;
} StructDeclaration;
typedef struct VariableDeclaration
{
int attributes;
const char *datatype;
StructDeclaration *anonymous_datatype;
ScalarOrArray *details;
const char *semantic;
Annotations *annotations;
Expression *initializer;
VariableLowLevel *lowlevel;
struct VariableDeclaration *next;
} VariableDeclaration;
typedef enum StatementType
{
STATEMENTTYPE_EMPTY,
STATEMENTTYPE_EXPRESSION,
STATEMENTTYPE_IF,
STATEMENTTYPE_SWITCH,
STATEMENTTYPE_FOR,
STATEMENTTYPE_DO,
STATEMENTTYPE_WHILE,
STATEMENTTYPE_RETURN,
STATEMENTTYPE_BREAK,
STATEMENTTYPE_CONTINUE,
STATEMENTTYPE_DISCARD,
STATEMENTTYPE_TYPEDEF,
STATEMENTTYPE_STRUCT,
STATEMENTTYPE_VARDECL,
} StatementType;
typedef struct Statement
{
StatementType type;
struct Statement *next;
} Statement;
typedef Statement EmptyStatement;
typedef Statement BreakStatement;
typedef Statement ContinueStatement;
typedef Statement DiscardStatement;
typedef struct ReturnStatement
{
StatementType type;
struct Statement *next;
Expression *expr;
} ReturnStatement;
typedef struct ExpressionStatement
{
StatementType type;
struct Statement *next;
Expression *expr;
} ExpressionStatement;
typedef struct IfStatement
{
StatementType type;
struct Statement *next;
int attributes;
Expression *expr;
Statement *statement;
Statement *else_statement;
} IfStatement;
typedef struct SwitchCases
{
Expression *expr;
Statement *statement;
struct SwitchCases *next;
} SwitchCases;
typedef struct SwitchStatement
{
StatementType type;
struct Statement *next;
int attributes;
Expression *expr;
SwitchCases *cases;
} SwitchStatement;
typedef struct WhileStatement
{
StatementType type;
struct Statement *next;
int64 unroll; // # times to unroll, 0 to loop, -1 for compiler's choice.
Expression *expr;
Statement *statement;
} WhileStatement;
typedef WhileStatement DoStatement;
typedef struct ForStatement
{
StatementType type;
struct Statement *next;
int64 unroll; // # times to unroll, 0 to loop, -1 for compiler's choice.
VariableDeclaration *var_decl;
Expression *initializer;
Expression *looptest;
Expression *counter;
Statement *statement;
} ForStatement;
typedef struct Typedef
{
int isconst;
const char *datatype;
ScalarOrArray *details;
} Typedef;
typedef struct TypedefStatement
{
StatementType type;
struct Statement *next;
Typedef *type_info;
} TypedefStatement;
typedef struct VarDeclStatement
{
StatementType type;
struct Statement *next;
VariableDeclaration *decl;
} VarDeclStatement;
typedef struct StructStatement
{
StatementType type;
struct Statement *next;
StructDeclaration *struct_info;
} StructStatement;
typedef struct CompilationUnitFunction
{
CompilationUnitType type;
struct CompilationUnit *next;
FunctionSignature *declaration;
Statement *definition;
} CompilationUnitFunction;
typedef struct CompilationUnitTypedef
{
CompilationUnitType type;
struct CompilationUnit *next;
Typedef *type_info;
} CompilationUnitTypedef;
typedef struct CompilationUnitStruct
{
CompilationUnitType type;
struct CompilationUnit *next;
StructDeclaration *struct_info;
} CompilationUnitStruct;
typedef struct CompilationUnitVariable
{
CompilationUnitType type;
struct CompilationUnit *next;
VariableDeclaration *declaration;
} CompilationUnitVariable;
// Compile state, passed around all over the place.
typedef struct Context
{
int isfail;
int out_of_memory;
MOJOSHADER_malloc malloc;
MOJOSHADER_free free;
void *malloc_data;
int error_count;
ErrorList *errors;
Preprocessor *preprocessor;
StringBucket *string_hashtable[256];
const char *usertypes[512]; // !!! FIXME: dynamic allocation
int usertype_count;
CompilationUnit *ast; // Abstract Syntax Tree
} Context;
// Convenience functions for allocators...
static inline void out_of_memory(Context *ctx)
{
Feb 21, 2010
Feb 21, 2010
495
if (!ctx->out_of_memory) printf("out of memory\n"); // !!! FIXME: placeholder.
Feb 19, 2010
Feb 19, 2010
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
ctx->isfail = ctx->out_of_memory = 1;
} // out_of_memory
static inline void *Malloc(Context *ctx, const size_t len)
{
void *retval = ctx->malloc((int) len, ctx->malloc_data);
if (retval == NULL)
out_of_memory(ctx);
return retval;
} // Malloc
static inline char *StrDup(Context *ctx, const char *str)
{
char *retval = (char *) Malloc(ctx, strlen(str) + 1);
if (retval != NULL)
strcpy(retval, str);
return retval;
} // StrDup
static inline void Free(Context *ctx, void *ptr)
{
if (ptr != NULL) // check for NULL in case of dumb free() impl.
ctx->free(ptr, ctx->malloc_data);
} // Free
Feb 21, 2010
Feb 21, 2010
521
522
523
524
525
526
static void fail(Context *ctx, const char *str)
{
// !!! FIXME: placeholder.
(void) ctx;
printf("FAIL: %s\n", str);
} // fail
Feb 19, 2010
Feb 19, 2010
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
// These functions are mostly for construction and cleanup of nodes in the
// parse tree. Mostly this is simple allocation and initialization, so we
// can do as little in the lemon code as possible, and then sort it all out
// afterwards.
#define NEW_AST_NODE(cls) \
cls *retval = Malloc(ctx, sizeof (cls)); \
if (retval == NULL) { return NULL; } \
#define DELETE_AST_NODE(cls) do {\
if (!cls) return; \
} while (0)
static void delete_compilation_unit(Context *ctx, CompilationUnit *unit);
static void delete_statement(Context *ctx, Statement *stmt);
Feb 20, 2010
Feb 20, 2010
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
static Expression *new_constructor_expr(Context *ctx, const char *datatype,
Expression *args)
{
NEW_AST_NODE(ExpressionConstructor);
retval->op = OP_CONSTRUCTOR;
retval->datatype = datatype;
retval->args = args;
return (Expression *) retval;
} // new_constructor_expr
static Expression *new_cast_expr(Context *ctx, const char *datatype,
Expression *operand)
{
NEW_AST_NODE(ExpressionCast);
retval->op = OP_CAST;
retval->datatype = datatype;
retval->operand = operand;
return (Expression *) retval;
} // new_cast_expr
Feb 9, 2010
Feb 9, 2010
564
565
566
static Expression *new_unary_expr(Context *ctx, const Operator op,
Expression *operand)
{
Feb 19, 2010
Feb 19, 2010
567
NEW_AST_NODE(ExpressionUnary);
Feb 9, 2010
Feb 9, 2010
568
569
570
571
572
573
574
575
576
assert(operator_is_unary(op));
retval->op = op;
retval->operand = operand;
return (Expression *) retval;
} // new_unary_expr
static Expression *new_binary_expr(Context *ctx, const Operator op,
Expression *left, Expression *right)
{
Feb 19, 2010
Feb 19, 2010
577
NEW_AST_NODE(ExpressionBinary);
Feb 9, 2010
Feb 9, 2010
578
579
580
581
582
583
584
585
586
587
588
assert(operator_is_binary(op));
retval->op = op;
retval->left = left;
retval->right = right;
return (Expression *) retval;
} // new_binary_expr
static Expression *new_ternary_expr(Context *ctx, const Operator op,
Expression *left, Expression *center,
Expression *right)
{
Feb 19, 2010
Feb 19, 2010
589
NEW_AST_NODE(ExpressionTernary);
Feb 9, 2010
Feb 9, 2010
590
591
592
593
594
595
596
597
598
599
assert(operator_is_ternary(op));
retval->op = op;
retval->left = left;
retval->center = center;
retval->right = right;
return (Expression *) retval;
} // new_ternary_expr
static Expression *new_identifier_expr(Context *ctx, const char *string)
{
Feb 19, 2010
Feb 19, 2010
600
NEW_AST_NODE(ExpressionIdentifier);
Feb 9, 2010
Feb 9, 2010
601
602
603
604
605
606
607
retval->op = OP_IDENTIFIER;
retval->identifier = string; // cached; don't copy string.
return (Expression *) retval;
} // new_identifier_expr
static Expression *new_literal_int_expr(Context *ctx, const int64 value)
{
Feb 19, 2010
Feb 19, 2010
608
NEW_AST_NODE(ExpressionIntLiteral);
Feb 9, 2010
Feb 9, 2010
609
610
611
612
613
614
615
retval->op = OP_INT_LITERAL;
retval->value = value;
return (Expression *) retval;
} // new_literal_int_expr
static Expression *new_literal_float_expr(Context *ctx, const double dbl)
{
Feb 19, 2010
Feb 19, 2010
616
NEW_AST_NODE(ExpressionFloatLiteral);
Feb 9, 2010
Feb 9, 2010
617
618
619
620
621
622
623
retval->op = OP_FLOAT_LITERAL;
retval->value = dbl;
return (Expression *) retval;
} // new_literal_float_expr
static Expression *new_literal_string_expr(Context *ctx, const char *string)
{
Feb 19, 2010
Feb 19, 2010
624
NEW_AST_NODE(ExpressionStringLiteral);
Feb 9, 2010
Feb 9, 2010
625
626
627
628
629
retval->op = OP_STRING_LITERAL;
retval->string = string; // cached; don't copy string.
return (Expression *) retval;
} // new_string_literal_expr
Feb 19, 2010
Feb 19, 2010
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
static void delete_expr(Context *ctx, Expression *expr)
{
DELETE_AST_NODE(expr);
if (operator_is_unary(expr->op))
{
const ExpressionUnary *unary = (const ExpressionUnary *) expr;
delete_expr(ctx, unary->operand);
} // if
else if (operator_is_binary(expr->op))
{
const ExpressionBinary *binary = (const ExpressionBinary *) expr;
delete_expr(ctx, binary->left);
delete_expr(ctx, binary->right);
} // else if
else if (operator_is_ternary(expr->op))
{
const ExpressionTernary *ternary = (const ExpressionTernary *) expr;
delete_expr(ctx, ternary->left);
delete_expr(ctx, ternary->center);
delete_expr(ctx, ternary->right);
} // else if
Feb 20, 2010
Feb 20, 2010
651
652
653
654
655
656
657
658
else if (expr->op == OP_CAST)
{
delete_expr(ctx, ((ExpressionCast *) expr)->operand);
} // else if
else if (expr->op == OP_CONSTRUCTOR)
{
delete_expr(ctx, ((ExpressionConstructor *) expr)->args);
} // else if
Feb 19, 2010
Feb 19, 2010
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
Free(ctx, expr);
} // delete_expr
static FunctionArguments *new_function_arg(Context *ctx,
const InputModifier inputmod,
const char *datatype,
const char *identifier,
const char *semantic,
const InterpolationModifier interpmod,
Expression *initializer)
{
NEW_AST_NODE(FunctionArguments);
retval->input_modifier = inputmod;
retval->datatype = datatype;
retval->identifier = identifier;
retval->semantic = semantic;
retval->interpolation_modifier = interpmod;
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_function_arg
static void delete_function_args(Context *ctx, FunctionArguments *args)
{
DELETE_AST_NODE(args);
delete_function_args(ctx, args->next);
delete_expr(ctx, args->initializer);
Free(ctx, args);
} // delete_function_args
static FunctionSignature *new_function_signature(Context *ctx,
const char *datatype,
const char *identifier,
FunctionArguments *args)
{
NEW_AST_NODE(FunctionSignature);
retval->datatype = datatype;
retval->identifier = identifier;
retval->args = args;
retval->storage_class = FNSTORECLS_NONE;
retval->semantic = NULL;
return retval;
} // new_function_signature
static void delete_function_signature(Context *ctx, FunctionSignature *sig)
{
DELETE_AST_NODE(sig);
delete_function_args(ctx, sig->args);
Free(ctx, sig);
} // delete_function_signature
static CompilationUnit *new_function(Context *ctx,
FunctionSignature *declaration,
Statement *definition)
{
NEW_AST_NODE(CompilationUnitFunction);
retval->type = COMPUNITTYPE_FUNCTION;
retval->next = NULL;
retval->declaration = declaration;
retval->definition = definition;
return (CompilationUnit *) retval;
} // new_function
static void delete_function(Context *ctx, CompilationUnitFunction *unitfn)
{
DELETE_AST_NODE(unitfn);
delete_compilation_unit(ctx, unitfn->next);
delete_function_signature(ctx, unitfn->declaration);
delete_statement(ctx, unitfn->definition);
Free(ctx, unitfn);
} // delete_function
static ScalarOrArray *new_scalar_or_array(Context *ctx, const char *ident,
const int isvec, Expression *dim)
{
NEW_AST_NODE(ScalarOrArray);
retval->identifier = ident;
retval->isarray = isvec;
retval->dimension = dim;
return retval;
} // new_scalar_or_array
static void delete_scalar_or_array(Context *ctx, ScalarOrArray *soa)
{
DELETE_AST_NODE(soa);
delete_expr(ctx, soa->dimension);
Free(ctx, soa);
} // delete_scalar_or_array
static Typedef *new_typedef(Context *ctx, int isconst, const char *datatype,
ScalarOrArray *soa)
{
NEW_AST_NODE(Typedef);
retval->isconst = isconst;
retval->datatype = datatype;
retval->details = soa;
return retval;
} // new_typedef
static void delete_typedef(Context *ctx, Typedef *td)
{
DELETE_AST_NODE(td);
delete_scalar_or_array(ctx, td->details);
Free(ctx, td);
} // delete_typedef
static PackOffset *new_pack_offset(Context *ctx, const char *a, const char *b)
{
NEW_AST_NODE(PackOffset);
retval->ident1 = a;
retval->ident2 = b;
return retval;
} // new_pack_offset
static void delete_pack_offset(Context *ctx, PackOffset *o)
{
DELETE_AST_NODE(o);
Free(ctx, o);
} // delete_pack_offset
static VariableLowLevel *new_variable_lowlevel(Context *ctx, PackOffset *po,
const char *reg)
{
NEW_AST_NODE(VariableLowLevel);
retval->packoffset = po;
retval->register_name = reg;
return retval;
} // new_variable_lowlevel
static void delete_variable_lowlevel(Context *ctx, VariableLowLevel *vll)
{
DELETE_AST_NODE(vll);
delete_pack_offset(ctx, vll->packoffset);
Free(ctx, vll);
} // delete_variable_lowlevel
static Annotations *new_annotation(Context *ctx, const char *datatype,
Expression *initializer)
{
NEW_AST_NODE(Annotations);
retval->datatype = datatype;
retval->initializer = initializer;
retval->next = NULL;
return retval;
} // new_annotation
static void delete_annotation(Context *ctx, Annotations *annotations)
{
DELETE_AST_NODE(annotations);
delete_annotation(ctx, annotations->next);
delete_expr(ctx, annotations->initializer);
Free(ctx, annotations);
} // delete_annotation
static VariableDeclaration *new_variable_declaration(Context *ctx,
ScalarOrArray *soa, const char *semantic,
Annotations *annotations, Expression *init,
VariableLowLevel *vll)
{
NEW_AST_NODE(VariableDeclaration)
retval->attributes = 0;
retval->datatype = NULL;
retval->anonymous_datatype = NULL;
retval->details = soa;
retval->semantic = semantic;
retval->annotations = annotations;
retval->initializer = init;
retval->lowlevel = vll;
retval->next = NULL;
return retval;
} // new_variable_declaration
static void delete_variable_declaration(Context *ctx, VariableDeclaration *dcl)
{
DELETE_AST_NODE(dcl);
delete_variable_declaration(ctx, dcl->next);
delete_scalar_or_array(ctx, dcl->details);
delete_annotation(ctx, dcl->annotations);
delete_expr(ctx, dcl->initializer);
delete_variable_lowlevel(ctx, dcl->lowlevel);
Free(ctx, dcl);
} // delete_variable_declaration
static CompilationUnit *new_global_variable(Context *ctx,
VariableDeclaration *decl)
{
NEW_AST_NODE(CompilationUnitVariable);
retval->type = COMPUNITTYPE_FUNCTION;
retval->next = NULL;
retval->declaration = decl;
return (CompilationUnit *) retval;
} // new_global_variable
static void delete_global_variable(Context *ctx, CompilationUnitVariable *var)
{
DELETE_AST_NODE(var);
delete_compilation_unit(ctx, var->next);
delete_variable_declaration(ctx, var->declaration);
Free(ctx, var);
} // delete_global_variable
static CompilationUnit *new_global_typedef(Context *ctx, Typedef *td)
{
NEW_AST_NODE(CompilationUnitTypedef)
retval->type = COMPUNITTYPE_TYPEDEF;
retval->next = NULL;
retval->type_info = td;
return (CompilationUnit *) retval;
} // new_global_typedef
static void delete_global_typedef(Context *ctx, CompilationUnitTypedef *unit)
{
DELETE_AST_NODE(unit);
delete_compilation_unit(ctx, unit->next);
delete_typedef(ctx, unit->type_info);
Free(ctx, unit);
} // delete_global_typedef
static StructMembers *new_struct_member(Context *ctx, ScalarOrArray *soa,
const char *semantic)
{
NEW_AST_NODE(StructMembers);
retval->datatype = NULL;
retval->semantic = semantic;
retval->details = soa;
retval->interpolation_mod = INTERPMOD_NONE;
retval->next = NULL;
return retval;
} // new_struct_member
static void delete_struct_member(Context *ctx, StructMembers *member)
{
DELETE_AST_NODE(member);
delete_struct_member(ctx, member->next);
delete_scalar_or_array(ctx, member->details);
Free(ctx, member);
} // delete_struct_member
static StructDeclaration *new_struct_declaration(Context *ctx,
const char *name, StructMembers *members)
{
NEW_AST_NODE(StructDeclaration);
retval->name = name;
retval->members = members;
return retval;
} // new_struct_declaration
static void delete_struct_declaration(Context *ctx, StructDeclaration *decl)
{
DELETE_AST_NODE(decl);
delete_struct_member(ctx, decl->members);
Free(ctx, decl);
} // delete_struct_declaration
static CompilationUnit *new_global_struct(Context *ctx, StructDeclaration *sd)
{
NEW_AST_NODE(CompilationUnitStruct)
retval->type = COMPUNITTYPE_STRUCT;
retval->next = NULL;
retval->struct_info = sd;
return (CompilationUnit *) retval;
} // new_global_struct
static void delete_global_struct(Context *ctx, CompilationUnitStruct *unit)
{
DELETE_AST_NODE(unit);
delete_compilation_unit(ctx, unit->next);
delete_struct_declaration(ctx, unit->struct_info);
Free(ctx, unit);
} // delete_global_struct
static void delete_compilation_unit(Context *ctx, CompilationUnit *unit)
{
if (!unit) return;
// it's important to not recurse too deeply here, since you may have
// thousands of items in this linked list (each line of a massive
// function, for example). To avoid this, we iterate the list here,
// deleting all children and making them think they have no reason
// to recurse in their own delete methods.
// Please note that everyone should _try_ to delete their "next" member,
// just in case, but hopefully this cleaned it out.
CompilationUnit *i = unit->next;
unit->next = NULL;
while (i)
{
CompilationUnit *next = i->next;
i->next = NULL;
delete_compilation_unit(ctx, i);
i = next;
} // while
switch (unit->type)
{
#define DELETE_UNIT(typ, cls, fn) \
case COMPUNITTYPE_##typ: delete_##fn(ctx, (cls *) unit); break;
DELETE_UNIT(FUNCTION, CompilationUnitFunction, function);
DELETE_UNIT(TYPEDEF, CompilationUnitTypedef, global_typedef);
DELETE_UNIT(VARIABLE, CompilationUnitVariable, global_variable);
DELETE_UNIT(STRUCT, CompilationUnitStruct, global_struct);
#undef DELETE_UNIT
default: assert(0 && "missing cleanup code"); break;
} // switch
// don't free (unit) here, the class-specific functions do it.
} // delete_compilation_unit
static Statement *new_typedef_statement(Context *ctx, Typedef *td)
{
NEW_AST_NODE(TypedefStatement)
retval->type = STATEMENTTYPE_TYPEDEF;
retval->next = NULL;
retval->type_info = td;
return (Statement *) retval;
} // new_typedef_statement
static void delete_typedef_statement(Context *ctx, TypedefStatement *stmt)
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_typedef(ctx, stmt->type_info);
Free(ctx, stmt);
} // delete_typedef_statement
static Statement *new_return_statement(Context *ctx, Expression *expr)
{
NEW_AST_NODE(ReturnStatement);
retval->type = STATEMENTTYPE_RETURN;
retval->next = NULL;
retval->expr = expr;
return (Statement *) retval;
} // new_return_statement
static void delete_return_statement(Context *ctx, ReturnStatement *stmt)
{
DELETE_AST_NODE(stmt);
delete_statement(ctx, stmt->next);
delete_expr(ctx, stmt->expr);
Free(ctx, stmt);
} // delete_return_statement