pmodules.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223
  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. { part of the package's used, aka contained units? }
  1312. uu:=tused_unit(current_module.used_units.first);
  1313. while assigned(uu) do
  1314. begin
  1315. if uu.u=hp then
  1316. break;
  1317. uu:=tused_unit(uu.next);
  1318. end;
  1319. if not assigned(uu) then
  1320. message2(package_n_implicit_unit_import,hp.realmodulename^,current_module.realmodulename^);
  1321. end;
  1322. { was this unit listed as a contained unit? If so => error }
  1323. if (hp<>current_module) and assigned(hp.package) then
  1324. begin
  1325. uu:=tused_unit(current_module.used_units.first);
  1326. while assigned(uu) do
  1327. begin
  1328. if uu.u=hp then
  1329. break;
  1330. uu:=tused_unit(uu.next);
  1331. end;
  1332. if assigned(uu) then
  1333. message2(package_e_unit_already_contained_in_package,hp.realmodulename^,hp.package.realpackagename^);
  1334. end;
  1335. hp:=tmodule(hp.next);
  1336. end;
  1337. {Insert the name of the main program into the symbol table.}
  1338. if current_module.realmodulename^<>'' then
  1339. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1340. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1341. symtablestack.push(current_module.localsymtable);
  1342. { create whole program optimisation information }
  1343. current_module.wpoinfo:=tunitwpoinfo.create;
  1344. { should we force unit initialization? }
  1345. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1346. if force_init_final or cnodeutils.force_init then
  1347. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1348. { Add symbol to the exports section for win32 so smartlinking a
  1349. DLL will include the edata section }
  1350. if assigned(exportlib) and
  1351. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1352. ((current_module.flags and uf_has_exports)<>0) then
  1353. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1354. { all labels must be defined before generating code }
  1355. if Errorcount=0 then
  1356. tstoredsymtable(current_module.localsymtable).checklabels;
  1357. symtablestack.pop(current_module.localsymtable);
  1358. { consume the last point }
  1359. consume(_END);
  1360. consume(_POINT);
  1361. if (Errorcount=0) then
  1362. begin
  1363. { test static symtable }
  1364. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1365. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1366. tstoredsymtable(current_module.localsymtable).check_forwards;
  1367. { Note: all contained units are considered as used }
  1368. end;
  1369. if target_info.system in systems_windows then
  1370. begin
  1371. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1372. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1373. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1374. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1375. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1376. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1377. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1378. end;
  1379. { leave when we got an error }
  1380. if (Errorcount>0) and not status.skip_error then
  1381. begin
  1382. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1383. status.skip_error:=true;
  1384. pkg.free;
  1385. exit;
  1386. end;
  1387. { remove all unused units, this happends when units are removed
  1388. from the uses clause in the source and the ppu was already being loaded }
  1389. hp:=tmodule(loaded_units.first);
  1390. while assigned(hp) do
  1391. begin
  1392. hp2:=hp;
  1393. hp:=tmodule(hp.next);
  1394. if assigned(hp2.package) then
  1395. add_package_unit_ref(hp2.package);
  1396. if hp2.is_unit and
  1397. not assigned(hp2.globalsymtable) then
  1398. loaded_units.remove(hp2);
  1399. end;
  1400. exportlib.ignoreduplicates:=true;
  1401. { force exports }
  1402. uu:=tused_unit(usedunits.first);
  1403. while assigned(uu) do
  1404. begin
  1405. if not assigned(systemunit) and (uu.u.modulename^='SYSTEM') then
  1406. begin
  1407. systemunit:=tglobalsymtable(uu.u.globalsymtable);
  1408. load_intern_types;
  1409. end;
  1410. if not assigned(uu.u.package) then
  1411. export_unit(uu.u);
  1412. uu:=tused_unit(uu.next);
  1413. end;
  1414. {$ifdef arm}
  1415. { Insert .pdata section for arm-wince.
  1416. It is needed for exception handling. }
  1417. if target_info.system in [system_arm_wince] then
  1418. InsertPData;
  1419. {$endif arm}
  1420. { generate debuginfo }
  1421. if (cs_debuginfo in current_settings.moduleswitches) then
  1422. current_debuginfo.inserttypeinfo;
  1423. exportlib.generatelib;
  1424. exportlib.ignoreduplicates:=false;
  1425. { create import libraries for all packages }
  1426. if packagelist.count>0 then
  1427. createimportlibfromexternals;
  1428. { generate imports }
  1429. if current_module.ImportLibraryList.Count>0 then
  1430. importlib.generatelib;
  1431. { Reference all DEBUGINFO sections from the main .fpc section }
  1432. if (cs_debuginfo in current_settings.moduleswitches) then
  1433. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1434. { insert own objectfile }
  1435. insertobjectfile;
  1436. { assemble and link }
  1437. create_objectfile;
  1438. { We might need the symbols info if not using
  1439. the default do_extractsymbolinfo
  1440. which is a dummy function PM }
  1441. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1442. { release all local symtables that are not needed anymore }
  1443. if (not needsymbolinfo) then
  1444. free_localsymtables(current_module.localsymtable);
  1445. { leave when we got an error }
  1446. if (Errorcount>0) and not status.skip_error then
  1447. begin
  1448. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1449. status.skip_error:=true;
  1450. pkg.free;
  1451. exit;
  1452. end;
  1453. if (not current_module.is_unit) then
  1454. begin
  1455. { we add all loaded units that are not part of a package to the
  1456. package; this includes units in the "contains" section as well
  1457. as implicitely imported ones }
  1458. hp:=tmodule(loaded_units.first);
  1459. while assigned(hp) do
  1460. begin
  1461. if (hp<>current_module) and not assigned(hp.package) then
  1462. pkg.addunit(hp);
  1463. hp:=tmodule(hp.next);
  1464. end;
  1465. pkg.initmoduleinfo(current_module);
  1466. { create the executable when we are at level 1 }
  1467. if (compile_level=1) then
  1468. begin
  1469. { create global resource file by collecting all resource files }
  1470. CollectResourceFiles;
  1471. { write .def file }
  1472. if (cs_link_deffile in current_settings.globalswitches) then
  1473. deffile.writefile;
  1474. { generate the pcp file }
  1475. pkg.savepcp;
  1476. { insert all .o files from all loaded units and
  1477. unload the units, we don't need them anymore.
  1478. Keep the current_module because that is still needed }
  1479. hp:=tmodule(loaded_units.first);
  1480. while assigned(hp) do
  1481. begin
  1482. { only link in those units which should become part of this
  1483. package }
  1484. if not assigned(hp.package) then
  1485. linker.AddModuleFiles(hp);
  1486. hp2:=tmodule(hp.next);
  1487. if (hp<>current_module) and
  1488. (not needsymbolinfo) then
  1489. begin
  1490. loaded_units.remove(hp);
  1491. hp.free;
  1492. end;
  1493. hp:=hp2;
  1494. end;
  1495. { add the library of directly used packages }
  1496. add_package_libs(linker);
  1497. { and now link the package library }
  1498. linker.MakeSharedLibrary
  1499. end;
  1500. { Give Fatal with error count for linker errors }
  1501. if (Errorcount>0) and not status.skip_error then
  1502. begin
  1503. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1504. status.skip_error:=true;
  1505. end;
  1506. pkg.free;
  1507. end;
  1508. end;
  1509. procedure proc_program(islibrary : boolean);
  1510. type
  1511. TProgramParam = record
  1512. name : ansistring;
  1513. nr : dword;
  1514. end;
  1515. var
  1516. main_file : tinputfile;
  1517. hp,hp2 : tmodule;
  1518. finalize_procinfo,
  1519. init_procinfo,
  1520. main_procinfo : tcgprocinfo;
  1521. force_init_final : boolean;
  1522. resources_used : boolean;
  1523. program_name : ansistring;
  1524. consume_semicolon_after_uses : boolean;
  1525. ps : tstaticvarsym;
  1526. paramnum : longint;
  1527. textsym : ttypesym;
  1528. sc : array of TProgramParam;
  1529. i : Longint;
  1530. begin
  1531. DLLsource:=islibrary;
  1532. Status.IsLibrary:=IsLibrary;
  1533. Status.IsPackage:=false;
  1534. Status.IsExe:=true;
  1535. parse_only:=false;
  1536. main_procinfo:=nil;
  1537. init_procinfo:=nil;
  1538. finalize_procinfo:=nil;
  1539. resources_used:=false;
  1540. { make the compiler happy and avoid an uninitialized variable warning on Setlength(sc,length(sc)+1); }
  1541. sc:=nil;
  1542. { DLL defaults to create reloc info }
  1543. if islibrary then
  1544. begin
  1545. if not RelocSectionSetExplicitly then
  1546. RelocSection:=true;
  1547. end;
  1548. { Relocation works only without stabs under Windows when }
  1549. { external linker (LD) is used. LD generates relocs for }
  1550. { stab sections which is not loaded in memory. It causes }
  1551. { AV error when DLL is loaded and relocation is needed. }
  1552. { Internal linker does not have this problem. }
  1553. if RelocSection and
  1554. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1555. (cs_link_extern in current_settings.globalswitches) then
  1556. begin
  1557. include(current_settings.globalswitches,cs_link_strip);
  1558. { Warning stabs info does not work with reloc section !! }
  1559. if (cs_debuginfo in current_settings.moduleswitches) and
  1560. (target_dbg.id=dbg_stabs) then
  1561. begin
  1562. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1563. Message(parser_w_parser_win32_debug_needs_WN);
  1564. exclude(current_settings.moduleswitches,cs_debuginfo);
  1565. end;
  1566. end;
  1567. { get correct output names }
  1568. main_file := current_scanner.inputfile;
  1569. while assigned(main_file.next) do
  1570. main_file := main_file.next;
  1571. current_module.SetFileName(main_file.path+main_file.name,true);
  1572. if islibrary then
  1573. begin
  1574. consume(_LIBRARY);
  1575. program_name:=orgpattern;
  1576. consume(_ID);
  1577. while token=_POINT do
  1578. begin
  1579. consume(_POINT);
  1580. program_name:=program_name+'.'+orgpattern;
  1581. consume(_ID);
  1582. end;
  1583. current_module.setmodulename(program_name);
  1584. current_module.islibrary:=true;
  1585. exportlib.preparelib(program_name);
  1586. if tf_library_needs_pic in target_info.flags then
  1587. include(current_settings.moduleswitches,cs_create_pic);
  1588. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1589. read, all following directives are parsed as well }
  1590. setupglobalswitches;
  1591. consume(_SEMICOLON);
  1592. end
  1593. else
  1594. { is there an program head ? }
  1595. if token=_PROGRAM then
  1596. begin
  1597. consume(_PROGRAM);
  1598. program_name:=orgpattern;
  1599. consume(_ID);
  1600. while token=_POINT do
  1601. begin
  1602. consume(_POINT);
  1603. program_name:=program_name+'.'+orgpattern;
  1604. consume(_ID);
  1605. end;
  1606. current_module.setmodulename(program_name);
  1607. if (target_info.system in systems_unit_program_exports) then
  1608. exportlib.preparelib(program_name);
  1609. if token=_LKLAMMER then
  1610. begin
  1611. consume(_LKLAMMER);
  1612. paramnum:=1;
  1613. repeat
  1614. if m_iso in current_settings.modeswitches then
  1615. begin
  1616. if (pattern<>'INPUT') and (pattern<>'OUTPUT') then
  1617. begin
  1618. { the symtablestack is not setup here, so text must be created later on }
  1619. Setlength(sc,length(sc)+1);
  1620. with sc[high(sc)] do
  1621. begin
  1622. name:=pattern;
  1623. nr:=paramnum;
  1624. end;
  1625. inc(paramnum);
  1626. end;
  1627. end;
  1628. consume(_ID);
  1629. until not try_to_consume(_COMMA);
  1630. consume(_RKLAMMER);
  1631. end;
  1632. { setup things using the switches, do this before the semicolon, because after the semicolon has been
  1633. read, all following directives are parsed as well }
  1634. setupglobalswitches;
  1635. consume(_SEMICOLON);
  1636. end
  1637. else
  1638. begin
  1639. if (target_info.system in systems_unit_program_exports) then
  1640. exportlib.preparelib(current_module.realmodulename^);
  1641. { setup things using the switches }
  1642. setupglobalswitches;
  1643. end;
  1644. { load all packages, so we know whether a unit is contained inside a
  1645. package or not }
  1646. load_packages;
  1647. { global switches are read, so further changes aren't allowed }
  1648. current_module.in_global:=false;
  1649. { set implementation flag }
  1650. current_module.in_interface:=false;
  1651. current_module.interface_compiled:=true;
  1652. { insert after the unit symbol tables the static symbol table
  1653. of the program }
  1654. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1655. { load standard units (system,objpas,profile unit) }
  1656. loaddefaultunits;
  1657. { Load units provided on the command line }
  1658. loadautounits;
  1659. { insert iso program parameters }
  1660. if length(sc)>0 then
  1661. begin
  1662. textsym:=search_system_type('TEXT');
  1663. if not(assigned(textsym)) then
  1664. internalerror(2013011201);
  1665. for i:=0 to high(sc) do
  1666. begin
  1667. ps:=cstaticvarsym.create(sc[i].name,vs_value,textsym.typedef,[]);
  1668. ps.isoindex:=sc[i].nr;
  1669. current_module.localsymtable.insert(ps,true);
  1670. cnodeutils.insertbssdata(tstaticvarsym(ps));
  1671. end;
  1672. end;
  1673. { Load the units used by the program we compile. }
  1674. if token=_USES then
  1675. begin
  1676. loadunits(nil);
  1677. consume_semicolon_after_uses:=true;
  1678. end
  1679. else
  1680. consume_semicolon_after_uses:=false;
  1681. { All units are read, now give them a number }
  1682. current_module.updatemaps;
  1683. { consume the semicolon after maps have been updated else conditional compiling expressions
  1684. might cause internal errors, see tw8611 }
  1685. if consume_semicolon_after_uses then
  1686. consume(_SEMICOLON);
  1687. {Insert the name of the main program into the symbol table.}
  1688. if current_module.realmodulename^<>'' then
  1689. tabstractunitsymtable(current_module.localsymtable).insertunit(cunitsym.create(current_module.realmodulename^,current_module));
  1690. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1691. symtablestack.push(current_module.localsymtable);
  1692. {$ifdef jvm}
  1693. { fake classdef to represent the class corresponding to the unit }
  1694. addmoduleclass;
  1695. {$endif}
  1696. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1697. maybe_load_got;
  1698. { create whole program optimisation information }
  1699. current_module.wpoinfo:=tunitwpoinfo.create;
  1700. { The program intialization needs an alias, so it can be called
  1701. from the bootstrap code.}
  1702. if islibrary then
  1703. begin
  1704. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1705. { Win32 startup code needs a single name }
  1706. if not(target_info.system in (systems_darwin+systems_aix)) then
  1707. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1708. else
  1709. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1710. end
  1711. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin+systems_aix)) then
  1712. begin
  1713. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1714. end
  1715. else
  1716. begin
  1717. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1718. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1719. end;
  1720. main_procinfo.parse_body;
  1721. { save file pos for debuginfo }
  1722. current_module.mainfilepos:=main_procinfo.entrypos;
  1723. { Generate specializations of objectdefs methods }
  1724. generate_specialization_procs;
  1725. { add implementations for synthetic method declarations added by
  1726. the compiler }
  1727. add_synthetic_method_implementations(current_module.localsymtable);
  1728. { should we force unit initialization? }
  1729. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1730. if force_init_final or cnodeutils.force_init then
  1731. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1732. { Add symbol to the exports section for win32 so smartlinking a
  1733. DLL will include the edata section }
  1734. if assigned(exportlib) and
  1735. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1736. ((current_module.flags and uf_has_exports)<>0) then
  1737. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1738. { finalize? }
  1739. if token=_FINALIZATION then
  1740. begin
  1741. { Parse the finalize }
  1742. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1743. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1744. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1745. finalize_procinfo.parse_body;
  1746. end
  1747. else
  1748. if force_init_final or cnodeutils.force_final then
  1749. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1750. { the finalization routine of libraries is generic (and all libraries need to }
  1751. { be finalized, so they can finalize any units they use }
  1752. if (islibrary) then
  1753. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1754. { all labels must be defined before generating code }
  1755. if Errorcount=0 then
  1756. tstoredsymtable(current_module.localsymtable).checklabels;
  1757. { See remark in unit init/final }
  1758. main_procinfo.generate_code;
  1759. main_procinfo.resetprocdef;
  1760. release_main_proc(main_procinfo);
  1761. if assigned(init_procinfo) then
  1762. begin
  1763. { initialization can be implicit only }
  1764. current_module.flags:=current_module.flags or uf_init;
  1765. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1766. init_procinfo.generate_code;
  1767. init_procinfo.resetprocdef;
  1768. release_main_proc(init_procinfo);
  1769. end;
  1770. if assigned(finalize_procinfo) then
  1771. begin
  1772. if force_init_final or
  1773. cnodeutils.force_init or
  1774. not(has_no_code(finalize_procinfo.code)) then
  1775. begin
  1776. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1777. finalize_procinfo.generate_code;
  1778. current_module.flags:=current_module.flags or uf_finalize;
  1779. end;
  1780. finalize_procinfo.resetprocdef;
  1781. release_main_proc(finalize_procinfo);
  1782. end;
  1783. symtablestack.pop(current_module.localsymtable);
  1784. { consume the last point }
  1785. consume(_POINT);
  1786. { reset wpo flags for all defs }
  1787. reset_all_defs;
  1788. if (Errorcount=0) then
  1789. begin
  1790. { test static symtable }
  1791. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1792. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1793. tstoredsymtable(current_module.localsymtable).check_forwards;
  1794. current_module.allunitsused;
  1795. end;
  1796. { leave when we got an error }
  1797. if (Errorcount>0) and not status.skip_error then
  1798. begin
  1799. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1800. status.skip_error:=true;
  1801. exit;
  1802. end;
  1803. { remove all unused units, this happens when units are removed
  1804. from the uses clause in the source and the ppu was already being loaded }
  1805. hp:=tmodule(loaded_units.first);
  1806. while assigned(hp) do
  1807. begin
  1808. hp2:=hp;
  1809. hp:=tmodule(hp.next);
  1810. if hp2.is_unit and
  1811. not assigned(hp2.globalsymtable) then
  1812. begin
  1813. loaded_units.remove(hp2);
  1814. unloaded_units.concat(hp2);
  1815. end;
  1816. end;
  1817. { do we need to add the variants unit? }
  1818. maybeloadvariantsunit;
  1819. { Now that everything has been compiled we know if we need resource
  1820. support. If not, remove the unit. }
  1821. resources_used:=MaybeRemoveResUnit;
  1822. linker.initsysinitunitname;
  1823. if target_info.system in systems_internal_sysinit then
  1824. begin
  1825. { add start/halt unit }
  1826. AddUnit(linker.sysinitunit);
  1827. end;
  1828. {$ifdef arm}
  1829. { Insert .pdata section for arm-wince.
  1830. It is needed for exception handling. }
  1831. if target_info.system in [system_arm_wince] then
  1832. InsertPData;
  1833. {$endif arm}
  1834. cnodeutils.InsertThreadvars;
  1835. { generate rtti/init tables }
  1836. write_persistent_type_info(current_module.localsymtable,false);
  1837. { if an Objective-C module, generate rtti and module info }
  1838. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  1839. { generate debuginfo }
  1840. if (cs_debuginfo in current_settings.moduleswitches) then
  1841. current_debuginfo.inserttypeinfo;
  1842. if islibrary or (target_info.system in systems_unit_program_exports) then
  1843. exportlib.generatelib;
  1844. { Reference all DEBUGINFO sections from the main .fpc section }
  1845. if (cs_debuginfo in current_settings.moduleswitches) then
  1846. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1847. { Resource strings }
  1848. GenerateResourceStrings;
  1849. { Windows widestring needing initialization }
  1850. cnodeutils.InsertWideInits;
  1851. { Resourcestring references (const foo:string=someresourcestring) }
  1852. cnodeutils.InsertResStrInits;
  1853. { insert Tables and StackLength }
  1854. cnodeutils.InsertInitFinalTable;
  1855. cnodeutils.InsertThreadvarTablesTable;
  1856. cnodeutils.InsertResourceTablesTable;
  1857. cnodeutils.InsertWideInitsTablesTable;
  1858. cnodeutils.InsertResStrTablesTable;
  1859. cnodeutils.InsertMemorySizes;
  1860. { Insert symbol to resource info }
  1861. cnodeutils.InsertResourceInfo(resources_used);
  1862. { create callframe info }
  1863. create_dwarf_frame;
  1864. { create import libraries for all packages }
  1865. if packagelist.count>0 then
  1866. createimportlibfromexternals;
  1867. { generate imports }
  1868. if current_module.ImportLibraryList.Count>0 then
  1869. importlib.generatelib;
  1870. { insert own objectfile }
  1871. insertobjectfile;
  1872. { assemble and link }
  1873. create_objectfile;
  1874. { We might need the symbols info if not using
  1875. the default do_extractsymbolinfo
  1876. which is a dummy function PM }
  1877. needsymbolinfo:=
  1878. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  1879. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  1880. { release all local symtables that are not needed anymore }
  1881. if (not needsymbolinfo) then
  1882. free_localsymtables(current_module.localsymtable);
  1883. { leave when we got an error }
  1884. if (Errorcount>0) and not status.skip_error then
  1885. begin
  1886. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1887. status.skip_error:=true;
  1888. exit;
  1889. end;
  1890. if (not current_module.is_unit) then
  1891. begin
  1892. { create the executable when we are at level 1 }
  1893. if (compile_level=1) then
  1894. begin
  1895. { create global resource file by collecting all resource files }
  1896. CollectResourceFiles;
  1897. { write .def file }
  1898. if (cs_link_deffile in current_settings.globalswitches) then
  1899. deffile.writefile;
  1900. { insert all .o files from all loaded units and
  1901. unload the units, we don't need them anymore.
  1902. Keep the current_module because that is still needed }
  1903. hp:=tmodule(loaded_units.first);
  1904. while assigned(hp) do
  1905. begin
  1906. if hp.flags and uf_in_library=0 then
  1907. linker.AddModuleFiles(hp);
  1908. hp2:=tmodule(hp.next);
  1909. if assigned(hp.package) then
  1910. add_package_unit_ref(hp.package);
  1911. if (hp<>current_module) and
  1912. (not needsymbolinfo) then
  1913. begin
  1914. loaded_units.remove(hp);
  1915. hp.free;
  1916. end;
  1917. hp:=hp2;
  1918. end;
  1919. { free also unneeded units we didn't free before }
  1920. if not needsymbolinfo then
  1921. unloaded_units.Clear;
  1922. { add all directly used packages as libraries }
  1923. add_package_libs(linker);
  1924. { finally we can create a executable }
  1925. if DLLSource then
  1926. linker.MakeSharedLibrary
  1927. else
  1928. linker.MakeExecutable;
  1929. { collect all necessary information for whole-program optimization }
  1930. wpoinfomanager.extractwpoinfofromprogram;
  1931. end;
  1932. { Give Fatal with error count for linker errors }
  1933. if (Errorcount>0) and not status.skip_error then
  1934. begin
  1935. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1936. status.skip_error:=true;
  1937. end;
  1938. end;
  1939. end;
  1940. end.