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.
 
 
 
 
 
 

188 lines
4.4 KiB

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