pmodules.pas 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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,cpuinfo,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;
  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=varsym) and
  208. (vo_is_thread_var in tvarsym(p).varoptions) then
  209. begin
  210. { address of threadvar }
  211. ltvTable.concat(tai_const.Createname(tvarsym(p).mangledname,AT_DATA,0));
  212. { size of threadvar }
  213. ltvTable.concat(tai_const.create_32bit(tvarsym(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.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 InsertResourceTablesTable;
  239. var
  240. hp : tused_unit;
  241. ResourceStringTables : taasmoutput;
  242. count : longint;
  243. begin
  244. ResourceStringTables:=TAAsmOutput.Create;
  245. count:=0;
  246. hp:=tused_unit(usedunits.first);
  247. while assigned(hp) do
  248. begin
  249. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  250. begin
  251. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  252. inc(count);
  253. end;
  254. hp:=tused_unit(hp.next);
  255. end;
  256. { Add program resources, if any }
  257. If ResourceStringList<>Nil then
  258. begin
  259. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  260. Inc(Count);
  261. end;
  262. { TableCount }
  263. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  264. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  265. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  266. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  267. { insert in data segment }
  268. maybe_new_object_file(dataSegment);
  269. dataSegment.concatlist(ResourceStringTables);
  270. ResourceStringTables.free;
  271. end;
  272. procedure InsertInitFinalTable;
  273. var
  274. hp : tused_unit;
  275. unitinits : taasmoutput;
  276. count : longint;
  277. begin
  278. unitinits:=TAAsmOutput.Create;
  279. count:=0;
  280. hp:=tused_unit(usedunits.first);
  281. while assigned(hp) do
  282. begin
  283. { call the unit init code and make it external }
  284. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  285. begin
  286. if (hp.u.flags and uf_init)<>0 then
  287. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  288. else
  289. unitinits.concat(Tai_const.Create_sym(nil));
  290. if (hp.u.flags and uf_finalize)<>0 then
  291. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  292. else
  293. unitinits.concat(Tai_const.Create_sym(nil));
  294. inc(count);
  295. end;
  296. hp:=tused_unit(hp.next);
  297. end;
  298. { Insert initialization/finalization of the program }
  299. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  300. begin
  301. if (current_module.flags and uf_init)<>0 then
  302. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  303. else
  304. unitinits.concat(Tai_const.Create_sym(nil));
  305. if (current_module.flags and uf_finalize)<>0 then
  306. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  307. else
  308. unitinits.concat(Tai_const.Create_sym(nil));
  309. inc(count);
  310. end;
  311. { TableCount,InitCount }
  312. unitinits.insert(Tai_const.Create_32bit(0));
  313. unitinits.insert(Tai_const.Create_32bit(count));
  314. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  315. unitinits.insert(Tai_align.Create(const_align(4)));
  316. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  317. { insert in data segment }
  318. maybe_new_object_file(dataSegment);
  319. dataSegment.concatlist(unitinits);
  320. unitinits.free;
  321. end;
  322. procedure insertmemorysizes;
  323. begin
  324. { stacksize can be specified and is now simulated }
  325. dataSegment.concat(Tai_align.Create(const_align(4)));
  326. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  327. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  328. dataSegment.concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,4));
  329. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  330. end;
  331. procedure loaddefaultunits;
  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. { insert unitsym }
  345. unitsym:=tunitsym.create(s,hp.globalsymtable);
  346. inc(unitsym.refs);
  347. refsymtable.insert(unitsym);
  348. { add to used units }
  349. current_module.addusedunit(hp,false,unitsym);
  350. end;
  351. begin
  352. { are we compiling the system unit? }
  353. if (cs_compilesystem in aktmoduleswitches) then
  354. begin
  355. { create system defines }
  356. createconstdefs;
  357. { we don't need to reset anything, it's already done in parser.pas }
  358. exit;
  359. end;
  360. { insert the system unit, it is allways the first }
  361. Symtablestack:=nil;
  362. AddUnit('System');
  363. SystemUnit:=TGlobalSymtable(Symtablestack);
  364. { read default constant definitions }
  365. make_ref:=false;
  366. readconstdefs;
  367. make_ref:=true;
  368. { Set the owner of errorsym and errortype to symtable to
  369. prevent crashes when accessing .owner }
  370. generrorsym.owner:=systemunit;
  371. generrortype.def.owner:=systemunit;
  372. {$ifdef cpufpemu}
  373. { Floating point emulation unit? }
  374. if (cs_fp_emulation in aktmoduleswitches) then
  375. AddUnit('SoftFpu');
  376. {$endif cpufpemu}
  377. { Thread support unit? }
  378. if (cs_threading in aktmoduleswitches) then
  379. AddUnit('SysThrds');
  380. { Objpas unit? }
  381. if m_objpas in aktmodeswitches then
  382. AddUnit('ObjPas');
  383. { Macpas unit? }
  384. if m_mac in aktmodeswitches then
  385. AddUnit('MacPas');
  386. { Profile unit? Needed for go32v2 only }
  387. if (cs_profile in aktmoduleswitches) and
  388. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  389. AddUnit('Profile');
  390. { Units only required for main module }
  391. if not(current_module.is_unit) then
  392. begin
  393. { Heaptrc unit }
  394. if (cs_gdb_heaptrc in aktglobalswitches) then
  395. AddUnit('HeapTrc');
  396. { Lineinfo unit }
  397. if (cs_gdb_lineinfo in aktglobalswitches) then
  398. AddUnit('LineInfo');
  399. { Lineinfo unit }
  400. if (cs_gdb_valgrind in aktglobalswitches) then
  401. AddUnit('CMem');
  402. end;
  403. { save default symtablestack }
  404. defaultsymtablestack:=symtablestack;
  405. end;
  406. procedure loadunits;
  407. var
  408. s,sorg : stringid;
  409. fn : string;
  410. pu : tused_unit;
  411. hp2 : tmodule;
  412. hp3 : tsymtable;
  413. unitsym : tunitsym;
  414. begin
  415. consume(_USES);
  416. {$ifdef DEBUG}
  417. test_symtablestack;
  418. {$endif DEBUG}
  419. repeat
  420. s:=pattern;
  421. sorg:=orgpattern;
  422. consume(_ID);
  423. { support "<unit> in '<file>'" construct, but not for tp7 }
  424. if not(m_tp7 in aktmodeswitches) then
  425. begin
  426. if try_to_consume(_OP_IN) then
  427. fn:=FixFileName(get_stringconst)
  428. else
  429. fn:='';
  430. end;
  431. { Give a warning if objpas is loaded }
  432. if s='OBJPAS' then
  433. Message(parser_w_no_objpas_use_mode);
  434. { Using the unit itself is not possible }
  435. if (s<>current_module.modulename^) then
  436. begin
  437. { check if the unit is already used }
  438. hp2:=nil;
  439. pu:=tused_unit(current_module.used_units.first);
  440. while assigned(pu) do
  441. begin
  442. if (pu.u.modulename^=s) then
  443. begin
  444. hp2:=pu.u;
  445. break;
  446. end;
  447. pu:=tused_unit(pu.next);
  448. end;
  449. if not assigned(hp2) then
  450. hp2:=registerunit(current_module,sorg,fn)
  451. else
  452. Message1(sym_e_duplicate_id,s);
  453. { Create unitsym, we need to use the name as specified, we
  454. can not use the modulename because that can be different
  455. when -Un is used }
  456. unitsym:=tunitsym.create(sorg,nil);
  457. refsymtable.insert(unitsym);
  458. { the current module uses the unit hp2 }
  459. current_module.addusedunit(hp2,true,unitsym);
  460. end
  461. else
  462. Message1(sym_e_duplicate_id,s);
  463. if token=_COMMA then
  464. begin
  465. pattern:='';
  466. consume(_COMMA);
  467. end
  468. else
  469. break;
  470. until false;
  471. consume(_SEMICOLON);
  472. { Load the units }
  473. pu:=tused_unit(current_module.used_units.first);
  474. while assigned(pu) do
  475. begin
  476. { Only load the units that are in the current
  477. (interface/implementation) uses clause }
  478. if pu.in_uses and
  479. (pu.in_interface=current_module.in_interface) then
  480. begin
  481. tppumodule(pu.u).loadppu;
  482. { is our module compiled? then we can stop }
  483. if current_module.state=ms_compiled then
  484. exit;
  485. { add this unit to the dependencies }
  486. pu.u.adddependency(current_module);
  487. { save crc values }
  488. pu.checksum:=pu.u.crc;
  489. pu.interface_checksum:=pu.u.interface_crc;
  490. { connect unitsym to the globalsymtable of the unit }
  491. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  492. { increase refs of the unitsym when the unit contains
  493. initialization/finalization code so it doesn't trigger
  494. the unit not used hint }
  495. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  496. inc(pu.unitsym.refs);
  497. end;
  498. pu:=tused_unit(pu.next);
  499. end;
  500. { set the symtable to systemunit so it gets reorderd correctly,
  501. then insert the units in the symtablestack }
  502. pu:=tused_unit(current_module.used_units.first);
  503. symtablestack:=defaultsymtablestack;
  504. while assigned(pu) do
  505. begin
  506. if pu.in_uses then
  507. begin
  508. { Reinsert in symtablestack }
  509. hp3:=symtablestack;
  510. while assigned(hp3) do
  511. begin
  512. { insert units only once ! }
  513. if pu.u.globalsymtable=hp3 then
  514. break;
  515. hp3:=hp3.next;
  516. { unit isn't inserted }
  517. if hp3=nil then
  518. begin
  519. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  520. symtablestack:=tsymtable(pu.u.globalsymtable);
  521. {$ifdef DEBUG}
  522. test_symtablestack;
  523. {$endif DEBUG}
  524. end;
  525. end;
  526. end;
  527. pu:=tused_unit(pu.next);
  528. end;
  529. end;
  530. {$IfDef GDB}
  531. procedure write_gdb_info;
  532. procedure reset_unit_type_info;
  533. var
  534. hp : tmodule;
  535. begin
  536. hp:=tmodule(loaded_units.first);
  537. while assigned(hp) do
  538. begin
  539. hp.is_stab_written:=false;
  540. hp:=tmodule(hp.next);
  541. end;
  542. end;
  543. procedure write_used_unit_type_info(hp:tmodule);
  544. var
  545. pu : tused_unit;
  546. begin
  547. pu:=tused_unit(hp.used_units.first);
  548. while assigned(pu) do
  549. begin
  550. if not pu.u.is_stab_written then
  551. begin
  552. { prevent infinte loop for circular dependencies }
  553. pu.u.is_stab_written:=true;
  554. { write type info from used units, use a depth first
  555. strategy to reduce the recursion in writing all
  556. dependent stabs }
  557. write_used_unit_type_info(pu.u);
  558. if assigned(pu.u.globalsymtable) then
  559. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  560. end;
  561. pu:=tused_unit(pu.next);
  562. end;
  563. end;
  564. var
  565. vardebuglist : taasmoutput;
  566. begin
  567. if not (cs_debuginfo in aktmoduleswitches) then
  568. exit;
  569. { include symbol that will be referenced from the program to be sure to
  570. include this debuginfo .o file }
  571. if current_module.is_unit then
  572. begin
  573. current_module.flags:=current_module.flags or uf_has_debuginfo;
  574. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.globalsymtable,''),AT_DATA,0));
  575. end
  576. else
  577. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  578. { first write all global/local symbols to a temp list. This will flag
  579. all required tdefs. Afterwards this list will be added }
  580. vardebuglist:=taasmoutput.create;
  581. new_section(vardebuglist,sec_data,'',0);
  582. if assigned(current_module.globalsymtable) then
  583. tglobalsymtable(current_module.globalsymtable).concatstabto(vardebuglist);
  584. if assigned(current_module.localsymtable) then
  585. tstaticsymtable(current_module.localsymtable).concatstabto(vardebuglist);
  586. { reset unit type info flag }
  587. reset_unit_type_info;
  588. { write used types from the used units }
  589. write_used_unit_type_info(current_module);
  590. { last write the types from this unit }
  591. if assigned(current_module.globalsymtable) then
  592. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  593. if assigned(current_module.localsymtable) then
  594. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  595. { now all defs have a type in the debuglist, we now can add the vardebuglist since
  596. all references to defs can be solved }
  597. debuglist.concatlist(vardebuglist);
  598. vardebuglist.free;
  599. { include files }
  600. if (cs_gdb_dbx in aktglobalswitches) then
  601. begin
  602. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  603. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  604. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  605. debugList.concat(Tai_stabs.Create(strpnew('"'+
  606. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  607. tostr(N_EINCL)+',0,0,0')));
  608. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  609. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  610. do_count_dbx:=false;
  611. end;
  612. end;
  613. {$EndIf GDB}
  614. procedure reset_all_defs;
  615. procedure reset_used_unit_defs(hp:tmodule);
  616. var
  617. pu : tused_unit;
  618. begin
  619. pu:=tused_unit(hp.used_units.first);
  620. while assigned(pu) do
  621. begin
  622. if not pu.u.is_reset then
  623. begin
  624. { prevent infinte loop for circular dependencies }
  625. pu.u.is_reset:=true;
  626. if assigned(pu.u.globalsymtable) then
  627. begin
  628. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  629. reset_used_unit_defs(pu.u);
  630. end;
  631. end;
  632. pu:=tused_unit(pu.next);
  633. end;
  634. end;
  635. var
  636. hp2 : tmodule;
  637. begin
  638. hp2:=tmodule(loaded_units.first);
  639. while assigned(hp2) do
  640. begin
  641. hp2.is_reset:=false;
  642. hp2:=tmodule(hp2.next);
  643. end;
  644. reset_used_unit_defs(current_module);
  645. end;
  646. procedure free_localsymtables(st:tsymtable);
  647. var
  648. def : tstoreddef;
  649. pd : tprocdef;
  650. begin
  651. def:=tstoreddef(st.defindex.first);
  652. while assigned(def) do
  653. begin
  654. if def.deftype=procdef then
  655. begin
  656. pd:=tprocdef(def);
  657. if assigned(pd.localst) and
  658. (pd.localst.symtabletype<>staticsymtable) and
  659. not((pd.proccalloption=pocall_inline) or
  660. ((current_module.flags and uf_local_browser)<>0)) then
  661. begin
  662. free_localsymtables(pd.localst);
  663. pd.localst.free;
  664. pd.localst:=nil;
  665. end;
  666. end;
  667. def:=tstoreddef(def.indexnext);
  668. end;
  669. end;
  670. procedure parse_implementation_uses;
  671. begin
  672. if token=_USES then
  673. begin
  674. loadunits;
  675. {$ifdef DEBUG}
  676. test_symtablestack;
  677. {$endif DEBUG}
  678. end;
  679. end;
  680. procedure setupglobalswitches;
  681. begin
  682. { can't have local browser when no global browser }
  683. if (cs_local_browser in aktmoduleswitches) and
  684. not(cs_browser in aktmoduleswitches) then
  685. exclude(aktmoduleswitches,cs_local_browser);
  686. { define a symbol in delphi,objfpc,tp,gpc mode }
  687. if (m_delphi in aktmodeswitches) then
  688. current_scanner.def_macro('FPC_DELPHI')
  689. else
  690. if (m_tp7 in aktmodeswitches) then
  691. current_scanner.def_macro('FPC_TP')
  692. else
  693. if (m_objfpc in aktmodeswitches) then
  694. current_scanner.def_macro('FPC_OBJFPC')
  695. else
  696. if (m_gpc in aktmodeswitches) then
  697. current_scanner.def_macro('FPC_GPC')
  698. else
  699. if (m_mac in aktmodeswitches) then
  700. current_scanner.def_macro('FPC_MACPAS');
  701. end;
  702. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  703. var
  704. stt : tsymtable;
  705. ps : tprocsym;
  706. pd : tprocdef;
  707. begin
  708. { there should be no current_procinfo available }
  709. if assigned(current_procinfo) then
  710. internalerror(200304275);
  711. {Generate a procsym for main}
  712. make_ref:=false;
  713. { try to insert in in static symtable ! }
  714. stt:=symtablestack;
  715. symtablestack:=st;
  716. { generate procsym }
  717. ps:=tprocsym.create('$'+name);
  718. { main are allways used }
  719. inc(ps.refs);
  720. symtablestack.insert(ps);
  721. pd:=tprocdef.create(main_program_level);
  722. include(pd.procoptions,po_public);
  723. pd.procsym:=ps;
  724. ps.addprocdef(pd);
  725. { restore symtable }
  726. make_ref:=true;
  727. symtablestack:=stt;
  728. { set procdef options }
  729. pd.proctypeoption:=potype;
  730. pd.proccalloption:=pocall_default;
  731. pd.forwarddef:=false;
  732. pd.setmangledname(target_info.cprefix+name);
  733. pd.aliasnames.insert(pd.mangledname);
  734. { We don't need is a local symtable. Change it into the static
  735. symtable }
  736. pd.localst.free;
  737. pd.localst:=st;
  738. { set procinfo and current_procinfo.procdef }
  739. current_procinfo:=cprocinfo.create(nil);
  740. current_module.procinfo:=current_procinfo;
  741. current_procinfo.procdef:=pd;
  742. { return procdef }
  743. create_main_proc:=pd;
  744. { main proc does always a call e.g. to init system unit }
  745. include(current_procinfo.flags,pi_do_call);
  746. end;
  747. procedure release_main_proc(pd:tprocdef);
  748. begin
  749. { this is a main proc, so there should be no parent }
  750. if not(assigned(current_procinfo)) or
  751. assigned(current_procinfo.parent) or
  752. not(current_procinfo.procdef=pd) then
  753. internalerror(200304276);
  754. { remove procinfo }
  755. current_module.procinfo:=nil;
  756. current_procinfo.free;
  757. current_procinfo:=nil;
  758. { remove localst as it was replaced by staticsymtable }
  759. pd.localst:=nil;
  760. end;
  761. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  762. var
  763. pd : tprocdef;
  764. begin
  765. { update module flags }
  766. current_module.flags:=current_module.flags or flag;
  767. { create procdef }
  768. case flag of
  769. uf_init :
  770. begin
  771. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  772. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  773. end;
  774. uf_finalize :
  775. begin
  776. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  777. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  778. end;
  779. else
  780. internalerror(200304253);
  781. end;
  782. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  783. tcgprocinfo(current_procinfo).generate_code;
  784. release_main_proc(pd);
  785. end;
  786. procedure proc_unit;
  787. function is_assembler_generated:boolean;
  788. begin
  789. is_assembler_generated:=(Errorcount=0) and
  790. not(
  791. codeSegment.empty and
  792. dataSegment.empty and
  793. bssSegment.empty and
  794. ((importssection=nil) or importsSection.empty) and
  795. ((resourcesection=nil) or resourceSection.empty) and
  796. ((resourcestringlist=nil) or resourcestringList.empty)
  797. );
  798. end;
  799. var
  800. main_file: tinputfile;
  801. st : tsymtable;
  802. unitst : tglobalsymtable;
  803. store_crc,store_interface_crc : cardinal;
  804. s1,s2 : ^string; {Saves stack space}
  805. force_init_final : boolean;
  806. pd : tprocdef;
  807. unitname8 : string[8];
  808. has_impl: boolean;
  809. begin
  810. consume(_UNIT);
  811. if compile_level=1 then
  812. Status.IsExe:=false;
  813. if token=_ID then
  814. begin
  815. { create filenames and unit name }
  816. main_file := current_scanner.inputfile;
  817. while assigned(main_file.next) do
  818. main_file := main_file.next;
  819. new(s1);
  820. s1^:=current_module.modulename^;
  821. current_module.SetFileName(main_file.path^+main_file.name^,true);
  822. current_module.SetModuleName(orgpattern);
  823. { check for system unit }
  824. new(s2);
  825. s2^:=upper(SplitName(main_file.name^));
  826. unitname8:=copy(current_module.modulename^,1,8);
  827. if (cs_check_unit_name in aktglobalswitches) and
  828. (
  829. not(
  830. (current_module.modulename^=s2^) or
  831. (
  832. (length(current_module.modulename^)>8) and
  833. (unitname8=s2^)
  834. )
  835. )
  836. or
  837. (
  838. (length(s1^)>8) and
  839. (s1^<>current_module.modulename^)
  840. )
  841. ) then
  842. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  843. if (current_module.modulename^='SYSTEM') then
  844. include(aktmoduleswitches,cs_compilesystem);
  845. dispose(s2);
  846. dispose(s1);
  847. end;
  848. consume(_ID);
  849. consume(_SEMICOLON);
  850. consume(_INTERFACE);
  851. { global switches are read, so further changes aren't allowed }
  852. current_module.in_global:=false;
  853. { handle the global switches }
  854. setupglobalswitches;
  855. message1(unit_u_loading_interface_units,current_module.modulename^);
  856. { update status }
  857. status.currentmodule:=current_module.realmodulename^;
  858. { maybe turn off m_objpas if we are compiling objpas }
  859. if (current_module.modulename^='OBJPAS') then
  860. exclude(aktmodeswitches,m_objpas);
  861. { maybe turn off m_mac if we are compiling macpas }
  862. if (current_module.modulename^='MACPAS') then
  863. exclude(aktmodeswitches,m_mac);
  864. parse_only:=true;
  865. { generate now the global symboltable }
  866. st:=tglobalsymtable.create(current_module.modulename^);
  867. refsymtable:=st;
  868. unitst:=tglobalsymtable(st);
  869. { define first as local to overcome dependency conflicts }
  870. current_module.localsymtable:=st;
  871. { the unit name must be usable as a unit specifier }
  872. { inside the unit itself (PM) }
  873. { this also forbids to have another symbol }
  874. { with the same name as the unit }
  875. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  876. { load default units, like the system unit }
  877. loaddefaultunits;
  878. { reset }
  879. make_ref:=true;
  880. { insert qualifier for the system unit (allows system.writeln) }
  881. if not(cs_compilesystem in aktmoduleswitches) then
  882. begin
  883. if token=_USES then
  884. begin
  885. loadunits;
  886. { has it been compiled at a higher level ?}
  887. if current_module.state=ms_compiled then
  888. exit;
  889. end;
  890. { ... but insert the symbol table later }
  891. st.next:=symtablestack;
  892. symtablestack:=st;
  893. end
  894. else
  895. { while compiling a system unit, some types are directly inserted }
  896. begin
  897. st.next:=symtablestack;
  898. symtablestack:=st;
  899. insert_intern_types(st);
  900. end;
  901. { now we know the place to insert the constants }
  902. constsymtable:=symtablestack;
  903. { move the global symtab from the temporary local to global }
  904. current_module.globalsymtable:=current_module.localsymtable;
  905. current_module.localsymtable:=nil;
  906. reset_all_defs;
  907. { number all units, so we know if a unit is used by this unit or
  908. needs to be added implicitly }
  909. current_module.numberunits;
  910. { ... parse the declarations }
  911. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  912. read_interface_declarations;
  913. { leave when we got an error }
  914. if (Errorcount>0) and not status.skip_error then
  915. begin
  916. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  917. status.skip_error:=true;
  918. exit;
  919. end;
  920. { Our interface is compiled, generate CRC and switch to implementation }
  921. if not(cs_compilesystem in aktmoduleswitches) and
  922. (Errorcount=0) then
  923. tppumodule(current_module).getppucrc;
  924. current_module.in_interface:=false;
  925. current_module.interface_compiled:=true;
  926. { First reload all units depending on our interface, we need to do this
  927. in the implementation part to prevent errorneous circular references }
  928. reload_flagged_units;
  929. { Parse the implementation section }
  930. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  931. has_impl:= false
  932. else
  933. begin
  934. consume(_IMPLEMENTATION);
  935. has_impl:= true;
  936. end;
  937. if has_impl then
  938. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  939. parse_only:=false;
  940. { generates static symbol table }
  941. st:=tstaticsymtable.create(current_module.modulename^);
  942. current_module.localsymtable:=st;
  943. { remove the globalsymtable from the symtable stack }
  944. { to reinsert it after loading the implementation units }
  945. symtablestack:=unitst.next;
  946. { we don't want implementation units symbols in unitsymtable !! PM }
  947. refsymtable:=st;
  948. { Read the implementation units }
  949. if has_impl then
  950. parse_implementation_uses;
  951. if current_module.state=ms_compiled then
  952. exit;
  953. { reset ranges/stabs in exported definitions }
  954. reset_all_defs;
  955. { All units are read, now give them a number }
  956. current_module.numberunits;
  957. { now we can change refsymtable }
  958. refsymtable:=st;
  959. { but reinsert the global symtable as lasts }
  960. unitst.next:=symtablestack;
  961. symtablestack:=unitst;
  962. {$ifdef DEBUG}
  963. test_symtablestack;
  964. {$endif DEBUG}
  965. constsymtable:=symtablestack;
  966. if has_impl then
  967. begin
  968. Message1(parser_u_parsing_implementation,current_module.modulename^);
  969. if current_module.in_interface then
  970. internalerror(200212285);
  971. { Compile the unit }
  972. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  973. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  974. tcgprocinfo(current_procinfo).parse_body;
  975. tcgprocinfo(current_procinfo).generate_code;
  976. tcgprocinfo(current_procinfo).resetprocdef;
  977. release_main_proc(pd);
  978. end;
  979. { if the unit contains ansi/widestrings, initialization and
  980. finalization code must be forced }
  981. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  982. tstaticsymtable(current_module.localsymtable).needs_init_final;
  983. { should we force unit initialization? }
  984. { this is a hack, but how can it be done better ? }
  985. if force_init_final and ((current_module.flags and uf_init)=0) then
  986. gen_implicit_initfinal(uf_init,st);
  987. { finalize? }
  988. if has_impl and (token=_FINALIZATION) then
  989. begin
  990. { set module options }
  991. current_module.flags:=current_module.flags or uf_finalize;
  992. { Compile the finalize }
  993. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  994. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  995. tcgprocinfo(current_procinfo).parse_body;
  996. tcgprocinfo(current_procinfo).generate_code;
  997. tcgprocinfo(current_procinfo).resetprocdef;
  998. release_main_proc(pd);
  999. end
  1000. else if force_init_final then
  1001. gen_implicit_initfinal(uf_finalize,st);
  1002. { the last char should always be a point }
  1003. consume(_POINT);
  1004. { Generate resoucestrings }
  1005. If ResourceStrings.ResStrCount>0 then
  1006. begin
  1007. ResourceStrings.CreateResourceStringList;
  1008. current_module.flags:=current_module.flags or uf_has_resources;
  1009. { only write if no errors found }
  1010. if (Errorcount=0) then
  1011. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1012. end;
  1013. if (Errorcount=0) then
  1014. begin
  1015. { tests, if all (interface) forwards are resolved }
  1016. tstoredsymtable(symtablestack).check_forwards;
  1017. { check if all private fields are used }
  1018. tstoredsymtable(symtablestack).allprivatesused;
  1019. { remove cross unit overloads }
  1020. tstoredsymtable(symtablestack).unchain_overloaded;
  1021. { test static symtable }
  1022. tstoredsymtable(st).allsymbolsused;
  1023. tstoredsymtable(st).allprivatesused;
  1024. tstoredsymtable(st).check_forwards;
  1025. tstoredsymtable(st).checklabels;
  1026. tstoredsymtable(st).unchain_overloaded;
  1027. { used units }
  1028. current_module.allunitsused;
  1029. end;
  1030. { leave when we got an error }
  1031. if (Errorcount>0) and not status.skip_error then
  1032. begin
  1033. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1034. status.skip_error:=true;
  1035. exit;
  1036. end;
  1037. { generate debuginfo }
  1038. {$ifdef GDB}
  1039. write_gdb_info;
  1040. {$endif GDB}
  1041. { generate a list of threadvars }
  1042. InsertThreadvars;
  1043. { generate imports }
  1044. if current_module.uses_imports then
  1045. importlib.generatelib;
  1046. { insert own objectfile, or say that it's in a library
  1047. (no check for an .o when loading) }
  1048. if is_assembler_generated then
  1049. insertobjectfile
  1050. else
  1051. current_module.flags:=current_module.flags or uf_no_link;
  1052. if cs_local_browser in aktmoduleswitches then
  1053. current_module.localsymtable:=refsymtable;
  1054. if is_assembler_generated then
  1055. begin
  1056. { create dwarf debuginfo }
  1057. create_dwarf;
  1058. { finish asmlist by adding segment starts }
  1059. insertsegment;
  1060. { assemble }
  1061. create_objectfile;
  1062. end;
  1063. { Write out the ppufile after the object file has been created }
  1064. store_interface_crc:=current_module.interface_crc;
  1065. store_crc:=current_module.crc;
  1066. if (Errorcount=0) then
  1067. tppumodule(current_module).writeppu;
  1068. if not(cs_compilesystem in aktmoduleswitches) then
  1069. if store_interface_crc<>current_module.interface_crc then
  1070. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1071. {$ifdef EXTDEBUG}
  1072. if not(cs_compilesystem in aktmoduleswitches) then
  1073. if (store_crc<>current_module.crc) and simplify_ppu then
  1074. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1075. {$endif EXTDEBUG}
  1076. { release all local symtables that are not needed anymore }
  1077. free_localsymtables(current_module.globalsymtable);
  1078. free_localsymtables(current_module.localsymtable);
  1079. { remove static symtable (=refsymtable) here to save some mem }
  1080. if not (cs_local_browser in aktmoduleswitches) then
  1081. begin
  1082. st.free;
  1083. current_module.localsymtable:=nil;
  1084. end;
  1085. { leave when we got an error }
  1086. if (Errorcount>0) and not status.skip_error then
  1087. begin
  1088. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1089. status.skip_error:=true;
  1090. exit;
  1091. end;
  1092. Message1(unit_u_finished_compiling,current_module.modulename^);
  1093. end;
  1094. procedure proc_program(islibrary : boolean);
  1095. var
  1096. main_file : tinputfile;
  1097. st : tsymtable;
  1098. hp,hp2 : tmodule;
  1099. pd : tprocdef;
  1100. begin
  1101. DLLsource:=islibrary;
  1102. Status.IsLibrary:=IsLibrary;
  1103. Status.IsExe:=true;
  1104. parse_only:=false;
  1105. { DLL defaults to create reloc info }
  1106. if islibrary then
  1107. begin
  1108. if not RelocSectionSetExplicitly then
  1109. RelocSection:=true;
  1110. end;
  1111. { relocation works only without stabs under win32 !! PM }
  1112. { internal assembler uses rva for stabs info
  1113. so it should work with relocated DLLs }
  1114. if RelocSection and
  1115. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1116. (target_info.assem<>as_i386_pecoff) then
  1117. begin
  1118. include(aktglobalswitches,cs_link_strip);
  1119. { Warning stabs info does not work with reloc section !! }
  1120. if cs_debuginfo in aktmoduleswitches then
  1121. begin
  1122. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1123. Message(parser_w_parser_win32_debug_needs_WN);
  1124. exclude(aktmoduleswitches,cs_debuginfo);
  1125. end;
  1126. end;
  1127. { get correct output names }
  1128. main_file := current_scanner.inputfile;
  1129. while assigned(main_file.next) do
  1130. main_file := main_file.next;
  1131. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1132. if islibrary then
  1133. begin
  1134. consume(_LIBRARY);
  1135. stringdispose(current_module.modulename);
  1136. current_module.modulename:=stringdup(pattern);
  1137. current_module.islibrary:=true;
  1138. exportlib.preparelib(pattern);
  1139. consume(_ID);
  1140. consume(_SEMICOLON);
  1141. end
  1142. else
  1143. { is there an program head ? }
  1144. if token=_PROGRAM then
  1145. begin
  1146. consume(_PROGRAM);
  1147. stringdispose(current_module.modulename);
  1148. stringdispose(current_module.realmodulename);
  1149. current_module.modulename:=stringdup(pattern);
  1150. current_module.realmodulename:=stringdup(orgpattern);
  1151. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1152. exportlib.preparelib(pattern);
  1153. consume(_ID);
  1154. if token=_LKLAMMER then
  1155. begin
  1156. consume(_LKLAMMER);
  1157. repeat
  1158. consume(_ID);
  1159. until not try_to_consume(_COMMA);
  1160. consume(_RKLAMMER);
  1161. end;
  1162. consume(_SEMICOLON);
  1163. end
  1164. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1165. exportlib.preparelib(current_module.modulename^);
  1166. { global switches are read, so further changes aren't allowed }
  1167. current_module.in_global:=false;
  1168. { setup things using the global switches }
  1169. setupglobalswitches;
  1170. { set implementation flag }
  1171. current_module.in_interface:=false;
  1172. current_module.interface_compiled:=true;
  1173. { insert after the unit symbol tables the static symbol table }
  1174. { of the program }
  1175. st:=tstaticsymtable.create(current_module.modulename^);;
  1176. current_module.localsymtable:=st;
  1177. refsymtable:=st;
  1178. { load standard units (system,objpas,profile unit) }
  1179. loaddefaultunits;
  1180. {Load the units used by the program we compile.}
  1181. if token=_USES then
  1182. loadunits;
  1183. { reset ranges/stabs in exported definitions }
  1184. reset_all_defs;
  1185. { All units are read, now give them a number }
  1186. current_module.numberunits;
  1187. {Insert the name of the main program into the symbol table.}
  1188. if current_module.realmodulename^<>'' then
  1189. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1190. { ...is also constsymtable, this is the symtable where }
  1191. { the elements of enumeration types are inserted }
  1192. constsymtable:=st;
  1193. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1194. { The program intialization needs an alias, so it can be called
  1195. from the bootstrap code.}
  1196. if islibrary or
  1197. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1198. begin
  1199. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1200. { Win32 startup code needs a single name }
  1201. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1202. pd.aliasnames.insert('PASCALMAIN');
  1203. end
  1204. else
  1205. begin
  1206. if (target_info.system = system_i386_netware) or
  1207. (target_info.system = system_i386_netwlibc) then
  1208. begin
  1209. pd:=create_main_proc('PASCALMAIN',potype_proginit,st); { main is need by the netware rtl }
  1210. end else
  1211. begin
  1212. pd:=create_main_proc('main',potype_proginit,st);
  1213. pd.aliasnames.insert('PASCALMAIN');
  1214. end;
  1215. end;
  1216. tcgprocinfo(current_procinfo).parse_body;
  1217. tcgprocinfo(current_procinfo).generate_code;
  1218. tcgprocinfo(current_procinfo).resetprocdef;
  1219. release_main_proc(pd);
  1220. { should we force unit initialization? }
  1221. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1222. begin
  1223. { initialize section }
  1224. gen_implicit_initfinal(uf_init,st);
  1225. { finalize section }
  1226. gen_implicit_initfinal(uf_finalize,st);
  1227. end;
  1228. { Add symbol to the exports section for win32 so smartlinking a
  1229. DLL will include the edata section }
  1230. if assigned(exportlib) and
  1231. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1232. assigned(current_module._exports.first) then
  1233. codesegment.concat(tai_const.create_sym(exportlib.edatalabel));
  1234. If ResourceStrings.ResStrCount>0 then
  1235. begin
  1236. ResourceStrings.CreateResourceStringList;
  1237. { only write if no errors found }
  1238. if (Errorcount=0) then
  1239. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1240. end;
  1241. { finalize? }
  1242. if token=_FINALIZATION then
  1243. begin
  1244. { set module options }
  1245. current_module.flags:=current_module.flags or uf_finalize;
  1246. { Compile the finalize }
  1247. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1248. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1249. tcgprocinfo(current_procinfo).parse_body;
  1250. tcgprocinfo(current_procinfo).generate_code;
  1251. tcgprocinfo(current_procinfo).resetprocdef;
  1252. release_main_proc(pd);
  1253. end;
  1254. { consume the last point }
  1255. consume(_POINT);
  1256. if (Errorcount=0) then
  1257. begin
  1258. { test static symtable }
  1259. tstoredsymtable(st).allsymbolsused;
  1260. tstoredsymtable(st).allprivatesused;
  1261. tstoredsymtable(st).check_forwards;
  1262. tstoredsymtable(st).checklabels;
  1263. tstoredsymtable(st).unchain_overloaded;
  1264. current_module.allunitsused;
  1265. end;
  1266. { leave when we got an error }
  1267. if (Errorcount>0) and not status.skip_error then
  1268. begin
  1269. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1270. status.skip_error:=true;
  1271. exit;
  1272. end;
  1273. { generate debuginfo }
  1274. {$ifdef GDB}
  1275. write_gdb_info;
  1276. {$endif GDB}
  1277. { generate a list of threadvars }
  1278. InsertThreadvars;
  1279. { generate imports }
  1280. if current_module.uses_imports then
  1281. importlib.generatelib;
  1282. if islibrary or
  1283. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1284. (target_info.system=system_i386_NETWARE) then
  1285. exportlib.generatelib;
  1286. { insert Tables and StackLength }
  1287. insertThreadVarTablesTable;
  1288. insertResourceTablesTable;
  1289. insertinitfinaltable;
  1290. insertmemorysizes;
  1291. { create dwarf debuginfo }
  1292. create_dwarf;
  1293. { finish asmlist by adding segment starts }
  1294. insertsegment;
  1295. { insert own objectfile }
  1296. insertobjectfile;
  1297. { assemble and link }
  1298. create_objectfile;
  1299. { release all local symtables that are not needed anymore }
  1300. free_localsymtables(current_module.localsymtable);
  1301. { leave when we got an error }
  1302. if (Errorcount>0) and not status.skip_error then
  1303. begin
  1304. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1305. status.skip_error:=true;
  1306. exit;
  1307. end;
  1308. { create the executable when we are at level 1 }
  1309. if (compile_level=1) then
  1310. begin
  1311. { remove all unused units, this happends when units are removed
  1312. from the uses clause in the source and the ppu was already being loaded }
  1313. hp:=tmodule(loaded_units.first);
  1314. while assigned(hp) do
  1315. begin
  1316. hp2:=hp;
  1317. hp:=tmodule(hp.next);
  1318. if hp2.is_unit and
  1319. not assigned(hp2.globalsymtable) then
  1320. loaded_units.remove(hp2);
  1321. end;
  1322. { insert all .o files from all loaded units }
  1323. hp:=tmodule(loaded_units.first);
  1324. while assigned(hp) do
  1325. begin
  1326. linker.AddModuleFiles(hp);
  1327. hp:=tmodule(hp.next);
  1328. end;
  1329. { write .def file }
  1330. if (cs_link_deffile in aktglobalswitches) then
  1331. deffile.writefile;
  1332. { finally we can create a executable }
  1333. if (not current_module.is_unit) then
  1334. begin
  1335. if DLLSource then
  1336. linker.MakeSharedLibrary
  1337. else
  1338. linker.MakeExecutable;
  1339. end;
  1340. end;
  1341. end;
  1342. end.
  1343. {
  1344. $Log$
  1345. Revision 1.169 2004-10-31 15:29:39 olle
  1346. + All sections get names in macos
  1347. Revision 1.168 2004/10/26 15:11:01 peter
  1348. * -Ch for heapsize added again
  1349. * __heapsize contains the heapsize
  1350. Revision 1.167 2004/10/25 15:38:41 peter
  1351. * heap and heapsize removed
  1352. * checkpointer fixes
  1353. Revision 1.166 2004/10/15 09:14:17 mazen
  1354. - remove $IFDEF DELPHI and related code
  1355. - remove $IFDEF FPCPROCVAR and related code
  1356. Revision 1.165 2004/10/04 18:26:51 peter
  1357. * debuginfo fixes
  1358. Revision 1.164 2004/09/14 16:33:46 peter
  1359. * release localsymtables when module is compiled
  1360. Revision 1.163 2004/09/04 21:18:47 armin
  1361. * target netwlibc added (libc is preferred for newer netware versions)
  1362. Revision 1.162 2004/09/03 16:12:32 armin
  1363. * dont create main for netware (only PASCALMAIN)
  1364. Revision 1.161 2004/08/16 22:52:35 olle
  1365. + Added automatic use of unit macpas under mode macpas
  1366. Revision 1.160 2004/07/06 20:23:25 peter
  1367. * remove unused and not loaded units before linking
  1368. Revision 1.159 2004/06/29 21:00:08 peter
  1369. * only enable dwarf for supported platforms
  1370. Revision 1.158 2004/06/20 08:55:30 florian
  1371. * logs truncated
  1372. Revision 1.157 2004/06/18 15:16:27 peter
  1373. * fixed debuginfo symbol
  1374. Revision 1.156 2004/06/16 20:07:09 florian
  1375. * dwarf branch merged
  1376. Revision 1.155 2004/05/23 20:56:42 peter
  1377. * initialize errorsym/errortype.def.owner to prevent crashes
  1378. Revision 1.154 2004/05/23 15:06:21 peter
  1379. * implicit_finally flag must be set in pass1
  1380. * add check whether the implicit frame is generated when expected
  1381. Revision 1.153 2004/05/19 21:16:13 peter
  1382. * add DEBUGINFO symbol to reference the .o file that includes the
  1383. stabs info for types and global/static variables
  1384. * debuginfo flag added to ppu to indicate whether debuginfo is
  1385. generated or not
  1386. }