Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

195 строки
4.7 KiB

  1. #! /usr/bin/env perl
  2. # Copyright 1999-2016 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. package x86nasm;
  9. *out=\@::out;
  10. $::lbdecor="L\$"; # local label decoration
  11. $nmdecor=$::netware?"":"_"; # external name decoration
  12. $drdecor=$::mwerks?".":""; # directive decoration
  13. $initseg="";
  14. sub ::generic
  15. { my $opcode=shift;
  16. my $tmp;
  17. if (!$::mwerks)
  18. { if ($opcode =~ m/^j/o && $#_==0) # optimize jumps
  19. { $_[0] = "NEAR $_[0]"; }
  20. elsif ($opcode eq "lea" && $#_==1) # wipe storage qualifier from lea
  21. { $_[1] =~ s/^[^\[]*\[/\[/o; }
  22. elsif ($opcode eq "clflush" && $#_==0)
  23. { $_[0] =~ s/^[^\[]*\[/\[/o; }
  24. }
  25. &::emit($opcode,@_);
  26. 1;
  27. }
  28. #
  29. # opcodes not covered by ::generic above, mostly inconsistent namings...
  30. #
  31. sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
  32. sub ::call_ptr { &::emit("call",@_); }
  33. sub ::jmp_ptr { &::emit("jmp",@_); }
  34. sub get_mem
  35. { my($size,$addr,$reg1,$reg2,$idx)=@_;
  36. my($post,$ret);
  37. if (!defined($idx) && 1*$reg2) { $idx=$reg2; $reg2=$reg1; undef $reg1; }
  38. if ($size ne "")
  39. { $ret .= "$size";
  40. $ret .= " PTR" if ($::mwerks);
  41. $ret .= " ";
  42. }
  43. $ret .= "[";
  44. $addr =~ s/^\s+//;
  45. # prepend global references with optional underscore
  46. $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
  47. # put address arithmetic expression in parenthesis
  48. $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
  49. if (($addr ne "") && ($addr ne 0))
  50. { if ($addr !~ /^-/) { $ret .= "$addr+"; }
  51. else { $post=$addr; }
  52. }
  53. if ($reg2 ne "")
  54. { $idx!=0 or $idx=1;
  55. $ret .= "$reg2*$idx";
  56. $ret .= "+$reg1" if ($reg1 ne "");
  57. }
  58. else
  59. { $ret .= "$reg1"; }
  60. $ret .= "$post]";
  61. $ret =~ s/\+\]/]/; # in case $addr was the only argument
  62. $ret;
  63. }
  64. sub ::BP { &get_mem("BYTE",@_); }
  65. sub ::DWP { &get_mem("DWORD",@_); }
  66. sub ::WP { &get_mem("WORD",@_); }
  67. sub ::QWP { &get_mem("",@_); }
  68. sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; }
  69. sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; }
  70. sub ::file
  71. { if ($::mwerks) { push(@out,".section\t.text,64\n"); }
  72. else
  73. { my $tmp=<<___;
  74. %ifidn __OUTPUT_FORMAT__,obj
  75. section code use32 class=code align=64
  76. %elifidn __OUTPUT_FORMAT__,win32
  77. %ifdef __YASM_VERSION_ID__
  78. %if __YASM_VERSION_ID__ < 01010000h
  79. %error yasm version 1.1.0 or later needed.
  80. %endif
  81. ; Yasm automatically includes @feat.00 and complains about redefining it.
  82. ; https://www.tortall.net/projects/yasm/manual/html/objfmt-win32-safeseh.html
  83. %else
  84. \$\@feat.00 equ 1
  85. %endif
  86. section .text code align=64
  87. %else
  88. section .text code
  89. %endif
  90. ___
  91. push(@out,$tmp);
  92. }
  93. }
  94. sub ::function_begin_B
  95. { my $func=shift;
  96. my $global=($func !~ /^_/);
  97. my $begin="${::lbdecor}_${func}_begin";
  98. $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
  99. &::LABEL($func,$global?"$begin":"$nmdecor$func");
  100. $func=$nmdecor.$func;
  101. push(@out,"${drdecor}global $func\n") if ($global);
  102. push(@out,"${drdecor}align 16\n");
  103. push(@out,"$func:\n");
  104. push(@out,"$begin:\n") if ($global);
  105. $::stack=4;
  106. }
  107. sub ::function_end_B
  108. { $::stack=0;
  109. &::wipe_labels();
  110. }
  111. sub ::file_end
  112. { if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
  113. { my $comm=<<___;
  114. ${drdecor}segment .bss
  115. ${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 16
  116. ___
  117. # comment out OPENSSL_ia32cap_P declarations
  118. grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
  119. push (@out,$comm)
  120. }
  121. push (@out,$initseg) if ($initseg);
  122. }
  123. sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
  124. sub ::external_label
  125. { foreach(@_)
  126. { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
  127. }
  128. sub ::public_label
  129. { push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  130. sub ::data_byte
  131. { push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
  132. sub ::data_short
  133. { push(@out,(($::mwerks)?".word\t":"dw\t").join(',',@_)."\n"); }
  134. sub ::data_word
  135. { push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
  136. sub ::align
  137. { push(@out,"${drdecor}align\t$_[0]\n"); }
  138. sub ::picmeup
  139. { my($dst,$sym)=@_;
  140. &::lea($dst,&::DWP($sym));
  141. }
  142. sub ::initseg
  143. { my $f=$nmdecor.shift;
  144. if ($::win32)
  145. { $initseg=<<___;
  146. segment .CRT\$XCU data align=4
  147. extern $f
  148. dd $f
  149. ___
  150. }
  151. }
  152. sub ::dataseg
  153. { if ($mwerks) { push(@out,".section\t.data,4\n"); }
  154. else { push(@out,"section\t.data align=4\n"); }
  155. }
  156. sub ::safeseh
  157. { my $nm=shift;
  158. push(@out,"%if __NASM_VERSION_ID__ >= 0x02030000\n");
  159. push(@out,"safeseh ".&::LABEL($nm,$nmdecor.$nm)."\n");
  160. push(@out,"%endif\n");
  161. }
  162. 1;