pmodules.pas 102 KB

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