pmodules.pas 81 KB

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