pmodules.pas 88 KB

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