fmodule.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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_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. constructor create(const s:string;_is_unit:boolean);
  111. destructor destroy;override;
  112. procedure reset;virtual;
  113. procedure numberunits;
  114. end;
  115. tused_unit = class(tlinkedlistitem)
  116. unitid : longint;
  117. name : pstring;
  118. realname : 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. realname:=stringdup(_u.realmodulename^);
  264. checksum:=_u.crc;
  265. interface_checksum:=_u.interface_crc;
  266. unitid:=0;
  267. end;
  268. constructor tused_unit.create_to_load(const n:string;c,intfc:cardinal;intface:boolean);
  269. begin
  270. u:=nil;
  271. in_interface:=intface;
  272. in_uses:=false;
  273. is_stab_written:=false;
  274. loaded:=false;
  275. name:=stringdup(upper(n));
  276. realname:=stringdup(n);
  277. checksum:=c;
  278. interface_checksum:=intfc;
  279. unitid:=0;
  280. end;
  281. destructor tused_unit.destroy;
  282. begin
  283. stringdispose(realname);
  284. stringdispose(name);
  285. inherited destroy;
  286. end;
  287. {****************************************************************************
  288. TDENPENDENT_UNIT
  289. ****************************************************************************}
  290. constructor tdependent_unit.create(_u : tmodule);
  291. begin
  292. u:=_u;
  293. end;
  294. {****************************************************************************
  295. TMODULE
  296. ****************************************************************************}
  297. constructor tmodule.create(const s:string;_is_unit:boolean);
  298. var
  299. p : dirstr;
  300. n : namestr;
  301. e : extstr;
  302. begin
  303. FSplit(s,p,n,e);
  304. { Programs have the name 'Program' to don't conflict with dup id's }
  305. if _is_unit then
  306. inherited create(n)
  307. else
  308. inherited create('Program');
  309. mainsource:=stringdup(s);
  310. { Dos has the famous 8.3 limit :( }
  311. {$ifdef shortasmprefix}
  312. asmprefix:=stringdup(FixFileName('as'));
  313. {$else}
  314. asmprefix:=stringdup(FixFileName(n));
  315. {$endif}
  316. setfilename(p+n,true);
  317. localunitsearchpath:=TSearchPathList.Create;
  318. localobjectsearchpath:=TSearchPathList.Create;
  319. localincludesearchpath:=TSearchPathList.Create;
  320. locallibrarysearchpath:=TSearchPathList.Create;
  321. used_units:=TLinkedList.Create;
  322. dependent_units:=TLinkedList.Create;
  323. resourcefiles:=TStringList.Create;
  324. linkunitofiles:=TLinkContainer.Create;
  325. linkunitstaticlibs:=TLinkContainer.Create;
  326. linkunitsharedlibs:=TLinkContainer.Create;
  327. linkotherofiles:=TLinkContainer.Create;
  328. linkotherstaticlibs:=TLinkContainer.Create;
  329. linkothersharedlibs:=TLinkContainer.Create;
  330. crc:=0;
  331. interface_crc:=0;
  332. flags:=0;
  333. scanner:=nil;
  334. map:=nil;
  335. globalsymtable:=nil;
  336. localsymtable:=nil;
  337. loaded_from:=nil;
  338. do_reload:=false;
  339. unitcount:=1;
  340. do_compile:=false;
  341. sources_avail:=true;
  342. sources_checked:=false;
  343. compiled:=false;
  344. recompile_reason:=rr_unknown;
  345. in_second_load:=false;
  346. in_second_compile:=false;
  347. in_implementation:=false;
  348. in_global:=true;
  349. is_unit:=_is_unit;
  350. islibrary:=false;
  351. uses_imports:=false;
  352. imports:=TLinkedList.Create;
  353. _exports:=TLinkedList.Create;
  354. externals:=TLinkedList.Create;
  355. end;
  356. destructor tmodule.Destroy;
  357. {$ifdef MEMDEBUG}
  358. var
  359. d : tmemdebug;
  360. {$endif}
  361. begin
  362. if assigned(map) then
  363. dispose(map);
  364. if assigned(imports) then
  365. imports.free;
  366. imports:=nil;
  367. if assigned(_exports) then
  368. _exports.free;
  369. _exports:=nil;
  370. if assigned(externals) then
  371. externals.free;
  372. externals:=nil;
  373. if assigned(scanner) then
  374. tscannerfile(scanner).invalid:=true;
  375. used_units.free;
  376. dependent_units.free;
  377. resourcefiles.Free;
  378. linkunitofiles.Free;
  379. linkunitstaticlibs.Free;
  380. linkunitsharedlibs.Free;
  381. linkotherofiles.Free;
  382. linkotherstaticlibs.Free;
  383. linkothersharedlibs.Free;
  384. stringdispose(objfilename);
  385. stringdispose(asmfilename);
  386. stringdispose(ppufilename);
  387. stringdispose(staticlibfilename);
  388. stringdispose(sharedlibfilename);
  389. stringdispose(exefilename);
  390. stringdispose(outputpath);
  391. stringdispose(path);
  392. stringdispose(modulename);
  393. stringdispose(realmodulename);
  394. stringdispose(mainsource);
  395. stringdispose(asmprefix);
  396. localunitsearchpath.Free;
  397. localobjectsearchpath.free;
  398. localincludesearchpath.free;
  399. locallibrarysearchpath.free;
  400. {$ifdef MEMDEBUG}
  401. d:=tmemdebug.create('symtable');
  402. {$endif}
  403. if assigned(globalsymtable) then
  404. globalsymtable.free;
  405. globalsymtable:=nil;
  406. if assigned(localsymtable) then
  407. localsymtable.free;
  408. localsymtable:=nil;
  409. {$ifdef MEMDEBUG}
  410. d.free;
  411. {$endif}
  412. inherited Destroy;
  413. end;
  414. procedure tmodule.reset;
  415. var
  416. pm : tdependent_unit;
  417. begin
  418. if assigned(scanner) then
  419. tscannerfile(scanner).invalid:=true;
  420. if assigned(globalsymtable) then
  421. begin
  422. globalsymtable.free;
  423. globalsymtable:=nil;
  424. end;
  425. if assigned(localsymtable) then
  426. begin
  427. localsymtable.free;
  428. localsymtable:=nil;
  429. end;
  430. if assigned(map) then
  431. begin
  432. dispose(map);
  433. map:=nil;
  434. end;
  435. sourcefiles.free;
  436. sourcefiles:=tinputfilemanager.create;
  437. imports.free;
  438. imports:=tlinkedlist.create;
  439. _exports.free;
  440. _exports:=tlinkedlist.create;
  441. externals.free;
  442. externals:=tlinkedlist.create;
  443. used_units.free;
  444. used_units:=TLinkedList.Create;
  445. { all units that depend on this one must be recompiled ! }
  446. pm:=tdependent_unit(dependent_units.first);
  447. while assigned(pm) do
  448. begin
  449. if pm.u.in_second_compile then
  450. Comment(v_debug,'No reload already in second compile: '+pm.u.modulename^)
  451. else
  452. begin
  453. pm.u.do_reload:=true;
  454. Comment(v_debug,'Reloading '+pm.u.modulename^+' needed because '+modulename^+' is reloaded');
  455. end;
  456. pm:=tdependent_unit(pm.next);
  457. end;
  458. dependent_units.free;
  459. dependent_units:=TLinkedList.Create;
  460. resourcefiles.Free;
  461. resourcefiles:=TStringList.Create;
  462. linkunitofiles.Free;
  463. linkunitofiles:=TLinkContainer.Create;
  464. linkunitstaticlibs.Free;
  465. linkunitstaticlibs:=TLinkContainer.Create;
  466. linkunitsharedlibs.Free;
  467. linkunitsharedlibs:=TLinkContainer.Create;
  468. linkotherofiles.Free;
  469. linkotherofiles:=TLinkContainer.Create;
  470. linkotherstaticlibs.Free;
  471. linkotherstaticlibs:=TLinkContainer.Create;
  472. linkothersharedlibs.Free;
  473. linkothersharedlibs:=TLinkContainer.Create;
  474. uses_imports:=false;
  475. do_compile:=false;
  476. { sources_avail:=true;
  477. should not be changed PM }
  478. compiled:=false;
  479. in_implementation:=false;
  480. in_global:=true;
  481. crc:=0;
  482. interface_crc:=0;
  483. flags:=0;
  484. {loaded_from:=nil;
  485. should not be changed PFV }
  486. unitcount:=1;
  487. recompile_reason:=rr_unknown;
  488. end;
  489. procedure tmodule.numberunits;
  490. var
  491. counter : longint;
  492. hp : tused_unit;
  493. hp1 : tmodule;
  494. begin
  495. { Reset all numbers to -1 }
  496. hp1:=tmodule(loaded_units.first);
  497. while assigned(hp1) do
  498. begin
  499. if assigned(hp1.globalsymtable) then
  500. hp1.globalsymtable.unitid:=$ffff;
  501. hp1:=tmodule(hp1.next);
  502. end;
  503. { Our own symtable gets unitid 0, for a program there is
  504. no globalsymtable }
  505. if assigned(globalsymtable) then
  506. globalsymtable.unitid:=0;
  507. { number units }
  508. counter:=1;
  509. hp:=tused_unit(used_units.first);
  510. while assigned(hp) do
  511. begin
  512. tsymtable(hp.u.globalsymtable).unitid:=counter;
  513. inc(counter);
  514. hp:=tused_unit(hp.next);
  515. end;
  516. end;
  517. end.
  518. {
  519. $Log$
  520. Revision 1.19 2001-11-06 14:53:48 jonas
  521. * compiles again with -dmemdebug
  522. Revision 1.18 2001/08/04 10:23:54 peter
  523. * updates so it works with the ide
  524. Revision 1.17 2001/06/04 11:49:08 peter
  525. * store used units in original type in ppu
  526. Revision 1.16 2001/06/03 15:15:31 peter
  527. * dllprt0 stub for linux shared libs
  528. * pass -init and -fini for linux shared libs
  529. * libprefix splitted into staticlibprefix and sharedlibprefix
  530. Revision 1.15 2001/05/09 14:11:10 jonas
  531. * range check error fixes from Peter
  532. Revision 1.14 2001/05/06 14:49:16 peter
  533. * ppu object to class rewrite
  534. * move ppu read and write stuff to fppu
  535. Revision 1.13 2001/04/18 22:01:53 peter
  536. * registration of targets and assemblers
  537. Revision 1.12 2001/04/13 18:08:37 peter
  538. * scanner object to class
  539. Revision 1.11 2001/04/13 01:22:07 peter
  540. * symtable change to classes
  541. * range check generation and errors fixed, make cycle DEBUG=1 works
  542. * memory leaks fixed
  543. Revision 1.10 2001/04/02 21:20:29 peter
  544. * resulttype rewrite
  545. Revision 1.9 2001/03/13 18:45:06 peter
  546. * fixed some memory leaks
  547. Revision 1.8 2001/03/06 18:28:02 peter
  548. * patch from Pavel with a new and much faster DLL Scanner for
  549. automatic importing so $linklib works for DLLs. Thanks Pavel!
  550. Revision 1.7 2001/02/20 21:41:15 peter
  551. * new fixfilename, findfile for unix. Look first for lowercase, then
  552. NormalCase and last for UPPERCASE names.
  553. Revision 1.6 2000/12/25 00:07:25 peter
  554. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  555. tlinkedlist objects)
  556. Revision 1.5 2000/11/07 20:48:33 peter
  557. * removed ref_count from pinputfile it's not used
  558. Revision 1.4 2000/10/31 22:02:46 peter
  559. * symtable splitted, no real code changes
  560. Revision 1.3 2000/10/15 07:47:51 peter
  561. * unit names and procedure names are stored mixed case
  562. Revision 1.2 2000/09/24 15:06:16 peter
  563. * use defines.inc
  564. Revision 1.1 2000/08/27 16:11:50 peter
  565. * moved some util functions from globals,cobjects to cutils
  566. * splitted files into finput,fmodule
  567. }