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.
 
 
 
 
 
 

293 lines
6.8 KiB

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