pmodules.pas 53 KB

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