pmodules.pas 89 KB

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