-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodegen_expression.c
More file actions
746 lines (652 loc) · 20.1 KB
/
Copy pathcodegen_expression.c
File metadata and controls
746 lines (652 loc) · 20.1 KB
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
#include "header.h"
#include "print_x86_64.h"
#include "printerstate.h"
#include "std.h"
#include "std_io.h"
static int count_args(const struct Vector /*<Expr>*/ *ref_args);
static void pass_args(struct PrinterState *ptr_prs,
const struct Vector /*<Expr>*/ *ref_args);
static void print_op_pointer_plusminus_int(int is_plus, int size)
{
gen_cltq();
gen_mul_by_const(size);
gen_op_8byte(is_plus ? "addq" : "subq");
}
/* size is garbage unless left_type is a pointer */
static void print_simple_binary_op(enum SimpleBinOp kind,
const struct Type *ref_left_type, int size)
{
const struct Type left_type = *ref_left_type;
assert(!is_struct_or_union(&left_type));
if (left_type.type_category == PTR_) {
switch (kind) {
case SIMPLE_BIN_OP_PLUS:
print_op_pointer_plusminus_int(1, size);
return;
case SIMPLE_BIN_OP_MINUS:
print_op_pointer_plusminus_int(0, size);
return;
case SIMPLE_BIN_OP_EQ_EQ:
gen_compare_ptrs("sete");
return;
case SIMPLE_BIN_OP_NOT_EQ:
gen_compare_ptrs("setne");
return;
case SIMPLE_BIN_OP_LT_EQ:
gen_compare_ptrs("setbe");
return;
case SIMPLE_BIN_OP_GT_EQ:
gen_compare_ptrs("setnb");
return;
case SIMPLE_BIN_OP_LT:
gen_compare_ptrs("setb");
return;
case SIMPLE_BIN_OP_GT:
gen_compare_ptrs("seta");
return;
default:
simple_error("unexpected pointer in binary operation\n");
}
}
switch (kind) {
case SIMPLE_BIN_OP_PLUS:
gen_op_ints("addl");
return;
case SIMPLE_BIN_OP_MINUS:
gen_op_ints("subl");
return;
case SIMPLE_BIN_OP_ASTERISK:
gen_mul_ints();
return;
case SIMPLE_BIN_OP_SLASH:
gen_div_ints();
return;
case SIMPLE_BIN_OP_PERCENT:
gen_rem_ints();
return;
case SIMPLE_BIN_OP_LT:
gen_compare_ints("setl");
return;
case SIMPLE_BIN_OP_LT_EQ:
gen_compare_ints("setle");
return;
case SIMPLE_BIN_OP_LSHIFT:
gen_shift_ints("sall");
return;
case SIMPLE_BIN_OP_GT:
gen_compare_ints("setg");
return;
case SIMPLE_BIN_OP_GT_EQ:
gen_compare_ints("setge");
return;
case SIMPLE_BIN_OP_RSHIFT:
gen_shift_ints("sarl");
return;
case SIMPLE_BIN_OP_AND:
gen_op_ints("andl");
return;
case SIMPLE_BIN_OP_OR:
gen_op_ints("orl");
return;
case SIMPLE_BIN_OP_EQ_EQ:
gen_compare_ints("sete");
return;
case SIMPLE_BIN_OP_NOT_EQ:
gen_compare_ints("setne");
return;
case SIMPLE_BIN_OP_HAT:
gen_op_ints("xorl");
return;
}
}
void print_address_of_lvalue(struct PrinterState *ptr_prs,
const struct Expr *ref_expr, const char *msg)
{
const struct Expr expr = *ref_expr;
switch (expr.category) {
case LOCAL_VAR_:
case GLOBAL_VAR_:
case UNARY_OP_EXPR:
case COMMA_EXPR: {
print_address_of_lvalue_or_struct_or_union(ptr_prs, ref_expr, msg);
return;
}
default:
fprintf(stderr, "context: %s\n", msg);
simple_error("doesn't seem like an lvalue\n");
}
}
void print_address_of_lvalue_or_struct_or_union(struct PrinterState *ptr_prs,
const struct Expr *ref_expr,
const char *msg)
{
const struct Expr expr = *ref_expr;
switch (expr.category) {
case CONDITIONAL_EXPR: {
int label1 = get_new_label_name(ptr_prs);
int label2 = get_new_label_name(ptr_prs);
print_expression(ptr_prs, expr.ptr1);
gen_if_zero_jmp_nbyte(
size_of_basic(&expr.ptr1->details.type,
"condition of conditional expression"),
label1, 0);
if (!is_struct_or_union(&expr.ptr2->details.type)) {
simple_error("the conditional operator is used as lvalue");
}
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr2, "true branch of ternary operator");
gen_jump(label2, "ternary operator");
gen_label(label1);
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr3, "false branch of ternary operator");
gen_label(label2);
gen_discard2nd();
return;
}
case STRUCT_OR_UNION_ASSIGNMENT_EXPR: {
print_address_of_lvalue(
ptr_prs, expr.ptr1, "left hand of struct assignment");
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr2, "right hand of struct assignment");
int size = expr.size_info_for_struct_or_union_assign;
gen_copy_1st_struct_or_union_to_2nd_and_discard(size);
print_address_of_lvalue(
ptr_prs, expr.ptr1, "left hand of struct assignment");
return;
}
case FPCALL_EXPR_RETURNING_INTEGER_CLASS: {
print_expression(ptr_prs, expr.ptr1);
pass_args(ptr_prs, &expr.args);
gen_pop_to_reg_8byte("r11");
int size = expr.size_info_for_struct_or_union_assign;
gen_raw_call_partA();
gen_call_reg_and_assign_integerclass_struct_or_union_or_union_to_local(
"r11", expr.local_var_offset, size);
gen_push_address_of_local(expr.local_var_offset);
return;
}
case FPCALL_EXPR_RETURNING_MEMORY_CLASS: {
print_expression(ptr_prs, expr.ptr1);
pass_args(ptr_prs, &expr.args);
gen_pop_to_reg_8byte("r11");
/* call a function that returns a void */
gen_raw_call_partA();
gen_call_reg_and_push_ret_of_nbyte(4, "r11");
gen_discard();
gen_push_address_of_local(expr.local_var_offset);
return;
}
case FUNCCALL_EXPR_RETURNING_INTEGER_CLASS: {
pass_args(ptr_prs, &expr.args);
int size = expr.size_info_for_struct_or_union_assign;
gen_raw_call_partA();
gen_call_and_assign_integerclass_struct_or_union_to_local(
expr.global_var_name, expr.local_var_offset, size);
gen_push_address_of_local(expr.local_var_offset);
return;
}
case FUNCCALL_EXPR_RETURNING_MEMORY_CLASS: {
int arg_stacksize = count_args(&expr.args) * 8;
if (arg_stacksize % 16 != 0) {
unsupported("misaligned stack");
}
gen_raw_call_partA();
pass_args(ptr_prs, &expr.args);
/* call a function that returns a void */
gen_push_ret_of_nbyte(4, expr.global_var_name, arg_stacksize);
gen_discard();
gen_push_address_of_local(expr.local_var_offset);
return;
}
case LOCAL_VAR_: {
gen_push_address_of_local(expr.local_var_offset);
return;
}
case GLOBAL_VAR_: {
gen_push_address_of_global(expr.global_var_name);
return;
}
case UNARY_OP_EXPR:
if (expr.unary_operator == UNARY_OP_ASTERISK) {
print_expression(ptr_prs, expr.ptr1);
return;
} else {
simple_error("the only unary operator that can create "
"lvalue is `*`\n");
}
case VOID_EXPR:
fprintf(stderr, "context: %s\n", msg);
simple_error("doesn't seem like an lvalue; it is a void expr.\n");
case COMMA_EXPR: {
print_expression_or_addr_of_struct_or_union(
ptr_prs, expr.ptr1, "struct as the first operand of comma");
if (!is_struct_or_union(&expr.ptr2->details.type)) {
simple_error(
"either the result of comma expression is used as an "
"lvalue, or the comma expression is used as if it were a "
"struct/union though the second operand of comma isn't.\n");
}
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr2, "struct/union as the second operand of comma");
gen_discard2nd();
return;
}
default:
fprintf(stderr, "context: %s\n", msg);
simple_error("doesn't seem like an lvalue or a struct\n");
}
}
void print_expression(struct PrinterState *ptr_prs,
const struct Expr *ref_expr);
static void print_expression_as_lvalue(struct PrinterState *ptr_prs,
const struct Expr *ref_expr);
static void print_expression_as_lvalue(struct PrinterState *ptr_prs,
const struct Expr *ref_expr)
{
const struct Expr expr = *ref_expr;
print_address_of_lvalue_or_struct_or_union(ptr_prs, &expr, "as lvalue");
switch (expr.category) {
case FPCALL_EXPR_RETURNING_INTEGER_CLASS:
case FPCALL_EXPR_RETURNING_MEMORY_CLASS:
case FUNCCALL_EXPR_RETURNING_INTEGER_CLASS:
case FUNCCALL_EXPR_RETURNING_MEMORY_CLASS: {
unsupported("struct returned by function used as a pure rvalue");
}
case LOCAL_VAR_: {
gen_push_from_local_nbyte(
size_of_basic(&expr.details.type, "local var as lvalue"),
expr.local_var_offset);
return;
}
case GLOBAL_VAR_: {
printf("//load from global `%s`\n", expr.global_var_name);
gen_peek_deref_push_nbyte(
size_of_basic(&expr.details.type, "global var as lvalue"));
return;
}
case UNARY_OP_EXPR:
if (expr.unary_operator == UNARY_OP_ASTERISK) {
gen_peek_deref_push_nbyte(
size_of_basic(&expr.details.type, "*expr as lvalue"));
return;
} else {
simple_error("the only unary operator that can create "
"lvalue is `*`\n");
}
default:
simple_error("doesn't seem like an lvalue\n");
}
}
void handle_builtin(struct PrinterState *ptr_prs, const char *ident_str)
{
if (strcmp(ident_str, "__builtin_va_end") == 0) {
printf("// do nothing for %s\n", ident_str);
return;
} else if (strcmp(ident_str, "__builtin_va_start") == 0) {
int float_arg_num = 0; /* since we still cannot handle them */
gen_va_start(8 * ptr_prs->integral_explicit_arg_num,
48 * (1 + float_arg_num), ptr_prs->reg_save_area);
return;
}
printf("***** Help me: %s\n", ident_str);
}
void print_expression(struct PrinterState *ptr_prs, const struct Expr *ref_expr)
{
const struct Expr expr = *ref_expr;
switch (expr.category) {
case FPCALL_EXPR_RETURNING_INTEGER_CLASS:
case FPCALL_EXPR_RETURNING_MEMORY_CLASS:
case FUNCCALL_EXPR_RETURNING_INTEGER_CLASS:
case FUNCCALL_EXPR_RETURNING_MEMORY_CLASS: {
unsupported("struct/union returned by function used as a pure rvalue");
}
case STRUCT_OR_UNION_ASSIGNMENT_EXPR: {
print_address_of_lvalue(
ptr_prs, expr.ptr1, "left hand of struct/union assignment");
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr2, "right hand of struct/union assignment");
int size = expr.size_info_for_struct_or_union_assign;
gen_copy_1st_struct_or_union_to_2nd_and_discard(size);
gen_push_int(143253); /* random garbage */
return;
}
case VOID_EXPR: {
gen_push_int(123); /* random garbage */
return;
}
case NULLPTR: {
gen_push_nullptr();
return;
}
case PTR_STRUCT_AND_OFFSET: {
print_address_of_lvalue_or_struct_or_union(
ptr_prs, expr.ptr1, "struct/union whose member is to be accessed");
int offset = expr.struct_offset;
if (offset) {
gen_push_int(offset);
gen_cltq();
gen_op_8byte("addq");
}
return;
}
case POINTER_MINUS_POINTER: {
print_expression(ptr_prs, expr.ptr1);
print_expression(ptr_prs, expr.ptr2);
int size = expr.size_info_for_pointer_arith;
gen_op_8byte("subq");
gen_div_by_const(size);
return;
}
case POINTER_PLUS_INT:
case POINTER_MINUS_INT: {
int size = expr.size_info_for_pointer_arith;
print_expression(ptr_prs, expr.ptr1);
print_expression(ptr_prs, expr.ptr2);
print_op_pointer_plusminus_int(expr.category == POINTER_PLUS_INT, size);
return;
}
case POSTFIX_INCREMENT:
case POSTFIX_DECREMENT: {
enum SimpleBinOp opkind2 = expr.category == POSTFIX_INCREMENT
? SIMPLE_BIN_OP_PLUS
: SIMPLE_BIN_OP_MINUS;
print_expression_as_lvalue(ptr_prs, expr.ptr1);
gen_push_int(1);
print_simple_binary_op(opkind2, &expr.ptr1->details.type,
expr.size_info_for_pointer_arith);
gen_assign_nbyte(
size_of_basic(&expr.ptr1->details.type, "postfix inc/dec"));
gen_push_int(-1);
print_simple_binary_op(opkind2, &expr.ptr1->details.type,
expr.size_info_for_pointer_arith);
return;
}
case LOCAL_VAR_: {
if (expr.details.true_type.type_category == ARRAY) {
gen_push_address_of_local(expr.local_var_offset);
return;
}
gen_push_from_local_nbyte(
size_of_basic(&expr.details.type, "local var as rvalue"),
expr.local_var_offset);
return;
}
case GLOBAL_VAR_: {
printf("//global `%s` as rvalue\n", expr.global_var_name);
if (expr.details.true_type.type_category == ARRAY) {
gen_push_address_of_global(expr.global_var_name);
return;
}
gen_push_address_of_global(expr.global_var_name);
gen_peek_and_dereference_nbyte(
size_of_basic(&expr.details.type, "global var as rvalue"));
return;
}
case SIMPLE_BINARY_EXPR: {
print_expression(ptr_prs, expr.ptr1);
print_expression(ptr_prs, expr.ptr2);
print_simple_binary_op(expr.simple_binary_operator,
&expr.ptr1->details.type,
expr.size_info_for_pointer_arith);
return;
}
case COMMA_EXPR: {
print_expression_or_addr_of_struct_or_union(
ptr_prs, expr.ptr1, "struct as the first operand of comma");
if (is_struct_or_union(&expr.ptr2->details.type)) {
simple_error("struct/union is used as the right operand of comma "
"operator, but the result of comma expression is used "
"in a non-struct/non-union manner.\n");
} else {
print_expression(ptr_prs, expr.ptr2);
}
gen_discard2nd();
return;
}
case LOGICAL_OR_EXPR: {
int label1 = get_new_label_name(ptr_prs);
int label2 = get_new_label_name(ptr_prs);
print_expression(ptr_prs, expr.ptr1);
gen_if_nonzero_jmp_nbyte(
size_of_basic(&expr.ptr1->details.type, "operand of logical or"),
label1, 0);
print_expression(ptr_prs, expr.ptr2);
gen_discard();
gen_if_nonzero_jmp_nbyte(
size_of_basic(&expr.ptr2->details.type, "operand of logical or"),
label1, -8);
gen_discard();
gen_push_int(0);
gen_jump(label2, "logical OR");
gen_label(label1);
gen_discard();
gen_push_int(1);
gen_label(label2);
return;
}
case LOGICAL_AND_EXPR: {
int label1 = get_new_label_name(ptr_prs);
int label2 = get_new_label_name(ptr_prs);
print_expression(ptr_prs, expr.ptr1);
gen_if_zero_jmp_nbyte(
size_of_basic(&expr.ptr1->details.type, "operand of logical and"),
label1, 0);
print_expression(ptr_prs, expr.ptr2);
gen_discard();
gen_if_zero_jmp_nbyte(
size_of_basic(&expr.ptr2->details.type, "operand of logical and"),
label1, -8);
gen_discard();
gen_push_int(1);
gen_jump(label2, "logical AND");
printf(".L%d:\n", label1);
gen_discard();
gen_push_int(0);
printf(".L%d:\n", label2);
return;
}
case ASSIGNMENT_EXPR: {
print_expression_as_lvalue(ptr_prs, expr.ptr1);
print_expression(ptr_prs, expr.ptr2);
gen_discard2nd();
struct Type type = expr.ptr1->details.type;
gen_assign_nbyte(size_of_basic(&type, "operand of assignment"));
return;
}
case COMPOUND_ASSIGNMENT_EXPR: {
print_expression_as_lvalue(ptr_prs, expr.ptr1);
print_expression(ptr_prs, expr.ptr2);
print_simple_binary_op(expr.simple_binary_operator,
&expr.ptr1->details.type,
expr.size_info_for_pointer_arith);
struct Type type = expr.ptr1->details.type;
gen_assign_nbyte(size_of_basic(&type, "operand of assignment"));
return;
}
case INT_VALUE:
gen_push_int(expr.int_value);
return;
case UNARY_OP_EXPR:
switch (expr.unary_operator) {
case UNARY_OP_NOT: {
print_expression(ptr_prs, expr.ptr1);
if (size_of_basic(&expr.ptr1->details.type,
"operand of logical not") == 8) {
gen_logical_not_of_pointer();
} else {
gen_unary_not();
}
return;
}
case UNARY_OP_TILDA:
print_expression(ptr_prs, expr.ptr1);
gen_unary("notl");
return;
case UNARY_OP_PLUS:
print_expression(ptr_prs, expr.ptr1);
/* do nothing */
return;
case UNARY_OP_MINUS:
print_expression(ptr_prs, expr.ptr1);
gen_unary("negl");
return;
case UNARY_OP_PLUS_PLUS:
case UNARY_OP_MINUS_MINUS: {
print_expression_as_lvalue(ptr_prs, expr.ptr1);
gen_push_int(1);
print_simple_binary_op(
expr.unary_operator == UNARY_OP_PLUS_PLUS ? SIMPLE_BIN_OP_PLUS
: SIMPLE_BIN_OP_MINUS,
&expr.ptr1->details.type, expr.size_info_for_pointer_arith);
struct Type type = expr.ptr1->details.type;
gen_assign_nbyte(size_of_basic(&type, "prefix inc/dec"));
return;
}
case UNARY_OP_AND: {
struct Type type = expr.ptr1->details.type;
struct Type true_type = expr.ptr1->details.true_type;
if (type.type_category == PTR_ &&
true_type.type_category == ARRAY) {
print_expression(ptr_prs, expr.ptr1);
} else {
print_address_of_lvalue(
ptr_prs, expr.ptr1, "address requested by & operator");
}
return;
}
case UNARY_OP_ASTERISK: {
print_expression(ptr_prs, expr.ptr1);
struct Type type = expr.details.type;
struct Type true_type = expr.details.true_type;
if (type.type_category == PTR_ &&
true_type.type_category == ARRAY) {
/* do not dereference, if it is an array */
return;
}
gen_peek_and_dereference_nbyte(
size_of_basic(&type, "*expr as rvalue"));
return;
}
}
return;
case CONDITIONAL_EXPR: {
int label1 = get_new_label_name(ptr_prs);
int label2 = get_new_label_name(ptr_prs);
print_expression(ptr_prs, expr.ptr1);
gen_if_zero_jmp_nbyte(
size_of_basic(&expr.ptr1->details.type,
"condition of conditional expression"),
label1, 0);
print_expression(ptr_prs, expr.ptr2);
gen_jump(label2, "ternary operator");
gen_label(label1);
print_expression(ptr_prs, expr.ptr3);
gen_label(label2);
gen_discard2nd();
return;
}
case FPCALL_EXPR: {
struct Type ret_type = expr.details.type;
print_expression(ptr_prs, expr.ptr1);
pass_args(ptr_prs, &expr.args);
gen_pop_to_reg_8byte("r11");
int size = ret_type.type_category == VOID_
? 4 /* for convenience */
: size_of_basic(&ret_type, "return value");
gen_raw_call_partA();
gen_call_reg_and_push_ret_of_nbyte(size, "r11");
return;
}
case FUNCCALL_EXPR: {
const char *ident_str = expr.global_var_name;
struct Type ret_type = expr.details.type;
int arg_stacksize = count_args(&expr.args) * 8;
if (arg_stacksize % 16 != 0) {
unsupported("misaligned stack");
}
gen_raw_call_partA();
pass_args(ptr_prs, &expr.args);
int size = ret_type.type_category == VOID_
? 4 /* for convenience */
: size_of_basic(&ret_type, "return value");
gen_push_ret_of_nbyte(size, ident_str, arg_stacksize);
return;
}
case BUILTIN_FUNCCALL_EXPR: {
const char *ident_str = expr.global_var_name;
for (int counter = expr.args.length - 1; counter >= 0; counter--) {
const struct Expr *ptr_expr_ = expr.args.vector[counter];
print_expression(ptr_prs, ptr_expr_);
}
handle_builtin(ptr_prs, ident_str);
for (int counter = expr.args.length - 1; counter >= 0; counter--) {
gen_discard();
}
gen_push_int(5432); /* a dummy value to be discarded */
return;
}
case STRING_LITERAL: {
const char *str = expr.literal_string;
push_vector(&ptr_prs->string_constant_pool, str);
gen_push_address_of_str(ptr_prs->string_constant_pool.length - 1);
}
}
}
static int count_args(const struct Vector /*<Expr>*/ *ref_args)
{
int ans = 0;
for (int counter = ref_args->length - 1; counter >= 0; counter--) {
ans++;
}
for (int counter = 0; counter < ref_args->length; counter++) {
if (counter >= 6) {
break;
}
const struct Expr *ptr_expr_ = ref_args->vector[counter];
switch (size_of_basic(&ptr_expr_->details.type, "argument")) {
case 1:
case 4:
case 8:
ans--;
break;
default:
unsupported("Unsupported width in function argument");
}
}
return ans;
}
static void pass_args(struct PrinterState *ptr_prs,
const struct Vector /*<Expr>*/ *ref_args)
{
for (int counter = ref_args->length - 1; counter >= 0; counter--) {
const struct Expr *ptr_expr_ = ref_args->vector[counter];
print_expression(ptr_prs, ptr_expr_);
}
for (int counter = 0; counter < ref_args->length; counter++) {
if (counter >= 6) {
break;
}
const struct Expr *ptr_expr_ = ref_args->vector[counter];
switch (size_of_basic(&ptr_expr_->details.type, "argument")) {
case 1:
case 4:
gen_pop_to_reg_4byte(get_reg_name_from_arg_pos_4byte(counter));
break;
case 8:
gen_pop_to_reg_8byte(get_reg_name_from_arg_pos_8byte(counter));
break;
default:
unsupported("Unsupported width in function argument");
}
}
}
void print_expression_or_addr_of_struct_or_union(struct PrinterState *ptr_prs,
const struct Expr *ref_expr,
const char *msg)
{
if (is_struct_or_union(&ref_expr->details.type)) {
print_address_of_lvalue_or_struct_or_union(ptr_prs, ref_expr, msg);
} else {
print_expression(ptr_prs, ref_expr);
}
}