pmodules.pas 89 KB

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