pmodules.pas 86 KB

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