fmodule.pas 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fmodule;
  18. {$i fpcdefs.inc}
  19. {$ifdef go32v2}
  20. {$define shortasmprefix}
  21. {$endif}
  22. {$ifdef watcom}
  23. {$define shortasmprefix}
  24. {$endif}
  25. {$ifdef tos}
  26. {$define shortasmprefix}
  27. {$endif}
  28. {$ifdef OS2}
  29. { Allthough OS/2 supports long filenames I play it safe and
  30. use 8.3 filenames, because this allows the compiler to run
  31. on a FAT partition. (DM) }
  32. {$define shortasmprefix}
  33. {$endif}
  34. interface
  35. uses
  36. cutils,cclasses,cfileutl,
  37. globtype,finput,ogbase,
  38. symbase,symsym,
  39. wpobase,
  40. aasmbase,aasmtai,aasmdata;
  41. const
  42. UNSPECIFIED_LIBRARY_NAME = '<none>';
  43. type
  44. trecompile_reason = (rr_unknown,
  45. rr_noppu,rr_sourcenewer,rr_build,rr_crcchanged
  46. );
  47. tlinkcontaineritem=class(tlinkedlistitem)
  48. public
  49. data : pshortstring;
  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. tmodule = class;
  61. tused_unit = class;
  62. tunitmaprec = record
  63. u : tmodule;
  64. { number of references }
  65. refs : longint;
  66. { index in the derefmap }
  67. derefidx : longint;
  68. end;
  69. punitmap = ^tunitmaprec;
  70. tderefmaprec = record
  71. u : tmodule;
  72. { modulename, used during ppu load }
  73. modulename : pshortstring;
  74. end;
  75. pderefmap = ^tderefmaprec;
  76. tmodule = class(tmodulebase)
  77. private
  78. FImportLibraryList : TFPHashObjectList;
  79. public
  80. do_reload, { force reloading of the unit }
  81. do_compile, { need to compile the sources }
  82. sources_avail, { if all sources are reachable }
  83. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  84. is_dbginfo_written,
  85. is_reset,
  86. is_unit,
  87. in_interface, { processing the implementation part? }
  88. { allow global settings }
  89. in_global : boolean;
  90. { Whether a mode switch is still allowed at this point in the parsing.}
  91. mode_switch_allowed,
  92. { generate pic helper which loads eip in ecx (for leave procedures) }
  93. requires_ecx_pic_helper,
  94. { generate pic helper which loads eip in ebx (for non leave procedures) }
  95. requires_ebx_pic_helper : boolean;
  96. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  97. mainfilepos : tfileposinfo;
  98. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  99. crc,
  100. interface_crc : cardinal;
  101. flags : cardinal; { the PPU flags }
  102. islibrary : boolean; { if it is a library (win32 dll) }
  103. IsPackage : boolean;
  104. moduleid : longint;
  105. unitmap : punitmap; { mapping of all used units }
  106. unitmapsize : longint; { number of units in the map }
  107. derefmap : pderefmap; { mapping of all units needed for deref }
  108. derefmapcnt : longint; { number of units in the map }
  109. derefmapsize : longint; { number of units in the map }
  110. derefdataintflen : longint;
  111. derefdata : tdynamicarray;
  112. deflist,
  113. symlist : TFPObjectList;
  114. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  115. globalsymtable, { pointer to the global symtable of this unit }
  116. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  117. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  118. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  119. scanner : TObject; { scanner object used }
  120. procinfo : TObject; { current procedure being compiled }
  121. asmdata : TObject; { Assembler data }
  122. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  123. debuginfo : TObject;
  124. loaded_from : tmodule;
  125. _exports : tlinkedlist;
  126. dllscannerinputlist : TFPHashList;
  127. resourcefiles : TCmdStrList;
  128. linkunitofiles,
  129. linkunitstaticlibs,
  130. linkunitsharedlibs,
  131. linkotherofiles, { objects,libs loaded from the source }
  132. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  133. linkotherstaticlibs,
  134. linkotherframeworks : tlinkcontainer;
  135. mainname : pshortstring; { alternate name for "main" procedure }
  136. used_units : tlinkedlist;
  137. dependent_units : tlinkedlist;
  138. localunitsearchpath, { local searchpaths }
  139. localobjectsearchpath,
  140. localincludesearchpath,
  141. locallibrarysearchpath,
  142. localframeworksearchpath : TSearchPathList;
  143. {create creates a new module which name is stored in 's'. LoadedFrom
  144. points to the module calling it. It is nil for the first compiled
  145. module. This allow inheritence of all path lists. MUST pay attention
  146. to that when creating link.res!!!!(mazen)}
  147. constructor create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  148. destructor destroy;override;
  149. procedure reset;virtual;
  150. procedure adddependency(callermodule:tmodule);
  151. procedure flagdependent(callermodule:tmodule);
  152. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  153. procedure updatemaps;
  154. function derefidx_unit(id:longint):longint;
  155. function resolve_unit(id:longint):tmodule;
  156. procedure allunitsused;
  157. procedure setmodulename(const s:string);
  158. procedure AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  159. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  160. end;
  161. tused_unit = class(tlinkedlistitem)
  162. checksum,
  163. interface_checksum : cardinal;
  164. in_uses,
  165. in_interface : boolean;
  166. u : tmodule;
  167. unitsym : tunitsym;
  168. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  169. end;
  170. tdependent_unit = class(tlinkedlistitem)
  171. u : tmodule;
  172. constructor create(_u : tmodule);
  173. end;
  174. var
  175. main_module : tmodule; { Main module of the program }
  176. current_module : tmodule; { Current module which is compiled or loaded }
  177. compiled_module : tmodule; { Current module which is compiled }
  178. usedunits : tlinkedlist; { Used units for this program }
  179. loaded_units : tlinkedlist; { All loaded units }
  180. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  181. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  182. used to delete them after linking }
  183. procedure set_current_module(p:tmodule);
  184. function get_module(moduleindex : longint) : tmodule;
  185. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  186. procedure addloadedunit(hp:tmodule);
  187. function find_module_from_symtable(st:tsymtable):tmodule;
  188. implementation
  189. uses
  190. SysUtils,globals,
  191. verbose,systems,
  192. scanner,ppu,dbgbase,
  193. procinfo;
  194. {$ifdef MEMDEBUG}
  195. var
  196. memsymtable : TMemDebug;
  197. {$endif}
  198. {*****************************************************************************
  199. Global Functions
  200. *****************************************************************************}
  201. function find_module_from_symtable(st:tsymtable):tmodule;
  202. var
  203. hp : tmodule;
  204. begin
  205. result:=nil;
  206. hp:=tmodule(loaded_units.first);
  207. while assigned(hp) do
  208. begin
  209. if (hp.globalsymtable=st) or
  210. (hp.localsymtable=st) then
  211. begin
  212. result:=hp;
  213. exit;
  214. end;
  215. hp:=tmodule(hp.next);
  216. end;
  217. end;
  218. procedure set_current_module(p:tmodule);
  219. begin
  220. { save the state of the scanner }
  221. if assigned(current_scanner) then
  222. current_scanner.tempcloseinputfile;
  223. { set new module }
  224. current_module:=p;
  225. { restore previous module settings }
  226. Fillchar(current_filepos,0,sizeof(current_filepos));
  227. if assigned(current_module) then
  228. begin
  229. current_asmdata:=tasmdata(current_module.asmdata);
  230. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  231. { restore scanner and file positions }
  232. current_scanner:=tscannerfile(current_module.scanner);
  233. if assigned(current_scanner) then
  234. begin
  235. current_scanner.tempopeninputfile;
  236. current_scanner.gettokenpos;
  237. parser_current_file:=current_scanner.inputfile.name^;
  238. end
  239. else
  240. begin
  241. current_filepos.moduleindex:=current_module.unit_index;
  242. parser_current_file:='';
  243. end;
  244. end
  245. else
  246. begin
  247. current_asmdata:=nil;
  248. current_scanner:=nil;
  249. current_debuginfo:=nil;
  250. end;
  251. end;
  252. function get_module(moduleindex : longint) : tmodule;
  253. var
  254. hp : tmodule;
  255. begin
  256. result:=nil;
  257. if moduleindex=0 then
  258. exit;
  259. result:=current_module;
  260. if not(assigned(loaded_units)) then
  261. exit;
  262. hp:=tmodule(loaded_units.first);
  263. while assigned(hp) and (hp.unit_index<>moduleindex) do
  264. hp:=tmodule(hp.next);
  265. result:=hp;
  266. end;
  267. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  268. var
  269. hp : tmodule;
  270. begin
  271. hp:=get_module(moduleindex);
  272. if assigned(hp) then
  273. get_source_file:=hp.sourcefiles.get_file(fileindex)
  274. else
  275. get_source_file:=nil;
  276. end;
  277. procedure addloadedunit(hp:tmodule);
  278. begin
  279. hp.moduleid:=loaded_units.count;
  280. loaded_units.concat(hp);
  281. end;
  282. {****************************************************************************
  283. TLinkContainerItem
  284. ****************************************************************************}
  285. constructor TLinkContainerItem.Create(const s:string;m:cardinal);
  286. begin
  287. inherited Create;
  288. data:=stringdup(s);
  289. needlink:=m;
  290. end;
  291. destructor TLinkContainerItem.Destroy;
  292. begin
  293. stringdispose(data);
  294. end;
  295. {****************************************************************************
  296. TLinkContainer
  297. ****************************************************************************}
  298. procedure TLinkContainer.add(const s : string;m:cardinal);
  299. begin
  300. inherited concat(TLinkContainerItem.Create(s,m));
  301. end;
  302. function TLinkContainer.get(var m:cardinal) : string;
  303. var
  304. p : tlinkcontaineritem;
  305. begin
  306. p:=tlinkcontaineritem(inherited getfirst);
  307. if p=nil then
  308. begin
  309. get:='';
  310. m:=0;
  311. end
  312. else
  313. begin
  314. get:=p.data^;
  315. m:=p.needlink;
  316. p.free;
  317. end;
  318. end;
  319. function TLinkContainer.getusemask(mask:cardinal) : string;
  320. var
  321. p : tlinkcontaineritem;
  322. found : boolean;
  323. begin
  324. found:=false;
  325. repeat
  326. p:=tlinkcontaineritem(inherited getfirst);
  327. if p=nil then
  328. begin
  329. getusemask:='';
  330. exit;
  331. end;
  332. getusemask:=p.data^;
  333. found:=(p.needlink and mask)<>0;
  334. p.free;
  335. until found;
  336. end;
  337. function TLinkContainer.find(const s:string):boolean;
  338. var
  339. newnode : tlinkcontaineritem;
  340. begin
  341. find:=false;
  342. newnode:=tlinkcontaineritem(First);
  343. while assigned(newnode) do
  344. begin
  345. if newnode.data^=s then
  346. begin
  347. find:=true;
  348. exit;
  349. end;
  350. newnode:=tlinkcontaineritem(newnode.next);
  351. end;
  352. end;
  353. {****************************************************************************
  354. TUSED_UNIT
  355. ****************************************************************************}
  356. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  357. begin
  358. u:=_u;
  359. in_interface:=intface;
  360. in_uses:=inuses;
  361. unitsym:=usym;
  362. if _u.state=ms_compiled then
  363. begin
  364. checksum:=u.crc;
  365. interface_checksum:=u.interface_crc;
  366. end
  367. else
  368. begin
  369. checksum:=0;
  370. interface_checksum:=0;
  371. end;
  372. end;
  373. {****************************************************************************
  374. TDENPENDENT_UNIT
  375. ****************************************************************************}
  376. constructor tdependent_unit.create(_u : tmodule);
  377. begin
  378. u:=_u;
  379. end;
  380. {****************************************************************************
  381. TMODULE
  382. ****************************************************************************}
  383. constructor tmodule.create(LoadedFrom:TModule;const s:string;_is_unit:boolean);
  384. var
  385. n : string;
  386. begin
  387. n:=ChangeFileExt(ExtractFileName(s),'');
  388. { Programs have the name 'Program' to don't conflict with dup id's }
  389. if _is_unit then
  390. inherited create(n)
  391. else
  392. inherited create('Program');
  393. mainsource:=stringdup(s);
  394. { Dos has the famous 8.3 limit :( }
  395. {$ifdef shortasmprefix}
  396. asmprefix:=stringdup(FixFileName('as'));
  397. {$else}
  398. asmprefix:=stringdup(FixFileName(n));
  399. {$endif}
  400. setfilename(s,true);
  401. localunitsearchpath:=TSearchPathList.Create;
  402. localobjectsearchpath:=TSearchPathList.Create;
  403. localincludesearchpath:=TSearchPathList.Create;
  404. locallibrarysearchpath:=TSearchPathList.Create;
  405. localframeworksearchpath:=TSearchPathList.Create;
  406. used_units:=TLinkedList.Create;
  407. dependent_units:=TLinkedList.Create;
  408. resourcefiles:=TCmdStrList.Create;
  409. linkunitofiles:=TLinkContainer.Create;
  410. linkunitstaticlibs:=TLinkContainer.Create;
  411. linkunitsharedlibs:=TLinkContainer.Create;
  412. linkotherofiles:=TLinkContainer.Create;
  413. linkotherstaticlibs:=TLinkContainer.Create;
  414. linkothersharedlibs:=TLinkContainer.Create;
  415. linkotherframeworks:=TLinkContainer.Create;
  416. mainname:=nil;
  417. FImportLibraryList:=TFPHashObjectList.Create(true);
  418. crc:=0;
  419. interface_crc:=0;
  420. flags:=0;
  421. scanner:=nil;
  422. unitmap:=nil;
  423. unitmapsize:=0;
  424. derefmap:=nil;
  425. derefmapsize:=0;
  426. derefmapcnt:=0;
  427. derefdata:=TDynamicArray.Create(1024);
  428. derefdataintflen:=0;
  429. deflist:=TFPObjectList.Create(false);
  430. symlist:=TFPObjectList.Create(false);
  431. wpoinfo:=nil;
  432. globalsymtable:=nil;
  433. localsymtable:=nil;
  434. globalmacrosymtable:=nil;
  435. localmacrosymtable:=nil;
  436. loaded_from:=LoadedFrom;
  437. do_reload:=false;
  438. do_compile:=false;
  439. sources_avail:=true;
  440. mainfilepos.line:=0;
  441. mainfilepos.column:=0;
  442. mainfilepos.fileindex:=0;
  443. recompile_reason:=rr_unknown;
  444. in_interface:=true;
  445. in_global:=true;
  446. is_unit:=_is_unit;
  447. islibrary:=false;
  448. ispackage:=false;
  449. is_dbginfo_written:=false;
  450. is_reset:=false;
  451. mode_switch_allowed:= true;
  452. _exports:=TLinkedList.Create;
  453. dllscannerinputlist:=TFPHashList.Create;
  454. asmdata:=TAsmData.create(realmodulename^);
  455. InitDebugInfo(self);
  456. end;
  457. destructor tmodule.Destroy;
  458. var
  459. i : longint;
  460. hpi : tprocinfo;
  461. begin
  462. if assigned(unitmap) then
  463. freemem(unitmap);
  464. if assigned(derefmap) then
  465. begin
  466. for i:=0 to derefmapcnt-1 do
  467. stringdispose(derefmap[i].modulename);
  468. freemem(derefmap);
  469. end;
  470. if assigned(_exports) then
  471. _exports.free;
  472. if assigned(dllscannerinputlist) then
  473. dllscannerinputlist.free;
  474. if assigned(scanner) then
  475. begin
  476. { also update current_scanner if it was pointing
  477. to this module }
  478. if current_scanner=tscannerfile(scanner) then
  479. current_scanner:=nil;
  480. tscannerfile(scanner).free;
  481. end;
  482. if assigned(asmdata) then
  483. begin
  484. if current_asmdata=asmdata then
  485. current_asmdata:=nil;
  486. asmdata.free;
  487. end;
  488. if assigned(procinfo) then
  489. begin
  490. if current_procinfo=tprocinfo(procinfo) then
  491. current_procinfo:=nil;
  492. { release procinfo tree }
  493. while assigned(procinfo) do
  494. begin
  495. hpi:=tprocinfo(procinfo).parent;
  496. tprocinfo(procinfo).free;
  497. procinfo:=hpi;
  498. end;
  499. end;
  500. DoneDebugInfo(self);
  501. used_units.free;
  502. dependent_units.free;
  503. resourcefiles.Free;
  504. linkunitofiles.Free;
  505. linkunitstaticlibs.Free;
  506. linkunitsharedlibs.Free;
  507. linkotherofiles.Free;
  508. linkotherstaticlibs.Free;
  509. linkothersharedlibs.Free;
  510. linkotherframeworks.Free;
  511. stringdispose(mainname);
  512. FImportLibraryList.Free;
  513. stringdispose(objfilename);
  514. stringdispose(asmfilename);
  515. stringdispose(ppufilename);
  516. stringdispose(importlibfilename);
  517. stringdispose(staticlibfilename);
  518. stringdispose(sharedlibfilename);
  519. stringdispose(exefilename);
  520. stringdispose(outputpath);
  521. stringdispose(path);
  522. stringdispose(realmodulename);
  523. stringdispose(mainsource);
  524. stringdispose(asmprefix);
  525. localunitsearchpath.Free;
  526. localobjectsearchpath.free;
  527. localincludesearchpath.free;
  528. locallibrarysearchpath.free;
  529. localframeworksearchpath.free;
  530. {$ifdef MEMDEBUG}
  531. memsymtable.start;
  532. {$endif}
  533. derefdata.free;
  534. deflist.free;
  535. symlist.free;
  536. wpoinfo.free;
  537. globalsymtable.free;
  538. localsymtable.free;
  539. globalmacrosymtable.free;
  540. localmacrosymtable.free;
  541. {$ifdef MEMDEBUG}
  542. memsymtable.stop;
  543. {$endif}
  544. stringdispose(modulename);
  545. inherited Destroy;
  546. end;
  547. procedure tmodule.reset;
  548. var
  549. hpi : tprocinfo;
  550. i : longint;
  551. begin
  552. if assigned(scanner) then
  553. begin
  554. { also update current_scanner if it was pointing
  555. to this module }
  556. if current_scanner=tscannerfile(scanner) then
  557. current_scanner:=nil;
  558. tscannerfile(scanner).free;
  559. scanner:=nil;
  560. end;
  561. if assigned(procinfo) then
  562. begin
  563. if current_procinfo=tprocinfo(procinfo) then
  564. current_procinfo:=nil;
  565. { release procinfo tree }
  566. while assigned(procinfo) do
  567. begin
  568. hpi:=tprocinfo(procinfo).parent;
  569. tprocinfo(procinfo).free;
  570. procinfo:=hpi;
  571. end;
  572. end;
  573. if assigned(asmdata) then
  574. begin
  575. if current_asmdata=TAsmData(asmdata) then
  576. current_asmdata:=nil;
  577. asmdata.free;
  578. asmdata:=nil;
  579. end;
  580. DoneDebugInfo(self);
  581. globalsymtable.free;
  582. globalsymtable:=nil;
  583. localsymtable.free;
  584. localsymtable:=nil;
  585. globalmacrosymtable.free;
  586. globalmacrosymtable:=nil;
  587. localmacrosymtable.free;
  588. localmacrosymtable:=nil;
  589. deflist.free;
  590. deflist:=TFPObjectList.Create(false);
  591. symlist.free;
  592. symlist:=TFPObjectList.Create(false);
  593. wpoinfo.free;
  594. wpoinfo:=nil;
  595. derefdata.free;
  596. derefdata:=TDynamicArray.Create(1024);
  597. if assigned(unitmap) then
  598. begin
  599. freemem(unitmap);
  600. unitmap:=nil;
  601. end;
  602. if assigned(derefmap) then
  603. begin
  604. for i:=0 to derefmapcnt-1 do
  605. stringdispose(derefmap[i].modulename);
  606. freemem(derefmap);
  607. derefmap:=nil;
  608. end;
  609. unitmapsize:=0;
  610. derefmapsize:=0;
  611. derefmapcnt:=0;
  612. derefdataintflen:=0;
  613. sourcefiles.free;
  614. sourcefiles:=tinputfilemanager.create;
  615. asmdata:=TAsmData.create(realmodulename^);
  616. InitDebugInfo(self);
  617. _exports.free;
  618. _exports:=tlinkedlist.create;
  619. dllscannerinputlist.free;
  620. dllscannerinputlist:=TFPHashList.create;
  621. used_units.free;
  622. used_units:=TLinkedList.Create;
  623. dependent_units.free;
  624. dependent_units:=TLinkedList.Create;
  625. resourcefiles.Free;
  626. resourcefiles:=TCmdStrList.Create;
  627. linkunitofiles.Free;
  628. linkunitofiles:=TLinkContainer.Create;
  629. linkunitstaticlibs.Free;
  630. linkunitstaticlibs:=TLinkContainer.Create;
  631. linkunitsharedlibs.Free;
  632. linkunitsharedlibs:=TLinkContainer.Create;
  633. linkotherofiles.Free;
  634. linkotherofiles:=TLinkContainer.Create;
  635. linkotherstaticlibs.Free;
  636. linkotherstaticlibs:=TLinkContainer.Create;
  637. linkothersharedlibs.Free;
  638. linkothersharedlibs:=TLinkContainer.Create;
  639. linkotherframeworks.Free;
  640. linkotherframeworks:=TLinkContainer.Create;
  641. stringdispose(mainname);
  642. FImportLibraryList.Free;
  643. FImportLibraryList:=TFPHashObjectList.Create;
  644. do_compile:=false;
  645. do_reload:=false;
  646. interface_compiled:=false;
  647. in_interface:=true;
  648. in_global:=true;
  649. mode_switch_allowed:=true;
  650. is_dbginfo_written:=false;
  651. is_reset:=false;
  652. crc:=0;
  653. interface_crc:=0;
  654. flags:=0;
  655. mainfilepos.line:=0;
  656. mainfilepos.column:=0;
  657. mainfilepos.fileindex:=0;
  658. recompile_reason:=rr_unknown;
  659. {
  660. The following fields should not
  661. be reset:
  662. mainsource
  663. state
  664. loaded_from
  665. sources_avail
  666. }
  667. end;
  668. procedure tmodule.adddependency(callermodule:tmodule);
  669. begin
  670. { This is not needed for programs }
  671. if not callermodule.is_unit then
  672. exit;
  673. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  674. dependent_units.concat(tdependent_unit.create(callermodule));
  675. end;
  676. procedure tmodule.flagdependent(callermodule:tmodule);
  677. var
  678. pm : tdependent_unit;
  679. begin
  680. { flag all units that depend on this unit for reloading }
  681. pm:=tdependent_unit(current_module.dependent_units.first);
  682. while assigned(pm) do
  683. begin
  684. { We do not have to reload the unit that wants to load
  685. this unit, unless this unit is already compiled during
  686. the loading }
  687. if (pm.u=callermodule) and
  688. (pm.u.state<>ms_compiled) then
  689. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  690. else
  691. if pm.u.state=ms_second_compile then
  692. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  693. else
  694. begin
  695. pm.u.do_reload:=true;
  696. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  697. end;
  698. pm:=tdependent_unit(pm.next);
  699. end;
  700. end;
  701. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  702. var
  703. pu : tused_unit;
  704. begin
  705. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  706. used_units.concat(pu);
  707. addusedunit:=pu;
  708. end;
  709. procedure tmodule.updatemaps;
  710. var
  711. oldmapsize : longint;
  712. hp : tmodule;
  713. i : longint;
  714. begin
  715. { Extend unitmap }
  716. oldmapsize:=unitmapsize;
  717. unitmapsize:=loaded_units.count;
  718. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  719. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  720. { Extend Derefmap }
  721. oldmapsize:=derefmapsize;
  722. derefmapsize:=loaded_units.count;
  723. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  724. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  725. { Add all units to unitmap }
  726. hp:=tmodule(loaded_units.first);
  727. i:=0;
  728. while assigned(hp) do
  729. begin
  730. if hp.moduleid>=unitmapsize then
  731. internalerror(200501151);
  732. { Verify old entries }
  733. if (i<oldmapsize) then
  734. begin
  735. if (hp.moduleid<>i) or
  736. (unitmap[hp.moduleid].u<>hp) then
  737. internalerror(200501156);
  738. end
  739. else
  740. begin
  741. unitmap[hp.moduleid].u:=hp;
  742. unitmap[hp.moduleid].derefidx:=-1;
  743. end;
  744. inc(i);
  745. hp:=tmodule(hp.next);
  746. end;
  747. end;
  748. function tmodule.derefidx_unit(id:longint):longint;
  749. begin
  750. if id>=unitmapsize then
  751. internalerror(2005011511);
  752. if unitmap[id].derefidx=-1 then
  753. begin
  754. unitmap[id].derefidx:=derefmapcnt;
  755. inc(derefmapcnt);
  756. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  757. end;
  758. if unitmap[id].derefidx>=derefmapsize then
  759. internalerror(2005011514);
  760. result:=unitmap[id].derefidx;
  761. end;
  762. function tmodule.resolve_unit(id:longint):tmodule;
  763. var
  764. hp : tmodule;
  765. begin
  766. if id>=derefmapsize then
  767. internalerror(200306231);
  768. result:=derefmap[id].u;
  769. if not assigned(result) then
  770. begin
  771. if not assigned(derefmap[id].modulename) or
  772. (derefmap[id].modulename^='') then
  773. internalerror(200501159);
  774. hp:=tmodule(loaded_units.first);
  775. while assigned(hp) do
  776. begin
  777. { only check for units. The main program is also
  778. as a unit in the loaded_units list. We simply need
  779. to ignore this entry (PFV) }
  780. if hp.is_unit and
  781. (hp.modulename^=derefmap[id].modulename^) then
  782. break;
  783. hp:=tmodule(hp.next);
  784. end;
  785. if not assigned(hp) then
  786. internalerror(2005011510);
  787. derefmap[id].u:=hp;
  788. result:=hp;
  789. end;
  790. end;
  791. procedure tmodule.allunitsused;
  792. var
  793. pu : tused_unit;
  794. begin
  795. pu:=tused_unit(used_units.first);
  796. while assigned(pu) do
  797. begin
  798. if assigned(pu.u.globalsymtable) then
  799. begin
  800. if unitmap[pu.u.moduleid].u<>pu.u then
  801. internalerror(200501157);
  802. { Give a note when the unit is not referenced, skip
  803. this is for units with an initialization/finalization }
  804. if (unitmap[pu.u.moduleid].refs=0) and
  805. ((pu.u.flags and (uf_init or uf_finalize))=0) then
  806. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  807. end;
  808. pu:=tused_unit(pu.next);
  809. end;
  810. end;
  811. procedure tmodule.setmodulename(const s:string);
  812. begin
  813. stringdispose(modulename);
  814. stringdispose(realmodulename);
  815. modulename:=stringdup(upper(s));
  816. realmodulename:=stringdup(s);
  817. { also update asmlibrary names }
  818. current_asmdata.name:=modulename^;
  819. current_asmdata.realname:=realmodulename^;
  820. end;
  821. procedure TModule.AddExternalImport(const libname,symname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  822. var
  823. ImportLibrary : TImportLibrary;
  824. ImportSymbol : TFPHashObject;
  825. begin
  826. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  827. if not assigned(ImportLibrary) then
  828. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  829. ImportSymbol:=TFPHashObject(ImportLibrary.ImportSymbolList.Find(symname));
  830. if not assigned(ImportSymbol) then
  831. begin
  832. if not ImportByOrdinalOnly then
  833. { negative ordinal number indicates import by name with ordinal number as hint }
  834. OrdNr:=-OrdNr;
  835. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,symname,OrdNr,isvar);
  836. end;
  837. end;
  838. initialization
  839. {$ifdef MEMDEBUG}
  840. memsymtable:=TMemDebug.create('Symtables');
  841. memsymtable.stop;
  842. {$endif MEMDEBUG}
  843. finalization
  844. {$ifdef MEMDEBUG}
  845. memsymtable.free;
  846. {$endif MEMDEBUG}
  847. end.