pmodules.pas 87 KB

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