pmodules.pas 89 KB

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