PR: 2003
[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 # AESNI extenstion
84 sub ::aeskeygenassist
85 { my($dst,$src,$imm)=@_;
86     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
87     {   &data_byte(0x66,0x0f,0x3a,0xdf,0xc0|($1<<3)|$2,$imm);   }
88 }
89 sub ::aescommon
90 { my($opcodelet,$dst,$src)=@_;
91     if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
92     {   &data_byte(0x66,0x0f,0x38,$opcodelet,0xc0|($1<<3)|$2);  }
93 }
94 sub ::aesimc            { ::aescommon(0xdb,@_); }
95 sub ::aesenc            { ::aescommon(0xdc,@_); }
96 sub ::aesenclast        { ::aescommon(0xdd,@_); }
97 sub ::aesdec            { ::aescommon(0xde,@_); }
98 sub ::aesdeclast        { ::aescommon(0xdf,@_); }
99
100 # label management
101 $lbdecor="L";           # local label decoration, set by package
102 $label="000";
103
104 sub ::islabel           # see is argument is a known label
105 { my $i;
106     foreach $i (values %label) { return $i if ($i eq $_[0]); }
107   $label{$_[0]};        # can be undef
108 }
109
110 sub ::label             # instantiate a function-scope label
111 {   if (!defined($label{$_[0]}))
112     {   $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++;   }
113   $label{$_[0]};
114 }
115
116 sub ::LABEL             # instantiate a file-scope label
117 {   $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
118   $label{$_[0]};
119 }
120
121 sub ::static_label      { &::LABEL($_[0],$lbdecor.$_[0]); }
122
123 sub ::set_label_B       { push(@out,"@_:\n"); }
124 sub ::set_label
125 { my $label=&::label($_[0]);
126     &::align($_[1]) if ($_[1]>1);
127     &::set_label_B($label);
128   $label;
129 }
130
131 sub ::wipe_labels       # wipes function-scope labels
132 {   foreach $i (keys %label)
133     {   delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
134 }
135
136 # subroutine management
137 sub ::function_begin
138 {   &function_begin_B(@_);
139     $stack=4;
140     &push("ebp");
141     &push("ebx");
142     &push("esi");
143     &push("edi");
144 }
145
146 sub ::function_end
147 {   &pop("edi");
148     &pop("esi");
149     &pop("ebx");
150     &pop("ebp");
151     &ret();
152     &function_end_B(@_);
153     $stack=0;
154     &wipe_labels();
155 }
156
157 sub ::function_end_A
158 {   &pop("edi");
159     &pop("esi");
160     &pop("ebx");
161     &pop("ebp");
162     &ret();
163     $stack+=16; # readjust esp as if we didn't pop anything
164 }
165
166 sub ::asciz
167 { my @str=unpack("C*",shift);
168     push @str,0;
169     while ($#str>15) {
170         &data_byte(@str[0..15]);
171         foreach (0..15) { shift @str; }
172     }
173     &data_byte(@str) if (@str);
174 }
175
176 sub ::asm_finish
177 {   &file_end();
178     print @out;
179 }
180
181 sub ::asm_init
182 { my ($type,$fn,$cpu)=@_;
183
184     $filename=$fn;
185     $i386=$cpu;
186
187     $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=0;
188     if    (($type eq "elf"))
189     {   $elf=1;                 require "x86gas.pl";    }
190     elsif (($type eq "a\.out"))
191     {   $aout=1;                require "x86gas.pl";    }
192     elsif (($type eq "coff" or $type eq "gaswin"))
193     {   $coff=1;                require "x86gas.pl";    }
194     elsif (($type eq "win32n"))
195     {   $win32=1;               require "x86nasm.pl";   }
196     elsif (($type eq "nw-nasm"))
197     {   $netware=1;             require "x86nasm.pl";   }
198     #elsif (($type eq "nw-mwasm"))
199     #{  $netware=1; $mwerks=1;  require "x86nasm.pl";   }
200     elsif (($type eq "win32"))
201     {   $win32=1;               require "x86masm.pl";   }
202     elsif (($type eq "macosx"))
203     {   $aout=1; $macosx=1;     require "x86gas.pl";    }
204     else
205     {   print STDERR <<"EOF";
206 Pick one target type from
207         elf     - Linux, FreeBSD, Solaris x86, etc.
208         a.out   - DJGPP, elder OpenBSD, etc.
209         coff    - GAS/COFF such as Win32 targets
210         win32n  - Windows 95/Windows NT NASM format
211         nw-nasm - NetWare NASM format
212         macosx  - Mac OS X
213 EOF
214         exit(1);
215     }
216
217     $pic=0;
218     for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
219
220     $filename =~ s/\.pl$//;
221     &file($filename);
222 }
223
224 1;