pmodules.pas 96 KB

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