ag386bin.pas 33 KB

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