Adding test case for text-decoration property state change on applying different...
[WebKit-https.git] / Source / JavaScriptCore / assembler / LinkBuffer.cpp
1 /*
2  * Copyright (C) 2012 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "LinkBuffer.h"
28
29 #if ENABLE(ASSEMBLER)
30
31 #include "Options.h"
32 #include <wtf/CompilationThread.h>
33
34 namespace JSC {
35
36 LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithoutDisassembly()
37 {
38     performFinalization();
39     
40     return CodeRef(m_executableMemory);
41 }
42
43 LinkBuffer::CodeRef LinkBuffer::finalizeCodeWithDisassembly(const char* format, ...)
44 {
45     ASSERT(Options::showDisassembly() || Options::showDFGDisassembly());
46     
47     CodeRef result = finalizeCodeWithoutDisassembly();
48     
49     dataLogF("Generated JIT code for ");
50     va_list argList;
51     va_start(argList, format);
52     WTF::dataLogFV(format, argList);
53     va_end(argList);
54     dataLogF(":\n");
55     
56     dataLogF("    Code at [%p, %p):\n", result.code().executableAddress(), static_cast<char*>(result.code().executableAddress()) + result.size());
57     disassemble(result.code(), m_size, "    ", WTF::dataFile());
58     
59     return result;
60 }
61
62 #if ENABLE(BRANCH_COMPACTION)
63 template <typename InstructionType>
64 void LinkBuffer::copyCompactAndLinkCode(void* ownerUID, JITCompilationEffort effort)
65 {
66     m_initialSize = m_assembler->m_assembler.codeSize();
67     m_executableMemory = m_vm->executableAllocator.allocate(*m_vm, m_initialSize, ownerUID, effort);
68     if (!m_executableMemory)
69         return;
70     m_code = (uint8_t*)m_executableMemory->start();
71     ASSERT(m_code);
72     ExecutableAllocator::makeWritable(m_code, m_initialSize);
73     uint8_t* inData = (uint8_t*)m_assembler->unlinkedCode();
74     uint8_t* outData = reinterpret_cast<uint8_t*>(m_code);
75     int readPtr = 0;
76     int writePtr = 0;
77     Vector<LinkRecord, 0, UnsafeVectorOverflow>& jumpsToLink = m_assembler->jumpsToLink();
78     unsigned jumpCount = jumpsToLink.size();
79     for (unsigned i = 0; i < jumpCount; ++i) {
80         int offset = readPtr - writePtr;
81         ASSERT(!(offset & 1));
82             
83         // Copy the instructions from the last jump to the current one.
84         size_t regionSize = jumpsToLink[i].from() - readPtr;
85         InstructionType* copySource = reinterpret_cast_ptr<InstructionType*>(inData + readPtr);
86         InstructionType* copyEnd = reinterpret_cast_ptr<InstructionType*>(inData + readPtr + regionSize);
87         InstructionType* copyDst = reinterpret_cast_ptr<InstructionType*>(outData + writePtr);
88         ASSERT(!(regionSize % 2));
89         ASSERT(!(readPtr % 2));
90         ASSERT(!(writePtr % 2));
91         while (copySource != copyEnd)
92             *copyDst++ = *copySource++;
93         m_assembler->recordLinkOffsets(readPtr, jumpsToLink[i].from(), offset);
94         readPtr += regionSize;
95         writePtr += regionSize;
96             
97         // Calculate absolute address of the jump target, in the case of backwards
98         // branches we need to be precise, forward branches we are pessimistic
99         const uint8_t* target;
100         if (jumpsToLink[i].to() >= jumpsToLink[i].from())
101             target = outData + jumpsToLink[i].to() - offset; // Compensate for what we have collapsed so far
102         else
103             target = outData + jumpsToLink[i].to() - m_assembler->executableOffsetFor(jumpsToLink[i].to());
104             
105         JumpLinkType jumpLinkType = m_assembler->computeJumpType(jumpsToLink[i], outData + writePtr, target);
106         // Compact branch if we can...
107         if (m_assembler->canCompact(jumpsToLink[i].type())) {
108             // Step back in the write stream
109             int32_t delta = m_assembler->jumpSizeDelta(jumpsToLink[i].type(), jumpLinkType);
110             if (delta) {
111                 writePtr -= delta;
112                 m_assembler->recordLinkOffsets(jumpsToLink[i].from() - delta, readPtr, readPtr - writePtr);
113             }
114         }
115         jumpsToLink[i].setFrom(writePtr);
116     }
117     // Copy everything after the last jump
118     memcpy(outData + writePtr, inData + readPtr, m_initialSize - readPtr);
119     m_assembler->recordLinkOffsets(readPtr, m_initialSize, readPtr - writePtr);
120         
121     for (unsigned i = 0; i < jumpCount; ++i) {
122         uint8_t* location = outData + jumpsToLink[i].from();
123         uint8_t* target = outData + jumpsToLink[i].to() - m_assembler->executableOffsetFor(jumpsToLink[i].to());
124         m_assembler->link(jumpsToLink[i], location, target);
125     }
126
127     jumpsToLink.clear();
128     m_size = writePtr + m_initialSize - readPtr;
129     m_executableMemory->shrink(m_size);
130
131 #if DUMP_LINK_STATISTICS
132     dumpLinkStatistics(m_code, m_initialSize, m_size);
133 #endif
134 #if DUMP_CODE
135     dumpCode(m_code, m_size);
136 #endif
137 }
138 #endif
139
140
141 void LinkBuffer::linkCode(void* ownerUID, JITCompilationEffort effort)
142 {
143     ASSERT(!m_code);
144 #if !ENABLE(BRANCH_COMPACTION)
145     m_executableMemory = m_assembler->m_assembler.executableCopy(*m_vm, ownerUID, effort);
146     if (!m_executableMemory)
147         return;
148     m_code = m_executableMemory->start();
149     m_size = m_assembler->m_assembler.codeSize();
150     ASSERT(m_code);
151 #elif CPU(ARM_THUMB2)
152     copyCompactAndLinkCode<uint16_t>(ownerUID, effort);
153 #elif CPU(ARM64)
154     copyCompactAndLinkCode<uint32_t>(ownerUID, effort);
155 #endif
156 }
157
158 void LinkBuffer::performFinalization()
159 {
160 #ifndef NDEBUG
161     ASSERT(!isCompilationThread());
162     ASSERT(!m_completed);
163     ASSERT(isValid());
164     m_completed = true;
165 #endif
166     
167 #if ENABLE(BRANCH_COMPACTION)
168     ExecutableAllocator::makeExecutable(code(), m_initialSize);
169 #else
170     ExecutableAllocator::makeExecutable(code(), m_size);
171 #endif
172     MacroAssembler::cacheFlush(code(), m_size);
173 }
174
175 #if DUMP_LINK_STATISTICS
176 void LinkBuffer::dumpLinkStatistics(void* code, size_t initializeSize, size_t finalSize)
177 {
178     static unsigned linkCount = 0;
179     static unsigned totalInitialSize = 0;
180     static unsigned totalFinalSize = 0;
181     linkCount++;
182     totalInitialSize += initialSize;
183     totalFinalSize += finalSize;
184     dataLogF("link %p: orig %u, compact %u (delta %u, %.2f%%)\n", 
185             code, static_cast<unsigned>(initialSize), static_cast<unsigned>(finalSize),
186             static_cast<unsigned>(initialSize - finalSize),
187             100.0 * (initialSize - finalSize) / initialSize);
188     dataLogF("\ttotal %u: orig %u, compact %u (delta %u, %.2f%%)\n", 
189             linkCount, totalInitialSize, totalFinalSize, totalInitialSize - totalFinalSize,
190             100.0 * (totalInitialSize - totalFinalSize) / totalInitialSize);
191 }
192 #endif
193
194 #if DUMP_CODE
195 void LinkBuffer::dumpCode(void* code, size_t size)
196 {
197 #if CPU(ARM_THUMB2)
198     // Dump the generated code in an asm file format that can be assembled and then disassembled
199     // for debugging purposes. For example, save this output as jit.s:
200     //   gcc -arch armv7 -c jit.s
201     //   otool -tv jit.o
202     static unsigned codeCount = 0;
203     unsigned short* tcode = static_cast<unsigned short*>(code);
204     size_t tsize = size / sizeof(short);
205     char nameBuf[128];
206     snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++);
207     dataLogF("\t.syntax unified\n"
208             "\t.section\t__TEXT,__text,regular,pure_instructions\n"
209             "\t.globl\t%s\n"
210             "\t.align 2\n"
211             "\t.code 16\n"
212             "\t.thumb_func\t%s\n"
213             "# %p\n"
214             "%s:\n", nameBuf, nameBuf, code, nameBuf);
215         
216     for (unsigned i = 0; i < tsize; i++)
217         dataLogF("\t.short\t0x%x\n", tcode[i]);
218 #elif CPU(ARM_TRADITIONAL)
219     //   gcc -c jit.s
220     //   objdump -D jit.o
221     static unsigned codeCount = 0;
222     unsigned int* tcode = static_cast<unsigned int*>(code);
223     size_t tsize = size / sizeof(unsigned int);
224     char nameBuf[128];
225     snprintf(nameBuf, sizeof(nameBuf), "_jsc_jit%u", codeCount++);
226     dataLogF("\t.globl\t%s\n"
227             "\t.align 4\n"
228             "\t.code 32\n"
229             "\t.text\n"
230             "# %p\n"
231             "%s:\n", nameBuf, code, nameBuf);
232
233     for (unsigned i = 0; i < tsize; i++)
234         dataLogF("\t.long\t0x%x\n", tcode[i]);
235 #endif
236 }
237 #endif
238
239 } // namespace JSC
240
241 #endif // ENABLE(ASSEMBLER)
242
243