pmodules.pas 90 KB

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