fmodule.pas 19 KB

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