/
Jit.xs
1638 lines (1511 loc) · 53.5 KB
/
Jit.xs
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
/* Jit.xs -*- mode:C c-basic-offset:4 -*-
*
* JIT (Just-in-time compile) the Perl5 runloop.
* Currently for intel x86 32+64bit. More CPU's later.
* Status:
* Works only for simple i386 and amd64,
* without maybranch ops (return op_other, op_last, ... ignored)
*
* Copyright (C) 2010 by Reini Urban
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
* http://gist.github.com/331867
* http://search.cpan.org/dist/Jit/
*/
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#ifndef _WIN32
#include <sys/mman.h>
#endif
#if defined(DEBUGGING) && defined(__GNUC__)
#include <sys/stat.h>
#endif
#if defined(DEBUGGING) \
&& !(defined(_WIN32) || (defined(__CYGWIN__) && (__GNUC__ > 3)) || defined(AIX))
#define HAVE_DEBOP
#endif
/* sync with lib/Jit.pm */
#define HINT_JIT_FLAGS 0x04000000
#if defined(HAVE_LIBDISASM) && defined(DEBUGGING)
#include <libdis.h>
#endif
typedef unsigned char CODE;
#define T_CHARARR static CODE
#undef JIT_CPU
/* if dealing with doubles on sse we want this */
#define ALIGN_16(c) (c%16?(c+(16-c%16)):c)
#define ALIGN_64(c) (c%64?(c+(64-c%64)):c)
#define ALIGN_N(n,c) (c%n?(c+(n-c%n)):c)
HV* otherops = NULL;
HV* otherops1 = NULL;
typedef struct jmptarget {
OP *op; /* the target op */
char *label; /* XXX not sure if need this */
CODE *target; /* the code points for label (run-time lookup for goto XS and goto label) */
} JMPTGT;
int jmpix = 0;
JMPTGT *jmptargets = NULL;
typedef struct loopstack {
CODE *nextop; /* the 3 loop targets */
CODE *lastop;
CODE *redoop;
} LOOPTGT;
int loopix = 0;
LOOPTGT *looptargets = NULL;
#define PUSH_JMP(jmp) \
jmptargets = (JMPTGT*)realloc(jmptargets, (jmpix+1)*sizeof(JMPTGT)); \
memcpy(&jmptargets[jmpix], &jmp, sizeof(JMPTGT)); jmpix++
#define POP_JMP (jmpix >= 0 ? &jmptargets[jmpix--] : NULL)
#define PUSH_LOOP(cx) \
looptargets = (LOOPTGT*)realloc(looptargets, (loopix+1)*sizeof(LOOPTGT)); \
memcpy(&looptargets[loopix], &cx, sizeof(LOOPTGT)); loopix++
#define POP_LOOP (loopix >= 0 ? &looptargets[loopix--] : NULL)
#ifdef DEBUGGING
int global_label;
int global_loops = 0;
int local_chains = 0;
#endif
int dispatch_needed(OP* op);
int maybranch(OP* op);
CODE *push_prolog(CODE *code);
long jit_chain(pTHX_ OP* op, CODE *code, CODE *code_start,
int jumpsize, OP* stopop
#ifdef DEBUGGING
,FILE *fh, FILE *stabs
#endif
);
/* search dynamically the code target for op in jmptargets */
CODE *jmp_search_label(OP* op);
/* When do we need PERL_ASYNC_CHECK?
* if (dispatch_needed(op)) if (PL_sig_pending) Perl_despatch_signals();
*
* Until 5.13.2 we had it after each and every op,
* since 5.13.2 only inside certain ops,
* which need to handle pending signals.
* In 5.6 it was a NOOP.
*/
/*#define _BYPASS_DISPATCH*/
#if (PERL_VERSION > 6) && (PERL_VERSION < 13)
#define HAVE_DISPATCH
#define DISPATCH_NEEDED(op) dispatch_needed(op)
#else
#undef HAVE_DISPATCH
#define DISPATCH_NEEDED(op) 0
#endif
#ifdef DEBUGGING
# define JIT_CHAIN_DRYRUN(op) jit_chain(aTHX_ op, NULL, NULL, 0, NULL, fh, stabs)
# define JIT_CHAIN_DRYRUN_FULL(op,stopop) jit_chain(aTHX_ op, NULL, NULL, 0, stopop, fh, stabs)
# define JIT_CHAIN(op) (CODE*)jit_chain(aTHX_ op, code, code_start, 0, NULL, fh, stabs)
# define JIT_CHAIN_FULL(op, size, stopop) \
(CODE*)jit_chain(aTHX_ op, code, code_start, size, stopop, fh, stabs)
# define DEB_PRINT_LOC(loc) printf(loc" \t= 0x%x\n", loc)
# if PERL_VERSION < 8
# define DEBUG_v(x) x
# endif
#else
# define JIT_CHAIN_DRYRUN(op) jit_chain(aTHX_ op, NULL, NULL, 0, NULL)
# define JIT_CHAIN_FULL_DRYRUN(op, stopop) jit_chain(aTHX_ op, NULL, NULL, 0, stopop)
# define JIT_CHAIN(op) (CODE*)jit_chain(aTHX_ op, code, code_start, 0, NULL)
# define JIT_CHAIN_FULL(op, size, stopop) \
(CODE*)jit_chain(aTHX_ op, code, code_start, size, stopop)
# define DEB_PRINT_LOC(loc)
# if PERL_VERSION < 8
# define DEBUG_v(x)
# endif
#endif
#define PTR2X(ptr) INT2PTR(long, ptr)
#ifdef USE_ITHREADS
/* first arg already my_perl, and already on stack */
# define PUSH_1ARG_DRYRUN size += sizeof(push_arg2)
# define PUSH_1ARG PUSHc(push_arg2)
#else
# define PUSH_1ARG_DRYRUN size += sizeof(push_arg1)
# define PUSH_1ARG PUSHc(push_arg1)
#endif
/*
C pseudocode of the Perl runloop:
not-threaded:
OP *op;
int *p = &Perl_Isig_pending_ptr;
if maybranch:
op = PL_op->op_next; # save away at ecx to check
PL_op = Perl_pp_opname(); # returns op_next, op_other, op_first, op_last
# or a new optree start
if (dispatch_needed)
if (*p) Perl_despatch_signals();
if (PL_op == op) #if maybranch label other targets
goto next_1;
# assemble op_other chain until 0
PL_op = Perl_pp_opname();
...
goto leave_1;
next_1: #continue with op_next
...
leave_1:
Perl_pp_leave()
return
else:
thisop:
PL_op = Perl_pp_opname(); #returns op->next
if dispatch_needed: if (*p) Perl_despatch_signals();
nextop:
threaded, same logic as above, just:
my_perl->Iop = PL_op->op_ppaddr(my_perl);
if (my_perl->Isig_pending) Perl_despatch_signals(my_perl);
*/
#define CALL_SIZE 4 /* size for the call instruction arg */
#define MOV_SIZE 4 /* size for the mov instruction arg */
/* multi without threads yet unhandled. should be a simple s/USE_ITHREADS/MULTIPLICITY/ */
#if defined(MULTIPLICITY) && !defined(USE_ITHREADS)
# error "MULTIPLICITY without ITHREADS not supported"
#endif
#if defined(USE_THREADS) && !defined(USE_ITHREADS)
# error "USE_THREADS without ITHREADS not supported"
#endif
#ifdef USE_ITHREADS
/* threads: offsets are perl version and ptrsize dependent */
# define IOP_OFFSET PTRSIZE /* my_perl->Iop_pending offset */
# define SIG_PENDING_OFFSET 4*PTRSIZE /* my_perl->Isig_pending offset */
# define PPADDR_OFFSET (IOP_OFFSET+2*PTRSIZE)
#else
# define PPADDR_OFFSET (2*PTRSIZE)
#endif
#define CALL_ABS(abs) code = call_abs(code,abs)
/*(U32)((unsigned char*)abs-code-3)*/
#define PUSHc(what) memcpy(code,what,sizeof(what)); code += sizeof(what)
/* force 4 byte for U32, 64bit uses 8 byte for U32, but 4 byte for call near */
#define PUSHcall(what) memcpy(code,&what,CALL_SIZE); code += CALL_SIZE
#define PUSHbyte(byte) { signed char b = (byte); *code++ = b; }
#ifdef DEBUGGING
# define dbg_cline(p) fprintf(fh,"%s\n",p); line++
# define dbg_cline1(s,a) fprintf(fh, s, a); line++
#else
# define dbg_cline(p)
# define dbg_cline1(p1,p2)
#endif
#if defined(__GNUC__) && defined(DEBUGGING)
# define dbg_stabs(s) fprintf(stabs, ".stabn 68,0,%d,%u /* %s */\n", line, code-code_start, s)
# define dbg_stabs1(s,p1) fprintf(stabs, ".stabn 68,0,%d,%u /* %s */\n", line, code-code_start, s)
# define dbg_lines(s) dbg_cline(s); dbg_stabs(s)
# define dbg_lines1(s, p1) dbg_cline1(s, p1); dbg_stabs1(s, p1)
#else
# define dbg_stabs(s)
# define dbg_stabs1(s,p1)
# ifdef DEBUGGING
# define dbg_lines(s) dbg_cline(s)
# define dbg_lines1(s, p1) dbg_cline1(s, p1)
# else
# define dbg_lines(s)
# define dbg_lines1(s, p1)
# endif
#endif
/* __amd64 defines __x86_64 */
#if defined(__x86_64__) || defined(__amd64) || defined(__amd64__) || defined(_M_X64)
#define JIT_CPU "amd64"
#define JIT_CPU_AMD64
#define CALL_ALIGN 0
#define MOV_REL
#define PUSH_SIZE 4 /* size for the push instruction arg 4/8 */
#define PUSHabs(what) memcpy(code,what,PUSH_SIZE); code += PUSH_SIZE
#define PUSHrel(where) { \
U32 r = (CODE*)where - (code+MOV_SIZE); \
memcpy(code,&r,MOV_SIZE); code += MOV_SIZE; \
}
/*void f_PUSHrel(CODE* code, void *where);*/
/*#define PUSHrel(where) f_PUSHrel(code,(void*)where); code += MOV_SIZE;*/
#define revword(m) (((unsigned long)m)&0xff),((((unsigned long)m)&0xff00)>>8), \
((((unsigned long)m)&0xff0000)>>16),((((unsigned long)m)&0xff000000)>>24), \
((((unsigned long)m)&0xff00000000)>>32),((((unsigned long)m)&0xff00000000)>>40), \
((((unsigned long)m)&0xff0000000000)>>48),((((unsigned long)m)&0xff0000000000)>>56)
#define revword4(m) (((unsigned int)m)&0xff),((((unsigned int)m)&0xff00)>>8), \
((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff000000)>>24)
T_CHARARR NOP[] = {0x90}; /* nop */
/* PROLOG */
#define enter 0xc8
#define enter_8 0xc8,0x08,0x00,0x00
#define push_rcx 0x51
#define push_rbx 0x53
#define push_rbp 0x55
/* wrong! XXX */
#define mov_rsp_rbp 0x48,0x89,0xe5
#define push_r12 0x41,0x54
#define mov_rax_rbx 0x48,0x89,0xc3
#define sub_x_rsp(byte) 0x48,0x83,0xec,byte
#define add_x_esp(byte) 0x48,0x83,0xc4,byte
#define fourbyte 0x00,0x00,0x00,0x00
#define mov_mem_eax(m) 0xa1,revword(m)
/* mov $memabs,(%rbx) &PL_op to ebx */
#define mov_mem_rbx 0x48,0x8b,0x1d /* mov &PL_op,%rbx */
#define mov_rebp_ebx(byte) 0x8b,0x5d,byte /* mov 0x8(%ebp),%ebx*/
#define mov_rrsp_rbx 0x48,0x8b,0x1c,0x24 /* mov (%rsp),%rbx ; my_perl from stack to rbx */
#define mov_mem_rcx 0x48,0x8b,0x0d /* mov &PL_sig_pending,%rcx */
#define test_rcx_rcx 0x48,0x85,0xc9
#define test_ecx_ecx 0x85,0xc9
#define push_imm_0 0x68
#define push_imm(m) 0x68,revword(m)
/* gcc __fastcall amd64 convention for param 1-2 passing */
#define mov_mem_esi 0xbe /* arg2 */
#define mov_mem_edi 0xbf /* arg1 */
#define mov_mem_ecx 0xb9 /* arg1 */
#define mov_eax_edi 0x89,0xc7
#define mov_eax_esi 0x89,0xc6
#define mov_eax_ecx 0x89,0xc1
#define mov_eax_edx 0x89,0xc2
#define mov_rax_rdi 0x48,0x89,0xc7
#define mov_rax_rsi 0x48,0x89,0xc6
#define mov_rax_rcx 0x48,0x89,0xc1
#define mov_rax_rdx 0x48,0x89,0xc2
#ifndef _WIN64
#define push_arg1_mem mov_mem_edi /* if call via register */
#define push_arg2_mem mov_mem_esi
#define push_arg1_eax mov_rax_rdi
#define push_arg2_eax mov_rax_rsi
#else
/* Win64 Visual C __fastcall uses rcx,rdx for the first int args, not rdi,rsi
We use less than 2GB for our vars and subs.
*/
#define mov_mem_edx 0xba /* arg2 */
#define push_arg1_mem mov_mem_ecx /* if call via register */
#define push_arg2_mem mov_mem_edx
#define push_arg1_eax mov_rax_rcx
#define push_arg2_eax mov_rax_rdx
#endif
#ifndef _WIN64
#define mov_rbx_arg1 0x48,0x89,0xdf /* my_perl => arg1 in rdi */
#else
#define mov_rbx_arg1 0x48,0x89,0xdf /* my_perl => arg1 in rcx */
#endif
#define mov_rebx_mem 0x48,0x89,0x1d /* movq (%ebx), &PL_op */
#define mov_mem_rebx 0x48,0xc7,0x03 /* movq &PL_op, (%ebx) */
#define mov_eax_rebx 0x89,0x03 /* movq %rax,(%rbx) &PL_op in ebx */
/* &PL_op in -4(%ebp) */
/* #define mov_mem_4ebp 0xc7,0x45,0xfc */
/* EPILOG */
#define pop_rcx 0x59
#define pop_rbx 0x5b
#define pop_r12 0x41,0x5c
#define leave 0xc9
#define ret 0xc3
/* maybranch: */
/* &op in -8(%ebp) */
#define mov_eax_8ebp 0x89,0x45,0xf8
#define cltq 0x48,0x98
#define mov_0_rax 0xb8,0x00,0x00,0x00,0x00
#define call 0xe8 /* + 4 rel */
#define ljmp(abs) 0xff,0x25 /* + 4 memabs */
#define mov_mem_rrsp 0xc7,0x04,0x24 /* store op->next on stack */
#define mov_eax_4ebp 0x89,0x45,0xfc
#define mov_mem_r12 0x41,0xbc /* movq PL_op->next, %r12 */
#define cmp_rax_r12 0x49,0x39,0xc4
#define je_0 0x74
#define je(byte) 0x74,(byte)
#define jew_0 0x0f,0x84
#define jew(word) 0x0f,0x84,revword4(word)
#define jmpq_0 0xe9 /* maybranch */
#define jmpq(word) 0xe9,revword(word)
/* mov %rax,(%rbx) &PL_op in rbx */
#define mov_rax_rrbx 0x48,0x89,0x03
#define mov_eax_rebx 0x89,0x03
#define mov_4ebp_edx 0x8b,0x55,0xfc
#define mov_reax_ebx 0x48,0x8b,0x18
#define mov_redx_eax 0x82,0x02
#define test_eax_eax 0x85,0xc0
/* skip call _Perl_despatch_signals */
#define mov_mem_rebp8 0xc7,0x45,0xf8 /* mov &op,-8(%rbp) */
#define cmp_eax_rebp8 0x39,0x45,0xf8 /* cmp %eax,-8(%rbp) */
#if 0
#define mov_mem_resp1 0xc7,0x44,0x24,0x0fc
#define cmp_eax_resp1 0x39,0x44,0x24,0xfc /* 4 byte cmp %eax,4(%rsp) */
#endif
/*#define cmp_rax_rrsp 0x48,0x39,0xe0 *//* 8 byte */
/*#define test_rax_rax 0x48,0x85,0xc0*/
#ifdef USE_ITHREADS
# include "amd64thr.c"
#else
# include "amd64.c"
#endif
#endif /* EOF amd64 */
#if !defined(JIT_CPU) && (defined(__i386__) || defined(__i386) || defined(_M_IX86))
#define JIT_CPU "i386"
#define JIT_CPU_X86
#define CALL_ALIGN 4
#undef MOV_REL
#define PUSH_SIZE 4 /* size for the push instruction arg 4/8 */
#define PUSHabs(what) memcpy(code,what,PUSH_SIZE); code += PUSH_SIZE
#define PUSHrel(what) memcpy(code,what,MOV_SIZE); code += MOV_SIZE
#define revword(m) (((unsigned int)m)&0xff),((((unsigned int)m)&0xff00)>>8), \
((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff000000)>>24)
#define absword(m) ((((unsigned int)m)&0xff000000)>>24), \
((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff00)>>8),(((unsigned int)m)&0xff)
T_CHARARR NOP[] = {0x90}; /* nop */
#define fourbyte 0x00,0x00,0x00,0x00
/* PROLOG */
#define enter_8 0xc8,0x08,0x00,0x00
#define push_ebx 0x53
#define push_ebp 0x55
#define mov_esp_ebp 0x89,0xe5
#define push_edi 0x57
#define push_esi 0x56
#define push_ecx 0x51
#define push_edx 0x52
#define sub_x_esp(byte) 0x83,0xec,byte
#define mov_eax_rebx 0x89,0x03 /* mov %rax,(%rbx) &PL_op in ebx */
#define push_imm_0 0x68
#define push_imm(m) 0x68,revword(m)
#define push_eax 0x50
#define push_arg1_eax push_eax
#define push_arg2_eax push_eax
#define push_arg1_mem push_imm_0
#define push_arg2_mem push_imm_0
/* mov $memabs,%eax PL_op in eax */
#define mov_mem_eax(m) 0xa1,revword(m)
/* mov $memabs,%ebx &PL_op in ebx */
#define mov_mem_ebx(m) 0xbb,revword(m)
/* mov $memabs,0(%esp) &PL_op to stack-0 */
#define mov_mem_ecx 0xb9
/* &PL_sig_pending in -4(%ebp) */
#define mov_mem_4ebp(m) 0xc7,0x45,0xfc,revword(m)
#define mov_4ebp_edx 0x8b,0x55,0xfc
#define mov_redx_eax 0x8b,0x02
#define test_eax_eax 0x85,0xc0
#define je_0 0x74
#define je(byte) 0x74,(byte)
#define mov_mem_rebp8 0xc7,0x45,0xf8 /* mov &op,-8(%rbp) */
#define cmp_eax_rebp8 0x39,0x45,0xf8 /* cmp %eax,,-8(%rbp) */
#if 0
#define mov_mem_resp1 0xc7,0x44,0x24,0xfc
#define cmp_eax_resp1 0x39,0x44,0x24,0xfc /* 4 byte cmp %eax,4(%rsp) */
#endif
#define cmp_ecx_eax 0x39,0xc8
#define mov_rebp_ebx(byte) 0x8b,0x5d,byte /* mov 0x8(%ebp),%ebx*/
#define test_eax_eax 0x85,0xc0
/* XXX on i386 also? */
#define jew_0 0x0f,0x84
#define jew(word) 0x0f,0x84,revword4(word)
/* EPILOG */
#define add_x_esp(byte) 0x83,0xc4,byte /* add $0x4,%esp */
#define pop_ecx 0x59
#define pop_ebx 0x5b
#define pop_edx 0x5a
#define pop_esi 0x5e
#define pop_edi 0x5f
#define pop_ebp 0x5d
#define leave 0xc9
#define ret 0xc3
/* maybranch: */
/* &op in -8(%ebp) */
#define mov_eax_8ebp 0x89,0x45,0xf8
#define mov_eax_4ebp 0x89,0x45,0xfc
#define call 0xe8 /* + 4 rel */
#define ljmp(abs) 0xff,0x25 /* + 4 memabs */
#define ljmp_0 0xff,0x25 /* + 4 memabs */
#define mov_eax_mem 0xa3 /* + 4 memabs */
#define jmpb_0 0xeb /* maybranch */
#define jmpb(byte) 0xeb,(byte) /* maybranch */
#define jmpq_0 0xe9 /* maybranch */
#define jmpq(word) 0xe9,revword(word)
#ifdef USE_ITHREADS
# include "i386thr.c"
#else
# include "i386.c"
#endif
/* save op = op->next
XXX TODO %ecx is not preserved between function calls. Need to use the stack.
*/
T_CHARARR gotorel[] = {
jmpq(0)
};
CODE *
push_gotorel(CODE *code, long label) {
CODE gotorel[] = {
jmpq_0};
PUSHc(gotorel);
PUSHabs(&label);
return code;
}
# define MAYBRANCH_PLOP maybranch_plop
# define GOTOREL gotorel
#endif /* EOF i386 */
#if defined(JIT_CPU_X86) || defined(JIT_CPU_AMD64)
int
sizeof_maybranch_check(int fw) {
if (abs(fw) > 128) {
return sizeof(maybranch_checkw);
} else {
return sizeof(maybranch_check);
}
}
T_CHARARR ifop0return[] = {
test_eax_eax,
je(sizeof(EPILOG))
};
T_CHARARR ifop0goto[] = {
test_eax_eax,
je_0, fourbyte
};
T_CHARARR ifop0gotow[] = {
test_eax_eax,
jew_0, fourbyte
};
CODE *
push_ifop0goto(CODE *code, long next) {
CODE ifop0goto[] = {
test_eax_eax,
je_0};
if (abs(next) > 128) {
CODE ifop0gotow[] = {
test_eax_eax,
jew_0};
PUSHc(ifop0gotow);
PUSHrel((CODE*)next);
} else {
PUSHc(ifop0goto);
PUSHbyte(next);
}
return code;
}
T_CHARARR add_eax_ppaddr[] = {
0x83,0xc0,PPADDR_OFFSET /* add $8,%eax */
};
T_CHARARR call_eax[] = {
0xff,0xd0 /* call *%eax */
};
#endif
#if defined(__ia64__) || defined(__ia64) || defined(_M_IA64)
#error "IA64 not supported so far"
#endif
#if defined(__sparcv9)
#error "SPARC V9 not supported so far"
#endif
#if defined(__arm__)
#error "ARM or THUMB not supported so far"
#endif
#ifndef JIT_CPU
#error "Only intel x86_32 and x86_64/amd64 supported so far"
#endif
#ifndef PUSHrel
# ifdef MOV_REL /* amd64 */
# define PUSHrel(where) { \
U32 r = (CODE*)where - (code+4); \
memcpy(code,&r,MOV_SIZE); code += MOV_SIZE; \
}
# else
# define PUSHrel(what) memcpy(code,what,MOV_SIZE); code += MOV_SIZE
# endif
#endif
/**********************************************************************************/
#ifdef PROFILING
#define NV_1E6 1000000.0
#ifdef WIN32
# include <time.h>
#else
# include <sys/time.h>
#endif
NV
mytime() {
struct timeval Tp;
struct timezone Tz;
int status;
status = gettimeofday (&Tp, &Tz);
if (status == 0) {
Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */
return Tp.tv_sec + (Tp.tv_usec / NV_1E6);
} else {
return -1.0;
}
}
#endif
int
dispatch_needed(OP* op) {
#ifdef BYPASS_DISPATCH
return 0; /* for TESTING only */
#endif
switch (op->op_type) { /* sync this list with B::CC */
case OP_WAIT:
case OP_WAITPID:
case OP_NEXTSTATE:
case OP_AND:
case OP_COND_EXPR:
case OP_UNSTACK:
case OP_OR:
case OP_DEFINED:
case OP_SUBST:
return 1;
default:
return 0;
}
}
int
maybranch(OP* op) {
#ifdef BYPASS_MAYBRANCH
return 0; /* for TESTING only */
#endif
switch (op->op_type) { /* XXX sync this list with Opcodes.pm */
case OP_SUBSTCONT: /* LOGOP other or next */
case OP_GREPWHILE: /* LOGOP other or next */
case OP_MAPWHILE: /* LOGOP other or next */
case OP_AND: /* LOGOP other or next */
case OP_OR: /* LOGOP other or next */
case OP_COND_EXPR: /* LOGOP other or next */
case OP_ANDASSIGN: /* LOGOP other or next */
case OP_ORASSIGN: /* LOGOP other or next */
#if PERL_VERSION > 8
case OP_DOR: /* LOGOP other or next */
case OP_DORASSIGN: /* LOGOP other or next */
case OP_ENTERWHEN: /* LOGOP other or next */
case OP_ENTERGIVEN: /* LOGOP other or next */
case OP_ONCE: /* LOGOP other or next */
#endif
case OP_RANGE: /* LOGOP other or next */
/* static */
case OP_FLIP: /* first->other or next. not tested */
case OP_SUBST: /* pmreplroot or other or next. nyi */
case OP_ENTERSUB: /* CvSTART(cv)|autoload or next */
case OP_DUMP: /* main_start. XXX makes no sense to support */
case OP_FORMLINE: /* initially only: doparseform */
case OP_GREPSTART: /* next->next or next->other */
/* contexts */
case OP_ENTERLOOP: /* LOOPOP cx->redoop|lastop|nextop */
case OP_ENTERITER: /* LOOPOP cx->redoop|lastop|nextop */
case OP_LAST: /* LOOPOP cx->lastop->next or cx->blk_sub|eval|format.retop */
case OP_NEXT: /* LOOPOP cx->nextop */
case OP_REDO: /* LOOPOP cx->redoop but if enter, enter->next */
case OP_RETURN: /* cx->blk_sub|eval|format.retop */
case OP_DBSTATE:
case OP_REQUIRE: /* ? */
case OP_ENTEREVAL: /* cx->blk_eval.retop */
case OP_ENTERTRY: /* cx->blk_eval.retop */
case OP_GOTO: /* cx->blk_sub.retop or CvSTART(cv)|autoload or findlabel */
case OP_LEAVEEVAL: /* ? */
#if PERL_VERSION > 8
case OP_CONTINUE: /* cx->blk_givwhen.leaveop */
case OP_BREAK: /* ? */
#endif
return 1;
default:
return 0;
}
}
CODE *
call_abs (CODE *code, void *addr) {
/* intel specific: */
register signed long rel = (CODE*)addr - code - sizeof(CALL) - CALL_SIZE;
if ((CALL_ALIGN == 4) && (rel > (unsigned int)PERL_ULONG_MAX)) {
PUSHc(JMP);
PUSHcall(addr);
/* 386 far calls prefer 2 nop's afterwards, align it to 4 (0,4,8,c)*/
#if CALL_ALIGN
while (((unsigned int)&code | 0xfffffff0) % CALL_ALIGN) { *(code++) = NOP[0]; }
#endif
} else {
U32 urel = (U32)rel;
PUSHc(CALL);
PUSHcall(urel);
}
return code;
}
/* Search dynamically (in Jit) the code target for op in jmptargets */
CODE *jmp_search_label(OP* op) {
int ix = jmpix;
while (ix-- > 0) {
if (jmptargets[ix].op == op) { /* no loop */
return jmptargets[ix].target;
}
}
return (CODE*)0;
}
long
jit_chain(pTHX_
OP *op,
CODE *code,
CODE *code_start,
int jumpsize,
OP *stopop
#ifdef DEBUGGING
,FILE *fh, FILE *stabs
#endif
)
{
int dryrun = !code;
long size = 0;
OP *startop = op;
OP *opnext;
#ifdef DEBUGGING
static int line = 3;
char *opname;
if (!dryrun) {
opname = (char*)PL_op_name[op->op_type];
fprintf(fh, "/* block jit_chain_%d op 0x%x pp_%s; */\n",
local_chains, PTR2X(op), opname);
local_chains++;
line++;
}
#endif
do {
opnext = op->op_next;
#ifdef DEBUGGING
if (!dryrun) {
opname = (char*)PL_op_name[op->op_type];
DEBUG_v( printf("# pp_%s \t= 0x%x / 0x%x\n", opname,
PTR2X(op->op_ppaddr),
PTR2X(op)));
}
# if defined(DEBUG_s_TEST_)
if (DEBUG_s_TEST_) {
if (dryrun) {
size += sizeof(CALL) + CALL_SIZE;
} else {
CALL_ABS(&Perl_debstack);
dbg_lines("debstack();");
}
}
# endif
# if defined(DEBUG_t_TEST_) && defined(HAVE_DEBOP)
if (DEBUG_t_TEST_ && op) {
T_CHARARR push_arg1[] = { push_arg1_mem };
# ifdef USE_ITHREADS
T_CHARARR push_arg2[] = { push_arg2_mem };
# endif
if (dryrun) {
PUSH_1ARG_DRYRUN;
size += PUSH_SIZE + sizeof(CALL) + CALL_SIZE;
} else {
PUSH_1ARG;
PUSHabs(op);
CALL_ABS(&Perl_debop);
DEBUG_v( printf("# debop(%x) %s\n", PTR2X(op),
(char*)PL_op_name[op->op_type]));
dbg_lines("debop(PL_op);");
}
}
# endif
#endif
if (op->op_type == OP_NULL) continue;
/* check labels -> store possible jmp targets.
Note: nextstate can also be normal jmp target as enter */
if (!dryrun) {
if ((op->op_type == OP_NEXTSTATE)|(op->op_type == OP_DBSTATE)) {
char *label = NULL;
JMPTGT cx;
#ifdef CopLABEL
label = (char *)CopLABEL(cCOPx(op));
#else
label = cCOPx(op)->cop_label;
#endif
if (label && *label) {
cx.op = op;
cx.label = label;
cx.target = code;
DEBUG_v( printf("# push jmp label %s at 0x%x for nextstate 0x%x\n", label,
PTR2X(code), PTR2X(op)));
PUSH_JMP(cx);
} else {
cx.op = op;
cx.label = NULL;
cx.target = code;
DEBUG_v( printf("# push jmp at 0x%x for nextstate op=0x%x\n",
PTR2X(code - code_start), PTR2X(op)));
PUSH_JMP(cx);
}
if (op->op_type == OP_ENTER) {
JMPTGT cx;
cx.op = op;
cx.label = NULL;
cx.target = code;
DEBUG_v( printf("# push jmp at 0x%x for enter op=0x%x\n",
PTR2X(code - code_start), PTR2X(op)));
PUSH_JMP(cx);
}
}
}
/* XXX TODO
* We have almost no chance to get the CvSTART of the sub here, better in the parser.
* But we can try checking a CvSTART:
* - On XS functions the CvSTART is added dynamically at run-time,
* but it makes no sense to jit already compiled XS code. Good.
* - autoloaded non-xs code would be good to jit, but we'd need
* a run-time check for the CvSTART, and Jit it at run-time.
* For now we can only call unjitted functions.
* We'd have to call entersub, but then we'd need the prev.
* context (name in the prev gv),
* ... and we cannot call the chain at compile-time.
*/
if (0 && (op->op_type == OP_ENTERSUB)) {
SV *sv; /* arg from the previous GV */
CV* cv;
/* first jit the sub, then loop through it.
loop CvROOT until !PL_op */
/*OP* next = Perl_pp_entersub(aTHX); / * find CvSTART */
OP* next = cUNOPx(op)->op_first;
if (next) {
next = next->op_next;
if (dryrun) {
size += JIT_CHAIN_FULL_DRYRUN(next, opnext);
} else {
long lsize;
DEBUG_v( printf("# entersub() => op=0x%x\n", PTR2X(next)));
dbg_lines1("sub_%d: {", global_label);
lsize = JIT_CHAIN_FULL_DRYRUN(next, opnext);
code = JIT_CHAIN_FULL(next, lsize, opnext);
dbg_lines("}");
dbg_lines1("next_%d:", global_label);
}
}
}
if (maybranch(op)) {
if (dryrun) {
size += sizeof(maybranch_plop);
} else {
/* store op->next at 0(%esp). Later cmp returned op == op->next => jmp */
DEBUG_v( printf("# maybranch %s 0x%p:\n", opname, op->op_ppaddr));
dbg_lines("op = PL_op->op_next;");
code = push_maybranch_plop(code, opnext);
}
}
if (dryrun) {
size += sizeof(CALL) + CALL_SIZE;
} else {
#ifdef USE_ITHREADS
dbg_cline1("my_perl->Iop = Perl_pp_%s(my_perl);\n", opname);
#else
dbg_cline1("PL_op = Perl_pp_%s();\n", opname);
#endif
dbg_stabs1("call pp_%s", opname);
CALL_ABS(op->op_ppaddr);
dbg_stabs("PL_op = eax");
}
if (dryrun) {
size += sizeof(SAVE_PLOP);
} else {
PUSHc(SAVE_PLOP);
}
#ifdef HAVE_DISPATCH
if (DISPATCH_NEEDED(op)) {
if (!dryrun) {
# ifdef USE_ITHREADS
dbg_lines("if (my_perl->Isig_pending)");
dbg_lines(" Perl_despatch_signals(my_perl);");
# else
dbg_lines("if (PL_sig_pending)");
dbg_lines(" Perl_despatch_signals();");
# endif
}
# ifdef DISPATCH_GETSIG
if (dryrun) {
size += sizeof(DISPATCH_GETSIG);
size += MOV_SIZE;
} else {
PUSHc(DISPATCH_GETSIG);
PUSHrel(&PL_sig_pending);
}
# endif
if (dryrun) {
size += sizeof(DISPATCH) + sizeof(CALL) + CALL_SIZE;
} else {
PUSHc(DISPATCH);
CALL_ABS(&Perl_despatch_signals);
}
}
#endif
/* other before next */
if (maybranch(op)) {
int lsize;
SV *keysv = newSViv(PTR2IV(op));
/* XXX avoid cyclic loops of already jitted other ops:
and => nextstate, cond_expr => enter, ... */
if (!otherops) {
otherops = newHV();
otherops1 = newHV();
}
if (dryrun) {
if (hv_exists_ent(otherops1, keysv, 0)) {
goto NEXT;
} else {
hv_store_ent(otherops1, keysv, newSViv(PTR2IV(code)), 0);
}
} else {
if (hv_exists_ent(otherops, keysv, 0)) {
DEBUG_v( printf("# %s 0x%x already jitted, code=0x%x\n", PL_op_name[op->op_type],
PTR2X(op), PTR2X(code)));
/* XXX when is a jmp to this op needed? patch later or use the code addr from the hash */
goto NEXT;
} else {
hv_store_ent(otherops, keysv, newSViv(PTR2IV(code)), 0);
}
/* dbg_lines1("if (PL_op == op) goto next_%d;", global_label); */
}
if (((PL_opargs[op->op_type] & OA_CLASS_MASK) == OA_LOGOP)
&& (op->op_type != OP_ENTERTRY))
{
if (dryrun) {
int i = JIT_CHAIN_DRYRUN(cLOGOPx(op)->op_other);
size += i + sizeof_maybranch_check(i);
size += sizeof(GOTOREL);
} else {
long next, other;
LOGOP* logop;
logop = cLOGOPx(op);
DEBUG_v( printf("# other_%d: %s => %s, ", global_label,
PL_op_name[op->op_type],
PL_op_name[cLOGOPx(op)->op_other->op_type]));
other = JIT_CHAIN_DRYRUN(logop->op_other); /* sizeof other */
other += sizeof(GOTOREL);
code = push_maybranch_check(code, other); /* if cmp: je => next */
dbg_lines1("if (PL_op != op) goto next_%d;", global_label);
DEBUG_v( printf("size=%x\n", PTR2X(other)) );
code = JIT_CHAIN(logop->op_other);
dbg_cline1("/*goto logop_%d;*/", global_label);
dbg_lines1("next_%d:", global_label);
next = JIT_CHAIN_DRYRUN(logop->op_next); /* sizeof next */
DEBUG_v( printf("# next_%d: %s, size=%x\n", global_label,
PL_op_name[logop->op_next->op_type], next));
code = push_gotorel(code, next);
/*dbg_lines1("logop_%d:", global_label);*/
}
} else { /* special branches */
int next;
T_CHARARR push_arg1[] = { push_arg1_mem };
# ifdef USE_ITHREADS
T_CHARARR push_arg2[] = { push_arg2_mem };
# endif
switch (op->op_type) { /* sync this list with B::CC */
case OP_FLIP:
DEBUG_v( printf("# flip_%d\n", global_label));
if ((op->op_flags & OPf_WANT) == OPf_WANT_LIST) {
/* need to check the returned op at runtime */
LOGOP* logop;
logop = cLOGOPx(cUNOPx(op)->op_first);
if (dryrun) {
int i = JIT_CHAIN_DRYRUN(logop->op_other);
size += i + sizeof_maybranch_check(i);
size += sizeof(GOTOREL);
} else {
int other;
other = JIT_CHAIN_DRYRUN(logop->op_other); /* sizeof other */
other += sizeof(GOTOREL);
code = push_maybranch_check(code, other); /* if cmp: je => next */
DEBUG_v( printf("# other_%d: %s, size=%x\n", global_label,
PL_op_name[logop->op_other->op_type], PTR2X(other)));
code = JIT_CHAIN(logop->op_other);
}
}
break;
/* store and jump to labels. */
case OP_ENTERLOOP:
case OP_ENTERITER:
if (!dryrun) {
int nextop, lastop, redoop;
LOOPTGT cx;
LOOP* loop;
loop = cLOOPx(op);
/* XXX Need to store away the branch targets in jmptargets, otherwise
unjitted code is executed.
We can also try to patchup the jmps afterwards.
*/
DEBUG_v( printf("# %s_%d:\n", PL_op_name[loop->op_type], global_label));
/* After each chain jump to the end, so we need all sizes. */
nextop = JIT_CHAIN_DRYRUN(loop->op_nextop); /* sizeof other */
lastop = JIT_CHAIN_DRYRUN(loop->op_lastop);
redoop = JIT_CHAIN_DRYRUN(loop->op_redoop);
lsize = nextop + lastop + redoop + 3*(sizeof(CALL)+CALL_SIZE);
dbg_lines1("goto branch_%d;", global_label);
code = push_gotorel(code, lsize); /* jump to end: nextop+lastop+redoop+3*goto */