diff options
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/perlasm/x86asm.pl | 333 |
1 files changed, 209 insertions, 124 deletions
diff --git a/src/lib/libcrypto/perlasm/x86asm.pl b/src/lib/libcrypto/perlasm/x86asm.pl index f535c9c7fa..4756a28e59 100644 --- a/src/lib/libcrypto/perlasm/x86asm.pl +++ b/src/lib/libcrypto/perlasm/x86asm.pl | |||
| @@ -1,136 +1,221 @@ | |||
| 1 | #!/usr/local/bin/perl | 1 | #!/usr/bin/env perl |
| 2 | 2 | ||
| 3 | # require 'x86asm.pl'; | 3 | # require 'x86asm.pl'; |
| 4 | # &asm_init("cpp","des-586.pl"); | 4 | # &asm_init(<flavor>,"des-586.pl"[,$i386only]); |
| 5 | # XXX | 5 | # &function_begin("foo"); |
| 6 | # XXX | 6 | # ... |
| 7 | # main'asm_finish | 7 | # &function_end("foo"); |
| 8 | 8 | # &asm_finish | |
| 9 | sub main'asm_finish | 9 | |
| 10 | { | 10 | $out=(); |
| 11 | &file_end(); | 11 | $i386=0; |
| 12 | &asm_finish_cpp() if $cpp; | 12 | |
| 13 | print &asm_get_output(); | 13 | # AUTOLOAD is this context has quite unpleasant side effect, namely |
| 14 | } | 14 | # that typos in function calls effectively go to assembler output, |
| 15 | 15 | # but on the pros side we don't have to implement one subroutine per | |
| 16 | sub main'asm_init | 16 | # each opcode... |
| 17 | { | 17 | sub ::AUTOLOAD |
| 18 | ($type,$fn,$i386)=@_; | 18 | { my $opcode = $AUTOLOAD; |
| 19 | $filename=$fn; | 19 | |
| 20 | 20 | die "more than 4 arguments passed to $opcode" if ($#_>3); | |
| 21 | $elf=$cpp=$coff=$aout=$win32=$netware=$mwerks=$openbsd=0; | 21 | |
| 22 | if ( ($type eq "elf")) | 22 | $opcode =~ s/.*:://; |
| 23 | { $elf=1; require "x86unix.pl"; } | 23 | if ($opcode =~ /^push/) { $stack+=4; } |
| 24 | elsif ( ($type eq "openbsd-elf")) | 24 | elsif ($opcode =~ /^pop/) { $stack-=4; } |
| 25 | { $openbsd=$elf=1; require "x86unix.pl"; } | 25 | |
| 26 | elsif ( ($type eq "openbsd-a.out")) | 26 | &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD"; |
| 27 | { $openbsd=1; require "x86unix.pl"; } | 27 | } |
| 28 | elsif ( ($type eq "a.out")) | 28 | |
| 29 | { $aout=1; require "x86unix.pl"; } | 29 | sub ::emit |
| 30 | elsif ( ($type eq "coff" or $type eq "gaswin")) | 30 | { my $opcode=shift; |
| 31 | { $coff=1; require "x86unix.pl"; } | 31 | |
| 32 | elsif ( ($type eq "cpp")) | 32 | if ($#_==-1) { push(@out,"\t$opcode\n"); } |
| 33 | { $cpp=1; require "x86unix.pl"; } | 33 | else { push(@out,"\t$opcode\t".join(',',@_)."\n"); } |
| 34 | elsif ( ($type eq "win32")) | 34 | } |
| 35 | { $win32=1; require "x86ms.pl"; } | 35 | |
| 36 | elsif ( ($type eq "win32n")) | 36 | sub ::emitraw |
| 37 | { $win32=1; require "x86nasm.pl"; } | 37 | { my $opcode=shift; |
| 38 | elsif ( ($type eq "nw-nasm")) | 38 | |
| 39 | { $netware=1; require "x86nasm.pl"; } | 39 | if ($#_==-1) { push(@out,"$opcode\n"); } |
| 40 | elsif ( ($type eq "nw-mwasm")) | 40 | else { push(@out,"$opcode\t".join(',',@_)."\n"); } |
| 41 | { $netware=1; $mwerks=1; require "x86nasm.pl"; } | 41 | } |
| 42 | else | 42 | |
| 43 | { | 43 | sub ::LB |
| 44 | print STDERR <<"EOF"; | 44 | { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'"; |
| 45 | $1."l"; | ||
| 46 | } | ||
| 47 | sub ::HB | ||
| 48 | { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'"; | ||
| 49 | $1."h"; | ||
| 50 | } | ||
| 51 | sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); } | ||
| 52 | sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); } | ||
| 53 | sub ::blindpop { &pop($_[0]); $stack+=4; } | ||
| 54 | sub ::wparam { &DWP($stack+4*$_[0],"esp"); } | ||
| 55 | sub ::swtmp { &DWP(4*$_[0],"esp"); } | ||
| 56 | |||
| 57 | sub ::bswap | ||
| 58 | { if ($i386) # emulate bswap for i386 | ||
| 59 | { &comment("bswap @_"); | ||
| 60 | &xchg(&HB(@_),&LB(@_)); | ||
| 61 | &ror (@_,16); | ||
| 62 | &xchg(&HB(@_),&LB(@_)); | ||
| 63 | } | ||
| 64 | else | ||
| 65 | { &generic("bswap",@_); } | ||
| 66 | } | ||
| 67 | # These are made-up opcodes introduced over the years essentially | ||
| 68 | # by ignorance, just alias them to real ones... | ||
| 69 | sub ::movb { &mov(@_); } | ||
| 70 | sub ::xorb { &xor(@_); } | ||
| 71 | sub ::rotl { &rol(@_); } | ||
| 72 | sub ::rotr { &ror(@_); } | ||
| 73 | sub ::exch { &xchg(@_); } | ||
| 74 | sub ::halt { &hlt; } | ||
| 75 | sub ::movz { &movzx(@_); } | ||
| 76 | sub ::pushf { &pushfd; } | ||
| 77 | sub ::popf { &popfd; } | ||
| 78 | |||
| 79 | # 3 argument instructions | ||
| 80 | sub ::movq | ||
| 81 | { my($p1,$p2,$optimize)=@_; | ||
| 82 | |||
| 83 | if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/) | ||
| 84 | # movq between mmx registers can sink Intel CPUs | ||
| 85 | { &::pshufw($p1,$p2,0xe4); } | ||
| 86 | else | ||
| 87 | { &::generic("movq",@_); } | ||
| 88 | } | ||
| 89 | |||
| 90 | # label management | ||
| 91 | $lbdecor="L"; # local label decoration, set by package | ||
| 92 | $label="000"; | ||
| 93 | |||
| 94 | sub ::islabel # see is argument is a known label | ||
| 95 | { my $i; | ||
| 96 | foreach $i (values %label) { return $i if ($i eq $_[0]); } | ||
| 97 | $label{$_[0]}; # can be undef | ||
| 98 | } | ||
| 99 | |||
| 100 | sub ::label # instantiate a function-scope label | ||
| 101 | { if (!defined($label{$_[0]})) | ||
| 102 | { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; } | ||
| 103 | $label{$_[0]}; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub ::LABEL # instantiate a file-scope label | ||
| 107 | { $label{$_[0]}=$_[1] if (!defined($label{$_[0]})); | ||
| 108 | $label{$_[0]}; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); } | ||
| 112 | |||
| 113 | sub ::set_label_B { push(@out,"@_:\n"); } | ||
| 114 | sub ::set_label | ||
| 115 | { my $label=&::label($_[0]); | ||
| 116 | &::align($_[1]) if ($_[1]>1); | ||
| 117 | &::set_label_B($label); | ||
| 118 | $label; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub ::wipe_labels # wipes function-scope labels | ||
| 122 | { foreach $i (keys %label) | ||
| 123 | { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); } | ||
| 124 | } | ||
| 125 | |||
| 126 | # subroutine management | ||
| 127 | sub ::function_begin | ||
| 128 | { &function_begin_B(@_); | ||
| 129 | $stack=4; | ||
| 130 | &push("ebp"); | ||
| 131 | &push("ebx"); | ||
| 132 | &push("esi"); | ||
| 133 | &push("edi"); | ||
| 134 | } | ||
| 135 | |||
| 136 | sub ::function_end | ||
| 137 | { &pop("edi"); | ||
| 138 | &pop("esi"); | ||
| 139 | &pop("ebx"); | ||
| 140 | &pop("ebp"); | ||
| 141 | &ret(); | ||
| 142 | &function_end_B(@_); | ||
| 143 | $stack=0; | ||
| 144 | &wipe_labels(); | ||
| 145 | } | ||
| 146 | |||
| 147 | sub ::function_end_A | ||
| 148 | { &pop("edi"); | ||
| 149 | &pop("esi"); | ||
| 150 | &pop("ebx"); | ||
| 151 | &pop("ebp"); | ||
| 152 | &ret(); | ||
| 153 | $stack+=16; # readjust esp as if we didn't pop anything | ||
| 154 | } | ||
| 155 | |||
| 156 | sub ::asciz | ||
| 157 | { my @str=unpack("C*",shift); | ||
| 158 | push @str,0; | ||
| 159 | while ($#str>15) { | ||
| 160 | &data_byte(@str[0..15]); | ||
| 161 | foreach (0..15) { shift @str; } | ||
| 162 | } | ||
| 163 | &data_byte(@str) if (@str); | ||
| 164 | } | ||
| 165 | |||
| 166 | sub ::asm_finish | ||
| 167 | { &file_end(); | ||
| 168 | print @out; | ||
| 169 | } | ||
| 170 | |||
| 171 | sub ::asm_init | ||
| 172 | { my ($type,$fn,$cpu)=@_; | ||
| 173 | |||
| 174 | $filename=$fn; | ||
| 175 | $i386=$cpu; | ||
| 176 | |||
| 177 | $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$openbsd=0; | ||
| 178 | if (($type eq "elf")) | ||
| 179 | { $elf=1; require "x86gas.pl"; } | ||
| 180 | elsif (($type eq "a\.out")) | ||
| 181 | { $aout=1; require "x86gas.pl"; } | ||
| 182 | elsif (($type eq "coff" or $type eq "gaswin")) | ||
| 183 | { $coff=1; require "x86gas.pl"; } | ||
| 184 | elsif (($type eq "win32n")) | ||
| 185 | { $win32=1; require "x86nasm.pl"; } | ||
| 186 | elsif (($type eq "nw-nasm")) | ||
| 187 | { $netware=1; require "x86nasm.pl"; } | ||
| 188 | #elsif (($type eq "nw-mwasm")) | ||
| 189 | #{ $netware=1; $mwerks=1; require "x86nasm.pl"; } | ||
| 190 | elsif (($type eq "win32")) | ||
| 191 | { $win32=1; require "x86masm.pl"; } | ||
| 192 | elsif (($type eq "macosx")) | ||
| 193 | { $aout=1; $macosx=1; require "x86gas.pl"; } | ||
| 194 | elsif (($type eq "openbsd-elf")) | ||
| 195 | { $openbsd=$elf=1; require "x86gas.pl"; } | ||
| 196 | elsif (($type eq "openbsd-a.out")) | ||
| 197 | { $openbsd=1; require "x86gas.pl"; } | ||
| 198 | else | ||
| 199 | { print STDERR <<"EOF"; | ||
| 45 | Pick one target type from | 200 | Pick one target type from |
| 46 | elf - Linux, FreeBSD, Solaris x86, etc. | 201 | elf - Linux, FreeBSD, Solaris x86, etc. |
| 47 | a.out - OpenBSD, DJGPP, etc. | 202 | a.out - DJGPP, elder OpenBSD, etc. |
| 48 | coff - GAS/COFF such as Win32 targets | 203 | coff - GAS/COFF such as Win32 targets |
| 49 | win32 - Windows 95/Windows NT | ||
| 50 | win32n - Windows 95/Windows NT NASM format | 204 | win32n - Windows 95/Windows NT NASM format |
| 51 | openbsd-elf - OpenBSD elf | 205 | openbsd-elf - OpenBSD elf |
| 52 | openbsd-a.out - OpenBSD a.out | 206 | openbsd-a.out - OpenBSD a.out |
| 53 | nw-nasm - NetWare NASM format | 207 | nw-nasm - NetWare NASM format |
| 54 | nw-mwasm- NetWare Metrowerks Assembler | 208 | macosx - Mac OS X |
| 55 | EOF | 209 | EOF |
| 56 | exit(1); | 210 | exit(1); |
| 57 | } | 211 | } |
| 58 | 212 | ||
| 59 | $pic=0; | 213 | $pic=0; |
| 60 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } | 214 | for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); } |
| 61 | 215 | ||
| 62 | &asm_init_output(); | 216 | ::emitraw("#include <machine/asm.h>\n") if $openbsd; |
| 63 | 217 | $filename =~ s/\.pl$//; | |
| 64 | &comment("Don't even think of reading this code"); | 218 | &file($filename); |
| 65 | &comment("It was automatically generated by $filename"); | 219 | } |
| 66 | &comment("Which is a perl program used to generate the x86 assember for"); | ||
| 67 | &comment("any of ELF, a.out, COFF, Win32, ..."); | ||
| 68 | &comment("eric <eay\@cryptsoft.com>"); | ||
| 69 | &comment(""); | ||
| 70 | |||
| 71 | $filename =~ s/\.pl$//; | ||
| 72 | &file($filename); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub asm_finish_cpp | ||
| 76 | { | ||
| 77 | return unless $cpp; | ||
| 78 | |||
| 79 | local($tmp,$i); | ||
| 80 | foreach $i (&get_labels()) | ||
| 81 | { | ||
| 82 | $tmp.="#define $i _$i\n"; | ||
| 83 | } | ||
| 84 | print <<"EOF"; | ||
| 85 | /* Run the C pre-processor over this file with one of the following defined | ||
| 86 | * ELF - elf object files, | ||
| 87 | * OUT - a.out object files, | ||
| 88 | * BSDI - BSDI style a.out object files | ||
| 89 | * SOL - Solaris style elf | ||
| 90 | */ | ||
| 91 | |||
| 92 | #define TYPE(a,b) .type a,b | ||
| 93 | #define SIZE(a,b) .size a,b | ||
| 94 | |||
| 95 | #if defined(OUT) || (defined(BSDI) && !defined(ELF)) | ||
| 96 | $tmp | ||
| 97 | #endif | ||
| 98 | |||
| 99 | #ifdef OUT | ||
| 100 | #define OK 1 | ||
| 101 | #define ALIGN 4 | ||
| 102 | #if defined(__CYGWIN__) || defined(__DJGPP__) || (__MINGW32__) | ||
| 103 | #undef SIZE | ||
| 104 | #undef TYPE | ||
| 105 | #define SIZE(a,b) | ||
| 106 | #define TYPE(a,b) .def a; .scl 2; .type 32; .endef | ||
| 107 | #endif /* __CYGWIN || __DJGPP */ | ||
| 108 | #endif | ||
| 109 | |||
| 110 | #if defined(BSDI) && !defined(ELF) | ||
| 111 | #define OK 1 | ||
| 112 | #define ALIGN 4 | ||
| 113 | #undef SIZE | ||
| 114 | #undef TYPE | ||
| 115 | #define SIZE(a,b) | ||
| 116 | #define TYPE(a,b) | ||
| 117 | #endif | ||
| 118 | |||
| 119 | #if defined(ELF) || defined(SOL) | ||
| 120 | #define OK 1 | ||
| 121 | #define ALIGN 16 | ||
| 122 | #endif | ||
| 123 | |||
| 124 | #ifndef OK | ||
| 125 | You need to define one of | ||
| 126 | ELF - elf systems - linux-elf, NetBSD and DG-UX | ||
| 127 | OUT - a.out systems - linux-a.out and FreeBSD | ||
| 128 | SOL - solaris systems, which are elf with strange comment lines | ||
| 129 | BSDI - a.out with a very primative version of as. | ||
| 130 | #endif | ||
| 131 | |||
| 132 | /* Let the Assembler begin :-) */ | ||
| 133 | EOF | ||
| 134 | } | ||
| 135 | 220 | ||
| 136 | 1; | 221 | 1; |
