-
Notifications
You must be signed in to change notification settings - Fork 33
/
code_gen.c
1472 lines (1333 loc) · 37.8 KB
/
code_gen.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
/******************************************************************************
Copyright (c) 1994, 1995, 1996 Xerox Corporation. All rights reserved.
Portions of this code were written by Stephen White, aka ghond.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
Pavel Curtis
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
Pavel@Xerox.Com
*****************************************************************************/
#include <limits.h>
#include "ast.h"
#include "exceptions.h"
#include "opcode.h"
#include "program.h"
#include "storage.h"
#include "structures.h"
#include "str_intern.h"
#include "utils.h"
#include "version.h"
#include "my-stdlib.h"
/*** The reader will likely find it useful to consult the file
*** `MOOCodeSequences.txt' in this directory while reading the code in this
*** file.
***/
enum fixup_kind {
FIXUP_LITERAL, FIXUP_FORK, FIXUP_LABEL, FIXUP_VAR_REF, FIXUP_STACK
};
struct fixup {
enum fixup_kind kind;
unsigned pc;
unsigned value;
unsigned prev_literals, prev_forks, prev_var_refs, prev_labels,
prev_stacks;
int next; /* chain for compiling IF/ELSEIF arms */
};
typedef struct fixup Fixup;
struct gstate {
unsigned total_var_refs; /* For duplicating an old bug... */
unsigned num_literals, max_literals;
Var *literals;
unsigned num_fork_vectors, max_fork_vectors;
Bytecodes *fork_vectors;
};
typedef struct gstate GState;
struct loop {
int id;
Fixup top_label;
unsigned top_stack;
int bottom_label;
unsigned bottom_stack;
};
typedef struct loop Loop;
struct state {
unsigned max_literal, max_fork, max_var_ref;
/* For telling how big the refs must be */
unsigned num_literals, num_forks, num_var_refs, num_labels, num_stacks;
/* For computing the final vector length */
unsigned num_fixups, max_fixups;
Fixup *fixups;
unsigned num_bytes, max_bytes;
Byte *bytes;
#ifdef BYTECODE_REDUCE_REF
Byte *pushmap;
Byte *trymap;
unsigned try_depth;
#endif /* BYTECODE_REDUCE_REF */
unsigned cur_stack, max_stack;
unsigned saved_stack;
unsigned num_loops, max_loops;
Loop *loops;
GState *gstate;
};
typedef struct state State;
#ifdef BYTECODE_REDUCE_REF
#define INCR_TRY_DEPTH(SSS) (++(SSS)->try_depth)
#define DECR_TRY_DEPTH(SSS) (--(SSS)->try_depth)
#define NON_VR_VAR_MASK ~((1 << SLOT_ARGSTR) | \
(1 << SLOT_DOBJ) | \
(1 << SLOT_DOBJSTR) | \
(1 << SLOT_PREPSTR) | \
(1 << SLOT_IOBJ) | \
(1 << SLOT_IOBJSTR) | \
(1 << SLOT_PLAYER))
#else /* no BYTECODE_REDUCE_REF */
#define INCR_TRY_DEPTH(SSS)
#define DECR_TRY_DEPTH(SSS)
#endif /* BYTECODE_REDUCE_REF */
static void
init_gstate(GState * gstate)
{
gstate->total_var_refs = 0;
gstate->num_literals = gstate->num_fork_vectors = 0;
gstate->max_literals = gstate->max_fork_vectors = 0;
gstate->fork_vectors = 0;
gstate->literals = 0;
}
static void
free_gstate(GState gstate)
{
if (gstate.literals)
myfree(gstate.literals, M_CODE_GEN);
if (gstate.fork_vectors)
myfree(gstate.fork_vectors, M_CODE_GEN);
}
static void
init_state(State * state, GState * gstate)
{
state->num_literals = state->num_forks = state->num_labels = 0;
state->num_var_refs = state->num_stacks = 0;
state->max_literal = state->max_fork = state->max_var_ref = 0;
state->num_fixups = 0;
state->max_fixups = 10;
state->fixups = mymalloc(sizeof(Fixup) * state->max_fixups, M_CODE_GEN);
state->num_bytes = 0;
state->max_bytes = 50;
state->bytes = mymalloc(sizeof(Byte) * state->max_bytes, M_BYTECODES);
#ifdef BYTECODE_REDUCE_REF
state->pushmap = mymalloc(sizeof(Byte) * state->max_bytes, M_BYTECODES);
state->trymap = mymalloc(sizeof(Byte) * state->max_bytes, M_BYTECODES);
state->try_depth = 0;
#endif /* BYTECODE_REDUCE_REF */
state->cur_stack = state->max_stack = 0;
state->saved_stack = UINT_MAX;
state->num_loops = 0;
state->max_loops = 5;
state->loops = mymalloc(sizeof(Loop) * state->max_loops, M_CODE_GEN);
state->gstate = gstate;
}
static void
free_state(State state)
{
myfree(state.fixups, M_CODE_GEN);
myfree(state.bytes, M_BYTECODES);
#ifdef BYTECODE_REDUCE_REF
myfree(state.pushmap, M_BYTECODES);
myfree(state.trymap, M_BYTECODES);
#endif /* BYTECODE_REDUCE_REF */
myfree(state.loops, M_CODE_GEN);
}
static void
emit_byte(Byte b, State * state)
{
if (state->num_bytes == state->max_bytes) {
unsigned new_max = 2 * state->max_bytes;
state->bytes = myrealloc(state->bytes, sizeof(Byte) * new_max,
M_BYTECODES);
#ifdef BYTECODE_REDUCE_REF
state->pushmap = myrealloc(state->pushmap, sizeof(Byte) * new_max,
M_BYTECODES);
state->trymap = myrealloc(state->trymap, sizeof(Byte) * new_max,
M_BYTECODES);
#endif /* BYTECODE_REDUCE_REF */
state->max_bytes = new_max;
}
#ifdef BYTECODE_REDUCE_REF
state->pushmap[state->num_bytes] = 0;
state->trymap[state->num_bytes] = state->try_depth;
#endif /* BYTECODE_REDUCE_REF */
state->bytes[state->num_bytes++] = b;
}
static void
emit_extended_byte(Byte b, State * state)
{
emit_byte(OP_EXTENDED, state);
emit_byte(b, state);
}
static int
add_known_fixup(Fixup f, State * state)
{
unsigned int i;
if (state->num_fixups == state->max_fixups) {
unsigned new_max = 2 * state->max_fixups;
Fixup *new_fixups = mymalloc(sizeof(Fixup) * new_max,
M_CODE_GEN);
for (i = 0; i < state->num_fixups; i++)
new_fixups[i] = state->fixups[i];
myfree(state->fixups, M_CODE_GEN);
state->fixups = new_fixups;
state->max_fixups = new_max;
}
f.pc = state->num_bytes;
state->fixups[i = state->num_fixups++] = f;
emit_byte(0, state); /* a placeholder for the eventual value */
return i;
}
static int
add_linked_fixup(enum fixup_kind kind, unsigned value, int next, State * state)
{
Fixup f;
f.kind = kind;
f.value = value;
f.prev_literals = state->num_literals;
f.prev_forks = state->num_forks;
f.prev_var_refs = state->num_var_refs;
f.prev_labels = state->num_labels;
f.prev_stacks = state->num_stacks;
f.next = next;
return add_known_fixup(f, state);
}
static int
add_fixup(enum fixup_kind kind, unsigned value, State * state)
{
return add_linked_fixup(kind, value, -1, state);
}
static void
add_literal(Var v, State * state)
{
GState *gstate = state->gstate;
Var *literals = gstate->literals;
unsigned i;
for (i = 0; i < gstate->num_literals; i++)
if (v.type == literals[i].type /* no int/float coercion here */
&& equality(v, literals[i], 1))
break;
if (i == gstate->num_literals) {
/* New literal to intern */
if (gstate->num_literals == gstate->max_literals) {
unsigned new_max = gstate->max_literals == 0
? 5 : 2 * gstate->max_literals;
Var *new_literals = mymalloc(sizeof(Var) * new_max,
M_CODE_GEN);
if (gstate->literals) {
for (i = 0; i < gstate->num_literals; i++)
new_literals[i] = literals[i];
myfree(literals, M_CODE_GEN);
}
gstate->literals = new_literals;
gstate->max_literals = new_max;
}
if (v.type == TYPE_STR) {
/* intern string if we can */
Var nv;
nv.type = TYPE_STR;
nv.v.str = str_intern(v.v.str);
gstate->literals[i = gstate->num_literals++] = nv;
} else {
gstate->literals[i = gstate->num_literals++] = var_ref(v);
}
}
add_fixup(FIXUP_LITERAL, i, state);
state->num_literals++;
if (i > state->max_literal)
state->max_literal = i;
}
static void
add_fork(Bytecodes b, State * state)
{
unsigned i;
GState *gstate = state->gstate;
if (gstate->num_fork_vectors == gstate->max_fork_vectors) {
unsigned new_max = gstate->max_fork_vectors == 0
? 1 : 2 * gstate->max_fork_vectors;
Bytecodes *new_fv = mymalloc(sizeof(Bytecodes) * new_max,
M_CODE_GEN);
if (gstate->fork_vectors) {
for (i = 0; i < gstate->num_fork_vectors; i++)
new_fv[i] = gstate->fork_vectors[i];
myfree(gstate->fork_vectors, M_CODE_GEN);
}
gstate->fork_vectors = new_fv;
gstate->max_fork_vectors = new_max;
}
gstate->fork_vectors[i = gstate->num_fork_vectors++] = b;
add_fixup(FIXUP_FORK, i, state);
state->num_forks++;
if (i > state->max_fork)
state->max_fork = i;
}
static void
add_var_ref(unsigned slot, State * state)
{
add_fixup(FIXUP_VAR_REF, slot, state);
state->num_var_refs++;
if (slot > state->max_var_ref)
state->max_var_ref = slot;
state->gstate->total_var_refs++;
}
static int
add_linked_label(int next, State * state)
{
int label = add_linked_fixup(FIXUP_LABEL, 0, next, state);
state->num_labels++;
return label;
}
static int
add_label(State * state)
{
return add_linked_label(-1, state);
}
static void
add_pseudo_label(unsigned value, State * state)
{
Fixup f;
f.kind = FIXUP_LABEL;
f.value = value;
f.prev_literals = f.prev_forks = 0;
f.prev_var_refs = f.prev_labels = 0;
f.prev_stacks = 0;
f.next = -1;
add_known_fixup(f, state);
state->num_labels++;
}
static int
add_known_label(Fixup f, State * state)
{
int label = add_known_fixup(f, state);
state->num_labels++;
return label;
}
static Fixup
capture_label(State * state)
{
Fixup f;
f.kind = FIXUP_LABEL;
f.value = state->num_bytes;
f.prev_literals = state->num_literals;
f.prev_forks = state->num_forks;
f.prev_var_refs = state->num_var_refs;
f.prev_labels = state->num_labels;
f.prev_stacks = state->num_stacks;
f.next = -1;
return f;
}
static void
define_label(int label, State * state)
{
unsigned value = state->num_bytes;
while (label != -1) {
Fixup *fixup = &(state->fixups[label]);
fixup->value = value;
fixup->prev_literals = state->num_literals;
fixup->prev_forks = state->num_forks;
fixup->prev_var_refs = state->num_var_refs;
fixup->prev_labels = state->num_labels;
fixup->prev_stacks = state->num_stacks;
label = fixup->next;
}
}
static void
add_stack_ref(unsigned index, State * state)
{
add_fixup(FIXUP_STACK, index, state);
}
static void
push_stack(unsigned n, State * state)
{
state->cur_stack += n;
if (state->cur_stack > state->max_stack)
state->max_stack = state->cur_stack;
}
static void
pop_stack(unsigned n, State * state)
{
state->cur_stack -= n;
}
static unsigned
save_stack_top(State * state)
{
unsigned old = state->saved_stack;
state->saved_stack = state->cur_stack - 1;
return old;
}
static unsigned
saved_stack_top(State * state)
{
return state->saved_stack;
}
static void
restore_stack_top(unsigned old, State * state)
{
state->saved_stack = old;
}
static void
enter_loop(int id, Fixup top_label, unsigned top_stack,
int bottom_label, unsigned bottom_stack, State * state)
{
unsigned int i;
Loop *loop;
if (state->num_loops == state->max_loops) {
unsigned new_max = 2 * state->max_loops;
Loop *new_loops = mymalloc(sizeof(Loop) * new_max,
M_CODE_GEN);
for (i = 0; i < state->num_loops; i++)
new_loops[i] = state->loops[i];
myfree(state->loops, M_CODE_GEN);
state->loops = new_loops;
state->max_loops = new_max;
}
loop = &(state->loops[state->num_loops++]);
loop->id = id;
loop->top_label = top_label;
loop->top_stack = top_stack;
loop->bottom_label = bottom_label;
loop->bottom_stack = bottom_stack;
}
static int
exit_loop(State * state)
{
return state->loops[--state->num_loops].bottom_label;
}
static void
emit_call_verb_op(Opcode op, State * state)
{
emit_byte(op, state);
#ifdef BYTECODE_REDUCE_REF
state->pushmap[state->num_bytes - 1] = OP_CALL_VERB;
#endif /* BYTECODE_REDUCE_REF */
}
static void
emit_ending_op(Opcode op, State * state)
{
emit_byte(op, state);
#ifdef BYTECODE_REDUCE_REF
state->pushmap[state->num_bytes - 1] = OP_DONE;
#endif /* BYTECODE_REDUCE_REF */
}
static void
emit_var_op(Opcode op, unsigned slot, State * state)
{
if (slot >= NUM_READY_VARS) {
emit_byte(op + NUM_READY_VARS, state);
add_var_ref(slot, state);
} else {
emit_byte(op + slot, state);
#ifdef BYTECODE_REDUCE_REF
state->pushmap[state->num_bytes - 1] = op;
#endif /* BYTECODE_REDUCE_REF */
}
}
static void generate_expr(Expr *, State *);
static void
generate_arg_list(Arg_List * args, State * state)
{
if (!args) {
emit_byte(OP_MAKE_EMPTY_LIST, state);
push_stack(1, state);
} else {
Opcode normal_op = OP_MAKE_SINGLETON_LIST, splice_op = OP_CHECK_LIST_FOR_SPLICE;
unsigned pop = 0;
for (; args; args = args->next) {
generate_expr(args->expr, state);
emit_byte(args->kind == ARG_NORMAL ? normal_op : splice_op, state);
pop_stack(pop, state);
normal_op = OP_LIST_ADD_TAIL;
splice_op = OP_LIST_APPEND;
pop = 1;
}
}
}
static void
push_lvalue(Expr * expr, int indexed_above, State * state)
{
unsigned old;
switch (expr->kind) {
case EXPR_RANGE:
push_lvalue(expr->e.range.base, 1, state);
old = save_stack_top(state);
generate_expr(expr->e.range.from, state);
generate_expr(expr->e.range.to, state);
restore_stack_top(old, state);
break;
case EXPR_INDEX:
push_lvalue(expr->e.bin.lhs, 1, state);
old = save_stack_top(state);
generate_expr(expr->e.bin.rhs, state);
restore_stack_top(old, state);
if (indexed_above) {
emit_byte(OP_PUSH_REF, state);
push_stack(1, state);
}
break;
case EXPR_ID:
if (indexed_above) {
emit_var_op(OP_PUSH, expr->e.id, state);
push_stack(1, state);
}
break;
case EXPR_PROP:
generate_expr(expr->e.bin.lhs, state);
generate_expr(expr->e.bin.rhs, state);
if (indexed_above) {
emit_byte(OP_PUSH_GET_PROP, state);
push_stack(1, state);
}
break;
default:
panic("Bad lvalue in PUSH_LVALUE()");
}
}
static void
generate_codes(Arg_List * codes, State * state)
{
if (codes)
generate_arg_list(codes, state);
else {
emit_byte(OPTIM_NUM_TO_OPCODE(0), state);
push_stack(1, state);
}
}
static void
generate_expr(Expr * expr, State * state)
{
switch (expr->kind) {
case EXPR_VAR:
{
Var v;
v = expr->e.var;
if (v.type == TYPE_INT && IN_OPTIM_NUM_RANGE(v.v.num))
emit_byte(OPTIM_NUM_TO_OPCODE(v.v.num), state);
else {
emit_byte(OP_IMM, state);
add_literal(v, state);
}
push_stack(1, state);
}
break;
case EXPR_ID:
emit_var_op(OP_PUSH, expr->e.id, state);
push_stack(1, state);
break;
case EXPR_AND:
case EXPR_OR:
{
int end_label;
generate_expr(expr->e.bin.lhs, state);
emit_byte(expr->kind == EXPR_AND ? OP_AND : OP_OR, state);
end_label = add_label(state);
pop_stack(1, state);
generate_expr(expr->e.bin.rhs, state);
define_label(end_label, state);
}
break;
case EXPR_NEGATE:
case EXPR_NOT:
generate_expr(expr->e.expr, state);
emit_byte(expr->kind == EXPR_NOT ? OP_NOT : OP_UNARY_MINUS, state);
break;
case EXPR_COMPLEMENT:
generate_expr(expr->e.expr, state);
emit_extended_byte(EOP_COMPLEMENT, state);
break;
case EXPR_EQ:
case EXPR_NE:
case EXPR_GE:
case EXPR_GT:
case EXPR_LE:
case EXPR_LT:
case EXPR_IN:
case EXPR_PLUS:
case EXPR_MINUS:
case EXPR_TIMES:
case EXPR_DIVIDE:
case EXPR_MOD:
case EXPR_PROP:
{
Opcode op = OP_ADD; /* initialize to silence warning */
generate_expr(expr->e.bin.lhs, state);
generate_expr(expr->e.bin.rhs, state);
switch (expr->kind) {
case EXPR_EQ:
op = OP_EQ;
break;
case EXPR_NE:
op = OP_NE;
break;
case EXPR_GE:
op = OP_GE;
break;
case EXPR_GT:
op = OP_GT;
break;
case EXPR_LE:
op = OP_LE;
break;
case EXPR_LT:
op = OP_LT;
break;
case EXPR_IN:
op = OP_IN;
break;
case EXPR_PLUS:
op = OP_ADD;
break;
case EXPR_MINUS:
op = OP_MINUS;
break;
case EXPR_TIMES:
op = OP_MULT;
break;
case EXPR_DIVIDE:
op = OP_DIV;
break;
case EXPR_MOD:
op = OP_MOD;
break;
case EXPR_PROP:
op = OP_GET_PROP;
break;
default:
panic("Not a binary operator in GENERATE_EXPR()");
}
emit_byte(op, state);
pop_stack(1, state);
}
break;
case EXPR_BITAND:
case EXPR_BITXOR:
case EXPR_BITOR:
case EXPR_SHL:
case EXPR_SHR:
case EXPR_LSHR:
{
Opcode op = EOP_BITAND; /* initialize to silence warning */
generate_expr(expr->e.bin.lhs, state);
generate_expr(expr->e.bin.rhs, state);
switch (expr->kind) {
case EXPR_BITAND:
op = EOP_BITAND;
break;
case EXPR_BITXOR:
op = EOP_BITXOR;
break;
case EXPR_BITOR:
op = EOP_BITOR;
break;
case EXPR_SHL:
op = EOP_SHL;
break;
case EXPR_SHR:
op = EOP_SHR;
break;
case EXPR_LSHR:
op = EOP_LSHR;
break;
default:
panic("Not a binary operator in GENERATE_EXPR()");
}
emit_extended_byte(op, state);
pop_stack(1, state);
}
break;
case EXPR_EXP:
generate_expr(expr->e.bin.lhs, state);
generate_expr(expr->e.bin.rhs, state);
emit_extended_byte(EOP_EXP, state);
pop_stack(1, state);
break;
case EXPR_INDEX:
{
unsigned old;
generate_expr(expr->e.bin.lhs, state);
old = save_stack_top(state);
generate_expr(expr->e.bin.rhs, state);
restore_stack_top(old, state);
emit_byte(OP_REF, state);
pop_stack(1, state);
}
break;
case EXPR_RANGE:
{
unsigned old;
generate_expr(expr->e.range.base, state);
old = save_stack_top(state);
generate_expr(expr->e.range.from, state);
generate_expr(expr->e.range.to, state);
restore_stack_top(old, state);
emit_byte(OP_RANGE_REF, state);
pop_stack(2, state);
}
break;
case EXPR_LENGTH:
{
unsigned saved = saved_stack_top(state);
if (saved != UINT_MAX) {
emit_extended_byte(EOP_LENGTH, state);
add_stack_ref(saved, state);
push_stack(1, state);
} else
panic("Missing saved stack for `$' in GENERATE_EXPR()");
}
break;
case EXPR_LIST:
generate_arg_list(expr->e.list, state);
break;
case EXPR_CALL:
generate_arg_list(expr->e.call.args, state);
emit_byte(OP_BI_FUNC_CALL, state);
emit_byte(expr->e.call.func, state);
break;
case EXPR_VERB:
generate_expr(expr->e.verb.obj, state);
generate_expr(expr->e.verb.verb, state);
generate_arg_list(expr->e.verb.args, state);
emit_call_verb_op(OP_CALL_VERB, state);
pop_stack(2, state);
break;
case EXPR_COND:
{
int else_label, end_label;
generate_expr(expr->e.cond.condition, state);
emit_byte(OP_IF_QUES, state);
else_label = add_label(state);
pop_stack(1, state);
generate_expr(expr->e.cond.consequent, state);
emit_byte(OP_JUMP, state);
end_label = add_label(state);
pop_stack(1, state);
define_label(else_label, state);
generate_expr(expr->e.cond.alternate, state);
define_label(end_label, state);
}
break;
case EXPR_ASGN:
{
Expr *e = expr->e.bin.lhs;
if (e->kind == EXPR_SCATTER) {
int nargs = 0, nreq = 0, rest = -1;
unsigned done;
Scatter *sc;
generate_expr(expr->e.bin.rhs, state);
for (sc = e->e.scatter; sc; sc = sc->next) {
nargs++;
if (sc->kind == SCAT_REQUIRED)
nreq++;
else if (sc->kind == SCAT_REST)
rest = nargs;
}
if (rest == -1)
rest = nargs + 1;
emit_extended_byte(EOP_SCATTER, state);
emit_byte(nargs, state);
emit_byte(nreq, state);
emit_byte(rest, state);
for (sc = e->e.scatter; sc; sc = sc->next) {
add_var_ref(sc->id, state);
if (sc->kind != SCAT_OPTIONAL)
add_pseudo_label(0, state);
else if (!sc->expr)
add_pseudo_label(1, state);
else
sc->label = add_label(state);
}
done = add_label(state);
for (sc = e->e.scatter; sc; sc = sc->next)
if (sc->kind == SCAT_OPTIONAL && sc->expr) {
define_label(sc->label, state);
generate_expr(sc->expr, state);
emit_var_op(OP_PUT, sc->id, state);
emit_byte(OP_POP, state);
pop_stack(1, state);
}
define_label(done, state);
} else {
int is_indexed = 0;
push_lvalue(e, 0, state);
generate_expr(expr->e.bin.rhs, state);
if (e->kind == EXPR_RANGE || e->kind == EXPR_INDEX)
emit_byte(OP_PUT_TEMP, state);
while (1) {
switch (e->kind) {
case EXPR_RANGE:
emit_extended_byte(EOP_RANGESET, state);
pop_stack(3, state);
e = e->e.range.base;
is_indexed = 1;
continue;
case EXPR_INDEX:
emit_byte(OP_INDEXSET, state);
pop_stack(2, state);
e = e->e.bin.lhs;
is_indexed = 1;
continue;
case EXPR_ID:
emit_var_op(OP_PUT, e->e.id, state);
break;
case EXPR_PROP:
emit_byte(OP_PUT_PROP, state);
pop_stack(2, state);
break;
default:
panic("Bad lvalue in GENERATE_EXPR()");
}
break;
}
if (is_indexed) {
emit_byte(OP_POP, state);
emit_byte(OP_PUSH_TEMP, state);
}
}
}
break;
case EXPR_CATCH:
{
int handler_label, end_label;
generate_codes(expr->e.catch.codes, state);
emit_extended_byte(EOP_PUSH_LABEL, state);
handler_label = add_label(state);
push_stack(1, state);
emit_extended_byte(EOP_CATCH, state);
push_stack(1, state);
INCR_TRY_DEPTH(state);
generate_expr(expr->e.expr, state);
DECR_TRY_DEPTH(state);
emit_extended_byte(EOP_END_CATCH, state);
end_label = add_label(state);
pop_stack(3, state); /* codes, label, catch */
define_label(handler_label, state);
/* After this label, we still have a value on the stack, but now,
* instead of it being the value of the main expression, we have
* the exception tuple pushed before entering the handler.
*/
if (expr->e.catch.except) {
emit_byte(OP_POP, state);
pop_stack(1, state);
generate_expr(expr->e.catch.except, state);
} else {
/* Select code from tuple */
emit_byte(OPTIM_NUM_TO_OPCODE(1), state);
emit_byte(OP_REF, state);
}
define_label(end_label, state);
}
break;
default:
panic("Can't happen in GENERATE_EXPR()");
}
}
static Bytecodes stmt_to_code(Stmt *, GState *);
static void
generate_stmt(Stmt * stmt, State * state)
{
for (; stmt; stmt = stmt->next) {
switch (stmt->kind) {
case STMT_COND:
{
Opcode if_op = OP_IF;
int end_label = -1;
Cond_Arm *arms;
for (arms = stmt->s.cond.arms; arms; arms = arms->next) {
int else_label;
generate_expr(arms->condition, state);
emit_byte(if_op, state);
else_label = add_label(state);
pop_stack(1, state);
generate_stmt(arms->stmt, state);
emit_byte(OP_JUMP, state);
end_label = add_linked_label(end_label, state);
define_label(else_label, state);
if_op = OP_EIF;
}
if (stmt->s.cond.otherwise)
generate_stmt(stmt->s.cond.otherwise, state);
define_label(end_label, state);
}
break;
case STMT_LIST:
{
Fixup loop_top;
int end_label;
generate_expr(stmt->s.list.expr, state);
emit_byte(OPTIM_NUM_TO_OPCODE(1), state); /* loop list index */
push_stack(1, state);
loop_top = capture_label(state);
emit_byte(OP_FOR_LIST, state);
add_var_ref(stmt->s.list.id, state);
end_label = add_label(state);
enter_loop(stmt->s.list.id, loop_top, state->cur_stack,
end_label, state->cur_stack - 2, state);
generate_stmt(stmt->s.list.body, state);
end_label = exit_loop(state);
emit_byte(OP_JUMP, state);
add_known_label(loop_top, state);
define_label(end_label, state);
pop_stack(2, state);
}
break;
case STMT_RANGE:
{
Fixup loop_top;
int end_label;
generate_expr(stmt->s.range.from, state);
generate_expr(stmt->s.range.to, state);
loop_top = capture_label(state);
emit_byte(OP_FOR_RANGE, state);
add_var_ref(stmt->s.range.id, state);
end_label = add_label(state);
enter_loop(stmt->s.range.id, loop_top, state->cur_stack,
end_label, state->cur_stack - 2, state);
generate_stmt(stmt->s.range.body, state);
end_label = exit_loop(state);
emit_byte(OP_JUMP, state);
add_known_label(loop_top, state);