x86unix.pl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #!/usr/local/bin/perl
  2. package x86unix;
  3. $label="L000";
  4. $align=($main'aout)?"4":"16";
  5. $under=($main'aout)?"_":"";
  6. $com_start=($main'sol)?"/":"#";
  7. sub main'asm_init_output { @out=(); }
  8. sub main'asm_get_output { return(@out); }
  9. sub main'get_labels { return(@labels); }
  10. sub main'external_label { push(@labels,@_); }
  11. if ($main'cpp)
  12. {
  13. $align="ALIGN";
  14. $under="";
  15. $com_start='/*';
  16. $com_end='*/';
  17. }
  18. %lb=( 'eax', '%al',
  19. 'ebx', '%bl',
  20. 'ecx', '%cl',
  21. 'edx', '%dl',
  22. 'ax', '%al',
  23. 'bx', '%bl',
  24. 'cx', '%cl',
  25. 'dx', '%dl',
  26. );
  27. %hb=( 'eax', '%ah',
  28. 'ebx', '%bh',
  29. 'ecx', '%ch',
  30. 'edx', '%dh',
  31. 'ax', '%ah',
  32. 'bx', '%bh',
  33. 'cx', '%ch',
  34. 'dx', '%dh',
  35. );
  36. %regs=( 'eax', '%eax',
  37. 'ebx', '%ebx',
  38. 'ecx', '%ecx',
  39. 'edx', '%edx',
  40. 'esi', '%esi',
  41. 'edi', '%edi',
  42. 'ebp', '%ebp',
  43. 'esp', '%esp',
  44. );
  45. %reg_val=(
  46. 'eax', 0x00,
  47. 'ebx', 0x03,
  48. 'ecx', 0x01,
  49. 'edx', 0x02,
  50. 'esi', 0x06,
  51. 'edi', 0x07,
  52. 'ebp', 0x05,
  53. 'esp', 0x04,
  54. );
  55. sub main'LB
  56. {
  57. (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
  58. return($lb{$_[0]});
  59. }
  60. sub main'HB
  61. {
  62. (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
  63. return($hb{$_[0]});
  64. }
  65. sub main'DWP
  66. {
  67. local($addr,$reg1,$reg2,$idx)=@_;
  68. $ret="";
  69. $addr =~ s/(^|[+ \t])([A-Za-z_]+[A-Za-z0-9_]+)($|[+ \t])/$1$under$2$3/;
  70. $reg1="$regs{$reg1}" if defined($regs{$reg1});
  71. $reg2="$regs{$reg2}" if defined($regs{$reg2});
  72. $ret.=$addr if ($addr ne "") && ($addr ne 0);
  73. if ($reg2 ne "")
  74. {
  75. if($idx ne "")
  76. { $ret.="($reg1,$reg2,$idx)"; }
  77. else
  78. { $ret.="($reg1,$reg2)"; }
  79. }
  80. else
  81. { $ret.="($reg1)" }
  82. return($ret);
  83. }
  84. sub main'BP
  85. {
  86. return(&main'DWP(@_));
  87. }
  88. sub main'BC
  89. {
  90. return @_;
  91. }
  92. sub main'DWC
  93. {
  94. return @_;
  95. }
  96. #sub main'BP
  97. # {
  98. # local($addr,$reg1,$reg2,$idx)=@_;
  99. #
  100. # $ret="";
  101. #
  102. # $addr =~ s/(^|[+ \t])([A-Za-z_]+)($|[+ \t])/$1$under$2$3/;
  103. # $reg1="$regs{$reg1}" if defined($regs{$reg1});
  104. # $reg2="$regs{$reg2}" if defined($regs{$reg2});
  105. # $ret.=$addr if ($addr ne "") && ($addr ne 0);
  106. # if ($reg2 ne "")
  107. # { $ret.="($reg1,$reg2,$idx)"; }
  108. # else
  109. # { $ret.="($reg1)" }
  110. # return($ret);
  111. # }
  112. sub main'mov { &out2("movl",@_); }
  113. sub main'movb { &out2("movb",@_); }
  114. sub main'and { &out2("andl",@_); }
  115. sub main'or { &out2("orl",@_); }
  116. sub main'shl { &out2("sall",@_); }
  117. sub main'shr { &out2("shrl",@_); }
  118. sub main'xor { &out2("xorl",@_); }
  119. sub main'xorb { &out2("xorb",@_); }
  120. sub main'add { &out2("addl",@_); }
  121. sub main'adc { &out2("adcl",@_); }
  122. sub main'sub { &out2("subl",@_); }
  123. sub main'rotl { &out2("roll",@_); }
  124. sub main'rotr { &out2("rorl",@_); }
  125. sub main'exch { &out2("xchg",@_); }
  126. sub main'cmp { &out2("cmpl",@_); }
  127. sub main'lea { &out2("leal",@_); }
  128. sub main'mul { &out1("mull",@_); }
  129. sub main'div { &out1("divl",@_); }
  130. sub main'jmp { &out1("jmp",@_); }
  131. sub main'jmp_ptr { &out1p("jmp",@_); }
  132. sub main'je { &out1("je",@_); }
  133. sub main'jle { &out1("jle",@_); }
  134. sub main'jne { &out1("jne",@_); }
  135. sub main'jnz { &out1("jnz",@_); }
  136. sub main'jz { &out1("jz",@_); }
  137. sub main'jge { &out1("jge",@_); }
  138. sub main'jl { &out1("jl",@_); }
  139. sub main'ja { &out1("ja",@_); }
  140. sub main'jae { &out1("jae",@_); }
  141. sub main'jb { &out1("jb",@_); }
  142. sub main'jbe { &out1("jbe",@_); }
  143. sub main'jc { &out1("jc",@_); }
  144. sub main'jnc { &out1("jnc",@_); }
  145. sub main'jno { &out1("jno",@_); }
  146. sub main'dec { &out1("decl",@_); }
  147. sub main'inc { &out1("incl",@_); }
  148. sub main'push { &out1("pushl",@_); $stack+=4; }
  149. sub main'pop { &out1("popl",@_); $stack-=4; }
  150. sub main'not { &out1("notl",@_); }
  151. sub main'call { &out1("call",$under.$_[0]); }
  152. sub main'ret { &out0("ret"); }
  153. sub main'nop { &out0("nop"); }
  154. # The bswapl instruction is new for the 486. Emulate if i386.
  155. sub main'bswap
  156. {
  157. if ($main'i386)
  158. {
  159. &main'comment("bswapl @_");
  160. &main'exch(main'HB(@_),main'LB(@_));
  161. &main'rotr(@_,16);
  162. &main'exch(main'HB(@_),main'LB(@_));
  163. }
  164. else
  165. {
  166. &out1("bswapl",@_);
  167. }
  168. }
  169. sub out2
  170. {
  171. local($name,$p1,$p2)=@_;
  172. local($l,$ll,$t);
  173. local(%special)=( "roll",0xD1C0,"rorl",0xD1C8,
  174. "rcll",0xD1D0,"rcrl",0xD1D8,
  175. "shll",0xD1E0,"shrl",0xD1E8,
  176. "sarl",0xD1F8);
  177. if ((defined($special{$name})) && defined($regs{$p1}) && ($p2 == 1))
  178. {
  179. $op=$special{$name}|$reg_val{$p1};
  180. $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
  181. $tmp2=sprintf(".byte %d\t",$op &0xff);
  182. push(@out,$tmp1);
  183. push(@out,$tmp2);
  184. $p2=&conv($p2);
  185. $p1=&conv($p1);
  186. &main'comment("$name $p2 $p1");
  187. return;
  188. }
  189. push(@out,"\t$name\t");
  190. $t=&conv($p2).",";
  191. $l=length($t);
  192. push(@out,$t);
  193. $ll=4-($l+9)/8;
  194. $tmp1=sprintf("\t" x $ll);
  195. push(@out,$tmp1);
  196. push(@out,&conv($p1)."\n");
  197. }
  198. sub out1
  199. {
  200. local($name,$p1)=@_;
  201. local($l,$t);
  202. local(%special)=("bswapl",0x0FC8);
  203. if ((defined($special{$name})) && defined($regs{$p1}))
  204. {
  205. $op=$special{$name}|$reg_val{$p1};
  206. $tmp1=sprintf(".byte %d\n",($op>>8)&0xff);
  207. $tmp2=sprintf(".byte %d\t",$op &0xff);
  208. push(@out,$tmp1);
  209. push(@out,$tmp2);
  210. $p2=&conv($p2);
  211. $p1=&conv($p1);
  212. &main'comment("$name $p2 $p1");
  213. return;
  214. }
  215. push(@out,"\t$name\t".&conv($p1)."\n");
  216. }
  217. sub out1p
  218. {
  219. local($name,$p1)=@_;
  220. local($l,$t);
  221. push(@out,"\t$name\t*".&conv($p1)."\n");
  222. }
  223. sub out0
  224. {
  225. push(@out,"\t$_[0]\n");
  226. }
  227. sub conv
  228. {
  229. local($p)=@_;
  230. # $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  231. $p=$regs{$p} if (defined($regs{$p}));
  232. $p =~ s/^(-{0,1}[0-9A-Fa-f]+)$/\$$1/;
  233. $p =~ s/^(0x[0-9A-Fa-f]+)$/\$$1/;
  234. return $p;
  235. }
  236. sub main'file
  237. {
  238. local($file)=@_;
  239. local($tmp)=<<"EOF";
  240. .file "$file.s"
  241. .version "01.01"
  242. gcc2_compiled.:
  243. EOF
  244. push(@out,$tmp);
  245. }
  246. sub main'function_begin
  247. {
  248. local($func)=@_;
  249. &main'external_label($func);
  250. $func=$under.$func;
  251. local($tmp)=<<"EOF";
  252. .text
  253. .align $align
  254. .globl $func
  255. EOF
  256. push(@out,$tmp);
  257. if ($main'cpp)
  258. { $tmp=push(@out,"\tTYPE($func,\@function)\n"); }
  259. elsif ($main'gaswin)
  260. { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
  261. else { $tmp=push(@out,"\t.type\t$func,\@function\n"); }
  262. push(@out,"$func:\n");
  263. $tmp=<<"EOF";
  264. pushl %ebp
  265. pushl %ebx
  266. pushl %esi
  267. pushl %edi
  268. EOF
  269. push(@out,$tmp);
  270. $stack=20;
  271. }
  272. sub main'function_begin_B
  273. {
  274. local($func,$extra)=@_;
  275. &main'external_label($func);
  276. $func=$under.$func;
  277. local($tmp)=<<"EOF";
  278. .text
  279. .align $align
  280. .globl $func
  281. EOF
  282. push(@out,$tmp);
  283. if ($main'cpp)
  284. { push(@out,"\tTYPE($func,\@function)\n"); }
  285. elsif ($main'gaswin)
  286. { $tmp=push(@out,"\t.def\t$func;\t.scl\t2;\t.type\t32;\t.endef\n"); }
  287. else { push(@out,"\t.type $func,\@function\n"); }
  288. push(@out,"$func:\n");
  289. $stack=4;
  290. }
  291. sub main'function_end
  292. {
  293. local($func)=@_;
  294. $func=$under.$func;
  295. local($tmp)=<<"EOF";
  296. popl %edi
  297. popl %esi
  298. popl %ebx
  299. popl %ebp
  300. ret
  301. .${func}_end:
  302. EOF
  303. push(@out,$tmp);
  304. if ($main'cpp)
  305. { push(@out,"\tSIZE($func,.${func}_end-$func)\n"); }
  306. elsif ($main'gaswin)
  307. { $tmp=push(@out,"\t.align 4\n"); }
  308. else { push(@out,"\t.size\t$func,.${func}_end-$func\n"); }
  309. push(@out,".ident \"$func\"\n");
  310. $stack=0;
  311. %label=();
  312. }
  313. sub main'function_end_A
  314. {
  315. local($func)=@_;
  316. local($tmp)=<<"EOF";
  317. popl %edi
  318. popl %esi
  319. popl %ebx
  320. popl %ebp
  321. ret
  322. EOF
  323. push(@out,$tmp);
  324. }
  325. sub main'function_end_B
  326. {
  327. local($func)=@_;
  328. $func=$under.$func;
  329. push(@out,".L_${func}_end:\n");
  330. if ($main'cpp)
  331. { push(@out,"\tSIZE($func,.L_${func}_end-$func)\n"); }
  332. elsif ($main'gaswin)
  333. { push(@out,"\t.align 4\n"); }
  334. else { push(@out,"\t.size\t$func,.L_${func}_end-$func\n"); }
  335. push(@out,".ident \"desasm.pl\"\n");
  336. $stack=0;
  337. %label=();
  338. }
  339. sub main'wparam
  340. {
  341. local($num)=@_;
  342. return(&main'DWP($stack+$num*4,"esp","",0));
  343. }
  344. sub main'stack_push
  345. {
  346. local($num)=@_;
  347. $stack+=$num*4;
  348. &main'sub("esp",$num*4);
  349. }
  350. sub main'stack_pop
  351. {
  352. local($num)=@_;
  353. $stack-=$num*4;
  354. &main'add("esp",$num*4);
  355. }
  356. sub main'swtmp
  357. {
  358. return(&main'DWP($_[0]*4,"esp","",0));
  359. }
  360. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  361. #sub main'wtmp
  362. # {
  363. # local($num)=@_;
  364. #
  365. # return(&main'DWP(-($num+1)*4,"esp","",0));
  366. # }
  367. sub main'comment
  368. {
  369. foreach (@_)
  370. {
  371. if (/^\s*$/)
  372. { push(@out,"\n"); }
  373. else
  374. { push(@out,"\t$com_start $_ $com_end\n"); }
  375. }
  376. }
  377. sub main'label
  378. {
  379. if (!defined($label{$_[0]}))
  380. {
  381. $label{$_[0]}=".${label}${_[0]}";
  382. $label++;
  383. }
  384. return($label{$_[0]});
  385. }
  386. sub main'set_label
  387. {
  388. if (!defined($label{$_[0]}))
  389. {
  390. $label{$_[0]}=".${label}${_[0]}";
  391. $label++;
  392. }
  393. push(@out,".align $align\n") if ($_[1] != 0);
  394. push(@out,"$label{$_[0]}:\n");
  395. }
  396. sub main'file_end
  397. {
  398. }
  399. sub main'data_word
  400. {
  401. push(@out,"\t.long $_[0]\n");
  402. }