pmodules.pas 103 KB

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