x86nasm.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. }
  17. &::emit($opcode,@_);
  18. 1;
  19. }
  20. #
  21. # opcodes not covered by ::generic above, mostly inconsistent namings...
  22. #
  23. sub ::call { &::emit("call",(&::islabel($_[0]) or "$nmdecor$_[0]")); }
  24. sub ::call_ptr { &::emit("call",@_); }
  25. sub ::jmp_ptr { &::emit("jmp",@_); }
  26. sub get_mem
  27. { my($size,$addr,$reg1,$reg2,$idx)=@_;
  28. my($post,$ret);
  29. if ($size ne "")
  30. { $ret .= "$size";
  31. $ret .= " PTR" if ($::mwerks);
  32. $ret .= " ";
  33. }
  34. $ret .= "[";
  35. $addr =~ s/^\s+//;
  36. # prepend global references with optional underscore
  37. $addr =~ s/^([^\+\-0-9][^\+\-]*)/::islabel($1) or "$nmdecor$1"/ige;
  38. # put address arithmetic expression in parenthesis
  39. $addr="($addr)" if ($addr =~ /^.+[\-\+].+$/);
  40. if (($addr ne "") && ($addr ne 0))
  41. { if ($addr !~ /^-/) { $ret .= "$addr+"; }
  42. else { $post=$addr; }
  43. }
  44. if ($reg2 ne "")
  45. { $idx!=0 or $idx=1;
  46. $ret .= "$reg2*$idx";
  47. $ret .= "+$reg1" if ($reg1 ne "");
  48. }
  49. else
  50. { $ret .= "$reg1"; }
  51. $ret .= "$post]";
  52. $ret =~ s/\+\]/]/; # in case $addr was the only argument
  53. $ret;
  54. }
  55. sub ::BP { &get_mem("BYTE",@_); }
  56. sub ::DWP { &get_mem("DWORD",@_); }
  57. sub ::QWP { &get_mem("",@_); }
  58. sub ::BC { (($::mwerks)?"":"BYTE ")."@_"; }
  59. sub ::DWC { (($::mwerks)?"":"DWORD ")."@_"; }
  60. sub ::file
  61. { if ($::mwerks) { push(@out,".section\t.text,64\n"); }
  62. else
  63. { my $tmp=<<___;
  64. %ifidn __OUTPUT_FORMAT__,obj
  65. section code use32 class=code align=64
  66. %elifidn __OUTPUT_FORMAT__,win32
  67. \$\@feat.00 equ 1
  68. section .text code align=64
  69. %else
  70. section .text code
  71. %endif
  72. ___
  73. push(@out,$tmp);
  74. }
  75. }
  76. sub ::function_begin_B
  77. { my $func=shift;
  78. my $global=($func !~ /^_/);
  79. my $begin="${::lbdecor}_${func}_begin";
  80. $begin =~ s/^\@/./ if ($::mwerks); # the torture never stops
  81. &::LABEL($func,$global?"$begin":"$nmdecor$func");
  82. $func=$nmdecor.$func;
  83. push(@out,"${drdecor}global $func\n") if ($global);
  84. push(@out,"${drdecor}align 16\n");
  85. push(@out,"$func:\n");
  86. push(@out,"$begin:\n") if ($global);
  87. $::stack=4;
  88. }
  89. sub ::function_end_B
  90. { $::stack=0;
  91. &::wipe_labels();
  92. }
  93. sub ::file_end
  94. { if (grep {/\b${nmdecor}OPENSSL_ia32cap_P\b/i} @out)
  95. { my $comm=<<___;
  96. ${drdecor}segment .bss
  97. ${drdecor}common ${nmdecor}OPENSSL_ia32cap_P 4
  98. ___
  99. # comment out OPENSSL_ia32cap_P declarations
  100. grep {s/(^extern\s+${nmdecor}OPENSSL_ia32cap_P)/\;$1/} @out;
  101. push (@out,$comm)
  102. }
  103. push (@out,$initseg) if ($initseg);
  104. }
  105. sub ::comment { foreach (@_) { push(@out,"\t; $_\n"); } }
  106. sub ::external_label
  107. { foreach(@_)
  108. { push(@out,"${drdecor}extern\t".&::LABEL($_,$nmdecor.$_)."\n"); }
  109. }
  110. sub ::public_label
  111. { push(@out,"${drdecor}global\t".&::LABEL($_[0],$nmdecor.$_[0])."\n"); }
  112. sub ::data_byte
  113. { push(@out,(($::mwerks)?".byte\t":"db\t").join(',',@_)."\n"); }
  114. sub ::data_word
  115. { push(@out,(($::mwerks)?".long\t":"dd\t").join(',',@_)."\n"); }
  116. sub ::align
  117. { push(@out,"${drdecor}align\t$_[0]\n"); }
  118. sub ::picmeup
  119. { my($dst,$sym)=@_;
  120. &::lea($dst,&::DWP($sym));
  121. }
  122. sub ::initseg
  123. { my $f=$nmdecor.shift;
  124. if ($::win32)
  125. { $initseg=<<___;
  126. segment .CRT\$XCU data align=4
  127. extern $f
  128. dd $f
  129. ___
  130. }
  131. }
  132. sub ::dataseg
  133. { if ($mwerks) { push(@out,".section\t.data,4\n"); }
  134. else { push(@out,"section\t.data align=4\n"); }
  135. }
  136. 1;