pmodules.pas 87 KB

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