x86gas.pl 5.8 KB

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