x86asm.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. &::generic("#ifdef __CET__\n");
  154. &::data_byte(0xf3,0x0f,0x1e,0xfb);
  155. &::generic("#endif\n");
  156. }
  157. # label management
  158. $lbdecor="L"; # local label decoration, set by package
  159. $label="000";
  160. sub ::islabel # see is argument is a known label
  161. { my $i;
  162. foreach $i (values %label) { return $i if ($i eq $_[0]); }
  163. $label{$_[0]}; # can be undef
  164. }
  165. sub ::label # instantiate a function-scope label
  166. { if (!defined($label{$_[0]}))
  167. { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
  168. $label{$_[0]};
  169. }
  170. sub ::LABEL # instantiate a file-scope label
  171. { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  172. $label{$_[0]};
  173. }
  174. sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
  175. sub ::set_label_B { push(@out,"@_:\n"); }
  176. sub ::set_label
  177. { my $label=&::label($_[0]);
  178. &::align($_[1]) if ($_[1]>1);
  179. &::set_label_B($label);
  180. $label;
  181. }
  182. sub ::wipe_labels # wipes function-scope labels
  183. { foreach $i (keys %label)
  184. { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
  185. }
  186. # subroutine management
  187. sub ::function_begin
  188. { &function_begin_B(@_);
  189. $stack=4;
  190. &push("ebp");
  191. &push("ebx");
  192. &push("esi");
  193. &push("edi");
  194. }
  195. sub ::function_end
  196. { &pop("edi");
  197. &pop("esi");
  198. &pop("ebx");
  199. &pop("ebp");
  200. &ret();
  201. &function_end_B(@_);
  202. $stack=0;
  203. &wipe_labels();
  204. }
  205. sub ::function_end_A
  206. { &pop("edi");
  207. &pop("esi");
  208. &pop("ebx");
  209. &pop("ebp");
  210. &ret();
  211. $stack+=16; # readjust esp as if we didn't pop anything
  212. }
  213. sub ::asciz
  214. { my @str=unpack("C*",shift);
  215. push @str,0;
  216. while ($#str>15) {
  217. &data_byte(@str[0..15]);
  218. foreach (0..15) { shift @str; }
  219. }
  220. &data_byte(@str) if (@str);
  221. }
  222. sub ::asm_finish
  223. { &file_end();
  224. print @out;
  225. }
  226. sub ::asm_init
  227. { my ($type,$cpu)=@_;
  228. $i386=$cpu;
  229. $elf=$cpp=$coff=$aout=$macosx=$win32=$mwerks=$android=0;
  230. if (($type eq "elf"))
  231. { $elf=1; require "x86gas.pl"; }
  232. elsif (($type eq "elf-1"))
  233. { $elf=-1; require "x86gas.pl"; }
  234. elsif (($type eq "a\.out"))
  235. { $aout=1; require "x86gas.pl"; }
  236. elsif (($type eq "coff" or $type eq "gaswin"))
  237. { $coff=1; require "x86gas.pl"; }
  238. elsif (($type eq "win32n"))
  239. { $win32=1; require "x86nasm.pl"; }
  240. elsif (($type eq "win32"))
  241. { $win32=1; require "x86masm.pl"; }
  242. elsif (($type eq "macosx"))
  243. { $aout=1; $macosx=1; require "x86gas.pl"; }
  244. elsif (($type eq "android"))
  245. { $elf=1; $android=1; require "x86gas.pl"; }
  246. else
  247. { print STDERR <<"EOF";
  248. Pick one target type from
  249. elf - Linux, FreeBSD, Solaris x86, etc.
  250. a.out - DJGPP, elder OpenBSD, etc.
  251. coff - GAS/COFF such as Win32 targets
  252. win32n - Windows 95/Windows NT NASM format
  253. macosx - Mac OS X
  254. EOF
  255. exit(1);
  256. }
  257. $pic=0;
  258. for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
  259. &file();
  260. }
  261. sub ::hidden {}
  262. 1;