fmodule.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. {$ifdef go32v1}
  21. {$define shortasmprefix}
  22. {$endif}
  23. {$ifdef go32v2}
  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;
  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_compile, { is it being compiled ?? }
  78. in_second_compile, { is this unit being compiled for the 2nd time? }
  79. in_second_load, { is this unit PPU loaded a 2nd time? }
  80. in_implementation, { processing the implementation part? }
  81. in_global : boolean; { allow global settings }
  82. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  83. crc,
  84. interface_crc : cardinal;
  85. flags : cardinal; { the PPU flags }
  86. islibrary : boolean; { if it is a library (win32 dll) }
  87. map : punitmap; { mapping of all used units }
  88. unitcount : longint; { local unit counter }
  89. globalsymtable, { pointer to the global symtable of this unit }
  90. localsymtable : tsymtable;{ pointer to the local symtable of this unit }
  91. scanner : pointer; { scanner object used }
  92. loaded_from : tmodule;
  93. uses_imports : boolean; { Set if the module imports from DLL's.}
  94. imports : tlinkedlist;
  95. _exports : tlinkedlist;
  96. externals : tlinkedlist; {Only for DLL scanners by using Unix-style $LINKLIB }
  97. resourcefiles : tstringlist;
  98. linkunitofiles,
  99. linkunitstaticlibs,
  100. linkunitsharedlibs,
  101. linkotherofiles, { objects,libs loaded from the source }
  102. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  103. linkotherstaticlibs : tlinkcontainer;
  104. used_units : tlinkedlist;
  105. dependent_units : tlinkedlist;
  106. localunitsearchpath, { local searchpaths }
  107. localobjectsearchpath,
  108. localincludesearchpath,
  109. locallibrarysearchpath : TSearchPathList;
  110. asmprefix : pstring; { prefix for the smartlink asmfiles }
  111. constructor create(const s:string;_is_unit:boolean);
  112. destructor destroy;override;
  113. procedure reset;virtual;
  114. procedure numberunits;
  115. end;
  116. tused_unit = class(tlinkedlistitem)
  117. unitid : longint;
  118. name : pstring;
  119. checksum,
  120. interface_checksum : cardinal;
  121. loaded : boolean;
  122. in_uses,
  123. in_interface,
  124. is_stab_written : boolean;
  125. u : tmodule;
  126. constructor create(_u : tmodule;intface:boolean);
  127. constructor create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  128. destructor destroy;override;
  129. end;
  130. tdependent_unit = class(tlinkedlistitem)
  131. u : tmodule;
  132. constructor create(_u : tmodule);
  133. end;
  134. var
  135. main_module : tmodule; { Main module of the program }
  136. current_module : tmodule; { Current module which is compiled or loaded }
  137. compiled_module : tmodule; { Current module which is compiled }
  138. usedunits : tlinkedlist; { Used units for this program }
  139. loaded_units : tlinkedlist; { All loaded units }
  140. SmartLinkOFiles : TStringList; { List of .o files which are generated,
  141. used to delete them after linking }
  142. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  143. implementation
  144. uses
  145. {$ifdef delphi}
  146. dmisc,
  147. {$else}
  148. dos,
  149. {$endif}
  150. globtype,verbose,systems,
  151. scanner;
  152. {*****************************************************************************
  153. Global Functions
  154. *****************************************************************************}
  155. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  156. var
  157. hp : tmodule;
  158. begin
  159. hp:=tmodule(loaded_units.first);
  160. while assigned(hp) and (hp.unit_index<>moduleindex) do
  161. hp:=tmodule(hp.next);
  162. if assigned(hp) then
  163. get_source_file:=hp.sourcefiles.get_file(fileindex)
  164. else
  165. get_source_file:=nil;
  166. end;
  167. {****************************************************************************
  168. TLinkContainerItem
  169. ****************************************************************************}
  170. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  171. begin
  172. inherited Create;
  173. data:=stringdup(s);
  174. needlink:=m;
  175. end;
  176. destructor TLinkContainerItem.Destroy;
  177. begin
  178. stringdispose(data);
  179. end;
  180. {****************************************************************************
  181. TLinkContainer
  182. ****************************************************************************}
  183. procedure TLinkContainer.add(const s : string;m:cardinal);
  184. begin
  185. inherited concat(TLinkContainerItem.Create(s,m));
  186. end;
  187. function TLinkContainer.get(var m:cardinal) : string;
  188. var
  189. p : tlinkcontaineritem;
  190. begin
  191. p:=tlinkcontaineritem(inherited getfirst);
  192. if p=nil then
  193. begin
  194. get:='';
  195. m:=0;
  196. end
  197. else
  198. begin
  199. get:=p.data^;
  200. m:=p.needlink;
  201. p.free;
  202. end;
  203. end;
  204. function TLinkContainer.getusemask(mask:cardinal) : string;
  205. var
  206. p : tlinkcontaineritem;
  207. found : boolean;
  208. begin
  209. found:=false;
  210. repeat
  211. p:=tlinkcontaineritem(inherited getfirst);
  212. if p=nil then
  213. begin
  214. getusemask:='';
  215. exit;
  216. end;
  217. getusemask:=p.data^;
  218. found:=(p.needlink and mask)<>0;
  219. p.free;
  220. until found;
  221. end;
  222. function TLinkContainer.find(const s:string):boolean;
  223. var
  224. newnode : tlinkcontaineritem;
  225. begin
  226. find:=false;
  227. newnode:=tlinkcontaineritem(First);
  228. while assigned(newnode) do
  229. begin
  230. if newnode.data^=s then
  231. begin
  232. find:=true;
  233. exit;
  234. end;
  235. newnode:=tlinkcontaineritem(newnode.next);
  236. end;
  237. end;
  238. {****************************************************************************
  239. TExternalsItem
  240. ****************************************************************************}
  241. constructor tExternalsItem.Create(const s:string);
  242. begin
  243. inherited Create;
  244. found:=false;
  245. data:=stringdup(s);
  246. end;
  247. destructor tExternalsItem.Destroy;
  248. begin
  249. stringdispose(data);
  250. inherited;
  251. end;
  252. {****************************************************************************
  253. TUSED_UNIT
  254. ****************************************************************************}
  255. constructor tused_unit.create(_u : tmodule;intface:boolean);
  256. begin
  257. u:=_u;
  258. in_interface:=intface;
  259. in_uses:=false;
  260. is_stab_written:=false;
  261. loaded:=true;
  262. name:=stringdup(_u.modulename^);
  263. checksum:=_u.crc;
  264. interface_checksum:=_u.interface_crc;
  265. unitid:=0;
  266. end;
  267. constructor tused_unit.create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  268. begin
  269. u:=nil;
  270. in_interface:=intface;
  271. in_uses:=false;
  272. is_stab_written:=false;
  273. loaded:=false;
  274. name:=stringdup(n);
  275. checksum:=c;
  276. interface_checksum:=intfc;
  277. unitid:=0;
  278. end;
  279. destructor tused_unit.destroy;
  280. begin
  281. stringdispose(name);
  282. inherited destroy;
  283. end;
  284. {****************************************************************************
  285. TDENPENDENT_UNIT
  286. ****************************************************************************}
  287. constructor tdependent_unit.create(_u : tmodule);
  288. begin
  289. u:=_u;
  290. end;
  291. {****************************************************************************
  292. TMODULE
  293. ****************************************************************************}
  294. constructor tmodule.create(const s:string;_is_unit:boolean);
  295. var
  296. p : dirstr;
  297. n : namestr;
  298. e : extstr;
  299. begin
  300. FSplit(s,p,n,e);
  301. { Programs have the name 'Program' to don't conflict with dup id's }
  302. if _is_unit then
  303. inherited create(n)
  304. else
  305. inherited create('Program');
  306. mainsource:=stringdup(s);
  307. { Dos has the famous 8.3 limit :( }
  308. {$ifdef shortasmprefix}
  309. asmprefix:=stringdup(FixFileName('as'));
  310. {$else}
  311. asmprefix:=stringdup(FixFileName(n));
  312. {$endif}
  313. setfilename(p+n,true);
  314. localunitsearchpath:=TSearchPathList.Create;
  315. localobjectsearchpath:=TSearchPathList.Create;
  316. localincludesearchpath:=TSearchPathList.Create;
  317. locallibrarysearchpath:=TSearchPathList.Create;
  318. used_units:=TLinkedList.Create;
  319. dependent_units:=TLinkedList.Create;
  320. resourcefiles:=TStringList.Create;
  321. linkunitofiles:=TLinkContainer.Create;
  322. linkunitstaticlibs:=TLinkContainer.Create;
  323. linkunitsharedlibs:=TLinkContainer.Create;
  324. linkotherofiles:=TLinkContainer.Create;
  325. linkotherstaticlibs:=TLinkContainer.Create;
  326. linkothersharedlibs:=TLinkContainer.Create;
  327. crc:=0;
  328. interface_crc:=0;
  329. flags:=0;
  330. scanner:=nil;
  331. map:=nil;
  332. globalsymtable:=nil;
  333. localsymtable:=nil;
  334. loaded_from:=nil;
  335. do_reload:=false;
  336. unitcount:=1;
  337. do_compile:=false;
  338. sources_avail:=true;
  339. sources_checked:=false;
  340. compiled:=false;
  341. recompile_reason:=rr_unknown;
  342. in_second_load:=false;
  343. in_compile:=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. end;
  354. destructor tmodule.Destroy;
  355. {$ifdef MEMDEBUG}
  356. var
  357. d : tmemdebug;
  358. {$endif}
  359. begin
  360. if assigned(map) then
  361. dispose(map);
  362. if assigned(imports) then
  363. imports.free;
  364. imports:=nil;
  365. if assigned(_exports) then
  366. _exports.free;
  367. _exports:=nil;
  368. if assigned(externals) then
  369. externals.free;
  370. externals:=nil;
  371. if assigned(scanner) then
  372. tscannerfile(scanner).invalid:=true;
  373. used_units.free;
  374. dependent_units.free;
  375. resourcefiles.Free;
  376. linkunitofiles.Free;
  377. linkunitstaticlibs.Free;
  378. linkunitsharedlibs.Free;
  379. linkotherofiles.Free;
  380. linkotherstaticlibs.Free;
  381. linkothersharedlibs.Free;
  382. stringdispose(objfilename);
  383. stringdispose(asmfilename);
  384. stringdispose(ppufilename);
  385. stringdispose(staticlibfilename);
  386. stringdispose(sharedlibfilename);
  387. stringdispose(exefilename);
  388. stringdispose(outputpath);
  389. stringdispose(path);
  390. stringdispose(modulename);
  391. stringdispose(realmodulename);
  392. stringdispose(mainsource);
  393. stringdispose(asmprefix);
  394. localunitsearchpath.Free;
  395. localobjectsearchpath.free;
  396. localincludesearchpath.free;
  397. locallibrarysearchpath.free;
  398. {$ifdef MEMDEBUG}
  399. d.init('symtable');
  400. {$endif}
  401. if assigned(globalsymtable) then
  402. globalsymtable.free;
  403. globalsymtable:=nil;
  404. if assigned(localsymtable) then
  405. localsymtable.free;
  406. localsymtable:=nil;
  407. {$ifdef MEMDEBUG}
  408. d.free;
  409. {$endif}
  410. inherited Destroy;
  411. end;
  412. procedure tmodule.reset;
  413. var
  414. pm : tdependent_unit;
  415. begin
  416. if assigned(scanner) then
  417. tscannerfile(scanner).invalid:=true;
  418. if assigned(globalsymtable) then
  419. begin
  420. globalsymtable.free;
  421. globalsymtable:=nil;
  422. end;
  423. if assigned(localsymtable) then
  424. begin
  425. localsymtable.free;
  426. localsymtable:=nil;
  427. end;
  428. if assigned(map) then
  429. begin
  430. dispose(map);
  431. map:=nil;
  432. end;
  433. sourcefiles.free;
  434. sourcefiles:=tinputfilemanager.create;
  435. imports.free;
  436. imports:=tlinkedlist.create;
  437. _exports.free;
  438. _exports:=tlinkedlist.create;
  439. externals.free;
  440. externals:=tlinkedlist.create;
  441. used_units.free;
  442. used_units:=TLinkedList.Create;
  443. { all units that depend on this one must be recompiled ! }
  444. pm:=tdependent_unit(dependent_units.first);
  445. while assigned(pm) do
  446. begin
  447. if pm.u.in_second_compile then
  448. Comment(v_debug,'No reload already in second compile: '+pm.u.modulename^)
  449. else
  450. begin
  451. pm.u.do_reload:=true;
  452. Comment(v_debug,'Reloading '+pm.u.modulename^+' needed because '+modulename^+' is reloaded');
  453. end;
  454. pm:=tdependent_unit(pm.next);
  455. end;
  456. dependent_units.free;
  457. dependent_units:=TLinkedList.Create;
  458. resourcefiles.Free;
  459. resourcefiles:=TStringList.Create;
  460. linkunitofiles.Free;
  461. linkunitofiles:=TLinkContainer.Create;
  462. linkunitstaticlibs.Free;
  463. linkunitstaticlibs:=TLinkContainer.Create;
  464. linkunitsharedlibs.Free;
  465. linkunitsharedlibs:=TLinkContainer.Create;
  466. linkotherofiles.Free;
  467. linkotherofiles:=TLinkContainer.Create;
  468. linkotherstaticlibs.Free;
  469. linkotherstaticlibs:=TLinkContainer.Create;
  470. linkothersharedlibs.Free;
  471. linkothersharedlibs:=TLinkContainer.Create;
  472. uses_imports:=false;
  473. do_compile:=false;
  474. { sources_avail:=true;
  475. should not be changed PM }
  476. compiled:=false;
  477. in_implementation:=false;
  478. in_global:=true;
  479. crc:=0;
  480. interface_crc:=0;
  481. flags:=0;
  482. {loaded_from:=nil;
  483. should not be changed PFV }
  484. unitcount:=1;
  485. recompile_reason:=rr_unknown;
  486. end;
  487. procedure tmodule.numberunits;
  488. var
  489. counter : longint;
  490. hp : tused_unit;
  491. hp1 : tmodule;
  492. begin
  493. { Reset all numbers to -1 }
  494. hp1:=tmodule(loaded_units.first);
  495. while assigned(hp1) do
  496. begin
  497. if assigned(hp1.globalsymtable) then
  498. hp1.globalsymtable.unitid:=$ffff;
  499. hp1:=tmodule(hp1.next);
  500. end;
  501. { Our own symtable gets unitid 0, for a program there is
  502. no globalsymtable }
  503. if assigned(globalsymtable) then
  504. globalsymtable.unitid:=0;
  505. { number units }
  506. counter:=1;
  507. hp:=tused_unit(used_units.first);
  508. while assigned(hp) do
  509. begin
  510. tsymtable(hp.u.globalsymtable).unitid:=counter;
  511. inc(counter);
  512. hp:=tused_unit(hp.next);
  513. end;
  514. end;
  515. end.
  516. {
  517. $Log$
  518. Revision 1.16 2001-06-03 15:15:31 peter
  519. * dllprt0 stub for linux shared libs
  520. * pass -init and -fini for linux shared libs
  521. * libprefix splitted into staticlibprefix and sharedlibprefix
  522. Revision 1.15 2001/05/09 14:11:10 jonas
  523. * range check error fixes from Peter
  524. Revision 1.14 2001/05/06 14:49:16 peter
  525. * ppu object to class rewrite
  526. * move ppu read and write stuff to fppu
  527. Revision 1.13 2001/04/18 22:01:53 peter
  528. * registration of targets and assemblers
  529. Revision 1.12 2001/04/13 18:08:37 peter
  530. * scanner object to class
  531. Revision 1.11 2001/04/13 01:22:07 peter
  532. * symtable change to classes
  533. * range check generation and errors fixed, make cycle DEBUG=1 works
  534. * memory leaks fixed
  535. Revision 1.10 2001/04/02 21:20:29 peter
  536. * resulttype rewrite
  537. Revision 1.9 2001/03/13 18:45:06 peter
  538. * fixed some memory leaks
  539. Revision 1.8 2001/03/06 18:28:02 peter
  540. * patch from Pavel with a new and much faster DLL Scanner for
  541. automatic importing so $linklib works for DLLs. Thanks Pavel!
  542. Revision 1.7 2001/02/20 21:41:15 peter
  543. * new fixfilename, findfile for unix. Look first for lowercase, then
  544. NormalCase and last for UPPERCASE names.
  545. Revision 1.6 2000/12/25 00:07:25 peter
  546. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  547. tlinkedlist objects)
  548. Revision 1.5 2000/11/07 20:48:33 peter
  549. * removed ref_count from pinputfile it's not used
  550. Revision 1.4 2000/10/31 22:02:46 peter
  551. * symtable splitted, no real code changes
  552. Revision 1.3 2000/10/15 07:47:51 peter
  553. * unit names and procedure names are stored mixed case
  554. Revision 1.2 2000/09/24 15:06:16 peter
  555. * use defines.inc
  556. Revision 1.1 2000/08/27 16:11:50 peter
  557. * moved some util functions from globals,cobjects to cutils
  558. * splitted files into finput,fmodule
  559. }