pmodules.pas 87 KB

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