pmodules.pas 55 KB

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