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