pmodules.pas 60 KB

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