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