summaryrefslogtreecommitdiff
path: root/erts/emulator/beam/emu/generators.tab
blob: 397ddb97e75ae7ac880beffe30ed2380c1273406 (plain)
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
// -*- c -*-
//
// %CopyrightBegin%
//
// Copyright Ericsson AB 2020. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// %CopyrightEnd%
//

// Generate the fastest instruction to fetch an integer from a binary.
gen.get_integer2(Fail, Ms, Live, Size, Unit, Flags, Dst) {
    BeamOp* op;
    UWord bits;

    $NewBeamOp(S, op);
    $NativeEndian(Flags);
    if (Size.type == TAG_i) {
        if (!beam_load_safe_mul(Size.val, Unit.val, &bits)) {
            goto error;
        } else if ((Flags.val & BSF_SIGNED) != 0) {
            goto generic;
        } else if (bits == 8) {
            $BeamOpNameArity(op, i_bs_get_integer_8, 3);
            op->a[0] = Ms;
            op->a[1] = Fail;
            op->a[2] = Dst;
        } else if (bits == 16 && (Flags.val & BSF_LITTLE) == 0) {
            $BeamOpNameArity(op, i_bs_get_integer_16, 3);
            op->a[0] = Ms;
            op->a[1] = Fail;
            op->a[2] = Dst;
#ifdef ARCH_64
        } else if (bits == 32 && (Flags.val & BSF_LITTLE) == 0) {
            $BeamOpNameArity(op, i_bs_get_integer_32, 3);
            op->a[0] = Ms;
            op->a[1] = Fail;
            op->a[2] = Dst;
#endif
        } else {
        generic:
            if (bits < SMALL_BITS) {
                $BeamOpNameArity(op, i_bs_get_integer_small_imm, 5);
                op->a[0] = Ms;
                op->a[1].type = TAG_u;
                op->a[1].val = bits;
                op->a[2] = Fail;
                op->a[3] = Flags;
                op->a[4] = Dst;
            } else {
                $BeamOpNameArity(op, i_bs_get_integer_imm, 6);
                op->a[0] = Ms;
                op->a[1].type = TAG_u;
                op->a[1].val = bits;
                op->a[2] = Live;
                op->a[3] = Fail;
                op->a[4] = Flags;
                op->a[5] = Dst;
            }
        }
    } else if (Size.type == TAG_q) {
        Eterm big = beamfile_get_literal(&S->beam, Size.val);
        Uint bigval;

        if (!term_to_Uint(big, &bigval)) {
        error:
            $BeamOpNameArity(op, jump, 1);
            op->a[0] = Fail;
        } else {
            if (!beam_load_safe_mul(bigval, Unit.val, &bits)) {
                goto error;
            }
            goto generic;
        }
    } else if (Size.type == TAG_x || Size.type == TAG_y) {
        $BeamOpNameArity(op, i_bs_get_integer, 6);
        op->a[0] = Ms;
        op->a[1] = Fail;
        op->a[2] = Live;
        op->a[3].type = TAG_u;
        op->a[3].val = (Unit.val << 3) | Flags.val;
        op->a[4] = Size;
        op->a[5] = Dst;
        return op;
    } else {
        /* Invalid literal size. */
        goto error;
    }
    return op;
}

gen.jump_tab(Src, Fail, Size, Rest) {
    Sint min, max;
    Sint i;
    Sint size;
    Sint arity;
    int fixed_args;
    BeamOp* op;

    ASSERT(Size.val >= 2 && Size.val % 2 == 0);

    /* Don't generate a jump table if there's only one choice */
    if (Size.val == 2) {
        BeamOp* jump;

        $NewBeamOp(S, op);
        $BeamOpNameArity(op, is_ne_exact, 3);
        op->a[0] = Rest[1];
        op->a[1] = Src;
        op->a[2] = Rest[0];

        $NewBeamOp(S, jump);
        $BeamOpNameArity(jump, jump, 1);
        jump->a[0] = Fail;

        op->next = jump;
        jump->next = NULL;
        return op;
    }

    /* Calculate the minimum and maximum values and size of jump table. */
    ASSERT(Rest[0].type == TAG_i);
    min = max = Rest[0].val;
    for (i = 2; i < Size.val; i += 2) {
        ASSERT(Rest[i].type == TAG_i && Rest[i+1].type == TAG_f);
        if (Rest[i].val < min) {
            min = Rest[i].val;
        } else if (max < Rest[i].val) {
            max = Rest[i].val;
        }
    }
    size = max - min + 1;

    /* Allocate structure and fill in the fixed fields. */
    $NewBeamOp(S, op);
    op->next = NULL;
    if (min == 0) {
        $BeamOpNameArity(op, i_jump_on_val_zero, 3);
    } else {
        $BeamOpNameArity(op, i_jump_on_val, 4);
    }
    fixed_args = op->arity;
    arity = fixed_args + size;
    $BeamOpArity(op, arity);
    op->a[0] = Src;
    op->a[1] = Fail;
    op->a[2].type = TAG_u;
    op->a[2].val = size;
    op->a[3].type = TAG_u;
    op->a[3].val = min;

    /* Fill in the jump table. */
    for (i = fixed_args; i < arity; i++) {
        op->a[i] = Fail;
    }

    for (i = 0; i < Size.val; i += 2) {
        Sint index = fixed_args + Rest[i].val - min;
        ASSERT(fixed_args <= index && index < arity);
        op->a[index] = Rest[i+1];
    }

    return op;
}

//
// Generate a select_val instruction.  We know that a jump table
// is not suitable, and that all values are of the same type
// (integer or atoms).
//
gen.select_val(Src, Fail, Size, Rest) {
    BeamOp* op;
    BeamOpArg *tmp;
    int arity = Size.val + 3;
    int size = Size.val / 2;
    int i, j, align = 0;

    if (size == 2) {
        /*
         * Use a special-cased instruction if there are only two values.
         */

        $NewBeamOp(S, op);
        op->next = NULL;
        $BeamOpNameArity(op, i_select_val2, 4);
        $BeamOpArity(op, arity - 1);
        op->a[0] = Src;
        op->a[1] = Fail;
        op->a[2] = Rest[0];
        op->a[3] = Rest[2];
        op->a[4] = Rest[1];
        op->a[5] = Rest[3];

        return op;
    }

    if (size <= 10) {
        /* Use linear search. Reserve place for a sentinel. */
        align = 1;
    }

    arity += 2*align;
    size  += align;

    $NewBeamOp(S, op);
    op->next = NULL;
    if (align == 0) {
        $BeamOpNameArity(op, i_select_val_bins, 3);
    } else {
        $BeamOpNameArity(op, i_select_val_lins, 3);
    }
    $BeamOpArity(op, arity);
    op->a[0] = Src;
    op->a[1] = Fail;
    op->a[2].type = TAG_u;
    op->a[2].val = size;

    tmp = (BeamOpArg *) erts_alloc(ERTS_ALC_T_LOADER_TMP, sizeof(BeamOpArg)*(arity-2*align));

    for (i = 3; i < arity - 2*align; i++) {
        tmp[i-3] = Rest[i-3];
    }

    /* Sort the values to make them useful for a binary or sentinel search. */
    beam_load_sort_select_vals(tmp, size - align);

    j = 3;
    for (i = 3; i < arity - 2*align; i += 2) {
        op->a[j]      = tmp[i-3];
        op->a[j+size] = tmp[i-2];
        j++;
    }

    erts_free(ERTS_ALC_T_LOADER_TMP, (void *) tmp);

    if (align) {
        /* Add sentinel for linear search. */
        op->a[j].type = TAG_u;
        op->a[j].val  = ~((BeamInstr)0);
        op->a[j+size] = Fail;
    }

#ifdef DEBUG
    for (i = 0; i < size - 1; i++) {
        ASSERT(op->a[i+3].val <= op->a[i+4].val);
    }
#endif

    return op;
}

//
// Generate a select_val instruction for big numbers.
//
gen.select_literals(Src, Fail, Size, Rest) {
    BeamOp* op;
    BeamOp* jump;
    BeamOp** prev_next = &op;

    int i;

    for (i = 0; i < Size.val; i += 2) {
        BeamOp* op;
        ASSERT(Rest[i].type == TAG_q);

        $NewBeamOp(S, op);
        $BeamOpNameArity(op, is_ne_exact, 3);
        op->a[0] = Rest[i+1];
        op->a[1] = Src;
        op->a[2] = Rest[i];
        *prev_next = op;
        prev_next = &op->next;
    }

    $NewBeamOp(S, jump);
    $BeamOpNameArity(jump, jump, 1);
    jump->next = NULL;
    jump->a[0] = Fail;
    *prev_next = jump;
    return op;
}

//
// Replace a select_val instruction with a constant controlling expression
// with a jump instruction.
//
gen.const_select_val(Src, Fail, Size, Rest) {
    BeamOp* op;
    int i;

    ASSERT(Size.type == TAG_u);

    $NewBeamOp(S, op);
    $BeamOpNameArity(op, jump, 1);
    op->next = NULL;

    /* Search for a literal matching the controlling expression. */
    switch (Src.type) {
    case TAG_q:
        {
            Eterm expr = beamfile_get_literal(&S->beam, Src.val);
            for (i = 0; i < Size.val; i += 2) {
                if (Rest[i].type == TAG_q) {
                    Eterm term = beamfile_get_literal(&S->beam, Rest[i].val);
                    if (eq(term, expr)) {
                        ASSERT(Rest[i+1].type == TAG_f);
                        op->a[0] = Rest[i+1];
                        return op;
                    }
                }
            }
        }
        break;
    case TAG_i:
    case TAG_a:
        for (i = 0; i < Size.val; i += 2) {
            if (Rest[i].val == Src.val && Rest[i].type == Src.type) {
                ASSERT(Rest[i+1].type == TAG_f);
                op->a[0] = Rest[i+1];
                return op;
            }
        }
        break;
    }

    /*
     * No match.  Use the failure label.
     */

    op->a[0] = Fail;
    return op;
}

//
// Split a list consisting of both small and bignumbers into two
// select_val instructions.
//
gen.split_values(Src, TypeFail, Fail, Size, Rest) {
    BeamOp* op1;
    BeamOp* op2;
    BeamOp* label;
    BeamOp* is_integer;
    int i;

    ASSERT(Size.val >= 2 && Size.val % 2 == 0);

    $NewBeamOp(S, is_integer);
    $BeamOpNameArity(is_integer, is_integer, 2);
    is_integer->a[0] = TypeFail;
    is_integer->a[1] = Src;

    $NewBeamOp(S, label);
    $BeamOpNameArity(label, label, 1);
    label->a[0].type = TAG_u;
    label->a[0].val = beam_load_new_label(S);

    $NewBeamOp(S, op1);
    $BeamOpNameArity(op1, select_val, 3);
    $BeamOpArity(op1, 3 + Size.val);
    op1->a[0] = Src;
    op1->a[1].type = TAG_f;
    op1->a[1].val = label->a[0].val;
    op1->a[2].type = TAG_u;
    op1->a[2].val = 0;

    $NewBeamOp(S, op2);
    $BeamOpNameArity(op2, select_val, 3);
    $BeamOpArity(op2, 3 + Size.val);
    op2->a[0] = Src;
    op2->a[1] = Fail;
    op2->a[2].type = TAG_u;
    op2->a[2].val = 0;

    /* Split the list. */
    ASSERT(Size.type == TAG_u);
    for (i = 0; i < Size.val; i += 2) {
        BeamOp* op = (Rest[i].type == TAG_q) ? op2 : op1;
        int dst = 3 + op->a[2].val;

        ASSERT(Rest[i+1].type == TAG_f);
        op->a[dst] = Rest[i];
        op->a[dst+1] = Rest[i+1];
        op->arity += 2;
        op->a[2].val += 2;
    }
    ASSERT(op1->a[2].val > 0);
    ASSERT(op2->a[2].val > 0);

    /* Order the instruction sequence appropriately. */
    if (TypeFail.val == Fail.val) {
        /*
         * select_val L1 S ... (small numbers)
         * label L1
         * is_integer Fail S
         * select_val Fail S ... (bignums)
         */
        op1->next = label;
        label->next = is_integer;
        is_integer->next = op2;
    } else {
        /*
         * is_integer TypeFail S
         * select_val L1 S ... (small numbers)
         * label L1
         * select_val Fail S ... (bignums)
         */
        is_integer->next = op1;
        op1->next = label;
        label->next = op2;
        op1 = is_integer;
    }
    op2->next = NULL;

    return op1;
}


//
// Tag the list of values with tuple arity tags.
//
gen.select_tuple_arity(Src, Fail, Size, Rest) {
    BeamOp* op;
    BeamOpArg *tmp;
    int arity = Size.val + 3;
    int size = Size.val / 2;
    int i, j, align = 0;

    /* Verify the validity of the list. */

    if (Size.val % 2 != 0) {
        return NULL;
    }

    for (i = 0; i < Size.val; i += 2) {
        if (Rest[i].type != TAG_u || Rest[i+1].type != TAG_f) {
            return NULL;
        }
    }

    /*
     * Use a special-cased instruction if there are only two values.
     */
    if (size == 2) {
        $NewBeamOp(S, op);
        $BeamOpNameArity(op, i_select_tuple_arity2, 4);
        $BeamOpArity(op, arity - 1);
        op->next = NULL;
        op->a[0] = Src;
        op->a[1] = Fail;
        op->a[2].type = TAG_u;
        op->a[2].val  = Rest[0].val;
        op->a[3].type = TAG_u;
        op->a[3].val  = Rest[2].val;
        op->a[4] = Rest[1];
        op->a[5] = Rest[3];

        return op;
    }

    /*
     * Generate the generic instruction.
     * Assumption:
     *   Few different tuple arities to select on (fewer than 20).
     *   Use linear scan approach.
     */

    align = 1;

    arity += 2*align;
    size  += align;

    $NewBeamOp(S, op);
    $BeamOpNameArity(op, i_select_tuple_arity, 3);
    $BeamOpArity(op, arity);
    op->next = NULL;
    op->a[0] = Src;
    op->a[1] = Fail;
    op->a[2].type = TAG_u;
    op->a[2].val = size;

    tmp = (BeamOpArg *) erts_alloc(ERTS_ALC_T_LOADER_TMP, sizeof(BeamOpArg)*(arity-2*align));

    for (i = 3; i < arity - 2*align; i+=2) {
        tmp[i-3].type = TAG_v;
        tmp[i-3].val  = make_arityval(Rest[i-3].val);
        tmp[i-2]      = Rest[i-2];
    }

    /* Sort the values to make them useful for a binary or sentinel search. */
    beam_load_sort_select_vals(tmp, size - align);

    j = 3;
    for (i = 3; i < arity - 2*align; i += 2) {
        op->a[j]        = tmp[i-3];
        op->a[j + size] = tmp[i-2];
        j++;
    }

    erts_free(ERTS_ALC_T_LOADER_TMP, (void *) tmp);

    op->a[j].type = TAG_u;
    op->a[j].val  = ~((BeamInstr)0);
    op->a[j+size] = Fail;

    return op;
}

gen.new_small_map_lit(Dst, Live, Size, Rest) {
    unsigned size = Size.val;
    Uint lit;
    unsigned i;
    BeamOp* op;
    BeamOpArg* dst;
    Eterm* tmp;
    Eterm* thp;
    Eterm keys;

    $NewBeamOp(S, op);
    $BeamOpNameArity(op, i_new_small_map_lit, 3);
    $BeamOpArity(op, 3 + size/2);
    op->next = NULL;

    tmp = thp = erts_alloc(ERTS_ALC_T_LOADER_TMP, (1 + size/2) * sizeof(*tmp));
    keys = make_tuple(thp);
    *thp++ = make_arityval(size/2);

    dst = op->a+3;

    for (i = 0; i < size; i += 2) {
        switch (Rest[i].type) {
        case TAG_a:
            *thp++ = Rest[i].val;
            ASSERT(is_atom(Rest[i].val));
            break;
        case TAG_i:
            *thp++ = make_small(Rest[i].val);
            break;
        case TAG_n:
            *thp++ = NIL;
            break;
        case TAG_q:
            *thp++ = beamfile_get_literal(&S->beam, Rest[i].val);
            break;
        }
        *dst++ = Rest[i + 1];
    }

    lit = beamfile_add_literal(&S->beam, keys);
    erts_free(ERTS_ALC_T_LOADER_TMP, tmp);

    op->a[0] = Dst;
    op->a[1] = Live;
    op->a[2].type = TAG_q;
    op->a[2].val = lit;

    return op;
}

// Macro for generating a timeout instruction for a literal timeout value.
gen_literal_timeout(stp, fail, time, succ_instr, fail_instr) {
    BeamOp* op;
    Sint timeout;

    $NewBeamOp($stp, op);
    $BeamOpNameArity(op, $succ_instr, 2);
    op->a[0].type = TAG_u;
    op->a[1] = $fail;

    if ($time.type == TAG_i && (timeout = $time.val) >= 0 &&
#if defined(ARCH_64)
        (timeout >> 32) == 0
#else
        1
#endif
        ) {
        op->a[0].val = timeout;
#if !defined(ARCH_64)
    } else if ($time.type == TAG_q) {
        Eterm big = beamfile_get_literal(&S->beam, $time.val);

        if (is_not_big(big)) {
            goto error;
        }

        if (big_arity(big) > 1 || big_sign(big)) {
            goto error;
        } else {
            Uint u;
            (void) term_to_Uint(big, &u);
            op->a[0].val = (BeamInstr) u;
        }
#endif
    } else {
#if !defined(ARCH_64)
    error:
#endif
        $BeamOpNameArity(op, $fail_instr, 0);
    }
    return op;
}

gen.literal_timeout(Fail, Time) {
    $gen_literal_timeout(S, Fail, Time, wait_timeout_unlocked_int, i_wait_error);
}

gen.literal_timeout_locked(Fail, Time) {
    $gen_literal_timeout(S, Fail, Time, wait_timeout_locked_int, i_wait_error_locked);
}

// Generate an instruction for element/2.
gen.element(Fail, Index, Tuple, Dst) {
    BeamOp* op;

    $NewBeamOp(S, op);
    op->next = NULL;

    if (Index.type == TAG_i && Index.val > 0 &&
        Index.val <= ERTS_MAX_TUPLE_SIZE &&
        (Tuple.type == TAG_x || Tuple.type == TAG_y)) {
        $BeamOpNameArity(op, i_fast_element, 4);
        op->a[0] = Tuple;
        op->a[1] = Fail;
        op->a[2].type = TAG_u;
        op->a[2].val = Index.val;
        op->a[3] = Dst;
    } else {
        $BeamOpNameArity(op, i_element, 4);
        op->a[0] = Tuple;
        op->a[1] = Fail;
        op->a[2] = Index;
        op->a[3] = Dst;
    }

    return op;
}

// Generate the fastest instruction to fetch a binary from a binary.
gen.get_binary2(Fail, Ms, Live, Size, Unit, Flags, Dst) {
    BeamOp* op;

    $NewBeamOp(S, op);
    $NativeEndian(Flags);

    if (Size.type == TAG_a && Size.val == am_all) {
        $BeamOpNameArity(op, i_bs_get_binary_all2, 5);
        op->a[0] = Ms;
        op->a[1] = Fail;
        op->a[2] = Live;
        op->a[3] = Unit;
        op->a[4] = Dst;
    } else if (Size.type == TAG_i) {
        $BeamOpNameArity(op, i_bs_get_binary_imm2, 6);
        op->a[0] = Ms;
        op->a[1] = Fail;
        op->a[2] = Live;
        op->a[3].type = TAG_u;
        if (!beam_load_safe_mul(Size.val, Unit.val, &op->a[3].val)) {
            goto error;
        }
        op->a[4] = Flags;
        op->a[5] = Dst;
    } else if (Size.type == TAG_q) {
        Eterm big = beamfile_get_literal(&S->beam, Size.val);
        Uint bigval;

        if (!term_to_Uint(big, &bigval)) {
        error:
            $BeamOpNameArity(op, jump, 1);
            op->a[0] = Fail;
        } else {
            $BeamOpNameArity(op, i_bs_get_binary_imm2, 6);
            op->a[0] = Ms;
            op->a[1] = Fail;
            op->a[2] = Live;
            op->a[3].type = TAG_u;
            if (!beam_load_safe_mul(bigval, Unit.val, &op->a[3].val)) {
                goto error;
            }
            op->a[4] = Flags;
            op->a[5] = Dst;
        }
    } else if (Size.type == TAG_x || Size.type == TAG_y) {
        $BeamOpNameArity(op, i_bs_get_binary2, 6);
        op->a[0] = Ms;
        op->a[1] = Fail;
        op->a[2] = Live;
        op->a[3] = Size;
        op->a[4].type = TAG_u;
        op->a[4].val = (Unit.val << 3) | Flags.val;
        op->a[5] = Dst;
    } else {
        /* Invalid literal size. */
        goto error;
    }
    op->next = NULL;
    return op;
}

gen.is_function2(Fail, Fun, Arity) {
    BeamOp* op;
    int literal_arity = Arity.type == TAG_i;
    int fun_is_reg = Fun.type == TAG_x || Fun.type == TAG_y;

    $NewBeamOp(S, op);

    if (fun_is_reg && literal_arity) {
        /*
         * Most common case. Fun in a register and arity
         * is an integer literal.
         */
        if (Arity.val > MAX_ARG) {
            /* Arity is negative or too big. */
            $BeamOpNameArity(op, jump, 1);
            op->a[0] = Fail;
            return op;
        } else {
            $BeamOpNameArity(op, hot_is_function2, 3);
            op->a[0] = Fail;
            op->a[1] = Fun;
            op->a[2].type = TAG_u;
            op->a[2].val = Arity.val;
            return op;
        }
    } else {
        /*
         * Handle extremely uncommon cases by a slower sequence.
         */
        BeamOp* move_fun;
        BeamOp* move_arity;

        $NewBeamOp(S, move_fun);
        $NewBeamOp(S, move_arity);

        move_fun->next = move_arity;
        move_arity->next = op;

        $BeamOpNameArity(move_fun, move, 2);
        move_fun->a[0] = Fun;
        move_fun->a[1].type = TAG_x;
        move_fun->a[1].val = 1022;

        $BeamOpNameArity(move_arity, move, 2);
        move_arity->a[0] = Arity;
        move_arity->a[1].type = TAG_x;
        move_arity->a[1].val = 1023;

        $BeamOpNameArity(op, cold_is_function2, 3);
        op->a[0] = Fail;
        op->a[1].type = TAG_x;
        op->a[1].val = 1022;
        op->a[2].type = TAG_x;
        op->a[2].val = 1023;
        return move_fun;
    }
}

INIT_YREGS(S, N) {
    int i;
    for (i = 0; i < $N; i++) {
	BeamOp* init;

	$NewBeamOp($S, init);
	$BeamOpNameArity(init, init, 1);
	init->a[0] = Yregs[i];
	*p = init;
	p = &init->next;
    }
}

gen.allocate(Ns, Live, N, Yregs) {
    BeamOp* alloc;
    BeamOp** p;

    $NewBeamOp(S, alloc);
    alloc->a[0] = Ns;
    alloc->a[1] = Live;

    if (Ns.val <= 2 * N.val) {
	/*
	 * At least half of the Y registers need explicit
	 * initialization. It will be cheaper to zero all Y registers.
	 */
	$BeamOpNameArity(alloc, allocate_zero, 2);
    } else {
	$BeamOpNameArity(alloc, allocate, 2);
	p = &alloc->next;
	$INIT_YREGS(S, N.val);
    }
    return alloc;
}

gen.allocate_heap(Ns, Nh, Live, N, Yregs) {
    BeamOp* alloc;
    BeamOp** p;

    $NewBeamOp(S, alloc);
    alloc->a[0] = Ns;
    alloc->a[1] = Nh;
    alloc->a[2] = Live;

    if (Ns.val <= 2 * N.val) {
	/*
	 * At least half of the Y registers need explicit
	 * initialization. It will be cheaper to zero all Y registers.
	 */
	$BeamOpNameArity(alloc, allocate_heap_zero, 3);
    } else {
	$BeamOpNameArity(alloc, allocate_heap, 3);
	p = &alloc->next;
	$INIT_YREGS(S, N.val);
    }
    return alloc;
}

gen.init_yregs(N, Yregs) {
    BeamOp* first = NULL;
    BeamOp** p;

    p = &first;
    $INIT_YREGS(S, N.val);
    return first;
}

gen.create_bin(Fail, Alloc, Live, Unit, Dst, N, Segments) {
    BeamOp* op;
    int fixed_args;
    BeamOpArg* src;
    BeamOpArg* dst;
    BeamOpArg* endp;

    endp = Segments + N.val;
    N.val = 5*N.val/6;

    $NewBeamOp(S, op);
    $BeamOpNameArity(op, i_bs_create_bin, 5);
    fixed_args = op->arity;
    $BeamOpArity(op, (N.val + fixed_args));

    op->a[0] = Fail;
    op->a[1] = Alloc;
    op->a[2] = Live;
    op->a[3] = Dst;
    op->a[4] = N;

    for (src = Segments, dst = op->a+fixed_args; src < endp; src += 6, dst += 5) {
        UWord unit;
        BeamOpArg Flags;
        Uint flags = 0;
        BeamOpArg Size;
        Uint type;
        Uint segment;

        ASSERT(src[0].type = TAG_a);
        ASSERT(src[1].type == TAG_u);
        ASSERT(src[2].type == TAG_u);
        segment = src[1].val;

        /* Get unit. */
        dst[1] = src[2];
        unit = dst[1].val;

        /* Translate flags. */
        Flags = src[3];              /* Flags */
        if (Flags.type != TAG_n) {
            if (Flags.type == TAG_q) {
                Eterm term = beamfile_get_literal(&S->beam, Flags.val);
                while (is_list(term)) {
                    Eterm* consp = list_val(term);
                    Eterm elem = CAR(consp);
                    switch (elem) {
                    case am_little:
                        flags |= BSF_LITTLE;
                        break;
                    case am_native:
                        flags |= BSF_NATIVE;
                        break;
                    }
                    term = CDR(consp);
                }
                ASSERT(is_nil(term));
            }
        }
        Flags.type = TAG_u;
        Flags.val = flags;
        $NativeEndian(Flags);
        Flags.val = (segment << 3) | Flags.val;
        dst[2] = Flags;

        /* Store source. */
        dst[3] = src[4];        /* Src */

        /* Get size */
        Size = src[5];          /* Size */

        /* Translate type. */
        switch (src[0].val) {
        case am_append:
            type = BSC_APPEND;
            break;
        case am_private_append:
            type = BSC_PRIVATE_APPEND;
            break;
        case am_binary:
            {
                UWord bits;
                type = BSC_BINARY;
                if (Size.type == TAG_a && Size.val == am_all) {
                    type = BSC_BINARY_ALL;
                } else if (Size.type == TAG_i &&
                           (Sint) Size.val >= 0 &&
                           beam_load_safe_mul(Size.val, unit, &bits) &&
                           (bits >> (sizeof(Uint)-1)*8) == 0) {
                    type = BSC_BINARY_FIXED_SIZE;
                    Size.type = TAG_u;
                    Size.val = bits;
                    unit = 0;
                }
            }
            break;
        case am_float:
            {
                UWord bits;
                type = BSC_FLOAT;
                if (Size.type == TAG_i &&
                    (Sint) Size.val >= 0 &&
                    beam_load_safe_mul(Size.val, unit, &bits) &&
                    (bits >> (sizeof(Uint)-1)*8) == 0) {
                    type = BSC_FLOAT_FIXED_SIZE;
                    Size.type = TAG_u;
                    Size.val = bits;
                    unit = 0;
                }
            }
            break;
        case am_integer:
            {
                UWord bits;
                type = BSC_INTEGER;
                if (Size.type == TAG_i &&
                    (Sint) Size.val >= 0 &&
                    beam_load_safe_mul(Size.val, unit, &bits) &&
                    (bits >> (sizeof(Uint)-1)*8) == 0) {
                    type = BSC_INTEGER_FIXED_SIZE;
                    Size.type = TAG_u;
                    Size.val = bits;
                    unit = 0;
                }
            }
            break;
        case am_string:
            type = BSC_STRING;
            ASSERT(Size.type == TAG_i);
            ASSERT(unit == 8);
            Size.type = TAG_u;
            Size.val = Size.val; /* Size of string in bytes. */
            unit = 0;
            break;
        case am_utf8:
            type = BSC_UTF8;
            break;
        case am_utf16:
            type = BSC_UTF16;
            break;
        case am_utf32:
            type = BSC_UTF32;
            Size.type = TAG_u;
            Size.val = 32;
            break;
        default:
            abort();
        }
        dst[0].type = TAG_u;
        dst[0].val = type;

        /* Store value of unit. */
        dst[1].val = unit;

        /* Store size. */
        dst[4] = Size;
    }
    return op;
}