fmodule.pas 20 KB

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