pmodules.pas 55 KB

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