ag386nsm.pas 21 KB

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