pmodules.pas 56 KB

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