fmodule.pas 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 atari}
  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,fpkg,
  38. symbase,symsym,
  39. wpobase,
  40. aasmbase,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. { unit options }
  48. tmoduleoption = (mo_none,
  49. mo_hint_deprecated,
  50. mo_hint_platform,
  51. mo_hint_library,
  52. mo_hint_unimplemented,
  53. mo_hint_experimental,
  54. mo_has_deprecated_msg
  55. );
  56. tmoduleoptions = set of tmoduleoption;
  57. tlinkcontaineritem=class(tlinkedlistitem)
  58. public
  59. data : TPathStr;
  60. needlink : cardinal;
  61. constructor Create(const s:TPathStr;m:cardinal);
  62. end;
  63. tlinkcontainer=class(tlinkedlist)
  64. procedure add(const s : TPathStr;m:cardinal);
  65. function get(var m:cardinal) : TPathStr;
  66. function getusemask(mask:cardinal) : TPathStr;
  67. function find(const s:TPathStr):boolean;
  68. end;
  69. tmodule = class;
  70. tused_unit = class;
  71. tunitmaprec = record
  72. u : tmodule;
  73. { number of references }
  74. refs : longint;
  75. { index in the derefmap }
  76. derefidx : longint;
  77. end;
  78. punitmap = ^tunitmaprec;
  79. tderefmaprec = record
  80. u : tmodule;
  81. { modulename, used during ppu load }
  82. modulename : pshortstring;
  83. end;
  84. pderefmap = ^tderefmaprec;
  85. { tmodule }
  86. tmodule = class(tmodulebase)
  87. private
  88. FImportLibraryList : TFPHashObjectList;
  89. public
  90. do_reload, { force reloading of the unit }
  91. do_compile, { need to compile the sources }
  92. sources_avail, { if all sources are reachable }
  93. interface_compiled, { if the interface section has been parsed/compiled/loaded }
  94. is_dbginfo_written,
  95. is_unit,
  96. in_interface, { processing the implementation part? }
  97. { allow global settings }
  98. in_global : boolean;
  99. { Whether a mode switch is still allowed at this point in the parsing.}
  100. mode_switch_allowed,
  101. { generate pic helper which loads eip in ecx (for leave procedures) }
  102. requires_ecx_pic_helper,
  103. { generate pic helper which loads eip in ebx (for non leave procedures) }
  104. requires_ebx_pic_helper : boolean;
  105. interface_only: boolean; { interface-only macpas unit; flag does not need saving/restoring to ppu }
  106. mainfilepos : tfileposinfo;
  107. recompile_reason : trecompile_reason; { the reason why the unit should be recompiled }
  108. crc,
  109. interface_crc,
  110. indirect_crc : cardinal;
  111. headerflags : cardinal; { the PPU header flags }
  112. longversion : cardinal; { longer version than what fits in the ppu header }
  113. moduleflags : tmoduleflags; { ppu flags that do not need to be known by just reading the ppu header }
  114. islibrary : boolean; { if it is a library (win32 dll) }
  115. IsPackage : boolean;
  116. moduleid : longint;
  117. unitmap : punitmap; { mapping of all used units }
  118. unitmapsize : longint; { number of units in the map }
  119. derefmap : pderefmap; { mapping of all units needed for deref }
  120. derefmapcnt : longint; { number of units in the map }
  121. derefmapsize : longint; { number of units in the map }
  122. derefdataintflen : longint;
  123. derefdata : tdynamicarray;
  124. checkforwarddefs,
  125. deflist,
  126. symlist : TFPObjectList;
  127. ptrdefs : THashSet; { list of pointerdefs created in this module so we can reuse them (not saved/restored) }
  128. arraydefs : THashSet; { list of single-element-arraydefs created in this module so we can reuse them (not saved/restored) }
  129. procaddrdefs : THashSet; { list of procvardefs created when getting the address of a procdef (not saved/restored) }
  130. {$ifdef llvm}
  131. llvmdefs : THashSet; { defs added for llvm-specific reasons (not saved/restored) }
  132. llvmusedsyms : TFPObjectList; { a list of asmsymbols and their defs that need to be added to llvm.used (so they're not removed by llvm optimisation passes nor by the linker) }
  133. llvmcompilerusedsyms : TFPObjectList; { a list of asmsymbols and their defs that need to be added to llvm.compiler.used (so they're not removed by llvm optimisation passes) }
  134. llvminitprocs,
  135. llvmfiniprocs : TFPList;
  136. {$endif llvm}
  137. ansistrdef : tobject; { an ansistring def redefined for the current module }
  138. wpoinfo : tunitwpoinfobase; { whole program optimization-related information that is generated during the current run for this unit }
  139. globalsymtable, { pointer to the global symtable of this unit }
  140. localsymtable : TSymtable;{ pointer to the local symtable of this unit }
  141. globalmacrosymtable, { pointer to the global macro symtable of this unit }
  142. localmacrosymtable : TSymtable;{ pointer to the local macro symtable of this unit }
  143. scanner : TObject; { scanner object used }
  144. procinfo : TObject; { current procedure being compiled }
  145. asmdata : TObject; { Assembler data }
  146. asmprefix : pshortstring; { prefix for the smartlink asmfiles }
  147. publicasmsyms : TFPHashObjectList; { contains the assembler symbols which need to be exported from a package }
  148. externasmsyms : TFPHashObjectList; { contains the assembler symbols which are imported from another unit }
  149. unitimportsyms : tfpobjectlist; { list of symbols that are imported from other units }
  150. debuginfo : TObject;
  151. loaded_from : tmodule;
  152. _exports : tlinkedlist;
  153. dllscannerinputlist : TFPHashList;
  154. resourcefiles,
  155. linkorderedsymbols : TCmdStrList;
  156. linkunitofiles,
  157. linkunitstaticlibs,
  158. linkunitsharedlibs,
  159. linkotherofiles, { objects,libs loaded from the source }
  160. linkothersharedlibs, { using $L or $LINKLIB or import lib (for linux) }
  161. linkotherstaticlibs,
  162. linkotherframeworks : tlinkcontainer;
  163. mainname : pshortstring; { alternate name for "main" procedure }
  164. package : tpackage;
  165. used_units : tlinkedlist;
  166. dependent_units : tlinkedlist;
  167. localunitsearchpath, { local searchpaths }
  168. localobjectsearchpath,
  169. localincludesearchpath,
  170. locallibrarysearchpath,
  171. localframeworksearchpath : TSearchPathList;
  172. moduleoptions: tmoduleoptions;
  173. deprecatedmsg: pshortstring;
  174. { contains a list of types that are extended by helper types; the key is
  175. the full name of the type and the data is a TFPObjectList of
  176. tobjectdef instances (the helper defs) }
  177. extendeddefs: TFPHashObjectList;
  178. { contains a list of the current topmost non-generic symbol for a
  179. typename of which at least one generic exists; the key is the
  180. non-generic typename and the data is a TFPObjectList of tgenericdummyentry
  181. instances whereby the last one is the current top most one }
  182. genericdummysyms: TFPHashObjectList;
  183. { contains a list of specializations for which the method bodies need
  184. to be generated }
  185. pendingspecializations : TFPHashObjectList;
  186. { this contains a list of units that needs to be waited for until the
  187. unit can be finished (code generated, etc.); this is needed to handle
  188. specializations in circular unit usages correctly }
  189. waitingforunit: tfpobjectlist;
  190. { this contains a list of all units that are waiting for this unit to be
  191. finished }
  192. waitingunits: tfpobjectlist;
  193. finishstate: pointer;
  194. globalstate: pointer;
  195. namespace: pshortstring; { for JVM target: corresponds to Java package name }
  196. { for targets that initialise typed constants via explicit assignments
  197. instead of by generating an initialised data section (holds typed
  198. constant assignments at the module level; does not have to be saved
  199. into the ppu file, because translated into code during compilation)
  200. -- actual type: tnode (but fmodule should not depend on node) }
  201. tcinitcode : tobject;
  202. {create creates a new module which name is stored in 's'. LoadedFrom
  203. points to the module calling it. It is nil for the first compiled
  204. module. This allow inheritence of all path lists. MUST pay attention
  205. to that when creating link.res!!!!(mazen)}
  206. constructor create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  207. destructor destroy;override;
  208. procedure reset;virtual;
  209. procedure adddependency(callermodule:tmodule);
  210. procedure flagdependent(callermodule:tmodule);
  211. procedure addimportedsym(sym:TSymEntry);
  212. function addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  213. procedure updatemaps;
  214. function derefidx_unit(id:longint):longint;
  215. function resolve_unit(id:longint):tmodule;
  216. procedure allunitsused;
  217. procedure end_of_parsing;virtual;
  218. procedure setmodulename(const s:string);
  219. procedure AddExternalImport(const libname,symname,symmangledname:string;OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  220. procedure add_public_asmsym(sym:TAsmSymbol);
  221. procedure add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  222. procedure add_extern_asmsym(sym:TAsmSymbol);
  223. procedure add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  224. property ImportLibraryList : TFPHashObjectList read FImportLibraryList;
  225. end;
  226. tused_unit = class(tlinkedlistitem)
  227. checksum,
  228. interface_checksum,
  229. indirect_checksum: cardinal;
  230. in_uses,
  231. in_interface : boolean;
  232. u : tmodule;
  233. unitsym : tunitsym;
  234. constructor create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  235. procedure check_hints;
  236. end;
  237. tdependent_unit = class(tlinkedlistitem)
  238. u : tmodule;
  239. constructor create(_u : tmodule);
  240. end;
  241. var
  242. main_module : tmodule; { Main module of the program }
  243. current_module : tmodule; { Current module which is compiled or loaded }
  244. compiled_module : tmodule; { Current module which is compiled }
  245. usedunits : tlinkedlist; { Used units for this program }
  246. loaded_units : tlinkedlist; { All loaded units }
  247. unloaded_units : tlinkedlist; { Units removed from loaded_units, to be freed }
  248. SmartLinkOFiles : TCmdStrList; { List of .o files which are generated,
  249. used to delete them after linking }
  250. procedure set_current_module(p:tmodule);
  251. function get_module(moduleindex : longint) : tmodule;
  252. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  253. procedure addloadedunit(hp:tmodule);
  254. function find_module_from_symtable(st:tsymtable):tmodule;
  255. implementation
  256. uses
  257. SysUtils,globals,
  258. verbose,systems,
  259. scanner,ppu,dbgbase,
  260. procinfo,symdef;
  261. {$ifdef MEMDEBUG}
  262. var
  263. memsymtable : TMemDebug;
  264. {$endif}
  265. {*****************************************************************************
  266. Global Functions
  267. *****************************************************************************}
  268. function find_module_from_symtable(st:tsymtable):tmodule;
  269. var
  270. hp : tmodule;
  271. begin
  272. result:=nil;
  273. hp:=tmodule(loaded_units.first);
  274. while assigned(hp) do
  275. begin
  276. if (hp.moduleid=st.moduleid) then
  277. begin
  278. result:=hp;
  279. exit;
  280. end;
  281. hp:=tmodule(hp.next);
  282. end;
  283. end;
  284. procedure set_current_module(p:tmodule);
  285. begin
  286. { save the state of the scanner }
  287. if assigned(current_scanner) then
  288. current_scanner.tempcloseinputfile;
  289. { set new module }
  290. current_module:=p;
  291. { restore previous module settings }
  292. Fillchar(current_filepos,0,sizeof(current_filepos));
  293. if assigned(current_module) then
  294. begin
  295. current_asmdata:=tasmdata(current_module.asmdata);
  296. current_debuginfo:=tdebuginfo(current_module.debuginfo);
  297. { restore scanner and file positions }
  298. current_scanner:=tscannerfile(current_module.scanner);
  299. if assigned(current_scanner) then
  300. begin
  301. current_scanner.tempopeninputfile;
  302. current_scanner.gettokenpos;
  303. parser_current_file:=current_scanner.inputfile.name;
  304. end
  305. else
  306. begin
  307. current_filepos.moduleindex:=current_module.unit_index;
  308. parser_current_file:='';
  309. end;
  310. end
  311. else
  312. begin
  313. current_asmdata:=nil;
  314. current_scanner:=nil;
  315. current_debuginfo:=nil;
  316. end;
  317. end;
  318. function get_module(moduleindex : longint) : tmodule;
  319. var
  320. hp : tmodule;
  321. begin
  322. result:=nil;
  323. if moduleindex=0 then
  324. exit;
  325. result:=current_module;
  326. if not(assigned(loaded_units)) then
  327. exit;
  328. hp:=tmodule(loaded_units.first);
  329. while assigned(hp) and (hp.unit_index<>moduleindex) do
  330. hp:=tmodule(hp.next);
  331. result:=hp;
  332. end;
  333. function get_source_file(moduleindex,fileindex : longint) : tinputfile;
  334. var
  335. hp : tmodule;
  336. begin
  337. hp:=get_module(moduleindex);
  338. if assigned(hp) then
  339. get_source_file:=hp.sourcefiles.get_file(fileindex)
  340. else
  341. get_source_file:=nil;
  342. end;
  343. procedure addloadedunit(hp:tmodule);
  344. begin
  345. hp.moduleid:=loaded_units.count;
  346. loaded_units.concat(hp);
  347. end;
  348. {****************************************************************************
  349. TLinkContainerItem
  350. ****************************************************************************}
  351. constructor TLinkContainerItem.Create(const s:TPathStr;m:cardinal);
  352. begin
  353. inherited Create;
  354. data:=s;
  355. needlink:=m;
  356. end;
  357. {****************************************************************************
  358. TLinkContainer
  359. ****************************************************************************}
  360. procedure TLinkContainer.add(const s : TPathStr;m:cardinal);
  361. begin
  362. inherited concat(TLinkContainerItem.Create(s,m));
  363. end;
  364. function TLinkContainer.get(var m:cardinal) : TPathStr;
  365. var
  366. p : tlinkcontaineritem;
  367. begin
  368. p:=tlinkcontaineritem(inherited getfirst);
  369. if p=nil then
  370. begin
  371. get:='';
  372. m:=0;
  373. end
  374. else
  375. begin
  376. get:=p.data;
  377. m:=p.needlink;
  378. p.free;
  379. end;
  380. end;
  381. function TLinkContainer.getusemask(mask:cardinal) : TPathStr;
  382. var
  383. p : tlinkcontaineritem;
  384. found : boolean;
  385. begin
  386. found:=false;
  387. repeat
  388. p:=tlinkcontaineritem(inherited getfirst);
  389. if p=nil then
  390. begin
  391. getusemask:='';
  392. exit;
  393. end;
  394. getusemask:=p.data;
  395. found:=(p.needlink and mask)<>0;
  396. p.free;
  397. until found;
  398. end;
  399. function TLinkContainer.find(const s:TPathStr):boolean;
  400. var
  401. newnode : tlinkcontaineritem;
  402. begin
  403. find:=false;
  404. newnode:=tlinkcontaineritem(First);
  405. while assigned(newnode) do
  406. begin
  407. if newnode.data=s then
  408. begin
  409. find:=true;
  410. exit;
  411. end;
  412. newnode:=tlinkcontaineritem(newnode.next);
  413. end;
  414. end;
  415. {****************************************************************************
  416. TUSED_UNIT
  417. ****************************************************************************}
  418. constructor tused_unit.create(_u : tmodule;intface,inuses:boolean;usym:tunitsym);
  419. begin
  420. u:=_u;
  421. in_interface:=intface;
  422. in_uses:=inuses;
  423. unitsym:=usym;
  424. if _u.state=ms_compiled then
  425. begin
  426. checksum:=u.crc;
  427. interface_checksum:=u.interface_crc;
  428. indirect_checksum:=u.indirect_crc;
  429. end
  430. else
  431. begin
  432. checksum:=0;
  433. interface_checksum:=0;
  434. indirect_checksum:=0;
  435. end;
  436. end;
  437. procedure tused_unit.check_hints;
  438. var
  439. uname: pshortstring;
  440. begin
  441. uname:=u.realmodulename;
  442. if mo_hint_deprecated in u.moduleoptions then
  443. if (mo_has_deprecated_msg in u.moduleoptions) and (u.deprecatedmsg <> nil) then
  444. MessagePos2(unitsym.fileinfo,sym_w_deprecated_unit_with_msg,uname^,u.deprecatedmsg^)
  445. else
  446. MessagePos1(unitsym.fileinfo,sym_w_deprecated_unit,uname^);
  447. if mo_hint_experimental in u.moduleoptions then
  448. MessagePos1(unitsym.fileinfo,sym_w_experimental_unit,uname^);
  449. if mo_hint_platform in u.moduleoptions then
  450. MessagePos1(unitsym.fileinfo,sym_w_non_portable_unit,uname^);
  451. if mo_hint_library in u.moduleoptions then
  452. MessagePos1(unitsym.fileinfo,sym_w_library_unit,uname^);
  453. if mo_hint_unimplemented in u.moduleoptions then
  454. MessagePos1(unitsym.fileinfo,sym_w_non_implemented_unit,uname^);
  455. end;
  456. {****************************************************************************
  457. TDENPENDENT_UNIT
  458. ****************************************************************************}
  459. constructor tdependent_unit.create(_u : tmodule);
  460. begin
  461. u:=_u;
  462. end;
  463. {****************************************************************************
  464. TMODULE
  465. ****************************************************************************}
  466. constructor tmodule.create(LoadedFrom:TModule;const amodulename: string; const afilename:TPathStr;_is_unit:boolean);
  467. var
  468. n:string;
  469. fn:TPathStr;
  470. begin
  471. if amodulename='' then
  472. n:=ChangeFileExt(ExtractFileName(afilename),'')
  473. else
  474. n:=amodulename;
  475. if afilename='' then
  476. fn:=amodulename
  477. else
  478. fn:=afilename;
  479. { Programs have the name 'Program' to don't conflict with dup id's }
  480. if _is_unit then
  481. inherited create(amodulename)
  482. else
  483. inherited create('Program');
  484. mainsource:=fn;
  485. { Dos has the famous 8.3 limit :( }
  486. {$ifdef shortasmprefix}
  487. asmprefix:=stringdup(FixFileName('as'));
  488. {$else}
  489. asmprefix:=stringdup(FixFileName(n));
  490. {$endif}
  491. setfilename(fn,true);
  492. localunitsearchpath:=TSearchPathList.Create;
  493. localobjectsearchpath:=TSearchPathList.Create;
  494. localincludesearchpath:=TSearchPathList.Create;
  495. locallibrarysearchpath:=TSearchPathList.Create;
  496. localframeworksearchpath:=TSearchPathList.Create;
  497. used_units:=TLinkedList.Create;
  498. dependent_units:=TLinkedList.Create;
  499. resourcefiles:=TCmdStrList.Create;
  500. linkorderedsymbols:=TCmdStrList.Create;
  501. linkunitofiles:=TLinkContainer.Create;
  502. linkunitstaticlibs:=TLinkContainer.Create;
  503. linkunitsharedlibs:=TLinkContainer.Create;
  504. linkotherofiles:=TLinkContainer.Create;
  505. linkotherstaticlibs:=TLinkContainer.Create;
  506. linkothersharedlibs:=TLinkContainer.Create;
  507. linkotherframeworks:=TLinkContainer.Create;
  508. mainname:=nil;
  509. FImportLibraryList:=TFPHashObjectList.Create(true);
  510. crc:=0;
  511. interface_crc:=0;
  512. indirect_crc:=0;
  513. headerflags:=0;
  514. longversion:=0;
  515. moduleflags:=[];
  516. scanner:=nil;
  517. unitmap:=nil;
  518. unitmapsize:=0;
  519. derefmap:=nil;
  520. derefmapsize:=0;
  521. derefmapcnt:=0;
  522. derefdata:=TDynamicArray.Create(1024);
  523. derefdataintflen:=0;
  524. deflist:=TFPObjectList.Create(false);
  525. symlist:=TFPObjectList.Create(false);
  526. ptrdefs:=THashSet.Create(64,true,false);
  527. arraydefs:=THashSet.Create(64,true,false);
  528. procaddrdefs:=THashSet.Create(64,true,false);
  529. {$ifdef llvm}
  530. llvmdefs:=THashSet.Create(64,true,false);
  531. llvmusedsyms:=TFPObjectList.Create(true);
  532. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  533. llvminitprocs:=TFPList.Create;
  534. llvmfiniprocs:=TFPList.Create;
  535. {$endif llvm}
  536. ansistrdef:=nil;
  537. wpoinfo:=nil;
  538. checkforwarddefs:=TFPObjectList.Create(false);
  539. extendeddefs:=TFPHashObjectList.Create(true);
  540. genericdummysyms:=tfphashobjectlist.create(true);
  541. pendingspecializations:=tfphashobjectlist.create(false);
  542. waitingforunit:=tfpobjectlist.create(false);
  543. waitingunits:=tfpobjectlist.create(false);
  544. globalsymtable:=nil;
  545. localsymtable:=nil;
  546. globalmacrosymtable:=nil;
  547. localmacrosymtable:=nil;
  548. loaded_from:=LoadedFrom;
  549. do_reload:=false;
  550. do_compile:=false;
  551. sources_avail:=true;
  552. mainfilepos.line:=0;
  553. mainfilepos.column:=0;
  554. mainfilepos.fileindex:=0;
  555. recompile_reason:=rr_unknown;
  556. in_interface:=true;
  557. in_global:=true;
  558. is_unit:=_is_unit;
  559. islibrary:=false;
  560. ispackage:=false;
  561. is_dbginfo_written:=false;
  562. mode_switch_allowed:= true;
  563. moduleoptions:=[];
  564. deprecatedmsg:=nil;
  565. namespace:=nil;
  566. tcinitcode:=nil;
  567. _exports:=TLinkedList.Create;
  568. dllscannerinputlist:=TFPHashList.Create;
  569. asmdata:=casmdata.create(modulename);
  570. unitimportsyms:=TFPObjectList.Create(false);
  571. publicasmsyms:=TFPHashObjectList.Create(true);
  572. externasmsyms:=TFPHashObjectList.Create(true);
  573. InitDebugInfo(self,false);
  574. end;
  575. destructor tmodule.Destroy;
  576. var
  577. i : longint;
  578. current_debuginfo_reset : boolean;
  579. begin
  580. if assigned(unitmap) then
  581. freemem(unitmap);
  582. if assigned(derefmap) then
  583. begin
  584. for i:=0 to derefmapcnt-1 do
  585. stringdispose(derefmap[i].modulename);
  586. freemem(derefmap);
  587. end;
  588. if assigned(_exports) then
  589. _exports.free;
  590. if assigned(dllscannerinputlist) then
  591. dllscannerinputlist.free;
  592. if assigned(scanner) then
  593. begin
  594. { also update current_scanner if it was pointing
  595. to this module }
  596. if current_scanner=tscannerfile(scanner) then
  597. current_scanner:=nil;
  598. tscannerfile(scanner).free;
  599. end;
  600. if assigned(asmdata) then
  601. begin
  602. if current_asmdata=asmdata then
  603. current_asmdata:=nil;
  604. asmdata.free;
  605. end;
  606. if assigned(procinfo) then
  607. begin
  608. if current_procinfo=tprocinfo(procinfo) then
  609. begin
  610. current_procinfo:=nil;
  611. current_structdef:=nil;
  612. current_genericdef:=nil;
  613. current_specializedef:=nil;
  614. end;
  615. { release procinfo tree }
  616. tprocinfo(procinfo).destroy_tree;
  617. end;
  618. DoneDebugInfo(self,current_debuginfo_reset);
  619. used_units.free;
  620. dependent_units.free;
  621. resourcefiles.Free;
  622. linkorderedsymbols.Free;
  623. linkunitofiles.Free;
  624. linkunitstaticlibs.Free;
  625. linkunitsharedlibs.Free;
  626. linkotherofiles.Free;
  627. linkotherstaticlibs.Free;
  628. linkothersharedlibs.Free;
  629. linkotherframeworks.Free;
  630. stringdispose(mainname);
  631. externasmsyms.Free;
  632. publicasmsyms.Free;
  633. unitimportsyms.Free;
  634. FImportLibraryList.Free;
  635. extendeddefs.Free;
  636. genericdummysyms.free;
  637. pendingspecializations.free;
  638. waitingforunit.free;
  639. waitingunits.free;
  640. stringdispose(asmprefix);
  641. stringdispose(deprecatedmsg);
  642. stringdispose(namespace);
  643. tcinitcode.free;
  644. localunitsearchpath.Free;
  645. localobjectsearchpath.free;
  646. localincludesearchpath.free;
  647. locallibrarysearchpath.free;
  648. localframeworksearchpath.free;
  649. {$ifdef MEMDEBUG}
  650. memsymtable.start;
  651. {$endif}
  652. derefdata.free;
  653. deflist.free;
  654. symlist.free;
  655. ptrdefs.free;
  656. arraydefs.free;
  657. procaddrdefs.free;
  658. {$ifdef llvm}
  659. llvmdefs.free;
  660. llvmusedsyms.free;
  661. llvmcompilerusedsyms.free;
  662. llvminitprocs.free;
  663. llvmfiniprocs.free;
  664. {$endif llvm}
  665. ansistrdef:=nil;
  666. wpoinfo.free;
  667. checkforwarddefs.free;
  668. globalsymtable.free;
  669. localsymtable.free;
  670. globalmacrosymtable.free;
  671. localmacrosymtable.free;
  672. {$ifdef MEMDEBUG}
  673. memsymtable.stop;
  674. {$endif}
  675. inherited Destroy;
  676. end;
  677. procedure tmodule.reset;
  678. var
  679. i : longint;
  680. current_debuginfo_reset : boolean;
  681. begin
  682. if assigned(scanner) then
  683. begin
  684. { also update current_scanner if it was pointing
  685. to this module }
  686. if current_scanner=tscannerfile(scanner) then
  687. current_scanner:=nil;
  688. tscannerfile(scanner).free;
  689. scanner:=nil;
  690. end;
  691. if assigned(procinfo) then
  692. begin
  693. if current_procinfo=tprocinfo(procinfo) then
  694. begin
  695. current_procinfo:=nil;
  696. current_structdef:=nil;
  697. current_genericdef:=nil;
  698. current_specializedef:=nil;
  699. end;
  700. { release procinfo tree }
  701. tprocinfo(procinfo).destroy_tree;
  702. end;
  703. if assigned(asmdata) then
  704. begin
  705. if current_asmdata=asmdata then
  706. current_asmdata:=nil;
  707. asmdata.free;
  708. asmdata:=nil;
  709. end;
  710. DoneDebugInfo(self,current_debuginfo_reset);
  711. globalsymtable.free;
  712. globalsymtable:=nil;
  713. localsymtable.free;
  714. localsymtable:=nil;
  715. globalmacrosymtable.free;
  716. globalmacrosymtable:=nil;
  717. localmacrosymtable.free;
  718. localmacrosymtable:=nil;
  719. deflist.free;
  720. deflist:=TFPObjectList.Create(false);
  721. symlist.free;
  722. symlist:=TFPObjectList.Create(false);
  723. ptrdefs.free;
  724. ptrdefs:=THashSet.Create(64,true,false);
  725. arraydefs.free;
  726. arraydefs:=THashSet.Create(64,true,false);
  727. procaddrdefs.free;
  728. procaddrdefs:=THashSet.Create(64,true,false);
  729. {$ifdef llvm}
  730. llvmdefs.free;
  731. llvmdefs:=THashSet.Create(64,true,false);
  732. llvmusedsyms.free;
  733. llvmusedsyms:=TFPObjectList.Create(true);
  734. llvmcompilerusedsyms.free;
  735. llvmcompilerusedsyms:=TFPObjectList.Create(true);
  736. llvminitprocs.free;
  737. llvminitprocs:=TFPList.Create;
  738. llvmfiniprocs.free;
  739. llvmfiniprocs:=TFPList.Create;
  740. {$endif llvm}
  741. wpoinfo.free;
  742. wpoinfo:=nil;
  743. checkforwarddefs.free;
  744. checkforwarddefs:=TFPObjectList.Create(false);
  745. publicasmsyms.free;
  746. publicasmsyms:=TFPHashObjectList.Create(true);
  747. externasmsyms.free;
  748. externasmsyms:=TFPHashObjectList.Create(true);
  749. unitimportsyms.free;
  750. unitimportsyms:=TFPObjectList.Create(false);
  751. derefdata.free;
  752. derefdata:=TDynamicArray.Create(1024);
  753. if assigned(unitmap) then
  754. begin
  755. freemem(unitmap);
  756. unitmap:=nil;
  757. end;
  758. if assigned(derefmap) then
  759. begin
  760. for i:=0 to derefmapcnt-1 do
  761. stringdispose(derefmap[i].modulename);
  762. freemem(derefmap);
  763. derefmap:=nil;
  764. end;
  765. unitmapsize:=0;
  766. derefmapsize:=0;
  767. derefmapcnt:=0;
  768. derefdataintflen:=0;
  769. sourcefiles.free;
  770. sourcefiles:=tinputfilemanager.create;
  771. asmdata:=casmdata.create(modulename);
  772. InitDebugInfo(self,current_debuginfo_reset);
  773. _exports.free;
  774. _exports:=tlinkedlist.create;
  775. dllscannerinputlist.free;
  776. dllscannerinputlist:=TFPHashList.create;
  777. used_units.free;
  778. used_units:=TLinkedList.Create;
  779. dependent_units.free;
  780. dependent_units:=TLinkedList.Create;
  781. resourcefiles.Free;
  782. resourcefiles:=TCmdStrList.Create;
  783. linkorderedsymbols.Free;
  784. linkorderedsymbols:=TCmdStrList.Create;;
  785. pendingspecializations.free;
  786. pendingspecializations:=tfphashobjectlist.create(false);
  787. if assigned(waitingforunit) and
  788. (waitingforunit.count<>0) then
  789. internalerror(2016070501);
  790. waitingforunit.free;
  791. waitingforunit:=tfpobjectlist.create(false);;
  792. linkunitofiles.Free;
  793. linkunitofiles:=TLinkContainer.Create;
  794. linkunitstaticlibs.Free;
  795. linkunitstaticlibs:=TLinkContainer.Create;
  796. linkunitsharedlibs.Free;
  797. linkunitsharedlibs:=TLinkContainer.Create;
  798. linkotherofiles.Free;
  799. linkotherofiles:=TLinkContainer.Create;
  800. linkotherstaticlibs.Free;
  801. linkotherstaticlibs:=TLinkContainer.Create;
  802. linkothersharedlibs.Free;
  803. linkothersharedlibs:=TLinkContainer.Create;
  804. linkotherframeworks.Free;
  805. linkotherframeworks:=TLinkContainer.Create;
  806. stringdispose(mainname);
  807. FImportLibraryList.Free;
  808. FImportLibraryList:=TFPHashObjectList.Create;
  809. do_compile:=false;
  810. do_reload:=false;
  811. interface_compiled:=false;
  812. in_interface:=true;
  813. in_global:=true;
  814. mode_switch_allowed:=true;
  815. stringdispose(deprecatedmsg);
  816. stringdispose(namespace);
  817. tcinitcode.free;
  818. tcinitcode:=nil;
  819. localunitsearchpath.Free;
  820. localunitsearchpath:=TSearchPathList.Create;
  821. localobjectsearchpath.free;
  822. localobjectsearchpath:=TSearchPathList.Create;
  823. localincludesearchpath.free;
  824. localincludesearchpath:=TSearchPathList.Create;
  825. locallibrarysearchpath.free;
  826. locallibrarysearchpath:=TSearchPathList.Create;
  827. localframeworksearchpath.free;
  828. localframeworksearchpath:=TSearchPathList.Create;
  829. moduleoptions:=[];
  830. is_dbginfo_written:=false;
  831. crc:=0;
  832. interface_crc:=0;
  833. indirect_crc:=0;
  834. headerflags:=0;
  835. longversion:=0;
  836. moduleflags:=[];
  837. mainfilepos.line:=0;
  838. mainfilepos.column:=0;
  839. mainfilepos.fileindex:=0;
  840. recompile_reason:=rr_unknown;
  841. {
  842. The following fields should not
  843. be reset:
  844. mainsource
  845. state
  846. loaded_from
  847. sources_avail
  848. }
  849. end;
  850. procedure tmodule.adddependency(callermodule:tmodule);
  851. begin
  852. { This is not needed for programs }
  853. if not callermodule.is_unit then
  854. exit;
  855. Message2(unit_u_add_depend_to,callermodule.modulename^,modulename^);
  856. dependent_units.concat(tdependent_unit.create(callermodule));
  857. end;
  858. procedure tmodule.flagdependent(callermodule:tmodule);
  859. var
  860. pm : tdependent_unit;
  861. begin
  862. { flag all units that depend on this unit for reloading }
  863. pm:=tdependent_unit(current_module.dependent_units.first);
  864. while assigned(pm) do
  865. begin
  866. { We do not have to reload the unit that wants to load
  867. this unit, unless this unit is already compiled during
  868. the loading }
  869. if (pm.u=callermodule) and
  870. (pm.u.state<>ms_compiled) then
  871. Message1(unit_u_no_reload_is_caller,pm.u.modulename^)
  872. else
  873. if pm.u.state=ms_second_compile then
  874. Message1(unit_u_no_reload_in_second_compile,pm.u.modulename^)
  875. else
  876. begin
  877. pm.u.do_reload:=true;
  878. Message1(unit_u_flag_for_reload,pm.u.modulename^);
  879. end;
  880. pm:=tdependent_unit(pm.next);
  881. end;
  882. end;
  883. procedure tmodule.addimportedsym(sym:TSymEntry);
  884. begin
  885. if unitimportsyms.IndexOf(sym)<0 then
  886. unitimportsyms.Add(sym);
  887. end;
  888. function tmodule.addusedunit(hp:tmodule;inuses:boolean;usym:tunitsym):tused_unit;
  889. var
  890. pu : tused_unit;
  891. begin
  892. pu:=tused_unit.create(hp,in_interface,inuses,usym);
  893. used_units.concat(pu);
  894. addusedunit:=pu;
  895. end;
  896. procedure tmodule.updatemaps;
  897. var
  898. oldmapsize : longint;
  899. hp : tmodule;
  900. i : longint;
  901. begin
  902. { Extend unitmap }
  903. oldmapsize:=unitmapsize;
  904. unitmapsize:=loaded_units.count;
  905. reallocmem(unitmap,unitmapsize*sizeof(tunitmaprec));
  906. fillchar(unitmap[oldmapsize],(unitmapsize-oldmapsize)*sizeof(tunitmaprec),0);
  907. { Extend Derefmap }
  908. oldmapsize:=derefmapsize;
  909. derefmapsize:=loaded_units.count;
  910. reallocmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  911. fillchar(derefmap[oldmapsize],(derefmapsize-oldmapsize)*sizeof(tderefmaprec),0);
  912. { Add all units to unitmap }
  913. hp:=tmodule(loaded_units.first);
  914. i:=0;
  915. while assigned(hp) do
  916. begin
  917. if hp.moduleid>=unitmapsize then
  918. internalerror(200501151);
  919. { Verify old entries }
  920. if (i<oldmapsize) then
  921. begin
  922. if (hp.moduleid<>i) or
  923. (unitmap[hp.moduleid].u<>hp) then
  924. internalerror(200501156);
  925. end
  926. else
  927. begin
  928. unitmap[hp.moduleid].u:=hp;
  929. unitmap[hp.moduleid].derefidx:=-1;
  930. end;
  931. inc(i);
  932. hp:=tmodule(hp.next);
  933. end;
  934. end;
  935. function tmodule.derefidx_unit(id:longint):longint;
  936. begin
  937. if id>=unitmapsize then
  938. internalerror(2005011511);
  939. if unitmap[id].derefidx=-1 then
  940. begin
  941. unitmap[id].derefidx:=derefmapcnt;
  942. inc(derefmapcnt);
  943. derefmap[unitmap[id].derefidx].u:=unitmap[id].u;
  944. end;
  945. if unitmap[id].derefidx>=derefmapsize then
  946. internalerror(2005011514);
  947. result:=unitmap[id].derefidx;
  948. end;
  949. function tmodule.resolve_unit(id:longint):tmodule;
  950. var
  951. hp : tmodule;
  952. begin
  953. if id>=derefmapsize then
  954. internalerror(200306231);
  955. result:=derefmap[id].u;
  956. if not assigned(result) then
  957. begin
  958. if not assigned(derefmap[id].modulename) or
  959. (derefmap[id].modulename^='') then
  960. internalerror(200501159);
  961. hp:=tmodule(loaded_units.first);
  962. while assigned(hp) do
  963. begin
  964. { only check for units. The main program is also
  965. as a unit in the loaded_units list. We simply need
  966. to ignore this entry (PFV) }
  967. if hp.is_unit and
  968. (hp.modulename^=derefmap[id].modulename^) then
  969. break;
  970. hp:=tmodule(hp.next);
  971. end;
  972. if not assigned(hp) then
  973. internalerror(2005011510);
  974. derefmap[id].u:=hp;
  975. result:=hp;
  976. end;
  977. end;
  978. procedure tmodule.allunitsused;
  979. var
  980. pu : tused_unit;
  981. begin
  982. pu:=tused_unit(used_units.first);
  983. while assigned(pu) do
  984. begin
  985. if assigned(pu.u.globalsymtable) then
  986. begin
  987. if unitmap[pu.u.moduleid].u<>pu.u then
  988. internalerror(200501157);
  989. { Give a note when the unit is not referenced, skip
  990. this is for units with an initialization/finalization }
  991. if (unitmap[pu.u.moduleid].refs=0) and
  992. pu.in_uses and
  993. ((pu.u.moduleflags * [mf_init,mf_finalize])=[]) then
  994. CGMessagePos2(pu.unitsym.fileinfo,sym_n_unit_not_used,pu.u.realmodulename^,realmodulename^);
  995. end;
  996. pu:=tused_unit(pu.next);
  997. end;
  998. end;
  999. procedure tmodule.end_of_parsing;
  1000. begin
  1001. { free asmdata }
  1002. if assigned(asmdata) then
  1003. begin
  1004. asmdata.free;
  1005. asmdata:=nil;
  1006. end;
  1007. { free scanner }
  1008. if assigned(scanner) then
  1009. begin
  1010. if current_scanner=tscannerfile(scanner) then
  1011. current_scanner:=nil;
  1012. tscannerfile(scanner).free;
  1013. scanner:=nil;
  1014. end;
  1015. { free symtable stack }
  1016. if assigned(symtablestack) then
  1017. begin
  1018. symtablestack.free;
  1019. symtablestack:=nil;
  1020. end;
  1021. if assigned(macrosymtablestack) then
  1022. begin
  1023. macrosymtablestack.free;
  1024. macrosymtablestack:=nil;
  1025. end;
  1026. waitingforunit.free;
  1027. waitingforunit:=nil;
  1028. localmacrosymtable.free;
  1029. localmacrosymtable:=nil;
  1030. ptrdefs.free;
  1031. ptrdefs:=nil;
  1032. arraydefs.free;
  1033. arraydefs:=nil;
  1034. procaddrdefs.free;
  1035. procaddrdefs:=nil;
  1036. {$ifdef llvm}
  1037. llvmdefs.free;
  1038. llvmdefs:=nil;
  1039. {$endif llvm}
  1040. checkforwarddefs.free;
  1041. checkforwarddefs:=nil;
  1042. tcinitcode.free;
  1043. tcinitcode:=nil;
  1044. localunitsearchpath.free;
  1045. localunitsearchpath:=nil;
  1046. localobjectsearchpath.free;
  1047. localobjectsearchpath:=nil;
  1048. localincludesearchpath.free;
  1049. localincludesearchpath:=nil;
  1050. locallibrarysearchpath.free;
  1051. locallibrarysearchpath:=nil;
  1052. localframeworksearchpath.free;
  1053. localframeworksearchpath:=nil;
  1054. end;
  1055. procedure tmodule.setmodulename(const s:string);
  1056. begin
  1057. stringdispose(modulename);
  1058. stringdispose(realmodulename);
  1059. modulename:=stringdup(upper(s));
  1060. realmodulename:=stringdup(s);
  1061. { also update asmlibrary names }
  1062. current_asmdata.name:=modulename;
  1063. end;
  1064. procedure TModule.AddExternalImport(const libname,symname,symmangledname:string;
  1065. OrdNr: longint;isvar:boolean;ImportByOrdinalOnly:boolean);
  1066. var
  1067. ImportLibrary,OtherIL : TImportLibrary;
  1068. ImportSymbol : TImportSymbol;
  1069. i : longint;
  1070. begin
  1071. ImportLibrary:=TImportLibrary(ImportLibraryList.Find(libname));
  1072. if not assigned(ImportLibrary) then
  1073. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,libname);
  1074. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList.Find(symname));
  1075. if not assigned(ImportSymbol) then
  1076. begin
  1077. { Check that the same name does not exist in another library }
  1078. { If it does and the same mangled name is used, issue a warning }
  1079. if ImportLibraryList.Count>1 then
  1080. for i:=0 To ImportLibraryList.Count-1 do
  1081. begin
  1082. OtherIL:=TImportLibrary(ImportLibraryList.Items[i]);
  1083. ImportSymbol:=TImportSymbol(OtherIL.ImportSymbolList.Find(symname));
  1084. if assigned(ImportSymbol) then
  1085. begin
  1086. if ImportSymbol.MangledName=symmangledname then
  1087. begin
  1088. CGMessage3(sym_w_library_overload,symname,libname,OtherIL.Name);
  1089. break;
  1090. end;
  1091. end;
  1092. end;
  1093. if not ImportByOrdinalOnly then
  1094. { negative ordinal number indicates import by name with ordinal number as hint }
  1095. OrdNr:=-OrdNr;
  1096. ImportSymbol:=TImportSymbol.Create(ImportLibrary.ImportSymbolList,
  1097. symname,symmangledname,OrdNr,isvar);
  1098. end;
  1099. end;
  1100. procedure tmodule.add_public_asmsym(sym:TAsmSymbol);
  1101. begin
  1102. add_public_asmsym(sym.name,sym.bind,sym.typ);
  1103. end;
  1104. procedure tmodule.add_public_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1105. var
  1106. sym : tasmsymbol;
  1107. begin
  1108. { ToDo: check for AB_GLOBAL, AB_EXTERNAL? }
  1109. sym:=tasmsymbol(publicasmsyms.find(name));
  1110. if assigned(sym) then
  1111. begin
  1112. if (sym.bind<>bind) or (sym.typ<>typ) then
  1113. internalerror(2016070101);
  1114. exit;
  1115. end;
  1116. tasmsymbol.create(publicasmsyms,name,bind,typ);
  1117. end;
  1118. procedure tmodule.add_extern_asmsym(sym:TAsmSymbol);
  1119. begin
  1120. add_extern_asmsym(sym.name,sym.bind,sym.typ);
  1121. end;
  1122. procedure tmodule.add_extern_asmsym(const name:TSymStr;bind:TAsmsymbind;typ:Tasmsymtype);
  1123. var
  1124. sym : tasmsymbol;
  1125. begin
  1126. { ToDo: check for AB_EXTERNAL? }
  1127. sym:=tasmsymbol(externasmsyms.find(name));
  1128. if assigned(sym) then
  1129. begin
  1130. if (sym.bind<>bind) or (sym.typ<>typ) then
  1131. internalerror(2016070102);
  1132. exit;
  1133. end;
  1134. tasmsymbol.create(externasmsyms,name,bind,typ);
  1135. end;
  1136. initialization
  1137. {$ifdef MEMDEBUG}
  1138. memsymtable:=TMemDebug.create('Symtables');
  1139. memsymtable.stop;
  1140. {$endif MEMDEBUG}
  1141. finalization
  1142. {$ifdef MEMDEBUG}
  1143. memsymtable.free;
  1144. {$endif MEMDEBUG}
  1145. end.