fa789a78b95acb7d4b76905ce5f578836a9c6002
[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)
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 {   push(@out,".extern\t".&::LABEL($_[0],$nmdecor.$_[0])."\n");   }
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     push(@out,$initseg) if ($initseg);
175 }
176
177 sub ::data_byte {   push(@out,".byte\t".join(',',@_)."\n");   }
178 sub ::data_word {   push(@out,".long\t".join(',',@_)."\n");   }
179
180 sub ::align
181 { my $val=$_[0],$p2,$i;
182     if ($::aout)
183     {   for ($p2=0;$val!=0;$val>>=1) { $p2++; }
184         $val=$p2-1;
185         $val.=",0x90";
186     }
187     push(@out,".align\t$val\n");
188 }
189
190 sub ::picmeup
191 { my($dst,$sym,$base,$reflabel)=@_;
192
193     if ($::pic && ($::elf || $::aout))
194     {   if (!defined($base))
195         {   &::call(&::label("PIC_me_up"));
196             &::set_label("PIC_me_up");
197             &::blindpop($dst);
198             &::add($dst,"\$${nmdecor}_GLOBAL_OFFSET_TABLE_+[.-".
199                             &::label("PIC_me_up") . "]");
200         }
201         else
202         {   &::lea($dst,&::DWP("${nmdecor}_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
203                             $base));
204         }
205         &::mov($dst,&::DWP("$nmdecor$sym\@GOT",$dst));
206     }
207     else
208     {   &::lea($dst,&::DWP($sym));      }
209 }
210
211 sub ::initseg
212 { my $f=$nmdecor.shift;
213
214     if ($::elf)
215     {   $initseg.=<<___;
216 .section        .init
217         call    $f
218         jmp     .Linitalign
219 .align  $align
220 .Linitalign:
221 ___
222     }
223     elsif ($::coff)
224     {   $initseg.=<<___;        # applies to both Cygwin and Mingw
225 .section        .ctors
226 .long   $f
227 ___
228     }
229     elsif ($::aout)
230     {   my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
231         $initseg.=".text\n";
232         $initseg.=".type        $ctor,\@function\n" if ($::pic);
233         $initseg.=<<___;        # OpenBSD way...
234 .globl  $ctor
235 .align  2
236 $ctor:
237         jmp     $f
238 ___
239     }
240 }
241
242 1;