pmodules.pas 103 KB

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