pmodules.pas 61 KB

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