pmodules.pas 83 KB

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