fmodule.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit implements the first loading and searching of the modules
  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 fmodule;
  19. {$i fpcdefs.inc}
  20. {$ifdef go32v2}
  21. {$define shortasmprefix}
  22. {$endif}
  23. {$ifdef tos}
  24. {$define shortasmprefix}
  25. {$endif}
  26. {$ifdef OS2}
  27. { Allthough OS/2 supports long filenames I play it safe and
  28. use 8.3 filenames, because this allows the compiler to run
  29. on a FAT partition. (DM) }
  30. {$define shortasmprefix}
  31. {$endif}
  32. interface
  33. uses
  34. cutils,cclasses,
  35. globals,finput,
  36. symbase,symsym,aasmbase;
  37. type
  38. trecompile_reason = (rr_unknown,
  39. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  40. );
  41. TExternalsItem=class(TLinkedListItem)
  42. public
  43. found : longbool;
  44. data : pstring;
  45. constructor Create(const s:string);
  46. Destructor Destroy;override;
  47. end;
  48. tlinkcontaineritem=class(tlinkedlistitem)
  49. public
  50. data : pstring;
  51. needlink : cardinal;
  52. constructor Create(const s:string;m:cardinal);
  53. destructor Destroy;override;
  54. end;
  55. tlinkcontainer=class(tlinkedlist)
  56. procedure add(const s : string;m:cardinal);
  57. function get(var m:cardinal) : string;
  58. function getusemask(mask:cardinal) : string;
  59. function find(const s:string):boolean;
  60. end;
  61. tmodule = class;
  62. tused_unit = class;
  63. tunitmaprec = record
  64. u : tmodule;
  65. unitsym : tunitsym;
  66. end;
  67. punitmap = ^tunitmaprec;
  68. tmodule = class(tmodulebase)
  69. do_reload, { force reloading of the unit }
  70. do_compile, { need to compile the sources }
  71. sources_avail, { if all sources are reachable }
  72. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  73. is_stab_written,
  74. is_reset,
  75. is_unit,
  76. in_interface, { processing the implementation part? }
  77. in_global : boolean; { allow global settings }
  78. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  79. crc,
  80. interface_crc : cardinal;
  81. flags : cardinal; { the PPU flags }
  82. islibrary : boolean; { if it is a library (win32 dll) }
  83. map : punitmap; { mapping of all used units }
  84. mapsize : longint; { number of units in the map }
  85. derefdataintflen : longint;
  86. derefdata : tdynamicarray;
  87. globalsymtable, { pointer to the global symtable of this unit }
  88. localsymtable : tsymtable;{ pointer to the local symtable of this unit }
  89. scanner : pointer; { scanner object used }
  90. procinfo : pointer; { current procedure being compiled }
  91. loaded_from : tmodule;
  92. uses_imports : boolean; { Set if the module imports from DLL's.}
  93. imports : tlinkedlist;
  94. _exports : tlinkedlist;
  95. externals : tlinkedlist; {Only for DLL scanners by using Unix-style $LINKLIB }
  96. resourcefiles : tstringlist;
  97. linkunitofiles,
  98. linkunitstaticlibs,
  99. linkunitsharedlibs,
  100. linkotherofiles, { objects,libs loaded from the source }
  101. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  102. linkotherstaticlibs : tlinkcontainer;
  103. used_units : tlinkedlist;
  104. dependent_units : tlinkedlist;
  105. localunitsearchpath, { local searchpaths }
  106. localobjectsearchpath,
  107. localincludesearchpath,
  108. locallibrarysearchpath : TSearchPathList;
  109. asmprefix : pstring; { prefix for the smartlink asmfiles }
  110. librarydata : tasmlibrarydata; { librarydata for this module }
  111. {create creates a new module which name is stored in 's'. LoadedFrom
  112. points to the module calling it. It is nil for the first compiled
  113. module. This allow inheritence of all path lists. MUST pay attention
  114. to that when creating link.res!!!!(mazen)}
  115. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  116. destructor destroy;override;
  117. procedure reset;virtual;
  118. procedure adddependency(callermodule:tmodule);
  119. procedure flagdependent(callermodule:tmodule);
  120. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  121. procedure numberunits;
  122. procedure allunitsused;
  123. procedure setmodulename(const s:string);
  124. end;
  125. tused_unit = class(tlinkedlistitem)
  126. checksum,
  127. interface_checksum : cardinal;
  128. in_uses,
  129. in_interface : boolean;
  130. u : tmodule;
  131. unitsym : tunitsym;
  132. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  133. end;
  134. tdependent_unit = class(tlinkedlistitem)
  135. u : tmodule;
  136. constructor create(_u : tmodule);
  137. end;
  138. var
  139. main_module : tmodule; { Main module of the program }
  140. current_module : tmodule; { Current module which is compiled or loaded }
  141. compiled_module : tmodule; { Current module which is compiled }
  142. usedunits : tlinkedlist; { Used units for this program }
  143. loaded_units : tlinkedlist; { All loaded units }
  144. SmartLinkOFiles : TStringList; { List of .o files which are generated,
  145. used to delete them after linking }
  146. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  147. implementation
  148. uses
  149. {$ifdef delphi}
  150. dmisc,
  151. {$else}
  152. dos,
  153. {$endif}
  154. verbose,systems,
  155. scanner,
  156. procinfo;
  157. {*****************************************************************************
  158. Global Functions
  159. *****************************************************************************}
  160. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  161. var
  162. hp : tmodule;
  163. begin
  164. hp:=tmodule(loaded_units.first);
  165. while assigned(hp) and (hp.unit_index<>moduleindex) do
  166. hp:=tmodule(hp.next);
  167. if assigned(hp) then
  168. get_source_file:=hp.sourcefiles.get_file(fileindex)
  169. else
  170. get_source_file:=nil;
  171. end;
  172. {****************************************************************************
  173. TLinkContainerItem
  174. ****************************************************************************}
  175. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  176. begin
  177. inherited Create;
  178. data:=stringdup(s);
  179. needlink:=m;
  180. end;
  181. destructor TLinkContainerItem.Destroy;
  182. begin
  183. stringdispose(data);
  184. end;
  185. {****************************************************************************
  186. TLinkContainer
  187. ****************************************************************************}
  188. procedure TLinkContainer.add(const s : string;m:cardinal);
  189. begin
  190. inherited concat(TLinkContainerItem.Create(s,m));
  191. end;
  192. function TLinkContainer.get(var m:cardinal) : string;
  193. var
  194. p : tlinkcontaineritem;
  195. begin
  196. p:=tlinkcontaineritem(inherited getfirst);
  197. if p=nil then
  198. begin
  199. get:='';
  200. m:=0;
  201. end
  202. else
  203. begin
  204. get:=p.data^;
  205. m:=p.needlink;
  206. p.free;
  207. end;
  208. end;
  209. function TLinkContainer.getusemask(mask:cardinal) : string;
  210. var
  211. p : tlinkcontaineritem;
  212. found : boolean;
  213. begin
  214. found:=false;
  215. repeat
  216. p:=tlinkcontaineritem(inherited getfirst);
  217. if p=nil then
  218. begin
  219. getusemask:='';
  220. exit;
  221. end;
  222. getusemask:=p.data^;
  223. found:=(p.needlink and mask)<>0;
  224. p.free;
  225. until found;
  226. end;
  227. function TLinkContainer.find(const s:string):boolean;
  228. var
  229. newnode : tlinkcontaineritem;
  230. begin
  231. find:=false;
  232. newnode:=tlinkcontaineritem(First);
  233. while assigned(newnode) do
  234. begin
  235. if newnode.data^=s then
  236. begin
  237. find:=true;
  238. exit;
  239. end;
  240. newnode:=tlinkcontaineritem(newnode.next);
  241. end;
  242. end;
  243. {****************************************************************************
  244. TExternalsItem
  245. ****************************************************************************}
  246. constructor tExternalsItem.Create(const s:string);
  247. begin
  248. inherited Create;
  249. found:=false;
  250. data:=stringdup(s);
  251. end;
  252. destructor tExternalsItem.Destroy;
  253. begin
  254. stringdispose(data);
  255. inherited;
  256. end;
  257. {****************************************************************************
  258. TUSED_UNIT
  259. ****************************************************************************}
  260. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  261. begin
  262. u:=_u;
  263. in_interface:=intface;
  264. in_uses:=inuses;
  265. unitsym:=usym;
  266. if _u.state=ms_compiled then
  267. begin
  268. checksum:=u.crc;
  269. interface_checksum:=u.interface_crc;
  270. end
  271. else
  272. begin
  273. checksum:=0;
  274. interface_checksum:=0;
  275. end;
  276. end;
  277. {****************************************************************************
  278. TDENPENDENT_UNIT
  279. ****************************************************************************}
  280. constructor tdependent_unit.create(_u : tmodule);
  281. begin
  282. u:=_u;
  283. end;
  284. {****************************************************************************
  285. TMODULE
  286. ****************************************************************************}
  287. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  288. var
  289. p : dirstr;
  290. n : namestr;
  291. e : extstr;
  292. begin
  293. FSplit(s,p,n,e);
  294. { Programs have the name 'Program' to don't conflict with dup id's }
  295. if _is_unit then
  296. inherited create(n)
  297. else
  298. inherited create('Program');
  299. mainsource:=stringdup(s);
  300. { Dos has the famous 8.3 limit :( }
  301. {$ifdef shortasmprefix}
  302. asmprefix:=stringdup(FixFileName('as'));
  303. {$else}
  304. asmprefix:=stringdup(FixFileName(n));
  305. {$endif}
  306. setfilename(p+n,true);
  307. localunitsearchpath:=TSearchPathList.Create;
  308. localobjectsearchpath:=TSearchPathList.Create;
  309. localincludesearchpath:=TSearchPathList.Create;
  310. locallibrarysearchpath:=TSearchPathList.Create;
  311. used_units:=TLinkedList.Create;
  312. dependent_units:=TLinkedList.Create;
  313. resourcefiles:=TStringList.Create;
  314. linkunitofiles:=TLinkContainer.Create;
  315. linkunitstaticlibs:=TLinkContainer.Create;
  316. linkunitsharedlibs:=TLinkContainer.Create;
  317. linkotherofiles:=TLinkContainer.Create;
  318. linkotherstaticlibs:=TLinkContainer.Create;
  319. linkothersharedlibs:=TLinkContainer.Create;
  320. crc:=0;
  321. interface_crc:=0;
  322. flags:=0;
  323. scanner:=nil;
  324. map:=nil;
  325. mapsize:=0;
  326. derefdata:=TDynamicArray.Create(1024);
  327. derefdataintflen:=0;
  328. globalsymtable:=nil;
  329. localsymtable:=nil;
  330. loaded_from:=LoadedFrom;
  331. do_reload:=false;
  332. do_compile:=false;
  333. sources_avail:=true;
  334. recompile_reason:=rr_unknown;
  335. in_interface:=true;
  336. in_global:=true;
  337. is_unit:=_is_unit;
  338. islibrary:=false;
  339. is_stab_written:=false;
  340. is_reset:=false;
  341. uses_imports:=false;
  342. imports:=TLinkedList.Create;
  343. _exports:=TLinkedList.Create;
  344. externals:=TLinkedList.Create;
  345. librarydata:=tasmlibrarydata.create(realmodulename^);
  346. end;
  347. destructor tmodule.Destroy;
  348. var
  349. {$ifdef MEMDEBUG}
  350. d : tmemdebug;
  351. {$endif}
  352. hpi : tprocinfo;
  353. begin
  354. dispose(map);
  355. if assigned(imports) then
  356. imports.free;
  357. if assigned(_exports) then
  358. _exports.free;
  359. if assigned(externals) then
  360. externals.free;
  361. if assigned(scanner) then
  362. begin
  363. { also update current_scanner if it was pointing
  364. to this module }
  365. if current_scanner=tscannerfile(scanner) then
  366. current_scanner:=nil;
  367. tscannerfile(scanner).free;
  368. end;
  369. if assigned(procinfo) then
  370. begin
  371. if current_procinfo=tprocinfo(procinfo) then
  372. current_procinfo:=nil;
  373. { release procinfo tree }
  374. while assigned(procinfo) do
  375. begin
  376. hpi:=tprocinfo(procinfo).parent;
  377. tprocinfo(procinfo).free;
  378. procinfo:=hpi;
  379. end;
  380. end;
  381. used_units.free;
  382. dependent_units.free;
  383. resourcefiles.Free;
  384. linkunitofiles.Free;
  385. linkunitstaticlibs.Free;
  386. linkunitsharedlibs.Free;
  387. linkotherofiles.Free;
  388. linkotherstaticlibs.Free;
  389. linkothersharedlibs.Free;
  390. stringdispose(objfilename);
  391. stringdispose(newfilename);
  392. stringdispose(ppufilename);
  393. stringdispose(staticlibfilename);
  394. stringdispose(sharedlibfilename);
  395. stringdispose(exefilename);
  396. stringdispose(outputpath);
  397. stringdispose(path);
  398. stringdispose(realmodulename);
  399. stringdispose(mainsource);
  400. stringdispose(asmprefix);
  401. localunitsearchpath.Free;
  402. localobjectsearchpath.free;
  403. localincludesearchpath.free;
  404. locallibrarysearchpath.free;
  405. {$ifdef MEMDEBUG}
  406. d:=tmemdebug.create(modulename^+' - symtable');
  407. {$endif}
  408. derefdata.free;
  409. if assigned(globalsymtable) then
  410. globalsymtable.free;
  411. if assigned(localsymtable) then
  412. localsymtable.free;
  413. {$ifdef MEMDEBUG}
  414. d.free;
  415. {$endif}
  416. {$ifdef MEMDEBUG}
  417. d:=tmemdebug.create(modulename^+' - librarydata');
  418. {$endif}
  419. librarydata.free;
  420. {$ifdef MEMDEBUG}
  421. d.free;
  422. {$endif}
  423. stringdispose(modulename);
  424. inherited Destroy;
  425. end;
  426. procedure tmodule.reset;
  427. var
  428. hpi : tprocinfo;
  429. begin
  430. if assigned(scanner) then
  431. begin
  432. { also update current_scanner if it was pointing
  433. to this module }
  434. if current_scanner=tscannerfile(scanner) then
  435. current_scanner:=nil;
  436. tscannerfile(scanner).free;
  437. scanner:=nil;
  438. end;
  439. if assigned(procinfo) then
  440. begin
  441. if current_procinfo=tprocinfo(procinfo) then
  442. current_procinfo:=nil;
  443. { release procinfo tree }
  444. while assigned(procinfo) do
  445. begin
  446. hpi:=tprocinfo(procinfo).parent;
  447. tprocinfo(procinfo).free;
  448. procinfo:=hpi;
  449. end;
  450. end;
  451. if assigned(globalsymtable) then
  452. begin
  453. globalsymtable.free;
  454. globalsymtable:=nil;
  455. end;
  456. if assigned(localsymtable) then
  457. begin
  458. localsymtable.free;
  459. localsymtable:=nil;
  460. end;
  461. derefdata.free;
  462. derefdata:=TDynamicArray.Create(1024);
  463. if assigned(map) then
  464. begin
  465. freemem(map);
  466. map:=nil;
  467. end;
  468. derefdataintflen:=0;
  469. mapsize:=0;
  470. sourcefiles.free;
  471. sourcefiles:=tinputfilemanager.create;
  472. librarydata.free;
  473. librarydata:=tasmlibrarydata.create(realmodulename^);
  474. imports.free;
  475. imports:=tlinkedlist.create;
  476. _exports.free;
  477. _exports:=tlinkedlist.create;
  478. externals.free;
  479. externals:=tlinkedlist.create;
  480. used_units.free;
  481. used_units:=TLinkedList.Create;
  482. dependent_units.free;
  483. dependent_units:=TLinkedList.Create;
  484. resourcefiles.Free;
  485. resourcefiles:=TStringList.Create;
  486. linkunitofiles.Free;
  487. linkunitofiles:=TLinkContainer.Create;
  488. linkunitstaticlibs.Free;
  489. linkunitstaticlibs:=TLinkContainer.Create;
  490. linkunitsharedlibs.Free;
  491. linkunitsharedlibs:=TLinkContainer.Create;
  492. linkotherofiles.Free;
  493. linkotherofiles:=TLinkContainer.Create;
  494. linkotherstaticlibs.Free;
  495. linkotherstaticlibs:=TLinkContainer.Create;
  496. linkothersharedlibs.Free;
  497. linkothersharedlibs:=TLinkContainer.Create;
  498. uses_imports:=false;
  499. do_compile:=false;
  500. do_reload:=false;
  501. interface_compiled:=false;
  502. in_interface:=true;
  503. in_global:=true;
  504. is_stab_written:=false;
  505. is_reset:=false;
  506. crc:=0;
  507. interface_crc:=0;
  508. flags:=0;
  509. recompile_reason:=rr_unknown;
  510. {
  511. The following fields should not
  512. be reset:
  513. mainsource
  514. state
  515. loaded_from
  516. sources_avail
  517. }
  518. end;
  519. procedure tmodule.adddependency(callermodule:tmodule);
  520. begin
  521. { This is not needed for programs }
  522. if not callermodule.is_unit then
  523. exit;
  524. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  525. dependent_units.concat(tdependent_unit.create(callermodule));
  526. end;
  527. procedure tmodule.flagdependent(callermodule:tmodule);
  528. var
  529. pm : tdependent_unit;
  530. begin
  531. { flag all units that depend on this unit for reloading }
  532. pm:=tdependent_unit(current_module.dependent_units.first);
  533. while assigned(pm) do
  534. begin
  535. { We do not have to reload the unit that wants to load
  536. this unit, unless this unit is already compiled during
  537. the loading }
  538. if (pm.u=callermodule) and
  539. (pm.u.state<>ms_compiled) then
  540. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  541. else
  542. if pm.u.state=ms_second_compile then
  543. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  544. else
  545. begin
  546. pm.u.do_reload:=true;
  547. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  548. end;
  549. pm:=tdependent_unit(pm.next);
  550. end;
  551. end;
  552. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  553. var
  554. pu : tused_unit;
  555. begin
  556. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  557. used_units.concat(pu);
  558. addusedunit:=pu;
  559. end;
  560. procedure tmodule.numberunits;
  561. var
  562. pu : tused_unit;
  563. hp : tmodule;
  564. i : integer;
  565. begin
  566. { Reset all numbers to -1 }
  567. hp:=tmodule(loaded_units.first);
  568. while assigned(hp) do
  569. begin
  570. if assigned(hp.globalsymtable) then
  571. hp.globalsymtable.unitid:=$ffff;
  572. hp:=tmodule(hp.next);
  573. end;
  574. { Allocate map }
  575. mapsize:=used_units.count+1;
  576. reallocmem(map,mapsize*sizeof(tunitmaprec));
  577. { Our own symtable gets unitid 0, for a program there is
  578. no globalsymtable }
  579. if assigned(globalsymtable) then
  580. globalsymtable.unitid:=0;
  581. map[0].u:=self;
  582. map[0].unitsym:=nil;
  583. { number units and map }
  584. i:=1;
  585. pu:=tused_unit(used_units.first);
  586. while assigned(pu) do
  587. begin
  588. if assigned(pu.u.globalsymtable) then
  589. begin
  590. tsymtable(pu.u.globalsymtable).unitid:=i;
  591. map[i].u:=pu.u;
  592. map[i].unitsym:=pu.unitsym;
  593. inc(i);
  594. end;
  595. pu:=tused_unit(pu.next);
  596. end;
  597. end;
  598. procedure tmodule.allunitsused;
  599. var
  600. i : longint;
  601. begin
  602. for i:=0 to mapsize-1 do
  603. begin
  604. if assigned(map[i].unitsym) and
  605. (map[i].unitsym.refs=0) then
  606. MessagePos2(map[i].unitsym.fileinfo,sym_n_unit_not_used,map[i].u.modulename^,modulename^);
  607. end;
  608. end;
  609. procedure tmodule.setmodulename(const s:string);
  610. begin
  611. stringdispose(modulename);
  612. stringdispose(realmodulename);
  613. modulename:=stringdup(upper(s));
  614. realmodulename:=stringdup(s);
  615. { also update asmlibrary names }
  616. librarydata.name:=modulename^;
  617. librarydata.realname:=realmodulename^;
  618. end;
  619. end.
  620. {
  621. $Log$
  622. Revision 1.45 2004-06-20 08:55:29 florian
  623. * logs truncated
  624. Revision 1.44 2004/03/08 22:07:46 peter
  625. * stabs updates to write stabs for def for all implictly used
  626. units
  627. }