js/src/assembler/TestMain.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5
michael@0 6 // A short test program with which to experiment with the assembler.
michael@0 7
michael@0 8 //satisfies CPU(X86_64)
michael@0 9 //#define WTF_CPU_X86_64
michael@0 10
michael@0 11 // satisfies ENABLE(ASSEMBLER)
michael@0 12 #define ENABLE_ASSEMBLER 1
michael@0 13
michael@0 14 // satisfies ENABLE(JIT)
michael@0 15 #define ENABLE_JIT 1
michael@0 16
michael@0 17 #define USE_SYSTEM_MALLOC 1
michael@0 18 // leads to FORCE_SYSTEM_MALLOC in wtf/FastMalloc.cpp
michael@0 19
michael@0 20 #include "assembler/jit/ExecutableAllocator.h"
michael@0 21 #include "assembler/assembler/LinkBuffer.h"
michael@0 22 #include "assembler/assembler/CodeLocation.h"
michael@0 23 #include "assembler/assembler/RepatchBuffer.h"
michael@0 24
michael@0 25 #include "assembler/assembler/MacroAssembler.h"
michael@0 26
michael@0 27 #include <stdio.h>
michael@0 28
michael@0 29 /////////////////////////////////////////////////////////////////
michael@0 30 // Temporary scaffolding for selecting the arch
michael@0 31 #undef ARCH_x86
michael@0 32 #undef ARCH_amd64
michael@0 33 #undef ARCH_arm
michael@0 34
michael@0 35 #if defined(__APPLE__) && defined(__i386__)
michael@0 36 # define ARCH_x86 1
michael@0 37 #elif defined(__APPLE__) && defined(__x86_64__)
michael@0 38 # define ARCH_amd64 1
michael@0 39 #elif defined(__linux__) && defined(__i386__)
michael@0 40 # define ARCH_x86 1
michael@0 41 #elif defined(__linux__) && defined(__x86_64__)
michael@0 42 # define ARCH_amd64 1
michael@0 43 #elif defined(__linux__) && defined(__arm__)
michael@0 44 # define ARCH_arm 1
michael@0 45 #elif defined(_MSC_VER) && defined(_M_IX86)
michael@0 46 # define ARCH_x86 1
michael@0 47 #endif
michael@0 48 /////////////////////////////////////////////////////////////////
michael@0 49
michael@0 50 // just somewhere convenient to put a breakpoint, before
michael@0 51 // running gdb
michael@0 52 #if WTF_COMPILER_GCC
michael@0 53 __attribute__((noinline))
michael@0 54 #endif
michael@0 55 void pre_run ( void ) { }
michael@0 56
michael@0 57 /////////////////////////////////////////////////////////////////
michael@0 58 //// test1 (simple straight line code)
michael@0 59 #if WTF_COMPILER_GCC
michael@0 60
michael@0 61 void test1 ( void )
michael@0 62 {
michael@0 63 printf("\n------------ Test 1 (straight line code) ------------\n\n" );
michael@0 64
michael@0 65 // Create new assembler
michael@0 66 JSC::MacroAssembler* am = new JSC::MacroAssembler();
michael@0 67
michael@0 68 #if defined(ARCH_amd64)
michael@0 69 JSC::X86Registers::RegisterID areg = JSC::X86Registers::r15;
michael@0 70 // dump some instructions into it
michael@0 71 // xor %r15,%r15
michael@0 72 // add $0x7b,%r15
michael@0 73 // add $0x141,%r15
michael@0 74 // retq
michael@0 75 am->xorPtr(areg,areg);
michael@0 76 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 77 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 78 am->ret();
michael@0 79 #endif
michael@0 80
michael@0 81 #if defined(ARCH_x86)
michael@0 82 JSC::X86Registers::RegisterID areg = JSC::X86Registers::edi;
michael@0 83 // dump some instructions into it
michael@0 84 // xor %edi,%edi
michael@0 85 // add $0x7b,%edi
michael@0 86 // add $0x141,%edi
michael@0 87 // ret
michael@0 88 am->xorPtr(areg,areg);
michael@0 89 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 90 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 91 am->ret();
michael@0 92 #endif
michael@0 93
michael@0 94 #if defined(ARCH_arm)
michael@0 95 JSC::ARMRegisters::RegisterID areg = JSC::ARMRegisters::r8;
michael@0 96 // eors r8, r8, r8
michael@0 97 // adds r8, r8, #123 ; 0x7b
michael@0 98 // mov r3, #256 ; 0x100
michael@0 99 // orr r3, r3, #65 ; 0x41
michael@0 100 // adds r8, r8, r3
michael@0 101 // mov pc, lr
michael@0 102 am->xorPtr(areg,areg);
michael@0 103 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 104 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 105 am->ret();
michael@0 106 #endif
michael@0 107
michael@0 108 // prepare a link buffer, into which we can copy the completed insns
michael@0 109 JSC::ExecutableAllocator* eal = new JSC::ExecutableAllocator();
michael@0 110
michael@0 111 // intermediate step .. get the pool suited for the size of code in 'am'
michael@0 112 //WTF::PassRefPtr<JSC::ExecutablePool> ep = eal->poolForSize( am->size() );
michael@0 113 JSC::ExecutablePool* ep = eal->poolForSize( am->size() );
michael@0 114
michael@0 115 // constructor for LinkBuffer asks ep to allocate r-x memory,
michael@0 116 // then copies it there.
michael@0 117 JSC::LinkBuffer patchBuffer(am, ep, JSC::METHOD_CODE);
michael@0 118
michael@0 119 // finalize
michael@0 120 JSC::MacroAssemblerCodeRef cr = patchBuffer.finalizeCode();
michael@0 121
michael@0 122 // cr now holds a pointer to the final runnable code.
michael@0 123 void* entry = cr.m_code.executableAddress();
michael@0 124
michael@0 125 printf("disas %p %p\n",
michael@0 126 entry, (char*)entry + cr.m_size);
michael@0 127 pre_run();
michael@0 128
michael@0 129 unsigned long result = 0x55555555;
michael@0 130
michael@0 131 #if defined(ARCH_amd64)
michael@0 132 // call the generated piece of code. It puts its result in r15.
michael@0 133 __asm__ __volatile__(
michael@0 134 "callq *%1" "\n\t"
michael@0 135 "movq %%r15, %0" "\n"
michael@0 136 :/*out*/ "=r"(result)
michael@0 137 :/*in*/ "r"(entry)
michael@0 138 :/*trash*/ "r15","cc"
michael@0 139 );
michael@0 140 #endif
michael@0 141 #if defined(ARCH_x86)
michael@0 142 // call the generated piece of code. It puts its result in edi.
michael@0 143 __asm__ __volatile__(
michael@0 144 "calll *%1" "\n\t"
michael@0 145 "movl %%edi, %0" "\n"
michael@0 146 :/*out*/ "=r"(result)
michael@0 147 :/*in*/ "r"(entry)
michael@0 148 :/*trash*/ "edi","cc"
michael@0 149 );
michael@0 150 #endif
michael@0 151 #if defined(ARCH_arm)
michael@0 152 // call the generated piece of code. It puts its result in r8.
michael@0 153 __asm__ __volatile__(
michael@0 154 "blx %1" "\n\t"
michael@0 155 "mov %0, %%r8" "\n"
michael@0 156 :/*out*/ "=r"(result)
michael@0 157 :/*in*/ "r"(entry)
michael@0 158 :/*trash*/ "r8","cc"
michael@0 159 );
michael@0 160 #endif
michael@0 161
michael@0 162 printf("\n");
michael@0 163 printf("value computed is %lu (expected 444)\n", result);
michael@0 164 printf("\n");
michael@0 165
michael@0 166 delete eal;
michael@0 167 delete am;
michael@0 168 }
michael@0 169
michael@0 170 #endif /* WTF_COMPILER_GCC */
michael@0 171
michael@0 172 /////////////////////////////////////////////////////////////////
michael@0 173 //// test2 (a simple counting-down loop)
michael@0 174 #if WTF_COMPILER_GCC
michael@0 175
michael@0 176 void test2 ( void )
michael@0 177 {
michael@0 178 printf("\n------------ Test 2 (mini loop) ------------\n\n" );
michael@0 179
michael@0 180 // Create new assembler
michael@0 181 JSC::MacroAssembler* am = new JSC::MacroAssembler();
michael@0 182
michael@0 183 #if defined(ARCH_amd64)
michael@0 184 JSC::X86Registers::RegisterID areg = JSC::X86Registers::r15;
michael@0 185 // xor %r15,%r15
michael@0 186 // add $0x7b,%r15
michael@0 187 // add $0x141,%r15
michael@0 188 // sub $0x1,%r15
michael@0 189 // mov $0x0,%r11
michael@0 190 // cmp %r11,%r15
michael@0 191 // jne 0x7ff6d3e6a00e
michael@0 192 // retq
michael@0 193 // so r15 always winds up being zero
michael@0 194 am->xorPtr(areg,areg);
michael@0 195 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 196 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 197
michael@0 198 JSC::MacroAssembler::Label loopHeadLabel(am);
michael@0 199 am->subPtr(JSC::MacroAssembler::Imm32(1), areg);
michael@0 200
michael@0 201 JSC::MacroAssembler::Jump j
michael@0 202 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 203 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 204 j.linkTo(loopHeadLabel, am);
michael@0 205
michael@0 206 am->ret();
michael@0 207 #endif
michael@0 208
michael@0 209 #if defined(ARCH_x86)
michael@0 210 JSC::X86Registers::RegisterID areg = JSC::X86Registers::edi;
michael@0 211 // xor %edi,%edi
michael@0 212 // add $0x7b,%edi
michael@0 213 // add $0x141,%edi
michael@0 214 // sub $0x1,%edi
michael@0 215 // test %edi,%edi
michael@0 216 // jne 0xf7f9700b
michael@0 217 // ret
michael@0 218 // so edi always winds up being zero
michael@0 219 am->xorPtr(areg,areg);
michael@0 220 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 221 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 222
michael@0 223 JSC::MacroAssembler::Label loopHeadLabel(am);
michael@0 224 am->subPtr(JSC::MacroAssembler::Imm32(1), areg);
michael@0 225
michael@0 226 JSC::MacroAssembler::Jump j
michael@0 227 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 228 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 229 j.linkTo(loopHeadLabel, am);
michael@0 230
michael@0 231 am->ret();
michael@0 232 #endif
michael@0 233
michael@0 234 #if defined(ARCH_arm)
michael@0 235 JSC::ARMRegisters::RegisterID areg = JSC::ARMRegisters::r8;
michael@0 236 // eors r8, r8, r8
michael@0 237 // adds r8, r8, #123 ; 0x7b
michael@0 238 // mov r3, #256 ; 0x100
michael@0 239 // orr r3, r3, #65 ; 0x41
michael@0 240 // adds r8, r8, r3
michael@0 241 // subs r8, r8, #1 ; 0x1
michael@0 242 // ldr r3, [pc, #8] ; 0x40026028
michael@0 243 // cmp r8, r3
michael@0 244 // bne 0x40026014
michael@0 245 // mov pc, lr
michael@0 246 // andeq r0, r0, r0 // DATA (0)
michael@0 247 // andeq r0, r0, r4, lsl r0 // DATA (?? what's this for?)
michael@0 248 // so r8 always winds up being zero
michael@0 249 am->xorPtr(areg,areg);
michael@0 250 am->addPtr(JSC::MacroAssembler::Imm32(123), areg);
michael@0 251 am->addPtr(JSC::MacroAssembler::Imm32(321), areg);
michael@0 252
michael@0 253 JSC::MacroAssembler::Label loopHeadLabel(am);
michael@0 254 am->subPtr(JSC::MacroAssembler::Imm32(1), areg);
michael@0 255
michael@0 256 JSC::MacroAssembler::Jump j
michael@0 257 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 258 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 259 j.linkTo(loopHeadLabel, am);
michael@0 260
michael@0 261 am->ret();
michael@0 262 #endif
michael@0 263
michael@0 264 // prepare a link buffer, into which we can copy the completed insns
michael@0 265 JSC::ExecutableAllocator* eal = new JSC::ExecutableAllocator();
michael@0 266
michael@0 267 // intermediate step .. get the pool suited for the size of code in 'am'
michael@0 268 //WTF::PassRefPtr<JSC::ExecutablePool> ep = eal->poolForSize( am->size() );
michael@0 269 JSC::ExecutablePool* ep = eal->poolForSize( am->size() );
michael@0 270
michael@0 271 // constructor for LinkBuffer asks ep to allocate r-x memory,
michael@0 272 // then copies it there.
michael@0 273 JSC::LinkBuffer patchBuffer(am, ep, JSC::METHOD_CODE);
michael@0 274
michael@0 275 // finalize
michael@0 276 JSC::MacroAssemblerCodeRef cr = patchBuffer.finalizeCode();
michael@0 277
michael@0 278 // cr now holds a pointer to the final runnable code.
michael@0 279 void* entry = cr.m_code.executableAddress();
michael@0 280
michael@0 281 printf("disas %p %p\n",
michael@0 282 entry, (char*)entry + cr.m_size);
michael@0 283 pre_run();
michael@0 284
michael@0 285 unsigned long result = 0x55555555;
michael@0 286
michael@0 287 #if defined(ARCH_amd64)
michael@0 288 // call the generated piece of code. It puts its result in r15.
michael@0 289 __asm__ __volatile__(
michael@0 290 "callq *%1" "\n\t"
michael@0 291 "movq %%r15, %0" "\n"
michael@0 292 :/*out*/ "=r"(result)
michael@0 293 :/*in*/ "r"(entry)
michael@0 294 :/*trash*/ "r15","cc"
michael@0 295 );
michael@0 296 #endif
michael@0 297 #if defined(ARCH_x86)
michael@0 298 // call the generated piece of code. It puts its result in edi.
michael@0 299 __asm__ __volatile__(
michael@0 300 "calll *%1" "\n\t"
michael@0 301 "movl %%edi, %0" "\n"
michael@0 302 :/*out*/ "=r"(result)
michael@0 303 :/*in*/ "r"(entry)
michael@0 304 :/*trash*/ "edi","cc"
michael@0 305 );
michael@0 306 #endif
michael@0 307 #if defined(ARCH_arm)
michael@0 308 // call the generated piece of code. It puts its result in r8.
michael@0 309 __asm__ __volatile__(
michael@0 310 "blx %1" "\n\t"
michael@0 311 "mov %0, %%r8" "\n"
michael@0 312 :/*out*/ "=r"(result)
michael@0 313 :/*in*/ "r"(entry)
michael@0 314 :/*trash*/ "r8","cc"
michael@0 315 );
michael@0 316 #endif
michael@0 317
michael@0 318 printf("\n");
michael@0 319 printf("value computed is %lu (expected 0)\n", result);
michael@0 320 printf("\n");
michael@0 321
michael@0 322 delete eal;
michael@0 323 delete am;
michael@0 324 }
michael@0 325
michael@0 326 #endif /* WTF_COMPILER_GCC */
michael@0 327
michael@0 328 /////////////////////////////////////////////////////////////////
michael@0 329 //// test3 (if-then-else)
michael@0 330 #if WTF_COMPILER_GCC
michael@0 331
michael@0 332 void test3 ( void )
michael@0 333 {
michael@0 334 printf("\n------------ Test 3 (if-then-else) ------------\n\n" );
michael@0 335
michael@0 336 // Create new assembler
michael@0 337 JSC::MacroAssembler* am = new JSC::MacroAssembler();
michael@0 338
michael@0 339 #if defined(ARCH_amd64)
michael@0 340 JSC::X86Registers::RegisterID areg = JSC::X86Registers::r15;
michael@0 341 // mov $0x64,%r15d
michael@0 342 // mov $0x0,%r11
michael@0 343 // cmp %r11,%r15
michael@0 344 // jne 0x7ff6d3e6a024
michael@0 345 // mov $0x40,%r15d
michael@0 346 // jmpq 0x7ff6d3e6a02a
michael@0 347 // mov $0x4,%r15d
michael@0 348 // retq
michael@0 349 // so r15 ends up being 4
michael@0 350
michael@0 351 // put a value in reg
michael@0 352 am->move(JSC::MacroAssembler::Imm32(100), areg);
michael@0 353
michael@0 354 // test, and conditionally jump to 'else' branch
michael@0 355 JSC::MacroAssembler::Jump jToElse
michael@0 356 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 357 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 358
michael@0 359 // 'then' branch
michael@0 360 am->move(JSC::MacroAssembler::Imm32(64), areg);
michael@0 361 JSC::MacroAssembler::Jump jToAfter
michael@0 362 = am->jump();
michael@0 363
michael@0 364 // 'else' branch
michael@0 365 JSC::MacroAssembler::Label elseLbl(am);
michael@0 366 am->move(JSC::MacroAssembler::Imm32(4), areg);
michael@0 367
michael@0 368 // after
michael@0 369 JSC::MacroAssembler::Label afterLbl(am);
michael@0 370
michael@0 371 am->ret();
michael@0 372 #endif
michael@0 373
michael@0 374 #if defined(ARCH_x86)
michael@0 375 JSC::X86Registers::RegisterID areg = JSC::X86Registers::edi;
michael@0 376 // mov $0x64,%edi
michael@0 377 // test %edi,%edi
michael@0 378 // jne 0xf7f22017
michael@0 379 // mov $0x40,%edi
michael@0 380 // jmp 0xf7f2201c
michael@0 381 // mov $0x4,%edi
michael@0 382 // ret
michael@0 383 // so edi ends up being 4
michael@0 384
michael@0 385 // put a value in reg
michael@0 386 am->move(JSC::MacroAssembler::Imm32(100), areg);
michael@0 387
michael@0 388 // test, and conditionally jump to 'else' branch
michael@0 389 JSC::MacroAssembler::Jump jToElse
michael@0 390 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 391 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 392
michael@0 393 // 'then' branch
michael@0 394 am->move(JSC::MacroAssembler::Imm32(64), areg);
michael@0 395 JSC::MacroAssembler::Jump jToAfter
michael@0 396 = am->jump();
michael@0 397
michael@0 398 // 'else' branch
michael@0 399 JSC::MacroAssembler::Label elseLbl(am);
michael@0 400 am->move(JSC::MacroAssembler::Imm32(4), areg);
michael@0 401
michael@0 402 // after
michael@0 403 JSC::MacroAssembler::Label afterLbl(am);
michael@0 404
michael@0 405 am->ret();
michael@0 406 #endif
michael@0 407
michael@0 408 #if defined(ARCH_arm)
michael@0 409 JSC::ARMRegisters::RegisterID areg = JSC::ARMRegisters::r8;
michael@0 410 // mov r8, #100 ; 0x64
michael@0 411 // ldr r3, [pc, #20] ; 0x40026020
michael@0 412 // cmp r8, r3
michael@0 413 // bne 0x40026018
michael@0 414 // mov r8, #64 ; 0x40
michael@0 415 // b 0x4002601c
michael@0 416 // mov r8, #4 ; 0x4
michael@0 417 // mov pc, lr
michael@0 418 // andeq r0, r0, r0 // DATA
michael@0 419 // andeq r0, r0, r8, lsl r0 // DATA
michael@0 420 // andeq r0, r0, r12, lsl r0 // DATA
michael@0 421 // ldr r3, [r3, -r3] // DATA
michael@0 422 // so r8 ends up being 4
michael@0 423
michael@0 424 // put a value in reg
michael@0 425 am->move(JSC::MacroAssembler::Imm32(100), areg);
michael@0 426
michael@0 427 // test, and conditionally jump to 'else' branch
michael@0 428 JSC::MacroAssembler::Jump jToElse
michael@0 429 = am->branchPtr(JSC::MacroAssembler::NotEqual,
michael@0 430 areg, JSC::MacroAssembler::ImmPtr(0));
michael@0 431
michael@0 432 // 'then' branch
michael@0 433 am->move(JSC::MacroAssembler::Imm32(64), areg);
michael@0 434 JSC::MacroAssembler::Jump jToAfter
michael@0 435 = am->jump();
michael@0 436
michael@0 437 // 'else' branch
michael@0 438 JSC::MacroAssembler::Label elseLbl(am);
michael@0 439 am->move(JSC::MacroAssembler::Imm32(4), areg);
michael@0 440
michael@0 441 // after
michael@0 442 JSC::MacroAssembler::Label afterLbl(am);
michael@0 443
michael@0 444 am->ret();
michael@0 445 #endif
michael@0 446
michael@0 447 // set branch targets appropriately
michael@0 448 jToElse.linkTo(elseLbl, am);
michael@0 449 jToAfter.linkTo(afterLbl, am);
michael@0 450
michael@0 451 // prepare a link buffer, into which we can copy the completed insns
michael@0 452 JSC::ExecutableAllocator* eal = new JSC::ExecutableAllocator();
michael@0 453
michael@0 454 // intermediate step .. get the pool suited for the size of code in 'am'
michael@0 455 //WTF::PassRefPtr<JSC::ExecutablePool> ep = eal->poolForSize( am->size() );
michael@0 456 JSC::ExecutablePool* ep = eal->poolForSize( am->size() );
michael@0 457
michael@0 458 // constructor for LinkBuffer asks ep to allocate r-x memory,
michael@0 459 // then copies it there.
michael@0 460 JSC::LinkBuffer patchBuffer(am, ep, JSC::METHOD_CODE);
michael@0 461
michael@0 462 // finalize
michael@0 463 JSC::MacroAssemblerCodeRef cr = patchBuffer.finalizeCode();
michael@0 464
michael@0 465 // cr now holds a pointer to the final runnable code.
michael@0 466 void* entry = cr.m_code.executableAddress();
michael@0 467
michael@0 468 printf("disas %p %p\n",
michael@0 469 entry, (char*)entry + cr.m_size);
michael@0 470 pre_run();
michael@0 471
michael@0 472 unsigned long result = 0x55555555;
michael@0 473
michael@0 474 #if defined(ARCH_amd64)
michael@0 475 // call the generated piece of code. It puts its result in r15.
michael@0 476 __asm__ __volatile__(
michael@0 477 "callq *%1" "\n\t"
michael@0 478 "movq %%r15, %0" "\n"
michael@0 479 :/*out*/ "=r"(result)
michael@0 480 :/*in*/ "r"(entry)
michael@0 481 :/*trash*/ "r15","cc"
michael@0 482 );
michael@0 483 #endif
michael@0 484 #if defined(ARCH_x86)
michael@0 485 // call the generated piece of code. It puts its result in edi.
michael@0 486 __asm__ __volatile__(
michael@0 487 "calll *%1" "\n\t"
michael@0 488 "movl %%edi, %0" "\n"
michael@0 489 :/*out*/ "=r"(result)
michael@0 490 :/*in*/ "r"(entry)
michael@0 491 :/*trash*/ "edi","cc"
michael@0 492 );
michael@0 493 #endif
michael@0 494 #if defined(ARCH_arm)
michael@0 495 // call the generated piece of code. It puts its result in r8.
michael@0 496 __asm__ __volatile__(
michael@0 497 "blx %1" "\n\t"
michael@0 498 "mov %0, %%r8" "\n"
michael@0 499 :/*out*/ "=r"(result)
michael@0 500 :/*in*/ "r"(entry)
michael@0 501 :/*trash*/ "r8","cc"
michael@0 502 );
michael@0 503 #endif
michael@0 504
michael@0 505 printf("\n");
michael@0 506 printf("value computed is %lu (expected 4)\n", result);
michael@0 507 printf("\n");
michael@0 508
michael@0 509 delete eal;
michael@0 510 delete am;
michael@0 511 }
michael@0 512
michael@0 513 #endif /* WTF_COMPILER_GCC */
michael@0 514
michael@0 515 /////////////////////////////////////////////////////////////////
michael@0 516 //// test4 (callable function)
michael@0 517
michael@0 518 void test4 ( void )
michael@0 519 {
michael@0 520 printf("\n------------ Test 4 (callable fn) ------------\n\n" );
michael@0 521
michael@0 522 // Create new assembler
michael@0 523 JSC::MacroAssembler* am = new JSC::MacroAssembler();
michael@0 524
michael@0 525 #if defined(ARCH_amd64)
michael@0 526 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 527 // push %rbp
michael@0 528 // mov %rsp,%rbp
michael@0 529 // push %rbx
michael@0 530 // push %r12
michael@0 531 // push %r13
michael@0 532 // push %r14
michael@0 533 // push %r15
michael@0 534 // xor %rax,%rax
michael@0 535 // add $0x7b,%rax
michael@0 536 // add $0x141,%rax
michael@0 537 // pop %r15
michael@0 538 // pop %r14
michael@0 539 // pop %r13
michael@0 540 // pop %r12
michael@0 541 // pop %rbx
michael@0 542 // mov %rbp,%rsp
michael@0 543 // pop %rbp
michael@0 544 // retq
michael@0 545 // callable as a normal function, returns 444
michael@0 546
michael@0 547 JSC::X86Registers::RegisterID rreg = JSC::X86Registers::eax;
michael@0 548 am->push(JSC::X86Registers::ebp);
michael@0 549 am->move(JSC::X86Registers::esp, JSC::X86Registers::ebp);
michael@0 550 am->push(JSC::X86Registers::ebx);
michael@0 551 am->push(JSC::X86Registers::r12);
michael@0 552 am->push(JSC::X86Registers::r13);
michael@0 553 am->push(JSC::X86Registers::r14);
michael@0 554 am->push(JSC::X86Registers::r15);
michael@0 555
michael@0 556 am->xorPtr(rreg,rreg);
michael@0 557 am->addPtr(JSC::MacroAssembler::Imm32(123), rreg);
michael@0 558 am->addPtr(JSC::MacroAssembler::Imm32(321), rreg);
michael@0 559
michael@0 560 am->pop(JSC::X86Registers::r15);
michael@0 561 am->pop(JSC::X86Registers::r14);
michael@0 562 am->pop(JSC::X86Registers::r13);
michael@0 563 am->pop(JSC::X86Registers::r12);
michael@0 564 am->pop(JSC::X86Registers::ebx);
michael@0 565 am->move(JSC::X86Registers::ebp, JSC::X86Registers::esp);
michael@0 566 am->pop(JSC::X86Registers::ebp);
michael@0 567 am->ret();
michael@0 568 #endif
michael@0 569
michael@0 570 #if defined(ARCH_x86)
michael@0 571 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 572 // push %ebp
michael@0 573 // mov %esp,%ebp
michael@0 574 // push %ebx
michael@0 575 // push %esi
michael@0 576 // push %edi
michael@0 577 // xor %eax,%eax
michael@0 578 // add $0x7b,%eax
michael@0 579 // add $0x141,%eax
michael@0 580 // pop %edi
michael@0 581 // pop %esi
michael@0 582 // pop %ebx
michael@0 583 // mov %ebp,%esp
michael@0 584 // pop %ebp
michael@0 585 // ret
michael@0 586 // callable as a normal function, returns 444
michael@0 587
michael@0 588 JSC::X86Registers::RegisterID rreg = JSC::X86Registers::eax;
michael@0 589
michael@0 590 am->push(JSC::X86Registers::ebp);
michael@0 591 am->move(JSC::X86Registers::esp, JSC::X86Registers::ebp);
michael@0 592 am->push(JSC::X86Registers::ebx);
michael@0 593 am->push(JSC::X86Registers::esi);
michael@0 594 am->push(JSC::X86Registers::edi);
michael@0 595
michael@0 596 am->xorPtr(rreg,rreg);
michael@0 597 am->addPtr(JSC::MacroAssembler::Imm32(123), rreg);
michael@0 598 am->addPtr(JSC::MacroAssembler::Imm32(321), rreg);
michael@0 599
michael@0 600 am->pop(JSC::X86Registers::edi);
michael@0 601 am->pop(JSC::X86Registers::esi);
michael@0 602 am->pop(JSC::X86Registers::ebx);
michael@0 603 am->move(JSC::X86Registers::ebp, JSC::X86Registers::esp);
michael@0 604 am->pop(JSC::X86Registers::ebp);
michael@0 605 am->ret();
michael@0 606 #endif
michael@0 607
michael@0 608 #if defined(ARCH_arm)
michael@0 609 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 610 // push {r4} ; (str r4, [sp, #-4]!)
michael@0 611 // push {r5} ; (str r5, [sp, #-4]!)
michael@0 612 // push {r6} ; (str r6, [sp, #-4]!)
michael@0 613 // push {r7} ; (str r7, [sp, #-4]!)
michael@0 614 // push {r8} ; (str r8, [sp, #-4]!)
michael@0 615 // push {r9} ; (str r9, [sp, #-4]!)
michael@0 616 // push {r10} ; (str r10, [sp, #-4]!)
michael@0 617 // push {r11} ; (str r11, [sp, #-4]!)
michael@0 618 // eors r0, r0, r0
michael@0 619 // adds r0, r0, #123 ; 0x7b
michael@0 620 // mov r3, #256 ; 0x100
michael@0 621 // orr r3, r3, #65 ; 0x41
michael@0 622 // adds r0, r0, r3
michael@0 623 // pop {r11} ; (ldr r11, [sp], #4)
michael@0 624 // pop {r10} ; (ldr r10, [sp], #4)
michael@0 625 // pop {r9} ; (ldr r9, [sp], #4)
michael@0 626 // pop {r8} ; (ldr r8, [sp], #4)
michael@0 627 // pop {r7} ; (ldr r7, [sp], #4)
michael@0 628 // pop {r6} ; (ldr r6, [sp], #4)
michael@0 629 // pop {r5} ; (ldr r5, [sp], #4)
michael@0 630 // pop {r4} ; (ldr r4, [sp], #4)
michael@0 631 // mov pc, lr
michael@0 632 // callable as a normal function, returns 444
michael@0 633
michael@0 634 JSC::ARMRegisters::RegisterID rreg = JSC::ARMRegisters::r0;
michael@0 635
michael@0 636 am->push(JSC::ARMRegisters::r4);
michael@0 637 am->push(JSC::ARMRegisters::r5);
michael@0 638 am->push(JSC::ARMRegisters::r6);
michael@0 639 am->push(JSC::ARMRegisters::r7);
michael@0 640 am->push(JSC::ARMRegisters::r8);
michael@0 641 am->push(JSC::ARMRegisters::r9);
michael@0 642 am->push(JSC::ARMRegisters::r10);
michael@0 643 am->push(JSC::ARMRegisters::r11);
michael@0 644
michael@0 645 am->xorPtr(rreg,rreg);
michael@0 646 am->addPtr(JSC::MacroAssembler::Imm32(123), rreg);
michael@0 647 am->addPtr(JSC::MacroAssembler::Imm32(321), rreg);
michael@0 648
michael@0 649 am->pop(JSC::ARMRegisters::r11);
michael@0 650 am->pop(JSC::ARMRegisters::r10);
michael@0 651 am->pop(JSC::ARMRegisters::r9);
michael@0 652 am->pop(JSC::ARMRegisters::r8);
michael@0 653 am->pop(JSC::ARMRegisters::r7);
michael@0 654 am->pop(JSC::ARMRegisters::r6);
michael@0 655 am->pop(JSC::ARMRegisters::r5);
michael@0 656 am->pop(JSC::ARMRegisters::r4);
michael@0 657
michael@0 658 am->ret();
michael@0 659 #endif
michael@0 660
michael@0 661 // prepare a link buffer, into which we can copy the completed insns
michael@0 662 JSC::ExecutableAllocator* eal = new JSC::ExecutableAllocator();
michael@0 663
michael@0 664 // intermediate step .. get the pool suited for the size of code in 'am'
michael@0 665 //WTF::PassRefPtr<JSC::ExecutablePool> ep = eal->poolForSize( am->size() );
michael@0 666 JSC::ExecutablePool* ep = eal->poolForSize( am->size() );
michael@0 667
michael@0 668 // constructor for LinkBuffer asks ep to allocate r-x memory,
michael@0 669 // then copies it there.
michael@0 670 JSC::LinkBuffer patchBuffer(am, ep, JSC::METHOD_CODE);
michael@0 671
michael@0 672 // now fix up any branches/calls
michael@0 673 //JSC::FunctionPtr target = JSC::FunctionPtr::FunctionPtr( &cube );
michael@0 674
michael@0 675 // finalize
michael@0 676 JSC::MacroAssemblerCodeRef cr = patchBuffer.finalizeCode();
michael@0 677
michael@0 678 // cr now holds a pointer to the final runnable code.
michael@0 679 void* entry = cr.m_code.executableAddress();
michael@0 680
michael@0 681 printf("disas %p %p\n",
michael@0 682 entry, (char*)entry + cr.m_size);
michael@0 683 pre_run();
michael@0 684
michael@0 685 // call the function
michael@0 686 unsigned long (*fn)(void) = (unsigned long (*)())entry;
michael@0 687 unsigned long result = fn();
michael@0 688
michael@0 689 printf("\n");
michael@0 690 printf("value computed is %lu (expected 444)\n", result);
michael@0 691 printf("\n");
michael@0 692
michael@0 693 delete eal;
michael@0 694 delete am;
michael@0 695 }
michael@0 696
michael@0 697
michael@0 698 /////////////////////////////////////////////////////////////////
michael@0 699 //// test5 (call in, out, repatch)
michael@0 700
michael@0 701 // a function which we will call from the JIT generated code
michael@0 702 unsigned long cube ( unsigned long x ) { return x * x * x; }
michael@0 703 unsigned long square ( unsigned long x ) { return x * x; }
michael@0 704
michael@0 705 void test5 ( void )
michael@0 706 {
michael@0 707 printf("\n--------- Test 5 (call in, out, repatch) ---------\n\n" );
michael@0 708
michael@0 709 // Create new assembler
michael@0 710 JSC::MacroAssembler* am = new JSC::MacroAssembler();
michael@0 711 JSC::MacroAssembler::Call cl;
michael@0 712 ptrdiff_t offset_of_call_insn;
michael@0 713
michael@0 714 #if defined(ARCH_amd64)
michael@0 715 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 716 // and then call a non-JIT-generated helper from within
michael@0 717 // this code
michael@0 718 // push %rbp
michael@0 719 // mov %rsp,%rbp
michael@0 720 // push %rbx
michael@0 721 // push %r12
michael@0 722 // push %r13
michael@0 723 // push %r14
michael@0 724 // push %r15
michael@0 725 // mov $0x9,%edi
michael@0 726 // mov $0x40187e,%r11
michael@0 727 // callq *%r11
michael@0 728 // pop %r15
michael@0 729 // pop %r14
michael@0 730 // pop %r13
michael@0 731 // pop %r12
michael@0 732 // pop %rbx
michael@0 733 // mov %rbp,%rsp
michael@0 734 // pop %rbp
michael@0 735 // retq
michael@0 736 JSC::MacroAssembler::Label startOfFnLbl(am);
michael@0 737 am->push(JSC::X86Registers::ebp);
michael@0 738 am->move(JSC::X86Registers::esp, JSC::X86Registers::ebp);
michael@0 739 am->push(JSC::X86Registers::ebx);
michael@0 740 am->push(JSC::X86Registers::r12);
michael@0 741 am->push(JSC::X86Registers::r13);
michael@0 742 am->push(JSC::X86Registers::r14);
michael@0 743 am->push(JSC::X86Registers::r15);
michael@0 744
michael@0 745 // let's compute cube(9). Move $9 to the first arg reg.
michael@0 746 am->move(JSC::MacroAssembler::Imm32(9), JSC::X86Registers::edi);
michael@0 747 cl = am->JSC::MacroAssembler::call();
michael@0 748
michael@0 749 // result is now in %rax. Leave it ther and just return.
michael@0 750
michael@0 751 am->pop(JSC::X86Registers::r15);
michael@0 752 am->pop(JSC::X86Registers::r14);
michael@0 753 am->pop(JSC::X86Registers::r13);
michael@0 754 am->pop(JSC::X86Registers::r12);
michael@0 755 am->pop(JSC::X86Registers::ebx);
michael@0 756 am->move(JSC::X86Registers::ebp, JSC::X86Registers::esp);
michael@0 757 am->pop(JSC::X86Registers::ebp);
michael@0 758 am->ret();
michael@0 759
michael@0 760 offset_of_call_insn
michael@0 761 = am->JSC::MacroAssembler::differenceBetween(startOfFnLbl, cl);
michael@0 762 if (0) printf("XXXXXXXX offset = %lu\n", offset_of_call_insn);
michael@0 763 #endif
michael@0 764
michael@0 765 #if defined(ARCH_x86)
michael@0 766 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 767 // and then call a non-JIT-generated helper from within
michael@0 768 // this code
michael@0 769 // push %ebp
michael@0 770 // mov %esp,%ebp
michael@0 771 // push %ebx
michael@0 772 // push %esi
michael@0 773 // push %edi
michael@0 774 // push $0x9
michael@0 775 // call 0x80490e9 <_Z4cubem>
michael@0 776 // add $0x4,%esp
michael@0 777 // pop %edi
michael@0 778 // pop %esi
michael@0 779 // pop %ebx
michael@0 780 // mov %ebp,%esp
michael@0 781 // pop %ebp
michael@0 782 // ret
michael@0 783 JSC::MacroAssembler::Label startOfFnLbl(am);
michael@0 784 am->push(JSC::X86Registers::ebp);
michael@0 785 am->move(JSC::X86Registers::esp, JSC::X86Registers::ebp);
michael@0 786 am->push(JSC::X86Registers::ebx);
michael@0 787 am->push(JSC::X86Registers::esi);
michael@0 788 am->push(JSC::X86Registers::edi);
michael@0 789
michael@0 790 // let's compute cube(9). Push $9 on the stack.
michael@0 791 am->push(JSC::MacroAssembler::Imm32(9));
michael@0 792 cl = am->JSC::MacroAssembler::call();
michael@0 793 am->addPtr(JSC::MacroAssembler::Imm32(4), JSC::X86Registers::esp);
michael@0 794 // result is now in %eax. Leave it there and just return.
michael@0 795
michael@0 796 am->pop(JSC::X86Registers::edi);
michael@0 797 am->pop(JSC::X86Registers::esi);
michael@0 798 am->pop(JSC::X86Registers::ebx);
michael@0 799 am->move(JSC::X86Registers::ebp, JSC::X86Registers::esp);
michael@0 800 am->pop(JSC::X86Registers::ebp);
michael@0 801 am->ret();
michael@0 802
michael@0 803 offset_of_call_insn
michael@0 804 = am->JSC::MacroAssembler::differenceBetween(startOfFnLbl, cl);
michael@0 805 if (0) printf("XXXXXXXX offset = %lu\n",
michael@0 806 (unsigned long)offset_of_call_insn);
michael@0 807 #endif
michael@0 808
michael@0 809 #if defined(ARCH_arm)
michael@0 810 // ADD FN PROLOGUE/EPILOGUE so as to make a mini-function
michael@0 811 // push {r4} ; (str r4, [sp, #-4]!)
michael@0 812 // push {r5} ; (str r5, [sp, #-4]!)
michael@0 813 // push {r6} ; (str r6, [sp, #-4]!)
michael@0 814 // push {r7} ; (str r7, [sp, #-4]!)
michael@0 815 // push {r8} ; (str r8, [sp, #-4]!)
michael@0 816 // push {r9} ; (str r9, [sp, #-4]!)
michael@0 817 // push {r10} ; (str r10, [sp, #-4]!)
michael@0 818 // push {r11} ; (str r11, [sp, #-4]!)
michael@0 819 // eors r0, r0, r0
michael@0 820 // adds r0, r0, #123 ; 0x7b
michael@0 821 // mov r3, #256 ; 0x100
michael@0 822 // orr r3, r3, #65 ; 0x41
michael@0 823 // adds r0, r0, r3
michael@0 824 // pop {r11} ; (ldr r11, [sp], #4)
michael@0 825 // pop {r10} ; (ldr r10, [sp], #4)
michael@0 826 // pop {r9} ; (ldr r9, [sp], #4)
michael@0 827 // pop {r8} ; (ldr r8, [sp], #4)
michael@0 828 // pop {r7} ; (ldr r7, [sp], #4)
michael@0 829 // pop {r6} ; (ldr r6, [sp], #4)
michael@0 830 // pop {r5} ; (ldr r5, [sp], #4)
michael@0 831 // pop {r4} ; (ldr r4, [sp], #4)
michael@0 832 // mov pc, lr
michael@0 833 // callable as a normal function, returns 444
michael@0 834 JSC::MacroAssembler::Label startOfFnLbl(am);
michael@0 835 am->push(JSC::ARMRegisters::r4);
michael@0 836 am->push(JSC::ARMRegisters::r5);
michael@0 837 am->push(JSC::ARMRegisters::r6);
michael@0 838 am->push(JSC::ARMRegisters::r7);
michael@0 839 am->push(JSC::ARMRegisters::r8);
michael@0 840 am->push(JSC::ARMRegisters::r9);
michael@0 841 am->push(JSC::ARMRegisters::r10);
michael@0 842 am->push(JSC::ARMRegisters::r11);
michael@0 843 am->push(JSC::ARMRegisters::lr);
michael@0 844
michael@0 845 // let's compute cube(9). Get $9 into r0.
michael@0 846 am->move(JSC::MacroAssembler::Imm32(9), JSC::ARMRegisters::r0);
michael@0 847 cl = am->JSC::MacroAssembler::call();
michael@0 848 // result is now in r0. Leave it there and just return.
michael@0 849
michael@0 850 am->pop(JSC::ARMRegisters::lr);
michael@0 851 am->pop(JSC::ARMRegisters::r11);
michael@0 852 am->pop(JSC::ARMRegisters::r10);
michael@0 853 am->pop(JSC::ARMRegisters::r9);
michael@0 854 am->pop(JSC::ARMRegisters::r8);
michael@0 855 am->pop(JSC::ARMRegisters::r7);
michael@0 856 am->pop(JSC::ARMRegisters::r6);
michael@0 857 am->pop(JSC::ARMRegisters::r5);
michael@0 858 am->pop(JSC::ARMRegisters::r4);
michael@0 859 am->ret();
michael@0 860
michael@0 861 offset_of_call_insn
michael@0 862 = am->JSC::MacroAssembler::differenceBetween(startOfFnLbl, cl);
michael@0 863 if (0) printf("XXXXXXXX offset = %lu\n",
michael@0 864 (unsigned long)offset_of_call_insn);
michael@0 865 #endif
michael@0 866
michael@0 867 // prepare a link buffer, into which we can copy the completed insns
michael@0 868 JSC::ExecutableAllocator* eal = new JSC::ExecutableAllocator();
michael@0 869
michael@0 870 // intermediate step .. get the pool suited for the size of code in 'am'
michael@0 871 //WTF::PassRefPtr<JSC::ExecutablePool> ep = eal->poolForSize( am->size() );
michael@0 872 JSC::ExecutablePool* ep = eal->poolForSize( am->size() );
michael@0 873
michael@0 874 // constructor for LinkBuffer asks ep to allocate r-x memory,
michael@0 875 // then copies it there.
michael@0 876 JSC::LinkBuffer patchBuffer(am, ep, JSC::METHOD_CODE);
michael@0 877
michael@0 878 // now fix up any branches/calls
michael@0 879 JSC::FunctionPtr target = JSC::FunctionPtr::FunctionPtr( &cube );
michael@0 880 patchBuffer.link(cl, target);
michael@0 881
michael@0 882 JSC::MacroAssemblerCodeRef cr = patchBuffer.finalizeCode();
michael@0 883
michael@0 884 // cr now holds a pointer to the final runnable code.
michael@0 885 void* entry = cr.m_code.executableAddress();
michael@0 886
michael@0 887 printf("disas %p %p\n",
michael@0 888 entry, (char*)entry + cr.m_size);
michael@0 889
michael@0 890
michael@0 891 pre_run();
michael@0 892
michael@0 893 printf("\n");
michael@0 894
michael@0 895 unsigned long (*fn)() = (unsigned long(*)())entry;
michael@0 896 unsigned long result = fn();
michael@0 897
michael@0 898 printf("value computed is %lu (expected 729)\n", result);
michael@0 899 printf("\n");
michael@0 900
michael@0 901 // now repatch the call in the JITted code to go elsewhere
michael@0 902 JSC::JITCode jc = JSC::JITCode::JITCode(entry, cr.m_size);
michael@0 903 JSC::CodeBlock cb = JSC::CodeBlock::CodeBlock(jc);
michael@0 904
michael@0 905 // the address of the call insn, that we want to prod
michael@0 906 JSC::MacroAssemblerCodePtr cp
michael@0 907 = JSC::MacroAssemblerCodePtr( ((char*)entry) + offset_of_call_insn );
michael@0 908
michael@0 909 JSC::RepatchBuffer repatchBuffer(&cb);
michael@0 910 repatchBuffer.relink( JSC::CodeLocationCall(cp),
michael@0 911 JSC::FunctionPtr::FunctionPtr( &square ));
michael@0 912
michael@0 913 result = fn();
michael@0 914 printf("value computed is %lu (expected 81)\n", result);
michael@0 915 printf("\n\n");
michael@0 916
michael@0 917 delete eal;
michael@0 918 delete am;
michael@0 919 }
michael@0 920
michael@0 921 /////////////////////////////////////////////////////////////////
michael@0 922
michael@0 923 int main ( void )
michael@0 924 {
michael@0 925 #if WTF_COMPILER_GCC
michael@0 926 test1();
michael@0 927 test2();
michael@0 928 test3();
michael@0 929 #endif
michael@0 930 test4();
michael@0 931 test5();
michael@0 932 return 0;
michael@0 933 }

mercurial