perlasm/x86_64-xlate.pl: refine sign extension in ea package.
[oweals/openssl.git] / crypto / perlasm / x86_64-xlate.pl
index 646f68058cec4da54336bfe79cb0b8f0ac677cc0..4298b3f418c89227ded643202d694be23c8deff9 100755 (executable)
@@ -151,7 +151,7 @@ my %globals;
        if ($gas) {
            if ($self->{op} eq "movz") {        # movz is pain...
                sprintf "%s%s%s",$self->{op},$self->{sz},shift;
-           } elsif ($self->{op} =~ /^set/) { 
+           } elsif ($self->{op} =~ /^set/) {
                "$self->{op}";
            } elsif ($self->{op} eq "ret") {
                my $epilogue = "";
@@ -170,15 +170,15 @@ my %globals;
            if ($self->{op} eq "ret") {
                $self->{op} = "";
                if ($win64 && $current_function->{abi} eq "svr4") {
-                   $self->{op} = "mov  rdi,QWORD${PTR}[8+rsp]\t;WIN64 epilogue\n\t".
-                                 "mov  rsi,QWORD${PTR}[16+rsp]\n\t";
+                   $self->{op} = "mov  rdi,QWORD$PTR\[8+rsp\]\t;WIN64 epilogue\n\t".
+                                 "mov  rsi,QWORD$PTR\[16+rsp\]\n\t";
                }
                $self->{op} .= "DB\t0F3h,0C3h\t\t;repret";
            } elsif ($self->{op} =~ /^(pop|push)f/) {
                $self->{op} .= $self->{sz};
            } elsif ($self->{op} eq "call" && $current_segment eq ".CRT\$XCU") {
                $self->{op} = "\tDQ";
-           } 
+           }
            $self->{op};
        }
     }
@@ -210,6 +210,7 @@ my %globals;
            # Solaris /usr/ccs/bin/as can't handle multiplications
            # in $self->{value}
            my $value = $self->{value};
+           no warnings;    # oct might complain about overflow, ignore here...
            $value =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
            if ($value =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg) {
                $self->{value} = $value;
@@ -227,7 +228,7 @@ my %globals;
        my      $self = {};
        my      $ret;
 
-       # optional * ---vvv--- appears in indirect jmp/call
+       # optional * ----vvv--- appears in indirect jmp/call
        if ($$line =~ /^(\*?)([^\(,]*)\(([%\w,]+)\)/) {
            bless $self, $class;
            $self->{asterisk} = $1;
@@ -261,11 +262,18 @@ my %globals;
        $self->{base}  =~ s/^[er](.?[0-9xpi])[d]?$/r\1/;
 
        # Solaris /usr/ccs/bin/as can't handle multiplications
-       # in $self->{label}, new gas requires sign extension...
+       # in $self->{label}...
        use integer;
        $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi;
        $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;
+
+       # Some assemblers insist on signed presentation of 32-bit
+       # offsets, but sign extension is a tricky business in perl...
+       if ((1<<31)<<1) {
+           $self->{label} =~ s/\b([0-9]+)\b/$1<<32>>32/eg;
+       } else {
+           $self->{label} =~ s/\b([0-9]+)\b/$1>>0/eg;
+       }
 
        if (!$self->{label} && $self->{index} && $self->{scale}==1 &&
            $self->{base} =~ /(rbp|r13)/) {
@@ -317,15 +325,16 @@ my %globals;
 }
 { package register;    # pick up registers, which start with %.
     sub re {
-       my      ($class, $line) = @_;
+       my      ($class, $line, $opcode) = @_;
        my      $self = {};
        my      $ret;
 
-       # optional * ---vvv--- appears in indirect jmp/call
+       # optional * ----vvv--- appears in indirect jmp/call
        if ($$line =~ /^(\*?)%(\w+)/) {
            bless $self,$class;
            $self->{asterisk} = $1;
            $self->{value} = $2;
+           $opcode->size($self->size());
            $ret = $self;
            $$line = substr($$line,@+[0]); $$line =~ s/^\s+//;
        }
@@ -399,8 +408,8 @@ my %globals;
            my $func =  "$current_function->{name}" .
                        ($nasm ? ":" : "\tPROC $current_function->{scope}") .
                        "\n";
-           $func .= "  mov     QWORD${PTR}[8+rsp],rdi\t;WIN64 prologue\n";
-           $func .= "  mov     QWORD${PTR}[16+rsp],rsi\n";
+           $func .= "  mov     QWORD$PTR\[8+rsp\],rdi\t;WIN64 prologue\n";
+           $func .= "  mov     QWORD$PTR\[16+rsp\],rsi\n";
            $func .= "  mov     rax,rsp\n";
            $func .= "${decor}SEH_begin_$current_function->{name}:";
            $func .= ":" if ($masm);
@@ -411,8 +420,8 @@ my %globals;
            $func .= "  mov     rsi,rdx\n" if ($narg>1);
            $func .= "  mov     rdx,r8\n"  if ($narg>2);
            $func .= "  mov     rcx,r9\n"  if ($narg>3);
-           $func .= "  mov     r8,QWORD${PTR}[40+rsp]\n" if ($narg>4);
-           $func .= "  mov     r9,QWORD${PTR}[48+rsp]\n" if ($narg>5);
+           $func .= "  mov     r8,QWORD$PTR\[40+rsp\]\n" if ($narg>4);
+           $func .= "  mov     r9,QWORD$PTR\[48+rsp\]\n" if ($narg>5);
            $func .= "\n";
        } else {
           "$current_function->{name}".
@@ -637,7 +646,7 @@ my %globals;
                                                        if ($sz eq "D" && ($current_segment=~/.[px]data/ || $dir eq ".rva"))
                                                        { $var=~s/([_a-z\$\@][_a-z0-9\$\@]*)/$nasm?"$1 wrt ..imagebase":"imagerel $1"/egi; }
                                                        $var;
-                                                   };  
+                                                   };
 
                                    $sz =~ tr/bvlrq/BWDDQ/;
                                    $self->{value} = "\tD$sz\t";
@@ -647,7 +656,7 @@ my %globals;
                                  };
                /\.byte/    && do { my @str=split(/,\s*/,$$line);
                                    map(s/(0b[0-1]+)/oct($1)/eig,@str);
-                                   map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);       
+                                   map(s/0x([0-9a-f]+)/0$1h/ig,@str) if ($masm);
                                    while ($#str>15) {
                                        $self->{value}.="DB\t"
                                                .join(",",@str[0..15])."\n";
@@ -691,7 +700,13 @@ sub rex {
    push @$opcode,($rex|0x40) if ($rex);
 }
 
-# older gas and ml64 don't handle SSE>2 instructions
+# Upon initial x86_64 introduction SSE>2 extensions were not introduced
+# yet. In order not to be bothered by tracing exact assembler versions,
+# but at the same time to provide a bare security minimum of AES-NI, we
+# hard-code some instructions. Extensions past AES-NI on the other hand
+# are traced by examining assembler version in individual perlasm
+# modules...
+
 my %regrm = (  "%eax"=>0, "%ecx"=>1, "%edx"=>2, "%ebx"=>3,
                "%esp"=>4, "%ebp"=>5, "%esi"=>6, "%edi"=>7      );
 
@@ -797,7 +812,7 @@ my $rdrand = sub {
       my @opcode=();
       my $dst=$1;
        if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
-       rex(\@opcode,0,$1,8);
+       rex(\@opcode,0,$dst,8);
        push @opcode,0x0f,0xc7,0xf0|($dst&7);
        @opcode;
     } else {
@@ -810,7 +825,7 @@ my $rdseed = sub {
       my @opcode=();
       my $dst=$1;
        if ($dst !~ /[0-9]+/) { $dst = $regrm{"%e$dst"}; }
-       rex(\@opcode,0,$1,8);
+       rex(\@opcode,0,$dst,8);
        push @opcode,0x0f,0xc7,0xf8|($dst&7);
        @opcode;
     } else {
@@ -857,6 +872,10 @@ my $vprotq = sub {
     }
 };
 
+my $endbranch = sub {
+    (0xf3,0x0f,0x1e,0xfa);
+};
+
 if ($nasm) {
     print <<___;
 default        rel
@@ -884,7 +903,7 @@ while(defined(my $line=<>)) {
        printf "%s",$directive->out();
     } elsif (my $opcode=opcode->re(\$line)) {
        my $asm = eval("\$".$opcode->mnemonic());
-       
+
        if ((ref($asm) eq 'CODE') && scalar(my @bytes=&$asm($line))) {
            print $gas?".byte\t":"DB\t",join(',',@bytes),"\n";
            next;
@@ -892,19 +911,19 @@ while(defined(my $line=<>)) {
 
        my @args;
        ARGUMENT: while (1) {
-       my $arg;
+           my $arg;
 
-       if ($arg=register->re(\$line))          { $opcode->size($arg->size()); }
-       elsif ($arg=const->re(\$line))          { }
-       elsif ($arg=ea->re(\$line, $opcode))    { }
-       elsif ($arg=expr->re(\$line, $opcode))  { }
-       else                                    { last ARGUMENT; }
+           ($arg=register->re(\$line, $opcode))||
+           ($arg=const->re(\$line))            ||
+           ($arg=ea->re(\$line, $opcode))      ||
+           ($arg=expr->re(\$line, $opcode))    ||
+           last ARGUMENT;
 
-       push @args,$arg;
+           push @args,$arg;
 
-       last ARGUMENT if ($line !~ /^,/);
+           last ARGUMENT if ($line !~ /^,/);
 
-       $line =~ s/^,\s*//;
+           $line =~ s/^,\s*//;
        } # ARGUMENT:
 
        if ($#args>=0) {
@@ -962,7 +981,7 @@ close STDOUT;
 # %r13         -               -
 # %r14         -               -
 # %r15         -               -
-# 
+#
 # (*)  volatile register
 # (-)  preserved by callee
 # (#)  Nth argument, volatile