Add -show_chain option to print out verified chain.
[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     else
91     {   &::generic("pextrd",@_);                }
92 }
93
94 sub ::pinsrd
95 { my($dst,$src,$imm)=@_;
96     if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
97     {   &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); }
98     else
99     {   &::generic("pinsrd",@_);                }
100 }
101
102 sub ::pshufb
103 { my($dst,$src)=@_;
104     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
105     {   &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2);        }
106     else
107     {   &::generic("pshufb",@_);                }
108 }
109
110 sub ::palignr
111 { my($dst,$src,$imm)=@_;
112     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
113     {   &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); }
114     else
115     {   &::generic("palignr",@_);               }
116 }
117
118 sub ::pclmulqdq
119 { my($dst,$src,$imm)=@_;
120     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
121     {   &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); }
122     else
123     {   &::generic("pclmulqdq",@_);             }
124 }
125
126 sub ::rdrand
127 { my ($dst)=@_;
128     if ($dst =~ /(e[a-dsd][ixp])/)
129     {   &::data_byte(0x0f,0xc7,0xf0|$regrm{$dst});      }
130     else
131     {   &::generic("rdrand",@_);        }
132 }
133
134 sub ::rdseed
135 { my ($dst)=@_;
136     if ($dst =~ /(e[a-dsd][ixp])/)
137     {   &::data_byte(0x0f,0xc7,0xf8|$regrm{$dst});      }
138     else
139     {   &::generic("rdrand",@_);        }
140 }
141
142 sub rxb {
143  local *opcode=shift;
144  my ($dst,$src1,$src2,$rxb)=@_;
145
146    $rxb|=0x7<<5;
147    $rxb&=~(0x04<<5) if($dst>=8);
148    $rxb&=~(0x01<<5) if($src1>=8);
149    $rxb&=~(0x02<<5) if($src2>=8);
150    push @opcode,$rxb;
151 }
152
153 sub ::vprotd
154 { my $args=join(',',@_);
155     if ($args =~ /xmm([0-7]),xmm([0-7]),([x0-9a-f]+)/)
156     { my @opcode=(0x8f);
157         rxb(\@opcode,$1,$2,-1,0x08);
158         push @opcode,0x78,0xc2;
159         push @opcode,0xc0|($2&7)|(($1&7)<<3);           # ModR/M
160         my $c=$3;
161         push @opcode,$c=~/^0/?oct($c):$c;
162         &::data_byte(@opcode);
163     }
164     else
165     {   &::generic("vprotd",@_);        }
166 }
167
168 # label management
169 $lbdecor="L";           # local label decoration, set by package
170 $label="000";
171
172 sub ::islabel           # see is argument is a known label
173 { my $i;
174     foreach $i (values %label) { return $i if ($i eq $_[0]); }
175   $label{$_[0]};        # can be undef
176 }
177
178 sub ::label             # instantiate a function-scope label
179 {   if (!defined($label{$_[0]}))
180     {   $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++;   }
181   $label{$_[0]};
182 }
183
184 sub ::LABEL             # instantiate a file-scope label
185 {   $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
186   $label{$_[0]};
187 }
188
189 sub ::static_label      { &::LABEL($_[0],$lbdecor.$_[0]); }
190
191 sub ::set_label_B       { push(@out,"@_:\n"); }
192 sub ::set_label
193 { my $label=&::label($_[0]);
194     &::align($_[1]) if ($_[1]>1);
195     &::set_label_B($label);
196   $label;
197 }
198
199 sub ::wipe_labels       # wipes function-scope labels
200 {   foreach $i (keys %label)
201     {   delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
202 }
203
204 # subroutine management
205 sub ::function_begin
206 {   &function_begin_B(@_);
207     $stack=4;
208     &push("ebp");
209     &push("ebx");
210     &push("esi");
211     &push("edi");
212 }
213
214 sub ::function_end
215 {   &pop("edi");
216     &pop("esi");
217     &pop("ebx");
218     &pop("ebp");
219     &ret();
220     &function_end_B(@_);
221     $stack=0;
222     &wipe_labels();
223 }
224
225 sub ::function_end_A
226 {   &pop("edi");
227     &pop("esi");
228     &pop("ebx");
229     &pop("ebp");
230     &ret();
231     $stack+=16; # readjust esp as if we didn't pop anything
232 }
233
234 sub ::asciz
235 { my @str=unpack("C*",shift);
236     push @str,0;
237     while ($#str>15) {
238         &data_byte(@str[0..15]);
239         foreach (0..15) { shift @str; }
240     }
241     &data_byte(@str) if (@str);
242 }
243
244 sub ::asm_finish
245 {   &file_end();
246     print @out;
247 }
248
249 sub ::asm_init
250 { my ($type,$fn,$cpu)=@_;
251
252     $filename=$fn;
253     $i386=$cpu;
254
255     $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
256     if    (($type eq "elf"))
257     {   $elf=1;                 require "x86gas.pl";    }
258     elsif (($type eq "a\.out"))
259     {   $aout=1;                require "x86gas.pl";    }
260     elsif (($type eq "coff" or $type eq "gaswin"))
261     {   $coff=1;                require "x86gas.pl";    }
262     elsif (($type eq "win32n"))
263     {   $win32=1;               require "x86nasm.pl";   }
264     elsif (($type eq "nw-nasm"))
265     {   $netware=1;             require "x86nasm.pl";   }
266     #elsif (($type eq "nw-mwasm"))
267     #{  $netware=1; $mwerks=1;  require "x86nasm.pl";   }
268     elsif (($type eq "win32"))
269     {   $win32=1;               require "x86masm.pl";   }
270     elsif (($type eq "macosx"))
271     {   $aout=1; $macosx=1;     require "x86gas.pl";    }
272     elsif (($type eq "android"))
273     {   $elf=1; $android=1;     require "x86gas.pl";    }
274     else
275     {   print STDERR <<"EOF";
276 Pick one target type from
277         elf     - Linux, FreeBSD, Solaris x86, etc.
278         a.out   - DJGPP, elder OpenBSD, etc.
279         coff    - GAS/COFF such as Win32 targets
280         win32n  - Windows 95/Windows NT NASM format
281         nw-nasm - NetWare NASM format
282         macosx  - Mac OS X
283 EOF
284         exit(1);
285     }
286
287     $pic=0;
288     for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
289
290     $filename =~ s/\.pl$//;
291     &file($filename);
292 }
293
294 sub ::hidden {}
295
296 1;