x86ms.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. #!/usr/local/bin/perl
  2. package x86ms;
  3. $label="L000";
  4. %lb=( 'eax', 'al',
  5. 'ebx', 'bl',
  6. 'ecx', 'cl',
  7. 'edx', 'dl',
  8. 'ax', 'al',
  9. 'bx', 'bl',
  10. 'cx', 'cl',
  11. 'dx', 'dl',
  12. );
  13. %hb=( 'eax', 'ah',
  14. 'ebx', 'bh',
  15. 'ecx', 'ch',
  16. 'edx', 'dh',
  17. 'ax', 'ah',
  18. 'bx', 'bh',
  19. 'cx', 'ch',
  20. 'dx', 'dh',
  21. );
  22. sub main'asm_init_output { @out=(); }
  23. sub main'asm_get_output { return(@out); }
  24. sub main'get_labels { return(@labels); }
  25. sub main'external_label
  26. {
  27. push(@labels,@_);
  28. foreach (@_) {
  29. push(@out, "EXTRN\t_$_:DWORD\n");
  30. }
  31. }
  32. sub main'LB
  33. {
  34. (defined($lb{$_[0]})) || die "$_[0] does not have a 'low byte'\n";
  35. return($lb{$_[0]});
  36. }
  37. sub main'HB
  38. {
  39. (defined($hb{$_[0]})) || die "$_[0] does not have a 'high byte'\n";
  40. return($hb{$_[0]});
  41. }
  42. sub main'BP
  43. {
  44. &get_mem("BYTE",@_);
  45. }
  46. sub main'DWP
  47. {
  48. &get_mem("DWORD",@_);
  49. }
  50. sub main'QWP
  51. {
  52. &get_mem("QWORD",@_);
  53. }
  54. sub main'BC
  55. {
  56. return @_;
  57. }
  58. sub main'DWC
  59. {
  60. return @_;
  61. }
  62. sub main'stack_push
  63. {
  64. local($num)=@_;
  65. $stack+=$num*4;
  66. &main'sub("esp",$num*4);
  67. }
  68. sub main'stack_pop
  69. {
  70. local($num)=@_;
  71. $stack-=$num*4;
  72. &main'add("esp",$num*4);
  73. }
  74. sub get_mem
  75. {
  76. local($size,$addr,$reg1,$reg2,$idx)=@_;
  77. local($t,$post);
  78. local($ret)="$size PTR ";
  79. $addr =~ s/^\s+//;
  80. if ($addr =~ /^(.+)\+(.+)$/)
  81. {
  82. $reg2=&conv($1);
  83. $addr="_$2";
  84. }
  85. elsif ($addr =~ /^[_a-z][_a-z0-9]*$/i)
  86. {
  87. $addr="_$addr";
  88. }
  89. if ($addr =~ /^.+\-.+$/) { $addr="($addr)"; }
  90. $reg1="$regs{$reg1}" if defined($regs{$reg1});
  91. $reg2="$regs{$reg2}" if defined($regs{$reg2});
  92. if (($addr ne "") && ($addr ne 0))
  93. {
  94. if ($addr !~ /^-/)
  95. { $ret.=$addr; }
  96. else { $post=$addr; }
  97. }
  98. if ($reg2 ne "")
  99. {
  100. $t="";
  101. $t="*$idx" if ($idx != 0);
  102. $reg1="+".$reg1 if ("$reg1$post" ne "");
  103. $ret.="[$reg2$t$reg1$post]";
  104. }
  105. else
  106. {
  107. $ret.="[$reg1$post]"
  108. }
  109. $ret =~ s/\[\]//; # in case $addr was the only argument
  110. return($ret);
  111. }
  112. sub main'mov { &out2("mov",@_); }
  113. sub main'movb { &out2("mov",@_); }
  114. sub main'and { &out2("and",@_); }
  115. sub main'or { &out2("or",@_); }
  116. sub main'shl { &out2("shl",@_); }
  117. sub main'shr { &out2("shr",@_); }
  118. sub main'xor { &out2("xor",@_); }
  119. sub main'xorb { &out2("xor",@_); }
  120. sub main'add { &out2("add",@_); }
  121. sub main'adc { &out2("adc",@_); }
  122. sub main'sub { &out2("sub",@_); }
  123. sub main'sbb { &out2("sbb",@_); }
  124. sub main'rotl { &out2("rol",@_); }
  125. sub main'rotr { &out2("ror",@_); }
  126. sub main'exch { &out2("xchg",@_); }
  127. sub main'cmp { &out2("cmp",@_); }
  128. sub main'lea { &out2("lea",@_); }
  129. sub main'mul { &out1("mul",@_); }
  130. sub main'div { &out1("div",@_); }
  131. sub main'dec { &out1("dec",@_); }
  132. sub main'inc { &out1("inc",@_); }
  133. sub main'jmp { &out1("jmp",@_); }
  134. sub main'jmp_ptr { &out1p("jmp",@_); }
  135. sub main'je { &out1("je",@_); }
  136. sub main'jle { &out1("jle",@_); }
  137. sub main'jz { &out1("jz",@_); }
  138. sub main'jge { &out1("jge",@_); }
  139. sub main'jl { &out1("jl",@_); }
  140. sub main'ja { &out1("ja",@_); }
  141. sub main'jae { &out1("jae",@_); }
  142. sub main'jb { &out1("jb",@_); }
  143. sub main'jbe { &out1("jbe",@_); }
  144. sub main'jc { &out1("jc",@_); }
  145. sub main'jnc { &out1("jnc",@_); }
  146. sub main'jnz { &out1("jnz",@_); }
  147. sub main'jne { &out1("jne",@_); }
  148. sub main'jno { &out1("jno",@_); }
  149. sub main'push { &out1("push",@_); $stack+=4; }
  150. sub main'pop { &out1("pop",@_); $stack-=4; }
  151. sub main'pushf { &out0("pushfd"); $stack+=4; }
  152. sub main'popf { &out0("popfd"); $stack-=4; }
  153. sub main'bswap { &out1("bswap",@_); &using486(); }
  154. sub main'not { &out1("not",@_); }
  155. sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
  156. sub main'call_ptr { &out1p("call",@_); }
  157. sub main'ret { &out0("ret"); }
  158. sub main'nop { &out0("nop"); }
  159. sub main'test { &out2("test",@_); }
  160. sub main'bt { &out2("bt",@_); }
  161. sub main'leave { &out0("leave"); }
  162. sub main'cpuid { &out0("DW\t0A20Fh"); }
  163. sub main'rdtsc { &out0("DW\t0310Fh"); }
  164. sub main'halt { &out0("hlt"); }
  165. sub main'movz { &out2("movzx",@_); }
  166. sub main'neg { &out1("neg",@_); }
  167. sub main'cld { &out0("cld"); }
  168. # SSE2
  169. sub main'emms { &out0("emms"); }
  170. sub main'movd { &out2("movd",@_); }
  171. sub main'movq { &out2("movq",@_); }
  172. sub main'movdqu { &out2("movdqu",@_); }
  173. sub main'movdqa { &out2("movdqa",@_); }
  174. sub main'movdq2q{ &out2("movdq2q",@_); }
  175. sub main'movq2dq{ &out2("movq2dq",@_); }
  176. sub main'paddq { &out2("paddq",@_); }
  177. sub main'pmuludq{ &out2("pmuludq",@_); }
  178. sub main'psrlq { &out2("psrlq",@_); }
  179. sub main'psllq { &out2("psllq",@_); }
  180. sub main'pxor { &out2("pxor",@_); }
  181. sub main'por { &out2("por",@_); }
  182. sub main'pand { &out2("pand",@_); }
  183. sub out2
  184. {
  185. local($name,$p1,$p2)=@_;
  186. local($l,$t);
  187. push(@out,"\t$name\t");
  188. $t=&conv($p1).",";
  189. $l=length($t);
  190. push(@out,$t);
  191. $l=4-($l+9)/8;
  192. push(@out,"\t" x $l);
  193. push(@out,&conv($p2));
  194. push(@out,"\n");
  195. }
  196. sub out0
  197. {
  198. local($name)=@_;
  199. push(@out,"\t$name\n");
  200. }
  201. sub out1
  202. {
  203. local($name,$p1)=@_;
  204. local($l,$t);
  205. push(@out,"\t$name\t".&conv($p1)."\n");
  206. }
  207. sub conv
  208. {
  209. local($p)=@_;
  210. $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  211. return $p;
  212. }
  213. sub using486
  214. {
  215. return if $using486;
  216. $using486++;
  217. grep(s/\.386/\.486/,@out);
  218. }
  219. sub main'file
  220. {
  221. local($file)=@_;
  222. local($tmp)=<<"EOF";
  223. TITLE $file.asm
  224. .386
  225. .model FLAT
  226. _TEXT\$ SEGMENT PAGE 'CODE'
  227. EOF
  228. push(@out,$tmp);
  229. }
  230. sub main'function_begin
  231. {
  232. local($func,$extra)=@_;
  233. push(@labels,$func);
  234. local($tmp)=<<"EOF";
  235. PUBLIC _$func
  236. $extra
  237. _$func PROC NEAR
  238. push ebp
  239. push ebx
  240. push esi
  241. push edi
  242. EOF
  243. push(@out,$tmp);
  244. $stack=20;
  245. }
  246. sub main'function_begin_B
  247. {
  248. local($func,$extra)=@_;
  249. local($tmp)=<<"EOF";
  250. PUBLIC _$func
  251. $extra
  252. _$func PROC NEAR
  253. EOF
  254. push(@out,$tmp);
  255. $stack=4;
  256. }
  257. sub main'function_end
  258. {
  259. local($func)=@_;
  260. local($tmp)=<<"EOF";
  261. pop edi
  262. pop esi
  263. pop ebx
  264. pop ebp
  265. ret
  266. _$func ENDP
  267. EOF
  268. push(@out,$tmp);
  269. $stack=0;
  270. %label=();
  271. }
  272. sub main'function_end_B
  273. {
  274. local($func)=@_;
  275. local($tmp)=<<"EOF";
  276. _$func ENDP
  277. EOF
  278. push(@out,$tmp);
  279. $stack=0;
  280. %label=();
  281. }
  282. sub main'function_end_A
  283. {
  284. local($func)=@_;
  285. local($tmp)=<<"EOF";
  286. pop edi
  287. pop esi
  288. pop ebx
  289. pop ebp
  290. ret
  291. EOF
  292. push(@out,$tmp);
  293. }
  294. sub main'file_end
  295. {
  296. # try to detect if SSE2 or MMX extensions were used...
  297. if (grep {/xmm[0-7]\s*,/i} @out) {
  298. grep {s/\.[3-7]86/\.686\n\t\.XMM/} @out;
  299. }
  300. elsif (grep {/mm[0-7]\s*,/i} @out) {
  301. grep {s/\.[3-7]86/\.686\n\t\.MMX/} @out;
  302. }
  303. push(@out,"_TEXT\$ ENDS\n");
  304. push(@out,"END\n");
  305. }
  306. sub main'wparam
  307. {
  308. local($num)=@_;
  309. return(&main'DWP($stack+$num*4,"esp","",0));
  310. }
  311. sub main'swtmp
  312. {
  313. return(&main'DWP($_[0]*4,"esp","",0));
  314. }
  315. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  316. #sub main'wtmp
  317. # {
  318. # local($num)=@_;
  319. #
  320. # return(&main'DWP(-(($num+1)*4),"esp","",0));
  321. # }
  322. sub main'comment
  323. {
  324. foreach (@_)
  325. {
  326. push(@out,"\t; $_\n");
  327. }
  328. }
  329. sub main'public_label
  330. {
  331. $label{$_[0]}="_$_[0]" if (!defined($label{$_[0]}));
  332. push(@out,"PUBLIC\t$label{$_[0]}\n");
  333. }
  334. sub main'label
  335. {
  336. if (!defined($label{$_[0]}))
  337. {
  338. $label{$_[0]}="\$${label}${_[0]}";
  339. $label++;
  340. }
  341. return($label{$_[0]});
  342. }
  343. sub main'set_label
  344. {
  345. if (!defined($label{$_[0]}))
  346. {
  347. $label{$_[0]}="\$${label}${_[0]}";
  348. $label++;
  349. }
  350. if ($_[1]!=0 && $_[1]>1)
  351. {
  352. main'align($_[1]);
  353. }
  354. if((defined $_[2]) && ($_[2] == 1))
  355. {
  356. push(@out,"$label{$_[0]}::\n");
  357. }
  358. elsif ($label{$_[0]} !~ /^\$/)
  359. {
  360. push(@out,"$label{$_[0]}\tLABEL PTR\n");
  361. }
  362. else
  363. {
  364. push(@out,"$label{$_[0]}:\n");
  365. }
  366. }
  367. sub main'data_byte
  368. {
  369. push(@out,"\tDB\t".join(',',@_)."\n");
  370. }
  371. sub main'data_word
  372. {
  373. push(@out,"\tDD\t".join(',',@_)."\n");
  374. }
  375. sub main'align
  376. {
  377. push(@out,"\tALIGN\t$_[0]\n");
  378. }
  379. sub out1p
  380. {
  381. local($name,$p1)=@_;
  382. local($l,$t);
  383. push(@out,"\t$name\t".&conv($p1)."\n");
  384. }
  385. sub main'picmeup
  386. {
  387. local($dst,$sym)=@_;
  388. &main'lea($dst,&main'DWP($sym));
  389. }
  390. sub main'blindpop { &out1("pop",@_); }
  391. sub main'initseg
  392. {
  393. local($f)=@_;
  394. local($tmp)=<<___;
  395. OPTION DOTNAME
  396. .CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
  397. EXTRN _$f:NEAR
  398. DD _$f
  399. .CRT\$XCU ENDS
  400. ___
  401. push(@out,$tmp);
  402. }
  403. 1;