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