pmodules.pas 102 KB

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