ag386nsm.pas 22 KB

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