fmodule.pas 30 KB

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