X-Git-Url: https://git.saurik.com/apple/javascriptcore.git/blobdiff_plain/81345200c95645a1b0d2635520f96ad55dfde63f..refs/heads/master:/assembler/MacroAssemblerX86Common.cpp diff --git a/assembler/MacroAssemblerX86Common.cpp b/assembler/MacroAssemblerX86Common.cpp index 0fab05f..0108ef4 100644 --- a/assembler/MacroAssemblerX86Common.cpp +++ b/assembler/MacroAssemblerX86Common.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2013 Apple Inc. All rights reserved. + * Copyright (C) 2013, 2014 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -30,53 +30,120 @@ namespace JSC { -#if USE(MASM_PROBE) +#if ENABLE(MASM_PROBE) -void MacroAssemblerX86Common::ProbeContext::dumpCPURegisters(const char* indentation) +#define INDENT printIndent(indentation) + +void MacroAssemblerX86Common::printCPURegisters(MacroAssemblerX86Common::CPUState& cpu, int indentation) { #if CPU(X86) - #define DUMP_GPREGISTER(_type, _regName) { \ + #define PRINT_GPREGISTER(_type, _regName) { \ int32_t value = reinterpret_cast(cpu._regName); \ - dataLogF("%s %6s: 0x%08x %d\n", indentation, #_regName, value, value) ; \ + INDENT, dataLogF("%6s: 0x%08x %d\n", #_regName, value, value) ; \ } #elif CPU(X86_64) - #define DUMP_GPREGISTER(_type, _regName) { \ + #define PRINT_GPREGISTER(_type, _regName) { \ int64_t value = reinterpret_cast(cpu._regName); \ - dataLogF("%s %6s: 0x%016llx %lld\n", indentation, #_regName, value, value) ; \ + INDENT, dataLogF("%6s: 0x%016llx %lld\n", #_regName, value, value) ; \ } #endif - FOR_EACH_CPU_GPREGISTER(DUMP_GPREGISTER) - FOR_EACH_CPU_SPECIAL_REGISTER(DUMP_GPREGISTER) - #undef DUMP_GPREGISTER + FOR_EACH_CPU_GPREGISTER(PRINT_GPREGISTER) + FOR_EACH_CPU_SPECIAL_REGISTER(PRINT_GPREGISTER) + #undef PRINT_GPREGISTER - #define DUMP_FPREGISTER(_type, _regName) { \ - uint32_t* u = reinterpret_cast(&cpu._regName); \ + #define PRINT_FPREGISTER(_type, _regName) { \ + uint64_t* u = reinterpret_cast(&cpu._regName); \ double* d = reinterpret_cast(&cpu._regName); \ - dataLogF("%s %6s: 0x%08x%08x 0x%08x%08x %12g %12g\n", \ - indentation, #_regName, u[3], u[2], u[1], u[0], d[1], d[0]); \ + INDENT, dataLogF("%6s: 0x%016llx %.13g\n", #_regName, *u, *d); \ } - FOR_EACH_CPU_FPREGISTER(DUMP_FPREGISTER) - #undef DUMP_FPREGISTER + FOR_EACH_CPU_FPREGISTER(PRINT_FPREGISTER) + #undef PRINT_FPREGISTER } -void MacroAssemblerX86Common::ProbeContext::dump(const char* indentation) +#undef INDENT + +void MacroAssemblerX86Common::printRegister(MacroAssemblerX86Common::CPUState& cpu, RegisterID regID) { - if (!indentation) - indentation = ""; + const char* name = CPUState::registerName(regID); + union { + void* voidPtr; + intptr_t intptrValue; + } u; + u.voidPtr = cpu.registerValue(regID); + dataLogF("%s:<%p %ld>", name, u.voidPtr, u.intptrValue); +} - dataLogF("%sProbeContext %p {\n", indentation, this); - dataLogF("%s probeFunction: %p\n", indentation, probeFunction); - dataLogF("%s arg1: %p %llu\n", indentation, arg1, reinterpret_cast(arg1)); - dataLogF("%s arg2: %p %llu\n", indentation, arg2, reinterpret_cast(arg2)); - dataLogF("%s cpu: {\n", indentation); +void MacroAssemblerX86Common::printRegister(MacroAssemblerX86Common::CPUState& cpu, FPRegisterID regID) +{ + const char* name = CPUState::registerName(regID); + union { + double doubleValue; + uint64_t uint64Value; + } u; + u.doubleValue = cpu.registerValue(regID); + dataLogF("%s:<0x%016llx %.13g>", name, u.uint64Value, u.doubleValue); +} - dumpCPURegisters(indentation); +extern "C" void ctiMasmProbeTrampoline(); - dataLogF("%s }\n", indentation); - dataLogF("%s}\n", indentation); +// What code is emitted for the probe? +// ================================== +// We want to keep the size of the emitted probe invocation code as compact as +// possible to minimize the perturbation to the JIT generated code. However, +// we also need to preserve the CPU registers and set up the ProbeContext to be +// passed to the user probe function. +// +// Hence, we do only the minimum here to preserve a scratch register (i.e. rax +// in this case) and the stack pointer (i.e. rsp), and pass the probe arguments. +// We'll let the ctiMasmProbeTrampoline handle the rest of the probe invocation +// work i.e. saving the CPUState (and setting up the ProbeContext), calling the +// user probe function, and restoring the CPUState before returning to JIT +// generated code. +// +// What registers need to be saved? +// =============================== +// The registers are saved for 2 reasons: +// 1. To preserve their state in the JITted code. This means that all registers +// that are not callee saved needs to be saved. We also need to save the +// condition code registers because the probe can be inserted between a test +// and a branch. +// 2. To allow the probe to inspect the values of the registers for debugging +// purposes. This means all registers need to be saved. +// +// In summary, save everything. But for reasons stated above, we should do the +// minimum here and let ctiMasmProbeTrampoline do the heavy lifting to save the +// full set. +// +// What values are in the saved registers? +// ====================================== +// Conceptually, the saved registers should contain values as if the probe +// is not present in the JIT generated code. Hence, they should contain values +// that are expected at the start of the instruction immediately following the +// probe. +// +// Specifically, the saved stack pointer register will point to the stack +// position before we push the ProbeContext frame. The saved rip will point to +// the address of the instruction immediately following the probe. + +void MacroAssemblerX86Common::probe(MacroAssemblerX86Common::ProbeFunction function, void* arg1, void* arg2) +{ + push(RegisterID::esp); + push(RegisterID::eax); + move(TrustedImmPtr(arg2), RegisterID::eax); + push(RegisterID::eax); + move(TrustedImmPtr(arg1), RegisterID::eax); + push(RegisterID::eax); + move(TrustedImmPtr(reinterpret_cast(function)), RegisterID::eax); + push(RegisterID::eax); + move(TrustedImmPtr(reinterpret_cast(ctiMasmProbeTrampoline)), RegisterID::eax); + call(RegisterID::eax); } -#endif // USE(MASM_PROBE) +#endif // ENABLE(MASM_PROBE) + +#if CPU(X86) && !OS(MAC_OS_X) +MacroAssemblerX86Common::SSE2CheckState MacroAssemblerX86Common::s_sse2CheckState = NotCheckedSSE2; +#endif } // namespace JSC