2
0

x86nasm.pl 4.1 KB

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