You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

264 lines
6.1 KiB

  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 ($::macosx) {
  87. push(@out,".private_extern\t$func\n");
  88. } else {
  89. push(@out,".hidden\t$func\n");
  90. }
  91. if ($::coff)
  92. { push(@out,".def\t$func;\t.scl\t".(3-$global).";\t.type\t32;\t.endef\n"); }
  93. elsif (($::aout and !$::pic) or $::macosx)
  94. { }
  95. else
  96. { push(@out,".type $func,\@function\n"); }
  97. push(@out,".align\t$align\n");
  98. push(@out,"$func:\n");
  99. push(@out,"$begin:\n") if ($global);
  100. $::stack=4;
  101. }
  102. sub ::function_end_B
  103. { my $func=shift;
  104. push(@out,".size\t$nmdecor$func,.-".&::LABEL($func)."\n") if ($::elf);
  105. $::stack=0;
  106. &::wipe_labels();
  107. }
  108. sub ::comment
  109. {
  110. if (!defined($com_start) or $::elf)
  111. { # Regarding $::elf above...
  112. # GNU and SVR4 as'es use different comment delimiters,
  113. push(@out,"\n"); # so we just skip ELF comments...
  114. return;
  115. }
  116. foreach (@_)
  117. {
  118. if (/^\s*$/)
  119. { push(@out,"\n"); }
  120. else
  121. { push(@out,"\t$com_start $_ $com_end\n"); }
  122. }
  123. }
  124. sub ::external_label
  125. { foreach(@_) { &::LABEL($_,$nmdecor.$_); } }
  126. sub ::public_label
  127. { push(@out,".globl\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  128. sub ::file_end
  129. { if ($::macosx)
  130. { if (%non_lazy_ptr)
  131. { push(@out,".section __IMPORT,__pointers,non_lazy_symbol_pointers\n");
  132. foreach $i (keys %non_lazy_ptr)
  133. { push(@out,"$non_lazy_ptr{$i}:\n.indirect_symbol\t$i\n.long\t0\n"); }
  134. }
  135. }
  136. if (0 && grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out) {
  137. my $tmp=".comm\t${nmdecor}OPENSSL_ia32cap_P,16";
  138. if ($::macosx) { push (@out,"$tmp,2\n"); }
  139. elsif ($::elf) { push (@out,"$tmp,4\n"); }
  140. else { push (@out,"$tmp\n"); }
  141. }
  142. push(@out,$initseg) if ($initseg);
  143. }
  144. sub ::data_byte { push(@out,".byte\t".join(',',@_)."\n"); }
  145. sub ::data_short{ push(@out,".value\t".join(',',@_)."\n"); }
  146. sub ::data_word { push(@out,".long\t".join(',',@_)."\n"); }
  147. sub ::align
  148. { my $val=$_[0];
  149. if ($::aout)
  150. { $val=int(log($val)/log(2));
  151. $val.=",0x90";
  152. }
  153. push(@out,".align\t$val\n");
  154. }
  155. sub ::picmeup
  156. { my($dst,$sym,$base,$reflabel)=@_;
  157. if (($::pic && ($::elf || $::aout)) || $::macosx)
  158. { if (!defined($base))
  159. { &::call(&::label("PIC_me_up"));
  160. &::set_label("PIC_me_up");
  161. &::blindpop($dst);
  162. $base=$dst;
  163. $reflabel=&::label("PIC_me_up");
  164. }
  165. if ($::macosx)
  166. { my $indirect=&::static_label("$nmdecor$sym\$non_lazy_ptr");
  167. &::mov($dst,&::DWP("$indirect-$reflabel",$base));
  168. $non_lazy_ptr{"$nmdecor$sym"}=$indirect;
  169. }
  170. elsif ($sym eq "OPENSSL_ia32cap_P" && $::elf>0)
  171. { &::lea($dst,&::DWP("$sym-$reflabel",$base)); }
  172. else
  173. { &::lea($dst,&::DWP("_GLOBAL_OFFSET_TABLE_+[.-$reflabel]",
  174. $base));
  175. &::mov($dst,&::DWP("$sym\@GOT",$dst));
  176. }
  177. }
  178. else
  179. { &::lea($dst,&::DWP($sym)); }
  180. }
  181. sub ::initseg
  182. { my $f=$nmdecor.shift;
  183. if ($::android)
  184. { $initseg.=<<___;
  185. .section .init_array
  186. .align 4
  187. .long $f
  188. ___
  189. }
  190. elsif ($::elf)
  191. { $initseg.=<<___;
  192. .section .init
  193. call $f
  194. ___
  195. }
  196. elsif ($::coff)
  197. { $initseg.=<<___; # applies to both Cygwin and Mingw
  198. .section .ctors
  199. .long $f
  200. ___
  201. }
  202. elsif ($::macosx)
  203. { $initseg.=<<___;
  204. .mod_init_func
  205. .align 2
  206. .long $f
  207. ___
  208. }
  209. elsif ($::aout)
  210. { my $ctor="${nmdecor}_GLOBAL_\$I\$$f";
  211. $initseg.=".text\n";
  212. $initseg.=".type $ctor,\@function\n" if ($::pic);
  213. $initseg.=<<___; # OpenBSD way...
  214. .globl $ctor
  215. .align 2
  216. $ctor:
  217. jmp $f
  218. ___
  219. }
  220. }
  221. sub ::dataseg
  222. { push(@out,".data\n"); }
  223. *::hidden = sub { push(@out,".hidden\t$nmdecor$_[0]\n"); } if ($::elf);
  224. 1;