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