pmodules.pas 81 KB

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