x86nasm.pl 7.7 KB

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