fmodule.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. tscannerfile(scanner).free;
  371. used_units.free;
  372. dependent_units.free;
  373. resourcefiles.Free;
  374. linkunitofiles.Free;
  375. linkunitstaticlibs.Free;
  376. linkunitsharedlibs.Free;
  377. linkotherofiles.Free;
  378. linkotherstaticlibs.Free;
  379. linkothersharedlibs.Free;
  380. stringdispose(objfilename);
  381. stringdispose(newfilename);
  382. stringdispose(ppufilename);
  383. stringdispose(staticlibfilename);
  384. stringdispose(sharedlibfilename);
  385. stringdispose(exefilename);
  386. stringdispose(outputpath);
  387. stringdispose(path);
  388. stringdispose(modulename);
  389. stringdispose(realmodulename);
  390. stringdispose(mainsource);
  391. stringdispose(asmprefix);
  392. localunitsearchpath.Free;
  393. localobjectsearchpath.free;
  394. localincludesearchpath.free;
  395. locallibrarysearchpath.free;
  396. {$ifdef MEMDEBUG}
  397. d:=tmemdebug.create('symtable');
  398. {$endif}
  399. if assigned(globalsymtable) then
  400. globalsymtable.free;
  401. if assigned(localsymtable) then
  402. localsymtable.free;
  403. {$ifdef MEMDEBUG}
  404. d.free;
  405. {$endif}
  406. {$ifdef MEMDEBUG}
  407. d:=tmemdebug.create('librarydata');
  408. {$endif}
  409. librarydata.free;
  410. {$ifdef MEMDEBUG}
  411. d.free;
  412. {$endif}
  413. inherited Destroy;
  414. end;
  415. procedure tmodule.reset;
  416. var
  417. pm : tdependent_unit;
  418. begin
  419. if assigned(scanner) then
  420. begin
  421. { also update current_scanner if it was pointing
  422. to this module }
  423. if current_scanner=tscannerfile(scanner) then
  424. current_scanner:=nil;
  425. tscannerfile(scanner).free;
  426. scanner:=nil;
  427. end;
  428. if assigned(globalsymtable) then
  429. begin
  430. globalsymtable.free;
  431. globalsymtable:=nil;
  432. end;
  433. if assigned(localsymtable) then
  434. begin
  435. localsymtable.free;
  436. localsymtable:=nil;
  437. end;
  438. if assigned(map) then
  439. begin
  440. dispose(map);
  441. map:=nil;
  442. end;
  443. sourcefiles.free;
  444. sourcefiles:=tinputfilemanager.create;
  445. librarydata.free;
  446. librarydata:=tasmlibrarydata.create(realmodulename^);
  447. imports.free;
  448. imports:=tlinkedlist.create;
  449. _exports.free;
  450. _exports:=tlinkedlist.create;
  451. externals.free;
  452. externals:=tlinkedlist.create;
  453. used_units.free;
  454. used_units:=TLinkedList.Create;
  455. { all units that depend on this one must be recompiled ! }
  456. pm:=tdependent_unit(dependent_units.first);
  457. while assigned(pm) do
  458. begin
  459. if pm.u.in_second_compile then
  460. Comment(v_debug,'No reload already in second compile: '+pm.u.modulename^)
  461. else
  462. begin
  463. pm.u.do_reload:=true;
  464. Comment(v_debug,'Reloading '+pm.u.modulename^+' needed because '+modulename^+' is reloaded');
  465. end;
  466. pm:=tdependent_unit(pm.next);
  467. end;
  468. dependent_units.free;
  469. dependent_units:=TLinkedList.Create;
  470. resourcefiles.Free;
  471. resourcefiles:=TStringList.Create;
  472. linkunitofiles.Free;
  473. linkunitofiles:=TLinkContainer.Create;
  474. linkunitstaticlibs.Free;
  475. linkunitstaticlibs:=TLinkContainer.Create;
  476. linkunitsharedlibs.Free;
  477. linkunitsharedlibs:=TLinkContainer.Create;
  478. linkotherofiles.Free;
  479. linkotherofiles:=TLinkContainer.Create;
  480. linkotherstaticlibs.Free;
  481. linkotherstaticlibs:=TLinkContainer.Create;
  482. linkothersharedlibs.Free;
  483. linkothersharedlibs:=TLinkContainer.Create;
  484. uses_imports:=false;
  485. do_compile:=false;
  486. { sources_avail:=true;
  487. should not be changed PM }
  488. compiled:=false;
  489. in_implementation:=false;
  490. in_global:=true;
  491. crc:=0;
  492. interface_crc:=0;
  493. flags:=0;
  494. {loaded_from:=nil;
  495. should not be changed PFV }
  496. unitcount:=1;
  497. recompile_reason:=rr_unknown;
  498. end;
  499. procedure tmodule.numberunits;
  500. var
  501. counter : longint;
  502. hp : tused_unit;
  503. hp1 : tmodule;
  504. begin
  505. { Reset all numbers to -1 }
  506. hp1:=tmodule(loaded_units.first);
  507. while assigned(hp1) do
  508. begin
  509. if assigned(hp1.globalsymtable) then
  510. hp1.globalsymtable.unitid:=$ffff;
  511. hp1:=tmodule(hp1.next);
  512. end;
  513. { Our own symtable gets unitid 0, for a program there is
  514. no globalsymtable }
  515. if assigned(globalsymtable) then
  516. globalsymtable.unitid:=0;
  517. { number units }
  518. counter:=1;
  519. hp:=tused_unit(used_units.first);
  520. while assigned(hp) do
  521. begin
  522. tsymtable(hp.u.globalsymtable).unitid:=counter;
  523. inc(counter);
  524. hp:=tused_unit(hp.next);
  525. end;
  526. end;
  527. end.
  528. {
  529. $Log$
  530. Revision 1.26 2002-08-12 16:46:04 peter
  531. * tscannerfile is now destroyed in tmodule.reset and current_scanner
  532. is updated accordingly. This removes all the loading and saving of
  533. the old scanner and the invalid flag marking
  534. Revision 1.25 2002/08/11 14:28:19 peter
  535. * TScannerFile.SetInvalid added that will also reset inputfile
  536. Revision 1.24 2002/08/11 13:24:11 peter
  537. * saving of asmsymbols in ppu supported
  538. * asmsymbollist global is removed and moved into a new class
  539. tasmlibrarydata that will hold the info of a .a file which
  540. corresponds with a single module. Added librarydata to tmodule
  541. to keep the library info stored for the module. In the future the
  542. objectfiles will also be stored to the tasmlibrarydata class
  543. * all getlabel/newasmsymbol and friends are moved to the new class
  544. Revision 1.23 2002/05/16 19:46:36 carl
  545. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  546. + try to fix temp allocation (still in ifdef)
  547. + generic constructor calls
  548. + start of tassembler / tmodulebase class cleanup
  549. Revision 1.22 2002/05/14 19:34:41 peter
  550. * removed old logs and updated copyright year
  551. Revision 1.21 2002/04/04 19:05:55 peter
  552. * removed unused units
  553. * use tlocation.size in cg.a_*loc*() routines
  554. Revision 1.20 2002/03/28 20:46:59 carl
  555. - remove go32v1 support
  556. }