ag386nsm.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. {
  2. $Id$
  3. Copyright (c) 1996,97 by Florian Klaempfl
  4. This unit implements an asmoutput class for the Nasm assembler with
  5. Intel syntax for the i386+
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. { R- Necessary for the in [] }
  20. {$ifdef TP}
  21. {$N+,E+,R-}
  22. {$endif}
  23. unit ag386nsm;
  24. interface
  25. uses aasm,assemble;
  26. type
  27. pi386nasmasmlist=^ti386nasmasmlist;
  28. ti386nasmasmlist = object(tasmlist)
  29. procedure WriteTree(p:paasmoutput);virtual;
  30. procedure WriteAsmList;virtual;
  31. end;
  32. implementation
  33. uses
  34. dos,globals,systems,cobjects,i386,
  35. strings,files,verbose
  36. {$ifdef GDB}
  37. ,gdb
  38. {$endif GDB}
  39. ;
  40. const
  41. line_length = 70;
  42. extstr : array[EXT_NEAR..EXT_ABS] of String[8] =
  43. ('NEAR','FAR','PROC','BYTE','WORD','DWORD',
  44. 'CODEPTR','DATAPTR','FWORD','PWORD','QWORD','TBYTE','ABS');
  45. function double2str(d : double) : string;
  46. var
  47. hs : string;
  48. p : byte;
  49. begin
  50. str(d,hs);
  51. { nasm expects a lowercase e }
  52. p:=pos('E',hs);
  53. if p>0 then
  54. hs[p]:='e';
  55. p:=pos('+',hs);
  56. if p>0 then
  57. delete(hs,p,1);
  58. double2str:=lower(hs);
  59. end;
  60. function extended2str(e : extended) : string;
  61. var
  62. hs : string;
  63. p : byte;
  64. begin
  65. {$ifdef VER0_99_5}
  66. str(double(e),hs);
  67. {$else}
  68. str(e,hs);
  69. {$endif}
  70. { nasm expects a lowercase e }
  71. p:=pos('E',hs);
  72. if p>0 then
  73. hs[p]:='e';
  74. p:=pos('+',hs);
  75. if p>0 then
  76. delete(hs,p,1);
  77. extended2str:=lower(hs);
  78. end;
  79. function comp2str(d : bestreal) : string;
  80. type
  81. pdouble = ^double;
  82. var
  83. c : comp;
  84. dd : pdouble;
  85. begin
  86. {$ifdef TP}
  87. c:=d;
  88. {$else}
  89. c:=comp(d);
  90. {$endif}
  91. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  92. comp2str:=double2str(dd^);
  93. end;
  94. function getreferencestring(const ref : treference) : string;
  95. var
  96. s : string;
  97. first : boolean;
  98. begin
  99. if ref.isintvalue then
  100. s:= tostr(ref.offset)
  101. else
  102. with ref do
  103. begin
  104. first:=true;
  105. if ref.segment<>R_DEFAULT_SEG then
  106. s:='['+int_reg2str[segment]+':'
  107. else
  108. s:='[';
  109. if assigned(symbol) then
  110. begin
  111. s:=s+symbol^;
  112. first:=false;
  113. end;
  114. if (base<>R_NO) then
  115. begin
  116. if not(first) then
  117. s:=s+'+'
  118. else
  119. first:=false;
  120. s:=s+int_reg2str[base];
  121. end;
  122. if (index<>R_NO) then
  123. begin
  124. if not(first) then
  125. s:=s+'+'
  126. else
  127. first:=false;
  128. s:=s+int_reg2str[index];
  129. if scalefactor<>0 then
  130. s:=s+'*'+tostr(scalefactor);
  131. end;
  132. if offset<0 then
  133. s:=s+tostr(offset)
  134. else if (offset>0) then
  135. s:=s+'+'+tostr(offset);
  136. s:=s+']';
  137. end;
  138. getreferencestring:=s;
  139. end;
  140. function getopstr(t : byte;o : pointer;s : topsize; _operator: tasmop;dest : boolean) : string;
  141. var
  142. hs : string;
  143. begin
  144. case t of
  145. top_reg : getopstr:=int_nasmreg2str[tregister(o)];
  146. top_const,
  147. top_ref : begin
  148. if t=top_const then
  149. hs := tostr(longint(o))
  150. else
  151. hs:=getreferencestring(preference(o)^);
  152. if not ((_operator = A_LEA) or (_operator = A_LGS) or
  153. (_operator = A_LSS) or (_operator = A_LFS) or
  154. (_operator = A_LES) or (_operator = A_LDS) or
  155. (_operator = A_SHR) or (_operator = A_SHL) or
  156. (_operator = A_SAR) or (_operator = A_SAL) or
  157. (_operator = A_OUT) or (_operator = A_IN)) then
  158. begin
  159. case s of
  160. S_B : hs:='byte '+hs;
  161. S_W : hs:='word '+hs;
  162. S_L : hs:='dword '+hs;
  163. S_IS : hs:='word '+hs;
  164. S_IL : hs:='dword '+hs;
  165. S_IQ : hs:='qword '+hs;
  166. S_FS : hs:='dword '+hs;
  167. S_FL : hs:='qword '+hs;
  168. S_FX : hs:='tword '+hs;
  169. S_BW : if dest then
  170. hs:='word '+hs
  171. else
  172. hs:='byte '+hs;
  173. S_BL : if dest then
  174. hs:='dword '+hs
  175. else
  176. hs:='byte '+hs;
  177. S_WL : if dest then
  178. hs:='dword '+hs
  179. else
  180. hs:='word '+hs;
  181. end
  182. end;
  183. getopstr:=hs;
  184. end;
  185. top_symbol : begin
  186. hs[0]:=chr(strlen(pchar(pcsymbol(o)^.symbol)));
  187. move(pchar(pcsymbol(o)^.symbol)^,hs[1],byte(hs[0]));
  188. hs:='dword '+hs;
  189. if pcsymbol(o)^.offset>0 then
  190. hs:=hs+'+'+tostr(pcsymbol(o)^.offset)
  191. else
  192. if pcsymbol(o)^.offset<0 then
  193. hs:=hs+tostr(pcsymbol(o)^.offset);
  194. getopstr:=hs;
  195. end;
  196. else
  197. internalerror(10001);
  198. end;
  199. end;
  200. function getopstr_jmp(t : byte;o : pointer) : string;
  201. var
  202. hs : string;
  203. begin
  204. case t of
  205. top_reg : getopstr_jmp:=int_reg2str[tregister(o)];
  206. top_ref : getopstr_jmp:=getreferencestring(preference(o)^);
  207. top_const : getopstr_jmp:=tostr(longint(o));
  208. top_symbol : begin
  209. hs[0]:=chr(strlen(pchar(pcsymbol(o)^.symbol)));
  210. move(pchar(pcsymbol(o)^.symbol)^,hs[1],byte(hs[0]));
  211. if pcsymbol(o)^.offset>0 then
  212. hs:=hs+'+'+tostr(pcsymbol(o)^.offset)
  213. else
  214. if pcsymbol(o)^.offset<0 then
  215. hs:=hs+tostr(pcsymbol(o)^.offset);
  216. getopstr_jmp:=hs;
  217. end;
  218. else
  219. internalerror(10001);
  220. end;
  221. end;
  222. {****************************************************************************
  223. Ti386nasmasmlist
  224. ****************************************************************************}
  225. var
  226. LastSec : tsection;
  227. const
  228. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  229. (#9'DD'#9,'',#9'DW'#9,#9'DB'#9);
  230. ait_section2nasmstr : array[tsection] of string[6]=
  231. ('','.text','.data','.bss','.idata');
  232. Function PadTabs(p:pchar;addch:char):string;
  233. var
  234. s : string;
  235. i : longint;
  236. begin
  237. i:=strlen(p);
  238. if addch<>#0 then
  239. begin
  240. inc(i);
  241. s:=StrPas(p)+addch;
  242. end
  243. else
  244. s:=StrPas(p);
  245. if i<8 then
  246. PadTabs:=s+#9#9
  247. else
  248. PadTabs:=s+#9;
  249. end;
  250. procedure ti386nasmasmlist.WriteTree(p:paasmoutput);
  251. type
  252. twowords=record
  253. word1,word2:word;
  254. end;
  255. var
  256. s,
  257. prefix,
  258. suffix : string;
  259. hp : pai;
  260. counter,
  261. lines,
  262. i,j,l : longint;
  263. consttyp : tait;
  264. found,
  265. quoted : boolean;
  266. begin
  267. if not assigned(p) then
  268. exit;
  269. hp:=pai(p^.first);
  270. while assigned(hp) do
  271. begin
  272. case hp^.typ of
  273. ait_comment : Begin
  274. AsmWrite(target_asm.comment);
  275. AsmWritePChar(pai_asm_comment(hp)^.str);
  276. AsmLn;
  277. End;
  278. ait_regalloc,
  279. ait_regdealloc :;
  280. ait_section : begin
  281. if pai_section(hp)^.sec<>sec_none then
  282. begin
  283. AsmLn;
  284. AsmWriteLn('SECTION '+ait_section2nasmstr[pai_section(hp)^.sec]);
  285. end;
  286. LastSec:=pai_section(hp)^.sec;
  287. end;
  288. ait_align : AsmWriteLn(#9'ALIGN '+tostr(pai_align(hp)^.aligntype));
  289. ait_external : AsmWriteLn('EXTERN '+StrPas(pai_external(hp)^.name));
  290. ait_datablock : begin
  291. if pai_datablock(hp)^.is_global then
  292. AsmWriteLn(#9'GLOBAL '+StrPas(pai_datablock(hp)^.name));
  293. AsmWriteLn(PadTabs(pai_datablock(hp)^.name,':')+'RESB'#9+tostr(pai_datablock(hp)^.size));
  294. end;
  295. ait_const_32bit,
  296. ait_const_8bit,
  297. ait_const_16bit : begin
  298. AsmWrite(ait_const2str[hp^.typ]+tostr(pai_const(hp)^.value));
  299. consttyp:=hp^.typ;
  300. l:=0;
  301. repeat
  302. found:=(not (Pai(hp^.next)=nil)) and (Pai(hp^.next)^.typ=consttyp);
  303. if found then
  304. begin
  305. hp:=Pai(hp^.next);
  306. s:=','+tostr(pai_const(hp)^.value);
  307. AsmWrite(s);
  308. inc(l,length(s));
  309. end;
  310. until (not found) or (l>line_length);
  311. AsmLn;
  312. end;
  313. ait_const_symbol : begin
  314. AsmWrite(#9#9+'DD '#9);
  315. AsmWritePChar(pchar(pai_const(hp)^.value));
  316. AsmLn;
  317. end;
  318. ait_real_32bit : AsmWriteLn(#9#9'DD'#9+double2str(pai_single(hp)^.value));
  319. ait_real_64bit : AsmWriteLn(#9#9'DQ'#9+double2str(pai_double(hp)^.value));
  320. ait_real_extended : AsmWriteLn(#9#9'DT'#9+extended2str(pai_extended(hp)^.value));
  321. ait_comp : AsmWriteLn(#9#9'DQ'#9+comp2str(pai_extended(hp)^.value));
  322. ait_string : begin
  323. counter := 0;
  324. lines := pai_string(hp)^.len div line_length;
  325. { separate lines in different parts }
  326. if pai_string(hp)^.len > 0 then
  327. Begin
  328. for j := 0 to lines-1 do
  329. begin
  330. AsmWrite(#9#9'DB'#9);
  331. quoted:=false;
  332. for i:=counter to counter+line_length do
  333. begin
  334. { it is an ascii character. }
  335. if (ord(pai_string(hp)^.str[i])>31) and
  336. (ord(pai_string(hp)^.str[i])<128) and
  337. (pai_string(hp)^.str[i]<>'"') then
  338. begin
  339. if not(quoted) then
  340. begin
  341. if i>counter then
  342. AsmWrite(',');
  343. AsmWrite('"');
  344. end;
  345. AsmWrite(pai_string(hp)^.str[i]);
  346. quoted:=true;
  347. end { if > 31 and < 128 and ord('"') }
  348. else
  349. begin
  350. if quoted then
  351. AsmWrite('"');
  352. if i>counter then
  353. AsmWrite(',');
  354. quoted:=false;
  355. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  356. end;
  357. end; { end for i:=0 to... }
  358. if quoted then AsmWrite('"');
  359. AsmWrite(target_os.newline);
  360. counter := counter+line_length;
  361. end; { end for j:=0 ... }
  362. { do last line of lines }
  363. AsmWrite(#9#9'DB'#9);
  364. quoted:=false;
  365. for i:=counter to pai_string(hp)^.len-1 do
  366. begin
  367. { it is an ascii character. }
  368. if (ord(pai_string(hp)^.str[i])>31) and
  369. (ord(pai_string(hp)^.str[i])<128) and
  370. (pai_string(hp)^.str[i]<>'"') then
  371. begin
  372. if not(quoted) then
  373. begin
  374. if i>counter then
  375. AsmWrite(',');
  376. AsmWrite('"');
  377. end;
  378. AsmWrite(pai_string(hp)^.str[i]);
  379. quoted:=true;
  380. end { if > 31 and < 128 and " }
  381. else
  382. begin
  383. if quoted then
  384. AsmWrite('"');
  385. if i>counter then
  386. AsmWrite(',');
  387. quoted:=false;
  388. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  389. end;
  390. end; { end for i:=0 to... }
  391. if quoted then
  392. AsmWrite('"');
  393. end;
  394. AsmLn;
  395. end;
  396. ait_label : begin
  397. if pai_label(hp)^.l^.is_used then
  398. AsmWriteLn(lab2str(pai_label(hp)^.l)+':');
  399. end;
  400. ait_direct : begin
  401. AsmWritePChar(pai_direct(hp)^.str);
  402. AsmLn;
  403. end;
  404. ait_labeled_instruction :
  405. begin
  406. if not (pai_labeled(hp)^._operator in [A_JMP,A_LOOP,A_LOOPZ,A_LOOPE,
  407. A_LOOPNZ,A_LOOPNE,A_JCXZ,A_JECXZ]) then
  408. AsmWriteLn(#9#9+int_op2str[pai_labeled(hp)^._operator]+#9+'near '+lab2str(pai_labeled(hp)^.lab))
  409. else
  410. AsmWriteLn(#9#9+int_op2str[pai_labeled(hp)^._operator]+#9+lab2str(pai_labeled(hp)^.lab));
  411. end;
  412. ait_symbol : begin
  413. if pai_symbol(hp)^.is_global then
  414. AsmWriteLn(#9'GLOBAL '+StrPas(pai_symbol(hp)^.name));
  415. AsmWritePChar(pai_symbol(hp)^.name);
  416. if assigned(hp^.next) and not(pai(hp^.next)^.typ in
  417. [ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_const_symbol,
  418. ait_real_64bit,ait_string]) then
  419. AsmWriteLn(':')
  420. end;
  421. ait_instruction : begin
  422. suffix:='';
  423. prefix:= '';
  424. { added prefix instructions, must be on same line as opcode }
  425. if (pai386(hp)^.op1t = top_none) and
  426. ((pai386(hp)^._operator = A_REP) or
  427. (pai386(hp)^._operator = A_LOCK) or
  428. (pai386(hp)^._operator = A_REPE) or
  429. (pai386(hp)^._operator = A_REPNE)) then
  430. Begin
  431. prefix:=int_op2str[pai386(hp)^._operator]+#9;
  432. hp:=Pai(hp^.next);
  433. { this is theorically impossible... }
  434. if hp=nil then
  435. begin
  436. s:=#9#9+prefix;
  437. AsmWriteLn(s);
  438. break;
  439. end;
  440. { nasm prefers prefix on a line alone }
  441. AsmWriteln(#9#9+prefix);
  442. prefix:='';
  443. end
  444. else
  445. prefix:= '';
  446. { A_FNSTS need the w as suffix at least for nasm}
  447. if (pai386(hp)^._operator = A_FNSTS) then
  448. pai386(hp)^._operator:=A_FNSTSW
  449. else
  450. if (pai386(hp)^._operator = A_FSTS) then
  451. pai386(hp)^._operator:=A_FSTSW;
  452. if pai386(hp)^.op1t<>top_none then
  453. begin
  454. if pai386(hp)^._operator in [A_CALL] then
  455. s:=getopstr_jmp(pai386(hp)^.op1t,pai386(hp)^.op1)
  456. else
  457. begin
  458. s:=getopstr(pai386(hp)^.op1t,pai386(hp)^.op1,pai386(hp)^.size,pai386(hp)^._operator,false);
  459. if pai386(hp)^.op3t<>top_none then
  460. begin
  461. if pai386(hp)^.op2t<>top_none then
  462. s:=getopstr(pai386(hp)^.op2t,pointer(longint(twowords(pai386(hp)^.op2).word1)),
  463. pai386(hp)^.size,pai386(hp)^._operator,true)+','+s;
  464. s:=getopstr(pai386(hp)^.op3t,pointer(longint(twowords(pai386(hp)^.op2).word2)),
  465. pai386(hp)^.size,pai386(hp)^._operator,false)+','+s;
  466. end
  467. else
  468. if pai386(hp)^.op2t<>top_none then
  469. s:=getopstr(pai386(hp)^.op2t,pai386(hp)^.op2,pai386(hp)^.size,
  470. pai386(hp)^._operator,true)+','+s;
  471. end;
  472. s:=#9+s;
  473. end
  474. else
  475. begin
  476. { check if string instruction }
  477. { long form, otherwise may give range check errors }
  478. { in turbo pascal... }
  479. if ((pai386(hp)^._operator = A_CMPS) or
  480. (pai386(hp)^._operator = A_INS) or
  481. (pai386(hp)^._operator = A_OUTS) or
  482. (pai386(hp)^._operator = A_SCAS) or
  483. (pai386(hp)^._operator = A_STOS) or
  484. (pai386(hp)^._operator = A_MOVS) or
  485. (pai386(hp)^._operator = A_LODS) or
  486. (pai386(hp)^._operator = A_XLAT)) then
  487. Begin
  488. case pai386(hp)^.size of
  489. S_B: suffix:='b';
  490. S_W: suffix:='w';
  491. S_L: suffix:='d';
  492. else
  493. Message(assem_f_invalid_suffix_intel);
  494. end;
  495. end;
  496. s:='';
  497. end;
  498. if pai386(hp)^._operator=A_FWAIT then
  499. AsmWriteln(#9#9'DB'#9'09bh')
  500. else
  501. AsmWriteLn(#9#9+prefix+int_op2str[pai386(hp)^._operator]+suffix+s);
  502. end;
  503. {$ifdef GDB}
  504. ait_stabn,
  505. ait_stabs,
  506. ait_stab_function_name : ;
  507. {$endif GDB}
  508. ait_marker : ;
  509. else
  510. internalerror(10000);
  511. end;
  512. hp:=pai(hp^.next);
  513. end;
  514. end;
  515. procedure ti386nasmasmlist.WriteAsmList;
  516. begin
  517. {$ifdef EXTDEBUG}
  518. if assigned(current_module^.mainsource) then
  519. comment(v_info,'Start writing nasm-styled assembler output for '+current_module^.mainsource^);
  520. {$endif}
  521. LastSec:=sec_none;
  522. AsmWriteLn('BITS 32');
  523. AsmLn;
  524. countlabelref:=false;
  525. WriteTree(externals);
  526. { Nasm doesn't support stabs
  527. WriteTree(debuglist);}
  528. WriteTree(codesegment);
  529. WriteTree(datasegment);
  530. WriteTree(consts);
  531. WriteTree(rttilist);
  532. WriteTree(bsssegment);
  533. countlabelref:=true;
  534. AsmLn;
  535. {$ifdef EXTDEBUG}
  536. if assigned(current_module^.mainsource) then
  537. comment(v_info,'Done writing nasm-styled assembler output for '+current_module^.mainsource^);
  538. {$endif EXTDEBUG}
  539. end;
  540. end.
  541. {
  542. $Log$
  543. Revision 1.10 1998-10-06 17:16:34 pierre
  544. * some memory leaks fixed (thanks to Peter for heaptrc !)
  545. Revision 1.9 1998/10/01 20:19:07 jonas
  546. + ait_marker support
  547. Revision 1.8 1998/09/20 17:11:22 jonas
  548. * released REGALLOC
  549. Revision 1.7 1998/08/11 14:01:43 peter
  550. * fixed fwait bug using direct opcode
  551. Revision 1.6 1998/08/10 15:49:39 peter
  552. * small fixes for 0.99.5
  553. Revision 1.5 1998/08/08 10:19:18 florian
  554. * small fixes to write the extended type correct
  555. Revision 1.4 1998/06/05 17:46:03 peter
  556. * tp doesn't like comp() typecast
  557. Revision 1.3 1998/05/28 17:24:27 peter
  558. - $R- for tp to solve range errors with in[]
  559. Revision 1.2 1998/05/25 17:11:37 pierre
  560. * firstpasscount bug fixed
  561. now all is already set correctly the first time
  562. under EXTDEBUG try -gp to skip all other firstpasses
  563. it works !!
  564. * small bug fixes
  565. - for smallsets with -dTESTSMALLSET
  566. - some warnings removed (by correcting code !)
  567. Revision 1.1 1998/05/23 01:20:56 peter
  568. + aktasmmode, aktoptprocessor, aktoutputformat
  569. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  570. + $LIBNAME to set the library name where the unit will be put in
  571. * splitted cgi386 a bit (codeseg to large for bp7)
  572. * nasm, tasm works again. nasm moved to ag386nsm.pas
  573. }