ag386nsm.pas 21 KB

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