pmodules.pas 61 KB

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