pmodules.pas 87 KB

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