pmodules.pas 60 KB

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