pmodules.pas 97 KB

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