pmodules.pas 86 KB

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