pmodules.pas 60 KB

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