pmodules.pas 103 KB

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