diff options
Diffstat (limited to 'src/lib/libcrypto/perlasm/x86_64-xlate.pl')
-rwxr-xr-x | src/lib/libcrypto/perlasm/x86_64-xlate.pl | 221 |
1 files changed, 192 insertions, 29 deletions
diff --git a/src/lib/libcrypto/perlasm/x86_64-xlate.pl b/src/lib/libcrypto/perlasm/x86_64-xlate.pl index 68b4c1ca80..50ff91e2f0 100755 --- a/src/lib/libcrypto/perlasm/x86_64-xlate.pl +++ b/src/lib/libcrypto/perlasm/x86_64-xlate.pl | |||
@@ -62,12 +62,8 @@ my $flavour = shift; | |||
62 | my $output = shift; | 62 | my $output = shift; |
63 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } | 63 | if ($flavour =~ /\./) { $output = $flavour; undef $flavour; } |
64 | 64 | ||
65 | { my ($stddev,$stdino,@junk)=stat(STDOUT); | 65 | open STDOUT,">$output" || die "can't open $output: $!" |
66 | my ($outdev,$outino,@junk)=stat($output); | 66 | if (defined($output)); |
67 | |||
68 | open STDOUT,">$output" || die "can't open $output: $!" | ||
69 | if (1 || $stddev!=$outdev || $stdino!=$outino); | ||
70 | } | ||
71 | 67 | ||
72 | my $gas=1; $gas=0 if ($output =~ /\.asm$/); | 68 | my $gas=1; $gas=0 if ($output =~ /\.asm$/); |
73 | my $elf=1; $elf=0 if (!$gas); | 69 | my $elf=1; $elf=0 if (!$gas); |
@@ -116,12 +112,16 @@ my %globals; | |||
116 | $line = substr($line,@+[0]); $line =~ s/^\s+//; | 112 | $line = substr($line,@+[0]); $line =~ s/^\s+//; |
117 | 113 | ||
118 | undef $self->{sz}; | 114 | undef $self->{sz}; |
119 | if ($self->{op} =~ /^(movz)b.*/) { # movz is pain... | 115 | if ($self->{op} =~ /^(movz)x?([bw]).*/) { # movz is pain... |
120 | $self->{op} = $1; | 116 | $self->{op} = $1; |
121 | $self->{sz} = "b"; | 117 | $self->{sz} = $2; |
122 | } elsif ($self->{op} =~ /call|jmp/) { | 118 | } elsif ($self->{op} =~ /call|jmp/) { |
123 | $self->{sz} = ""; | 119 | $self->{sz} = ""; |
124 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op)/) { # SSEn | 120 | } elsif ($self->{op} =~ /^p/ && $' !~ /^(ush|op|insrw)/) { # SSEn |
121 | $self->{sz} = ""; | ||
122 | } elsif ($self->{op} =~ /^v/) { # VEX | ||
123 | $self->{sz} = ""; | ||
124 | } elsif ($self->{op} =~ /movq/ && $line =~ /%xmm/) { | ||
125 | $self->{sz} = ""; | 125 | $self->{sz} = ""; |
126 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { | 126 | } elsif ($self->{op} =~ /([a-z]{3,})([qlwb])$/) { |
127 | $self->{op} = $1; | 127 | $self->{op} = $1; |
@@ -247,35 +247,39 @@ my %globals; | |||
247 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | 247 | $self->{index} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
248 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; | 248 | $self->{base} =~ s/^[er](.?[0-9xpi])[d]?$/r\1/; |
249 | 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 | |||
250 | if ($gas) { | 257 | if ($gas) { |
251 | # Solaris /usr/ccs/bin/as can't handle multiplications | ||
252 | # in $self->{label}, new gas requires sign extension... | ||
253 | use integer; | ||
254 | $self->{label} =~ s/(?<![\w\$\.])(0x?[0-9a-f]+)/oct($1)/egi; | ||
255 | $self->{label} =~ s/([0-9]+\s*[\*\/\%]\s*[0-9]+)/eval($1)/eg; | ||
256 | $self->{label} =~ s/([0-9]+)/$1<<32>>32/eg; | ||
257 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64"); | 258 | $self->{label} =~ s/^___imp_/__imp__/ if ($flavour eq "mingw64"); |
258 | 259 | ||
259 | if (defined($self->{index})) { | 260 | if (defined($self->{index})) { |
260 | sprintf "%s%s(%%%s,%%%s,%d)",$self->{asterisk}, | 261 | sprintf "%s%s(%s,%%%s,%d)",$self->{asterisk}, |
261 | $self->{label},$self->{base}, | 262 | $self->{label}, |
263 | $self->{base}?"%$self->{base}":"", | ||
262 | $self->{index},$self->{scale}; | 264 | $self->{index},$self->{scale}; |
263 | } else { | 265 | } else { |
264 | sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; | 266 | sprintf "%s%s(%%%s)", $self->{asterisk},$self->{label},$self->{base}; |
265 | } | 267 | } |
266 | } else { | 268 | } else { |
267 | %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", q=>"QWORD$PTR" ); | 269 | %szmap = ( b=>"BYTE$PTR", w=>"WORD$PTR", l=>"DWORD$PTR", |
270 | q=>"QWORD$PTR",o=>"OWORD$PTR",x=>"XMMWORD$PTR" ); | ||
268 | 271 | ||
269 | $self->{label} =~ s/\./\$/g; | 272 | $self->{label} =~ s/\./\$/g; |
270 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; | 273 | $self->{label} =~ s/(?<![\w\$\.])0x([0-9a-f]+)/0$1h/ig; |
271 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); | 274 | $self->{label} = "($self->{label})" if ($self->{label} =~ /[\*\+\-\/]/); |
272 | $sz="q" if ($self->{asterisk}); | 275 | $sz="q" if ($self->{asterisk} || opcode->mnemonic() eq "movq"); |
276 | $sz="l" if (opcode->mnemonic() eq "movd"); | ||
273 | 277 | ||
274 | if (defined($self->{index})) { | 278 | if (defined($self->{index})) { |
275 | sprintf "%s[%s%s*%d+%s]",$szmap{$sz}, | 279 | sprintf "%s[%s%s*%d%s]",$szmap{$sz}, |
276 | $self->{label}?"$self->{label}+":"", | 280 | $self->{label}?"$self->{label}+":"", |
277 | $self->{index},$self->{scale}, | 281 | $self->{index},$self->{scale}, |
278 | $self->{base}; | 282 | $self->{base}?"+$self->{base}":""; |
279 | } elsif ($self->{base} eq "rip") { | 283 | } elsif ($self->{base} eq "rip") { |
280 | sprintf "%s[%s]",$szmap{$sz},$self->{label}; | 284 | sprintf "%s[%s]",$szmap{$sz},$self->{label}; |
281 | } else { | 285 | } else { |
@@ -507,6 +511,12 @@ my %globals; | |||
507 | } | 511 | } |
508 | } elsif ($dir =~ /\.(text|data)/) { | 512 | } elsif ($dir =~ /\.(text|data)/) { |
509 | $current_segment=".$1"; | 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"); | ||
510 | } | 520 | } |
511 | $line = ""; | 521 | $line = ""; |
512 | return $self; | 522 | return $self; |
@@ -556,7 +566,8 @@ my %globals; | |||
556 | $v.=" READONLY"; | 566 | $v.=" READONLY"; |
557 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref); | 567 | $v.=" ALIGN(".($1 eq "p" ? 4 : 8).")" if ($masm>=$masmref); |
558 | } elsif ($line=~/\.CRT\$/i) { | 568 | } elsif ($line=~/\.CRT\$/i) { |
559 | $v.=" READONLY DWORD"; | 569 | $v.=" READONLY "; |
570 | $v.=$masm>=$masmref ? "ALIGN(8)" : "DWORD"; | ||
560 | } | 571 | } |
561 | } | 572 | } |
562 | $current_segment = $line; | 573 | $current_segment = $line; |
@@ -578,7 +589,7 @@ my %globals; | |||
578 | $self->{value}="${decor}SEH_end_$current_function->{name}:"; | 589 | $self->{value}="${decor}SEH_end_$current_function->{name}:"; |
579 | $self->{value}.=":\n" if($masm); | 590 | $self->{value}.=":\n" if($masm); |
580 | } | 591 | } |
581 | $self->{value}.="$current_function->{name}\tENDP" if($masm); | 592 | $self->{value}.="$current_function->{name}\tENDP" if($masm && $current_function->{name}); |
582 | undef $current_function; | 593 | undef $current_function; |
583 | } | 594 | } |
584 | last; | 595 | last; |
@@ -614,6 +625,19 @@ my %globals; | |||
614 | .join(",",@str) if (@str); | 625 | .join(",",@str) if (@str); |
615 | last; | 626 | last; |
616 | }; | 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 | }; | ||
617 | } | 641 | } |
618 | $line = ""; | 642 | $line = ""; |
619 | } | 643 | } |
@@ -626,11 +650,135 @@ my %globals; | |||
626 | } | 650 | } |
627 | } | 651 | } |
628 | 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 | |||
629 | print "#include <machine/asm.h>\n"; | 776 | print "#include <machine/asm.h>\n"; |
630 | 777 | ||
631 | if ($nasm) { | 778 | if ($nasm) { |
632 | print <<___; | 779 | print <<___; |
633 | default rel | 780 | default rel |
781 | %define XMMWORD | ||
634 | ___ | 782 | ___ |
635 | } elsif ($masm) { | 783 | } elsif ($masm) { |
636 | print <<___; | 784 | print <<___; |
@@ -647,14 +795,22 @@ while($line=<>) { | |||
647 | 795 | ||
648 | undef $label; | 796 | undef $label; |
649 | undef $opcode; | 797 | undef $opcode; |
650 | undef $sz; | ||
651 | undef @args; | 798 | undef @args; |
652 | 799 | ||
653 | if ($label=label->re(\$line)) { print $label->out(); } | 800 | if ($label=label->re(\$line)) { print $label->out(); } |
654 | 801 | ||
655 | if (directive->re(\$line)) { | 802 | if (directive->re(\$line)) { |
656 | printf "%s",directive->out(); | 803 | printf "%s",directive->out(); |
657 | } elsif ($opcode=opcode->re(\$line)) { ARGUMENT: while (1) { | 804 | } elsif ($opcode=opcode->re(\$line)) { |
805 | my $asm = eval("\$".$opcode->mnemonic()); | ||
806 | undef @bytes; | ||
807 | |||
808 | if ((ref($asm) eq 'CODE') && scalar(@bytes=&$asm($line))) { | ||
809 | print $gas?".byte\t":"DB\t",join(',',@bytes),"\n"; | ||
810 | next; | ||
811 | } | ||
812 | |||
813 | ARGUMENT: while (1) { | ||
658 | my $arg; | 814 | my $arg; |
659 | 815 | ||
660 | if ($arg=register->re(\$line)) { opcode->size($arg->size()); } | 816 | if ($arg=register->re(\$line)) { opcode->size($arg->size()); } |
@@ -670,19 +826,26 @@ while($line=<>) { | |||
670 | $line =~ s/^,\s*//; | 826 | $line =~ s/^,\s*//; |
671 | } # ARGUMENT: | 827 | } # ARGUMENT: |
672 | 828 | ||
673 | $sz=opcode->size(); | ||
674 | |||
675 | if ($#args>=0) { | 829 | if ($#args>=0) { |
676 | my $insn; | 830 | my $insn; |
831 | my $sz=opcode->size(); | ||
832 | |||
677 | if ($gas) { | 833 | if ($gas) { |
678 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); | 834 | $insn = $opcode->out($#args>=1?$args[$#args]->size():$sz); |
835 | @args = map($_->out($sz),@args); | ||
836 | printf "\t%s\t%s",$insn,join(",",@args); | ||
679 | } else { | 837 | } else { |
680 | $insn = $opcode->out(); | 838 | $insn = $opcode->out(); |
681 | $insn .= $sz if (map($_->out() =~ /x?mm/,@args)); | 839 | foreach (@args) { |
840 | my $arg = $_->out(); | ||
841 | # $insn.=$sz compensates for movq, pinsrw, ... | ||
842 | if ($arg =~ /^xmm[0-9]+$/) { $insn.=$sz; $sz="x" if(!$sz); last; } | ||
843 | if ($arg =~ /^mm[0-9]+$/) { $insn.=$sz; $sz="q" if(!$sz); last; } | ||
844 | } | ||
682 | @args = reverse(@args); | 845 | @args = reverse(@args); |
683 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); | 846 | undef $sz if ($nasm && $opcode->mnemonic() eq "lea"); |
847 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); | ||
684 | } | 848 | } |
685 | printf "\t%s\t%s",$insn,join(",",map($_->out($sz),@args)); | ||
686 | } else { | 849 | } else { |
687 | printf "\t%s",$opcode->out(); | 850 | printf "\t%s",$opcode->out(); |
688 | } | 851 | } |