66cfaca0e009193c6c2fdb9844761c16f58156b3
[openssl.git] / crypto / perlasm / x86_64-xlate.pl
1 #! /usr/bin/env perl
2 # Copyright 2005-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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. To support legacy code a synthetic directive, .picmeup,
55 #    is implemented. It puts address of the *next* instruction into
56 #    target register, e.g.:
57 #
58 #               .picmeup        %rax
59 #               lea             .Label-.(%rax),%rax
60 #
61 # 8. In order to provide for structured exception handling unified
62 #    Win64 prologue copies %rsp value to %rax. For further details
63 #    see SEH paragraph at the end.
64 # 9. .init segment is allowed to contain calls to functions only.
65 # a. If function accepts more than 4 arguments *and* >4th argument
66 #    is declared as non 64-bit value, do clear its upper part.
67 \f
68
69 use strict;
70
71 my $flavour = shift;
72 my $output  = shift;
73 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
74
75 open STDOUT,">$output" || die "can't open $output: $!"
76         if (defined($output));
77
78 my $gas=1;      $gas=0 if ($output =~ /\.asm$/);
79 my $elf=1;      $elf=0 if (!$gas);
80 my $win64=0;
81 my $prefix="";
82 my $decor=".L";
83
84 my $masmref=8 + 50727*2**-32;   # 8.00.50727 shipped with VS2005
85 my $masm=0;
86 my $PTR=" PTR";
87
88 my $nasmref=2.03;
89 my $nasm=0;
90
91 if    ($flavour eq "mingw64")   { $gas=1; $elf=0; $win64=1;
92                                   $prefix=`echo __USER_LABEL_PREFIX__ | $ENV{CC} -E -P -`;
93                                   $prefix =~ s|\R$||; # Better chomp
94                                 }
95 elsif ($flavour eq "macosx")    { $gas=1; $elf=0; $prefix="_"; $decor="L\$"; }
96 elsif ($flavour eq "masm")      { $gas=0; $elf=0; $masm=$masmref; $win64=1; $decor="\$L\$"; }
97 elsif ($flavour eq "nasm")      { $gas=0; $elf=0; $nasm=$nasmref; $win64=1; $decor="\$L\$"; $PTR=""; }
98 elsif (!$gas)
99 {   if ($ENV{ASM} =~ m/nasm/ && `nasm -v` =~ m/version ([0-9]+)\.([0-9]+)/i)
100     {   $nasm = $1 + $2*0.01; $PTR="";  }
101     elsif (`ml64 2>&1` =~ m/Version ([0-9]+)\.([0-9]+)(\.([0-9]+))?/)
102     {   $masm = $1 + $2*2**-16 + $4*2**-32;   }
103     die "no assembler found on %PATH%" if (!($nasm || $masm));
104     $win64=1;
105     $elf=0;
106     $decor="\$L\$";
107 }
108
109 my $current_segment;
110 my $current_function;
111 my %globals;
112
113 { package opcode;       # pick up opcodes
114     sub re {
115         my      ($class, $line) = @_;
116         my      $self = {};
117         my      $ret;
118
119         if ($$line =~ /^([a-z][a-z0-9]*)/i) {
120             bless $self,$class;
121             $self->{op} = $1;
122             $ret = $self;
123             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
124
125             undef $self->{sz};
126             if ($self->{op} =~ /^(movz)x?([bw]).*/) {   # movz is pain...
127                 $self->{op} = $1;
128                 $self->{sz} = $2;
129             } elsif ($self->{op} =~ /call|jmp/) {
130                 $self->{sz} = "";
131             } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn
132                 $self->{sz} = "";
133             } elsif ($self->{op} =~ /^[vk]/) { # VEX or k* such as kmov
134                 $self->{sz} = "";
135             } elsif ($self->{op} =~ /mov[dq]/ && $$line =~ /%xmm/) {
136                 $self->{sz} = "";
137             } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
138                 $self->{op} = $1;
139                 $self->{sz} = $2;
140             }
141         }
142         $ret;
143     }
144     sub size {
145         my ($self, $sz) = @_;
146         $self->{sz} = $sz if (defined($sz) && !defined($self->{sz}));
147         $self->{sz};
148     }
149     sub out {
150         my $self = shift;
151         if ($gas) {
152             if ($self->{op} eq "movz") {        # movz is pain...
153                 sprintf "%s%s%s",$self->{op},$self->{sz},shift;
154             } elsif ($self->{op} =~ /^set/) {
155                 "$self->{op}";
156             } elsif ($self->{op} eq "ret") {
157                 my $epilogue = "";
158                 if ($win64 && $current_function->{abi} eq "svr4") {
159                     $epilogue = "movq   8(%rsp),%rdi\n\t" .
160                                 "movq   16(%rsp),%rsi\n\t";
161                 }
162                 $epilogue . ".byte      0xf3,0xc3";
163             } elsif ($self->{op} eq "call" && !$elf && $current_segment eq ".init") {
164                 ".p2align\t3\n\t.quad";
165             } else {
166                 "$self->{op}$self->{sz}";
167             }
168         } else {
169             $self->{op} =~ s/^movz/movzx/;
170             if ($self->{op} eq "ret") {
171                 $self->{op} = "";
172                 if ($win64 && $current_function->{abi} eq "svr4") {
173                     $self->{op} = "mov  rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
174                                   "mov  rsi,QWORD$PTR\[16+rsp\]\n\t";
175                 }
176                 $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
177             } elsif ($self->{op} =~ /^(pop|push)f/) {
178                 $self->{op} .= $self->{sz};
179             } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
180                 $self->{op} = "\tDQ";
181             }
182             $self->{op};
183         }
184     }
185     sub mnemonic {
186         my ($self, $op) = @_;
187         $self->{op}=$op if (defined($op));
188         $self->{op};
189     }
190 }
191 { package const;        # pick up constants, which start with $
192     sub re {
193         my      ($class, $line) = @_;
194         my      $self = {};
195         my      $ret;
196
197         if ($$line =~ /^\$([^,]+)/) {
198             bless $self, $class;
199             $self->{value} = $1;
200             $ret = $self;
201             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
202         }
203         $ret;
204     }
205     sub out {
206         my $self = shift;
207
208         $self->{value} =~ s/\b(0b[0-1]+)/oct($1)/eig;
209         if ($gas) {
210             # Solaris /usr/ccs/bin/as can't handle multiplications
211             # in $self->{value}
212             my $value = $self->{value};
213             no warnings;    # oct might complain about overflow, ignore here...
214             $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
215             if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
216                 $self->{value} = $value;
217             }
218             sprintf "\$%s",$self->{value};
219         } else {
220             $self->{value} =~ s/0x([0-9a-f]+)/0$1h/ig if ($masm);
221             sprintf "%s",$self->{value};
222         }
223     }
224 }
225 { package ea;           # pick up effective addresses: expr(%reg,%reg,scale)
226
227     my %szmap = (       b=>"BYTE$PTR",    w=>"WORD$PTR",
228                         l=>"DWORD$PTR",   d=>"DWORD$PTR",
229                         q=>"QWORD$PTR",   o=>"OWORD$PTR",
230                         x=>"XMMWORD$PTR", y=>"YMMWORD$PTR",
231                         z=>"ZMMWORD$PTR" ) if (!$gas);
232
233     sub re {
234         my      ($class, $line, $opcode) = @_;
235         my      $self = {};
236         my      $ret;
237
238         # optional * ----vvv--- appears in indirect jmp/call
239         if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)((?:{[^}]+})*)/) {
240             bless $self, $class;
241             $self->{asterisk} = $1;
242             $self->{label} = $2;
243             ($self->{base},$self->{index},$self->{scale})=split(/,/,$3);
244             $self->{scale} = 1 if (!defined($self->{scale}));
245             $self->{opmask} = $4;
246             $ret = $self;
247             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
248
249             if ($win64 && $self->{label} =~ s/\@GOTPCREL//) {
250                 die if ($opcode->mnemonic() ne "mov");
251                 $opcode->mnemonic("lea");
252             }
253             $self->{base}  =~ s/^%//;
254             $self->{index} =~ s/^%// if (defined($self->{index}));
255             $self->{opcode} = $opcode;
256         }
257         $ret;
258     }
259     sub size {}
260     sub out {
261         my ($self, $sz) = @_;
262
263         $self->{label} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
264         $self->{label} =~ s/\.L/$decor/g;
265
266         # Silently convert all EAs to 64-bit. This is required for
267         # elder GNU assembler and results in more compact code,
268         # *but* most importantly AES module depends on this feature!
269         $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
270         $self->{base}  =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
271
272         # Solaris /usr/ccs/bin/as can't handle multiplications
273         # in $self->{label}...
274         use integer;
275         $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
276         $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
277
278         # Some assemblers insist on signed presentation of 32-bit
279         # offsets, but sign extension is a tricky business in perl...
280         if ((1<<31)<<1) {
281             $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
282         } else {
283             $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
284         }
285
286         # if base register is %rbp or %r13, see if it's possible to
287         # flip base and ingex registers [for better performance]
288         if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
289             $self->{base} =~ /(rbp|r13)/) {
290                 $self->{base} = $self->{index}; $self->{index} = $1;
291         }
292
293         if ($gas) {
294             $self->{label} =~ s/^___imp_/__imp__/   if ($flavour eq "mingw64");
295
296             if (defined($self->{index})) {
297                 sprintf "%s%s(%s,%%%s,%d)%s",
298                                         $self->{asterisk},$self->{label},
299                                         $self->{base}?"%$self->{base}":"",
300                                         $self->{index},$self->{scale},
301                                         $self->{opmask};
302             } else {
303                 sprintf "%s%s(%%%s)%s", $self->{asterisk},$self->{label},
304                                         $self->{base},$self->{opmask};
305             }
306         } else {
307             $self->{label} =~ s/\./\$/g;
308             $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
309             $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
310
311             my $mnemonic = $self->{opcode}->mnemonic();
312             ($self->{asterisk})                         && ($sz="q") ||
313             ($mnemonic =~ /^v?mov([qd])$/)              && ($sz=$1)  ||
314             ($mnemonic =~ /^v?pinsr([qdwb])$/)          && ($sz=$1)  ||
315             ($mnemonic =~ /^vpbroadcast([qdwb])$/)      && ($sz=$1)  ||
316             ($mnemonic =~ /^v(?!perm)[a-z]+[fi]128$/)   && ($sz="x");
317
318             $self->{opmask}  =~ s/%(k[0-7])/$1/;
319
320             if (defined($self->{index})) {
321                 sprintf "%s[%s%s*%d%s]%s",$szmap{$sz},
322                                         $self->{label}?"$self->{label}+":"",
323                                         $self->{index},$self->{scale},
324                                         $self->{base}?"+$self->{base}":"",
325                                         $self->{opmask};
326             } elsif ($self->{base} eq "rip") {
327                 sprintf "%s[%s]",$szmap{$sz},$self->{label};
328             } else {
329                 sprintf "%s[%s%s]%s",   $szmap{$sz},
330                                         $self->{label}?"$self->{label}+":"",
331                                         $self->{base},$self->{opmask};
332             }
333         }
334     }
335 }
336 { package register;     # pick up registers, which start with %.
337     sub re {
338         my      ($class, $line, $opcode) = @_;
339         my      $self = {};
340         my      $ret;
341
342         # optional * ----vvv--- appears in indirect jmp/call
343         if ($$line =~ /^(\*?)%(\w+)((?:{[^}]+})*)/) {
344             bless $self,$class;
345             $self->{asterisk} = $1;
346             $self->{value} = $2;
347             $self->{opmask} = $3;
348             $opcode->size($self->size());
349             $ret = $self;
350             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
351         }
352         $ret;
353     }
354     sub size {
355         my      $self = shift;
356         my      $ret;
357
358         if    ($self->{value} =~ /^r[\d]+b$/i)  { $ret="b"; }
359         elsif ($self->{value} =~ /^r[\d]+w$/i)  { $ret="w"; }
360         elsif ($self->{value} =~ /^r[\d]+d$/i)  { $ret="l"; }
361         elsif ($self->{value} =~ /^r[\w]+$/i)   { $ret="q"; }
362         elsif ($self->{value} =~ /^[a-d][hl]$/i){ $ret="b"; }
363         elsif ($self->{value} =~ /^[\w]{2}l$/i) { $ret="b"; }
364         elsif ($self->{value} =~ /^[\w]{2}$/i)  { $ret="w"; }
365         elsif ($self->{value} =~ /^e[a-z]{2}$/i){ $ret="l"; }
366
367         $ret;
368     }
369     sub out {
370         my $self = shift;
371         if ($gas)       { sprintf "%s%%%s%s",   $self->{asterisk},
372                                                 $self->{value},
373                                                 $self->{opmask}; }
374         else            { $self->{opmask} =~ s/%(k[0-7])/$1/;
375                           $self->{value}.$self->{opmask}; }
376     }
377 }
378 { package label;        # pick up labels, which end with :
379     sub re {
380         my      ($class, $line) = @_;
381         my      $self = {};
382         my      $ret;
383
384         if ($$line =~ /(^[\.\w]+)\:/) {
385             bless $self,$class;
386             $self->{value} = $1;
387             $ret = $self;
388             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
389
390             $self->{value} =~ s/^\.L/$decor/;
391         }
392         $ret;
393     }
394     sub out {
395         my $self = shift;
396
397         if ($gas) {
398             my $func = ($globals{$self->{value}} or $self->{value}) . ":";
399             if ($win64  && $current_function->{name} eq $self->{value}
400                         && $current_function->{abi} eq "svr4") {
401                 $func .= "\n";
402                 $func .= "      movq    %rdi,8(%rsp)\n";
403                 $func .= "      movq    %rsi,16(%rsp)\n";
404                 $func .= "      movq    %rsp,%rax\n";
405                 $func .= "${decor}SEH_begin_$current_function->{name}:\n";
406                 my $narg = $current_function->{narg};
407                 $narg=6 if (!defined($narg));
408                 $func .= "      movq    %rcx,%rdi\n" if ($narg>0);
409                 $func .= "      movq    %rdx,%rsi\n" if ($narg>1);
410                 $func .= "      movq    %r8,%rdx\n"  if ($narg>2);
411                 $func .= "      movq    %r9,%rcx\n"  if ($narg>3);
412                 $func .= "      movq    40(%rsp),%r8\n" if ($narg>4);
413                 $func .= "      movq    48(%rsp),%r9\n" if ($narg>5);
414             }
415             $func;
416         } elsif ($self->{value} ne "$current_function->{name}") {
417             # Make all labels in masm global.
418             $self->{value} .= ":" if ($masm);
419             $self->{value} . ":";
420         } elsif ($win64 && $current_function->{abi} eq "svr4") {
421             my $func =  "$current_function->{name}" .
422                         ($nasm ? ":" : "\tPROC $current_function->{scope}") .
423                         "\n";
424             $func .= "  mov     QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
425             $func .= "  mov     QWORD$PTR\[16+rsp\],rsi\n";
426             $func .= "  mov     rax,rsp\n";
427             $func .= "${decor}SEH_begin_$current_function->{name}:";
428             $func .= ":" if ($masm);
429             $func .= "\n";
430             my $narg = $current_function->{narg};
431             $narg=6 if (!defined($narg));
432             $func .= "  mov     rdi,rcx\n" if ($narg>0);
433             $func .= "  mov     rsi,rdx\n" if ($narg>1);
434             $func .= "  mov     rdx,r8\n"  if ($narg>2);
435             $func .= "  mov     rcx,r9\n"  if ($narg>3);
436             $func .= "  mov     r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
437             $func .= "  mov     r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
438             $func .= "\n";
439         } else {
440            "$current_function->{name}".
441                         ($nasm ? ":" : "\tPROC $current_function->{scope}");
442         }
443     }
444 }
445 { package expr;         # pick up expressioins
446     sub re {
447         my      ($class, $line, $opcode) = @_;
448         my      $self = {};
449         my      $ret;
450
451         if ($$line =~ /(^[^,]+)/) {
452             bless $self,$class;
453             $self->{value} = $1;
454             $ret = $self;
455             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
456
457             $self->{value} =~ s/\@PLT// if (!$elf);
458             $self->{value} =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
459             $self->{value} =~ s/\.L/$decor/g;
460             $self->{opcode} = $opcode;
461         }
462         $ret;
463     }
464     sub out {
465         my $self = shift;
466         if ($nasm && $self->{opcode}->mnemonic()=~m/^j(?![re]cxz)/) {
467             "NEAR ".$self->{value};
468         } else {
469             $self->{value};
470         }
471     }
472 }
473 { package directive;    # pick up directives, which start with .
474     sub re {
475         my      ($class, $line) = @_;
476         my      $self = {};
477         my      $ret;
478         my      $dir;
479
480         if ($$line =~ /^\s*(\.\w+)/) {
481             bless $self,$class;
482             $dir = $1;
483             $ret = $self;
484             undef $self->{value};
485             $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
486
487             SWITCH: for ($dir) {
488                 # obsolete, to be removed
489                 /\.picmeup/ && do { if ($$line =~ /(%r[\w]+)/i) {
490                                         my %opcode = # lea 2f-1f(%rip),%dst; 1: nop; 2:
491                                                    ( "%rax"=>0x01058d48, "%rcx"=>0x010d8d48,
492                                                      "%rdx"=>0x01158d48, "%rbx"=>0x011d8d48,
493                                                      "%rsp"=>0x01258d48, "%rbp"=>0x012d8d48,
494                                                      "%rsi"=>0x01358d48, "%rdi"=>0x013d8d48,
495                                                      "%r8" =>0x01058d4c, "%r9" =>0x010d8d4c,
496                                                      "%r10"=>0x01158d4c, "%r11"=>0x011d8d4c,
497                                                      "%r12"=>0x01258d4c, "%r13"=>0x012d8d4c,
498                                                      "%r14"=>0x01358d4c, "%r15"=>0x013d8d4c);
499
500
501                                         $dir="\t.long";
502                                         $$line=sprintf "0x%x,0x90000000",$opcode{$1};
503                                     }
504                                     last;
505                                   };
506                 /\.global|\.globl|\.extern/
507                             && do { $globals{$$line} = $prefix . $$line;
508                                     $$line = $globals{$$line} if ($prefix);
509                                     last;
510                                   };
511                 /\.type/    && do { my ($sym,$type,$narg) = split(',',$$line);
512                                     if ($type eq "\@function") {
513                                         undef $current_function;
514                                         $current_function->{name} = $sym;
515                                         $current_function->{abi}  = "svr4";
516                                         $current_function->{narg} = $narg;
517                                         $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
518                                     } elsif ($type eq "\@abi-omnipotent") {
519                                         undef $current_function;
520                                         $current_function->{name} = $sym;
521                                         $current_function->{scope} = defined($globals{$sym})?"PUBLIC":"PRIVATE";
522                                     }
523                                     $$line =~ s/\@abi\-omnipotent/\@function/;
524                                     $$line =~ s/\@function.*/\@function/;
525                                     last;
526                                   };
527                 /\.asciz/   && do { if ($$line =~ /^"(.*)"$/) {
528                                         $dir  = ".byte";
529                                         $$line = join(",",unpack("C*",$1),0);
530                                     }
531                                     last;
532                                   };
533                 /\.rva|\.long|\.quad/
534                             && do { $$line =~ s/([_a-z][_a-z0-9]*)/$globals{$1} or $1/gei;
535                                     $$line =~ s/\.L/$decor/g;
536                                     last;
537                                   };
538             }
539
540             if ($gas) {
541                 $self->{value} = $dir . "\t" . $$line;
542
543                 if ($dir =~ /\.extern/) {
544                     $self->{value} = ""; # swallow extern
545                 } elsif (!$elf && $dir =~ /\.type/) {
546                     $self->{value} = "";
547                     $self->{value} = ".def\t" . ($globals{$1} or $1) . ";\t" .
548                                 (defined($globals{$1})?".scl 2;":".scl 3;") .
549                                 "\t.type 32;\t.endef"
550                                 if ($win64 && $$line =~ /([^,]+),\@function/);
551                 } elsif (!$elf && $dir =~ /\.size/) {
552                     $self->{value} = "";
553                     if (defined($current_function)) {
554                         $self->{value} .= "${decor}SEH_end_$current_function->{name}:"
555                                 if ($win64 && $current_function->{abi} eq "svr4");
556                         undef $current_function;
557                     }
558                 } elsif (!$elf && $dir =~ /\.align/) {
559                     $self->{value} = ".p2align\t" . (log($$line)/log(2));
560                 } elsif ($dir eq ".section") {
561                     $current_segment=$$line;
562                     if (!$elf && $current_segment eq ".init") {
563                         if      ($flavour eq "macosx")  { $self->{value} = ".mod_init_func"; }
564                         elsif   ($flavour eq "mingw64") { $self->{value} = ".section\t.ctors"; }
565                     }
566                 } elsif ($dir =~ /\.(text|data)/) {
567                     $current_segment=".$1";
568                 } elsif ($dir =~ /\.hidden/) {
569                     if    ($flavour eq "macosx")  { $self->{value} = ".private_extern\t$prefix$$line"; }
570                     elsif ($flavour eq "mingw64") { $self->{value} = ""; }
571                 } elsif ($dir =~ /\.comm/) {
572                     $self->{value} = "$dir\t$prefix$$line";
573                     $self->{value} =~ s|,([0-9]+),([0-9]+)$|",$1,".log($2)/log(2)|e if ($flavour eq "macosx");
574                 }
575                 $$line = "";
576                 return $self;
577             }
578
579             # non-gas case or nasm/masm
580             SWITCH: for ($dir) {
581                 /\.text/    && do { my $v=undef;
582                                     if ($nasm) {
583                                         $v="section     .text code align=64\n";
584                                     } else {
585                                         $v="$current_segment\tENDS\n" if ($current_segment);
586                                         $current_segment = ".text\$";
587                                         $v.="$current_segment\tSEGMENT ";
588                                         $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
589                                         $v.=" 'CODE'";
590                                     }
591                                     $self->{value} = $v;
592                                     last;
593                                   };
594                 /\.data/    && do { my $v=undef;
595                                     if ($nasm) {
596                                         $v="section     .data data align=8\n";
597                                     } else {
598                                         $v="$current_segment\tENDS\n" if ($current_segment);
599                                         $current_segment = "_DATA";
600                                         $v.="$current_segment\tSEGMENT";
601                                     }
602                                     $self->{value} = $v;
603                                     last;
604                                   };
605                 /\.section/ && do { my $v=undef;
606                                     $$line =~ s/([^,]*).*/$1/;
607                                     $$line = ".CRT\$XCU" if ($$line eq ".init");
608                                     if ($nasm) {
609                                         $v="section     $$line";
610                                         if ($$line=~/\.([px])data/) {
611                                             $v.=" rdata align=";
612                                             $v.=$1 eq "p"? 4 : 8;
613                                         } elsif ($$line=~/\.CRT\$/i) {
614                                             $v.=" rdata align=8";
615                                         }
616                                     } else {
617                                         $v="$current_segment\tENDS\n" if ($current_segment);
618                                         $v.="$$line\tSEGMENT";
619                                         if ($$line=~/\.([px])data/) {
620                                             $v.=" READONLY";
621                                             $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref);
622                                         } elsif ($$line=~/\.CRT\$/i) {
623                                             $v.=" READONLY ";
624                                             $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD";
625                                         }
626                                     }
627                                     $current_segment = $$line;
628                                     $self->{value} = $v;
629                                     last;
630                                   };
631                 /\.extern/  && do { $self->{value}  = "EXTERN\t".$$line;
632                                     $self->{value} .= ":NEAR" if ($masm);
633                                     last;
634                                   };
635                 /\.globl|.global/
636                             && do { $self->{value}  = $masm?"PUBLIC":"global";
637                                     $self->{value} .= "\t".$$line;
638                                     last;
639                                   };
640                 /\.size/    && do { if (defined($current_function)) {
641                                         undef $self->{value};
642                                         if ($current_function->{abi} eq "svr4") {
643                                             $self->{value}="${decor}SEH_end_$current_function->{name}:";
644                                             $self->{value}.=":\n" if($masm);
645                                         }
646                                         $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name});
647                                         undef $current_function;
648                                     }
649                                     last;
650                                   };
651                 /\.align/   && do { my $max = ($masm && $masm>=$masmref) ? 256 : 4096;
652                                     $self->{value} = "ALIGN\t".($$line>$max?$max:$$line);
653                                     last;
654                                   };
655                 /\.(value|long|rva|quad)/
656                             && do { my $sz  = substr($1,0,1);
657                                     my @arr = split(/,\s*/,$$line);
658                                     my $last = pop(@arr);
659                                     my $conv = sub  {   my $var=shift;
660                                                         $var=~s/^(0b[0-1]+)/oct($1)/eig;
661                                                         $var=~s/^0x([0-9a-f]+)/0$1h/ig if ($masm);
662                                                         if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
663                                                         { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
664                                                         $var;
665                                                     };
666
667                                     $sz =~ tr/bvlrq/BWDDQ/;
668                                     $self->{value} = "\tD$sz\t";
669                                     for (@arr) { $self->{value} .= &$conv($_).","; }
670                                     $self->{value} .= &$conv($last);
671                                     last;
672                                   };
673                 /\.byte/    && do { my @str=split(/,\s*/,$$line);
674                                     map(s/(0b[0-1]+)/oct($1)/eig,@str);
675                                     map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
676                                     while ($#str>15) {
677                                         $self->{value}.="DB\t"
678                                                 .join(",",@str[0..15])."\n";
679                                         foreach (0..15) { shift @str; }
680                                     }
681                                     $self->{value}.="DB\t"
682                                                 .join(",",@str) if (@str);
683                                     last;
684                                   };
685                 /\.comm/    && do { my @str=split(/,\s*/,$$line);
686                                     my $v=undef;
687                                     if ($nasm) {
688                                         $v.="common     $prefix@str[0] @str[1]";
689                                     } else {
690                                         $v="$current_segment\tENDS\n" if ($current_segment);
691                                         $current_segment = "_DATA";
692                                         $v.="$current_segment\tSEGMENT\n";
693                                         $v.="COMM       @str[0]:DWORD:".@str[1]/4;
694                                     }
695                                     $self->{value} = $v;
696                                     last;
697                                   };
698             }
699             $$line = "";
700         }
701
702         $ret;
703     }
704     sub out {
705         my $self = shift;
706         $self->{value};
707     }
708 }
709
710 # Upon initial x86_64 introduction SSE>2 extensions were not introduced
711 # yet. In order not to be bothered by tracing exact assembler versions,
712 # but at the same time to provide a bare security minimum of AES-NI, we
713 # hard-code some instructions. Extensions past AES-NI on the other hand
714 # are traced by examining assembler version in individual perlasm
715 # modules...
716
717 my %regrm = (   "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
718                 "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7      );
719
720 sub rex {
721  my $opcode=shift;
722  my ($dst,$src,$rex)=@_;
723
724    $rex|=0x04 if($dst>=8);
725    $rex|=0x01 if($src>=8);
726    push @$opcode,($rex|0x40) if ($rex);
727 }
728
729 my $movq = sub {        # elderly gas can't handle inter-register movq
730   my $arg = shift;
731   my @opcode=(0x66);
732     if ($arg =~ /%xmm([0-9]+),\s*%r(\w+)/) {
733         my ($src,$dst)=($1,$2);
734         if ($dst !~ /[0-9]+/)   { $dst = $regrm{"%e$dst"}; }
735         rex(\@opcode,$src,$dst,0x8);
736         push @opcode,0x0f,0x7e;
737         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
738         @opcode;
739     } elsif ($arg =~ /%r(\w+),\s*%xmm([0-9]+)/) {
740         my ($src,$dst)=($2,$1);
741         if ($dst !~ /[0-9]+/)   { $dst = $regrm{"%e$dst"}; }
742         rex(\@opcode,$src,$dst,0x8);
743         push @opcode,0x0f,0x6e;
744         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
745         @opcode;
746     } else {
747         ();
748     }
749 };
750
751 my $pextrd = sub {
752     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*(%\w+)/) {
753       my @opcode=(0x66);
754         my $imm=$1;
755         my $src=$2;
756         my $dst=$3;
757         if ($dst =~ /%r([0-9]+)d/)      { $dst = $1; }
758         elsif ($dst =~ /%e/)            { $dst = $regrm{$dst}; }
759         rex(\@opcode,$src,$dst);
760         push @opcode,0x0f,0x3a,0x16;
761         push @opcode,0xc0|(($src&7)<<3)|($dst&7);       # ModR/M
762         push @opcode,$imm;
763         @opcode;
764     } else {
765         ();
766     }
767 };
768
769 my $pinsrd = sub {
770     if (shift =~ /\$([0-9]+),\s*(%\w+),\s*%xmm([0-9]+)/) {
771       my @opcode=(0x66);
772         my $imm=$1;
773         my $src=$2;
774         my $dst=$3;
775         if ($src =~ /%r([0-9]+)/)       { $src = $1; }
776         elsif ($src =~ /%e/)            { $src = $regrm{$src}; }
777         rex(\@opcode,$dst,$src);
778         push @opcode,0x0f,0x3a,0x22;
779         push @opcode,0xc0|(($dst&7)<<3)|($src&7);       # ModR/M
780         push @opcode,$imm;
781         @opcode;
782     } else {
783         ();
784     }
785 };
786
787 my $pshufb = sub {
788     if (shift =~ /%xmm([0-9]+),\s*%xmm([0-9]+)/) {
789       my @opcode=(0x66);
790         rex(\@opcode,$2,$1);
791         push @opcode,0x0f,0x38,0x00;
792         push @opcode,0xc0|($1&7)|(($2&7)<<3);           # ModR/M
793         @opcode;
794     } else {
795         ();
796     }
797 };
798
799 my $palignr = sub {
800     if (shift =~ /\$([0-9]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
801       my @opcode=(0x66);
802         rex(\@opcode,$3,$2);
803         push @opcode,0x0f,0x3a,0x0f;
804         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
805         push @opcode,$1;
806         @opcode;
807     } else {
808         ();
809     }
810 };
811
812 my $pclmulqdq = sub {
813     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
814       my @opcode=(0x66);
815         rex(\@opcode,$3,$2);
816         push @opcode,0x0f,0x3a,0x44;
817         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
818         my $c=$1;
819         push @opcode,$c=~/^0/?oct($c):$c;
820         @opcode;
821     } else {
822         ();
823     }
824 };
825
826 my $rdrand = sub {
827     if (shift =~ /%[er](\w+)/) {
828       my @opcode=();
829       my $dst=$1;
830         if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
831         rex(\@opcode,0,$dst,8);
832         push @opcode,0x0f,0xc7,0xf0|($dst&7);
833         @opcode;
834     } else {
835         ();
836     }
837 };
838
839 my $rdseed = sub {
840     if (shift =~ /%[er](\w+)/) {
841       my @opcode=();
842       my $dst=$1;
843         if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
844         rex(\@opcode,0,$dst,8);
845         push @opcode,0x0f,0xc7,0xf8|($dst&7);
846         @opcode;
847     } else {
848         ();
849     }
850 };
851
852 # Not all AVX-capable assemblers recognize AMD XOP extension. Since we
853 # are using only two instructions hand-code them in order to be excused
854 # from chasing assembler versions...
855
856 sub rxb {
857  my $opcode=shift;
858  my ($dst,$src1,$src2,$rxb)=@_;
859
860    $rxb|=0x7<<5;
861    $rxb&=~(0x04<<5) if($dst>=8);
862    $rxb&=~(0x01<<5) if($src1>=8);
863    $rxb&=~(0x02<<5) if($src2>=8);
864    push @$opcode,$rxb;
865 }
866
867 my $vprotd = sub {
868     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
869       my @opcode=(0x8f);
870         rxb(\@opcode,$3,$2,-1,0x08);
871         push @opcode,0x78,0xc2;
872         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
873         my $c=$1;
874         push @opcode,$c=~/^0/?oct($c):$c;
875         @opcode;
876     } else {
877         ();
878     }
879 };
880
881 my $vprotq = sub {
882     if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
883       my @opcode=(0x8f);
884         rxb(\@opcode,$3,$2,-1,0x08);
885         push @opcode,0x78,0xc3;
886         push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
887         my $c=$1;
888         push @opcode,$c=~/^0/?oct($c):$c;
889         @opcode;
890     } else {
891         ();
892     }
893 };
894
895 # Intel Control-flow Enforcement Technology extension. All functions and
896 # indirect branch targets will have to start with this instruction...
897
898 my $endbranch = sub {
899     (0xf3,0x0f,0x1e,0xfa);
900 };
901
902 ########################################################################
903
904 if ($nasm) {
905     print <<___;
906 default rel
907 %define XMMWORD
908 %define YMMWORD
909 %define ZMMWORD
910 ___
911 } elsif ($masm) {
912     print <<___;
913 OPTION  DOTNAME
914 ___
915 }
916 while(defined(my $line=<>)) {
917
918     $line =~ s|\R$||;           # Better chomp
919
920     $line =~ s|[#!].*$||;       # get rid of asm-style comments...
921     $line =~ s|/\*.*\*/||;      # ... and C-style comments...
922     $line =~ s|^\s+||;          # ... and skip white spaces in beginning
923     $line =~ s|\s+$||;          # ... and at the end
924
925     if (my $label=label->re(\$line))    { print $label->out(); }
926
927     if (my $directive=directive->re(\$line)) {
928         printf "%s",$directive->out();
929     } elsif (my $opcode=opcode->re(\$line)) {
930         my $asm = eval("\$".$opcode->mnemonic());
931
932         if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
933             print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
934             next;
935         }
936
937         my @args;
938         ARGUMENT: while (1) {
939             my $arg;
940
941             ($arg=register->re(\$line, $opcode))||
942             ($arg=const->re(\$line))            ||
943             ($arg=ea->re(\$line, $opcode))      ||
944             ($arg=expr->re(\$line, $opcode))    ||
945             last ARGUMENT;
946
947             push @args,$arg;
948
949             last ARGUMENT if ($line !~ /^,/);
950
951             $line =~ s/^,\s*//;
952         } # ARGUMENT:
953
954         if ($#args>=0) {
955             my $insn;
956             my $sz=$opcode->size();
957
958             if ($gas) {
959                 $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz);
960                 @args = map($_->out($sz),@args);
961                 printf "\t%s\t%s",$insn,join(",",@args);
962             } else {
963                 $insn = $opcode->out();
964                 foreach (@args) {
965                     my $arg = $_->out();
966                     # $insn.=$sz compensates for movq, pinsrw, ...
967                     if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
968                     if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
969                     if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
970                     if ($arg =~ /^mm[0-9]+$/)  { $insn.=$sz; $sz="q" if(!$sz); last; }
971                 }
972                 @args = reverse(@args);
973                 undef $sz if ($nasm && $opcode->mnemonic() eq "lea");
974                 printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args));
975             }
976         } else {
977             printf "\t%s",$opcode->out();
978         }
979     }
980
981     print $line,"\n";
982 }
983
984 print "\n$current_segment\tENDS\n"      if ($current_segment && $masm);
985 print "END\n"                           if ($masm);
986
987 close STDOUT;
988
989 \f#################################################
990 # Cross-reference x86_64 ABI "card"
991 #
992 #               Unix            Win64
993 # %rax          *               *
994 # %rbx          -               -
995 # %rcx          #4              #1
996 # %rdx          #3              #2
997 # %rsi          #2              -
998 # %rdi          #1              -
999 # %rbp          -               -
1000 # %rsp          -               -
1001 # %r8           #5              #3
1002 # %r9           #6              #4
1003 # %r10          *               *
1004 # %r11          *               *
1005 # %r12          -               -
1006 # %r13          -               -
1007 # %r14          -               -
1008 # %r15          -               -
1009 #
1010 # (*)   volatile register
1011 # (-)   preserved by callee
1012 # (#)   Nth argument, volatile
1013 #
1014 # In Unix terms top of stack is argument transfer area for arguments
1015 # which could not be accommodated in registers. Or in other words 7th
1016 # [integer] argument resides at 8(%rsp) upon function entry point.
1017 # 128 bytes above %rsp constitute a "red zone" which is not touched
1018 # by signal handlers and can be used as temporal storage without
1019 # allocating a frame.
1020 #
1021 # In Win64 terms N*8 bytes on top of stack is argument transfer area,
1022 # which belongs to/can be overwritten by callee. N is the number of
1023 # arguments passed to callee, *but* not less than 4! This means that
1024 # upon function entry point 5th argument resides at 40(%rsp), as well
1025 # as that 32 bytes from 8(%rsp) can always be used as temporal
1026 # storage [without allocating a frame]. One can actually argue that
1027 # one can assume a "red zone" above stack pointer under Win64 as well.
1028 # Point is that at apparently no occasion Windows kernel would alter
1029 # the area above user stack pointer in true asynchronous manner...
1030 #
1031 # All the above means that if assembler programmer adheres to Unix
1032 # register and stack layout, but disregards the "red zone" existense,
1033 # it's possible to use following prologue and epilogue to "gear" from
1034 # Unix to Win64 ABI in leaf functions with not more than 6 arguments.
1035 #
1036 # omnipotent_function:
1037 # ifdef WIN64
1038 #       movq    %rdi,8(%rsp)
1039 #       movq    %rsi,16(%rsp)
1040 #       movq    %rcx,%rdi       ; if 1st argument is actually present
1041 #       movq    %rdx,%rsi       ; if 2nd argument is actually ...
1042 #       movq    %r8,%rdx        ; if 3rd argument is ...
1043 #       movq    %r9,%rcx        ; if 4th argument ...
1044 #       movq    40(%rsp),%r8    ; if 5th ...
1045 #       movq    48(%rsp),%r9    ; if 6th ...
1046 # endif
1047 #       ...
1048 # ifdef WIN64
1049 #       movq    8(%rsp),%rdi
1050 #       movq    16(%rsp),%rsi
1051 # endif
1052 #       ret
1053 #
1054 \f#################################################
1055 # Win64 SEH, Structured Exception Handling.
1056 #
1057 # Unlike on Unix systems(*) lack of Win64 stack unwinding information
1058 # has undesired side-effect at run-time: if an exception is raised in
1059 # assembler subroutine such as those in question (basically we're
1060 # referring to segmentation violations caused by malformed input
1061 # parameters), the application is briskly terminated without invoking
1062 # any exception handlers, most notably without generating memory dump
1063 # or any user notification whatsoever. This poses a problem. It's
1064 # possible to address it by registering custom language-specific
1065 # handler that would restore processor context to the state at
1066 # subroutine entry point and return "exception is not handled, keep
1067 # unwinding" code. Writing such handler can be a challenge... But it's
1068 # doable, though requires certain coding convention. Consider following
1069 # snippet:
1070 #
1071 # .type function,@function
1072 # function:
1073 #       movq    %rsp,%rax       # copy rsp to volatile register
1074 #       pushq   %r15            # save non-volatile registers
1075 #       pushq   %rbx
1076 #       pushq   %rbp
1077 #       movq    %rsp,%r11
1078 #       subq    %rdi,%r11       # prepare [variable] stack frame
1079 #       andq    $-64,%r11
1080 #       movq    %rax,0(%r11)    # check for exceptions
1081 #       movq    %r11,%rsp       # allocate [variable] stack frame
1082 #       movq    %rax,0(%rsp)    # save original rsp value
1083 # magic_point:
1084 #       ...
1085 #       movq    0(%rsp),%rcx    # pull original rsp value
1086 #       movq    -24(%rcx),%rbp  # restore non-volatile registers
1087 #       movq    -16(%rcx),%rbx
1088 #       movq    -8(%rcx),%r15
1089 #       movq    %rcx,%rsp       # restore original rsp
1090 # magic_epilogue:
1091 #       ret
1092 # .size function,.-function
1093 #
1094 # The key is that up to magic_point copy of original rsp value remains
1095 # in chosen volatile register and no non-volatile register, except for
1096 # rsp, is modified. While past magic_point rsp remains constant till
1097 # the very end of the function. In this case custom language-specific
1098 # exception handler would look like this:
1099 #
1100 # EXCEPTION_DISPOSITION handler (EXCEPTION_RECORD *rec,ULONG64 frame,
1101 #               CONTEXT *context,DISPATCHER_CONTEXT *disp)
1102 # {     ULONG64 *rsp = (ULONG64 *)context->Rax;
1103 #       ULONG64  rip = context->Rip;
1104 #
1105 #       if (rip >= magic_point)
1106 #       {   rsp = (ULONG64 *)context->Rsp;
1107 #           if (rip < magic_epilogue)
1108 #           {   rsp = (ULONG64 *)rsp[0];
1109 #               context->Rbp = rsp[-3];
1110 #               context->Rbx = rsp[-2];
1111 #               context->R15 = rsp[-1];
1112 #           }
1113 #       }
1114 #       context->Rsp = (ULONG64)rsp;
1115 #       context->Rdi = rsp[1];
1116 #       context->Rsi = rsp[2];
1117 #
1118 #       memcpy (disp->ContextRecord,context,sizeof(CONTEXT));
1119 #       RtlVirtualUnwind(UNW_FLAG_NHANDLER,disp->ImageBase,
1120 #               dips->ControlPc,disp->FunctionEntry,disp->ContextRecord,
1121 #               &disp->HandlerData,&disp->EstablisherFrame,NULL);
1122 #       return ExceptionContinueSearch;
1123 # }
1124 #
1125 # It's appropriate to implement this handler in assembler, directly in
1126 # function's module. In order to do that one has to know members'
1127 # offsets in CONTEXT and DISPATCHER_CONTEXT structures and some constant
1128 # values. Here they are:
1129 #
1130 #       CONTEXT.Rax                             120
1131 #       CONTEXT.Rcx                             128
1132 #       CONTEXT.Rdx                             136
1133 #       CONTEXT.Rbx                             144
1134 #       CONTEXT.Rsp                             152
1135 #       CONTEXT.Rbp                             160
1136 #       CONTEXT.Rsi                             168
1137 #       CONTEXT.Rdi                             176
1138 #       CONTEXT.R8                              184
1139 #       CONTEXT.R9                              192
1140 #       CONTEXT.R10                             200
1141 #       CONTEXT.R11                             208
1142 #       CONTEXT.R12                             216
1143 #       CONTEXT.R13                             224
1144 #       CONTEXT.R14                             232
1145 #       CONTEXT.R15                             240
1146 #       CONTEXT.Rip                             248
1147 #       CONTEXT.Xmm6                            512
1148 #       sizeof(CONTEXT)                         1232
1149 #       DISPATCHER_CONTEXT.ControlPc            0
1150 #       DISPATCHER_CONTEXT.ImageBase            8
1151 #       DISPATCHER_CONTEXT.FunctionEntry        16
1152 #       DISPATCHER_CONTEXT.EstablisherFrame     24
1153 #       DISPATCHER_CONTEXT.TargetIp             32
1154 #       DISPATCHER_CONTEXT.ContextRecord        40
1155 #       DISPATCHER_CONTEXT.LanguageHandler      48
1156 #       DISPATCHER_CONTEXT.HandlerData          56
1157 #       UNW_FLAG_NHANDLER                       0
1158 #       ExceptionContinueSearch                 1
1159 #
1160 # In order to tie the handler to the function one has to compose
1161 # couple of structures: one for .xdata segment and one for .pdata.
1162 #
1163 # UNWIND_INFO structure for .xdata segment would be
1164 #
1165 # function_unwind_info:
1166 #       .byte   9,0,0,0
1167 #       .rva    handler
1168 #
1169 # This structure designates exception handler for a function with
1170 # zero-length prologue, no stack frame or frame register.
1171 #
1172 # To facilitate composing of .pdata structures, auto-generated "gear"
1173 # prologue copies rsp value to rax and denotes next instruction with
1174 # .LSEH_begin_{function_name} label. This essentially defines the SEH
1175 # styling rule mentioned in the beginning. Position of this label is
1176 # chosen in such manner that possible exceptions raised in the "gear"
1177 # prologue would be accounted to caller and unwound from latter's frame.
1178 # End of function is marked with respective .LSEH_end_{function_name}
1179 # label. To summarize, .pdata segment would contain
1180 #
1181 #       .rva    .LSEH_begin_function
1182 #       .rva    .LSEH_end_function
1183 #       .rva    function_unwind_info
1184 #
1185 # Reference to function_unwind_info from .xdata segment is the anchor.
1186 # In case you wonder why references are 32-bit .rvas and not 64-bit
1187 # .quads. References put into these two segments are required to be
1188 # *relative* to the base address of the current binary module, a.k.a.
1189 # image base. No Win64 module, be it .exe or .dll, can be larger than
1190 # 2GB and thus such relative references can be and are accommodated in
1191 # 32 bits.
1192 #
1193 # Having reviewed the example function code, one can argue that "movq
1194 # %rsp,%rax" above is redundant. It is not! Keep in mind that on Unix
1195 # rax would contain an undefined value. If this "offends" you, use
1196 # another register and refrain from modifying rax till magic_point is
1197 # reached, i.e. as if it was a non-volatile register. If more registers
1198 # are required prior [variable] frame setup is completed, note that
1199 # nobody says that you can have only one "magic point." You can
1200 # "liberate" non-volatile registers by denoting last stack off-load
1201 # instruction and reflecting it in finer grade unwind logic in handler.
1202 # After all, isn't it why it's called *language-specific* handler...
1203 #
1204 # SE handlers are also involved in unwinding stack when executable is
1205 # profiled or debugged. Profiling implies additional limitations that
1206 # are too subtle to discuss here. For now it's sufficient to say that
1207 # in order to simplify handlers one should either a) offload original
1208 # %rsp to stack (like discussed above); or b) if you have a register to
1209 # spare for frame pointer, choose volatile one.
1210 #
1211 # (*)   Note that we're talking about run-time, not debug-time. Lack of
1212 #       unwind information makes debugging hard on both Windows and
1213 #       Unix. "Unlike" referes to the fact that on Unix signal handler
1214 #       will always be invoked, core dumped and appropriate exit code
1215 #       returned to parent (for user notification).