pmodules.pas 90 KB

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