pmodules.pas 59 KB

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