1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=79:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Copyright (C) 2008 Apple Inc. All rights reserved.
6 : *
7 : * Redistribution and use in source and binary forms, with or without
8 : * modification, are permitted provided that the following conditions
9 : * are met:
10 : * 1. Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * 2. Redistributions in binary form must reproduce the above copyright
13 : * notice, this list of conditions and the following disclaimer in the
14 : * documentation and/or other materials provided with the distribution.
15 : *
16 : * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
17 : * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 : * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 : * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
20 : * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 : * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 : * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 : * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 : * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 : *
28 : * ***** END LICENSE BLOCK ***** */
29 :
30 : #ifndef AbstractMacroAssembler_h
31 : #define AbstractMacroAssembler_h
32 :
33 : #include "assembler/wtf/Platform.h"
34 : #include "assembler/assembler/MacroAssemblerCodeRef.h"
35 : #include "assembler/assembler/CodeLocation.h"
36 :
37 : #if ENABLE_ASSEMBLER
38 :
39 : namespace JSC {
40 :
41 : class LinkBuffer;
42 : class RepatchBuffer;
43 :
44 : template <class AssemblerType>
45 1526632 : class AbstractMacroAssembler {
46 : public:
47 : typedef AssemblerType AssemblerType_T;
48 :
49 : typedef MacroAssemblerCodePtr CodePtr;
50 : typedef MacroAssemblerCodeRef CodeRef;
51 :
52 : class Jump;
53 :
54 : typedef typename AssemblerType::RegisterID RegisterID;
55 : typedef typename AssemblerType::FPRegisterID FPRegisterID;
56 : typedef typename AssemblerType::JmpSrc JmpSrc;
57 : typedef typename AssemblerType::JmpDst JmpDst;
58 :
59 : #ifdef DEBUG
60 134091 : void setSpewPath(bool isOOLPath)
61 : {
62 134091 : m_assembler.isOOLPath = isOOLPath;
63 134091 : }
64 : #endif
65 :
66 : // Section 1: MacroAssembler operand types
67 : //
68 : // The following types are used as operands to MacroAssembler operations,
69 : // describing immediate and memory operands to the instructions to be planted.
70 :
71 :
72 : enum Scale {
73 : TimesOne,
74 : TimesTwo,
75 : TimesFour,
76 : TimesEight
77 : };
78 :
79 : // Address:
80 : //
81 : // Describes a simple base-offset address.
82 : struct Address {
83 181223 : explicit Address() {}
84 :
85 90765935 : explicit Address(RegisterID base, int32_t offset = 0)
86 : : base(base)
87 90765935 : , offset(offset)
88 : {
89 90765935 : }
90 :
91 : RegisterID base;
92 : int32_t offset;
93 : };
94 :
95 : struct ExtendedAddress {
96 3435 : explicit ExtendedAddress(RegisterID base, intptr_t offset = 0)
97 : : base(base)
98 3435 : , offset(offset)
99 : {
100 3435 : }
101 :
102 : RegisterID base;
103 : intptr_t offset;
104 : };
105 :
106 : // ImplicitAddress:
107 : //
108 : // This class is used for explicit 'load' and 'store' operations
109 : // (as opposed to situations in which a memory operand is provided
110 : // to a generic operation, such as an integer arithmetic instruction).
111 : //
112 : // In the case of a load (or store) operation we want to permit
113 : // addresses to be implicitly constructed, e.g. the two calls:
114 : //
115 : // load32(Address(addrReg), destReg);
116 : // load32(addrReg, destReg);
117 : //
118 : // Are equivalent, and the explicit wrapping of the Address in the former
119 : // is unnecessary.
120 : struct ImplicitAddress {
121 20325 : ImplicitAddress(RegisterID base)
122 : : base(base)
123 20325 : , offset(0)
124 : {
125 20325 : }
126 :
127 60685991 : ImplicitAddress(Address address)
128 : : base(address.base)
129 60685991 : , offset(address.offset)
130 : {
131 60685991 : }
132 :
133 : RegisterID base;
134 : int32_t offset;
135 : };
136 :
137 : // BaseIndex:
138 : //
139 : // Describes a complex addressing mode.
140 : struct BaseIndex {
141 315799 : BaseIndex(RegisterID base, RegisterID index, Scale scale, int32_t offset = 0)
142 : : base(base)
143 : , index(index)
144 : , scale(scale)
145 315799 : , offset(offset)
146 : {
147 315799 : }
148 :
149 : RegisterID base;
150 : RegisterID index;
151 : Scale scale;
152 : int32_t offset;
153 : };
154 :
155 : // AbsoluteAddress:
156 : //
157 : // Describes an memory operand given by a pointer. For regular load & store
158 : // operations an unwrapped void* will be used, rather than using this.
159 : struct AbsoluteAddress {
160 389199 : explicit AbsoluteAddress(void* ptr)
161 389199 : : m_ptr(ptr)
162 : {
163 389199 : }
164 :
165 : void* m_ptr;
166 : };
167 :
168 : // TrustedImmPtr:
169 : //
170 : // A pointer sized immediate operand to an instruction - this is wrapped
171 : // in a class requiring explicit construction in order to differentiate
172 : // from pointers used as absolute addresses to memory operations
173 : struct TrustedImmPtr {
174 12068984 : explicit TrustedImmPtr(const void* value)
175 12068984 : : m_value(value)
176 : {
177 12068984 : }
178 :
179 12068984 : intptr_t asIntptr()
180 : {
181 12068984 : return reinterpret_cast<intptr_t>(m_value);
182 : }
183 :
184 : const void* m_value;
185 : };
186 :
187 : struct ImmPtr : public TrustedImmPtr {
188 12062415 : explicit ImmPtr(const void* value)
189 12062415 : : TrustedImmPtr(value)
190 : {
191 12062415 : }
192 : };
193 :
194 : // TrustedImm32:
195 : //
196 : // A 32bit immediate operand to an instruction - this is wrapped in a
197 : // class requiring explicit construction in order to prevent RegisterIDs
198 : // (which are implemented as an enum) from accidentally being passed as
199 : // immediate values.
200 : struct TrustedImm32 {
201 24930774 : explicit TrustedImm32(int32_t value)
202 24930774 : : m_value(value)
203 : #if WTF_CPU_ARM || WTF_CPU_MIPS
204 : , m_isPointer(false)
205 : #endif
206 : {
207 24930774 : }
208 :
209 : #if !WTF_CPU_X86_64
210 6130009 : explicit TrustedImm32(TrustedImmPtr ptr)
211 6130009 : : m_value(ptr.asIntptr())
212 : #if WTF_CPU_ARM || WTF_CPU_MIPS
213 : , m_isPointer(true)
214 : #endif
215 : {
216 6130009 : }
217 : #endif
218 :
219 : int32_t m_value;
220 : #if WTF_CPU_ARM || WTF_CPU_MIPS
221 : // We rely on being able to regenerate code to recover exception handling
222 : // information. Since ARMv7 supports 16-bit immediates there is a danger
223 : // that if pointer values change the layout of the generated code will change.
224 : // To avoid this problem, always generate pointers (and thus Imm32s constructed
225 : // from ImmPtrs) with a code sequence that is able to represent any pointer
226 : // value - don't use a more compact form in these cases.
227 : // Same for MIPS.
228 : bool m_isPointer;
229 : #endif
230 : };
231 :
232 :
233 : struct Imm32 : public TrustedImm32 {
234 24641319 : explicit Imm32(int32_t value)
235 24641319 : : TrustedImm32(value)
236 : {
237 24641319 : }
238 : #if !WTF_CPU_X86_64
239 6130009 : explicit Imm32(TrustedImmPtr ptr)
240 6130009 : : TrustedImm32(ptr)
241 : {
242 6130009 : }
243 : #endif
244 : };
245 :
246 : struct ImmDouble {
247 : union {
248 : struct {
249 : #if WTF_CPU_BIG_ENDIAN || WTF_CPU_MIDDLE_ENDIAN
250 : uint32_t msb, lsb;
251 : #else
252 : uint32_t lsb, msb;
253 : #endif
254 : } s;
255 : uint64_t u64;
256 : double d;
257 : } u;
258 :
259 116 : explicit ImmDouble(double d) {
260 116 : u.d = d;
261 116 : }
262 : };
263 :
264 : // Section 2: MacroAssembler code buffer handles
265 : //
266 : // The following types are used to reference items in the code buffer
267 : // during JIT code generation. For example, the type Jump is used to
268 : // track the location of a jump instruction so that it may later be
269 : // linked to a label marking its destination.
270 :
271 :
272 : // Label:
273 : //
274 : // A Label records a point in the generated instruction stream, typically such that
275 : // it may be used as a destination for a jump.
276 : class Label {
277 : template<class TemplateAssemblerType>
278 : friend class AbstractMacroAssembler;
279 : friend class Jump;
280 : friend class MacroAssemblerCodeRef;
281 : friend class LinkBuffer;
282 :
283 : public:
284 47363367 : Label()
285 47363367 : {
286 47363367 : }
287 :
288 46830175 : Label(AbstractMacroAssembler<AssemblerType>* masm)
289 46830175 : : m_label(masm->m_assembler.label())
290 : {
291 46830175 : }
292 :
293 : bool isUsed() const { return m_label.isUsed(); }
294 : void used() { m_label.used(); }
295 716980 : bool isSet() const { return m_label.isValid(); }
296 : private:
297 : JmpDst m_label;
298 : };
299 :
300 : // DataLabelPtr:
301 : //
302 : // A DataLabelPtr is used to refer to a location in the code containing a pointer to be
303 : // patched after the code has been generated.
304 6569 : class DataLabelPtr {
305 : template<class TemplateAssemblerType>
306 : friend class AbstractMacroAssembler;
307 : friend class LinkBuffer;
308 : public:
309 19463734 : DataLabelPtr()
310 19463734 : {
311 19463734 : }
312 :
313 4756260 : DataLabelPtr(AbstractMacroAssembler<AssemblerType>* masm)
314 4756260 : : m_label(masm->m_assembler.label())
315 : {
316 4756260 : }
317 :
318 : bool isSet() const { return m_label.isValid(); }
319 :
320 : private:
321 : JmpDst m_label;
322 : };
323 :
324 : // DataLabel32:
325 : //
326 : // A DataLabel32 is used to refer to a location in the code containing a
327 : // 32-bit constant to be patched after the code has been generated.
328 : class DataLabel32 {
329 : template<class TemplateAssemblerType>
330 : friend class AbstractMacroAssembler;
331 : friend class LinkBuffer;
332 : public:
333 30763 : DataLabel32()
334 30763 : {
335 30763 : }
336 :
337 63841 : DataLabel32(AbstractMacroAssembler<AssemblerType>* masm)
338 63841 : : m_label(masm->m_assembler.label())
339 : {
340 63841 : }
341 :
342 : private:
343 : JmpDst m_label;
344 : };
345 :
346 : // Call:
347 : //
348 : // A Call object is a reference to a call instruction that has been planted
349 : // into the code buffer - it is typically used to link the call, setting the
350 : // relative offset such that when executed it will call to the desired
351 : // destination.
352 : class Call {
353 : template<class TemplateAssemblerType>
354 : friend class AbstractMacroAssembler;
355 :
356 : public:
357 : enum Flags {
358 : None = 0x0,
359 : Linkable = 0x1,
360 : Near = 0x2,
361 : LinkableNear = 0x3
362 : };
363 :
364 1419934 : Call()
365 1419934 : : m_flags(None)
366 : {
367 1419934 : }
368 :
369 4905615 : Call(JmpSrc jmp, Flags flags)
370 : : m_jmp(jmp)
371 4905615 : , m_flags(flags)
372 : {
373 4905615 : }
374 :
375 7744413 : bool isFlagSet(Flags flag)
376 : {
377 7744413 : return !!(m_flags & flag);
378 : }
379 :
380 : static Call fromTailJump(Jump jump)
381 : {
382 : return Call(jump.m_jmp, Linkable);
383 : }
384 :
385 : JmpSrc m_jmp;
386 : private:
387 : Flags m_flags;
388 : };
389 :
390 : // Jump:
391 : //
392 : // A jump object is a reference to a jump instruction that has been planted
393 : // into the code buffer - it is typically used to link the jump, setting the
394 : // relative offset such that when executed it will jump to the desired
395 : // destination.
396 2951656 : class Jump {
397 : template<class TemplateAssemblerType>
398 : friend class AbstractMacroAssembler;
399 : friend class Call;
400 : friend class LinkBuffer;
401 : public:
402 10975224 : Jump()
403 10975224 : {
404 10975224 : }
405 :
406 17707037 : Jump(JmpSrc jmp)
407 17707037 : : m_jmp(jmp)
408 : {
409 17707037 : }
410 :
411 557155 : void link(AbstractMacroAssembler<AssemblerType>* masm)
412 : {
413 557155 : masm->m_assembler.linkJump(m_jmp, masm->m_assembler.label());
414 557155 : }
415 :
416 7045481 : void linkTo(Label label, AbstractMacroAssembler<AssemblerType>* masm)
417 : {
418 7045481 : masm->m_assembler.linkJump(m_jmp, label.m_label);
419 7045481 : }
420 :
421 : private:
422 : JmpSrc m_jmp;
423 : };
424 :
425 : // JumpList:
426 : //
427 : // A JumpList is a set of Jump objects.
428 : // All jumps in the set will be linked to the same destination.
429 3007903 : class JumpList {
430 : friend class LinkBuffer;
431 :
432 : public:
433 : typedef js::Vector<Jump, 16 ,js::SystemAllocPolicy > JumpVector;
434 :
435 883811 : JumpList() {}
436 :
437 2124092 : JumpList(const JumpList &other)
438 2124092 : {
439 2124092 : m_jumps.append(other.m_jumps);
440 2124092 : }
441 :
442 : JumpList &operator=(const JumpList &other)
443 : {
444 : m_jumps.clear();
445 : m_jumps.append(other.m_jumps);
446 : return *this;
447 : }
448 :
449 348156 : void link(AbstractMacroAssembler<AssemblerType>* masm)
450 : {
451 348156 : size_t size = m_jumps.length();
452 837411 : for (size_t i = 0; i < size; ++i)
453 489255 : m_jumps[i].link(masm);
454 348156 : m_jumps.clear();
455 348156 : }
456 :
457 47508 : void linkTo(Label label, AbstractMacroAssembler<AssemblerType>* masm)
458 : {
459 47508 : size_t size = m_jumps.length();
460 135302 : for (size_t i = 0; i < size; ++i)
461 87794 : m_jumps[i].linkTo(label, masm);
462 47508 : m_jumps.clear();
463 47508 : }
464 :
465 577049 : void append(Jump jump)
466 : {
467 577049 : m_jumps.append(jump);
468 577049 : }
469 :
470 408113 : void append(const JumpList& other)
471 : {
472 408113 : m_jumps.append(other.m_jumps.begin(), other.m_jumps.length());
473 408113 : }
474 :
475 202547 : void clear()
476 : {
477 202547 : m_jumps.clear();
478 202547 : }
479 :
480 113408 : bool empty()
481 : {
482 113408 : return !m_jumps.length();
483 : }
484 :
485 : const JumpVector& jumps() const { return m_jumps; }
486 :
487 : private:
488 : JumpVector m_jumps;
489 : };
490 :
491 :
492 : // Section 3: Misc admin methods
493 :
494 : static CodePtr trampolineAt(CodeRef ref, Label label)
495 : {
496 : return CodePtr(AssemblerType::getRelocatedAddress(ref.m_code.dataLocation(), label.m_label));
497 : }
498 :
499 7547327 : size_t size()
500 : {
501 7547327 : return m_assembler.size();
502 : }
503 :
504 : unsigned char *buffer()
505 : {
506 : return m_assembler.buffer();
507 : }
508 :
509 2260344 : bool oom()
510 : {
511 2260344 : return m_assembler.oom();
512 : }
513 :
514 257654 : void executableCopy(void* buffer)
515 : {
516 257654 : ASSERT(!oom());
517 257654 : m_assembler.executableCopy(buffer);
518 257654 : }
519 :
520 46255840 : Label label()
521 : {
522 46255840 : return Label(this);
523 : }
524 :
525 63841 : DataLabel32 dataLabel32()
526 : {
527 63841 : return DataLabel32(this);
528 : }
529 :
530 : Label align()
531 : {
532 : m_assembler.align(16);
533 : return Label(this);
534 : }
535 :
536 527351 : ptrdiff_t differenceBetween(Label from, Jump to)
537 : {
538 527351 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_jmp);
539 : }
540 :
541 : ptrdiff_t differenceBetween(Label from, Call to)
542 : {
543 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_jmp);
544 : }
545 :
546 8288961 : ptrdiff_t differenceBetween(Label from, Label to)
547 : {
548 8288961 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
549 : }
550 :
551 476707 : ptrdiff_t differenceBetween(Label from, DataLabelPtr to)
552 : {
553 476707 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
554 : }
555 :
556 33078 : ptrdiff_t differenceBetween(Label from, DataLabel32 to)
557 : {
558 33078 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
559 : }
560 :
561 63841 : ptrdiff_t differenceBetween(DataLabel32 from, Label to)
562 : {
563 63841 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
564 : }
565 :
566 : ptrdiff_t differenceBetween(DataLabelPtr from, Label to)
567 : {
568 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
569 : }
570 :
571 : ptrdiff_t differenceBetween(DataLabelPtr from, Jump to)
572 : {
573 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_jmp);
574 : }
575 :
576 : ptrdiff_t differenceBetween(DataLabelPtr from, DataLabelPtr to)
577 : {
578 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_label);
579 : }
580 :
581 : ptrdiff_t differenceBetween(DataLabelPtr from, Call to)
582 : {
583 : return AssemblerType::getDifferenceBetweenLabels(from.m_label, to.m_jmp);
584 : }
585 :
586 : protected:
587 : AssemblerType m_assembler;
588 :
589 : friend class LinkBuffer;
590 : friend class RepatchBuffer;
591 :
592 9954223 : static void linkJump(void* code, Jump jump, CodeLocationLabel target)
593 : {
594 9954223 : AssemblerType::linkJump(code, jump.m_jmp, target.dataLocation());
595 9954223 : }
596 :
597 2325033 : static void linkPointer(void* code, typename AssemblerType::JmpDst label, void* value)
598 : {
599 2325033 : AssemblerType::linkPointer(code, label, value);
600 2325033 : }
601 :
602 2037320 : static void* getLinkerAddress(void* code, typename AssemblerType::JmpSrc label)
603 : {
604 2037320 : return AssemblerType::getRelocatedAddress(code, label);
605 : }
606 :
607 15988181 : static void* getLinkerAddress(void* code, typename AssemblerType::JmpDst label)
608 : {
609 15988181 : return AssemblerType::getRelocatedAddress(code, label);
610 : }
611 :
612 4762722 : static unsigned getLinkerCallReturnOffset(Call call)
613 : {
614 4762722 : return AssemblerType::getCallReturnOffset(call.m_jmp);
615 : }
616 :
617 347394 : static void repatchJump(CodeLocationJump jump, CodeLocationLabel destination)
618 : {
619 347394 : AssemblerType::relinkJump(jump.dataLocation(), destination.dataLocation());
620 347394 : }
621 :
622 13004 : static bool canRepatchJump(CodeLocationJump jump, CodeLocationLabel destination)
623 : {
624 13004 : return AssemblerType::canRelinkJump(jump.dataLocation(), destination.dataLocation());
625 : }
626 :
627 : static void repatchNearCall(CodeLocationNearCall nearCall, CodeLocationLabel destination)
628 : {
629 : AssemblerType::relinkCall(nearCall.dataLocation(), destination.executableAddress());
630 : }
631 :
632 672202 : static void repatchInt32(CodeLocationDataLabel32 dataLabel32, int32_t value)
633 : {
634 672202 : AssemblerType::repatchInt32(dataLabel32.dataLocation(), value);
635 672202 : }
636 :
637 355607 : static void repatchPointer(CodeLocationDataLabelPtr dataLabelPtr, void* value)
638 : {
639 355607 : AssemblerType::repatchPointer(dataLabelPtr.dataLocation(), value);
640 355607 : }
641 :
642 101792 : static void repatchLoadPtrToLEA(CodeLocationInstruction instruction)
643 : {
644 101792 : AssemblerType::repatchLoadPtrToLEA(instruction.dataLocation());
645 101792 : }
646 :
647 : static void repatchLEAToLoadPtr(CodeLocationInstruction instruction)
648 : {
649 : AssemblerType::repatchLEAToLoadPtr(instruction.dataLocation());
650 : }
651 : };
652 :
653 : } // namespace JSC
654 :
655 : #endif // ENABLE(ASSEMBLER)
656 :
657 : #endif // AbstractMacroAssembler_h
|