x86ms.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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'imul { &out2("imul",@_); }
  131. sub main'div { &out1("div",@_); }
  132. sub main'dec { &out1("dec",@_); }
  133. sub main'inc { &out1("inc",@_); }
  134. sub main'jmp { &out1("jmp",@_); }
  135. sub main'jmp_ptr { &out1p("jmp",@_); }
  136. sub main'je { &out1("je",@_); }
  137. sub main'jle { &out1("jle",@_); }
  138. sub main'jz { &out1("jz",@_); }
  139. sub main'jge { &out1("jge",@_); }
  140. sub main'jl { &out1("jl",@_); }
  141. sub main'ja { &out1("ja",@_); }
  142. sub main'jae { &out1("jae",@_); }
  143. sub main'jb { &out1("jb",@_); }
  144. sub main'jbe { &out1("jbe",@_); }
  145. sub main'jc { &out1("jc",@_); }
  146. sub main'jnc { &out1("jnc",@_); }
  147. sub main'jnz { &out1("jnz",@_); }
  148. sub main'jne { &out1("jne",@_); }
  149. sub main'jno { &out1("jno",@_); }
  150. sub main'push { &out1("push",@_); $stack+=4; }
  151. sub main'pop { &out1("pop",@_); $stack-=4; }
  152. sub main'pushf { &out0("pushfd"); $stack+=4; }
  153. sub main'popf { &out0("popfd"); $stack-=4; }
  154. sub main'bswap { &out1("bswap",@_); &using486(); }
  155. sub main'not { &out1("not",@_); }
  156. sub main'call { &out1("call",($_[0]=~/^\$L/?'':'_').$_[0]); }
  157. sub main'call_ptr { &out1p("call",@_); }
  158. sub main'ret { &out0("ret"); }
  159. sub main'nop { &out0("nop"); }
  160. sub main'test { &out2("test",@_); }
  161. sub main'bt { &out2("bt",@_); }
  162. sub main'leave { &out0("leave"); }
  163. sub main'cpuid { &out0("DW\t0A20Fh"); }
  164. sub main'rdtsc { &out0("DW\t0310Fh"); }
  165. sub main'halt { &out0("hlt"); }
  166. sub main'movz { &out2("movzx",@_); }
  167. sub main'neg { &out1("neg",@_); }
  168. sub main'cld { &out0("cld"); }
  169. # SSE2
  170. sub main'emms { &out0("emms"); }
  171. sub main'movd { &out2("movd",@_); }
  172. sub main'movq { &out2("movq",@_); }
  173. sub main'movdqu { &out2("movdqu",@_); }
  174. sub main'movdqa { &out2("movdqa",@_); }
  175. sub main'movdq2q{ &out2("movdq2q",@_); }
  176. sub main'movq2dq{ &out2("movq2dq",@_); }
  177. sub main'paddq { &out2("paddq",@_); }
  178. sub main'pmuludq{ &out2("pmuludq",@_); }
  179. sub main'psrlq { &out2("psrlq",@_); }
  180. sub main'psllq { &out2("psllq",@_); }
  181. sub main'pxor { &out2("pxor",@_); }
  182. sub main'por { &out2("por",@_); }
  183. sub main'pand { &out2("pand",@_); }
  184. sub out2
  185. {
  186. local($name,$p1,$p2)=@_;
  187. local($l,$t,$line);
  188. $line="\t$name\t";
  189. $t=&conv($p1).",";
  190. $l=length($t);
  191. $line.="$t";
  192. $l=4-($l+9)/8;
  193. $line.="\t" x $l;
  194. $line.=&conv($p2);
  195. if ($line=~/\bxmm[0-7]\b/i) { $line=~s/\b[A-Z]+WORD\s+PTR/XMMWORD PTR/i; }
  196. push(@out,$line."\n");
  197. }
  198. sub out0
  199. {
  200. local($name)=@_;
  201. push(@out,"\t$name\n");
  202. }
  203. sub out1
  204. {
  205. local($name,$p1)=@_;
  206. local($l,$t);
  207. push(@out,"\t$name\t".&conv($p1)."\n");
  208. }
  209. sub conv
  210. {
  211. local($p)=@_;
  212. $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  213. return $p;
  214. }
  215. sub using486
  216. {
  217. return if $using486;
  218. $using486++;
  219. grep(s/\.386/\.486/,@out);
  220. }
  221. sub main'file
  222. {
  223. local($file)=@_;
  224. local($tmp)=<<"EOF";
  225. TITLE $file.asm
  226. .386
  227. .model FLAT
  228. _TEXT\$ SEGMENT PAGE 'CODE'
  229. EOF
  230. push(@out,$tmp);
  231. }
  232. sub main'function_begin
  233. {
  234. local($func,$extra)=@_;
  235. push(@labels,$func);
  236. local($tmp)=<<"EOF";
  237. PUBLIC _$func
  238. $extra
  239. _$func PROC NEAR
  240. push ebp
  241. push ebx
  242. push esi
  243. push edi
  244. EOF
  245. push(@out,$tmp);
  246. $stack=20;
  247. }
  248. sub main'function_begin_B
  249. {
  250. local($func,$extra)=@_;
  251. local($tmp)=<<"EOF";
  252. PUBLIC _$func
  253. $extra
  254. _$func PROC NEAR
  255. EOF
  256. push(@out,$tmp);
  257. $stack=4;
  258. }
  259. sub main'function_end
  260. {
  261. local($func)=@_;
  262. local($tmp)=<<"EOF";
  263. pop edi
  264. pop esi
  265. pop ebx
  266. pop ebp
  267. ret
  268. _$func ENDP
  269. EOF
  270. push(@out,$tmp);
  271. $stack=0;
  272. %label=();
  273. }
  274. sub main'function_end_B
  275. {
  276. local($func)=@_;
  277. local($tmp)=<<"EOF";
  278. _$func ENDP
  279. EOF
  280. push(@out,$tmp);
  281. $stack=0;
  282. %label=();
  283. }
  284. sub main'function_end_A
  285. {
  286. local($func)=@_;
  287. local($tmp)=<<"EOF";
  288. pop edi
  289. pop esi
  290. pop ebx
  291. pop ebp
  292. ret
  293. EOF
  294. push(@out,$tmp);
  295. }
  296. sub main'file_end
  297. {
  298. # try to detect if SSE2 or MMX extensions were used...
  299. my $xmmheader=<<___;
  300. .686
  301. .XMM
  302. IF \@Version LT 800
  303. XMMWORD STRUCT 16
  304. DQ 2 dup (?)
  305. XMMWORD ENDS
  306. ENDIF
  307. ___
  308. if (grep {/\b[x]?mm[0-7]\b/i} @out) {
  309. grep {s/\.[3-7]86/$xmmheader/} @out;
  310. }
  311. push(@out,"_TEXT\$ ENDS\n");
  312. push(@out,"END\n");
  313. }
  314. sub main'wparam
  315. {
  316. local($num)=@_;
  317. return(&main'DWP($stack+$num*4,"esp","",0));
  318. }
  319. sub main'swtmp
  320. {
  321. return(&main'DWP($_[0]*4,"esp","",0));
  322. }
  323. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  324. #sub main'wtmp
  325. # {
  326. # local($num)=@_;
  327. #
  328. # return(&main'DWP(-(($num+1)*4),"esp","",0));
  329. # }
  330. sub main'comment
  331. {
  332. foreach (@_)
  333. {
  334. push(@out,"\t; $_\n");
  335. }
  336. }
  337. sub main'public_label
  338. {
  339. $label{$_[0]}="_$_[0]" if (!defined($label{$_[0]}));
  340. push(@out,"PUBLIC\t$label{$_[0]}\n");
  341. }
  342. sub main'label
  343. {
  344. if (!defined($label{$_[0]}))
  345. {
  346. $label{$_[0]}="\$${label}${_[0]}";
  347. $label++;
  348. }
  349. return($label{$_[0]});
  350. }
  351. sub main'set_label
  352. {
  353. if (!defined($label{$_[0]}))
  354. {
  355. $label{$_[0]}="\$${label}${_[0]}";
  356. $label++;
  357. }
  358. if ($_[1]!=0 && $_[1]>1)
  359. {
  360. main'align($_[1]);
  361. }
  362. if((defined $_[2]) && ($_[2] == 1))
  363. {
  364. push(@out,"$label{$_[0]}::\n");
  365. }
  366. elsif ($label{$_[0]} !~ /^\$/)
  367. {
  368. push(@out,"$label{$_[0]}\tLABEL PTR\n");
  369. }
  370. else
  371. {
  372. push(@out,"$label{$_[0]}:\n");
  373. }
  374. }
  375. sub main'data_byte
  376. {
  377. push(@out,"\tDB\t".join(',',@_)."\n");
  378. }
  379. sub main'data_word
  380. {
  381. push(@out,"\tDD\t".join(',',@_)."\n");
  382. }
  383. sub main'align
  384. {
  385. push(@out,"\tALIGN\t$_[0]\n");
  386. }
  387. sub out1p
  388. {
  389. local($name,$p1)=@_;
  390. local($l,$t);
  391. push(@out,"\t$name\t".&conv($p1)."\n");
  392. }
  393. sub main'picmeup
  394. {
  395. local($dst,$sym)=@_;
  396. &main'lea($dst,&main'DWP($sym));
  397. }
  398. sub main'blindpop { &out1("pop",@_); }
  399. sub main'initseg
  400. {
  401. local($f)=@_;
  402. local($tmp)=<<___;
  403. OPTION DOTNAME
  404. .CRT\$XCU SEGMENT DWORD PUBLIC 'DATA'
  405. EXTRN _$f:NEAR
  406. DD _$f
  407. .CRT\$XCU ENDS
  408. ___
  409. push(@out,$tmp);
  410. }
  411. 1;