ag386bin.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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,fmodule,finput,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. cutils,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.7 2000-08-27 16:11:49 peter
  972. * moved some util functions from globals,cobjects to cutils
  973. * splitted files into finput,fmodule
  974. Revision 1.6 2000/08/12 15:34:22 peter
  975. + usedasmsymbollist to check and reset only the used symbols (merged)
  976. Revision 1.5 2000/08/08 19:28:57 peter
  977. * memdebug/memory patches (merged)
  978. * only once illegal directive (merged)
  979. Revision 1.4 2000/08/04 22:00:50 peter
  980. * merges from fixes
  981. Revision 1.3 2000/07/13 12:08:24 michael
  982. + patched to 1.1.0 with former 1.09patch from peter
  983. Revision 1.2 2000/07/13 11:32:29 michael
  984. + removed logs
  985. }