perlasm/x86_64-xlate.pl: handle inter-bank movd.
[openssl.git] / crypto / perlasm / x86_64-xlate.pl
index 1492e1c2e7819b918e21acc46b10d0eeb47d6e8b..aae8288386d3189ec8aa7482565f4e985b2a5634 100755 (executable)
@@ -62,12 +62,8 @@ my $flavour = shift;
 my $output  = shift;
 if ($flavour =~ /\./) { $output = $flavour; undef $flavour; }
 
-{ my ($stddev,$stdino,@junk)=stat(STDOUT);
-  my ($outdev,$outino,@junk)=stat($output);
-
-    open STDOUT,">$output" || die "can't open $output: $!"
-       if ($stddev!=$outdev || $stdino!=$outino);
-}
+open STDOUT,">$output" || die "can't open $output: $!"
+       if (defined($output));
 
 my $gas=1;     $gas=0 if ($output =~ /\.asm$/);
 my $elf=1;     $elf=0 if (!$gas);
@@ -125,7 +121,7 @@ my %globals;
                $self->{sz} = "";
            } elsif ($self->{op} =~ /^v/) { # VEX
                $self->{sz} = "";
-           } elsif ($self->{op} =~ /movq/ && $line =~ /%xmm/) {
+           } elsif ($self->{op} =~ /mov[dq]/ && $line =~ /%xmm/) {
                $self->{sz} = "";
            } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) {
                $self->{op} = $1;
@@ -254,8 +250,13 @@ my %globals;
        # in $self->{label}, new gas requires sign extension...
        use integer;
        $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
-       $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg;
-       $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg;
+       $self->{label} =~ s/\b([0-9]+\s*[\*\/\%]\s*[0-9]+)\b/eval($1)/eg;
+       $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
+
+       if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
+           $self->{base} =~ /(rbp|r13)/) {
+               $self->{base} = $self->{index}; $self->{index} = $1;
+       }
 
        if ($gas) {
            $self->{label} =~ s/^___imp_/__imp__/   if ($flavour eq "mingw64");
@@ -269,14 +270,20 @@ my %globals;
                sprintf "%s%s(%%%s)",   $self->{asterisk},$self->{label},$self->{base};
            }
        } else {
-           %szmap = (  b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR",
-                       q=>"QWORD$PTR",o=>"OWORD$PTR",x=>"XMMWORD$PTR" );
+           %szmap = (  b=>"BYTE$PTR",  w=>"WORD$PTR",
+                       l=>"DWORD$PTR", d=>"DWORD$PTR",
+                       q=>"QWORD$PTR", o=>"OWORD$PTR",
+                       x=>"XMMWORD$PTR", y=>"YMMWORD$PTR", z=>"ZMMWORD$PTR" );
 
            $self->{label} =~ s/\./\$/g;
            $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig;
            $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/);
-           $sz="q" if ($self->{asterisk} || opcode->mnemonic() eq "movq");
-           $sz="l" if (opcode->mnemonic() eq "movd");
+
+           ($self->{asterisk})                                 && ($sz="q") ||
+           (opcode->mnemonic() =~ /^v?mov([qd])$/)             && ($sz=$1)  ||
+           (opcode->mnemonic() =~ /^v?pinsr([qdwb])$/)         && ($sz=$1)  ||
+           (opcode->mnemonic() =~ /^vpbroadcast([qdwb])$/)     && ($sz=$1)  ||
+           (opcode->mnemonic() =~ /^vinsert[fi]128$/)          && ($sz="x");
 
            if (defined($self->{index})) {
                sprintf "%s[%s%s*%d%s]",$szmap{$sz},
@@ -416,7 +423,7 @@ my %globals;
     }
     sub out {
        my $self = shift;
-       if ($nasm && opcode->mnemonic()=~m/^j/) {
+       if ($nasm && opcode->mnemonic()=~m/^j(?![re]cxz)/) {
            "NEAR ".$self->{value};
        } else {
            $self->{value};
@@ -534,7 +541,7 @@ my %globals;
                                        $v="$current_segment\tENDS\n" if ($current_segment);
                                        $current_segment = ".text\$";
                                        $v.="$current_segment\tSEGMENT ";
-                                       $v.=$masm>=$masmref ? "ALIGN(64)" : "PAGE";
+                                       $v.=$masm>=$masmref ? "ALIGN(256)" : "PAGE";
                                        $v.=" 'CODE'";
                                    }
                                    $self->{value} = $v;
@@ -776,10 +783,64 @@ my $rdrand = sub {
     }
 };
 
+my $rdseed = sub {
+    if (shift =~ /%[er](\w+)/) {
+      my @opcode=();
+      my $dst=$1;
+       if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
+       rex(\@opcode,0,$1,8);
+       push @opcode,0x0f,0xc7,0xf8|($dst&7);
+       @opcode;
+    } else {
+       ();
+    }
+};
+
+sub rxb {
+ local *opcode=shift;
+ my ($dst,$src1,$src2,$rxb)=@_;
+
+   $rxb|=0x7<<5;
+   $rxb&=~(0x04<<5) if($dst>=8);
+   $rxb&=~(0x01<<5) if($src1>=8);
+   $rxb&=~(0x02<<5) if($src2>=8);
+   push @opcode,$rxb;
+}
+
+my $vprotd = sub {
+    if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
+      my @opcode=(0x8f);
+       rxb(\@opcode,$3,$2,-1,0x08);
+       push @opcode,0x78,0xc2;
+       push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
+       my $c=$1;
+       push @opcode,$c=~/^0/?oct($c):$c;
+       @opcode;
+    } else {
+       ();
+    }
+};
+
+my $vprotq = sub {
+    if (shift =~ /\$([x0-9a-f]+),\s*%xmm([0-9]+),\s*%xmm([0-9]+)/) {
+      my @opcode=(0x8f);
+       rxb(\@opcode,$3,$2,-1,0x08);
+       push @opcode,0x78,0xc3;
+       push @opcode,0xc0|($2&7)|(($3&7)<<3);           # ModR/M
+       my $c=$1;
+       push @opcode,$c=~/^0/?oct($c):$c;
+       @opcode;
+    } else {
+       ();
+    }
+};
+
 if ($nasm) {
     print <<___;
 default        rel
 %define XMMWORD
+%define YMMWORD
+%define ZMMWORD
 ___
 } elsif ($masm) {
     print <<___;
@@ -793,6 +854,7 @@ while($line=<>) {
     $line =~ s|[#!].*$||;      # get rid of asm-style comments...
     $line =~ s|/\*.*\*/||;     # ... and C-style comments...
     $line =~ s|^\s+||;         # ... and skip white spaces in beginning
+    $line =~ s|\s+$||;         # ... and at the end
 
     undef $label;
     undef $opcode;
@@ -841,6 +903,8 @@ while($line=<>) {
                    my $arg = $_->out();
                    # $insn.=$sz compensates for movq, pinsrw, ...
                    if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; }
+                   if ($arg =~ /^ymm[0-9]+$/) { $insn.=$sz; $sz="y" if(!$sz); last; }
+                   if ($arg =~ /^zmm[0-9]+$/) { $insn.=$sz; $sz="z" if(!$sz); last; }
                    if ($arg =~ /^mm[0-9]+$/)  { $insn.=$sz; $sz="q" if(!$sz); last; }
                }
                @args = reverse(@args);
@@ -1050,7 +1114,7 @@ close STDOUT;
 #      .rva    .LSEH_end_function
 #      .rva    function_unwind_info
 #
-# Reference to functon_unwind_info from .xdata segment is the anchor.
+# Reference to function_unwind_info from .xdata segment is the anchor.
 # In case you wonder why references are 32-bit .rvas and not 64-bit
 # .quads. References put into these two segments are required to be
 # *relative* to the base address of the current binary module, a.k.a.