pmodules.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105
  1. {
  2. Copyright (c) 1998-2008 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. function proc_unit:boolean;
  21. procedure proc_package;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. SysUtils,
  26. globtype,version,systems,tokens,
  27. cutils,cfileutl,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,globstat,
  29. symconst,symbase,symtype,symdef,symsym,symtable,symcreat,
  30. wpoinfo,
  31. aasmtai,aasmdata,aasmcpu,aasmbase,
  32. cgbase,cgobj,ngenutil,
  33. nbas,nutils,ncgutil,
  34. link,assemble,import,export,gendef,entfile,ppu,comprsrc,dbgbase,
  35. cresstr,procinfo,
  36. pexports,
  37. objcgutl,
  38. pkgutil,
  39. wpobase,
  40. scanner,pbase,pexpr,psystem,psub,pdecsub,ncgvmt,
  41. cpuinfo;
  42. procedure create_objectfile;
  43. var
  44. DLLScanner : TDLLScanner;
  45. s : string;
  46. KeepShared : TCmdStrList;
  47. begin
  48. { try to create import entries from system dlls }
  49. if (tf_has_dllscanner in target_info.flags) and
  50. (not current_module.linkOtherSharedLibs.Empty) then
  51. begin
  52. { Init DLLScanner }
  53. if assigned(CDLLScanner[target_info.system]) then
  54. DLLScanner:=CDLLScanner[target_info.system].Create
  55. else
  56. internalerror(200104121);
  57. KeepShared:=TCmdStrList.Create;
  58. { Walk all shared libs }
  59. While not current_module.linkOtherSharedLibs.Empty do
  60. begin
  61. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  62. if not DLLScanner.scan(s) then
  63. KeepShared.Concat(s);
  64. end;
  65. DLLscanner.Free;
  66. { Recreate import section }
  67. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  68. begin
  69. if assigned(current_asmdata.asmlists[al_imports]) then
  70. current_asmdata.asmlists[al_imports].clear
  71. else
  72. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  73. importlib.generatelib;
  74. end;
  75. { Readd the not processed files }
  76. while not KeepShared.Empty do
  77. begin
  78. s:=KeepShared.GetFirst;
  79. current_module.linkOtherSharedLibs.add(s,link_always);
  80. end;
  81. KeepShared.Free;
  82. end;
  83. { Start and end module debuginfo, at least required for stabs
  84. to insert n_sourcefile lines }
  85. if (cs_debuginfo in current_settings.moduleswitches) or
  86. (cs_use_lineinfo in current_settings.globalswitches) then
  87. current_debuginfo.insertmoduleinfo;
  88. { create the .s file and assemble it }
  89. if not(create_smartlink_library) or not(tf_no_objectfiles_when_smartlinking in target_info.flags) then
  90. GenerateAsm(false);
  91. { Also create a smartlinked version ? }
  92. if create_smartlink_library then
  93. begin
  94. GenerateAsm(true);
  95. if (af_needar in target_asm.flags) then
  96. Linker.MakeStaticLibrary;
  97. end;
  98. { resource files }
  99. CompileResourceFiles;
  100. end;
  101. procedure insertobjectfile;
  102. { Insert the used object file for this unit in the used list for this unit }
  103. begin
  104. current_module.linkunitofiles.add(current_module.objfilename,link_static);
  105. current_module.flags:=current_module.flags or uf_static_linked;
  106. if create_smartlink_library then
  107. begin
  108. current_module.linkunitstaticlibs.add(current_module.staticlibfilename ,link_smart);
  109. current_module.flags:=current_module.flags or uf_smart_linked;
  110. end;
  111. end;
  112. procedure create_dwarf_frame;
  113. begin
  114. { Dwarf conflicts with smartlinking in separate .a files }
  115. if create_smartlink_library then
  116. exit;
  117. { Call frame information }
  118. { MWE: we write our own info, so dwarf asm support is not really needed }
  119. { if (af_supports_dwarf in target_asm.flags) and }
  120. { CFI is currently broken for Darwin }
  121. if not(target_info.system in systems_darwin) and
  122. (
  123. (tf_needs_dwarf_cfi in target_info.flags) or
  124. (paratargetdbg in [dbg_dwarf2, dbg_dwarf3])
  125. ) then
  126. begin
  127. current_asmdata.asmlists[al_dwarf_frame].Free;
  128. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  129. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  130. end;
  131. end;
  132. Function CheckResourcesUsed : boolean;
  133. var
  134. hp : tused_unit;
  135. found : Boolean;
  136. begin
  137. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  138. if not CheckResourcesUsed then exit;
  139. hp:=tused_unit(usedunits.first);
  140. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  141. If not found then
  142. While Assigned(hp) and not found do
  143. begin
  144. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  145. hp:=tused_unit(hp.next);
  146. end;
  147. CheckResourcesUsed:=found;
  148. end;
  149. procedure AddUnit(const s:string);
  150. var
  151. hp : tppumodule;
  152. unitsym : tunitsym;
  153. begin
  154. { load unit }
  155. hp:=registerunit(current_module,s,'');
  156. hp.loadppu;
  157. hp.adddependency(current_module);
  158. { add to symtable stack }
  159. symtablestack.push(hp.globalsymtable);
  160. if (m_mac in current_settings.modeswitches) and
  161. assigned(hp.globalmacrosymtable) then
  162. macrosymtablestack.push(hp.globalmacrosymtable);
  163. { insert unitsym }
  164. unitsym:=cunitsym.create(s,hp);
  165. inc(unitsym.refs);
  166. tabstractunitsymtable(current_module.localsymtable).insertunit(unitsym);
  167. { add to used units }
  168. current_module.addusedunit(hp,false,unitsym);
  169. end;
  170. procedure maybeloadvariantsunit;
  171. var
  172. hp : tmodule;
  173. begin
  174. { Do we need the variants unit? Skip this
  175. for VarUtils unit for bootstrapping }
  176. if (current_module.flags and uf_uses_variants=0) or
  177. (current_module.modulename^='VARUTILS') then
  178. exit;
  179. { Variants unit already loaded? }
  180. hp:=tmodule(loaded_units.first);
  181. while assigned(hp) do
  182. begin
  183. if hp.modulename^='VARIANTS' then
  184. exit;
  185. hp:=tmodule(hp.next);
  186. end;
  187. { Variants unit is not loaded yet, load it now }
  188. Message(parser_w_implicit_uses_of_variants_unit);
  189. AddUnit('variants');
  190. end;
  191. function MaybeRemoveResUnit : boolean;
  192. var
  193. resources_used : boolean;
  194. hp : tmodule;
  195. uu : tused_unit;
  196. unitname : shortstring;
  197. begin
  198. { We simply remove the unit from:
  199. - usedunit list, so that things like init/finalization table won't
  200. contain references to this unit
  201. - loaded_units list, so that the unit object file doesn't get linked
  202. with the executable. }
  203. { Note: on windows we always need resources! }
  204. resources_used:=(target_info.system in systems_all_windows)
  205. or CheckResourcesUsed;
  206. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  207. begin
  208. { resources aren't used, so we don't need this unit }
  209. if target_res.id=res_ext then
  210. unitname:='FPEXTRES'
  211. else
  212. unitname:='FPINTRES';
  213. Message1(unit_u_unload_resunit,unitname);
  214. { find the module }
  215. hp:=tmodule(loaded_units.first);
  216. while assigned(hp) do
  217. begin
  218. if hp.is_unit and (hp.modulename^=unitname) then break;
  219. hp:=tmodule(hp.next);
  220. end;
  221. if not assigned(hp) then
  222. internalerror(200801071);
  223. { find its tused_unit in the global list }
  224. uu:=tused_unit(usedunits.first);
  225. while assigned(uu) do
  226. begin
  227. if uu.u=hp then break;
  228. uu:=tused_unit(uu.next);
  229. end;
  230. if not assigned(uu) then
  231. internalerror(200801072);
  232. { remove the tused_unit }
  233. usedunits.Remove(uu);
  234. uu.Free;
  235. { remove the module }
  236. loaded_units.Remove(hp);
  237. unloaded_units.Concat(hp);
  238. end;
  239. MaybeRemoveResUnit:=resources_used;
  240. end;
  241. procedure loaddefaultunits;
  242. begin
  243. { we are going to rebuild the symtablestack, clear it first }
  244. symtablestack.clear;
  245. macrosymtablestack.clear;
  246. { macro symtable }
  247. macrosymtablestack.push(initialmacrosymtable);
  248. { are we compiling the system unit? }
  249. if (cs_compilesystem in current_settings.moduleswitches) then
  250. begin
  251. systemunit:=tglobalsymtable(current_module.localsymtable);
  252. { create system defines }
  253. create_intern_types;
  254. create_intern_symbols;
  255. { Set the owner of errorsym and errortype to symtable to
  256. prevent crashes when accessing .owner }
  257. generrorsym.owner:=systemunit;
  258. generrordef.owner:=systemunit;
  259. exit;
  260. end;
  261. { insert the system unit, it is allways the first. Load also the
  262. internal types from the system unit }
  263. AddUnit('system');
  264. systemunit:=tglobalsymtable(symtablestack.top);
  265. load_intern_types;
  266. { Set the owner of errorsym and errortype to symtable to
  267. prevent crashes when accessing .owner }
  268. generrorsym.owner:=systemunit;
  269. generrordef.owner:=systemunit;
  270. { Units only required for main module }
  271. if not(current_module.is_unit) then
  272. begin
  273. { Heaptrc unit, load heaptrace before any other units especially objpas }
  274. if (cs_use_heaptrc in current_settings.globalswitches) then
  275. AddUnit('heaptrc');
  276. { Lineinfo unit }
  277. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  278. case paratargetdbg of
  279. dbg_stabs:
  280. AddUnit('lineinfo');
  281. dbg_stabx:
  282. AddUnit('lnfogdb');
  283. else
  284. AddUnit('lnfodwrf');
  285. end;
  286. end;
  287. { Valgrind requires c memory manager }
  288. if (cs_gdb_valgrind in current_settings.globalswitches) then
  289. AddUnit('cmem');
  290. {$ifdef cpufpemu}
  291. { Floating point emulation unit?
  292. softfpu must be in the system unit anyways (FK)
  293. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  294. AddUnit('softfpu');
  295. }
  296. {$endif cpufpemu}
  297. { Which kind of resource support?
  298. Note: if resources aren't used this unit will be removed later,
  299. otherwise we need it here since it must be loaded quite early }
  300. if (tf_has_winlike_resources in target_info.flags) then
  301. if target_res.id=res_ext then
  302. AddUnit('fpextres')
  303. else
  304. AddUnit('fpintres');
  305. end;
  306. { Objpas unit? }
  307. if m_objpas in current_settings.modeswitches then
  308. AddUnit('objpas');
  309. { Macpas unit? }
  310. if m_mac in current_settings.modeswitches then
  311. AddUnit('macpas');
  312. if m_iso in current_settings.modeswitches then
  313. AddUnit('iso7185');
  314. { default char=widechar? }
  315. if m_default_unicodestring in current_settings.modeswitches then
  316. AddUnit('uuchar');
  317. { Objective-C support unit? }
  318. if (m_objectivec1 in current_settings.modeswitches) then
  319. begin
  320. { interface to Objective-C run time }
  321. AddUnit('objc');
  322. loadobjctypes;
  323. { NSObject }
  324. if not(current_module.is_unit) or
  325. (current_module.modulename^<>'OBJCBASE') then
  326. AddUnit('objcbase');
  327. end;
  328. { Profile unit? Needed for go32v2 only }
  329. if (cs_profile in current_settings.moduleswitches) and
  330. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  331. AddUnit('profile');
  332. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  333. begin
  334. AddUnit('fpcylix');
  335. AddUnit('dynlibs');
  336. end;
  337. { CPU targets with microcontroller support can add a controller specific unit }
  338. {$if defined(ARM) or defined(AVR) or defined(MIPSEL)}
  339. if (target_info.system in systems_embedded) and (current_settings.controllertype<>ct_none) and
  340. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') then
  341. AddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  342. {$endif ARM AVR MIPSEL}
  343. end;
  344. procedure loadautounits;
  345. var
  346. hs,s : string;
  347. begin
  348. hs:=autoloadunits;
  349. repeat
  350. s:=GetToken(hs,',');
  351. if s='' then
  352. break;
  353. AddUnit(s);
  354. until false;
  355. end;
  356. procedure loadunits(preservest:tsymtable);
  357. var
  358. s,sorg : ansistring;
  359. fn : string;
  360. pu : tused_unit;
  361. hp2 : tmodule;
  362. unitsym : tunitsym;
  363. filepos : tfileposinfo;
  364. begin
  365. consume(_USES);
  366. repeat
  367. s:=pattern;
  368. sorg:=orgpattern;
  369. filepos:=current_tokenpos;
  370. consume(_ID);
  371. while token=_POINT do
  372. begin
  373. consume(_POINT);
  374. s:=s+'.'+pattern;
  375. sorg:=sorg+'.'+orgpattern;
  376. consume(_ID);
  377. end;
  378. { support "<unit> in '<file>'" construct, but not for tp7 }
  379. fn:='';
  380. if not(m_tp7 in current_settings.modeswitches) and
  381. try_to_consume(_OP_IN) then
  382. fn:=FixFileName(get_stringconst);
  383. { Give a warning if lineinfo is loaded }
  384. if s='LINEINFO' then
  385. begin
  386. Message(parser_w_no_lineinfo_use_switch);
  387. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  388. s := 'LNFODWRF';
  389. sorg := s;
  390. end;
  391. { Give a warning if objpas is loaded }
  392. if s='OBJPAS' then
  393. Message(parser_w_no_objpas_use_mode);
  394. { Using the unit itself is not possible }
  395. if (s<>current_module.modulename^) then
  396. begin
  397. { check if the unit is already used }
  398. hp2:=nil;
  399. pu:=tused_unit(current_module.used_units.first);
  400. while assigned(pu) do
  401. begin
  402. if (pu.u.modulename^=s) then
  403. begin
  404. hp2:=pu.u;
  405. break;
  406. end;
  407. pu:=tused_unit(pu.next);
  408. end;
  409. if not assigned(hp2) then
  410. hp2:=registerunit(current_module,sorg,fn)
  411. else
  412. Message1(sym_e_duplicate_id,s);
  413. { Create unitsym, we need to use the name as specified, we
  414. can not use the modulename because that can be different
  415. when -Un is used }
  416. current_tokenpos:=filepos;
  417. unitsym:=cunitsym.create(sorg,nil);
  418. tabstractunitsymtable(current_module.localsymtable).insertunit(unitsym);
  419. { the current module uses the unit hp2 }
  420. current_module.addusedunit(hp2,true,unitsym);
  421. end
  422. else
  423. Message1(sym_e_duplicate_id,s);
  424. if token=_COMMA then
  425. begin
  426. pattern:='';
  427. consume(_COMMA);
  428. end
  429. else
  430. break;
  431. until false;
  432. { Load the units }
  433. pu:=tused_unit(current_module.used_units.first);
  434. while assigned(pu) do
  435. begin
  436. { Only load the units that are in the current
  437. (interface/implementation) uses clause }
  438. if pu.in_uses and
  439. (pu.in_interface=current_module.in_interface) then
  440. begin
  441. tppumodule(pu.u).loadppu;
  442. { is our module compiled? then we can stop }
  443. if current_module.state=ms_compiled then
  444. exit;
  445. { add this unit to the dependencies }
  446. pu.u.adddependency(current_module);
  447. { save crc values }
  448. pu.checksum:=pu.u.crc;
  449. pu.interface_checksum:=pu.u.interface_crc;
  450. pu.indirect_checksum:=pu.u.indirect_crc;
  451. { connect unitsym to the module }
  452. pu.unitsym.module:=pu.u;
  453. { add to symtable stack }
  454. if assigned(preservest) then
  455. symtablestack.pushafter(pu.u.globalsymtable,preservest)
  456. else
  457. symtablestack.push(pu.u.globalsymtable);
  458. if (m_mac in current_settings.modeswitches) and
  459. assigned(pu.u.globalmacrosymtable) then
  460. macrosymtablestack.push(pu.u.globalmacrosymtable);
  461. { check hints }
  462. pu.check_hints;
  463. end;
  464. pu:=tused_unit(pu.next);
  465. end;
  466. end;
  467. procedure reset_all_defs;
  468. begin
  469. if assigned(current_module.wpoinfo) then
  470. current_module.wpoinfo.resetdefs;
  471. end;
  472. procedure free_localsymtables(st:TSymtable);
  473. var
  474. i : longint;
  475. def : tstoreddef;
  476. pd : tprocdef;
  477. begin
  478. for i:=0 to st.DefList.Count-1 do
  479. begin
  480. def:=tstoreddef(st.DefList[i]);
  481. if def.typ=procdef then
  482. begin
  483. pd:=tprocdef(def);
  484. if assigned(pd.localst) and
  485. (pd.localst.symtabletype<>staticsymtable) and
  486. not(po_inline in pd.procoptions) then
  487. begin
  488. free_localsymtables(pd.localst);
  489. pd.localst.free;
  490. pd.localst:=nil;
  491. end;
  492. end;
  493. end;
  494. end;
  495. procedure setupglobalswitches;
  496. begin
  497. if (cs_create_pic in current_settings.moduleswitches) then
  498. begin
  499. def_system_macro('FPC_PIC');
  500. def_system_macro('PIC');
  501. end;
  502. end;
  503. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  504. var
  505. ps : tprocsym;
  506. pd : tprocdef;
  507. begin
  508. { there should be no current_procinfo available }
  509. if assigned(current_procinfo) then
  510. internalerror(200304275);
  511. {Generate a procsym for main}
  512. ps:=cprocsym.create('$'+name);
  513. { main are allways used }
  514. inc(ps.refs);
  515. st.insert(ps);
  516. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  517. { We don't need is a local symtable. Change it into the static
  518. symtable }
  519. pd.localst.free;
  520. pd.localst:=st;
  521. handle_calling_convention(pd);
  522. { set procinfo and current_procinfo.procdef }
  523. result:=tcgprocinfo(cprocinfo.create(nil));
  524. result.procdef:=pd;
  525. { main proc does always a call e.g. to init system unit }
  526. include(result.flags,pi_do_call);
  527. end;
  528. procedure release_main_proc(pi:tcgprocinfo);
  529. begin
  530. { remove localst as it was replaced by staticsymtable }
  531. pi.procdef.localst:=nil;
  532. { remove procinfo }
  533. current_module.procinfo:=nil;
  534. pi.free;
  535. pi:=nil;
  536. end;
  537. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  538. procedure maybe_load_got;
  539. {$if defined(i386) or defined (sparc)}
  540. var
  541. gotvarsym : tstaticvarsym;
  542. {$endif i386 or sparc}
  543. begin
  544. {$if defined(i386) or defined(sparc)}
  545. if (cs_create_pic in current_settings.moduleswitches) and
  546. (tf_pic_uses_got in target_info.flags) then
  547. begin
  548. { insert symbol for got access in assembler code}
  549. gotvarsym:=cstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  550. vs_value,voidpointertype,[vo_is_external]);
  551. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  552. current_module.localsymtable.insert(gotvarsym);
  553. { avoid unnecessary warnings }
  554. gotvarsym.varstate:=vs_read;
  555. gotvarsym.refs:=1;
  556. end;
  557. {$endif i386 or sparc}
  558. end;
  559. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  560. begin
  561. { create procdef }
  562. case flag of
  563. uf_init :
  564. begin
  565. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  566. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  567. end;
  568. uf_finalize :
  569. begin
  570. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  571. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  572. if (not current_module.is_unit) then
  573. result.procdef.aliasnames.insert('PASCALFINALIZE');
  574. end;
  575. else
  576. internalerror(200304253);
  577. end;
  578. result.code:=cnothingnode.create;
  579. end;
  580. procedure copy_macro(p:TObject; arg:pointer);
  581. begin
  582. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  583. end;
  584. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  585. var
  586. deprecated_seen,
  587. last_is_deprecated:boolean;
  588. begin
  589. try_consume_hintdirective:=false;
  590. deprecated_seen:=false;
  591. repeat
  592. last_is_deprecated:=false;
  593. case idtoken of
  594. _LIBRARY :
  595. begin
  596. include(moduleopt,mo_hint_library);
  597. try_consume_hintdirective:=true;
  598. end;
  599. _DEPRECATED :
  600. begin
  601. { allow deprecated only once }
  602. if deprecated_seen then
  603. break;
  604. include(moduleopt,mo_hint_deprecated);
  605. try_consume_hintdirective:=true;
  606. last_is_deprecated:=true;
  607. deprecated_seen:=true;
  608. end;
  609. _EXPERIMENTAL :
  610. begin
  611. include(moduleopt,mo_hint_experimental);
  612. try_consume_hintdirective:=true;
  613. end;
  614. _PLATFORM :
  615. begin
  616. include(moduleopt,mo_hint_platform);
  617. try_consume_hintdirective:=true;
  618. end;
  619. _UNIMPLEMENTED :
  620. begin
  621. include(moduleopt,mo_hint_unimplemented);
  622. try_consume_hintdirective:=true;
  623. end;
  624. else
  625. break;
  626. end;
  627. consume(Token);
  628. { handle deprecated message }
  629. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  630. begin
  631. if deprecatedmsg<>nil then
  632. internalerror(201001221);
  633. if token=_CSTRING then
  634. deprecatedmsg:=stringdup(cstringpattern)
  635. else
  636. deprecatedmsg:=stringdup(pattern);
  637. consume(token);
  638. include(moduleopt,mo_has_deprecated_msg);
  639. end;
  640. until false;
  641. end;
  642. {$ifdef jvm}
  643. procedure addmoduleclass;
  644. var
  645. def: tobjectdef;
  646. typesym: ttypesym;
  647. begin
  648. { java_jlobject may not have been parsed yet (system unit); in any
  649. case, we only use this to refer to the class type, so inheritance
  650. does not matter }
  651. def:=cobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil);
  652. include(def.objectoptions,oo_is_external);
  653. include(def.objectoptions,oo_is_sealed);
  654. def.objextname:=stringdup(current_module.realmodulename^);
  655. typesym:=ctypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  656. symtablestack.top.insert(typesym);
  657. end;
  658. {$endif jvm}
  659. type
  660. tfinishstate=record
  661. init_procinfo:tcgprocinfo;
  662. end;
  663. pfinishstate=^tfinishstate;
  664. procedure finish_unit(module:tmodule;immediate:boolean);forward;
  665. function proc_unit:boolean;
  666. var
  667. main_file: tinputfile;
  668. s1,s2 : ^string; {Saves stack space}
  669. init_procinfo : tcgprocinfo;
  670. unitname : ansistring;
  671. unitname8 : string[8];
  672. i,j : longint;
  673. finishstate:pfinishstate;
  674. globalstate:pglobalstate;
  675. consume_semicolon_after_uses:boolean;
  676. begin
  677. result:=true;
  678. init_procinfo:=nil;
  679. if m_mac in current_settings.modeswitches then
  680. current_module.mode_switch_allowed:= false;
  681. consume(_UNIT);
  682. if compile_level=1 then
  683. Status.IsExe:=false;
  684. unitname:=orgpattern;
  685. consume(_ID);
  686. while token=_POINT do
  687. begin
  688. consume(_POINT);
  689. unitname:=unitname+'.'+orgpattern;
  690. consume(_ID);
  691. end;
  692. { create filenames and unit name }
  693. main_file := current_scanner.inputfile;
  694. while assigned(main_file.next) do
  695. main_file := main_file.next;
  696. new(s1);
  697. s1^:=current_module.modulename^;
  698. current_module.SetFileName(main_file.path+main_file.name,true);
  699. current_module.SetModuleName(unitname);
  700. { check for system unit }
  701. new(s2);
  702. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  703. unitname8:=copy(current_module.modulename^,1,8);
  704. if (cs_check_unit_name in current_settings.globalswitches) and
  705. (
  706. not(
  707. (current_module.modulename^=s2^) or
  708. (
  709. (length(current_module.modulename^)>8) and
  710. (unitname8=s2^)
  711. )
  712. )
  713. or
  714. (
  715. (length(s1^)>8) and
  716. (s1^<>current_module.modulename^)
  717. )
  718. ) then
  719. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  720. if (current_module.modulename^='SYSTEM') then
  721. include(current_settings.moduleswitches,cs_compilesystem);
  722. dispose(s2);
  723. dispose(s1);
  724. if (target_info.system in systems_unit_program_exports) then
  725. exportlib.preparelib(current_module.realmodulename^);
  726. { parse hint directives }
  727. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  728. consume(_SEMICOLON);
  729. { handle the global switches, do this before interface, because after interface has been
  730. read, all following directives are parsed as well }
  731. setupglobalswitches;
  732. consume(_INTERFACE);
  733. { global switches are read, so further changes aren't allowed }
  734. current_module.in_global:=false;
  735. message1(unit_u_loading_interface_units,current_module.modulename^);
  736. { update status }
  737. status.currentmodule:=current_module.realmodulename^;
  738. { maybe turn off m_objpas if we are compiling objpas }
  739. if (current_module.modulename^='OBJPAS') then
  740. exclude(current_settings.modeswitches,m_objpas);
  741. { maybe turn off m_mac if we are compiling macpas }
  742. if (current_module.modulename^='MACPAS') then
  743. exclude(current_settings.modeswitches,m_mac);
  744. parse_only:=true;
  745. { generate now the global symboltable,
  746. define first as local to overcome dependency conflicts }
  747. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  748. { insert unitsym of this unit to prevent other units having
  749. the same name }
  750. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  751. { load default units, like the system unit }
  752. loaddefaultunits;
  753. { insert qualifier for the system unit (allows system.writeln) }
  754. if not(cs_compilesystem in current_settings.moduleswitches) and
  755. (token=_USES) then
  756. begin
  757. loadunits(nil);
  758. { has it been compiled at a higher level ?}
  759. if current_module.state=ms_compiled then
  760. exit;
  761. consume_semicolon_after_uses:=true;
  762. end
  763. else
  764. consume_semicolon_after_uses:=false;
  765. { move the global symtable from the temporary local to global }
  766. current_module.globalsymtable:=current_module.localsymtable;
  767. current_module.localsymtable:=nil;
  768. { number all units, so we know if a unit is used by this unit or
  769. needs to be added implicitly }
  770. current_module.updatemaps;
  771. { consume the semicolon after maps have been updated else conditional compiling expressions
  772. might cause internal errors, see tw8611 }
  773. if consume_semicolon_after_uses then
  774. consume(_SEMICOLON);
  775. { create whole program optimisation information (may already be
  776. updated in the interface, e.g., in case of classrefdef typed
  777. constants }
  778. current_module.wpoinfo:=tunitwpoinfo.create;
  779. { ... parse the declarations }
  780. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  781. symtablestack.push(current_module.globalsymtable);
  782. {$ifdef jvm}
  783. { fake classdef to represent the class corresponding to the unit }
  784. addmoduleclass;
  785. {$endif}
  786. read_interface_declarations;
  787. { Export macros defined in the interface for macpas. The macros
  788. are put in the globalmacrosymtable that will only be used by other
  789. units. The current unit continues to use the localmacrosymtable }
  790. if (m_mac in current_settings.modeswitches) then
  791. begin
  792. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  793. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  794. end;
  795. { leave when we got an error }
  796. if (Errorcount>0) and not status.skip_error then
  797. begin
  798. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  799. status.skip_error:=true;
  800. symtablestack.pop(current_module.globalsymtable);
  801. exit;
  802. end;
  803. { Our interface is compiled, generate CRC and switch to implementation }
  804. if not(cs_compilesystem in current_settings.moduleswitches) and
  805. (Errorcount=0) then
  806. tppumodule(current_module).getppucrc;
  807. current_module.in_interface:=false;
  808. current_module.interface_compiled:=true;
  809. { First reload all units depending on our interface, we need to do this
  810. in the implementation part to prevent erroneous circular references }
  811. tppumodule(current_module).setdefgeneration;
  812. tppumodule(current_module).reload_flagged_units;
  813. { Parse the implementation section }
  814. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  815. current_module.interface_only:=true
  816. else
  817. current_module.interface_only:=false;
  818. parse_only:=false;
  819. { create static symbol table }
  820. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  821. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  822. maybe_load_got;
  823. if not current_module.interface_only then
  824. begin
  825. consume(_IMPLEMENTATION);
  826. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  827. { Read the implementation units }
  828. if token=_USES then
  829. begin
  830. loadunits(current_module.globalsymtable);
  831. consume(_SEMICOLON);
  832. end;
  833. end;
  834. if current_module.state=ms_compiled then
  835. begin
  836. symtablestack.pop(current_module.globalsymtable);
  837. exit;
  838. end;
  839. { All units are read, now give them a number }
  840. current_module.updatemaps;
  841. symtablestack.push(current_module.localsymtable);
  842. if not current_module.interface_only then
  843. begin
  844. Message1(parser_u_parsing_implementation,current_module.modulename^);
  845. if current_module.in_interface then
  846. internalerror(200212285);
  847. { Compile the unit }
  848. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  849. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  850. init_procinfo.parse_body;
  851. { save file pos for debuginfo }
  852. current_module.mainfilepos:=init_procinfo.entrypos;
  853. end;
  854. { remove all units that we are waiting for that are already waiting for
  855. us => breaking up circles }
  856. for i:=0 to current_module.waitingunits.count-1 do
  857. for j:=current_module.waitingforunit.count-1 downto 0 do
  858. if current_module.waitingunits[i]=current_module.waitingforunit[j] then
  859. current_module.waitingforunit.delete(j);
  860. {$ifdef DEBUG_UNITWAITING}
  861. Writeln('Units waiting for ', current_module.modulename^, ': ',
  862. current_module.waitingforunit.Count);
  863. {$endif}
  864. result:=current_module.waitingforunit.count=0;
  865. { save all information that is needed for finishing the unit }
  866. New(finishstate);
  867. finishstate^.init_procinfo:=init_procinfo;
  868. current_module.finishstate:=finishstate;
  869. if result then
  870. finish_unit(current_module,true)
  871. else
  872. begin
  873. { save the current state, so the parsing can continue where we left
  874. of here }
  875. New(globalstate);
  876. save_global_state(globalstate^,true);
  877. current_module.globalstate:=globalstate;
  878. end;
  879. end;
  880. procedure finish_unit(module:tmodule;immediate:boolean);
  881. function is_assembler_generated:boolean;
  882. var
  883. hal : tasmlisttype;
  884. begin
  885. result:=false;
  886. if Errorcount=0 then
  887. begin
  888. for hal:=low(TasmlistType) to high(TasmlistType) do
  889. if not current_asmdata.asmlists[hal].empty then
  890. begin
  891. result:=true;
  892. exit;
  893. end;
  894. end;
  895. end;
  896. procedure module_is_done;inline;
  897. begin
  898. dispose(pglobalstate(current_module.globalstate));
  899. current_module.globalstate:=nil;
  900. dispose(pfinishstate(current_module.finishstate));
  901. current_module.finishstate:=nil;
  902. end;
  903. var
  904. {$ifdef EXTDEBUG}
  905. store_crc,
  906. {$endif EXTDEBUG}
  907. store_interface_crc,
  908. store_indirect_crc: cardinal;
  909. force_init_final : boolean;
  910. init_procinfo,
  911. finalize_procinfo : tcgprocinfo;
  912. i : longint;
  913. ag : boolean;
  914. finishstate : tfinishstate;
  915. globalstate : tglobalstate;
  916. waitingmodule : tmodule;
  917. begin
  918. fillchar(globalstate,sizeof(tglobalstate),0);
  919. if not immediate then
  920. begin
  921. {$ifdef DEBUG_UNITWAITING}
  922. writeln('finishing waiting unit ''', module.modulename^, '''');
  923. {$endif DEBUG_UNITWAITING}
  924. { restore the state when we stopped working on the unit }
  925. save_global_state(globalstate,true);
  926. if not assigned(module.globalstate) then
  927. internalerror(2012091802);
  928. restore_global_state(pglobalstate(module.globalstate)^,true);
  929. end;
  930. { current_module is now module }
  931. if not assigned(current_module.finishstate) then
  932. internalerror(2012091801);
  933. finishstate:=pfinishstate(current_module.finishstate)^;
  934. finalize_procinfo:=nil;
  935. init_procinfo:=finishstate.init_procinfo;
  936. { Generate specializations of objectdefs methods }
  937. generate_specialization_procs;
  938. { add implementations for synthetic method declarations added by
  939. the compiler }
  940. add_synthetic_method_implementations(current_module.globalsymtable);
  941. add_synthetic_method_implementations(current_module.localsymtable);
  942. { if the unit contains ansi/widestrings, initialization and
  943. finalization code must be forced }
  944. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  945. tstaticsymtable(current_module.localsymtable).needs_init_final;
  946. { should we force unit initialization? }
  947. { this is a hack, but how can it be done better ? }
  948. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  949. is it needed at all? (Sergei) }
  950. { it's needed in case cnodeutils.force_init = true }
  951. if (force_init_final or cnodeutils.force_init) and
  952. assigned(init_procinfo) and
  953. has_no_code(init_procinfo.code) then
  954. begin
  955. { first release the not used init procinfo }
  956. if assigned(init_procinfo) then
  957. release_main_proc(init_procinfo);
  958. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  959. end;
  960. { finalize? }
  961. if not current_module.interface_only and (token=_FINALIZATION) then
  962. begin
  963. { Compile the finalize }
  964. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  965. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  966. finalize_procinfo.parse_body;
  967. end
  968. else if force_init_final or cnodeutils.force_final then
  969. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  970. { Now both init and finalize bodies are read and it is known
  971. which variables are used in both init and finalize we can now
  972. generate the code. This is required to prevent putting a variable in
  973. a register that is also used in the finalize body (PFV) }
  974. if assigned(init_procinfo) then
  975. begin
  976. if (force_init_final or cnodeutils.force_init) or
  977. not(has_no_code(init_procinfo.code)) then
  978. begin
  979. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  980. init_procinfo.generate_code;
  981. current_module.flags:=current_module.flags or uf_init;
  982. end;
  983. init_procinfo.resetprocdef;
  984. release_main_proc(init_procinfo);
  985. end;
  986. if assigned(finalize_procinfo) then
  987. begin
  988. if force_init_final or
  989. cnodeutils.force_init or
  990. not(has_no_code(finalize_procinfo.code)) then
  991. begin
  992. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  993. finalize_procinfo.generate_code;
  994. current_module.flags:=current_module.flags or uf_finalize;
  995. end;
  996. finalize_procinfo.resetprocdef;
  997. release_main_proc(finalize_procinfo);
  998. end;
  999. symtablestack.pop(current_module.localsymtable);
  1000. symtablestack.pop(current_module.globalsymtable);
  1001. { the last char should always be a point }
  1002. consume(_POINT);
  1003. { reset wpo flags for all defs }
  1004. reset_all_defs;
  1005. if (Errorcount=0) then
  1006. begin
  1007. { tests, if all (interface) forwards are resolved }
  1008. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1009. { check if all private fields are used }
  1010. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1011. { test static symtable }
  1012. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1013. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1014. tstoredsymtable(current_module.localsymtable).check_forwards;
  1015. tstoredsymtable(current_module.localsymtable).checklabels;
  1016. { used units }
  1017. current_module.allunitsused;
  1018. end;
  1019. { leave when we got an error }
  1020. if (Errorcount>0) and not status.skip_error then
  1021. begin
  1022. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1023. status.skip_error:=true;
  1024. module_is_done;
  1025. if not immediate then
  1026. restore_global_state(globalstate,true);
  1027. exit;
  1028. end;
  1029. { if an Objective-C module, generate rtti and module info }
  1030. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1031. { do we need to add the variants unit? }
  1032. maybeloadvariantsunit;
  1033. { generate rtti/init tables }
  1034. write_persistent_type_info(current_module.globalsymtable,true);
  1035. write_persistent_type_info(current_module.localsymtable,false);
  1036. { Tables }
  1037. cnodeutils.InsertThreadvars;
  1038. { Resource strings }
  1039. GenerateResourceStrings;
  1040. { Widestring typed constants }
  1041. cnodeutils.InsertWideInits;
  1042. { Resourcestring references }
  1043. cnodeutils.InsertResStrInits;
  1044. { generate debuginfo }
  1045. if (cs_debuginfo in current_settings.moduleswitches) then
  1046. current_debuginfo.inserttypeinfo;
  1047. { generate imports }
  1048. if current_module.ImportLibraryList.Count>0 then
  1049. importlib.generatelib;
  1050. { insert own objectfile, or say that it's in a library
  1051. (no check for an .o when loading) }
  1052. ag:=is_assembler_generated;
  1053. if ag then
  1054. insertobjectfile
  1055. else
  1056. begin
  1057. current_module.flags:=current_module.flags or uf_no_link;
  1058. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1059. end;
  1060. if ag then
  1061. begin
  1062. { create callframe info }
  1063. create_dwarf_frame;
  1064. { assemble }
  1065. create_objectfile;
  1066. end;
  1067. { Write out the ppufile after the object file has been created }
  1068. store_interface_crc:=current_module.interface_crc;
  1069. store_indirect_crc:=current_module.indirect_crc;
  1070. {$ifdef EXTDEBUG}
  1071. store_crc:=current_module.crc;
  1072. {$endif EXTDEBUG}
  1073. if (Errorcount=0) then
  1074. tppumodule(current_module).writeppu;
  1075. if not(cs_compilesystem in current_settings.moduleswitches) then
  1076. begin
  1077. if store_interface_crc<>current_module.interface_crc then
  1078. Message1(unit_u_interface_crc_changed,current_module.ppufilename);
  1079. if store_indirect_crc<>current_module.indirect_crc then
  1080. Message1(unit_u_indirect_crc_changed,current_module.ppufilename);
  1081. end;
  1082. {$ifdef EXTDEBUG}
  1083. if not(cs_compilesystem in current_settings.moduleswitches) then
  1084. if (store_crc<>current_module.crc) and simplify_ppu then
  1085. Message1(unit_u_implementation_crc_changed,current_module.ppufilename);
  1086. {$endif EXTDEBUG}
  1087. { release local symtables that are not needed anymore }
  1088. free_localsymtables(current_module.globalsymtable);
  1089. free_localsymtables(current_module.localsymtable);
  1090. { leave when we got an error }
  1091. if (Errorcount>0) and not status.skip_error then
  1092. begin
  1093. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1094. status.skip_error:=true;
  1095. module_is_done;
  1096. if not immediate then
  1097. restore_global_state(globalstate,true);
  1098. exit;
  1099. end;
  1100. {$ifdef debug_devirt}
  1101. { print out all instantiated class/object types }
  1102. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1103. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1104. begin
  1105. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1106. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1107. objectdef:
  1108. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1109. odt_object:
  1110. writeln(' (object)');
  1111. odt_class:
  1112. writeln(' (class)');
  1113. else
  1114. internalerror(2008101103);
  1115. end;
  1116. else
  1117. internalerror(2008101104);
  1118. end;
  1119. end;
  1120. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1121. begin
  1122. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1123. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1124. objectdef:
  1125. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1126. odt_class:
  1127. writeln(' (classrefdef)');
  1128. else
  1129. internalerror(2008101105);
  1130. end
  1131. else
  1132. internalerror(2008101102);
  1133. end;
  1134. end;
  1135. {$endif debug_devirt}
  1136. Message1(unit_u_finished_compiling,current_module.modulename^);
  1137. module_is_done;
  1138. if not immediate then
  1139. restore_global_state(globalstate,true);
  1140. for i:=0 to module.waitingunits.count-1 do
  1141. begin
  1142. waitingmodule:=tmodule(module.waitingunits[i]);
  1143. waitingmodule.waitingforunit.remove(module);
  1144. { only finish the module if it isn't already finished }
  1145. if (waitingmodule.waitingforunit.count=0) and
  1146. assigned(waitingmodule.finishstate) then
  1147. begin
  1148. finish_unit(waitingmodule,false);
  1149. waitingmodule.end_of_parsing;
  1150. end;
  1151. end;
  1152. end;
  1153. procedure proc_package;
  1154. var
  1155. main_file : tinputfile;
  1156. hp,hp2 : tmodule;
  1157. {finalize_procinfo,
  1158. init_procinfo,
  1159. main_procinfo : tcgprocinfo;}
  1160. force_init_final : boolean;
  1161. uu : tused_unit;
  1162. module_name: ansistring;
  1163. begin
  1164. Status.IsPackage:=true;
  1165. Status.IsExe:=true;
  1166. parse_only:=false;
  1167. {main_procinfo:=nil;
  1168. init_procinfo:=nil;
  1169. finalize_procinfo:=nil;}
  1170. if not (tf_supports_packages in target_info.flags) then
  1171. message1(parser_e_packages_not_supported,target_info.name);
  1172. if not RelocSectionSetExplicitly then
  1173. RelocSection:=true;
  1174. { Relocation works only without stabs under Windows when }
  1175. { external linker (LD) is used. LD generates relocs for }
  1176. { stab sections which is not loaded in memory. It causes }
  1177. { AV error when DLL is loaded and relocation is needed. }
  1178. { Internal linker does not have this problem. }
  1179. if RelocSection and
  1180. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1181. (cs_link_extern in current_settings.globalswitches) then
  1182. begin
  1183. include(current_settings.globalswitches,cs_link_strip);
  1184. { Warning stabs info does not work with reloc section !! }
  1185. if (cs_debuginfo in current_settings.moduleswitches) and
  1186. (target_dbg.id=dbg_stabs) then
  1187. begin
  1188. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1189. Message(parser_w_parser_win32_debug_needs_WN);
  1190. exclude(current_settings.moduleswitches,cs_debuginfo);
  1191. end;
  1192. end;
  1193. { get correct output names }
  1194. main_file := current_scanner.inputfile;
  1195. while assigned(main_file.next) do
  1196. main_file := main_file.next;
  1197. current_module.SetFileName(main_file.path+main_file.name,true);
  1198. { consume _PACKAGE word }
  1199. consume(_ID);
  1200. module_name:=orgpattern;
  1201. consume(_ID);
  1202. while token=_POINT do
  1203. begin
  1204. consume(_POINT);
  1205. module_name:=module_name+'.'+orgpattern;
  1206. consume(_ID);
  1207. end;
  1208. current_module.setmodulename(module_name);
  1209. current_module.ispackage:=true;
  1210. exportlib.preparelib(module_name);
  1211. if tf_library_needs_pic in target_info.flags then
  1212. include(current_settings.moduleswitches,cs_create_pic);
  1213. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1214. read, all following directives are parsed as well }
  1215. setupglobalswitches;
  1216. consume(_SEMICOLON);
  1217. { global switches are read, so further changes aren't allowed }
  1218. current_module.in_global:=false;
  1219. { set implementation flag }
  1220. current_module.in_interface:=false;
  1221. current_module.interface_compiled:=true;
  1222. { insert after the unit symbol tables the static symbol table }
  1223. { of the program }
  1224. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1225. {Load the units used by the program we compile.}
  1226. if token=_REQUIRES then
  1227. begin
  1228. end;
  1229. {Load the units used by the program we compile.}
  1230. if (token=_ID) and (idtoken=_CONTAINS) then
  1231. begin
  1232. { consume _CONTAINS word }
  1233. consume(_ID);
  1234. while true do
  1235. begin
  1236. if token=_ID then
  1237. begin
  1238. module_name:=pattern;
  1239. consume(_ID);
  1240. while token=_POINT do
  1241. begin
  1242. consume(_POINT);
  1243. module_name:=module_name+'.'+orgpattern;
  1244. consume(_ID);
  1245. end;
  1246. AddUnit(module_name);
  1247. end
  1248. else
  1249. consume(_ID);
  1250. if token=_COMMA then
  1251. consume(_COMMA)
  1252. else break;
  1253. end;
  1254. consume(_SEMICOLON);
  1255. end;
  1256. { All units are read, now give them a number }
  1257. current_module.updatemaps;
  1258. {Insert the name of the main program into the symbol table.}
  1259. if current_module.realmodulename^<>'' then
  1260. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1261. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1262. symtablestack.push(current_module.localsymtable);
  1263. { create whole program optimisation information }
  1264. current_module.wpoinfo:=tunitwpoinfo.create;
  1265. { should we force unit initialization? }
  1266. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1267. if force_init_final or cnodeutils.force_init then
  1268. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1269. { Add symbol to the exports section for win32 so smartlinking a
  1270. DLL will include the edata section }
  1271. if assigned(exportlib) and
  1272. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1273. ((current_module.flags and uf_has_exports)<>0) then
  1274. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1275. { all labels must be defined before generating code }
  1276. if Errorcount=0 then
  1277. tstoredsymtable(current_module.localsymtable).checklabels;
  1278. symtablestack.pop(current_module.localsymtable);
  1279. { consume the last point }
  1280. consume(_END);
  1281. consume(_POINT);
  1282. if (Errorcount=0) then
  1283. begin
  1284. { test static symtable }
  1285. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1286. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1287. tstoredsymtable(current_module.localsymtable).check_forwards;
  1288. current_module.allunitsused;
  1289. end;
  1290. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1291. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1292. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1293. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1294. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1295. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1296. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1297. { leave when we got an error }
  1298. if (Errorcount>0) and not status.skip_error then
  1299. begin
  1300. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1301. status.skip_error:=true;
  1302. exit;
  1303. end;
  1304. { remove all unused units, this happends when units are removed
  1305. from the uses clause in the source and the ppu was already being loaded }
  1306. hp:=tmodule(loaded_units.first);
  1307. while assigned(hp) do
  1308. begin
  1309. hp2:=hp;
  1310. hp:=tmodule(hp.next);
  1311. if hp2.is_unit and
  1312. not assigned(hp2.globalsymtable) then
  1313. loaded_units.remove(hp2);
  1314. end;
  1315. { force exports }
  1316. uu:=tused_unit(usedunits.first);
  1317. while assigned(uu) do
  1318. begin
  1319. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1320. { check localsymtable for exports too to get public symbols }
  1321. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1322. { create special exports }
  1323. if (uu.u.flags and uf_init)<>0 then
  1324. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1325. if (uu.u.flags and uf_finalize)<>0 then
  1326. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1327. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1328. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1329. uu:=tused_unit(uu.next);
  1330. end;
  1331. {$ifdef arm}
  1332. { Insert .pdata section for arm-wince.
  1333. It is needed for exception handling. }
  1334. if target_info.system in [system_arm_wince] then
  1335. InsertPData;
  1336. {$endif arm}
  1337. { generate debuginfo }
  1338. if (cs_debuginfo in current_settings.moduleswitches) then
  1339. current_debuginfo.inserttypeinfo;
  1340. exportlib.generatelib;
  1341. { write all our exports to the import library,
  1342. needs to be done after exportlib.generatelib; }
  1343. createimportlibfromexports;
  1344. { generate imports }
  1345. if current_module.ImportLibraryList.Count>0 then
  1346. importlib.generatelib;
  1347. { Reference all DEBUGINFO sections from the main .fpc section }
  1348. if (cs_debuginfo in current_settings.moduleswitches) then
  1349. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1350. { insert own objectfile }
  1351. insertobjectfile;
  1352. { assemble and link }
  1353. create_objectfile;
  1354. { We might need the symbols info if not using
  1355. the default do_extractsymbolinfo
  1356. which is a dummy function PM }
  1357. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1358. { release all local symtables that are not needed anymore }
  1359. if (not needsymbolinfo) then
  1360. free_localsymtables(current_module.localsymtable);
  1361. { leave when we got an error }
  1362. if (Errorcount>0) and not status.skip_error then
  1363. begin
  1364. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1365. status.skip_error:=true;
  1366. exit;
  1367. end;
  1368. if (not current_module.is_unit) then
  1369. begin
  1370. { finally rewrite all units included into the package }
  1371. uu:=tused_unit(usedunits.first);
  1372. while assigned(uu) do
  1373. begin
  1374. RewritePPU(uu.u.ppufilename,uu.u.ppufilename);
  1375. uu:=tused_unit(uu.next);
  1376. end;
  1377. { create the executable when we are at level 1 }
  1378. if (compile_level=1) then
  1379. begin
  1380. { create global resource file by collecting all resource files }
  1381. CollectResourceFiles;
  1382. { write .def file }
  1383. if (cs_link_deffile in current_settings.globalswitches) then
  1384. deffile.writefile;
  1385. { insert all .o files from all loaded units and
  1386. unload the units, we don't need them anymore.
  1387. Keep the current_module because that is still needed }
  1388. hp:=tmodule(loaded_units.first);
  1389. while assigned(hp) do
  1390. begin
  1391. { the package itself contains no code so far }
  1392. linker.AddModuleFiles(hp);
  1393. hp2:=tmodule(hp.next);
  1394. if (hp<>current_module) and
  1395. (not needsymbolinfo) then
  1396. begin
  1397. loaded_units.remove(hp);
  1398. hp.free;
  1399. end;
  1400. hp:=hp2;
  1401. end;
  1402. linker.MakeSharedLibrary
  1403. end;
  1404. { Give Fatal with error count for linker errors }
  1405. if (Errorcount>0) and not status.skip_error then
  1406. begin
  1407. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1408. status.skip_error:=true;
  1409. end;
  1410. end;
  1411. end;
  1412. procedure proc_program(islibrary : boolean);
  1413. type
  1414. TProgramParam = record
  1415. name : ansistring;
  1416. nr : dword;
  1417. end;
  1418. var
  1419. main_file : tinputfile;
  1420. hp,hp2 : tmodule;
  1421. finalize_procinfo,
  1422. init_procinfo,
  1423. main_procinfo : tcgprocinfo;
  1424. force_init_final : boolean;
  1425. resources_used : boolean;
  1426. program_name : ansistring;
  1427. consume_semicolon_after_uses : boolean;
  1428. ps : tstaticvarsym;
  1429. paramnum : longint;
  1430. textsym : ttypesym;
  1431. sc : array of TProgramParam;
  1432. i : Longint;
  1433. begin
  1434. DLLsource:=islibrary;
  1435. Status.IsLibrary:=IsLibrary;
  1436. Status.IsPackage:=false;
  1437. Status.IsExe:=true;
  1438. parse_only:=false;
  1439. main_procinfo:=nil;
  1440. init_procinfo:=nil;
  1441. finalize_procinfo:=nil;
  1442. resources_used:=false;
  1443. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  1444. sc:=nil;
  1445. { DLL defaults to create reloc info }
  1446. if islibrary then
  1447. begin
  1448. if not RelocSectionSetExplicitly then
  1449. RelocSection:=true;
  1450. end;
  1451. { Relocation works only without stabs under Windows when }
  1452. { external linker (LD) is used. LD generates relocs for }
  1453. { stab sections which is not loaded in memory. It causes }
  1454. { AV error when DLL is loaded and relocation is needed. }
  1455. { Internal linker does not have this problem. }
  1456. if RelocSection and
  1457. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1458. (cs_link_extern in current_settings.globalswitches) then
  1459. begin
  1460. include(current_settings.globalswitches,cs_link_strip);
  1461. { Warning stabs info does not work with reloc section !! }
  1462. if (cs_debuginfo in current_settings.moduleswitches) and
  1463. (target_dbg.id=dbg_stabs) then
  1464. begin
  1465. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1466. Message(parser_w_parser_win32_debug_needs_WN);
  1467. exclude(current_settings.moduleswitches,cs_debuginfo);
  1468. end;
  1469. end;
  1470. { get correct output names }
  1471. main_file := current_scanner.inputfile;
  1472. while assigned(main_file.next) do
  1473. main_file := main_file.next;
  1474. current_module.SetFileName(main_file.path+main_file.name,true);
  1475. if islibrary then
  1476. begin
  1477. consume(_LIBRARY);
  1478. program_name:=orgpattern;
  1479. consume(_ID);
  1480. while token=_POINT do
  1481. begin
  1482. consume(_POINT);
  1483. program_name:=program_name+'.'+orgpattern;
  1484. consume(_ID);
  1485. end;
  1486. current_module.setmodulename(program_name);
  1487. current_module.islibrary:=true;
  1488. exportlib.preparelib(program_name);
  1489. if tf_library_needs_pic in target_info.flags then
  1490. include(current_settings.moduleswitches,cs_create_pic);
  1491. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1492. read, all following directives are parsed as well }
  1493. setupglobalswitches;
  1494. consume(_SEMICOLON);
  1495. end
  1496. else
  1497. { is there an program head ? }
  1498. if token=_PROGRAM then
  1499. begin
  1500. consume(_PROGRAM);
  1501. program_name:=orgpattern;
  1502. consume(_ID);
  1503. while token=_POINT do
  1504. begin
  1505. consume(_POINT);
  1506. program_name:=program_name+'.'+orgpattern;
  1507. consume(_ID);
  1508. end;
  1509. current_module.setmodulename(program_name);
  1510. if (target_info.system in systems_unit_program_exports) then
  1511. exportlib.preparelib(program_name);
  1512. if token=_LKLAMMER then
  1513. begin
  1514. consume(_LKLAMMER);
  1515. paramnum:=1;
  1516. repeat
  1517. if m_iso in current_settings.modeswitches then
  1518. begin
  1519. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  1520. begin
  1521. { the symtablestack is not setup here, so text must be created later on }
  1522. Setlength(sc,length(sc)+1);
  1523. with sc[high(sc)] do
  1524. begin
  1525. name:=pattern;
  1526. nr:=paramnum;
  1527. end;
  1528. inc(paramnum);
  1529. end;
  1530. end;
  1531. consume(_ID);
  1532. until not try_to_consume(_COMMA);
  1533. consume(_RKLAMMER);
  1534. end;
  1535. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1536. read, all following directives are parsed as well }
  1537. setupglobalswitches;
  1538. consume(_SEMICOLON);
  1539. end
  1540. else
  1541. begin
  1542. if (target_info.system in systems_unit_program_exports) then
  1543. exportlib.preparelib(current_module.realmodulename^);
  1544. { setup things using the switches }
  1545. setupglobalswitches;
  1546. end;
  1547. { global switches are read, so further changes aren't allowed }
  1548. current_module.in_global:=false;
  1549. { set implementation flag }
  1550. current_module.in_interface:=false;
  1551. current_module.interface_compiled:=true;
  1552. { insert after the unit symbol tables the static symbol table
  1553. of the program }
  1554. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1555. { load standard units (system,objpas,profile unit) }
  1556. loaddefaultunits;
  1557. { Load units provided on the command line }
  1558. loadautounits;
  1559. { insert iso program parameters }
  1560. if length(sc)>0 then
  1561. begin
  1562. textsym:=search_system_type('TEXT');
  1563. if not(assigned(textsym)) then
  1564. internalerror(2013011201);
  1565. for i:=0 to high(sc) do
  1566. begin
  1567. ps:=cstaticvarsym.create(sc[i].name,vs_value,textsym.typedef,[]);
  1568. ps.isoindex:=sc[i].nr;
  1569. current_module.localsymtable.insert(ps,true);
  1570. cnodeutils.insertbssdata(tstaticvarsym(ps));
  1571. end;
  1572. end;
  1573. { Load the units used by the program we compile. }
  1574. if token=_USES then
  1575. begin
  1576. loadunits(nil);
  1577. consume_semicolon_after_uses:=true;
  1578. end
  1579. else
  1580. consume_semicolon_after_uses:=false;
  1581. { All units are read, now give them a number }
  1582. current_module.updatemaps;
  1583. { consume the semicolon after maps have been updated else conditional compiling expressions
  1584. might cause internal errors, see tw8611 }
  1585. if consume_semicolon_after_uses then
  1586. consume(_SEMICOLON);
  1587. {Insert the name of the main program into the symbol table.}
  1588. if current_module.realmodulename^<>'' then
  1589. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1590. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1591. symtablestack.push(current_module.localsymtable);
  1592. {$ifdef jvm}
  1593. { fake classdef to represent the class corresponding to the unit }
  1594. addmoduleclass;
  1595. {$endif}
  1596. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1597. maybe_load_got;
  1598. { create whole program optimisation information }
  1599. current_module.wpoinfo:=tunitwpoinfo.create;
  1600. { The program intialization needs an alias, so it can be called
  1601. from the bootstrap code.}
  1602. if islibrary then
  1603. begin
  1604. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1605. { Win32 startup code needs a single name }
  1606. if not(target_info.system in (systems_darwin+systems_aix)) then
  1607. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1608. else
  1609. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1610. end
  1611. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin+systems_aix)) then
  1612. begin
  1613. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1614. end
  1615. else
  1616. begin
  1617. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1618. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1619. end;
  1620. main_procinfo.parse_body;
  1621. { save file pos for debuginfo }
  1622. current_module.mainfilepos:=main_procinfo.entrypos;
  1623. { Generate specializations of objectdefs methods }
  1624. generate_specialization_procs;
  1625. { add implementations for synthetic method declarations added by
  1626. the compiler }
  1627. add_synthetic_method_implementations(current_module.localsymtable);
  1628. { should we force unit initialization? }
  1629. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1630. if force_init_final or cnodeutils.force_init then
  1631. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1632. { Add symbol to the exports section for win32 so smartlinking a
  1633. DLL will include the edata section }
  1634. if assigned(exportlib) and
  1635. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1636. ((current_module.flags and uf_has_exports)<>0) then
  1637. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1638. { finalize? }
  1639. if token=_FINALIZATION then
  1640. begin
  1641. { Parse the finalize }
  1642. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1643. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1644. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1645. finalize_procinfo.parse_body;
  1646. end
  1647. else
  1648. if force_init_final or cnodeutils.force_final then
  1649. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1650. { the finalization routine of libraries is generic (and all libraries need to }
  1651. { be finalized, so they can finalize any units they use }
  1652. if (islibrary) then
  1653. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1654. { all labels must be defined before generating code }
  1655. if Errorcount=0 then
  1656. tstoredsymtable(current_module.localsymtable).checklabels;
  1657. { See remark in unit init/final }
  1658. main_procinfo.generate_code;
  1659. main_procinfo.resetprocdef;
  1660. release_main_proc(main_procinfo);
  1661. if assigned(init_procinfo) then
  1662. begin
  1663. { initialization can be implicit only }
  1664. current_module.flags:=current_module.flags or uf_init;
  1665. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1666. init_procinfo.generate_code;
  1667. init_procinfo.resetprocdef;
  1668. release_main_proc(init_procinfo);
  1669. end;
  1670. if assigned(finalize_procinfo) then
  1671. begin
  1672. if force_init_final or
  1673. cnodeutils.force_init or
  1674. not(has_no_code(finalize_procinfo.code)) then
  1675. begin
  1676. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1677. finalize_procinfo.generate_code;
  1678. current_module.flags:=current_module.flags or uf_finalize;
  1679. end;
  1680. finalize_procinfo.resetprocdef;
  1681. release_main_proc(finalize_procinfo);
  1682. end;
  1683. symtablestack.pop(current_module.localsymtable);
  1684. { consume the last point }
  1685. consume(_POINT);
  1686. { reset wpo flags for all defs }
  1687. reset_all_defs;
  1688. if (Errorcount=0) then
  1689. begin
  1690. { test static symtable }
  1691. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1692. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1693. tstoredsymtable(current_module.localsymtable).check_forwards;
  1694. current_module.allunitsused;
  1695. end;
  1696. { leave when we got an error }
  1697. if (Errorcount>0) and not status.skip_error then
  1698. begin
  1699. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1700. status.skip_error:=true;
  1701. exit;
  1702. end;
  1703. { remove all unused units, this happens when units are removed
  1704. from the uses clause in the source and the ppu was already being loaded }
  1705. hp:=tmodule(loaded_units.first);
  1706. while assigned(hp) do
  1707. begin
  1708. hp2:=hp;
  1709. hp:=tmodule(hp.next);
  1710. if hp2.is_unit and
  1711. not assigned(hp2.globalsymtable) then
  1712. begin
  1713. loaded_units.remove(hp2);
  1714. unloaded_units.concat(hp2);
  1715. end;
  1716. end;
  1717. { do we need to add the variants unit? }
  1718. maybeloadvariantsunit;
  1719. { Now that everything has been compiled we know if we need resource
  1720. support. If not, remove the unit. }
  1721. resources_used:=MaybeRemoveResUnit;
  1722. linker.initsysinitunitname;
  1723. if target_info.system in systems_internal_sysinit then
  1724. begin
  1725. { add start/halt unit }
  1726. AddUnit(linker.sysinitunit);
  1727. end;
  1728. {$ifdef arm}
  1729. { Insert .pdata section for arm-wince.
  1730. It is needed for exception handling. }
  1731. if target_info.system in [system_arm_wince] then
  1732. InsertPData;
  1733. {$endif arm}
  1734. cnodeutils.InsertThreadvars;
  1735. { generate rtti/init tables }
  1736. write_persistent_type_info(current_module.localsymtable,false);
  1737. { if an Objective-C module, generate rtti and module info }
  1738. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  1739. { generate imports }
  1740. if current_module.ImportLibraryList.Count>0 then
  1741. importlib.generatelib;
  1742. { generate debuginfo }
  1743. if (cs_debuginfo in current_settings.moduleswitches) then
  1744. current_debuginfo.inserttypeinfo;
  1745. if islibrary or (target_info.system in systems_unit_program_exports) then
  1746. exportlib.generatelib;
  1747. { Reference all DEBUGINFO sections from the main .fpc section }
  1748. if (cs_debuginfo in current_settings.moduleswitches) then
  1749. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1750. { Resource strings }
  1751. GenerateResourceStrings;
  1752. { Windows widestring needing initialization }
  1753. cnodeutils.InsertWideInits;
  1754. { Resourcestring references (const foo:string=someresourcestring) }
  1755. cnodeutils.InsertResStrInits;
  1756. { insert Tables and StackLength }
  1757. cnodeutils.InsertInitFinalTable;
  1758. cnodeutils.InsertThreadvarTablesTable;
  1759. cnodeutils.InsertResourceTablesTable;
  1760. cnodeutils.InsertWideInitsTablesTable;
  1761. cnodeutils.InsertResStrTablesTable;
  1762. cnodeutils.InsertMemorySizes;
  1763. { Insert symbol to resource info }
  1764. cnodeutils.InsertResourceInfo(resources_used);
  1765. { create callframe info }
  1766. create_dwarf_frame;
  1767. { insert own objectfile }
  1768. insertobjectfile;
  1769. { assemble and link }
  1770. create_objectfile;
  1771. { We might need the symbols info if not using
  1772. the default do_extractsymbolinfo
  1773. which is a dummy function PM }
  1774. needsymbolinfo:=
  1775. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  1776. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  1777. { release all local symtables that are not needed anymore }
  1778. if (not needsymbolinfo) then
  1779. free_localsymtables(current_module.localsymtable);
  1780. { leave when we got an error }
  1781. if (Errorcount>0) and not status.skip_error then
  1782. begin
  1783. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1784. status.skip_error:=true;
  1785. exit;
  1786. end;
  1787. if (not current_module.is_unit) then
  1788. begin
  1789. { create the executable when we are at level 1 }
  1790. if (compile_level=1) then
  1791. begin
  1792. { create global resource file by collecting all resource files }
  1793. CollectResourceFiles;
  1794. { write .def file }
  1795. if (cs_link_deffile in current_settings.globalswitches) then
  1796. deffile.writefile;
  1797. { insert all .o files from all loaded units and
  1798. unload the units, we don't need them anymore.
  1799. Keep the current_module because that is still needed }
  1800. hp:=tmodule(loaded_units.first);
  1801. while assigned(hp) do
  1802. begin
  1803. linker.AddModuleFiles(hp);
  1804. hp2:=tmodule(hp.next);
  1805. if (hp<>current_module) and
  1806. (not needsymbolinfo) then
  1807. begin
  1808. loaded_units.remove(hp);
  1809. hp.free;
  1810. end;
  1811. hp:=hp2;
  1812. end;
  1813. { free also unneeded units we didn't free before }
  1814. if not needsymbolinfo then
  1815. unloaded_units.Clear;
  1816. { finally we can create a executable }
  1817. if DLLSource then
  1818. linker.MakeSharedLibrary
  1819. else
  1820. linker.MakeExecutable;
  1821. { collect all necessary information for whole-program optimization }
  1822. wpoinfomanager.extractwpoinfofromprogram;
  1823. end;
  1824. { Give Fatal with error count for linker errors }
  1825. if (Errorcount>0) and not status.skip_error then
  1826. begin
  1827. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1828. status.skip_error:=true;
  1829. end;
  1830. end;
  1831. end;
  1832. end.