x86nasm.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #! /usr/bin/env perl
  2. # Copyright 1999-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. package x86nasm;
  9. *out=\@::out;
  10. $::lbdecor="L\$"; # local label decoration
  11. $nmdecor="_"; # 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. \$\@feat.00 equ 1
  78. section .text code align=64
  79. %else
  80. section .text code
  81. %endif
  82. ___
  83. push(@out,$tmp);
  84. }
  85. }
  86. sub ::function_begin_B
  87. { my $func=shift;
  88. my $global=($func !~ /^_/);
  89. my $begin="${::lbdecor}_${func}_begin";
  90. $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
  91. &::LABEL($func,$global?"$begin":"$nmdecor$func");
  92. $func=$nmdecor.$func;
  93. push(@out,"${drdecor}global $func\n") if ($global);
  94. push(@out,"${drdecor}align 16\n");
  95. push(@out,"$func:\n");
  96. push(@out,"$begin:\n") if ($global);
  97. $::stack=4;
  98. }
  99. sub ::function_end_B
  100. { $::stack=0;
  101. &::wipe_labels();
  102. }
  103. sub ::file_end
  104. { if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
  105. { my $comm=<<___;
  106. ${drdecor}segment .bss
  107. ${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 16
  108. ___
  109. # comment out OPENSSL_ia32cap_P declarations
  110. grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
  111. push (@out,$comm)
  112. }
  113. push (@out,$initseg) if ($initseg);
  114. }
  115. sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
  116. sub ::external_label
  117. { foreach(@_)
  118. { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
  119. }
  120. sub ::public_label
  121. { push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  122. sub ::data_byte
  123. { push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
  124. sub ::data_short
  125. { push(@out,(($::mwerks)?".word\t":"dw\t").join(',',@_)."\n"); }
  126. sub ::data_word
  127. { push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
  128. sub ::align
  129. { push(@out,"${drdecor}align\t$_[0]\n"); }
  130. sub ::picmeup
  131. { my($dst,$sym)=@_;
  132. &::lea($dst,&::DWP($sym));
  133. }
  134. sub ::initseg
  135. { my $f=$nmdecor.shift;
  136. if ($::win32)
  137. { $initseg=<<___;
  138. segment .CRT\$XCU data align=4
  139. extern $f
  140. dd $f
  141. ___
  142. }
  143. }
  144. sub ::dataseg
  145. { if ($mwerks) { push(@out,".section\t.data,4\n"); }
  146. else { push(@out,"section\t.data align=4\n"); }
  147. }
  148. sub ::safeseh
  149. { my $nm=shift;
  150. push(@out,"%if __NASM_VERSION_ID__ >= 0x02030000\n");
  151. push(@out,"safeseh ".&::LABEL($nm,$nmdecor.$nm)."\n");
  152. push(@out,"%endif\n");
  153. }
  154. 1;