ag386bin.pas 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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. cobjects,
  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 : pasmsymbol;
  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 : pasmsymbol;
  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 : pasmsymbol;
  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 : pasmsymbol;
  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. UsedAsmSymbolListInsert(sym);
  575. end;
  576. end;
  577. end;
  578. end;
  579. end;
  580. ait_direct :
  581. Message(asmw_f_direct_not_supported);
  582. ait_cut :
  583. if SmartAsm then
  584. break;
  585. end;
  586. hp:=Tai(hp.next);
  587. end;
  588. TreePass1:=hp;
  589. end;
  590. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  591. var
  592. l : longint;
  593. {$ifdef I386}
  594. co : comp;
  595. {$endif I386}
  596. begin
  597. { main loop }
  598. while assigned(hp) do
  599. begin
  600. {$ifdef GDB}
  601. { write stabs }
  602. if ((cs_debuginfo in aktmoduleswitches) or
  603. (cs_gdb_lineinfo in aktglobalswitches)) then
  604. begin
  605. if (objectdata.currsec<>sec_none) and
  606. not(hp.typ in [
  607. ait_label,
  608. ait_regalloc,ait_tempalloc,
  609. ait_stabn,ait_stabs,ait_section,
  610. ait_cut,ait_marker,ait_align,ait_stab_function_name]) then
  611. WriteFileLineInfo(hp.fileinfo);
  612. end;
  613. {$endif GDB}
  614. case hp.typ of
  615. ait_align :
  616. objectdata.writebytes(Tai_align(hp).getfillbuf^,Tai_align(hp).fillsize);
  617. ait_section :
  618. begin
  619. objectdata.defaultsection(Tai_section(hp).sec);
  620. {$ifdef GDB}
  621. case Tai_section(hp).sec of
  622. sec_code : n_line:=n_textline;
  623. sec_data : n_line:=n_dataline;
  624. sec_bss : n_line:=n_bssline;
  625. else
  626. n_line:=n_dataline;
  627. end;
  628. stabslastfileinfo.line:=-1;
  629. {$endif GDB}
  630. end;
  631. ait_symbol :
  632. begin
  633. objectdata.writesymbol(Tai_symbol(hp).sym);
  634. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  635. end;
  636. ait_datablock :
  637. begin
  638. objectdata.writesymbol(Tai_datablock(hp).sym);
  639. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  640. if SmartAsm or (not Tai_datablock(hp).is_global) then
  641. begin
  642. l:=Tai_datablock(hp).size;
  643. if l>2 then
  644. objectdata.allocalign(4)
  645. else if l>1 then
  646. objectdata.allocalign(2);
  647. objectdata.alloc(Tai_datablock(hp).size);
  648. end;
  649. end;
  650. ait_const_32bit :
  651. objectdata.writebytes(Tai_const(hp).value,4);
  652. ait_const_16bit :
  653. objectdata.writebytes(Tai_const(hp).value,2);
  654. ait_const_8bit :
  655. objectdata.writebytes(Tai_const(hp).value,1);
  656. ait_real_80bit :
  657. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  658. ait_real_64bit :
  659. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  660. ait_real_32bit :
  661. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  662. ait_comp_64bit :
  663. begin
  664. {$ifdef FPC}
  665. co:=comp(Tai_comp_64bit(hp).value);
  666. {$else}
  667. co:=Tai_comp_64bit(hp).value;
  668. {$endif}
  669. objectdata.writebytes(co,8);
  670. end;
  671. ait_string :
  672. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  673. ait_const_rva :
  674. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  675. Tai_const_symbol(hp).sym,relative_rva);
  676. ait_const_symbol :
  677. objectdata.writereloc(Tai_const_symbol(hp).offset,4,
  678. Tai_const_symbol(hp).sym,relative_false);
  679. ait_label :
  680. begin
  681. objectdata.writesymbol(Tai_label(hp).l);
  682. { exporting shouldn't be necessary as labels are local,
  683. but it's better to be on the safe side (PFV) }
  684. objectoutput.exportsymbol(Tai_label(hp).l);
  685. end;
  686. ait_instruction :
  687. Taicpu(hp).Pass2;
  688. {$ifdef GDB}
  689. ait_stabn :
  690. convertstabs(Tai_stabn(hp).str);
  691. ait_stabs :
  692. convertstabs(Tai_stabs(hp).str);
  693. ait_stab_function_name :
  694. if assigned(Tai_stab_function_name(hp).str) then
  695. funcname:=getasmsymbol(strpas(Tai_stab_function_name(hp).str))
  696. else
  697. funcname:=nil;
  698. ait_force_line :
  699. stabslastfileinfo.line:=0;
  700. {$endif}
  701. ait_cut :
  702. if SmartAsm then
  703. break;
  704. end;
  705. hp:=Tai(hp.next);
  706. end;
  707. TreePass2:=hp;
  708. end;
  709. procedure TInternalAssembler.writetree;
  710. var
  711. hp : Tai;
  712. label
  713. doexit;
  714. begin
  715. objectalloc.resetsections;
  716. objectalloc.setsection(sec_code);
  717. objectoutput.initwriting(ObjFile);
  718. objectdata:=objectoutput.data;
  719. objectdata.defaultsection(sec_code);
  720. { reset the asmsymbol list }
  721. CreateUsedAsmsymbolList;
  722. {$ifdef MULTIPASS}
  723. { Pass 0 }
  724. currpass:=0;
  725. objectalloc.setsection(sec_code);
  726. { start with list 1 }
  727. currlistidx:=1;
  728. currlist:=list[currlistidx];
  729. hp:=Tai(currList.first);
  730. while assigned(hp) do
  731. begin
  732. hp:=TreePass0(hp);
  733. MaybeNextList(hp);
  734. end;
  735. { leave if errors have occured }
  736. if errorcount>0 then
  737. goto doexit;
  738. {$endif}
  739. { Pass 1 }
  740. currpass:=1;
  741. objectalloc.resetsections;
  742. objectalloc.setsection(sec_code);
  743. {$ifdef GDB}
  744. StartFileLineInfo;
  745. {$endif GDB}
  746. { start with list 1 }
  747. currlistidx:=1;
  748. currlist:=list[currlistidx];
  749. hp:=Tai(currList.first);
  750. while assigned(hp) do
  751. begin
  752. hp:=TreePass1(hp);
  753. MaybeNextList(hp);
  754. end;
  755. {$ifdef GDB}
  756. EndFileLineInfo;
  757. {$endif GDB}
  758. { check for undefined labels and reset }
  759. UsedAsmSymbolListCheckUndefined;
  760. { set section sizes }
  761. objectdata.setsectionsizes(objectalloc.secsize);
  762. { leave if errors have occured }
  763. if errorcount>0 then
  764. goto doexit;
  765. { Pass 2 }
  766. currpass:=2;
  767. {$ifdef GDB}
  768. StartFileLineInfo;
  769. {$endif GDB}
  770. { start with list 1 }
  771. currlistidx:=1;
  772. currlist:=list[currlistidx];
  773. hp:=Tai(currList.first);
  774. while assigned(hp) do
  775. begin
  776. hp:=TreePass2(hp);
  777. MaybeNextList(hp);
  778. end;
  779. {$ifdef GDB}
  780. EndFileLineInfo;
  781. {$endif GDB}
  782. { leave if errors have occured }
  783. if errorcount>0 then
  784. goto doexit;
  785. { write last objectfile }
  786. objectoutput.donewriting;
  787. objectdata:=nil;
  788. doexit:
  789. { reset the used symbols back, must be after the .o has been
  790. written }
  791. UsedAsmsymbolListReset;
  792. DestroyUsedAsmsymbolList;
  793. end;
  794. procedure TInternalAssembler.writetreesmart;
  795. var
  796. hp : Tai;
  797. startsec : tsection;
  798. place: tcutplace;
  799. begin
  800. objectalloc.resetsections;
  801. objectalloc.setsection(sec_code);
  802. NextSmartName(cut_normal);
  803. objectoutput.initwriting(ObjFile);
  804. objectdata:=objectoutput.data;
  805. objectdata.defaultsection(sec_code);
  806. startsec:=sec_code;
  807. { start with list 1 }
  808. currlistidx:=1;
  809. currlist:=list[currlistidx];
  810. hp:=Tai(currList.first);
  811. while assigned(hp) do
  812. begin
  813. { reset the asmsymbol list }
  814. CreateUsedAsmSymbolList;
  815. {$ifdef MULTIPASS}
  816. { Pass 0 }
  817. currpass:=0;
  818. objectalloc.resetsections;
  819. objectalloc.setsection(startsec);
  820. TreePass0(hp);
  821. { leave if errors have occured }
  822. if errorcount>0 then
  823. exit;
  824. {$endif MULTIPASS}
  825. { Pass 1 }
  826. currpass:=1;
  827. objectalloc.resetsections;
  828. objectalloc.setsection(startsec);
  829. {$ifdef GDB}
  830. StartFileLineInfo;
  831. {$endif GDB}
  832. TreePass1(hp);
  833. {$ifdef GDB}
  834. EndFileLineInfo;
  835. {$endif GDB}
  836. { check for undefined labels }
  837. UsedAsmSymbolListCheckUndefined;
  838. { set section sizes }
  839. objectdata.setsectionsizes(objectalloc.secsize);
  840. { leave if errors have occured }
  841. if errorcount>0 then
  842. exit;
  843. { Pass 2 }
  844. currpass:=2;
  845. objectdata.defaultsection(startsec);
  846. {$ifdef GDB}
  847. StartFileLineInfo;
  848. {$endif GDB}
  849. hp:=TreePass2(hp);
  850. {$ifdef GDB}
  851. EndFileLineInfo;
  852. {$endif GDB}
  853. { leave if errors have occured }
  854. if errorcount>0 then
  855. exit;
  856. { if not end then write the current objectfile }
  857. objectoutput.donewriting;
  858. objectdata:=nil;
  859. { reset the used symbols back, must be after the .o has been
  860. written }
  861. UsedAsmsymbolListReset;
  862. DestroyUsedAsmsymbolList;
  863. { end of lists? }
  864. if not MaybeNextList(hp) then
  865. break;
  866. { save section for next loop }
  867. { this leads to a problem if startsec is sec_none !! PM }
  868. startsec:=objectalloc.currsec;
  869. { we will start a new objectfile so reset everything }
  870. { The place can still change in the next while loop, so don't init }
  871. { the writer yet (JM) }
  872. if (hp.typ=ait_cut) then
  873. place := Tai_cut(hp).place
  874. else
  875. place := cut_normal;
  876. { avoid empty files }
  877. while assigned(hp.next) and
  878. (Tai(hp.next).typ in [ait_marker,ait_comment,ait_section,ait_cut]) do
  879. begin
  880. if Tai(hp.next).typ=ait_section then
  881. startsec:=Tai_section(hp.next).sec
  882. else if (Tai(hp.next).typ=ait_cut) then
  883. place := Tai_cut(hp).place;
  884. hp:=Tai(hp.next);
  885. end;
  886. NextSmartName(place);
  887. objectoutput.initwriting(ObjFile);
  888. objectdata:=objectoutput.data;
  889. hp:=Tai(hp.next);
  890. { there is a problem if startsec is sec_none !! PM }
  891. if startsec=sec_none then
  892. startsec:=sec_code;
  893. if not MaybeNextList(hp) then
  894. break;
  895. end;
  896. end;
  897. procedure TInternalAssembler.writebin;
  898. procedure addlist(p:TAAsmoutput);
  899. begin
  900. inc(lists);
  901. list[lists]:=p;
  902. end;
  903. begin
  904. if cs_debuginfo in aktmoduleswitches then
  905. addlist(debuglist);
  906. addlist(codesegment);
  907. addlist(datasegment);
  908. addlist(consts);
  909. addlist(rttilist);
  910. if assigned(resourcestringlist) then
  911. addlist(resourcestringlist);
  912. addlist(bsssegment);
  913. if assigned(importssection) then
  914. addlist(importssection);
  915. if assigned(exportssection) and not UseDeffileForExport then
  916. addlist(exportssection);
  917. if assigned(resourcesection) then
  918. addlist(resourcesection);
  919. if SmartAsm then
  920. writetreesmart
  921. else
  922. writetree;
  923. end;
  924. constructor TInternalAssembler.create(t:togtype;smart:boolean);
  925. begin
  926. inherited create(smart);
  927. case t of
  928. og_none :
  929. Message(asmw_f_no_binary_writer_selected);
  930. og_coff :
  931. objectoutput:=tcoffobjectoutput.createdjgpp(smart);
  932. og_pecoff :
  933. objectoutput:=tcoffobjectoutput.createwin32(smart);
  934. og_elf :
  935. objectoutput:=telf32objectoutput.create(smart);
  936. else
  937. internalerror(43243432);
  938. end;
  939. objectalloc:=tobjectalloc.create;
  940. SmartAsm:=smart;
  941. currpass:=0;
  942. end;
  943. destructor TInternalAssembler.destroy;
  944. {$ifdef MEMDEBUG}
  945. var
  946. d : tmemdebug;
  947. {$endif}
  948. begin
  949. {$ifdef MEMDEBUG}
  950. d.init('agbin');
  951. {$endif}
  952. objectoutput.free;
  953. objectalloc.free;
  954. {$ifdef MEMDEBUG}
  955. d.done;
  956. {$endif}
  957. end;
  958. end.
  959. {
  960. $Log$
  961. Revision 1.5 2001-03-05 21:39:11 peter
  962. * changed to class with common TAssembler also for internal assembler
  963. Revision 1.4 2000/12/25 00:07:31 peter
  964. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  965. tlinkedlist objects)
  966. Revision 1.3 2000/12/23 19:59:35 peter
  967. * object to class for ow/og objects
  968. * split objectdata from objectoutput
  969. Revision 1.2 2000/12/12 19:50:21 peter
  970. * clear usedasmsymbol at exit of writetree
  971. Revision 1.1 2000/11/30 22:18:48 florian
  972. * moved to i386
  973. Revision 1.9 2000/11/12 22:20:37 peter
  974. * create generic toutputsection for binary writers
  975. Revision 1.8 2000/09/24 15:06:10 peter
  976. * use defines.inc
  977. Revision 1.7 2000/08/27 16:11:49 peter
  978. * moved some util functions from globals,cobjects to cutils
  979. * splitted files into finput,fmodule
  980. Revision 1.6 2000/08/12 15:34:22 peter
  981. + usedasmsymbollist to check and reset only the used symbols (merged)
  982. Revision 1.5 2000/08/08 19:28:57 peter
  983. * memdebug/memory patches (merged)
  984. * only once illegal directive (merged)
  985. Revision 1.4 2000/08/04 22:00:50 peter
  986. * merges from fixes
  987. Revision 1.3 2000/07/13 12:08:24 michael
  988. + patched to 1.1.0 with former 1.09patch from peter
  989. Revision 1.2 2000/07/13 11:32:29 michael
  990. + removed logs
  991. }