pmodules.pas 56 KB

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