-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm_ppcspe.dasc
3691 lines (3626 loc) · 102 KB
/
vm_ppcspe.dasc
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
|// Low-level VM code for PowerPC/e500 CPUs.
|// Bytecode interpreter, fast functions and helper functions.
|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
|
|.arch ppc
|.section code_op, code_sub
|
|.actionlist build_actionlist
|.globals GLOB_
|.globalnames globnames
|.externnames extnames
|
|// Note: The ragged indentation of the instructions is intentional.
|// The starting columns indicate data dependencies.
|
|//-----------------------------------------------------------------------
|
|// Fixed register assignments for the interpreter.
|// Don't use: r1 = sp, r2 and r13 = reserved and/or small data area ptr
|
|// The following must be C callee-save (but BASE is often refetched).
|.define BASE, r14 // Base of current Lua stack frame.
|.define KBASE, r15 // Constants of current Lua function.
|.define PC, r16 // Next PC.
|.define DISPATCH, r17 // Opcode dispatch table.
|.define LREG, r18 // Register holding lua_State (also in SAVE_L).
|.define MULTRES, r19 // Size of multi-result: (nresults+1)*8.
|
|// Constants for vectorized type-comparisons (hi+low GPR). C callee-save.
|.define TISNUM, r22
|.define TISSTR, r23
|.define TISTAB, r24
|.define TISFUNC, r25
|.define TISNIL, r26
|.define TOBIT, r27
|.define ZERO, TOBIT // Zero in lo word.
|
|// The following temporaries are not saved across C calls, except for RA.
|.define RA, r20 // Callee-save.
|.define RB, r10
|.define RC, r11
|.define RD, r12
|.define INS, r7 // Overlaps CARG5.
|
|.define TMP0, r0
|.define TMP1, r8
|.define TMP2, r9
|.define TMP3, r6 // Overlaps CARG4.
|
|// Saved temporaries.
|.define SAVE0, r21
|
|// Calling conventions.
|.define CARG1, r3
|.define CARG2, r4
|.define CARG3, r5
|.define CARG4, r6 // Overlaps TMP3.
|.define CARG5, r7 // Overlaps INS.
|
|.define CRET1, r3
|.define CRET2, r4
|
|// Stack layout while in interpreter. Must match with lj_frame.h.
|.define SAVE_LR, 188(sp)
|.define CFRAME_SPACE, 184 // Delta for sp.
|// Back chain for sp: 184(sp) <-- sp entering interpreter
|.define SAVE_r31, 176(sp) // 64 bit register saves.
|.define SAVE_r30, 168(sp)
|.define SAVE_r29, 160(sp)
|.define SAVE_r28, 152(sp)
|.define SAVE_r27, 144(sp)
|.define SAVE_r26, 136(sp)
|.define SAVE_r25, 128(sp)
|.define SAVE_r24, 120(sp)
|.define SAVE_r23, 112(sp)
|.define SAVE_r22, 104(sp)
|.define SAVE_r21, 96(sp)
|.define SAVE_r20, 88(sp)
|.define SAVE_r19, 80(sp)
|.define SAVE_r18, 72(sp)
|.define SAVE_r17, 64(sp)
|.define SAVE_r16, 56(sp)
|.define SAVE_r15, 48(sp)
|.define SAVE_r14, 40(sp)
|.define SAVE_CR, 36(sp)
|.define UNUSED1, 32(sp)
|.define SAVE_ERRF, 28(sp) // 32 bit C frame info.
|.define SAVE_NRES, 24(sp)
|.define SAVE_CFRAME, 20(sp)
|.define SAVE_L, 16(sp)
|.define SAVE_PC, 12(sp)
|.define SAVE_MULTRES, 8(sp)
|// Next frame lr: 4(sp)
|// Back chain for sp: 0(sp) <-- sp while in interpreter
|
|.macro save_, reg; evstdd reg, SAVE_..reg; .endmacro
|.macro rest_, reg; evldd reg, SAVE_..reg; .endmacro
|
|.macro saveregs
| stwu sp, -CFRAME_SPACE(sp)
| save_ r14; save_ r15; save_ r16; save_ r17; save_ r18; save_ r19
| mflr r0; mfcr r12
| save_ r20; save_ r21; save_ r22; save_ r23; save_ r24; save_ r25
| stw r0, SAVE_LR; stw r12, SAVE_CR
| save_ r26; save_ r27; save_ r28; save_ r29; save_ r30; save_ r31
|.endmacro
|
|.macro restoreregs
| lwz r0, SAVE_LR; lwz r12, SAVE_CR
| rest_ r14; rest_ r15; rest_ r16; rest_ r17; rest_ r18; rest_ r19
| mtlr r0; mtcrf 0x38, r12
| rest_ r20; rest_ r21; rest_ r22; rest_ r23; rest_ r24; rest_ r25
| rest_ r26; rest_ r27; rest_ r28; rest_ r29; rest_ r30; rest_ r31
| addi sp, sp, CFRAME_SPACE
|.endmacro
|
|// Type definitions. Some of these are only used for documentation.
|.type L, lua_State, LREG
|.type GL, global_State
|.type TVALUE, TValue
|.type GCOBJ, GCobj
|.type STR, GCstr
|.type TAB, GCtab
|.type LFUNC, GCfuncL
|.type CFUNC, GCfuncC
|.type PROTO, GCproto
|.type UPVAL, GCupval
|.type NODE, Node
|.type NARGS8, int
|.type TRACE, GCtrace
|
|//-----------------------------------------------------------------------
|
|// These basic macros should really be part of DynASM.
|.macro srwi, rx, ry, n; rlwinm rx, ry, 32-n, n, 31; .endmacro
|.macro slwi, rx, ry, n; rlwinm rx, ry, n, 0, 31-n; .endmacro
|.macro rotlwi, rx, ry, n; rlwinm rx, ry, n, 0, 31; .endmacro
|.macro rotlw, rx, ry, rn; rlwnm rx, ry, rn, 0, 31; .endmacro
|.macro subi, rx, ry, i; addi rx, ry, -i; .endmacro
|
|// Trap for not-yet-implemented parts.
|.macro NYI; tw 4, sp, sp; .endmacro
|
|//-----------------------------------------------------------------------
|
|// Access to frame relative to BASE.
|.define FRAME_PC, -8
|.define FRAME_FUNC, -4
|
|// Instruction decode.
|.macro decode_OP4, dst, ins; rlwinm dst, ins, 2, 22, 29; .endmacro
|.macro decode_RA8, dst, ins; rlwinm dst, ins, 27, 21, 28; .endmacro
|.macro decode_RB8, dst, ins; rlwinm dst, ins, 11, 21, 28; .endmacro
|.macro decode_RC8, dst, ins; rlwinm dst, ins, 19, 21, 28; .endmacro
|.macro decode_RD8, dst, ins; rlwinm dst, ins, 19, 13, 28; .endmacro
|
|.macro decode_OP1, dst, ins; rlwinm dst, ins, 0, 24, 31; .endmacro
|.macro decode_RD4, dst, ins; rlwinm dst, ins, 18, 14, 29; .endmacro
|
|// Instruction fetch.
|.macro ins_NEXT1
| lwz INS, 0(PC)
| addi PC, PC, 4
|.endmacro
|// Instruction decode+dispatch.
|.macro ins_NEXT2
| decode_OP4 TMP1, INS
| decode_RB8 RB, INS
| decode_RD8 RD, INS
| lwzx TMP0, DISPATCH, TMP1
| decode_RA8 RA, INS
| decode_RC8 RC, INS
| mtctr TMP0
| bctr
|.endmacro
|.macro ins_NEXT
| ins_NEXT1
| ins_NEXT2
|.endmacro
|
|// Instruction footer.
|.if 1
| // Replicated dispatch. Less unpredictable branches, but higher I-Cache use.
| .define ins_next, ins_NEXT
| .define ins_next_, ins_NEXT
| .define ins_next1, ins_NEXT1
| .define ins_next2, ins_NEXT2
|.else
| // Common dispatch. Lower I-Cache use, only one (very) unpredictable branch.
| // Affects only certain kinds of benchmarks (and only with -j off).
| .macro ins_next
| b ->ins_next
| .endmacro
| .macro ins_next1
| .endmacro
| .macro ins_next2
| b ->ins_next
| .endmacro
| .macro ins_next_
| ->ins_next:
| ins_NEXT
| .endmacro
|.endif
|
|// Call decode and dispatch.
|.macro ins_callt
| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
| lwz PC, LFUNC:RB->pc
| lwz INS, 0(PC)
| addi PC, PC, 4
| decode_OP4 TMP1, INS
| decode_RA8 RA, INS
| lwzx TMP0, DISPATCH, TMP1
| add RA, RA, BASE
| mtctr TMP0
| bctr
|.endmacro
|
|.macro ins_call
| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
| stw PC, FRAME_PC(BASE)
| ins_callt
|.endmacro
|
|//-----------------------------------------------------------------------
|
|// Macros to test operand types.
|.macro checknum, reg; evcmpltu reg, TISNUM; .endmacro
|.macro checkstr, reg; evcmpeq reg, TISSTR; .endmacro
|.macro checktab, reg; evcmpeq reg, TISTAB; .endmacro
|.macro checkfunc, reg; evcmpeq reg, TISFUNC; .endmacro
|.macro checknil, reg; evcmpeq reg, TISNIL; .endmacro
|.macro checkok, label; blt label; .endmacro
|.macro checkfail, label; bge label; .endmacro
|.macro checkanyfail, label; bns label; .endmacro
|.macro checkallok, label; bso label; .endmacro
|
|.macro branch_RD
| srwi TMP0, RD, 1
| add PC, PC, TMP0
| addis PC, PC, -(BCBIAS_J*4 >> 16)
|.endmacro
|
|// Assumes DISPATCH is relative to GL.
#define DISPATCH_GL(field) (GG_DISP2G + (int)offsetof(global_State, field))
#define DISPATCH_J(field) (GG_DISP2J + (int)offsetof(jit_State, field))
|
#define PC2PROTO(field) ((int)offsetof(GCproto, field)-(int)sizeof(GCproto))
|
|.macro hotloop
| NYI
|.endmacro
|
|.macro hotcall
| NYI
|.endmacro
|
|// Set current VM state. Uses TMP0.
|.macro li_vmstate, st; li TMP0, ~LJ_VMST_..st; .endmacro
|.macro st_vmstate; stw TMP0, DISPATCH_GL(vmstate)(DISPATCH); .endmacro
|
|// Move table write barrier back. Overwrites mark and tmp.
|.macro barrierback, tab, mark, tmp
| lwz tmp, DISPATCH_GL(gc.grayagain)(DISPATCH)
| // Assumes LJ_GC_BLACK is 0x04.
| rlwinm mark, mark, 0, 30, 28 // black2gray(tab)
| stw tab, DISPATCH_GL(gc.grayagain)(DISPATCH)
| stb mark, tab->marked
| stw tmp, tab->gclist
|.endmacro
|
|//-----------------------------------------------------------------------
/* Generate subroutines used by opcodes and other parts of the VM. */
/* The .code_sub section should be last to help static branch prediction. */
static void build_subroutines(BuildCtx *ctx)
{
|.code_sub
|
|//-----------------------------------------------------------------------
|//-- Return handling ----------------------------------------------------
|//-----------------------------------------------------------------------
|
|->vm_returnp:
| // See vm_return. Also: TMP2 = previous base.
| andi. TMP0, PC, FRAME_P
| evsplati TMP1, LJ_TTRUE
| beq ->cont_dispatch
|
| // Return from pcall or xpcall fast func.
| lwz PC, FRAME_PC(TMP2) // Fetch PC of previous frame.
| mr BASE, TMP2 // Restore caller base.
| // Prepending may overwrite the pcall frame, so do it at the end.
| stwu TMP1, FRAME_PC(RA) // Prepend true to results.
|
|->vm_returnc:
| addi RD, RD, 8 // RD = (nresults+1)*8.
| andi. TMP0, PC, FRAME_TYPE
| cmpwi cr1, RD, 0
| li CRET1, LUA_YIELD
| beq cr1, ->vm_unwind_c_eh
| mr MULTRES, RD
| beq ->BC_RET_Z // Handle regular return to Lua.
|
|->vm_return:
| // BASE = base, RA = resultptr, RD/MULTRES = (nresults+1)*8, PC = return
| // TMP0 = PC & FRAME_TYPE
| cmpwi TMP0, FRAME_C
| rlwinm TMP2, PC, 0, 0, 28
| li_vmstate C
| sub TMP2, BASE, TMP2 // TMP2 = previous base.
| bne ->vm_returnp
|
| addic. TMP1, RD, -8
| stw TMP2, L->base
| lwz TMP2, SAVE_NRES
| subi BASE, BASE, 8
| st_vmstate
| slwi TMP2, TMP2, 3
| beq >2
|1:
| addic. TMP1, TMP1, -8
| evldd TMP0, 0(RA)
| addi RA, RA, 8
| evstdd TMP0, 0(BASE)
| addi BASE, BASE, 8
| bne <1
|
|2:
| cmpw TMP2, RD // More/less results wanted?
| bne >6
|3:
| stw BASE, L->top // Store new top.
|
|->vm_leave_cp:
| lwz TMP0, SAVE_CFRAME // Restore previous C frame.
| li CRET1, 0 // Ok return status for vm_pcall.
| stw TMP0, L->cframe
|
|->vm_leave_unw:
| restoreregs
| blr
|
|6:
| ble >7 // Less results wanted?
| // More results wanted. Check stack size and fill up results with nil.
| lwz TMP1, L->maxstack
| cmplw BASE, TMP1
| bge >8
| evstdd TISNIL, 0(BASE)
| addi RD, RD, 8
| addi BASE, BASE, 8
| b <2
|
|7: // Less results wanted.
| sub TMP0, RD, TMP2
| cmpwi TMP2, 0 // LUA_MULTRET+1 case?
| sub TMP0, BASE, TMP0 // Subtract the difference.
| iseleq BASE, BASE, TMP0 // Either keep top or shrink it.
| b <3
|
|8: // Corner case: need to grow stack for filling up results.
| // This can happen if:
| // - A C function grows the stack (a lot).
| // - The GC shrinks the stack in between.
| // - A return back from a lua_call() with (high) nresults adjustment.
| stw BASE, L->top // Save current top held in BASE (yes).
| mr SAVE0, RD
| mr CARG2, TMP2
| mr CARG1, L
| bl extern lj_state_growstack // (lua_State *L, int n)
| lwz TMP2, SAVE_NRES
| mr RD, SAVE0
| slwi TMP2, TMP2, 3
| lwz BASE, L->top // Need the (realloced) L->top in BASE.
| b <2
|
|->vm_unwind_c: // Unwind C stack, return from vm_pcall.
| // (void *cframe, int errcode)
| mr sp, CARG1
| mr CRET1, CARG2
|->vm_unwind_c_eh: // Landing pad for external unwinder.
| lwz L, SAVE_L
| li TMP0, ~LJ_VMST_C
| lwz GL:TMP1, L->glref
| stw TMP0, GL:TMP1->vmstate
| b ->vm_leave_unw
|
|->vm_unwind_ff: // Unwind C stack, return from ff pcall.
| // (void *cframe)
| rlwinm sp, CARG1, 0, 0, 29
|->vm_unwind_ff_eh: // Landing pad for external unwinder.
| lwz L, SAVE_L
| evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
| evsplati TISFUNC, LJ_TFUNC
| lus TOBIT, 0x4338
| evsplati TISTAB, LJ_TTAB
| li TMP0, 0
| lwz BASE, L->base
| evmergelo TOBIT, TOBIT, TMP0
| lwz DISPATCH, L->glref // Setup pointer to dispatch table.
| evsplati TISSTR, LJ_TSTR
| li TMP1, LJ_TFALSE
| evsplati TISNIL, LJ_TNIL
| li_vmstate INTERP
| lwz PC, FRAME_PC(BASE) // Fetch PC of previous frame.
| la RA, -8(BASE) // Results start at BASE-8.
| addi DISPATCH, DISPATCH, GG_G2DISP
| stw TMP1, 0(RA) // Prepend false to error message.
| li RD, 16 // 2 results: false + error message.
| st_vmstate
| b ->vm_returnc
|
|//-----------------------------------------------------------------------
|//-- Grow stack for calls -----------------------------------------------
|//-----------------------------------------------------------------------
|
|->vm_growstack_c: // Grow stack for C function.
| li CARG2, LUA_MINSTACK
| b >2
|
|->vm_growstack_l: // Grow stack for Lua function.
| // BASE = new base, RA = BASE+framesize*8, RC = nargs*8, PC = first PC
| add RC, BASE, RC
| sub RA, RA, BASE
| stw BASE, L->base
| addi PC, PC, 4 // Must point after first instruction.
| stw RC, L->top
| srwi CARG2, RA, 3
|2:
| // L->base = new base, L->top = top
| stw PC, SAVE_PC
| mr CARG1, L
| bl extern lj_state_growstack // (lua_State *L, int n)
| lwz BASE, L->base
| lwz RC, L->top
| lwz LFUNC:RB, FRAME_FUNC(BASE)
| sub RC, RC, BASE
| // BASE = new base, RB = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
| ins_callt // Just retry the call.
|
|//-----------------------------------------------------------------------
|//-- Entry points into the assembler VM ---------------------------------
|//-----------------------------------------------------------------------
|
|->vm_resume: // Setup C frame and resume thread.
| // (lua_State *L, TValue *base, int nres1 = 0, ptrdiff_t ef = 0)
| saveregs
| mr L, CARG1
| lwz DISPATCH, L->glref // Setup pointer to dispatch table.
| mr BASE, CARG2
| lbz TMP1, L->status
| stw L, SAVE_L
| li PC, FRAME_CP
| addi TMP0, sp, CFRAME_RESUME
| addi DISPATCH, DISPATCH, GG_G2DISP
| stw CARG3, SAVE_NRES
| cmplwi TMP1, 0
| stw CARG3, SAVE_ERRF
| stw TMP0, L->cframe
| stw CARG3, SAVE_CFRAME
| stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
| beq >3
|
| // Resume after yield (like a return).
| mr RA, BASE
| lwz BASE, L->base
| evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
| lwz TMP1, L->top
| evsplati TISFUNC, LJ_TFUNC
| lus TOBIT, 0x4338
| evsplati TISTAB, LJ_TTAB
| lwz PC, FRAME_PC(BASE)
| li TMP2, 0
| evsplati TISSTR, LJ_TSTR
| sub RD, TMP1, BASE
| evmergelo TOBIT, TOBIT, TMP2
| stb CARG3, L->status
| andi. TMP0, PC, FRAME_TYPE
| li_vmstate INTERP
| addi RD, RD, 8
| evsplati TISNIL, LJ_TNIL
| mr MULTRES, RD
| st_vmstate
| beq ->BC_RET_Z
| b ->vm_return
|
|->vm_pcall: // Setup protected C frame and enter VM.
| // (lua_State *L, TValue *base, int nres1, ptrdiff_t ef)
| saveregs
| li PC, FRAME_CP
| stw CARG4, SAVE_ERRF
| b >1
|
|->vm_call: // Setup C frame and enter VM.
| // (lua_State *L, TValue *base, int nres1)
| saveregs
| li PC, FRAME_C
|
|1: // Entry point for vm_pcall above (PC = ftype).
| lwz TMP1, L:CARG1->cframe
| stw CARG3, SAVE_NRES
| mr L, CARG1
| stw CARG1, SAVE_L
| mr BASE, CARG2
| stw sp, L->cframe // Add our C frame to cframe chain.
| lwz DISPATCH, L->glref // Setup pointer to dispatch table.
| stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
| stw TMP1, SAVE_CFRAME
| addi DISPATCH, DISPATCH, GG_G2DISP
|
|3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
| lwz TMP2, L->base // TMP2 = old base (used in vmeta_call).
| evsplati TISNUM, LJ_TISNUM+1 // Setup type comparison constants.
| lwz TMP1, L->top
| evsplati TISFUNC, LJ_TFUNC
| add PC, PC, BASE
| evsplati TISTAB, LJ_TTAB
| lus TOBIT, 0x4338
| li TMP0, 0
| sub PC, PC, TMP2 // PC = frame delta + frame type
| evsplati TISSTR, LJ_TSTR
| sub NARGS8:RC, TMP1, BASE
| evmergelo TOBIT, TOBIT, TMP0
| li_vmstate INTERP
| evsplati TISNIL, LJ_TNIL
| st_vmstate
|
|->vm_call_dispatch:
| // TMP2 = old base, BASE = new base, RC = nargs*8, PC = caller PC
| li TMP0, -8
| evlddx LFUNC:RB, BASE, TMP0
| checkfunc LFUNC:RB
| checkfail ->vmeta_call
|
|->vm_call_dispatch_f:
| ins_call
| // BASE = new base, RB = func, RC = nargs*8, PC = caller PC
|
|->vm_cpcall: // Setup protected C frame, call C.
| // (lua_State *L, lua_CFunction func, void *ud, lua_CPFunction cp)
| saveregs
| mr L, CARG1
| lwz TMP0, L:CARG1->stack
| stw CARG1, SAVE_L
| lwz TMP1, L->top
| stw CARG1, SAVE_PC // Any value outside of bytecode is ok.
| sub TMP0, TMP0, TMP1 // Compute -savestack(L, L->top).
| lwz TMP1, L->cframe
| stw sp, L->cframe // Add our C frame to cframe chain.
| li TMP2, 0
| stw TMP0, SAVE_NRES // Neg. delta means cframe w/o frame.
| stw TMP2, SAVE_ERRF // No error function.
| stw TMP1, SAVE_CFRAME
| mtctr CARG4
| bctrl // (lua_State *L, lua_CFunction func, void *ud)
| mr. BASE, CRET1
| lwz DISPATCH, L->glref // Setup pointer to dispatch table.
| li PC, FRAME_CP
| addi DISPATCH, DISPATCH, GG_G2DISP
| bne <3 // Else continue with the call.
| b ->vm_leave_cp // No base? Just remove C frame.
|
|//-----------------------------------------------------------------------
|//-- Metamethod handling ------------------------------------------------
|//-----------------------------------------------------------------------
|
|// The lj_meta_* functions (except for lj_meta_cat) don't reallocate the
|// stack, so BASE doesn't need to be reloaded across these calls.
|
|//-- Continuation dispatch ----------------------------------------------
|
|->cont_dispatch:
| // BASE = meta base, RA = resultptr, RD = (nresults+1)*8
| lwz TMP0, -12(BASE) // Continuation.
| mr RB, BASE
| mr BASE, TMP2 // Restore caller BASE.
| lwz LFUNC:TMP1, FRAME_FUNC(TMP2)
| cmplwi TMP0, 0
| lwz PC, -16(RB) // Restore PC from [cont|PC].
| beq >1
| subi TMP2, RD, 8
| lwz TMP1, LFUNC:TMP1->pc
| evstddx TISNIL, RA, TMP2 // Ensure one valid arg.
| lwz KBASE, PC2PROTO(k)(TMP1)
| // BASE = base, RA = resultptr, RB = meta base
| mtctr TMP0
| bctr // Jump to continuation.
|
|1: // Tail call from C function.
| subi TMP1, RB, 16
| sub RC, TMP1, BASE
| b ->vm_call_tail
|
|->cont_cat: // RA = resultptr, RB = meta base
| lwz INS, -4(PC)
| subi CARG2, RB, 16
| decode_RB8 SAVE0, INS
| evldd TMP0, 0(RA)
| add TMP1, BASE, SAVE0
| stw BASE, L->base
| cmplw TMP1, CARG2
| sub CARG3, CARG2, TMP1
| decode_RA8 RA, INS
| evstdd TMP0, 0(CARG2)
| bne ->BC_CAT_Z
| evstddx TMP0, BASE, RA
| b ->cont_nop
|
|//-- Table indexing metamethods -----------------------------------------
|
|->vmeta_tgets1:
| evmergelo STR:RC, TISSTR, STR:RC
| la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
| decode_RB8 RB, INS
| evstdd STR:RC, 0(CARG3)
| add CARG2, BASE, RB
| b >1
|
|->vmeta_tgets:
| evmergelo TAB:RB, TISTAB, TAB:RB
| la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
| evmergelo STR:RC, TISSTR, STR:RC
| evstdd TAB:RB, 0(CARG2)
| la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
| evstdd STR:RC, 0(CARG3)
| b >1
|
|->vmeta_tgetb: // TMP0 = index
| efdcfsi TMP0, TMP0
| decode_RB8 RB, INS
| la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
| add CARG2, BASE, RB
| evstdd TMP0, 0(CARG3)
| b >1
|
|->vmeta_tgetv:
| decode_RB8 RB, INS
| decode_RC8 RC, INS
| add CARG2, BASE, RB
| add CARG3, BASE, RC
|1:
| stw BASE, L->base
| mr CARG1, L
| stw PC, SAVE_PC
| bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
| // Returns TValue * (finished) or NULL (metamethod).
| cmplwi CRET1, 0
| beq >3
| evldd TMP0, 0(CRET1)
| evstddx TMP0, BASE, RA
| ins_next
|
|3: // Call __index metamethod.
| // BASE = base, L->top = new base, stack = cont/func/t/k
| subfic TMP1, BASE, FRAME_CONT
| lwz BASE, L->top
| stw PC, -16(BASE) // [cont|PC]
| add PC, TMP1, BASE
| lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
| li NARGS8:RC, 16 // 2 args for func(t, k).
| b ->vm_call_dispatch_f
|
|//-----------------------------------------------------------------------
|
|->vmeta_tsets1:
| evmergelo STR:RC, TISSTR, STR:RC
| la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
| decode_RB8 RB, INS
| evstdd STR:RC, 0(CARG3)
| add CARG2, BASE, RB
| b >1
|
|->vmeta_tsets:
| evmergelo TAB:RB, TISTAB, TAB:RB
| la CARG2, DISPATCH_GL(tmptv)(DISPATCH)
| evmergelo STR:RC, TISSTR, STR:RC
| evstdd TAB:RB, 0(CARG2)
| la CARG3, DISPATCH_GL(tmptv2)(DISPATCH)
| evstdd STR:RC, 0(CARG3)
| b >1
|
|->vmeta_tsetb: // TMP0 = index
| efdcfsi TMP0, TMP0
| decode_RB8 RB, INS
| la CARG3, DISPATCH_GL(tmptv)(DISPATCH)
| add CARG2, BASE, RB
| evstdd TMP0, 0(CARG3)
| b >1
|
|->vmeta_tsetv:
| decode_RB8 RB, INS
| decode_RC8 RC, INS
| add CARG2, BASE, RB
| add CARG3, BASE, RC
|1:
| stw BASE, L->base
| mr CARG1, L
| stw PC, SAVE_PC
| bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
| // Returns TValue * (finished) or NULL (metamethod).
| cmplwi CRET1, 0
| evlddx TMP0, BASE, RA
| beq >3
| // NOBARRIER: lj_meta_tset ensures the table is not black.
| evstdd TMP0, 0(CRET1)
| ins_next
|
|3: // Call __newindex metamethod.
| // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
| subfic TMP1, BASE, FRAME_CONT
| lwz BASE, L->top
| stw PC, -16(BASE) // [cont|PC]
| add PC, TMP1, BASE
| lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
| li NARGS8:RC, 24 // 3 args for func(t, k, v)
| evstdd TMP0, 16(BASE) // Copy value to third argument.
| b ->vm_call_dispatch_f
|
|//-- Comparison metamethods ---------------------------------------------
|
|->vmeta_comp:
| mr CARG1, L
| subi PC, PC, 4
| add CARG2, BASE, RA
| stw PC, SAVE_PC
| add CARG3, BASE, RD
| stw BASE, L->base
| decode_OP1 CARG4, INS
| bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
| // Returns 0/1 or TValue * (metamethod).
|3:
| cmplwi CRET1, 1
| bgt ->vmeta_binop
|4:
| lwz INS, 0(PC)
| addi PC, PC, 4
| decode_RD4 TMP2, INS
| addis TMP3, PC, -(BCBIAS_J*4 >> 16)
| add TMP2, TMP2, TMP3
| isellt PC, PC, TMP2
|->cont_nop:
| ins_next
|
|->cont_ra: // RA = resultptr
| lwz INS, -4(PC)
| evldd TMP0, 0(RA)
| decode_RA8 TMP1, INS
| evstddx TMP0, BASE, TMP1
| b ->cont_nop
|
|->cont_condt: // RA = resultptr
| lwz TMP0, 0(RA)
| li TMP1, LJ_TTRUE
| cmplw TMP1, TMP0 // Branch if result is true.
| b <4
|
|->cont_condf: // RA = resultptr
| lwz TMP0, 0(RA)
| li TMP1, LJ_TFALSE
| cmplw TMP0, TMP1 // Branch if result is false.
| b <4
|
|->vmeta_equal:
| // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
| subi PC, PC, 4
| stw BASE, L->base
| mr CARG1, L
| stw PC, SAVE_PC
| bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
| // Returns 0/1 or TValue * (metamethod).
| b <3
|
|//-- Arithmetic metamethods ---------------------------------------------
|
|->vmeta_arith_vn:
| add CARG3, BASE, RB
| add CARG4, KBASE, RC
| b >1
|
|->vmeta_arith_nv:
| add CARG3, KBASE, RC
| add CARG4, BASE, RB
| b >1
|
|->vmeta_unm:
| add CARG3, BASE, RD
| mr CARG4, CARG3
| b >1
|
|->vmeta_arith_vv:
| add CARG3, BASE, RB
| add CARG4, BASE, RC
|1:
| add CARG2, BASE, RA
| stw BASE, L->base
| mr CARG1, L
| stw PC, SAVE_PC
| decode_OP1 CARG5, INS // Caveat: CARG5 overlaps INS.
| bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
| // Returns NULL (finished) or TValue * (metamethod).
| cmplwi CRET1, 0
| beq ->cont_nop
|
| // Call metamethod for binary op.
|->vmeta_binop:
| // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
| sub TMP1, CRET1, BASE
| stw PC, -16(CRET1) // [cont|PC]
| mr TMP2, BASE
| addi PC, TMP1, FRAME_CONT
| mr BASE, CRET1
| li NARGS8:RC, 16 // 2 args for func(o1, o2).
| b ->vm_call_dispatch
|
|->vmeta_len:
#if LJ_52
| mr SAVE0, CARG1
#endif
| add CARG2, BASE, RD
| stw BASE, L->base
| mr CARG1, L
| stw PC, SAVE_PC
| bl extern lj_meta_len // (lua_State *L, TValue *o)
| // Returns NULL (retry) or TValue * (metamethod base).
#if LJ_52
| cmplwi CRET1, 0
| bne ->vmeta_binop // Binop call for compatibility.
| mr CARG1, SAVE0
| b ->BC_LEN_Z
#else
| b ->vmeta_binop // Binop call for compatibility.
#endif
|
|//-- Call metamethod ----------------------------------------------------
|
|->vmeta_call: // Resolve and call __call metamethod.
| // TMP2 = old base, BASE = new base, RC = nargs*8
| mr CARG1, L
| stw TMP2, L->base // This is the callers base!
| subi CARG2, BASE, 8
| stw PC, SAVE_PC
| add CARG3, BASE, RC
| mr SAVE0, NARGS8:RC
| bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
| lwz LFUNC:RB, FRAME_FUNC(BASE) // Guaranteed to be a function here.
| addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
| ins_call
|
|->vmeta_callt: // Resolve __call for BC_CALLT.
| // BASE = old base, RA = new base, RC = nargs*8
| mr CARG1, L
| stw BASE, L->base
| subi CARG2, RA, 8
| stw PC, SAVE_PC
| add CARG3, RA, RC
| mr SAVE0, NARGS8:RC
| bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
| lwz TMP1, FRAME_PC(BASE)
| addi NARGS8:RC, SAVE0, 8 // Got one more argument now.
| lwz LFUNC:RB, FRAME_FUNC(RA) // Guaranteed to be a function here.
| b ->BC_CALLT_Z
|
|//-- Argument coercion for 'for' statement ------------------------------
|
|->vmeta_for:
| mr CARG1, L
| stw BASE, L->base
| mr CARG2, RA
| stw PC, SAVE_PC
| mr SAVE0, INS
| bl extern lj_meta_for // (lua_State *L, TValue *base)
|.if JIT
| decode_OP1 TMP0, SAVE0
|.endif
| decode_RA8 RA, SAVE0
|.if JIT
| cmpwi TMP0, BC_JFORI
|.endif
| decode_RD8 RD, SAVE0
|.if JIT
| beq =>BC_JFORI
|.endif
| b =>BC_FORI
|
|//-----------------------------------------------------------------------
|//-- Fast functions -----------------------------------------------------
|//-----------------------------------------------------------------------
|
|.macro .ffunc, name
|->ff_ .. name:
|.endmacro
|
|.macro .ffunc_1, name
|->ff_ .. name:
| cmplwi NARGS8:RC, 8
| evldd CARG1, 0(BASE)
| blt ->fff_fallback
|.endmacro
|
|.macro .ffunc_2, name
|->ff_ .. name:
| cmplwi NARGS8:RC, 16
| evldd CARG1, 0(BASE)
| evldd CARG2, 8(BASE)
| blt ->fff_fallback
|.endmacro
|
|.macro .ffunc_n, name
| .ffunc_1 name
| checknum CARG1
| checkfail ->fff_fallback
|.endmacro
|
|.macro .ffunc_nn, name
| .ffunc_2 name
| evmergehi TMP0, CARG1, CARG2
| checknum TMP0
| checkanyfail ->fff_fallback
|.endmacro
|
|// Inlined GC threshold check. Caveat: uses TMP0 and TMP1.
|.macro ffgccheck
| lwz TMP0, DISPATCH_GL(gc.total)(DISPATCH)
| lwz TMP1, DISPATCH_GL(gc.threshold)(DISPATCH)
| cmplw TMP0, TMP1
| bgel ->fff_gcstep
|.endmacro
|
|//-- Base library: checks -----------------------------------------------
|
|.ffunc assert
| cmplwi NARGS8:RC, 8
| evldd TMP0, 0(BASE)
| blt ->fff_fallback
| evaddw TMP1, TISNIL, TISNIL // Synthesize LJ_TFALSE.
| la RA, -8(BASE)
| evcmpltu cr1, TMP0, TMP1
| lwz PC, FRAME_PC(BASE)
| bge cr1, ->fff_fallback
| evstdd TMP0, 0(RA)
| addi RD, NARGS8:RC, 8 // Compute (nresults+1)*8.
| beq ->fff_res // Done if exactly 1 argument.
| li TMP1, 8
| subi RC, RC, 8
|1:
| cmplw TMP1, RC
| evlddx TMP0, BASE, TMP1
| evstddx TMP0, RA, TMP1
| addi TMP1, TMP1, 8
| bne <1
| b ->fff_res
|
|.ffunc type
| cmplwi NARGS8:RC, 8
| lwz CARG1, 0(BASE)
| blt ->fff_fallback
| li TMP2, ~LJ_TNUMX
| cmplw CARG1, TISNUM
| not TMP1, CARG1
| isellt TMP1, TMP2, TMP1
| slwi TMP1, TMP1, 3
| la TMP2, CFUNC:RB->upvalue
| evlddx STR:CRET1, TMP2, TMP1
| b ->fff_restv
|
|//-- Base library: getters and setters ---------------------------------
|
|.ffunc_1 getmetatable
| checktab CARG1
| evmergehi TMP1, CARG1, CARG1
| checkfail >6
|1: // Field metatable must be at same offset for GCtab and GCudata!
| lwz TAB:RB, TAB:CARG1->metatable
|2:
| evmr CRET1, TISNIL
| cmplwi TAB:RB, 0
| lwz STR:RC, DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])(DISPATCH)
| beq ->fff_restv
| lwz TMP0, TAB:RB->hmask
| evmergelo CRET1, TISTAB, TAB:RB // Use metatable as default result.
| lwz TMP1, STR:RC->hash
| lwz NODE:TMP2, TAB:RB->node
| evmergelo STR:RC, TISSTR, STR:RC
| and TMP1, TMP1, TMP0 // idx = str->hash & tab->hmask
| slwi TMP0, TMP1, 5
| slwi TMP1, TMP1, 3
| sub TMP1, TMP0, TMP1
| add NODE:TMP2, NODE:TMP2, TMP1 // node = tab->node + (idx*32-idx*8)
|3: // Rearranged logic, because we expect _not_ to find the key.
| evldd TMP0, NODE:TMP2->key
| evldd TMP1, NODE:TMP2->val
| evcmpeq TMP0, STR:RC
| lwz NODE:TMP2, NODE:TMP2->next
| checkallok >5
| cmplwi NODE:TMP2, 0
| beq ->fff_restv // Not found, keep default result.
| b <3
|5:
| checknil TMP1