-
Notifications
You must be signed in to change notification settings - Fork 35
/
expressc.c
3088 lines (2726 loc) · 124 KB
/
expressc.c
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
/* ------------------------------------------------------------------------- */
/* "expressc" : The expression code generator */
/* */
/* Part of Inform 6.43 */
/* copyright (c) Graham Nelson 1993 - 2024 */
/* */
/* ------------------------------------------------------------------------- */
#include "header.h"
int vivc_flag; /* TRUE if the last code-generated
expression produced a "value in void
context" error: used to help the syntax
analyser recover from unknown-keyword
errors, since unknown keywords are
treated as yet-to-be-defined constants
and thus as values in void context */
/* These data structures are global, because they're too useful to be
static. */
assembly_operand stack_pointer, temp_var1, temp_var2, temp_var3,
temp_var4, zero_operand, one_operand, two_operand, three_operand,
four_operand, valueless_operand;
static void make_operands(void)
{
if (!glulx_mode) {
INITAOTV(&stack_pointer, VARIABLE_OT, 0);
INITAOTV(&temp_var1, VARIABLE_OT, 255);
INITAOTV(&temp_var2, VARIABLE_OT, 254);
INITAOTV(&temp_var3, VARIABLE_OT, 253);
INITAOTV(&temp_var4, VARIABLE_OT, 252);
INITAOTV(&zero_operand, SHORT_CONSTANT_OT, 0);
INITAOTV(&one_operand, SHORT_CONSTANT_OT, 1);
INITAOTV(&two_operand, SHORT_CONSTANT_OT, 2);
INITAOTV(&three_operand, SHORT_CONSTANT_OT, 3);
INITAOTV(&four_operand, SHORT_CONSTANT_OT, 4);
INITAOTV(&valueless_operand, OMITTED_OT, 0);
}
else {
INITAOTV(&stack_pointer, LOCALVAR_OT, 0);
INITAOTV(&temp_var1, GLOBALVAR_OT, MAX_LOCAL_VARIABLES+0);
INITAOTV(&temp_var2, GLOBALVAR_OT, MAX_LOCAL_VARIABLES+1);
INITAOTV(&temp_var3, GLOBALVAR_OT, MAX_LOCAL_VARIABLES+2);
INITAOTV(&temp_var4, GLOBALVAR_OT, MAX_LOCAL_VARIABLES+3);
INITAOTV(&zero_operand, ZEROCONSTANT_OT, 0);
INITAOTV(&one_operand, BYTECONSTANT_OT, 1);
INITAOTV(&two_operand, BYTECONSTANT_OT, 2);
INITAOTV(&three_operand, BYTECONSTANT_OT, 3);
INITAOTV(&four_operand, BYTECONSTANT_OT, 4);
INITAOTV(&valueless_operand, OMITTED_OT, 0);
}
}
/* ------------------------------------------------------------------------- */
/* The table of conditionals. (Only used in Glulx) */
#define ZERO_CC (500)
#define EQUAL_CC (502)
#define LT_CC (504)
#define GT_CC (506)
#define HAS_CC (508)
#define IN_CC (510)
#define OFCLASS_CC (512)
#define PROVIDES_CC (514)
#define FIRST_CC (500)
#define LAST_CC (515)
typedef struct condclass_s {
int32 posform; /* Opcode for the conditional in its positive form. */
int32 negform; /* Opcode for the conditional in its negated form. */
} condclass;
condclass condclasses[] = {
{ jz_gc, jnz_gc },
{ jeq_gc, jne_gc },
{ jlt_gc, jge_gc },
{ jgt_gc, jle_gc },
{ -1, -1 },
{ -1, -1 },
{ -1, -1 },
{ -1, -1 }
};
/* ------------------------------------------------------------------------- */
/* The table of operators.
The ordering in this table is not significant except that it must match
the #define's in "header.h" */
operator operators[NUM_OPERATORS] =
{
/* ------------------------ */
/* Level 0: , */
/* ------------------------ */
{ 0, SEP_TT, COMMA_SEP, IN_U, L_A, 0, -1, -1, 0, 0, "comma" },
/* ------------------------ */
/* Level 1: = */
/* ------------------------ */
{ 1, SEP_TT, SETEQUALS_SEP, IN_U, R_A, 1, -1, -1, 1, 0,
"assignment operator '='" },
/* ------------------------ */
/* Level 2: ~~ && || */
/* ------------------------ */
{ 2, SEP_TT, LOGAND_SEP, IN_U, L_A, 0, -1, -1, 0, LOGOR_OP,
"logical conjunction '&&'" },
{ 2, SEP_TT, LOGOR_SEP, IN_U, L_A, 0, -1, -1, 0, LOGAND_OP,
"logical disjunction '||'" },
{ 2, SEP_TT, LOGNOT_SEP, PRE_U, R_A, 0, -1, -1, 0, LOGNOT_OP,
"logical negation '~~'" },
/* ------------------------ */
/* Level 3: == ~= */
/* > >= < <= */
/* has hasnt */
/* in notin */
/* provides */
/* ofclass */
/* ------------------------ */
{ 3, -1, -1, -1, 0, 0, 400 + jz_zc, ZERO_CC+0, 0, NONZERO_OP,
"expression used as condition then negated" },
{ 3, -1, -1, -1, 0, 0, 800 + jz_zc, ZERO_CC+1, 0, ZERO_OP,
"expression used as condition" },
{ 3, SEP_TT, CONDEQUALS_SEP, IN_U, 0, 0, 400 + je_zc, EQUAL_CC+0, 0, NOTEQUAL_OP,
"'==' condition" },
{ 3, SEP_TT, NOTEQUAL_SEP, IN_U, 0, 0, 800 + je_zc, EQUAL_CC+1, 0, CONDEQUALS_OP,
"'~=' condition" },
{ 3, SEP_TT, GE_SEP, IN_U, 0, 0, 800 + jl_zc, LT_CC+1, 0, LESS_OP,
"'>=' condition" },
{ 3, SEP_TT, GREATER_SEP, IN_U, 0, 0, 400 + jg_zc, GT_CC+0, 0, LE_OP,
"'>' condition" },
{ 3, SEP_TT, LE_SEP, IN_U, 0, 0, 800 + jg_zc, GT_CC+1, 0, GREATER_OP,
"'<=' condition" },
{ 3, SEP_TT, LESS_SEP, IN_U, 0, 0, 400 + jl_zc, LT_CC+0, 0, GE_OP,
"'<' condition" },
{ 3, CND_TT, HAS_COND, IN_U, 0, 0, 400 + test_attr_zc, HAS_CC+0, 0, HASNT_OP,
"'has' condition" },
{ 3, CND_TT, HASNT_COND, IN_U, 0, 0, 800 + test_attr_zc, HAS_CC+1, 0, HAS_OP,
"'hasnt' condition" },
{ 3, CND_TT, IN_COND, IN_U, 0, 0, 400 + jin_zc, IN_CC+0, 0, NOTIN_OP,
"'in' condition" },
{ 3, CND_TT, NOTIN_COND, IN_U, 0, 0, 800 + jin_zc, IN_CC+1, 0, IN_OP,
"'notin' condition" },
{ 3, CND_TT, OFCLASS_COND, IN_U, 0, 0, 600, OFCLASS_CC+0, 0, NOTOFCLASS_OP,
"'ofclass' condition" },
{ 3, CND_TT, PROVIDES_COND, IN_U, 0, 0, 601, PROVIDES_CC+0, 0, NOTPROVIDES_OP,
"'provides' condition" },
{ 3, -1, -1, -1, 0, 0, 1000, OFCLASS_CC+1, 0, OFCLASS_OP,
"negated 'ofclass' condition" },
{ 3, -1, -1, -1, 0, 0, 1001, PROVIDES_CC+1, 0, PROVIDES_OP,
"negated 'provides' condition" },
/* ------------------------ */
/* Level 4: or */
/* ------------------------ */
{ 4, CND_TT, OR_COND, IN_U, L_A, 0, -1, -1, 0, 0, "'or'" },
/* ------------------------ */
/* Level 5: + binary - */
/* ------------------------ */
{ 5, SEP_TT, PLUS_SEP, IN_U, L_A, 0, add_zc, add_gc, 0, 0, "'+'" },
{ 5, SEP_TT, MINUS_SEP, IN_U, L_A, 0, sub_zc, sub_gc, 0, 0, "'-'" },
/* ------------------------ */
/* Level 6: * / % */
/* & | ~ */
/* ------------------------ */
{ 6, SEP_TT, TIMES_SEP, IN_U, L_A, 0, mul_zc, mul_gc, 0, 0, "'*'" },
{ 6, SEP_TT, DIVIDE_SEP, IN_U, L_A, 0, div_zc, div_gc, 0, 0, "'/'" },
{ 6, SEP_TT, REMAINDER_SEP, IN_U, L_A, 0, mod_zc, mod_gc, 0, 0,
"remainder after division '%'" },
{ 6, SEP_TT, ARTAND_SEP, IN_U, L_A, 0, and_zc, bitand_gc, 0, 0,
"bitwise AND '&'" },
{ 6, SEP_TT, ARTOR_SEP, IN_U, L_A, 0, or_zc, bitor_gc, 0, 0,
"bitwise OR '|'" },
{ 6, SEP_TT, ARTNOT_SEP, PRE_U, R_A, 0, -1, bitnot_gc, 0, 0,
"bitwise NOT '~'" },
/* ------------------------ */
/* Level 7: -> --> */
/* ------------------------ */
{ 7, SEP_TT, ARROW_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"byte array operator '->'" },
{ 7, SEP_TT, DARROW_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"word array operator '-->'" },
/* ------------------------ */
/* Level 8: unary - */
/* ------------------------ */
{ 8, SEP_TT, UNARY_MINUS_SEP, PRE_U, R_A, 0, -1, neg_gc, 0, 0,
"unary minus" },
/* ------------------------ */
/* Level 9: ++ -- */
/* (prefix or postfix) */
/* ------------------------ */
{ 9, SEP_TT, INC_SEP, PRE_U, R_A, 2, -1, -1, 1, 0,
"pre-increment operator '++'" },
{ 9, SEP_TT, POST_INC_SEP, POST_U, R_A, 3, -1, -1, 1, 0,
"post-increment operator '++'" },
{ 9, SEP_TT, DEC_SEP, PRE_U, R_A, 4, -1, -1, 1, 0,
"pre-decrement operator '--'" },
{ 9, SEP_TT, POST_DEC_SEP, POST_U, R_A, 5, -1, -1, 1, 0,
"post-decrement operator '--'" },
/* ------------------------ */
/* Level 10: .& .# */
/* ..& ..# */
/* ------------------------ */
{10, SEP_TT, PROPADD_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"property address operator '.&'" },
{10, SEP_TT, PROPNUM_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"property length operator '.#'" },
{10, SEP_TT, MPROPADD_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"individual property address operator '..&'" },
{10, SEP_TT, MPROPNUM_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"individual property length operator '..#'" },
/* ------------------------ */
/* Level 11: function ( */
/* ------------------------ */
{11, SEP_TT, OPENB_SEP, IN_U, L_A, 0, -1, -1, 1, 0,
"function call" },
/* ------------------------ */
/* Level 12: . .. */
/* ------------------------ */
{12, SEP_TT, MESSAGE_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"individual property selector '..'" },
{12, SEP_TT, PROPERTY_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"property selector '.'" },
/* ------------------------ */
/* Level 13: :: */
/* ------------------------ */
{13, SEP_TT, SUPERCLASS_SEP, IN_U, L_A, 0, -1, -1, 0, 0,
"superclass operator '::'" },
/* ------------------------ */
/* Miscellaneous operators */
/* generated at lvalue */
/* checking time */
/* ------------------------ */
{ 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> = */
"byte array entry assignment" },
{ 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> = */
"word array entry assignment" },
{ 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. = */
"individual property assignment" },
{ 1, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . = */
"common property assignment" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ -> */
"byte array entry preincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ --> */
"word array entry preincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ .. */
"individual property preincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* ++ . */
"common property preincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- -> */
"byte array entry predecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- --> */
"word array entry predecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- .. */
"individual property predecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -- . */
"common property predecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> ++ */
"byte array entry postincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> ++ */
"word array entry postincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. ++ */
"individual property postincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . ++ */
"common property postincrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* -> -- */
"byte array entry postdecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* --> -- */
"word array entry postdecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* .. -- */
"individual property postdecrement" },
{ 9, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* . -- */
"common property postdecrement" },
{11, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* x.y(args) */
"call to common property" },
{11, -1, -1, -1, -1, 0, -1, -1, 1, 0, /* x..y(args) */
"call to individual property" },
/* ------------------------ */
/* And one Glulx-only op */
/* which just pushes its */
/* argument on the stack, */
/* unchanged. */
/* ------------------------ */
{14, -1, -1, -1, -1, 0, -1, -1, 1, 0,
"push on stack" }
};
/* --- Condition annotater ------------------------------------------------- */
static void annotate_for_conditions(int n, int a, int b)
{ int i, opnum = ET[n].operator_number;
ET[n].label_after = -1;
ET[n].to_expression = FALSE;
ET[n].true_label = a;
ET[n].false_label = b;
if (ET[n].down == -1) return;
if ((operators[opnum].precedence == 2)
|| (operators[opnum].precedence == 3))
{ if ((a == -1) && (b == -1))
{ if (opnum == LOGAND_OP)
{ b = next_label++;
ET[n].false_label = b;
ET[n].to_expression = TRUE;
}
else
{ a = next_label++;
ET[n].true_label = a;
ET[n].to_expression = TRUE;
}
}
}
switch(opnum)
{ case LOGAND_OP:
if (b == -1)
{ b = next_label++;
ET[n].false_label = b;
ET[n].label_after = b;
}
annotate_for_conditions(ET[n].down, -1, b);
if (b == ET[n].label_after)
annotate_for_conditions(ET[ET[n].down].right, a, -1);
else annotate_for_conditions(ET[ET[n].down].right, a, b);
return;
case LOGOR_OP:
if (a == -1)
{ a = next_label++;
ET[n].true_label = a;
ET[n].label_after = a;
}
annotate_for_conditions(ET[n].down, a, -1);
if (a == ET[n].label_after)
annotate_for_conditions(ET[ET[n].down].right, -1, b);
else annotate_for_conditions(ET[ET[n].down].right, a, b);
return;
}
i = ET[n].down;
while (i != -1)
{ annotate_for_conditions(i, -1, -1); i = ET[i].right; }
}
/* --- Code generator ------------------------------------------------------ */
static void value_in_void_context_z(assembly_operand AO)
{ char *t;
ASSERT_ZCODE();
switch(AO.type)
{ case LONG_CONSTANT_OT:
case SHORT_CONSTANT_OT:
t = "<constant>";
if (AO.marker == SYMBOL_MV)
t = (symbols[AO.value].name);
break;
case VARIABLE_OT:
t = variable_name(AO.value);
break;
default:
compiler_error("Unable to print value in void context");
t = "<expression>";
break;
}
vivc_flag = TRUE;
if (strcmp(t, "print_paddr") == 0)
obsolete_warning("ignoring 'print_paddr': use 'print (string)' instead");
else
if (strcmp(t, "print_addr") == 0)
obsolete_warning("ignoring 'print_addr': use 'print (address)' instead");
else
if (strcmp(t, "print_char") == 0)
obsolete_warning("ignoring 'print_char': use 'print (char)' instead");
else
ebf_error("expression with side-effects", t);
}
static void write_result_z(assembly_operand to, assembly_operand from)
{ if (to.value == from.value) return;
if (to.value == 0) assemblez_1(push_zc, from);
else assemblez_store(to, from);
}
static void pop_zm_stack(void)
{ assembly_operand st;
if (version_number < 5) assemblez_0(pop_zc);
else
{ INITAOTV(&st, VARIABLE_OT, 0);
assemblez_1_branch(jz_zc, st, -2, TRUE);
}
}
static void access_memory_z(int oc, assembly_operand AO1, assembly_operand AO2,
assembly_operand AO3)
{ int vr = 0;
assembly_operand zero_ao, max_ao, size_ao, en_ao, type_ao, an_ao,
index_ao;
int x = 0, y = 0, byte_flag = FALSE, read_flag = FALSE;
INITAO(&zero_ao);
INITAO(&size_ao);
INITAO(&type_ao);
if (AO1.marker == ARRAY_MV || AO1.marker == STATIC_ARRAY_MV)
{
if ((oc == loadb_zc) || (oc == storeb_zc)) byte_flag=TRUE;
else byte_flag = FALSE;
if ((oc == loadb_zc) || (oc == loadw_zc)) read_flag=TRUE;
else read_flag = FALSE;
zero_ao.type = SHORT_CONSTANT_OT;
zero_ao.value = 0;
size_ao = zero_ao; size_ao.value = -1;
for (x=0; x<no_arrays; x++)
{ if (((AO1.marker == ARRAY_MV) == (!arrays[x].loc))
&& (AO1.value == symbols[arrays[x].symbol].value))
{ size_ao.value = arrays[x].size; y=x;
}
}
if (arrays[y].loc && !read_flag) {
error("Cannot write to a static array");
}
if (size_ao.value==-1) {
/* This case was originally meant for module linking.
It should no longer be possible. */
compiler_error("Array size cannot be negative");
}
else {
type_ao = zero_ao; type_ao.value = arrays[y].type;
if ((!is_systemfile()))
{ if (byte_flag)
{
if ((arrays[y].type == WORD_ARRAY)
|| (arrays[y].type == TABLE_ARRAY))
warning("Using '->' to access a --> or table array");
}
else
{
if ((arrays[y].type == BYTE_ARRAY)
|| (arrays[y].type == STRING_ARRAY))
warning("Using '-->' to access a -> or string array");
}
}
}
}
if ((!runtime_error_checking_switch) || (veneer_mode))
{ if ((oc == loadb_zc) || (oc == loadw_zc))
assemblez_2_to(oc, AO1, AO2, AO3);
else
assemblez_3(oc, AO1, AO2, AO3);
return;
}
/* If we recognise AO1 as arising textually from a declared
array, we can check bounds explicitly. */
if ((AO1.marker == ARRAY_MV || AO1.marker == STATIC_ARRAY_MV))
{
int passed_label = next_label++, failed_label = next_label++,
final_label = next_label++;
/* Calculate the largest permitted array entry + 1
Here "size_ao.value" = largest permitted entry of its own kind */
max_ao = size_ao;
if (byte_flag
&& ((arrays[y].type == WORD_ARRAY)
|| (arrays[y].type == TABLE_ARRAY)))
{ max_ao.value = size_ao.value*2 + 1;
type_ao.value += 8;
}
if ((!byte_flag)
&& ((arrays[y].type == BYTE_ARRAY)
|| (arrays[y].type == STRING_ARRAY)
|| (arrays[y].type == BUFFER_ARRAY)))
{ if ((size_ao.value % 2) == 0)
max_ao.value = size_ao.value/2 - 1;
else max_ao.value = (size_ao.value-1)/2;
type_ao.value += 16;
}
max_ao.value++;
if (size_ao.value >= 256) size_ao.type = LONG_CONSTANT_OT;
if (max_ao.value >= 256) max_ao.type = LONG_CONSTANT_OT;
/* Can't write to the size entry in a string or table */
if (((arrays[y].type == STRING_ARRAY)
|| (arrays[y].type == TABLE_ARRAY))
&& (!read_flag))
{ if ((arrays[y].type == TABLE_ARRAY) && byte_flag)
zero_ao.value = 2;
else zero_ao.value = 1;
}
en_ao = zero_ao; en_ao.value = ABOUNDS_RTE;
switch(oc) { case loadb_zc: en_ao.value = ABOUNDS_RTE; break;
case loadw_zc: en_ao.value = ABOUNDS_RTE+1; break;
case storeb_zc: en_ao.value = ABOUNDS_RTE+2; break;
case storew_zc: en_ao.value = ABOUNDS_RTE+3; break; }
index_ao = AO2;
if ((AO2.type == VARIABLE_OT)&&(AO2.value == 0))
{ assemblez_store(temp_var2, AO2);
assemblez_store(AO2, temp_var2);
index_ao = temp_var2;
}
assemblez_2_branch(jl_zc, index_ao, zero_ao, failed_label, TRUE);
assemblez_2_branch(jl_zc, index_ao, max_ao, passed_label, TRUE);
assemble_label_no(failed_label);
an_ao = zero_ao; an_ao.value = y;
assemblez_6(call_vn2_zc, veneer_routine(RT__Err_VR), en_ao,
index_ao, size_ao, type_ao, an_ao);
/* We have to clear any of AO1, AO2, AO3 off the stack if
present, so that we can achieve the same effect on the stack
that executing the opcode would have had */
if ((AO1.type == VARIABLE_OT) && (AO1.value == 0)) pop_zm_stack();
if ((AO2.type == VARIABLE_OT) && (AO2.value == 0)) pop_zm_stack();
if ((AO3.type == VARIABLE_OT) && (AO3.value == 0))
{ if ((oc == loadb_zc) || (oc == loadw_zc))
{ assemblez_store(AO3, zero_ao);
}
else pop_zm_stack();
}
assemblez_jump(final_label);
assemble_label_no(passed_label);
if ((oc == loadb_zc) || (oc == loadw_zc))
assemblez_2_to(oc, AO1, AO2, AO3);
else
assemblez_3(oc, AO1, AO2, AO3);
assemble_label_no(final_label);
return;
}
/* Otherwise, compile a call to the veneer which verifies that
the proposed read/write is within dynamic Z-machine memory. */
switch(oc) { case loadb_zc: vr = RT__ChLDB_VR; break;
case loadw_zc: vr = RT__ChLDW_VR; break;
case storeb_zc: vr = RT__ChSTB_VR; break;
case storew_zc: vr = RT__ChSTW_VR; break;
default: compiler_error("unknown array opcode");
}
if ((oc == loadb_zc) || (oc == loadw_zc))
assemblez_3_to(call_vs_zc, veneer_routine(vr), AO1, AO2, AO3);
else
assemblez_4(call_vn_zc, veneer_routine(vr), AO1, AO2, AO3);
}
static assembly_operand check_nonzero_at_runtime_z(assembly_operand AO1,
int error_label, int rte_number)
{ assembly_operand AO2, AO3;
int check_sp = FALSE, passed_label, failed_label, last_label;
if (veneer_mode) return AO1;
/* Assemble to code to check that the operand AO1 is ofclass Object:
if it is, execution should continue and the stack should be
unchanged. Otherwise, call the veneer's run-time-error routine
with the given error number, and then: if the label isn't -1,
switch execution to this label, with the value popped from
the stack if it was on the stack in the first place;
if the label is -1, either replace the top of the stack with
the constant 2, or return the operand (short constant) 2.
The point of 2 is that object 2 is the class-object Object
and therefore has no parent, child or sibling, so that the
built-in tree functions will safely return 0 on this object. */
/* Sometimes we can already see that the object number is valid. */
if (((AO1.type == LONG_CONSTANT_OT) || (AO1.type == SHORT_CONSTANT_OT))
&& (AO1.marker == 0) && (AO1.value >= 1) && (AO1.value < no_objects))
return AO1;
passed_label = next_label++;
failed_label = next_label++;
INITAOTV(&AO2, LONG_CONSTANT_OT, actual_largest_object_SC);
AO2.marker = INCON_MV;
INITAOTV(&AO3, SHORT_CONSTANT_OT, 5);
if ((rte_number == IN_RTE) || (rte_number == HAS_RTE)
|| (rte_number == PROPERTY_RTE) || (rte_number == PROP_NUM_RTE)
|| (rte_number == PROP_ADD_RTE))
{ /* Allow classes */
AO3.value = 1;
if ((AO1.type == VARIABLE_OT) && (AO1.value == 0))
{ /* That is, if AO1 is the stack pointer */
check_sp = TRUE;
assemblez_store(temp_var2, AO1);
assemblez_store(AO1, temp_var2);
assemblez_2_branch(jg_zc, AO3, temp_var2, failed_label, TRUE);
assemblez_2_branch(jg_zc, temp_var2, AO2, passed_label, FALSE);
}
else
{ assemblez_2_branch(jg_zc, AO3, AO1, failed_label, TRUE);
assemblez_2_branch(jg_zc, AO1, AO2, passed_label, FALSE);
}
}
else
{ if ((AO1.type == VARIABLE_OT) && (AO1.value == 0))
{ /* That is, if AO1 is the stack pointer */
check_sp = TRUE;
assemblez_store(temp_var2, AO1);
assemblez_store(AO1, temp_var2);
assemblez_2_branch(jg_zc, AO3, temp_var2, failed_label, TRUE);
assemblez_2_branch(jg_zc, temp_var2, AO2, failed_label, TRUE);
AO3.value = 1;
assemblez_2_branch(jin_zc, temp_var2, AO3, passed_label, FALSE);
}
else
{ assemblez_2_branch(jg_zc, AO3, AO1, failed_label, TRUE);
assemblez_2_branch(jg_zc, AO1, AO2, failed_label, TRUE);
AO3.value = 1;
assemblez_2_branch(jin_zc, AO1, AO3, passed_label, FALSE);
}
}
assemble_label_no(failed_label);
INITAOTV(&AO2, SHORT_CONSTANT_OT, rte_number);
if (version_number >= 5)
assemblez_3(call_vn_zc, veneer_routine(RT__Err_VR), AO2, AO1);
else
assemblez_3_to(call_zc, veneer_routine(RT__Err_VR), AO2, AO1, temp_var2);
if (error_label != -1)
{ /* Jump to the error label */
if (error_label == -3) assemblez_0(rfalse_zc);
else if (error_label == -4) assemblez_0(rtrue_zc);
else assemblez_jump(error_label);
}
else
{ if (check_sp)
{ /* Push the short constant 2 */
INITAOTV(&AO2, SHORT_CONSTANT_OT, 2);
assemblez_store(AO1, AO2);
}
else
{ /* Store either short constant 2 or the operand's value in
the temporary variable */
INITAOTV(&AO2, SHORT_CONSTANT_OT, 2);
AO3 = temp_var2; assemblez_store(AO3, AO2);
last_label = next_label++;
assemblez_jump(last_label);
assemble_label_no(passed_label);
assemblez_store(AO3, AO1);
assemble_label_no(last_label);
return AO3;
}
}
assemble_label_no(passed_label);
return AO1;
}
static void compile_conditional_z(int oc,
assembly_operand AO1, assembly_operand AO2, int label, int flag)
{ assembly_operand AO3; int the_zc, error_label = label,
va_flag = FALSE, va_label = 0;
ASSERT_ZCODE();
switch (oc) {
case test_attr_zc:
check_warn_symbol_type(&AO1, OBJECT_T, 0, "\"has/hasnt\" expression");
check_warn_symbol_type(&AO2, ATTRIBUTE_T, 0, "\"has/hasnt\" expression");
break;
case jin_zc:
check_warn_symbol_type(&AO1, OBJECT_T, 0, "\"in/notin\" expression");
check_warn_symbol_type(&AO2, OBJECT_T, CLASS_T, "\"in/notin\" expression");
break;
case 200:
/* first argument can be anything */
check_warn_symbol_type(&AO2, CLASS_T, 0, "\"ofclass\" expression");
break;
case 201:
/* first argument can be anything */
check_warn_symbol_type(&AO2, PROPERTY_T, INDIVIDUAL_PROPERTY_T, "\"provides\" expression");
break;
}
if (oc<200)
{ if ((runtime_error_checking_switch) && (oc == jin_zc))
{ if (flag) error_label = next_label++;
AO1 = check_nonzero_at_runtime(AO1, error_label, IN_RTE);
}
if ((runtime_error_checking_switch) && (oc == test_attr_zc))
{ if (flag) error_label = next_label++;
AO1 = check_nonzero_at_runtime(AO1, error_label, HAS_RTE);
switch(AO2.type)
{ case SHORT_CONSTANT_OT:
case LONG_CONSTANT_OT:
if (AO2.marker == 0)
{ if ((AO2.value < 0) || (AO2.value > 47))
error("'has'/'hasnt' applied to illegal attribute number");
break;
}
/* Fall through */
case VARIABLE_OT:
{ int pa_label = next_label++, fa_label = next_label++;
assembly_operand en_ao, zero_ao, max_ao;
assemblez_store(temp_var1, AO1);
if ((AO1.type == VARIABLE_OT)&&(AO1.value == 0))
assemblez_store(AO1, temp_var1);
assemblez_store(temp_var2, AO2);
if ((AO2.type == VARIABLE_OT)&&(AO2.value == 0))
assemblez_store(AO2, temp_var2);
INITAOT(&zero_ao, SHORT_CONSTANT_OT);
zero_ao.value = 0;
max_ao = zero_ao; max_ao.value = 48;
assemblez_2_branch(jl_zc,temp_var2,zero_ao,fa_label,TRUE);
assemblez_2_branch(jl_zc,temp_var2,max_ao,pa_label,TRUE);
assemble_label_no(fa_label);
en_ao = zero_ao; en_ao.value = 19;
assemblez_4(call_vn_zc, veneer_routine(RT__Err_VR),
en_ao, temp_var1, temp_var2);
va_flag = TRUE; va_label = next_label++;
assemblez_jump(va_label);
assemble_label_no(pa_label);
}
}
}
assemblez_2_branch(oc, AO1, AO2, label, flag);
if (error_label != label) assemble_label_no(error_label);
if (va_flag) assemble_label_no(va_label);
return;
}
INITAOTV(&AO3, VARIABLE_OT, 0);
the_zc = (version_number == 3)?call_zc:call_vs_zc;
if (oc == 201)
assemblez_3_to(the_zc, veneer_routine(OP__Pr_VR), AO1, AO2, AO3);
else
assemblez_3_to(the_zc, veneer_routine(OC__Cl_VR), AO1, AO2, AO3);
assemblez_1_branch(jz_zc, AO3, label, !flag);
}
static void value_in_void_context_g(assembly_operand AO)
{ char *t;
ASSERT_GLULX();
switch(AO.type)
{ case CONSTANT_OT:
case HALFCONSTANT_OT:
case BYTECONSTANT_OT:
case ZEROCONSTANT_OT:
t = "<constant>";
if (AO.marker == SYMBOL_MV)
t = (symbols[AO.value].name);
break;
case GLOBALVAR_OT:
case LOCALVAR_OT:
t = variable_name(AO.value);
break;
default:
compiler_error("Unable to print value in void context");
t = "<expression>";
break;
}
vivc_flag = TRUE;
ebf_error("expression with side-effects", t);
}
static void write_result_g(assembly_operand to, assembly_operand from)
{ if (to.value == from.value && to.type == from.type) return;
assembleg_store(to, from);
}
static void access_memory_g(int oc, assembly_operand AO1, assembly_operand AO2,
assembly_operand AO3)
{ int vr = 0;
int data_len, read_flag;
assembly_operand zero_ao, max_ao, size_ao, en_ao, type_ao, an_ao,
index_ao, five_ao;
int passed_label, failed_label, final_label, x = 0, y = 0;
if ((oc == aloadb_gc) || (oc == astoreb_gc)) data_len = 1;
else if ((oc == aloads_gc) || (oc == astores_gc)) data_len = 2;
else data_len = 4;
if ((oc == aloadb_gc) || (oc == aloads_gc) || (oc == aload_gc))
read_flag = TRUE;
else
read_flag = FALSE;
INITAO(&zero_ao);
INITAO(&size_ao);
INITAO(&type_ao);
if (AO1.marker == ARRAY_MV || AO1.marker == STATIC_ARRAY_MV)
{
size_ao = zero_ao; size_ao.value = -1;
for (x=0; x<no_arrays; x++)
{ if (((AO1.marker == ARRAY_MV) == (!arrays[x].loc))
&& (AO1.value == symbols[arrays[x].symbol].value))
{ size_ao.value = arrays[x].size; y=x;
}
}
if (size_ao.value==-1) compiler_error("Array size can't be found");
type_ao = zero_ao; type_ao.value = arrays[y].type;
if (arrays[y].loc && !read_flag) {
error("Cannot write to a static array");
}
if ((!is_systemfile()))
{ if (data_len == 1)
{
if ((arrays[y].type == WORD_ARRAY)
|| (arrays[y].type == TABLE_ARRAY))
warning("Using '->' to access a --> or table array");
}
else
{
if ((arrays[y].type == BYTE_ARRAY)
|| (arrays[y].type == STRING_ARRAY))
warning("Using '-->' to access a -> or string array");
}
}
}
if ((!runtime_error_checking_switch) || (veneer_mode))
{
assembleg_3(oc, AO1, AO2, AO3);
return;
}
/* If we recognise AO1 as arising textually from a declared
array, we can check bounds explicitly. */
if (AO1.marker == ARRAY_MV || AO1.marker == STATIC_ARRAY_MV)
{
/* Calculate the largest permitted array entry + 1
Here "size_ao.value" = largest permitted entry of its own kind */
max_ao = size_ao;
if (data_len == 1
&& ((arrays[y].type == WORD_ARRAY)
|| (arrays[y].type == TABLE_ARRAY)))
{ max_ao.value = size_ao.value*4 + 3;
type_ao.value += 8;
}
if (data_len == 4
&& ((arrays[y].type == BYTE_ARRAY)
|| (arrays[y].type == STRING_ARRAY)
|| (arrays[y].type == BUFFER_ARRAY)))
{ max_ao.value = (size_ao.value-3)/4;
type_ao.value += 16;
}
max_ao.value++;
/* Can't write to the size entry in a string or table */
if (((arrays[y].type == STRING_ARRAY)
|| (arrays[y].type == TABLE_ARRAY))
&& (!read_flag))
{ if ((arrays[y].type == TABLE_ARRAY) && data_len == 1)
zero_ao.value = 4;
else zero_ao.value = 1;
}
en_ao = zero_ao; en_ao.value = ABOUNDS_RTE;
switch(oc) { case aloadb_gc: en_ao.value = ABOUNDS_RTE; break;
case aload_gc: en_ao.value = ABOUNDS_RTE+1; break;
case astoreb_gc: en_ao.value = ABOUNDS_RTE+2; break;
case astore_gc: en_ao.value = ABOUNDS_RTE+3; break; }
set_constant_ot(&zero_ao);
set_constant_ot(&size_ao);
set_constant_ot(&max_ao);
set_constant_ot(&type_ao);
set_constant_ot(&en_ao);
/* If we recognize A02 as a constant, we can do the test right
now. */
if (is_constant_ot(AO2.type) && AO2.marker == 0) {
if (AO2.value < zero_ao.value || AO2.value >= max_ao.value) {
error("Array reference is out-of-bounds");
}
assembleg_3(oc, AO1, AO2, AO3);
return;
}
passed_label = next_label++;
failed_label = next_label++;
final_label = next_label++;
index_ao = AO2;
if ((AO2.type == LOCALVAR_OT)&&(AO2.value == 0))
{ assembleg_store(temp_var2, AO2); /* ### could peek */
assembleg_store(AO2, temp_var2);
index_ao = temp_var2;
}
assembleg_2_branch(jlt_gc, index_ao, zero_ao, failed_label);
assembleg_2_branch(jlt_gc, index_ao, max_ao, passed_label);
assemble_label_no(failed_label);
an_ao = zero_ao; an_ao.value = y;
set_constant_ot(&an_ao);
five_ao = zero_ao; five_ao.value = 5;
set_constant_ot(&five_ao);
/* Call the error veneer routine. */
assembleg_store(stack_pointer, an_ao);
assembleg_store(stack_pointer, type_ao);
assembleg_store(stack_pointer, size_ao);
assembleg_store(stack_pointer, index_ao);
assembleg_store(stack_pointer, en_ao);
assembleg_3(call_gc, veneer_routine(RT__Err_VR),
five_ao, zero_operand);
/* We have to clear any of AO1, AO2, AO3 off the stack if
present, so that we can achieve the same effect on the stack
that executing the opcode would have had */
if ((AO1.type == LOCALVAR_OT) && (AO1.value == 0))
assembleg_2(copy_gc, stack_pointer, zero_operand);
if ((AO2.type == LOCALVAR_OT) && (AO2.value == 0))
assembleg_2(copy_gc, stack_pointer, zero_operand);
if ((AO3.type == LOCALVAR_OT) && (AO3.value == 0))
{ if ((oc == aloadb_gc) || (oc == aload_gc))
{ assembleg_store(AO3, zero_ao);
}
else assembleg_2(copy_gc, stack_pointer, zero_operand);
}
assembleg_jump(final_label);
assemble_label_no(passed_label);
assembleg_3(oc, AO1, AO2, AO3);
assemble_label_no(final_label);
return;
}
/* Otherwise, compile a call to the veneer which verifies that
the proposed read/write is within dynamic Z-machine memory. */
switch(oc) {
case aloadb_gc: vr = RT__ChLDB_VR; break;
case aload_gc: vr = RT__ChLDW_VR; break;
case astoreb_gc: vr = RT__ChSTB_VR; break;
case astore_gc: vr = RT__ChSTW_VR; break;
default: compiler_error("unknown array opcode");
}
if ((oc == aloadb_gc) || (oc == aload_gc))
assembleg_call_2(veneer_routine(vr), AO1, AO2, AO3);
else
assembleg_call_3(veneer_routine(vr), AO1, AO2, AO3, zero_operand);
}