pmodules.pas 60 KB

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