pmodules.pas 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  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. if not(m_tp7 in aktmodeswitches) then
  481. begin
  482. if try_to_consume(_OP_IN) then
  483. fn:=FixFileName(get_stringconst)
  484. else
  485. fn:='';
  486. end;
  487. { Give a warning if objpas is loaded }
  488. if s='OBJPAS' then
  489. Message(parser_w_no_objpas_use_mode);
  490. { Using the unit itself is not possible }
  491. if (s<>current_module.modulename^) then
  492. begin
  493. { check if the unit is already used }
  494. hp2:=nil;
  495. pu:=tused_unit(current_module.used_units.first);
  496. while assigned(pu) do
  497. begin
  498. if (pu.u.modulename^=s) then
  499. begin
  500. hp2:=pu.u;
  501. break;
  502. end;
  503. pu:=tused_unit(pu.next);
  504. end;
  505. if not assigned(hp2) then
  506. hp2:=registerunit(current_module,sorg,fn)
  507. else
  508. Message1(sym_e_duplicate_id,s);
  509. { Create unitsym, we need to use the name as specified, we
  510. can not use the modulename because that can be different
  511. when -Un is used }
  512. unitsym:=tunitsym.create(sorg,nil);
  513. current_module.localsymtable.insert(unitsym);
  514. { the current module uses the unit hp2 }
  515. current_module.addusedunit(hp2,true,unitsym);
  516. end
  517. else
  518. Message1(sym_e_duplicate_id,s);
  519. if token=_COMMA then
  520. begin
  521. pattern:='';
  522. consume(_COMMA);
  523. end
  524. else
  525. break;
  526. until false;
  527. { Load the units }
  528. pu:=tused_unit(current_module.used_units.first);
  529. while assigned(pu) do
  530. begin
  531. { Only load the units that are in the current
  532. (interface/implementation) uses clause }
  533. if pu.in_uses and
  534. (pu.in_interface=current_module.in_interface) then
  535. begin
  536. tppumodule(pu.u).loadppu;
  537. { is our module compiled? then we can stop }
  538. if current_module.state=ms_compiled then
  539. exit;
  540. { add this unit to the dependencies }
  541. pu.u.adddependency(current_module);
  542. { save crc values }
  543. pu.checksum:=pu.u.crc;
  544. pu.interface_checksum:=pu.u.interface_crc;
  545. { connect unitsym to the module }
  546. pu.unitsym.module:=pu.u;
  547. { add to symtable stack }
  548. symtablestack.push(pu.u.globalsymtable);
  549. if (m_mac in aktmodeswitches) and
  550. assigned(pu.u.globalmacrosymtable) then
  551. macrosymtablestack.push(pu.u.globalmacrosymtable);
  552. end;
  553. pu:=tused_unit(pu.next);
  554. end;
  555. consume(_SEMICOLON);
  556. end;
  557. procedure reset_all_defs;
  558. procedure reset_used_unit_defs(hp:tmodule);
  559. var
  560. pu : tused_unit;
  561. begin
  562. pu:=tused_unit(hp.used_units.first);
  563. while assigned(pu) do
  564. begin
  565. if not pu.u.is_reset then
  566. begin
  567. { prevent infinte loop for circular dependencies }
  568. pu.u.is_reset:=true;
  569. if assigned(pu.u.globalsymtable) then
  570. begin
  571. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  572. reset_used_unit_defs(pu.u);
  573. end;
  574. end;
  575. pu:=tused_unit(pu.next);
  576. end;
  577. end;
  578. var
  579. hp2 : tmodule;
  580. begin
  581. hp2:=tmodule(loaded_units.first);
  582. while assigned(hp2) do
  583. begin
  584. hp2.is_reset:=false;
  585. hp2:=tmodule(hp2.next);
  586. end;
  587. reset_used_unit_defs(current_module);
  588. end;
  589. procedure free_localsymtables(st:tsymtable);
  590. var
  591. def : tstoreddef;
  592. pd : tprocdef;
  593. begin
  594. def:=tstoreddef(st.defindex.first);
  595. while assigned(def) do
  596. begin
  597. if def.deftype=procdef then
  598. begin
  599. pd:=tprocdef(def);
  600. if assigned(pd.localst) and
  601. (pd.localst.symtabletype<>staticsymtable) and
  602. not((po_inline in pd.procoptions) or
  603. ((current_module.flags and uf_local_browser)<>0)) then
  604. begin
  605. free_localsymtables(pd.localst);
  606. pd.localst.free;
  607. pd.localst:=nil;
  608. end;
  609. end;
  610. def:=tstoreddef(def.indexnext);
  611. end;
  612. end;
  613. procedure parse_implementation_uses;
  614. begin
  615. if token=_USES then
  616. loadunits;
  617. end;
  618. procedure setupglobalswitches;
  619. begin
  620. { can't have local browser when no global browser }
  621. if (cs_local_browser in aktmoduleswitches) and
  622. not(cs_browser in aktmoduleswitches) then
  623. exclude(aktmoduleswitches,cs_local_browser);
  624. if (cs_create_pic in aktmoduleswitches) then
  625. def_system_macro('FPC_PIC');
  626. end;
  627. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  628. var
  629. ps : tprocsym;
  630. pd : tprocdef;
  631. begin
  632. { there should be no current_procinfo available }
  633. if assigned(current_procinfo) then
  634. internalerror(200304275);
  635. {Generate a procsym for main}
  636. ps:=tprocsym.create('$'+name);
  637. { main are allways used }
  638. inc(ps.refs);
  639. st.insert(ps);
  640. pd:=tprocdef.create(main_program_level);
  641. include(pd.procoptions,po_global);
  642. pd.procsym:=ps;
  643. ps.addprocdef(pd);
  644. { set procdef options }
  645. pd.proctypeoption:=potype;
  646. pd.proccalloption:=pocall_default;
  647. pd.forwarddef:=false;
  648. pd.setmangledname(target_info.cprefix+name);
  649. pd.aliasnames.insert(pd.mangledname);
  650. handle_calling_convention(pd);
  651. { We don't need is a local symtable. Change it into the static
  652. symtable }
  653. pd.localst.free;
  654. pd.localst:=st;
  655. { set procinfo and current_procinfo.procdef }
  656. current_procinfo:=cprocinfo.create(nil);
  657. current_module.procinfo:=current_procinfo;
  658. current_procinfo.procdef:=pd;
  659. { return procdef }
  660. create_main_proc:=pd;
  661. { main proc does always a call e.g. to init system unit }
  662. include(current_procinfo.flags,pi_do_call);
  663. end;
  664. procedure release_main_proc(pd:tprocdef);
  665. begin
  666. { this is a main proc, so there should be no parent }
  667. if not(assigned(current_procinfo)) or
  668. assigned(current_procinfo.parent) or
  669. not(current_procinfo.procdef=pd) then
  670. internalerror(200304276);
  671. { remove procinfo }
  672. current_module.procinfo:=nil;
  673. current_procinfo.free;
  674. current_procinfo:=nil;
  675. { remove localst as it was replaced by staticsymtable }
  676. pd.localst:=nil;
  677. end;
  678. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  679. var
  680. pd : tprocdef;
  681. begin
  682. { update module flags }
  683. current_module.flags:=current_module.flags or flag;
  684. { create procdef }
  685. case flag of
  686. uf_init :
  687. begin
  688. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  689. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  690. end;
  691. uf_finalize :
  692. begin
  693. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  694. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  695. end;
  696. else
  697. internalerror(200304253);
  698. end;
  699. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  700. tcgprocinfo(current_procinfo).generate_code;
  701. release_main_proc(pd);
  702. end;
  703. procedure copy_macro(p:TNamedIndexItem; arg:pointer);
  704. begin
  705. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  706. end;
  707. procedure proc_unit;
  708. function is_assembler_generated:boolean;
  709. var
  710. hal : tasmlist;
  711. begin
  712. result:=false;
  713. if Errorcount=0 then
  714. begin
  715. for hal:=low(Tasmlist) to high(Tasmlist) do
  716. if not asmlist[hal].empty then
  717. begin
  718. result:=true;
  719. exit;
  720. end;
  721. end;
  722. end;
  723. var
  724. main_file: tinputfile;
  725. {$ifdef EXTDEBUG}
  726. store_crc,
  727. {$endif EXTDEBUG}
  728. store_interface_crc : cardinal;
  729. s1,s2 : ^string; {Saves stack space}
  730. force_init_final : boolean;
  731. pd : tprocdef;
  732. unitname8 : string[8];
  733. has_impl,ag: boolean;
  734. globalvarsym : tglobalvarsym;
  735. begin
  736. if m_mac in aktmodeswitches then
  737. current_module.mode_switch_allowed:= false;
  738. consume(_UNIT);
  739. if compile_level=1 then
  740. Status.IsExe:=false;
  741. if token=_ID then
  742. begin
  743. { create filenames and unit name }
  744. main_file := current_scanner.inputfile;
  745. while assigned(main_file.next) do
  746. main_file := main_file.next;
  747. new(s1);
  748. s1^:=current_module.modulename^;
  749. current_module.SetFileName(main_file.path^+main_file.name^,true);
  750. current_module.SetModuleName(orgpattern);
  751. { check for system unit }
  752. new(s2);
  753. s2^:=upper(SplitName(main_file.name^));
  754. unitname8:=copy(current_module.modulename^,1,8);
  755. if (cs_check_unit_name in aktglobalswitches) and
  756. (
  757. not(
  758. (current_module.modulename^=s2^) or
  759. (
  760. (length(current_module.modulename^)>8) and
  761. (unitname8=s2^)
  762. )
  763. )
  764. or
  765. (
  766. (length(s1^)>8) and
  767. (s1^<>current_module.modulename^)
  768. )
  769. ) then
  770. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  771. if (current_module.modulename^='SYSTEM') then
  772. include(aktmoduleswitches,cs_compilesystem);
  773. dispose(s2);
  774. dispose(s1);
  775. end;
  776. if (target_info.system in system_unit_program_exports) then
  777. exportlib.preparelib(current_module.realmodulename^);
  778. consume(_ID);
  779. consume(_SEMICOLON);
  780. consume(_INTERFACE);
  781. { global switches are read, so further changes aren't allowed }
  782. current_module.in_global:=false;
  783. { handle the global switches }
  784. setupglobalswitches;
  785. message1(unit_u_loading_interface_units,current_module.modulename^);
  786. { update status }
  787. status.currentmodule:=current_module.realmodulename^;
  788. { maybe turn off m_objpas if we are compiling objpas }
  789. if (current_module.modulename^='OBJPAS') then
  790. exclude(aktmodeswitches,m_objpas);
  791. { maybe turn off m_mac if we are compiling macpas }
  792. if (current_module.modulename^='MACPAS') then
  793. exclude(aktmodeswitches,m_mac);
  794. parse_only:=true;
  795. { generate now the global symboltable,
  796. define first as local to overcome dependency conflicts }
  797. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  798. { insert unitsym of this unit to prevent other units having
  799. the same name }
  800. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  801. { load default units, like the system unit }
  802. loaddefaultunits;
  803. { reset }
  804. make_ref:=true;
  805. { insert qualifier for the system unit (allows system.writeln) }
  806. if not(cs_compilesystem in aktmoduleswitches) and
  807. (token=_USES) then
  808. begin
  809. loadunits;
  810. { has it been compiled at a higher level ?}
  811. if current_module.state=ms_compiled then
  812. exit;
  813. end;
  814. { move the global symtable from the temporary local to global }
  815. current_module.globalsymtable:=current_module.localsymtable;
  816. current_module.localsymtable:=nil;
  817. reset_all_defs;
  818. { number all units, so we know if a unit is used by this unit or
  819. needs to be added implicitly }
  820. current_module.updatemaps;
  821. { ... parse the declarations }
  822. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  823. symtablestack.push(current_module.globalsymtable);
  824. read_interface_declarations;
  825. symtablestack.pop(current_module.globalsymtable);
  826. { Export macros defined in the interface for macpas. The macros
  827. are put in the globalmacrosymtable that will only be used by other
  828. units. The current unit continues to use the localmacrosymtable }
  829. if (m_mac in aktmodeswitches) then
  830. begin
  831. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  832. current_module.localmacrosymtable.foreach_static(@copy_macro,nil);
  833. end;
  834. { leave when we got an error }
  835. if (Errorcount>0) and not status.skip_error then
  836. begin
  837. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  838. status.skip_error:=true;
  839. exit;
  840. end;
  841. { Our interface is compiled, generate CRC and switch to implementation }
  842. if not(cs_compilesystem in aktmoduleswitches) and
  843. (Errorcount=0) then
  844. tppumodule(current_module).getppucrc;
  845. current_module.in_interface:=false;
  846. current_module.interface_compiled:=true;
  847. { First reload all units depending on our interface, we need to do this
  848. in the implementation part to prevent errorneous circular references }
  849. reload_flagged_units;
  850. { Parse the implementation section }
  851. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  852. has_impl:= false
  853. else
  854. has_impl:= true;
  855. parse_only:=false;
  856. { generates static symbol table }
  857. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  858. {$ifdef i386}
  859. if cs_create_pic in aktmoduleswitches then
  860. begin
  861. { insert symbol for got access in assembler code}
  862. globalvarsym:=tglobalvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external,vo_is_C_var]);
  863. globalvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  864. current_module.localsymtable.insert(globalvarsym);
  865. { avoid unnecessary warnings }
  866. globalvarsym.varstate:=vs_read;
  867. globalvarsym.refs:=1;
  868. end;
  869. {$endif i386}
  870. if has_impl then
  871. begin
  872. consume(_IMPLEMENTATION);
  873. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  874. { Read the implementation units }
  875. parse_implementation_uses;
  876. end;
  877. if current_module.state=ms_compiled then
  878. exit;
  879. { reset ranges/stabs in exported definitions }
  880. reset_all_defs;
  881. { All units are read, now give them a number }
  882. current_module.updatemaps;
  883. symtablestack.push(current_module.globalsymtable);
  884. symtablestack.push(current_module.localsymtable);
  885. if has_impl then
  886. begin
  887. Message1(parser_u_parsing_implementation,current_module.modulename^);
  888. if current_module.in_interface then
  889. internalerror(200212285);
  890. { Compile the unit }
  891. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  892. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  893. tcgprocinfo(current_procinfo).parse_body;
  894. tcgprocinfo(current_procinfo).generate_code;
  895. tcgprocinfo(current_procinfo).resetprocdef;
  896. { save file pos for debuginfo }
  897. current_module.mainfilepos:=current_procinfo.entrypos;
  898. release_main_proc(pd);
  899. end;
  900. { Generate specializations of objectdefs methods }
  901. generate_specialization_procs;
  902. { if the unit contains ansi/widestrings, initialization and
  903. finalization code must be forced }
  904. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  905. tstaticsymtable(current_module.localsymtable).needs_init_final;
  906. { should we force unit initialization? }
  907. { this is a hack, but how can it be done better ? }
  908. if force_init_final and ((current_module.flags and uf_init)=0) then
  909. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  910. { finalize? }
  911. if has_impl and (token=_FINALIZATION) then
  912. begin
  913. { set module options }
  914. current_module.flags:=current_module.flags or uf_finalize;
  915. { Compile the finalize }
  916. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  917. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  918. tcgprocinfo(current_procinfo).parse_body;
  919. tcgprocinfo(current_procinfo).generate_code;
  920. tcgprocinfo(current_procinfo).resetprocdef;
  921. release_main_proc(pd);
  922. end
  923. else if force_init_final then
  924. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  925. symtablestack.pop(current_module.localsymtable);
  926. symtablestack.pop(current_module.globalsymtable);
  927. { the last char should always be a point }
  928. consume(_POINT);
  929. { Generate resoucestrings }
  930. If resourcestrings.ResStrCount>0 then
  931. begin
  932. resourcestrings.CreateResourceStringList;
  933. current_module.flags:=current_module.flags or uf_has_resources;
  934. { only write if no errors found }
  935. if (Errorcount=0) then
  936. resourcestrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  937. end;
  938. if (Errorcount=0) then
  939. begin
  940. { tests, if all (interface) forwards are resolved }
  941. tstoredsymtable(current_module.globalsymtable).check_forwards;
  942. { check if all private fields are used }
  943. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  944. { remove cross unit overloads }
  945. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  946. { test static symtable }
  947. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  948. tstoredsymtable(current_module.localsymtable).allprivatesused;
  949. tstoredsymtable(current_module.localsymtable).check_forwards;
  950. tstoredsymtable(current_module.localsymtable).checklabels;
  951. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  952. { used units }
  953. current_module.allunitsused;
  954. end;
  955. { leave when we got an error }
  956. if (Errorcount>0) and not status.skip_error then
  957. begin
  958. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  959. status.skip_error:=true;
  960. exit;
  961. end;
  962. { do we need to add the variants unit? }
  963. maybeloadvariantsunit;
  964. { generate debuginfo }
  965. if (cs_debuginfo in aktmoduleswitches) then
  966. debuginfo.inserttypeinfo;
  967. { generate wrappers for interfaces }
  968. gen_intf_wrappers(asmlist[al_procedures],current_module.globalsymtable);
  969. gen_intf_wrappers(asmlist[al_procedures],current_module.localsymtable);
  970. { generate pic helpers to load eip if necessary }
  971. gen_pic_helpers(asmlist[al_procedures]);
  972. { generate a list of threadvars }
  973. if not(tf_section_threadvars in target_info.flags) then
  974. InsertThreadvars;
  975. { generate imports }
  976. if current_module.uses_imports then
  977. importlib.generatelib;
  978. { insert own objectfile, or say that it's in a library
  979. (no check for an .o when loading) }
  980. ag:=is_assembler_generated;
  981. if ag then
  982. insertobjectfile
  983. else
  984. begin
  985. current_module.flags:=current_module.flags or uf_no_link;
  986. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  987. end;
  988. if ag then
  989. begin
  990. { create dwarf debuginfo }
  991. create_dwarf;
  992. { assemble }
  993. create_objectfile;
  994. end;
  995. { Write out the ppufile after the object file has been created }
  996. store_interface_crc:=current_module.interface_crc;
  997. {$ifdef EXTDEBUG}
  998. store_crc:=current_module.crc;
  999. {$endif EXTDEBUG}
  1000. if (Errorcount=0) then
  1001. tppumodule(current_module).writeppu;
  1002. if not(cs_compilesystem in aktmoduleswitches) then
  1003. if store_interface_crc<>current_module.interface_crc then
  1004. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1005. {$ifdef EXTDEBUG}
  1006. if not(cs_compilesystem in aktmoduleswitches) then
  1007. if (store_crc<>current_module.crc) and simplify_ppu then
  1008. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1009. {$endif EXTDEBUG}
  1010. { release all overload references and local symtables that
  1011. are not needed anymore }
  1012. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1013. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1014. free_localsymtables(current_module.globalsymtable);
  1015. free_localsymtables(current_module.localsymtable);
  1016. { leave when we got an error }
  1017. if (Errorcount>0) and not status.skip_error then
  1018. begin
  1019. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1020. status.skip_error:=true;
  1021. exit;
  1022. end;
  1023. Message1(unit_u_finished_compiling,current_module.modulename^);
  1024. end;
  1025. procedure proc_program(islibrary : boolean);
  1026. var
  1027. main_file : tinputfile;
  1028. hp,hp2 : tmodule;
  1029. pd : tprocdef;
  1030. begin
  1031. DLLsource:=islibrary;
  1032. Status.IsLibrary:=IsLibrary;
  1033. Status.IsExe:=true;
  1034. parse_only:=false;
  1035. { DLL defaults to create reloc info }
  1036. if islibrary then
  1037. begin
  1038. if not RelocSectionSetExplicitly then
  1039. RelocSection:=true;
  1040. end;
  1041. { relocation works only without stabs under win32 !! PM }
  1042. { internal assembler uses rva for stabs info
  1043. so it should work with relocated DLLs }
  1044. if RelocSection and
  1045. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1046. (target_info.assem<>as_i386_pecoff) then
  1047. begin
  1048. include(aktglobalswitches,cs_link_strip);
  1049. { Warning stabs info does not work with reloc section !! }
  1050. if cs_debuginfo in aktmoduleswitches then
  1051. begin
  1052. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1053. Message(parser_w_parser_win32_debug_needs_WN);
  1054. exclude(aktmoduleswitches,cs_debuginfo);
  1055. end;
  1056. end;
  1057. { get correct output names }
  1058. main_file := current_scanner.inputfile;
  1059. while assigned(main_file.next) do
  1060. main_file := main_file.next;
  1061. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1062. if islibrary then
  1063. begin
  1064. consume(_LIBRARY);
  1065. stringdispose(current_module.modulename);
  1066. stringdispose(current_module.realmodulename);
  1067. current_module.modulename:=stringdup(pattern);
  1068. current_module.realmodulename:=stringdup(orgpattern);
  1069. current_module.islibrary:=true;
  1070. exportlib.preparelib(orgpattern);
  1071. if tf_library_needs_pic in target_info.flags then
  1072. include(aktmoduleswitches,cs_create_pic);
  1073. consume(_ID);
  1074. consume(_SEMICOLON);
  1075. end
  1076. else
  1077. { is there an program head ? }
  1078. if token=_PROGRAM then
  1079. begin
  1080. consume(_PROGRAM);
  1081. stringdispose(current_module.modulename);
  1082. stringdispose(current_module.realmodulename);
  1083. current_module.modulename:=stringdup(pattern);
  1084. current_module.realmodulename:=stringdup(orgpattern);
  1085. if (target_info.system in system_unit_program_exports) then
  1086. exportlib.preparelib(orgpattern);
  1087. consume(_ID);
  1088. if token=_LKLAMMER then
  1089. begin
  1090. consume(_LKLAMMER);
  1091. repeat
  1092. consume(_ID);
  1093. until not try_to_consume(_COMMA);
  1094. consume(_RKLAMMER);
  1095. end;
  1096. consume(_SEMICOLON);
  1097. end
  1098. else if (target_info.system in system_unit_program_exports) then
  1099. exportlib.preparelib(current_module.realmodulename^);
  1100. { global switches are read, so further changes aren't allowed }
  1101. current_module.in_global:=false;
  1102. { setup things using the switches }
  1103. setupglobalswitches;
  1104. { set implementation flag }
  1105. current_module.in_interface:=false;
  1106. current_module.interface_compiled:=true;
  1107. { insert after the unit symbol tables the static symbol table }
  1108. { of the program }
  1109. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1110. { load standard units (system,objpas,profile unit) }
  1111. loaddefaultunits;
  1112. { Load units provided on the command line }
  1113. loadautounits;
  1114. {Load the units used by the program we compile.}
  1115. if token=_USES then
  1116. loadunits;
  1117. { reset ranges/stabs in exported definitions }
  1118. reset_all_defs;
  1119. { All units are read, now give them a number }
  1120. current_module.updatemaps;
  1121. {Insert the name of the main program into the symbol table.}
  1122. if current_module.realmodulename^<>'' then
  1123. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1124. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1125. symtablestack.push(current_module.localsymtable);
  1126. { The program intialization needs an alias, so it can be called
  1127. from the bootstrap code.}
  1128. if islibrary then
  1129. begin
  1130. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1131. { Win32 startup code needs a single name }
  1132. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1133. pd.aliasnames.insert('PASCALMAIN');
  1134. end
  1135. else if (target_info.system = system_i386_netware) or
  1136. (target_info.system = system_i386_netwlibc) then
  1137. begin
  1138. pd:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1139. end
  1140. else
  1141. begin
  1142. pd:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1143. pd.aliasnames.insert('PASCALMAIN');
  1144. end;
  1145. tcgprocinfo(current_procinfo).parse_body;
  1146. tcgprocinfo(current_procinfo).generate_code;
  1147. tcgprocinfo(current_procinfo).resetprocdef;
  1148. { save file pos for debuginfo }
  1149. current_module.mainfilepos:=current_procinfo.entrypos;
  1150. release_main_proc(pd);
  1151. { Generate specializations of objectdefs methods }
  1152. generate_specialization_procs;
  1153. { should we force unit initialization? }
  1154. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1155. begin
  1156. { initialize section }
  1157. gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1158. { finalize section }
  1159. gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1160. end;
  1161. { Add symbol to the exports section for win32 so smartlinking a
  1162. DLL will include the edata section }
  1163. if assigned(exportlib) and
  1164. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1165. ((current_module.flags and uf_has_exports)<>0) then
  1166. asmlist[al_procedures].concat(tai_const.create_sym(exportlib.edatalabel));
  1167. If resourcestrings.ResStrCount>0 then
  1168. begin
  1169. resourcestrings.CreateResourceStringList;
  1170. { only write if no errors found }
  1171. if (Errorcount=0) then
  1172. resourcestrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1173. end;
  1174. { finalize? }
  1175. if token=_FINALIZATION then
  1176. begin
  1177. { set module options }
  1178. current_module.flags:=current_module.flags or uf_finalize;
  1179. { Compile the finalize }
  1180. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1181. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1182. tcgprocinfo(current_procinfo).parse_body;
  1183. tcgprocinfo(current_procinfo).generate_code;
  1184. tcgprocinfo(current_procinfo).resetprocdef;
  1185. release_main_proc(pd);
  1186. end;
  1187. symtablestack.pop(current_module.localsymtable);
  1188. { consume the last point }
  1189. consume(_POINT);
  1190. if (Errorcount=0) then
  1191. begin
  1192. { test static symtable }
  1193. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1194. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1195. tstoredsymtable(current_module.localsymtable).check_forwards;
  1196. tstoredsymtable(current_module.localsymtable).checklabels;
  1197. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1198. current_module.allunitsused;
  1199. end;
  1200. { leave when we got an error }
  1201. if (Errorcount>0) and not status.skip_error then
  1202. begin
  1203. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1204. status.skip_error:=true;
  1205. exit;
  1206. end;
  1207. { remove all unused units, this happends when units are removed
  1208. from the uses clause in the source and the ppu was already being loaded }
  1209. hp:=tmodule(loaded_units.first);
  1210. while assigned(hp) do
  1211. begin
  1212. hp2:=hp;
  1213. hp:=tmodule(hp.next);
  1214. if hp2.is_unit and
  1215. not assigned(hp2.globalsymtable) then
  1216. loaded_units.remove(hp2);
  1217. end;
  1218. { do we need to add the variants unit? }
  1219. maybeloadvariantsunit;
  1220. { generate debuginfo }
  1221. if (cs_debuginfo in aktmoduleswitches) then
  1222. debuginfo.inserttypeinfo;
  1223. { generate wrappers for interfaces }
  1224. gen_intf_wrappers(asmlist[al_procedures],current_module.localsymtable);
  1225. { generate pic helpers to load eip if necessary }
  1226. gen_pic_helpers(asmlist[al_procedures]);
  1227. { generate a list of threadvars }
  1228. if not(tf_section_threadvars in target_info.flags) then
  1229. InsertThreadvars;
  1230. { generate imports }
  1231. if current_module.uses_imports then
  1232. importlib.generatelib;
  1233. if islibrary or (target_info.system in system_unit_program_exports) then
  1234. exportlib.generatelib;
  1235. { insert Tables and StackLength }
  1236. if not(tf_section_threadvars in target_info.flags) then
  1237. insertThreadVarTablesTable;
  1238. insertResourceTablesTable;
  1239. insertinitfinaltable;
  1240. insertmemorysizes;
  1241. { Insert symbol to resource info }
  1242. InsertResourceInfo;
  1243. { create dwarf debuginfo }
  1244. create_dwarf;
  1245. { insert own objectfile }
  1246. insertobjectfile;
  1247. { assemble and link }
  1248. create_objectfile;
  1249. { release all local symtables that are not needed anymore }
  1250. free_localsymtables(current_module.localsymtable);
  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. { create the executable when we are at level 1 }
  1259. if (compile_level=1) then
  1260. begin
  1261. { insert all .o files from all loaded units }
  1262. hp:=tmodule(loaded_units.first);
  1263. while assigned(hp) do
  1264. begin
  1265. linker.AddModuleFiles(hp);
  1266. hp:=tmodule(hp.next);
  1267. end;
  1268. { write .def file }
  1269. if (cs_link_deffile in aktglobalswitches) then
  1270. deffile.writefile;
  1271. { finally we can create a executable }
  1272. if (not current_module.is_unit) then
  1273. begin
  1274. if DLLSource then
  1275. linker.MakeSharedLibrary
  1276. else
  1277. linker.MakeExecutable;
  1278. end;
  1279. end;
  1280. end;
  1281. end.