x86asm.pl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 2 arguments passed to $opcode" if ($#_>1);
  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. sub ::pshufw { &::emit("pshufw",@_); }
  71. sub ::shld { &::emit("shld",@_); }
  72. sub ::shrd { &::emit("shrd",@_); }
  73. # label management
  74. $lbdecor="L"; # local label decoration, set by package
  75. $label="000";
  76. sub ::islabel # see is argument is a known label
  77. { my $i;
  78. foreach $i (values %label) { return $i if ($i eq $_[0]); }
  79. $label{$_[0]}; # can be undef
  80. }
  81. sub ::label # instantiate a function-scope label
  82. { if (!defined($label{$_[0]}))
  83. { $label{$_[0]}="${lbdecor}${label}${_[0]}"; $label++; }
  84. $label{$_[0]};
  85. }
  86. sub ::LABEL # instantiate a file-scope label
  87. { $label{$_[0]}=$_[1] if (!defined($label{$_[0]}));
  88. $label{$_[0]};
  89. }
  90. sub ::static_label { &::LABEL($_[0],$lbdecor.$_[0]); }
  91. sub ::set_label_B { push(@out,"@_:\n"); }
  92. sub ::set_label
  93. { my $label=&::label($_[0]);
  94. &::align($_[1]) if ($_[1]>1);
  95. &::set_label_B($label);
  96. $label;
  97. }
  98. sub ::wipe_labels # wipes function-scope labels
  99. { foreach $i (keys %label)
  100. { delete $label{$i} if ($label{$i} =~ /^\Q${lbdecor}\E[0-9]{3}/); }
  101. }
  102. # subroutine management
  103. sub ::function_begin
  104. { &function_begin_B(@_);
  105. $stack=4;
  106. &push("ebp");
  107. &push("ebx");
  108. &push("esi");
  109. &push("edi");
  110. }
  111. sub ::function_end
  112. { &pop("edi");
  113. &pop("esi");
  114. &pop("ebx");
  115. &pop("ebp");
  116. &ret();
  117. &function_end_B(@_);
  118. $stack=0;
  119. &wipe_labels();
  120. }
  121. sub ::function_end_A
  122. { &pop("edi");
  123. &pop("esi");
  124. &pop("ebx");
  125. &pop("ebp");
  126. &ret();
  127. $stack+=16; # readjust esp as if we didn't pop anything
  128. }
  129. sub ::asciz
  130. { my @str=unpack("C*",shift);
  131. push @str,0;
  132. while ($#str>15) {
  133. &data_byte(@str[0..15]);
  134. foreach (0..15) { shift @str; }
  135. }
  136. &data_byte(@str) if (@str);
  137. }
  138. sub ::asm_finish
  139. { &file_end();
  140. print @out;
  141. }
  142. sub ::asm_init
  143. { my ($type,$fn,$cpu)=@_;
  144. $filename=$fn;
  145. $i386=$cpu;
  146. $elf=$cpp=$coff=$aout=$macosx=$win32=$netware=$mwerks=0;
  147. if (($type eq "elf"))
  148. { $elf=1; require "x86gas.pl"; }
  149. elsif (($type eq "a\.out"))
  150. { $aout=1; require "x86gas.pl"; }
  151. elsif (($type eq "coff" or $type eq "gaswin"))
  152. { $coff=1; require "x86gas.pl"; }
  153. elsif (($type eq "win32n"))
  154. { $win32=1; require "x86nasm.pl"; }
  155. elsif (($type eq "nw-nasm"))
  156. { $netware=1; require "x86nasm.pl"; }
  157. #elsif (($type eq "nw-mwasm"))
  158. #{ $netware=1; $mwerks=1; require "x86nasm.pl"; }
  159. elsif (($type eq "win32"))
  160. { $win32=1; require "x86masm.pl"; }
  161. elsif (($type eq "macosx"))
  162. { $aout=1; $macosx=1; require "x86gas.pl"; }
  163. else
  164. { print STDERR <<"EOF";
  165. Pick one target type from
  166. elf - Linux, FreeBSD, Solaris x86, etc.
  167. a.out - DJGPP, elder OpenBSD, etc.
  168. coff - GAS/COFF such as Win32 targets
  169. win32n - Windows 95/Windows NT NASM format
  170. nw-nasm - NetWare NASM format
  171. macosx - Mac OS X
  172. EOF
  173. exit(1);
  174. }
  175. $pic=0;
  176. for (@ARGV) { $pic=1 if (/\-[fK]PIC/i); }
  177. $filename =~ s/\.pl$//;
  178. &file($filename);
  179. }
  180. 1;