pmodules.pas 103 KB

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