pmodules.pas 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319
  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,ptype,
  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. consume(_SEMICOLON);
  461. end;
  462. procedure reset_all_defs;
  463. begin
  464. if assigned(current_module.wpoinfo) then
  465. current_module.wpoinfo.resetdefs;
  466. end;
  467. procedure free_localsymtables(st:TSymtable);
  468. var
  469. i : longint;
  470. def : tstoreddef;
  471. pd : tprocdef;
  472. begin
  473. for i:=0 to st.DefList.Count-1 do
  474. begin
  475. def:=tstoreddef(st.DefList[i]);
  476. if def.typ=procdef then
  477. begin
  478. pd:=tprocdef(def);
  479. if assigned(pd.localst) and
  480. (pd.localst.symtabletype<>staticsymtable) and
  481. not(po_inline in pd.procoptions) then
  482. begin
  483. free_localsymtables(pd.localst);
  484. pd.localst.free;
  485. pd.localst:=nil;
  486. end;
  487. end;
  488. end;
  489. end;
  490. procedure parse_implementation_uses;
  491. begin
  492. if token=_USES then
  493. loadunits;
  494. end;
  495. procedure setupglobalswitches;
  496. begin
  497. if (cs_create_pic in current_settings.moduleswitches) then
  498. begin
  499. def_system_macro('FPC_PIC');
  500. def_system_macro('PIC');
  501. end;
  502. end;
  503. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  504. var
  505. ps : tprocsym;
  506. pd : tprocdef;
  507. begin
  508. { there should be no current_procinfo available }
  509. if assigned(current_procinfo) then
  510. internalerror(200304275);
  511. {Generate a procsym for main}
  512. ps:=tprocsym.create('$'+name);
  513. { main are allways used }
  514. inc(ps.refs);
  515. st.insert(ps);
  516. pd:=tprocdef(cnodeutils.create_main_procdef(target_info.cprefix+name,potype,ps));
  517. { We don't need is a local symtable. Change it into the static
  518. symtable }
  519. pd.localst.free;
  520. pd.localst:=st;
  521. handle_calling_convention(pd);
  522. { set procinfo and current_procinfo.procdef }
  523. result:=tcgprocinfo(cprocinfo.create(nil));
  524. result.procdef:=pd;
  525. { main proc does always a call e.g. to init system unit }
  526. include(result.flags,pi_do_call);
  527. end;
  528. procedure release_main_proc(pi:tcgprocinfo);
  529. begin
  530. { remove localst as it was replaced by staticsymtable }
  531. pi.procdef.localst:=nil;
  532. { remove procinfo }
  533. current_module.procinfo:=nil;
  534. pi.free;
  535. pi:=nil;
  536. end;
  537. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  538. procedure maybe_load_got;
  539. {$ifdef i386}
  540. var
  541. gotvarsym : tstaticvarsym;
  542. {$endif i386}
  543. begin
  544. {$ifdef i386}
  545. if (cs_create_pic in current_settings.moduleswitches) and
  546. (tf_pic_uses_got in target_info.flags) then
  547. begin
  548. { insert symbol for got access in assembler code}
  549. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  550. vs_value,voidpointertype,[vo_is_external]);
  551. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  552. current_module.localsymtable.insert(gotvarsym);
  553. { avoid unnecessary warnings }
  554. gotvarsym.varstate:=vs_read;
  555. gotvarsym.refs:=1;
  556. end;
  557. {$endif i386}
  558. end;
  559. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  560. begin
  561. { create procdef }
  562. case flag of
  563. uf_init :
  564. begin
  565. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  566. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  567. end;
  568. uf_finalize :
  569. begin
  570. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  571. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  572. if (not current_module.is_unit) then
  573. result.procdef.aliasnames.insert('PASCALFINALIZE');
  574. end;
  575. else
  576. internalerror(200304253);
  577. end;
  578. result.code:=cnothingnode.create;
  579. end;
  580. procedure copy_macro(p:TObject; arg:pointer);
  581. begin
  582. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  583. end;
  584. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  585. var
  586. deprecated_seen,
  587. last_is_deprecated:boolean;
  588. begin
  589. try_consume_hintdirective:=false;
  590. deprecated_seen:=false;
  591. repeat
  592. last_is_deprecated:=false;
  593. case idtoken of
  594. _LIBRARY :
  595. begin
  596. include(moduleopt,mo_hint_library);
  597. try_consume_hintdirective:=true;
  598. end;
  599. _DEPRECATED :
  600. begin
  601. { allow deprecated only once }
  602. if deprecated_seen then
  603. break;
  604. include(moduleopt,mo_hint_deprecated);
  605. try_consume_hintdirective:=true;
  606. last_is_deprecated:=true;
  607. deprecated_seen:=true;
  608. end;
  609. _EXPERIMENTAL :
  610. begin
  611. include(moduleopt,mo_hint_experimental);
  612. try_consume_hintdirective:=true;
  613. end;
  614. _PLATFORM :
  615. begin
  616. include(moduleopt,mo_hint_platform);
  617. try_consume_hintdirective:=true;
  618. end;
  619. _UNIMPLEMENTED :
  620. begin
  621. include(moduleopt,mo_hint_unimplemented);
  622. try_consume_hintdirective:=true;
  623. end;
  624. else
  625. break;
  626. end;
  627. consume(Token);
  628. { handle deprecated message }
  629. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  630. begin
  631. if deprecatedmsg<>nil then
  632. internalerror(201001221);
  633. if token=_CSTRING then
  634. deprecatedmsg:=stringdup(cstringpattern)
  635. else
  636. deprecatedmsg:=stringdup(pattern);
  637. consume(token);
  638. include(moduleopt,mo_has_deprecated_msg);
  639. end;
  640. until false;
  641. end;
  642. {$ifdef jvm}
  643. procedure addmoduleclass;
  644. var
  645. def: tobjectdef;
  646. typesym: ttypesym;
  647. begin
  648. { java_jlobject may not have been parsed yet (system unit); in any
  649. case, we only use this to refer to the class type, so inheritance
  650. does not matter }
  651. def:=tobjectdef.create(odt_javaclass,'__FPC_JVM_Module_Class_Alias$',nil);
  652. include(def.objectoptions,oo_is_external);
  653. include(def.objectoptions,oo_is_sealed);
  654. def.objextname:=stringdup(current_module.realmodulename^);
  655. typesym:=ttypesym.create('__FPC_JVM_Module_Class_Alias$',def);
  656. symtablestack.top.insert(typesym);
  657. end;
  658. {$endif jvm}
  659. type
  660. tfinishstate=record
  661. init_procinfo:tcgprocinfo;
  662. end;
  663. pfinishstate=^tfinishstate;
  664. procedure finish_unit(module:tmodule;immediate:boolean);forward;
  665. function proc_unit:boolean;
  666. var
  667. main_file: tinputfile;
  668. {
  669. {$ifdef EXTDEBUG}
  670. store_crc,
  671. {$endif EXTDEBUG}
  672. store_interface_crc,
  673. store_indirect_crc: cardinal;}
  674. s1,s2 : ^string; {Saves stack space}
  675. init_procinfo : tcgprocinfo;
  676. unitname : ansistring;
  677. unitname8 : string[8];
  678. i,j : longint;
  679. finishstate:pfinishstate;
  680. globalstate:pglobalstate;
  681. begin
  682. result:=true;
  683. init_procinfo:=nil;
  684. if m_mac in current_settings.modeswitches then
  685. current_module.mode_switch_allowed:= false;
  686. consume(_UNIT);
  687. if compile_level=1 then
  688. Status.IsExe:=false;
  689. unitname:=orgpattern;
  690. consume(_ID);
  691. while token=_POINT do
  692. begin
  693. consume(_POINT);
  694. unitname:=unitname+'.'+orgpattern;
  695. consume(_ID);
  696. end;
  697. { create filenames and unit name }
  698. main_file := current_scanner.inputfile;
  699. while assigned(main_file.next) do
  700. main_file := main_file.next;
  701. new(s1);
  702. s1^:=current_module.modulename^;
  703. current_module.SetFileName(main_file.path+main_file.name,true);
  704. current_module.SetModuleName(unitname);
  705. { check for system unit }
  706. new(s2);
  707. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name),''));
  708. unitname8:=copy(current_module.modulename^,1,8);
  709. if (cs_check_unit_name in current_settings.globalswitches) and
  710. (
  711. not(
  712. (current_module.modulename^=s2^) or
  713. (
  714. (length(current_module.modulename^)>8) and
  715. (unitname8=s2^)
  716. )
  717. )
  718. or
  719. (
  720. (length(s1^)>8) and
  721. (s1^<>current_module.modulename^)
  722. )
  723. ) then
  724. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  725. if (current_module.modulename^='SYSTEM') then
  726. include(current_settings.moduleswitches,cs_compilesystem);
  727. dispose(s2);
  728. dispose(s1);
  729. if (target_info.system in systems_unit_program_exports) then
  730. exportlib.preparelib(current_module.realmodulename^);
  731. { parse hint directives }
  732. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  733. consume(_SEMICOLON);
  734. consume(_INTERFACE);
  735. { global switches are read, so further changes aren't allowed }
  736. current_module.in_global:=false;
  737. { handle the global switches }
  738. setupglobalswitches;
  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(tunitsym.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;
  762. { has it been compiled at a higher level ?}
  763. if current_module.state=ms_compiled then
  764. exit;
  765. end;
  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. { create whole program optimisation information (may already be
  773. updated in the interface, e.g., in case of classrefdef typed
  774. constants }
  775. current_module.wpoinfo:=tunitwpoinfo.create;
  776. { ... parse the declarations }
  777. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  778. symtablestack.push(current_module.globalsymtable);
  779. {$ifdef jvm}
  780. { fake classdef to represent the class corresponding to the unit }
  781. addmoduleclass;
  782. {$endif}
  783. read_interface_declarations;
  784. symtablestack.pop(current_module.globalsymtable);
  785. { Export macros defined in the interface for macpas. The macros
  786. are put in the globalmacrosymtable that will only be used by other
  787. units. The current unit continues to use the localmacrosymtable }
  788. if (m_mac in current_settings.modeswitches) then
  789. begin
  790. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  791. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  792. end;
  793. { leave when we got an error }
  794. if (Errorcount>0) and not status.skip_error then
  795. begin
  796. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  797. status.skip_error:=true;
  798. exit;
  799. end;
  800. { Our interface is compiled, generate CRC and switch to implementation }
  801. if not(cs_compilesystem in current_settings.moduleswitches) and
  802. (Errorcount=0) then
  803. tppumodule(current_module).getppucrc;
  804. current_module.in_interface:=false;
  805. current_module.interface_compiled:=true;
  806. { First reload all units depending on our interface, we need to do this
  807. in the implementation part to prevent erroneous circular references }
  808. tppumodule(current_module).setdefgeneration;
  809. tppumodule(current_module).reload_flagged_units;
  810. { Parse the implementation section }
  811. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  812. current_module.interface_only:=true
  813. else
  814. current_module.interface_only:=false;
  815. parse_only:=false;
  816. { create static symbol table }
  817. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  818. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  819. maybe_load_got;
  820. if not current_module.interface_only then
  821. begin
  822. consume(_IMPLEMENTATION);
  823. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  824. { Read the implementation units }
  825. parse_implementation_uses;
  826. end;
  827. if current_module.state=ms_compiled then
  828. exit;
  829. { All units are read, now give them a number }
  830. current_module.updatemaps;
  831. symtablestack.push(current_module.globalsymtable);
  832. symtablestack.push(current_module.localsymtable);
  833. if not current_module.interface_only then
  834. begin
  835. Message1(parser_u_parsing_implementation,current_module.modulename^);
  836. if current_module.in_interface then
  837. internalerror(200212285);
  838. { Compile the unit }
  839. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  840. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  841. init_procinfo.parse_body;
  842. { save file pos for debuginfo }
  843. current_module.mainfilepos:=init_procinfo.entrypos;
  844. end;
  845. { remove all units that we are waiting for that are already waiting for
  846. us => breaking up circles }
  847. for i:=0 to current_module.waitingunits.count-1 do
  848. for j:=current_module.waitingforunit.count-1 downto 0 do
  849. if current_module.waitingunits[i]=current_module.waitingforunit[j] then
  850. current_module.waitingforunit.delete(j);
  851. {$ifdef DEBUG_UNITWAITING}
  852. Writeln('Units waiting for ', current_module.modulename^, ': ',
  853. current_module.waitingforunit.Count);
  854. {$endif}
  855. result:=current_module.waitingforunit.count=0;
  856. { save all information that is needed for finishing the unit }
  857. New(finishstate);
  858. finishstate^.init_procinfo:=init_procinfo;
  859. current_module.finishstate:=finishstate;
  860. if result then
  861. finish_unit(current_module,true)
  862. else
  863. begin
  864. { save the current state, so the parsing can continue where we left
  865. of here }
  866. New(globalstate);
  867. save_global_state(globalstate^,true);
  868. current_module.globalstate:=globalstate;
  869. end;
  870. end;
  871. procedure finish_unit(module:tmodule;immediate:boolean);
  872. function is_assembler_generated:boolean;
  873. var
  874. hal : tasmlisttype;
  875. begin
  876. result:=false;
  877. if Errorcount=0 then
  878. begin
  879. for hal:=low(TasmlistType) to high(TasmlistType) do
  880. if not current_asmdata.asmlists[hal].empty then
  881. begin
  882. result:=true;
  883. exit;
  884. end;
  885. end;
  886. end;
  887. procedure module_is_done;inline;
  888. begin
  889. dispose(pglobalstate(current_module.globalstate));
  890. current_module.globalstate:=nil;
  891. dispose(pfinishstate(current_module.finishstate));
  892. current_module.finishstate:=nil;
  893. end;
  894. var
  895. {$ifdef EXTDEBUG}
  896. store_crc,
  897. {$endif EXTDEBUG}
  898. store_interface_crc,
  899. store_indirect_crc: cardinal;
  900. force_init_final : boolean;
  901. init_procinfo,
  902. finalize_procinfo : tcgprocinfo;
  903. i,idx : longint;
  904. ag : boolean;
  905. finishstate : tfinishstate;
  906. globalstate : tglobalstate;
  907. waitingmodule : tmodule;
  908. begin
  909. if not immediate then
  910. begin
  911. {$ifdef DEBUG_UNITWAITING}
  912. writeln('finishing waiting unit ''', module.modulename^, '''');
  913. {$endif DEBUG_UNITWAITING}
  914. { restore the state when we stopped working on the unit }
  915. save_global_state(globalstate,true);
  916. if not assigned(module.globalstate) then
  917. internalerror(2012091802);
  918. restore_global_state(pglobalstate(module.globalstate)^,true);
  919. end;
  920. { current_module is now module }
  921. if not assigned(current_module.finishstate) then
  922. internalerror(2012091801);
  923. finishstate:=pfinishstate(current_module.finishstate)^;
  924. finalize_procinfo:=nil;
  925. init_procinfo:=finishstate.init_procinfo;
  926. { Generate specializations of objectdefs methods }
  927. generate_specialization_procs;
  928. { add implementations for synthetic method declarations added by
  929. the compiler }
  930. add_synthetic_method_implementations(current_module.globalsymtable);
  931. add_synthetic_method_implementations(current_module.localsymtable);
  932. { if the unit contains ansi/widestrings, initialization and
  933. finalization code must be forced }
  934. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  935. tstaticsymtable(current_module.localsymtable).needs_init_final;
  936. { should we force unit initialization? }
  937. { this is a hack, but how can it be done better ? }
  938. { Now the sole purpose of this is to change 'init' to 'init_implicit',
  939. is it needed at all? (Sergei) }
  940. { it's needed in case cnodeutils.force_init = true }
  941. if (force_init_final or cnodeutils.force_init) and
  942. assigned(init_procinfo) and
  943. has_no_code(init_procinfo.code) then
  944. begin
  945. { first release the not used init procinfo }
  946. if assigned(init_procinfo) then
  947. release_main_proc(init_procinfo);
  948. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  949. end;
  950. { finalize? }
  951. if not current_module.interface_only and (token=_FINALIZATION) then
  952. begin
  953. { Compile the finalize }
  954. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  955. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  956. finalize_procinfo.parse_body;
  957. end
  958. else if force_init_final or cnodeutils.force_final then
  959. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  960. { Now both init and finalize bodies are read and it is known
  961. which variables are used in both init and finalize we can now
  962. generate the code. This is required to prevent putting a variable in
  963. a register that is also used in the finalize body (PFV) }
  964. if assigned(init_procinfo) then
  965. begin
  966. if (force_init_final or cnodeutils.force_init) or
  967. not(has_no_code(init_procinfo.code)) then
  968. begin
  969. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  970. init_procinfo.generate_code;
  971. current_module.flags:=current_module.flags or uf_init;
  972. end;
  973. init_procinfo.resetprocdef;
  974. release_main_proc(init_procinfo);
  975. end;
  976. if assigned(finalize_procinfo) then
  977. begin
  978. if force_init_final or
  979. cnodeutils.force_init or
  980. not(has_no_code(finalize_procinfo.code)) then
  981. begin
  982. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  983. finalize_procinfo.generate_code;
  984. current_module.flags:=current_module.flags or uf_finalize;
  985. end;
  986. finalize_procinfo.resetprocdef;
  987. release_main_proc(finalize_procinfo);
  988. end;
  989. symtablestack.pop(current_module.localsymtable);
  990. symtablestack.pop(current_module.globalsymtable);
  991. { the last char should always be a point }
  992. consume(_POINT);
  993. { reset wpo flags for all defs }
  994. reset_all_defs;
  995. if (Errorcount=0) then
  996. begin
  997. { tests, if all (interface) forwards are resolved }
  998. tstoredsymtable(current_module.globalsymtable).check_forwards;
  999. { check if all private fields are used }
  1000. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1001. { test static symtable }
  1002. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1003. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1004. tstoredsymtable(current_module.localsymtable).check_forwards;
  1005. tstoredsymtable(current_module.localsymtable).checklabels;
  1006. { used units }
  1007. current_module.allunitsused;
  1008. end;
  1009. { leave when we got an error }
  1010. if (Errorcount>0) and not status.skip_error then
  1011. begin
  1012. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1013. status.skip_error:=true;
  1014. module_is_done;
  1015. if not immediate then
  1016. restore_global_state(globalstate,true);
  1017. exit;
  1018. end;
  1019. { if an Objective-C module, generate rtti and module info }
  1020. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1021. { do we need to add the variants unit? }
  1022. maybeloadvariantsunit;
  1023. { generate wrappers for interfaces }
  1024. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable,false);
  1025. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  1026. { generate rtti/init tables }
  1027. write_persistent_type_info(current_module.globalsymtable,true);
  1028. write_persistent_type_info(current_module.localsymtable,false);
  1029. { Tables }
  1030. cnodeutils.InsertThreadvars;
  1031. { Resource strings }
  1032. GenerateResourceStrings;
  1033. { Widestring typed constants }
  1034. cnodeutils.InsertWideInits;
  1035. { Resourcestring references }
  1036. cnodeutils.InsertResStrInits;
  1037. { generate debuginfo }
  1038. if (cs_debuginfo in current_settings.moduleswitches) then
  1039. current_debuginfo.inserttypeinfo;
  1040. { generate imports }
  1041. if current_module.ImportLibraryList.Count>0 then
  1042. importlib.generatelib;
  1043. { insert own objectfile, or say that it's in a library
  1044. (no check for an .o when loading) }
  1045. ag:=is_assembler_generated;
  1046. if ag then
  1047. insertobjectfile
  1048. else
  1049. begin
  1050. current_module.flags:=current_module.flags or uf_no_link;
  1051. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1052. end;
  1053. if ag then
  1054. begin
  1055. { create callframe info }
  1056. create_dwarf_frame;
  1057. { assemble }
  1058. create_objectfile;
  1059. end;
  1060. { Write out the ppufile after the object file has been created }
  1061. store_interface_crc:=current_module.interface_crc;
  1062. store_indirect_crc:=current_module.indirect_crc;
  1063. {$ifdef EXTDEBUG}
  1064. store_crc:=current_module.crc;
  1065. {$endif EXTDEBUG}
  1066. if (Errorcount=0) then
  1067. tppumodule(current_module).writeppu;
  1068. if not(cs_compilesystem in current_settings.moduleswitches) then
  1069. begin
  1070. if store_interface_crc<>current_module.interface_crc then
  1071. Message1(unit_u_interface_crc_changed,current_module.ppufilename);
  1072. if store_indirect_crc<>current_module.indirect_crc then
  1073. Message1(unit_u_indirect_crc_changed,current_module.ppufilename);
  1074. end;
  1075. {$ifdef EXTDEBUG}
  1076. if not(cs_compilesystem in current_settings.moduleswitches) then
  1077. if (store_crc<>current_module.crc) and simplify_ppu then
  1078. Message1(unit_u_implementation_crc_changed,current_module.ppufilename);
  1079. {$endif EXTDEBUG}
  1080. { release local symtables that are not needed anymore }
  1081. free_localsymtables(current_module.globalsymtable);
  1082. free_localsymtables(current_module.localsymtable);
  1083. { leave when we got an error }
  1084. if (Errorcount>0) and not status.skip_error then
  1085. begin
  1086. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1087. status.skip_error:=true;
  1088. module_is_done;
  1089. if not immediate then
  1090. restore_global_state(globalstate,true);
  1091. exit;
  1092. end;
  1093. {$ifdef debug_devirt}
  1094. { print out all instantiated class/object types }
  1095. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1096. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1097. begin
  1098. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1099. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1100. objectdef:
  1101. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1102. odt_object:
  1103. writeln(' (object)');
  1104. odt_class:
  1105. writeln(' (class)');
  1106. else
  1107. internalerror(2008101103);
  1108. end;
  1109. else
  1110. internalerror(2008101104);
  1111. end;
  1112. end;
  1113. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1114. begin
  1115. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1116. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1117. objectdef:
  1118. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1119. odt_class:
  1120. writeln(' (classrefdef)');
  1121. else
  1122. internalerror(2008101105);
  1123. end
  1124. else
  1125. internalerror(2008101102);
  1126. end;
  1127. end;
  1128. {$endif debug_devirt}
  1129. Message1(unit_u_finished_compiling,current_module.modulename^);
  1130. module_is_done;
  1131. if not immediate then
  1132. restore_global_state(globalstate,true);
  1133. for i:=0 to module.waitingunits.count-1 do
  1134. begin
  1135. waitingmodule:=tmodule(module.waitingunits[i]);
  1136. waitingmodule.waitingforunit.remove(module);
  1137. { only finish the module if it isn't already finished }
  1138. if (waitingmodule.waitingforunit.count=0) and
  1139. assigned(waitingmodule.finishstate) then
  1140. begin
  1141. finish_unit(waitingmodule,false);
  1142. waitingmodule.end_of_parsing;
  1143. end;
  1144. end;
  1145. end;
  1146. procedure procexport(const s : string);
  1147. var
  1148. hp : texported_item;
  1149. begin
  1150. hp:=texported_item.create;
  1151. hp.name:=stringdup(s);
  1152. hp.options:=hp.options or eo_name;
  1153. exportlib.exportprocedure(hp);
  1154. end;
  1155. procedure varexport(const s : string);
  1156. var
  1157. hp : texported_item;
  1158. begin
  1159. hp:=texported_item.create;
  1160. hp.name:=stringdup(s);
  1161. hp.options:=hp.options or eo_name;
  1162. exportlib.exportvar(hp);
  1163. end;
  1164. procedure insert_export(sym : TObject;arg:pointer);
  1165. var
  1166. i : longint;
  1167. item : TCmdStrListItem;
  1168. begin
  1169. case TSym(sym).typ of
  1170. { ignore: }
  1171. unitsym,
  1172. syssym,
  1173. constsym,
  1174. enumsym,
  1175. typesym:
  1176. ;
  1177. procsym:
  1178. begin
  1179. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1180. begin
  1181. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1182. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1183. ((tsymtable(arg).symtabletype=globalsymtable) or
  1184. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1185. ) then
  1186. begin
  1187. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1188. { walk through all aliases }
  1189. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1190. while assigned(item) do
  1191. begin
  1192. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1193. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1194. procexport(item.str);
  1195. item:=TCmdStrListItem(item.next);
  1196. end;
  1197. end;
  1198. end;
  1199. end;
  1200. staticvarsym:
  1201. begin
  1202. varexport(tsym(sym).mangledname);
  1203. end;
  1204. else
  1205. begin
  1206. writeln('unknown: ',ord(TSym(sym).typ));
  1207. end;
  1208. end;
  1209. end;
  1210. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1211. Var
  1212. MakeStatic : Boolean;
  1213. Var
  1214. buffer : array[0..$1fff] of byte;
  1215. inppu,
  1216. outppu : tppufile;
  1217. b,
  1218. untilb : byte;
  1219. l,m : longint;
  1220. f : file;
  1221. ext,
  1222. s : string;
  1223. ppuversion : dword;
  1224. begin
  1225. Result:=false;
  1226. MakeStatic:=False;
  1227. inppu:=tppufile.create(PPUFn);
  1228. if not inppu.openfile then
  1229. begin
  1230. inppu.free;
  1231. Comment(V_Error,'Could not open : '+PPUFn);
  1232. Exit;
  1233. end;
  1234. { Check the ppufile }
  1235. if not inppu.CheckPPUId then
  1236. begin
  1237. inppu.free;
  1238. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1239. Exit;
  1240. end;
  1241. ppuversion:=inppu.GetPPUVersion;
  1242. if ppuversion<CurrentPPUVersion then
  1243. begin
  1244. inppu.free;
  1245. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1246. Exit;
  1247. end;
  1248. { No .o file generated for this ppu, just skip }
  1249. if (inppu.header.flags and uf_no_link)<>0 then
  1250. begin
  1251. inppu.free;
  1252. Result:=true;
  1253. Exit;
  1254. end;
  1255. { Already a lib? }
  1256. if (inppu.header.flags and uf_in_library)<>0 then
  1257. begin
  1258. inppu.free;
  1259. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1260. Exit;
  1261. end;
  1262. { We need a static linked unit }
  1263. if (inppu.header.flags and uf_static_linked)=0 then
  1264. begin
  1265. inppu.free;
  1266. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1267. Exit;
  1268. end;
  1269. { Check if shared is allowed }
  1270. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1271. begin
  1272. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1273. MakeStatic:=true;
  1274. end;
  1275. { Create the new ppu }
  1276. if PPUFn=PPLFn then
  1277. outppu:=tppufile.create('ppumove.$$$')
  1278. else
  1279. outppu:=tppufile.create(PPLFn);
  1280. outppu.createfile;
  1281. { Create new header, with the new flags }
  1282. outppu.header:=inppu.header;
  1283. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1284. if MakeStatic then
  1285. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1286. else
  1287. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1288. { read until the object files are found }
  1289. untilb:=iblinkunitofiles;
  1290. repeat
  1291. b:=inppu.readentry;
  1292. if b in [ibendinterface,ibend] then
  1293. begin
  1294. inppu.free;
  1295. outppu.free;
  1296. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1297. Exit;
  1298. end;
  1299. if b<>untilb then
  1300. begin
  1301. repeat
  1302. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1303. outppu.putdata(buffer,l);
  1304. until l<sizeof(buffer);
  1305. outppu.writeentry(b);
  1306. end;
  1307. until (b=untilb);
  1308. { we have now reached the section for the files which need to be added,
  1309. now add them to the list }
  1310. case b of
  1311. iblinkunitofiles :
  1312. begin
  1313. { add all o files, and save the entry when not creating a static
  1314. library to keep staticlinking possible }
  1315. while not inppu.endofentry do
  1316. begin
  1317. s:=inppu.getstring;
  1318. m:=inppu.getlongint;
  1319. if not MakeStatic then
  1320. begin
  1321. outppu.putstring(s);
  1322. outppu.putlongint(m);
  1323. end;
  1324. current_module.linkotherofiles.add(s,link_always);;
  1325. end;
  1326. if not MakeStatic then
  1327. outppu.writeentry(b);
  1328. end;
  1329. { iblinkunitstaticlibs :
  1330. begin
  1331. AddToLinkFiles(ExtractLib(inppu.getstring));
  1332. if not inppu.endofentry then
  1333. begin
  1334. repeat
  1335. inppu.getdatabuf(buffer^,bufsize,l);
  1336. outppu.putdata(buffer^,l);
  1337. until l<bufsize;
  1338. outppu.writeentry(b);
  1339. end;
  1340. end; }
  1341. end;
  1342. { just add a new entry with the new lib }
  1343. if MakeStatic then
  1344. begin
  1345. outppu.putstring('imp'+current_module.realmodulename^);
  1346. outppu.putlongint(link_static);
  1347. outppu.writeentry(iblinkunitstaticlibs)
  1348. end
  1349. else
  1350. begin
  1351. outppu.putstring('imp'+current_module.realmodulename^);
  1352. outppu.putlongint(link_shared);
  1353. outppu.writeentry(iblinkunitsharedlibs);
  1354. end;
  1355. { read all entries until the end and write them also to the new ppu }
  1356. repeat
  1357. b:=inppu.readentry;
  1358. { don't write ibend, that's written automatically }
  1359. if b<>ibend then
  1360. begin
  1361. if b=iblinkothersharedlibs then
  1362. begin
  1363. while not inppu.endofentry do
  1364. begin
  1365. s:=inppu.getstring;
  1366. m:=inppu.getlongint;
  1367. outppu.putstring(s);
  1368. outppu.putlongint(m);
  1369. { strip lib prefix }
  1370. if copy(s,1,3)='lib' then
  1371. delete(s,1,3);
  1372. ext:=ExtractFileExt(s);
  1373. if ext<>'' then
  1374. delete(s,length(s)-length(ext)+1,length(ext));
  1375. current_module.linkOtherSharedLibs.add(s,link_always);
  1376. end;
  1377. end
  1378. else
  1379. repeat
  1380. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1381. outppu.putdata(buffer,l);
  1382. until l<sizeof(buffer);
  1383. outppu.writeentry(b);
  1384. end;
  1385. until b=ibend;
  1386. { write the last stuff and close }
  1387. outppu.flush;
  1388. outppu.writeheader;
  1389. outppu.free;
  1390. inppu.free;
  1391. { rename }
  1392. if PPUFn=PPLFn then
  1393. begin
  1394. {$push}{$I-}
  1395. assign(f,PPUFn);
  1396. erase(f);
  1397. assign(f,'ppumove.$$$');
  1398. rename(f,PPUFn);
  1399. {$pop}
  1400. if ioresult<>0 then;
  1401. end;
  1402. Result:=True;
  1403. end;
  1404. procedure createimportlibfromexports;
  1405. var
  1406. hp : texported_item;
  1407. begin
  1408. hp:=texported_item(current_module._exports.first);
  1409. while assigned(hp) do
  1410. begin
  1411. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.name^,hp.index,hp.is_var,false);
  1412. hp:=texported_item(hp.next);
  1413. end;
  1414. end;
  1415. procedure proc_package;
  1416. var
  1417. main_file : tinputfile;
  1418. hp,hp2 : tmodule;
  1419. {finalize_procinfo,
  1420. init_procinfo,
  1421. main_procinfo : tcgprocinfo;}
  1422. force_init_final : boolean;
  1423. uu : tused_unit;
  1424. module_name: ansistring;
  1425. begin
  1426. Status.IsPackage:=true;
  1427. Status.IsExe:=true;
  1428. parse_only:=false;
  1429. {main_procinfo:=nil;
  1430. init_procinfo:=nil;
  1431. finalize_procinfo:=nil;}
  1432. if not RelocSectionSetExplicitly then
  1433. RelocSection:=true;
  1434. { Relocation works only without stabs under Windows when }
  1435. { external linker (LD) is used. LD generates relocs for }
  1436. { stab sections which is not loaded in memory. It causes }
  1437. { AV error when DLL is loaded and relocation is needed. }
  1438. { Internal linker does not have this problem. }
  1439. if RelocSection and
  1440. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1441. (cs_link_extern in current_settings.globalswitches) then
  1442. begin
  1443. include(current_settings.globalswitches,cs_link_strip);
  1444. { Warning stabs info does not work with reloc section !! }
  1445. if (cs_debuginfo in current_settings.moduleswitches) and
  1446. (target_dbg.id=dbg_stabs) then
  1447. begin
  1448. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1449. Message(parser_w_parser_win32_debug_needs_WN);
  1450. exclude(current_settings.moduleswitches,cs_debuginfo);
  1451. end;
  1452. end;
  1453. { get correct output names }
  1454. main_file := current_scanner.inputfile;
  1455. while assigned(main_file.next) do
  1456. main_file := main_file.next;
  1457. current_module.SetFileName(main_file.path+main_file.name,true);
  1458. { consume _PACKAGE word }
  1459. consume(_ID);
  1460. module_name:=orgpattern;
  1461. consume(_ID);
  1462. while token=_POINT do
  1463. begin
  1464. consume(_POINT);
  1465. module_name:=module_name+'.'+orgpattern;
  1466. consume(_ID);
  1467. end;
  1468. current_module.setmodulename(module_name);
  1469. current_module.ispackage:=true;
  1470. exportlib.preparelib(module_name);
  1471. if tf_library_needs_pic in target_info.flags then
  1472. include(current_settings.moduleswitches,cs_create_pic);
  1473. consume(_SEMICOLON);
  1474. { global switches are read, so further changes aren't allowed }
  1475. current_module.in_global:=false;
  1476. { setup things using the switches }
  1477. setupglobalswitches;
  1478. { set implementation flag }
  1479. current_module.in_interface:=false;
  1480. current_module.interface_compiled:=true;
  1481. { insert after the unit symbol tables the static symbol table }
  1482. { of the program }
  1483. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1484. {Load the units used by the program we compile.}
  1485. if token=_REQUIRES then
  1486. begin
  1487. end;
  1488. {Load the units used by the program we compile.}
  1489. if (token=_ID) and (idtoken=_CONTAINS) then
  1490. begin
  1491. { consume _CONTAINS word }
  1492. consume(_ID);
  1493. while true do
  1494. begin
  1495. if token=_ID then
  1496. begin
  1497. module_name:=pattern;
  1498. consume(_ID);
  1499. while token=_POINT do
  1500. begin
  1501. consume(_POINT);
  1502. module_name:=module_name+'.'+orgpattern;
  1503. consume(_ID);
  1504. end;
  1505. AddUnit(module_name);
  1506. end
  1507. else
  1508. consume(_ID);
  1509. if token=_COMMA then
  1510. consume(_COMMA)
  1511. else break;
  1512. end;
  1513. consume(_SEMICOLON);
  1514. end;
  1515. { All units are read, now give them a number }
  1516. current_module.updatemaps;
  1517. {Insert the name of the main program into the symbol table.}
  1518. if current_module.realmodulename^<>'' then
  1519. tabstractunitsymtable(current_module.localsymtable).insertunit(tunitsym.create(current_module.realmodulename^,current_module));
  1520. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1521. symtablestack.push(current_module.localsymtable);
  1522. { create whole program optimisation information }
  1523. current_module.wpoinfo:=tunitwpoinfo.create;
  1524. { should we force unit initialization? }
  1525. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1526. if force_init_final or cnodeutils.force_init then
  1527. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1528. { Add symbol to the exports section for win32 so smartlinking a
  1529. DLL will include the edata section }
  1530. if assigned(exportlib) and
  1531. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1532. ((current_module.flags and uf_has_exports)<>0) then
  1533. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1534. { all labels must be defined before generating code }
  1535. if Errorcount=0 then
  1536. tstoredsymtable(current_module.localsymtable).checklabels;
  1537. symtablestack.pop(current_module.localsymtable);
  1538. { consume the last point }
  1539. consume(_END);
  1540. consume(_POINT);
  1541. if (Errorcount=0) then
  1542. begin
  1543. { test static symtable }
  1544. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1545. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1546. tstoredsymtable(current_module.localsymtable).check_forwards;
  1547. current_module.allunitsused;
  1548. end;
  1549. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1550. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1551. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1552. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1553. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1554. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1555. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1556. { leave when we got an error }
  1557. if (Errorcount>0) and not status.skip_error then
  1558. begin
  1559. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1560. status.skip_error:=true;
  1561. exit;
  1562. end;
  1563. { remove all unused units, this happends when units are removed
  1564. from the uses clause in the source and the ppu was already being loaded }
  1565. hp:=tmodule(loaded_units.first);
  1566. while assigned(hp) do
  1567. begin
  1568. hp2:=hp;
  1569. hp:=tmodule(hp.next);
  1570. if hp2.is_unit and
  1571. not assigned(hp2.globalsymtable) then
  1572. loaded_units.remove(hp2);
  1573. end;
  1574. { force exports }
  1575. uu:=tused_unit(usedunits.first);
  1576. while assigned(uu) do
  1577. begin
  1578. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1579. { check localsymtable for exports too to get public symbols }
  1580. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1581. { create special exports }
  1582. if (uu.u.flags and uf_init)<>0 then
  1583. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1584. if (uu.u.flags and uf_finalize)<>0 then
  1585. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1586. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1587. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1588. uu:=tused_unit(uu.next);
  1589. end;
  1590. {$ifdef arm}
  1591. { Insert .pdata section for arm-wince.
  1592. It is needed for exception handling. }
  1593. if target_info.system in [system_arm_wince] then
  1594. InsertPData;
  1595. {$endif arm}
  1596. { generate debuginfo }
  1597. if (cs_debuginfo in current_settings.moduleswitches) then
  1598. current_debuginfo.inserttypeinfo;
  1599. exportlib.generatelib;
  1600. { write all our exports to the import library,
  1601. needs to be done after exportlib.generatelib; }
  1602. createimportlibfromexports;
  1603. { generate imports }
  1604. if current_module.ImportLibraryList.Count>0 then
  1605. importlib.generatelib;
  1606. { Reference all DEBUGINFO sections from the main .fpc section }
  1607. if (cs_debuginfo in current_settings.moduleswitches) then
  1608. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1609. { insert own objectfile }
  1610. insertobjectfile;
  1611. { assemble and link }
  1612. create_objectfile;
  1613. { We might need the symbols info if not using
  1614. the default do_extractsymbolinfo
  1615. which is a dummy function PM }
  1616. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1617. { release all local symtables that are not needed anymore }
  1618. if (not needsymbolinfo) then
  1619. free_localsymtables(current_module.localsymtable);
  1620. { leave when we got an error }
  1621. if (Errorcount>0) and not status.skip_error then
  1622. begin
  1623. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1624. status.skip_error:=true;
  1625. exit;
  1626. end;
  1627. if (not current_module.is_unit) then
  1628. begin
  1629. { finally rewrite all units included into the package }
  1630. uu:=tused_unit(usedunits.first);
  1631. while assigned(uu) do
  1632. begin
  1633. RewritePPU(uu.u.ppufilename,uu.u.ppufilename);
  1634. uu:=tused_unit(uu.next);
  1635. end;
  1636. { create the executable when we are at level 1 }
  1637. if (compile_level=1) then
  1638. begin
  1639. { create global resource file by collecting all resource files }
  1640. CollectResourceFiles;
  1641. { write .def file }
  1642. if (cs_link_deffile in current_settings.globalswitches) then
  1643. deffile.writefile;
  1644. { insert all .o files from all loaded units and
  1645. unload the units, we don't need them anymore.
  1646. Keep the current_module because that is still needed }
  1647. hp:=tmodule(loaded_units.first);
  1648. while assigned(hp) do
  1649. begin
  1650. { the package itself contains no code so far }
  1651. linker.AddModuleFiles(hp);
  1652. hp2:=tmodule(hp.next);
  1653. if (hp<>current_module) and
  1654. (not needsymbolinfo) then
  1655. begin
  1656. loaded_units.remove(hp);
  1657. hp.free;
  1658. end;
  1659. hp:=hp2;
  1660. end;
  1661. linker.MakeSharedLibrary
  1662. end;
  1663. { Give Fatal with error count for linker errors }
  1664. if (Errorcount>0) and not status.skip_error then
  1665. begin
  1666. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1667. status.skip_error:=true;
  1668. end;
  1669. end;
  1670. end;
  1671. procedure proc_program(islibrary : boolean);
  1672. var
  1673. main_file : tinputfile;
  1674. hp,hp2 : tmodule;
  1675. finalize_procinfo,
  1676. init_procinfo,
  1677. main_procinfo : tcgprocinfo;
  1678. force_init_final : boolean;
  1679. resources_used : boolean;
  1680. program_name : ansistring;
  1681. begin
  1682. DLLsource:=islibrary;
  1683. Status.IsLibrary:=IsLibrary;
  1684. Status.IsPackage:=false;
  1685. Status.IsExe:=true;
  1686. parse_only:=false;
  1687. main_procinfo:=nil;
  1688. init_procinfo:=nil;
  1689. finalize_procinfo:=nil;
  1690. resources_used:=false;
  1691. { DLL defaults to create reloc info }
  1692. if islibrary then
  1693. begin
  1694. if not RelocSectionSetExplicitly then
  1695. RelocSection:=true;
  1696. end;
  1697. { Relocation works only without stabs under Windows when }
  1698. { external linker (LD) is used. LD generates relocs for }
  1699. { stab sections which is not loaded in memory. It causes }
  1700. { AV error when DLL is loaded and relocation is needed. }
  1701. { Internal linker does not have this problem. }
  1702. if RelocSection and
  1703. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1704. (cs_link_extern in current_settings.globalswitches) then
  1705. begin
  1706. include(current_settings.globalswitches,cs_link_strip);
  1707. { Warning stabs info does not work with reloc section !! }
  1708. if (cs_debuginfo in current_settings.moduleswitches) and
  1709. (target_dbg.id=dbg_stabs) then
  1710. begin
  1711. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource);
  1712. Message(parser_w_parser_win32_debug_needs_WN);
  1713. exclude(current_settings.moduleswitches,cs_debuginfo);
  1714. end;
  1715. end;
  1716. { get correct output names }
  1717. main_file := current_scanner.inputfile;
  1718. while assigned(main_file.next) do
  1719. main_file := main_file.next;
  1720. current_module.SetFileName(main_file.path+main_file.name,true);
  1721. if islibrary then
  1722. begin
  1723. consume(_LIBRARY);
  1724. program_name:=orgpattern;
  1725. consume(_ID);
  1726. while token=_POINT do
  1727. begin
  1728. consume(_POINT);
  1729. program_name:=program_name+'.'+orgpattern;
  1730. consume(_ID);
  1731. end;
  1732. current_module.setmodulename(program_name);
  1733. current_module.islibrary:=true;
  1734. exportlib.preparelib(program_name);
  1735. if tf_library_needs_pic in target_info.flags then
  1736. include(current_settings.moduleswitches,cs_create_pic);
  1737. consume(_SEMICOLON);
  1738. end
  1739. else
  1740. { is there an program head ? }
  1741. if token=_PROGRAM then
  1742. begin
  1743. consume(_PROGRAM);
  1744. program_name:=orgpattern;
  1745. consume(_ID);
  1746. while token=_POINT do
  1747. begin
  1748. consume(_POINT);
  1749. program_name:=program_name+'.'+orgpattern;
  1750. consume(_ID);
  1751. end;
  1752. current_module.setmodulename(program_name);
  1753. if (target_info.system in systems_unit_program_exports) then
  1754. exportlib.preparelib(program_name);
  1755. if token=_LKLAMMER then
  1756. begin
  1757. consume(_LKLAMMER);
  1758. repeat
  1759. consume(_ID);
  1760. until not try_to_consume(_COMMA);
  1761. consume(_RKLAMMER);
  1762. end;
  1763. consume(_SEMICOLON);
  1764. end
  1765. else if (target_info.system in systems_unit_program_exports) then
  1766. exportlib.preparelib(current_module.realmodulename^);
  1767. { global switches are read, so further changes aren't allowed }
  1768. current_module.in_global:=false;
  1769. { setup things using the switches }
  1770. setupglobalswitches;
  1771. { set implementation flag }
  1772. current_module.in_interface:=false;
  1773. current_module.interface_compiled:=true;
  1774. { insert after the unit symbol tables the static symbol table }
  1775. { of the program }
  1776. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1777. { load standard units (system,objpas,profile unit) }
  1778. loaddefaultunits;
  1779. { Load units provided on the command line }
  1780. loadautounits;
  1781. {Load the units used by the program we compile.}
  1782. if token=_USES then
  1783. loadunits;
  1784. { All units are read, now give them a number }
  1785. current_module.updatemaps;
  1786. {Insert the name of the main program into the symbol table.}
  1787. if current_module.realmodulename^<>'' then
  1788. tabstractunitsymtable(current_module.localsymtable).insertunit(tunitsym.create(current_module.realmodulename^,current_module));
  1789. Message1(parser_u_parsing_implementation,current_module.mainsource);
  1790. symtablestack.push(current_module.localsymtable);
  1791. {$ifdef jvm}
  1792. { fake classdef to represent the class corresponding to the unit }
  1793. addmoduleclass;
  1794. {$endif}
  1795. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1796. maybe_load_got;
  1797. { create whole program optimisation information }
  1798. current_module.wpoinfo:=tunitwpoinfo.create;
  1799. { The program intialization needs an alias, so it can be called
  1800. from the bootstrap code.}
  1801. if islibrary then
  1802. begin
  1803. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1804. { Win32 startup code needs a single name }
  1805. if not(target_info.system in (systems_darwin+systems_aix)) then
  1806. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1807. else
  1808. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1809. end
  1810. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin+systems_aix)) then
  1811. begin
  1812. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1813. end
  1814. else
  1815. begin
  1816. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1817. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1818. end;
  1819. main_procinfo.parse_body;
  1820. { save file pos for debuginfo }
  1821. current_module.mainfilepos:=main_procinfo.entrypos;
  1822. { Generate specializations of objectdefs methods }
  1823. generate_specialization_procs;
  1824. { add implementations for synthetic method declarations added by
  1825. the compiler }
  1826. add_synthetic_method_implementations(current_module.localsymtable);
  1827. { should we force unit initialization? }
  1828. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1829. if force_init_final or cnodeutils.force_init then
  1830. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1831. { Add symbol to the exports section for win32 so smartlinking a
  1832. DLL will include the edata section }
  1833. if assigned(exportlib) and
  1834. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1835. ((current_module.flags and uf_has_exports)<>0) then
  1836. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1837. { finalize? }
  1838. if token=_FINALIZATION then
  1839. begin
  1840. { Parse the finalize }
  1841. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1842. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1843. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1844. finalize_procinfo.parse_body;
  1845. end
  1846. else
  1847. if force_init_final or cnodeutils.force_final then
  1848. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1849. { the finalization routine of libraries is generic (and all libraries need to }
  1850. { be finalized, so they can finalize any units they use }
  1851. if (islibrary) then
  1852. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1853. { all labels must be defined before generating code }
  1854. if Errorcount=0 then
  1855. tstoredsymtable(current_module.localsymtable).checklabels;
  1856. { See remark in unit init/final }
  1857. main_procinfo.generate_code;
  1858. main_procinfo.resetprocdef;
  1859. release_main_proc(main_procinfo);
  1860. if assigned(init_procinfo) then
  1861. begin
  1862. { initialization can be implicit only }
  1863. current_module.flags:=current_module.flags or uf_init;
  1864. init_procinfo.code:=cnodeutils.wrap_proc_body(init_procinfo.procdef,init_procinfo.code);
  1865. init_procinfo.generate_code;
  1866. init_procinfo.resetprocdef;
  1867. release_main_proc(init_procinfo);
  1868. end;
  1869. if assigned(finalize_procinfo) then
  1870. begin
  1871. if force_init_final or
  1872. cnodeutils.force_init or
  1873. not(has_no_code(finalize_procinfo.code)) then
  1874. begin
  1875. finalize_procinfo.code:=cnodeutils.wrap_proc_body(finalize_procinfo.procdef,finalize_procinfo.code);
  1876. finalize_procinfo.generate_code;
  1877. current_module.flags:=current_module.flags or uf_finalize;
  1878. end;
  1879. finalize_procinfo.resetprocdef;
  1880. release_main_proc(finalize_procinfo);
  1881. end;
  1882. symtablestack.pop(current_module.localsymtable);
  1883. { consume the last point }
  1884. consume(_POINT);
  1885. { reset wpo flags for all defs }
  1886. reset_all_defs;
  1887. if (Errorcount=0) then
  1888. begin
  1889. { test static symtable }
  1890. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1891. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1892. tstoredsymtable(current_module.localsymtable).check_forwards;
  1893. current_module.allunitsused;
  1894. end;
  1895. { leave when we got an error }
  1896. if (Errorcount>0) and not status.skip_error then
  1897. begin
  1898. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1899. status.skip_error:=true;
  1900. exit;
  1901. end;
  1902. { remove all unused units, this happens when units are removed
  1903. from the uses clause in the source and the ppu was already being loaded }
  1904. hp:=tmodule(loaded_units.first);
  1905. while assigned(hp) do
  1906. begin
  1907. hp2:=hp;
  1908. hp:=tmodule(hp.next);
  1909. if hp2.is_unit and
  1910. not assigned(hp2.globalsymtable) then
  1911. begin
  1912. loaded_units.remove(hp2);
  1913. unloaded_units.concat(hp2);
  1914. end;
  1915. end;
  1916. { do we need to add the variants unit? }
  1917. maybeloadvariantsunit;
  1918. { Now that everything has been compiled we know if we need resource
  1919. support. If not, remove the unit. }
  1920. resources_used:=MaybeRemoveResUnit;
  1921. linker.initsysinitunitname;
  1922. if target_info.system in systems_internal_sysinit then
  1923. begin
  1924. { add start/halt unit }
  1925. AddUnit(linker.sysinitunit);
  1926. end;
  1927. {$ifdef arm}
  1928. { Insert .pdata section for arm-wince.
  1929. It is needed for exception handling. }
  1930. if target_info.system in [system_arm_wince] then
  1931. InsertPData;
  1932. {$endif arm}
  1933. cnodeutils.InsertThreadvars;
  1934. { generate rtti/init tables }
  1935. write_persistent_type_info(current_module.localsymtable,false);
  1936. { if an Objective-C module, generate rtti and module info }
  1937. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  1938. { generate wrappers for interfaces }
  1939. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  1940. { generate imports }
  1941. if current_module.ImportLibraryList.Count>0 then
  1942. importlib.generatelib;
  1943. { generate debuginfo }
  1944. if (cs_debuginfo in current_settings.moduleswitches) then
  1945. current_debuginfo.inserttypeinfo;
  1946. if islibrary or (target_info.system in systems_unit_program_exports) then
  1947. exportlib.generatelib;
  1948. { Reference all DEBUGINFO sections from the main .fpc section }
  1949. if (cs_debuginfo in current_settings.moduleswitches) then
  1950. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1951. { Resource strings }
  1952. GenerateResourceStrings;
  1953. { Windows widestring needing initialization }
  1954. cnodeutils.InsertWideInits;
  1955. { Resourcestring references (const foo:string=someresourcestring) }
  1956. cnodeutils.InsertResStrInits;
  1957. { insert Tables and StackLength }
  1958. cnodeutils.InsertInitFinalTable;
  1959. cnodeutils.InsertThreadvarTablesTable;
  1960. cnodeutils.InsertResourceTablesTable;
  1961. cnodeutils.InsertWideInitsTablesTable;
  1962. cnodeutils.InsertResStrTablesTable;
  1963. cnodeutils.InsertMemorySizes;
  1964. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1965. if target_info.system in systems_interrupt_table then
  1966. InsertInterruptTable;
  1967. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  1968. { Insert symbol to resource info }
  1969. cnodeutils.InsertResourceInfo(resources_used);
  1970. { create callframe info }
  1971. create_dwarf_frame;
  1972. { insert own objectfile }
  1973. insertobjectfile;
  1974. { assemble and link }
  1975. create_objectfile;
  1976. { We might need the symbols info if not using
  1977. the default do_extractsymbolinfo
  1978. which is a dummy function PM }
  1979. needsymbolinfo:=
  1980. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  1981. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  1982. { release all local symtables that are not needed anymore }
  1983. if (not needsymbolinfo) then
  1984. free_localsymtables(current_module.localsymtable);
  1985. { leave when we got an error }
  1986. if (Errorcount>0) and not status.skip_error then
  1987. begin
  1988. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1989. status.skip_error:=true;
  1990. exit;
  1991. end;
  1992. if (not current_module.is_unit) then
  1993. begin
  1994. { create the executable when we are at level 1 }
  1995. if (compile_level=1) then
  1996. begin
  1997. { create global resource file by collecting all resource files }
  1998. CollectResourceFiles;
  1999. { write .def file }
  2000. if (cs_link_deffile in current_settings.globalswitches) then
  2001. deffile.writefile;
  2002. { insert all .o files from all loaded units and
  2003. unload the units, we don't need them anymore.
  2004. Keep the current_module because that is still needed }
  2005. hp:=tmodule(loaded_units.first);
  2006. while assigned(hp) do
  2007. begin
  2008. linker.AddModuleFiles(hp);
  2009. hp2:=tmodule(hp.next);
  2010. if (hp<>current_module) and
  2011. (not needsymbolinfo) then
  2012. begin
  2013. loaded_units.remove(hp);
  2014. hp.free;
  2015. end;
  2016. hp:=hp2;
  2017. end;
  2018. { free also unneeded units we didn't free before }
  2019. if not needsymbolinfo then
  2020. unloaded_units.Clear;
  2021. { finally we can create a executable }
  2022. if DLLSource then
  2023. linker.MakeSharedLibrary
  2024. else
  2025. linker.MakeExecutable;
  2026. { collect all necessary information for whole-program optimization }
  2027. wpoinfomanager.extractwpoinfofromprogram;
  2028. end;
  2029. { Give Fatal with error count for linker errors }
  2030. if (Errorcount>0) and not status.skip_error then
  2031. begin
  2032. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2033. status.skip_error:=true;
  2034. end;
  2035. end;
  2036. end;
  2037. end.