pmodules.pas 94 KB

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