pmodules.pas 79 KB

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