perlasm/x86*: add support to SSE>2 and pclmulqdq. x86_64-xlate.pl provides
[openssl.git] / crypto / perlasm / x86asm.pl
1 #!/usr/bin/env perl
2
3 # require 'x86asm.pl';
4 # &asm_init(<flavor>,"des-586.pl"[,$i386only]);
5 # &function_begin("foo");
6 # ...
7 # &function_end("foo");
8 # &asm_finish
9
10 $out=();
11 $i386=0;
12
13 # AUTOLOAD is this context has quite unpleasant side effect, namely
14 # that typos in function calls effectively go to assembler output,
15 # but on the pros side we don't have to implement one subroutine per
16 # each opcode...
17 sub ::AUTOLOAD
18 { my $opcode = $AUTOLOAD;
19
20     die "more than 4 arguments passed to $opcode" if ($#_>3);
21
22     $opcode =~ s/.*:://;
23     if    ($opcode =~ /^push/) { $stack+=4; }
24     elsif ($opcode =~ /^pop/)  { $stack-=4; }
25
26     &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
27 }
28
29 sub ::emit
30 { my $opcode=shift;
31
32     if ($#_==-1)    { push(@out,"\t$opcode\n");                         }
33     else            { push(@out,"\t$opcode\t".join(',',@_)."\n");       }
34 }
35
36 sub ::LB
37 {   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
38   $1."l";
39 }
40 sub ::HB
41 {   $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
42   $1."h";
43 }
44 sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num);      }
45 sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num);      }
46 sub ::blindpop  { &pop($_[0]); $stack+=4;                               }
47 sub ::wparam    { &DWP($stack+4*$_[0],"esp");                           }
48 sub ::swtmp     { &DWP(4*$_[0],"esp");                                  }
49
50 sub ::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...
62 sub ::movb      { &mov(@_);     }
63 sub ::xorb      { &xor(@_);     }
64 sub ::rotl      { &rol(@_);     }
65 sub ::rotr      { &ror(@_);     }
66 sub ::exch      { &xchg(@_);    }
67 sub ::halt      { &hlt;         }
68 sub ::movz      { &movzx(@_);   }
69 sub ::pushf     { &pushfd;      }
70 sub ::popf      { &popfd;       }
71
72 # 3 argument instructions
73 sub ::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 # SSE>2 instructions
84 my %regrm = (   "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
85                 "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7  );
86 sub ::pextrd
87 { my($dst,$src,$imm)=@_;
88     if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
89     {   &data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm);   }
90 }
91
92 sub ::pinsrd
93 { my($dst,$src,$imm)=@_;
94     if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
95     {   &data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm);   }
96 }
97
98 sub ::pshufb
99 { my($dst,$src)=@_;
100     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
101     {   &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2);        }
102 }
103
104 # AESNI extenstion
105 sub ::aeskeygenassist
106 { my($dst,$src,$imm)=@_;
107     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
108     {   &data_byte(0x66,0x0f,0x3a,0xdf,0xc0|($1<<3)|$2,$imm);   }
109 }
110 sub ::aescommon
111 { my($opcodelet,$dst,$src)=@_;
112     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
113     {   &data_byte(0x66,0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2);  }
114 }
115 sub ::aesimc            { ::aescommon(0xdb,@_); }
116 sub ::aesenc            { ::aescommon(0xdc,@_); }
117 sub ::aesenclast        { ::aescommon(0xdd,@_); }
118 sub ::aesdec            { ::aescommon(0xde,@_); }
119 sub ::aesdeclast        { ::aescommon(0xdf,@_); }
120
121 sub ::pclmulqdq
122 { my($dst,$src,$imm)=@_;
123     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
124     {   &data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm);   }
125 }
126
127 # label management
128 $lbdecor="L";           # local label decoration, set by package
129 $label="000";
130
131 sub ::islabel           # see is argument is a known label
132 { my $i;
133     foreach $i (values %label) { return $i if ($i eq $_[0]); }
134   $label{$_[0]};        # can be undef
135 }
136
137 sub ::label             # instantiate a function-scope label
138 {   if (!defined($label{$_[0]}))
139     {   $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++;   }
140   $label{$_[0]};
141 }
142
143 sub ::LABEL             # instantiate a file-scope label
144 {   $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
145   $label{$_[0]};
146 }
147
148 sub ::static_label      { &::LABEL($_[0],$lbdecor.$_[0]); }
149
150 sub ::set_label_B       { push(@out,"@_:\n"); }
151 sub ::set_label
152 { my $label=&::label($_[0]);
153     &::align($_[1]) if ($_[1]>1);
154     &::set_label_B($label);
155   $label;
156 }
157
158 sub ::wipe_labels       # wipes function-scope labels
159 {   foreach $i (keys %label)
160     {   delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
161 }
162
163 # subroutine management
164 sub ::function_begin
165 {   &function_begin_B(@_);
166     $stack=4;
167     &push("ebp");
168     &push("ebx");
169     &push("esi");
170     &push("edi");
171 }
172
173 sub ::function_end
174 {   &pop("edi");
175     &pop("esi");
176     &pop("ebx");
177     &pop("ebp");
178     &ret();
179     &function_end_B(@_);
180     $stack=0;
181     &wipe_labels();
182 }
183
184 sub ::function_end_A
185 {   &pop("edi");
186     &pop("esi");
187     &pop("ebx");
188     &pop("ebp");
189     &ret();
190     $stack+=16; # readjust esp as if we didn't pop anything
191 }
192
193 sub ::asciz
194 { my @str=unpack("C*",shift);
195     push @str,0;
196     while ($#str>15) {
197         &data_byte(@str[0..15]);
198         foreach (0..15) { shift @str; }
199     }
200     &data_byte(@str) if (@str);
201 }
202
203 sub ::asm_finish
204 {   &file_end();
205     print @out;
206 }
207
208 sub ::asm_init
209 { my ($type,$fn,$cpu)=@_;
210
211     $filename=$fn;
212     $i386=$cpu;
213
214     $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=0;
215     if    (($type eq "elf"))
216     {   $elf=1;                 require "x86gas.pl";    }
217     elsif (($type eq "a\.out"))
218     {   $aout=1;                require "x86gas.pl";    }
219     elsif (($type eq "coff" or $type eq "gaswin"))
220     {   $coff=1;                require "x86gas.pl";    }
221     elsif (($type eq "win32n"))
222     {   $win32=1;               require "x86nasm.pl";   }
223     elsif (($type eq "nw-nasm"))
224     {   $netware=1;             require "x86nasm.pl";   }
225     #elsif (($type eq "nw-mwasm"))
226     #{  $netware=1; $mwerks=1;  require "x86nasm.pl";   }
227     elsif (($type eq "win32"))
228     {   $win32=1;               require "x86masm.pl";   }
229     elsif (($type eq "macosx"))
230     {   $aout=1; $macosx=1;     require "x86gas.pl";    }
231     else
232     {   print STDERR <<"EOF";
233 Pick one target type from
234         elf     - Linux, FreeBSD, Solaris x86, etc.
235         a.out   - DJGPP, elder OpenBSD, etc.
236         coff    - GAS/COFF such as Win32 targets
237         win32n  - Windows 95/Windows NT NASM format
238         nw-nasm - NetWare NASM format
239         macosx  - Mac OS X
240 EOF
241         exit(1);
242     }
243
244     $pic=0;
245     for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
246
247     $filename =~ s/\.pl$//;
248     &file($filename);
249 }
250
251 1;