2
0

fmodule.pas 30 KB

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