f87f1a5b9605813ef45a30b8099a7a656730dc39
[openssl.git] / crypto / perlasm / x86_64-xlate.pl
1 #! /usr/bin/env perl
2 # Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9
10 # Ascetic x86_64 AT&T to MASM/NASM assembler translator by <appro>.
11 #
12 # Why AT&T to MASM and not vice versa? Several reasons. Because AT&T
13 # format is way easier to parse. Because it's simpler to "gear" from
14 # Unix ABI to Windows one [see cross-reference "card" at the end of
15 # file]. Because Linux targets were available first...
16 #
17 # In addition the script also "distills" code suitable for GNU
18 # assembler, so that it can be compiled with more rigid assemblers,
19 # such as Solaris /usr/ccs/bin/as.
20 #
21 # This translator is not designed to convert *arbitrary* assembler
22 # code from AT&T format to MASM one. It's designed to convert just
23 # enough to provide for dual-ABI OpenSSL modules development...
24 # There *are* limitations and you might have to modify your assembler
25 # code or this script to achieve the desired result...
26 #
27 # Currently recognized limitations:
28 #
29 # - can't use multiple ops per line;
30 #
31 # Dual-ABI styling rules.
32 #
33 # 1. Adhere to Unix register and stack layout [see cross-reference
34 #    ABI "card" at the end for explanation].
35 # 2. Forget about "red zone," stick to more traditional blended
36 #    stack frame allocation. If volatile storage is actually required
37 #    that is. If not, just leave the stack as is.
38 # 3. Functions tagged with ".type name,@function" get crafted with
39 #    unified Win64 prologue and epilogue automatically. If you want
40 #    to take care of ABI differences yourself, tag functions as
41 #    ".type name,@abi-omnipotent" instead.
42 # 4. To optimize the Win64 prologue you can specify number of input
43 #    arguments as ".type name,@function,N." Keep in mind that if N is
44 #    larger than 6, then you *have to* write "abi-omnipotent" code,
45 #    because >6 cases can't be addressed with unified prologue.
46 # 5. Name local labels as .L*, do *not* use dynamic labels such as 1:
47 #    (sorry about latter).
48 # 6. Don't use [or hand-code with .byte] "rep ret." "ret" mnemonic is
49 #    required to identify the spots, where to inject Win64 epilogue!
50 #    But on the pros, it's then prefixed with rep automatically:-)
51 # 7. Stick to explicit ip-relative addressing. If you have to use
52 #    GOTPCREL addressing, stick to mov symbol@GOTPCREL(%rip),%r??.
53 #    Both are recognized and translated to proper Win64 addressing
54 #    modes.
55 #
56 # 8. In order to provide for structured exception handling unified
57 #    Win64 prologue copies %rsp value to %rax. For further details
58 #    see SEH paragraph at the end.
59 # 9. .init segment is allowed to contain calls to functions only.
60 # a. If function accepts more than 4 arguments *and* >4th argument
61 #    is declared as non 64-bit value, do clear its upper part.
62 \f
63
64 use strict;
65
66 my $flavour = shift;
67 my $output  = shift;
68 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
69
70 open STDOUT,">$output" || die "can't open $output: $!"
71         if (defined($output));
72
73 my $gas=1;      $gas=0 if ($output =~ /\.asm$/);
74 my $elf=1;      $elf=0 if (!$gas);
75 my $win64=0;
76 my $prefix="";
77 my $decor=".L";
78
79 my $masmref=8 + 50727*2**-32;   # 8.00.50727 shipped with VS2005
80 my $masm=0;
81 my $PTR=" PTR";
82
83 my $nasmref=2.03;
84 my $nasm=0;
85
86 if    ($flavour eq "mingw64")   { $gas=1; $elf=0; $win64=1;
87                                   $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
88                                   $prefix =~ s|\R$||; # Better chomp
89                                 }
90 elsif ($flavour eq "macosx")    { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
91 elsif ($flavour eq "masm")      { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
92 elsif ($flavour eq "nasm")      { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
93 elsif (!$gas)
94 {   if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
95     {   $nasm = $1 + $2*0.01; $PTR="";  }
96     elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
97     {   $masm = $1 + $2*2**-16 + $4*2**-32;   }
98     die "no assembler found on %PATH%" if (!($nasm || $masm));
99     $win64=1;
100     $elf=0;
101     $decor="\$L\$";
102 }
103
104 my $cet_property;
105 if ($flavour =~ /elf/) {
106         # Always generate .note.gnu.property section for ELF outputs to
107         # mark Intel CET support since all input files must be marked
108         # with Intel CET support in order for linker to mark output with
109         # Intel CET support.
110         my $p2align=3; $p2align=2 if ($flavour eq "elf32");
111         $cet_property = <<_____;
112         .section ".note.gnu.property", "a"
113         .p2align $p2align
114         .long 1f - 0f
115         .long 4f - 1f
116         .long 5
117 0:
118         # "GNU" encoded with .byte, since .asciz isn't supported
119         # on Solaris.
120         .byte 0x47
121         .byte 0x4e
122         .byte 0x55
123         .byte 0
124 1:
125         .p2align $p2align
126         .long 0xc0000002
127         .long 3f - 2f
128 2:
129         .long 3
130 3:
131         .p2align $p2align
132 4:
133 _____
134 }
135
136 my $current_segment;
137 my $current_function;
138 my %globals;
139
140 { package opcode;       # pick up opcodes
141     sub re {
142         my      ($class, $line) = @_;
143         my      $self = {};
144         my      $ret;
145
146         if ($$line =~ /^([a-z][a-z0-9]*)/i) {
147             bless $self,$class;
148             $self->{op} = $1;
149             $ret = $self;
150             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
151
152             undef $self->{sz};
153             if ($self->{op} =~ /^(movz)x?([bw]).*/) {   # movz is pain...
154                 $self->{op} = $1;
155                 $self->{sz} = $2;
156             } elsif ($self->{op} =~ /call|jmp/) {
157                 $self->{sz} = "";
158             } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
159                 $self->{sz} = "";
160             } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
161                 $self->{sz} = "";
162             } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
163                 $self->{sz} = "";
164             } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
165                 $self->{op} = $1;
166                 $self->{sz} = $2;
167             }
168         }
169         $ret;
170     }
171     sub size {
172         my ($self, $sz) = @_;
173         $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
174         $self->{sz};
175     }
176     sub out {
177         my $self = shift;
178         if ($gas) {
179             if ($self->{op} eq "movz") {        # movz is pain...
180                 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
181             } elsif ($self->{op} =~ /^set/) {
182                 "$self->{op}";
183             } elsif ($self->{op} eq "ret") {
184                 my $epilogue = "";
185                 if ($win64 && $current_function->{abi} eq "svr4") {
186                     $epilogue = "movq   8(%rsp),%rdi\n\t" .
187                                 "movq   16(%rsp),%rsi\n\t";
188                 }
189                 $epilogue . ".byte      0xf3,0xc3";
190             } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
191                 ".p2align\t3\n\t.quad";
192             } else {
193                 "$self->{op}$self->{sz}";
194             }
195         } else {
196             $self->{op} =~ s/^movz/movzx/;
197             if ($self->{op} eq "ret") {
198                 $self->{op} = "";
199                 if ($win64 && $current_function->{abi} eq "svr4") {
200                     $self->{op} = "mov  rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
201                                   "mov  rsi,QWORD$PTR\[16+rsp\]\n\t";
202                 }
203                 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
204             } elsif ($self->{op} =~ /^(pop|push)f/) {
205                 $self->{op} .= $self->{sz};
206             } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
207                 $self->{op} = "\tDQ";
208             }
209             $self->{op};
210         }
211     }
212     sub mnemonic {
213         my ($self, $op) = @_;
214         $self->{op}=$op if (defined($op));
215         $self->{op};
216     }
217 }
218 { package const;        # pick up constants, which start with $
219     sub re {
220         my      ($class, $line) = @_;
221         my      $self = {};
222         my      $ret;
223
224         if ($$line =~ /^\$([^,]+)/) {
225             bless $self, $class;
226             $self->{value} = $1;
227             $ret = $self;
228             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
229         }
230         $ret;
231     }
232     sub out {
233         my $self = shift;
234
235         $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
236         if ($gas) {
237             # Solaris /usr/ccs/bin/as can't handle multiplications
238             # in $self->{value}
239             my $value = $self->{value};
240             no warnings;    # oct might complain about overflow, ignore here...
241             $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
242             if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
243                 $self->{value} = $value;
244             }
245             sprintf "\$%s",$self->{value};
246         } else {
247             my $value = $self->{value};
248             $value =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
249             sprintf "%s",$value;
250         }
251     }
252 }
253 { package ea;           # pick up effective addresses: expr(%reg,%reg,scale)
254
255     my %szmap = (       b=>"BYTE$PTR",    w=>"WORD$PTR",
256                         l=>"DWORD$PTR",   d=>"DWORD$PTR",
257                         q=>"QWORD$PTR",   o=>"OWORD$PTR",
258                         x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
259                         z=>"ZMMWORD$PTR" ) if (!$gas);
260
261     sub re {
262         my      ($class, $line, $opcode) = @_;
263         my      $self = {};
264         my      $ret;
265
266         # optional * ----vvv--- appears in indirect jmp/call
267         if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
268             bless $self, $class;
269             $self->{asterisk} = $1;
270             $self->{label} = $2;
271             ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
272             $self->{scale} = 1 if (!defined($self->{scale}));
273             $self->{opmask} = $4;
274             $ret = $self;
275             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
276
277             if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
278                 die if ($opcode->mnemonic() ne "mov");
279                 $opcode->mnemonic("lea");
280             }
281             $self->{base}  =~ s/^%//;
282             $self->{index} =~ s/^%// if (defined($self->{index}));
283             $self->{opcode} = $opcode;
284         }
285         $ret;
286     }
287     sub size {}
288     sub out {
289         my ($self, $sz) = @_;
290
291         $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
292         $self->{label} =~ s/\.L/$decor/g;
293
294         # Silently convert all EAs to 64-bit. This is required for
295         # elder GNU assembler and results in more compact code,
296         # *but* most importantly AES module depends on this feature!
297         $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
298         $self->{base}  =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
299
300         # Solaris /usr/ccs/bin/as can't handle multiplications
301         # in $self->{label}...
302         use integer;
303         $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
304         $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
305
306         # Some assemblers insist on signed presentation of 32-bit
307         # offsets, but sign extension is a tricky business in perl...
308         if ((1<<31)<<1) {
309             $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
310         } else {
311             $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
312         }
313
314         # if base register is %rbp or %r13, see if it's possible to
315         # flip base and index registers [for better performance]
316         if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
317             $self->{base} =~ /(rbp|r13)/) {
318                 $self->{base} = $self->{index}; $self->{index} = $1;
319         }
320
321         if ($gas) {
322             $self->{label} =~ s/^___imp_/__imp__/   if ($flavour eq "mingw64");
323
324             if (defined($self->{index})) {
325                 sprintf "%s%s(%s,%%%s,%d)%s",
326                                         $self->{asterisk},$self->{label},
327                                         $self->{base}?"%$self->{base}":"",
328                                         $self->{index},$self->{scale},
329                                         $self->{opmask};
330             } else {
331                 sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
332                                         $self->{base},$self->{opmask};
333             }
334         } else {
335             $self->{label} =~ s/\./\$/g;
336             $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
337             $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
338
339             my $mnemonic = $self->{opcode}->mnemonic();
340             ($self->{asterisk})                         && ($sz="q") ||
341             ($mnemonic =~ /^v?mov([qd])$/)              && ($sz=$1)  ||
342             ($mnemonic =~ /^v?pinsr([qdwb])$/)          && ($sz=$1)  ||
343             ($mnemonic =~ /^vpbroadcast([qdwb])$/)      && ($sz=$1)  ||
344             ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/)   && ($sz="x");
345
346             $self->{opmask}  =~ s/%(k[0-7])/$1/;
347
348             if (defined($self->{index})) {
349                 sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
350                                         $self->{label}?"$self->{label}+":"",
351                                         $self->{index},$self->{scale},
352                                         $self->{base}?"+$self->{base}":"",
353                                         $self->{opmask};
354             } elsif ($self->{base} eq "rip") {
355                 sprintf "%s[%s]",$szmap{$sz},$self->{label};
356             } else {
357                 sprintf "%s[%s%s]%s",   $szmap{$sz},
358                                         $self->{label}?"$self->{label}+":"",
359                                         $self->{base},$self->{opmask};
360             }
361         }
362     }
363 }
364 { package register;     # pick up registers, which start with %.
365     sub re {
366         my      ($class, $line, $opcode) = @_;
367         my      $self = {};
368         my      $ret;
369
370         # optional * ----vvv--- appears in indirect jmp/call
371         if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
372             bless $self,$class;
373             $self->{asterisk} = $1;
374             $self->{value} = $2;
375             $self->{opmask} = $3;
376             $opcode->size($self->size());
377             $ret = $self;
378             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
379         }
380         $ret;
381     }
382     sub size {
383         my      $self = shift;
384         my      $ret;
385
386         if    ($self->{value} =~ /^r[\d]+b$/i)  { $ret="b"; }
387         elsif ($self->{value} =~ /^r[\d]+w$/i)  { $ret="w"; }
388         elsif ($self->{value} =~ /^r[\d]+d$/i)  { $ret="l"; }
389         elsif ($self->{value} =~ /^r[\w]+$/i)   { $ret="q"; }
390         elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
391         elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
392         elsif ($self->{value} =~ /^[\w]{2}$/i)  { $ret="w"; }
393         elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
394
395         $ret;
396     }
397     sub out {
398         my $self = shift;
399         if ($gas)       { sprintf "%s%%%s%s",   $self->{asterisk},
400                                                 $self->{value},
401                                                 $self->{opmask}; }
402         else            { $self->{opmask} =~ s/%(k[0-7])/$1/;
403                           $self->{value}.$self->{opmask}; }
404     }
405 }
406 { package label;        # pick up labels, which end with :
407     sub re {
408         my      ($class, $line) = @_;
409         my      $self = {};
410         my      $ret;
411
412         if ($$line =~ /(^[\.\w]+)\:/) {
413             bless $self,$class;
414             $self->{value} = $1;
415             $ret = $self;
416             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
417
418             $self->{value} =~ s/^\.L/$decor/;
419         }
420         $ret;
421     }
422     sub out {
423         my $self = shift;
424
425         if ($gas) {
426             my $func = ($globals{$self->{value}} or $self->{value}) . ":";
427             if ($win64  && $current_function->{name} eq $self->{value}
428                         && $current_function->{abi} eq "svr4") {
429                 $func .= "\n";
430                 $func .= "      movq    %rdi,8(%rsp)\n";
431                 $func .= "      movq    %rsi,16(%rsp)\n";
432                 $func .= "      movq    %rsp,%rax\n";
433                 $func .= "${decor}SEH_begin_$current_function->{name}:\n";
434                 my $narg = $current_function->{narg};
435                 $narg=6 if (!defined($narg));
436                 $func .= "      movq    %rcx,%rdi\n" if ($narg>0);
437                 $func .= "      movq    %rdx,%rsi\n" if ($narg>1);
438                 $func .= "      movq    %r8,%rdx\n"  if ($narg>2);
439                 $func .= "      movq    %r9,%rcx\n"  if ($narg>3);
440                 $func .= "      movq    40(%rsp),%r8\n" if ($narg>4);
441                 $func .= "      movq    48(%rsp),%r9\n" if ($narg>5);
442             }
443             $func;
444         } elsif ($self->{value} ne "$current_function->{name}") {
445             # Make all labels in masm global.
446             $self->{value} .= ":" if ($masm);
447             $self->{value} . ":";
448         } elsif ($win64 && $current_function->{abi} eq "svr4") {
449             my $func =  "$current_function->{name}" .
450                         ($nasm ? ":" : "\tPROC $current_function->{scope}") .
451                         "\n";
452             $func .= "  mov     QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
453             $func .= "  mov     QWORD$PTR\[16+rsp\],rsi\n";
454             $func .= "  mov     rax,rsp\n";
455             $func .= "${decor}SEH_begin_$current_function->{name}:";
456             $func .= ":" if ($masm);
457             $func .= "\n";
458             my $narg = $current_function->{narg};
459             $narg=6 if (!defined($narg));
460             $func .= "  mov     rdi,rcx\n" if ($narg>0);
461             $func .= "  mov     rsi,rdx\n" if ($narg>1);
462             $func .= "  mov     rdx,r8\n"  if ($narg>2);
463             $func .= "  mov     rcx,r9\n"  if ($narg>3);
464             $func .= "  mov     r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
465             $func .= "  mov     r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
466             $func .= "\n";
467         } else {
468            "$current_function->{name}".
469                         ($nasm ? ":" : "\tPROC $current_function->{scope}");
470         }
471     }
472 }
473 { package expr;         # pick up expressions
474     sub re {
475         my      ($class, $line, $opcode) = @_;
476         my      $self = {};
477         my      $ret;
478
479         if ($$line =~ /(^[^,]+)/) {
480             bless $self,$class;
481             $self->{value} = $1;
482             $ret = $self;
483             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
484
485             $self->{value} =~ s/\@PLT// if (!$elf);
486             $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
487             $self->{value} =~ s/\.L/$decor/g;
488             $self->{opcode} = $opcode;
489         }
490         $ret;
491     }
492     sub out {
493         my $self = shift;
494         if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
495             "NEAR ".$self->{value};
496         } else {
497             $self->{value};
498         }
499     }
500 }
501 { package cfi_directive;
502     # CFI directives annotate instructions that are significant for
503     # stack unwinding procedure compliant with DWARF specification,
504     # see http://dwarfstd.org/. Besides naturally expected for this
505     # script platform-specific filtering function, this module adds
506     # three auxiliary synthetic directives not recognized by [GNU]
507     # assembler:
508     #
509     # - .cfi_push to annotate push instructions in prologue, which
510     #   translates to .cfi_adjust_cfa_offset (if needed) and
511     #   .cfi_offset;
512     # - .cfi_pop to annotate pop instructions in epilogue, which
513     #   translates to .cfi_adjust_cfa_offset (if needed) and
514     #   .cfi_restore;
515     # - [and most notably] .cfi_cfa_expression which encodes
516     #   DW_CFA_def_cfa_expression and passes it to .cfi_escape as
517     #   byte vector;
518     #
519     # CFA expressions were introduced in DWARF specification version
520     # 3 and describe how to deduce CFA, Canonical Frame Address. This
521     # becomes handy if your stack frame is variable and you can't
522     # spare register for [previous] frame pointer. Suggested directive
523     # syntax is made-up mix of DWARF operator suffixes [subset of]
524     # and references to registers with optional bias. Following example
525     # describes offloaded *original* stack pointer at specific offset
526     # from *current* stack pointer:
527     #
528     #   .cfi_cfa_expression     %rsp+40,deref,+8
529     #
530     # Final +8 has everything to do with the fact that CFA is defined
531     # as reference to top of caller's stack, and on x86_64 call to
532     # subroutine pushes 8-byte return address. In other words original
533     # stack pointer upon entry to a subroutine is 8 bytes off from CFA.
534
535     # Below constants are taken from "DWARF Expressions" section of the
536     # DWARF specification, section is numbered 7.7 in versions 3 and 4.
537     my %DW_OP_simple = (        # no-arg operators, mapped directly
538         deref   => 0x06,        dup     => 0x12,
539         drop    => 0x13,        over    => 0x14,
540         pick    => 0x15,        swap    => 0x16,
541         rot     => 0x17,        xderef  => 0x18,
542
543         abs     => 0x19,        and     => 0x1a,
544         div     => 0x1b,        minus   => 0x1c,
545         mod     => 0x1d,        mul     => 0x1e,
546         neg     => 0x1f,        not     => 0x20,
547         or      => 0x21,        plus    => 0x22,
548         shl     => 0x24,        shr     => 0x25,
549         shra    => 0x26,        xor     => 0x27,
550         );
551
552     my %DW_OP_complex = (       # used in specific subroutines
553         constu          => 0x10,        # uleb128
554         consts          => 0x11,        # sleb128
555         plus_uconst     => 0x23,        # uleb128
556         lit0            => 0x30,        # add 0-31 to opcode
557         reg0            => 0x50,        # add 0-31 to opcode
558         breg0           => 0x70,        # add 0-31 to opcole, sleb128
559         regx            => 0x90,        # uleb28
560         fbreg           => 0x91,        # sleb128
561         bregx           => 0x92,        # uleb128, sleb128
562         piece           => 0x93,        # uleb128
563         );
564
565     # Following constants are defined in x86_64 ABI supplement, for
566     # example available at https://www.uclibc.org/docs/psABI-x86_64.pdf,
567     # see section 3.7 "Stack Unwind Algorithm".
568     my %DW_reg_idx = (
569         "%rax"=>0,  "%rdx"=>1,  "%rcx"=>2,  "%rbx"=>3,
570         "%rsi"=>4,  "%rdi"=>5,  "%rbp"=>6,  "%rsp"=>7,
571         "%r8" =>8,  "%r9" =>9,  "%r10"=>10, "%r11"=>11,
572         "%r12"=>12, "%r13"=>13, "%r14"=>14, "%r15"=>15
573         );
574
575     my ($cfa_reg, $cfa_rsp);
576     my @cfa_stack;
577
578     # [us]leb128 format is variable-length integer representation base
579     # 2^128, with most significant bit of each byte being 0 denoting
580     # *last* most significant digit. See "Variable Length Data" in the
581     # DWARF specification, numbered 7.6 at least in versions 3 and 4.
582     sub sleb128 {
583         use integer;    # get right shift extend sign
584
585         my $val = shift;
586         my $sign = ($val < 0) ? -1 : 0;
587         my @ret = ();
588
589         while(1) {
590             push @ret, $val&0x7f;
591
592             # see if remaining bits are same and equal to most
593             # significant bit of the current digit, if so, it's
594             # last digit...
595             last if (($val>>6) == $sign);
596
597             @ret[-1] |= 0x80;
598             $val >>= 7;
599         }
600
601         return @ret;
602     }
603     sub uleb128 {
604         my $val = shift;
605         my @ret = ();
606
607         while(1) {
608             push @ret, $val&0x7f;
609
610             # see if it's last significant digit...
611             last if (($val >>= 7) == 0);
612
613             @ret[-1] |= 0x80;
614         }
615
616         return @ret;
617     }
618     sub const {
619         my $val = shift;
620
621         if ($val >= 0 && $val < 32) {
622             return ($DW_OP_complex{lit0}+$val);
623         }
624         return ($DW_OP_complex{consts}, sleb128($val));
625     }
626     sub reg {
627         my $val = shift;
628
629         return if ($val !~ m/^(%r\w+)(?:([\+\-])((?:0x)?[0-9a-f]+))?/);
630
631         my $reg = $DW_reg_idx{$1};
632         my $off = eval ("0 $2 $3");
633
634         return (($DW_OP_complex{breg0} + $reg), sleb128($off));
635         # Yes, we use DW_OP_bregX+0 to push register value and not
636         # DW_OP_regX, because latter would require even DW_OP_piece,
637         # which would be a waste under the circumstances. If you have
638         # to use DWP_OP_reg, use "regx:N"...
639     }
640     sub cfa_expression {
641         my $line = shift;
642         my @ret;
643
644         foreach my $token (split(/,\s*/,$line)) {
645             if ($token =~ /^%r/) {
646                 push @ret,reg($token);
647             } elsif ($token =~ /((?:0x)?[0-9a-f]+)\((%r\w+)\)/) {
648                 push @ret,reg("$2+$1");
649             } elsif ($token =~ /(\w+):(\-?(?:0x)?[0-9a-f]+)(U?)/i) {
650                 my $i = 1*eval($2);
651                 push @ret,$DW_OP_complex{$1}, ($3 ? uleb128($i) : sleb128($i));
652             } elsif (my $i = 1*eval($token) or $token eq "0") {
653                 if ($token =~ /^\+/) {
654                     push @ret,$DW_OP_complex{plus_uconst},uleb128($i);
655                 } else {
656                     push @ret,const($i);
657                 }
658             } else {
659                 push @ret,$DW_OP_simple{$token};
660             }
661         }
662
663         # Finally we return DW_CFA_def_cfa_expression, 15, followed by
664         # length of the expression and of course the expression itself.
665         return (15,scalar(@ret),@ret);
666     }
667     sub re {
668         my      ($class, $line) = @_;
669         my      $self = {};
670         my      $ret;
671
672         if ($$line =~ s/^\s*\.cfi_(\w+)\s*//) {
673             bless $self,$class;
674             $ret = $self;
675             undef $self->{value};
676             my $dir = $1;
677
678             SWITCH: for ($dir) {
679             # What is $cfa_rsp? Effectively it's difference between %rsp
680             # value and current CFA, Canonical Frame Address, which is
681             # why it starts with -8. Recall that CFA is top of caller's
682             # stack...
683             /startproc/ && do { ($cfa_reg, $cfa_rsp) = ("%rsp", -8); last; };
684             /endproc/   && do { ($cfa_reg, $cfa_rsp) = ("%rsp",  0);
685                                 # .cfi_remember_state directives that are not
686                                 # matched with .cfi_restore_state are
687                                 # unnecessary.
688                                 die "unpaired .cfi_remember_state" if (@cfa_stack);
689                                 last;
690                               };
691             /def_cfa_register/
692                         && do { $cfa_reg = $$line; last; };
693             /def_cfa_offset/
694                         && do { $cfa_rsp = -1*eval($$line) if ($cfa_reg eq "%rsp");
695                                 last;
696                               };
697             /adjust_cfa_offset/
698                         && do { $cfa_rsp -= 1*eval($$line) if ($cfa_reg eq "%rsp");
699                                 last;
700                               };
701             /def_cfa/   && do { if ($$line =~ /(%r\w+)\s*,\s*(.+)/) {
702                                     $cfa_reg = $1;
703                                     $cfa_rsp = -1*eval($2) if ($cfa_reg eq "%rsp");
704                                 }
705                                 last;
706                               };
707             /push/      && do { $dir = undef;
708                                 $cfa_rsp -= 8;
709                                 if ($cfa_reg eq "%rsp") {
710                                     $self->{value} = ".cfi_adjust_cfa_offset\t8\n";
711                                 }
712                                 $self->{value} .= ".cfi_offset\t$$line,$cfa_rsp";
713                                 last;
714                               };
715             /pop/       && do { $dir = undef;
716                                 $cfa_rsp += 8;
717                                 if ($cfa_reg eq "%rsp") {
718                                     $self->{value} = ".cfi_adjust_cfa_offset\t-8\n";
719                                 }
720                                 $self->{value} .= ".cfi_restore\t$$line";
721                                 last;
722                               };
723             /cfa_expression/
724                         && do { $dir = undef;
725                                 $self->{value} = ".cfi_escape\t" .
726                                         join(",", map(sprintf("0x%02x", $_),
727                                                       cfa_expression($$line)));
728                                 last;
729                               };
730             /remember_state/
731                         && do { push @cfa_stack, [$cfa_reg, $cfa_rsp];
732                                 last;
733                               };
734             /restore_state/
735                         && do { ($cfa_reg, $cfa_rsp) = @{pop @cfa_stack};
736                                 last;
737                               };
738             }
739
740             $self->{value} = ".cfi_$dir\t$$line" if ($dir);
741
742             $$line = "";
743         }
744
745         return $ret;
746     }
747     sub out {
748         my $self = shift;
749         return ($elf ? $self->{value} : undef);
750     }
751 }
752 { package directive;    # pick up directives, which start with .
753     sub re {
754         my      ($class, $line) = @_;
755         my      $self = {};
756         my      $ret;
757         my      $dir;
758
759         # chain-call to cfi_directive
760         $ret = cfi_directive->re($line) and return $ret;
761
762         if ($$line =~ /^\s*(\.\w+)/) {
763             bless $self,$class;
764             $dir = $1;
765             $ret = $self;
766             undef $self->{value};
767             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
768
769             SWITCH: for ($dir) {
770                 /\.global|\.globl|\.extern/
771                             && do { $globals{$$line} = $prefix . $$line;
772                                     $$line = $globals{$$line} if ($prefix);
773                                     last;
774                                   };
775                 /\.type/    && do { my ($sym,$type,$narg) = split(',',$$line);
776                                     if ($type eq "\@function") {
777                                         undef $current_function;
778                                         $current_function->{name} = $sym;
779                                         $current_function->{abi}  = "svr4";
780                                         $current_function->{narg} = $narg;
781                                         $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
782                                     } elsif ($type eq "\@abi-omnipotent") {
783                                         undef $current_function;
784                                         $current_function->{name} = $sym;
785                                         $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
786                                     }
787                                     $$line =~ s/\@abi\-omnipotent/\@function/;
788                                     $$line =~ s/\@function.*/\@function/;
789                                     last;
790                                   };
791                 /\.asciz/   && do { if ($$line =~ /^"(.*)"$/) {
792                                         $dir  = ".byte";
793                                         $$line = join(",",unpack("C*",$1),0);
794                                     }
795                                     last;
796                                   };
797                 /\.rva|\.long|\.quad/
798                             && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
799                                     $$line =~ s/\.L/$decor/g;
800                                     last;
801                                   };
802             }
803
804             if ($gas) {
805                 $self->{value} = $dir . "\t" . $$line;
806
807                 if ($dir =~ /\.extern/) {
808                     $self->{value} = ""; # swallow extern
809                 } elsif (!$elf && $dir =~ /\.type/) {
810                     $self->{value} = "";
811                     $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
812                                 (defined($globals{$1})?".scl 2;":".scl 3;") .
813                                 "\t.type 32;\t.endef"
814                                 if ($win64 && $$line =~ /([^,]+),\@function/);
815                 } elsif (!$elf && $dir =~ /\.size/) {
816                     $self->{value} = "";
817                     if (defined($current_function)) {
818                         $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
819                                 if ($win64 && $current_function->{abi} eq "svr4");
820                         undef $current_function;
821                     }
822                 } elsif (!$elf && $dir =~ /\.align/) {
823                     $self->{value} = ".p2align\t" . (log($$line)/log(2));
824                 } elsif ($dir eq ".section") {
825                     $current_segment=$$line;
826                     if (!$elf && $current_segment eq ".init") {
827                         if      ($flavour eq "macosx")  { $self->{value} = ".mod_init_func"; }
828                         elsif   ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
829                     }
830                 } elsif ($dir =~ /\.(text|data)/) {
831                     $current_segment=".$1";
832                 } elsif ($dir =~ /\.hidden/) {
833                     if    ($flavour eq "macosx")  { $self->{value} = ".private_extern\t$prefix$$line"; }
834                     elsif ($flavour eq "mingw64") { $self->{value} = ""; }
835                 } elsif ($dir =~ /\.comm/) {
836                     $self->{value} = "$dir\t$prefix$$line";
837                     $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
838                 }
839                 $$line = "";
840                 return $self;
841             }
842
843             # non-gas case or nasm/masm
844             SWITCH: for ($dir) {
845                 /\.text/    && do { my $v=undef;
846                                     if ($nasm) {
847                                         $v="section     .text code align=64\n";
848                                     } else {
849                                         $v="$current_segment\tENDS\n" if ($current_segment);
850                                         $current_segment = ".text\$";
851                                         $v.="$current_segment\tSEGMENT ";
852                                         $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
853                                         $v.=" 'CODE'";
854                                     }
855                                     $self->{value} = $v;
856                                     last;
857                                   };
858                 /\.data/    && do { my $v=undef;
859                                     if ($nasm) {
860                                         $v="section     .data data align=8\n";
861                                     } else {
862                                         $v="$current_segment\tENDS\n" if ($current_segment);
863                                         $current_segment = "_DATA";
864                                         $v.="$current_segment\tSEGMENT";
865                                     }
866                                     $self->{value} = $v;
867                                     last;
868                                   };
869                 /\.section/ && do { my $v=undef;
870                                     $$line =~ s/([^,]*).*/$1/;
871                                     $$line = ".CRT\$XCU" if ($$line eq ".init");
872                                     if ($nasm) {
873                                         $v="section     $$line";
874                                         if ($$line=~/\.([px])data/) {
875                                             $v.=" rdata align=";
876                                             $v.=$1 eq "p"? 4 : 8;
877                                         } elsif ($$line=~/\.CRT\$/i) {
878                                             $v.=" rdata align=8";
879                                         }
880                                     } else {
881                                         $v="$current_segment\tENDS\n" if ($current_segment);
882                                         $v.="$$line\tSEGMENT";
883                                         if ($$line=~/\.([px])data/) {
884                                             $v.=" READONLY";
885                                             $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
886                                         } elsif ($$line=~/\.CRT\$/i) {
887                                             $v.=" READONLY ";
888                                             $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
889                                         }
890                                     }
891                                     $current_segment = $$line;
892                                     $self->{value} = $v;
893                                     last;
894                                   };
895                 /\.extern/  && do { $self->{value}  = "EXTERN\t".$$line;
896                                     $self->{value} .= ":NEAR" if ($masm);
897                                     last;
898                                   };
899                 /\.globl|.global/
900                             && do { $self->{value}  = $masm?"PUBLIC":"global";
901                                     $self->{value} .= "\t".$$line;
902                                     last;
903                                   };
904                 /\.size/    && do { if (defined($current_function)) {
905                                         undef $self->{value};
906                                         if ($current_function->{abi} eq "svr4") {
907                                             $self->{value}="${decor}SEH_end_$current_function->{name}:";
908                                             $self->{value}.=":\n" if($masm);
909                                         }
910                                         $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
911                                         undef $current_function;
912                                     }
913                                     last;
914                                   };
915                 /\.align/   && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
916                                     $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
917                                     last;
918                                   };
919                 /\.(value|long|rva|quad)/
920                             && do { my $sz  = substr($1,0,1);
921                                     my @arr = split(/,\s*/,$$line);
922                                     my $last = pop(@arr);
923                                     my $conv = sub  {   my $var=shift;
924                                                         $var=~s/^(0b[0-1]+)/oct($1)/eig;
925                                                         $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
926                                                         if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
927                                                         { $var=~s/^([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
928                                                         $var;
929                                                     };
930
931                                     $sz =~ tr/bvlrq/BWDDQ/;
932                                     $self->{value} = "\tD$sz\t";
933                                     for (@arr) { $self->{value} .= &$conv($_).","; }
934                                     $self->{value} .= &$conv($last);
935                                     last;
936                                   };
937                 /\.byte/    && do { my @str=split(/,\s*/,$$line);
938                                     map(s/(0b[0-1]+)/oct($1)/eig,@str);
939                                     map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
940                                     while ($#str>15) {
941                                         $self->{value}.="DB\t"
942                                                 .join(",",@str[0..15])."\n";
943                                         foreach (0..15) { shift @str; }
944                                     }
945                                     $self->{value}.="DB\t"
946                                                 .join(",",@str) if (@str);
947                                     last;
948                                   };
949                 /\.comm/    && do { my @str=split(/,\s*/,$$line);
950                                     my $v=undef;
951                                     if ($nasm) {
952                                         $v.="common     $prefix@str[0] @str[1]";
953                                     } else {
954                                         $v="$current_segment\tENDS\n" if ($current_segment);
955                                         $current_segment = "_DATA";
956                                         $v.="$current_segment\tSEGMENT\n";
957                                         $v.="COMM       @str[0]:DWORD:".@str[1]/4;
958                                     }
959                                     $self->{value} = $v;
960                                     last;
961                                   };
962             }
963             $$line = "";
964         }
965
966         $ret;
967     }
968     sub out {
969         my $self = shift;
970         $self->{value};
971     }
972 }
973
974 # Upon initial x86_64 introduction SSE>2 extensions were not introduced
975 # yet. In order not to be bothered by tracing exact assembler versions,
976 # but at the same time to provide a bare security minimum of AES-NI, we
977 # hard-code some instructions. Extensions past AES-NI on the other hand
978 # are traced by examining assembler version in individual perlasm
979 # modules...
980
981 my %regrm = (   "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
982                 "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7      );
983
984 sub rex {
985  my $opcode=shift;
986  my ($dst,$src,$rex)=@_;
987
988    $rex|=0x04 if($dst>=8);
989    $rex|=0x01 if($src>=8);
990    push @$opcode,($rex|0x40) if ($rex);
991 }
992
993 my $movq = sub {        # elderly gas can't handle inter-register movq
994   my $arg = shift;
995   my @opcode=(0x66);
996     if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
997         my ($src,$dst)=($1,$2);
998         if ($dst !~ /[0-9]+/)   { $dst = $regrm{"%e$dst"}; }
999         rex(\@opcode,$src,$dst,0x8);
1000         push @opcode,0x0f,0x7e;
1001         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
1002         @opcode;
1003     } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
1004         my ($src,$dst)=($2,$1);
1005         if ($dst !~ /[0-9]+/)   { $dst = $regrm{"%e$dst"}; }
1006         rex(\@opcode,$src,$dst,0x8);
1007         push @opcode,0x0f,0x6e;
1008         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
1009         @opcode;
1010     } else {
1011         ();
1012     }
1013 };
1014
1015 my $pextrd = sub {
1016     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
1017       my @opcode=(0x66);
1018         my $imm=$1;
1019         my $src=$2;
1020         my $dst=$3;
1021         if ($dst =~ /%r([0-9]+)d/)      { $dst = $1; }
1022         elsif ($dst =~ /%e/)            { $dst = $regrm{$dst}; }
1023         rex(\@opcode,$src,$dst);
1024         push @opcode,0x0f,0x3a,0x16;
1025         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
1026         push @opcode,$imm;
1027         @opcode;
1028     } else {
1029         ();
1030     }
1031 };
1032
1033 my $pinsrd = sub {
1034     if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
1035       my @opcode=(0x66);
1036         my $imm=$1;
1037         my $src=$2;
1038         my $dst=$3;
1039         if ($src =~ /%r([0-9]+)/)       { $src = $1; }
1040         elsif ($src =~ /%e/)            { $src = $regrm{$src}; }
1041         rex(\@opcode,$dst,$src);
1042         push @opcode,0x0f,0x3a,0x22;
1043         push @opcode,0xc0|(($dst&7)<<3)|($src&7);       # ModR/M
1044         push @opcode,$imm;
1045         @opcode;
1046     } else {
1047         ();
1048     }
1049 };
1050
1051 my $pshufb = sub {
1052     if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1053       my @opcode=(0x66);
1054         rex(\@opcode,$2,$1);
1055         push @opcode,0x0f,0x38,0x00;
1056         push @opcode,0xc0|($1&7)|(($2&7)<<3);           # ModR/M
1057         @opcode;
1058     } else {
1059         ();
1060     }
1061 };
1062
1063 my $palignr = sub {
1064     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1065       my @opcode=(0x66);
1066         rex(\@opcode,$3,$2);
1067         push @opcode,0x0f,0x3a,0x0f;
1068         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
1069         push @opcode,$1;
1070         @opcode;
1071     } else {
1072         ();
1073     }
1074 };
1075
1076 my $pclmulqdq = sub {
1077     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1078       my @opcode=(0x66);
1079         rex(\@opcode,$3,$2);
1080         push @opcode,0x0f,0x3a,0x44;
1081         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
1082         my $c=$1;
1083         push @opcode,$c=~/^0/?oct($c):$c;
1084         @opcode;
1085     } else {
1086         ();
1087     }
1088 };
1089
1090 my $rdrand = sub {
1091     if (shift =~ /%[er](\w+)/) {
1092       my @opcode=();
1093       my $dst=$1;
1094         if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
1095         rex(\@opcode,0,$dst,8);
1096         push @opcode,0x0f,0xc7,0xf0|($dst&7);
1097         @opcode;
1098     } else {
1099         ();
1100     }
1101 };
1102
1103 my $rdseed = sub {
1104     if (shift =~ /%[er](\w+)/) {
1105       my @opcode=();
1106       my $dst=$1;
1107         if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
1108         rex(\@opcode,0,$dst,8);
1109         push @opcode,0x0f,0xc7,0xf8|($dst&7);
1110         @opcode;
1111     } else {
1112         ();
1113     }
1114 };
1115
1116 # Not all AVX-capable assemblers recognize AMD XOP extension. Since we
1117 # are using only two instructions hand-code them in order to be excused
1118 # from chasing assembler versions...
1119
1120 sub rxb {
1121  my $opcode=shift;
1122  my ($dst,$src1,$src2,$rxb)=@_;
1123
1124    $rxb|=0x7<<5;
1125    $rxb&=~(0x04<<5) if($dst>=8);
1126    $rxb&=~(0x01<<5) if($src1>=8);
1127    $rxb&=~(0x02<<5) if($src2>=8);
1128    push @$opcode,$rxb;
1129 }
1130
1131 my $vprotd = sub {
1132     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1133       my @opcode=(0x8f);
1134         rxb(\@opcode,$3,$2,-1,0x08);
1135         push @opcode,0x78,0xc2;
1136         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
1137         my $c=$1;
1138         push @opcode,$c=~/^0/?oct($c):$c;
1139         @opcode;
1140     } else {
1141         ();
1142     }
1143 };
1144
1145 my $vprotq = sub {
1146     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
1147       my @opcode=(0x8f);
1148         rxb(\@opcode,$3,$2,-1,0x08);
1149         push @opcode,0x78,0xc3;
1150         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
1151         my $c=$1;
1152         push @opcode,$c=~/^0/?oct($c):$c;
1153         @opcode;
1154     } else {
1155         ();
1156     }
1157 };
1158
1159 # Intel Control-flow Enforcement Technology extension. All functions and
1160 # indirect branch targets will have to start with this instruction...
1161
1162 my $endbranch = sub {
1163     (0xf3,0x0f,0x1e,0xfa);
1164 };
1165
1166 ########################################################################
1167
1168 if ($nasm) {
1169     print <<___;
1170 default rel
1171 %define XMMWORD
1172 %define YMMWORD
1173 %define ZMMWORD
1174 ___
1175 } elsif ($masm) {
1176     print <<___;
1177 OPTION  DOTNAME
1178 ___
1179 }
1180 while(defined(my $line=<>)) {
1181
1182     $line =~ s|\R$||;           # Better chomp
1183
1184     $line =~ s|[#!].*$||;       # get rid of asm-style comments...
1185     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
1186     $line =~ s|^\s+||;          # ... and skip white spaces in beginning
1187     $line =~ s|\s+$||;          # ... and at the end
1188
1189     if (my $label=label->re(\$line))    { print $label->out(); }
1190
1191     if (my $directive=directive->re(\$line)) {
1192         printf "%s",$directive->out();
1193     } elsif (my $opcode=opcode->re(\$line)) {
1194         my $asm = eval("\$".$opcode->mnemonic());
1195
1196         if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
1197             print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
1198             next;
1199         }
1200
1201         my @args;
1202         ARGUMENT: while (1) {
1203             my $arg;
1204
1205             ($arg=register->re(\$line, $opcode))||
1206             ($arg=const->re(\$line))            ||
1207             ($arg=ea->re(\$line, $opcode))      ||
1208             ($arg=expr->re(\$line, $opcode))    ||
1209             last ARGUMENT;
1210
1211             push @args,$arg;
1212
1213             last ARGUMENT if ($line !~ /^,/);
1214
1215             $line =~ s/^,\s*//;
1216         } # ARGUMENT:
1217
1218         if ($#args>=0) {
1219             my $insn;
1220             my $sz=$opcode->size();
1221
1222             if ($gas) {
1223                 $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
1224                 @args = map($_->out($sz),@args);
1225                 printf "\t%s\t%s",$insn,join(",",@args);
1226             } else {
1227                 $insn = $opcode->out();
1228                 foreach (@args) {
1229                     my $arg = $_->out();
1230                     # $insn.=$sz compensates for movq, pinsrw, ...
1231                     if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
1232                     if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
1233                     if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
1234                     if ($arg =~ /^mm[0-9]+$/)  { $insn.=$sz; $sz="q" if(!$sz); last; }
1235                 }
1236                 @args = reverse(@args);
1237                 undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
1238                 printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
1239             }
1240         } else {
1241             printf "\t%s",$opcode->out();
1242         }
1243     }
1244
1245     print $line,"\n";
1246 }
1247
1248 print "$cet_property"                   if ($cet_property);
1249 print "\n$current_segment\tENDS\n"      if ($current_segment && $masm);
1250 print "END\n"                           if ($masm);
1251
1252 close STDOUT or die "error closing STDOUT: $!;"
1253
1254 \f#################################################
1255 # Cross-reference x86_64 ABI "card"
1256 #
1257 #               Unix            Win64
1258 # %rax          *               *
1259 # %rbx          -               -
1260 # %rcx          #4              #1
1261 # %rdx          #3              #2
1262 # %rsi          #2              -
1263 # %rdi          #1              -
1264 # %rbp          -               -
1265 # %rsp          -               -
1266 # %r8           #5              #3
1267 # %r9           #6              #4
1268 # %r10          *               *
1269 # %r11          *               *
1270 # %r12          -               -
1271 # %r13          -               -
1272 # %r14          -               -
1273 # %r15          -               -
1274 #
1275 # (*)   volatile register
1276 # (-)   preserved by callee
1277 # (#)   Nth argument, volatile
1278 #
1279 # In Unix terms top of stack is argument transfer area for arguments
1280 # which could not be accommodated in registers. Or in other words 7th
1281 # [integer] argument resides at 8(%rsp) upon function entry point.
1282 # 128 bytes above %rsp constitute a "red zone" which is not touched
1283 # by signal handlers and can be used as temporal storage without
1284 # allocating a frame.
1285 #
1286 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
1287 # which belongs to/can be overwritten by callee. N is the number of
1288 # arguments passed to callee, *but* not less than 4! This means that
1289 # upon function entry point 5th argument resides at 40(%rsp), as well
1290 # as that 32 bytes from 8(%rsp) can always be used as temporal
1291 # storage [without allocating a frame]. One can actually argue that
1292 # one can assume a "red zone" above stack pointer under Win64 as well.
1293 # Point is that at apparently no occasion Windows kernel would alter
1294 # the area above user stack pointer in true asynchronous manner...
1295 #
1296 # All the above means that if assembler programmer adheres to Unix
1297 # register and stack layout, but disregards the "red zone" existence,
1298 # it's possible to use following prologue and epilogue to "gear" from
1299 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
1300 #
1301 # omnipotent_function:
1302 # ifdef WIN64
1303 #       movq    %rdi,8(%rsp)
1304 #       movq    %rsi,16(%rsp)
1305 #       movq    %rcx,%rdi       ; if 1st argument is actually present
1306 #       movq    %rdx,%rsi       ; if 2nd argument is actually ...
1307 #       movq    %r8,%rdx        ; if 3rd argument is ...
1308 #       movq    %r9,%rcx        ; if 4th argument ...
1309 #       movq    40(%rsp),%r8    ; if 5th ...
1310 #       movq    48(%rsp),%r9    ; if 6th ...
1311 # endif
1312 #       ...
1313 # ifdef WIN64
1314 #       movq    8(%rsp),%rdi
1315 #       movq    16(%rsp),%rsi
1316 # endif
1317 #       ret
1318 #
1319 \f#################################################
1320 # Win64 SEH, Structured Exception Handling.
1321 #
1322 # Unlike on Unix systems(*) lack of Win64 stack unwinding information
1323 # has undesired side-effect at run-time: if an exception is raised in
1324 # assembler subroutine such as those in question (basically we're
1325 # referring to segmentation violations caused by malformed input
1326 # parameters), the application is briskly terminated without invoking
1327 # any exception handlers, most notably without generating memory dump
1328 # or any user notification whatsoever. This poses a problem. It's
1329 # possible to address it by registering custom language-specific
1330 # handler that would restore processor context to the state at
1331 # subroutine entry point and return "exception is not handled, keep
1332 # unwinding" code. Writing such handler can be a challenge... But it's
1333 # doable, though requires certain coding convention. Consider following
1334 # snippet:
1335 #
1336 # .type function,@function
1337 # function:
1338 #       movq    %rsp,%rax       # copy rsp to volatile register
1339 #       pushq   %r15            # save non-volatile registers
1340 #       pushq   %rbx
1341 #       pushq   %rbp
1342 #       movq    %rsp,%r11
1343 #       subq    %rdi,%r11       # prepare [variable] stack frame
1344 #       andq    $-64,%r11
1345 #       movq    %rax,0(%r11)    # check for exceptions
1346 #       movq    %r11,%rsp       # allocate [variable] stack frame
1347 #       movq    %rax,0(%rsp)    # save original rsp value
1348 # magic_point:
1349 #       ...
1350 #       movq    0(%rsp),%rcx    # pull original rsp value
1351 #       movq    -24(%rcx),%rbp  # restore non-volatile registers
1352 #       movq    -16(%rcx),%rbx
1353 #       movq    -8(%rcx),%r15
1354 #       movq    %rcx,%rsp       # restore original rsp
1355 # magic_epilogue:
1356 #       ret
1357 # .size function,.-function
1358 #
1359 # The key is that up to magic_point copy of original rsp value remains
1360 # in chosen volatile register and no non-volatile register, except for
1361 # rsp, is modified. While past magic_point rsp remains constant till
1362 # the very end of the function. In this case custom language-specific
1363 # exception handler would look like this:
1364 #
1365 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1366 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
1367 # {     ULONG64 *rsp = (ULONG64 *)context->Rax;
1368 #       ULONG64  rip = context->Rip;
1369 #
1370 #       if (rip >= magic_point)
1371 #       {   rsp = (ULONG64 *)context->Rsp;
1372 #           if (rip < magic_epilogue)
1373 #           {   rsp = (ULONG64 *)rsp[0];
1374 #               context->Rbp = rsp[-3];
1375 #               context->Rbx = rsp[-2];
1376 #               context->R15 = rsp[-1];
1377 #           }
1378 #       }
1379 #       context->Rsp = (ULONG64)rsp;
1380 #       context->Rdi = rsp[1];
1381 #       context->Rsi = rsp[2];
1382 #
1383 #       memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
1384 #       RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
1385 #               dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
1386 #               &disp->HandlerData,&disp->EstablisherFrame,NULL);
1387 #       return ExceptionContinueSearch;
1388 # }
1389 #
1390 # It's appropriate to implement this handler in assembler, directly in
1391 # function's module. In order to do that one has to know members'
1392 # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
1393 # values. Here they are:
1394 #
1395 #       CONTEXT.Rax                             120
1396 #       CONTEXT.Rcx                             128
1397 #       CONTEXT.Rdx                             136
1398 #       CONTEXT.Rbx                             144
1399 #       CONTEXT.Rsp                             152
1400 #       CONTEXT.Rbp                             160
1401 #       CONTEXT.Rsi                             168
1402 #       CONTEXT.Rdi                             176
1403 #       CONTEXT.R8                              184
1404 #       CONTEXT.R9                              192
1405 #       CONTEXT.R10                             200
1406 #       CONTEXT.R11                             208
1407 #       CONTEXT.R12                             216
1408 #       CONTEXT.R13                             224
1409 #       CONTEXT.R14                             232
1410 #       CONTEXT.R15                             240
1411 #       CONTEXT.Rip                             248
1412 #       CONTEXT.Xmm6                            512
1413 #       sizeof(CONTEXT)                         1232
1414 #       DISPATCHER_CONTEXT.ControlPc            0
1415 #       DISPATCHER_CONTEXT.ImageBase            8
1416 #       DISPATCHER_CONTEXT.FunctionEntry        16
1417 #       DISPATCHER_CONTEXT.EstablisherFrame     24
1418 #       DISPATCHER_CONTEXT.TargetIp             32
1419 #       DISPATCHER_CONTEXT.ContextRecord        40
1420 #       DISPATCHER_CONTEXT.LanguageHandler      48
1421 #       DISPATCHER_CONTEXT.HandlerData          56
1422 #       UNW_FLAG_NHANDLER                       0
1423 #       ExceptionContinueSearch                 1
1424 #
1425 # In order to tie the handler to the function one has to compose
1426 # couple of structures: one for .xdata segment and one for .pdata.
1427 #
1428 # UNWIND_INFO structure for .xdata segment would be
1429 #
1430 # function_unwind_info:
1431 #       .byte   9,0,0,0
1432 #       .rva    handler
1433 #
1434 # This structure designates exception handler for a function with
1435 # zero-length prologue, no stack frame or frame register.
1436 #
1437 # To facilitate composing of .pdata structures, auto-generated "gear"
1438 # prologue copies rsp value to rax and denotes next instruction with
1439 # .LSEH_begin_{function_name} label. This essentially defines the SEH
1440 # styling rule mentioned in the beginning. Position of this label is
1441 # chosen in such manner that possible exceptions raised in the "gear"
1442 # prologue would be accounted to caller and unwound from latter's frame.
1443 # End of function is marked with respective .LSEH_end_{function_name}
1444 # label. To summarize, .pdata segment would contain
1445 #
1446 #       .rva    .LSEH_begin_function
1447 #       .rva    .LSEH_end_function
1448 #       .rva    function_unwind_info
1449 #
1450 # Reference to function_unwind_info from .xdata segment is the anchor.
1451 # In case you wonder why references are 32-bit .rvas and not 64-bit
1452 # .quads. References put into these two segments are required to be
1453 # *relative* to the base address of the current binary module, a.k.a.
1454 # image base. No Win64 module, be it .exe or .dll, can be larger than
1455 # 2GB and thus such relative references can be and are accommodated in
1456 # 32 bits.
1457 #
1458 # Having reviewed the example function code, one can argue that "movq
1459 # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
1460 # rax would contain an undefined value. If this "offends" you, use
1461 # another register and refrain from modifying rax till magic_point is
1462 # reached, i.e. as if it was a non-volatile register. If more registers
1463 # are required prior [variable] frame setup is completed, note that
1464 # nobody says that you can have only one "magic point." You can
1465 # "liberate" non-volatile registers by denoting last stack off-load
1466 # instruction and reflecting it in finer grade unwind logic in handler.
1467 # After all, isn't it why it's called *language-specific* handler...
1468 #
1469 # SE handlers are also involved in unwinding stack when executable is
1470 # profiled or debugged. Profiling implies additional limitations that
1471 # are too subtle to discuss here. For now it's sufficient to say that
1472 # in order to simplify handlers one should either a) offload original
1473 # %rsp to stack (like discussed above); or b) if you have a register to
1474 # spare for frame pointer, choose volatile one.
1475 #
1476 # (*)   Note that we're talking about run-time, not debug-time. Lack of
1477 #       unwind information makes debugging hard on both Windows and
1478 #       Unix. "Unlike" refers to the fact that on Unix signal handler
1479 #       will always be invoked, core dumped and appropriate exit code
1480 #       returned to parent (for user notification).