agatt.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. This unit implements an abstract assembler write for GNU AT&T
  5. assebler syntax
  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 agatt;
  20. interface
  21. uses
  22. globals,systems,{errors,}cobjects,aasm,strings,files,assemble,cpuasm
  23. {$ifdef GDB}
  24. ,gdb
  25. {$endif GDB}
  26. ,cpubase,globtype
  27. ;
  28. type
  29. pattasmlist=^tattasmlist;
  30. tattasmlist=object(tasmlist)
  31. { convert an operand to a string }
  32. function getopstr(const o:toper) : string;
  33. { convert a treference to a string }
  34. function getreferencestring(var ref : treference) : string; Virtual;
  35. { convert a call/jmp operand to a string }
  36. function getopstr_jmp(const o:toper) : string; Virtual;
  37. { write an ait_instruction }
  38. Procedure WriteInstruction (P : paicpu); virtual;
  39. procedure WriteTree(p:paasmoutput);virtual;
  40. procedure WriteAsmList;virtual;
  41. {$ifdef GDB}
  42. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  43. {$endif}
  44. end;
  45. implementation
  46. function tattasmlist.getreferencestring(var ref : treference) : string;
  47. begin
  48. Abstract
  49. End;
  50. function tattasmlist.getopstr_jmp(const o:toper) : string;
  51. Begin
  52. Abstract
  53. end;
  54. Procedure tattasmlist.WriteInstruction (P : paicpu);
  55. Begin
  56. Abstract
  57. End;
  58. function tattasmlist.getopstr(const o:toper) : string;
  59. var
  60. hs : string;
  61. begin
  62. case o.typ of
  63. top_reg :
  64. getopstr:=att_reg2str[o.reg];
  65. top_ref :
  66. getopstr:=getreferencestring(o.ref^);
  67. top_const :
  68. getopstr:='$'+tostr(o.val);
  69. top_symbol :
  70. begin
  71. if assigned(o.sym) then
  72. hs:='$'+o.sym^.name
  73. else
  74. hs:='$';
  75. if o.symofs>0 then
  76. hs:=hs+'+'+tostr(o.symofs)
  77. else
  78. if o.symofs<0 then
  79. hs:=hs+tostr(o.symofs)
  80. else
  81. if not(assigned(o.sym)) then
  82. hs:=hs+'0';
  83. getopstr:=hs;
  84. end;
  85. else
  86. {$ifndef testing}
  87. internalerror(10001);
  88. {$else testing}
  89. begin
  90. writeln('internalerror 10001');
  91. halt(1);
  92. end;
  93. {$endif testing}
  94. end;
  95. end;
  96. {$ifdef GDB}
  97. procedure tattasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  98. var
  99. curr_n : byte;
  100. begin
  101. if not (cs_debuginfo in aktmoduleswitches) then
  102. exit;
  103. { file changed ? (must be before line info) }
  104. if (fileinfo.fileindex<>0) and
  105. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  106. begin
  107. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  108. if includecount=0 then
  109. curr_n:=n_sourcefile
  110. else
  111. curr_n:=n_includefile;
  112. if (infile^.path^<>'') then
  113. begin
  114. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+
  115. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  116. end;
  117. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile^.name^))+'",'+
  118. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  119. AsmWriteLn('Ltext'+ToStr(IncludeCount)+':');
  120. inc(includecount);
  121. end;
  122. { line changed ? }
  123. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  124. begin
  125. if (n_line=n_textline) and assigned(funcname) and
  126. (target_os.use_function_relative_addresses) then
  127. begin
  128. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  129. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  130. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  131. AsmWritePChar(FuncName);
  132. AsmLn;
  133. inc(linecount);
  134. end
  135. else
  136. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  137. end;
  138. stabslastfileinfo:=fileinfo;
  139. end;
  140. {$endif GDB}
  141. procedure tattasmlist.WriteTree(p:paasmoutput);
  142. const
  143. allocstr : array[boolean] of string[10]=(' released',' allocated');
  144. type
  145. t64bitarray = array[0..7] of byte;
  146. t32bitarray = array[0..3] of byte;
  147. var
  148. ch : char;
  149. hp : pai;
  150. consttyp : tait;
  151. s : string;
  152. found : boolean;
  153. i,pos,l : longint;
  154. co : comp;
  155. sin : single;
  156. d : double;
  157. e : extended;
  158. op : tasmop;
  159. calljmp,
  160. do_line : boolean;
  161. sep : char;
  162. begin
  163. if not assigned(p) then
  164. exit;
  165. do_line:=(cs_debuginfo in aktmoduleswitches) or (cs_asm_source in aktglobalswitches);
  166. hp:=pai(p^.first);
  167. while assigned(hp) do
  168. begin
  169. aktfilepos:=hp^.fileinfo;
  170. if do_line then
  171. begin
  172. { I think it is better to write stabs before source line PM }
  173. {$ifdef GDB}
  174. { write stabs }
  175. if cs_debuginfo in aktmoduleswitches then
  176. begin
  177. if not (hp^.typ in [
  178. ait_label,
  179. ait_regalloc,ait_tempalloc,
  180. ait_stabn,ait_stabs,ait_section,
  181. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  182. begin
  183. WriteFileLineInfo(hp^.fileinfo);
  184. end;
  185. end;
  186. {$endif GDB}
  187. { load infile }
  188. if lastfileinfo.fileindex<>hp^.fileinfo.fileindex then
  189. begin
  190. infile:=current_module^.sourcefiles^.get_file(hp^.fileinfo.fileindex);
  191. { open only if needed !! }
  192. if (cs_asm_source in aktglobalswitches) then
  193. infile^.open;
  194. { avoid unnecessary reopens of the same file !! }
  195. lastfileinfo.fileindex:=hp^.fileinfo.fileindex;
  196. { be sure to change line !! }
  197. lastfileinfo.line:=-1;
  198. end;
  199. { write source }
  200. if (cs_asm_source in aktglobalswitches) and
  201. not (hp^.typ in [
  202. ait_label,
  203. ait_stabn,ait_stabs,ait_section,
  204. ait_cut,ait_align,ait_stab_function_name]) then
  205. begin
  206. if (infile<>lastinfile) and assigned(lastinfile) then
  207. begin
  208. AsmWriteLn(target_asm.comment+'['+infile^.name^+']');
  209. lastinfile^.close;
  210. end;
  211. if (hp^.fileinfo.line<>lastfileinfo.line) and
  212. (hp^.fileinfo.line<infile^.maxlinebuf) then
  213. begin
  214. if (hp^.fileinfo.line<>0) and
  215. (infile^.linebuf^[hp^.fileinfo.line]>=0) then
  216. AsmWriteLn(target_asm.comment+'['+tostr(hp^.fileinfo.line)+'] '+
  217. fixline(infile^.GetLineStr(hp^.fileinfo.line)));
  218. { set it to a negative value !
  219. to make that is has been read already !! PM }
  220. infile^.linebuf^[hp^.fileinfo.line]:=-infile^.linebuf^[hp^.fileinfo.line]-1;
  221. end;
  222. lastfileinfo:=hp^.fileinfo;
  223. lastinfile:=infile;
  224. end;
  225. end;
  226. case hp^.typ of
  227. ait_comment :
  228. Begin
  229. AsmWrite(target_asm.comment);
  230. AsmWritePChar(pai_asm_comment(hp)^.str);
  231. AsmLn;
  232. End;
  233. ait_regalloc :
  234. begin
  235. if (cs_asm_regalloc in aktglobalswitches) then
  236. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[pairegalloc(hp)^.reg]+
  237. allocstr[pairegalloc(hp)^.allocation]);
  238. end;
  239. ait_tempalloc :
  240. begin
  241. if (cs_asm_tempalloc in aktglobalswitches) then
  242. AsmWriteLn(target_asm.comment+'Temp '+tostr(paitempalloc(hp)^.temppos)+','+
  243. tostr(paitempalloc(hp)^.tempsize)+allocstr[paitempalloc(hp)^.allocation]);
  244. end;
  245. ait_align :
  246. begin
  247. AsmWrite(#9'.balign '+tostr(pai_align(hp)^.aligntype));
  248. if pai_align(hp)^.use_op then
  249. AsmWrite(','+tostr(pai_align(hp)^.fillop));
  250. AsmLn;
  251. end;
  252. ait_section :
  253. begin
  254. if pai_section(hp)^.sec<>sec_none then
  255. begin
  256. AsmLn;
  257. AsmWriteLn(ait_section2str(pai_section(hp)^.sec));
  258. {$ifdef GDB}
  259. lastfileinfo.line:=-1;
  260. {$endif GDB}
  261. end;
  262. end;
  263. ait_datablock :
  264. begin
  265. if pai_datablock(hp)^.is_global then
  266. AsmWrite(#9'.comm'#9)
  267. else
  268. AsmWrite(#9'.lcomm'#9);
  269. AsmWrite(pai_datablock(hp)^.sym^.name);
  270. AsmWriteLn(','+tostr(pai_datablock(hp)^.size));
  271. end;
  272. ait_const_32bit,
  273. ait_const_16bit,
  274. ait_const_8bit :
  275. begin
  276. AsmWrite(ait_const2str[hp^.typ]+tostr(pai_const(hp)^.value));
  277. consttyp:=hp^.typ;
  278. l:=0;
  279. repeat
  280. found:=(not (Pai(hp^.next)=nil)) and (Pai(hp^.next)^.typ=consttyp);
  281. if found then
  282. begin
  283. hp:=Pai(hp^.next);
  284. s:=','+tostr(pai_const(hp)^.value);
  285. AsmWrite(s);
  286. inc(l,length(s));
  287. end;
  288. until (not found) or (l>line_length);
  289. AsmLn;
  290. end;
  291. ait_const_symbol :
  292. begin
  293. AsmWrite(#9'.long'#9+pai_const_symbol(hp)^.sym^.name);
  294. if pai_const_symbol(hp)^.offset>0 then
  295. AsmWrite('+'+tostr(pai_const_symbol(hp)^.offset))
  296. else if pai_const_symbol(hp)^.offset<0 then
  297. AsmWrite(tostr(pai_const_symbol(hp)^.offset));
  298. AsmLn;
  299. end;
  300. ait_const_rva :
  301. AsmWriteLn(#9'.rva'#9+pai_const_symbol(hp)^.sym^.name);
  302. ait_real_64bit :
  303. begin
  304. if do_line then
  305. AsmWriteLn(target_asm.comment+double2str(pai_real_64bit(hp)^.value));
  306. d:=pai_real_64bit(hp)^.value;
  307. AsmWrite(#9'.byte'#9);
  308. for i:=0 to 7 do
  309. begin
  310. if i<>0 then
  311. AsmWrite(',');
  312. AsmWrite(tostr(t64bitarray(d)[i]));
  313. end;
  314. AsmLn;
  315. end;
  316. ait_real_32bit :
  317. begin
  318. if do_line then
  319. AsmWriteLn(target_asm.comment+single2str(pai_real_32bit(hp)^.value));
  320. sin:=pai_real_32bit(hp)^.value;
  321. AsmWrite(#9'.byte'#9);
  322. for i:=0 to 3 do
  323. begin
  324. if i<>0 then
  325. AsmWrite(',');
  326. AsmWrite(tostr(t32bitarray(sin)[i]));
  327. end;
  328. AsmLn;
  329. end;
  330. ait_comp_64bit :
  331. begin
  332. if do_line then
  333. AsmWriteLn(target_asm.comment+comp2str(pai_comp_64bit(hp)^.value));
  334. AsmWrite(#9'.byte'#9);
  335. {$ifdef FPC}
  336. co:=comp(pai_comp_64bit(hp)^.value);
  337. {$else}
  338. co:=pai_comp_64bit(hp)^.value;
  339. {$endif}
  340. for i:=0 to 7 do
  341. begin
  342. if i<>0 then
  343. AsmWrite(',');
  344. AsmWrite(tostr(t64bitarray(co)[i]));
  345. end;
  346. AsmLn;
  347. end;
  348. ait_direct :
  349. begin
  350. AsmWritePChar(pai_direct(hp)^.str);
  351. AsmLn;
  352. {$IfDef GDB}
  353. if strpos(pai_direct(hp)^.str,'.data')<>nil then
  354. n_line:=n_dataline
  355. else if strpos(pai_direct(hp)^.str,'.text')<>nil then
  356. n_line:=n_textline
  357. else if strpos(pai_direct(hp)^.str,'.bss')<>nil then
  358. n_line:=n_bssline;
  359. {$endif GDB}
  360. end;
  361. ait_string :
  362. begin
  363. pos:=0;
  364. for i:=1 to pai_string(hp)^.len do
  365. begin
  366. if pos=0 then
  367. begin
  368. AsmWrite(#9'.ascii'#9'"');
  369. pos:=20;
  370. end;
  371. ch:=pai_string(hp)^.str[i-1];
  372. case ch of
  373. #0, {This can't be done by range, because a bug in FPC}
  374. #1..#31,
  375. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  376. '"' : s:='\"';
  377. '\' : s:='\\';
  378. else
  379. s:=ch;
  380. end;
  381. AsmWrite(s);
  382. inc(pos,length(s));
  383. if (pos>line_length) or (i=pai_string(hp)^.len) then
  384. begin
  385. AsmWriteLn('"');
  386. pos:=0;
  387. end;
  388. end;
  389. end;
  390. ait_label :
  391. begin
  392. if (pai_label(hp)^.l^.is_used) then
  393. begin
  394. if pai_label(hp)^.l^.typ=AS_GLOBAL then
  395. AsmWriteLn('.globl'#9+pai_label(hp)^.l^.name);
  396. AsmWriteLn(pai_label(hp)^.l^.name+':');
  397. end;
  398. end;
  399. ait_symbol :
  400. begin
  401. if pai_symbol(hp)^.is_global then
  402. AsmWriteLn('.globl'#9+pai_symbol(hp)^.sym^.name);
  403. if target_info.target=target_i386_linux then
  404. begin
  405. AsmWrite(#9'.type'#9+pai_symbol(hp)^.sym^.name);
  406. if assigned(pai(hp^.next)) and
  407. (pai(hp^.next)^.typ in [ait_const_symbol,ait_const_rva,
  408. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  409. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  410. AsmWriteLn(',@object')
  411. else
  412. AsmWriteLn(',@function');
  413. if pai_symbol(hp)^.sym^.size>0 then
  414. AsmWriteLn(#9'.size'#9+pai_symbol(hp)^.sym^.name+', '+tostr(pai_symbol(hp)^.sym^.size));
  415. end;
  416. AsmWriteLn(pai_symbol(hp)^.sym^.name+':');
  417. end;
  418. ait_symbol_end :
  419. begin
  420. if target_info.target=target_i386_linux then
  421. begin
  422. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  423. inc(symendcount);
  424. AsmWriteLn(s+':');
  425. AsmWriteLn(#9'.size'#9+pai_symbol(hp)^.sym^.name+', '+s+' - '+pai_symbol(hp)^.sym^.name);
  426. end;
  427. end;
  428. ait_instruction : WriteInstruction(Hp);
  429. {$ifdef GDB}
  430. ait_stabs :
  431. begin
  432. AsmWrite(#9'.stabs ');
  433. AsmWritePChar(pai_stabs(hp)^.str);
  434. AsmLn;
  435. end;
  436. ait_stabn :
  437. begin
  438. AsmWrite(#9'.stabn ');
  439. AsmWritePChar(pai_stabn(hp)^.str);
  440. AsmLn;
  441. end;
  442. ait_force_line :
  443. stabslastfileinfo.line:=0;
  444. ait_stab_function_name:
  445. funcname:=pai_stab_function_name(hp)^.str;
  446. {$endif GDB}
  447. ait_cut :
  448. begin
  449. if SmartAsm then
  450. begin
  451. { only reset buffer if nothing has changed }
  452. if AsmSize=AsmStartSize then
  453. AsmClear
  454. else
  455. begin
  456. AsmClose;
  457. DoAssemble;
  458. if pai_cut(hp)^.EndName then
  459. IsEndFile:=true;
  460. AsmCreate;
  461. end;
  462. { avoid empty files }
  463. while assigned(hp^.next) and (pai(hp^.next)^.typ in [ait_cut,ait_section,ait_comment]) do
  464. begin
  465. if pai(hp^.next)^.typ=ait_section then
  466. lastsec:=pai_section(hp^.next)^.sec;
  467. hp:=pai(hp^.next);
  468. end;
  469. {$ifdef GDB}
  470. { force write of filename }
  471. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  472. includecount:=0;
  473. funcname:=nil;
  474. WriteFileLineInfo(hp^.fileinfo);
  475. {$endif GDB}
  476. if lastsec<>sec_none then
  477. AsmWriteLn(ait_section2str(lastsec));
  478. AsmStartSize:=AsmSize;
  479. end;
  480. end;
  481. ait_marker :
  482. ;
  483. else
  484. {$ifndef testing}
  485. internalerror(10000);
  486. {$else testing}
  487. begin
  488. writeln('internalerror 10001');
  489. halt(1);
  490. end;
  491. {$endif testing}
  492. end;
  493. hp:=pai(hp^.next);
  494. end;
  495. end;
  496. procedure tattasmlist.WriteAsmList;
  497. var
  498. p:dirstr;
  499. n:namestr;
  500. e:extstr;
  501. {$ifdef GDB}
  502. fileinfo : tfileposinfo;
  503. {$endif GDB}
  504. begin
  505. {$ifdef EXTDEBUG}
  506. if assigned(current_module^.mainsource) then
  507. Comment(v_info,'Start writing att-styled assembler output for '+current_module^.mainsource^);
  508. {$endif}
  509. LastSec:=sec_none;
  510. {$ifdef GDB}
  511. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  512. {$endif GDB}
  513. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  514. LastInfile:=nil;
  515. if assigned(current_module^.mainsource) then
  516. fsplit(current_module^.mainsource^,p,n,e)
  517. else
  518. begin
  519. p:=inputdir;
  520. n:=inputfile;
  521. e:=inputextension;
  522. end;
  523. { to get symify to work }
  524. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  525. {$ifdef GDB}
  526. n_line:=n_bssline;
  527. funcname:=nil;
  528. linecount:=1;
  529. includecount:=0;
  530. fileinfo.fileindex:=1;
  531. fileinfo.line:=1;
  532. { Write main file }
  533. WriteFileLineInfo(fileinfo);
  534. {$endif GDB}
  535. AsmStartSize:=AsmSize;
  536. symendcount:=0;
  537. countlabelref:=false;
  538. If (cs_debuginfo in aktmoduleswitches) then
  539. WriteTree(debuglist);
  540. WriteTree(codesegment);
  541. WriteTree(datasegment);
  542. WriteTree(consts);
  543. WriteTree(rttilist);
  544. Writetree(resourcestringlist);
  545. WriteTree(bsssegment);
  546. Writetree(importssection);
  547. Writetree(exportssection);
  548. Writetree(resourcesection);
  549. countlabelref:=true;
  550. AsmLn;
  551. {$ifdef EXTDEBUG}
  552. if assigned(current_module^.mainsource) then
  553. comment(v_info,'Done writing att-styled assembler output for '+current_module^.mainsource^);
  554. {$endif EXTDEBUG}
  555. end;
  556. Procedure tattasmlist.WriteInstruction (P : Pai); virtual;
  557. begin
  558. RunError(255);
  559. end;
  560. end.
  561. {
  562. $Log$
  563. Revision 1.2 2002-09-07 15:25:14 peter
  564. * old logs removed and tabs fixed
  565. }