x86gas.pl 5.8 KB

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