x86nasm.pl 3.5 KB

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