pmodules.pas 57 KB

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