x86nasm.pl 3.8 KB

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