x86asm.pl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #!/usr/bin/env perl
  2. # require 'x86asm.pl';
  3. # &asm_init(<flavor>,"des-586.pl"[,$i386only]);
  4. # &function_begin("foo");
  5. # ...
  6. # &function_end("foo");
  7. # &asm_finish
  8. $out=();
  9. $i386=0;
  10. # AUTOLOAD is this context has quite unpleasant side effect, namely
  11. # that typos in function calls effectively go to assembler output,
  12. # but on the pros side we don't have to implement one subroutine per
  13. # each opcode...
  14. sub ::AUTOLOAD
  15. { my $opcode = $AUTOLOAD;
  16. die "more than 4 arguments passed to $opcode" if ($#_>3);
  17. $opcode =~ s/.*:://;
  18. if ($opcode =~ /^push/) { $stack+=4; }
  19. elsif ($opcode =~ /^pop/) { $stack-=4; }
  20. &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
  21. }
  22. sub ::emit
  23. { my $opcode=shift;
  24. if ($#_==-1) { push(@out,"\t$opcode\n"); }
  25. else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
  26. }
  27. sub ::LB
  28. { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
  29. $1."l";
  30. }
  31. sub ::HB
  32. { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
  33. $1."h";
  34. }
  35. sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
  36. sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
  37. sub ::blindpop { &pop($_[0]); $stack+=4; }
  38. sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
  39. sub ::swtmp { &DWP(4*$_[0],"esp"); }
  40. sub ::bswap
  41. { if ($i386) # emulate bswap for i386
  42. { &comment("bswap @_");
  43. &xchg(&HB(@_),&LB(@_));
  44. &ror (@_,16);
  45. &xchg(&HB(@_),&LB(@_));
  46. }
  47. else
  48. { &generic("bswap",@_); }
  49. }
  50. # These are made-up opcodes introduced over the years essentially
  51. # by ignorance, just alias them to real ones...
  52. sub ::movb { &mov(@_); }
  53. sub ::xorb { &xor(@_); }
  54. sub ::rotl { &rol(@_); }
  55. sub ::rotr { &ror(@_); }
  56. sub ::exch { &xchg(@_); }
  57. sub ::halt { &hlt; }
  58. sub ::movz { &movzx(@_); }
  59. sub ::pushf { &pushfd; }
  60. sub ::popf { &popfd; }
  61. # 3 argument instructions
  62. sub ::movq
  63. { my($p1,$p2,$optimize)=@_;
  64. if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
  65. # movq between mmx registers can sink Intel CPUs
  66. { &::pshufw($p1,$p2,0xe4); }
  67. else
  68. { &::generic("movq",@_); }
  69. }
  70. # SSE>2 instructions
  71. my %regrm = ( "eax"=>0, "ecx"=>1, "edx"=>2, "ebx"=>3,
  72. "esp"=>4, "ebp"=>5, "esi"=>6, "edi"=>7 );
  73. sub ::pextrd
  74. { my($dst,$src,$imm)=@_;
  75. if ("$dst:$src" =~ /(e[a-dsd][ixp]):xmm([0-7])/)
  76. { &::data_byte(0x66,0x0f,0x3a,0x16,0xc0|($2<<3)|$regrm{$1},$imm); }
  77. else
  78. { &::generic("pextrd",@_); }
  79. }
  80. sub ::pinsrd
  81. { my($dst,$src,$imm)=@_;
  82. if ("$dst:$src" =~ /xmm([0-7]):(e[a-dsd][ixp])/)
  83. { &::data_byte(0x66,0x0f,0x3a,0x22,0xc0|($1<<3)|$regrm{$2},$imm); }
  84. else
  85. { &::generic("pinsrd",@_); }
  86. }
  87. sub ::pshufb
  88. { my($dst,$src)=@_;
  89. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  90. { &data_byte(0x66,0x0f,0x38,0x00,0xc0|($1<<3)|$2); }
  91. else
  92. { &::generic("pshufb",@_); }
  93. }
  94. sub ::palignr
  95. { my($dst,$src,$imm)=@_;
  96. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  97. { &::data_byte(0x66,0x0f,0x3a,0x0f,0xc0|($1<<3)|$2,$imm); }
  98. else
  99. { &::generic("palignr",@_); }
  100. }
  101. sub ::pclmulqdq
  102. { my($dst,$src,$imm)=@_;
  103. if ("$dst:$src" =~ /xmm([0-7]):xmm([0-7])/)
  104. { &::data_byte(0x66,0x0f,0x3a,0x44,0xc0|($1<<3)|$2,$imm); }
  105. else
  106. { &::generic("pclmulqdq",@_); }
  107. }
  108. sub ::rdrand
  109. { my ($dst)=@_;
  110. if ($dst =~ /(e[a-dsd][ixp])/)
  111. { &::data_byte(0x0f,0xc7,0xf0|$regrm{$dst}); }
  112. else
  113. { &::generic("rdrand",@_); }
  114. }
  115. # label management
  116. $lbdecor="L"; # local label decoration, set by package
  117. $label="000";
  118. sub ::islabel # see is argument is a known label
  119. { my $i;
  120. foreach $i (values %label) { return $i if ($i eq $_[0]); }
  121. $label{$_[0]}; # can be undef
  122. }
  123. sub ::label # instantiate a function-scope label
  124. { if (!defined($label{$_[0]}))
  125. { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
  126. $label{$_[0]};
  127. }
  128. sub ::LABEL # instantiate a file-scope label
  129. { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  130. $label{$_[0]};
  131. }
  132. sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
  133. sub ::set_label_B { push(@out,"@_:\n"); }
  134. sub ::set_label
  135. { my $label=&::label($_[0]);
  136. &::align($_[1]) if ($_[1]>1);
  137. &::set_label_B($label);
  138. $label;
  139. }
  140. sub ::wipe_labels # wipes function-scope labels
  141. { foreach $i (keys %label)
  142. { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
  143. }
  144. # subroutine management
  145. sub ::function_begin
  146. { &function_begin_B(@_);
  147. $stack=4;
  148. &push("ebp");
  149. &push("ebx");
  150. &push("esi");
  151. &push("edi");
  152. }
  153. sub ::function_end
  154. { &pop("edi");
  155. &pop("esi");
  156. &pop("ebx");
  157. &pop("ebp");
  158. &ret();
  159. &function_end_B(@_);
  160. $stack=0;
  161. &wipe_labels();
  162. }
  163. sub ::function_end_A
  164. { &pop("edi");
  165. &pop("esi");
  166. &pop("ebx");
  167. &pop("ebp");
  168. &ret();
  169. $stack+=16; # readjust esp as if we didn't pop anything
  170. }
  171. sub ::asciz
  172. { my @str=unpack("C*",shift);
  173. push @str,0;
  174. while ($#str>15) {
  175. &data_byte(@str[0..15]);
  176. foreach (0..15) { shift @str; }
  177. }
  178. &data_byte(@str) if (@str);
  179. }
  180. sub ::asm_finish
  181. { &file_end();
  182. print @out;
  183. }
  184. sub ::asm_init
  185. { my ($type,$fn,$cpu)=@_;
  186. $filename=$fn;
  187. $i386=$cpu;
  188. $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=0;
  189. if (($type eq "elf"))
  190. { $elf=1; require "x86gas.pl"; }
  191. elsif (($type eq "a\.out"))
  192. { $aout=1; require "x86gas.pl"; }
  193. elsif (($type eq "coff" or $type eq "gaswin"))
  194. { $coff=1; require "x86gas.pl"; }
  195. elsif (($type eq "win32n"))
  196. { $win32=1; require "x86nasm.pl"; }
  197. elsif (($type eq "nw-nasm"))
  198. { $netware=1; require "x86nasm.pl"; }
  199. #elsif (($type eq "nw-mwasm"))
  200. #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
  201. elsif (($type eq "win32"))
  202. { $win32=1; require "x86masm.pl"; }
  203. elsif (($type eq "macosx"))
  204. { $aout=1; $macosx=1; require "x86gas.pl"; }
  205. else
  206. { print STDERR <<"EOF";
  207. Pick one target type from
  208. elf - Linux, FreeBSD, Solaris x86, etc.
  209. a.out - DJGPP, elder OpenBSD, etc.
  210. coff - GAS/COFF such as Win32 targets
  211. win32n - Windows 95/Windows NT NASM format
  212. nw-nasm - NetWare NASM format
  213. macosx - Mac OS X
  214. EOF
  215. exit(1);
  216. }
  217. $pic=0;
  218. for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
  219. $filename =~ s/\.pl$//;
  220. &file($filename);
  221. }
  222. 1;