1 /// -*- mode: asm; asm-comment-char: ?/ -*-
3 /// Common definitions for asesembler source files
5 /// (c) 2015 Straylight/Edgeware
8 ///----- Licensing notice ---------------------------------------------------
10 /// This file is part of Catacomb.
12 /// Catacomb is free software; you can redistribute it and/or modify
13 /// it under the terms of the GNU Library General Public License as
14 /// published by the Free Software Foundation; either version 2 of the
15 /// License, or (at your option) any later version.
17 /// Catacomb is distributed in the hope that it will be useful,
18 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
19 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 /// GNU Library General Public License for more details.
22 /// You should have received a copy of the GNU Library General Public
23 /// License along with Catacomb; if not, write to the Free
24 /// Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25 /// MA 02111-1307, USA.
27 #ifndef CATACOMB_ASM_COMMON_H
28 #define CATACOMB_ASM_COMMON_H
30 ///--------------------------------------------------------------------------
31 /// General definitions.
33 // Preprocessor hacks.
34 #define STRINGY(x) _STRINGY(x, y)
35 #define _STRINGY(x) #x
36 #define GLUE(x, y) _GLUE(x, y)
37 #define _GLUE(x, y) x##y
40 // Some useful variables.
43 // Literal pools done the hard way.
44 #define _LIT .text .L$_subsec + 1
45 #define _ENDLIT .text .L$_subsec
46 #define _LTORG .L$_subsec = .L$_subsec + 2; .text .L$_subsec
51 # define _SECTTY(ty) %ty
53 # define _SECTTY(ty) @ty
58 #define TEXT .text .L$_subsec
60 # define RODATA .section .rdata, "dr"
62 # define RODATA .section .rodata, "a", _SECTTY(progbits)
68 // Announcing an internal function.
69 #define INTFUNC(name) \
71 .macro ENDFUNC; _ENDFUNC(name); .endm; \
72 .L$_prologue_p = 0; .L$_frameptr_p = 0; \
77 // Announcing an external function.
82 // Marking the end of a function.
83 #define _ENDFUNC(name) \
84 .if ~ .L$_prologue_p; .error "Missing `endprologue'"; .endif; \
85 .if .L$_frameptr_p; .purgem dropfp; .endif; \
91 // Make a helper function, if necessary.
93 .ifndef .L$_auxfn_def.name; \
95 .macro _ENDAUXFN; _ENDAUXFN_TAIL(name); .endm; \
98 #define _ENDAUXFN_TAIL(name) \
101 .L$_auxfn_def.name = 1
102 #define ENDAUXFN _ENDAUXFN; .endif
104 ///--------------------------------------------------------------------------
105 /// ELF-specific hacking.
109 #if __PIC__ || __PIE__
113 #define TYPE_FUNC(name) .type name, STT_FUNC
115 #define SIZE_OBJ(name) .size name, . - name
119 ///--------------------------------------------------------------------------
120 /// Windows-specific hacking.
125 # define F(name) _##name
130 ///--------------------------------------------------------------------------
131 /// x86- and amd64-specific hacking.
133 /// It's (slightly) easier to deal with both of these in one go.
135 #if CPUFAM_X86 || CPUFAM_AMD64
145 // Set the function hooks.
146 #define FUNC_PREHOOK(_) .balign 16
148 // On Windows, arrange to install stack-unwinding data.
149 #if CPUFAM_AMD64 && ABI_WIN
150 # define FUNC_POSTHOOK(name) .seh_proc name
151 # define ENDFUNC_HOOK(_) .seh_endproc
152 // Procedures are expected to invoke `.seh_setframe' if necessary, and
153 // `.seh_pushreg' and friends, and `.seh_endprologue'.
157 # define FUNC_POSTHOOK(_) .cfi_startproc
158 # define ENDFUNC_HOOK(_) .cfi_endproc
161 // Don't use the wretched AT&T syntax. It's festooned with pointless
162 // punctuation, and all of the data movement is backwards. Ugh!
163 .intel_syntax noprefix
165 // Call external subroutine at ADDR, possibly via PLT.
174 // Do I need to arrange a spare GOT register?
175 #if WANT_PIC && CPUFAM_X86
178 #define GOTREG ebx // Not needed in AMD64 so don't care.
180 // Maybe load GOT address into GOT.
181 .macro ldgot got=GOTREG
182 #if WANT_PIC && CPUFAM_X86
188 add \got, offset _GLOBAL_OFFSET_TABLE_
192 // Load address of external symbol ADDR into REG, maybe using GOT.
193 .macro leaext reg, addr, got=GOTREG
196 mov \reg, [\got + \addr@GOT]
199 mov \reg, \addr@GOTPCREL[rip]
203 mov \reg, offset \addr
211 // Address expression (possibly using a base register, and a displacement)
212 // referring to ADDR, which is within our module, maybe using GOT.
213 #define INTADDR(...) INTADDR__0(__VA_ARGS__, GOTREG, dummy)
214 #define INTADDR__0(addr, got, ...) INTADDR__1(addr, got)
216 # define INTADDR__1(addr, got) addr + rip
218 # define INTADDR__1(addr, got) got + addr@GOTOFF
220 # define INTADDR__1(addr, got) addr
223 // Permutations for SIMD instructions. SHUF(A, B, C, D) is an immediate,
224 // suitable for use in `pshufd' or `shufpd', which copies element A
225 // (0 <= A < 4) of the source to element 0 of the destination, element B to
226 // element 1, element C to element 2, and element D to element 3.
227 #define SHUF(a, b, c, d) ((a) + 4*(b) + 16*(c) + 64*(d))
229 // Map register names to their individual pieces.
231 // Apply decoration decor to (internal) register name reg of type ty.
233 // See `R_...' for internal register names. Decorations are as follows.
235 // b low byte (e.g., `al', `r8b')
236 // h high byte (e.g., `ah')
237 // w word (e.g., `ax', `r8w')
238 // d doubleword (e.g., `eax', `r8d')
239 // q quadword (e.g., `rax', `r8')
240 // r whole register (doubleword on x86, quadword on amd64)
242 // And types are as follows.
244 // abcd the four traditional registers `a', `b', `c', `d'
245 // xp the four pointer registers `si', `di', `bp', `sp'
246 // ip the instruction pointer `ip'
247 // rn the AMD64 numbered registers `r8'--`r15'
248 #define _DECOR(ty, decor, reg) _DECOR_##ty##_##decor(reg)
250 // Internal macros: _DECOR_ty_decor(reg) applies decoration decor to
251 // (internal) register name reg of type ty.
253 #define _DECOR_abcd_b(reg) reg##l
254 #define _DECOR_abcd_h(reg) reg##h
255 #define _DECOR_abcd_w(reg) reg##x
256 #define _DECOR_abcd_d(reg) e##reg##x
258 # define _DECOR_abcd_q(reg) r##reg##x
261 #define _DECOR_xp_w(reg) reg
262 #define _DECOR_xp_d(reg) e##reg
264 # define _DECOR_xp_b(reg) reg##l
265 # define _DECOR_xp_q(reg) r##reg
268 #define _DECOR_ip_w(reg) reg
269 #define _DECOR_ip_d(reg) e##reg
271 # define _DECOR_ip_q(reg) r##reg
275 # define _DECOR_rn_b(reg) reg##b
276 # define _DECOR_rn_w(reg) reg##w
277 # define _DECOR_rn_d(reg) reg##d
278 # define _DECOR_rn_q(reg) reg
279 # define _DECOR_rn_r(reg) reg
282 #define _DECOR_mem_b(addr) byte ptr addr
283 #define _DECOR_mem_w(addr) word ptr addr
284 #define _DECOR_mem_d(addr) dword ptr addr
286 # define _DECOR_mem_q(addr) qword ptr addr
289 #define _DECOR_imm_b(imm) byte imm
290 #define _DECOR_imm_w(imm) word imm
291 #define _DECOR_imm_d(imm) dword imm
293 # define _DECOR_imm_q(imm) qword imm
297 # define _DECOR_abcd_r(reg) e##reg##x
298 # define _DECOR_xp_r(reg) e##reg
299 # define _DECOR_ip_r(reg) e##reg
300 # define _DECOR_mem_r(addr) dword ptr addr
301 # define _DECOR_imm_r(imm) dword imm
304 # define _DECOR_abcd_r(reg) r##reg##x
305 # define _DECOR_xp_r(reg) r##reg
306 # define _DECOR_ip_r(reg) r##reg
307 # define _DECOR_mem_r(addr) qword ptr addr
308 # define _DECOR_imm_r(imm) qword imm
311 // R_r(decor) applies decoration decor to register r, which is an internal
312 // register name. The internal register names are: `ip', `a', `b', `c', `d',
313 // `si', `di', `bp', `sp', `r8'--`r15'.
314 #define R_nil(decor) nil
315 #define R_ip(decor) _DECOR(ip, decor, ip)
316 #define R_a(decor) _DECOR(abcd, decor, a)
317 #define R_b(decor) _DECOR(abcd, decor, b)
318 #define R_c(decor) _DECOR(abcd, decor, c)
319 #define R_d(decor) _DECOR(abcd, decor, d)
320 #define R_si(decor) _DECOR(xp, decor, si)
321 #define R_di(decor) _DECOR(xp, decor, di)
322 #define R_bp(decor) _DECOR(xp, decor, bp)
323 #define R_sp(decor) _DECOR(xp, decor, sp)
325 # define R_r8(decor) _DECOR(rn, decor, r8)
326 # define R_r9(decor) _DECOR(rn, decor, r9)
327 # define R_r10(decor) _DECOR(rn, decor, r10)
328 # define R_r11(decor) _DECOR(rn, decor, r11)
329 # define R_r12(decor) _DECOR(rn, decor, r12)
330 # define R_r13(decor) _DECOR(rn, decor, r13)
331 # define R_r14(decor) _DECOR(rn, decor, r14)
332 # define R_r15(decor) _DECOR(rn, decor, r15)
335 // Refer to an in-memory datum of the type implied by decor residing at
336 // address addr (which should supply its own square-brackets).
337 #define MEM(decor, addr) _DECOR(mem, decor, addr)
339 // Refer to an immediate datum of the type implied by decor.
340 #define IMM(decor, imm) _DECOR(mem, decor, imm)
342 // Applies decoration decor to assembler-level register name reg.
343 #define _REGFORM(reg, decor) _GLUE(_REGFORM_, reg)(decor)
345 // Internal macros: _REGFORM_r(decor) applies decoration decor to an
346 // assembler-level register name, in place of any decoration that register
349 #define _REGFORM_nil(decor) R_nil(decor)
351 #define _REGFORM_ip(decor) R_ip(decor)
352 #define _REGFORM_eip(decor) R_ip(decor)
354 #define _REGFORM_a(decor) R_a(decor)
355 #define _REGFORM_al(decor) R_a(decor)
356 #define _REGFORM_ah(decor) R_a(decor)
357 #define _REGFORM_ax(decor) R_a(decor)
358 #define _REGFORM_eax(decor) R_a(decor)
360 #define _REGFORM_b(decor) R_b(decor)
361 #define _REGFORM_bl(decor) R_b(decor)
362 #define _REGFORM_bh(decor) R_b(decor)
363 #define _REGFORM_bx(decor) R_b(decor)
364 #define _REGFORM_ebx(decor) R_b(decor)
366 #define _REGFORM_c(decor) R_c(decor)
367 #define _REGFORM_cl(decor) R_c(decor)
368 #define _REGFORM_ch(decor) R_c(decor)
369 #define _REGFORM_cx(decor) R_c(decor)
370 #define _REGFORM_ecx(decor) R_c(decor)
372 #define _REGFORM_d(decor) R_d(decor)
373 #define _REGFORM_dl(decor) R_d(decor)
374 #define _REGFORM_dh(decor) R_d(decor)
375 #define _REGFORM_dx(decor) R_d(decor)
376 #define _REGFORM_edx(decor) R_d(decor)
378 #define _REGFORM_si(decor) R_si(decor)
379 #define _REGFORM_sil(decor) R_si(decor)
380 #define _REGFORM_esi(decor) R_si(decor)
382 #define _REGFORM_di(decor) R_di(decor)
383 #define _REGFORM_dil(decor) R_di(decor)
384 #define _REGFORM_edi(decor) R_di(decor)
386 #define _REGFORM_bp(decor) R_bp(decor)
387 #define _REGFORM_bpl(decor) R_bp(decor)
388 #define _REGFORM_ebp(decor) R_bp(decor)
390 #define _REGFORM_sp(decor) R_sp(decor)
391 #define _REGFORM_spl(decor) R_sp(decor)
392 #define _REGFORM_esp(decor) R_sp(decor)
396 # define _REGFORM_rip(decor) R_ip(decor)
397 # define _REGFORM_rsp(decor) R_sp(decor)
398 # define _REGFORM_rbp(decor) R_bp(decor)
399 # define _REGFORM_rdi(decor) R_di(decor)
400 # define _REGFORM_rsi(decor) R_si(decor)
401 # define _REGFORM_rdx(decor) R_d(decor)
402 # define _REGFORM_rcx(decor) R_c(decor)
403 # define _REGFORM_rbx(decor) R_b(decor)
404 # define _REGFORM_rax(decor) R_a(decor)
406 # define _REGFORM_r8(decor) R_r8(decor)
407 # define _REGFORM_r8b(decor) R_r8(decor)
408 # define _REGFORM_r8w(decor) R_r8(decor)
409 # define _REGFORM_r8d(decor) R_r8(decor)
411 # define _REGFORM_r9(decor) R_r9(decor)
412 # define _REGFORM_r9b(decor) R_r9(decor)
413 # define _REGFORM_r9w(decor) R_r9(decor)
414 # define _REGFORM_r9d(decor) R_r9(decor)
416 # define _REGFORM_r10(decor) R_r10(decor)
417 # define _REGFORM_r10b(decor) R_r10(decor)
418 # define _REGFORM_r10w(decor) R_r10(decor)
419 # define _REGFORM_r10d(decor) R_r10(decor)
421 # define _REGFORM_r11(decor) R_r11(decor)
422 # define _REGFORM_r11b(decor) R_r11(decor)
423 # define _REGFORM_r11w(decor) R_r11(decor)
424 # define _REGFORM_r11d(decor) R_r11(decor)
426 # define _REGFORM_r12(decor) R_r12(decor)
427 # define _REGFORM_r12b(decor) R_r12(decor)
428 # define _REGFORM_r12w(decor) R_r12(decor)
429 # define _REGFORM_r12d(decor) R_r12(decor)
431 # define _REGFORM_r13(decor) R_r13(decor)
432 # define _REGFORM_r13b(decor) R_r13(decor)
433 # define _REGFORM_r13w(decor) R_r13(decor)
434 # define _REGFORM_r13d(decor) R_r13(decor)
436 # define _REGFORM_r14(decor) R_r14(decor)
437 # define _REGFORM_r14b(decor) R_r14(decor)
438 # define _REGFORM_r14w(decor) R_r14(decor)
439 # define _REGFORM_r14d(decor) R_r14(decor)
441 # define _REGFORM_r15(decor) R_r15(decor)
442 # define _REGFORM_r15b(decor) R_r15(decor)
443 # define _REGFORM_r15w(decor) R_r15(decor)
444 # define _REGFORM_r15d(decor) R_r15(decor)
448 // Macros for converting register names.
449 #define BYTE(reg) _REGFORM(reg, b)
450 #define HIBYTE(reg) _REGFORM(reg, h)
451 #define WORD(reg) _REGFORM(reg, w)
452 #define DWORD(reg) _REGFORM(reg, d)
454 # define QWORD(reg) _REGFORM(reg, q)
456 #define WHOLE(reg) _REGFORM(reg, r)
458 // Macros for some common registers.
468 // Stack management and unwinding.
469 .macro setfp fp=BP, offset=0
473 .cfi_def_cfa_register \fp
475 #if ABI_WIN && CPUFAM_AMD64
479 lea \fp, [SP + \offset]
481 .cfi_def_cfa_register \fp
482 .cfi_adjust_cfa_offset -\offset
484 #if ABI_WIN && CPUFAM_AMD64
485 .seh_setframe \fp, \offset
489 .macro dropfp; _dropfp \fp, \offset; .endm
492 .macro _dropfp fp, offset=0
496 .cfi_def_cfa_register SP
499 lea SP, [\fp - \offset]
501 .cfi_def_cfa_register SP
502 .cfi_adjust_cfa_offset +\offset
512 .cfi_adjust_cfa_offset +\n
514 #if ABI_WIN && CPUFAM_AMD64
522 .cfi_adjust_cfa_offset -\n
529 .cfi_adjust_cfa_offset +WORDSZ
530 .cfi_rel_offset \r, 0
532 #if ABI_WIN && CPUFAM_AMD64
540 .cfi_adjust_cfa_offset -WORDSZ
545 .macro savexmm r, offset
546 movdqa [SP + \offset], \r
547 #if ABI_WIN && CPUFAM_AMD64
548 .seh_savexmm \r, \offset
552 .macro rstrxmm r, offset
553 movdqa \r, [SP + \offset]
557 #if ABI_WIN && CPUFAM_AMD64
565 ///--------------------------------------------------------------------------
566 /// ARM-specific hacking.
570 // ARM/Thumb mode things. Use ARM by default.
571 #define ARM .arm; .L$_pcoff = 8
572 #define THUMB .thumb; .L$_pcoff = 4
575 // Set the function hooks.
576 #define FUNC_PREHOOK(_) .balign 4; .fnstart
577 #define ENDFUNC_HOOK(_) .fnend; .ltorg
579 // Call external subroutine at ADDR, possibly via PLT.
580 .macro callext addr, cond=
588 // Do I need to arrange a spare GOT register?
594 // Maybe load GOT address into GOT.
595 .macro ldgot cond=, got=GOTREG
597 ldr\cond \got, .L$_ldgot$\@
599 add\cond \got, pc, \got
603 .word _GLOBAL_OFFSET_TABLE_ - .L$_ldgot_pc$\@ - .L$_pcoff
608 // Load address of external symbol ADDR into REG, maybe using GOT.
609 .macro leaext reg, addr, cond=, got=GOTREG
611 ldr\cond \reg, .L$_leaext$\@
612 ldr\cond \reg, [\got, \reg]
619 ldr\cond \reg, =\addr
623 // Load address of external symbol ADDR into REG directly.
624 .macro leaextq reg, addr, cond=
626 ldr\cond \reg, .L$_leaextq$\@
629 ldr\cond \reg, [pc, \reg]
632 ldr\cond \reg, [\reg]
637 .word \addr(GOT_PREL) + (. - .L$_leaextq_pc$\@ - .L$_pcoff)
640 ldr\cond \reg, =\addr
645 // Set VZ (default q15) to zero.
649 .macro vshl128 vd, vn, nbit, vz=q15
650 // Set VD to VN shifted left by NBIT. Assume VZ (default q15) is
651 // all-bits-zero. NBIT must be a multiple of 8.
653 .error "shift quantity must be whole number of bytes"
655 vext.8 \vd, \vz, \vn, #16 - (\nbit >> 3)
658 .macro vshr128 vd, vn, nbit, vz=q15
659 // Set VD to VN shifted right by NBIT. Assume VZ (default q15) is
660 // all-bits-zero. NBIT must be a multiple of 8.
662 .error "shift quantity must be whole number of bytes"
664 vext.8 \vd, \vn, \vz, #\nbit >> 3
667 // Apply decoration decor to register name reg.
668 #define _REGFORM(reg, decor) _GLUE(_REGFORM_, reg)(decor)
670 // Internal macros: `_REGFORM_r(decor)' applies decoration decor to register
673 #define _REGFORM_nil(decor) nil
675 #define _REGFORM_s0(decor) _DECOR(s, decor, 0)
676 #define _REGFORM_s1(decor) _DECOR(s, decor, 1)
677 #define _REGFORM_s2(decor) _DECOR(s, decor, 2)
678 #define _REGFORM_s3(decor) _DECOR(s, decor, 3)
679 #define _REGFORM_s4(decor) _DECOR(s, decor, 4)
680 #define _REGFORM_s5(decor) _DECOR(s, decor, 5)
681 #define _REGFORM_s6(decor) _DECOR(s, decor, 6)
682 #define _REGFORM_s7(decor) _DECOR(s, decor, 7)
683 #define _REGFORM_s8(decor) _DECOR(s, decor, 8)
684 #define _REGFORM_s9(decor) _DECOR(s, decor, 9)
685 #define _REGFORM_s10(decor) _DECOR(s, decor, 10)
686 #define _REGFORM_s11(decor) _DECOR(s, decor, 11)
687 #define _REGFORM_s12(decor) _DECOR(s, decor, 12)
688 #define _REGFORM_s13(decor) _DECOR(s, decor, 13)
689 #define _REGFORM_s14(decor) _DECOR(s, decor, 14)
690 #define _REGFORM_s15(decor) _DECOR(s, decor, 15)
691 #define _REGFORM_s16(decor) _DECOR(s, decor, 16)
692 #define _REGFORM_s17(decor) _DECOR(s, decor, 17)
693 #define _REGFORM_s18(decor) _DECOR(s, decor, 18)
694 #define _REGFORM_s19(decor) _DECOR(s, decor, 19)
695 #define _REGFORM_s20(decor) _DECOR(s, decor, 20)
696 #define _REGFORM_s21(decor) _DECOR(s, decor, 21)
697 #define _REGFORM_s22(decor) _DECOR(s, decor, 22)
698 #define _REGFORM_s23(decor) _DECOR(s, decor, 23)
699 #define _REGFORM_s24(decor) _DECOR(s, decor, 24)
700 #define _REGFORM_s25(decor) _DECOR(s, decor, 25)
701 #define _REGFORM_s26(decor) _DECOR(s, decor, 26)
702 #define _REGFORM_s27(decor) _DECOR(s, decor, 27)
703 #define _REGFORM_s28(decor) _DECOR(s, decor, 28)
704 #define _REGFORM_s29(decor) _DECOR(s, decor, 29)
705 #define _REGFORM_s30(decor) _DECOR(s, decor, 30)
706 #define _REGFORM_s31(decor) _DECOR(s, decor, 31)
708 #define _REGFORM_d0(decor) _DECOR(d, decor, 0)
709 #define _REGFORM_d1(decor) _DECOR(d, decor, 1)
710 #define _REGFORM_d2(decor) _DECOR(d, decor, 2)
711 #define _REGFORM_d3(decor) _DECOR(d, decor, 3)
712 #define _REGFORM_d4(decor) _DECOR(d, decor, 4)
713 #define _REGFORM_d5(decor) _DECOR(d, decor, 5)
714 #define _REGFORM_d6(decor) _DECOR(d, decor, 6)
715 #define _REGFORM_d7(decor) _DECOR(d, decor, 7)
716 #define _REGFORM_d8(decor) _DECOR(d, decor, 8)
717 #define _REGFORM_d9(decor) _DECOR(d, decor, 9)
718 #define _REGFORM_d10(decor) _DECOR(d, decor, 10)
719 #define _REGFORM_d11(decor) _DECOR(d, decor, 11)
720 #define _REGFORM_d12(decor) _DECOR(d, decor, 12)
721 #define _REGFORM_d13(decor) _DECOR(d, decor, 13)
722 #define _REGFORM_d14(decor) _DECOR(d, decor, 14)
723 #define _REGFORM_d15(decor) _DECOR(d, decor, 15)
724 #define _REGFORM_d16(decor) _DECOR(d, decor, 16)
725 #define _REGFORM_d17(decor) _DECOR(d, decor, 17)
726 #define _REGFORM_d18(decor) _DECOR(d, decor, 18)
727 #define _REGFORM_d19(decor) _DECOR(d, decor, 19)
728 #define _REGFORM_d20(decor) _DECOR(d, decor, 20)
729 #define _REGFORM_d21(decor) _DECOR(d, decor, 21)
730 #define _REGFORM_d22(decor) _DECOR(d, decor, 22)
731 #define _REGFORM_d23(decor) _DECOR(d, decor, 23)
732 #define _REGFORM_d24(decor) _DECOR(d, decor, 24)
733 #define _REGFORM_d25(decor) _DECOR(d, decor, 25)
734 #define _REGFORM_d26(decor) _DECOR(d, decor, 26)
735 #define _REGFORM_d27(decor) _DECOR(d, decor, 27)
736 #define _REGFORM_d28(decor) _DECOR(d, decor, 28)
737 #define _REGFORM_d29(decor) _DECOR(d, decor, 29)
738 #define _REGFORM_d30(decor) _DECOR(d, decor, 30)
739 #define _REGFORM_d31(decor) _DECOR(d, decor, 31)
741 #define _REGFORM_q0(decor) _DECOR(q, decor, 0)
742 #define _REGFORM_q1(decor) _DECOR(q, decor, 1)
743 #define _REGFORM_q2(decor) _DECOR(q, decor, 2)
744 #define _REGFORM_q3(decor) _DECOR(q, decor, 3)
745 #define _REGFORM_q4(decor) _DECOR(q, decor, 4)
746 #define _REGFORM_q5(decor) _DECOR(q, decor, 5)
747 #define _REGFORM_q6(decor) _DECOR(q, decor, 6)
748 #define _REGFORM_q7(decor) _DECOR(q, decor, 7)
749 #define _REGFORM_q8(decor) _DECOR(q, decor, 8)
750 #define _REGFORM_q9(decor) _DECOR(q, decor, 9)
751 #define _REGFORM_q10(decor) _DECOR(q, decor, 10)
752 #define _REGFORM_q11(decor) _DECOR(q, decor, 11)
753 #define _REGFORM_q12(decor) _DECOR(q, decor, 12)
754 #define _REGFORM_q13(decor) _DECOR(q, decor, 13)
755 #define _REGFORM_q14(decor) _DECOR(q, decor, 14)
756 #define _REGFORM_q15(decor) _DECOR(q, decor, 15)
758 // `_LOPART(n)' and `_HIPART(n)' return the numbers of the register halves of
759 // register n, i.e., 2*n and 2*n + 1 respectively.
760 #define _LOPART(n) _GLUE(_LOPART_, n)
761 #define _HIPART(n) _GLUE(_HIPART_, n)
763 // Internal macros: `_LOPART_n' and `_HIPART_n' return the numbers of the
764 // register halves of register n, i.e., 2*n and 2*n + 1 respectively.
786 #define _LOPART_10 20
787 #define _HIPART_10 21
788 #define _LOPART_11 22
789 #define _HIPART_11 23
790 #define _LOPART_12 24
791 #define _HIPART_12 25
792 #define _LOPART_13 26
793 #define _HIPART_13 27
794 #define _LOPART_14 28
795 #define _HIPART_14 29
796 #define _LOPART_15 30
797 #define _HIPART_15 31
799 // Return the register number of the pair containing register n, i.e.,
801 #define _PAIR(n) _GLUE(_PAIR_, n)
803 // Internal macros: `_PAIR_n' returns the register number of the pair
804 // containing register n, i.e., floor(n/2).
838 // Apply decoration decor to register number n of type ty. Decorations are
841 // decor types meaning
842 // Q s, d the NEON qN register containing this one
843 // D s the NEON dN register containing this one
844 // D0 q the low 64-bit half of this one
845 // D1 q the high 64-bit half of this one
846 // S0 d, q the first 32-bit piece of this one
847 // S1 d, q the second 32-bit piece of this one
848 // S2 q the third 32-bit piece of this one
849 // S3 q the fourth 32-bit piece of this one
850 // Bn q the nth byte of this register, as a scalar
851 // Hn q the nth halfword of this register, as a scalar
852 // Wn q the nth word of this register, as a scalar
853 #define _DECOR(ty, decor, n) _DECOR_##ty##_##decor(n)
855 // Internal macros: `_DECOR_ty_decor(n)' applies decoration decor to register
856 // number n of type ty.
858 #define _DECOR_s_Q(n) GLUE(q, _PAIR(_PAIR(n)))
859 #define _DECOR_s_D(n) GLUE(d, _PAIR(n))
861 #define _DECOR_d_Q(n) GLUE(q, _PAIR(n))
862 #define _DECOR_d_S0(n) GLUE(s, _LOPART(n))
863 #define _DECOR_d_S1(n) GLUE(s, _LOPART(n))
865 #define _DECOR_q_D0(n) GLUE(d, _LOPART(n))
866 #define _DECOR_q_D1(n) GLUE(d, _HIPART(n))
867 #define _DECOR_q_S0(n) GLUE(s, _LOPART(_LOPART(n)))
868 #define _DECOR_q_S1(n) GLUE(s, _HIPART(_LOPART(n)))
869 #define _DECOR_q_S2(n) GLUE(s, _LOPART(_HIPART(n)))
870 #define _DECOR_q_S3(n) GLUE(s, _HIPART(_HIPART(n)))
871 #define _DECOR_q_W0(n) GLUE(d, _LOPART(n))[0]
872 #define _DECOR_q_W1(n) GLUE(d, _LOPART(n))[1]
873 #define _DECOR_q_W2(n) GLUE(d, _HIPART(n))[0]
874 #define _DECOR_q_W3(n) GLUE(d, _HIPART(n))[1]
875 #define _DECOR_q_H0(n) GLUE(d, _LOPART(n))[0]
876 #define _DECOR_q_H1(n) GLUE(d, _LOPART(n))[1]
877 #define _DECOR_q_H2(n) GLUE(d, _LOPART(n))[2]
878 #define _DECOR_q_H3(n) GLUE(d, _LOPART(n))[3]
879 #define _DECOR_q_H4(n) GLUE(d, _HIPART(n))[0]
880 #define _DECOR_q_H5(n) GLUE(d, _HIPART(n))[1]
881 #define _DECOR_q_H6(n) GLUE(d, _HIPART(n))[2]
882 #define _DECOR_q_H7(n) GLUE(d, _HIPART(n))[3]
883 #define _DECOR_q_B0(n) GLUE(d, _LOPART(n))[0]
884 #define _DECOR_q_B1(n) GLUE(d, _LOPART(n))[1]
885 #define _DECOR_q_B2(n) GLUE(d, _LOPART(n))[2]
886 #define _DECOR_q_B3(n) GLUE(d, _LOPART(n))[3]
887 #define _DECOR_q_B4(n) GLUE(d, _LOPART(n))[4]
888 #define _DECOR_q_B5(n) GLUE(d, _LOPART(n))[5]
889 #define _DECOR_q_B6(n) GLUE(d, _LOPART(n))[6]
890 #define _DECOR_q_B7(n) GLUE(d, _LOPART(n))[7]
891 #define _DECOR_q_B8(n) GLUE(d, _HIPART(n))[0]
892 #define _DECOR_q_B9(n) GLUE(d, _HIPART(n))[1]
893 #define _DECOR_q_B10(n) GLUE(d, _HIPART(n))[2]
894 #define _DECOR_q_B11(n) GLUE(d, _HIPART(n))[3]
895 #define _DECOR_q_B12(n) GLUE(d, _HIPART(n))[4]
896 #define _DECOR_q_B13(n) GLUE(d, _HIPART(n))[5]
897 #define _DECOR_q_B14(n) GLUE(d, _HIPART(n))[6]
898 #define _DECOR_q_B15(n) GLUE(d, _HIPART(n))[7]
900 // Macros for navigating the NEON register hierarchy.
901 #define S0(reg) _REGFORM(reg, S0)
902 #define S1(reg) _REGFORM(reg, S1)
903 #define S2(reg) _REGFORM(reg, S2)
904 #define S3(reg) _REGFORM(reg, S3)
905 #define D(reg) _REGFORM(reg, D)
906 #define D0(reg) _REGFORM(reg, D0)
907 #define D1(reg) _REGFORM(reg, D1)
908 #define Q(reg) _REGFORM(reg, Q)
910 // Macros for indexing quadword registers.
911 #define QB(reg, i) _REGFORM(reg, B##i)
912 #define QH(reg, i) _REGFORM(reg, H##i)
913 #define QW(reg, i) _REGFORM(reg, W##i)
915 // Macros for converting vldm/vstm ranges.
916 #define QQ(qlo, qhi) D0(qlo)-D1(qhi)
918 // Stack management and unwinding.
919 .macro setfp fp=r11, offset=0
924 add \fp, sp, #\offset
925 .setfp \fp, sp, #\offset
927 .macro dropfp; _dropfp \fp, \offset; .endm
931 .macro _dropfp fp, offset=0
935 sub sp, \fp, #\offset
951 .macro pushreg rr:vararg
956 .macro popreg rr:vararg
960 .macro pushvfp rr:vararg
965 .macro popvfp rr:vararg
972 // No need for prologue markers on ARM.
973 #define FUNC_POSTHOOK(_) .L$_prologue_p = -1
977 ///--------------------------------------------------------------------------
978 /// AArch64-specific hacking.
982 // Set the function hooks.
983 #define FUNC_PREHOOK(_) .balign 4
984 #define FUNC_POSTHOOK(_) .cfi_startproc; .L$_prologue_p = -1
985 #define ENDFUNC_HOOK(_) .cfi_endproc
987 // Call external subroutine at ADDR, possibly via PLT.
992 // Load address of external symbol ADDR into REG.
993 .macro leaext reg, addr
995 adrp \reg, :got:\addr
996 ldr \reg, [\reg, #:got_lo12:\addr]
999 add \reg, \reg, #:lo12:\addr
1004 // Set VZ (default v31) to zero.
1008 .macro vshl128 vd, vn, nbit, vz=v31
1009 // Set VD to VN shifted left by NBIT. Assume VZ (default v31) is
1010 // all-bits-zero. NBIT must be a multiple of 8.
1012 .error "shift quantity must be whole number of bytes"
1014 ext \vd\().16b, \vz\().16b, \vn\().16b, #16 - (\nbit >> 3)
1017 .macro vshr128 vd, vn, nbit, vz=v31
1018 // Set VD to VN shifted right by NBIT. Assume VZ (default v31) is
1019 // all-bits-zero. NBIT must be a multiple of 8.
1021 .error "shift quantity must be whole number of bytes"
1023 ext \vd\().16b, \vn\().16b, \vz\().16b, #\nbit >> 3
1026 // Stack management and unwinding.
1027 .macro setfp fp=x29, offset=0
1028 // If you're just going through the motions with a fixed-size stack frame,
1029 // then you want to say `add x29, sp, #OFFSET' directly, which will avoid
1030 // pointlessly restoring sp later.
1033 .cfi_def_cfa_register \fp
1035 add \fp, sp, #\offset
1036 .cfi_def_cfa_register \fp
1037 .cfi_adjust_cfa_offset -\offset
1039 .macro dropfp; _dropfp \fp, \offset; .endm
1043 .macro _dropfp fp, offset=0
1046 .cfi_def_cfa_register sp
1048 sub sp, \fp, #\offset
1049 .cfi_def_cfa_register sp
1050 .cfi_adjust_cfa_offset +\offset
1058 .cfi_adjust_cfa_offset +\n
1063 .cfi_adjust_cfa_offset -\n
1066 .macro pushreg x, y=nil
1069 .cfi_adjust_cfa_offset +16
1070 .cfi_rel_offset \x, 0
1072 stp \x, \y, [sp, #-16]!
1073 .cfi_adjust_cfa_offset +16
1074 .cfi_rel_offset \x, 0
1075 .cfi_rel_offset \y, 8
1079 .macro popreg x, y=nil
1083 .cfi_adjust_cfa_offset -16
1085 ldp \x, \y, [sp], #16
1088 .cfi_adjust_cfa_offset -16
1092 .macro savereg x, y, z=nil
1095 .cfi_rel_offset \x, \y
1097 stp \x, \y, [sp, #\z]
1098 .cfi_rel_offset \x, \z
1099 .cfi_rel_offset \y, \z + 8
1103 .macro rstrreg x, y, z=nil
1108 ldp \x, \y, [sp, #\z]
1117 // cmov RD, RN, CC: set RD to RN if CC is satisfied, otherwise do nothing
1118 .macro cmov rd, rn, cc
1119 csel \rd, \rn, \rd, \cc
1122 // Notational improvement: write `csel.CC' etc., rather than `csel ..., CC'.
1124 _(eq) _(ne) _(cs) _(cc) _(vs) _(vc) _(mi) _(pl) \
1125 _(ge) _(lt) _(gt) _(le) _(hi) _(ls) _(al) _(nv) \
1130 _(csinc) _(cinc) _(cset) \
1132 _(csinv) _(cinv) _(csetm)
1133 #define _CONDVAR(cc) _definstvar cc;
1134 #define _INSTVARS(inst) \
1135 .macro _definstvar cc; \
1136 .macro inst.\cc args:vararg; inst \args, \cc; .endm; \
1139 .purgem _definstvar;
1146 // Flag bits for `ccmp' and friends.
1152 // Flag settings for satisfying conditions.
1153 #define CCMP_MI CCMP_N
1155 #define CCMP_EQ CCMP_Z
1157 #define CCMP_CS CCMP_C
1158 #define CCMP_HS CCMP_C
1161 #define CCMP_VS CCMP_V
1163 #define CCMP_HI CCMP_C
1165 #define CCMP_LT CCMP_N
1167 #define CCMP_LE CCMP_N
1172 ///--------------------------------------------------------------------------
1175 // Default values for the various hooks.
1176 #ifndef FUNC_PREHOOK
1177 # define FUNC_PREHOOK(_)
1179 #ifndef FUNC_POSTHOOK
1180 # define FUNC_POSTHOOK(_)
1182 #ifndef ENDFUNC_HOOK
1183 # define ENDFUNC_HOOK(_)
1188 # define F(name) _##name
1190 # define F(name) name
1195 # define TYPE_FUNC(name)
1199 # define SIZE_OBJ(name)
1202 #if __ELF__ && !defined(FORCE_EXECUTABLE_STACK)
1203 .pushsection .note.GNU-stack, "", _SECTTY(progbits)
1207 ///----- That's all, folks --------------------------------------------------