x86asm.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #!/usr/bin/env perl
  2. # require 'x86asm.pl';
  3. # &asm_init(<flavor>,"des-586.pl"[,$i386only]);
  4. # &function_begin("foo");
  5. # ...
  6. # &function_end("foo");
  7. # &asm_finish
  8. $out=();
  9. $i386=0;
  10. # AUTOLOAD is this context has quite unpleasant side effect, namely
  11. # that typos in function calls effectively go to assembler output,
  12. # but on the pros side we don't have to implement one subroutine per
  13. # each opcode...
  14. sub ::AUTOLOAD
  15. { my $opcode = $AUTOLOAD;
  16. die "more than 4 arguments passed to $opcode" if ($#_>3);
  17. $opcode =~ s/.*:://;
  18. if ($opcode =~ /^push/) { $stack+=4; }
  19. elsif ($opcode =~ /^pop/) { $stack-=4; }
  20. &generic($opcode,@_) or die "undefined subroutine \&$AUTOLOAD";
  21. }
  22. sub ::emit
  23. { my $opcode=shift;
  24. if ($#_==-1) { push(@out,"\t$opcode\n"); }
  25. else { push(@out,"\t$opcode\t".join(',',@_)."\n"); }
  26. }
  27. sub ::LB
  28. { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'low byte'";
  29. $1."l";
  30. }
  31. sub ::HB
  32. { $_[0] =~ m/^e?([a-d])x$/o or die "$_[0] does not have a 'high byte'";
  33. $1."h";
  34. }
  35. sub ::stack_push{ my $num=$_[0]*4; $stack+=$num; &sub("esp",$num); }
  36. sub ::stack_pop { my $num=$_[0]*4; $stack-=$num; &add("esp",$num); }
  37. sub ::blindpop { &pop($_[0]); $stack+=4; }
  38. sub ::wparam { &DWP($stack+4*$_[0],"esp"); }
  39. sub ::swtmp { &DWP(4*$_[0],"esp"); }
  40. sub ::bswap
  41. { if ($i386) # emulate bswap for i386
  42. { &comment("bswap @_");
  43. &xchg(&HB(@_),&LB(@_));
  44. &ror (@_,16);
  45. &xchg(&HB(@_),&LB(@_));
  46. }
  47. else
  48. { &generic("bswap",@_); }
  49. }
  50. # These are made-up opcodes introduced over the years essentially
  51. # by ignorance, just alias them to real ones...
  52. sub ::movb { &mov(@_); }
  53. sub ::xorb { &xor(@_); }
  54. sub ::rotl { &rol(@_); }
  55. sub ::rotr { &ror(@_); }
  56. sub ::exch { &xchg(@_); }
  57. sub ::halt { &hlt; }
  58. sub ::movz { &movzx(@_); }
  59. sub ::pushf { &pushfd; }
  60. sub ::popf { &popfd; }
  61. # 3 argument instructions
  62. sub ::movq
  63. { my($p1,$p2,$optimize)=@_;
  64. if ($optimize && $p1=~/^mm[0-7]$/ && $p2=~/^mm[0-7]$/)
  65. # movq between mmx registers can sink Intel CPUs
  66. { &::pshufw($p1,$p2,0xe4); }
  67. else
  68. { &::generic("movq",@_); }
  69. }
  70. # label management
  71. $lbdecor="L"; # local label decoration, set by package
  72. $label="000";
  73. sub ::islabel # see is argument is a known label
  74. { my $i;
  75. foreach $i (values %label) { return $i if ($i eq $_[0]); }
  76. $label{$_[0]}; # can be undef
  77. }
  78. sub ::label # instantiate a function-scope label
  79. { if (!defined($label{$_[0]}))
  80. { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
  81. $label{$_[0]};
  82. }
  83. sub ::LABEL # instantiate a file-scope label
  84. { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  85. $label{$_[0]};
  86. }
  87. sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
  88. sub ::set_label_B { push(@out,"@_:\n"); }
  89. sub ::set_label
  90. { my $label=&::label($_[0]);
  91. &::align($_[1]) if ($_[1]>1);
  92. &::set_label_B($label);
  93. $label;
  94. }
  95. sub ::wipe_labels # wipes function-scope labels
  96. { foreach $i (keys %label)
  97. { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
  98. }
  99. # subroutine management
  100. sub ::function_begin
  101. { &function_begin_B(@_);
  102. $stack=4;
  103. &push("ebp");
  104. &push("ebx");
  105. &push("esi");
  106. &push("edi");
  107. }
  108. sub ::function_end
  109. { &pop("edi");
  110. &pop("esi");
  111. &pop("ebx");
  112. &pop("ebp");
  113. &ret();
  114. &function_end_B(@_);
  115. $stack=0;
  116. &wipe_labels();
  117. }
  118. sub ::function_end_A
  119. { &pop("edi");
  120. &pop("esi");
  121. &pop("ebx");
  122. &pop("ebp");
  123. &ret();
  124. $stack+=16; # readjust esp as if we didn't pop anything
  125. }
  126. sub ::asciz
  127. { my @str=unpack("C*",shift);
  128. push @str,0;
  129. while ($#str>15) {
  130. &data_byte(@str[0..15]);
  131. foreach (0..15) { shift @str; }
  132. }
  133. &data_byte(@str) if (@str);
  134. }
  135. sub ::asm_finish
  136. { &file_end();
  137. print @out;
  138. }
  139. sub ::asm_init
  140. { my ($type,$fn,$cpu)=@_;
  141. $filename=$fn;
  142. $i386=$cpu;
  143. $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=0;
  144. if (($type eq "elf"))
  145. { $elf=1; require "x86gas.pl"; }
  146. elsif (($type eq "a\.out"))
  147. { $aout=1; require "x86gas.pl"; }
  148. elsif (($type eq "coff" or $type eq "gaswin"))
  149. { $coff=1; require "x86gas.pl"; }
  150. elsif (($type eq "win32n"))
  151. { $win32=1; require "x86nasm.pl"; }
  152. elsif (($type eq "nw-nasm"))
  153. { $netware=1; require "x86nasm.pl"; }
  154. #elsif (($type eq "nw-mwasm"))
  155. #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
  156. elsif (($type eq "win32"))
  157. { $win32=1; require "x86masm.pl"; }
  158. elsif (($type eq "macosx"))
  159. { $aout=1; $macosx=1; require "x86gas.pl"; }
  160. else
  161. { print STDERR <<"EOF";
  162. Pick one target type from
  163. elf - Linux, FreeBSD, Solaris x86, etc.
  164. a.out - DJGPP, elder OpenBSD, etc.
  165. coff - GAS/COFF such as Win32 targets
  166. win32n - Windows 95/Windows NT NASM format
  167. nw-nasm - NetWare NASM format
  168. macosx - Mac OS X
  169. EOF
  170. exit(1);
  171. }
  172. $pic=0;
  173. for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
  174. $filename =~ s/\.pl$//;
  175. &file($filename);
  176. }
  177. 1;