pmodules.pas 89 KB

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