pmodules.pas 94 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  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 pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. function proc_unit:boolean;
  21. procedure proc_package;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. SysUtils,
  26. globtype,systems,tokens,
  27. cutils,cfileutl,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,globstat,fpcp,fpkg,
  29. symconst,symbase,symtype,symdef,symsym,symtable,symcreat,
  30. wpoinfo,
  31. aasmtai,aasmdata,aasmbase,aasmcpu,
  32. cgbase,ngenutil,
  33. nbas,nutils,ncgutil,
  34. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  35. cresstr,procinfo,
  36. objcgutl,
  37. pkgutil,
  38. wpobase,
  39. scanner,pbase,pexpr,psystem,psub,pdecsub,pgenutil,pparautl,ncgvmt,ncgrtti,
  40. cpuinfo;
  41. procedure create_objectfile;
  42. var
  43. DLLScanner : TDLLScanner;
  44. s : string;
  45. KeepShared : TCmdStrList;
  46. begin
  47. { try to create import entries from system dlls }
  48. if (tf_has_dllscanner in target_info.flags) and
  49. (not current_module.linkOtherSharedLibs.Empty) then
  50. begin
  51. { Init DLLScanner }
  52. if assigned(CDLLScanner[target_info.system]) then
  53. DLLScanner:=CDLLScanner[target_info.system].Create
  54. else
  55. internalerror(200104121);
  56. KeepShared:=TCmdStrList.Create;
  57. { Walk all shared libs }
  58. While not current_module.linkOtherSharedLibs.Empty do
  59. begin
  60. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  61. if not DLLScanner.scan(s) then
  62. KeepShared.Concat(s);
  63. end;
  64. DLLscanner.Free;
  65. { Recreate import section }
  66. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  67. begin
  68. if assigned(current_asmdata.asmlists[al_imports]) then
  69. current_asmdata.asmlists[al_imports].clear
  70. else
  71. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  72. importlib.generatelib;
  73. end;
  74. { Readd the not processed files }
  75. while not KeepShared.Empty do
  76. begin
  77. s:=KeepShared.GetFirst;
  78. current_module.linkOtherSharedLibs.add(s,link_always);
  79. end;
  80. KeepShared.Free;
  81. end;
  82. { allow a target-specific pass over all assembler code (used by LLVM
  83. to insert type definitions }
  84. cnodeutils.InsertObjectInfo;
  85. { Start and end module debuginfo, at least required for stabs
  86. to insert n_sourcefile lines }
  87. if (cs_debuginfo in current_settings.moduleswitches) or
  88. (cs_use_lineinfo in current_settings.globalswitches) then
  89. current_debuginfo.insertmoduleinfo;
  90. { create the .s file and assemble it }
  91. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  92. GenerateAsm(false);
  93. { Also create a smartlinked version ? }
  94. if create_smartlink_library then
  95. begin
  96. GenerateAsm(true);
  97. if (af_needar in target_asm.flags) then
  98. Linker.MakeStaticLibrary;
  99. end;
  100. { resource files }
  101. CompileResourceFiles;
  102. end;
  103. procedure insertobjectfile;
  104. { Insert the used object file for this unit in the used list for this unit }
  105. begin
  106. current_module.linkunitofiles.add(current_module.objfilename,link_static);
  107. current_module.headerflags:=current_module.headerflags or uf_static_linked;
  108. if create_smartlink_library then
  109. begin
  110. current_module.linkunitstaticlibs.add(current_module.staticlibfilename ,link_smart);
  111. current_module.headerflags:=current_module.headerflags or uf_smart_linked;
  112. end;
  113. if cs_lto in current_settings.moduleswitches then
  114. begin
  115. current_module.linkunitofiles.add(ChangeFileExt(current_module.objfilename,LTOExt),link_lto);
  116. current_module.headerflags:=current_module.headerflags or uf_lto_linked;
  117. end;
  118. end;
  119. procedure create_dwarf_frame;
  120. begin
  121. { Dwarf conflicts with smartlinking in separate .a files }
  122. if create_smartlink_library then
  123. exit;
  124. { Call frame information }
  125. { MWE: we write our own info, so dwarf asm support is not really needed }
  126. { if (af_supports_dwarf in target_asm.flags) and }
  127. { CFI is currently broken for Darwin }
  128. if not(target_info.system in systems_darwin) and
  129. (
  130. (tf_needs_dwarf_cfi in target_info.flags) or
  131. (target_dbg.id in [dbg_dwarf2, dbg_dwarf3])
  132. ) then
  133. begin
  134. current_asmdata.asmlists[al_dwarf_frame].Free;
  135. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  136. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  137. end;
  138. end;
  139. Function CheckResourcesUsed : boolean;
  140. var
  141. hp : tused_unit;
  142. found : Boolean;
  143. begin
  144. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  145. if not CheckResourcesUsed then exit;
  146. hp:=tused_unit(usedunits.first);
  147. found:=mf_has_resourcefiles in current_module.moduleflags;
  148. while Assigned(hp) and not found do
  149. begin
  150. found:=mf_has_resourcefiles in hp.u.moduleflags;
  151. hp:=tused_unit(hp.next);
  152. end;
  153. CheckResourcesUsed:=found;
  154. end;
  155. function AddUnit(const s:string;addasused:boolean): tppumodule;
  156. var
  157. hp : tppumodule;
  158. unitsym : tunitsym;
  159. begin
  160. { load unit }
  161. hp:=registerunit(current_module,s,'');
  162. hp.loadppu;
  163. hp.adddependency(current_module);
  164. { add to symtable stack }
  165. symtablestack.push(hp.globalsymtable);
  166. if (m_mac in current_settings.modeswitches) and
  167. assigned(hp.globalmacrosymtable) then
  168. macrosymtablestack.push(hp.globalmacrosymtable);
  169. { insert unitsym }
  170. unitsym:=cunitsym.create(hp.modulename^,hp);
  171. inc(unitsym.refs);
  172. tabstractunitsymtable(current_module.localsymtable).insertunit(unitsym);
  173. if addasused then
  174. { add to used units }
  175. current_module.addusedunit(hp,false,unitsym);
  176. result:=hp;
  177. end;
  178. function AddUnit(const s:string):tppumodule;
  179. begin
  180. result:=AddUnit(s,true);
  181. end;
  182. procedure maybeloadvariantsunit;
  183. var
  184. hp : tmodule;
  185. begin
  186. { Do we need the variants unit? Skip this
  187. for VarUtils unit for bootstrapping }
  188. if not(mf_uses_variants in current_module.moduleflags) or
  189. (current_module.modulename^='VARUTILS') then
  190. exit;
  191. { Variants unit already loaded? }
  192. hp:=tmodule(loaded_units.first);
  193. while assigned(hp) do
  194. begin
  195. if hp.modulename^='VARIANTS' then
  196. exit;
  197. hp:=tmodule(hp.next);
  198. end;
  199. { Variants unit is not loaded yet, load it now }
  200. Message(parser_w_implicit_uses_of_variants_unit);
  201. AddUnit('variants');
  202. end;
  203. function MaybeRemoveResUnit : boolean;
  204. var
  205. resources_used : boolean;
  206. hp : tmodule;
  207. uu : tused_unit;
  208. unitname : shortstring;
  209. begin
  210. { We simply remove the unit from:
  211. - usedunit list, so that things like init/finalization table won't
  212. contain references to this unit
  213. - loaded_units list, so that the unit object file doesn't get linked
  214. with the executable. }
  215. { Note: on windows we always need resources! }
  216. resources_used:=(target_info.system in systems_all_windows)
  217. or CheckResourcesUsed;
  218. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  219. begin
  220. { resources aren't used, so we don't need this unit }
  221. if target_res.id=res_ext then
  222. unitname:='FPEXTRES'
  223. else
  224. unitname:='FPINTRES';
  225. Message1(unit_u_unload_resunit,unitname);
  226. { find the module }
  227. hp:=tmodule(loaded_units.first);
  228. while assigned(hp) do
  229. begin
  230. if hp.is_unit and (hp.modulename^=unitname) then break;
  231. hp:=tmodule(hp.next);
  232. end;
  233. if not assigned(hp) then
  234. internalerror(200801071);
  235. { find its tused_unit in the global list }
  236. uu:=tused_unit(usedunits.first);
  237. while assigned(uu) do
  238. begin
  239. if uu.u=hp then break;
  240. uu:=tused_unit(uu.next);
  241. end;
  242. if not assigned(uu) then
  243. internalerror(200801072);
  244. { remove the tused_unit }
  245. usedunits.Remove(uu);
  246. uu.Free;
  247. { remove the module }
  248. loaded_units.Remove(hp);
  249. unloaded_units.Concat(hp);
  250. end;
  251. MaybeRemoveResUnit:=resources_used;
  252. end;
  253. procedure loadsystemunit;
  254. begin
  255. { we are going to rebuild the symtablestack, clear it first }
  256. symtablestack.clear;
  257. macrosymtablestack.clear;
  258. { macro symtable }
  259. macrosymtablestack.push(initialmacrosymtable);
  260. { are we compiling the system unit? }
  261. if (cs_compilesystem in current_settings.moduleswitches) then
  262. begin
  263. systemunit:=tglobalsymtable(current_module.localsymtable);
  264. { create system defines }
  265. create_intern_types;
  266. create_intern_symbols;
  267. { Set the owner of errorsym and errortype to symtable to
  268. prevent crashes when accessing .owner }
  269. generrorsym.owner:=systemunit;
  270. generrordef.owner:=systemunit;
  271. exit;
  272. end;
  273. { insert the system unit, it is allways the first. Load also the
  274. internal types from the system unit }
  275. AddUnit('system');
  276. systemunit:=tglobalsymtable(symtablestack.top);
  277. load_intern_types;
  278. { Set the owner of errorsym and errortype to symtable to
  279. prevent crashes when accessing .owner }
  280. generrorsym.owner:=systemunit;
  281. generrordef.owner:=systemunit;
  282. end;
  283. procedure loaddefaultunits;
  284. begin
  285. { Units only required for main module }
  286. if not(current_module.is_unit) then
  287. begin
  288. { Heaptrc unit, load heaptrace before any other units especially objpas }
  289. if (cs_use_heaptrc in current_settings.globalswitches) then
  290. AddUnit('heaptrc');
  291. { Valgrind requires c memory manager }
  292. if (cs_gdb_valgrind in current_settings.globalswitches) then
  293. AddUnit('cmem');
  294. { Lineinfo unit }
  295. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  296. case target_dbg.id of
  297. dbg_stabs:
  298. AddUnit('lineinfo');
  299. dbg_stabx:
  300. AddUnit('lnfogdb');
  301. else
  302. AddUnit('lnfodwrf');
  303. end;
  304. end;
  305. {$ifdef cpufpemu}
  306. { Floating point emulation unit?
  307. softfpu must be in the system unit anyways (FK)
  308. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  309. AddUnit('softfpu');
  310. }
  311. {$endif cpufpemu}
  312. { Which kind of resource support?
  313. Note: if resources aren't used this unit will be removed later,
  314. otherwise we need it here since it must be loaded quite early }
  315. if (tf_has_winlike_resources in target_info.flags) then
  316. if target_res.id=res_ext then
  317. AddUnit('fpextres')
  318. else
  319. AddUnit('fpintres');
  320. end
  321. else if (cs_checkpointer in current_settings.localswitches) then
  322. AddUnit('heaptrc');
  323. { Objpas unit? }
  324. if m_objpas in current_settings.modeswitches then
  325. AddUnit('objpas');
  326. { Macpas unit? }
  327. if m_mac in current_settings.modeswitches then
  328. AddUnit('macpas');
  329. if m_iso in current_settings.modeswitches then
  330. AddUnit('iso7185');
  331. if m_extpas in current_settings.modeswitches then
  332. begin
  333. { basic procedures for Extended Pascal are for now provided by the iso unit }
  334. AddUnit('iso7185');
  335. AddUnit('extpas');
  336. end;
  337. { blocks support? }
  338. if m_blocks in current_settings.modeswitches then
  339. AddUnit('blockrtl');
  340. { default char=widechar? }
  341. if m_default_unicodestring in current_settings.modeswitches then
  342. AddUnit('uuchar');
  343. { Objective-C support unit? }
  344. if (m_objectivec1 in current_settings.modeswitches) then
  345. begin
  346. { interface to Objective-C run time }
  347. AddUnit('objc');
  348. loadobjctypes;
  349. { NSObject }
  350. if not(current_module.is_unit) or
  351. (current_module.modulename^<>'OBJCBASE') then
  352. AddUnit('objcbase');
  353. end;
  354. { Profile unit? Needed for go32v2 only }
  355. if (cs_profile in current_settings.moduleswitches) and
  356. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  357. AddUnit('profile');
  358. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  359. begin
  360. AddUnit('fpcylix');
  361. AddUnit('dynlibs');
  362. end;
  363. {$push}
  364. {$warn 6018 off} { Unreachable code due to compile time evaluation }
  365. { CPU targets with microcontroller support can add a controller specific unit }
  366. if ControllerSupport and (target_info.system in (systems_embedded+systems_freertos)) and
  367. (current_settings.controllertype<>ct_none) and
  368. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') then
  369. AddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  370. {$pop}
  371. end;
  372. procedure loadautounits;
  373. var
  374. hs,s : string;
  375. begin
  376. hs:=autoloadunits;
  377. repeat
  378. s:=GetToken(hs,',');
  379. if s='' then
  380. break;
  381. AddUnit(s);
  382. until false;
  383. end;
  384. procedure loadunits(preservest:tsymtable);
  385. var
  386. s,sorg : ansistring;
  387. fn : string;
  388. pu,pu2 : tused_unit;
  389. hp2 : tmodule;
  390. unitsym : tunitsym;
  391. filepos : tfileposinfo;
  392. begin
  393. consume(_USES);
  394. repeat
  395. s:=pattern;
  396. sorg:=orgpattern;
  397. filepos:=current_tokenpos;
  398. consume(_ID);
  399. while token=_POINT do
  400. begin
  401. consume(_POINT);
  402. s:=s+'.'+pattern;
  403. sorg:=sorg+'.'+orgpattern;
  404. consume(_ID);
  405. end;
  406. { support "<unit> in '<file>'" construct, but not for tp7 }
  407. fn:='';
  408. if not(m_tp7 in current_settings.modeswitches) and
  409. try_to_consume(_OP_IN) then
  410. fn:=FixFileName(get_stringconst);
  411. { Give a warning if lineinfo is loaded }
  412. if s='LINEINFO' then
  413. begin
  414. Message(parser_w_no_lineinfo_use_switch);
  415. if (target_dbg.id in [dbg_dwarf2, dbg_dwarf3]) then
  416. s := 'LNFODWRF';
  417. sorg := s;
  418. end;
  419. { Give a warning if objpas is loaded }
  420. if s='OBJPAS' then
  421. Message(parser_w_no_objpas_use_mode);
  422. { Using the unit itself is not possible }
  423. if (s<>current_module.modulename^) then
  424. begin
  425. { check if the unit is already used }
  426. hp2:=nil;
  427. pu:=tused_unit(current_module.used_units.first);
  428. while assigned(pu) do
  429. begin
  430. if (pu.u.modulename^=s) then
  431. begin
  432. hp2:=pu.u;
  433. break;
  434. end;
  435. pu:=tused_unit(pu.next);
  436. end;
  437. if not assigned(hp2) then
  438. hp2:=registerunit(current_module,sorg,fn)
  439. else
  440. Message1(sym_e_duplicate_id,s);
  441. { Create unitsym, we need to use the name as specified, we
  442. can not use the modulename because that can be different
  443. when -Un is used }
  444. current_tokenpos:=filepos;
  445. unitsym:=cunitsym.create(sorg,nil);
  446. { the current module uses the unit hp2 }
  447. current_module.addusedunit(hp2,true,unitsym);
  448. end
  449. else
  450. Message1(sym_e_duplicate_id,s);
  451. if token=_COMMA then
  452. begin
  453. pattern:='';
  454. consume(_COMMA);
  455. end
  456. else
  457. break;
  458. until false;
  459. { Load the units }
  460. pu:=tused_unit(current_module.used_units.first);
  461. while assigned(pu) do
  462. begin
  463. { Only load the units that are in the current
  464. (interface/implementation) uses clause }
  465. if pu.in_uses and
  466. (pu.in_interface=current_module.in_interface) then
  467. begin
  468. tppumodule(pu.u).loadppu;
  469. { is our module compiled? then we can stop }
  470. if current_module.state=ms_compiled then
  471. exit;
  472. { add this unit to the dependencies }
  473. pu.u.adddependency(current_module);
  474. { save crc values }
  475. pu.checksum:=pu.u.crc;
  476. pu.interface_checksum:=pu.u.interface_crc;
  477. pu.indirect_checksum:=pu.u.indirect_crc;
  478. if tppumodule(pu.u).nsprefix<>'' then
  479. begin
  480. { use the name as declared in the uses section for -Un }
  481. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  482. s:=upper(sorg);
  483. { check whether the module was already loaded }
  484. hp2:=nil;
  485. pu2:=tused_unit(current_module.used_units.first);
  486. while assigned(pu2) and (pu2<>pu) do
  487. begin
  488. if (pu2.u.modulename^=s) then
  489. begin
  490. hp2:=pu.u;
  491. break;
  492. end;
  493. pu2:=tused_unit(pu2.next);
  494. end;
  495. if assigned(hp2) then
  496. begin
  497. MessagePos1(pu.unitsym.fileinfo,sym_e_duplicate_id,s);
  498. pu:=tused_unit(pu.next);
  499. continue;
  500. end;
  501. { update unitsym now that we have access to the full name }
  502. pu.unitsym.free;
  503. pu.unitsym:=cunitsym.create(sorg,pu.u);
  504. end
  505. else
  506. begin
  507. { connect unitsym to the module }
  508. pu.unitsym.module:=pu.u;
  509. pu.unitsym.register_sym;
  510. end;
  511. tabstractunitsymtable(current_module.localsymtable).insertunit(pu.unitsym);
  512. { add to symtable stack }
  513. if assigned(preservest) then
  514. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  515. else
  516. symtablestack.push(pu.u.globalsymtable);
  517. if (m_mac in current_settings.modeswitches) and
  518. assigned(pu.u.globalmacrosymtable) then
  519. macrosymtablestack.push(pu.u.globalmacrosymtable);
  520. { check hints }
  521. pu.check_hints;
  522. end;
  523. pu:=tused_unit(pu.next);
  524. end;
  525. end;
  526. procedure reset_all_defs;
  527. begin
  528. if assigned(current_module.wpoinfo) then
  529. current_module.wpoinfo.resetdefs;
  530. end;
  531. procedure free_localsymtables(st:TSymtable);
  532. var
  533. i : longint;
  534. def : tstoreddef;
  535. pd : tprocdef;
  536. begin
  537. for i:=0 to st.DefList.Count-1 do
  538. begin
  539. def:=tstoreddef(st.DefList[i]);
  540. if def.typ=procdef then
  541. begin
  542. pd:=tprocdef(def);
  543. if assigned(pd.localst) and
  544. (pd.localst.symtabletype<>staticsymtable) and
  545. not(po_inline in pd.procoptions) then
  546. begin
  547. free_localsymtables(pd.localst);
  548. pd.localst.free;
  549. pd.localst:=nil;
  550. end;
  551. pd.freeimplprocdefinfo;
  552. pd.done_paraloc_info(calleeside);
  553. end;
  554. end;
  555. end;
  556. procedure free_unregistered_localsymtable_elements;
  557. var
  558. i: longint;
  559. def: tdef;
  560. sym: tsym;
  561. begin
  562. for i:=current_module.localsymtable.deflist.count-1 downto 0 do
  563. begin
  564. def:=tdef(current_module.localsymtable.deflist[i]);
  565. { since commit 48986 deflist might have NIL entries }
  566. if not assigned(def) then
  567. continue;
  568. { this also frees def, as the defs are owned by the symtable }
  569. if not def.is_registered and
  570. not(df_not_registered_no_free in def.defoptions) then
  571. begin
  572. { if it's a procdef, unregister it from its procsym first,
  573. unless that sym hasn't been registered either (it's possible
  574. to have one overload in the interface and another in the
  575. implementation) }
  576. if (def.typ=procdef) and
  577. tprocdef(def).procsym.is_registered then
  578. tprocsym(tprocdef(def).procsym).ProcdefList.Remove(def);
  579. current_module.localsymtable.deletedef(def);
  580. end;
  581. end;
  582. { from high to low so we hopefully have moves of less data }
  583. for i:=current_module.localsymtable.symlist.count-1 downto 0 do
  584. begin
  585. sym:=tsym(current_module.localsymtable.symlist[i]);
  586. { this also frees sym, as the symbols are owned by the symtable }
  587. if not sym.is_registered then
  588. current_module.localsymtable.Delete(sym);
  589. end;
  590. end;
  591. procedure setupglobalswitches;
  592. begin
  593. if (cs_create_pic in current_settings.moduleswitches) then
  594. begin
  595. def_system_macro('FPC_PIC');
  596. def_system_macro('PIC');
  597. end;
  598. end;
  599. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  600. var
  601. ps : tprocsym;
  602. pd : tprocdef;
  603. begin
  604. { there should be no current_procinfo available }
  605. if assigned(current_procinfo) then
  606. internalerror(200304275);
  607. {Generate a procsym for main}
  608. ps:=cprocsym.create('$'+name);
  609. { always register the symbol }
  610. ps.register_sym;
  611. { main are allways used }
  612. inc(ps.refs);
  613. st.insert(ps);
  614. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  615. { We don't need a local symtable, change it into the static symtable }
  616. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  617. begin
  618. pd.localst.free;
  619. pd.localst:=st;
  620. end
  621. else if (potype=potype_pkgstub) and
  622. (target_info.system in systems_all_windows+systems_nativent) then
  623. pd.proccalloption:=pocall_stdcall
  624. else
  625. pd.proccalloption:=pocall_cdecl;
  626. handle_calling_convention(pd,hcc_default_actions_impl);
  627. { set procinfo and current_procinfo.procdef }
  628. result:=tcgprocinfo(cprocinfo.create(nil));
  629. result.procdef:=pd;
  630. { main proc does always a call e.g. to init system unit }
  631. if potype<>potype_pkgstub then
  632. include(result.flags,pi_do_call);
  633. end;
  634. procedure release_main_proc(pi:tcgprocinfo);
  635. begin
  636. { remove localst as it was replaced by staticsymtable }
  637. pi.procdef.localst:=nil;
  638. { remove procinfo }
  639. current_module.procinfo:=nil;
  640. pi.free;
  641. pi:=nil;
  642. end;
  643. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  644. procedure maybe_load_got;
  645. {$if defined(i386) or defined (sparcgen)}
  646. var
  647. gotvarsym : tstaticvarsym;
  648. {$endif i386 or sparcgen}
  649. begin
  650. {$if defined(i386) or defined(sparcgen)}
  651. if (cs_create_pic in current_settings.moduleswitches) and
  652. (tf_pic_uses_got in target_info.flags) then
  653. begin
  654. { insert symbol for got access in assembler code}
  655. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  656. vs_value,voidpointertype,[vo_is_external]);
  657. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  658. current_module.localsymtable.insert(gotvarsym);
  659. { avoid unnecessary warnings }
  660. gotvarsym.varstate:=vs_read;
  661. gotvarsym.refs:=1;
  662. end;
  663. {$endif i386 or sparcgen}
  664. end;
  665. function gen_implicit_initfinal(flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  666. begin
  667. { create procdef }
  668. case flag of
  669. mf_init :
  670. begin
  671. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit$'),potype_unitinit,st);
  672. result.procdef.aliasnames.concat(make_mangledname('INIT$',current_module.localsymtable,''));
  673. end;
  674. mf_finalize :
  675. begin
  676. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  677. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  678. if (not current_module.is_unit) then
  679. result.procdef.aliasnames.concat('PASCALFINALIZE');
  680. end;
  681. else
  682. internalerror(200304253);
  683. end;
  684. result.code:=cnothingnode.create;
  685. end;
  686. procedure copy_macro(p:TObject; arg:pointer);
  687. begin
  688. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  689. end;
  690. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  691. var
  692. deprecated_seen,
  693. last_is_deprecated:boolean;
  694. begin
  695. try_consume_hintdirective:=false;
  696. deprecated_seen:=false;
  697. repeat
  698. last_is_deprecated:=false;
  699. case idtoken of
  700. _LIBRARY :
  701. begin
  702. include(moduleopt,mo_hint_library);
  703. try_consume_hintdirective:=true;
  704. end;
  705. _DEPRECATED :
  706. begin
  707. { allow deprecated only once }
  708. if deprecated_seen then
  709. break;
  710. include(moduleopt,mo_hint_deprecated);
  711. try_consume_hintdirective:=true;
  712. last_is_deprecated:=true;
  713. deprecated_seen:=true;
  714. end;
  715. _EXPERIMENTAL :
  716. begin
  717. include(moduleopt,mo_hint_experimental);
  718. try_consume_hintdirective:=true;
  719. end;
  720. _PLATFORM :
  721. begin
  722. include(moduleopt,mo_hint_platform);
  723. try_consume_hintdirective:=true;
  724. end;
  725. _UNIMPLEMENTED :
  726. begin
  727. include(moduleopt,mo_hint_unimplemented);
  728. try_consume_hintdirective:=true;
  729. end;
  730. else
  731. break;
  732. end;
  733. consume(Token);
  734. { handle deprecated message }
  735. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  736. begin
  737. if deprecatedmsg<>nil then
  738. internalerror(201001221);
  739. if token=_CSTRING then
  740. deprecatedmsg:=stringdup(cstringpattern)
  741. else
  742. deprecatedmsg:=stringdup(pattern);
  743. consume(token);
  744. include(moduleopt,mo_has_deprecated_msg);
  745. end;
  746. until false;
  747. end;
  748. {$ifdef jvm}
  749. procedure addmoduleclass;
  750. var
  751. def: tobjectdef;
  752. typesym: ttypesym;
  753. begin
  754. { java_jlobject may not have been parsed yet (system unit); in any
  755. case, we only use this to refer to the class type, so inheritance
  756. does not matter }
  757. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  758. include(def.objectoptions,oo_is_external);
  759. include(def.objectoptions,oo_is_sealed);
  760. def.objextname:=stringdup(current_module.realmodulename^);
  761. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  762. symtablestack.top.insert(typesym);
  763. end;
  764. {$endif jvm}
  765. type
  766. tfinishstate=record
  767. init_procinfo:tcgprocinfo;
  768. finalize_procinfo:tcgprocinfo;
  769. end;
  770. pfinishstate=^tfinishstate;
  771. procedure finish_unit(module:tmodule;immediate:boolean);forward;
  772. function proc_unit:boolean;
  773. var
  774. main_file: tinputfile;
  775. s1,s2 : ^string; {Saves stack space}
  776. finalize_procinfo,
  777. init_procinfo : tcgprocinfo;
  778. unitname : ansistring;
  779. unitname8 : string[8];
  780. i,j : longint;
  781. finishstate:pfinishstate;
  782. globalstate:pglobalstate;
  783. consume_semicolon_after_uses:boolean;
  784. feature : tfeature;
  785. begin
  786. result:=true;
  787. init_procinfo:=nil;
  788. finalize_procinfo:=nil;
  789. if m_mac in current_settings.modeswitches then
  790. current_module.mode_switch_allowed:= false;
  791. consume(_UNIT);
  792. if compile_level=1 then
  793. Status.IsExe:=false;
  794. unitname:=orgpattern;
  795. consume(_ID);
  796. while token=_POINT do
  797. begin
  798. consume(_POINT);
  799. unitname:=unitname+'.'+orgpattern;
  800. consume(_ID);
  801. end;
  802. { create filenames and unit name }
  803. main_file := current_scanner.inputfile;
  804. while assigned(main_file.next) do
  805. main_file := main_file.next;
  806. new(s1);
  807. s1^:=current_module.modulename^;
  808. current_module.SetFileName(main_file.path+main_file.name,true);
  809. current_module.SetModuleName(unitname);
  810. {$ifdef DEBUG_NODE_XML}
  811. XMLInitializeNodeFile('unit', unitname);
  812. {$endif DEBUG_NODE_XML}
  813. { check for system unit }
  814. new(s2);
  815. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  816. unitname8:=copy(current_module.modulename^,1,8);
  817. if (cs_check_unit_name in current_settings.globalswitches) and
  818. (
  819. not(
  820. (current_module.modulename^=s2^) or
  821. (
  822. (length(current_module.modulename^)>8) and
  823. (unitname8=s2^)
  824. )
  825. )
  826. or
  827. (
  828. (length(s1^)>8) and
  829. (s1^<>current_module.modulename^)
  830. )
  831. ) then
  832. Message2(unit_e_illegal_unit_name,current_module.realmodulename^,s1^);
  833. if (current_module.modulename^='SYSTEM') then
  834. include(current_settings.moduleswitches,cs_compilesystem);
  835. dispose(s2);
  836. dispose(s1);
  837. if (target_info.system in systems_unit_program_exports) then
  838. exportlib.preparelib(current_module.realmodulename^);
  839. { parse hint directives }
  840. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  841. consume(_SEMICOLON);
  842. { handle the global switches, do this before interface, because after interface has been
  843. read, all following directives are parsed as well }
  844. setupglobalswitches;
  845. { generate now the global symboltable,
  846. define first as local to overcome dependency conflicts }
  847. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  848. { insert unitsym of this unit to prevent other units having
  849. the same name }
  850. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  851. { load default system unit, it must be loaded before interface is parsed
  852. else we cannot use e.g. feature switches before the next real token }
  853. loadsystemunit;
  854. { system unit is loaded, now insert feature defines }
  855. for feature:=low(tfeature) to high(tfeature) do
  856. if feature in features then
  857. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  858. consume(_INTERFACE);
  859. { global switches are read, so further changes aren't allowed }
  860. current_module.in_global:=false;
  861. message1(unit_u_loading_interface_units,current_module.modulename^);
  862. { update status }
  863. status.currentmodule:=current_module.realmodulename^;
  864. { maybe turn off m_objpas if we are compiling objpas }
  865. if (current_module.modulename^='OBJPAS') then
  866. exclude(current_settings.modeswitches,m_objpas);
  867. { maybe turn off m_mac if we are compiling macpas }
  868. if (current_module.modulename^='MACPAS') then
  869. exclude(current_settings.modeswitches,m_mac);
  870. parse_only:=true;
  871. { load default units, like language mode units }
  872. if not(cs_compilesystem in current_settings.moduleswitches) then
  873. loaddefaultunits;
  874. { insert qualifier for the system unit (allows system.writeln) }
  875. if not(cs_compilesystem in current_settings.moduleswitches) and
  876. (token=_USES) then
  877. begin
  878. loadunits(nil);
  879. { has it been compiled at a higher level ?}
  880. if current_module.state=ms_compiled then
  881. begin
  882. Message1(parser_u_already_compiled,current_module.realmodulename^);
  883. exit;
  884. end;
  885. consume_semicolon_after_uses:=true;
  886. end
  887. else
  888. consume_semicolon_after_uses:=false;
  889. { move the global symtable from the temporary local to global }
  890. current_module.globalsymtable:=current_module.localsymtable;
  891. current_module.localsymtable:=nil;
  892. { number all units, so we know if a unit is used by this unit or
  893. needs to be added implicitly }
  894. current_module.updatemaps;
  895. { consume the semicolon after maps have been updated else conditional compiling expressions
  896. might cause internal errors, see tw8611 }
  897. if consume_semicolon_after_uses then
  898. consume(_SEMICOLON);
  899. { create whole program optimisation information (may already be
  900. updated in the interface, e.g., in case of classrefdef typed
  901. constants }
  902. current_module.wpoinfo:=tunitwpoinfo.create;
  903. { ... parse the declarations }
  904. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  905. symtablestack.push(current_module.globalsymtable);
  906. {$ifdef jvm}
  907. { fake classdef to represent the class corresponding to the unit }
  908. addmoduleclass;
  909. {$endif}
  910. read_interface_declarations;
  911. { Export macros defined in the interface for macpas. The macros
  912. are put in the globalmacrosymtable that will only be used by other
  913. units. The current unit continues to use the localmacrosymtable }
  914. if (m_mac in current_settings.modeswitches) then
  915. begin
  916. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  917. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  918. end;
  919. { leave when we got an error }
  920. if (Errorcount>0) and not status.skip_error then
  921. begin
  922. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  923. status.skip_error:=true;
  924. symtablestack.pop(current_module.globalsymtable);
  925. {$ifdef DEBUG_NODE_XML}
  926. XMLFinalizeNodeFile('unit');
  927. {$endif DEBUG_NODE_XML}
  928. exit;
  929. end;
  930. { Our interface is compiled, generate CRC and switch to implementation }
  931. if not(cs_compilesystem in current_settings.moduleswitches) and
  932. (Errorcount=0) then
  933. tppumodule(current_module).getppucrc;
  934. current_module.in_interface:=false;
  935. current_module.interface_compiled:=true;
  936. { First reload all units depending on our interface, we need to do this
  937. in the implementation part to prevent erroneous circular references }
  938. tppumodule(current_module).setdefgeneration;
  939. tppumodule(current_module).reload_flagged_units;
  940. { Parse the implementation section }
  941. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  942. current_module.interface_only:=true
  943. else
  944. current_module.interface_only:=false;
  945. parse_only:=false;
  946. { create static symbol table }
  947. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  948. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  949. maybe_load_got;
  950. if not current_module.interface_only then
  951. begin
  952. consume(_IMPLEMENTATION);
  953. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  954. { Read the implementation units }
  955. if token=_USES then
  956. begin
  957. loadunits(current_module.globalsymtable);
  958. consume(_SEMICOLON);
  959. end;
  960. end;
  961. if current_module.state=ms_compiled then
  962. begin
  963. symtablestack.pop(current_module.globalsymtable);
  964. exit;
  965. end;
  966. { All units are read, now give them a number }
  967. current_module.updatemaps;
  968. { further, changing the globalsymtable is not allowed anymore }
  969. current_module.globalsymtable.sealed:=true;
  970. symtablestack.push(current_module.localsymtable);
  971. if not current_module.interface_only then
  972. begin
  973. Message1(parser_u_parsing_implementation,current_module.modulename^);
  974. if current_module.in_interface then
  975. internalerror(200212285);
  976. { Compile the unit }
  977. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init$'),potype_unitinit,current_module.localsymtable);
  978. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',current_module.localsymtable,''));
  979. init_procinfo.parse_body;
  980. { save file pos for debuginfo }
  981. current_module.mainfilepos:=init_procinfo.entrypos;
  982. { parse finalization section }
  983. if token=_FINALIZATION then
  984. begin
  985. { Compile the finalize }
  986. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize$'),potype_unitfinalize,current_module.localsymtable);
  987. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  988. finalize_procinfo.parse_body;
  989. end
  990. end;
  991. { remove all units that we are waiting for that are already waiting for
  992. us => breaking up circles }
  993. for i:=0 to current_module.waitingunits.count-1 do
  994. for j:=current_module.waitingforunit.count-1 downto 0 do
  995. if current_module.waitingunits[i]=current_module.waitingforunit[j] then
  996. current_module.waitingforunit.delete(j);
  997. {$ifdef DEBUG_UNITWAITING}
  998. Writeln('Units waiting for ', current_module.modulename^, ': ',
  999. current_module.waitingforunit.Count);
  1000. {$endif}
  1001. result:=current_module.waitingforunit.count=0;
  1002. { save all information that is needed for finishing the unit }
  1003. New(finishstate);
  1004. finishstate^.init_procinfo:=init_procinfo;
  1005. finishstate^.finalize_procinfo:=finalize_procinfo;
  1006. current_module.finishstate:=finishstate;
  1007. if result then
  1008. finish_unit(current_module,true)
  1009. else
  1010. begin
  1011. { save the current state, so the parsing can continue where we left
  1012. of here }
  1013. New(globalstate);
  1014. save_global_state(globalstate^,true);
  1015. current_module.globalstate:=globalstate;
  1016. end;
  1017. end;
  1018. procedure finish_unit(module:tmodule;immediate:boolean);
  1019. function is_assembler_generated:boolean;
  1020. var
  1021. hal : tasmlisttype;
  1022. begin
  1023. result:=false;
  1024. if Errorcount=0 then
  1025. begin
  1026. for hal:=low(TasmlistType) to high(TasmlistType) do
  1027. if not current_asmdata.asmlists[hal].empty then
  1028. begin
  1029. result:=true;
  1030. exit;
  1031. end;
  1032. end;
  1033. end;
  1034. procedure module_is_done;inline;
  1035. begin
  1036. dispose(pglobalstate(current_module.globalstate));
  1037. current_module.globalstate:=nil;
  1038. dispose(pfinishstate(current_module.finishstate));
  1039. current_module.finishstate:=nil;
  1040. end;
  1041. var
  1042. {$ifdef EXTDEBUG}
  1043. store_crc,
  1044. {$endif EXTDEBUG}
  1045. store_interface_crc,
  1046. store_indirect_crc: cardinal;
  1047. force_init_final : boolean;
  1048. init_procinfo,
  1049. finalize_procinfo : tcgprocinfo;
  1050. i : longint;
  1051. ag : boolean;
  1052. finishstate : tfinishstate;
  1053. globalstate : tglobalstate;
  1054. waitingmodule : tmodule;
  1055. begin
  1056. fillchar(globalstate,sizeof(tglobalstate),0);
  1057. if not immediate then
  1058. begin
  1059. {$ifdef DEBUG_UNITWAITING}
  1060. writeln('finishing waiting unit ''', module.modulename^, '''');
  1061. {$endif DEBUG_UNITWAITING}
  1062. { restore the state when we stopped working on the unit }
  1063. save_global_state(globalstate,true);
  1064. if not assigned(module.globalstate) then
  1065. internalerror(2012091802);
  1066. restore_global_state(pglobalstate(module.globalstate)^,true);
  1067. end;
  1068. { current_module is now module }
  1069. if not assigned(current_module.finishstate) then
  1070. internalerror(2012091801);
  1071. finishstate:=pfinishstate(current_module.finishstate)^;
  1072. finalize_procinfo:=finishstate.finalize_procinfo;
  1073. init_procinfo:=finishstate.init_procinfo;
  1074. { Generate specializations of objectdefs methods }
  1075. generate_specialization_procs;
  1076. { Generate VMTs }
  1077. if Errorcount=0 then
  1078. begin
  1079. write_vmts(current_module.globalsymtable,true);
  1080. write_vmts(current_module.localsymtable,false);
  1081. end;
  1082. { add implementations for synthetic method declarations added by
  1083. the compiler }
  1084. add_synthetic_method_implementations(current_module.globalsymtable);
  1085. add_synthetic_method_implementations(current_module.localsymtable);
  1086. { generate construction functions for all attributes in the unit }
  1087. generate_attr_constrs(current_module.used_rtti_attrs);
  1088. { if the unit contains ansi/widestrings, initialization and
  1089. finalization code must be forced }
  1090. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1091. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1092. { should we force unit initialization? }
  1093. { this is a hack, but how can it be done better ? }
  1094. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1095. is it needed at all? (Sergei) }
  1096. { it's needed in case cnodeutils.force_init = true }
  1097. if (force_init_final or cnodeutils.force_init) and
  1098. (
  1099. not assigned(init_procinfo) or
  1100. has_no_code(init_procinfo.code)
  1101. ) then
  1102. begin
  1103. { first release the not used init procinfo }
  1104. if assigned(init_procinfo) then
  1105. begin
  1106. release_proc_symbol(init_procinfo.procdef);
  1107. release_main_proc(init_procinfo);
  1108. end;
  1109. init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable);
  1110. end;
  1111. if (force_init_final or cnodeutils.force_final) and
  1112. (
  1113. not assigned(finalize_procinfo) or
  1114. has_no_code(finalize_procinfo.code)
  1115. ) then
  1116. begin
  1117. { first release the not used finalize procinfo }
  1118. if assigned(finalize_procinfo) then
  1119. begin
  1120. release_proc_symbol(finalize_procinfo.procdef);
  1121. release_main_proc(finalize_procinfo);
  1122. end;
  1123. finalize_procinfo:=gen_implicit_initfinal(mf_finalize,current_module.localsymtable);
  1124. end;
  1125. { Now both init and finalize bodies are read and it is known
  1126. which variables are used in both init and finalize we can now
  1127. generate the code. This is required to prevent putting a variable in
  1128. a register that is also used in the finalize body (PFV) }
  1129. if assigned(init_procinfo) then
  1130. begin
  1131. if (force_init_final or cnodeutils.force_init) or
  1132. not(has_no_code(init_procinfo.code)) then
  1133. begin
  1134. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1135. init_procinfo.generate_code;
  1136. include(current_module.moduleflags,mf_init);
  1137. end
  1138. else
  1139. release_proc_symbol(init_procinfo.procdef);
  1140. init_procinfo.resetprocdef;
  1141. release_main_proc(init_procinfo);
  1142. end;
  1143. if assigned(finalize_procinfo) then
  1144. begin
  1145. if force_init_final or
  1146. cnodeutils.force_init or
  1147. not(has_no_code(finalize_procinfo.code)) then
  1148. begin
  1149. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1150. finalize_procinfo.generate_code;
  1151. include(current_module.moduleflags,mf_finalize);
  1152. end
  1153. else
  1154. release_proc_symbol(finalize_procinfo.procdef);
  1155. finalize_procinfo.resetprocdef;
  1156. release_main_proc(finalize_procinfo);
  1157. end;
  1158. symtablestack.pop(current_module.localsymtable);
  1159. symtablestack.pop(current_module.globalsymtable);
  1160. { the last char should always be a point }
  1161. consume(_POINT);
  1162. { reset wpo flags for all defs }
  1163. reset_all_defs;
  1164. if (Errorcount=0) then
  1165. begin
  1166. { tests, if all (interface) forwards are resolved }
  1167. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1168. { check if all private fields are used }
  1169. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1170. { test static symtable }
  1171. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1172. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1173. tstoredsymtable(current_module.localsymtable).check_forwards;
  1174. tstoredsymtable(current_module.localsymtable).checklabels;
  1175. { used units }
  1176. current_module.allunitsused;
  1177. end;
  1178. { leave when we got an error }
  1179. if (Errorcount>0) and not status.skip_error then
  1180. begin
  1181. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1182. status.skip_error:=true;
  1183. module_is_done;
  1184. if not immediate then
  1185. restore_global_state(globalstate,true);
  1186. {$ifdef DEBUG_NODE_XML}
  1187. XMLFinalizeNodeFile('unit');
  1188. {$endif DEBUG_NODE_XML}
  1189. exit;
  1190. end;
  1191. { if an Objective-C module, generate rtti and module info }
  1192. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1193. { do we need to add the variants unit? }
  1194. maybeloadvariantsunit;
  1195. { generate rtti/init tables }
  1196. write_persistent_type_info(current_module.globalsymtable,true);
  1197. write_persistent_type_info(current_module.localsymtable,false);
  1198. { Tables }
  1199. cnodeutils.InsertThreadvars;
  1200. { Resource strings }
  1201. GenerateResourceStrings;
  1202. { Widestring typed constants }
  1203. cnodeutils.InsertWideInits;
  1204. { Resourcestring references }
  1205. cnodeutils.InsertResStrInits;
  1206. { generate debuginfo }
  1207. if (cs_debuginfo in current_settings.moduleswitches) then
  1208. current_debuginfo.inserttypeinfo;
  1209. { generate imports }
  1210. if current_module.ImportLibraryList.Count>0 then
  1211. importlib.generatelib;
  1212. { insert own objectfile, or say that it's in a library
  1213. (no check for an .o when loading) }
  1214. ag:=is_assembler_generated;
  1215. if ag then
  1216. insertobjectfile
  1217. else
  1218. begin
  1219. current_module.headerflags:=current_module.headerflags or uf_no_link;
  1220. exclude(current_module.moduleflags,mf_has_stabs_debuginfo);
  1221. exclude(current_module.moduleflags,mf_has_dwarf_debuginfo);
  1222. end;
  1223. if ag then
  1224. begin
  1225. { create callframe info }
  1226. create_dwarf_frame;
  1227. { assemble }
  1228. create_objectfile;
  1229. end;
  1230. { Write out the ppufile after the object file has been created }
  1231. store_interface_crc:=current_module.interface_crc;
  1232. store_indirect_crc:=current_module.indirect_crc;
  1233. {$ifdef EXTDEBUG}
  1234. store_crc:=current_module.crc;
  1235. {$endif EXTDEBUG}
  1236. if (Errorcount=0) then
  1237. tppumodule(current_module).writeppu;
  1238. if not(cs_compilesystem in current_settings.moduleswitches) then
  1239. begin
  1240. if store_interface_crc<>current_module.interface_crc then
  1241. Message1(unit_u_interface_crc_changed,current_module.ppufilename);
  1242. if store_indirect_crc<>current_module.indirect_crc then
  1243. Message1(unit_u_indirect_crc_changed,current_module.ppufilename);
  1244. end;
  1245. {$ifdef EXTDEBUG}
  1246. if not(cs_compilesystem in current_settings.moduleswitches) then
  1247. if (store_crc<>current_module.crc) then
  1248. Message1(unit_u_implementation_crc_changed,current_module.ppufilename);
  1249. {$endif EXTDEBUG}
  1250. { release unregistered defs/syms from the localsymtable }
  1251. free_unregistered_localsymtable_elements;
  1252. { release local symtables that are not needed anymore }
  1253. free_localsymtables(current_module.globalsymtable);
  1254. free_localsymtables(current_module.localsymtable);
  1255. { leave when we got an error }
  1256. if (Errorcount>0) and not status.skip_error then
  1257. begin
  1258. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1259. status.skip_error:=true;
  1260. module_is_done;
  1261. if not immediate then
  1262. restore_global_state(globalstate,true);
  1263. {$ifdef DEBUG_NODE_XML}
  1264. XMLFinalizeNodeFile('unit');
  1265. {$endif DEBUG_NODE_XML}
  1266. exit;
  1267. end;
  1268. {$ifdef debug_devirt}
  1269. { print out all instantiated class/object types }
  1270. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1271. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1272. begin
  1273. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1274. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1275. objectdef:
  1276. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1277. odt_object:
  1278. writeln(' (object)');
  1279. odt_class:
  1280. writeln(' (class)');
  1281. else
  1282. internalerror(2008101103);
  1283. end;
  1284. else
  1285. internalerror(2008101104);
  1286. end;
  1287. end;
  1288. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1289. begin
  1290. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1291. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1292. objectdef:
  1293. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1294. odt_class:
  1295. writeln(' (classrefdef)');
  1296. else
  1297. internalerror(2008101105);
  1298. end
  1299. else
  1300. internalerror(2008101102);
  1301. end;
  1302. end;
  1303. {$endif debug_devirt}
  1304. Message1(unit_u_finished_compiling,current_module.modulename^);
  1305. module_is_done;
  1306. if not immediate then
  1307. restore_global_state(globalstate,true);
  1308. for i:=0 to module.waitingunits.count-1 do
  1309. begin
  1310. waitingmodule:=tmodule(module.waitingunits[i]);
  1311. waitingmodule.waitingforunit.remove(module);
  1312. { only finish the module if it isn't already finished }
  1313. if (waitingmodule.waitingforunit.count=0) and
  1314. assigned(waitingmodule.finishstate) then
  1315. begin
  1316. finish_unit(waitingmodule,false);
  1317. waitingmodule.end_of_parsing;
  1318. end;
  1319. end;
  1320. {$ifdef DEBUG_NODE_XML}
  1321. XMLFinalizeNodeFile('unit');
  1322. {$endif DEBUG_NODE_XML}
  1323. end;
  1324. procedure proc_package;
  1325. var
  1326. main_file : tinputfile;
  1327. hp,hp2 : tmodule;
  1328. pkg : tpcppackage;
  1329. {finalize_procinfo,
  1330. init_procinfo,}
  1331. main_procinfo : tcgprocinfo;
  1332. force_init_final : boolean;
  1333. uu : tused_unit;
  1334. module_name: ansistring;
  1335. pentry: ppackageentry;
  1336. feature : tfeature;
  1337. begin
  1338. Status.IsPackage:=true;
  1339. Status.IsExe:=true;
  1340. parse_only:=false;
  1341. main_procinfo:=nil;
  1342. {init_procinfo:=nil;
  1343. finalize_procinfo:=nil;}
  1344. if not (tf_supports_packages in target_info.flags) then
  1345. message1(parser_e_packages_not_supported,target_info.name);
  1346. if not RelocSectionSetExplicitly then
  1347. RelocSection:=true;
  1348. { Relocation works only without stabs under Windows when }
  1349. { external linker (LD) is used. LD generates relocs for }
  1350. { stab sections which is not loaded in memory. It causes }
  1351. { AV error when DLL is loaded and relocation is needed. }
  1352. { Internal linker does not have this problem. }
  1353. if RelocSection and
  1354. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1355. (cs_link_extern in current_settings.globalswitches) then
  1356. begin
  1357. include(current_settings.globalswitches,cs_link_strip);
  1358. { Warning stabs info does not work with reloc section !! }
  1359. if (cs_debuginfo in current_settings.moduleswitches) and
  1360. (target_dbg.id=dbg_stabs) then
  1361. begin
  1362. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1363. Message(parser_w_parser_win32_debug_needs_WN);
  1364. exclude(current_settings.moduleswitches,cs_debuginfo);
  1365. end;
  1366. end;
  1367. { get correct output names }
  1368. main_file := current_scanner.inputfile;
  1369. while assigned(main_file.next) do
  1370. main_file := main_file.next;
  1371. current_module.SetFileName(main_file.path+main_file.name,true);
  1372. { consume _PACKAGE word }
  1373. consume(_ID);
  1374. module_name:=orgpattern;
  1375. consume(_ID);
  1376. while token=_POINT do
  1377. begin
  1378. consume(_POINT);
  1379. module_name:=module_name+'.'+orgpattern;
  1380. consume(_ID);
  1381. end;
  1382. current_module.setmodulename(module_name);
  1383. current_module.ispackage:=true;
  1384. exportlib.preparelib(module_name);
  1385. pkg:=tpcppackage.create(module_name);
  1386. if tf_library_needs_pic in target_info.flags then
  1387. include(current_settings.moduleswitches,cs_create_pic);
  1388. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1389. read, all following directives are parsed as well }
  1390. setupglobalswitches;
  1391. {$ifdef DEBUG_NODE_XML}
  1392. XMLInitializeNodeFile('package', module_name);
  1393. {$endif DEBUG_NODE_XML}
  1394. consume(_SEMICOLON);
  1395. { global switches are read, so further changes aren't allowed }
  1396. current_module.in_global:=false;
  1397. { set implementation flag }
  1398. current_module.in_interface:=false;
  1399. current_module.interface_compiled:=true;
  1400. { insert after the unit symbol tables the static symbol table }
  1401. { of the program }
  1402. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1403. { ensure that no packages are picked up from the options }
  1404. packagelist.clear;
  1405. {Read the packages used by the package we compile.}
  1406. if (token=_ID) and (idtoken=_REQUIRES) then
  1407. begin
  1408. { consume _REQUIRES word }
  1409. consume(_ID);
  1410. while true do
  1411. begin
  1412. if token=_ID then
  1413. begin
  1414. module_name:=orgpattern;
  1415. consume(_ID);
  1416. while token=_POINT do
  1417. begin
  1418. consume(_POINT);
  1419. module_name:=module_name+'.'+orgpattern;
  1420. consume(_ID);
  1421. end;
  1422. add_package(module_name,false,true);
  1423. end
  1424. else
  1425. consume(_ID);
  1426. if token=_COMMA then
  1427. consume(_COMMA)
  1428. else
  1429. break;
  1430. end;
  1431. consume(_SEMICOLON);
  1432. end;
  1433. { now load all packages, so that we can determine whether a unit is
  1434. already provided by one of the loaded packages }
  1435. load_packages;
  1436. if packagelist.Count>0 then
  1437. begin
  1438. { this means the SYSTEM unit *must* be part of one of the required
  1439. packages, so load it }
  1440. AddUnit('system',false);
  1441. systemunit:=tglobalsymtable(symtablestack.top);
  1442. load_intern_types;
  1443. { system unit is loaded, now insert feature defines }
  1444. for feature:=low(tfeature) to high(tfeature) do
  1445. if feature in features then
  1446. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1447. end;
  1448. {Load the units used by the program we compile.}
  1449. if (token=_ID) and (idtoken=_CONTAINS) then
  1450. begin
  1451. { consume _CONTAINS word }
  1452. consume(_ID);
  1453. while true do
  1454. begin
  1455. if token=_ID then
  1456. begin
  1457. module_name:=orgpattern;
  1458. consume(_ID);
  1459. while token=_POINT do
  1460. begin
  1461. consume(_POINT);
  1462. module_name:=module_name+'.'+orgpattern;
  1463. consume(_ID);
  1464. end;
  1465. hp:=AddUnit(module_name);
  1466. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1467. begin
  1468. systemunit:=tglobalsymtable(hp.globalsymtable);
  1469. load_intern_types;
  1470. end;
  1471. end
  1472. else
  1473. consume(_ID);
  1474. if token=_COMMA then
  1475. consume(_COMMA)
  1476. else break;
  1477. end;
  1478. consume(_SEMICOLON);
  1479. end;
  1480. { All units are read, now give them a number }
  1481. current_module.updatemaps;
  1482. hp:=tmodule(loaded_units.first);
  1483. while assigned(hp) do
  1484. begin
  1485. if (hp<>current_module) and not assigned(hp.package) then
  1486. begin
  1487. if mf_package_deny in hp.moduleflags then
  1488. message1(package_e_unit_deny_package,hp.realmodulename^);
  1489. { part of the package's used, aka contained units? }
  1490. uu:=tused_unit(current_module.used_units.first);
  1491. while assigned(uu) do
  1492. begin
  1493. if uu.u=hp then
  1494. break;
  1495. uu:=tused_unit(uu.next);
  1496. end;
  1497. if not assigned(uu) then
  1498. message2(package_n_implicit_unit_import,hp.realmodulename^,current_module.realmodulename^);
  1499. end;
  1500. { was this unit listed as a contained unit? If so => error }
  1501. if (hp<>current_module) and assigned(hp.package) then
  1502. begin
  1503. uu:=tused_unit(current_module.used_units.first);
  1504. while assigned(uu) do
  1505. begin
  1506. if uu.u=hp then
  1507. break;
  1508. uu:=tused_unit(uu.next);
  1509. end;
  1510. if assigned(uu) then
  1511. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1512. end;
  1513. hp:=tmodule(hp.next);
  1514. end;
  1515. {Insert the name of the main program into the symbol table.}
  1516. if current_module.realmodulename^<>'' then
  1517. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1518. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1519. symtablestack.push(current_module.localsymtable);
  1520. { create whole program optimisation information }
  1521. current_module.wpoinfo:=tunitwpoinfo.create;
  1522. { should we force unit initialization? }
  1523. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1524. if force_init_final or cnodeutils.force_init then
  1525. {init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable)};
  1526. { Add symbol to the exports section for win32 so smartlinking a
  1527. DLL will include the edata section }
  1528. if assigned(exportlib) and
  1529. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1530. (mf_has_exports in current_module.moduleflags) then
  1531. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1532. { all labels must be defined before generating code }
  1533. if Errorcount=0 then
  1534. tstoredsymtable(current_module.localsymtable).checklabels;
  1535. symtablestack.pop(current_module.localsymtable);
  1536. { consume the last point }
  1537. consume(_END);
  1538. consume(_POINT);
  1539. if (Errorcount=0) then
  1540. begin
  1541. { test static symtable }
  1542. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1543. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1544. tstoredsymtable(current_module.localsymtable).check_forwards;
  1545. { Note: all contained units are considered as used }
  1546. end;
  1547. if target_info.system in systems_all_windows+systems_nativent then
  1548. begin
  1549. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,current_module.localsymtable);
  1550. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1551. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1552. main_procinfo.generate_code;
  1553. end;
  1554. {$ifdef DEBUG_NODE_XML}
  1555. XMLFinalizeNodeFile('package');
  1556. {$endif DEBUG_NODE_XML}
  1557. { leave when we got an error }
  1558. if (Errorcount>0) and not status.skip_error then
  1559. begin
  1560. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1561. status.skip_error:=true;
  1562. pkg.free;
  1563. exit;
  1564. end;
  1565. { remove all unused units, this happends when units are removed
  1566. from the uses clause in the source and the ppu was already being loaded }
  1567. hp:=tmodule(loaded_units.first);
  1568. while assigned(hp) do
  1569. begin
  1570. hp2:=hp;
  1571. hp:=tmodule(hp.next);
  1572. if assigned(hp2.package) then
  1573. add_package_unit_ref(hp2.package);
  1574. if hp2.is_unit and
  1575. not assigned(hp2.globalsymtable) then
  1576. loaded_units.remove(hp2);
  1577. end;
  1578. exportlib.ignoreduplicates:=true;
  1579. { force exports }
  1580. uu:=tused_unit(usedunits.first);
  1581. while assigned(uu) do
  1582. begin
  1583. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1584. begin
  1585. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1586. load_intern_types;
  1587. end;
  1588. if not assigned(uu.u.package) then
  1589. export_unit(uu.u);
  1590. uu:=tused_unit(uu.next);
  1591. end;
  1592. {$ifdef arm}
  1593. { Insert .pdata section for arm-wince.
  1594. It is needed for exception handling. }
  1595. if target_info.system in [system_arm_wince] then
  1596. InsertPData;
  1597. {$endif arm}
  1598. { generate debuginfo }
  1599. if (cs_debuginfo in current_settings.moduleswitches) then
  1600. current_debuginfo.inserttypeinfo;
  1601. exportlib.generatelib;
  1602. exportlib.ignoreduplicates:=false;
  1603. { create import libraries for all packages }
  1604. if packagelist.count>0 then
  1605. createimportlibfromexternals;
  1606. { generate imports }
  1607. if current_module.ImportLibraryList.Count>0 then
  1608. importlib.generatelib;
  1609. { Reference all DEBUGINFO sections from the main .fpc section }
  1610. if (cs_debuginfo in current_settings.moduleswitches) then
  1611. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1612. { insert own objectfile }
  1613. insertobjectfile;
  1614. { assemble and link }
  1615. create_objectfile;
  1616. { We might need the symbols info if not using
  1617. the default do_extractsymbolinfo
  1618. which is a dummy function PM }
  1619. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1620. { release all local symtables that are not needed anymore }
  1621. if (not needsymbolinfo) then
  1622. free_localsymtables(current_module.localsymtable);
  1623. { leave when we got an error }
  1624. if (Errorcount>0) and not status.skip_error then
  1625. begin
  1626. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1627. status.skip_error:=true;
  1628. pkg.free;
  1629. exit;
  1630. end;
  1631. if (not current_module.is_unit) then
  1632. begin
  1633. { we add all loaded units that are not part of a package to the
  1634. package; this includes units in the "contains" section as well
  1635. as implicitely imported ones }
  1636. hp:=tmodule(loaded_units.first);
  1637. while assigned(hp) do
  1638. begin
  1639. if (hp<>current_module) then
  1640. begin
  1641. if not assigned(hp.package) then
  1642. begin
  1643. pkg.addunit(hp);
  1644. check_for_indirect_package_usages(hp.used_units);
  1645. end
  1646. else
  1647. begin
  1648. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1649. if not assigned(pentry) then
  1650. internalerror(2015112301);
  1651. pkg.add_required_package(hp.package);
  1652. end;
  1653. end;
  1654. hp:=tmodule(hp.next);
  1655. end;
  1656. pkg.initmoduleinfo(current_module);
  1657. { create the executable when we are at level 1 }
  1658. if (compile_level=1) then
  1659. begin
  1660. { create global resource file by collecting all resource files }
  1661. CollectResourceFiles;
  1662. { write .def file }
  1663. if (cs_link_deffile in current_settings.globalswitches) then
  1664. deffile.writefile;
  1665. { generate the pcp file }
  1666. pkg.savepcp;
  1667. { insert all .o files from all loaded units and
  1668. unload the units, we don't need them anymore.
  1669. Keep the current_module because that is still needed }
  1670. hp:=tmodule(loaded_units.first);
  1671. while assigned(hp) do
  1672. begin
  1673. { only link in those units which should become part of this
  1674. package }
  1675. if not assigned(hp.package) then
  1676. linker.AddModuleFiles(hp);
  1677. hp2:=tmodule(hp.next);
  1678. if (hp<>current_module) and
  1679. (not needsymbolinfo) then
  1680. begin
  1681. loaded_units.remove(hp);
  1682. hp.free;
  1683. end;
  1684. hp:=hp2;
  1685. end;
  1686. { add the library of directly used packages }
  1687. add_package_libs(linker);
  1688. { and now link the package library }
  1689. linker.MakeSharedLibrary
  1690. end;
  1691. { Give Fatal with error count for linker errors }
  1692. if (Errorcount>0) and not status.skip_error then
  1693. begin
  1694. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1695. status.skip_error:=true;
  1696. end;
  1697. pkg.free;
  1698. end;
  1699. end;
  1700. procedure proc_program(islibrary : boolean);
  1701. type
  1702. TProgramParam = record
  1703. name : ansistring;
  1704. nr : dword;
  1705. end;
  1706. var
  1707. main_file : tinputfile;
  1708. hp,hp2 : tmodule;
  1709. initpd : tprocdef;
  1710. finalize_procinfo,
  1711. init_procinfo,
  1712. main_procinfo : tcgprocinfo;
  1713. force_init_final : boolean;
  1714. resources_used : boolean;
  1715. program_uses_checkpointer : boolean;
  1716. program_name : ansistring;
  1717. consume_semicolon_after_uses : boolean;
  1718. ps : tprogramparasym;
  1719. paramnum : longint;
  1720. textsym : ttypesym;
  1721. sc : array of TProgramParam;
  1722. i : Longint;
  1723. sysinitmod: tmodule;
  1724. feature : tfeature;
  1725. begin
  1726. Status.IsLibrary:=IsLibrary;
  1727. Status.IsPackage:=false;
  1728. Status.IsExe:=true;
  1729. parse_only:=false;
  1730. main_procinfo:=nil;
  1731. init_procinfo:=nil;
  1732. finalize_procinfo:=nil;
  1733. resources_used:=false;
  1734. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  1735. sc:=nil;
  1736. { DLL defaults to create reloc info }
  1737. if islibrary or (target_info.system in [system_aarch64_win64]) then
  1738. begin
  1739. if not RelocSectionSetExplicitly then
  1740. RelocSection:=true;
  1741. end;
  1742. { Relocation works only without stabs under Windows when }
  1743. { external linker (LD) is used. LD generates relocs for }
  1744. { stab sections which is not loaded in memory. It causes }
  1745. { AV error when DLL is loaded and relocation is needed. }
  1746. { Internal linker does not have this problem. }
  1747. if RelocSection and
  1748. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1749. (cs_link_extern in current_settings.globalswitches) then
  1750. begin
  1751. include(current_settings.globalswitches,cs_link_strip);
  1752. { Warning stabs info does not work with reloc section !! }
  1753. if (cs_debuginfo in current_settings.moduleswitches) and
  1754. (target_dbg.id=dbg_stabs) then
  1755. begin
  1756. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1757. Message(parser_w_parser_win32_debug_needs_WN);
  1758. exclude(current_settings.moduleswitches,cs_debuginfo);
  1759. end;
  1760. end;
  1761. { get correct output names }
  1762. main_file := current_scanner.inputfile;
  1763. while assigned(main_file.next) do
  1764. main_file := main_file.next;
  1765. current_module.SetFileName(main_file.path+main_file.name,true);
  1766. if islibrary then
  1767. begin
  1768. consume(_LIBRARY);
  1769. program_name:=orgpattern;
  1770. consume(_ID);
  1771. while token=_POINT do
  1772. begin
  1773. consume(_POINT);
  1774. program_name:=program_name+'.'+orgpattern;
  1775. consume(_ID);
  1776. end;
  1777. current_module.setmodulename(program_name);
  1778. current_module.islibrary:=true;
  1779. exportlib.preparelib(program_name);
  1780. if tf_library_needs_pic in target_info.flags then
  1781. begin
  1782. include(current_settings.moduleswitches,cs_create_pic);
  1783. { also set create_pic for all unit compilation }
  1784. include(init_settings.moduleswitches,cs_create_pic);
  1785. end;
  1786. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1787. read, all following directives are parsed as well }
  1788. setupglobalswitches;
  1789. consume(_SEMICOLON);
  1790. {$ifdef DEBUG_NODE_XML}
  1791. XMLInitializeNodeFile('library', program_name);
  1792. {$endif DEBUG_NODE_XML}
  1793. end
  1794. else
  1795. { is there an program head ? }
  1796. if token=_PROGRAM then
  1797. begin
  1798. consume(_PROGRAM);
  1799. program_name:=orgpattern;
  1800. consume(_ID);
  1801. while token=_POINT do
  1802. begin
  1803. consume(_POINT);
  1804. program_name:=program_name+'.'+orgpattern;
  1805. consume(_ID);
  1806. end;
  1807. current_module.setmodulename(program_name);
  1808. if (target_info.system in systems_unit_program_exports) then
  1809. exportlib.preparelib(program_name);
  1810. if token=_LKLAMMER then
  1811. begin
  1812. consume(_LKLAMMER);
  1813. paramnum:=1;
  1814. repeat
  1815. if m_isolike_program_para in current_settings.modeswitches then
  1816. begin
  1817. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  1818. begin
  1819. { the symtablestack is not setup here, so text must be created later on }
  1820. Setlength(sc,length(sc)+1);
  1821. with sc[high(sc)] do
  1822. begin
  1823. name:=pattern;
  1824. nr:=paramnum;
  1825. end;
  1826. inc(paramnum);
  1827. end;
  1828. end;
  1829. consume(_ID);
  1830. until not try_to_consume(_COMMA);
  1831. consume(_RKLAMMER);
  1832. end;
  1833. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1834. read, all following directives are parsed as well }
  1835. setupglobalswitches;
  1836. consume(_SEMICOLON);
  1837. {$ifdef DEBUG_NODE_XML}
  1838. XMLInitializeNodeFile('program', program_name);
  1839. {$endif DEBUG_NODE_XML}
  1840. end
  1841. else
  1842. begin
  1843. if (target_info.system in systems_unit_program_exports) then
  1844. exportlib.preparelib(current_module.realmodulename^);
  1845. { setup things using the switches }
  1846. setupglobalswitches;
  1847. {$ifdef DEBUG_NODE_XML}
  1848. XMLInitializeNodeFile('program', current_module.realmodulename^);
  1849. {$endif DEBUG_NODE_XML}
  1850. end;
  1851. { load all packages, so we know whether a unit is contained inside a
  1852. package or not }
  1853. load_packages;
  1854. { global switches are read, so further changes aren't allowed }
  1855. current_module.in_global:=false;
  1856. { set implementation flag }
  1857. current_module.in_interface:=false;
  1858. current_module.interface_compiled:=true;
  1859. { insert after the unit symbol tables the static symbol table
  1860. of the program }
  1861. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1862. { load system unit }
  1863. loadsystemunit;
  1864. { system unit is loaded, now insert feature defines }
  1865. for feature:=low(tfeature) to high(tfeature) do
  1866. if feature in features then
  1867. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1868. { load standard units, e.g objpas,profile unit }
  1869. loaddefaultunits;
  1870. { Load units provided on the command line }
  1871. loadautounits;
  1872. { insert iso program parameters }
  1873. if length(sc)>0 then
  1874. begin
  1875. textsym:=search_system_type('TEXT');
  1876. if not(assigned(textsym)) then
  1877. internalerror(2013011201);
  1878. for i:=0 to high(sc) do
  1879. begin
  1880. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  1881. current_module.localsymtable.insert(ps,true);
  1882. end;
  1883. end;
  1884. { Load the units used by the program we compile. }
  1885. if token=_USES then
  1886. begin
  1887. loadunits(nil);
  1888. consume_semicolon_after_uses:=true;
  1889. end
  1890. else
  1891. consume_semicolon_after_uses:=false;
  1892. { All units are read, now give them a number }
  1893. current_module.updatemaps;
  1894. { consume the semicolon after maps have been updated else conditional compiling expressions
  1895. might cause internal errors, see tw8611 }
  1896. if consume_semicolon_after_uses then
  1897. consume(_SEMICOLON);
  1898. {Insert the name of the main program into the symbol table.}
  1899. if current_module.realmodulename^<>'' then
  1900. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1901. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1902. symtablestack.push(current_module.localsymtable);
  1903. {$ifdef jvm}
  1904. { fake classdef to represent the class corresponding to the unit }
  1905. addmoduleclass;
  1906. {$endif}
  1907. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1908. maybe_load_got;
  1909. { create whole program optimisation information }
  1910. current_module.wpoinfo:=tunitwpoinfo.create;
  1911. { The program intialization needs an alias, so it can be called
  1912. from the bootstrap code.}
  1913. if islibrary then
  1914. begin
  1915. initpd:=nil;
  1916. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  1917. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  1918. if target_info.system in systems_darwin then
  1919. begin
  1920. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',current_module.localsymtable,'stub'),potype_libmainstub,current_module.localsymtable);
  1921. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  1922. initpd:=main_procinfo.procdef;
  1923. main_procinfo.free;
  1924. end;
  1925. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1926. { Win32 startup code needs a single name }
  1927. if not(target_info.system in (systems_darwin+systems_aix)) then
  1928. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  1929. else
  1930. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  1931. if not(target_info.system in systems_darwin) then
  1932. initpd:=main_procinfo.procdef;
  1933. cnodeutils.RegisterModuleInitFunction(initpd);
  1934. end
  1935. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  1936. begin
  1937. { create a stub with the name of the desired main routine, with
  1938. the same signature as the C "main" function, and call through to
  1939. FPC_SYSTEMMAIN, which will initialise everything based on its
  1940. parameters. This function cannot be in the system unit, because
  1941. its name can be configured on the command line (for use with e.g.
  1942. SDL, where the main function should be called SDL_main) }
  1943. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,current_module.localsymtable);
  1944. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  1945. main_procinfo.free;
  1946. { now create the PASCALMAIN routine (which will be called from
  1947. FPC_SYSTEMMAIN) }
  1948. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1949. end
  1950. else
  1951. begin
  1952. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1953. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  1954. end;
  1955. main_procinfo.parse_body;
  1956. { save file pos for debuginfo }
  1957. current_module.mainfilepos:=main_procinfo.entrypos;
  1958. { finalize? }
  1959. if token=_FINALIZATION then
  1960. begin
  1961. { Parse the finalize }
  1962. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize$'),potype_unitfinalize,current_module.localsymtable);
  1963. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1964. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  1965. finalize_procinfo.parse_body;
  1966. end;
  1967. { Generate specializations of objectdefs methods }
  1968. generate_specialization_procs;
  1969. { Generate VMTs }
  1970. if Errorcount=0 then
  1971. write_vmts(current_module.localsymtable,false);
  1972. { add implementations for synthetic method declarations added by
  1973. the compiler }
  1974. add_synthetic_method_implementations(current_module.localsymtable);
  1975. { generate construction functions for all attributes in the program }
  1976. generate_attr_constrs(current_module.used_rtti_attrs);
  1977. { should we force unit initialization? }
  1978. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1979. if force_init_final or cnodeutils.force_init then
  1980. init_procinfo:=gen_implicit_initfinal(mf_init,current_module.localsymtable);
  1981. { Add symbol to the exports section for win32 so smartlinking a
  1982. DLL will include the edata section }
  1983. if assigned(exportlib) and
  1984. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1985. (mf_has_exports in current_module.moduleflags) then
  1986. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1987. if (force_init_final or cnodeutils.force_final) and
  1988. (
  1989. not assigned(finalize_procinfo)
  1990. or has_no_code(finalize_procinfo.code)
  1991. ) then
  1992. begin
  1993. { first release the not used finalize procinfo }
  1994. if assigned(finalize_procinfo) then
  1995. begin
  1996. release_proc_symbol(finalize_procinfo.procdef);
  1997. release_main_proc(finalize_procinfo);
  1998. end;
  1999. finalize_procinfo:=gen_implicit_initfinal(mf_finalize,current_module.localsymtable);
  2000. end;
  2001. { the finalization routine of libraries is generic (and all libraries need to }
  2002. { be finalized, so they can finalize any units they use }
  2003. { Place in "pure assembler" list so that the llvm assembler writer
  2004. directly emits the generated directives }
  2005. if (islibrary) then
  2006. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2007. { all labels must be defined before generating code }
  2008. if Errorcount=0 then
  2009. tstoredsymtable(current_module.localsymtable).checklabels;
  2010. { See remark in unit init/final }
  2011. main_procinfo.generate_code;
  2012. main_procinfo.resetprocdef;
  2013. release_main_proc(main_procinfo);
  2014. if assigned(init_procinfo) then
  2015. begin
  2016. { initialization can be implicit only }
  2017. include(current_module.moduleflags,mf_init);
  2018. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2019. init_procinfo.generate_code;
  2020. init_procinfo.resetprocdef;
  2021. release_main_proc(init_procinfo);
  2022. end;
  2023. if assigned(finalize_procinfo) then
  2024. begin
  2025. if force_init_final or
  2026. cnodeutils.force_init or
  2027. not(has_no_code(finalize_procinfo.code)) then
  2028. begin
  2029. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2030. finalize_procinfo.generate_code;
  2031. include(current_module.moduleflags,mf_finalize);
  2032. end;
  2033. finalize_procinfo.resetprocdef;
  2034. release_main_proc(finalize_procinfo);
  2035. end;
  2036. symtablestack.pop(current_module.localsymtable);
  2037. { consume the last point }
  2038. consume(_POINT);
  2039. {$ifdef DEBUG_NODE_XML}
  2040. if IsLibrary then
  2041. XMLFinalizeNodeFile('library')
  2042. else
  2043. XMLFinalizeNodeFile('program');
  2044. {$endif DEBUG_NODE_XML}
  2045. { reset wpo flags for all defs }
  2046. reset_all_defs;
  2047. if (Errorcount=0) then
  2048. begin
  2049. { test static symtable }
  2050. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2051. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2052. tstoredsymtable(current_module.localsymtable).check_forwards;
  2053. current_module.allunitsused;
  2054. end;
  2055. { leave when we got an error }
  2056. if (Errorcount>0) and not status.skip_error then
  2057. begin
  2058. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2059. status.skip_error:=true;
  2060. exit;
  2061. end;
  2062. { remove all unused units, this happens when units are removed
  2063. from the uses clause in the source and the ppu was already being loaded }
  2064. hp:=tmodule(loaded_units.first);
  2065. while assigned(hp) do
  2066. begin
  2067. hp2:=hp;
  2068. hp:=tmodule(hp.next);
  2069. if hp2.is_unit and
  2070. not assigned(hp2.globalsymtable) then
  2071. begin
  2072. loaded_units.remove(hp2);
  2073. unloaded_units.concat(hp2);
  2074. end;
  2075. end;
  2076. { do we need to add the variants unit? }
  2077. maybeloadvariantsunit;
  2078. { Now that everything has been compiled we know if we need resource
  2079. support. If not, remove the unit. }
  2080. resources_used:=MaybeRemoveResUnit;
  2081. linker.initsysinitunitname;
  2082. if target_info.system in systems_internal_sysinit then
  2083. begin
  2084. { add start/halt unit }
  2085. sysinitmod:=AddUnit(linker.sysinitunit);
  2086. end
  2087. else
  2088. sysinitmod:=nil;
  2089. {$ifdef arm}
  2090. { Insert .pdata section for arm-wince.
  2091. It is needed for exception handling. }
  2092. if target_info.system in [system_arm_wince] then
  2093. InsertPData;
  2094. {$endif arm}
  2095. cnodeutils.InsertThreadvars;
  2096. { generate rtti/init tables }
  2097. write_persistent_type_info(current_module.localsymtable,false);
  2098. { if an Objective-C module, generate rtti and module info }
  2099. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2100. { generate debuginfo }
  2101. if (cs_debuginfo in current_settings.moduleswitches) then
  2102. current_debuginfo.inserttypeinfo;
  2103. if islibrary or (target_info.system in systems_unit_program_exports) then
  2104. exportlib.generatelib;
  2105. { Reference all DEBUGINFO sections from the main .fpc section }
  2106. if (cs_debuginfo in current_settings.moduleswitches) then
  2107. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2108. { Resource strings }
  2109. GenerateResourceStrings;
  2110. { Windows widestring needing initialization }
  2111. cnodeutils.InsertWideInits;
  2112. { Resourcestring references (const foo:string=someresourcestring) }
  2113. cnodeutils.InsertResStrInits;
  2114. { insert Tables and StackLength }
  2115. cnodeutils.InsertInitFinalTable;
  2116. cnodeutils.InsertThreadvarTablesTable;
  2117. cnodeutils.InsertResourceTablesTable;
  2118. cnodeutils.InsertWideInitsTablesTable;
  2119. cnodeutils.InsertResStrTablesTable;
  2120. cnodeutils.InsertMemorySizes;
  2121. { Insert symbol to resource info }
  2122. cnodeutils.InsertResourceInfo(resources_used);
  2123. { create callframe info }
  2124. create_dwarf_frame;
  2125. { create import library for all packages }
  2126. if packagelist.count>0 then
  2127. createimportlibfromexternals;
  2128. { generate imports }
  2129. if current_module.ImportLibraryList.Count>0 then
  2130. importlib.generatelib;
  2131. { insert own objectfile }
  2132. insertobjectfile;
  2133. { assemble and link }
  2134. create_objectfile;
  2135. { We might need the symbols info if not using
  2136. the default do_extractsymbolinfo
  2137. which is a dummy function PM }
  2138. needsymbolinfo:=
  2139. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2140. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2141. { release all local symtables that are not needed anymore }
  2142. if (not needsymbolinfo) then
  2143. free_localsymtables(current_module.localsymtable);
  2144. { leave when we got an error }
  2145. if (Errorcount>0) and not status.skip_error then
  2146. begin
  2147. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2148. status.skip_error:=true;
  2149. exit;
  2150. end;
  2151. if (not current_module.is_unit) then
  2152. begin
  2153. { create the executable when we are at level 1 }
  2154. if (compile_level=1) then
  2155. begin
  2156. { create global resource file by collecting all resource files }
  2157. CollectResourceFiles;
  2158. { write .def file }
  2159. if (cs_link_deffile in current_settings.globalswitches) then
  2160. deffile.writefile;
  2161. { link SysInit (if any) first, to have behavior consistent with
  2162. assembler startup files }
  2163. if assigned(sysinitmod) then
  2164. linker.AddModuleFiles(sysinitmod);
  2165. { Does any unit use checkpointer function }
  2166. program_uses_checkpointer:=false;
  2167. { insert all .o files from all loaded units and
  2168. unload the units, we don't need them anymore.
  2169. Keep the current_module because that is still needed }
  2170. hp:=tmodule(loaded_units.first);
  2171. while assigned(hp) do
  2172. begin
  2173. if (hp<>sysinitmod) and not assigned(hp.package) then
  2174. begin
  2175. linker.AddModuleFiles(hp);
  2176. if mf_checkpointer_called in hp.moduleflags then
  2177. program_uses_checkpointer:=true;
  2178. end;
  2179. hp2:=tmodule(hp.next);
  2180. if assigned(hp.package) then
  2181. add_package_unit_ref(hp.package);
  2182. if (hp<>current_module) and
  2183. (not needsymbolinfo) then
  2184. begin
  2185. loaded_units.remove(hp);
  2186. hp.free;
  2187. end;
  2188. hp:=hp2;
  2189. end;
  2190. { free also unneeded units we didn't free before }
  2191. if not needsymbolinfo then
  2192. unloaded_units.Clear;
  2193. { Does any unit use checkpointer function }
  2194. if program_uses_checkpointer then
  2195. Message1(link_w_program_uses_checkpointer,current_module.modulename^);
  2196. { add all directly used packages as libraries }
  2197. add_package_libs(linker);
  2198. { finally we can create an executable }
  2199. if current_module.islibrary then
  2200. linker.MakeSharedLibrary
  2201. else
  2202. linker.MakeExecutable;
  2203. { collect all necessary information for whole-program optimization }
  2204. wpoinfomanager.extractwpoinfofromprogram;
  2205. end;
  2206. { Give Fatal with error count for linker errors }
  2207. if (Errorcount>0) and not status.skip_error then
  2208. begin
  2209. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2210. status.skip_error:=true;
  2211. end;
  2212. end;
  2213. end;
  2214. end.