ag386bin.pas 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Peter Vreman
  4. This unit implements an binary assembler output class
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ag386bin;
  19. {$i defines.inc}
  20. {$define MULTIPASS}
  21. {$define EXTERNALBSS}
  22. interface
  23. uses
  24. cpubase,cobjects,aasm,fmodule,finput,assemble;
  25. type
  26. togtype=(og_none,og_dbg,og_coff,og_pecoff,og_elf);
  27. pi386binasmlist=^ti386binasmlist;
  28. ti386binasmlist=object
  29. SmartAsm : boolean;
  30. constructor init(t:togtype;smart:boolean);
  31. destructor done;
  32. procedure WriteBin;
  33. private
  34. { the aasmoutput lists that need to be processed }
  35. lists : byte;
  36. list : array[1..maxoutputlists] of paasmoutput;
  37. { current processing }
  38. currlistidx : byte;
  39. currlist : paasmoutput;
  40. currpass : byte;
  41. {$ifdef GDB}
  42. n_line : byte; { different types of source lines }
  43. linecount,
  44. includecount : longint;
  45. funcname : pasmsymbol;
  46. stabslastfileinfo : tfileposinfo;
  47. procedure convertstabs(p:pchar);
  48. procedure emitlineinfostabs(nidx,line : longint);
  49. procedure emitstabs(s:string);
  50. procedure WriteFileLineInfo(var fileinfo : tfileposinfo);
  51. procedure StartFileLineInfo;
  52. procedure EndFileLineInfo;
  53. {$endif}
  54. function MaybeNextList(var hp:pai):boolean;
  55. function TreePass0(hp:pai):pai;
  56. function TreePass1(hp:pai):pai;
  57. function TreePass2(hp:pai):pai;
  58. procedure writetree;
  59. procedure writetreesmart;
  60. end;
  61. implementation
  62. uses
  63. {$ifdef delphi}
  64. sysutils,
  65. {$else}
  66. strings,
  67. {$endif}
  68. cutils,globtype,globals,systems,verbose,
  69. cpuasm,
  70. {$ifdef GDB}
  71. gdb,
  72. {$endif}
  73. og386,og386dbg,og386cff,og386elf;
  74. {$ifdef GDB}
  75. procedure ti386binasmlist.convertstabs(p:pchar);
  76. var
  77. ofs,
  78. nidx,nother,ii,i,line,j : longint;
  79. code : integer;
  80. hp : pchar;
  81. reloc : boolean;
  82. sec : tsection;
  83. ps : pasmsymbol;
  84. s : string;
  85. begin
  86. ofs:=0;
  87. reloc:=true;
  88. ps:=nil;
  89. sec:=sec_none;
  90. if p[0]='"' then
  91. begin
  92. i:=1;
  93. { we can have \" inside the string !! PM }
  94. while not ((p[i]='"') and (p[i-1]<>'\')) do
  95. inc(i);
  96. p[i]:=#0;
  97. ii:=i;
  98. hp:=@p[1];
  99. s:=StrPas(@P[i+2]);
  100. end
  101. else
  102. begin
  103. hp:=nil;
  104. s:=StrPas(P);
  105. i:=-2; {needed below (PM) }
  106. end;
  107. { When in pass 1 then only alloc and leave }
  108. if currpass=1 then
  109. begin
  110. objectalloc^.staballoc(hp);
  111. if assigned(hp) then
  112. p[i]:='"';
  113. exit;
  114. end;
  115. { Parse the rest of the stabs }
  116. if s='' then
  117. internalerror(33000);
  118. j:=pos(',',s);
  119. if j=0 then
  120. internalerror(33001);
  121. Val(Copy(s,1,j-1),nidx,code);
  122. if code<>0 then
  123. internalerror(33002);
  124. i:=i+2+j;
  125. Delete(s,1,j);
  126. j:=pos(',',s);
  127. if (j=0) then
  128. internalerror(33003);
  129. Val(Copy(s,1,j-1),nother,code);
  130. if code<>0 then
  131. internalerror(33004);
  132. i:=i+j;
  133. Delete(s,1,j);
  134. j:=pos(',',s);
  135. if j=0 then
  136. begin
  137. j:=256;
  138. ofs:=-1;
  139. end;
  140. Val(Copy(s,1,j-1),line,code);
  141. if code<>0 then
  142. internalerror(33005);
  143. if ofs=0 then
  144. begin
  145. Delete(s,1,j);
  146. i:=i+j;
  147. Val(s,ofs,code);
  148. if code=0 then
  149. reloc:=false
  150. else
  151. begin
  152. ofs:=0;
  153. s:=strpas(@p[i]);
  154. { handle asmsymbol or
  155. asmsymbol - asmsymbol }
  156. j:=pos(' ',s);
  157. if j=0 then
  158. j:=pos('-',s);
  159. { single asmsymbol }
  160. if j=0 then
  161. j:=256;
  162. { the symbol can be external
  163. so we must use newasmsymbol and
  164. not getasmsymbol !! PM }
  165. ps:=newasmsymbol(copy(s,1,j-1));
  166. if not assigned(ps) then
  167. internalerror(33006)
  168. else
  169. begin
  170. sec:=ps^.section;
  171. ofs:=ps^.address;
  172. reloc:=true;
  173. UsedAsmSymbolListInsert(ps);
  174. end;
  175. if j<256 then
  176. begin
  177. i:=i+j;
  178. s:=strpas(@p[i]);
  179. if (s<>'') and (s[1]=' ') then
  180. begin
  181. j:=0;
  182. while (s[j+1]=' ') do
  183. inc(j);
  184. i:=i+j;
  185. s:=strpas(@p[i]);
  186. end;
  187. ps:=getasmsymbol(s);
  188. if not assigned(ps) then
  189. internalerror(33007)
  190. else
  191. begin
  192. if ps^.section<>sec then
  193. internalerror(33008);
  194. ofs:=ofs-ps^.address;
  195. reloc:=false;
  196. UsedAsmSymbolListInsert(ps);
  197. end;
  198. end;
  199. end;
  200. end;
  201. { external bss need speical handling (PM) }
  202. if assigned(ps) and (ps^.section=sec_none) then
  203. begin
  204. if currpass=2 then
  205. objectoutput^.writesymbol(ps);
  206. objectoutput^.WriteSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  207. end
  208. else
  209. objectoutput^.WriteStabs(sec,ofs,hp,nidx,nother,line,reloc);
  210. if assigned(hp) then
  211. p[ii]:='"';
  212. end;
  213. procedure ti386binasmlist.emitlineinfostabs(nidx,line : longint);
  214. var
  215. sec : tsection;
  216. begin
  217. if currpass=1 then
  218. begin
  219. objectalloc^.staballoc(nil);
  220. exit;
  221. end;
  222. if (nidx=n_textline) and assigned(funcname) and
  223. (target_os.use_function_relative_addresses) then
  224. objectoutput^.WriteStabs(sec_code,objectoutput^.sectionsize(sec_code)-funcname^.address,
  225. nil,nidx,0,line,false)
  226. else
  227. begin
  228. if nidx=n_textline then
  229. sec:=sec_code
  230. else if nidx=n_dataline then
  231. sec:=sec_data
  232. else
  233. sec:=sec_bss;
  234. objectoutput^.WriteStabs(sec,objectoutput^.sectionsize(sec),
  235. nil,nidx,0,line,true);
  236. end;
  237. end;
  238. procedure ti386binasmlist.emitstabs(s:string);
  239. begin
  240. s:=s+#0;
  241. ConvertStabs(@s[1]);
  242. end;
  243. procedure ti386binasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  244. var
  245. curr_n : byte;
  246. hp : pasmsymbol;
  247. infile : pinputfile;
  248. begin
  249. if not ((cs_debuginfo in aktmoduleswitches) or
  250. (cs_gdb_lineinfo in aktglobalswitches)) then
  251. exit;
  252. { file changed ? (must be before line info) }
  253. if (fileinfo.fileindex<>0) and
  254. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  255. begin
  256. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  257. if includecount=0 then
  258. curr_n:=n_sourcefile
  259. else
  260. curr_n:=n_includefile;
  261. { get symbol for this includefile }
  262. hp:=newasmsymboltype('Ltext'+ToStr(IncludeCount),AB_LOCAL,AT_FUNCTION);
  263. if currpass=1 then
  264. begin
  265. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  266. UsedAsmSymbolListInsert(hp);
  267. end
  268. else
  269. objectoutput^.writesymbol(hp);
  270. { emit stabs }
  271. if (infile^.path^<>'') then
  272. EmitStabs('"'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+tostr(curr_n)+
  273. ',0,0,Ltext'+ToStr(IncludeCount));
  274. EmitStabs('"'+lower(FixFileName(infile^.name^))+'",'+tostr(curr_n)+
  275. ',0,0,Ltext'+ToStr(IncludeCount));
  276. inc(includecount);
  277. end;
  278. { line changed ? }
  279. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  280. emitlineinfostabs(n_line,fileinfo.line);
  281. stabslastfileinfo:=fileinfo;
  282. end;
  283. procedure ti386binasmlist.StartFileLineInfo;
  284. var
  285. fileinfo : tfileposinfo;
  286. begin
  287. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  288. n_line:=n_textline;
  289. funcname:=nil;
  290. linecount:=1;
  291. includecount:=0;
  292. fileinfo.fileindex:=1;
  293. fileinfo.line:=1;
  294. WriteFileLineInfo(fileinfo);
  295. end;
  296. procedure ti386binasmlist.EndFileLineInfo;
  297. var
  298. hp : pasmsymbol;
  299. store_sec : tsection;
  300. begin
  301. if not ((cs_debuginfo in aktmoduleswitches) or
  302. (cs_gdb_lineinfo in aktglobalswitches)) then
  303. exit;
  304. store_sec:=objectalloc^.currsec;
  305. objectalloc^.setsection(sec_code);
  306. hp:=newasmsymboltype('Letext',AB_LOCAL,AT_FUNCTION);
  307. if currpass=1 then
  308. begin
  309. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  310. UsedAsmSymbolListInsert(hp);
  311. end
  312. else
  313. objectoutput^.writesymbol(hp);
  314. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  315. objectalloc^.setsection(store_sec);
  316. end;
  317. {$endif GDB}
  318. function ti386binasmlist.MaybeNextList(var hp:pai):boolean;
  319. begin
  320. { maybe end of list }
  321. while not assigned(hp) do
  322. begin
  323. if currlistidx<lists then
  324. begin
  325. inc(currlistidx);
  326. currlist:=list[currlistidx];
  327. hp:=pai(currlist^.first);
  328. end
  329. else
  330. begin
  331. MaybeNextList:=false;
  332. exit;
  333. end;
  334. end;
  335. MaybeNextList:=true;
  336. end;
  337. function ti386binasmlist.TreePass0(hp:pai):pai;
  338. var
  339. l : longint;
  340. begin
  341. while assigned(hp) do
  342. begin
  343. case hp^.typ of
  344. ait_align :
  345. begin
  346. { always use the maximum fillsize in this pass to avoid possible
  347. short jumps to become out of range }
  348. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype;
  349. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  350. end;
  351. ait_datablock :
  352. begin
  353. {$ifdef EXTERNALBSS}
  354. if not SmartAsm then
  355. begin
  356. if not pai_datablock(hp)^.is_global then
  357. begin
  358. l:=pai_datablock(hp)^.size;
  359. if l>2 then
  360. objectalloc^.sectionalign(4)
  361. else if l>1 then
  362. objectalloc^.sectionalign(2);
  363. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  364. end;
  365. end
  366. else
  367. begin
  368. {$endif}
  369. l:=pai_datablock(hp)^.size;
  370. if l>2 then
  371. objectalloc^.sectionalign(4)
  372. else if l>1 then
  373. objectalloc^.sectionalign(2);
  374. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  375. end;
  376. end;
  377. ait_const_32bit :
  378. objectalloc^.sectionalloc(4);
  379. ait_const_16bit :
  380. objectalloc^.sectionalloc(2);
  381. ait_const_8bit :
  382. objectalloc^.sectionalloc(1);
  383. ait_real_80bit :
  384. objectalloc^.sectionalloc(10);
  385. ait_real_64bit :
  386. objectalloc^.sectionalloc(8);
  387. ait_real_32bit :
  388. objectalloc^.sectionalloc(4);
  389. ait_comp_64bit :
  390. objectalloc^.sectionalloc(8);
  391. ait_const_rva,
  392. ait_const_symbol :
  393. objectalloc^.sectionalloc(4);
  394. ait_section:
  395. objectalloc^.setsection(pai_section(hp)^.sec);
  396. ait_symbol :
  397. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  398. ait_label :
  399. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  400. ait_string :
  401. objectalloc^.sectionalloc(pai_string(hp)^.len);
  402. ait_instruction :
  403. begin
  404. { reset instructions which could change in pass 2 }
  405. paicpu(hp)^.resetpass2;
  406. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  407. end;
  408. ait_cut :
  409. if SmartAsm then
  410. break;
  411. end;
  412. hp:=pai(hp^.next);
  413. end;
  414. TreePass0:=hp;
  415. end;
  416. function ti386binasmlist.TreePass1(hp:pai):pai;
  417. var
  418. i,l : longint;
  419. begin
  420. while assigned(hp) do
  421. begin
  422. {$ifdef GDB}
  423. { write stabs }
  424. if ((cs_debuginfo in aktmoduleswitches) or
  425. (cs_gdb_lineinfo in aktglobalswitches)) then
  426. begin
  427. if (objectalloc^.currsec<>sec_none) and
  428. not(hp^.typ in [
  429. ait_label,
  430. ait_regalloc,ait_tempalloc,
  431. ait_stabn,ait_stabs,ait_section,
  432. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  433. WriteFileLineInfo(hp^.fileinfo);
  434. end;
  435. {$endif GDB}
  436. case hp^.typ of
  437. ait_align :
  438. begin
  439. { here we must determine the fillsize which is used in pass2 }
  440. pai_align(hp)^.fillsize:=align(objectalloc^.sectionsize,pai_align(hp)^.aligntype)-
  441. objectalloc^.sectionsize;
  442. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  443. end;
  444. ait_datablock :
  445. begin
  446. if objectalloc^.currsec<>sec_bss then
  447. Message(asmw_e_alloc_data_only_in_bss);
  448. {$ifdef EXTERNALBSS}
  449. if not SmartAsm then
  450. begin
  451. if pai_datablock(hp)^.is_global then
  452. begin
  453. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  454. { force to be common/external, must be after setaddress as that would
  455. set it to AS_GLOBAL }
  456. pai_datablock(hp)^.sym^.bind:=AB_COMMON;
  457. end
  458. else
  459. begin
  460. l:=pai_datablock(hp)^.size;
  461. if l>2 then
  462. objectalloc^.sectionalign(4)
  463. else if l>1 then
  464. objectalloc^.sectionalign(2);
  465. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,
  466. pai_datablock(hp)^.size);
  467. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  468. end;
  469. end
  470. else
  471. {$endif}
  472. begin
  473. l:=pai_datablock(hp)^.size;
  474. if l>2 then
  475. objectalloc^.sectionalign(4)
  476. else if l>1 then
  477. objectalloc^.sectionalign(2);
  478. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  479. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  480. end;
  481. UsedAsmSymbolListInsert(pai_datablock(hp)^.sym);
  482. end;
  483. ait_const_32bit :
  484. objectalloc^.sectionalloc(4);
  485. ait_const_16bit :
  486. objectalloc^.sectionalloc(2);
  487. ait_const_8bit :
  488. objectalloc^.sectionalloc(1);
  489. ait_real_80bit :
  490. objectalloc^.sectionalloc(10);
  491. ait_real_64bit :
  492. objectalloc^.sectionalloc(8);
  493. ait_real_32bit :
  494. objectalloc^.sectionalloc(4);
  495. ait_comp_64bit :
  496. objectalloc^.sectionalloc(8);
  497. ait_const_rva,
  498. ait_const_symbol :
  499. begin
  500. objectalloc^.sectionalloc(4);
  501. UsedAsmSymbolListInsert(pai_const_symbol(hp)^.sym);
  502. end;
  503. ait_section:
  504. begin
  505. objectalloc^.setsection(pai_section(hp)^.sec);
  506. {$ifdef GDB}
  507. case pai_section(hp)^.sec of
  508. sec_code : n_line:=n_textline;
  509. sec_data : n_line:=n_dataline;
  510. sec_bss : n_line:=n_bssline;
  511. else
  512. n_line:=n_dataline;
  513. end;
  514. stabslastfileinfo.line:=-1;
  515. {$endif GDB}
  516. end;
  517. {$ifdef GDB}
  518. ait_stabn :
  519. convertstabs(pai_stabn(hp)^.str);
  520. ait_stabs :
  521. convertstabs(pai_stabs(hp)^.str);
  522. ait_stab_function_name :
  523. begin
  524. if assigned(pai_stab_function_name(hp)^.str) then
  525. begin
  526. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str));
  527. UsedAsmSymbolListInsert(funcname);
  528. end
  529. else
  530. funcname:=nil;
  531. end;
  532. ait_force_line :
  533. stabslastfileinfo.line:=0;
  534. {$endif}
  535. ait_symbol :
  536. begin
  537. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  538. UsedAsmSymbolListInsert(pai_symbol(hp)^.sym);
  539. end;
  540. ait_symbol_end :
  541. begin
  542. if target_info.target=target_i386_linux then
  543. begin
  544. pai_symbol(hp)^.sym^.size:=objectalloc^.sectionsize-pai_symbol(hp)^.sym^.address;
  545. UsedAsmSymbolListInsert(pai_symbol(hp)^.sym);
  546. end;
  547. end;
  548. ait_label :
  549. begin
  550. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  551. UsedAsmSymbolListInsert(pai_label(hp)^.l);
  552. end;
  553. ait_string :
  554. objectalloc^.sectionalloc(pai_string(hp)^.len);
  555. ait_instruction :
  556. begin
  557. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  558. { fixup the references }
  559. for i:=1 to paicpu(hp)^.ops do
  560. begin
  561. with paicpu(hp)^.oper[i-1] do
  562. begin
  563. case typ of
  564. top_ref :
  565. begin
  566. if assigned(ref^.symbol) then
  567. UsedAsmSymbolListInsert(ref^.symbol);
  568. end;
  569. top_symbol :
  570. begin
  571. UsedAsmSymbolListInsert(sym);
  572. end;
  573. end;
  574. end;
  575. end;
  576. end;
  577. ait_direct :
  578. Message(asmw_f_direct_not_supported);
  579. ait_cut :
  580. if SmartAsm then
  581. break;
  582. end;
  583. hp:=pai(hp^.next);
  584. end;
  585. TreePass1:=hp;
  586. end;
  587. function ti386binasmlist.TreePass2(hp:pai):pai;
  588. var
  589. l : longint;
  590. {$ifdef I386}
  591. co : comp;
  592. {$endif I386}
  593. begin
  594. { main loop }
  595. while assigned(hp) do
  596. begin
  597. {$ifdef GDB}
  598. { write stabs }
  599. if ((cs_debuginfo in aktmoduleswitches) or
  600. (cs_gdb_lineinfo in aktglobalswitches)) then
  601. begin
  602. if (objectoutput^.currsec<>sec_none) and
  603. not(hp^.typ in [
  604. ait_label,
  605. ait_regalloc,ait_tempalloc,
  606. ait_stabn,ait_stabs,ait_section,
  607. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  608. WriteFileLineInfo(hp^.fileinfo);
  609. end;
  610. {$endif GDB}
  611. case hp^.typ of
  612. ait_align :
  613. objectoutput^.writebytes(pai_align(hp)^.getfillbuf^,pai_align(hp)^.fillsize);
  614. ait_section :
  615. begin
  616. objectoutput^.defaultsection(pai_section(hp)^.sec);
  617. {$ifdef GDB}
  618. case pai_section(hp)^.sec of
  619. sec_code : n_line:=n_textline;
  620. sec_data : n_line:=n_dataline;
  621. sec_bss : n_line:=n_bssline;
  622. else
  623. n_line:=n_dataline;
  624. end;
  625. stabslastfileinfo.line:=-1;
  626. {$endif GDB}
  627. end;
  628. ait_symbol :
  629. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  630. ait_datablock :
  631. begin
  632. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  633. if SmartAsm
  634. {$ifdef EXTERNALBSS}
  635. or (not pai_datablock(hp)^.is_global)
  636. {$endif}
  637. then
  638. begin
  639. l:=pai_datablock(hp)^.size;
  640. if l>2 then
  641. objectoutput^.writealign(4)
  642. else if l>1 then
  643. objectoutput^.writealign(2);
  644. objectoutput^.writealloc(pai_datablock(hp)^.size);
  645. end;
  646. end;
  647. ait_const_32bit :
  648. objectoutput^.writebytes(pai_const(hp)^.value,4);
  649. ait_const_16bit :
  650. objectoutput^.writebytes(pai_const(hp)^.value,2);
  651. ait_const_8bit :
  652. objectoutput^.writebytes(pai_const(hp)^.value,1);
  653. ait_real_80bit :
  654. objectoutput^.writebytes(pai_real_80bit(hp)^.value,10);
  655. ait_real_64bit :
  656. objectoutput^.writebytes(pai_real_64bit(hp)^.value,8);
  657. ait_real_32bit :
  658. objectoutput^.writebytes(pai_real_32bit(hp)^.value,4);
  659. ait_comp_64bit :
  660. begin
  661. {$ifdef FPC}
  662. co:=comp(pai_comp_64bit(hp)^.value);
  663. {$else}
  664. co:=pai_comp_64bit(hp)^.value;
  665. {$endif}
  666. objectoutput^.writebytes(co,8);
  667. end;
  668. ait_string :
  669. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  670. ait_const_rva :
  671. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  672. pai_const_symbol(hp)^.sym,relative_rva);
  673. ait_const_symbol :
  674. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  675. pai_const_symbol(hp)^.sym,relative_false);
  676. ait_label :
  677. objectoutput^.writesymbol(pai_label(hp)^.l);
  678. ait_instruction :
  679. paicpu(hp)^.Pass2;
  680. {$ifdef GDB}
  681. ait_stabn :
  682. convertstabs(pai_stabn(hp)^.str);
  683. ait_stabs :
  684. convertstabs(pai_stabs(hp)^.str);
  685. ait_stab_function_name :
  686. if assigned(pai_stab_function_name(hp)^.str) then
  687. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  688. else
  689. funcname:=nil;
  690. ait_force_line :
  691. stabslastfileinfo.line:=0;
  692. {$endif}
  693. ait_cut :
  694. if SmartAsm then
  695. break;
  696. end;
  697. hp:=pai(hp^.next);
  698. end;
  699. TreePass2:=hp;
  700. end;
  701. procedure ti386binasmlist.writetree;
  702. var
  703. hp : pai;
  704. begin
  705. objectalloc^.resetsections;
  706. objectalloc^.setsection(sec_code);
  707. objectoutput^.initwriting(cut_normal);
  708. objectoutput^.defaultsection(sec_code);
  709. { reset the asmsymbol list }
  710. InitUsedAsmsymbolList;
  711. {$ifdef MULTIPASS}
  712. { Pass 0 }
  713. currpass:=0;
  714. objectalloc^.setsection(sec_code);
  715. { start with list 1 }
  716. currlistidx:=1;
  717. currlist:=list[currlistidx];
  718. hp:=pai(currlist^.first);
  719. while assigned(hp) do
  720. begin
  721. hp:=TreePass0(hp);
  722. MaybeNextList(hp);
  723. end;
  724. { leave if errors have occured }
  725. if errorcount>0 then
  726. exit;
  727. {$endif}
  728. { Pass 1 }
  729. currpass:=1;
  730. objectalloc^.resetsections;
  731. objectalloc^.setsection(sec_code);
  732. {$ifdef GDB}
  733. StartFileLineInfo;
  734. {$endif GDB}
  735. { start with list 1 }
  736. currlistidx:=1;
  737. currlist:=list[currlistidx];
  738. hp:=pai(currlist^.first);
  739. while assigned(hp) do
  740. begin
  741. hp:=TreePass1(hp);
  742. MaybeNextList(hp);
  743. end;
  744. {$ifdef GDB}
  745. EndFileLineInfo;
  746. {$endif GDB}
  747. { check for undefined labels and reset }
  748. UsedAsmSymbolListCheckUndefined;
  749. { set section sizes }
  750. objectoutput^.setsectionsizes(objectalloc^.secsize);
  751. { leave if errors have occured }
  752. if errorcount>0 then
  753. exit;
  754. { Pass 2 }
  755. currpass:=2;
  756. {$ifdef GDB}
  757. StartFileLineInfo;
  758. {$endif GDB}
  759. { start with list 1 }
  760. currlistidx:=1;
  761. currlist:=list[currlistidx];
  762. hp:=pai(currlist^.first);
  763. while assigned(hp) do
  764. begin
  765. hp:=TreePass2(hp);
  766. MaybeNextList(hp);
  767. end;
  768. {$ifdef GDB}
  769. EndFileLineInfo;
  770. {$endif GDB}
  771. { leave if errors have occured }
  772. if errorcount>0 then
  773. exit;
  774. { write last objectfile }
  775. objectoutput^.donewriting;
  776. { reset the used symbols back, must be after the .o has been
  777. written }
  778. UsedAsmsymbolListReset;
  779. DoneUsedAsmsymbolList;
  780. end;
  781. procedure ti386binasmlist.writetreesmart;
  782. var
  783. hp : pai;
  784. startsec : tsection;
  785. place: tcutplace;
  786. begin
  787. objectalloc^.resetsections;
  788. objectalloc^.setsection(sec_code);
  789. objectoutput^.initwriting(cut_normal);
  790. objectoutput^.defaultsection(sec_code);
  791. startsec:=sec_code;
  792. { start with list 1 }
  793. currlistidx:=1;
  794. currlist:=list[currlistidx];
  795. hp:=pai(currlist^.first);
  796. while assigned(hp) do
  797. begin
  798. { reset the asmsymbol list }
  799. InitUsedAsmSymbolList;
  800. {$ifdef MULTIPASS}
  801. { Pass 0 }
  802. currpass:=0;
  803. objectalloc^.resetsections;
  804. objectalloc^.setsection(startsec);
  805. TreePass0(hp);
  806. {$endif}
  807. { leave if errors have occured }
  808. if errorcount>0 then
  809. exit;
  810. { Pass 1 }
  811. currpass:=1;
  812. objectalloc^.resetsections;
  813. objectalloc^.setsection(startsec);
  814. {$ifdef GDB}
  815. StartFileLineInfo;
  816. {$endif GDB}
  817. TreePass1(hp);
  818. {$ifdef GDB}
  819. EndFileLineInfo;
  820. {$endif GDB}
  821. { check for undefined labels }
  822. UsedAsmSymbolListCheckUndefined;
  823. { set section sizes }
  824. objectoutput^.setsectionsizes(objectalloc^.secsize);
  825. { leave if errors have occured }
  826. if errorcount>0 then
  827. exit;
  828. { Pass 2 }
  829. currpass:=2;
  830. objectoutput^.defaultsection(startsec);
  831. {$ifdef GDB}
  832. StartFileLineInfo;
  833. {$endif GDB}
  834. hp:=TreePass2(hp);
  835. {$ifdef GDB}
  836. EndFileLineInfo;
  837. {$endif GDB}
  838. { leave if errors have occured }
  839. if errorcount>0 then
  840. exit;
  841. { if not end then write the current objectfile }
  842. objectoutput^.donewriting;
  843. { reset the used symbols back, must be after the .o has been
  844. written }
  845. UsedAsmsymbolListReset;
  846. DoneUsedAsmsymbolList;
  847. { end of lists? }
  848. if not MaybeNextList(hp) then
  849. break;
  850. { save section for next loop }
  851. { this leads to a problem if startsec is sec_none !! PM }
  852. startsec:=objectalloc^.currsec;
  853. { we will start a new objectfile so reset everything }
  854. { The place can still change in the next while loop, so don't init }
  855. { the writer yet (JM) }
  856. if (hp^.typ=ait_cut) then
  857. place := pai_cut(hp)^.place
  858. else
  859. place := cut_normal;
  860. { avoid empty files }
  861. while assigned(hp^.next) and
  862. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  863. begin
  864. if pai(hp^.next)^.typ=ait_section then
  865. startsec:=pai_section(hp^.next)^.sec
  866. else if (pai(hp^.next)^.typ=ait_cut) then
  867. place := pai_cut(hp)^.place;
  868. hp:=pai(hp^.next);
  869. end;
  870. objectoutput^.initwriting(place);
  871. hp:=pai(hp^.next);
  872. { there is a problem if startsec is sec_none !! PM }
  873. if startsec=sec_none then
  874. startsec:=sec_code;
  875. if not MaybeNextList(hp) then
  876. break;
  877. end;
  878. end;
  879. procedure ti386binasmlist.writebin;
  880. procedure addlist(p:paasmoutput);
  881. begin
  882. inc(lists);
  883. list[lists]:=p;
  884. end;
  885. begin
  886. if cs_debuginfo in aktmoduleswitches then
  887. addlist(debuglist);
  888. addlist(codesegment);
  889. addlist(datasegment);
  890. addlist(consts);
  891. addlist(rttilist);
  892. if assigned(resourcestringlist) then
  893. addlist(resourcestringlist);
  894. addlist(bsssegment);
  895. if assigned(importssection) then
  896. addlist(importssection);
  897. if assigned(exportssection) and not UseDeffileForExport then
  898. addlist(exportssection);
  899. if assigned(resourcesection) then
  900. addlist(resourcesection);
  901. if SmartAsm then
  902. writetreesmart
  903. else
  904. writetree;
  905. end;
  906. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  907. begin
  908. case t of
  909. og_none :
  910. Message(asmw_f_no_binary_writer_selected);
  911. og_dbg :
  912. objectoutput:=new(pdbgoutput,init(smart));
  913. og_coff :
  914. objectoutput:=new(pdjgppcoffoutput,init(smart));
  915. og_pecoff :
  916. objectoutput:=new(pwin32coffoutput,init(smart));
  917. og_elf :
  918. objectoutput:=new(pelf32output,init(smart));
  919. end;
  920. objectalloc:=new(pobjectalloc,init);
  921. SmartAsm:=smart;
  922. currpass:=0;
  923. end;
  924. destructor ti386binasmlist.done;
  925. {$ifdef MEMDEBUG}
  926. var
  927. d : tmemdebug;
  928. {$endif}
  929. begin
  930. {$ifdef MEMDEBUG}
  931. d.init('agbin');
  932. {$endif}
  933. dispose(objectoutput,done);
  934. dispose(objectalloc,done);
  935. {$ifdef MEMDEBUG}
  936. d.done;
  937. {$endif}
  938. end;
  939. end.
  940. {
  941. $Log$
  942. Revision 1.8 2000-09-24 15:06:10 peter
  943. * use defines.inc
  944. Revision 1.7 2000/08/27 16:11:49 peter
  945. * moved some util functions from globals,cobjects to cutils
  946. * splitted files into finput,fmodule
  947. Revision 1.6 2000/08/12 15:34:22 peter
  948. + usedasmsymbollist to check and reset only the used symbols (merged)
  949. Revision 1.5 2000/08/08 19:28:57 peter
  950. * memdebug/memory patches (merged)
  951. * only once illegal directive (merged)
  952. Revision 1.4 2000/08/04 22:00:50 peter
  953. * merges from fixes
  954. Revision 1.3 2000/07/13 12:08:24 michael
  955. + patched to 1.1.0 with former 1.09patch from peter
  956. Revision 1.2 2000/07/13 11:32:29 michael
  957. + removed logs
  958. }