pmodules.pas 84 KB

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