pmodules.pas 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877
  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. procedure restorestate;
  579. begin
  580. state.restore(true);
  581. if assigned(current_scanner) and (current_module.scanner=current_scanner) then
  582. begin
  583. if assigned(current_scanner.inputfile) then
  584. current_scanner.tempopeninputfile;
  585. end;
  586. state.free;
  587. end;
  588. begin
  589. Result:=true;
  590. mwait:=nil;
  591. current_scanner.tempcloseinputfile;
  592. state:=tglobalstate.create(true);
  593. { Load the units }
  594. pu:=tused_unit(curr.used_units.first);
  595. while assigned(pu) do
  596. begin
  597. { Only load the units that are in the current
  598. (interface/implementation) uses clause }
  599. if pu.in_uses and
  600. (pu.in_interface=frominterface) then
  601. begin
  602. if (pu.u.state in [ms_processed, ms_compiled,ms_compiling_waitimpl]) then
  603. isLoaded:=true
  604. else if (pu.u.state=ms_registered) then
  605. // try to load
  606. isLoaded:=tppumodule(pu.u).loadppu(curr)
  607. else
  608. isLoaded:=False;
  609. isLoaded:=IsLoaded and not pu.u.is_reset;
  610. if not IsLoaded then
  611. begin
  612. if mwait=nil then
  613. mwait:=pu.u;
  614. // In case of is_reset, the task handler will discard the state if the module was already there
  615. task_handler.addmodule(pu.u);
  616. end;
  617. Result:=Result and IsLoaded;
  618. { is our module compiled? then we can stop }
  619. if curr.state in [ms_compiled,ms_processed] then
  620. begin
  621. Restorestate;
  622. exit;
  623. end;
  624. { add this unit to the dependencies }
  625. pu.u.adddependency(curr,frominterface);
  626. { check hints }
  627. pu.check_hints;
  628. end;
  629. pu:=tused_unit(pu.next);
  630. end;
  631. Restorestate;
  632. end;
  633. {
  634. Connect loaded units: check crc and add to symbol tables.
  635. this can only be called after all units were actually loaded!
  636. }
  637. procedure connect_loaded_units(_module : tmodule; preservest:tsymtable);
  638. var
  639. pu : tused_unit;
  640. sorg : ansistring;
  641. unitsymtable: tabstractunitsymtable;
  642. begin
  643. // writeln(_module.get_modulename,': Connecting units');
  644. pu:=tused_unit(_module.used_units.first);
  645. while assigned(pu) do
  646. begin
  647. {
  648. Writeln('Connect : ',Assigned(_module.modulename), ' ', assigned(pu.u), ' ' ,assigned(pu.u.modulename));
  649. if assigned(pu.u) then
  650. begin
  651. if assigned(pu.u.modulename) then
  652. Writeln(_module.modulename^,': Examining connect of file ',pu._fn,' (',pu.u.modulename^,')')
  653. else
  654. Writeln(_module.modulename^,': Examining connect of file ',pu._fn);
  655. end
  656. else
  657. Writeln(_module.modulename^,': Examining unit without module... ');
  658. }
  659. if not (pu.in_uses and
  660. (pu.in_interface=_module.in_interface)) then
  661. begin
  662. // writeln('Must not connect ',pu.u.modulename^,' (pu.in_interface: ',pu.in_interface,' <> module.in_interface',_module.in_interface,')');
  663. end
  664. else
  665. begin
  666. // writeln('Must connect ',pu.u.modulename^,'(sym: ',pu.unitsym.realname,')');
  667. { save crc values }
  668. pu.checksum:=pu.u.crc;
  669. pu.interface_checksum:=pu.u.interface_crc;
  670. pu.indirect_checksum:=pu.u.indirect_crc;
  671. if tppumodule(pu.u).nsprefix<>'' then
  672. begin
  673. { use the name as declared in the uses section for -Un }
  674. sorg:=tppumodule(pu.u).nsprefix+'.'+pu.unitsym.realname;
  675. { update unitsym now that we have access to the full name }
  676. pu.unitsym.free;
  677. pu.unitsym:=cunitsym.create(sorg,pu.u);
  678. end
  679. else
  680. begin
  681. { connect unitsym to the module }
  682. pu.unitsym.module:=pu.u;
  683. pu.unitsym.register_sym;
  684. end;
  685. {
  686. Add the unit symbol in the current symtable.
  687. localsymtable will be nil after the interface uses clause is parsed and the local symtable
  688. is moved to the global.
  689. }
  690. if assigned(_module.localsymtable) then
  691. unitsymtable:=tabstractunitsymtable(_module.localsymtable)
  692. else
  693. unitsymtable:=tabstractunitsymtable(_module.globalsymtable);
  694. // Writeln('Adding used unit sym ',pu.unitsym.realName,' to table ',unitsymtable.get_name);
  695. unitsymtable.insertunit(pu.unitsym);
  696. { add to symtable stack }
  697. // Writeln('Adding used unit symtable ',pu.u.globalsymtable.name^,' (',pu.u.globalsymtable.DefList.Count, ' defs) to stack');
  698. if assigned(preservest) then
  699. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  700. else
  701. symtablestack.push(pu.u.globalsymtable);
  702. if (m_mac in current_settings.modeswitches) and
  703. assigned(pu.u.globalmacrosymtable) then
  704. macrosymtablestack.push(pu.u.globalmacrosymtable);
  705. end;
  706. pu:=tused_unit(pu.next);
  707. end;
  708. // writeln(_module.get_modulename,': Done Connecting units');
  709. end;
  710. procedure reset_all_defs(curr: tmodule);
  711. begin
  712. if assigned(curr.wpoinfo) then
  713. curr.wpoinfo.resetdefs;
  714. end;
  715. procedure free_localsymtables(st:TSymtable);
  716. var
  717. i : longint;
  718. def : tstoreddef;
  719. pd : tprocdef;
  720. begin
  721. for i:=0 to st.DefList.Count-1 do
  722. begin
  723. def:=tstoreddef(st.DefList[i]);
  724. if def.typ=procdef then
  725. begin
  726. pd:=tprocdef(def);
  727. if assigned(pd.localst) and
  728. (pd.localst.symtabletype<>staticsymtable) and
  729. not(po_inline in pd.procoptions) then
  730. begin
  731. free_localsymtables(pd.localst);
  732. pd.localst.free;
  733. pd.localst:=nil;
  734. end;
  735. pd.freeimplprocdefinfo;
  736. pd.done_paraloc_info(calleeside);
  737. end;
  738. end;
  739. end;
  740. procedure free_unregistered_localsymtable_elements(curr : tmodule);
  741. var
  742. i: longint;
  743. def: tdef;
  744. sym: tsym;
  745. begin
  746. for i:=curr.localsymtable.deflist.count-1 downto 0 do
  747. begin
  748. def:=tdef(curr.localsymtable.deflist[i]);
  749. { since commit 48986 deflist might have NIL entries }
  750. if not assigned(def) then
  751. continue;
  752. { this also frees def, as the defs are owned by the symtable }
  753. if not def.is_registered and
  754. not(df_not_registered_no_free in def.defoptions) then
  755. begin
  756. { if it's a procdef, unregister it from its procsym first,
  757. unless that sym hasn't been registered either (it's possible
  758. to have one overload in the interface and another in the
  759. implementation) }
  760. if (def.typ=procdef) and
  761. tprocdef(def).procsym.is_registered then
  762. tprocsym(tprocdef(def).procsym).ProcdefList.Remove(def);
  763. curr.localsymtable.deletedef(def);
  764. end;
  765. end;
  766. { from high to low so we hopefully have moves of less data }
  767. for i:=curr.localsymtable.symlist.count-1 downto 0 do
  768. begin
  769. sym:=tsym(curr.localsymtable.symlist[i]);
  770. { this also frees sym, as the symbols are owned by the symtable }
  771. if not sym.is_registered then
  772. curr.localsymtable.DeleteSym(sym);
  773. end;
  774. end;
  775. procedure setupglobalswitches;
  776. begin
  777. if (cs_create_pic in current_settings.moduleswitches) then
  778. begin
  779. def_system_macro('FPC_PIC');
  780. def_system_macro('PIC');
  781. end;
  782. end;
  783. function create_main_proc(const name:TSymStr;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  784. var
  785. ps : tprocsym;
  786. pd : tprocdef;
  787. begin
  788. { there should be no current_procinfo available }
  789. if assigned(current_procinfo) then
  790. internalerror(200304275);
  791. {Generate a procsym for main}
  792. ps:=cprocsym.create('$'+name);
  793. { always register the symbol }
  794. ps.register_sym;
  795. { main are allways used }
  796. inc(ps.refs);
  797. st.insertsym(ps);
  798. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  799. { We don't need a local symtable, change it into the static symtable }
  800. if not (potype in [potype_mainstub,potype_pkgstub,potype_libmainstub]) then
  801. begin
  802. pd.localst.free;
  803. pd.localst:=st;
  804. end
  805. else if (potype=potype_pkgstub) and
  806. (target_info.system in systems_all_windows+systems_nativent) then
  807. pd.proccalloption:=pocall_stdcall
  808. else
  809. pd.proccalloption:=pocall_cdecl;
  810. handle_calling_convention(pd,hcc_default_actions_impl);
  811. { set procinfo and current_procinfo.procdef }
  812. result:=tcgprocinfo(cprocinfo.create(nil));
  813. result.procdef:=pd;
  814. { main proc does always a call e.g. to init system unit }
  815. if potype<>potype_pkgstub then
  816. include(result.flags,pi_do_call);
  817. end;
  818. procedure release_main_proc(curr: tmodule; pi:tcgprocinfo);
  819. begin
  820. { remove localst as it was replaced by staticsymtable }
  821. pi.procdef.localst:=nil;
  822. { remove procinfo }
  823. curr.procinfo:=nil;
  824. pi.free;
  825. pi:=nil;
  826. end;
  827. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  828. procedure maybe_load_got(curr: tmodule);
  829. {$if defined(i386) or defined (sparcgen)}
  830. var
  831. gotvarsym : tstaticvarsym;
  832. {$endif i386 or sparcgen}
  833. begin
  834. {$if defined(i386) or defined(sparcgen)}
  835. if (cs_create_pic in current_settings.moduleswitches) and
  836. (tf_pic_uses_got in target_info.flags) then
  837. begin
  838. { insert symbol for got access in assembler code}
  839. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  840. vs_value,voidpointertype,[vo_is_external]);
  841. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  842. curr.localsymtable.insertsym(gotvarsym);
  843. { avoid unnecessary warnings }
  844. gotvarsym.varstate:=vs_read;
  845. gotvarsym.refs:=1;
  846. end;
  847. {$endif i386 or sparcgen}
  848. end;
  849. function gen_implicit_initfinal(curr: tmodule; flag:tmoduleflag;st:TSymtable):tcgprocinfo;
  850. begin
  851. { create procdef }
  852. case flag of
  853. mf_init :
  854. begin
  855. result:=create_main_proc(make_mangledname('',curr.localsymtable,'init_implicit$'),potype_unitinit,st);
  856. result.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  857. end;
  858. mf_finalize :
  859. begin
  860. result:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize_implicit$'),potype_unitfinalize,st);
  861. result.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  862. if (not curr.is_unit) then
  863. result.procdef.aliasnames.concat('PASCALFINALIZE');
  864. end;
  865. else
  866. internalerror(200304253);
  867. end;
  868. result.code:=cnothingnode.create;
  869. end;
  870. procedure copy_macro(p:TObject; arg:pointer);
  871. begin
  872. TModule(arg).globalmacrosymtable.insertsym(tmacro(p).getcopy);
  873. end;
  874. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  875. var
  876. deprecated_seen,
  877. last_is_deprecated:boolean;
  878. begin
  879. try_consume_hintdirective:=false;
  880. deprecated_seen:=false;
  881. repeat
  882. last_is_deprecated:=false;
  883. case idtoken of
  884. _LIBRARY :
  885. begin
  886. include(moduleopt,mo_hint_library);
  887. try_consume_hintdirective:=true;
  888. end;
  889. _DEPRECATED :
  890. begin
  891. { allow deprecated only once }
  892. if deprecated_seen then
  893. break;
  894. include(moduleopt,mo_hint_deprecated);
  895. try_consume_hintdirective:=true;
  896. last_is_deprecated:=true;
  897. deprecated_seen:=true;
  898. end;
  899. _EXPERIMENTAL :
  900. begin
  901. include(moduleopt,mo_hint_experimental);
  902. try_consume_hintdirective:=true;
  903. end;
  904. _PLATFORM :
  905. begin
  906. include(moduleopt,mo_hint_platform);
  907. try_consume_hintdirective:=true;
  908. end;
  909. _UNIMPLEMENTED :
  910. begin
  911. include(moduleopt,mo_hint_unimplemented);
  912. try_consume_hintdirective:=true;
  913. end;
  914. else
  915. break;
  916. end;
  917. consume(Token);
  918. { handle deprecated message }
  919. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  920. begin
  921. if deprecatedmsg<>nil then
  922. internalerror(201001221);
  923. if token=_CSTRING then
  924. deprecatedmsg:=stringdup(cstringpattern)
  925. else
  926. deprecatedmsg:=stringdup(pattern);
  927. consume(token);
  928. include(moduleopt,mo_has_deprecated_msg);
  929. end;
  930. until false;
  931. end;
  932. {$ifdef jvm}
  933. procedure addmoduleclass(curr : tmodule);
  934. var
  935. def: tobjectdef;
  936. typesym: ttypesym;
  937. begin
  938. { java_jlobject may not have been parsed yet (system unit); in any
  939. case, we only use this to refer to the class type, so inheritance
  940. does not matter }
  941. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil,true);
  942. include(def.objectoptions,oo_is_external);
  943. include(def.objectoptions,oo_is_sealed);
  944. def.objextname:=stringdup(curr.realmodulename^);
  945. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  946. symtablestack.top.insertsym(typesym);
  947. end;
  948. {$endif jvm}
  949. type
  950. tfinishstate=record
  951. init_procinfo:tcgprocinfo;
  952. finalize_procinfo:tcgprocinfo;
  953. end;
  954. pfinishstate=^tfinishstate;
  955. function proc_unit_implementation(curr: tmodule):boolean;
  956. var
  957. init_procinfo,
  958. finalize_procinfo : tcgprocinfo;
  959. i,j : integer;
  960. finishstate:pfinishstate;
  961. begin
  962. result:=true;
  963. init_procinfo:=nil;
  964. finalize_procinfo:=nil;
  965. finishstate:=nil;
  966. set_current_module(curr);
  967. { We get here only after used modules were loaded }
  968. connect_loaded_units(curr,curr.globalsymtable);
  969. { All units are read, now give them a number }
  970. curr.updatemaps;
  971. { Consume the semicolon if needed.
  972. At this point the units in the uses clause have at least been parsed
  973. and are connected, and conditional compilation expressions can
  974. use the symbols from those units }
  975. if curr.consume_semicolon_after_uses then
  976. consume(_SEMICOLON);
  977. { further, changing the globalsymtable is not allowed anymore }
  978. curr.globalsymtable.sealed:=true;
  979. symtablestack.push(curr.localsymtable);
  980. if not curr.interface_only then
  981. begin
  982. Message1(parser_u_parsing_implementation,curr.modulename^);
  983. if curr.in_interface then
  984. internalerror(200212285);
  985. { Compile the unit }
  986. init_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'init$'),potype_unitinit,curr.localsymtable);
  987. init_procinfo.procdef.aliasnames.concat(make_mangledname('INIT$',curr.localsymtable,''));
  988. init_procinfo.parse_body;
  989. { save file pos for debuginfo }
  990. curr.mainfilepos:=init_procinfo.entrypos;
  991. { parse finalization section }
  992. if token=_FINALIZATION then
  993. begin
  994. { Compile the finalize }
  995. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  996. finalize_procinfo.procdef.aliasnames.concat(make_mangledname('FINALIZE$',curr.localsymtable,''));
  997. finalize_procinfo.parse_body;
  998. end
  999. end;
  1000. { remove all units that we are waiting for that are already waiting for
  1001. us => breaking up circles }
  1002. for i:=0 to curr.waitingunits.count-1 do
  1003. for j:=curr.waitingforunit.count-1 downto 0 do
  1004. if curr.waitingunits[i]=curr.waitingforunit[j] then
  1005. curr.waitingforunit.delete(j);
  1006. {$ifdef DEBUG_UNITWAITING}
  1007. Writeln('Units waiting for ', curr.modulename^, ': ',
  1008. curr.waitingforunit.Count);
  1009. {$endif}
  1010. result:=curr.waitingforunit.count=0;
  1011. { save all information that is needed for finishing the unit }
  1012. New(finishstate);
  1013. finishstate^.init_procinfo:=init_procinfo;
  1014. finishstate^.finalize_procinfo:=finalize_procinfo;
  1015. curr.finishstate:=finishstate;
  1016. if result then
  1017. finish_unit(curr)
  1018. else
  1019. curr.state:=ms_compiling_waitfinish;
  1020. end;
  1021. function parse_unit_interface_declarations(curr : tmodule) : boolean;
  1022. begin
  1023. result:=true;
  1024. set_current_module(curr);
  1025. { update the symtable }
  1026. connect_loaded_units(curr,nil);
  1027. { We must do this again, because units can have been added to the list while another task was being handled }
  1028. curr.updatemaps;
  1029. { consume the semicolon after maps have been updated else conditional compiling expressions
  1030. might cause internal errors, see tw8611 }
  1031. if curr.consume_semicolon_after_uses then
  1032. consume(_SEMICOLON);
  1033. { now push our own symtable }
  1034. symtablestack.push(curr.globalsymtable);
  1035. { Dump stack
  1036. Write(curr.modulename^);
  1037. symtablestack.dump;
  1038. }
  1039. { ... parse the declarations }
  1040. Message1(parser_u_parsing_interface,curr.realmodulename^);
  1041. {$ifdef jvm}
  1042. { fake classdef to represent the class corresponding to the unit }
  1043. addmoduleclass(curr);
  1044. {$endif}
  1045. read_interface_declarations;
  1046. { Export macros defined in the interface for macpas. The macros
  1047. are put in the globalmacrosymtable that will only be used by other
  1048. units. The current unit continues to use the localmacrosymtable }
  1049. if (m_mac in current_settings.modeswitches) then
  1050. begin
  1051. curr.globalmacrosymtable:=tmacrosymtable.create(true);
  1052. curr.localmacrosymtable.SymList.ForEachCall(@copy_macro,curr);
  1053. end;
  1054. { leave when we got an error }
  1055. if (Errorcount>0) and not status.skip_error then
  1056. begin
  1057. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1058. status.skip_error:=true;
  1059. symtablestack.pop(curr.globalsymtable);
  1060. {$ifdef DEBUG_NODE_XML}
  1061. XMLFinalizeNodeFile('unit');
  1062. {$endif DEBUG_NODE_XML}
  1063. exit;
  1064. end;
  1065. { Our interface is compiled, generate CRC and switch to implementation }
  1066. if not(cs_compilesystem in current_settings.moduleswitches) and
  1067. (Errorcount=0) then
  1068. tppumodule(curr).getppucrc;
  1069. curr.in_interface:=false;
  1070. curr.interface_compiled:=true;
  1071. { First reload all units depending on our interface, we need to do this
  1072. in the implementation part to prevent erroneous circular references }
  1073. tppumodule(curr).setdefgeneration;
  1074. tppumodule(curr).reload_flagged_units;
  1075. { Parse the implementation section }
  1076. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1077. curr.interface_only:=true
  1078. else
  1079. curr.interface_only:=false;
  1080. parse_only:=false;
  1081. { create static symbol table }
  1082. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1083. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1084. maybe_load_got(curr);
  1085. if not curr.interface_only then
  1086. begin
  1087. consume(_IMPLEMENTATION);
  1088. Message1(unit_u_loading_implementation_units,curr.modulename^);
  1089. { Read the implementation units }
  1090. if token=_USES then
  1091. begin
  1092. parseusesclause(curr);
  1093. if not loadunits(curr,false) then
  1094. curr.state:=ms_compiling_waitimpl;
  1095. { do not consume the semicolon yet, because the units in the uses clause
  1096. may not yet be loaded and conditional compilation expressions may
  1097. depend on symbols from those units }
  1098. curr.consume_semicolon_after_uses:=True;
  1099. end
  1100. else
  1101. curr.consume_semicolon_after_uses:=False;
  1102. end;
  1103. if curr.state in [ms_compiled,ms_processed] then
  1104. begin
  1105. // Writeln('Popping global symtable ?');
  1106. symtablestack.pop(curr.globalsymtable);
  1107. end;
  1108. { Can we continue compiling ? }
  1109. result:=curr.state<>ms_compiling_waitimpl;
  1110. if result then
  1111. result:=proc_unit_implementation(curr)
  1112. end;
  1113. function proc_unit(curr: tmodule):boolean;
  1114. var
  1115. main_file: tinputfile;
  1116. s1,s2 : ^string; {Saves stack space}
  1117. unitname : ansistring;
  1118. unitname8 : string[8];
  1119. consume_semicolon_after_uses:boolean;
  1120. feature : tfeature;
  1121. load_ok : boolean;
  1122. begin
  1123. result:=true;
  1124. if m_mac in current_settings.modeswitches then
  1125. curr.mode_switch_allowed:= false;
  1126. consume(_UNIT);
  1127. if curr.is_initial then
  1128. Status.IsExe:=false;
  1129. unitname:=orgpattern;
  1130. consume(_ID);
  1131. while token=_POINT do
  1132. begin
  1133. consume(_POINT);
  1134. unitname:=unitname+'.'+orgpattern;
  1135. consume(_ID);
  1136. end;
  1137. { create filenames and unit name }
  1138. main_file := current_scanner.inputfile;
  1139. while assigned(main_file.next) do
  1140. main_file := main_file.next;
  1141. new(s1);
  1142. s1^:=curr.modulename^;
  1143. curr.SetFileName(main_file.path+main_file.name,true);
  1144. curr.SetModuleName(unitname);
  1145. {$ifdef DEBUG_NODE_XML}
  1146. XMLInitializeNodeFile('unit', unitname);
  1147. {$endif DEBUG_NODE_XML}
  1148. { check for system unit }
  1149. new(s2);
  1150. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  1151. unitname8:=copy(curr.modulename^,1,8);
  1152. if (cs_check_unit_name in current_settings.globalswitches) and
  1153. (
  1154. not(
  1155. (curr.modulename^=s2^) or
  1156. (
  1157. (length(curr.modulename^)>8) and
  1158. (unitname8=s2^)
  1159. )
  1160. )
  1161. or
  1162. (
  1163. (length(s1^)>8) and
  1164. (s1^<>curr.modulename^)
  1165. )
  1166. ) then
  1167. Message2(unit_e_illegal_unit_name,curr.realmodulename^,s1^);
  1168. if (curr.modulename^='SYSTEM') then
  1169. include(current_settings.moduleswitches,cs_compilesystem);
  1170. dispose(s2);
  1171. dispose(s1);
  1172. if (target_info.system in systems_unit_program_exports) then
  1173. exportlib.preparelib(curr.realmodulename^);
  1174. { parse hint directives }
  1175. try_consume_hintdirective(curr.moduleoptions, curr.deprecatedmsg);
  1176. consume(_SEMICOLON);
  1177. { handle the global switches, do this before interface, because after interface has been
  1178. read, all following directives are parsed as well }
  1179. setupglobalswitches;
  1180. { generate now the global symboltable,
  1181. define first as local to overcome dependency conflicts }
  1182. curr.localsymtable:=tglobalsymtable.create(curr.modulename^,curr.moduleid);
  1183. { insert unitsym of this unit to prevent other units having
  1184. the same name }
  1185. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1186. { load default system unit, it must be loaded before interface is parsed
  1187. else we cannot use e.g. feature switches before the next real token }
  1188. load_ok:=loadsystemunit(curr);
  1189. { system unit is loaded, now insert feature defines }
  1190. for feature:=low(tfeature) to high(tfeature) do
  1191. if feature in features then
  1192. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1193. consume(_INTERFACE);
  1194. { global switches are read, so further changes aren't allowed }
  1195. curr.in_global:=false;
  1196. message1(unit_u_loading_interface_units,curr.modulename^);
  1197. { update status }
  1198. status.currentmodule:=curr.realmodulename^;
  1199. { maybe turn off m_objpas if we are compiling objpas }
  1200. if (curr.modulename^='OBJPAS') then
  1201. exclude(current_settings.modeswitches,m_objpas);
  1202. { maybe turn off m_mac if we are compiling macpas }
  1203. if (curr.modulename^='MACPAS') then
  1204. exclude(current_settings.modeswitches,m_mac);
  1205. parse_only:=true;
  1206. { load default units, like language mode units }
  1207. if not(cs_compilesystem in current_settings.moduleswitches) then
  1208. load_ok:=loaddefaultunits(curr) and load_ok;
  1209. { insert qualifier for the system unit (allows system.writeln) }
  1210. if not(cs_compilesystem in current_settings.moduleswitches) and
  1211. (token=_USES) then
  1212. begin
  1213. // We do this as late as possible.
  1214. if Assigned(curr) then
  1215. curr.Loadlocalnamespacelist
  1216. else
  1217. current_namespacelist:=Nil;
  1218. parseusesclause(curr);
  1219. load_ok:=loadunits(curr,true) and load_ok;
  1220. { has it been compiled at a higher level ?}
  1221. if curr.state in [ms_compiled,ms_processed] then
  1222. begin
  1223. Message1(parser_u_already_compiled,curr.realmodulename^);
  1224. exit;
  1225. end;
  1226. consume_semicolon_after_uses:=true;
  1227. end
  1228. else
  1229. consume_semicolon_after_uses:=false;
  1230. { we need to store this in case compilation is transferred to another unit }
  1231. curr.consume_semicolon_after_uses:=consume_semicolon_after_uses;
  1232. { move the global symtable from the temporary local to global }
  1233. current_module.globalsymtable:=current_module.localsymtable;
  1234. current_module.localsymtable:=nil;
  1235. { Now we check if we can continue. }
  1236. if not load_ok then
  1237. curr.state:=ms_compiling_waitintf;
  1238. { create whole program optimisation information (may already be
  1239. updated in the interface, e.g., in case of classrefdef typed
  1240. constants }
  1241. curr.wpoinfo:=tunitwpoinfo.create;
  1242. { Can we continue compiling ? }
  1243. result:=curr.state<>ms_compiling_waitintf;
  1244. if result then
  1245. result:=parse_unit_interface_declarations(curr);
  1246. end;
  1247. procedure finish_unit(module:tmodule);
  1248. function is_assembler_generated:boolean;
  1249. var
  1250. hal : tasmlisttype;
  1251. begin
  1252. result:=false;
  1253. if Errorcount=0 then
  1254. begin
  1255. for hal:=low(TasmlistType) to high(TasmlistType) do
  1256. if not current_asmdata.asmlists[hal].empty then
  1257. begin
  1258. result:=true;
  1259. exit;
  1260. end;
  1261. end;
  1262. end;
  1263. procedure module_is_done(curr: tmodule);inline;
  1264. begin
  1265. dispose(pfinishstate(curr.finishstate));
  1266. curr.finishstate:=nil;
  1267. end;
  1268. var
  1269. {$ifdef EXTDEBUG}
  1270. store_crc,
  1271. {$endif EXTDEBUG}
  1272. store_interface_crc,
  1273. store_indirect_crc: cardinal;
  1274. force_init_final : boolean;
  1275. init_procinfo,
  1276. finalize_procinfo : tcgprocinfo;
  1277. i : longint;
  1278. ag : boolean;
  1279. finishstate : tfinishstate;
  1280. waitingmodule : tmodule;
  1281. begin
  1282. { curr is now module }
  1283. if not assigned(module.finishstate) then
  1284. internalerror(2012091801);
  1285. finishstate:=pfinishstate(module.finishstate)^;
  1286. finalize_procinfo:=finishstate.finalize_procinfo;
  1287. init_procinfo:=finishstate.init_procinfo;
  1288. { Generate specializations of objectdefs methods }
  1289. generate_specialization_procs;
  1290. // This needs to be done before we generate the VMTs
  1291. if (target_cpu=tsystemcpu.cpu_wasm32) then
  1292. begin
  1293. add_synthetic_interface_classes_for_st(module.globalsymtable);
  1294. add_synthetic_interface_classes_for_st(module.localsymtable);
  1295. end;
  1296. { generate construction functions for all attributes in the unit:
  1297. this must be done before writing the VMTs because
  1298. during VMT writing the extended field info is written }
  1299. generate_attr_constrs(current_module.used_rtti_attrs);
  1300. { Generate VMTs }
  1301. if Errorcount=0 then
  1302. begin
  1303. write_vmts(module.globalsymtable,true);
  1304. write_vmts(module.localsymtable,false);
  1305. end;
  1306. { add implementations for synthetic method declarations added by
  1307. the compiler }
  1308. add_synthetic_method_implementations(module.globalsymtable);
  1309. add_synthetic_method_implementations(module.localsymtable);
  1310. { if the unit contains ansi/widestrings, initialization and
  1311. finalization code must be forced }
  1312. force_init_final:=tglobalsymtable(module.globalsymtable).needs_init_final or
  1313. tstaticsymtable(module.localsymtable).needs_init_final;
  1314. { should we force unit initialization? }
  1315. { this is a hack, but how can it be done better ? }
  1316. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  1317. is it needed at all? (Sergei) }
  1318. { it's needed in case cnodeutils.force_init = true }
  1319. if (force_init_final or cnodeutils.force_init) and
  1320. (
  1321. not assigned(init_procinfo) or
  1322. has_no_code(init_procinfo.code)
  1323. ) then
  1324. begin
  1325. { first release the not used init procinfo }
  1326. if assigned(init_procinfo) then
  1327. begin
  1328. release_proc_symbol(init_procinfo.procdef);
  1329. release_main_proc(module,init_procinfo);
  1330. end;
  1331. init_procinfo:=gen_implicit_initfinal(module,mf_init,module.localsymtable);
  1332. end;
  1333. if (force_init_final or cnodeutils.force_final) and
  1334. (
  1335. not assigned(finalize_procinfo) or
  1336. has_no_code(finalize_procinfo.code)
  1337. ) then
  1338. begin
  1339. { first release the not used finalize procinfo }
  1340. if assigned(finalize_procinfo) then
  1341. begin
  1342. release_proc_symbol(finalize_procinfo.procdef);
  1343. release_main_proc(module,finalize_procinfo);
  1344. end;
  1345. finalize_procinfo:=gen_implicit_initfinal(module,mf_finalize,module.localsymtable);
  1346. end;
  1347. { Now both init and finalize bodies are read and it is known
  1348. which variables are used in both init and finalize we can now
  1349. generate the code. This is required to prevent putting a variable in
  1350. a register that is also used in the finalize body (PFV) }
  1351. if assigned(init_procinfo) then
  1352. begin
  1353. if (force_init_final or cnodeutils.force_init) or
  1354. not(has_no_code(init_procinfo.code)) then
  1355. begin
  1356. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1357. init_procinfo.generate_code_tree;
  1358. include(module.moduleflags,mf_init);
  1359. end
  1360. else
  1361. release_proc_symbol(init_procinfo.procdef);
  1362. init_procinfo.resetprocdef;
  1363. release_main_proc(module,init_procinfo);
  1364. end;
  1365. if assigned(finalize_procinfo) then
  1366. begin
  1367. if force_init_final or
  1368. cnodeutils.force_init or
  1369. not(has_no_code(finalize_procinfo.code)) then
  1370. begin
  1371. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1372. finalize_procinfo.generate_code_tree;
  1373. include(module.moduleflags,mf_finalize);
  1374. end
  1375. else
  1376. release_proc_symbol(finalize_procinfo.procdef);
  1377. finalize_procinfo.resetprocdef;
  1378. release_main_proc(module,finalize_procinfo);
  1379. end;
  1380. symtablestack.pop(module.localsymtable);
  1381. symtablestack.pop(module.globalsymtable);
  1382. { the last char should always be a point }
  1383. { Do not attempt to read next token after dot,
  1384. there may be a #0 when the unit was finished in a separate stage }
  1385. consume_last_dot;
  1386. { reset wpo flags for all defs }
  1387. reset_all_defs(module);
  1388. if (Errorcount=0) then
  1389. begin
  1390. { tests, if all (interface) forwards are resolved }
  1391. tstoredsymtable(module.globalsymtable).check_forwards;
  1392. { check if all private fields are used }
  1393. tstoredsymtable(module.globalsymtable).allprivatesused;
  1394. { test static symtable }
  1395. tstoredsymtable(module.localsymtable).allsymbolsused;
  1396. tstoredsymtable(module.localsymtable).allprivatesused;
  1397. tstoredsymtable(module.localsymtable).check_forwards;
  1398. tstoredsymtable(module.localsymtable).checklabels;
  1399. { used units }
  1400. module.allunitsused;
  1401. end;
  1402. { leave when we got an error }
  1403. if (Errorcount>0) and not status.skip_error then
  1404. begin
  1405. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1406. status.skip_error:=true;
  1407. module_is_done(module);
  1408. {$ifdef DEBUG_NODE_XML}
  1409. XMLFinalizeNodeFile('unit');
  1410. {$endif DEBUG_NODE_XML}
  1411. exit;
  1412. end;
  1413. { if an Objective-C module, generate rtti and module info }
  1414. MaybeGenerateObjectiveCImageInfo(module.globalsymtable,module.localsymtable);
  1415. { do we need to add the variants unit? }
  1416. maybeloadvariantsunit(module);
  1417. { generate rtti/init tables }
  1418. write_persistent_type_info(module.globalsymtable,true);
  1419. write_persistent_type_info(module.localsymtable,false);
  1420. { Tables }
  1421. cnodeutils.InsertThreadvars;
  1422. { Resource strings }
  1423. GenerateResourceStrings;
  1424. { Widestring typed constants }
  1425. cnodeutils.InsertWideInits;
  1426. { Resourcestring references }
  1427. cnodeutils.InsertResStrInits;
  1428. { generate debuginfo }
  1429. if (cs_debuginfo in current_settings.moduleswitches) then
  1430. current_debuginfo.inserttypeinfo;
  1431. { generate imports }
  1432. if module.ImportLibraryList.Count>0 then
  1433. importlib.generatelib;
  1434. { insert own objectfile, or say that it's in a library
  1435. (no check for an .o when loading) }
  1436. ag:=is_assembler_generated;
  1437. if ag then
  1438. insertobjectfile(module)
  1439. else
  1440. begin
  1441. module.headerflags:=module.headerflags or uf_no_link;
  1442. exclude(module.moduleflags,mf_has_stabs_debuginfo);
  1443. exclude(module.moduleflags,mf_has_dwarf_debuginfo);
  1444. end;
  1445. if ag then
  1446. begin
  1447. { create callframe info }
  1448. create_dwarf_frame;
  1449. { assemble }
  1450. create_objectfile(module);
  1451. end;
  1452. { Write out the ppufile after the object file has been created }
  1453. store_interface_crc:=module.interface_crc;
  1454. store_indirect_crc:=module.indirect_crc;
  1455. {$ifdef EXTDEBUG}
  1456. store_crc:=module.crc;
  1457. {$endif EXTDEBUG}
  1458. if (Errorcount=0) then
  1459. tppumodule(module).writeppu;
  1460. if not(cs_compilesystem in current_settings.moduleswitches) then
  1461. begin
  1462. if store_interface_crc<>module.interface_crc then
  1463. Message1(unit_u_interface_crc_changed,module.ppufilename);
  1464. if store_indirect_crc<>module.indirect_crc then
  1465. Message1(unit_u_indirect_crc_changed,module.ppufilename);
  1466. end;
  1467. {$ifdef EXTDEBUG}
  1468. if not(cs_compilesystem in current_settings.moduleswitches) then
  1469. if (store_crc<>module.crc) then
  1470. Message1(unit_u_implementation_crc_changed,module.ppufilename);
  1471. {$endif EXTDEBUG}
  1472. { release unregistered defs/syms from the localsymtable }
  1473. free_unregistered_localsymtable_elements(module);
  1474. { release local symtables that are not needed anymore }
  1475. free_localsymtables(module.globalsymtable);
  1476. free_localsymtables(module.localsymtable);
  1477. { leave when we got an error }
  1478. if (Errorcount>0) and not status.skip_error then
  1479. begin
  1480. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1481. status.skip_error:=true;
  1482. module_is_done(module);
  1483. {$ifdef DEBUG_NODE_XML}
  1484. XMLFinalizeNodeFile('unit');
  1485. {$endif DEBUG_NODE_XML}
  1486. exit;
  1487. end;
  1488. {$ifdef debug_devirt}
  1489. { print out all instantiated class/object types }
  1490. writeln('constructed object/class/classreftypes in ',module.realmodulename^);
  1491. for i := 0 to module.wpoinfo.createdobjtypes.count-1 do
  1492. begin
  1493. write(' ',tdef(module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1494. case tdef(module.wpoinfo.createdobjtypes[i]).typ of
  1495. objectdef:
  1496. case tobjectdef(module.wpoinfo.createdobjtypes[i]).objecttype of
  1497. odt_object:
  1498. writeln(' (object)');
  1499. odt_class:
  1500. writeln(' (class)');
  1501. else
  1502. internalerror(2008101103);
  1503. end;
  1504. else
  1505. internalerror(2008101104);
  1506. end;
  1507. end;
  1508. for i := 0 to module.wpoinfo.createdclassrefobjtypes.count-1 do
  1509. begin
  1510. write(' Class Of ',tdef(module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1511. case tdef(module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1512. objectdef:
  1513. case tobjectdef(module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1514. odt_class:
  1515. writeln(' (classrefdef)');
  1516. else
  1517. internalerror(2008101105);
  1518. end
  1519. else
  1520. internalerror(2008101102);
  1521. end;
  1522. end;
  1523. {$endif debug_devirt}
  1524. Message1(unit_u_finished_compiling,module.modulename^);
  1525. module_is_done(module);
  1526. module.end_of_parsing;
  1527. for i:=0 to module.waitingunits.count-1 do
  1528. begin
  1529. waitingmodule:=tmodule(module.waitingunits[i]);
  1530. waitingmodule.remove_from_waitingforunits(module);
  1531. end;
  1532. {$ifdef DEBUG_NODE_XML}
  1533. XMLFinalizeNodeFile('unit');
  1534. {$endif DEBUG_NODE_XML}
  1535. end;
  1536. function proc_package(curr: tmodule) : boolean;
  1537. var
  1538. main_file : tinputfile;
  1539. hp,hp2 : tmodule;
  1540. pkg : tpcppackage;
  1541. main_procinfo : tcgprocinfo;
  1542. force_init_final : boolean;
  1543. uu : tused_unit;
  1544. module_name: ansistring;
  1545. pentry: ppackageentry;
  1546. feature : tfeature;
  1547. begin
  1548. Result:=True;
  1549. Status.IsPackage:=true;
  1550. Status.IsExe:=true;
  1551. parse_only:=false;
  1552. main_procinfo:=nil;
  1553. {init_procinfo:=nil;
  1554. finalize_procinfo:=nil;}
  1555. if not (tf_supports_packages in target_info.flags) then
  1556. message1(parser_e_packages_not_supported,target_info.name);
  1557. if not RelocSectionSetExplicitly then
  1558. RelocSection:=true;
  1559. { Relocation works only without stabs under Windows when }
  1560. { external linker (LD) is used. LD generates relocs for }
  1561. { stab sections which is not loaded in memory. It causes }
  1562. { AV error when DLL is loaded and relocation is needed. }
  1563. { Internal linker does not have this problem. }
  1564. if RelocSection and
  1565. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1566. (cs_link_extern in current_settings.globalswitches) then
  1567. begin
  1568. include(current_settings.globalswitches,cs_link_strip);
  1569. { Warning stabs info does not work with reloc section !! }
  1570. if (cs_debuginfo in current_settings.moduleswitches) and
  1571. (target_dbg.id=dbg_stabs) then
  1572. begin
  1573. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  1574. Message(parser_w_parser_win32_debug_needs_WN);
  1575. exclude(current_settings.moduleswitches,cs_debuginfo);
  1576. end;
  1577. end;
  1578. { get correct output names }
  1579. main_file := current_scanner.inputfile;
  1580. while assigned(main_file.next) do
  1581. main_file := main_file.next;
  1582. curr.SetFileName(main_file.path+main_file.name,true);
  1583. { consume _PACKAGE word }
  1584. consume(_ID);
  1585. module_name:=orgpattern;
  1586. consume(_ID);
  1587. while token=_POINT do
  1588. begin
  1589. consume(_POINT);
  1590. module_name:=module_name+'.'+orgpattern;
  1591. consume(_ID);
  1592. end;
  1593. curr.setmodulename(module_name);
  1594. curr.ispackage:=true;
  1595. exportlib.preparelib(module_name);
  1596. pkg:=tpcppackage.create(module_name);
  1597. if tf_library_needs_pic in target_info.flags then
  1598. include(current_settings.moduleswitches,cs_create_pic);
  1599. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1600. read, all following directives are parsed as well }
  1601. setupglobalswitches;
  1602. {$ifdef DEBUG_NODE_XML}
  1603. XMLInitializeNodeFile('package', module_name);
  1604. {$endif DEBUG_NODE_XML}
  1605. consume(_SEMICOLON);
  1606. { global switches are read, so further changes aren't allowed }
  1607. curr.in_global:=false;
  1608. { set implementation flag }
  1609. curr.in_interface:=false;
  1610. curr.interface_compiled:=true;
  1611. { insert after the unit symbol tables the static symbol table }
  1612. { of the program }
  1613. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  1614. { ensure that no packages are picked up from the options }
  1615. packagelist.clear;
  1616. // There should always be a requires, except for the system package. So we load here
  1617. if Assigned(curr) then
  1618. curr.Loadlocalnamespacelist
  1619. else
  1620. current_namespacelist:=Nil;
  1621. {Read the packages used by the package we compile.}
  1622. if (token=_ID) and (idtoken=_REQUIRES) then
  1623. begin
  1624. { consume _REQUIRES word }
  1625. consume(_ID);
  1626. while true do
  1627. begin
  1628. if token=_ID then
  1629. begin
  1630. module_name:=orgpattern;
  1631. consume(_ID);
  1632. while token=_POINT do
  1633. begin
  1634. consume(_POINT);
  1635. module_name:=module_name+'.'+orgpattern;
  1636. consume(_ID);
  1637. end;
  1638. add_package(module_name,false,true);
  1639. end
  1640. else
  1641. consume(_ID);
  1642. if token=_COMMA then
  1643. consume(_COMMA)
  1644. else
  1645. break;
  1646. end;
  1647. consume(_SEMICOLON);
  1648. end;
  1649. { now load all packages, so that we can determine whether a unit is
  1650. already provided by one of the loaded packages }
  1651. load_packages;
  1652. if packagelist.Count>0 then
  1653. begin
  1654. { this means the SYSTEM unit *must* be part of one of the required
  1655. packages, so load it }
  1656. AddUnit(curr,'system',false);
  1657. systemunit:=tglobalsymtable(symtablestack.top);
  1658. load_intern_types;
  1659. { system unit is loaded, now insert feature defines }
  1660. for feature:=low(tfeature) to high(tfeature) do
  1661. if feature in features then
  1662. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  1663. end;
  1664. {Load the units used by the program we compile.}
  1665. if (token=_ID) and (idtoken=_CONTAINS) then
  1666. begin
  1667. { consume _CONTAINS word }
  1668. consume(_ID);
  1669. while true do
  1670. begin
  1671. if token=_ID then
  1672. begin
  1673. module_name:=orgpattern;
  1674. consume(_ID);
  1675. while token=_POINT do
  1676. begin
  1677. consume(_POINT);
  1678. module_name:=module_name+'.'+orgpattern;
  1679. consume(_ID);
  1680. end;
  1681. hp:=AddUnit(curr,module_name);
  1682. if (hp.modulename^='SYSTEM') and not assigned(systemunit) then
  1683. begin
  1684. systemunit:=tglobalsymtable(hp.globalsymtable);
  1685. load_intern_types;
  1686. end;
  1687. end
  1688. else
  1689. consume(_ID);
  1690. if token=_COMMA then
  1691. consume(_COMMA)
  1692. else break;
  1693. end;
  1694. consume(_SEMICOLON);
  1695. end;
  1696. { All units are read, now give them a number }
  1697. curr.updatemaps;
  1698. hp:=tmodule(loaded_units.first);
  1699. while assigned(hp) do
  1700. begin
  1701. if (hp<>curr) and not assigned(hp.package) then
  1702. begin
  1703. if mf_package_deny in hp.moduleflags then
  1704. message1(package_e_unit_deny_package,hp.realmodulename^);
  1705. { part of the package's used, aka contained units? }
  1706. uu:=tused_unit(curr.used_units.first);
  1707. while assigned(uu) do
  1708. begin
  1709. if uu.u=hp then
  1710. break;
  1711. uu:=tused_unit(uu.next);
  1712. end;
  1713. if not assigned(uu) then
  1714. message2(package_n_implicit_unit_import,hp.realmodulename^,curr.realmodulename^);
  1715. end;
  1716. { was this unit listed as a contained unit? If so => error }
  1717. if (hp<>curr) and assigned(hp.package) then
  1718. begin
  1719. uu:=tused_unit(curr.used_units.first);
  1720. while assigned(uu) do
  1721. begin
  1722. if uu.u=hp then
  1723. break;
  1724. uu:=tused_unit(uu.next);
  1725. end;
  1726. if assigned(uu) then
  1727. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1728. end;
  1729. hp:=tmodule(hp.next);
  1730. end;
  1731. {Insert the name of the main program into the symbol table.}
  1732. if curr.realmodulename^<>'' then
  1733. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  1734. Message1(parser_u_parsing_implementation,curr.mainsource);
  1735. symtablestack.push(curr.localsymtable);
  1736. { create whole program optimisation information }
  1737. curr.wpoinfo:=tunitwpoinfo.create;
  1738. { should we force unit initialization? }
  1739. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  1740. if force_init_final or cnodeutils.force_init then
  1741. {init_procinfo:=gen_implicit_initfinal(mf_init,curr.localsymtable)};
  1742. { Add symbol to the exports section for win32 so smartlinking a
  1743. DLL will include the edata section }
  1744. if assigned(exportlib) and
  1745. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1746. (mf_has_exports in curr.moduleflags) then
  1747. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  1748. { all labels must be defined before generating code }
  1749. if Errorcount=0 then
  1750. tstoredsymtable(curr.localsymtable).checklabels;
  1751. symtablestack.pop(curr.localsymtable);
  1752. { consume the last point }
  1753. consume(_END);
  1754. consume(_POINT);
  1755. if (Errorcount=0) then
  1756. begin
  1757. { test static symtable }
  1758. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1759. tstoredsymtable(curr.localsymtable).allprivatesused;
  1760. tstoredsymtable(curr.localsymtable).check_forwards;
  1761. { Note: all contained units are considered as used }
  1762. end;
  1763. if target_info.system in systems_all_windows+systems_nativent then
  1764. begin
  1765. main_procinfo:=create_main_proc('_PkgEntryPoint',potype_pkgstub,curr.localsymtable);
  1766. main_procinfo.procdef.aliasnames.concat('_DLLMainCRTStartup');
  1767. main_procinfo.code:=generate_pkg_stub(main_procinfo.procdef);
  1768. main_procinfo.generate_code;
  1769. end;
  1770. {$ifdef DEBUG_NODE_XML}
  1771. XMLFinalizeNodeFile('package');
  1772. {$endif DEBUG_NODE_XML}
  1773. { leave when we got an error }
  1774. if (Errorcount>0) and not status.skip_error then
  1775. begin
  1776. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1777. status.skip_error:=true;
  1778. pkg.free;
  1779. exit;
  1780. end;
  1781. { remove all unused units, this happends when units are removed
  1782. from the uses clause in the source and the ppu was already being loaded }
  1783. hp:=tmodule(loaded_units.first);
  1784. while assigned(hp) do
  1785. begin
  1786. hp2:=hp;
  1787. hp:=tmodule(hp.next);
  1788. if assigned(hp2.package) then
  1789. add_package_unit_ref(hp2.package);
  1790. if hp2.is_unit and
  1791. not assigned(hp2.globalsymtable) then
  1792. loaded_units.remove(hp2);
  1793. end;
  1794. exportlib.ignoreduplicates:=true;
  1795. { force exports }
  1796. uu:=tused_unit(usedunits.first);
  1797. while assigned(uu) do
  1798. begin
  1799. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1800. begin
  1801. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1802. load_intern_types;
  1803. end;
  1804. if not assigned(uu.u.package) then
  1805. export_unit(uu.u);
  1806. uu:=tused_unit(uu.next);
  1807. end;
  1808. {$ifdef arm}
  1809. { Insert .pdata section for arm-wince.
  1810. It is needed for exception handling. }
  1811. if target_info.system in [system_arm_wince] then
  1812. InsertPData;
  1813. {$endif arm}
  1814. { generate debuginfo }
  1815. if (cs_debuginfo in current_settings.moduleswitches) then
  1816. current_debuginfo.inserttypeinfo;
  1817. exportlib.generatelib;
  1818. exportlib.ignoreduplicates:=false;
  1819. { create import libraries for all packages }
  1820. if packagelist.count>0 then
  1821. createimportlibfromexternals;
  1822. { generate imports }
  1823. if curr.ImportLibraryList.Count>0 then
  1824. importlib.generatelib;
  1825. { Reference all DEBUGINFO sections from the main .fpc section }
  1826. if (cs_debuginfo in current_settings.moduleswitches) then
  1827. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1828. { insert own objectfile }
  1829. insertobjectfile(curr);
  1830. { assemble and link }
  1831. create_objectfile(curr);
  1832. { We might need the symbols info if not using
  1833. the default do_extractsymbolinfo
  1834. which is a dummy function PM }
  1835. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1836. { release all local symtables that are not needed anymore }
  1837. if (not needsymbolinfo) then
  1838. free_localsymtables(curr.localsymtable);
  1839. { leave when we got an error }
  1840. if (Errorcount>0) and not status.skip_error then
  1841. begin
  1842. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1843. status.skip_error:=true;
  1844. pkg.free;
  1845. exit;
  1846. end;
  1847. if (not curr.is_unit) then
  1848. begin
  1849. { we add all loaded units that are not part of a package to the
  1850. package; this includes units in the "contains" section as well
  1851. as implicitely imported ones }
  1852. hp:=tmodule(loaded_units.first);
  1853. while assigned(hp) do
  1854. begin
  1855. if (hp<>curr) then
  1856. begin
  1857. if not assigned(hp.package) then
  1858. begin
  1859. pkg.addunit(hp);
  1860. check_for_indirect_package_usages(hp.used_units);
  1861. end
  1862. else
  1863. begin
  1864. pentry:=ppackageentry(packagelist.find(hp.package.packagename^));
  1865. if not assigned(pentry) then
  1866. internalerror(2015112301);
  1867. pkg.add_required_package(hp.package);
  1868. end;
  1869. end;
  1870. hp:=tmodule(hp.next);
  1871. end;
  1872. pkg.initmoduleinfo(curr);
  1873. { create the executable when we are at level 1 }
  1874. if (curr.is_initial) then
  1875. begin
  1876. { create global resource file by collecting all resource files }
  1877. CollectResourceFiles;
  1878. { write .def file }
  1879. if (cs_link_deffile in current_settings.globalswitches) then
  1880. deffile.writefile;
  1881. { generate the pcp file }
  1882. pkg.savepcp;
  1883. { insert all .o files from all loaded units and
  1884. unload the units, we don't need them anymore.
  1885. Keep the curr because that is still needed }
  1886. hp:=tmodule(loaded_units.first);
  1887. while assigned(hp) do
  1888. begin
  1889. { only link in those units which should become part of this
  1890. package }
  1891. if not assigned(hp.package) then
  1892. linker.AddModuleFiles(hp);
  1893. hp2:=tmodule(hp.next);
  1894. if (hp<>curr) and
  1895. (not needsymbolinfo) then
  1896. begin
  1897. loaded_units.remove(hp);
  1898. hp.free;
  1899. end;
  1900. hp:=hp2;
  1901. end;
  1902. { add the library of directly used packages }
  1903. add_package_libs(linker);
  1904. { and now link the package library }
  1905. linker.MakeSharedLibrary
  1906. end;
  1907. { Give Fatal with error count for linker errors }
  1908. if (Errorcount>0) and not status.skip_error then
  1909. begin
  1910. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1911. status.skip_error:=true;
  1912. end;
  1913. pkg.free;
  1914. end;
  1915. end;
  1916. procedure proc_create_executable(curr, sysinitmod: tmodule; islibrary : boolean);
  1917. var
  1918. program_uses_checkpointer : boolean;
  1919. hp,hp2 : tmodule;
  1920. begin
  1921. { create global resource file by collecting all resource files }
  1922. CollectResourceFiles;
  1923. { write .def file }
  1924. if (cs_link_deffile in current_settings.globalswitches) then
  1925. deffile.writefile;
  1926. { link SysInit (if any) first, to have behavior consistent with
  1927. assembler startup files }
  1928. if assigned(sysinitmod) then
  1929. linker.AddModuleFiles(sysinitmod);
  1930. { Does any unit use checkpointer function }
  1931. program_uses_checkpointer:=false;
  1932. { insert all .o files from all loaded units and
  1933. unload the units, we don't need them anymore.
  1934. Keep the curr because that is still needed }
  1935. hp:=tmodule(loaded_units.first);
  1936. while assigned(hp) do
  1937. begin
  1938. if (hp<>sysinitmod) and not assigned(hp.package) then
  1939. begin
  1940. linker.AddModuleFiles(hp);
  1941. if mf_checkpointer_called in hp.moduleflags then
  1942. program_uses_checkpointer:=true;
  1943. end;
  1944. hp2:=tmodule(hp.next);
  1945. if assigned(hp.package) then
  1946. add_package_unit_ref(hp.package);
  1947. if (hp<>curr) and
  1948. (not needsymbolinfo) then
  1949. begin
  1950. loaded_units.remove(hp);
  1951. hp.free;
  1952. end;
  1953. hp:=hp2;
  1954. end;
  1955. { free also unneeded units we didn't free before }
  1956. if not needsymbolinfo then
  1957. unloaded_units.Clear;
  1958. { Does any unit use checkpointer function }
  1959. if program_uses_checkpointer then
  1960. Message1(link_w_program_uses_checkpointer,curr.modulename^);
  1961. { add all directly used packages as libraries }
  1962. add_package_libs(linker);
  1963. { finally we can create an executable }
  1964. if curr.islibrary then
  1965. linker.MakeSharedLibrary
  1966. else
  1967. linker.MakeExecutable;
  1968. { collect all necessary information for whole-program optimization }
  1969. wpoinfomanager.extractwpoinfofromprogram;
  1970. end;
  1971. procedure proc_program_after_parsing(curr : tmodule; islibrary : boolean);
  1972. var
  1973. sysinitmod, hp,hp2 : tmodule;
  1974. resources_used : boolean;
  1975. begin
  1976. sysinitmod:=nil;
  1977. hp:=nil;
  1978. hp2:=nil;
  1979. resources_used:=false;
  1980. {$ifdef DEBUG_NODE_XML}
  1981. if IsLibrary then
  1982. XMLFinalizeNodeFile('library')
  1983. else
  1984. XMLFinalizeNodeFile('program');
  1985. {$endif DEBUG_NODE_XML}
  1986. { reset wpo flags for all defs }
  1987. reset_all_defs(curr);
  1988. if (Errorcount=0) then
  1989. begin
  1990. { test static symtable }
  1991. tstoredsymtable(curr.localsymtable).allsymbolsused;
  1992. tstoredsymtable(curr.localsymtable).allprivatesused;
  1993. tstoredsymtable(curr.localsymtable).check_forwards;
  1994. curr.allunitsused;
  1995. end;
  1996. { leave when we got an error }
  1997. if (Errorcount>0) and not status.skip_error then
  1998. begin
  1999. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2000. status.skip_error:=true;
  2001. exit;
  2002. end;
  2003. { remove all unused units, this happens when units are removed
  2004. from the uses clause in the source and the ppu was already being loaded }
  2005. hp:=tmodule(loaded_units.first);
  2006. while assigned(hp) do
  2007. begin
  2008. hp2:=hp;
  2009. hp:=tmodule(hp.next);
  2010. if hp2.is_unit and
  2011. not assigned(hp2.globalsymtable) then
  2012. begin
  2013. loaded_units.remove(hp2);
  2014. unloaded_units.concat(hp2);
  2015. end;
  2016. end;
  2017. { do we need to add the variants unit? }
  2018. maybeloadvariantsunit(curr);
  2019. { Now that everything has been compiled we know if we need resource
  2020. support. If not, remove the unit. }
  2021. resources_used:=MaybeRemoveResUnit(curr);
  2022. linker.initsysinitunitname;
  2023. if target_info.system in systems_internal_sysinit then
  2024. begin
  2025. { add start/halt unit }
  2026. sysinitmod:=AddUnit(curr,linker.sysinitunit);
  2027. end
  2028. else
  2029. sysinitmod:=nil;
  2030. {$ifdef arm}
  2031. { Insert .pdata section for arm-wince.
  2032. It is needed for exception handling. }
  2033. if target_info.system in [system_arm_wince] then
  2034. InsertPData;
  2035. {$endif arm}
  2036. cnodeutils.InsertThreadvars;
  2037. { generate rtti/init tables }
  2038. write_persistent_type_info(curr.localsymtable,false);
  2039. { if an Objective-C module, generate rtti and module info }
  2040. MaybeGenerateObjectiveCImageInfo(nil,curr.localsymtable);
  2041. { generate debuginfo }
  2042. if (cs_debuginfo in current_settings.moduleswitches) then
  2043. current_debuginfo.inserttypeinfo;
  2044. if islibrary or (target_info.system in systems_unit_program_exports) then
  2045. exportlib.generatelib;
  2046. { Reference all DEBUGINFO sections from the main .fpc section }
  2047. if (cs_debuginfo in current_settings.moduleswitches) then
  2048. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2049. { Resource strings }
  2050. GenerateResourceStrings;
  2051. { Windows widestring needing initialization }
  2052. cnodeutils.InsertWideInits;
  2053. { Resourcestring references (const foo:string=someresourcestring) }
  2054. cnodeutils.InsertResStrInits;
  2055. { insert Tables and StackLength }
  2056. cnodeutils.InsertInitFinalTable(curr);
  2057. cnodeutils.InsertThreadvarTablesTable;
  2058. cnodeutils.InsertResourceTablesTable;
  2059. cnodeutils.InsertWideInitsTablesTable;
  2060. cnodeutils.InsertResStrTablesTable;
  2061. cnodeutils.InsertMemorySizes;
  2062. { Insert symbol to resource info }
  2063. cnodeutils.InsertResourceInfo(resources_used);
  2064. { create callframe info }
  2065. create_dwarf_frame;
  2066. { create import library for all packages }
  2067. if packagelist.count>0 then
  2068. createimportlibfromexternals;
  2069. { generate imports }
  2070. if curr.ImportLibraryList.Count>0 then
  2071. importlib.generatelib;
  2072. { insert own objectfile }
  2073. insertobjectfile(curr);
  2074. { assemble and link }
  2075. create_objectfile(curr);
  2076. { We might need the symbols info if not using
  2077. the default do_extractsymbolinfo
  2078. which is a dummy function PM }
  2079. needsymbolinfo:=
  2080. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2081. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2082. { release all local symtables that are not needed anymore }
  2083. if (not needsymbolinfo) then
  2084. free_localsymtables(curr.localsymtable);
  2085. { leave when we got an error }
  2086. if (Errorcount>0) and not status.skip_error then
  2087. begin
  2088. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2089. status.skip_error:=true;
  2090. exit;
  2091. end;
  2092. { create the executable when we are at level 1 }
  2093. if (not curr.is_unit) and (curr.is_initial) then
  2094. proc_create_executable(curr,sysinitmod,islibrary);
  2095. { Give Fatal with error count for linker errors }
  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. end;
  2101. curr.state:=ms_processed;
  2102. end;
  2103. function proc_program_declarations(curr : tmodule; islibrary : boolean) : boolean;
  2104. var
  2105. initpd : tprocdef;
  2106. finalize_procinfo,
  2107. init_procinfo,
  2108. main_procinfo : tcgprocinfo;
  2109. force_init_final : boolean;
  2110. begin
  2111. result:=true;
  2112. main_procinfo:=nil;
  2113. init_procinfo:=nil;
  2114. finalize_procinfo:=nil;
  2115. set_current_module(curr);
  2116. { All units are read, now give them a number }
  2117. curr.updatemaps;
  2118. connect_loaded_units(curr,nil);
  2119. { consume the semicolon after maps have been updated else conditional compiling expressions
  2120. might cause internal errors, see tw8611 }
  2121. if curr.consume_semicolon_after_uses then
  2122. consume(_SEMICOLON);
  2123. {Insert the name of the main program into the symbol table.}
  2124. if curr.realmodulename^<>'' then
  2125. tabstractunitsymtable(curr.localsymtable).insertunit(cunitsym.create(curr.realmodulename^,curr));
  2126. Message1(parser_u_parsing_implementation,curr.mainsource);
  2127. symtablestack.push(curr.localsymtable);
  2128. {$ifdef jvm}
  2129. { fake classdef to represent the class corresponding to the unit }
  2130. addmoduleclass(curr);
  2131. {$endif}
  2132. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  2133. maybe_load_got(curr);
  2134. { create whole program optimisation information }
  2135. curr.wpoinfo:=tunitwpoinfo.create;
  2136. { The program intialization needs an alias, so it can be called
  2137. from the bootstrap code.}
  2138. if islibrary then
  2139. begin
  2140. initpd:=nil;
  2141. { ToDo: other systems that use indirect entry info, but check back with Windows! }
  2142. { we need to call FPC_LIBMAIN in sysinit which in turn will call PascalMain -> create dummy stub }
  2143. if target_info.system in systems_darwin then
  2144. begin
  2145. main_procinfo:=create_main_proc(make_mangledname('sysinitcallthrough',curr.localsymtable,'stub'),potype_libmainstub,curr.localsymtable);
  2146. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_LIBMAIN');
  2147. initpd:=main_procinfo.procdef;
  2148. main_procinfo.free;
  2149. end;
  2150. main_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,mainaliasname),potype_proginit,curr.localsymtable);
  2151. { Win32 startup code needs a single name }
  2152. if not(target_info.system in (systems_darwin+systems_aix)) then
  2153. main_procinfo.procdef.aliasnames.concat('PASCALMAIN')
  2154. else
  2155. main_procinfo.procdef.aliasnames.concat(target_info.Cprefix+'PASCALMAIN');
  2156. if not(target_info.system in systems_darwin) then
  2157. initpd:=main_procinfo.procdef;
  2158. cnodeutils.RegisterModuleInitFunction(initpd);
  2159. end
  2160. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macosclassic]+systems_darwin+systems_aix)) then
  2161. begin
  2162. { create a stub with the name of the desired main routine, with
  2163. the same signature as the C "main" function, and call through to
  2164. FPC_SYSTEMMAIN, which will initialise everything based on its
  2165. parameters. This function cannot be in the system unit, because
  2166. its name can be configured on the command line (for use with e.g.
  2167. SDL, where the main function should be called SDL_main) }
  2168. main_procinfo:=create_main_proc(mainaliasname,potype_mainstub,curr.localsymtable);
  2169. call_through_new_name(main_procinfo.procdef,target_info.cprefix+'FPC_SYSTEMMAIN');
  2170. main_procinfo.free;
  2171. { now create the PASCALMAIN routine (which will be called from
  2172. FPC_SYSTEMMAIN) }
  2173. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,curr.localsymtable);
  2174. end
  2175. else
  2176. begin
  2177. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,curr.localsymtable);
  2178. main_procinfo.procdef.aliasnames.concat('PASCALMAIN');
  2179. end;
  2180. main_procinfo.parse_body;
  2181. { save file pos for debuginfo }
  2182. curr.mainfilepos:=main_procinfo.entrypos;
  2183. { finalize? }
  2184. if token=_FINALIZATION then
  2185. begin
  2186. { Parse the finalize }
  2187. finalize_procinfo:=create_main_proc(make_mangledname('',curr.localsymtable,'finalize$'),potype_unitfinalize,curr.localsymtable);
  2188. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',curr.localsymtable,''));
  2189. finalize_procinfo.procdef.aliasnames.concat('PASCALFINALIZE');
  2190. finalize_procinfo.parse_body;
  2191. end;
  2192. { Generate specializations of objectdefs methods }
  2193. if Errorcount=0 then
  2194. generate_specialization_procs;
  2195. { This needs to be done before we generate the VMTs }
  2196. if (target_cpu=tsystemcpu.cpu_wasm32) then
  2197. add_synthetic_interface_classes_for_st(curr.localsymtable);
  2198. { Generate VMTs }
  2199. if Errorcount=0 then
  2200. write_vmts(curr.localsymtable,false);
  2201. { add implementations for synthetic method declarations added by
  2202. the compiler }
  2203. add_synthetic_method_implementations(curr.localsymtable);
  2204. { generate construction functions for all attributes in the program }
  2205. generate_attr_constrs(curr.used_rtti_attrs);
  2206. { should we force unit initialization? }
  2207. force_init_final:=tstaticsymtable(curr.localsymtable).needs_init_final;
  2208. if force_init_final or cnodeutils.force_init then
  2209. init_procinfo:=gen_implicit_initfinal(curr,mf_init,curr.localsymtable);
  2210. { Add symbol to the exports section for win32 so smartlinking a
  2211. DLL will include the edata section }
  2212. if assigned(exportlib) and
  2213. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2214. (mf_has_exports in curr.moduleflags) then
  2215. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',curr.localsymtable,''),0));
  2216. if (force_init_final or cnodeutils.force_final) and
  2217. (
  2218. not assigned(finalize_procinfo)
  2219. or has_no_code(finalize_procinfo.code)
  2220. ) then
  2221. begin
  2222. { first release the not used finalize procinfo }
  2223. if assigned(finalize_procinfo) then
  2224. begin
  2225. release_proc_symbol(finalize_procinfo.procdef);
  2226. release_main_proc(curr,finalize_procinfo);
  2227. end;
  2228. finalize_procinfo:=gen_implicit_initfinal(curr,mf_finalize,curr.localsymtable);
  2229. end;
  2230. { the finalization routine of libraries is generic (and all libraries need to }
  2231. { be finalized, so they can finalize any units they use }
  2232. { Place in "pure assembler" list so that the llvm assembler writer
  2233. directly emits the generated directives }
  2234. if (islibrary) then
  2235. cnodeutils.RegisterModuleFiniFunction(search_system_proc('fpc_lib_exit'));
  2236. { all labels must be defined before generating code }
  2237. if Errorcount=0 then
  2238. tstoredsymtable(curr.localsymtable).checklabels;
  2239. { See remark in unit init/final }
  2240. main_procinfo.generate_code_tree;
  2241. main_procinfo.resetprocdef;
  2242. release_main_proc(curr,main_procinfo);
  2243. if assigned(init_procinfo) then
  2244. begin
  2245. { initialization can be implicit only }
  2246. include(curr.moduleflags,mf_init);
  2247. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  2248. init_procinfo.generate_code;
  2249. init_procinfo.resetprocdef;
  2250. release_main_proc(curr,init_procinfo);
  2251. end;
  2252. if assigned(finalize_procinfo) then
  2253. begin
  2254. if force_init_final or
  2255. cnodeutils.force_init or
  2256. not(has_no_code(finalize_procinfo.code)) then
  2257. begin
  2258. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  2259. finalize_procinfo.generate_code_tree;
  2260. include(curr.moduleflags,mf_finalize);
  2261. end;
  2262. finalize_procinfo.resetprocdef;
  2263. release_main_proc(curr,finalize_procinfo);
  2264. end;
  2265. symtablestack.pop(curr.localsymtable);
  2266. { consume the last point }
  2267. consume(_POINT);
  2268. proc_program_after_parsing(curr,islibrary);
  2269. end;
  2270. procedure proc_library_header(curr: tmodule);
  2271. var
  2272. program_name : ansistring;
  2273. begin
  2274. consume(_LIBRARY);
  2275. program_name:=orgpattern;
  2276. consume(_ID);
  2277. while token=_POINT do
  2278. begin
  2279. consume(_POINT);
  2280. program_name:=program_name+'.'+orgpattern;
  2281. consume(_ID);
  2282. end;
  2283. curr.setmodulename(program_name);
  2284. curr.islibrary:=true;
  2285. exportlib.preparelib(program_name);
  2286. if tf_library_needs_pic in target_info.flags then
  2287. begin
  2288. include(current_settings.moduleswitches,cs_create_pic);
  2289. { also set create_pic for all unit compilation }
  2290. include(init_settings.moduleswitches,cs_create_pic);
  2291. end;
  2292. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2293. read, all following directives are parsed as well }
  2294. setupglobalswitches;
  2295. {$ifdef DEBUG_NODE_XML}
  2296. XMLInitializeNodeFile('library', program_name);
  2297. {$endif DEBUG_NODE_XML}
  2298. end;
  2299. type
  2300. TProgramParam = record
  2301. name : ansistring;
  2302. nr : dword;
  2303. end;
  2304. TProgramParamArray = array of TProgramParam;
  2305. procedure proc_program_header(curr: tmodule; out sc : TProgramParamArray);
  2306. var
  2307. program_name : ansistring;
  2308. paramnum : integer;
  2309. begin
  2310. sc:=nil;
  2311. consume(_PROGRAM);
  2312. program_name:=orgpattern;
  2313. consume(_ID);
  2314. while token=_POINT do
  2315. begin
  2316. consume(_POINT);
  2317. program_name:=program_name+'.'+orgpattern;
  2318. consume(_ID);
  2319. end;
  2320. curr.setmodulename(program_name);
  2321. if (target_info.system in systems_unit_program_exports) then
  2322. exportlib.preparelib(program_name);
  2323. if token=_LKLAMMER then
  2324. begin
  2325. consume(_LKLAMMER);
  2326. paramnum:=1;
  2327. repeat
  2328. if m_isolike_program_para in current_settings.modeswitches then
  2329. begin
  2330. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  2331. begin
  2332. { the symtablestack is not setup here, so text must be created later on }
  2333. Setlength(sc,length(sc)+1);
  2334. with sc[high(sc)] do
  2335. begin
  2336. name:=pattern;
  2337. nr:=paramnum;
  2338. end;
  2339. inc(paramnum);
  2340. end;
  2341. end;
  2342. consume(_ID);
  2343. until not try_to_consume(_COMMA);
  2344. consume(_RKLAMMER);
  2345. end;
  2346. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  2347. read, all following directives are parsed as well }
  2348. setupglobalswitches;
  2349. {$ifdef DEBUG_NODE_XML}
  2350. XMLInitializeNodeFile('program', program_name);
  2351. {$endif DEBUG_NODE_XML}
  2352. end;
  2353. function proc_program(curr: tmodule; islibrary : boolean) : boolean;
  2354. var
  2355. main_file : tinputfile;
  2356. consume_semicolon_after_uses,
  2357. consume_semicolon_after_loaded : boolean;
  2358. ps : tprogramparasym;
  2359. textsym : ttypesym;
  2360. sc : TProgramParamArray;
  2361. i : Longint;
  2362. feature : tfeature;
  2363. load_ok : boolean;
  2364. begin
  2365. result:=true;
  2366. Status.IsLibrary:=IsLibrary;
  2367. Status.IsPackage:=false;
  2368. Status.IsExe:=true;
  2369. parse_only:=false;
  2370. consume_semicolon_after_loaded:=false;
  2371. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  2372. sc:=nil;
  2373. { DLL defaults to create reloc info }
  2374. if islibrary or (target_info.system in [system_aarch64_win64]) then
  2375. begin
  2376. if not RelocSectionSetExplicitly then
  2377. RelocSection:=true;
  2378. end;
  2379. { Relocation works only without stabs under Windows when }
  2380. { external linker (LD) is used. LD generates relocs for }
  2381. { stab sections which is not loaded in memory. It causes }
  2382. { AV error when DLL is loaded and relocation is needed. }
  2383. { Internal linker does not have this problem. }
  2384. if RelocSection and
  2385. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  2386. (cs_link_extern in current_settings.globalswitches) then
  2387. begin
  2388. include(current_settings.globalswitches,cs_link_strip);
  2389. { Warning stabs info does not work with reloc section !! }
  2390. if (cs_debuginfo in current_settings.moduleswitches) and
  2391. (target_dbg.id=dbg_stabs) then
  2392. begin
  2393. Message1(parser_w_parser_reloc_no_debug,curr.mainsource);
  2394. Message(parser_w_parser_win32_debug_needs_WN);
  2395. exclude(current_settings.moduleswitches,cs_debuginfo);
  2396. end;
  2397. end;
  2398. { get correct output names }
  2399. main_file := current_scanner.inputfile;
  2400. while assigned(main_file.next) do
  2401. main_file := main_file.next;
  2402. curr.SetFileName(main_file.path+main_file.name,true);
  2403. if islibrary then
  2404. begin
  2405. proc_library_header(curr);
  2406. consume_semicolon_after_loaded:=true;
  2407. end
  2408. else if token=_PROGRAM then
  2409. { is there an program head ? }
  2410. begin
  2411. proc_program_header(curr,sc);
  2412. consume_semicolon_after_loaded:=true;
  2413. end
  2414. else
  2415. begin
  2416. if (target_info.system in systems_unit_program_exports) then
  2417. exportlib.preparelib(curr.realmodulename^);
  2418. { setup things using the switches }
  2419. setupglobalswitches;
  2420. {$ifdef DEBUG_NODE_XML}
  2421. XMLInitializeNodeFile('program', curr.realmodulename^);
  2422. {$endif DEBUG_NODE_XML}
  2423. end;
  2424. { load all packages, so we know whether a unit is contained inside a
  2425. package or not }
  2426. load_packages;
  2427. { set implementation flag }
  2428. curr.in_interface:=false;
  2429. curr.interface_compiled:=true;
  2430. { insert after the unit symbol tables the static symbol table
  2431. of the program }
  2432. curr.localsymtable:=tstaticsymtable.create(curr.modulename^,curr.moduleid);
  2433. { load system unit }
  2434. load_ok:=loadsystemunit(curr);
  2435. { consume the semicolon now that the system unit is loaded }
  2436. if consume_semicolon_after_loaded then
  2437. consume(_SEMICOLON);
  2438. { global switches are read, so further changes aren't allowed }
  2439. curr.in_global:=false;
  2440. { system unit is loaded, now insert feature defines }
  2441. for feature:=low(tfeature) to high(tfeature) do
  2442. if feature in features then
  2443. def_system_macro('FPC_HAS_FEATURE_'+featurestr[feature]);
  2444. { load standard units, e.g objpas,profile unit }
  2445. load_ok:=loaddefaultunits(curr) and load_ok;
  2446. { Load units provided on the command line }
  2447. load_ok:=loadautounits(curr) and load_ok;
  2448. { insert iso program parameters }
  2449. if length(sc)>0 then
  2450. begin
  2451. textsym:=search_system_type('TEXT');
  2452. if not(assigned(textsym)) then
  2453. internalerror(2013011201);
  2454. for i:=0 to high(sc) do
  2455. begin
  2456. ps:=cprogramparasym.create(sc[i].name,sc[i].nr);
  2457. curr.localsymtable.insertsym(ps,true);
  2458. end;
  2459. end;
  2460. { Load the units used by the program we compile. }
  2461. if token=_USES then
  2462. begin
  2463. // We can do this here: if there is no uses then the namespace directive makes no sense.
  2464. if Assigned(curr) then
  2465. curr.Loadlocalnamespacelist
  2466. else
  2467. current_namespacelist:=Nil;
  2468. parseusesclause(curr);
  2469. load_ok:=loadunits(curr,false) and load_ok;
  2470. consume_semicolon_after_uses:=true;
  2471. end
  2472. else
  2473. consume_semicolon_after_uses:=false;
  2474. Curr.consume_semicolon_after_uses:=consume_semicolon_after_uses;
  2475. if not load_ok then
  2476. curr.state:=ms_compiling_wait;
  2477. { Can we continue compiling ? }
  2478. result:=curr.state<>ms_compiling_wait;
  2479. if result then
  2480. result:=proc_program_declarations(curr,islibrary)
  2481. end;
  2482. end.