ag386nsm.pas 21 KB

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