pmodules.pas 84 KB

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