pmodules.pas 58 KB

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