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