agatt.pas 19 KB

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