x86asm.pl 7.0 KB

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