pmodules.pas 89 KB

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