x86asm.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. sub ::rdseed
  116. { my ($dst)=@_;
  117. if ($dst =~ /(e[a-dsd][ixp])/)
  118. { &::data_byte(0x0f,0xc7,0xf8|$regrm{$dst}); }
  119. else
  120. { &::generic("rdrand",@_); }
  121. }
  122. sub rxb {
  123. local *opcode=shift;
  124. my ($dst,$src1,$src2,$rxb)=@_;
  125. $rxb|=0x7<<5;
  126. $rxb&=~(0x04<<5) if($dst>=8);
  127. $rxb&=~(0x01<<5) if($src1>=8);
  128. $rxb&=~(0x02<<5) if($src2>=8);
  129. push @opcode,$rxb;
  130. }
  131. sub ::vprotd
  132. { my $args=join(',',@_);
  133. if ($args =~ /xmm([0-7]),xmm([0-7]),([x0-9a-f]+)/)
  134. { my @opcode=(0x8f);
  135. rxb(\@opcode,$1,$2,-1,0x08);
  136. push @opcode,0x78,0xc2;
  137. push @opcode,0xc0|($2&7)|(($1&7)<<3); # ModR/M
  138. my $c=$3;
  139. push @opcode,$c=~/^0/?oct($c):$c;
  140. &::data_byte(@opcode);
  141. }
  142. else
  143. { &::generic("vprotd",@_); }
  144. }
  145. # label management
  146. $lbdecor="L"; # local label decoration, set by package
  147. $label="000";
  148. sub ::islabel # see is argument is a known label
  149. { my $i;
  150. foreach $i (values %label) { return $i if ($i eq $_[0]); }
  151. $label{$_[0]}; # can be undef
  152. }
  153. sub ::label # instantiate a function-scope label
  154. { if (!defined($label{$_[0]}))
  155. { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
  156. $label{$_[0]};
  157. }
  158. sub ::LABEL # instantiate a file-scope label
  159. { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  160. $label{$_[0]};
  161. }
  162. sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
  163. sub ::set_label_B { push(@out,"@_:\n"); }
  164. sub ::set_label
  165. { my $label=&::label($_[0]);
  166. &::align($_[1]) if ($_[1]>1);
  167. &::set_label_B($label);
  168. $label;
  169. }
  170. sub ::wipe_labels # wipes function-scope labels
  171. { foreach $i (keys %label)
  172. { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
  173. }
  174. # subroutine management
  175. sub ::function_begin
  176. { &function_begin_B(@_);
  177. $stack=4;
  178. &push("ebp");
  179. &push("ebx");
  180. &push("esi");
  181. &push("edi");
  182. }
  183. sub ::function_end
  184. { &pop("edi");
  185. &pop("esi");
  186. &pop("ebx");
  187. &pop("ebp");
  188. &ret();
  189. &function_end_B(@_);
  190. $stack=0;
  191. &wipe_labels();
  192. }
  193. sub ::function_end_A
  194. { &pop("edi");
  195. &pop("esi");
  196. &pop("ebx");
  197. &pop("ebp");
  198. &ret();
  199. $stack+=16; # readjust esp as if we didn't pop anything
  200. }
  201. sub ::asciz
  202. { my @str=unpack("C*",shift);
  203. push @str,0;
  204. while ($#str>15) {
  205. &data_byte(@str[0..15]);
  206. foreach (0..15) { shift @str; }
  207. }
  208. &data_byte(@str) if (@str);
  209. }
  210. sub ::asm_finish
  211. { &file_end();
  212. print @out;
  213. }
  214. sub ::asm_init
  215. { my ($type,$fn,$cpu)=@_;
  216. $filename=$fn;
  217. $i386=$cpu;
  218. $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=$android=0;
  219. if (($type eq "elf"))
  220. { $elf=1; require "x86gas.pl"; }
  221. elsif (($type eq "elf-1"))
  222. { $elf=-1; require "x86gas.pl"; }
  223. elsif (($type eq "a\.out"))
  224. { $aout=1; require "x86gas.pl"; }
  225. elsif (($type eq "coff" or $type eq "gaswin"))
  226. { $coff=1; require "x86gas.pl"; }
  227. elsif (($type eq "win32n"))
  228. { $win32=1; require "x86nasm.pl"; }
  229. elsif (($type eq "nw-nasm"))
  230. { $netware=1; require "x86nasm.pl"; }
  231. #elsif (($type eq "nw-mwasm"))
  232. #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
  233. elsif (($type eq "win32"))
  234. { $win32=1; require "x86masm.pl"; }
  235. elsif (($type eq "macosx"))
  236. { $aout=1; $macosx=1; require "x86gas.pl"; }
  237. elsif (($type eq "android"))
  238. { $elf=1; $android=1; require "x86gas.pl"; }
  239. else
  240. { print STDERR <<"EOF";
  241. Pick one target type from
  242. elf - Linux, FreeBSD, Solaris x86, etc.
  243. a.out - DJGPP, elder OpenBSD, etc.
  244. coff - GAS/COFF such as Win32 targets
  245. win32n - Windows 95/Windows NT NASM format
  246. nw-nasm - NetWare NASM format
  247. macosx - Mac OS X
  248. EOF
  249. exit(1);
  250. }
  251. $pic=0;
  252. for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
  253. $filename =~ s/\.pl$//;
  254. &file($filename);
  255. }
  256. sub ::hidden {}
  257. 1;