3f2eb7ae054c6370a4f6456e592aaf416d8f1dd0
[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(@_)
164     {   push(@out,".extern\t".&::LABEL($_,$nmdecor.$_)."\n");   }
165 }
166
167 sub ::public_label
168 {   push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");   }
169
170 sub ::file_end
171 {   if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
172         my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,4";
173         if ($::elf)     { push (@out,"$tmp,4\n"); }
174         else            { push (@out,"$tmp\n"); }
175     }
176     if ($::macosx)
177     {   grep {s/^\.extern\s+.*$//} @out;
178         if (%non_lazy_ptr)
179         {   push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
180             foreach $i (keys %non_lazy_ptr)
181             {   push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n");   }
182         }
183     }
184     push(@out,$initseg) if ($initseg);
185 }
186
187 sub ::data_byte {   push(@out,".byte\t".join(',',@_)."\n");   }
188 sub ::data_word {   push(@out,".long\t".join(',',@_)."\n");   }
189
190 sub ::align
191 { my $val=$_[0],$p2,$i;
192     if ($::aout)
193     {   for ($p2=0;$val!=0;$val>>=1) { $p2++; }
194         $val=$p2-1;
195         $val.=",0x90";
196     }
197     push(@out,".align\t$val\n");
198 }
199
200 sub ::picmeup
201 { my($dst,$sym,$base,$reflabel)=@_;
202
203     if ($::pic && ($::elf || $::aout))
204     {   if (!defined($base))
205         {   &::call(&::label("PIC_me_up"));
206             &::set_label("PIC_me_up");
207             &::blindpop($dst);
208             $base=$dst;
209             $reflabel=&::label("PIC_me_up");
210         }
211         if ($::macosx)
212         {   my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
213             &::mov($dst,&::DWP("$indirect-$reflabel",$base));
214             $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
215         }
216         else
217         {   &::lea($dst,&::DWP("${nmdecor}_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
218                             $base));
219             &::mov($dst,&::DWP("$nmdecor$sym\@GOT",$dst));
220         }
221     }
222     else
223     {   &::lea($dst,&::DWP($sym));      }
224 }
225
226 sub ::initseg
227 { my $f=$nmdecor.shift;
228
229     if ($::elf)
230     {   $initseg.=<<___;
231 .section        .init
232         call    $f
233         jmp     .Linitalign
234 .align  $align
235 .Linitalign:
236 ___
237     }
238     elsif ($::coff)
239     {   $initseg.=<<___;        # applies to both Cygwin and Mingw
240 .section        .ctors
241 .long   $f
242 ___
243     }
244     elsif ($::macosx)
245     {   $initseg.=<<___;
246 .mod_init_func
247 .align 2
248 .long   $f
249 ___
250     }
251     elsif ($::aout)
252     {   my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
253         $initseg.=".text\n";
254         $initseg.=".type        $ctor,\@function\n" if ($::pic);
255         $initseg.=<<___;        # OpenBSD way...
256 .globl  $ctor
257 .align  2
258 $ctor:
259         jmp     $f
260 ___
261     }
262 }
263
264 1;