pmodules.pas 103 KB

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