pmodules.pas 62 KB

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