ag386bin.pas 33 KB

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