forked from ziglang/zig
/
ir_print.cpp
1776 lines (1617 loc) · 66.8 KB
/
ir_print.cpp
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
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
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
/*
* Copyright (c) 2016 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/
#include "analyze.hpp"
#include "ir.hpp"
#include "ir_print.hpp"
#include "os.hpp"
struct IrPrint {
CodeGen *codegen;
FILE *f;
int indent;
int indent_size;
};
static void ir_print_other_instruction(IrPrint *irp, IrInstruction *instruction);
static void ir_print_indent(IrPrint *irp) {
for (int i = 0; i < irp->indent; i += 1) {
fprintf(irp->f, " ");
}
}
static void ir_print_prefix(IrPrint *irp, IrInstruction *instruction) {
ir_print_indent(irp);
const char *type_name = instruction->value.type ? buf_ptr(&instruction->value.type->name) : "(unknown)";
const char *ref_count = ir_has_side_effects(instruction) ?
"-" : buf_ptr(buf_sprintf("%" ZIG_PRI_usize "", instruction->ref_count));
fprintf(irp->f, "#%-3zu| %-12s| %-2s| ", instruction->debug_id, type_name, ref_count);
}
static void ir_print_const_value(IrPrint *irp, ConstExprValue *const_val) {
Buf buf = BUF_INIT;
buf_resize(&buf, 0);
render_const_value(irp->codegen, &buf, const_val);
fprintf(irp->f, "%s", buf_ptr(&buf));
}
static void ir_print_var_instruction(IrPrint *irp, IrInstruction *instruction) {
fprintf(irp->f, "#%" ZIG_PRI_usize "", instruction->debug_id);
}
static void ir_print_other_instruction(IrPrint *irp, IrInstruction *instruction) {
if (instruction == nullptr) {
fprintf(irp->f, "(null)");
return;
}
if (instruction->value.special != ConstValSpecialRuntime) {
ir_print_const_value(irp, &instruction->value);
} else {
ir_print_var_instruction(irp, instruction);
}
}
static void ir_print_other_block(IrPrint *irp, IrBasicBlock *bb) {
fprintf(irp->f, "$%s_%" ZIG_PRI_usize "", bb->name_hint, bb->debug_id);
}
static void ir_print_return(IrPrint *irp, IrInstructionReturn *return_instruction) {
assert(return_instruction->value);
fprintf(irp->f, "return ");
ir_print_other_instruction(irp, return_instruction->value);
}
static void ir_print_const(IrPrint *irp, IrInstructionConst *const_instruction) {
ir_print_const_value(irp, &const_instruction->base.value);
}
static const char *ir_bin_op_id_str(IrBinOp op_id) {
switch (op_id) {
case IrBinOpInvalid:
zig_unreachable();
case IrBinOpBoolOr:
return "BoolOr";
case IrBinOpBoolAnd:
return "BoolAnd";
case IrBinOpCmpEq:
return "==";
case IrBinOpCmpNotEq:
return "!=";
case IrBinOpCmpLessThan:
return "<";
case IrBinOpCmpGreaterThan:
return ">";
case IrBinOpCmpLessOrEq:
return "<=";
case IrBinOpCmpGreaterOrEq:
return ">=";
case IrBinOpBinOr:
return "|";
case IrBinOpBinXor:
return "^";
case IrBinOpBinAnd:
return "&";
case IrBinOpBitShiftLeftLossy:
return "<<";
case IrBinOpBitShiftLeftExact:
return "@shlExact";
case IrBinOpBitShiftRightLossy:
return ">>";
case IrBinOpBitShiftRightExact:
return "@shrExact";
case IrBinOpAdd:
return "+";
case IrBinOpAddWrap:
return "+%";
case IrBinOpSub:
return "-";
case IrBinOpSubWrap:
return "-%";
case IrBinOpMult:
return "*";
case IrBinOpMultWrap:
return "*%";
case IrBinOpDivUnspecified:
return "/";
case IrBinOpDivTrunc:
return "@divTrunc";
case IrBinOpDivFloor:
return "@divFloor";
case IrBinOpDivExact:
return "@divExact";
case IrBinOpRemUnspecified:
return "%";
case IrBinOpRemRem:
return "@rem";
case IrBinOpRemMod:
return "@mod";
case IrBinOpArrayCat:
return "++";
case IrBinOpArrayMult:
return "**";
case IrBinOpMergeErrorSets:
return "||";
}
zig_unreachable();
}
static const char *ir_un_op_id_str(IrUnOp op_id) {
switch (op_id) {
case IrUnOpInvalid:
zig_unreachable();
case IrUnOpBinNot:
return "~";
case IrUnOpNegation:
return "-";
case IrUnOpNegationWrap:
return "-%";
case IrUnOpDereference:
return "*";
case IrUnOpOptional:
return "?";
}
zig_unreachable();
}
static void ir_print_un_op(IrPrint *irp, IrInstructionUnOp *un_op_instruction) {
fprintf(irp->f, "%s ", ir_un_op_id_str(un_op_instruction->op_id));
ir_print_other_instruction(irp, un_op_instruction->value);
}
static void ir_print_bin_op(IrPrint *irp, IrInstructionBinOp *bin_op_instruction) {
ir_print_other_instruction(irp, bin_op_instruction->op1);
fprintf(irp->f, " %s ", ir_bin_op_id_str(bin_op_instruction->op_id));
ir_print_other_instruction(irp, bin_op_instruction->op2);
if (!bin_op_instruction->safety_check_on) {
fprintf(irp->f, " // no safety");
}
}
static void ir_print_decl_var(IrPrint *irp, IrInstructionDeclVar *decl_var_instruction) {
const char *var_or_const = decl_var_instruction->var->gen_is_const ? "const" : "var";
const char *name = buf_ptr(&decl_var_instruction->var->name);
if (decl_var_instruction->var_type) {
fprintf(irp->f, "%s %s: ", var_or_const, name);
ir_print_other_instruction(irp, decl_var_instruction->var_type);
fprintf(irp->f, " ");
} else {
fprintf(irp->f, "%s %s ", var_or_const, name);
}
if (decl_var_instruction->align_value) {
fprintf(irp->f, "align ");
ir_print_other_instruction(irp, decl_var_instruction->align_value);
fprintf(irp->f, " ");
}
fprintf(irp->f, "= ");
ir_print_other_instruction(irp, decl_var_instruction->init_value);
if (decl_var_instruction->var->is_comptime != nullptr) {
fprintf(irp->f, " // comptime = ");
ir_print_other_instruction(irp, decl_var_instruction->var->is_comptime);
}
}
static void ir_print_cast(IrPrint *irp, IrInstructionCast *cast_instruction) {
fprintf(irp->f, "cast ");
ir_print_other_instruction(irp, cast_instruction->value);
fprintf(irp->f, " to %s", buf_ptr(&cast_instruction->dest_type->name));
}
static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) {
if (call_instruction->is_async) {
fprintf(irp->f, "async");
if (call_instruction->async_allocator != nullptr) {
fprintf(irp->f, "<");
ir_print_other_instruction(irp, call_instruction->async_allocator);
fprintf(irp->f, ">");
}
fprintf(irp->f, " ");
}
if (call_instruction->fn_entry) {
fprintf(irp->f, "%s", buf_ptr(&call_instruction->fn_entry->symbol_name));
} else {
assert(call_instruction->fn_ref);
ir_print_other_instruction(irp, call_instruction->fn_ref);
}
fprintf(irp->f, "(");
for (size_t i = 0; i < call_instruction->arg_count; i += 1) {
IrInstruction *arg = call_instruction->args[i];
if (i != 0)
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, arg);
}
fprintf(irp->f, ")");
}
static void ir_print_cond_br(IrPrint *irp, IrInstructionCondBr *cond_br_instruction) {
fprintf(irp->f, "if (");
ir_print_other_instruction(irp, cond_br_instruction->condition);
fprintf(irp->f, ") ");
ir_print_other_block(irp, cond_br_instruction->then_block);
fprintf(irp->f, " else ");
ir_print_other_block(irp, cond_br_instruction->else_block);
if (cond_br_instruction->is_comptime != nullptr) {
fprintf(irp->f, " // comptime = ");
ir_print_other_instruction(irp, cond_br_instruction->is_comptime);
}
}
static void ir_print_br(IrPrint *irp, IrInstructionBr *br_instruction) {
fprintf(irp->f, "goto ");
ir_print_other_block(irp, br_instruction->dest_block);
if (br_instruction->is_comptime != nullptr) {
fprintf(irp->f, " // comptime = ");
ir_print_other_instruction(irp, br_instruction->is_comptime);
}
}
static void ir_print_phi(IrPrint *irp, IrInstructionPhi *phi_instruction) {
assert(phi_instruction->incoming_count != 0);
assert(phi_instruction->incoming_count != SIZE_MAX);
for (size_t i = 0; i < phi_instruction->incoming_count; i += 1) {
IrBasicBlock *incoming_block = phi_instruction->incoming_blocks[i];
IrInstruction *incoming_value = phi_instruction->incoming_values[i];
if (i != 0)
fprintf(irp->f, " ");
ir_print_other_block(irp, incoming_block);
fprintf(irp->f, ":");
ir_print_other_instruction(irp, incoming_value);
}
}
static void ir_print_container_init_list(IrPrint *irp, IrInstructionContainerInitList *instruction) {
ir_print_other_instruction(irp, instruction->container_type);
fprintf(irp->f, "{");
if (instruction->item_count > 50) {
fprintf(irp->f, "...(%" ZIG_PRI_usize " items)...", instruction->item_count);
} else {
for (size_t i = 0; i < instruction->item_count; i += 1) {
IrInstruction *item = instruction->items[i];
if (i != 0)
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, item);
}
}
fprintf(irp->f, "}");
}
static void ir_print_container_init_fields(IrPrint *irp, IrInstructionContainerInitFields *instruction) {
ir_print_other_instruction(irp, instruction->container_type);
fprintf(irp->f, "{");
for (size_t i = 0; i < instruction->field_count; i += 1) {
IrInstructionContainerInitFieldsField *field = &instruction->fields[i];
const char *comma = (i == 0) ? "" : ", ";
fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field->name));
ir_print_other_instruction(irp, field->value);
}
fprintf(irp->f, "} // container init");
}
static void ir_print_struct_init(IrPrint *irp, IrInstructionStructInit *instruction) {
fprintf(irp->f, "%s {", buf_ptr(&instruction->struct_type->name));
for (size_t i = 0; i < instruction->field_count; i += 1) {
IrInstructionStructInitField *field = &instruction->fields[i];
Buf *field_name = field->type_struct_field->name;
const char *comma = (i == 0) ? "" : ", ";
fprintf(irp->f, "%s.%s = ", comma, buf_ptr(field_name));
ir_print_other_instruction(irp, field->value);
}
fprintf(irp->f, "} // struct init");
}
static void ir_print_union_init(IrPrint *irp, IrInstructionUnionInit *instruction) {
Buf *field_name = instruction->field->enum_field->name;
fprintf(irp->f, "%s {", buf_ptr(&instruction->union_type->name));
fprintf(irp->f, ".%s = ", buf_ptr(field_name));
ir_print_other_instruction(irp, instruction->init_value);
fprintf(irp->f, "} // union init");
}
static void ir_print_unreachable(IrPrint *irp, IrInstructionUnreachable *instruction) {
fprintf(irp->f, "unreachable");
}
static void ir_print_elem_ptr(IrPrint *irp, IrInstructionElemPtr *instruction) {
fprintf(irp->f, "&");
ir_print_other_instruction(irp, instruction->array_ptr);
fprintf(irp->f, "[");
ir_print_other_instruction(irp, instruction->elem_index);
fprintf(irp->f, "]");
if (!instruction->safety_check_on) {
fprintf(irp->f, " // no safety");
}
}
static void ir_print_var_ptr(IrPrint *irp, IrInstructionVarPtr *instruction) {
fprintf(irp->f, "&%s", buf_ptr(&instruction->var->name));
}
static void ir_print_load_ptr(IrPrint *irp, IrInstructionLoadPtr *instruction) {
fprintf(irp->f, "*");
ir_print_other_instruction(irp, instruction->ptr);
}
static void ir_print_store_ptr(IrPrint *irp, IrInstructionStorePtr *instruction) {
fprintf(irp->f, "*");
ir_print_var_instruction(irp, instruction->ptr);
fprintf(irp->f, " = ");
ir_print_other_instruction(irp, instruction->value);
}
static void ir_print_typeof(IrPrint *irp, IrInstructionTypeOf *instruction) {
fprintf(irp->f, "@typeOf(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_to_ptr_type(IrPrint *irp, IrInstructionToPtrType *instruction) {
fprintf(irp->f, "@toPtrType(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_ptr_type_child(IrPrint *irp, IrInstructionPtrTypeChild *instruction) {
fprintf(irp->f, "@ptrTypeChild(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_field_ptr(IrPrint *irp, IrInstructionFieldPtr *instruction) {
if (instruction->field_name_buffer) {
fprintf(irp->f, "fieldptr ");
ir_print_other_instruction(irp, instruction->container_ptr);
fprintf(irp->f, ".%s", buf_ptr(instruction->field_name_buffer));
} else {
assert(instruction->field_name_expr);
fprintf(irp->f, "@field(");
ir_print_other_instruction(irp, instruction->container_ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->field_name_expr);
fprintf(irp->f, ")");
}
}
static void ir_print_struct_field_ptr(IrPrint *irp, IrInstructionStructFieldPtr *instruction) {
fprintf(irp->f, "@StructFieldPtr(&");
ir_print_other_instruction(irp, instruction->struct_ptr);
fprintf(irp->f, ".%s", buf_ptr(instruction->field->name));
fprintf(irp->f, ")");
}
static void ir_print_union_field_ptr(IrPrint *irp, IrInstructionUnionFieldPtr *instruction) {
fprintf(irp->f, "@UnionFieldPtr(&");
ir_print_other_instruction(irp, instruction->union_ptr);
fprintf(irp->f, ".%s", buf_ptr(instruction->field->enum_field->name));
fprintf(irp->f, ")");
}
static void ir_print_set_cold(IrPrint *irp, IrInstructionSetCold *instruction) {
fprintf(irp->f, "@setCold(");
ir_print_other_instruction(irp, instruction->is_cold);
fprintf(irp->f, ")");
}
static void ir_print_set_runtime_safety(IrPrint *irp, IrInstructionSetRuntimeSafety *instruction) {
fprintf(irp->f, "@setRuntimeSafety(");
ir_print_other_instruction(irp, instruction->safety_on);
fprintf(irp->f, ")");
}
static void ir_print_set_float_mode(IrPrint *irp, IrInstructionSetFloatMode *instruction) {
fprintf(irp->f, "@setFloatMode(");
ir_print_other_instruction(irp, instruction->scope_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->mode_value);
fprintf(irp->f, ")");
}
static void ir_print_array_type(IrPrint *irp, IrInstructionArrayType *instruction) {
fprintf(irp->f, "[");
ir_print_other_instruction(irp, instruction->size);
fprintf(irp->f, "]");
ir_print_other_instruction(irp, instruction->child_type);
}
static void ir_print_promise_type(IrPrint *irp, IrInstructionPromiseType *instruction) {
fprintf(irp->f, "promise");
if (instruction->payload_type != nullptr) {
fprintf(irp->f, "->");
ir_print_other_instruction(irp, instruction->payload_type);
}
}
static void ir_print_slice_type(IrPrint *irp, IrInstructionSliceType *instruction) {
const char *const_kw = instruction->is_const ? "const " : "";
fprintf(irp->f, "[]%s", const_kw);
ir_print_other_instruction(irp, instruction->child_type);
}
static void ir_print_asm(IrPrint *irp, IrInstructionAsm *instruction) {
assert(instruction->base.source_node->type == NodeTypeAsmExpr);
AstNodeAsmExpr *asm_expr = &instruction->base.source_node->data.asm_expr;
const char *volatile_kw = instruction->has_side_effects ? " volatile" : "";
fprintf(irp->f, "asm%s (\"%s\") : ", volatile_kw, buf_ptr(asm_expr->asm_template));
for (size_t i = 0; i < asm_expr->output_list.length; i += 1) {
AsmOutput *asm_output = asm_expr->output_list.at(i);
if (i != 0) fprintf(irp->f, ", ");
fprintf(irp->f, "[%s] \"%s\" (",
buf_ptr(asm_output->asm_symbolic_name),
buf_ptr(asm_output->constraint));
if (asm_output->return_type) {
fprintf(irp->f, "-> ");
ir_print_other_instruction(irp, instruction->output_types[i]);
} else {
fprintf(irp->f, "%s", buf_ptr(asm_output->variable_name));
}
fprintf(irp->f, ")");
}
fprintf(irp->f, " : ");
for (size_t i = 0; i < asm_expr->input_list.length; i += 1) {
AsmInput *asm_input = asm_expr->input_list.at(i);
if (i != 0) fprintf(irp->f, ", ");
fprintf(irp->f, "[%s] \"%s\" (",
buf_ptr(asm_input->asm_symbolic_name),
buf_ptr(asm_input->constraint));
ir_print_other_instruction(irp, instruction->input_list[i]);
fprintf(irp->f, ")");
}
fprintf(irp->f, " : ");
for (size_t i = 0; i < asm_expr->clobber_list.length; i += 1) {
Buf *reg_name = asm_expr->clobber_list.at(i);
if (i != 0) fprintf(irp->f, ", ");
fprintf(irp->f, "\"%s\"", buf_ptr(reg_name));
}
fprintf(irp->f, ")");
}
static void ir_print_size_of(IrPrint *irp, IrInstructionSizeOf *instruction) {
fprintf(irp->f, "@sizeOf(");
ir_print_other_instruction(irp, instruction->type_value);
fprintf(irp->f, ")");
}
static void ir_print_test_null(IrPrint *irp, IrInstructionTestNonNull *instruction) {
fprintf(irp->f, "*");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, " != null");
}
static void ir_print_unwrap_maybe(IrPrint *irp, IrInstructionUnwrapOptional *instruction) {
fprintf(irp->f, "&??*");
ir_print_other_instruction(irp, instruction->value);
if (!instruction->safety_check_on) {
fprintf(irp->f, " // no safety");
}
}
static void ir_print_clz(IrPrint *irp, IrInstructionClz *instruction) {
fprintf(irp->f, "@clz(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_ctz(IrPrint *irp, IrInstructionCtz *instruction) {
fprintf(irp->f, "@ctz(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_pop_count(IrPrint *irp, IrInstructionPopCount *instruction) {
fprintf(irp->f, "@popCount(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_switch_br(IrPrint *irp, IrInstructionSwitchBr *instruction) {
fprintf(irp->f, "switch (");
ir_print_other_instruction(irp, instruction->target_value);
fprintf(irp->f, ") ");
for (size_t i = 0; i < instruction->case_count; i += 1) {
IrInstructionSwitchBrCase *this_case = &instruction->cases[i];
ir_print_other_instruction(irp, this_case->value);
fprintf(irp->f, " => ");
ir_print_other_block(irp, this_case->block);
fprintf(irp->f, ", ");
}
fprintf(irp->f, "else => ");
ir_print_other_block(irp, instruction->else_block);
if (instruction->is_comptime != nullptr) {
fprintf(irp->f, " // comptime = ");
ir_print_other_instruction(irp, instruction->is_comptime);
}
}
static void ir_print_switch_var(IrPrint *irp, IrInstructionSwitchVar *instruction) {
fprintf(irp->f, "switchvar ");
ir_print_other_instruction(irp, instruction->target_value_ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->prong_value);
}
static void ir_print_switch_target(IrPrint *irp, IrInstructionSwitchTarget *instruction) {
fprintf(irp->f, "switchtarget ");
ir_print_other_instruction(irp, instruction->target_value_ptr);
}
static void ir_print_union_tag(IrPrint *irp, IrInstructionUnionTag *instruction) {
fprintf(irp->f, "uniontag ");
ir_print_other_instruction(irp, instruction->value);
}
static void ir_print_import(IrPrint *irp, IrInstructionImport *instruction) {
fprintf(irp->f, "@import(");
ir_print_other_instruction(irp, instruction->name);
fprintf(irp->f, ")");
}
static void ir_print_array_len(IrPrint *irp, IrInstructionArrayLen *instruction) {
ir_print_other_instruction(irp, instruction->array_value);
fprintf(irp->f, ".len");
}
static void ir_print_ref(IrPrint *irp, IrInstructionRef *instruction) {
const char *const_str = instruction->is_const ? "const " : "";
const char *volatile_str = instruction->is_volatile ? "volatile " : "";
fprintf(irp->f, "%s%sref ", const_str, volatile_str);
ir_print_other_instruction(irp, instruction->value);
}
static void ir_print_compile_err(IrPrint *irp, IrInstructionCompileErr *instruction) {
fprintf(irp->f, "@compileError(");
ir_print_other_instruction(irp, instruction->msg);
fprintf(irp->f, ")");
}
static void ir_print_compile_log(IrPrint *irp, IrInstructionCompileLog *instruction) {
fprintf(irp->f, "@compileLog(");
for (size_t i = 0; i < instruction->msg_count; i += 1) {
if (i != 0)
fprintf(irp->f, ",");
IrInstruction *msg = instruction->msg_list[i];
ir_print_other_instruction(irp, msg);
}
fprintf(irp->f, ")");
}
static void ir_print_err_name(IrPrint *irp, IrInstructionErrName *instruction) {
fprintf(irp->f, "@errorName(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_c_import(IrPrint *irp, IrInstructionCImport *instruction) {
fprintf(irp->f, "@cImport(...)");
}
static void ir_print_c_include(IrPrint *irp, IrInstructionCInclude *instruction) {
fprintf(irp->f, "@cInclude(");
ir_print_other_instruction(irp, instruction->name);
fprintf(irp->f, ")");
}
static void ir_print_c_define(IrPrint *irp, IrInstructionCDefine *instruction) {
fprintf(irp->f, "@cDefine(");
ir_print_other_instruction(irp, instruction->name);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_c_undef(IrPrint *irp, IrInstructionCUndef *instruction) {
fprintf(irp->f, "@cUndef(");
ir_print_other_instruction(irp, instruction->name);
fprintf(irp->f, ")");
}
static void ir_print_embed_file(IrPrint *irp, IrInstructionEmbedFile *instruction) {
fprintf(irp->f, "@embedFile(");
ir_print_other_instruction(irp, instruction->name);
fprintf(irp->f, ")");
}
static void ir_print_cmpxchg(IrPrint *irp, IrInstructionCmpxchg *instruction) {
fprintf(irp->f, "@cmpxchg(");
ir_print_other_instruction(irp, instruction->ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->cmp_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->new_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->success_order_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->failure_order_value);
fprintf(irp->f, ")");
}
static void ir_print_fence(IrPrint *irp, IrInstructionFence *instruction) {
fprintf(irp->f, "@fence(");
ir_print_other_instruction(irp, instruction->order_value);
fprintf(irp->f, ")");
}
static void ir_print_truncate(IrPrint *irp, IrInstructionTruncate *instruction) {
fprintf(irp->f, "@truncate(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_cast(IrPrint *irp, IrInstructionIntCast *instruction) {
fprintf(irp->f, "@intCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_cast(IrPrint *irp, IrInstructionFloatCast *instruction) {
fprintf(irp->f, "@floatCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_err_set_cast(IrPrint *irp, IrInstructionErrSetCast *instruction) {
fprintf(irp->f, "@errSetCast(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_from_bytes(IrPrint *irp, IrInstructionFromBytes *instruction) {
fprintf(irp->f, "@bytesToSlice(");
ir_print_other_instruction(irp, instruction->dest_child_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_to_bytes(IrPrint *irp, IrInstructionToBytes *instruction) {
fprintf(irp->f, "@sliceToBytes(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_to_float(IrPrint *irp, IrInstructionIntToFloat *instruction) {
fprintf(irp->f, "@intToFloat(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_float_to_int(IrPrint *irp, IrInstructionFloatToInt *instruction) {
fprintf(irp->f, "@floatToInt(");
ir_print_other_instruction(irp, instruction->dest_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_bool_to_int(IrPrint *irp, IrInstructionBoolToInt *instruction) {
fprintf(irp->f, "@boolToInt(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_type(IrPrint *irp, IrInstructionIntType *instruction) {
fprintf(irp->f, "@IntType(");
ir_print_other_instruction(irp, instruction->is_signed);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->bit_count);
fprintf(irp->f, ")");
}
static void ir_print_bool_not(IrPrint *irp, IrInstructionBoolNot *instruction) {
fprintf(irp->f, "! ");
ir_print_other_instruction(irp, instruction->value);
}
static void ir_print_memset(IrPrint *irp, IrInstructionMemset *instruction) {
fprintf(irp->f, "@memset(");
ir_print_other_instruction(irp, instruction->dest_ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->byte);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->count);
fprintf(irp->f, ")");
}
static void ir_print_memcpy(IrPrint *irp, IrInstructionMemcpy *instruction) {
fprintf(irp->f, "@memcpy(");
ir_print_other_instruction(irp, instruction->dest_ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->src_ptr);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->count);
fprintf(irp->f, ")");
}
static void ir_print_slice(IrPrint *irp, IrInstructionSlice *instruction) {
ir_print_other_instruction(irp, instruction->ptr);
fprintf(irp->f, "[");
ir_print_other_instruction(irp, instruction->start);
fprintf(irp->f, "..");
if (instruction->end)
ir_print_other_instruction(irp, instruction->end);
fprintf(irp->f, "]");
}
static void ir_print_member_count(IrPrint *irp, IrInstructionMemberCount *instruction) {
fprintf(irp->f, "@memberCount(");
ir_print_other_instruction(irp, instruction->container);
fprintf(irp->f, ")");
}
static void ir_print_member_type(IrPrint *irp, IrInstructionMemberType *instruction) {
fprintf(irp->f, "@memberType(");
ir_print_other_instruction(irp, instruction->container_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->member_index);
fprintf(irp->f, ")");
}
static void ir_print_member_name(IrPrint *irp, IrInstructionMemberName *instruction) {
fprintf(irp->f, "@memberName(");
ir_print_other_instruction(irp, instruction->container_type);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->member_index);
fprintf(irp->f, ")");
}
static void ir_print_breakpoint(IrPrint *irp, IrInstructionBreakpoint *instruction) {
fprintf(irp->f, "@breakpoint()");
}
static void ir_print_frame_address(IrPrint *irp, IrInstructionFrameAddress *instruction) {
fprintf(irp->f, "@frameAddress()");
}
static void ir_print_handle(IrPrint *irp, IrInstructionHandle *instruction) {
fprintf(irp->f, "@handle()");
}
static void ir_print_return_address(IrPrint *irp, IrInstructionReturnAddress *instruction) {
fprintf(irp->f, "@returnAddress()");
}
static void ir_print_align_of(IrPrint *irp, IrInstructionAlignOf *instruction) {
fprintf(irp->f, "@alignOf(");
ir_print_other_instruction(irp, instruction->type_value);
fprintf(irp->f, ")");
}
static void ir_print_overflow_op(IrPrint *irp, IrInstructionOverflowOp *instruction) {
switch (instruction->op) {
case IrOverflowOpAdd:
fprintf(irp->f, "@addWithOverflow(");
break;
case IrOverflowOpSub:
fprintf(irp->f, "@subWithOverflow(");
break;
case IrOverflowOpMul:
fprintf(irp->f, "@mulWithOverflow(");
break;
case IrOverflowOpShl:
fprintf(irp->f, "@shlWithOverflow(");
break;
}
ir_print_other_instruction(irp, instruction->type_value);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->op1);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->op2);
fprintf(irp->f, ", ");
ir_print_other_instruction(irp, instruction->result_ptr);
fprintf(irp->f, ")");
}
static void ir_print_test_err(IrPrint *irp, IrInstructionTestErr *instruction) {
fprintf(irp->f, "@testError(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_unwrap_err_code(IrPrint *irp, IrInstructionUnwrapErrCode *instruction) {
fprintf(irp->f, "@unwrapErrorCode(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayload *instruction) {
fprintf(irp->f, "@unwrapErrorPayload(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
if (!instruction->safety_check_on) {
fprintf(irp->f, " // no safety");
}
}
static void ir_print_maybe_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
fprintf(irp->f, "@maybeWrap(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_err_wrap_code(IrPrint *irp, IrInstructionErrWrapCode *instruction) {
fprintf(irp->f, "@errWrapCode(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_err_wrap_payload(IrPrint *irp, IrInstructionErrWrapPayload *instruction) {
fprintf(irp->f, "@errWrapPayload(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_fn_proto(IrPrint *irp, IrInstructionFnProto *instruction) {
fprintf(irp->f, "fn(");
for (size_t i = 0; i < instruction->base.source_node->data.fn_proto.params.length; i += 1) {
if (i != 0)
fprintf(irp->f, ",");
if (instruction->is_var_args && i == instruction->base.source_node->data.fn_proto.params.length - 1) {
fprintf(irp->f, "...");
} else {
ir_print_other_instruction(irp, instruction->param_types[i]);
}
}
fprintf(irp->f, ")");
if (instruction->align_value != nullptr) {
fprintf(irp->f, " align ");
ir_print_other_instruction(irp, instruction->align_value);
fprintf(irp->f, " ");
}
fprintf(irp->f, "->");
ir_print_other_instruction(irp, instruction->return_type);
}
static void ir_print_test_comptime(IrPrint *irp, IrInstructionTestComptime *instruction) {
fprintf(irp->f, "@testComptime(");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_ptr_cast(IrPrint *irp, IrInstructionPtrCast *instruction) {
fprintf(irp->f, "@ptrCast(");
if (instruction->dest_type) {
ir_print_other_instruction(irp, instruction->dest_type);
}
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->ptr);
fprintf(irp->f, ")");
}
static void ir_print_bit_cast(IrPrint *irp, IrInstructionBitCast *instruction) {
fprintf(irp->f, "@bitCast(");
if (instruction->dest_type) {
ir_print_other_instruction(irp, instruction->dest_type);
}
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->value);
fprintf(irp->f, ")");
}
static void ir_print_widen_or_shorten(IrPrint *irp, IrInstructionWidenOrShorten *instruction) {
fprintf(irp->f, "@widenOrShorten(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_ptr_to_int(IrPrint *irp, IrInstructionPtrToInt *instruction) {
fprintf(irp->f, "@ptrToInt(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_to_ptr(IrPrint *irp, IrInstructionIntToPtr *instruction) {
fprintf(irp->f, "@intToPtr(");
if (instruction->dest_type == nullptr) {
fprintf(irp->f, "(null)");
} else {
ir_print_other_instruction(irp, instruction->dest_type);
}
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_int_to_enum(IrPrint *irp, IrInstructionIntToEnum *instruction) {
fprintf(irp->f, "@intToEnum(");
if (instruction->dest_type == nullptr) {
fprintf(irp->f, "(null)");
} else {
ir_print_other_instruction(irp, instruction->dest_type);
}
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_enum_to_int(IrPrint *irp, IrInstructionEnumToInt *instruction) {
fprintf(irp->f, "@enumToInt(");
ir_print_other_instruction(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_check_runtime_scope(IrPrint *irp, IrInstructionCheckRuntimeScope *instruction) {
fprintf(irp->f, "@checkRuntimeScope(");
ir_print_other_instruction(irp, instruction->scope_is_comptime);
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->is_comptime);
fprintf(irp->f, ")");
}
static void ir_print_int_to_err(IrPrint *irp, IrInstructionIntToErr *instruction) {
fprintf(irp->f, "inttoerr ");
ir_print_other_instruction(irp, instruction->target);
}
static void ir_print_err_to_int(IrPrint *irp, IrInstructionErrToInt *instruction) {
fprintf(irp->f, "errtoint ");
ir_print_other_instruction(irp, instruction->target);
}
static void ir_print_check_switch_prongs(IrPrint *irp, IrInstructionCheckSwitchProngs *instruction) {
fprintf(irp->f, "@checkSwitchProngs(");
ir_print_other_instruction(irp, instruction->target_value);
fprintf(irp->f, ",");
for (size_t i = 0; i < instruction->range_count; i += 1) {
if (i != 0)
fprintf(irp->f, ",");
ir_print_other_instruction(irp, instruction->ranges[i].start);
fprintf(irp->f, "...");
ir_print_other_instruction(irp, instruction->ranges[i].end);
}
const char *have_else_str = instruction->have_else_prong ? "yes" : "no";
fprintf(irp->f, ")else:%s", have_else_str);
}
static void ir_print_check_statement_is_void(IrPrint *irp, IrInstructionCheckStatementIsVoid *instruction) {
fprintf(irp->f, "@checkStatementIsVoid(");
ir_print_other_instruction(irp, instruction->statement_value);
fprintf(irp->f, ")");
}
static void ir_print_type_name(IrPrint *irp, IrInstructionTypeName *instruction) {
fprintf(irp->f, "typename ");
ir_print_other_instruction(irp, instruction->type_value);
}
static void ir_print_tag_name(IrPrint *irp, IrInstructionTagName *instruction) {
fprintf(irp->f, "tagname ");
ir_print_other_instruction(irp, instruction->target);
}
static void ir_print_ptr_type(IrPrint *irp, IrInstructionPtrType *instruction) {
fprintf(irp->f, "&");
if (instruction->align_value != nullptr) {
fprintf(irp->f, "align(");