pmodules.pas 95 KB

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