fmodule.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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,aasmbase;
  37. const
  38. maxunits = 1024;
  39. type
  40. trecompile_reason = (rr_unknown,
  41. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  42. );
  43. TExternalsItem=class(TLinkedListItem)
  44. public
  45. found : longbool;
  46. data : pstring;
  47. constructor Create(const s:string);
  48. Destructor Destroy;override;
  49. end;
  50. tlinkcontaineritem=class(tlinkedlistitem)
  51. public
  52. data : pstring;
  53. needlink : cardinal;
  54. constructor Create(const s:string;m:cardinal);
  55. destructor Destroy;override;
  56. end;
  57. tlinkcontainer=class(tlinkedlist)
  58. procedure add(const s : string;m:cardinal);
  59. function get(var m:cardinal) : string;
  60. function getusemask(mask:cardinal) : string;
  61. function find(const s:string):boolean;
  62. end;
  63. {$ifndef NEWMAP}
  64. tunitmap = array[0..maxunits-1] of pointer;
  65. punitmap = ^tunitmap;
  66. {$else NEWMAP}
  67. tunitmap = array[0..maxunits-1] of tmodule;
  68. punitmap = ^tunitmap;
  69. {$endif NEWMAP}
  70. tmodule = class(tmodulebase)
  71. compiled, { unit is already compiled }
  72. do_reload, { force reloading of the unit }
  73. do_compile, { need to compile the sources }
  74. sources_avail, { if all sources are reachable }
  75. sources_checked, { if there is already done a check for the sources }
  76. is_unit,
  77. in_second_compile, { is this unit being compiled for the 2nd time? }
  78. in_second_load, { is this unit PPU loaded a 2nd time? }
  79. in_implementation, { processing the implementation part? }
  80. in_global : boolean; { allow global settings }
  81. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  82. crc,
  83. interface_crc : cardinal;
  84. flags : cardinal; { the PPU flags }
  85. islibrary : boolean; { if it is a library (win32 dll) }
  86. map : punitmap; { mapping of all used units }
  87. unitcount : longint; { local unit counter }
  88. globalsymtable, { pointer to the global symtable of this unit }
  89. localsymtable : tsymtable;{ pointer to the local symtable of this unit }
  90. scanner : pointer; { scanner object used }
  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 numberunits;
  119. end;
  120. tused_unit = class(tlinkedlistitem)
  121. unitid : longint;
  122. name : pstring;
  123. realname : pstring;
  124. checksum,
  125. interface_checksum : cardinal;
  126. loaded : boolean;
  127. in_uses,
  128. in_interface,
  129. is_stab_written : boolean;
  130. u : tmodule;
  131. constructor create(_u : tmodule;intface:boolean);
  132. constructor create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  133. destructor destroy;override;
  134. end;
  135. tdependent_unit = class(tlinkedlistitem)
  136. u : tmodule;
  137. constructor create(_u : tmodule);
  138. end;
  139. var
  140. main_module : tmodule; { Main module of the program }
  141. current_module : tmodule; { Current module which is compiled or loaded }
  142. compiled_module : tmodule; { Current module which is compiled }
  143. usedunits : tlinkedlist; { Used units for this program }
  144. loaded_units : tlinkedlist; { All loaded units }
  145. SmartLinkOFiles : TStringList; { List of .o files which are generated,
  146. used to delete them after linking }
  147. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  148. implementation
  149. uses
  150. {$ifdef delphi}
  151. dmisc,
  152. {$else}
  153. dos,
  154. {$endif}
  155. verbose,systems,
  156. scanner;
  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:boolean);
  261. begin
  262. u:=_u;
  263. in_interface:=intface;
  264. in_uses:=false;
  265. is_stab_written:=false;
  266. loaded:=true;
  267. name:=stringdup(_u.modulename^);
  268. realname:=stringdup(_u.realmodulename^);
  269. checksum:=_u.crc;
  270. interface_checksum:=_u.interface_crc;
  271. unitid:=0;
  272. end;
  273. constructor tused_unit.create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  274. begin
  275. u:=nil;
  276. in_interface:=intface;
  277. in_uses:=false;
  278. is_stab_written:=false;
  279. loaded:=false;
  280. name:=stringdup(upper(n));
  281. realname:=stringdup(n);
  282. checksum:=c;
  283. interface_checksum:=intfc;
  284. unitid:=0;
  285. end;
  286. destructor tused_unit.destroy;
  287. begin
  288. stringdispose(realname);
  289. stringdispose(name);
  290. inherited destroy;
  291. end;
  292. {****************************************************************************
  293. TDENPENDENT_UNIT
  294. ****************************************************************************}
  295. constructor tdependent_unit.create(_u : tmodule);
  296. begin
  297. u:=_u;
  298. end;
  299. {****************************************************************************
  300. TMODULE
  301. ****************************************************************************}
  302. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  303. var
  304. p : dirstr;
  305. n : namestr;
  306. e : extstr;
  307. begin
  308. FSplit(s,p,n,e);
  309. { Programs have the name 'Program' to don't conflict with dup id's }
  310. if _is_unit then
  311. inherited create(n)
  312. else
  313. inherited create('Program');
  314. mainsource:=stringdup(s);
  315. { Dos has the famous 8.3 limit :( }
  316. {$ifdef shortasmprefix}
  317. asmprefix:=stringdup(FixFileName('as'));
  318. {$else}
  319. asmprefix:=stringdup(FixFileName(n));
  320. {$endif}
  321. setfilename(p+n,true);
  322. localunitsearchpath:=TSearchPathList.Create;
  323. localobjectsearchpath:=TSearchPathList.Create;
  324. localincludesearchpath:=TSearchPathList.Create;
  325. locallibrarysearchpath:=TSearchPathList.Create;
  326. used_units:=TLinkedList.Create;
  327. dependent_units:=TLinkedList.Create;
  328. resourcefiles:=TStringList.Create;
  329. linkunitofiles:=TLinkContainer.Create;
  330. linkunitstaticlibs:=TLinkContainer.Create;
  331. linkunitsharedlibs:=TLinkContainer.Create;
  332. linkotherofiles:=TLinkContainer.Create;
  333. linkotherstaticlibs:=TLinkContainer.Create;
  334. linkothersharedlibs:=TLinkContainer.Create;
  335. crc:=0;
  336. interface_crc:=0;
  337. flags:=0;
  338. scanner:=nil;
  339. map:=nil;
  340. globalsymtable:=nil;
  341. localsymtable:=nil;
  342. loaded_from:=LoadedFrom;
  343. do_reload:=false;
  344. unitcount:=1;
  345. do_compile:=false;
  346. sources_avail:=true;
  347. sources_checked:=false;
  348. compiled:=false;
  349. recompile_reason:=rr_unknown;
  350. in_second_load:=false;
  351. in_second_compile:=false;
  352. in_implementation:=false;
  353. in_global:=true;
  354. is_unit:=_is_unit;
  355. islibrary:=false;
  356. uses_imports:=false;
  357. imports:=TLinkedList.Create;
  358. _exports:=TLinkedList.Create;
  359. externals:=TLinkedList.Create;
  360. librarydata:=tasmlibrarydata.create(realmodulename^);
  361. end;
  362. destructor tmodule.Destroy;
  363. {$ifdef MEMDEBUG}
  364. var
  365. d : tmemdebug;
  366. {$endif}
  367. begin
  368. if assigned(map) then
  369. dispose(map);
  370. if assigned(imports) then
  371. imports.free;
  372. if assigned(_exports) then
  373. _exports.free;
  374. if assigned(externals) then
  375. externals.free;
  376. if assigned(scanner) then
  377. begin
  378. { also update current_scanner if it was pointing
  379. to this module }
  380. if current_scanner=tscannerfile(scanner) then
  381. current_scanner:=nil;
  382. tscannerfile(scanner).free;
  383. end;
  384. used_units.free;
  385. dependent_units.free;
  386. resourcefiles.Free;
  387. linkunitofiles.Free;
  388. linkunitstaticlibs.Free;
  389. linkunitsharedlibs.Free;
  390. linkotherofiles.Free;
  391. linkotherstaticlibs.Free;
  392. linkothersharedlibs.Free;
  393. stringdispose(objfilename);
  394. stringdispose(newfilename);
  395. stringdispose(ppufilename);
  396. stringdispose(staticlibfilename);
  397. stringdispose(sharedlibfilename);
  398. stringdispose(exefilename);
  399. stringdispose(outputpath);
  400. stringdispose(path);
  401. stringdispose(realmodulename);
  402. stringdispose(mainsource);
  403. stringdispose(asmprefix);
  404. localunitsearchpath.Free;
  405. localobjectsearchpath.free;
  406. localincludesearchpath.free;
  407. locallibrarysearchpath.free;
  408. {$ifdef MEMDEBUG}
  409. d:=tmemdebug.create(modulename^+' - symtable');
  410. {$endif}
  411. if assigned(globalsymtable) then
  412. globalsymtable.free;
  413. if assigned(localsymtable) then
  414. localsymtable.free;
  415. {$ifdef MEMDEBUG}
  416. d.free;
  417. {$endif}
  418. {$ifdef MEMDEBUG}
  419. d:=tmemdebug.create(modulename^+' - librarydata');
  420. {$endif}
  421. librarydata.free;
  422. {$ifdef MEMDEBUG}
  423. d.free;
  424. {$endif}
  425. stringdispose(modulename);
  426. inherited Destroy;
  427. end;
  428. procedure tmodule.reset;
  429. var
  430. pm : tdependent_unit;
  431. begin
  432. if assigned(scanner) then
  433. begin
  434. { also update current_scanner if it was pointing
  435. to this module }
  436. if current_scanner=tscannerfile(scanner) then
  437. current_scanner:=nil;
  438. tscannerfile(scanner).free;
  439. scanner:=nil;
  440. end;
  441. if assigned(globalsymtable) then
  442. begin
  443. globalsymtable.free;
  444. globalsymtable:=nil;
  445. end;
  446. if assigned(localsymtable) then
  447. begin
  448. localsymtable.free;
  449. localsymtable:=nil;
  450. end;
  451. if assigned(map) then
  452. begin
  453. dispose(map);
  454. map:=nil;
  455. end;
  456. sourcefiles.free;
  457. sourcefiles:=tinputfilemanager.create;
  458. librarydata.free;
  459. librarydata:=tasmlibrarydata.create(realmodulename^);
  460. imports.free;
  461. imports:=tlinkedlist.create;
  462. _exports.free;
  463. _exports:=tlinkedlist.create;
  464. externals.free;
  465. externals:=tlinkedlist.create;
  466. used_units.free;
  467. used_units:=TLinkedList.Create;
  468. { all units that depend on this one must be recompiled ! }
  469. pm:=tdependent_unit(dependent_units.first);
  470. while assigned(pm) do
  471. begin
  472. if pm.u.in_second_compile then
  473. Comment(v_debug,'No reload already in second compile: '+pm.u.modulename^)
  474. else
  475. begin
  476. pm.u.do_reload:=true;
  477. Comment(v_debug,'Reloading '+pm.u.modulename^+' needed because '+modulename^+' is reloaded');
  478. end;
  479. pm:=tdependent_unit(pm.next);
  480. end;
  481. dependent_units.free;
  482. dependent_units:=TLinkedList.Create;
  483. resourcefiles.Free;
  484. resourcefiles:=TStringList.Create;
  485. linkunitofiles.Free;
  486. linkunitofiles:=TLinkContainer.Create;
  487. linkunitstaticlibs.Free;
  488. linkunitstaticlibs:=TLinkContainer.Create;
  489. linkunitsharedlibs.Free;
  490. linkunitsharedlibs:=TLinkContainer.Create;
  491. linkotherofiles.Free;
  492. linkotherofiles:=TLinkContainer.Create;
  493. linkotherstaticlibs.Free;
  494. linkotherstaticlibs:=TLinkContainer.Create;
  495. linkothersharedlibs.Free;
  496. linkothersharedlibs:=TLinkContainer.Create;
  497. uses_imports:=false;
  498. do_compile:=false;
  499. { sources_avail:=true;
  500. should not be changed PM }
  501. compiled:=false;
  502. in_implementation:=false;
  503. in_global:=true;
  504. crc:=0;
  505. interface_crc:=0;
  506. flags:=0;
  507. {loaded_from:=nil;
  508. should not be changed PFV }
  509. unitcount:=1;
  510. recompile_reason:=rr_unknown;
  511. end;
  512. procedure tmodule.numberunits;
  513. var
  514. counter : longint;
  515. hp : tused_unit;
  516. hp1 : tmodule;
  517. begin
  518. { Reset all numbers to -1 }
  519. hp1:=tmodule(loaded_units.first);
  520. while assigned(hp1) do
  521. begin
  522. if assigned(hp1.globalsymtable) then
  523. hp1.globalsymtable.unitid:=$ffff;
  524. hp1:=tmodule(hp1.next);
  525. end;
  526. { Our own symtable gets unitid 0, for a program there is
  527. no globalsymtable }
  528. if assigned(globalsymtable) then
  529. globalsymtable.unitid:=0;
  530. { number units }
  531. counter:=1;
  532. hp:=tused_unit(used_units.first);
  533. while assigned(hp) do
  534. begin
  535. tsymtable(hp.u.globalsymtable).unitid:=counter;
  536. inc(counter);
  537. hp:=tused_unit(hp.next);
  538. end;
  539. end;
  540. end.
  541. {
  542. $Log$
  543. Revision 1.30 2002-11-24 18:19:56 carl
  544. + tos also has short filenames
  545. Revision 1.29 2002/11/20 12:36:23 mazen
  546. * $UNITPATH directive is now working
  547. Revision 1.28 2002/09/05 19:29:42 peter
  548. * memdebug enhancements
  549. Revision 1.27 2002/08/16 15:31:08 peter
  550. * fixed possible crashes with current_scanner
  551. Revision 1.26 2002/08/12 16:46:04 peter
  552. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  553. is updated accordingly. This removes all the loading and saving of
  554. the old scanner and the invalid flag marking
  555. Revision 1.25 2002/08/11 14:28:19 peter
  556. * TScannerFile.SetInvalid added that will also reset inputfile
  557. Revision 1.24 2002/08/11 13:24:11 peter
  558. * saving of asmsymbols in ppu supported
  559. * asmsymbollist global is removed and moved into a new class
  560. tasmlibrarydata that will hold the info of a .a file which
  561. corresponds with a single module. Added librarydata to tmodule
  562. to keep the library info stored for the module. In the future the
  563. objectfiles will also be stored to the tasmlibrarydata class
  564. * all getlabel/newasmsymbol and friends are moved to the new class
  565. Revision 1.23 2002/05/16 19:46:36 carl
  566. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  567. + try to fix temp allocation (still in ifdef)
  568. + generic constructor calls
  569. + start of tassembler / tmodulebase class cleanup
  570. Revision 1.22 2002/05/14 19:34:41 peter
  571. * removed old logs and updated copyright year
  572. Revision 1.21 2002/04/04 19:05:55 peter
  573. * removed unused units
  574. * use tlocation.size in cg.a_*loc*() routines
  575. Revision 1.20 2002/03/28 20:46:59 carl
  576. - remove go32v1 support
  577. }