-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvm_arm.dasc
4487 lines (4420 loc) · 120 KB
/
vm_arm.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 ARM CPUs.
|// Bytecode interpreter, fast functions and helper functions.
|// Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
|
|.arch arm
|.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.
|
|// The following must be C callee-save.
|.define MASKR8, r4 // 255*8 constant for fast bytecode decoding.
|.define KBASE, r5 // Constants of current Lua function.
|.define PC, r6 // Next PC.
|.define DISPATCH, r7 // Opcode dispatch table.
|.define LREG, r8 // Register holding lua_State (also in SAVE_L).
|
|// C callee-save in EABI, but often refetched. Temporary in iOS 3.0+.
|.define BASE, r9 // Base of current Lua stack frame.
|
|// The following temporaries are not saved across C calls, except for RA/RC.
|.define RA, r10 // Callee-save.
|.define RC, r11 // Callee-save.
|.define RB, r12
|.define OP, r12 // Overlaps RB, must not be lr.
|.define INS, lr
|
|// Calling conventions. Also used as temporaries.
|.define CARG1, r0
|.define CARG2, r1
|.define CARG3, r2
|.define CARG4, r3
|.define CARG12, r0 // For 1st soft-fp double.
|.define CARG34, r2 // For 2nd soft-fp double.
|
|.define CRET1, r0
|.define CRET2, r1
|
|// Stack layout while in interpreter. Must match with lj_frame.h.
|.define SAVE_R4, [sp, #28]
|.define CFRAME_SPACE, #28
|.define SAVE_ERRF, [sp, #24]
|.define SAVE_NRES, [sp, #20]
|.define SAVE_CFRAME, [sp, #16]
|.define SAVE_L, [sp, #12]
|.define SAVE_PC, [sp, #8]
|.define SAVE_MULTRES, [sp, #4]
|.define ARG5, [sp]
|
|.define TMPDhi, [sp, #4]
|.define TMPDlo, [sp]
|.define TMPD, [sp]
|.define TMPDp, sp
|
|.if FPU
|.macro saveregs
| push {r5, r6, r7, r8, r9, r10, r11, lr}
| vpush {d8-d15}
| sub sp, sp, CFRAME_SPACE+4
| str r4, SAVE_R4
|.endmacro
|.macro restoreregs_ret
| ldr r4, SAVE_R4
| add sp, sp, CFRAME_SPACE+4
| vpop {d8-d15}
| pop {r5, r6, r7, r8, r9, r10, r11, pc}
|.endmacro
|.else
|.macro saveregs
| push {r4, r5, r6, r7, r8, r9, r10, r11, lr}
| sub sp, sp, CFRAME_SPACE
|.endmacro
|.macro restoreregs_ret
| add sp, sp, CFRAME_SPACE
| pop {r4, r5, r6, r7, r8, r9, r10, r11, pc}
|.endmacro
|.endif
|
|// 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
|
|//-----------------------------------------------------------------------
|
|// Trap for not-yet-implemented parts.
|.macro NYI; ud; .endmacro
|
|//-----------------------------------------------------------------------
|
|// Access to frame relative to BASE.
|.define FRAME_FUNC, #-8
|.define FRAME_PC, #-4
|
|.macro decode_RA8, dst, ins; and dst, MASKR8, ins, lsr #5; .endmacro
|.macro decode_RB8, dst, ins; and dst, MASKR8, ins, lsr #21; .endmacro
|.macro decode_RC8, dst, ins; and dst, MASKR8, ins, lsr #13; .endmacro
|.macro decode_RD, dst, ins; lsr dst, ins, #16; .endmacro
|.macro decode_OP, dst, ins; and dst, ins, #255; .endmacro
|
|// Instruction fetch.
|.macro ins_NEXT1
| ldrb OP, [PC]
|.endmacro
|.macro ins_NEXT2
| ldr INS, [PC], #4
|.endmacro
|// Instruction decode+dispatch.
|.macro ins_NEXT3
| ldr OP, [DISPATCH, OP, lsl #2]
| decode_RA8 RA, INS
| decode_RD RC, INS
| bx OP
|.endmacro
|.macro ins_NEXT
| ins_NEXT1
| ins_NEXT2
| ins_NEXT3
|.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
| .define ins_next3, ins_NEXT3
|.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
| .endmacro
| .macro ins_next3
| b ->ins_next
| .endmacro
| .macro ins_next_
| ->ins_next:
| ins_NEXT
| .endmacro
|.endif
|
|// Avoid register name substitution for field name.
#define field_pc pc
|
|// Call decode and dispatch.
|.macro ins_callt
| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, FRAME_PC(BASE) = PC
| ldr PC, LFUNC:CARG3->field_pc
| ldrb OP, [PC] // STALL: load PC. early PC.
| ldr INS, [PC], #4
| ldr OP, [DISPATCH, OP, lsl #2] // STALL: load OP. early OP.
| decode_RA8 RA, INS
| add RA, RA, BASE
| bx OP
|.endmacro
|
|.macro ins_call
| // BASE = new base, CARG3 = LFUNC/CFUNC, RC = nargs*8, PC = caller PC
| str PC, [BASE, FRAME_PC]
| ins_callt // STALL: locked PC.
|.endmacro
|
|//-----------------------------------------------------------------------
|
|// Macros to test operand types.
|.macro checktp, reg, tp; cmn reg, #-tp; .endmacro
|.macro checktpeq, reg, tp; cmneq reg, #-tp; .endmacro
|.macro checktpne, reg, tp; cmnne reg, #-tp; .endmacro
|.macro checkstr, reg, target; checktp reg, LJ_TSTR; bne target; .endmacro
|.macro checktab, reg, target; checktp reg, LJ_TTAB; bne target; .endmacro
|.macro checkfunc, reg, target; checktp reg, LJ_TFUNC; bne target; .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 hotcheck, delta
| lsr CARG1, PC, #1
| and CARG1, CARG1, #126
| sub CARG1, CARG1, #-GG_DISP2HOT
| ldrh CARG2, [DISPATCH, CARG1]
| subs CARG2, CARG2, #delta
| strh CARG2, [DISPATCH, CARG1]
|.endmacro
|
|.macro hotloop
| hotcheck HOTCOUNT_LOOP
| blo ->vm_hotloop
|.endmacro
|
|.macro hotcall
| hotcheck HOTCOUNT_CALL
| blo ->vm_hotcall
|.endmacro
|
|// Set current VM state.
|.macro mv_vmstate, reg, st; mvn reg, #LJ_VMST_..st; .endmacro
|.macro st_vmstate, reg; str reg, [DISPATCH, #DISPATCH_GL(vmstate)]; .endmacro
|
|// Move table write barrier back. Overwrites mark and tmp.
|.macro barrierback, tab, mark, tmp
| ldr tmp, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
| bic mark, mark, #LJ_GC_BLACK // black2gray(tab)
| str tab, [DISPATCH, #DISPATCH_GL(gc.grayagain)]
| strb mark, tab->marked
| str tmp, tab->gclist
|.endmacro
|
|.macro .IOS, a, b
|.if IOS
| a, b
|.endif
|.endmacro
|
|//-----------------------------------------------------------------------
#if !LJ_DUALNUM
#error "Only dual-number mode supported for ARM target"
#endif
/* 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: RB = previous base.
| tst PC, #FRAME_P
| beq ->cont_dispatch
|
| // Return from pcall or xpcall fast func.
| ldr PC, [RB, FRAME_PC] // Fetch PC of previous frame.
| mvn CARG2, #~LJ_TTRUE
| mov BASE, RB
| // Prepending may overwrite the pcall frame, so do it at the end.
| str CARG2, [RA, FRAME_PC] // Prepend true to results.
| sub RA, RA, #8
|
|->vm_returnc:
| adds RC, RC, #8 // RC = (nresults+1)*8.
| mov CRET1, #LUA_YIELD
| beq ->vm_unwind_c_eh
| str RC, SAVE_MULTRES
| ands CARG1, PC, #FRAME_TYPE
| beq ->BC_RET_Z // Handle regular return to Lua.
|
|->vm_return:
| // BASE = base, RA = resultptr, RC/MULTRES = (nresults+1)*8, PC = return
| // CARG1 = PC & FRAME_TYPE
| bic RB, PC, #FRAME_TYPEP
| cmp CARG1, #FRAME_C
| sub RB, BASE, RB // RB = previous base.
| bne ->vm_returnp
|
| str RB, L->base
| ldr KBASE, SAVE_NRES
| mv_vmstate CARG4, C
| sub BASE, BASE, #8
| subs CARG3, RC, #8
| lsl KBASE, KBASE, #3 // KBASE = (nresults_wanted+1)*8
| st_vmstate CARG4
| beq >2
|1:
| subs CARG3, CARG3, #8
| ldrd CARG12, [RA], #8
| strd CARG12, [BASE], #8
| bne <1
|2:
| cmp KBASE, RC // More/less results wanted?
| bne >6
|3:
| str BASE, L->top // Store new top.
|
|->vm_leave_cp:
| ldr RC, SAVE_CFRAME // Restore previous C frame.
| mov CRET1, #0 // Ok return status for vm_pcall.
| str RC, L->cframe
|
|->vm_leave_unw:
| restoreregs_ret
|
|6:
| blt >7 // Less results wanted?
| // More results wanted. Check stack size and fill up results with nil.
| ldr CARG3, L->maxstack
| mvn CARG2, #~LJ_TNIL
| cmp BASE, CARG3
| bhs >8
| str CARG2, [BASE, #4]
| add RC, RC, #8
| add BASE, BASE, #8
| b <2
|
|7: // Less results wanted.
| sub CARG1, RC, KBASE
| cmp KBASE, #0 // LUA_MULTRET+1 case?
| subne BASE, BASE, CARG1 // 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.
| str BASE, L->top // Save current top held in BASE (yes).
| mov CARG2, KBASE
| mov CARG1, L
| bl extern lj_state_growstack // (lua_State *L, int n)
| ldr 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)
| mov sp, CARG1
| mov CRET1, CARG2
|->vm_unwind_c_eh: // Landing pad for external unwinder.
| ldr L, SAVE_L
| mv_vmstate CARG4, C
| ldr GL:CARG3, L->glref
| str CARG4, GL:CARG3->vmstate
| b ->vm_leave_unw
|
|->vm_unwind_ff: // Unwind C stack, return from ff pcall.
| // (void *cframe)
| bic CARG1, CARG1, #~CFRAME_RAWMASK // Use two steps: bic sp is deprecated.
| mov sp, CARG1
|->vm_unwind_ff_eh: // Landing pad for external unwinder.
| ldr L, SAVE_L
| mov MASKR8, #255
| mov RC, #16 // 2 results: false + error message.
| lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
| ldr BASE, L->base
| ldr DISPATCH, L->glref // Setup pointer to dispatch table.
| mvn CARG1, #~LJ_TFALSE
| sub RA, BASE, #8 // Results start at BASE-8.
| ldr PC, [BASE, FRAME_PC] // Fetch PC of previous frame.
| add DISPATCH, DISPATCH, #GG_G2DISP
| mv_vmstate CARG2, INTERP
| str CARG1, [BASE, #-4] // Prepend false to error message.
| st_vmstate CARG2
| b ->vm_returnc
|
|//-----------------------------------------------------------------------
|//-- Grow stack for calls -----------------------------------------------
|//-----------------------------------------------------------------------
|
|->vm_growstack_c: // Grow stack for C function.
| // CARG1 = L
| mov 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
| mov CARG1, L
| str BASE, L->base
| add PC, PC, #4 // Must point after first instruction.
| str RC, L->top
| lsr CARG3, RA, #3
|2:
| // L->base = new base, L->top = top
| str PC, SAVE_PC
| bl extern lj_state_growstack // (lua_State *L, int n)
| ldr BASE, L->base
| ldr RC, L->top
| ldr LFUNC:CARG3, [BASE, FRAME_FUNC]
| sub NARGS8: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
| mov L, CARG1
| ldr DISPATCH, L:CARG1->glref // Setup pointer to dispatch table.
| mov BASE, CARG2
| add DISPATCH, DISPATCH, #GG_G2DISP
| str L, SAVE_L
| mov PC, #FRAME_CP
| str CARG3, SAVE_NRES
| add CARG2, sp, #CFRAME_RESUME
| ldrb CARG1, L->status
| str CARG3, SAVE_ERRF
| str CARG2, L->cframe
| str CARG3, SAVE_CFRAME
| cmp CARG1, #0
| str L, SAVE_PC // Any value outside of bytecode is ok.
| beq >3
|
| // Resume after yield (like a return).
| mov RA, BASE
| ldr BASE, L->base
| ldr CARG1, L->top
| mov MASKR8, #255
| strb CARG3, L->status
| sub RC, CARG1, BASE
| ldr PC, [BASE, FRAME_PC]
| lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
| mv_vmstate CARG2, INTERP
| add RC, RC, #8
| ands CARG1, PC, #FRAME_TYPE
| st_vmstate CARG2
| str RC, SAVE_MULTRES
| 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
| mov PC, #FRAME_CP
| str CARG4, SAVE_ERRF
| b >1
|
|->vm_call: // Setup C frame and enter VM.
| // (lua_State *L, TValue *base, int nres1)
| saveregs
| mov PC, #FRAME_C
|
|1: // Entry point for vm_pcall above (PC = ftype).
| ldr RC, L:CARG1->cframe
| str CARG3, SAVE_NRES
| mov L, CARG1
| str CARG1, SAVE_L
| mov BASE, CARG2
| str sp, L->cframe // Add our C frame to cframe chain.
| ldr DISPATCH, L->glref // Setup pointer to dispatch table.
| str CARG1, SAVE_PC // Any value outside of bytecode is ok.
| str RC, SAVE_CFRAME
| add DISPATCH, DISPATCH, #GG_G2DISP
|
|3: // Entry point for vm_cpcall/vm_resume (BASE = base, PC = ftype).
| ldr RB, L->base // RB = old base (for vmeta_call).
| ldr CARG1, L->top
| mov MASKR8, #255
| add PC, PC, BASE
| lsl MASKR8, MASKR8, #3 // MASKR8 = 255*8.
| sub PC, PC, RB // PC = frame delta + frame type
| mv_vmstate CARG2, INTERP
| sub NARGS8:RC, CARG1, BASE
| st_vmstate CARG2
|
|->vm_call_dispatch:
| // RB = old base, BASE = new base, RC = nargs*8, PC = caller PC
| ldrd CARG34, [BASE, FRAME_FUNC]
| checkfunc CARG4, ->vmeta_call
|
|->vm_call_dispatch_f:
| ins_call
| // BASE = new base, CARG3 = 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
| mov L, CARG1
| ldr RA, L:CARG1->stack
| str CARG1, SAVE_L
| ldr RB, L->top
| str CARG1, SAVE_PC // Any value outside of bytecode is ok.
| ldr RC, L->cframe
| sub RA, RA, RB // Compute -savestack(L, L->top).
| str sp, L->cframe // Add our C frame to cframe chain.
| mov RB, #0
| str RA, SAVE_NRES // Neg. delta means cframe w/o frame.
| str RB, SAVE_ERRF // No error function.
| str RC, SAVE_CFRAME
| blx CARG4 // (lua_State *L, lua_CFunction func, void *ud)
| ldr DISPATCH, L->glref // Setup pointer to dispatch table.
| movs BASE, CRET1
| mov PC, #FRAME_CP
| add DISPATCH, DISPATCH, #GG_G2DISP
| bne <3 // Else continue with the call.
| b ->vm_leave_cp // No base? Just remove C frame.
|
|//-----------------------------------------------------------------------
|//-- Metamethod handling ------------------------------------------------
|//-----------------------------------------------------------------------
|
|//-- Continuation dispatch ----------------------------------------------
|
|->cont_dispatch:
| // BASE = meta base, RA = resultptr, RC = (nresults+1)*8
| ldr LFUNC:CARG3, [RB, FRAME_FUNC]
| ldr CARG1, [BASE, #-16] // Get continuation.
| mov CARG4, BASE
| mov BASE, RB // Restore caller BASE.
|.if FFI
| cmp CARG1, #1
|.endif
| ldr PC, [CARG4, #-12] // Restore PC from [cont|PC].
| ldr CARG3, LFUNC:CARG3->field_pc
| mvn INS, #~LJ_TNIL
| add CARG2, RA, RC
| str INS, [CARG2, #-4] // Ensure one valid arg.
|.if FFI
| bls >1
|.endif
| ldr KBASE, [CARG3, #PC2PROTO(k)]
| // BASE = base, RA = resultptr, CARG4 = meta base
| bx CARG1
|
|.if FFI
|1:
| beq ->cont_ffi_callback // cont = 1: return from FFI callback.
| // cont = 0: tailcall from C function.
| ldr CARG3, [BASE, FRAME_FUNC]
| sub CARG4, CARG4, #16
| sub RC, CARG4, BASE
| b ->vm_call_tail
|.endif
|
|->cont_cat: // RA = resultptr, CARG4 = meta base
| ldr INS, [PC, #-4]
| sub CARG2, CARG4, #16
| ldrd CARG34, [RA]
| str BASE, L->base
| decode_RB8 RC, INS
| decode_RA8 RA, INS
| add CARG1, BASE, RC
| subs CARG1, CARG2, CARG1
| strdne CARG34, [CARG2]
| movne CARG3, CARG1
| bne ->BC_CAT_Z
| strd CARG34, [BASE, RA]
| b ->cont_nop
|
|//-- Table indexing metamethods -----------------------------------------
|
|->vmeta_tgets1:
| add CARG2, BASE, RB
| b >2
|
|->vmeta_tgets:
| sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
| mvn CARG4, #~LJ_TTAB
| str TAB:RB, [CARG2]
| str CARG4, [CARG2, #4]
|2:
| mvn CARG4, #~LJ_TSTR
| str STR:RC, TMPDlo
| str CARG4, TMPDhi
| mov CARG3, TMPDp
| b >1
|
|->vmeta_tgetb: // RC = index
| decode_RB8 RB, INS
| str RC, TMPDlo
| mvn CARG4, #~LJ_TISNUM
| add CARG2, BASE, RB
| str CARG4, TMPDhi
| mov CARG3, TMPDp
| b >1
|
|->vmeta_tgetv:
| add CARG2, BASE, RB
| add CARG3, BASE, RC
|1:
| str BASE, L->base
| mov CARG1, L
| str PC, SAVE_PC
| bl extern lj_meta_tget // (lua_State *L, TValue *o, TValue *k)
| // Returns TValue * (finished) or NULL (metamethod).
| .IOS ldr BASE, L->base
| cmp CRET1, #0
| beq >3
| ldrd CARG34, [CRET1]
| ins_next1
| ins_next2
| strd CARG34, [BASE, RA]
| ins_next3
|
|3: // Call __index metamethod.
| // BASE = base, L->top = new base, stack = cont/func/t/k
| rsb CARG1, BASE, #FRAME_CONT
| ldr BASE, L->top
| mov NARGS8:RC, #16 // 2 args for func(t, k).
| str PC, [BASE, #-12] // [cont|PC]
| add PC, CARG1, BASE
| ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
| b ->vm_call_dispatch_f
|
|//-----------------------------------------------------------------------
|
|->vmeta_tsets1:
| add CARG2, BASE, RB
| b >2
|
|->vmeta_tsets:
| sub CARG2, DISPATCH, #-DISPATCH_GL(tmptv)
| mvn CARG4, #~LJ_TTAB
| str TAB:RB, [CARG2]
| str CARG4, [CARG2, #4]
|2:
| mvn CARG4, #~LJ_TSTR
| str STR:RC, TMPDlo
| str CARG4, TMPDhi
| mov CARG3, TMPDp
| b >1
|
|->vmeta_tsetb: // RC = index
| decode_RB8 RB, INS
| str RC, TMPDlo
| mvn CARG4, #~LJ_TISNUM
| add CARG2, BASE, RB
| str CARG4, TMPDhi
| mov CARG3, TMPDp
| b >1
|
|->vmeta_tsetv:
| add CARG2, BASE, RB
| add CARG3, BASE, RC
|1:
| str BASE, L->base
| mov CARG1, L
| str PC, SAVE_PC
| bl extern lj_meta_tset // (lua_State *L, TValue *o, TValue *k)
| // Returns TValue * (finished) or NULL (metamethod).
| .IOS ldr BASE, L->base
| cmp CRET1, #0
| ldrd CARG34, [BASE, RA]
| beq >3
| ins_next1
| // NOBARRIER: lj_meta_tset ensures the table is not black.
| strd CARG34, [CRET1]
| ins_next2
| ins_next3
|
|3: // Call __newindex metamethod.
| // BASE = base, L->top = new base, stack = cont/func/t/k/(v)
| rsb CARG1, BASE, #FRAME_CONT
| ldr BASE, L->top
| mov NARGS8:RC, #24 // 3 args for func(t, k, v).
| strd CARG34, [BASE, #16] // Copy value to third argument.
| str PC, [BASE, #-12] // [cont|PC]
| add PC, CARG1, BASE
| ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
| b ->vm_call_dispatch_f
|
|//-- Comparison metamethods ---------------------------------------------
|
|->vmeta_comp:
| mov CARG1, L
| sub PC, PC, #4
| mov CARG2, RA
| str BASE, L->base
| mov CARG3, RC
| str PC, SAVE_PC
| decode_OP CARG4, INS
| bl extern lj_meta_comp // (lua_State *L, TValue *o1, *o2, int op)
| // Returns 0/1 or TValue * (metamethod).
|3:
| .IOS ldr BASE, L->base
| cmp CRET1, #1
| bhi ->vmeta_binop
|4:
| ldrh RB, [PC, #2]
| add PC, PC, #4
| add RB, PC, RB, lsl #2
| subhs PC, RB, #0x20000
|->cont_nop:
| ins_next
|
|->cont_ra: // RA = resultptr
| ldr INS, [PC, #-4]
| ldrd CARG12, [RA]
| decode_RA8 CARG3, INS
| strd CARG12, [BASE, CARG3]
| b ->cont_nop
|
|->cont_condt: // RA = resultptr
| ldr CARG2, [RA, #4]
| mvn CARG1, #~LJ_TTRUE
| cmp CARG1, CARG2 // Branch if result is true.
| b <4
|
|->cont_condf: // RA = resultptr
| ldr CARG2, [RA, #4]
| checktp CARG2, LJ_TFALSE // Branch if result is false.
| b <4
|
|->vmeta_equal:
| // CARG2, CARG3, CARG4 are already set by BC_ISEQV/BC_ISNEV.
| sub PC, PC, #4
| str BASE, L->base
| mov CARG1, L
| str PC, SAVE_PC
| bl extern lj_meta_equal // (lua_State *L, GCobj *o1, *o2, int ne)
| // Returns 0/1 or TValue * (metamethod).
| b <3
|
|->vmeta_equal_cd:
|.if FFI
| sub PC, PC, #4
| str BASE, L->base
| mov CARG1, L
| mov CARG2, INS
| str PC, SAVE_PC
| bl extern lj_meta_equal_cd // (lua_State *L, BCIns op)
| // Returns 0/1 or TValue * (metamethod).
| b <3
|.endif
|
|//-- Arithmetic metamethods ---------------------------------------------
|
|->vmeta_arith_vn:
| decode_RB8 RB, INS
| decode_RC8 RC, INS
| add CARG3, BASE, RB
| add CARG4, KBASE, RC
| b >1
|
|->vmeta_arith_nv:
| decode_RB8 RB, INS
| decode_RC8 RC, INS
| add CARG4, BASE, RB
| add CARG3, KBASE, RC
| b >1
|
|->vmeta_unm:
| ldr INS, [PC, #-8]
| sub PC, PC, #4
| add CARG3, BASE, RC
| add CARG4, BASE, RC
| b >1
|
|->vmeta_arith_vv:
| decode_RB8 RB, INS
| decode_RC8 RC, INS
| add CARG3, BASE, RB
| add CARG4, BASE, RC
|1:
| decode_OP OP, INS
| add CARG2, BASE, RA
| str BASE, L->base
| mov CARG1, L
| str PC, SAVE_PC
| str OP, ARG5
| bl extern lj_meta_arith // (lua_State *L, TValue *ra,*rb,*rc, BCReg op)
| // Returns NULL (finished) or TValue * (metamethod).
| .IOS ldr BASE, L->base
| cmp CRET1, #0
| beq ->cont_nop
|
| // Call metamethod for binary op.
|->vmeta_binop:
| // BASE = old base, CRET1 = new base, stack = cont/func/o1/o2
| sub CARG2, CRET1, BASE
| str PC, [CRET1, #-12] // [cont|PC]
| add PC, CARG2, #FRAME_CONT
| mov BASE, CRET1
| mov NARGS8:RC, #16 // 2 args for func(o1, o2).
| b ->vm_call_dispatch
|
|->vmeta_len:
| add CARG2, BASE, RC
| str BASE, L->base
| mov CARG1, L
| str PC, SAVE_PC
| bl extern lj_meta_len // (lua_State *L, TValue *o)
| // Returns NULL (retry) or TValue * (metamethod base).
| .IOS ldr BASE, L->base
#if LJ_52
| cmp CRET1, #0
| bne ->vmeta_binop // Binop call for compatibility.
| ldr TAB:CARG1, [BASE, RC]
| b ->BC_LEN_Z
#else
| b ->vmeta_binop // Binop call for compatibility.
#endif
|
|//-- Call metamethod ----------------------------------------------------
|
|->vmeta_call: // Resolve and call __call metamethod.
| // RB = old base, BASE = new base, RC = nargs*8
| mov CARG1, L
| str RB, L->base // This is the callers base!
| sub CARG2, BASE, #8
| str PC, SAVE_PC
| add CARG3, BASE, NARGS8:RC
| .IOS mov RA, BASE
| bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
| .IOS mov BASE, RA
| ldr LFUNC:CARG3, [BASE, FRAME_FUNC] // Guaranteed to be a function here.
| add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
| ins_call
|
|->vmeta_callt: // Resolve __call for BC_CALLT.
| // BASE = old base, RA = new base, RC = nargs*8
| mov CARG1, L
| str BASE, L->base
| sub CARG2, RA, #8
| str PC, SAVE_PC
| add CARG3, RA, NARGS8:RC
| bl extern lj_meta_call // (lua_State *L, TValue *func, TValue *top)
| .IOS ldr BASE, L->base
| ldr LFUNC:CARG3, [RA, FRAME_FUNC] // Guaranteed to be a function here.
| ldr PC, [BASE, FRAME_PC]
| add NARGS8:RC, NARGS8:RC, #8 // Got one more argument now.
| b ->BC_CALLT2_Z
|
|//-- Argument coercion for 'for' statement ------------------------------
|
|->vmeta_for:
| mov CARG1, L
| str BASE, L->base
| mov CARG2, RA
| str PC, SAVE_PC
| bl extern lj_meta_for // (lua_State *L, TValue *base)
| .IOS ldr BASE, L->base
|.if JIT
| ldrb OP, [PC, #-4]
|.endif
| ldr INS, [PC, #-4]
|.if JIT
| cmp OP, #BC_JFORI
|.endif
| decode_RA8 RA, INS
| decode_RD RC, INS
|.if JIT
| beq =>BC_JFORI
|.endif
| b =>BC_FORI
|
|//-----------------------------------------------------------------------
|//-- Fast functions -----------------------------------------------------
|//-----------------------------------------------------------------------
|
|.macro .ffunc, name
|->ff_ .. name:
|.endmacro
|
|.macro .ffunc_1, name
|->ff_ .. name:
| ldrd CARG12, [BASE]
| cmp NARGS8:RC, #8
| blo ->fff_fallback
|.endmacro
|
|.macro .ffunc_2, name
|->ff_ .. name:
| ldrd CARG12, [BASE]
| ldrd CARG34, [BASE, #8]
| cmp NARGS8:RC, #16
| blo ->fff_fallback
|.endmacro
|
|.macro .ffunc_n, name
| .ffunc_1 name
| checktp CARG2, LJ_TISNUM
| bhs ->fff_fallback
|.endmacro
|
|.macro .ffunc_nn, name
| .ffunc_2 name
| checktp CARG2, LJ_TISNUM
| cmnlo CARG4, #-LJ_TISNUM
| bhs ->fff_fallback
|.endmacro
|
|.macro .ffunc_d, name
| .ffunc name
| ldr CARG2, [BASE, #4]
| cmp NARGS8:RC, #8
| vldr d0, [BASE]
| blo ->fff_fallback
| checktp CARG2, LJ_TISNUM
| bhs ->fff_fallback
|.endmacro
|
|.macro .ffunc_dd, name
| .ffunc name
| ldr CARG2, [BASE, #4]
| ldr CARG4, [BASE, #12]
| cmp NARGS8:RC, #16
| vldr d0, [BASE]
| vldr d1, [BASE, #8]
| blo ->fff_fallback
| checktp CARG2, LJ_TISNUM
| cmnlo CARG4, #-LJ_TISNUM
| bhs ->fff_fallback
|.endmacro
|
|// Inlined GC threshold check. Caveat: uses CARG1 and CARG2.
|.macro ffgccheck
| ldr CARG1, [DISPATCH, #DISPATCH_GL(gc.total)]
| ldr CARG2, [DISPATCH, #DISPATCH_GL(gc.threshold)]
| cmp CARG1, CARG2
| blge ->fff_gcstep
|.endmacro
|
|//-- Base library: checks -----------------------------------------------
|
|.ffunc_1 assert
| checktp CARG2, LJ_TTRUE
| bhi ->fff_fallback
| ldr PC, [BASE, FRAME_PC]
| strd CARG12, [BASE, #-8]
| mov RB, BASE
| subs RA, NARGS8:RC, #8
| add RC, NARGS8:RC, #8 // Compute (nresults+1)*8.
| beq ->fff_res // Done if exactly 1 argument.
|1:
| ldrd CARG12, [RB, #8]
| subs RA, RA, #8
| strd CARG12, [RB], #8
| bne <1
| b ->fff_res
|
|.ffunc type
| ldr CARG2, [BASE, #4]
| cmp NARGS8:RC, #8
| blo ->fff_fallback
| checktp CARG2, LJ_TISNUM
| mvnlo CARG2, #~LJ_TISNUM
| rsb CARG4, CARG2, #(int)(offsetof(GCfuncC, upvalue)>>3)-1
| lsl CARG4, CARG4, #3
| ldrd CARG12, [CFUNC:CARG3, CARG4]
| b ->fff_restv
|
|//-- Base library: getters and setters ---------------------------------
|
|.ffunc_1 getmetatable
| checktp CARG2, LJ_TTAB
| cmnne CARG2, #-LJ_TUDATA
| bne >6
|1: // Field metatable must be at same offset for GCtab and GCudata!
| ldr TAB:RB, TAB:CARG1->metatable
|2:
| mvn CARG2, #~LJ_TNIL
| ldr STR:RC, [DISPATCH, #DISPATCH_GL(gcroot[GCROOT_MMNAME+MM_metatable])]
| cmp TAB:RB, #0
| beq ->fff_restv
| ldr CARG3, TAB:RB->hmask
| ldr CARG4, STR:RC->hash
| ldr NODE:INS, TAB:RB->node
| and CARG3, CARG3, CARG4 // idx = str->hash & tab->hmask
| add CARG3, CARG3, CARG3, lsl #1
| add NODE:INS, NODE:INS, CARG3, lsl #3 // node = tab->node + idx*3*8
|3: // Rearranged logic, because we expect _not_ to find the key.
| ldrd CARG34, NODE:INS->key // STALL: early NODE:INS.
| ldrd CARG12, NODE:INS->val
| ldr NODE:INS, NODE:INS->next
| checktp CARG4, LJ_TSTR
| cmpeq CARG3, STR:RC
| beq >5
| cmp NODE:INS, #0
| bne <3
|4:
| mov CARG1, RB // Use metatable as default result.
| mvn CARG2, #~LJ_TTAB
| b ->fff_restv
|5:
| checktp CARG2, LJ_TNIL
| bne ->fff_restv
| b <4
|
|6:
| checktp CARG2, LJ_TISNUM
| mvnhs CARG2, CARG2
| movlo CARG2, #~LJ_TISNUM
| add CARG4, DISPATCH, CARG2, lsl #2
| ldr TAB:RB, [CARG4, #DISPATCH_GL(gcroot[GCROOT_BASEMT])]
| b <2
|