agatt.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. {
  2. $Id$
  3. Copyright (c) 1997 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. Procedure WriteInstruction (P : Pai); virtual;
  32. procedure WriteTree(p:paasmoutput);virtual;
  33. procedure WriteAsmList;virtual;
  34. {$ifdef GDB}
  35. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  36. {$endif}
  37. end;
  38. implementation
  39. const
  40. op2str : array[tasmop] of string[14] = (
  41. 'addf','addg','addl','addq',
  42. 'adds','addt','amask','and','beq','bge',
  43. 'bgt','bic','bis','blbc','blbs','ble',
  44. 'blt','bne','br','bsr','call_pal','cmoveq',
  45. 'cmovge','cmovgt','cmovlbc','cmovlbs','cmovle','cmovlt',
  46. 'cmovne','cmpbge','cmpeq','cmpgeq','cmpgle','cmpglt',
  47. 'cmple','cmplt','cmpteq','cmptle','cmptlt','cmptun',
  48. 'cmpule','cmpult','cpys','cpyse','cpysn','ctlz',
  49. 'ctpop','cttz','cvtdg','cvtgd','cvtgf','cvtgq',
  50. 'cvtlq','cvtqf','cvtqg','cvtql','cvtqs','cvtqt',
  51. 'cvtst','cvttq','cvtts','divf','divg','divs',
  52. 'divt','ecb','eqv','excb','extbl','extlh',
  53. 'extll','extqh','extql','extwh','extwl','fbeq',
  54. 'fbge','fbgt','fble','fblt','fbne','fcmoveq',
  55. 'fcmovge','fcmovgt','fcmovle','fcmovlt','fcmovne','fetch',
  56. 'fetch_m','ftois','ftoit','implver','insbl','inslh',
  57. 'insll','insqh','insql','inswh','inswl','itoff',
  58. 'itofs','itoft','jmp','jsr','jsr_coroutine','lda',
  59. 'ldah','ldbu','ldwu','ldf','ldg','ldl',
  60. 'ldl_l','ldq','ldq_l','ldq_u','lds','ldt',
  61. 'maxsb8','maxsw4','maxub8','maxuw4','mb','mf_fpcr',
  62. 'minsb8','minsw4','minub8','minuw4','mskbl','msklh',
  63. 'mskll','mskqh','mskql','mskwh','mskwl','mt_fpcr',
  64. 'mulf','mulg','mull','mulq',
  65. 'muls','mult','ornot','perr','pklb','pkwb',
  66. 'rc','ret','rpcc','rs','s4addl','s4addq',
  67. 's4subl','s4subq','s8addl','s8addq','s8subl','s8subq',
  68. 'sextb','sextw','sll','sqrtf','sqrtg','sqrts',
  69. 'sqrtt','sra','srl','stb','stf','stg',
  70. 'sts','stl','stl_c','stq','stq_c','stq_u',
  71. 'stt','stw','subf','subg','subl',
  72. 'subq','subs','subt','trapb','umulh','unpkbl',
  73. 'unpkbw','wh64','wmb','xor','zap','zapnot');
  74. {$ifdef GDB}
  75. procedure tattasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  76. var
  77. curr_n : byte;
  78. begin
  79. if not (cs_debuginfo in aktmoduleswitches) then
  80. exit;
  81. { file changed ? (must be before line info) }
  82. if (fileinfo.fileindex<>0) and
  83. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  84. begin
  85. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  86. if includecount=0 then
  87. curr_n:=n_sourcefile
  88. else
  89. curr_n:=n_includefile;
  90. if (infile^.path^<>'') then
  91. begin
  92. AsmWriteLn(#9'.stabs "'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+
  93. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  94. end;
  95. AsmWriteLn(#9'.stabs "'+lower(FixFileName(infile^.name^))+'",'+
  96. tostr(curr_n)+',0,0,'+'Ltext'+ToStr(IncludeCount));
  97. AsmWriteLn('Ltext'+ToStr(IncludeCount)+':');
  98. inc(includecount);
  99. end;
  100. { line changed ? }
  101. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  102. begin
  103. if (n_line=n_textline) and assigned(funcname) and
  104. (target_os.use_function_relative_addresses) then
  105. begin
  106. AsmWriteLn(target_asm.labelprefix+'l'+tostr(linecount)+':');
  107. AsmWrite(#9'.stabn '+tostr(n_line)+',0,'+tostr(fileinfo.line)+','+
  108. target_asm.labelprefix+'l'+tostr(linecount)+' - ');
  109. AsmWritePChar(FuncName);
  110. AsmLn;
  111. inc(linecount);
  112. end
  113. else
  114. AsmWriteLn(#9'.stabd'#9+tostr(n_line)+',0,'+tostr(fileinfo.line));
  115. end;
  116. stabslastfileinfo:=fileinfo;
  117. end;
  118. {$endif GDB}
  119. procedure tattasmlist.WriteTree(p:paasmoutput);
  120. const
  121. allocstr : array[boolean] of string[10]=(' released',' allocated');
  122. type
  123. t64bitarray = array[0..7] of byte;
  124. t32bitarray = array[0..3] of byte;
  125. var
  126. ch : char;
  127. hp : pai;
  128. consttyp : tait;
  129. s : string;
  130. found : boolean;
  131. i,pos,l : longint;
  132. co : comp;
  133. sin : single;
  134. d : double;
  135. e : extended;
  136. op : tasmop;
  137. calljmp,
  138. do_line : boolean;
  139. sep : char;
  140. begin
  141. if not assigned(p) then
  142. exit;
  143. do_line:=(cs_debuginfo in aktmoduleswitches) or (cs_asm_source in aktglobalswitches);
  144. hp:=pai(p^.first);
  145. while assigned(hp) do
  146. begin
  147. aktfilepos:=hp^.fileinfo;
  148. if do_line then
  149. begin
  150. { I think it is better to write stabs before source line PM }
  151. {$ifdef GDB}
  152. { write stabs }
  153. if cs_debuginfo in aktmoduleswitches then
  154. begin
  155. if not (hp^.typ in [
  156. ait_label,
  157. ait_regalloc,ait_tempalloc,
  158. ait_stabn,ait_stabs,ait_section,
  159. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  160. begin
  161. WriteFileLineInfo(hp^.fileinfo);
  162. end;
  163. end;
  164. {$endif GDB}
  165. { load infile }
  166. if lastfileinfo.fileindex<>hp^.fileinfo.fileindex then
  167. begin
  168. infile:=current_module^.sourcefiles^.get_file(hp^.fileinfo.fileindex);
  169. { open only if needed !! }
  170. if (cs_asm_source in aktglobalswitches) then
  171. infile^.open;
  172. { avoid unnecessary reopens of the same file !! }
  173. lastfileinfo.fileindex:=hp^.fileinfo.fileindex;
  174. { be sure to change line !! }
  175. lastfileinfo.line:=-1;
  176. end;
  177. { write source }
  178. if (cs_asm_source in aktglobalswitches) and
  179. not (hp^.typ in [
  180. ait_label,
  181. ait_stabn,ait_stabs,ait_section,
  182. ait_cut,ait_align,ait_stab_function_name]) then
  183. begin
  184. if (infile<>lastinfile) and assigned(lastinfile) then
  185. begin
  186. AsmWriteLn(target_asm.comment+'['+infile^.name^+']');
  187. lastinfile^.close;
  188. end;
  189. if (hp^.fileinfo.line<>lastfileinfo.line) and
  190. (hp^.fileinfo.line<infile^.maxlinebuf) then
  191. begin
  192. if (hp^.fileinfo.line<>0) and
  193. (infile^.linebuf^[hp^.fileinfo.line]>=0) then
  194. AsmWriteLn(target_asm.comment+'['+tostr(hp^.fileinfo.line)+'] '+
  195. fixline(infile^.GetLineStr(hp^.fileinfo.line)));
  196. { set it to a negative value !
  197. to make that is has been read already !! PM }
  198. infile^.linebuf^[hp^.fileinfo.line]:=-infile^.linebuf^[hp^.fileinfo.line]-1;
  199. end;
  200. lastfileinfo:=hp^.fileinfo;
  201. lastinfile:=infile;
  202. end;
  203. end;
  204. case hp^.typ of
  205. ait_comment :
  206. Begin
  207. AsmWrite(target_asm.comment);
  208. AsmWritePChar(pai_asm_comment(hp)^.str);
  209. AsmLn;
  210. End;
  211. ait_regalloc :
  212. begin
  213. if (cs_asm_regalloc in aktglobalswitches) then
  214. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[pairegalloc(hp)^.reg]+
  215. allocstr[pairegalloc(hp)^.allocation]);
  216. end;
  217. ait_tempalloc :
  218. begin
  219. if (cs_asm_tempalloc in aktglobalswitches) then
  220. AsmWriteLn(target_asm.comment+'Temp '+tostr(paitempalloc(hp)^.temppos)+','+
  221. tostr(paitempalloc(hp)^.tempsize)+allocstr[paitempalloc(hp)^.allocation]);
  222. end;
  223. ait_align :
  224. begin
  225. AsmWrite(#9'.balign '+tostr(pai_align(hp)^.aligntype));
  226. if pai_align(hp)^.use_op then
  227. AsmWrite(','+tostr(pai_align(hp)^.fillop));
  228. AsmLn;
  229. end;
  230. ait_section :
  231. begin
  232. if pai_section(hp)^.sec<>sec_none then
  233. begin
  234. AsmLn;
  235. AsmWriteLn(ait_section2str(pai_section(hp)^.sec));
  236. {$ifdef GDB}
  237. lastfileinfo.line:=-1;
  238. {$endif GDB}
  239. end;
  240. end;
  241. ait_datablock :
  242. begin
  243. if pai_datablock(hp)^.is_global then
  244. AsmWrite(#9'.comm'#9)
  245. else
  246. AsmWrite(#9'.lcomm'#9);
  247. AsmWrite(pai_datablock(hp)^.sym^.name);
  248. AsmWriteLn(','+tostr(pai_datablock(hp)^.size));
  249. end;
  250. ait_const_32bit,
  251. ait_const_16bit,
  252. ait_const_8bit :
  253. begin
  254. AsmWrite(ait_const2str[hp^.typ]+tostr(pai_const(hp)^.value));
  255. consttyp:=hp^.typ;
  256. l:=0;
  257. repeat
  258. found:=(not (Pai(hp^.next)=nil)) and (Pai(hp^.next)^.typ=consttyp);
  259. if found then
  260. begin
  261. hp:=Pai(hp^.next);
  262. s:=','+tostr(pai_const(hp)^.value);
  263. AsmWrite(s);
  264. inc(l,length(s));
  265. end;
  266. until (not found) or (l>line_length);
  267. AsmLn;
  268. end;
  269. ait_const_symbol :
  270. begin
  271. AsmWrite(#9'.long'#9+pai_const_symbol(hp)^.sym^.name);
  272. if pai_const_symbol(hp)^.offset>0 then
  273. AsmWrite('+'+tostr(pai_const_symbol(hp)^.offset))
  274. else if pai_const_symbol(hp)^.offset<0 then
  275. AsmWrite(tostr(pai_const_symbol(hp)^.offset));
  276. AsmLn;
  277. end;
  278. ait_const_rva :
  279. AsmWriteLn(#9'.rva'#9+pai_const_symbol(hp)^.sym^.name);
  280. ait_real_64bit :
  281. begin
  282. if do_line then
  283. AsmWriteLn(target_asm.comment+double2str(pai_real_64bit(hp)^.value));
  284. d:=pai_real_64bit(hp)^.value;
  285. AsmWrite(#9'.byte'#9);
  286. for i:=0 to 7 do
  287. begin
  288. if i<>0 then
  289. AsmWrite(',');
  290. AsmWrite(tostr(t64bitarray(d)[i]));
  291. end;
  292. AsmLn;
  293. end;
  294. ait_real_32bit :
  295. begin
  296. if do_line then
  297. AsmWriteLn(target_asm.comment+single2str(pai_real_32bit(hp)^.value));
  298. sin:=pai_real_32bit(hp)^.value;
  299. AsmWrite(#9'.byte'#9);
  300. for i:=0 to 3 do
  301. begin
  302. if i<>0 then
  303. AsmWrite(',');
  304. AsmWrite(tostr(t32bitarray(sin)[i]));
  305. end;
  306. AsmLn;
  307. end;
  308. ait_comp_64bit :
  309. begin
  310. if do_line then
  311. AsmWriteLn(target_asm.comment+comp2str(pai_comp_64bit(hp)^.value));
  312. AsmWrite(#9'.byte'#9);
  313. {$ifdef FPC}
  314. co:=comp(pai_comp_64bit(hp)^.value);
  315. {$else}
  316. co:=pai_comp_64bit(hp)^.value;
  317. {$endif}
  318. for i:=0 to 7 do
  319. begin
  320. if i<>0 then
  321. AsmWrite(',');
  322. AsmWrite(tostr(t64bitarray(co)[i]));
  323. end;
  324. AsmLn;
  325. end;
  326. ait_direct :
  327. begin
  328. AsmWritePChar(pai_direct(hp)^.str);
  329. AsmLn;
  330. {$IfDef GDB}
  331. if strpos(pai_direct(hp)^.str,'.data')<>nil then
  332. n_line:=n_dataline
  333. else if strpos(pai_direct(hp)^.str,'.text')<>nil then
  334. n_line:=n_textline
  335. else if strpos(pai_direct(hp)^.str,'.bss')<>nil then
  336. n_line:=n_bssline;
  337. {$endif GDB}
  338. end;
  339. ait_string :
  340. begin
  341. pos:=0;
  342. for i:=1 to pai_string(hp)^.len do
  343. begin
  344. if pos=0 then
  345. begin
  346. AsmWrite(#9'.ascii'#9'"');
  347. pos:=20;
  348. end;
  349. ch:=pai_string(hp)^.str[i-1];
  350. case ch of
  351. #0, {This can't be done by range, because a bug in FPC}
  352. #1..#31,
  353. #128..#255 : s:='\'+tostr(ord(ch) shr 6)+tostr((ord(ch) and 63) shr 3)+tostr(ord(ch) and 7);
  354. '"' : s:='\"';
  355. '\' : s:='\\';
  356. else
  357. s:=ch;
  358. end;
  359. AsmWrite(s);
  360. inc(pos,length(s));
  361. if (pos>line_length) or (i=pai_string(hp)^.len) then
  362. begin
  363. AsmWriteLn('"');
  364. pos:=0;
  365. end;
  366. end;
  367. end;
  368. ait_label :
  369. begin
  370. if (pai_label(hp)^.l^.is_used) then
  371. begin
  372. if pai_label(hp)^.l^.typ=AS_GLOBAL then
  373. AsmWriteLn('.globl'#9+pai_label(hp)^.l^.name);
  374. AsmWriteLn(pai_label(hp)^.l^.name+':');
  375. end;
  376. end;
  377. ait_symbol :
  378. begin
  379. if pai_symbol(hp)^.is_global then
  380. AsmWriteLn('.globl'#9+pai_symbol(hp)^.sym^.name);
  381. if target_info.target=target_i386_linux then
  382. begin
  383. AsmWrite(#9'.type'#9+pai_symbol(hp)^.sym^.name);
  384. if assigned(pai(hp^.next)) and
  385. (pai(hp^.next)^.typ in [ait_const_symbol,ait_const_rva,
  386. ait_const_32bit,ait_const_16bit,ait_const_8bit,ait_datablock,
  387. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit]) then
  388. AsmWriteLn(',@object')
  389. else
  390. AsmWriteLn(',@function');
  391. if pai_symbol(hp)^.sym^.size>0 then
  392. AsmWriteLn(#9'.size'#9+pai_symbol(hp)^.sym^.name+', '+tostr(pai_symbol(hp)^.sym^.size));
  393. end;
  394. AsmWriteLn(pai_symbol(hp)^.sym^.name+':');
  395. end;
  396. ait_symbol_end :
  397. begin
  398. if target_info.target=target_i386_linux then
  399. begin
  400. s:=target_asm.labelprefix+'e'+tostr(symendcount);
  401. inc(symendcount);
  402. AsmWriteLn(s+':');
  403. AsmWriteLn(#9'.size'#9+pai_symbol(hp)^.sym^.name+', '+s+' - '+pai_symbol(hp)^.sym^.name);
  404. end;
  405. end;
  406. ait_instruction : WriteInstruction(Hp);
  407. {$ifdef GDB}
  408. ait_stabs :
  409. begin
  410. AsmWrite(#9'.stabs ');
  411. AsmWritePChar(pai_stabs(hp)^.str);
  412. AsmLn;
  413. end;
  414. ait_stabn :
  415. begin
  416. AsmWrite(#9'.stabn ');
  417. AsmWritePChar(pai_stabn(hp)^.str);
  418. AsmLn;
  419. end;
  420. ait_force_line :
  421. stabslastfileinfo.line:=0;
  422. ait_stab_function_name:
  423. funcname:=pai_stab_function_name(hp)^.str;
  424. {$endif GDB}
  425. ait_cut :
  426. begin
  427. if SmartAsm then
  428. begin
  429. { only reset buffer if nothing has changed }
  430. if AsmSize=AsmStartSize then
  431. AsmClear
  432. else
  433. begin
  434. AsmClose;
  435. DoAssemble;
  436. if pai_cut(hp)^.EndName then
  437. IsEndFile:=true;
  438. AsmCreate;
  439. end;
  440. { avoid empty files }
  441. while assigned(hp^.next) and (pai(hp^.next)^.typ in [ait_cut,ait_section,ait_comment]) do
  442. begin
  443. if pai(hp^.next)^.typ=ait_section then
  444. lastsec:=pai_section(hp^.next)^.sec;
  445. hp:=pai(hp^.next);
  446. end;
  447. {$ifdef GDB}
  448. { force write of filename }
  449. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  450. includecount:=0;
  451. funcname:=nil;
  452. WriteFileLineInfo(hp^.fileinfo);
  453. {$endif GDB}
  454. if lastsec<>sec_none then
  455. AsmWriteLn(ait_section2str(lastsec));
  456. AsmStartSize:=AsmSize;
  457. end;
  458. end;
  459. ait_marker :
  460. ;
  461. else
  462. internalerror(10000);
  463. end;
  464. hp:=pai(hp^.next);
  465. end;
  466. end;
  467. procedure tattasmlist.WriteAsmList;
  468. var
  469. p:dirstr;
  470. n:namestr;
  471. e:extstr;
  472. {$ifdef GDB}
  473. fileinfo : tfileposinfo;
  474. {$endif GDB}
  475. begin
  476. {$ifdef EXTDEBUG}
  477. if assigned(current_module^.mainsource) then
  478. Comment(v_info,'Start writing att-styled assembler output for '+current_module^.mainsource^);
  479. {$endif}
  480. LastSec:=sec_none;
  481. {$ifdef GDB}
  482. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  483. {$endif GDB}
  484. FillChar(lastfileinfo,sizeof(lastfileinfo),0);
  485. LastInfile:=nil;
  486. if assigned(current_module^.mainsource) then
  487. fsplit(current_module^.mainsource^,p,n,e)
  488. else
  489. begin
  490. p:=inputdir;
  491. n:=inputfile;
  492. e:=inputextension;
  493. end;
  494. { to get symify to work }
  495. AsmWriteLn(#9'.file "'+FixFileName(n+e)+'"');
  496. {$ifdef GDB}
  497. n_line:=n_bssline;
  498. funcname:=nil;
  499. linecount:=1;
  500. includecount:=0;
  501. fileinfo.fileindex:=1;
  502. fileinfo.line:=1;
  503. { Write main file }
  504. WriteFileLineInfo(fileinfo);
  505. {$endif GDB}
  506. AsmStartSize:=AsmSize;
  507. symendcount:=0;
  508. countlabelref:=false;
  509. If (cs_debuginfo in aktmoduleswitches) then
  510. WriteTree(debuglist);
  511. WriteTree(codesegment);
  512. WriteTree(datasegment);
  513. WriteTree(consts);
  514. WriteTree(rttilist);
  515. Writetree(resourcestringlist);
  516. WriteTree(bsssegment);
  517. Writetree(importssection);
  518. Writetree(exportssection);
  519. Writetree(resourcesection);
  520. countlabelref:=true;
  521. AsmLn;
  522. {$ifdef EXTDEBUG}
  523. if assigned(current_module^.mainsource) then
  524. comment(v_info,'Done writing att-styled assembler output for '+current_module^.mainsource^);
  525. {$endif EXTDEBUG}
  526. end;
  527. Procedure tattasmlist.WriteInstruction (P : Pai); virtual;
  528. begin
  529. RunError(255);
  530. end;
  531. end.
  532. {
  533. $Log$
  534. Revision 1.1 1999-08-03 13:48:50 michael
  535. + Initial implementation
  536. }