pmodules.pas 56 KB

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