pmodules.pas 98 KB

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