x86_64-xlate.pl: implement indirect jump/calls, support for Win64 SEH.
[openssl.git] / crypto / perlasm / x86gas.pl
1 #!/usr/bin/env perl
2
3 package x86gas;
4
5 *out=\@::out;
6
7 $::lbdecor=$::aout?"L":".L";            # local label decoration
8 $nmdecor=($::aout or $::coff)?"_":"";   # external name decoration
9
10 $initseg="";
11
12 $align=16;
13 $align=log($align)/log(2) if ($::aout);
14 $com_start="#" if ($::aout or $::coff);
15
16 sub opsize()
17 { my $reg=shift;
18     if    ($reg =~ m/^%e/o)             { "l"; }
19     elsif ($reg =~ m/^%[a-d][hl]$/o)    { "b"; }
20     elsif ($reg =~ m/^%[xm]/o)          { undef; }
21     else                                { "w"; }
22 }
23
24 # swap arguments;
25 # expand opcode with size suffix;
26 # prefix numeric constants with $;
27 sub ::generic
28 { my($opcode,$dst,$src)=@_;
29   my($tmp,$suffix,@arg);
30
31     if (defined($src))
32     {   $src =~ s/^(e?[a-dsixphl]{2})$/%$1/o;
33         $src =~ s/^(x?mm[0-7])$/%$1/o;
34         $src =~ s/^(\-?[0-9]+)$/\$$1/o;
35         $src =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o;
36         push(@arg,$src);
37     }
38     if (defined($dst))
39     {   $dst =~ s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o;
40         $dst =~ s/^(x?mm[0-7])$/%$1/o;
41         $dst =~ s/^(\-?[0-9]+)$/\$$1/o          if(!defined($src));
42         $dst =~ s/^(\-?0x[0-9a-f]+)$/\$$1/o     if(!defined($src));
43         push(@arg,$dst);
44     }
45
46     if    ($dst =~ m/^%/o)      { $suffix=&opsize($dst); }
47     elsif ($src =~ m/^%/o)      { $suffix=&opsize($src); }
48     else                        { $suffix="l";           }
49     undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o);
50
51     if ($#_==0)                         { &::emit($opcode);             }
52     elsif ($opcode =~ m/^j/o && $#_==1) { &::emit($opcode,@arg);        }
53     elsif ($opcode eq "call" && $#_==1) { &::emit($opcode,@arg);        }
54     elsif ($opcode =~ m/^set/&& $#_==1) { &::emit($opcode,@arg);        }
55     else                                { &::emit($opcode.$suffix,@arg);}
56
57   1;
58 }
59 #
60 # opcodes not covered by ::generic above, mostly inconsistent namings...
61 #
62 sub ::movzx     { &::movzb(@_);                 }
63 sub ::pushfd    { &::pushfl;                    }
64 sub ::popfd     { &::popfl;                     }
65 sub ::cpuid     { &::emit(".byte\t0x0f,0xa2");  }
66 sub ::rdtsc     { &::emit(".byte\t0x0f,0x31");  }
67
68 sub ::call      { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
69 sub ::call_ptr  { &::generic("call","*$_[0]");  }
70 sub ::jmp_ptr   { &::generic("jmp","*$_[0]");   }
71
72 *::bswap = sub  { &::emit("bswap","%$_[0]");    } if (!$::i386);
73
74 *::pshufw = sub
75 { my($dst,$src,$magic)=@_;
76     &::emit("pshufw","\$$magic","%$src","%$dst");
77 };
78 *::shld = sub
79 { my($dst,$src,$bits)=@_;
80     &::emit("shldl",$bit eq "cl"?"%cl":"\$$bits","%$src","%$dst");
81 };
82 *::shrd = sub
83 { my($dst,$src,$bits)=@_;
84     &::emit("shrdl",$bit eq "cl"?"%cl":"\$$bits","%$src","%$dst");
85 };
86
87 sub ::DWP
88 { my($addr,$reg1,$reg2,$idx)=@_;
89   my $ret="";
90
91     $addr =~ s/^\s+//;
92     # prepend global references with optional underscore
93     $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
94
95     $reg1 = "%$reg1" if ($reg1);
96     $reg2 = "%$reg2" if ($reg2);
97
98     $ret .= $addr if (($addr ne "") && ($addr ne 0));
99
100     if ($reg2)
101     {   $idx!= 0 or $idx=1;
102         $ret .= "($reg1,$reg2,$idx)";
103     }
104     elsif ($reg1)
105     {   $ret .= "($reg1)";      }
106
107   $ret;
108 }
109 sub ::QWP       { &::DWP(@_);   }
110 sub ::BP        { &::DWP(@_);   }
111 sub ::BC        { @_;           }
112 sub ::DWC       { @_;           }
113
114 sub ::file
115 {   push(@out,".file\t\"$_[0].s\"\n.text\n");   }
116
117 sub ::function_begin_B
118 { my $func=shift;
119   my $global=($func !~ /^_/);
120   my $begin="${::lbdecor}_${func}_begin";
121
122     &::LABEL($func,$global?"$begin":"$nmdecor$func");
123     $func=$nmdecor.$func;
124
125     push(@out,".globl\t$func\n")        if ($global);
126     if ($::coff)
127     {   push(@out,".def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
128     elsif (($::aout and !$::pic) or $::macosx)
129     { }
130     else
131     {   push(@out,".type        $func,\@function\n"); }
132     push(@out,".align\t$align\n");
133     push(@out,"$func:\n");
134     push(@out,"$begin:\n")              if ($global);
135     $::stack=4;
136 }
137
138 sub ::function_end_B
139 { my $func=shift;
140     push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
141     $::stack=0;
142     &::wipe_labels();
143 }
144
145 sub ::comment
146         {
147         if (!defined($com_start) or $::elf)
148                 {       # Regarding $::elf above...
149                         # GNU and SVR4 as'es use different comment delimiters,
150                 push(@out,"\n");        # so we just skip ELF comments...
151                 return;
152                 }
153         foreach (@_)
154                 {
155                 if (/^\s*$/)
156                         { push(@out,"\n"); }
157                 else
158                         { push(@out,"\t$com_start $_ $com_end\n"); }
159                 }
160         }
161
162 sub ::external_label
163 {   foreach(@_) { &::LABEL($_,$nmdecor.$_); }   }
164
165 sub ::public_label
166 {   push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");   }
167
168 sub ::file_end
169 {   if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
170         my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,4";
171         if ($::elf)     { push (@out,"$tmp,4\n"); }
172         else            { push (@out,"$tmp\n"); }
173     }
174     if ($::macosx)
175     {   if (%non_lazy_ptr)
176         {   push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
177             foreach $i (keys %non_lazy_ptr)
178             {   push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n");   }
179         }
180     }
181     push(@out,$initseg) if ($initseg);
182 }
183
184 sub ::data_byte {   push(@out,".byte\t".join(',',@_)."\n");   }
185 sub ::data_word {   push(@out,".long\t".join(',',@_)."\n");   }
186
187 sub ::align
188 { my $val=$_[0],$p2,$i;
189     if ($::aout)
190     {   for ($p2=0;$val!=0;$val>>=1) { $p2++; }
191         $val=$p2-1;
192         $val.=",0x90";
193     }
194     push(@out,".align\t$val\n");
195 }
196
197 sub ::picmeup
198 { my($dst,$sym,$base,$reflabel)=@_;
199
200     if ($::pic && ($::elf || $::aout))
201     {   if (!defined($base))
202         {   &::call(&::label("PIC_me_up"));
203             &::set_label("PIC_me_up");
204             &::blindpop($dst);
205             $base=$dst;
206             $reflabel=&::label("PIC_me_up");
207         }
208         if ($::macosx)
209         {   my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
210             &::mov($dst,&::DWP("$indirect-$reflabel",$base));
211             $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
212         }
213         else
214         {   &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
215                             $base));
216             &::mov($dst,&::DWP("$sym\@GOT",$dst));
217         }
218     }
219     else
220     {   &::lea($dst,&::DWP($sym));      }
221 }
222
223 sub ::initseg
224 { my $f=$nmdecor.shift;
225
226     if ($::elf)
227     {   $initseg.=<<___;
228 .section        .init
229         call    $f
230         jmp     .Linitalign
231 .align  $align
232 .Linitalign:
233 ___
234     }
235     elsif ($::coff)
236     {   $initseg.=<<___;        # applies to both Cygwin and Mingw
237 .section        .ctors
238 .long   $f
239 ___
240     }
241     elsif ($::macosx)
242     {   $initseg.=<<___;
243 .mod_init_func
244 .align 2
245 .long   $f
246 ___
247     }
248     elsif ($::aout)
249     {   my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
250         $initseg.=".text\n";
251         $initseg.=".type        $ctor,\@function\n" if ($::pic);
252         $initseg.=<<___;        # OpenBSD way...
253 .globl  $ctor
254 .align  2
255 $ctor:
256         jmp     $f
257 ___
258     }
259 }
260
261 1;