pmodules.pas 60 KB

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