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