fmodule.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 OS2}
  24. { Allthough OS/2 supports long filenames I play it safe and
  25. use 8.3 filenames, because this allows the compiler to run
  26. on a FAT partition. (DM) }
  27. {$define shortasmprefix}
  28. {$endif}
  29. interface
  30. uses
  31. cutils,cclasses,
  32. globals,finput,
  33. symbase,aasmbase;
  34. const
  35. maxunits = 1024;
  36. type
  37. trecompile_reason = (rr_unknown,
  38. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  39. );
  40. TExternalsItem=class(TLinkedListItem)
  41. public
  42. found : longbool;
  43. data : pstring;
  44. constructor Create(const s:string);
  45. Destructor Destroy;override;
  46. end;
  47. tlinkcontaineritem=class(tlinkedlistitem)
  48. public
  49. data : pstring;
  50. needlink : cardinal;
  51. constructor Create(const s:string;m:cardinal);
  52. destructor Destroy;override;
  53. end;
  54. tlinkcontainer=class(tlinkedlist)
  55. procedure add(const s : string;m:cardinal);
  56. function get(var m:cardinal) : string;
  57. function getusemask(mask:cardinal) : string;
  58. function find(const s:string):boolean;
  59. end;
  60. {$ifndef NEWMAP}
  61. tunitmap = array[0..maxunits-1] of pointer;
  62. punitmap = ^tunitmap;
  63. {$else NEWMAP}
  64. tunitmap = array[0..maxunits-1] of tmodule;
  65. punitmap = ^tunitmap;
  66. {$endif NEWMAP}
  67. tmodule = class(tmodulebase)
  68. compiled, { unit is already compiled }
  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. sources_checked, { if there is already done a check for the sources }
  73. is_unit,
  74. in_second_compile, { is this unit being compiled for the 2nd time? }
  75. in_second_load, { is this unit PPU loaded a 2nd time? }
  76. in_implementation, { 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. unitcount : longint; { local unit counter }
  85. globalsymtable, { pointer to the global symtable of this unit }
  86. localsymtable : tsymtable;{ pointer to the local symtable of this unit }
  87. scanner : pointer; { scanner object used }
  88. loaded_from : tmodule;
  89. uses_imports : boolean; { Set if the module imports from DLL's.}
  90. imports : tlinkedlist;
  91. _exports : tlinkedlist;
  92. externals : tlinkedlist; {Only for DLL scanners by using Unix-style $LINKLIB }
  93. resourcefiles : tstringlist;
  94. linkunitofiles,
  95. linkunitstaticlibs,
  96. linkunitsharedlibs,
  97. linkotherofiles, { objects,libs loaded from the source }
  98. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  99. linkotherstaticlibs : tlinkcontainer;
  100. used_units : tlinkedlist;
  101. dependent_units : tlinkedlist;
  102. localunitsearchpath, { local searchpaths }
  103. localobjectsearchpath,
  104. localincludesearchpath,
  105. locallibrarysearchpath : TSearchPathList;
  106. asmprefix : pstring; { prefix for the smartlink asmfiles }
  107. librarydata : tasmlibrarydata; { librarydata for this module }
  108. {create creates a new module which name is stored in 's'. LoadedFrom
  109. points to the module calling it. It is nil for the first compiled
  110. module. This allow inheritence of all path lists. MUST pay attention
  111. to that when creating link.res!!!!(mazen)}
  112. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  113. destructor destroy;override;
  114. procedure reset;virtual;
  115. procedure numberunits;
  116. end;
  117. tused_unit = class(tlinkedlistitem)
  118. unitid : longint;
  119. name : pstring;
  120. realname : pstring;
  121. checksum,
  122. interface_checksum : cardinal;
  123. loaded : boolean;
  124. in_uses,
  125. in_interface,
  126. is_stab_written : boolean;
  127. u : tmodule;
  128. constructor create(_u : tmodule;intface:boolean);
  129. constructor create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  130. destructor destroy;override;
  131. end;
  132. tdependent_unit = class(tlinkedlistitem)
  133. u : tmodule;
  134. constructor create(_u : tmodule);
  135. end;
  136. var
  137. main_module : tmodule; { Main module of the program }
  138. current_module : tmodule; { Current module which is compiled or loaded }
  139. compiled_module : tmodule; { Current module which is compiled }
  140. usedunits : tlinkedlist; { Used units for this program }
  141. loaded_units : tlinkedlist; { All loaded units }
  142. SmartLinkOFiles : TStringList; { List of .o files which are generated,
  143. used to delete them after linking }
  144. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  145. implementation
  146. uses
  147. {$ifdef delphi}
  148. dmisc,
  149. {$else}
  150. dos,
  151. {$endif}
  152. verbose,systems,
  153. scanner;
  154. {*****************************************************************************
  155. Global Functions
  156. *****************************************************************************}
  157. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  158. var
  159. hp : tmodule;
  160. begin
  161. hp:=tmodule(loaded_units.first);
  162. while assigned(hp) and (hp.unit_index<>moduleindex) do
  163. hp:=tmodule(hp.next);
  164. if assigned(hp) then
  165. get_source_file:=hp.sourcefiles.get_file(fileindex)
  166. else
  167. get_source_file:=nil;
  168. end;
  169. {****************************************************************************
  170. TLinkContainerItem
  171. ****************************************************************************}
  172. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  173. begin
  174. inherited Create;
  175. data:=stringdup(s);
  176. needlink:=m;
  177. end;
  178. destructor TLinkContainerItem.Destroy;
  179. begin
  180. stringdispose(data);
  181. end;
  182. {****************************************************************************
  183. TLinkContainer
  184. ****************************************************************************}
  185. procedure TLinkContainer.add(const s : string;m:cardinal);
  186. begin
  187. inherited concat(TLinkContainerItem.Create(s,m));
  188. end;
  189. function TLinkContainer.get(var m:cardinal) : string;
  190. var
  191. p : tlinkcontaineritem;
  192. begin
  193. p:=tlinkcontaineritem(inherited getfirst);
  194. if p=nil then
  195. begin
  196. get:='';
  197. m:=0;
  198. end
  199. else
  200. begin
  201. get:=p.data^;
  202. m:=p.needlink;
  203. p.free;
  204. end;
  205. end;
  206. function TLinkContainer.getusemask(mask:cardinal) : string;
  207. var
  208. p : tlinkcontaineritem;
  209. found : boolean;
  210. begin
  211. found:=false;
  212. repeat
  213. p:=tlinkcontaineritem(inherited getfirst);
  214. if p=nil then
  215. begin
  216. getusemask:='';
  217. exit;
  218. end;
  219. getusemask:=p.data^;
  220. found:=(p.needlink and mask)<>0;
  221. p.free;
  222. until found;
  223. end;
  224. function TLinkContainer.find(const s:string):boolean;
  225. var
  226. newnode : tlinkcontaineritem;
  227. begin
  228. find:=false;
  229. newnode:=tlinkcontaineritem(First);
  230. while assigned(newnode) do
  231. begin
  232. if newnode.data^=s then
  233. begin
  234. find:=true;
  235. exit;
  236. end;
  237. newnode:=tlinkcontaineritem(newnode.next);
  238. end;
  239. end;
  240. {****************************************************************************
  241. TExternalsItem
  242. ****************************************************************************}
  243. constructor tExternalsItem.Create(const s:string);
  244. begin
  245. inherited Create;
  246. found:=false;
  247. data:=stringdup(s);
  248. end;
  249. destructor tExternalsItem.Destroy;
  250. begin
  251. stringdispose(data);
  252. inherited;
  253. end;
  254. {****************************************************************************
  255. TUSED_UNIT
  256. ****************************************************************************}
  257. constructor tused_unit.create(_u : tmodule;intface:boolean);
  258. begin
  259. u:=_u;
  260. in_interface:=intface;
  261. in_uses:=false;
  262. is_stab_written:=false;
  263. loaded:=true;
  264. name:=stringdup(_u.modulename^);
  265. realname:=stringdup(_u.realmodulename^);
  266. checksum:=_u.crc;
  267. interface_checksum:=_u.interface_crc;
  268. unitid:=0;
  269. end;
  270. constructor tused_unit.create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  271. begin
  272. u:=nil;
  273. in_interface:=intface;
  274. in_uses:=false;
  275. is_stab_written:=false;
  276. loaded:=false;
  277. name:=stringdup(upper(n));
  278. realname:=stringdup(n);
  279. checksum:=c;
  280. interface_checksum:=intfc;
  281. unitid:=0;
  282. end;
  283. destructor tused_unit.destroy;
  284. begin
  285. stringdispose(realname);
  286. stringdispose(name);
  287. inherited destroy;
  288. end;
  289. {****************************************************************************
  290. TDENPENDENT_UNIT
  291. ****************************************************************************}
  292. constructor tdependent_unit.create(_u : tmodule);
  293. begin
  294. u:=_u;
  295. end;
  296. {****************************************************************************
  297. TMODULE
  298. ****************************************************************************}
  299. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  300. var
  301. p : dirstr;
  302. n : namestr;
  303. e : extstr;
  304. begin
  305. FSplit(s,p,n,e);
  306. { Programs have the name 'Program' to don't conflict with dup id's }
  307. if _is_unit then
  308. inherited create(n)
  309. else
  310. inherited create('Program');
  311. mainsource:=stringdup(s);
  312. { Dos has the famous 8.3 limit :( }
  313. {$ifdef shortasmprefix}
  314. asmprefix:=stringdup(FixFileName('as'));
  315. {$else}
  316. asmprefix:=stringdup(FixFileName(n));
  317. {$endif}
  318. setfilename(p+n,true);
  319. localunitsearchpath:=TSearchPathList.Create;
  320. localobjectsearchpath:=TSearchPathList.Create;
  321. localincludesearchpath:=TSearchPathList.Create;
  322. locallibrarysearchpath:=TSearchPathList.Create;
  323. used_units:=TLinkedList.Create;
  324. dependent_units:=TLinkedList.Create;
  325. resourcefiles:=TStringList.Create;
  326. linkunitofiles:=TLinkContainer.Create;
  327. linkunitstaticlibs:=TLinkContainer.Create;
  328. linkunitsharedlibs:=TLinkContainer.Create;
  329. linkotherofiles:=TLinkContainer.Create;
  330. linkotherstaticlibs:=TLinkContainer.Create;
  331. linkothersharedlibs:=TLinkContainer.Create;
  332. crc:=0;
  333. interface_crc:=0;
  334. flags:=0;
  335. scanner:=nil;
  336. map:=nil;
  337. globalsymtable:=nil;
  338. localsymtable:=nil;
  339. loaded_from:=LoadedFrom;
  340. do_reload:=false;
  341. unitcount:=1;
  342. do_compile:=false;
  343. sources_avail:=true;
  344. sources_checked:=false;
  345. compiled:=false;
  346. recompile_reason:=rr_unknown;
  347. in_second_load:=false;
  348. in_second_compile:=false;
  349. in_implementation:=false;
  350. in_global:=true;
  351. is_unit:=_is_unit;
  352. islibrary:=false;
  353. uses_imports:=false;
  354. imports:=TLinkedList.Create;
  355. _exports:=TLinkedList.Create;
  356. externals:=TLinkedList.Create;
  357. librarydata:=tasmlibrarydata.create(realmodulename^);
  358. end;
  359. destructor tmodule.Destroy;
  360. {$ifdef MEMDEBUG}
  361. var
  362. d : tmemdebug;
  363. {$endif}
  364. begin
  365. if assigned(map) then
  366. dispose(map);
  367. if assigned(imports) then
  368. imports.free;
  369. if assigned(_exports) then
  370. _exports.free;
  371. if assigned(externals) then
  372. externals.free;
  373. if assigned(scanner) then
  374. begin
  375. { also update current_scanner if it was pointing
  376. to this module }
  377. if current_scanner=tscannerfile(scanner) then
  378. current_scanner:=nil;
  379. tscannerfile(scanner).free;
  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. if assigned(globalsymtable) then
  409. globalsymtable.free;
  410. if assigned(localsymtable) then
  411. localsymtable.free;
  412. {$ifdef MEMDEBUG}
  413. d.free;
  414. {$endif}
  415. {$ifdef MEMDEBUG}
  416. d:=tmemdebug.create(modulename^+' - librarydata');
  417. {$endif}
  418. librarydata.free;
  419. {$ifdef MEMDEBUG}
  420. d.free;
  421. {$endif}
  422. stringdispose(modulename);
  423. inherited Destroy;
  424. end;
  425. procedure tmodule.reset;
  426. var
  427. pm : tdependent_unit;
  428. begin
  429. if assigned(scanner) then
  430. begin
  431. { also update current_scanner if it was pointing
  432. to this module }
  433. if current_scanner=tscannerfile(scanner) then
  434. current_scanner:=nil;
  435. tscannerfile(scanner).free;
  436. scanner:=nil;
  437. end;
  438. if assigned(globalsymtable) then
  439. begin
  440. globalsymtable.free;
  441. globalsymtable:=nil;
  442. end;
  443. if assigned(localsymtable) then
  444. begin
  445. localsymtable.free;
  446. localsymtable:=nil;
  447. end;
  448. if assigned(map) then
  449. begin
  450. dispose(map);
  451. map:=nil;
  452. end;
  453. sourcefiles.free;
  454. sourcefiles:=tinputfilemanager.create;
  455. librarydata.free;
  456. librarydata:=tasmlibrarydata.create(realmodulename^);
  457. imports.free;
  458. imports:=tlinkedlist.create;
  459. _exports.free;
  460. _exports:=tlinkedlist.create;
  461. externals.free;
  462. externals:=tlinkedlist.create;
  463. used_units.free;
  464. used_units:=TLinkedList.Create;
  465. { all units that depend on this one must be recompiled ! }
  466. pm:=tdependent_unit(dependent_units.first);
  467. while assigned(pm) do
  468. begin
  469. if pm.u.in_second_compile then
  470. Comment(v_debug,'No reload already in second compile: '+pm.u.modulename^)
  471. else
  472. begin
  473. pm.u.do_reload:=true;
  474. Comment(v_debug,'Reloading '+pm.u.modulename^+' needed because '+modulename^+' is reloaded');
  475. end;
  476. pm:=tdependent_unit(pm.next);
  477. end;
  478. dependent_units.free;
  479. dependent_units:=TLinkedList.Create;
  480. resourcefiles.Free;
  481. resourcefiles:=TStringList.Create;
  482. linkunitofiles.Free;
  483. linkunitofiles:=TLinkContainer.Create;
  484. linkunitstaticlibs.Free;
  485. linkunitstaticlibs:=TLinkContainer.Create;
  486. linkunitsharedlibs.Free;
  487. linkunitsharedlibs:=TLinkContainer.Create;
  488. linkotherofiles.Free;
  489. linkotherofiles:=TLinkContainer.Create;
  490. linkotherstaticlibs.Free;
  491. linkotherstaticlibs:=TLinkContainer.Create;
  492. linkothersharedlibs.Free;
  493. linkothersharedlibs:=TLinkContainer.Create;
  494. uses_imports:=false;
  495. do_compile:=false;
  496. { sources_avail:=true;
  497. should not be changed PM }
  498. compiled:=false;
  499. in_implementation:=false;
  500. in_global:=true;
  501. crc:=0;
  502. interface_crc:=0;
  503. flags:=0;
  504. {loaded_from:=nil;
  505. should not be changed PFV }
  506. unitcount:=1;
  507. recompile_reason:=rr_unknown;
  508. end;
  509. procedure tmodule.numberunits;
  510. var
  511. counter : longint;
  512. hp : tused_unit;
  513. hp1 : tmodule;
  514. begin
  515. { Reset all numbers to -1 }
  516. hp1:=tmodule(loaded_units.first);
  517. while assigned(hp1) do
  518. begin
  519. if assigned(hp1.globalsymtable) then
  520. hp1.globalsymtable.unitid:=$ffff;
  521. hp1:=tmodule(hp1.next);
  522. end;
  523. { Our own symtable gets unitid 0, for a program there is
  524. no globalsymtable }
  525. if assigned(globalsymtable) then
  526. globalsymtable.unitid:=0;
  527. { number units }
  528. counter:=1;
  529. hp:=tused_unit(used_units.first);
  530. while assigned(hp) do
  531. begin
  532. tsymtable(hp.u.globalsymtable).unitid:=counter;
  533. inc(counter);
  534. hp:=tused_unit(hp.next);
  535. end;
  536. end;
  537. end.
  538. {
  539. $Log$
  540. Revision 1.29 2002-11-20 12:36:23 mazen
  541. * $UNITPATH directive is now working
  542. Revision 1.28 2002/09/05 19:29:42 peter
  543. * memdebug enhancements
  544. Revision 1.27 2002/08/16 15:31:08 peter
  545. * fixed possible crashes with current_scanner
  546. Revision 1.26 2002/08/12 16:46:04 peter
  547. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  548. is updated accordingly. This removes all the loading and saving of
  549. the old scanner and the invalid flag marking
  550. Revision 1.25 2002/08/11 14:28:19 peter
  551. * TScannerFile.SetInvalid added that will also reset inputfile
  552. Revision 1.24 2002/08/11 13:24:11 peter
  553. * saving of asmsymbols in ppu supported
  554. * asmsymbollist global is removed and moved into a new class
  555. tasmlibrarydata that will hold the info of a .a file which
  556. corresponds with a single module. Added librarydata to tmodule
  557. to keep the library info stored for the module. In the future the
  558. objectfiles will also be stored to the tasmlibrarydata class
  559. * all getlabel/newasmsymbol and friends are moved to the new class
  560. Revision 1.23 2002/05/16 19:46:36 carl
  561. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  562. + try to fix temp allocation (still in ifdef)
  563. + generic constructor calls
  564. + start of tassembler / tmodulebase class cleanup
  565. Revision 1.22 2002/05/14 19:34:41 peter
  566. * removed old logs and updated copyright year
  567. Revision 1.21 2002/04/04 19:05:55 peter
  568. * removed unused units
  569. * use tlocation.size in cg.a_*loc*() routines
  570. Revision 1.20 2002/03/28 20:46:59 carl
  571. - remove go32v1 support
  572. }