pmodules.pas 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534
  1. {
  2. Copyright (c) 1998-2002 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_program(islibrary : boolean);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,version,systems,tokens,
  26. cutils,cfileutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmdata,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,ncgutil,
  32. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  33. cresstr,procinfo,
  34. pexports,
  35. scanner,pbase,pexpr,psystem,psub,pdecsub;
  36. procedure create_objectfile;
  37. var
  38. DLLScanner : TDLLScanner;
  39. s : string;
  40. KeepShared : TStringList;
  41. begin
  42. { try to create import entries from system dlls }
  43. if (tf_has_dllscanner in target_info.flags) and
  44. (not current_module.linkOtherSharedLibs.Empty) then
  45. begin
  46. { Init DLLScanner }
  47. if assigned(CDLLScanner[target_info.system]) then
  48. DLLScanner:=CDLLScanner[target_info.system].Create
  49. else
  50. internalerror(200104121);
  51. KeepShared:=TStringList.Create;
  52. { Walk all shared libs }
  53. While not current_module.linkOtherSharedLibs.Empty do
  54. begin
  55. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  56. if not DLLScanner.scan(s) then
  57. KeepShared.Concat(s);
  58. end;
  59. DLLscanner.Free;
  60. { Recreate import section }
  61. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  62. begin
  63. if assigned(current_asmdata.asmlists[al_imports]) then
  64. current_asmdata.asmlists[al_imports].clear
  65. else
  66. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  67. importlib.generatelib;
  68. end;
  69. { Readd the not processed files }
  70. while not KeepShared.Empty do
  71. begin
  72. s:=KeepShared.GetFirst;
  73. current_module.linkOtherSharedLibs.add(s,link_always);
  74. end;
  75. KeepShared.Free;
  76. end;
  77. { Start and end module debuginfo, at least required for stabs
  78. to insert n_sourcefile lines }
  79. if (cs_debuginfo in current_settings.moduleswitches) or
  80. (cs_use_lineinfo in current_settings.globalswitches) then
  81. debuginfo.insertmoduleinfo;
  82. { create the .s file and assemble it }
  83. GenerateAsm(false);
  84. { Also create a smartlinked version ? }
  85. if (cs_create_smart in current_settings.moduleswitches) and
  86. (not use_smartlink_section) then
  87. begin
  88. GenerateAsm(true);
  89. if (af_needar in target_asm.flags) then
  90. Linker.MakeStaticLibrary;
  91. end;
  92. { resource files }
  93. CompileResourceFiles;
  94. end;
  95. procedure insertobjectfile;
  96. { Insert the used object file for this unit in the used list for this unit }
  97. begin
  98. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  99. current_module.flags:=current_module.flags or uf_static_linked;
  100. if (cs_create_smart in current_settings.moduleswitches) and
  101. not use_smartlink_section then
  102. begin
  103. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  104. current_module.flags:=current_module.flags or uf_smart_linked;
  105. end;
  106. end;
  107. procedure create_dwarf;
  108. begin
  109. { Dwarf conflicts with smartlinking in separate .a files }
  110. if (cs_create_smart in current_settings.moduleswitches) and
  111. not use_smartlink_section then
  112. exit;
  113. { Call frame information }
  114. if (tf_needs_dwarf_cfi in target_info.flags) and
  115. (af_supports_dwarf in target_asm.flags) then
  116. begin
  117. current_asmdata.asmlists[al_dwarf]:=TAsmList.create;
  118. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  119. end;
  120. end;
  121. procedure InsertThreadvarTablesTable;
  122. var
  123. hp : tused_unit;
  124. ltvTables : TAsmList;
  125. count : longint;
  126. begin
  127. if (tf_section_threadvars in target_info.flags) then
  128. exit;
  129. ltvTables:=TAsmList.Create;
  130. count:=0;
  131. hp:=tused_unit(usedunits.first);
  132. while assigned(hp) do
  133. begin
  134. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  135. begin
  136. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  137. inc(count);
  138. end;
  139. hp:=tused_unit(hp.next);
  140. end;
  141. { Add program threadvars, if any }
  142. If (current_module.flags and uf_threadvars)=uf_threadvars then
  143. begin
  144. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  145. inc(count);
  146. end;
  147. { Insert TableCount at start }
  148. ltvTables.insert(Tai_const.Create_32bit(count));
  149. { insert in data segment }
  150. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  151. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(aint));
  152. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  153. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  154. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  155. ltvTables.free;
  156. end;
  157. procedure AddToThreadvarList(p:TObject;arg:pointer);
  158. var
  159. ltvTable : TAsmList;
  160. begin
  161. ltvTable:=TAsmList(arg);
  162. if (tsym(p).typ=staticvarsym) and
  163. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  164. begin
  165. { address of threadvar }
  166. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  167. { size of threadvar }
  168. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  169. end;
  170. end;
  171. procedure InsertThreadvars;
  172. var
  173. s : string;
  174. ltvTable : TAsmList;
  175. begin
  176. if (tf_section_threadvars in target_info.flags) then
  177. exit;
  178. ltvTable:=TAsmList.create;
  179. if assigned(current_module.globalsymtable) then
  180. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  181. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  182. if ltvTable.first<>nil then
  183. begin
  184. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  185. { end of the list marker }
  186. ltvTable.concat(tai_const.create_sym(nil));
  187. { add to datasegment }
  188. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  189. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(aint));
  190. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  191. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  192. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  193. current_module.flags:=current_module.flags or uf_threadvars;
  194. end;
  195. ltvTable.Free;
  196. end;
  197. {$ifdef arm}
  198. procedure InsertPData;
  199. var
  200. prolog: TAsmList;
  201. begin
  202. prolog:=TAsmList.create;
  203. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(aint),secorder_begin);
  204. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  205. prolog.concat(Tai_const.Create_32bit(0));
  206. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  207. current_asmdata.asmlists[al_start].insertList(prolog);
  208. prolog.Free;
  209. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(aint));
  210. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  211. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit($cfffff02));
  212. end;
  213. {$endif arm}
  214. Procedure InsertResourceInfo;
  215. var
  216. hp : tused_unit;
  217. found : Boolean;
  218. I : Integer;
  219. ResourceInfo : TAsmList;
  220. begin
  221. if target_res.id=res_elf then
  222. begin
  223. hp:=tused_unit(usedunits.first);
  224. found:=false;
  225. Found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  226. If not found then
  227. While Assigned(hp) and not Found do
  228. begin
  229. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  230. hp:=tused_unit(hp.next);
  231. end;
  232. ResourceInfo:=TAsmList.Create;
  233. if found then
  234. begin
  235. { Valid pointer to resource information }
  236. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  237. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0));
  238. {$ifdef EXTERNALRESPTRS}
  239. current_module.linkotherofiles.add('resptrs.o',link_always);
  240. {$else EXTERNALRESPTRS}
  241. new_section(ResourceInfo,sec_fpc,'resptrs',4);
  242. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESSYMBOL',AT_DATA,0));
  243. For I:=1 to 32 do
  244. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  245. {$endif EXTERNALRESPTRS}
  246. end
  247. else
  248. begin
  249. { Nil pointer to resource information }
  250. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  251. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  252. end;
  253. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  254. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  255. ResourceInfo.free;
  256. end;
  257. end;
  258. Procedure InsertResourceTablesTable;
  259. var
  260. hp : tmodule;
  261. ResourceStringTables : tasmlist;
  262. count : longint;
  263. begin
  264. ResourceStringTables:=tasmlist.Create;
  265. count:=0;
  266. hp:=tmodule(loaded_units.first);
  267. while assigned(hp) do
  268. begin
  269. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  270. begin
  271. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  272. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  273. inc(count);
  274. end;
  275. hp:=tmodule(hp.next);
  276. end;
  277. { Insert TableCount at start }
  278. ResourceStringTables.insert(Tai_const.Create_aint(count));
  279. { Add to data segment }
  280. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  281. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(aint));
  282. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  283. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  284. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  285. ResourceStringTables.free;
  286. end;
  287. procedure InsertInitFinalTable;
  288. var
  289. hp : tused_unit;
  290. unitinits : TAsmList;
  291. count : longint;
  292. begin
  293. unitinits:=TAsmList.Create;
  294. count:=0;
  295. hp:=tused_unit(usedunits.first);
  296. while assigned(hp) do
  297. begin
  298. { call the unit init code and make it external }
  299. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  300. begin
  301. if (hp.u.flags and uf_init)<>0 then
  302. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  303. else
  304. unitinits.concat(Tai_const.Create_sym(nil));
  305. if (hp.u.flags and uf_finalize)<>0 then
  306. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  307. else
  308. unitinits.concat(Tai_const.Create_sym(nil));
  309. inc(count);
  310. end;
  311. hp:=tused_unit(hp.next);
  312. end;
  313. { Insert initialization/finalization of the program }
  314. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  315. begin
  316. if (current_module.flags and uf_init)<>0 then
  317. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  318. else
  319. unitinits.concat(Tai_const.Create_sym(nil));
  320. if (current_module.flags and uf_finalize)<>0 then
  321. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  322. else
  323. unitinits.concat(Tai_const.Create_sym(nil));
  324. inc(count);
  325. end;
  326. { Insert TableCount,InitCount at start }
  327. unitinits.insert(Tai_const.Create_32bit(0));
  328. unitinits.insert(Tai_const.Create_32bit(count));
  329. { Add to data segment }
  330. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  331. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(aint));
  332. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  333. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  334. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  335. unitinits.free;
  336. end;
  337. procedure insertmemorysizes;
  338. {$IFDEF POWERPC}
  339. var
  340. stkcookie: string;
  341. {$ENDIF POWERPC}
  342. begin
  343. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  344. { Insert Ident of the compiler in the .fpc.version section }
  345. current_asmdata.asmlists[al_globals].concat(Tai_section.create(sec_fpc,'version',0));
  346. current_asmdata.asmlists[al_globals].concat(Tai_align.Create(const_align(32)));
  347. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  348. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  349. { stacksize can be specified and is now simulated }
  350. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(aint));
  351. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(aint)));
  352. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(stacksize));
  353. {$IFDEF POWERPC}
  354. { AmigaOS4 "stack cookie" support }
  355. if ( target_info.system = system_powerpc_amiga ) then
  356. begin
  357. { this symbol is needed to ignite powerpc amigaos' }
  358. { stack allocation magic for us with the given stack size. }
  359. { note: won't work for m68k amigaos or morphos. (KB) }
  360. str(stacksize,stkcookie);
  361. stkcookie:='$STACK: '+stkcookie+#0;
  362. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  363. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  364. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  365. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  366. end;
  367. {$ENDIF POWERPC}
  368. { Initial heapsize }
  369. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  370. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(aint));
  371. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(aint)));
  372. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(heapsize));
  373. { Initial heapsize }
  374. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  375. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  376. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  377. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  378. end;
  379. procedure AddUnit(const s:string);
  380. var
  381. hp : tppumodule;
  382. unitsym : tunitsym;
  383. begin
  384. { load unit }
  385. hp:=registerunit(current_module,s,'');
  386. hp.loadppu;
  387. hp.adddependency(current_module);
  388. { add to symtable stack }
  389. symtablestack.push(hp.globalsymtable);
  390. if (m_mac in current_settings.modeswitches) and
  391. assigned(hp.globalmacrosymtable) then
  392. macrosymtablestack.push(hp.globalmacrosymtable);
  393. { insert unitsym }
  394. unitsym:=tunitsym.create(s,hp);
  395. inc(unitsym.refs);
  396. current_module.localsymtable.insert(unitsym);
  397. { add to used units }
  398. current_module.addusedunit(hp,false,unitsym);
  399. end;
  400. procedure maybeloadvariantsunit;
  401. var
  402. hp : tmodule;
  403. begin
  404. { Do we need the variants unit? Skip this
  405. for VarUtils unit for bootstrapping }
  406. if (current_module.flags and uf_uses_variants=0) or
  407. (current_module.modulename^='VARUTILS') then
  408. exit;
  409. { Variants unit already loaded? }
  410. hp:=tmodule(loaded_units.first);
  411. while assigned(hp) do
  412. begin
  413. if hp.modulename^='VARIANTS' then
  414. exit;
  415. hp:=tmodule(hp.next);
  416. end;
  417. { Variants unit is not loaded yet, load it now }
  418. Message(parser_w_implicit_uses_of_variants_unit);
  419. AddUnit('Variants');
  420. end;
  421. procedure loaddefaultunits;
  422. begin
  423. { we are going to rebuild the symtablestack, clear it first }
  424. symtablestack.clear;
  425. macrosymtablestack.clear;
  426. { macro symtable }
  427. macrosymtablestack.push(initialmacrosymtable);
  428. { are we compiling the system unit? }
  429. if (cs_compilesystem in current_settings.moduleswitches) then
  430. begin
  431. systemunit:=tglobalsymtable(current_module.localsymtable);
  432. { create system defines }
  433. create_intern_symbols;
  434. create_intern_types;
  435. { Set the owner of errorsym and errortype to symtable to
  436. prevent crashes when accessing .owner }
  437. generrorsym.owner:=systemunit;
  438. generrordef.owner:=systemunit;
  439. exit;
  440. end;
  441. { insert the system unit, it is allways the first. Load also the
  442. internal types from the system unit }
  443. AddUnit('System');
  444. systemunit:=tglobalsymtable(symtablestack.top);
  445. load_intern_types;
  446. { Set the owner of errorsym and errortype to symtable to
  447. prevent crashes when accessing .owner }
  448. generrorsym.owner:=systemunit;
  449. generrordef.owner:=systemunit;
  450. { Units only required for main module }
  451. if not(current_module.is_unit) then
  452. begin
  453. { Heaptrc unit, load heaptrace before any other units especially objpas }
  454. if (cs_use_heaptrc in current_settings.globalswitches) then
  455. AddUnit('HeapTrc');
  456. { Lineinfo unit }
  457. if (cs_use_lineinfo in current_settings.globalswitches) then
  458. AddUnit('LineInfo');
  459. { Lineinfo unit }
  460. if (cs_gdb_valgrind in current_settings.globalswitches) then
  461. AddUnit('CMem');
  462. {$ifdef cpufpemu}
  463. { Floating point emulation unit?
  464. softfpu must be in the system unit anyways (FK)
  465. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  466. AddUnit('SoftFpu');
  467. }
  468. {$endif cpufpemu}
  469. end;
  470. { Objpas unit? }
  471. if m_objpas in current_settings.modeswitches then
  472. AddUnit('ObjPas');
  473. { Macpas unit? }
  474. if m_mac in current_settings.modeswitches then
  475. AddUnit('MacPas');
  476. { Profile unit? Needed for go32v2 only }
  477. if (cs_profile in current_settings.moduleswitches) and
  478. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  479. AddUnit('Profile');
  480. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  481. begin
  482. AddUnit('FPCylix');
  483. AddUnit('DynLibs');
  484. end;
  485. end;
  486. procedure loadautounits;
  487. var
  488. hs,s : string;
  489. begin
  490. hs:=autoloadunits;
  491. repeat
  492. s:=GetToken(hs,',');
  493. if s='' then
  494. break;
  495. AddUnit(s);
  496. until false;
  497. end;
  498. procedure loadunits;
  499. var
  500. s,sorg : TIDString;
  501. fn : string;
  502. pu : tused_unit;
  503. hp2 : tmodule;
  504. unitsym : tunitsym;
  505. begin
  506. consume(_USES);
  507. repeat
  508. s:=pattern;
  509. sorg:=orgpattern;
  510. consume(_ID);
  511. { support "<unit> in '<file>'" construct, but not for tp7 }
  512. fn:='';
  513. if not(m_tp7 in current_settings.modeswitches) and
  514. try_to_consume(_OP_IN) then
  515. fn:=FixFileName(get_stringconst);
  516. { Give a warning if objpas is loaded }
  517. if s='OBJPAS' then
  518. Message(parser_w_no_objpas_use_mode);
  519. { Using the unit itself is not possible }
  520. if (s<>current_module.modulename^) then
  521. begin
  522. { check if the unit is already used }
  523. hp2:=nil;
  524. pu:=tused_unit(current_module.used_units.first);
  525. while assigned(pu) do
  526. begin
  527. if (pu.u.modulename^=s) then
  528. begin
  529. hp2:=pu.u;
  530. break;
  531. end;
  532. pu:=tused_unit(pu.next);
  533. end;
  534. if not assigned(hp2) then
  535. hp2:=registerunit(current_module,sorg,fn)
  536. else
  537. Message1(sym_e_duplicate_id,s);
  538. { Create unitsym, we need to use the name as specified, we
  539. can not use the modulename because that can be different
  540. when -Un is used }
  541. unitsym:=tunitsym.create(sorg,nil);
  542. current_module.localsymtable.insert(unitsym);
  543. { the current module uses the unit hp2 }
  544. current_module.addusedunit(hp2,true,unitsym);
  545. end
  546. else
  547. Message1(sym_e_duplicate_id,s);
  548. if token=_COMMA then
  549. begin
  550. pattern:='';
  551. consume(_COMMA);
  552. end
  553. else
  554. break;
  555. until false;
  556. { Load the units }
  557. pu:=tused_unit(current_module.used_units.first);
  558. while assigned(pu) do
  559. begin
  560. { Only load the units that are in the current
  561. (interface/implementation) uses clause }
  562. if pu.in_uses and
  563. (pu.in_interface=current_module.in_interface) then
  564. begin
  565. tppumodule(pu.u).loadppu;
  566. { is our module compiled? then we can stop }
  567. if current_module.state=ms_compiled then
  568. exit;
  569. { add this unit to the dependencies }
  570. pu.u.adddependency(current_module);
  571. { save crc values }
  572. pu.checksum:=pu.u.crc;
  573. pu.interface_checksum:=pu.u.interface_crc;
  574. { connect unitsym to the module }
  575. pu.unitsym.module:=pu.u;
  576. { add to symtable stack }
  577. symtablestack.push(pu.u.globalsymtable);
  578. if (m_mac in current_settings.modeswitches) and
  579. assigned(pu.u.globalmacrosymtable) then
  580. macrosymtablestack.push(pu.u.globalmacrosymtable);
  581. end;
  582. pu:=tused_unit(pu.next);
  583. end;
  584. consume(_SEMICOLON);
  585. end;
  586. procedure reset_all_defs;
  587. procedure reset_used_unit_defs(hp:tmodule);
  588. var
  589. pu : tused_unit;
  590. begin
  591. pu:=tused_unit(hp.used_units.first);
  592. while assigned(pu) do
  593. begin
  594. if not pu.u.is_reset then
  595. begin
  596. { prevent infinte loop for circular dependencies }
  597. pu.u.is_reset:=true;
  598. if assigned(pu.u.globalsymtable) then
  599. begin
  600. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  601. reset_used_unit_defs(pu.u);
  602. end;
  603. end;
  604. pu:=tused_unit(pu.next);
  605. end;
  606. end;
  607. var
  608. hp2 : tmodule;
  609. begin
  610. hp2:=tmodule(loaded_units.first);
  611. while assigned(hp2) do
  612. begin
  613. hp2.is_reset:=false;
  614. hp2:=tmodule(hp2.next);
  615. end;
  616. reset_used_unit_defs(current_module);
  617. end;
  618. procedure free_localsymtables(st:TSymtable);
  619. var
  620. i : longint;
  621. def : tstoreddef;
  622. pd : tprocdef;
  623. begin
  624. for i:=0 to st.DefList.Count-1 do
  625. begin
  626. def:=tstoreddef(st.DefList[i]);
  627. if def.typ=procdef then
  628. begin
  629. pd:=tprocdef(def);
  630. if assigned(pd.localst) and
  631. (pd.localst.symtabletype<>staticsymtable) and
  632. not(po_inline in pd.procoptions) then
  633. begin
  634. free_localsymtables(pd.localst);
  635. pd.localst.free;
  636. pd.localst:=nil;
  637. end;
  638. end;
  639. end;
  640. end;
  641. procedure parse_implementation_uses;
  642. begin
  643. if token=_USES then
  644. loadunits;
  645. end;
  646. procedure setupglobalswitches;
  647. begin
  648. if (cs_create_pic in current_settings.moduleswitches) then
  649. begin
  650. def_system_macro('FPC_PIC');
  651. def_system_macro('PIC');
  652. end;
  653. end;
  654. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  655. var
  656. ps : tprocsym;
  657. pd : tprocdef;
  658. begin
  659. { there should be no current_procinfo available }
  660. if assigned(current_procinfo) then
  661. internalerror(200304275);
  662. {Generate a procsym for main}
  663. ps:=tprocsym.create('$'+name);
  664. { main are allways used }
  665. inc(ps.refs);
  666. st.insert(ps);
  667. pd:=tprocdef.create(main_program_level);
  668. include(pd.procoptions,po_global);
  669. pd.procsym:=ps;
  670. ps.ProcdefList.Add(pd);
  671. { set procdef options }
  672. pd.proctypeoption:=potype;
  673. pd.proccalloption:=pocall_default;
  674. pd.forwarddef:=false;
  675. pd.setmangledname(target_info.cprefix+name);
  676. pd.aliasnames.insert(pd.mangledname);
  677. handle_calling_convention(pd);
  678. { We don't need is a local symtable. Change it into the static
  679. symtable }
  680. pd.localst.free;
  681. pd.localst:=st;
  682. { set procinfo and current_procinfo.procdef }
  683. result:=tcgprocinfo(cprocinfo.create(nil));
  684. result.procdef:=pd;
  685. { main proc does always a call e.g. to init system unit }
  686. include(result.flags,pi_do_call);
  687. end;
  688. procedure release_main_proc(pi:tcgprocinfo);
  689. begin
  690. { remove localst as it was replaced by staticsymtable }
  691. pi.procdef.localst:=nil;
  692. { remove procinfo }
  693. current_module.procinfo:=nil;
  694. pi.free;
  695. pi:=nil;
  696. end;
  697. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  698. begin
  699. { update module flags }
  700. current_module.flags:=current_module.flags or flag;
  701. { create procdef }
  702. case flag of
  703. uf_init :
  704. begin
  705. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  706. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  707. end;
  708. uf_finalize :
  709. begin
  710. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  711. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  712. end;
  713. else
  714. internalerror(200304253);
  715. end;
  716. result.code:=cnothingnode.create;
  717. end;
  718. procedure copy_macro(p:TObject; arg:pointer);
  719. begin
  720. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  721. end;
  722. procedure proc_unit;
  723. function is_assembler_generated:boolean;
  724. var
  725. hal : tasmlisttype;
  726. begin
  727. result:=false;
  728. if Errorcount=0 then
  729. begin
  730. for hal:=low(TasmlistType) to high(TasmlistType) do
  731. if not current_asmdata.asmlists[hal].empty then
  732. begin
  733. result:=true;
  734. exit;
  735. end;
  736. end;
  737. end;
  738. var
  739. main_file: tinputfile;
  740. {$ifdef EXTDEBUG}
  741. store_crc,
  742. {$endif EXTDEBUG}
  743. store_interface_crc : cardinal;
  744. s1,s2 : ^string; {Saves stack space}
  745. force_init_final : boolean;
  746. init_procinfo,
  747. finalize_procinfo : tcgprocinfo;
  748. unitname8 : string[8];
  749. has_impl,ag: boolean;
  750. {$ifdef i386}
  751. gotvarsym : tstaticvarsym;
  752. {$endif i386}
  753. begin
  754. init_procinfo:=nil;
  755. finalize_procinfo:=nil;
  756. if m_mac in current_settings.modeswitches then
  757. current_module.mode_switch_allowed:= false;
  758. consume(_UNIT);
  759. if compile_level=1 then
  760. Status.IsExe:=false;
  761. if token=_ID then
  762. begin
  763. { create filenames and unit name }
  764. main_file := current_scanner.inputfile;
  765. while assigned(main_file.next) do
  766. main_file := main_file.next;
  767. new(s1);
  768. s1^:=current_module.modulename^;
  769. current_module.SetFileName(main_file.path^+main_file.name^,true);
  770. current_module.SetModuleName(orgpattern);
  771. { check for system unit }
  772. new(s2);
  773. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  774. unitname8:=copy(current_module.modulename^,1,8);
  775. if (cs_check_unit_name in current_settings.globalswitches) and
  776. (
  777. not(
  778. (current_module.modulename^=s2^) or
  779. (
  780. (length(current_module.modulename^)>8) and
  781. (unitname8=s2^)
  782. )
  783. )
  784. or
  785. (
  786. (length(s1^)>8) and
  787. (s1^<>current_module.modulename^)
  788. )
  789. ) then
  790. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  791. if (current_module.modulename^='SYSTEM') then
  792. include(current_settings.moduleswitches,cs_compilesystem);
  793. dispose(s2);
  794. dispose(s1);
  795. end;
  796. if (target_info.system in system_unit_program_exports) then
  797. exportlib.preparelib(current_module.realmodulename^);
  798. consume(_ID);
  799. consume(_SEMICOLON);
  800. consume(_INTERFACE);
  801. { global switches are read, so further changes aren't allowed }
  802. current_module.in_global:=false;
  803. { handle the global switches }
  804. setupglobalswitches;
  805. message1(unit_u_loading_interface_units,current_module.modulename^);
  806. { update status }
  807. status.currentmodule:=current_module.realmodulename^;
  808. { maybe turn off m_objpas if we are compiling objpas }
  809. if (current_module.modulename^='OBJPAS') then
  810. exclude(current_settings.modeswitches,m_objpas);
  811. { maybe turn off m_mac if we are compiling macpas }
  812. if (current_module.modulename^='MACPAS') then
  813. exclude(current_settings.modeswitches,m_mac);
  814. parse_only:=true;
  815. { generate now the global symboltable,
  816. define first as local to overcome dependency conflicts }
  817. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  818. { insert unitsym of this unit to prevent other units having
  819. the same name }
  820. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  821. { load default units, like the system unit }
  822. loaddefaultunits;
  823. { insert qualifier for the system unit (allows system.writeln) }
  824. if not(cs_compilesystem in current_settings.moduleswitches) and
  825. (token=_USES) then
  826. begin
  827. loadunits;
  828. { has it been compiled at a higher level ?}
  829. if current_module.state=ms_compiled then
  830. exit;
  831. end;
  832. { move the global symtable from the temporary local to global }
  833. current_module.globalsymtable:=current_module.localsymtable;
  834. current_module.localsymtable:=nil;
  835. reset_all_defs;
  836. { number all units, so we know if a unit is used by this unit or
  837. needs to be added implicitly }
  838. current_module.updatemaps;
  839. { ... parse the declarations }
  840. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  841. symtablestack.push(current_module.globalsymtable);
  842. read_interface_declarations;
  843. symtablestack.pop(current_module.globalsymtable);
  844. { Export macros defined in the interface for macpas. The macros
  845. are put in the globalmacrosymtable that will only be used by other
  846. units. The current unit continues to use the localmacrosymtable }
  847. if (m_mac in current_settings.modeswitches) then
  848. begin
  849. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  850. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  851. end;
  852. { leave when we got an error }
  853. if (Errorcount>0) and not status.skip_error then
  854. begin
  855. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  856. status.skip_error:=true;
  857. exit;
  858. end;
  859. { Our interface is compiled, generate CRC and switch to implementation }
  860. if not(cs_compilesystem in current_settings.moduleswitches) and
  861. (Errorcount=0) then
  862. tppumodule(current_module).getppucrc;
  863. current_module.in_interface:=false;
  864. current_module.interface_compiled:=true;
  865. { First reload all units depending on our interface, we need to do this
  866. in the implementation part to prevent errorneous circular references }
  867. reload_flagged_units;
  868. { Parse the implementation section }
  869. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  870. has_impl:= false
  871. else
  872. has_impl:= true;
  873. parse_only:=false;
  874. { generates static symbol table }
  875. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  876. {$ifdef i386}
  877. if cs_create_pic in current_settings.moduleswitches then
  878. begin
  879. { insert symbol for got access in assembler code}
  880. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  881. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  882. current_module.localsymtable.insert(gotvarsym);
  883. { avoid unnecessary warnings }
  884. gotvarsym.varstate:=vs_read;
  885. gotvarsym.refs:=1;
  886. end;
  887. {$endif i386}
  888. if has_impl then
  889. begin
  890. consume(_IMPLEMENTATION);
  891. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  892. { Read the implementation units }
  893. parse_implementation_uses;
  894. end;
  895. if current_module.state=ms_compiled then
  896. exit;
  897. { reset ranges/stabs in exported definitions }
  898. reset_all_defs;
  899. { All units are read, now give them a number }
  900. current_module.updatemaps;
  901. symtablestack.push(current_module.globalsymtable);
  902. symtablestack.push(current_module.localsymtable);
  903. if has_impl then
  904. begin
  905. Message1(parser_u_parsing_implementation,current_module.modulename^);
  906. if current_module.in_interface then
  907. internalerror(200212285);
  908. { Compile the unit }
  909. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  910. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  911. init_procinfo.parse_body;
  912. { save file pos for debuginfo }
  913. current_module.mainfilepos:=init_procinfo.entrypos;
  914. end;
  915. { Generate specializations of objectdefs methods }
  916. generate_specialization_procs;
  917. { if the unit contains ansi/widestrings, initialization and
  918. finalization code must be forced }
  919. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  920. tstaticsymtable(current_module.localsymtable).needs_init_final;
  921. { should we force unit initialization? }
  922. { this is a hack, but how can it be done better ? }
  923. if force_init_final and ((current_module.flags and uf_init)=0) then
  924. begin
  925. { first release the not used init procinfo }
  926. if assigned(init_procinfo) then
  927. release_main_proc(init_procinfo);
  928. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  929. end;
  930. { finalize? }
  931. if has_impl and (token=_FINALIZATION) then
  932. begin
  933. { set module options }
  934. current_module.flags:=current_module.flags or uf_finalize;
  935. { Compile the finalize }
  936. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  937. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  938. finalize_procinfo.parse_body;
  939. end
  940. else if force_init_final then
  941. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  942. { Now both init and finalize bodies are read and it is known
  943. which variables are used in both init and finalize we can now
  944. generate the code. This is required to prevent putting a variable in
  945. a register that is also used in the finalize body (PFV) }
  946. if assigned(init_procinfo) then
  947. begin
  948. init_procinfo.generate_code;
  949. init_procinfo.resetprocdef;
  950. release_main_proc(init_procinfo);
  951. end;
  952. if assigned(finalize_procinfo) then
  953. begin
  954. finalize_procinfo.generate_code;
  955. finalize_procinfo.resetprocdef;
  956. release_main_proc(finalize_procinfo);
  957. end;
  958. symtablestack.pop(current_module.localsymtable);
  959. symtablestack.pop(current_module.globalsymtable);
  960. { the last char should always be a point }
  961. consume(_POINT);
  962. if (Errorcount=0) then
  963. begin
  964. { tests, if all (interface) forwards are resolved }
  965. tstoredsymtable(current_module.globalsymtable).check_forwards;
  966. { check if all private fields are used }
  967. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  968. { remove cross unit overloads }
  969. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  970. { test static symtable }
  971. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  972. tstoredsymtable(current_module.localsymtable).allprivatesused;
  973. tstoredsymtable(current_module.localsymtable).check_forwards;
  974. tstoredsymtable(current_module.localsymtable).checklabels;
  975. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  976. { used units }
  977. current_module.allunitsused;
  978. end;
  979. { leave when we got an error }
  980. if (Errorcount>0) and not status.skip_error then
  981. begin
  982. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  983. status.skip_error:=true;
  984. exit;
  985. end;
  986. { do we need to add the variants unit? }
  987. maybeloadvariantsunit;
  988. { generate debuginfo }
  989. if (cs_debuginfo in current_settings.moduleswitches) then
  990. debuginfo.inserttypeinfo;
  991. { generate wrappers for interfaces }
  992. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  993. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  994. { generate pic helpers to load eip if necessary }
  995. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  996. { Tables }
  997. insertThreadVars;
  998. { Resource strings }
  999. GenerateResourceStrings;
  1000. { generate imports }
  1001. if current_module.ImportLibraryList.Count>0 then
  1002. importlib.generatelib;
  1003. { insert own objectfile, or say that it's in a library
  1004. (no check for an .o when loading) }
  1005. ag:=is_assembler_generated;
  1006. if ag then
  1007. insertobjectfile
  1008. else
  1009. begin
  1010. current_module.flags:=current_module.flags or uf_no_link;
  1011. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1012. end;
  1013. if ag then
  1014. begin
  1015. { create dwarf debuginfo }
  1016. create_dwarf;
  1017. { assemble }
  1018. create_objectfile;
  1019. end;
  1020. { Write out the ppufile after the object file has been created }
  1021. store_interface_crc:=current_module.interface_crc;
  1022. {$ifdef EXTDEBUG}
  1023. store_crc:=current_module.crc;
  1024. {$endif EXTDEBUG}
  1025. if (Errorcount=0) then
  1026. tppumodule(current_module).writeppu;
  1027. if not(cs_compilesystem in current_settings.moduleswitches) then
  1028. if store_interface_crc<>current_module.interface_crc then
  1029. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1030. {$ifdef EXTDEBUG}
  1031. if not(cs_compilesystem in current_settings.moduleswitches) then
  1032. if (store_crc<>current_module.crc) and simplify_ppu then
  1033. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1034. {$endif EXTDEBUG}
  1035. { release all overload references and local symtables that
  1036. are not needed anymore }
  1037. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1038. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1039. free_localsymtables(current_module.globalsymtable);
  1040. free_localsymtables(current_module.localsymtable);
  1041. { leave when we got an error }
  1042. if (Errorcount>0) and not status.skip_error then
  1043. begin
  1044. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1045. status.skip_error:=true;
  1046. exit;
  1047. end;
  1048. Message1(unit_u_finished_compiling,current_module.modulename^);
  1049. end;
  1050. procedure proc_program(islibrary : boolean);
  1051. var
  1052. main_file : tinputfile;
  1053. hp,hp2 : tmodule;
  1054. finalize_procinfo,
  1055. init_procinfo,
  1056. main_procinfo : tcgprocinfo;
  1057. force_init_final : boolean;
  1058. begin
  1059. DLLsource:=islibrary;
  1060. Status.IsLibrary:=IsLibrary;
  1061. Status.IsExe:=true;
  1062. parse_only:=false;
  1063. main_procinfo:=nil;
  1064. init_procinfo:=nil;
  1065. finalize_procinfo:=nil;
  1066. { DLL defaults to create reloc info }
  1067. if islibrary then
  1068. begin
  1069. if not RelocSectionSetExplicitly then
  1070. RelocSection:=true;
  1071. end;
  1072. { relocation works only without stabs under win32 !! PM }
  1073. { internal assembler uses rva for stabs info
  1074. so it should work with relocated DLLs }
  1075. if RelocSection and
  1076. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1077. (target_info.assem<>as_i386_pecoff) then
  1078. begin
  1079. include(current_settings.globalswitches,cs_link_strip);
  1080. { Warning stabs info does not work with reloc section !! }
  1081. if cs_debuginfo in current_settings.moduleswitches then
  1082. begin
  1083. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1084. Message(parser_w_parser_win32_debug_needs_WN);
  1085. exclude(current_settings.moduleswitches,cs_debuginfo);
  1086. end;
  1087. end;
  1088. { get correct output names }
  1089. main_file := current_scanner.inputfile;
  1090. while assigned(main_file.next) do
  1091. main_file := main_file.next;
  1092. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1093. if islibrary then
  1094. begin
  1095. consume(_LIBRARY);
  1096. stringdispose(current_module.modulename);
  1097. stringdispose(current_module.realmodulename);
  1098. current_module.modulename:=stringdup(pattern);
  1099. current_module.realmodulename:=stringdup(orgpattern);
  1100. current_module.islibrary:=true;
  1101. exportlib.preparelib(orgpattern);
  1102. if tf_library_needs_pic in target_info.flags then
  1103. include(current_settings.moduleswitches,cs_create_pic);
  1104. consume(_ID);
  1105. consume(_SEMICOLON);
  1106. end
  1107. else
  1108. { is there an program head ? }
  1109. if token=_PROGRAM then
  1110. begin
  1111. consume(_PROGRAM);
  1112. stringdispose(current_module.modulename);
  1113. stringdispose(current_module.realmodulename);
  1114. current_module.modulename:=stringdup(pattern);
  1115. current_module.realmodulename:=stringdup(orgpattern);
  1116. if (target_info.system in system_unit_program_exports) then
  1117. exportlib.preparelib(orgpattern);
  1118. consume(_ID);
  1119. if token=_LKLAMMER then
  1120. begin
  1121. consume(_LKLAMMER);
  1122. repeat
  1123. consume(_ID);
  1124. until not try_to_consume(_COMMA);
  1125. consume(_RKLAMMER);
  1126. end;
  1127. consume(_SEMICOLON);
  1128. end
  1129. else if (target_info.system in system_unit_program_exports) then
  1130. exportlib.preparelib(current_module.realmodulename^);
  1131. { global switches are read, so further changes aren't allowed }
  1132. current_module.in_global:=false;
  1133. { setup things using the switches }
  1134. setupglobalswitches;
  1135. { set implementation flag }
  1136. current_module.in_interface:=false;
  1137. current_module.interface_compiled:=true;
  1138. { insert after the unit symbol tables the static symbol table }
  1139. { of the program }
  1140. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1141. { load standard units (system,objpas,profile unit) }
  1142. loaddefaultunits;
  1143. { Load units provided on the command line }
  1144. loadautounits;
  1145. {Load the units used by the program we compile.}
  1146. if token=_USES then
  1147. loadunits;
  1148. { reset ranges/stabs in exported definitions }
  1149. reset_all_defs;
  1150. { All units are read, now give them a number }
  1151. current_module.updatemaps;
  1152. {Insert the name of the main program into the symbol table.}
  1153. if current_module.realmodulename^<>'' then
  1154. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1155. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1156. symtablestack.push(current_module.localsymtable);
  1157. { The program intialization needs an alias, so it can be called
  1158. from the bootstrap code.}
  1159. if islibrary then
  1160. begin
  1161. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1162. { Win32 startup code needs a single name }
  1163. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1164. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1165. else
  1166. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1167. end
  1168. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1169. begin
  1170. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1171. end
  1172. else
  1173. begin
  1174. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1175. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1176. end;
  1177. main_procinfo.parse_body;
  1178. { save file pos for debuginfo }
  1179. current_module.mainfilepos:=main_procinfo.entrypos;
  1180. { Generate specializations of objectdefs methods }
  1181. generate_specialization_procs;
  1182. { should we force unit initialization? }
  1183. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1184. if force_init_final then
  1185. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1186. { Add symbol to the exports section for win32 so smartlinking a
  1187. DLL will include the edata section }
  1188. if assigned(exportlib) and
  1189. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1190. ((current_module.flags and uf_has_exports)<>0) then
  1191. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1192. { finalize? }
  1193. if token=_FINALIZATION then
  1194. begin
  1195. { set module options }
  1196. current_module.flags:=current_module.flags or uf_finalize;
  1197. { Parse the finalize }
  1198. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1199. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1200. finalize_procinfo.parse_body;
  1201. end
  1202. else
  1203. if force_init_final then
  1204. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1205. { See remark in unit init/final }
  1206. main_procinfo.generate_code;
  1207. main_procinfo.resetprocdef;
  1208. release_main_proc(main_procinfo);
  1209. if assigned(init_procinfo) then
  1210. begin
  1211. init_procinfo.generate_code;
  1212. init_procinfo.resetprocdef;
  1213. release_main_proc(init_procinfo);
  1214. end;
  1215. if assigned(finalize_procinfo) then
  1216. begin
  1217. finalize_procinfo.generate_code;
  1218. finalize_procinfo.resetprocdef;
  1219. release_main_proc(finalize_procinfo);
  1220. end;
  1221. symtablestack.pop(current_module.localsymtable);
  1222. { consume the last point }
  1223. consume(_POINT);
  1224. if (Errorcount=0) then
  1225. begin
  1226. { test static symtable }
  1227. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1228. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1229. tstoredsymtable(current_module.localsymtable).check_forwards;
  1230. tstoredsymtable(current_module.localsymtable).checklabels;
  1231. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1232. current_module.allunitsused;
  1233. end;
  1234. { leave when we got an error }
  1235. if (Errorcount>0) and not status.skip_error then
  1236. begin
  1237. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1238. status.skip_error:=true;
  1239. exit;
  1240. end;
  1241. { remove all unused units, this happends when units are removed
  1242. from the uses clause in the source and the ppu was already being loaded }
  1243. hp:=tmodule(loaded_units.first);
  1244. while assigned(hp) do
  1245. begin
  1246. hp2:=hp;
  1247. hp:=tmodule(hp.next);
  1248. if hp2.is_unit and
  1249. not assigned(hp2.globalsymtable) then
  1250. loaded_units.remove(hp2);
  1251. end;
  1252. { do we need to add the variants unit? }
  1253. maybeloadvariantsunit;
  1254. linker.initsysinitunitname;
  1255. if target_info.system in systems_internal_sysinit then
  1256. begin
  1257. { add start/halt unit }
  1258. AddUnit(linker.sysinitunit);
  1259. end;
  1260. {$ifdef arm}
  1261. { Insert .pdata section for arm-wince.
  1262. It is needed for exception handling. }
  1263. if target_info.system in [system_arm_wince] then
  1264. InsertPData;
  1265. {$endif arm}
  1266. { generate debuginfo }
  1267. if (cs_debuginfo in current_settings.moduleswitches) then
  1268. debuginfo.inserttypeinfo;
  1269. InsertThreadvars;
  1270. { generate wrappers for interfaces }
  1271. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1272. { generate pic helpers to load eip if necessary }
  1273. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1274. { generate imports }
  1275. if current_module.ImportLibraryList.Count>0 then
  1276. importlib.generatelib;
  1277. if islibrary or (target_info.system in system_unit_program_exports) then
  1278. exportlib.generatelib;
  1279. { Reference all DEBUGINFO sections from the main .fpc section }
  1280. if (cs_debuginfo in current_settings.moduleswitches) then
  1281. debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1282. { Resource strings }
  1283. GenerateResourceStrings;
  1284. { insert Tables and StackLength }
  1285. insertinitfinaltable;
  1286. InsertThreadvarTablesTable;
  1287. InsertResourceTablesTable;
  1288. insertmemorysizes;
  1289. { Insert symbol to resource info }
  1290. InsertResourceInfo;
  1291. { create dwarf debuginfo }
  1292. create_dwarf;
  1293. { insert own objectfile }
  1294. insertobjectfile;
  1295. { assemble and link }
  1296. create_objectfile;
  1297. { release all local symtables that are not needed anymore }
  1298. free_localsymtables(current_module.localsymtable);
  1299. { leave when we got an error }
  1300. if (Errorcount>0) and not status.skip_error then
  1301. begin
  1302. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1303. status.skip_error:=true;
  1304. exit;
  1305. end;
  1306. if (not current_module.is_unit) then
  1307. begin
  1308. { create the executable when we are at level 1 }
  1309. if (compile_level=1) then
  1310. begin
  1311. { write .def file }
  1312. if (cs_link_deffile in current_settings.globalswitches) then
  1313. deffile.writefile;
  1314. { insert all .o files from all loaded units and
  1315. unload the units, we don't need them anymore.
  1316. Keep the current_module because that is still needed }
  1317. hp:=tmodule(loaded_units.first);
  1318. while assigned(hp) do
  1319. begin
  1320. linker.AddModuleFiles(hp);
  1321. hp2:=tmodule(hp.next);
  1322. if hp<>current_module then
  1323. begin
  1324. loaded_units.remove(hp);
  1325. hp.free;
  1326. end;
  1327. hp:=hp2;
  1328. end;
  1329. { finally we can create a executable }
  1330. if DLLSource then
  1331. linker.MakeSharedLibrary
  1332. else
  1333. linker.MakeExecutable;
  1334. end;
  1335. { Give Fatal with error count for linker errors }
  1336. if (Errorcount>0) and not status.skip_error then
  1337. begin
  1338. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1339. status.skip_error:=true;
  1340. end;
  1341. end;
  1342. end;
  1343. end.