2
0

pmodules.pas 60 KB

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