x86nasm.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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'div { &out1("div",@_); }
  139. sub main'dec { &out1("dec",@_); }
  140. sub main'inc { &out1("inc",@_); }
  141. sub main'jmp { &out1("jmp",@_); }
  142. sub main'jmp_ptr { &out1p("jmp",@_); }
  143. # This is a bit of a kludge: declare all branches as NEAR.
  144. $near=($main'mwerks)?'':'NEAR';
  145. sub main'je { &out1("je $near",@_); }
  146. sub main'jle { &out1("jle $near",@_); }
  147. sub main'jz { &out1("jz $near",@_); }
  148. sub main'jge { &out1("jge $near",@_); }
  149. sub main'jl { &out1("jl $near",@_); }
  150. sub main'ja { &out1("ja $near",@_); }
  151. sub main'jae { &out1("jae $near",@_); }
  152. sub main'jb { &out1("jb $near",@_); }
  153. sub main'jbe { &out1("jbe $near",@_); }
  154. sub main'jc { &out1("jc $near",@_); }
  155. sub main'jnc { &out1("jnc $near",@_); }
  156. sub main'jnz { &out1("jnz $near",@_); }
  157. sub main'jne { &out1("jne $near",@_); }
  158. sub main'jno { &out1("jno $near",@_); }
  159. sub main'push { &out1("push",@_); $stack+=4; }
  160. sub main'pop { &out1("pop",@_); $stack-=4; }
  161. sub main'pushf { &out0("pushfd"); $stack+=4; }
  162. sub main'popf { &out0("popfd"); $stack-=4; }
  163. sub main'bswap { &out1("bswap",@_); &using486(); }
  164. sub main'not { &out1("not",@_); }
  165. sub main'call { &out1("call",($_[0]=~/^\@L/?'':$under).$_[0]); }
  166. sub main'call_ptr { &out1p("call",@_); }
  167. sub main'ret { &out0("ret"); }
  168. sub main'nop { &out0("nop"); }
  169. sub main'test { &out2("test",@_); }
  170. sub main'bt { &out2("bt",@_); }
  171. sub main'leave { &out0("leave"); }
  172. sub main'cpuid { &out0("cpuid"); }
  173. sub main'rdtsc { &out0("rdtsc"); }
  174. sub main'halt { &out0("hlt"); }
  175. sub main'movz { &out2("movzx",@_); }
  176. sub main'neg { &out1("neg",@_); }
  177. sub main'cld { &out0("cld"); }
  178. # SSE2
  179. sub main'emms { &out0("emms"); }
  180. sub main'movd { &out2("movd",@_); }
  181. sub main'movq { &out2("movq",@_); }
  182. sub main'movdqu { &out2("movdqu",@_); }
  183. sub main'movdqa { &out2("movdqa",@_); }
  184. sub main'movdq2q{ &out2("movdq2q",@_); }
  185. sub main'movq2dq{ &out2("movq2dq",@_); }
  186. sub main'paddq { &out2("paddq",@_); }
  187. sub main'pmuludq{ &out2("pmuludq",@_); }
  188. sub main'psrlq { &out2("psrlq",@_); }
  189. sub main'psllq { &out2("psllq",@_); }
  190. sub main'pxor { &out2("pxor",@_); }
  191. sub main'por { &out2("por",@_); }
  192. sub main'pand { &out2("pand",@_); }
  193. sub out2
  194. {
  195. my($name,$p1,$p2)=@_;
  196. my($l,$t);
  197. push(@out,"\t$name\t");
  198. if (!$main'mwerks and $name eq "lea")
  199. {
  200. $p1 =~ s/^[^\[]*\[/\[/;
  201. $p2 =~ s/^[^\[]*\[/\[/;
  202. }
  203. $t=&conv($p1).",";
  204. $l=length($t);
  205. push(@out,$t);
  206. $l=4-($l+9)/8;
  207. push(@out,"\t" x $l);
  208. push(@out,&conv($p2));
  209. push(@out,"\n");
  210. }
  211. sub out0
  212. {
  213. my($name)=@_;
  214. push(@out,"\t$name\n");
  215. }
  216. sub out1
  217. {
  218. my($name,$p1)=@_;
  219. my($l,$t);
  220. push(@out,"\t$name\t".&conv($p1)."\n");
  221. }
  222. sub conv
  223. {
  224. my($p)=@_;
  225. $p =~ s/0x([0-9A-Fa-f]+)/0$1h/;
  226. return $p;
  227. }
  228. sub using486
  229. {
  230. return if $using486;
  231. $using486++;
  232. grep(s/\.386/\.486/,@out);
  233. }
  234. sub main'file
  235. {
  236. if ($main'mwerks) { push(@out,".section\t.text\n"); }
  237. else {
  238. local $tmp=<<___;
  239. %ifdef __omf__
  240. section code use32 class=code
  241. %else
  242. section .text
  243. %endif
  244. ___
  245. push(@out,$tmp);
  246. }
  247. }
  248. sub main'function_begin
  249. {
  250. my($func,$extra)=@_;
  251. push(@labels,$func);
  252. my($tmp)=<<"EOF";
  253. global $under$func
  254. $under$func:
  255. push ebp
  256. push ebx
  257. push esi
  258. push edi
  259. EOF
  260. push(@out,$tmp);
  261. $stack=20;
  262. }
  263. sub main'function_begin_B
  264. {
  265. my($func,$extra)=@_;
  266. my($tmp)=<<"EOF";
  267. global $under$func
  268. $under$func:
  269. EOF
  270. push(@out,$tmp);
  271. $stack=4;
  272. }
  273. sub main'function_end
  274. {
  275. my($func)=@_;
  276. my($tmp)=<<"EOF";
  277. pop edi
  278. pop esi
  279. pop ebx
  280. pop ebp
  281. ret
  282. EOF
  283. push(@out,$tmp);
  284. $stack=0;
  285. %label=();
  286. }
  287. sub main'function_end_B
  288. {
  289. $stack=0;
  290. %label=();
  291. }
  292. sub main'function_end_A
  293. {
  294. my($func)=@_;
  295. my($tmp)=<<"EOF";
  296. pop edi
  297. pop esi
  298. pop ebx
  299. pop ebp
  300. ret
  301. EOF
  302. push(@out,$tmp);
  303. }
  304. sub main'file_end
  305. {
  306. }
  307. sub main'wparam
  308. {
  309. my($num)=@_;
  310. return(&main'DWP($stack+$num*4,"esp","",0));
  311. }
  312. sub main'swtmp
  313. {
  314. return(&main'DWP($_[0]*4,"esp","",0));
  315. }
  316. # Should use swtmp, which is above esp. Linix can trash the stack above esp
  317. #sub main'wtmp
  318. # {
  319. # my($num)=@_;
  320. #
  321. # return(&main'DWP(-(($num+1)*4),"esp","",0));
  322. # }
  323. sub main'comment
  324. {
  325. foreach (@_)
  326. {
  327. push(@out,"\t; $_\n");
  328. }
  329. }
  330. sub main'public_label
  331. {
  332. $label{$_[0]}="${under}${_[0]}" if (!defined($label{$_[0]}));
  333. push(@out,"global\t$label{$_[0]}\n");
  334. }
  335. sub main'label
  336. {
  337. if (!defined($label{$_[0]}))
  338. {
  339. $label{$_[0]}="\@${label}${_[0]}";
  340. $label++;
  341. }
  342. return($label{$_[0]});
  343. }
  344. sub main'set_label
  345. {
  346. if (!defined($label{$_[0]}))
  347. {
  348. $label{$_[0]}="\@${label}${_[0]}";
  349. $label++;
  350. }
  351. if ($_[1]!=0 && $_[1]>1)
  352. {
  353. main'align($_[1]);
  354. }
  355. push(@out,"$label{$_[0]}:\n");
  356. }
  357. sub main'data_byte
  358. {
  359. push(@out,(($main'mwerks)?".byte\t":"DB\t").join(',',@_)."\n");
  360. }
  361. sub main'data_word
  362. {
  363. push(@out,(($main'mwerks)?".long\t":"DD\t").join(',',@_)."\n");
  364. }
  365. sub main'align
  366. {
  367. push(@out,".") if ($main'mwerks);
  368. push(@out,"align\t$_[0]\n");
  369. }
  370. sub out1p
  371. {
  372. my($name,$p1)=@_;
  373. my($l,$t);
  374. push(@out,"\t$name\t".&conv($p1)."\n");
  375. }
  376. sub main'picmeup
  377. {
  378. local($dst,$sym)=@_;
  379. &main'lea($dst,&main'DWP($sym));
  380. }
  381. sub main'blindpop { &out1("pop",@_); }
  382. sub main'initseg
  383. {
  384. local($f)=@_;
  385. if ($main'win32)
  386. {
  387. local($tmp)=<<___;
  388. segment .CRT\$XCU data
  389. extern $under$f
  390. DD $under$f
  391. ___
  392. push(@out,$tmp);
  393. }
  394. }
  395. 1;