| 1 |
|
| /* Jit.xs -*- mode:C c-basic-offset:4 -*- |
| 2 |
|
| * |
| 3 |
|
| * JIT (Just-in-time compile) the Perl5 runloop. |
| 4 |
|
| * Currently for intel x86 32+64bit. More CPU's later. |
| 5 |
|
| * Status: |
| 6 |
|
| * Works only for simple i386 and amd64, |
| 7 |
|
| * without maybranch ops (return op_other, op_last, ... ignored) |
| 8 |
|
| * |
| 9 |
|
| * Copyright (C) 2010 by Reini Urban |
| 10 |
|
| * You may distribute under the terms of either the GNU General Public |
| 11 |
|
| * License or the Artistic License, as specified in the README file. |
| 12 |
|
| * |
| 13 |
|
| * http://gist.github.com/331867 |
| 14 |
|
| * http://search.cpan.org/dist/Jit/ |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #include <EXTERN.h> |
| 18 |
|
| #include <perl.h> |
| 19 |
|
| #include <XSUB.h> |
| 20 |
|
| |
| 21 |
|
| #ifndef _WIN32 |
| 22 |
|
| #include <sys/mman.h> |
| 23 |
|
| #endif |
| 24 |
|
| #if defined(DEBUGGING) && defined(__GNUC__) |
| 25 |
|
| #include <sys/stat.h> |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
|
| #if defined(HAVE_LIBDISASM) && defined(DEBUGGING) |
| 29 |
|
| #include <libdis.h> |
| 30 |
|
| #endif |
| 31 |
|
| |
| 32 |
|
| typedef unsigned char CODE; |
| 33 |
|
| #define T_CHARARR static CODE |
| 34 |
|
| #undef JIT_CPU |
| 35 |
|
| /* if dealing with doubles on sse we want this */ |
| 36 |
|
| #define ALIGN_16(c) (c%16?(c+(16-c%16)):c) |
| 37 |
|
| #define ALIGN_64(c) (c%64?(c+(64-c%64)):c) |
| 38 |
|
| #define ALIGN_N(n,c) (c%n?(c+(n-c%n)):c) |
| 39 |
|
| |
| 40 |
|
| HV* otherops = NULL; |
| 41 |
|
| HV* otherops1 = NULL; |
| 42 |
|
| typedef struct jmptarget { |
| 43 |
|
| OP *op; /* the target op */ |
| 44 |
|
| char *label; /* XXX not sure if need this */ |
| 45 |
|
| CODE *target; /* the code points for label (run-time lookup for goto XS and goto label) */ |
| 46 |
|
| } JMPTGT; |
| 47 |
|
| int jmpix = 0; |
| 48 |
|
| JMPTGT *jmptargets = NULL; |
| 49 |
|
| |
| 50 |
|
| typedef struct loopstack { |
| 51 |
|
| CODE *nextop; /* the 3 loop targets */ |
| 52 |
|
| CODE *lastop; |
| 53 |
|
| CODE *redoop; |
| 54 |
|
| } LOOPTGT; |
| 55 |
|
| int loopix = 0; |
| 56 |
|
| LOOPTGT *looptargets = NULL; |
| 57 |
|
| |
| 58 |
|
| #define PUSH_JMP(jmp) \ |
| 59 |
|
| jmptargets = (JMPTGT*)realloc(jmptargets, (jmpix+1)*sizeof(JMPTGT)); \ |
| 60 |
|
| memcpy(&jmptargets[jmpix], &jmp, sizeof(JMPTGT)); jmpix++ |
| 61 |
|
| #define POP_JMP (jmpix >= 0 ? &jmptargets[jmpix--] : NULL) |
| 62 |
|
| #define PUSH_LOOP(cx) \ |
| 63 |
|
| looptargets = (LOOPTGT*)realloc(looptargets, (loopix+1)*sizeof(LOOPTGT)); \ |
| 64 |
|
| memcpy(&looptargets[loopix], &cx, sizeof(LOOPTGT)); loopix++ |
| 65 |
|
| #define POP_LOOP (loopix >= 0 ? &looptargets[loopix--] : NULL) |
| 66 |
|
| |
| 67 |
|
| #ifdef DEBUGGING |
| 68 |
|
| int global_label; |
| 69 |
|
| int global_loops = 0; |
| 70 |
|
| int local_chains = 0; |
| 71 |
|
| #endif |
| 72 |
|
| |
| 73 |
|
| int dispatch_needed(OP* op); |
| 74 |
|
| int maybranch(OP* op); |
| 75 |
|
| CODE *push_prolog(CODE *code); |
| 76 |
|
| long jit_chain(pTHX_ OP* op, CODE *code, CODE *code_start, |
| 77 |
|
| int jumpsize, OP* stopop |
| 78 |
|
| #ifdef DEBUGGING |
| 79 |
|
| ,FILE *fh, FILE *stabs |
| 80 |
|
| #endif |
| 81 |
|
| ); |
| 82 |
|
| /* search dynamically the code target for op in jmptargets */ |
| 83 |
|
| CODE *jmp_search_label(OP* op); |
| 84 |
|
| |
| 85 |
|
| /* When do we need PERL_ASYNC_CHECK? |
| 86 |
|
| * if (dispatch_needed(op)) if (PL_sig_pending) Perl_despatch_signals(); |
| 87 |
|
| * |
| 88 |
|
| * Until 5.13.2 we had it after each and every op, |
| 89 |
|
| * since 5.13.2 only inside certain ops, |
| 90 |
|
| * which need to handle pending signals. |
| 91 |
|
| * In 5.6 it was a NOOP. |
| 92 |
|
| */ |
| 93 |
|
| /*#define _BYPASS_DISPATCH*/ |
| 94 |
|
| #if (PERL_VERSION > 6) && (PERL_VERSION < 13) |
| 95 |
|
| #define HAVE_DISPATCH |
| 96 |
|
| #define DISPATCH_NEEDED(op) dispatch_needed(op) |
| 97 |
|
| #else |
| 98 |
|
| #undef HAVE_DISPATCH |
| 99 |
|
| #define DISPATCH_NEEDED(op) 0 |
| 100 |
|
| #endif |
| 101 |
|
| |
| 102 |
|
| #ifdef DEBUGGING |
| 103 |
|
| # define JIT_CHAIN_DRYRUN(op) jit_chain(aTHX_ op, NULL, NULL, 0, NULL, fh, stabs) |
| 104 |
|
| # define JIT_CHAIN_DRYRUN_FULL(op,stopop) jit_chain(aTHX_ op, NULL, NULL, 0, stopop, fh, stabs) |
| 105 |
|
| # define JIT_CHAIN(op) (CODE*)jit_chain(aTHX_ op, code, code_start, 0, NULL, fh, stabs) |
| 106 |
|
| # define JIT_CHAIN_FULL(op, size, stopop) \ |
| 107 |
|
| (CODE*)jit_chain(aTHX_ op, code, code_start, size, stopop, fh, stabs) |
| 108 |
|
| # define DEB_PRINT_LOC(loc) printf(loc" \t= 0x%x\n", loc) |
| 109 |
|
| # if PERL_VERSION < 8 |
| 110 |
|
| # define DEBUG_v(x) x |
| 111 |
|
| # endif |
| 112 |
|
| #else |
| 113 |
|
| # define JIT_CHAIN_DRYRUN(op) jit_chain(aTHX_ op, NULL, NULL, 0, NULL) |
| 114 |
|
| # define JIT_CHAIN_FULL_DRYRUN(op, stopop) jit_chain(aTHX_ op, NULL, NULL, 0, stopop) |
| 115 |
|
| # define JIT_CHAIN(op) (CODE*)jit_chain(aTHX_ op, code, code_start, 0, NULL) |
| 116 |
|
| # define JIT_CHAIN_FULL(op, size, stopop) \ |
| 117 |
|
| (CODE*)jit_chain(aTHX_ op, code, code_start, size, stopop) |
| 118 |
|
| # define DEB_PRINT_LOC(loc) |
| 119 |
|
| # if PERL_VERSION < 8 |
| 120 |
|
| # define DEBUG_v(x) |
| 121 |
|
| # endif |
| 122 |
|
| #endif |
| 123 |
|
| |
| 124 |
|
| #ifdef USE_ITHREADS |
| 125 |
|
| /* first arg already my_perl, and already on stack */ |
| 126 |
|
| # define PUSH_1ARG_DRYRUN size += sizeof(push_arg2) |
| 127 |
|
| # define PUSH_1ARG PUSHc(push_arg2) |
| 128 |
|
| #else |
| 129 |
|
| # define PUSH_1ARG_DRYRUN size += sizeof(push_arg1) |
| 130 |
|
| # define PUSH_1ARG PUSHc(push_arg1) |
| 131 |
|
| #endif |
| 132 |
|
| |
| 133 |
|
| /* |
| 134 |
|
| C pseudocode of the Perl runloop: |
| 135 |
|
| |
| 136 |
|
| not-threaded: |
| 137 |
|
| |
| 138 |
|
| OP *op; |
| 139 |
|
| int *p = &Perl_Isig_pending_ptr; |
| 140 |
|
| if maybranch: |
| 141 |
|
| op = PL_op->op_next; # save away at ecx to check |
| 142 |
|
| PL_op = Perl_pp_opname(); # returns op_next, op_other, op_first, op_last |
| 143 |
|
| # or a new optree start |
| 144 |
|
| if (dispatch_needed) |
| 145 |
|
| if (*p) Perl_despatch_signals(); |
| 146 |
|
| |
| 147 |
|
| if (PL_op == op) #if maybranch label other targets |
| 148 |
|
| goto next_1; |
| 149 |
|
| # assemble op_other chain until 0 |
| 150 |
|
| PL_op = Perl_pp_opname(); |
| 151 |
|
| ... |
| 152 |
|
| goto leave_1; |
| 153 |
|
| next_1: #continue with op_next |
| 154 |
|
| ... |
| 155 |
|
| leave_1: |
| 156 |
|
| Perl_pp_leave() |
| 157 |
|
| return |
| 158 |
|
| |
| 159 |
|
| else: |
| 160 |
|
| |
| 161 |
|
| thisop: |
| 162 |
|
| PL_op = Perl_pp_opname(); #returns op->next |
| 163 |
|
| if dispatch_needed: if (*p) Perl_despatch_signals(); |
| 164 |
|
| nextop: |
| 165 |
|
| |
| 166 |
|
| |
| 167 |
|
| threaded, same logic as above, just: |
| 168 |
|
| |
| 169 |
|
| my_perl->Iop = PL_op->op_ppaddr(my_perl); |
| 170 |
|
| if (my_perl->Isig_pending) Perl_despatch_signals(my_perl); |
| 171 |
|
| |
| 172 |
|
| */ |
| 173 |
|
| |
| 174 |
|
| #define CALL_SIZE 4 /* size for the call instruction arg */ |
| 175 |
|
| #define MOV_SIZE 4 /* size for the mov instruction arg */ |
| 176 |
|
| |
| 177 |
|
| /* multi without threads yet unhandled. should be a simple s/USE_ITHREADS/MULTIPLICITY/ */ |
| 178 |
|
| #if defined(MULTIPLICITY) && !defined(USE_ITHREADS) |
| 179 |
|
| # error "MULTIPLICITY without ITHREADS not supported" |
| 180 |
|
| #endif |
| 181 |
|
| #if defined(USE_THREADS) && !defined(USE_ITHREADS) |
| 182 |
|
| # error "USE_THREADS without ITHREADS not supported" |
| 183 |
|
| #endif |
| 184 |
|
| #ifdef USE_ITHREADS |
| 185 |
|
| /* threads: offsets are perl version and ptrsize dependent */ |
| 186 |
|
| # define IOP_OFFSET PTRSIZE /* my_perl->Iop_pending offset */ |
| 187 |
|
| # define SIG_PENDING_OFFSET 4*PTRSIZE /* my_perl->Isig_pending offset */ |
| 188 |
|
| # define PPADDR_OFFSET (IOP_OFFSET+2*PTRSIZE) |
| 189 |
|
| #else |
| 190 |
|
| # define PPADDR_OFFSET (2*PTRSIZE) |
| 191 |
|
| #endif |
| 192 |
|
| |
| 193 |
|
| |
| 194 |
|
| #define CALL_ABS(abs) code = call_abs(code,abs) |
| 195 |
|
| /*(U32)((unsigned char*)abs-code-3)*/ |
| 196 |
|
| #define PUSHc(what) memcpy(code,what,sizeof(what)); code += sizeof(what) |
| 197 |
|
| /* force 4 byte for U32, 64bit uses 8 byte for U32, but 4 byte for call near */ |
| 198 |
|
| #define PUSHcall(what) memcpy(code,&what,CALL_SIZE); code += CALL_SIZE |
| 199 |
|
| #define PUSHbyte(byte) { signed char b = (byte); *code++ = b; } |
| 200 |
|
| |
| 201 |
|
| #ifdef DEBUGGING |
| 202 |
|
| # define dbg_cline(p1) fprintf(fh, p1); line++ |
| 203 |
|
| # define dbg_cline1(p1,p2) fprintf(fh, p1, p2); line++ |
| 204 |
|
| #else |
| 205 |
|
| # define dbg_cline(p1) |
| 206 |
|
| # define dbg_cline1(p1,p2) |
| 207 |
|
| #endif |
| 208 |
|
| |
| 209 |
|
| #if defined(__GNUC__) && defined(DEBUGGING) |
| 210 |
|
| # define dbg_stabs(s) fprintf(stabs, ".stabn 68,0,%d,%d /* "s" */\n", line, code-code_start) |
| 211 |
|
| # define dbg_stabs1(s,p1) fprintf(stabs, ".stabn 68,0,%d,%d /* "s" */\n", line, code-code_start, p1) |
| 212 |
|
| # define dbg_lines(s) dbg_cline(s"\n"); dbg_stabs(s) |
| 213 |
|
| # define dbg_lines1(s, p1) dbg_cline1(s"\n", p1); dbg_stabs1(s, p1) |
| 214 |
|
| #else |
| 215 |
|
| # define dbg_stabs(s) |
| 216 |
|
| # define dbg_stabs1(s,p1) |
| 217 |
|
| # ifdef DEBUGGING |
| 218 |
|
| # define dbg_lines(s) dbg_cline(s"\n"); |
| 219 |
|
| # define dbg_lines1(s, p1) dbg_cline1(s"\n", p1); |
| 220 |
|
| # else |
| 221 |
|
| # define dbg_lines(s) |
| 222 |
|
| # define dbg_lines1(s, p1) |
| 223 |
|
| # endif |
| 224 |
|
| #endif |
| 225 |
|
| |
| 226 |
|
| |
| 227 |
|
| /* __amd64 defines __x86_64 */ |
| 228 |
|
| #if defined(__x86_64__) || defined(__amd64) || defined(__amd64__) || defined(_M_X64) |
| 229 |
|
| #define JIT_CPU "amd64" |
| 230 |
|
| #define JIT_CPU_AMD64 |
| 231 |
|
| #define CALL_ALIGN 0 |
| 232 |
|
| #define MOV_REL |
| 233 |
|
| #define PUSH_SIZE 4 /* size for the push instruction arg 4/8 */ |
| 234 |
|
| |
| 235 |
|
| #define PUSHabs(what) memcpy(code,what,PUSH_SIZE); code += PUSH_SIZE |
| 236 |
|
| #define PUSHrel(where) { \ |
| 237 |
|
| U32 r = (CODE*)where - (code+MOV_SIZE); \ |
| 238 |
|
| memcpy(code,&r,MOV_SIZE); code += MOV_SIZE; \ |
| 239 |
|
| } |
| 240 |
|
| /*void f_PUSHrel(CODE* code, void *where);*/ |
| 241 |
|
| /*#define PUSHrel(where) f_PUSHrel(code,(void*)where); code += MOV_SIZE;*/ |
| 242 |
|
| #define revword(m) (((unsigned long)m)&0xff),((((unsigned long)m)&0xff00)>>8), \ |
| 243 |
|
| ((((unsigned long)m)&0xff0000)>>16),((((unsigned long)m)&0xff000000)>>24), \ |
| 244 |
|
| ((((unsigned long)m)&0xff00000000)>>32),((((unsigned long)m)&0xff00000000)>>40), \ |
| 245 |
|
| ((((unsigned long)m)&0xff0000000000)>>48),((((unsigned long)m)&0xff0000000000)>>56) |
| 246 |
|
| #define revword4(m) (((unsigned int)m)&0xff),((((unsigned int)m)&0xff00)>>8), \ |
| 247 |
|
| ((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff000000)>>24) |
| 248 |
|
| |
| 249 |
|
| T_CHARARR NOP[] = {0x90}; /* nop */ |
| 250 |
|
| |
| 251 |
|
| /* PROLOG */ |
| 252 |
|
| #define enter 0xc8 |
| 253 |
|
| #define enter_8 0xc8,0x08,0x00,0x00 |
| 254 |
|
| #define push_rcx 0x51 |
| 255 |
|
| #define push_rbx 0x53 |
| 256 |
|
| #define push_rbp 0x55 |
| 257 |
|
| #define mov_rsp_rbp 0x48,0x89,0xe5 |
| 258 |
|
| #define push_r12 0x41,0x54 |
| 259 |
|
| #define mov_rax_rbx 0x48,0x89,0xc3 |
| 260 |
|
| #define sub_x_rsp(byte) 0x48,0x83,0xec,byte |
| 261 |
|
| #define add_x_esp(byte) 0x48,0x83,0xc4,byte |
| 262 |
|
| #define fourbyte 0x00,0x00,0x00,0x00 |
| 263 |
|
| #define mov_mem_eax(m) 0xa1,revword(m) |
| 264 |
|
| /* mov $memabs,(%ebx) &PL_op in ebx */ |
| 265 |
|
| #define mov_mem_rbx 0x48,0x8b,0x1d /* mov &PL_op,%rbx */ |
| 266 |
|
| #define mov_rebp_ebx(byte) 0x8b,0x5d,byte /* mov 0x8(%ebp),%ebx*/ |
| 267 |
|
| #define mov_rrsp_rbx 0x48,0x8b,0x1c,0x24 /* mov (%rsp),%rbx ; my_perl from stack to rbx */ |
| 268 |
|
| |
| 269 |
|
| #define mov_mem_rcx 0x48,0x8b,0x0d /* mov &PL_sig_pending,%rcx */ |
| 270 |
|
| #define test_rcx_rcx 0x48,0x85,0xc9 |
| 271 |
|
| #define test_ecx_ecx 0x85,0xc9 |
| 272 |
|
| |
| 273 |
|
| #define push_imm_0 0x68 |
| 274 |
|
| #define push_imm(m) 0x68,revword(m) |
| 275 |
|
| /* gcc __fastcall amd64 convention for param 1-2 passing */ |
| 276 |
|
| #define mov_mem_esi 0xbe /* arg2 */ |
| 277 |
|
| #define mov_mem_edi 0xbf /* arg1 */ |
| 278 |
|
| #define mov_mem_ecx 0xb9 /* arg1 */ |
| 279 |
|
| #define mov_eax_edi 0x89,0xc7 |
| 280 |
|
| #define mov_eax_esi 0x89,0xc6 |
| 281 |
|
| #define mov_eax_ecx 0x89,0xc1 |
| 282 |
|
| #define mov_eax_edx 0x89,0xc2 |
| 283 |
|
| #define mov_rax_rdi 0x48,0x89,0xc7 |
| 284 |
|
| #define mov_rax_rsi 0x48,0x89,0xc6 |
| 285 |
|
| #define mov_rax_rcx 0x48,0x89,0xc1 |
| 286 |
|
| #define mov_rax_rdx 0x48,0x89,0xc2 |
| 287 |
|
| |
| 288 |
|
| #ifndef _WIN64 |
| 289 |
|
| #define push_arg1_mem mov_mem_edi /* if call via register */ |
| 290 |
|
| #define push_arg2_mem mov_mem_esi |
| 291 |
|
| #define push_arg1_eax mov_rax_rdi |
| 292 |
|
| #define push_arg2_eax mov_rax_rsi |
| 293 |
|
| #else |
| 294 |
|
| /* Win64 Visual C __fastcall uses rcx,rdx for the first int args, not rdi,rsi |
| 295 |
|
| We use less than 2GB for our vars and subs. |
| 296 |
|
| */ |
| 297 |
|
| #define mov_mem_edx 0xba /* arg2 */ |
| 298 |
|
| #define push_arg1_mem mov_mem_ecx /* if call via register */ |
| 299 |
|
| #define push_arg2_mem mov_mem_edx |
| 300 |
|
| #define push_arg1_eax mov_rax_rcx |
| 301 |
|
| #define push_arg2_eax mov_rax_rdx |
| 302 |
|
| #endif |
| 303 |
|
| |
| 304 |
|
| #ifndef _WIN64 |
| 305 |
|
| #define mov_rbx_arg1 0x48,0x89,0xdf /* my_perl => arg1 in rdi */ |
| 306 |
|
| #else |
| 307 |
|
| #define mov_rbx_arg1 0x48,0x89,0xdf /* my_perl => arg1 in rcx */ |
| 308 |
|
| #endif |
| 309 |
|
| #define mov_rebx_mem 0x48,0x89,0x1d /* movq (%ebx), &PL_op */ |
| 310 |
|
| #define mov_mem_rebx 0x48,0xc7,0x03 /* movq &PL_op, (%ebx) */ |
| 311 |
|
| #define mov_eax_rebx 0x89,0x03 /* movq %rax,(%rbx) &PL_op in ebx */ |
| 312 |
|
| /* &PL_op in -4(%ebp) */ |
| 313 |
|
| /* #define mov_mem_4ebp 0xc7,0x45,0xfc */ |
| 314 |
|
| |
| 315 |
|
| /* EPILOG */ |
| 316 |
|
| #define pop_rcx 0x59 |
| 317 |
|
| #define pop_rbx 0x5b |
| 318 |
|
| #define pop_r12 0x41,0x5c |
| 319 |
|
| #define leave 0xc9 |
| 320 |
|
| #define ret 0xc3 |
| 321 |
|
| |
| 322 |
|
| /* maybranch: */ |
| 323 |
|
| /* &op in -8(%ebp) */ |
| 324 |
|
| #define mov_eax_8ebp 0x89,0x45,0xf8 |
| 325 |
|
| #define cltq 0x48,0x98 |
| 326 |
|
| #define mov_0_rax 0xb8,0x00,0x00,0x00,0x00 |
| 327 |
|
| |
| 328 |
|
| #define call 0xe8 /* + 4 rel */ |
| 329 |
|
| #define ljmp(abs) 0xff,0x25 /* + 4 memabs */ |
| 330 |
|
| |
| 331 |
|
| #define mov_mem_rrsp 0xc7,0x04,0x24 /* store op->next on stack */ |
| 332 |
|
| #define mov_eax_4ebp 0x89,0x45,0xfc |
| 333 |
|
| #define mov_mem_r12 0x41,0xbc /* movq PL_op->next, %r12 */ |
| 334 |
|
| #define cmp_rax_r12 0x49,0x39,0xc4 |
| 335 |
|
| |
| 336 |
|
| #define je_0 0x74 |
| 337 |
|
| #define je(byte) 0x74,(byte) |
| 338 |
|
| #define jew_0 0x0f,0x84 |
| 339 |
|
| #define jew(word) 0x0f,0x84,revword4(word) |
| 340 |
|
| #define jmpq_0 0xe9 /* maybranch */ |
| 341 |
|
| #define jmpq(word) 0xe9,revword(word) |
| 342 |
|
| |
| 343 |
|
| /* mov %rax,(%rbx) &PL_op in ebx */ |
| 344 |
|
| #define mov_rax_memr 0x48,0x89,0x05 /* + 4 rel */ |
| 345 |
|
| #define mov_eax_rebx 0x89,0x03 |
| 346 |
|
| #define mov_4ebp_edx 0x8b,0x55,0xfc |
| 347 |
|
| #define mov_reax_ebx 0x48,0x8b,0x18 |
| 348 |
|
| #define mov_redx_eax 0x82,0x02 |
| 349 |
|
| #define test_eax_eax 0x85,0xc0 |
| 350 |
|
| /* skip call _Perl_despatch_signals */ |
| 351 |
|
| #define mov_mem_rebp8 0xc7,0x45,0xf8 /* mov &op,-8(%rbp) */ |
| 352 |
|
| #define cmp_eax_rebp8 0x39,0x45,0xf8 /* cmp %eax,-8(%rbp) */ |
| 353 |
|
| #if 0 |
| 354 |
|
| #define mov_mem_resp1 0xc7,0x44,0x24,0x0fc |
| 355 |
|
| #define cmp_eax_resp1 0x39,0x44,0x24,0xfc /* 4 byte cmp %eax,4(%rsp) */ |
| 356 |
|
| #endif |
| 357 |
|
| /*#define cmp_rax_rrsp 0x48,0x39,0xe0 *//* 8 byte */ |
| 358 |
|
| /*#define test_rax_rax 0x48,0x85,0xc0*/ |
| 359 |
|
| |
| 360 |
|
| #ifdef USE_ITHREADS |
| 361 |
|
| # include "amd64thr.c" |
| 362 |
|
| #else |
| 363 |
|
| # include "amd64.c" |
| 364 |
|
| #endif |
| 365 |
|
| |
| 366 |
|
| #endif /* EOF amd64 */ |
| 367 |
|
| |
| 368 |
|
| #if !defined(JIT_CPU) && (defined(__i386__) || defined(__i386) || defined(_M_IX86)) |
| 369 |
|
| #define JIT_CPU "i386" |
| 370 |
|
| #define JIT_CPU_X86 |
| 371 |
|
| #define CALL_ALIGN 4 |
| 372 |
|
| #undef MOV_REL |
| 373 |
|
| #define PUSH_SIZE 4 /* size for the push instruction arg 4/8 */ |
| 374 |
|
| |
| 375 |
|
| #define PUSHabs(what) memcpy(code,what,PUSH_SIZE); code += PUSH_SIZE |
| 376 |
|
| #define PUSHrel(what) memcpy(code,what,MOV_SIZE); code += MOV_SIZE |
| 377 |
|
| #define revword(m) (((unsigned int)m)&0xff),((((unsigned int)m)&0xff00)>>8), \ |
| 378 |
|
| ((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff000000)>>24) |
| 379 |
|
| #define absword(m) ((((unsigned int)m)&0xff000000)>>24), \ |
| 380 |
|
| ((((unsigned int)m)&0xff0000)>>16),((((unsigned int)m)&0xff00)>>8),(((unsigned int)m)&0xff) |
| 381 |
|
| |
| 382 |
|
| T_CHARARR NOP[] = {0x90}; /* nop */ |
| 383 |
|
| #define fourbyte 0x00,0x00,0x00,0x00 |
| 384 |
|
| |
| 385 |
|
| /* PROLOG */ |
| 386 |
|
| #define enter_8 0xc8,0x08,0x00,0x00 |
| 387 |
|
| #define push_ebx 0x53 |
| 388 |
|
| #define push_ebp 0x55 |
| 389 |
|
| #define mov_esp_ebp 0x89,0xe5 |
| 390 |
|
| #define push_edi 0x57 |
| 391 |
|
| #define push_esi 0x56 |
| 392 |
|
| #define push_ecx 0x51 |
| 393 |
|
| #define push_edx 0x52 |
| 394 |
|
| #define sub_x_esp(byte) 0x83,0xec,byte |
| 395 |
|
| #define mov_eax_rebx 0x89,0x03 /* mov %rax,(%rbx) &PL_op in ebx */ |
| 396 |
|
| |
| 397 |
|
| #define push_imm_0 0x68 |
| 398 |
|
| #define push_imm(m) 0x68,revword(m) |
| 399 |
|
| #define push_eax 0x50 |
| 400 |
|
| #define push_arg1_eax push_eax |
| 401 |
|
| #define push_arg2_eax push_eax |
| 402 |
|
| #define push_arg1_mem push_imm_0 |
| 403 |
|
| #define push_arg2_mem push_imm_0 |
| 404 |
|
| |
| 405 |
|
| /* mov $memabs,%eax PL_op in eax */ |
| 406 |
|
| #define mov_mem_eax(m) 0xa1,revword(m) |
| 407 |
|
| /* mov $memabs,%ebx &PL_op in ebx */ |
| 408 |
|
| #define mov_mem_ebx(m) 0xbb,revword(m) |
| 409 |
|
| /* mov $memabs,0(%esp) &PL_op to stack-0 */ |
| 410 |
|
| #define mov_mem_ecx 0xb9 |
| 411 |
|
| /* &PL_sig_pending in -4(%ebp) */ |
| 412 |
|
| #define mov_mem_4ebp(m) 0xc7,0x45,0xfc,revword(m) |
| 413 |
|
| #define mov_4ebp_edx 0x8b,0x55,0xfc |
| 414 |
|
| #define mov_redx_eax 0x8b,0x02 |
| 415 |
|
| #define test_eax_eax 0x85,0xc0 |
| 416 |
|
| #define je_0 0x74 |
| 417 |
|
| #define je(byte) 0x74,(byte) |
| 418 |
|
| |
| 419 |
|
| #define mov_mem_rebp8 0xc7,0x45,0xf8 /* mov &op,-8(%rbp) */ |
| 420 |
|
| #define cmp_eax_rebp8 0x39,0x45,0xf8 /* cmp %eax,,-8(%rbp) */ |
| 421 |
|
| #if 0 |
| 422 |
|
| #define mov_mem_resp1 0xc7,0x44,0x24,0xfc |
| 423 |
|
| #define cmp_eax_resp1 0x39,0x44,0x24,0xfc /* 4 byte cmp %eax,4(%rsp) */ |
| 424 |
|
| #endif |
| 425 |
|
| #define cmp_ecx_eax 0x39,0xc8 |
| 426 |
|
| #define mov_rebp_ebx(byte) 0x8b,0x5d,byte /* mov 0x8(%ebp),%ebx*/ |
| 427 |
|
| #define test_eax_eax 0x85,0xc0 |
| 428 |
|
| |
| 429 |
|
| /* XXX on i386 also? */ |
| 430 |
|
| #define jew_0 0x0f,0x84 |
| 431 |
|
| #define jew(word) 0x0f,0x84,revword4(word) |
| 432 |
|
| |
| 433 |
|
| |
| 434 |
|
| /* EPILOG */ |
| 435 |
|
| #define add_x_esp(byte) 0x83,0xc4,byte /* add $0x4,%esp */ |
| 436 |
|
| #define pop_ecx 0x59 |
| 437 |
|
| #define pop_ebx 0x5b |
| 438 |
|
| #define pop_edx 0x5a |
| 439 |
|
| #define pop_esi 0x5e |
| 440 |
|
| #define pop_edi 0x5f |
| 441 |
|
| #define pop_ebp 0x5d |
| 442 |
|
| #define leave 0xc9 |
| 443 |
|
| #define ret 0xc3 |
| 444 |
|
| |
| 445 |
|
| /* maybranch: */ |
| 446 |
|
| /* &op in -8(%ebp) */ |
| 447 |
|
| #define mov_eax_8ebp 0x89,0x45,0xf8 |
| 448 |
|
| #define mov_eax_4ebp 0x89,0x45,0xfc |
| 449 |
|
| |
| 450 |
|
| #define call 0xe8 /* + 4 rel */ |
| 451 |
|
| #define ljmp(abs) 0xff,0x25 /* + 4 memabs */ |
| 452 |
|
| #define ljmp_0 0xff,0x25 /* + 4 memabs */ |
| 453 |
|
| #define mov_eax_mem 0xa3 /* + 4 memabs */ |
| 454 |
|
| #define jmpb_0 0xeb /* maybranch */ |
| 455 |
|
| #define jmpb(byte) 0xeb,(byte) /* maybranch */ |
| 456 |
|
| #define jmpq_0 0xe9 /* maybranch */ |
| 457 |
|
| #define jmpq(word) 0xe9,revword(word) |
| 458 |
|
| |
| 459 |
|
| #ifdef USE_ITHREADS |
| 460 |
|
| # include "i386thr.c" |
| 461 |
|
| #else |
| 462 |
|
| # include "i386.c" |
| 463 |
|
| #endif |
| 464 |
|
| |
| 465 |
|
| /* save op = op->next |
| 466 |
|
| XXX TODO %ecx is not preserved between function calls. Need to use the stack. |
| 467 |
|
| */ |
| 468 |
|
| T_CHARARR gotorel[] = { |
| 469 |
|
| jmpq(0) |
| 470 |
|
| }; |
| 471 |
|
| CODE * |
| 472 |
|
| push_gotorel(CODE *code, int label) { |
| 473 |
|
| CODE gotorel[] = { |
| 474 |
|
| jmpq_0}; |
| 475 |
|
| PUSHc(gotorel); |
| 476 |
|
| PUSHabs(&label); |
| 477 |
|
| return code; |
| 478 |
|
| } |
| 479 |
|
| |
| 480 |
|
| # define MAYBRANCH_PLOP maybranch_plop |
| 481 |
|
| # define GOTOREL gotorel |
| 482 |
|
| |
| 483 |
|
| #endif /* EOF i386 */ |
| 484 |
|
| |
| 485 |
|
| #if defined(JIT_CPU_X86) || defined(JIT_CPU_AMD64) |
| 486 |
|
| |
| 487 |
|
| int |
| 488 |
0 |
| sizeof_maybranch_check(int fw) { |
| 489 |
0 |
▶ | if (abs(fw) > 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs(fw) > 128) { |
0 |
0 |
0 |
|
| 490 |
0 |
| return sizeof(maybranch_checkw); |
| 491 |
|
| } else { |
| 492 |
0 |
| return sizeof(maybranch_check); |
| 493 |
|
| } |
| 494 |
|
| } |
| 495 |
|
| |
| 496 |
|
| T_CHARARR ifop0return[] = { |
| 497 |
|
| test_eax_eax, |
| 498 |
|
| je(sizeof(EPILOG)) |
| 499 |
|
| }; |
| 500 |
|
| T_CHARARR ifop0goto[] = { |
| 501 |
|
| test_eax_eax, |
| 502 |
|
| je_0, fourbyte |
| 503 |
|
| }; |
| 504 |
|
| T_CHARARR ifop0gotow[] = { |
| 505 |
|
| test_eax_eax, |
| 506 |
|
| jew_0, fourbyte |
| 507 |
|
| }; |
| 508 |
|
| CODE * |
| 509 |
0 |
| push_ifop0goto(CODE *code, int next) { |
| 510 |
0 |
| CODE ifop0goto[] = { |
| 511 |
|
| test_eax_eax, |
| 512 |
|
| je_0}; |
| 513 |
0 |
▶ | if (abs(next) > 128) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (abs(next) > 128) { |
0 |
0 |
0 |
|
| 514 |
0 |
| CODE ifop0gotow[] = { |
| 515 |
|
| test_eax_eax, |
| 516 |
|
| jew_0}; |
| 517 |
0 |
| PUSHc(ifop0gotow); |
| 518 |
0 |
| PUSHrel((CODE*)next); |
| 519 |
|
| } else { |
| 520 |
0 |
| PUSHc(ifop0goto); |
| 521 |
0 |
| PUSHbyte(next); |
| 522 |
|
| } |
| 523 |
0 |
| return code; |
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
|
| T_CHARARR add_eax_ppaddr[] = { |
| 527 |
|
| 0x83,0xc0,PPADDR_OFFSET /* add $8,%eax */ |
| 528 |
|
| }; |
| 529 |
|
| T_CHARARR call_eax[] = { |
| 530 |
|
| 0xff,0xd0 /* call *%eax */ |
| 531 |
|
| }; |
| 532 |
|
| #endif |
| 533 |
|
| |
| 534 |
|
| #if defined(__ia64__) || defined(__ia64) || defined(_M_IA64) |
| 535 |
|
| #error "IA64 not supported so far" |
| 536 |
|
| #endif |
| 537 |
|
| #if defined(__sparcv9) |
| 538 |
|
| #error "SPARC V9 not supported so far" |
| 539 |
|
| #endif |
| 540 |
|
| #if defined(__arm__) |
| 541 |
|
| #error "ARM or THUMB not supported so far" |
| 542 |
|
| #endif |
| 543 |
|
| |
| 544 |
|
| #ifndef JIT_CPU |
| 545 |
|
| #error "Only intel x86_32 and x86_64/amd64 supported so far" |
| 546 |
|
| #endif |
| 547 |
|
| |
| 548 |
|
| #ifndef PUSHrel |
| 549 |
|
| # ifdef MOV_REL /* amd64 */ |
| 550 |
|
| # define PUSHrel(where) { \ |
| 551 |
|
| U32 r = (CODE*)where - (code+4); \ |
| 552 |
|
| memcpy(code,&r,MOV_SIZE); code += MOV_SIZE; \ |
| 553 |
|
| } |
| 554 |
|
| # else |
| 555 |
|
| # define PUSHrel(what) memcpy(code,what,MOV_SIZE); code += MOV_SIZE |
| 556 |
|
| # endif |
| 557 |
|
| #endif |
| 558 |
|
| |
| 559 |
|
| /**********************************************************************************/ |
| 560 |
|
| |
| 561 |
|
| #ifdef PROFILING |
| 562 |
|
| #define NV_1E6 1000000.0 |
| 563 |
|
| #ifdef WIN32 |
| 564 |
|
| # include <time.h> |
| 565 |
|
| #else |
| 566 |
|
| # include <sys/time.h> |
| 567 |
|
| #endif |
| 568 |
|
| NV |
| 569 |
|
| mytime() { |
| 570 |
|
| struct timeval Tp; |
| 571 |
|
| struct timezone Tz; |
| 572 |
|
| int status; |
| 573 |
|
| status = gettimeofday (&Tp, &Tz); |
| 574 |
|
| if (status == 0) { |
| 575 |
|
| Tp.tv_sec += Tz.tz_minuteswest * 60; /* adjust for TZ */ |
| 576 |
|
| return Tp.tv_sec + (Tp.tv_usec / NV_1E6); |
| 577 |
|
| } else { |
| 578 |
|
| return -1.0; |
| 579 |
|
| } |
| 580 |
|
| } |
| 581 |
|
| #endif |
| 582 |
|
| |
| 583 |
|
| int |
| 584 |
0 |
| dispatch_needed(OP* op) { |
| 585 |
|
| #ifdef BYPASS_DISPATCH |
| 586 |
|
| return 0; /* for TESTING only */ |
| 587 |
|
| #endif |
| 588 |
0 |
▶ | switch (op->op_type) { /* sync this list with B::CC */ |
Branch: 1 branch
| Branch | True | False | Total |
| switch (op->op_type) { /* sync this list with B::CC */ |
0 |
0 |
0 |
|
| 589 |
0 |
| case OP_WAIT: |
| 590 |
|
| case OP_WAITPID: |
| 591 |
|
| case OP_NEXTSTATE: |
| 592 |
|
| case OP_AND: |
| 593 |
|
| case OP_COND_EXPR: |
| 594 |
|
| case OP_UNSTACK: |
| 595 |
|
| case OP_OR: |
| 596 |
|
| case OP_DEFINED: |
| 597 |
|
| case OP_SUBST: |
| 598 |
0 |
| return 1; |
| 599 |
0 |
| default: |
| 600 |
0 |
| return 0; |
| 601 |
|
| } |
| 602 |
|
| } |
| 603 |
|
| |
| 604 |
|
| int |
| 605 |
0 |
| maybranch(OP* op) { |
| 606 |
|
| #ifdef BYPASS_MAYBRANCH |
| 607 |
|
| return 0; /* for TESTING only */ |
| 608 |
|
| #endif |
| 609 |
0 |
▶ | switch (op->op_type) { /* XXX sync this list with Opcodes.pm */ |
Branch: 1 branch
| Branch | True | False | Total |
| switch (op->op_type) { /* XXX sync this list with Opcodes.pm */ |
0 |
0 |
0 |
|
| 610 |
0 |
| case OP_SUBSTCONT: /* LOGOP other or next */ |
| 611 |
|
| case OP_GREPWHILE: /* LOGOP other or next */ |
| 612 |
|
| case OP_MAPWHILE: /* LOGOP other or next */ |
| 613 |
|
| case OP_AND: /* LOGOP other or next */ |
| 614 |
|
| case OP_OR: /* LOGOP other or next */ |
| 615 |
|
| case OP_COND_EXPR: /* LOGOP other or next */ |
| 616 |
|
| case OP_ANDASSIGN: /* LOGOP other or next */ |
| 617 |
|
| case OP_ORASSIGN: /* LOGOP other or next */ |
| 618 |
|
| #if PERL_VERSION > 8 |
| 619 |
|
| case OP_DOR: /* LOGOP other or next */ |
| 620 |
|
| case OP_DORASSIGN: /* LOGOP other or next */ |
| 621 |
|
| case OP_ENTERWHEN: /* LOGOP other or next */ |
| 622 |
|
| case OP_ENTERGIVEN: /* LOGOP other or next */ |
| 623 |
|
| case OP_ONCE: /* LOGOP other or next */ |
| 624 |
|
| #endif |
| 625 |
|
| case OP_RANGE: /* LOGOP other or next */ |
| 626 |
|
| /* static */ |
| 627 |
|
| case OP_FLIP: /* first->other or next. not tested */ |
| 628 |
|
| case OP_SUBST: /* pmreplroot or other or next. nyi */ |
| 629 |
|
| case OP_ENTERSUB: /* CvSTART(cv)|autoload or next */ |
| 630 |
|
| case OP_DUMP: /* main_start. XXX makes no sense to support */ |
| 631 |
|
| case OP_FORMLINE: /* initially only: doparseform */ |
| 632 |
|
| case OP_GREPSTART: /* next->next or next->other */ |
| 633 |
|
| /* contexts */ |
| 634 |
|
| case OP_ENTERLOOP: /* LOOPOP cx->redoop|lastop|nextop */ |
| 635 |
|
| case OP_ENTERITER: /* LOOPOP cx->redoop|lastop|nextop */ |
| 636 |
|
| case OP_LAST: /* LOOPOP cx->lastop->next or cx->blk_sub|eval|format.retop */ |
| 637 |
|
| case OP_NEXT: /* LOOPOP cx->nextop */ |
| 638 |
|
| case OP_REDO: /* LOOPOP cx->redoop but if enter, enter->next */ |
| 639 |
|
| case OP_RETURN: /* cx->blk_sub|eval|format.retop */ |
| 640 |
|
| case OP_DBSTATE: |
| 641 |
|
| case OP_REQUIRE: /* ? */ |
| 642 |
|
| case OP_ENTEREVAL: /* cx->blk_eval.retop */ |
| 643 |
|
| case OP_ENTERTRY: /* cx->blk_eval.retop */ |
| 644 |
|
| case OP_GOTO: /* cx->blk_sub.retop or CvSTART(cv)|autoload or findlabel */ |
| 645 |
|
| case OP_LEAVEEVAL: /* ? */ |
| 646 |
|
| #if PERL_VERSION > 8 |
| 647 |
|
| case OP_CONTINUE: /* cx->blk_givwhen.leaveop */ |
| 648 |
|
| case OP_BREAK: /* ? */ |
| 649 |
|
| #endif |
| 650 |
0 |
| return 1; |
| 651 |
0 |
| default: |
| 652 |
0 |
| return 0; |
| 653 |
|
| } |
| 654 |
|
| } |
| 655 |
|
| |
| 656 |
|
| int |
| 657 |
0 |
| returnother(OP* op) { |
| 658 |
0 |
▶ | switch (op->op_type) { /* sync this list with B::CC */ |
Branch: 1 branch
| Branch | True | False | Total |
| switch (op->op_type) { /* sync this list with B::CC */ |
0 |
0 |
0 |
|
| 659 |
0 |
| case OP_AND: |
| 660 |
|
| case OP_OR: |
| 661 |
|
| #if PERL_VERSION > 8 |
| 662 |
|
| case OP_DOR: |
| 663 |
|
| case OP_DORASSIGN: |
| 664 |
|
| #endif |
| 665 |
|
| case OP_COND_EXPR: |
| 666 |
|
| case OP_ANDASSIGN: |
| 667 |
|
| case OP_ORASSIGN: |
| 668 |
0 |
| return 1; |
| 669 |
0 |
| default: |
| 670 |
0 |
| return 0; |
| 671 |
|
| } |
| 672 |
|
| } |
| 673 |
|
| |
| 674 |
|
| CODE * |
| 675 |
0 |
| call_abs (CODE *code, void *addr) { |
| 676 |
|
| /* intel specific: */ |
| 677 |
0 |
| register signed long rel = (CODE*)addr - code - sizeof(CALL) - CALL_SIZE; |
| 678 |
0 |
▶ | if (rel > (unsigned int)PERL_ULONG_MAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rel > (unsigned int)PERL_ULONG_MAX) { |
0 |
0 |
0 |
|
| 679 |
0 |
| PUSHc(JMP); |
| 680 |
0 |
| PUSHcall(addr); |
| 681 |
|
| /* 386 far calls prefer 2 nop's afterwards, align it to 4 (0,4,8,c)*/ |
| 682 |
|
| #if CALL_ALIGN |
| 683 |
|
| while (((unsigned int)&code | 0xfffffff0) % CALL_ALIGN) { *(code++) = NOP[0]; } |
| 684 |
|
| #endif |
| 685 |
|
| } else { |
| 686 |
0 |
| U32 urel = (U32)rel; |
| 687 |
0 |
| PUSHc(CALL); |
| 688 |
0 |
| PUSHcall(urel); |
| 689 |
|
| } |
| 690 |
0 |
| return code; |
| 691 |
|
| } |
| 692 |
|
| |
| 693 |
|
| /* Search dynamically (in Jit) the code target for op in jmptargets */ |
| 694 |
0 |
| CODE *jmp_search_label(OP* op) { |
| 695 |
0 |
| int ix = jmpix; |
| 696 |
0 |
▶ | while (ix-- > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (ix-- > 0) { |
0 |
0 |
0 |
|
| 697 |
0 |
▶ | if (jmptargets[ix].op == op) { /* no loop */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (jmptargets[ix].op == op) { /* no loop */ |
0 |
0 |
0 |
|
| 698 |
0 |
| return jmptargets[ix].target; |
| 699 |
|
| } |
| 700 |
|
| } |
| 701 |
0 |
| return (CODE*)0; |
| 702 |
|
| } |
| 703 |
|
| |
| 704 |
|
| long |
| 705 |
0 |
| jit_chain(pTHX_ |
| 706 |
|
| OP *op, |
| 707 |
|
| CODE *code, |
| 708 |
|
| CODE *code_start, |
| 709 |
|
| int jumpsize, |
| 710 |
|
| OP *stopop |
| 711 |
|
| #ifdef DEBUGGING |
| 712 |
|
| ,FILE *fh, FILE *stabs |
| 713 |
|
| #endif |
| 714 |
|
| ) |
| 715 |
|
| { |
| 716 |
0 |
| int dryrun = !code; |
| 717 |
0 |
| int size = 0; |
| 718 |
0 |
| OP *startop = op; |
| 719 |
|
| OP *opnext; |
| 720 |
|
| #ifdef DEBUGGING |
| 721 |
|
| static int line = 3; |
| 722 |
|
| char *opname; |
| 723 |
|
| |
| 724 |
|
| if (!dryrun) { |
| 725 |
|
| opname = (char*)PL_op_name[op->op_type]; |
| 726 |
|
| fprintf(fh, "/* block jit_chain_%d op 0x%x pp_%s; */\n", local_chains, op, opname); |
| 727 |
|
| local_chains++; |
| 728 |
|
| line++; |
| 729 |
|
| } |
| 730 |
|
| #endif |
| 731 |
|
| |
| 732 |
|
| do { |
| 733 |
0 |
| opnext = op->op_next; |
| 734 |
|
| #ifdef DEBUGGING |
| 735 |
|
| if (!dryrun) { |
| 736 |
|
| opname = (char*)PL_op_name[op->op_type]; |
| 737 |
|
| DEBUG_v( printf("# pp_%s \t= 0x%x / 0x%x\n", opname, op->op_ppaddr, op)); |
| 738 |
|
| } |
| 739 |
|
| # if defined(DEBUG_s_TEST_) |
| 740 |
|
| if (DEBUG_s_TEST_) { |
| 741 |
|
| if (dryrun) { |
| 742 |
|
| size += sizeof(CALL) + CALL_SIZE; |
| 743 |
|
| } else { |
| 744 |
|
| CALL_ABS(&Perl_debstack); |
| 745 |
|
| dbg_lines("debstack();"); |
| 746 |
|
| } |
| 747 |
|
| } |
| 748 |
|
| # endif |
| 749 |
|
| # if defined(DEBUG_t_TEST_) |
| 750 |
|
| if (DEBUG_t_TEST_ && op) { |
| 751 |
|
| T_CHARARR push_arg1[] = { push_arg1_mem }; |
| 752 |
|
| # ifdef USE_ITHREADS |
| 753 |
|
| T_CHARARR push_arg2[] = { push_arg2_mem }; |
| 754 |
|
| # endif |
| 755 |
|
| if (dryrun) { |
| 756 |
|
| PUSH_1ARG_DRYRUN; |
| 757 |
|
| size += PUSH_SIZE + sizeof(CALL) + CALL_SIZE; |
| 758 |
|
| } else { |
| 759 |
|
| PUSH_1ARG; |
| 760 |
|
| PUSHabs(op); |
| 761 |
|
| CALL_ABS(&Perl_debop); |
| 762 |
|
| DEBUG_v( printf("# debop(%x) %s\n", op, (char*)PL_op_name[op->op_type])); |
| 763 |
|
| dbg_lines("debop(PL_op);"); |
| 764 |
|
| } |
| 765 |
|
| } |
| 766 |
|
| # endif |
| 767 |
|
| #endif |
| 768 |
|
| |
| 769 |
0 |
▶ | if (op->op_type == OP_NULL) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_type == OP_NULL) continue; |
0 |
0 |
0 |
|
| 770 |
|
| |
| 771 |
|
| /* check labels -> store possible jmp targets. |
| 772 |
|
| Note: nextstate can also be normal jmp target as enter */ |
| 773 |
0 |
▶ | if (!dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dryrun) { |
0 |
0 |
0 |
|
| 774 |
0 |
▶ | if ((op->op_type == OP_NEXTSTATE)|(op->op_type == OP_DBSTATE)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((op->op_type == OP_NEXTSTATE)|(op->op_type == OP_DBSTATE)) { |
0 |
0 |
0 |
|
| 775 |
|
| char *label; |
| 776 |
|
| JMPTGT cx; |
| 777 |
|
| #ifdef CopLABEL |
| 778 |
0 |
▶ | if (label = CopLABEL(cCOPx(op))) |
Branch: 1 branch
| Branch | True | False | Total |
| if (label = CopLABEL(cCOPx(op))) |
0 |
0 |
0 |
|
| 779 |
|
| #else |
| 780 |
|
| if (label = cCOPx(op)->cop_label) |
| 781 |
|
| #endif |
| 782 |
|
| { |
| 783 |
0 |
| cx.op = op; |
| 784 |
0 |
| cx.label = label; |
| 785 |
0 |
| cx.target = code; |
| 786 |
|
| DEBUG_v( printf("# push jmp label %s at 0x%x for nextstate 0x%x\n", label, code, op)); |
| 787 |
0 |
| PUSH_JMP(cx); |
| 788 |
|
| } else { |
| 789 |
0 |
| cx.op = op; |
| 790 |
0 |
| cx.label = NULL; |
| 791 |
0 |
| cx.target = code; |
| 792 |
|
| DEBUG_v( printf("# push jmp at 0x%x for nextstate op=0x%x\n", code-code_start, op)); |
| 793 |
0 |
| PUSH_JMP(cx); |
| 794 |
|
| } |
| 795 |
0 |
▶ | if (op->op_type == OP_ENTER) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_type == OP_ENTER) { |
0 |
0 |
0 |
|
| 796 |
|
| JMPTGT cx; |
| 797 |
0 |
| cx.op = op; |
| 798 |
0 |
| cx.label = NULL; |
| 799 |
0 |
| cx.target = code; |
| 800 |
|
| DEBUG_v( printf("# push jmp at 0x%x for enter op=0x%x\n", code-code_start, op)); |
| 801 |
0 |
| PUSH_JMP(cx); |
| 802 |
|
| } |
| 803 |
|
| } |
| 804 |
|
| } |
| 805 |
|
| |
| 806 |
|
| /* XXX TODO |
| 807 |
|
| * We have almost no chance to get the CvSTART of the sub here, better in the parser. |
| 808 |
|
| * But we can try checking a CvSTART: |
| 809 |
|
| * - On XS functions the CvSTART is added dynamically at run-time, |
| 810 |
|
| * but it makes no sense to jit already compiled XS code. Good. |
| 811 |
|
| * - autoloaded non-xs code would be good to jit, but we'd need |
| 812 |
|
| * a run-time check for the CvSTART, and Jit it at run-time. |
| 813 |
|
| * For now we can only call unjitted functions. |
| 814 |
|
| * We'd have to call entersub, but then we'd need the prev. |
| 815 |
|
| * context (name in the prev gv), |
| 816 |
|
| * ... and we cannot call the chain at compile-time. |
| 817 |
|
| */ |
| 818 |
|
| if (0 && (op->op_type == OP_ENTERSUB)) { |
| 819 |
|
| SV *sv; /* arg from the previous GV */ |
| 820 |
|
| CV* cv; |
| 821 |
|
| /* first jit the sub, then loop through it. |
| 822 |
|
| loop CvROOT until !PL_op */ |
| 823 |
|
| /*OP* next = Perl_pp_entersub(aTHX); / * find CvSTART */ |
| 824 |
|
| UNOP* next = cUNOPx(op)->op_first; |
| 825 |
|
| if (next) { |
| 826 |
|
| next = (UNOP*)(next->op_next); |
| 827 |
|
| if (dryrun) { |
| 828 |
|
| size += JIT_CHAIN_FULL_DRYRUN((OP*)next, opnext); |
| 829 |
|
| } else { |
| 830 |
|
| int lsize; |
| 831 |
|
| DEBUG_v( printf("# entersub() => op=0x%x\n", next)); |
| 832 |
|
| dbg_lines1("sub_%d: {", global_label); |
| 833 |
|
| lsize = JIT_CHAIN_FULL_DRYRUN((OP*)next, opnext); |
| 834 |
|
| code = JIT_CHAIN_FULL((OP*)next, lsize, opnext); |
| 835 |
|
| dbg_lines("}"); |
| 836 |
|
| dbg_lines1("next_%d:", global_label); |
| 837 |
|
| } |
| 838 |
|
| } |
| 839 |
|
| } |
| 840 |
|
| |
| 841 |
0 |
▶ | if (maybranch(op)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (maybranch(op)) { |
0 |
0 |
0 |
|
| 842 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 843 |
0 |
| size += sizeof(maybranch_plop); |
| 844 |
|
| } else { |
| 845 |
|
| /* store op->next at 0(%esp). Later cmp returned op == op->next => jmp */ |
| 846 |
|
| DEBUG_v( printf("# maybranch %s:\n", opname, op->op_ppaddr)); |
| 847 |
|
| dbg_lines("op = PL_op->op_next;"); |
| 848 |
0 |
| code = push_maybranch_plop(code, opnext); |
| 849 |
|
| } |
| 850 |
|
| } |
| 851 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 852 |
0 |
| size += sizeof(CALL) + CALL_SIZE; |
| 853 |
|
| } else { |
| 854 |
|
| #ifdef USE_ITHREADS |
| 855 |
|
| dbg_cline1("my_perl->Iop = Perl_pp_%s(my_perl);\n", opname); |
| 856 |
|
| #else |
| 857 |
|
| dbg_cline1("PL_op = Perl_pp_%s();\n", opname); |
| 858 |
|
| #endif |
| 859 |
|
| dbg_stabs1("call pp_%s", opname); |
| 860 |
0 |
| CALL_ABS(op->op_ppaddr); |
| 861 |
|
| dbg_stabs("PL_op = eax"); |
| 862 |
|
| } |
| 863 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 864 |
0 |
| size += sizeof(SAVE_PLOP); |
| 865 |
|
| #if defined(JIT_CPU_AMD64) && !defined(USE_ITHREADS) |
| 866 |
0 |
| size += MOV_SIZE; |
| 867 |
|
| #endif |
| 868 |
|
| } else { |
| 869 |
0 |
| PUSHc(SAVE_PLOP); |
| 870 |
|
| #if defined(JIT_CPU_AMD64) && !defined(USE_ITHREADS) |
| 871 |
0 |
| PUSHrel(&PL_op); |
| 872 |
|
| #endif |
| 873 |
|
| } |
| 874 |
|
| |
| 875 |
|
| #ifdef HAVE_DISPATCH |
| 876 |
|
| if (DISPATCH_NEEDED(op)) { |
| 877 |
|
| if (!dryrun) { |
| 878 |
|
| # ifdef USE_ITHREADS |
| 879 |
|
| dbg_lines("if (my_perl->Isig_pending)"); |
| 880 |
|
| dbg_lines(" Perl_despatch_signals(my_perl);"); |
| 881 |
|
| # else |
| 882 |
|
| dbg_lines("if (PL_sig_pending)"); |
| 883 |
|
| dbg_lines(" Perl_despatch_signals();"); |
| 884 |
|
| # endif |
| 885 |
|
| } |
| 886 |
|
| # ifdef DISPATCH_GETSIG |
| 887 |
|
| if (dryrun) { |
| 888 |
|
| size += sizeof(DISPATCH_GETSIG); |
| 889 |
|
| size += MOV_SIZE; |
| 890 |
|
| } else { |
| 891 |
|
| PUSHc(DISPATCH_GETSIG); |
| 892 |
|
| PUSHrel(&PL_sig_pending); |
| 893 |
|
| } |
| 894 |
|
| # endif |
| 895 |
|
| if (dryrun) { |
| 896 |
|
| size += sizeof(DISPATCH) + sizeof(CALL) + CALL_SIZE; |
| 897 |
|
| } else { |
| 898 |
|
| PUSHc(DISPATCH); |
| 899 |
|
| CALL_ABS(&Perl_despatch_signals); |
| 900 |
|
| } |
| 901 |
|
| } |
| 902 |
|
| #endif |
| 903 |
|
| |
| 904 |
|
| /* other before next */ |
| 905 |
0 |
▶ | if (maybranch(op)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (maybranch(op)) { |
0 |
0 |
0 |
|
| 906 |
|
| int lsize; |
| 907 |
0 |
| SV *keysv = newSViv(PTR2IV(op)); |
| 908 |
|
| /* XXX avoid cyclic loops of already jitted other ops: |
| 909 |
|
| and => nextstate, cond_expr => enter, ... */ |
| 910 |
0 |
▶ | if (!otherops) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!otherops) { |
0 |
0 |
0 |
|
| 911 |
0 |
| otherops = newHV(); |
| 912 |
0 |
| otherops1 = newHV(); |
| 913 |
|
| } |
| 914 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 915 |
0 |
▶ | if (hv_exists_ent(otherops1, keysv, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(otherops1, keysv, 0)) { |
0 |
0 |
0 |
|
| 916 |
0 |
| goto NEXT; |
| 917 |
|
| } else { |
| 918 |
0 |
| hv_store_ent(otherops1, keysv, newSViv((int)code), 0); |
| 919 |
|
| } |
| 920 |
|
| } else { |
| 921 |
0 |
▶ | if (hv_exists_ent(otherops, keysv, 0)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists_ent(otherops, keysv, 0)) { |
0 |
0 |
0 |
|
| 922 |
|
| DEBUG_v( printf("# %s 0x%x already jitted, code=0x%x\n", PL_op_name[op->op_type], |
| 923 |
|
| op, code)); |
| 924 |
|
| /* XXX when is a jmp to this op needed? patch later or use the code addr from the hash */ |
| 925 |
0 |
| goto NEXT; |
| 926 |
|
| } else { |
| 927 |
0 |
| hv_store_ent(otherops, keysv, newSViv((int)code), 0); |
| 928 |
|
| } |
| 929 |
|
| /* dbg_lines1("if (PL_op == op) goto next_%d;", global_label); */ |
| 930 |
|
| } |
| 931 |
0 |
▶ | if ((PL_opargs[op->op_type] & OA_CLASS_MASK) == OA_LOGOP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((PL_opargs[op->op_type] & OA_CLASS_MASK) == OA_LOGOP) { |
0 |
0 |
0 |
|
| 932 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 933 |
0 |
| int i = JIT_CHAIN_DRYRUN(cLOGOPx(op)->op_other); |
| 934 |
0 |
| size += i + sizeof_maybranch_check(i); |
| 935 |
0 |
| size += sizeof(GOTOREL); |
| 936 |
|
| } else { |
| 937 |
|
| int next, other; |
| 938 |
|
| LOGOP* logop; |
| 939 |
0 |
| logop = cLOGOPx(op); |
| 940 |
|
| DEBUG_v( printf("# other_%d: %s => %s, ", global_label, |
| 941 |
|
| PL_op_name[op->op_type], |
| 942 |
|
| PL_op_name[cLOGOPx(op)->op_other->op_type])); |
| 943 |
0 |
| other = JIT_CHAIN_DRYRUN(logop->op_other); /* sizeof other */ |
| 944 |
0 |
| other += sizeof(GOTOREL); |
| 945 |
0 |
| code = push_maybranch_check(code, other); /* if cmp: je => next */ |
| 946 |
|
| dbg_lines1("if (PL_op != op) goto next_%d;", global_label); |
| 947 |
|
| DEBUG_v( printf("size=%x\n", other) ); |
| 948 |
0 |
| code = JIT_CHAIN(logop->op_other); |
| 949 |
|
| dbg_cline1("/*goto logop_%d;*/", global_label); |
| 950 |
|
| dbg_lines1("next_%d:", global_label); |
| 951 |
0 |
| next = JIT_CHAIN_DRYRUN(logop->op_next); /* sizeof next */ |
| 952 |
|
| DEBUG_v( printf("# next_%d: %s, size=%x\n", global_label, |
| 953 |
|
| PL_op_name[logop->op_next->op_type], next)); |
| 954 |
0 |
| code = push_gotorel(code, next); |
| 955 |
|
| /*dbg_lines1("logop_%d:", global_label);*/ |
| 956 |
|
| } |
| 957 |
|
| } else { /* special branches */ |
| 958 |
|
| int next; |
| 959 |
|
| T_CHARARR push_arg1[] = { push_arg1_mem }; |
| 960 |
|
| # ifdef USE_ITHREADS |
| 961 |
|
| T_CHARARR push_arg2[] = { push_arg2_mem }; |
| 962 |
|
| # endif |
| 963 |
0 |
| switch (op->op_type) { /* sync this list with B::CC */ |
| 964 |
0 |
| case OP_FLIP: |
| 965 |
|
| DEBUG_v( printf("# flip_%d\n", global_label)); |
| 966 |
0 |
▶ | if ((op->op_flags & OPf_WANT) == OPf_WANT_LIST) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((op->op_flags & OPf_WANT) == OPf_WANT_LIST) { |
0 |
0 |
0 |
|
| 967 |
|
| /* need to check the returned op at runtime */ |
| 968 |
|
| LOGOP* logop; |
| 969 |
0 |
| logop = cLOGOPx(cUNOPx(op)->op_first); |
| 970 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 971 |
0 |
| int i = JIT_CHAIN_DRYRUN(logop->op_other); |
| 972 |
0 |
| size += i + sizeof_maybranch_check(i); |
| 973 |
0 |
| size += sizeof(GOTOREL); |
| 974 |
|
| } else { |
| 975 |
|
| int other; |
| 976 |
0 |
| other = JIT_CHAIN_DRYRUN(logop->op_other); /* sizeof other */ |
| 977 |
0 |
| other += sizeof(GOTOREL); |
| 978 |
0 |
| code = push_maybranch_check(code, other); /* if cmp: je => next */ |
| 979 |
|
| DEBUG_v( printf("# other_%d: %s, size=%x\n", global_label, |
| 980 |
|
| PL_op_name[logop->op_other->op_type], other)); |
| 981 |
0 |
| code = JIT_CHAIN(logop->op_other); |
| 982 |
|
| } |
| 983 |
|
| } |
| 984 |
0 |
| break; |
| 985 |
|
| /* store and jump to labels. */ |
| 986 |
0 |
| case OP_ENTERLOOP: |
| 987 |
|
| case OP_ENTERITER: |
| 988 |
0 |
▶ | if (!dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!dryrun) { |
0 |
0 |
0 |
|
| 989 |
|
| int nextop, lastop, redoop; |
| 990 |
|
| LOOPTGT cx; |
| 991 |
|
| LOOP* loop; |
| 992 |
|
| |
| 993 |
0 |
| loop = cLOOPx(op); |
| 994 |
|
| /* XXX Need to store away the branch targets in jmptargets, otherwise |
| 995 |
|
| unjitted code is executed. |
| 996 |
|
| We can also try to patchup the jmps afterwards. |
| 997 |
|
| */ |
| 998 |
|
| DEBUG_v( printf("# %s_%d:\n", PL_op_name[loop->op_type], global_label)); |
| 999 |
|
| /* After each chain jump to the end, so we need all sizes. */ |
| 1000 |
0 |
| nextop = JIT_CHAIN_DRYRUN(loop->op_nextop); /* sizeof other */ |
| 1001 |
0 |
| lastop = JIT_CHAIN_DRYRUN(loop->op_lastop); |
| 1002 |
0 |
| redoop = JIT_CHAIN_DRYRUN(loop->op_redoop); |
| 1003 |
0 |
| lsize = nextop + lastop + redoop + 3*(sizeof(CALL)+CALL_SIZE); |
| 1004 |
|
| dbg_lines1("goto branch_%d;", global_label); |
| 1005 |
0 |
| code = push_gotorel(code, lsize); /* jump to end: nextop+lastop+redoop+3*goto */ |
| 1006 |
|
| |
| 1007 |
|
| DEBUG_v( printf("# nextop_%d: %s, size=%x\n", global_label, |
| 1008 |
|
| PL_op_name[loop->op_nextop->op_type], lsize)); |
| 1009 |
|
| dbg_lines1("nextop_%d:", global_label); |
| 1010 |
0 |
| cx.nextop = code; |
| 1011 |
0 |
| code = JIT_CHAIN(loop->op_nextop); |
| 1012 |
|
| |
| 1013 |
0 |
| lsize -= nextop + sizeof(CALL)+CALL_SIZE; |
| 1014 |
|
| dbg_lines1("goto branch_%d;", global_label); |
| 1015 |
0 |
| code = push_gotorel(code, lsize); /* jump to end */ |
| 1016 |
|
| DEBUG_v( printf("# lastop_%d: %s\tsize=%x\n", global_label, |
| 1017 |
|
| PL_op_name[loop->op_lastop->op_type], lsize)); |
| 1018 |
0 |
| cx.lastop = code; |
| 1019 |
|
| dbg_lines1("lastop_%d:", global_label); |
| 1020 |
0 |
| code = JIT_CHAIN(loop->op_lastop); |
| 1021 |
|
| |
| 1022 |
0 |
| lsize -= lastop + sizeof(CALL)+CALL_SIZE; |
| 1023 |
|
| dbg_lines1("goto branch_%d;", global_label); |
| 1024 |
0 |
| code = push_gotorel(code, lsize); /* jump to end */ |
| 1025 |
|
| DEBUG_v( printf("# redoop_%d: %s, size=%x\n", global_label, |
| 1026 |
|
| PL_op_name[loop->op_redoop->op_type], lsize)); |
| 1027 |
0 |
| cx.redoop = code; |
| 1028 |
|
| dbg_lines1("redoop_%d:", global_label); |
| 1029 |
0 |
| code = JIT_CHAIN(loop->op_redoop); |
| 1030 |
0 |
| PUSH_LOOP(cx); |
| 1031 |
|
| dbg_lines1("branch_%d:", global_label); |
| 1032 |
|
| } |
| 1033 |
0 |
| break; |
| 1034 |
0 |
| case OP_SUBSTCONT: /* need to jit other and the PMREPLSTART. What logic? */ |
| 1035 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1036 |
0 |
| size += JIT_CHAIN_DRYRUN(cLOGOPx(op)->op_other); |
| 1037 |
|
| } else { |
| 1038 |
|
| DEBUG_v( printf("# substcont other\n")); |
| 1039 |
0 |
| code = JIT_CHAIN(cLOGOPx(op)->op_other); |
| 1040 |
|
| /*size += next-(int)code;*/ |
| 1041 |
|
| } |
| 1042 |
|
| #if PERL_VERSION > 8 |
| 1043 |
|
| # define PMREPLSTART(op) (op)->op_pmstashstartu.op_pmreplstart |
| 1044 |
|
| #else |
| 1045 |
|
| # define PMREPLSTART(op) (op)->op_pmreplstart |
| 1046 |
|
| #endif |
| 1047 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1048 |
0 |
| size += JIT_CHAIN_DRYRUN(PMREPLSTART(cPMOPx(op))); |
| 1049 |
|
| } else { |
| 1050 |
|
| DEBUG_v( printf("# pmreplstart\n")); |
| 1051 |
0 |
| code = JIT_CHAIN(PMREPLSTART(cPMOPx(op))); |
| 1052 |
|
| /*size += lsize-(int)code;*/ |
| 1053 |
|
| } |
| 1054 |
0 |
| break; |
| 1055 |
|
| |
| 1056 |
0 |
| case OP_NEXT: |
| 1057 |
|
| case OP_REDO: |
| 1058 |
|
| case OP_LAST: |
| 1059 |
|
| /* XXX if not OPf_SPECIAL pop label op->pv from jmptargets (prev. called cxstack), |
| 1060 |
|
| else just next jmp */ |
| 1061 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1062 |
0 |
| size += sizeof(GOTOREL); |
| 1063 |
|
| } else { |
| 1064 |
|
| LOOPTGT *cx; |
| 1065 |
|
| CODE* tgtop; |
| 1066 |
0 |
▶ | cx = POP_LOOP; |
Branch: 1 branch
| Branch | True | False | Total |
| cx = POP_LOOP; |
0 |
0 |
0 |
|
| 1067 |
0 |
▶ | if (op->op_type == OP_NEXT) tgtop = cx->nextop; |
Branch: 1 branch
| Branch | True | False | Total |
| if (op->op_type == OP_NEXT) tgtop = cx->nextop; |
0 |
0 |
0 |
|
| 1068 |
0 |
▶ | else if (op->op_type == OP_REDO) tgtop = cx->redoop; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (op->op_type == OP_REDO) tgtop = cx->redoop; |
0 |
0 |
0 |
|
| 1069 |
0 |
▶ | else if (op->op_type == OP_LAST) tgtop = cx->lastop; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (op->op_type == OP_LAST) tgtop = cx->lastop; |
0 |
0 |
0 |
|
| 1070 |
|
| DEBUG_v( printf("# %s %x\n", PL_op_name[op->op_type], tgtop)); |
| 1071 |
0 |
| code = push_gotorel(code, (int)tgtop); /* jmp or rel? */ |
| 1072 |
|
| } |
| 1073 |
0 |
| break; |
| 1074 |
0 |
| case OP_ENTERSUB: |
| 1075 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1076 |
0 |
| size += sizeof(ifop0return); |
| 1077 |
0 |
| size += sizeof(EPILOG); |
| 1078 |
0 |
| size += sizeof(maybranch_check); |
| 1079 |
0 |
| size += sizeof(add_eax_ppaddr); |
| 1080 |
0 |
| size += sizeof(call_eax); |
| 1081 |
0 |
| size += sizeof(SAVE_PLOP); |
| 1082 |
|
| #if defined(JIT_CPU_AMD64) && !defined(USE_ITHREADS) |
| 1083 |
0 |
| size += MOV_SIZE; |
| 1084 |
|
| #endif |
| 1085 |
|
| } else { |
| 1086 |
0 |
| size = 0; |
| 1087 |
|
| DEBUG_v( printf("# entersub: call unjitted sub\n") ); |
| 1088 |
|
| /* retval maybe DIE, check PL_op==0 */ |
| 1089 |
|
| dbg_lines("if (!PL_op)"); |
| 1090 |
0 |
| code = PUSHc(ifop0return); |
| 1091 |
|
| dbg_lines(" return;"); |
| 1092 |
0 |
| code = PUSHc(EPILOG); |
| 1093 |
|
| /* else */ |
| 1094 |
|
| dbg_lines1("if (PL_op == op) goto next_%d;", global_label); |
| 1095 |
|
| /* XXX TODO check if we have jitted the returned %eax CvSTART */ |
| 1096 |
|
| /* (search for existing CvSTART targets) */ |
| 1097 |
|
| /* else call unjitted retval */ |
| 1098 |
0 |
| size = sizeof(add_eax_ppaddr) + sizeof(call_eax); |
| 1099 |
0 |
| size += sizeof(SAVE_PLOP); |
| 1100 |
|
| #if defined(JIT_CPU_AMD64) && !defined(USE_ITHREADS) |
| 1101 |
0 |
| size += MOV_SIZE; |
| 1102 |
|
| #endif |
| 1103 |
0 |
| code = push_maybranch_check(code, size); /* if cmp: je => next */ |
| 1104 |
|
| |
| 1105 |
|
| #if defined(DEBUG_t_TEST_) |
| 1106 |
0 |
▶ | if (DEBUG_t_TEST_ && op) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (DEBUG_t_TEST_ && op) { |
0 |
0 |
0 |
| if (DEBUG_t_TEST_ && op) { |
0 |
0 |
0 |
|
| 1107 |
|
| T_CHARARR push_arg1[] = { push_arg1_eax }; |
| 1108 |
|
| # ifdef USE_ITHREADS |
| 1109 |
|
| T_CHARARR push_arg2[] = { push_arg2_eax }; |
| 1110 |
|
| # endif |
| 1111 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1112 |
0 |
| PUSH_1ARG_DRYRUN; |
| 1113 |
0 |
| size += sizeof(CALL) + CALL_SIZE; |
| 1114 |
|
| } else { |
| 1115 |
0 |
| PUSH_1ARG; |
| 1116 |
0 |
| CALL_ABS(&Perl_debop); |
| 1117 |
|
| DEBUG_v( printf("# debop(%x) %s\n", op, (char*)PL_op_name[op->op_type])); |
| 1118 |
|
| dbg_lines("debop(PL_op);"); |
| 1119 |
|
| } |
| 1120 |
|
| } |
| 1121 |
|
| #endif |
| 1122 |
|
| dbg_lines("else (PL_op->op_ppaddr)();"); /* not found, continue unjitted */ |
| 1123 |
0 |
| PUSHc(add_eax_ppaddr); /* ppaddr offset from %eax */ |
| 1124 |
0 |
| PUSHc(call_eax); |
| 1125 |
0 |
| PUSHc(SAVE_PLOP); |
| 1126 |
|
| #if defined(JIT_CPU_AMD64) && !defined(USE_ITHREADS) |
| 1127 |
0 |
| PUSHrel(&PL_op); |
| 1128 |
|
| #endif |
| 1129 |
|
| dbg_lines1("next_%d:", global_label); |
| 1130 |
|
| } |
| 1131 |
0 |
| break; |
| 1132 |
|
| |
| 1133 |
|
| #if 0 |
| 1134 |
|
| /* XXX If we know the CvSTART, then we could jit the sub, then loop through it. |
| 1135 |
|
| loop CvROOT until !PL_op or op->next */ |
| 1136 |
|
| { |
| 1137 |
|
| OP* next = Perl_pp_entersub(aTHX); /* find CvSTART */ |
| 1138 |
|
| if (next) { |
| 1139 |
|
| next = next->op_next; |
| 1140 |
|
| if (dryrun) { |
| 1141 |
|
| size += JIT_CHAIN_FULL_DRYRUN(next, opnext); |
| 1142 |
|
| } else { |
| 1143 |
|
| dbg_lines1("sub_%d: {", global_label); |
| 1144 |
|
| lsize = JIT_CHAIN_FULL_DRYRUN(next, opnext); |
| 1145 |
|
| code = JIT_CHAIN_FULL(next, lsize, opnext); |
| 1146 |
|
| dbg_lines("}"); |
| 1147 |
|
| dbg_lines1("next_%d:", global_label); |
| 1148 |
|
| } |
| 1149 |
|
| } |
| 1150 |
|
| } |
| 1151 |
|
| #endif |
| 1152 |
|
| break; |
| 1153 |
|
| |
| 1154 |
0 |
| default: |
| 1155 |
|
| if (!dryrun) { |
| 1156 |
|
| DEBUG_v( printf("NYI unsupported maybranch op %s\n", PL_op_name[op->op_type]) ); |
| 1157 |
|
| } |
| 1158 |
0 |
| break; |
| 1159 |
|
| |
| 1160 |
|
| /* goto and other search at run-time |
| 1161 |
|
| for possible jump targets in jmptargets info. |
| 1162 |
|
| If no cx record is found, continue with the unjitted OP. |
| 1163 |
|
| */ |
| 1164 |
0 |
| case OP_GOTO: |
| 1165 |
|
| /* XXX We can only jump to jitted and recorded labels, else jump to unjitted code. |
| 1166 |
|
| if (PL_op != ($sym)->op_next && PL_op != (OP*)0){return PL_op;} */ |
| 1167 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1168 |
0 |
| int i = 0; |
| 1169 |
0 |
| size += sizeof(ifop0return); |
| 1170 |
0 |
| size += sizeof(EPILOG); |
| 1171 |
0 |
| PUSH_1ARG_DRYRUN; |
| 1172 |
0 |
| i += PUSH_SIZE + sizeof(CALL) + CALL_SIZE; |
| 1173 |
0 |
| i += sizeof(maybranch_check); |
| 1174 |
0 |
| i += sizeof(GOTOREL); |
| 1175 |
0 |
| size += i; |
| 1176 |
|
| |
| 1177 |
|
| } else { /* get back a OP* address. but we can only jump to PUSH_CX ops */ |
| 1178 |
|
| CODE* jumptgt; |
| 1179 |
|
| char *label; |
| 1180 |
0 |
| OP *retop = NULL; |
| 1181 |
0 |
| size = 0; |
| 1182 |
|
| |
| 1183 |
|
| dbg_lines1("if (PL_op == op) goto next_%d;", global_label); |
| 1184 |
0 |
| PUSH_1ARG_DRYRUN; |
| 1185 |
0 |
| size += PUSH_SIZE + sizeof(CALL) + CALL_SIZE; |
| 1186 |
0 |
| size += sizeof(maybranch_check); |
| 1187 |
0 |
| size += sizeof(GOTOREL); |
| 1188 |
|
| |
| 1189 |
0 |
| code = PUSHc(ifop0return); |
| 1190 |
0 |
| code = PUSHc(EPILOG); |
| 1191 |
0 |
| code = push_maybranch_check(code, size); /* if cmp: je => next */ |
| 1192 |
|
| |
| 1193 |
|
| /* The retop with the found label is only retrieved dynamic, within jit. |
| 1194 |
|
| so we need to call jmp_search_label to get the target */ |
| 1195 |
0 |
▶ | if ((op->op_type == OP_GOTO) && !(op->op_flags & (OPf_STACKED|OPf_SPECIAL))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((op->op_type == OP_GOTO) && !(op->op_flags & (OPf_STACKED|OPf_SPECIAL))) { |
0 |
0 |
0 |
|
| 1196 |
|
| #ifdef DEBUGGING |
| 1197 |
|
| label = ((PVOP*)op)->op_pv; |
| 1198 |
|
| DEBUG_v( printf("# pp_goto %s via jmp_search_label(op)\n", label)); |
| 1199 |
|
| #endif |
| 1200 |
|
| } |
| 1201 |
0 |
| PUSH_1ARG; |
| 1202 |
|
| dbg_lines("unsigned char *jumptgt = jmp_search_label(op);"); |
| 1203 |
0 |
| PUSHabs(op); |
| 1204 |
0 |
| CALL_ABS(&jmp_search_label); |
| 1205 |
|
| |
| 1206 |
|
| dbg_lines("if (jumptgt) goto jumptgt"); |
| 1207 |
|
| dbg_lines("else (PL_op->op_ppaddr)();"); /* not found, continue unjitted */ |
| 1208 |
0 |
| code = push_gotorel(code, (int)jumptgt); |
| 1209 |
|
| |
| 1210 |
|
| dbg_lines1("next_%d", global_label); |
| 1211 |
|
| } |
| 1212 |
0 |
| break; |
| 1213 |
|
| } |
| 1214 |
|
| } |
| 1215 |
|
| #ifdef DEBUGGING |
| 1216 |
|
| global_label++; |
| 1217 |
|
| #endif |
| 1218 |
|
| } |
| 1219 |
0 |
| NEXT: |
| 1220 |
0 |
▶ | if (stopop) { /* entersub loop until some op_next? */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (stopop) { /* entersub loop until some op_next? */ |
0 |
0 |
0 |
|
| 1221 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1222 |
0 |
| size += sizeof(maybranch_check); |
| 1223 |
0 |
| size += sizeof(GOTOREL); |
| 1224 |
|
| } else { |
| 1225 |
|
| DEBUG_v( printf("# check !PL_op or PL_op 0x%x != op->next 0x%x (entersub)\n", |
| 1226 |
|
| opnext, stopop)); |
| 1227 |
0 |
| int i = sizeof(maybranch_check); |
| 1228 |
0 |
| i += sizeof(GOTOREL); |
| 1229 |
0 |
| code = push_maybranch_check(code, i); |
| 1230 |
0 |
| code = push_gotorel(code, jumpsize); |
| 1231 |
|
| dbg_lines1("if (PL_op == op) goto next_%d;", global_label); |
| 1232 |
|
| } |
| 1233 |
0 |
▶ | if (stopop == opnext) goto OUT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stopop == opnext) goto OUT; |
0 |
0 |
0 |
|
| 1234 |
|
| } |
| 1235 |
0 |
▶ | } while (op = opnext); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (op = opnext); |
0 |
0 |
0 |
|
| 1236 |
0 |
| OUT: |
| 1237 |
0 |
▶ | if (dryrun) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (dryrun) { |
0 |
0 |
0 |
|
| 1238 |
0 |
| op = startop; |
| 1239 |
0 |
| return size; |
| 1240 |
|
| } else { |
| 1241 |
0 |
| return (long)code; |
| 1242 |
|
| } |
| 1243 |
|
| } |
| 1244 |
|
| |
| 1245 |
|
| /* |
| 1246 |
|
| Faster jitted execution path without loop, |
| 1247 |
|
| selected with -MJit or (later) with perl -j. |
| 1248 |
|
| |
| 1249 |
|
| All ops are unrolled in execution order for the CPU cache, |
| 1250 |
|
| prefetching is the main advantage of this function. |
| 1251 |
|
| The ASYNC check is only done when necessary. |
| 1252 |
|
| |
| 1253 |
|
| For now only implemented for x86/amd64 with certain hardcoded |
| 1254 |
|
| my_perl offsets for threaded perls. |
| 1255 |
|
| XXX Need to check offsets for older threaded perls. |
| 1256 |
|
| */ |
| 1257 |
|
| int |
| 1258 |
0 |
| Perl_runops_jit(pTHX) |
| 1259 |
|
| { |
| 1260 |
|
| #ifdef dVAR |
| 1261 |
|
| dVAR; |
| 1262 |
|
| #endif |
| 1263 |
|
| #ifdef DEBUGGING |
| 1264 |
|
| static int line = 0; |
| 1265 |
|
| register int i; |
| 1266 |
|
| FILE *fh; |
| 1267 |
|
| char *opname; |
| 1268 |
|
| #if defined(DEBUGGING) && defined(__GNUC__) |
| 1269 |
|
| FILE *stabs; |
| 1270 |
|
| #endif |
| 1271 |
|
| #endif |
| 1272 |
|
| U32 rel; /* 4 byte int */ |
| 1273 |
|
| CODE *code, *code_start; |
| 1274 |
|
| OP *root; |
| 1275 |
0 |
| int pagesize = 4096, size = 0; |
| 1276 |
|
| #ifdef PROFILING |
| 1277 |
|
| SV *sv_prof; |
| 1278 |
|
| NV bench; |
| 1279 |
|
| int profiling = 1; /* default on, but can be turned off via $Jit::_profiling=0; */ |
| 1280 |
|
| sv_prof = get_sv("Jit::_profiling", 0); |
| 1281 |
|
| if (sv_prof) { |
| 1282 |
|
| profiling = SvIV_nomg(sv_prof); |
| 1283 |
|
| } |
| 1284 |
|
| #endif |
| 1285 |
|
| |
| 1286 |
|
| #ifdef DEBUGGING |
| 1287 |
|
| # if PERL_VERSION > 11 |
| 1288 |
|
| if (!PL_op) { |
| 1289 |
|
| Perl_ck_warner_d(aTHX_ packWARN(WARN_DEBUGGING), "NULL OP IN JIT RUN"); |
| 1290 |
|
| return 0; |
| 1291 |
|
| } |
| 1292 |
|
| # endif |
| 1293 |
|
| DEBUG_l(Perl_deb(aTHX_ "Entering new RUNOPS JIT level\n")); |
| 1294 |
|
| #endif |
| 1295 |
|
| |
| 1296 |
|
| /* quirky pass 1: need size to allocate code. |
| 1297 |
|
| PL_slab_count should be near the optree size, but our method is safe. |
| 1298 |
|
| Need to time that against an realloc checker in pass 2. |
| 1299 |
|
| */ |
| 1300 |
0 |
| code = 0; |
| 1301 |
|
| #ifdef DEBUGGING |
| 1302 |
|
| fh = fopen("run-jit.c", "a"); |
| 1303 |
|
| fprintf(fh, |
| 1304 |
|
| "struct op { OP* op_next; OP* op_other } OP;" |
| 1305 |
|
| # ifdef USE_ITHREADS |
| 1306 |
|
| "struct PerlInterpreter { OP* IOp; int Isig_pending; };" |
| 1307 |
|
| # endif |
| 1308 |
|
| # if (PERL_VERSION > 6) && (PERL_VERSION < 13) |
| 1309 |
|
| "void *PL_sig_pending;" |
| 1310 |
|
| # endif |
| 1311 |
|
| "OP *PL_op; void runops_jit_%d (void);\n" |
| 1312 |
|
| "void runops_jit_%d (void){ OP* op;\n" |
| 1313 |
|
| , global_loops, global_loops); |
| 1314 |
|
| line += 2; |
| 1315 |
|
| #endif |
| 1316 |
0 |
| root = PL_op; |
| 1317 |
|
| #ifdef PROFILING |
| 1318 |
|
| if (profiling) { |
| 1319 |
|
| bench = mytime(); |
| 1320 |
|
| } |
| 1321 |
|
| #endif |
| 1322 |
0 |
| size = 0; |
| 1323 |
0 |
| size += sizeof(PROLOG); |
| 1324 |
0 |
| size += JIT_CHAIN_DRYRUN(PL_op); |
| 1325 |
0 |
| size += sizeof(EPILOG); |
| 1326 |
0 |
▶ | while ((size | 0xfffffff0) % PTRSIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size | 0xfffffff0) % PTRSIZE) { |
0 |
0 |
0 |
|
| 1327 |
0 |
| size++; |
| 1328 |
|
| } |
| 1329 |
|
| #ifdef _WIN32 |
| 1330 |
|
| code = VirtualAlloc(NULL, size, |
| 1331 |
|
| MEM_COMMIT | MEM_RESERVE, |
| 1332 |
|
| PAGE_EXECUTE_READWRITE); |
| 1333 |
|
| #else |
| 1334 |
|
| /* amd64/linux+bsd disallow mprotect'ing an unaligned heap. Windows/cygwin even on i386. |
| 1335 |
|
| We NEED to start it in a fresh new page. */ |
| 1336 |
|
| # ifdef HAS_GETPAGESIZE |
| 1337 |
0 |
| pagesize = getpagesize(); |
| 1338 |
|
| # endif |
| 1339 |
|
| # ifdef HAVE_MEMALIGN |
| 1340 |
0 |
| code = (char*)memalign(pagesize, size*sizeof(char)); |
| 1341 |
|
| # else |
| 1342 |
|
| # ifdef HAVE_POSIX_MEMALIGN |
| 1343 |
|
| if (posix_memalign((void**)&code, pagesize, size*sizeof(char))) { |
| 1344 |
|
| croak("posix_memalign(code,%d,%d) failed", pagesize, size); |
| 1345 |
|
| } |
| 1346 |
|
| # else |
| 1347 |
|
| /* e.g. openbsd has no memalign, but aligns automatically to page boundary |
| 1348 |
|
| if the size is "big enough", around the pagesize. */ |
| 1349 |
|
| if (size < pagesize) size = pagesize; |
| 1350 |
|
| code = (char*)malloc(size); |
| 1351 |
|
| if ((int)code & (pagesize-1)) { /* need to align it manually to 0x1000 */ |
| 1352 |
|
| int newsize = pagesize - ((int)code & (pagesize-1)); |
| 1353 |
|
| free(code); |
| 1354 |
|
| code = (char*)malloc(size + newsize); |
| 1355 |
|
| DEBUG_v( printf("# manually align code=0x%x newsize=%x\n",code, size + newsize) ); |
| 1356 |
|
| if ((int)code & (pagesize-1)) { |
| 1357 |
|
| /* hardcode pagesize = 4096 */ |
| 1358 |
|
| # if PTRSIZE == 4 |
| 1359 |
|
| code = (char*)(((int)code & 0xfffff000) + 0x1000); |
| 1360 |
|
| # else |
| 1361 |
|
| code = (char*)(((int)code & 0xfffffffffffff000) + 0x1000); |
| 1362 |
|
| # endif |
| 1363 |
|
| DEBUG_v( printf("# re-aligned stripped code=0x%x size=%u\n",code, size) ); |
| 1364 |
|
| } |
| 1365 |
|
| } |
| 1366 |
|
| # endif |
| 1367 |
|
| # endif |
| 1368 |
|
| #endif |
| 1369 |
0 |
| code_start = code; |
| 1370 |
|
| |
| 1371 |
|
| #if defined(DEBUGGING) && defined(__GNUC__) |
| 1372 |
|
| stabs = fopen("run-jit.s", "a"); |
| 1373 |
|
| /* filename info */ |
| 1374 |
|
| fprintf(stabs, ".data\n.text\n"); /* darwin needs that */ |
| 1375 |
|
| fprintf(stabs, ".file \"run-jit.c\"\n"); |
| 1376 |
|
| fprintf(stabs, ".stabs \"%s\",100,0,0,0\n", "run-jit.c"); /* filename */ |
| 1377 |
|
| /* jit_func start addr */ |
| 1378 |
|
| fprintf(stabs, ".stabs \"runops_jit_%d:F(0,1)\",36,0,2,%p\n", |
| 1379 |
|
| global_loops, code); |
| 1380 |
|
| fprintf(stabs, ".stabs \"Void:t(0,0)=(0,1)\",128,0,0,0\n"); /* stack variable types */ |
| 1381 |
|
| fprintf(stabs, ".stabs \"struct op:t(0,5)=*(0,6)\",128,0,0,0\n"); |
| 1382 |
|
| # ifdef USE_ITHREADS |
| 1383 |
|
| fprintf(stabs, ".stabs \"PerlInterpreter:S(0,12)\",38,0,0,%p\n", /* variable in data section */ |
| 1384 |
|
| # ifdef MOV_REL |
| 1385 |
|
| 0 /*(CODE*)&my_perl - code*/ |
| 1386 |
|
| # else |
| 1387 |
|
| (char*)&my_perl |
| 1388 |
|
| # endif |
| 1389 |
|
| ); |
| 1390 |
|
| # endif |
| 1391 |
|
| fprintf(stabs, ".stabn 68,0,%d,0\n", line); |
| 1392 |
|
| #endif |
| 1393 |
|
| #ifdef PROFILING |
| 1394 |
|
| if (profiling) { |
| 1395 |
|
| printf("jit pass 1:\t%0.12f\n", mytime() - bench); |
| 1396 |
|
| bench = mytime(); |
| 1397 |
|
| } |
| 1398 |
|
| #endif |
| 1399 |
|
| |
| 1400 |
|
| /* pass 2: jit */ |
| 1401 |
0 |
| code = push_prolog(code); |
| 1402 |
0 |
| PL_op = root; |
| 1403 |
0 |
| code = JIT_CHAIN(PL_op); |
| 1404 |
0 |
| PUSHc(EPILOG); |
| 1405 |
0 |
▶ | while (((unsigned int)code | 0xfffffff0) % PTRSIZE) { *(code++) = NOP[0]; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (((unsigned int)code | 0xfffffff0) % PTRSIZE) { *(code++) = NOP[0]; } |
0 |
0 |
0 |
|
| 1406 |
|
| /* XXX TODO patchup missed jmp or sub or loop targets */ |
| 1407 |
|
| |
| 1408 |
|
| #ifdef PROFILING |
| 1409 |
|
| if (profiling) { |
| 1410 |
|
| printf("jit pass 2:\t%0.12f\n", mytime() - bench); |
| 1411 |
|
| bench = mytime(); |
| 1412 |
|
| } |
| 1413 |
|
| #endif |
| 1414 |
|
| |
| 1415 |
|
| #ifdef DEBUGGING |
| 1416 |
|
| fprintf(fh, "}\n"); |
| 1417 |
|
| line++; |
| 1418 |
|
| fclose(fh); |
| 1419 |
|
| # if defined(DEBUGGING) && defined(__GNUC__) |
| 1420 |
|
| fprintf(stabs, ".stabs \"\",36,0,1,%p\n", (char *)size); /* eof */ |
| 1421 |
|
| /* for stabs: as run-jit.s; gdb add-symbol-file run-jit.o 0 */ |
| 1422 |
|
| fclose(stabs); |
| 1423 |
|
| system("as run-jit.s -o run-jit.o"); |
| 1424 |
|
| # endif |
| 1425 |
|
| # ifdef HAVE_DISPATCH |
| 1426 |
|
| DEBUG_v( printf("# Perl_despatch_signals \t= 0x%x\n", |
| 1427 |
|
| Perl_despatch_signals) ); |
| 1428 |
|
| # if !defined(USE_ITHREADS) |
| 1429 |
|
| DEBUG_v( printf("# &PL_sig_pending \t= 0x%x\n", &PL_sig_pending) ); |
| 1430 |
|
| # endif |
| 1431 |
|
| # endif |
| 1432 |
|
| global_loops++; |
| 1433 |
|
| #endif |
| 1434 |
0 |
▶ | if (jmptargets) free(jmptargets); |
Branch: 1 branch
| Branch | True | False | Total |
| if (jmptargets) free(jmptargets); |
0 |
0 |
0 |
|
| 1435 |
0 |
▶ | if (looptargets) free(looptargets); |
Branch: 1 branch
| Branch | True | False | Total |
| if (looptargets) free(looptargets); |
0 |
0 |
0 |
|
| 1436 |
|
| /*I_ASSERT(size == (code - code_start));*/ |
| 1437 |
|
| /*size = code - code_start;*/ |
| 1438 |
|
| |
| 1439 |
0 |
| PL_op = root; |
| 1440 |
0 |
| code = code_start; |
| 1441 |
|
| #ifdef HAS_MPROTECT |
| 1442 |
0 |
▶ | if (mprotect(code,size*sizeof(char),PROT_EXEC|PROT_READ|PROT_WRITE) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (mprotect(code,size*sizeof(char),PROT_EXEC|PROT_READ|PROT_WRITE) < 0) |
0 |
0 |
0 |
|
| 1443 |
0 |
| croak ("mprotect code=0x%x for size=%u failed", code, size); |
| 1444 |
|
| #endif |
| 1445 |
|
| /* XXX Missing. Prepare for execution: flush CPU cache. Needed only on ppc32 and ppc64 */ |
| 1446 |
|
| |
| 1447 |
|
| /* gdb: disassemble code code+200 */ |
| 1448 |
|
| #if defined(DEBUGGING) && defined(DEBUG_v_TEST) |
| 1449 |
|
| if (DEBUG_v_TEST) { |
| 1450 |
|
| DEBUG_v( printf("# &PL_op \t= 0x%x / *0x%x\n", &PL_op, PL_op) ); |
| 1451 |
|
| DEBUG_t( printf("# debop \t= 0x%x\n", &Perl_debop) ); |
| 1452 |
|
| # ifdef USE_ITHREADS |
| 1453 |
|
| DEBUG_v( printf("# &my_perl \t= 0x%x / *0x%x\n", &my_perl, my_perl) ); |
| 1454 |
|
| # endif |
| 1455 |
|
| DEBUG_v( printf("# code() 0x%x size %d (0x%x)",code,size,size) ); |
| 1456 |
|
| # ifndef HAVE_LIBDISASM |
| 1457 |
|
| for (i=0; i < size; i++) { |
| 1458 |
|
| if (!(i % 8)) DEBUG_v( printf("\n#(code+%3x): ", i) ); |
| 1459 |
|
| DEBUG_v( printf("%02x ",code[i]) ); |
| 1460 |
|
| } |
| 1461 |
|
| # endif |
| 1462 |
|
| DEBUG_v( printf("\n# runops_jit_%d\n",global_loops-1) ); |
| 1463 |
|
| } |
| 1464 |
|
| |
| 1465 |
|
| /* How to disassemble per command line: |
| 1466 |
|
| echo "55 89 e5 53 51 83 ec 08" |xxd -r -p - > xx |
| 1467 |
|
| objdump -D --target=binary --architecture i386:intel xx |
| 1468 |
|
| |
| 1469 |
|
| 0: 55 push ebp |
| 1470 |
|
| 1: 89 e5 mov ebp,esp |
| 1471 |
|
| 3: 53 push ebx |
| 1472 |
|
| 4: 51 push ecx |
| 1473 |
|
| 5: 83 ec 08 sub esp,0x8 |
| 1474 |
|
| |
| 1475 |
|
| */ |
| 1476 |
|
| if (DEBUG_v_TEST) { |
| 1477 |
|
| # ifdef HAVE_LIBDISASM |
| 1478 |
|
| # define LINE_SIZE 255 |
| 1479 |
|
| char line[LINE_SIZE]; |
| 1480 |
|
| int pos = 0; |
| 1481 |
|
| int insnsize; /* size of instruction */ |
| 1482 |
|
| x86_insn_t insn; /* one instruction */ |
| 1483 |
|
| |
| 1484 |
|
| x86_init(opt_none, NULL, NULL); |
| 1485 |
|
| while ( pos < size ) { |
| 1486 |
|
| insnsize = x86_disasm(code, size, 0, pos, &insn); |
| 1487 |
|
| if ( insnsize ) { |
| 1488 |
|
| x86_format_insn(&insn, line, LINE_SIZE, att_syntax); |
| 1489 |
|
| printf("#(code+%3x): ", pos); |
| 1490 |
|
| for ( i = 0; i < 10; i++ ) { |
| 1491 |
|
| if ( i < insn.size ) printf(" %02x", insn.bytes[i]); |
| 1492 |
|
| else printf(" "); |
| 1493 |
|
| } |
| 1494 |
|
| printf("%s\n", line); |
| 1495 |
|
| pos += insnsize; |
| 1496 |
|
| } else { |
| 1497 |
|
| printf("# Invalid instruction at 0x%x. size=0x%x\n", pos, size); |
| 1498 |
|
| pos++; |
| 1499 |
|
| } |
| 1500 |
|
| } |
| 1501 |
|
| x86_cleanup(); |
| 1502 |
|
| # else |
| 1503 |
|
| fh = fopen("run-jit.bin", "w"); |
| 1504 |
|
| fwrite(code,size,1,fh); |
| 1505 |
|
| fclose(fh); |
| 1506 |
|
| system("objdump -D --target=binary --architecture i386" |
| 1507 |
|
| # ifdef JIT_CPU_AMD64 |
| 1508 |
|
| ":x86-64" |
| 1509 |
|
| # endif |
| 1510 |
|
| " run-jit.bin"); |
| 1511 |
|
| # endif |
| 1512 |
|
| } |
| 1513 |
|
| #endif |
| 1514 |
|
| |
| 1515 |
|
| /*================= Jit.xs:1486 runops_jit_0 == disassemble code code+40 =====*/ |
| 1516 |
0 |
| (*((void (*)(pTHX))code))(aTHX); |
| 1517 |
|
| /*================= runops_jit ==============================================*/ |
| 1518 |
|
| DEBUG_l(Perl_deb(aTHX_ "leaving RUNOPS JIT level\n")); |
| 1519 |
|
| #ifdef PROFILING |
| 1520 |
|
| if (profiling) { |
| 1521 |
|
| printf("jit runloop:\t%0.12f\n", mytime() - bench); |
| 1522 |
|
| bench = mytime(); |
| 1523 |
|
| } |
| 1524 |
|
| #endif |
| 1525 |
|
| |
| 1526 |
0 |
| TAINT_NOT; |
| 1527 |
|
| #ifdef _WIN32 |
| 1528 |
|
| VirtualFree(code, 0, MEM_RELEASE); |
| 1529 |
|
| #else |
| 1530 |
0 |
| free(code); |
| 1531 |
|
| #endif |
| 1532 |
0 |
▶ | if (otherops) |
Branch: 1 branch
| Branch | True | False | Total |
| if (otherops) |
0 |
0 |
0 |
|
| 1533 |
0 |
| sv_free((SV*)otherops); |
| 1534 |
0 |
▶ | if (otherops1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (otherops1) |
0 |
0 |
0 |
|
| 1535 |
0 |
| sv_free((SV*)otherops1); |
| 1536 |
|
| |
| 1537 |
|
| #ifdef PROFILING |
| 1538 |
|
| if (profiling) { |
| 1539 |
|
| PL_op = root; |
| 1540 |
|
| register OP *op = PL_op; |
| 1541 |
|
| while ((PL_op = op = op->op_ppaddr(aTHX))) { |
| 1542 |
|
| } |
| 1543 |
|
| printf("unjit runloop:\t%0.12f\n", mytime() - bench); |
| 1544 |
|
| } |
| 1545 |
|
| #endif |
| 1546 |
|
| |
| 1547 |
0 |
| return 0; |
| 1548 |
|
| } |
| 1549 |
|
| |
| 1550 |
|
| MODULE=Jit PACKAGE=Jit |
| 1551 |
|
| |
| 1552 |
|
| PROTOTYPES: DISABLE |
| 1553 |
|
| |
| 1554 |
|
| BOOT: |
| 1555 |
|
| #ifdef DEBUGGING |
| 1556 |
|
| # ifdef __GNUC__ |
| 1557 |
|
| struct stat statbuf; |
| 1558 |
|
| # endif |
| 1559 |
|
| unlink("run-jit.c"); |
| 1560 |
|
| # ifdef __GNUC__ |
| 1561 |
|
| if (!stat("run-jit.s", &statbuf)) |
| 1562 |
|
| unlink("run-jit.s"); |
| 1563 |
|
| # endif |
| 1564 |
|
| #endif |
| 1565 |
|
| #ifdef JIT_CPU |
| 1566 |
0 |
| sv_setsv(get_sv("Jit::CPU", GV_ADD), newSVpv(JIT_CPU, 0)); |
| 1567 |
0 |
| PL_runops = Perl_runops_jit; |
| 1568 |
|
| #endif |