pmodules.pas 61 KB

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