pmodules.pas 82 KB

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