| # You may obtain a copy of the License at |
| 7 | # |
| 8 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 |
| 16 |
| 17 | package x86gas; |
| 18 |
| 19 | *out=\@::out; |
| 20 |
| 21 | $::lbdecor=$::aout?"L":".L"; # local label decoration |
| 22 | $nmdecor=($::aout or $::coff)?"_":""; # external name decoration |
| 23 |
| 24 | $initseg=""; |
| 25 |
| 26 | $align=16; |
| 27 | $align=log($align)/log(2) if ($::aout); |
| 28 | $com_start="#" if ($::aout or $::coff); |
| 29 |
| 30 | sub opsize() |
| 31 | { my $reg=shift; |
| 32 | if ($reg =~ m/^%e/o) { "l"; } |
| 33 | elsif ($reg =~ m/^%[a-d][hl]$/o) { "b"; } |
| 34 | elsif ($reg =~ m/^%[yxm]/o) { undef; } |
| 35 | else { "w"; } |
| 36 | } |
| 37 |
| 38 | # swap arguments; |
| 39 | # expand opcode with size suffix; |
| 40 | # prefix numeric constants with $; |
| 41 | sub ::generic |
| 42 | { my($opcode,@arg)=@_; |
| 43 | my($suffix,$dst,$src); |
| 44 |
| 45 | @arg=reverse(@arg); |
| 46 |
| 47 | for (@arg) |
| 48 | { s/^(\*?)(e?[a-dsixphl]{2})$/$1%$2/o; # gp registers |
| 49 | s/^([xy]?mm[0-7])$/%$1/o; # xmm/mmx registers |
| 50 | s/^(\-?[0-9]+)$/\$$1/o; # constants |
| 51 | s/^(\-?0x[0-9a-f]+)$/\$$1/o; # constants |
| 52 | } |
| 53 |
| 54 | $dst = $arg[$#arg] if ($#arg>=0); |
| 55 | $src = $arg[$#arg-1] if ($#arg>=1); |
| 56 | if ($dst =~ m/^%/o) { $suffix=&opsize($dst); } |
| 57 | elsif ($src =~ m/^%/o) { $suffix=&opsize($src); } |
| 58 | else { $suffix="l"; } |
| 59 | undef $suffix if ($dst =~ m/^%[xm]/o || $src =~ m/^%[xm]/o); |
| 60 |
| 61 | if ($#_==0) { &::emit($opcode); } |
| 62 | elsif ($#_==1 && $opcode =~ m/^(call|clflush|j|loop|set)/o) |
| 63 | { &::emit($opcode,@arg); } |
| 64 | else { &::emit($opcode.$suffix,@arg);} |
| 65 |
| 66 | 1; |
| 67 | } |
| 68 | # |
| 69 | # opcodes not covered by ::generic above, mostly inconsistent namings... |
| 70 | # |
| 71 | sub ::movzx { &::movzb(@_); } |
| 72 | sub ::pushfd { &::pushfl; } |
| 73 | sub ::popfd { &::popfl; } |
| 74 | sub ::cpuid { &::emit(".byte\t0x0f,0xa2"); } |
| 75 | sub ::rdtsc { &::emit(".byte\t0x0f,0x31"); } |
| 76 |
| 77 | sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); } |
| 78 | sub ::call_ptr { &::generic("call","*$_[0]"); } |
| 79 | sub ::jmp_ptr { &::generic("jmp","*$_[0]"); } |
| 80 |
| 81 | *::bswap = sub { &::emit("bswap","%$_[0]"); } if (!$::i386); |
| 82 |
| 83 | sub ::DWP |
| 84 | { my($addr,$reg1,$reg2,$idx)=@_; |
| 85 | my $ret=""; |
| 86 |
| 87 | if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; } |
| 88 |
| 89 | $addr =~ s/^\s+//; |
| 90 | # prepend global references with optional underscore |
| 91 | $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige; |
| 92 |
| 93 | $reg1 = "%$reg1" if ($reg1); |
| 94 | $reg2 = "%$reg2" if ($reg2); |
| 95 |
| 96 | $ret .= $addr if (($addr ne "") && ($addr ne 0)); |
| 97 |
| 98 | if ($reg2) |
| 99 | { $idx!= 0 or $idx=1; |
| 100 | $ret .= "($reg1,$reg2,$idx)"; |
| 101 | } |
| 102 | elsif ($reg1) |
| 103 | { $ret .= "($reg1)"; } |
| 104 |
| 105 | $ret; |
| 106 | } |
| 107 | sub ::QWP { &::DWP(@_); } |
| 108 | sub ::BP { &::DWP(@_); } |
| 109 | sub ::WP { &::DWP(@_); } |
| 110 | sub ::BC { @_; } |
| 111 | sub ::DWC { @_; } |
| 112 |
| 113 | sub ::file |
| 114 | { push(@out,".text\n"); } |
| 115 |
| 116 | sub ::function_begin_B |
| 117 | { my $func=shift; |
| 118 | my $global=($func !~ /^_/); |
| 119 | my $begin="${::lbdecor}_${func}_begin"; |
| 120 |
| 121 | &::LABEL($func,$global?"$begin":"$nmdecor$func"); |
| 122 | $func=$nmdecor.$func; |
| 123 |
| 124 | push(@out,".globl\t$func\n") if ($global); |
| 125 | if ($::macosx) { |
| 126 | push(@out,".private_extern\t$func\n"); |
| 127 | } else { |
| 128 | push(@out,".hidden\t$func\n"); |
| 129 | } |
| 130 | if ($::coff) |
| 131 | { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); } |
| 132 | elsif (($::aout and !$::pic) or $::macosx) |
| 133 | { } |
| 134 | else |
| 135 | { push(@out,".type $func,\@function\n"); } |
| 136 | push(@out,".align\t$align\n"); |
| 137 | push(@out,"$func:\n"); |
| 138 | push(@out,"$begin:\n") if ($global); |
| 139 | $::stack=4; |
| 140 | } |
| 141 |
| 142 | sub ::function_end_B |
| 143 | { my $func=shift; |
| 144 | push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf); |
| 145 | $::stack=0; |
| 146 | &::wipe_labels(); |
| 147 | } |
| 148 |
| 149 | sub ::comment |
| 150 | { |
| 151 | if (!defined($com_start) or $::elf) |
| 152 | { # Regarding $::elf above... |
| 153 | # GNU and SVR4 as'es use different comment delimiters, |
| 154 | push(@out,"\n"); # so we just skip ELF comments... |
| 155 | return; |
| 156 | } |
| 157 | foreach (@_) |
| 158 | { |
| 159 | if (/^\s*$/) |
| 160 | { push(@out,"\n"); } |
| 161 | else |
| 162 | { push(@out,"\t$com_start $_ $com_end\n"); } |
| 163 | } |
| 164 | } |
| 165 |
| 166 | sub ::external_label |
| 167 | { foreach(@_) { &::LABEL($_,$nmdecor.$_); } } |
| 168 |
| 169 | sub ::public_label |
| 170 | { push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); } |
| 171 |
| 172 | sub ::file_end |
| 173 | { if ($::macosx) |
| 174 | { if (%non_lazy_ptr) |
| 175 | { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n"); |
| 176 | foreach $i (keys %non_lazy_ptr) |
| 177 | { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); } |
| 178 | } |
| 179 | } |
| 180 | if (0 && grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) { |
| 181 | my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16"; |
| 182 | if ($::macosx) { push (@out,"$tmp,2\n"); } |
| 183 | elsif ($::elf) { push (@out,"$tmp,4\n"); } |
| 184 | else { push (@out,"$tmp\n"); } |
| 185 | } |
| 186 | push(@out,$initseg) if ($initseg); |
| 187 | } |
| 188 |
| 189 | sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); } |
| 190 | sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); } |
| 191 | sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); } |
| 192 |
| 193 | sub ::align |
| 194 | { my $val=$_[0]; |
| 195 | if ($::aout) |
| 196 | { $val=int(log($val)/log(2)); |
| 197 | $val.=",0x90"; |
| 198 | } |
| 199 | push(@out,".align\t$val\n"); |
| 200 | } |
| 201 |
| 202 | sub ::picmeup |
| 203 | { my($dst,$sym,$base,$reflabel)=@_; |
| 204 |
| 205 | if (($::pic && ($::elf || $::aout)) || $::macosx) |
| 206 | { if (!defined($base)) |
| 207 | { &::call(&::label("PIC_me_up")); |
| 208 | &::set_label("PIC_me_up"); |
| 209 | &::blindpop($dst); |
| 210 | $base=$dst; |
| 211 | $reflabel=&::label("PIC_me_up"); |
| 212 | } |
| 213 | if ($::macosx) |
| 214 | { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr"); |
| 215 | &::mov($dst,&::DWP("$indirect-$reflabel",$base)); |
| 216 | $non_lazy_ptr{"$nmdecor$sym"}=$indirect; |
| 217 | } |
| 218 | elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0) |
| 219 | { &::lea($dst,&::DWP("$sym-$reflabel",$base)); } |
| 220 | else |
| 221 | { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]", |
| 222 | $base)); |
| 223 | &::mov($dst,&::DWP("$sym\@GOT",$dst)); |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | { &::lea($dst,&::DWP($sym)); } |
| 228 | } |
| 229 |
| 230 | sub ::initseg |
| 231 | { my $f=$nmdecor.shift; |
| 232 |
| 233 | if ($::android) |
| 234 | { $initseg.=<<___; |
| 235 | .section .init_array |
| 236 | .align 4 |
| 237 | .long $f |
| 238 | ___ |
| 239 | } |
| 240 | elsif ($::elf) |
| 241 | { $initseg.=<<___; |
| 242 | .section .init |
| 243 | call $f |
| 244 | ___ |
| 245 | } |
| 246 | elsif ($::coff) |
| 247 | { $initseg.=<<___; # applies to both Cygwin and Mingw |
| 248 | .section .ctors |
| 249 | .long $f |
| 250 | ___ |
| 251 | } |
| 252 | elsif ($::macosx) |
| 253 | { $initseg.=<<___; |
| 254 | .mod_init_func |
| 255 | .align 2 |
| 256 | .long $f |
| 257 | ___ |
| 258 | } |
| 259 | elsif ($::aout) |
| 260 | { my $ctor="${nmdecor}_GLOBAL_\$I\$$f"; |
| 261 | $initseg.=".text\n"; |
| 262 | $initseg.=".type $ctor,\@function\n" if ($::pic); |
| 263 | $initseg.=<<___; # OpenBSD way... |
| 264 | .globl $ctor |
| 265 | .align 2 |
| 266 | $ctor: |
| 267 | jmp $f |
| 268 | ___ |
| 269 | } |
| 270 | } |
| 271 |
| 272 | sub ::dataseg |
| 273 | { push(@out,".data\n"); } |
| 274 |
| 275 | sub ::preprocessor_ifdef |
| 276 | { my($define)=@_; |
| 277 | push(@out,"#ifdef ${define}\n"); |
| 278 | } |
| 279 |
| 280 | sub ::preprocessor_endif |
| 281 | { push(@out,"#endif\n"); } |
| 282 |
| 283 | *::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf); |
| 284 |
| 285 | 1; |
| 286 |