ag386nsm.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. {$ifdef TP}
  20. {$N+,E+}
  21. {$endif}
  22. unit ag386nsm;
  23. interface
  24. uses aasm,assemble;
  25. type
  26. pi386nasmasmlist=^ti386nasmasmlist;
  27. ti386nasmasmlist = object(tasmlist)
  28. procedure WriteTree(p:paasmoutput);virtual;
  29. procedure WriteAsmList;virtual;
  30. procedure WriteExternals;
  31. end;
  32. implementation
  33. uses
  34. strings,
  35. globtype,globals,systems,cobjects,
  36. files,verbose,cpubase,cpuasm
  37. {$ifdef GDB}
  38. ,gdb
  39. {$endif GDB}
  40. ;
  41. const
  42. line_length = 64;
  43. {$ifdef EXTTYPE}
  44. extstr : array[EXT_NEAR..EXT_ABS] of String[8] =
  45. ('NEAR','FAR','PROC','BYTE','WORD','DWORD',
  46. 'CODEPTR','DATAPTR','FWORD','PWORD','QWORD','TBYTE','ABS');
  47. {$endif}
  48. function single2str(d : single) : string;
  49. var
  50. hs : string;
  51. p : byte;
  52. begin
  53. str(d,hs);
  54. { nasm expects a lowercase e }
  55. p:=pos('E',hs);
  56. if p>0 then
  57. hs[p]:='e';
  58. p:=pos('+',hs);
  59. if p>0 then
  60. delete(hs,p,1);
  61. single2str:=lower(hs);
  62. end;
  63. function double2str(d : double) : string;
  64. var
  65. hs : string;
  66. p : byte;
  67. begin
  68. str(d,hs);
  69. { nasm expects a lowercase e }
  70. p:=pos('E',hs);
  71. if p>0 then
  72. hs[p]:='e';
  73. p:=pos('+',hs);
  74. if p>0 then
  75. delete(hs,p,1);
  76. double2str:=lower(hs);
  77. end;
  78. function extended2str(e : extended) : string;
  79. var
  80. hs : string;
  81. p : byte;
  82. begin
  83. str(e,hs);
  84. { nasm expects a lowercase e }
  85. p:=pos('E',hs);
  86. if p>0 then
  87. hs[p]:='e';
  88. p:=pos('+',hs);
  89. if p>0 then
  90. delete(hs,p,1);
  91. extended2str:=lower(hs);
  92. end;
  93. function comp2str(d : bestreal) : string;
  94. type
  95. pdouble = ^double;
  96. var
  97. c : comp;
  98. dd : pdouble;
  99. begin
  100. {$ifdef FPC}
  101. c:=comp(d);
  102. {$else}
  103. c:=d;
  104. {$endif}
  105. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  106. comp2str:=double2str(dd^);
  107. end;
  108. function getreferencestring(const ref : treference) : string;
  109. var
  110. s : string;
  111. first : boolean;
  112. begin
  113. if ref.is_immediate then
  114. begin
  115. getreferencestring:=tostr(ref.offset);
  116. exit;
  117. end
  118. else
  119. with ref do
  120. begin
  121. first:=true;
  122. if ref.segment<>R_NO then
  123. s:='['+int_reg2str[segment]+':'
  124. else
  125. s:='[';
  126. if assigned(symbol) then
  127. begin
  128. s:=s+symbol^.name;
  129. first:=false;
  130. end;
  131. if (base<>R_NO) then
  132. begin
  133. if not(first) then
  134. s:=s+'+'
  135. else
  136. first:=false;
  137. s:=s+int_reg2str[base];
  138. end;
  139. if (index<>R_NO) then
  140. begin
  141. if not(first) then
  142. s:=s+'+'
  143. else
  144. first:=false;
  145. s:=s+int_reg2str[index];
  146. if scalefactor<>0 then
  147. s:=s+'*'+tostr(scalefactor);
  148. end;
  149. if offset<0 then
  150. s:=s+tostr(offset)
  151. else if (offset>0) then
  152. s:=s+'+'+tostr(offset);
  153. s:=s+']';
  154. end;
  155. getreferencestring:=s;
  156. end;
  157. function sizestr(s:topsize;dest:boolean):string;
  158. begin
  159. case s of
  160. S_B : sizestr:='byte ';
  161. S_W : sizestr:='word ';
  162. S_L : sizestr:='dword ';
  163. S_IS : sizestr:='word ';
  164. S_IL : sizestr:='dword ';
  165. S_IQ : sizestr:='qword ';
  166. S_FS : sizestr:='dword ';
  167. S_FL : sizestr:='qword ';
  168. S_FX : sizestr:='tword ';
  169. S_BW : if dest then
  170. sizestr:='word '
  171. else
  172. sizestr:='byte ';
  173. S_BL : if dest then
  174. sizestr:='dword '
  175. else
  176. sizestr:='byte ';
  177. S_WL : if dest then
  178. sizestr:='dword '
  179. else
  180. sizestr:='word ';
  181. end;
  182. end;
  183. function getopstr(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean) : string;
  184. var
  185. hs : string;
  186. begin
  187. case o.typ of
  188. top_reg :
  189. getopstr:=int_nasmreg2str[o.reg];
  190. top_const :
  191. begin
  192. if (ops=1) and (opcode<>A_RET) then
  193. getopstr:=sizestr(s,dest)+tostr(o.val)
  194. else
  195. getopstr:=tostr(o.val);
  196. end;
  197. top_symbol :
  198. begin
  199. if assigned(o.sym) then
  200. hs:='dword '+o.sym^.name
  201. else
  202. hs:='dword ';
  203. if o.symofs>0 then
  204. hs:=hs+'+'+tostr(o.symofs)
  205. else
  206. if o.symofs<0 then
  207. hs:=hs+tostr(o.symofs)
  208. else
  209. if not(assigned(o.sym)) then
  210. hs:=hs+'0';
  211. getopstr:=hs;
  212. end;
  213. top_ref :
  214. begin
  215. hs:=getreferencestring(o.ref^);
  216. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  217. (opcode = A_LSS) or (opcode = A_LFS) or
  218. (opcode = A_LES) or (opcode = A_LDS) or
  219. (opcode = A_SHR) or (opcode = A_SHL) or
  220. (opcode = A_SAR) or (opcode = A_SAL) or
  221. (opcode = A_OUT) or (opcode = A_IN)) then
  222. begin
  223. hs:=sizestr(s,dest)+hs;
  224. end;
  225. getopstr:=hs;
  226. end;
  227. else
  228. internalerror(10001);
  229. end;
  230. end;
  231. function getopstr_jmp(const o:toper) : string;
  232. var
  233. hs : string;
  234. begin
  235. case o.typ of
  236. top_reg :
  237. getopstr_jmp:=int_nasmreg2str[o.reg];
  238. top_ref :
  239. getopstr_jmp:=getreferencestring(o.ref^);
  240. top_const :
  241. getopstr_jmp:=tostr(o.val);
  242. top_symbol :
  243. begin
  244. hs:=o.sym^.name;
  245. if o.symofs>0 then
  246. hs:=hs+'+'+tostr(o.symofs)
  247. else
  248. if o.symofs<0 then
  249. hs:=hs+tostr(o.symofs);
  250. getopstr_jmp:='NEAR '+hs;
  251. end;
  252. else
  253. internalerror(10001);
  254. end;
  255. end;
  256. {****************************************************************************
  257. Ti386nasmasmlist
  258. ****************************************************************************}
  259. var
  260. LastSec : tsection;
  261. const
  262. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  263. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  264. Function PadTabs(const p:string;addch:char):string;
  265. var
  266. s : string;
  267. i : longint;
  268. begin
  269. i:=length(p);
  270. if addch<>#0 then
  271. begin
  272. inc(i);
  273. s:=p+addch;
  274. end
  275. else
  276. s:=p;
  277. if i<8 then
  278. PadTabs:=s+#9#9
  279. else
  280. PadTabs:=s+#9;
  281. end;
  282. procedure ti386nasmasmlist.WriteTree(p:paasmoutput);
  283. const
  284. allocstr : array[boolean] of string[10]=(' released',' allocated');
  285. var
  286. s,
  287. prefix,
  288. suffix : string;
  289. hp : pai;
  290. counter,
  291. lines,
  292. i,j,l : longint;
  293. consttyp : tait;
  294. found,
  295. quoted : boolean;
  296. sep : char;
  297. begin
  298. if not assigned(p) then
  299. exit;
  300. hp:=pai(p^.first);
  301. while assigned(hp) do
  302. begin
  303. case hp^.typ of
  304. ait_comment :
  305. Begin
  306. AsmWrite(target_asm.comment);
  307. AsmWritePChar(pai_asm_comment(hp)^.str);
  308. AsmLn;
  309. End;
  310. ait_regalloc :
  311. begin
  312. if (cs_asm_regalloc in aktglobalswitches) then
  313. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[pairegalloc(hp)^.reg]+
  314. allocstr[pairegalloc(hp)^.allocation]);
  315. end;
  316. ait_tempalloc :
  317. begin
  318. if (cs_asm_tempalloc in aktglobalswitches) then
  319. AsmWriteLn(target_asm.comment+'Temp '+tostr(paitempalloc(hp)^.temppos)+','+
  320. tostr(paitempalloc(hp)^.tempsize)+allocstr[paitempalloc(hp)^.allocation]);
  321. end;
  322. ait_section :
  323. begin
  324. if pai_section(hp)^.sec<>sec_none then
  325. begin
  326. AsmLn;
  327. AsmWriteLn('SECTION '+target_asm.secnames[pai_section(hp)^.sec]);
  328. end;
  329. LastSec:=pai_section(hp)^.sec;
  330. end;
  331. ait_align :
  332. AsmWriteLn(#9'ALIGN '+tostr(pai_align(hp)^.aligntype));
  333. ait_datablock :
  334. begin
  335. if pai_datablock(hp)^.is_global then
  336. begin
  337. AsmWrite(#9'GLOBAL ');
  338. AsmWriteLn(pai_datablock(hp)^.sym^.name);
  339. end;
  340. AsmWrite(PadTabs(pai_datablock(hp)^.sym^.name,':'));
  341. AsmWriteLn('RESB'#9+tostr(pai_datablock(hp)^.size));
  342. end;
  343. ait_const_32bit,
  344. ait_const_16bit,
  345. ait_const_8bit :
  346. begin
  347. AsmWrite(ait_const2str[hp^.typ]+tostr(pai_const(hp)^.value));
  348. consttyp:=hp^.typ;
  349. l:=0;
  350. repeat
  351. found:=(not (Pai(hp^.next)=nil)) and (Pai(hp^.next)^.typ=consttyp);
  352. if found then
  353. begin
  354. hp:=Pai(hp^.next);
  355. s:=','+tostr(pai_const(hp)^.value);
  356. AsmWrite(s);
  357. inc(l,length(s));
  358. end;
  359. until (not found) or (l>line_length);
  360. AsmLn;
  361. end;
  362. ait_const_symbol :
  363. begin
  364. AsmWrite(#9#9'DD'#9);
  365. AsmWrite(pai_const_symbol(hp)^.sym^.name);
  366. if pai_const_symbol(hp)^.offset>0 then
  367. AsmWrite('+'+tostr(pai_const_symbol(hp)^.offset))
  368. else if pai_const_symbol(hp)^.offset<0 then
  369. AsmWrite(tostr(pai_const_symbol(hp)^.offset));
  370. AsmLn;
  371. end;
  372. ait_const_rva :
  373. begin
  374. AsmWrite(#9#9'RVA'#9);
  375. AsmWriteLn(pai_const_symbol(hp)^.sym^.name);
  376. end;
  377. ait_real_32bit :
  378. AsmWriteLn(#9#9'DD'#9+single2str(pai_real_32bit(hp)^.value));
  379. ait_real_64bit :
  380. AsmWriteLn(#9#9'DQ'#9+double2str(pai_real_64bit(hp)^.value));
  381. ait_real_80bit :
  382. AsmWriteLn(#9#9'DT'#9+extended2str(pai_real_80bit(hp)^.value));
  383. ait_comp_64bit :
  384. AsmWriteLn(#9#9'DQ'#9+comp2str(pai_real_80bit(hp)^.value));
  385. ait_string :
  386. begin
  387. counter := 0;
  388. lines := pai_string(hp)^.len div line_length;
  389. { separate lines in different parts }
  390. if pai_string(hp)^.len > 0 then
  391. Begin
  392. for j := 0 to lines-1 do
  393. begin
  394. AsmWrite(#9#9'DB'#9);
  395. quoted:=false;
  396. for i:=counter to counter+line_length-1 do
  397. begin
  398. { it is an ascii character. }
  399. if (ord(pai_string(hp)^.str[i])>31) and
  400. (ord(pai_string(hp)^.str[i])<128) and
  401. (pai_string(hp)^.str[i]<>'"') then
  402. begin
  403. if not(quoted) then
  404. begin
  405. if i>counter then
  406. AsmWrite(',');
  407. AsmWrite('"');
  408. end;
  409. AsmWrite(pai_string(hp)^.str[i]);
  410. quoted:=true;
  411. end { if > 31 and < 128 and ord('"') }
  412. else
  413. begin
  414. if quoted then
  415. AsmWrite('"');
  416. if i>counter then
  417. AsmWrite(',');
  418. quoted:=false;
  419. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  420. end;
  421. end; { end for i:=0 to... }
  422. if quoted then AsmWrite('"');
  423. AsmWrite(target_os.newline);
  424. inc(counter,line_length);
  425. end; { end for j:=0 ... }
  426. { do last line of lines }
  427. AsmWrite(#9#9'DB'#9);
  428. quoted:=false;
  429. for i:=counter to pai_string(hp)^.len-1 do
  430. begin
  431. { it is an ascii character. }
  432. if (ord(pai_string(hp)^.str[i])>31) and
  433. (ord(pai_string(hp)^.str[i])<128) and
  434. (pai_string(hp)^.str[i]<>'"') then
  435. begin
  436. if not(quoted) then
  437. begin
  438. if i>counter then
  439. AsmWrite(',');
  440. AsmWrite('"');
  441. end;
  442. AsmWrite(pai_string(hp)^.str[i]);
  443. quoted:=true;
  444. end { if > 31 and < 128 and " }
  445. else
  446. begin
  447. if quoted then
  448. AsmWrite('"');
  449. if i>counter then
  450. AsmWrite(',');
  451. quoted:=false;
  452. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  453. end;
  454. end; { end for i:=0 to... }
  455. if quoted then
  456. AsmWrite('"');
  457. end;
  458. AsmLn;
  459. end;
  460. ait_label :
  461. begin
  462. if pai_label(hp)^.l^.is_used then
  463. AsmWriteLn(pai_label(hp)^.l^.name+':');
  464. end;
  465. ait_direct :
  466. begin
  467. AsmWritePChar(pai_direct(hp)^.str);
  468. AsmLn;
  469. end;
  470. ait_symbol :
  471. begin
  472. if pai_symbol(hp)^.is_global then
  473. begin
  474. AsmWrite(#9'GLOBAL ');
  475. AsmWriteLn(pai_symbol(hp)^.sym^.name);
  476. end;
  477. AsmWrite(pai_symbol(hp)^.sym^.name);
  478. if assigned(hp^.next) and not(pai(hp^.next)^.typ in
  479. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  480. ait_const_symbol,ait_const_rva,
  481. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  482. AsmWriteLn(':')
  483. end;
  484. ait_symbol_end :
  485. begin
  486. end;
  487. ait_instruction :
  488. begin
  489. { We need intel order, no At&t }
  490. paicpu(hp)^.SwapOperands;
  491. { Reset }
  492. suffix:='';
  493. prefix:='';
  494. s:='';
  495. if paicpu(hp)^.ops<>0 then
  496. begin
  497. if is_calljmp(paicpu(hp)^.opcode) then
  498. s:=#9+getopstr_jmp(paicpu(hp)^.oper[0])
  499. else
  500. begin
  501. for i:=0to paicpu(hp)^.ops-1 do
  502. begin
  503. if i=0 then
  504. sep:=#9
  505. else
  506. sep:=',';
  507. s:=s+sep+getopstr(paicpu(hp)^.oper[i],paicpu(hp)^.opsize,paicpu(hp)^.opcode,
  508. paicpu(hp)^.ops,(i=2));
  509. end;
  510. end;
  511. end;
  512. if paicpu(hp)^.opcode=A_FWAIT then
  513. AsmWriteln(#9#9'DB'#9'09bh')
  514. else
  515. AsmWriteLn(#9#9+prefix+int_op2str[paicpu(hp)^.opcode]+
  516. cond2str[paicpu(hp)^.condition]+suffix+s);
  517. end;
  518. {$ifdef GDB}
  519. ait_stabn,
  520. ait_stabs,
  521. ait_force_line,
  522. ait_stab_function_name : ;
  523. {$endif GDB}
  524. ait_cut :
  525. begin
  526. { only reset buffer if nothing has changed }
  527. if AsmSize=AsmStartSize then
  528. AsmClear
  529. else
  530. begin
  531. AsmClose;
  532. DoAssemble;
  533. AsmCreate(pai_cut(hp)^.place);
  534. end;
  535. { avoid empty files }
  536. while assigned(hp^.next) and (pai(hp^.next)^.typ in [ait_cut,ait_section,ait_comment]) do
  537. begin
  538. if pai(hp^.next)^.typ=ait_section then
  539. lastsec:=pai_section(hp^.next)^.sec;
  540. hp:=pai(hp^.next);
  541. end;
  542. if lastsec<>sec_none then
  543. AsmWriteLn('SECTION '+target_asm.secnames[lastsec]);
  544. AsmStartSize:=AsmSize;
  545. end;
  546. ait_marker : ;
  547. else
  548. internalerror(10000);
  549. end;
  550. hp:=pai(hp^.next);
  551. end;
  552. end;
  553. var
  554. currentasmlist : PAsmList;
  555. procedure writeexternal(p:pnamedindexobject);{$ifndef FPC}far;{$endif}
  556. begin
  557. if pasmsymbol(p)^.typ=AS_EXTERNAL then
  558. currentasmlist^.AsmWriteln('EXTERN'#9+p^.name);
  559. end;
  560. procedure ti386nasmasmlist.WriteExternals;
  561. begin
  562. currentasmlist:=@self;
  563. AsmSymbolList^.foreach({$ifndef TP}@{$endif}writeexternal);
  564. end;
  565. procedure ti386nasmasmlist.WriteAsmList;
  566. begin
  567. {$ifdef EXTDEBUG}
  568. if assigned(current_module^.mainsource) then
  569. comment(v_info,'Start writing nasm-styled assembler output for '+current_module^.mainsource^);
  570. {$endif}
  571. LastSec:=sec_none;
  572. AsmWriteLn('BITS 32');
  573. AsmLn;
  574. countlabelref:=false;
  575. WriteExternals;
  576. { Nasm doesn't support stabs
  577. WriteTree(debuglist);}
  578. WriteTree(codesegment);
  579. WriteTree(datasegment);
  580. WriteTree(consts);
  581. WriteTree(rttilist);
  582. WriteTree(resourcestringlist);
  583. WriteTree(bsssegment);
  584. countlabelref:=true;
  585. AsmLn;
  586. {$ifdef EXTDEBUG}
  587. if assigned(current_module^.mainsource) then
  588. comment(v_info,'Done writing nasm-styled assembler output for '+current_module^.mainsource^);
  589. {$endif EXTDEBUG}
  590. end;
  591. end.
  592. {
  593. $Log$
  594. Revision 1.56 2000-02-09 13:22:43 peter
  595. * log truncated
  596. Revision 1.55 2000/01/07 01:14:18 peter
  597. * updated copyright to 2000
  598. Revision 1.54 1999/11/06 14:34:16 peter
  599. * truncated log to 20 revs
  600. Revision 1.53 1999/11/02 15:06:56 peter
  601. * import library fixes for win32
  602. * alignment works again
  603. Revision 1.52 1999/09/13 16:27:24 peter
  604. * fix for jmps to be always near
  605. * string writing fixed
  606. Revision 1.51 1999/09/10 15:41:18 peter
  607. * added symbol_end
  608. Revision 1.50 1999/09/02 18:47:43 daniel
  609. * Could not compile with TP, some arrays moved to heap
  610. * NOAG386BIN default for TP
  611. * AG386* files were not compatible with TP, fixed.
  612. Revision 1.49 1999/08/25 11:59:38 jonas
  613. * changed pai386, paippc and paiapha (same for tai*) to paicpu (taicpu)
  614. Revision 1.48 1999/08/04 00:22:37 florian
  615. * renamed i386asm and i386base to cpuasm and cpubase
  616. Revision 1.47 1999/08/01 18:28:10 florian
  617. * modifications for the new code generator
  618. Revision 1.46 1999/07/22 09:37:33 florian
  619. + resourcestring implemented
  620. + start of longstring support
  621. }