25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

x86gas.pl 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
  58. $addr =~ s/^\s+//;
  59. # prepend global references with optional underscore
  60. $addr =~ s/^([^\+\-0-9][^\+\-]*)/&::islabel($1) or "$nmdecor$1"/ige;
  61. $reg1 = "%$reg1" if ($reg1);
  62. $reg2 = "%$reg2" if ($reg2);
  63. $ret .= $addr if (($addr ne "") && ($addr ne 0));
  64. if ($reg2)
  65. { $idx!= 0 or $idx=1;
  66. $ret .= "($reg1,$reg2,$idx)";
  67. }
  68. elsif ($reg1)
  69. { $ret .= "($reg1)"; }
  70. $ret;
  71. }
  72. sub ::QWP { &::DWP(@_); }
  73. sub ::BP { &::DWP(@_); }
  74. sub ::WP { &::DWP(@_); }
  75. sub ::BC { @_; }
  76. sub ::DWC { @_; }
  77. sub ::file
  78. { push(@out,".file\t\"$_[0].S\"\n.text\n"); }
  79. sub ::function_begin_B
  80. { my $func=shift;
  81. my $global=($func !~ /^_/);
  82. my $begin="${::lbdecor}_${func}_begin";
  83. &::LABEL($func,$global?"$begin":"$nmdecor$func");
  84. $func=$nmdecor.$func;
  85. push(@out,".globl\t$func\n") if ($global);
  86. if ($::coff)
  87. { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
  88. elsif (($::aout and !$::pic) or $::macosx)
  89. { }
  90. else
  91. { push(@out,".type $func,\@function\n"); }
  92. push(@out,".align\t$align\n");
  93. push(@out,"$func:\n");
  94. push(@out,"$begin:\n") if ($global);
  95. $::stack=4;
  96. }
  97. sub ::function_end_B
  98. { my $func=shift;
  99. push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
  100. $::stack=0;
  101. &::wipe_labels();
  102. }
  103. sub ::comment
  104. {
  105. if (!defined($com_start) or $::elf)
  106. { # Regarding $::elf above...
  107. # GNU and SVR4 as'es use different comment delimiters,
  108. push(@out,"\n"); # so we just skip ELF comments...
  109. return;
  110. }
  111. foreach (@_)
  112. {
  113. if (/^\s*$/)
  114. { push(@out,"\n"); }
  115. else
  116. { push(@out,"\t$com_start $_ $com_end\n"); }
  117. }
  118. }
  119. sub ::external_label
  120. { foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
  121. sub ::public_label
  122. { push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  123. sub ::file_end
  124. { if ($::macosx)
  125. { if (%non_lazy_ptr)
  126. { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
  127. foreach $i (keys %non_lazy_ptr)
  128. { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
  129. }
  130. }
  131. if (0 && grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
  132. my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
  133. if ($::macosx) { push (@out,"$tmp,2\n"); }
  134. elsif ($::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];
  144. if ($::aout)
  145. { $val=int(log($val)/log(2));
  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. elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
  166. { &::lea($dst,&::DWP("$sym-$reflabel",$base)); }
  167. else
  168. { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
  169. $base));
  170. &::mov($dst,&::DWP("$sym\@GOT",$dst));
  171. }
  172. }
  173. else
  174. { &::lea($dst,&::DWP($sym)); }
  175. }
  176. sub ::initseg
  177. { my $f=$nmdecor.shift;
  178. if ($::android)
  179. { $initseg.=<<___;
  180. .section .init_array
  181. .align 4
  182. .long $f
  183. ___
  184. }
  185. elsif ($::elf)
  186. { $initseg.=<<___;
  187. .section .init
  188. call $f
  189. ___
  190. }
  191. elsif ($::coff)
  192. { $initseg.=<<___; # applies to both Cygwin and Mingw
  193. .section .ctors
  194. .long $f
  195. ___
  196. }
  197. elsif ($::macosx)
  198. { $initseg.=<<___;
  199. .mod_init_func
  200. .align 2
  201. .long $f
  202. ___
  203. }
  204. elsif ($::aout)
  205. { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
  206. $initseg.=".text\n";
  207. $initseg.=".type $ctor,\@function\n" if ($::pic);
  208. $initseg.=<<___; # OpenBSD way...
  209. .globl $ctor
  210. .align 2
  211. $ctor:
  212. jmp $f
  213. ___
  214. }
  215. }
  216. sub ::dataseg
  217. { push(@out,".data\n"); }
  218. *::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
  219. 1;