pmodules.pas 86 KB

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