pmodules.pas 59 KB

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