ag386bin.pas 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. 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. end;
  197. end;
  198. end;
  199. end;
  200. { external bss need speical handling (PM) }
  201. if assigned(ps) and (ps^.section=sec_none) then
  202. begin
  203. if currpass<>1 then
  204. objectoutput^.writesymbol(ps);
  205. objectoutput^.WriteSymStabs(sec,ofs,hp,ps,nidx,nother,line,reloc)
  206. end
  207. else
  208. objectoutput^.WriteStabs(sec,ofs,hp,nidx,nother,line,reloc);
  209. if assigned(hp) then
  210. p[ii]:='"';
  211. end;
  212. {$ifdef unused}
  213. procedure ti386binasmlist.emitsymbolstabs(s : string;nidx,nother,line : longint;
  214. firstasm,secondasm : pasmsymbol);
  215. var
  216. hp : pchar;
  217. begin
  218. if s='' then
  219. hp:=nil
  220. else
  221. begin
  222. s:=s+#0;
  223. hp:=@s[1];
  224. end;
  225. if not assigned(secondasm) then
  226. begin
  227. if not assigned(firstasm) then
  228. internalerror(33009);
  229. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address,hp,nidx,nother,line,true);
  230. end
  231. else
  232. begin
  233. if firstasm^.section<>secondasm^.section then
  234. internalerror(33010);
  235. objectoutput^.WriteStabs(firstasm^.section,firstasm^.address-secondasm^.address,
  236. hp,nidx,nother,line,false);
  237. end;
  238. end;
  239. {$endif}
  240. procedure ti386binasmlist.emitlineinfostabs(nidx,line : longint);
  241. var
  242. sec : tsection;
  243. begin
  244. if currpass=1 then
  245. begin
  246. objectalloc^.staballoc(nil);
  247. exit;
  248. end;
  249. if (nidx=n_textline) and assigned(funcname) and
  250. (target_os.use_function_relative_addresses) then
  251. objectoutput^.WriteStabs(sec_code,objectoutput^.sectionsize(sec_code)-funcname^.address,
  252. nil,nidx,0,line,false)
  253. else
  254. begin
  255. if nidx=n_textline then
  256. sec:=sec_code
  257. else if nidx=n_dataline then
  258. sec:=sec_data
  259. else
  260. sec:=sec_bss;
  261. objectoutput^.WriteStabs(sec,objectoutput^.sectionsize(sec),
  262. nil,nidx,0,line,true);
  263. end;
  264. end;
  265. procedure ti386binasmlist.emitstabs(s:string);
  266. begin
  267. s:=s+#0;
  268. ConvertStabs(@s[1]);
  269. end;
  270. procedure ti386binasmlist.WriteFileLineInfo(var fileinfo : tfileposinfo);
  271. var
  272. curr_n : byte;
  273. hp : pasmsymbol;
  274. infile : pinputfile;
  275. begin
  276. if not ((cs_debuginfo in aktmoduleswitches) or
  277. (cs_gdb_lineinfo in aktglobalswitches)) then
  278. exit;
  279. { file changed ? (must be before line info) }
  280. if (fileinfo.fileindex<>0) and
  281. (stabslastfileinfo.fileindex<>fileinfo.fileindex) then
  282. begin
  283. infile:=current_module^.sourcefiles^.get_file(fileinfo.fileindex);
  284. if includecount=0 then
  285. curr_n:=n_sourcefile
  286. else
  287. curr_n:=n_includefile;
  288. { get symbol for this includefile }
  289. hp:=newasmsymbol('Ltext'+ToStr(IncludeCount));
  290. if currpass=1 then
  291. begin
  292. hp^.setbind(AB_LOCAL);
  293. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  294. end
  295. else
  296. objectoutput^.writesymbol(hp);
  297. { emit stabs }
  298. if (infile^.path^<>'') then
  299. EmitStabs('"'+lower(BsToSlash(FixPath(infile^.path^,false)))+'",'+tostr(curr_n)+
  300. ',0,0,Ltext'+ToStr(IncludeCount));
  301. EmitStabs('"'+lower(FixFileName(infile^.name^))+'",'+tostr(curr_n)+
  302. ',0,0,Ltext'+ToStr(IncludeCount));
  303. inc(includecount);
  304. end;
  305. { line changed ? }
  306. if (stabslastfileinfo.line<>fileinfo.line) and (fileinfo.line<>0) then
  307. emitlineinfostabs(n_line,fileinfo.line);
  308. stabslastfileinfo:=fileinfo;
  309. end;
  310. procedure ti386binasmlist.StartFileLineInfo;
  311. var
  312. fileinfo : tfileposinfo;
  313. begin
  314. FillChar(stabslastfileinfo,sizeof(stabslastfileinfo),0);
  315. n_line:=n_textline;
  316. funcname:=nil;
  317. linecount:=1;
  318. includecount:=0;
  319. fileinfo.fileindex:=1;
  320. fileinfo.line:=1;
  321. WriteFileLineInfo(fileinfo);
  322. end;
  323. procedure ti386binasmlist.EndFileLineInfo;
  324. var
  325. hp : pasmsymbol;
  326. store_sec : tsection;
  327. begin
  328. if not ((cs_debuginfo in aktmoduleswitches) or
  329. (cs_gdb_lineinfo in aktglobalswitches)) then
  330. exit;
  331. store_sec:=objectalloc^.currsec;
  332. objectalloc^.setsection(sec_code);
  333. hp:=newasmsymbol('Letext');
  334. if currpass=1 then
  335. begin
  336. hp^.setbind(AB_LOCAL);
  337. hp^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  338. end
  339. else
  340. objectoutput^.writesymbol(hp);
  341. EmitStabs('"",'+tostr(n_sourcefile)+',0,0,Letext');
  342. objectalloc^.setsection(store_sec);
  343. end;
  344. {$endif GDB}
  345. function ti386binasmlist.MaybeNextList(var hp:pai):boolean;
  346. begin
  347. { maybe end of list }
  348. while not assigned(hp) do
  349. begin
  350. if currlistidx<lists then
  351. begin
  352. inc(currlistidx);
  353. currlist:=list[currlistidx];
  354. hp:=pai(currlist^.first);
  355. end
  356. else
  357. begin
  358. MaybeNextList:=false;
  359. exit;
  360. end;
  361. end;
  362. MaybeNextList:=true;
  363. end;
  364. function ti386binasmlist.TreePass0(hp:pai):pai;
  365. var
  366. l : longint;
  367. begin
  368. while assigned(hp) do
  369. begin
  370. case hp^.typ of
  371. ait_align :
  372. begin
  373. { always use the maximum fillsize in this pass to avoid possible
  374. short jumps to become out of range }
  375. pai_align(hp)^.fillsize:=pai_align(hp)^.aligntype;
  376. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  377. end;
  378. ait_datablock :
  379. begin
  380. {$ifdef EXTERNALBSS}
  381. if not SmartAsm then
  382. begin
  383. if not pai_datablock(hp)^.is_global then
  384. begin
  385. l:=pai_datablock(hp)^.size;
  386. if l>2 then
  387. objectalloc^.sectionalign(4)
  388. else if l>1 then
  389. objectalloc^.sectionalign(2);
  390. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  391. end;
  392. end
  393. else
  394. begin
  395. {$endif}
  396. l:=pai_datablock(hp)^.size;
  397. if l>2 then
  398. objectalloc^.sectionalign(4)
  399. else if l>1 then
  400. objectalloc^.sectionalign(2);
  401. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  402. end;
  403. end;
  404. ait_const_32bit :
  405. objectalloc^.sectionalloc(4);
  406. ait_const_16bit :
  407. objectalloc^.sectionalloc(2);
  408. ait_const_8bit :
  409. objectalloc^.sectionalloc(1);
  410. ait_real_80bit :
  411. objectalloc^.sectionalloc(10);
  412. ait_real_64bit :
  413. objectalloc^.sectionalloc(8);
  414. ait_real_32bit :
  415. objectalloc^.sectionalloc(4);
  416. ait_comp_64bit :
  417. objectalloc^.sectionalloc(8);
  418. ait_const_rva,
  419. ait_const_symbol :
  420. objectalloc^.sectionalloc(4);
  421. ait_section:
  422. objectalloc^.setsection(pai_section(hp)^.sec);
  423. ait_symbol :
  424. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  425. ait_label :
  426. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  427. ait_string :
  428. objectalloc^.sectionalloc(pai_string(hp)^.len);
  429. ait_instruction :
  430. begin
  431. { reset instructions which could change in pass 2 }
  432. paicpu(hp)^.resetpass2;
  433. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  434. end;
  435. ait_cut :
  436. if SmartAsm then
  437. break;
  438. end;
  439. hp:=pai(hp^.next);
  440. end;
  441. TreePass0:=hp;
  442. end;
  443. function ti386binasmlist.TreePass1(hp:pai):pai;
  444. var
  445. l : longint;
  446. begin
  447. while assigned(hp) do
  448. begin
  449. {$ifdef GDB}
  450. { write stabs }
  451. if ((cs_debuginfo in aktmoduleswitches) or
  452. (cs_gdb_lineinfo in aktglobalswitches)) then
  453. begin
  454. if (objectalloc^.currsec<>sec_none) and
  455. not(hp^.typ in [
  456. ait_label,
  457. ait_regalloc,ait_tempalloc,
  458. ait_stabn,ait_stabs,ait_section,
  459. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  460. WriteFileLineInfo(hp^.fileinfo);
  461. end;
  462. {$endif GDB}
  463. case hp^.typ of
  464. ait_align :
  465. begin
  466. { here we must determine the fillsize which is used in pass2 }
  467. pai_align(hp)^.fillsize:=align(objectalloc^.sectionsize,pai_align(hp)^.aligntype)-
  468. objectalloc^.sectionsize;
  469. objectalloc^.sectionalloc(pai_align(hp)^.fillsize);
  470. end;
  471. ait_datablock :
  472. begin
  473. if objectalloc^.currsec<>sec_bss then
  474. Message(asmw_e_alloc_data_only_in_bss);
  475. {$ifdef EXTERNALBSS}
  476. if not SmartAsm then
  477. begin
  478. if pai_datablock(hp)^.is_global then
  479. begin
  480. pai_datablock(hp)^.sym^.setbind(AB_COMMON);
  481. pai_datablock(hp)^.sym^.setaddress(sec_none,pai_datablock(hp)^.size,pai_datablock(hp)^.size);
  482. end
  483. else
  484. begin
  485. l:=pai_datablock(hp)^.size;
  486. if l>2 then
  487. objectalloc^.sectionalign(4)
  488. else if l>1 then
  489. objectalloc^.sectionalign(2);
  490. pai_datablock(hp)^.sym^.setbind(AB_LOCAL);
  491. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,
  492. pai_datablock(hp)^.size);
  493. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  494. end;
  495. end
  496. else
  497. {$endif}
  498. begin
  499. if pai_datablock(hp)^.is_global then
  500. pai_datablock(hp)^.sym^.setbind(AB_GLOBAL)
  501. else
  502. pai_datablock(hp)^.sym^.setbind(AB_LOCAL);
  503. l:=pai_datablock(hp)^.size;
  504. if l>2 then
  505. objectalloc^.sectionalign(4)
  506. else if l>1 then
  507. objectalloc^.sectionalign(2);
  508. pai_datablock(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,pai_datablock(hp)^.size);
  509. objectalloc^.sectionalloc(pai_datablock(hp)^.size);
  510. end;
  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. objectalloc^.sectionalloc(4);
  529. ait_section:
  530. begin
  531. objectalloc^.setsection(pai_section(hp)^.sec);
  532. {$ifdef GDB}
  533. case pai_section(hp)^.sec of
  534. sec_code : n_line:=n_textline;
  535. sec_data : n_line:=n_dataline;
  536. sec_bss : n_line:=n_bssline;
  537. else
  538. n_line:=n_dataline;
  539. end;
  540. stabslastfileinfo.line:=-1;
  541. {$endif GDB}
  542. end;
  543. {$ifdef GDB}
  544. ait_stabn :
  545. convertstabs(pai_stabn(hp)^.str);
  546. ait_stabs :
  547. convertstabs(pai_stabs(hp)^.str);
  548. ait_stab_function_name :
  549. if assigned(pai_stab_function_name(hp)^.str) then
  550. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  551. else
  552. funcname:=nil;
  553. ait_force_line :
  554. stabslastfileinfo.line:=0;
  555. {$endif}
  556. ait_symbol :
  557. begin
  558. if pai_symbol(hp)^.is_global then
  559. pai_symbol(hp)^.sym^.setbind(AB_GLOBAL)
  560. else
  561. pai_symbol(hp)^.sym^.setbind(AB_LOCAL);
  562. pai_symbol(hp)^.sym^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  563. end;
  564. ait_symbol_end :
  565. begin
  566. if target_info.target=target_i386_linux then
  567. pai_symbol(hp)^.sym^.size:=objectalloc^.sectionsize-pai_symbol(hp)^.sym^.address;
  568. end;
  569. ait_label :
  570. begin
  571. if pai_label(hp)^.is_global then
  572. pai_label(hp)^.l^.setbind(AB_GLOBAL)
  573. else
  574. pai_label(hp)^.l^.setbind(AB_LOCAL);
  575. pai_label(hp)^.l^.setaddress(objectalloc^.currsec,objectalloc^.sectionsize,0);
  576. end;
  577. ait_string :
  578. objectalloc^.sectionalloc(pai_string(hp)^.len);
  579. ait_instruction :
  580. objectalloc^.sectionalloc(paicpu(hp)^.Pass1(objectalloc^.sectionsize));
  581. ait_direct :
  582. Message(asmw_f_direct_not_supported);
  583. ait_cut :
  584. if SmartAsm then
  585. break;
  586. end;
  587. hp:=pai(hp^.next);
  588. end;
  589. TreePass1:=hp;
  590. end;
  591. function ti386binasmlist.TreePass2(hp:pai):pai;
  592. var
  593. l : longint;
  594. {$ifdef I386}
  595. co : comp;
  596. {$endif I386}
  597. begin
  598. { main loop }
  599. while assigned(hp) do
  600. begin
  601. {$ifdef GDB}
  602. { write stabs }
  603. if ((cs_debuginfo in aktmoduleswitches) or
  604. (cs_gdb_lineinfo in aktglobalswitches)) then
  605. begin
  606. if (objectoutput^.currsec<>sec_none) and
  607. not(hp^.typ in [
  608. ait_label,
  609. ait_regalloc,ait_tempalloc,
  610. ait_stabn,ait_stabs,ait_section,
  611. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  612. WriteFileLineInfo(hp^.fileinfo);
  613. end;
  614. {$endif GDB}
  615. case hp^.typ of
  616. ait_align :
  617. objectoutput^.writebytes(pai_align(hp)^.getfillbuf^,pai_align(hp)^.fillsize);
  618. ait_section :
  619. begin
  620. objectoutput^.defaultsection(pai_section(hp)^.sec);
  621. {$ifdef GDB}
  622. case pai_section(hp)^.sec of
  623. sec_code : n_line:=n_textline;
  624. sec_data : n_line:=n_dataline;
  625. sec_bss : n_line:=n_bssline;
  626. else
  627. n_line:=n_dataline;
  628. end;
  629. stabslastfileinfo.line:=-1;
  630. {$endif GDB}
  631. end;
  632. ait_symbol :
  633. objectoutput^.writesymbol(pai_symbol(hp)^.sym);
  634. ait_datablock :
  635. begin
  636. objectoutput^.writesymbol(pai_datablock(hp)^.sym);
  637. if SmartAsm
  638. {$ifdef EXTERNALBSS}
  639. or (not pai_datablock(hp)^.is_global)
  640. {$endif}
  641. then
  642. begin
  643. l:=pai_datablock(hp)^.size;
  644. if l>2 then
  645. objectoutput^.writealign(4)
  646. else if l>1 then
  647. objectoutput^.writealign(2);
  648. objectoutput^.writealloc(pai_datablock(hp)^.size);
  649. end;
  650. end;
  651. ait_const_32bit :
  652. objectoutput^.writebytes(pai_const(hp)^.value,4);
  653. ait_const_16bit :
  654. objectoutput^.writebytes(pai_const(hp)^.value,2);
  655. ait_const_8bit :
  656. objectoutput^.writebytes(pai_const(hp)^.value,1);
  657. ait_real_80bit :
  658. objectoutput^.writebytes(pai_real_80bit(hp)^.value,10);
  659. ait_real_64bit :
  660. objectoutput^.writebytes(pai_real_64bit(hp)^.value,8);
  661. ait_real_32bit :
  662. objectoutput^.writebytes(pai_real_32bit(hp)^.value,4);
  663. ait_comp_64bit :
  664. begin
  665. {$ifdef FPC}
  666. co:=comp(pai_comp_64bit(hp)^.value);
  667. {$else}
  668. co:=pai_comp_64bit(hp)^.value;
  669. {$endif}
  670. objectoutput^.writebytes(co,8);
  671. end;
  672. ait_string :
  673. objectoutput^.writebytes(pai_string(hp)^.str^,pai_string(hp)^.len);
  674. ait_const_rva :
  675. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  676. pai_const_symbol(hp)^.sym,relative_rva);
  677. ait_const_symbol :
  678. objectoutput^.writereloc(pai_const_symbol(hp)^.offset,4,
  679. pai_const_symbol(hp)^.sym,relative_false);
  680. ait_label :
  681. objectoutput^.writesymbol(pai_label(hp)^.l);
  682. ait_instruction :
  683. paicpu(hp)^.Pass2;
  684. {$ifdef GDB}
  685. ait_stabn :
  686. convertstabs(pai_stabn(hp)^.str);
  687. ait_stabs :
  688. convertstabs(pai_stabs(hp)^.str);
  689. ait_stab_function_name :
  690. if assigned(pai_stab_function_name(hp)^.str) then
  691. funcname:=getasmsymbol(strpas(pai_stab_function_name(hp)^.str))
  692. else
  693. funcname:=nil;
  694. ait_force_line :
  695. stabslastfileinfo.line:=0;
  696. {$endif}
  697. ait_cut :
  698. if SmartAsm then
  699. break;
  700. end;
  701. hp:=pai(hp^.next);
  702. end;
  703. TreePass2:=hp;
  704. end;
  705. procedure ti386binasmlist.writetree;
  706. var
  707. hp : pai;
  708. begin
  709. objectalloc^.resetsections;
  710. objectalloc^.setsection(sec_code);
  711. objectoutput^.initwriting(cut_normal);
  712. objectoutput^.defaultsection(sec_code);
  713. { reset the asmsymbol list }
  714. ResetAsmsymbolList;
  715. objectoutput^.defaultsection(sec_code);
  716. {$ifdef MULTIPASS}
  717. { Pass 0 }
  718. currpass:=0;
  719. objectalloc^.setsection(sec_code);
  720. { start with list 1 }
  721. currlistidx:=1;
  722. currlist:=list[currlistidx];
  723. hp:=pai(currlist^.first);
  724. while assigned(hp) do
  725. begin
  726. hp:=TreePass0(hp);
  727. MaybeNextList(hp);
  728. end;
  729. { leave if errors have occured }
  730. if errorcount>0 then
  731. exit;
  732. {$endif}
  733. { Pass 1 }
  734. currpass:=1;
  735. objectalloc^.resetsections;
  736. objectalloc^.setsection(sec_code);
  737. {$ifdef GDB}
  738. StartFileLineInfo;
  739. {$endif GDB}
  740. { start with list 1 }
  741. currlistidx:=1;
  742. currlist:=list[currlistidx];
  743. hp:=pai(currlist^.first);
  744. while assigned(hp) do
  745. begin
  746. hp:=TreePass1(hp);
  747. MaybeNextList(hp);
  748. end;
  749. {$ifdef GDB}
  750. EndFileLineInfo;
  751. {$endif GDB}
  752. { check for undefined labels }
  753. CheckAsmSymbolListUndefined;
  754. { set section sizes }
  755. objectoutput^.setsectionsizes(objectalloc^.secsize);
  756. { leave if errors have occured }
  757. if errorcount>0 then
  758. exit;
  759. { Pass 2 }
  760. currpass:=2;
  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:=TreePass2(hp);
  771. MaybeNextList(hp);
  772. end;
  773. {$ifdef GDB}
  774. EndFileLineInfo;
  775. {$endif GDB}
  776. { leave if errors have occured }
  777. if errorcount>0 then
  778. exit;
  779. { write last objectfile }
  780. objectoutput^.donewriting;
  781. end;
  782. procedure ti386binasmlist.writetreesmart;
  783. var
  784. hp : pai;
  785. startsec : tsection;
  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. ResetAsmsymbolList;
  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. CheckAsmSymbolListUndefined;
  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. { end of lists? }
  844. if not MaybeNextList(hp) then
  845. break;
  846. { save section for next loop }
  847. { this leads to a problem if startsec is sec_none !! PM }
  848. startsec:=objectalloc^.currsec;
  849. { we will start a new objectfile so reset everything }
  850. if (hp^.typ=ait_cut) then
  851. objectoutput^.initwriting(pai_cut(hp)^.place)
  852. else
  853. objectoutput^.initwriting(cut_normal);
  854. { avoid empty files }
  855. while assigned(hp^.next) and
  856. (pai(hp^.next)^.typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  857. begin
  858. if pai(hp^.next)^.typ=ait_section then
  859. startsec:=pai_section(hp^.next)^.sec;
  860. hp:=pai(hp^.next);
  861. end;
  862. hp:=pai(hp^.next);
  863. { there is a problem if startsec is sec_none !! PM }
  864. if startsec=sec_none then
  865. startsec:=sec_code;
  866. if not MaybeNextList(hp) then
  867. break;
  868. end;
  869. end;
  870. procedure ti386binasmlist.writebin;
  871. procedure addlist(p:paasmoutput);
  872. begin
  873. inc(lists);
  874. list[lists]:=p;
  875. end;
  876. begin
  877. if cs_debuginfo in aktmoduleswitches then
  878. addlist(debuglist);
  879. addlist(codesegment);
  880. addlist(datasegment);
  881. addlist(consts);
  882. addlist(rttilist);
  883. if assigned(resourcestringlist) then
  884. addlist(resourcestringlist);
  885. addlist(bsssegment);
  886. if assigned(importssection) then
  887. addlist(importssection);
  888. if assigned(exportssection) and not UseDeffileForExport then
  889. addlist(exportssection);
  890. if assigned(resourcesection) then
  891. addlist(resourcesection);
  892. if SmartAsm then
  893. writetreesmart
  894. else
  895. writetree;
  896. end;
  897. constructor ti386binasmlist.init(t:togtype;smart:boolean);
  898. begin
  899. case t of
  900. og_none :
  901. Message(asmw_f_no_binary_writer_selected);
  902. og_dbg :
  903. objectoutput:=new(pdbgoutput,init(smart));
  904. og_coff :
  905. objectoutput:=new(pdjgppcoffoutput,init(smart));
  906. og_pecoff :
  907. objectoutput:=new(pwin32coffoutput,init(smart));
  908. og_elf :
  909. objectoutput:=new(pelf32output,init(smart));
  910. end;
  911. objectalloc:=new(pobjectalloc,init);
  912. SmartAsm:=smart;
  913. currpass:=0;
  914. end;
  915. destructor ti386binasmlist.done;
  916. begin
  917. dispose(objectoutput,done);
  918. dispose(objectalloc,done);
  919. end;
  920. end.
  921. {
  922. $Log$
  923. Revision 1.3 2000-07-13 12:08:24 michael
  924. + patched to 1.1.0 with former 1.09patch from peter
  925. Revision 1.2 2000/07/13 11:32:29 michael
  926. + removed logs
  927. }