pmodules.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. {
  2. Copyright (c) 1998-2002 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. procedure proc_unit;
  21. procedure proc_program(islibrary : boolean);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,version,systems,tokens,
  26. cutils,cfileutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmdata,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,ncgutil,
  32. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  33. cresstr,procinfo,
  34. pexports,
  35. scanner,pbase,pexpr,psystem,psub,pdecsub,ptype;
  36. procedure create_objectfile;
  37. var
  38. DLLScanner : TDLLScanner;
  39. s : string;
  40. KeepShared : TStringList;
  41. begin
  42. { try to create import entries from system dlls }
  43. if (tf_has_dllscanner in target_info.flags) and
  44. (not current_module.linkOtherSharedLibs.Empty) then
  45. begin
  46. { Init DLLScanner }
  47. if assigned(CDLLScanner[target_info.system]) then
  48. DLLScanner:=CDLLScanner[target_info.system].Create
  49. else
  50. internalerror(200104121);
  51. KeepShared:=TStringList.Create;
  52. { Walk all shared libs }
  53. While not current_module.linkOtherSharedLibs.Empty do
  54. begin
  55. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  56. if not DLLScanner.scan(s) then
  57. KeepShared.Concat(s);
  58. end;
  59. DLLscanner.Free;
  60. { Recreate import section }
  61. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  62. begin
  63. if assigned(current_asmdata.asmlists[al_imports]) then
  64. current_asmdata.asmlists[al_imports].clear
  65. else
  66. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  67. importlib.generatelib;
  68. end;
  69. { Readd the not processed files }
  70. while not KeepShared.Empty do
  71. begin
  72. s:=KeepShared.GetFirst;
  73. current_module.linkOtherSharedLibs.add(s,link_always);
  74. end;
  75. KeepShared.Free;
  76. end;
  77. { Start and end module debuginfo, at least required for stabs
  78. to insert n_sourcefile lines }
  79. if (cs_debuginfo in current_settings.moduleswitches) or
  80. (cs_use_lineinfo in current_settings.globalswitches) then
  81. debuginfo.insertmoduleinfo;
  82. { create the .s file and assemble it }
  83. GenerateAsm(false);
  84. { Also create a smartlinked version ? }
  85. if (cs_create_smart in current_settings.moduleswitches) and
  86. (not use_smartlink_section) then
  87. begin
  88. GenerateAsm(true);
  89. if (af_needar in target_asm.flags) then
  90. Linker.MakeStaticLibrary;
  91. end;
  92. { resource files }
  93. CompileResourceFiles;
  94. end;
  95. procedure insertobjectfile;
  96. { Insert the used object file for this unit in the used list for this unit }
  97. begin
  98. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  99. current_module.flags:=current_module.flags or uf_static_linked;
  100. if (cs_create_smart in current_settings.moduleswitches) and
  101. not use_smartlink_section then
  102. begin
  103. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  104. current_module.flags:=current_module.flags or uf_smart_linked;
  105. end;
  106. end;
  107. procedure create_dwarf;
  108. begin
  109. { Dwarf conflicts with smartlinking in separate .a files }
  110. if (cs_create_smart in current_settings.moduleswitches) and
  111. not use_smartlink_section then
  112. exit;
  113. { Call frame information }
  114. if (tf_needs_dwarf_cfi in target_info.flags) and
  115. (af_supports_dwarf in target_asm.flags) then
  116. begin
  117. current_asmdata.asmlists[al_dwarf]:=TAsmList.create;
  118. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  119. end;
  120. end;
  121. procedure InsertThreadvarTablesTable;
  122. var
  123. hp : tused_unit;
  124. ltvTables : TAsmList;
  125. count : longint;
  126. begin
  127. if (tf_section_threadvars in target_info.flags) then
  128. exit;
  129. ltvTables:=TAsmList.Create;
  130. count:=0;
  131. hp:=tused_unit(usedunits.first);
  132. while assigned(hp) do
  133. begin
  134. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  135. begin
  136. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  137. inc(count);
  138. end;
  139. hp:=tused_unit(hp.next);
  140. end;
  141. { Add program threadvars, if any }
  142. If (current_module.flags and uf_threadvars)=uf_threadvars then
  143. begin
  144. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  145. inc(count);
  146. end;
  147. { Insert TableCount at start }
  148. ltvTables.insert(Tai_const.Create_32bit(count));
  149. { insert in data segment }
  150. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  151. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(aint));
  152. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  153. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  154. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  155. ltvTables.free;
  156. end;
  157. procedure AddToThreadvarList(p:TObject;arg:pointer);
  158. var
  159. ltvTable : TAsmList;
  160. begin
  161. ltvTable:=TAsmList(arg);
  162. if (tsym(p).typ=staticvarsym) and
  163. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  164. begin
  165. { address of threadvar }
  166. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  167. { size of threadvar }
  168. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  169. end;
  170. end;
  171. procedure InsertThreadvars;
  172. var
  173. s : string;
  174. ltvTable : TAsmList;
  175. begin
  176. if (tf_section_threadvars in target_info.flags) then
  177. exit;
  178. ltvTable:=TAsmList.create;
  179. if assigned(current_module.globalsymtable) then
  180. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  181. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  182. if ltvTable.first<>nil then
  183. begin
  184. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  185. { end of the list marker }
  186. ltvTable.concat(tai_const.create_sym(nil));
  187. { add to datasegment }
  188. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  189. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(aint));
  190. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  191. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  192. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  193. current_module.flags:=current_module.flags or uf_threadvars;
  194. end;
  195. ltvTable.Free;
  196. end;
  197. Procedure InsertResourceInfo;
  198. var
  199. hp : tused_unit;
  200. found : Boolean;
  201. I : Integer;
  202. ResourceInfo : TAsmList;
  203. begin
  204. if target_res.id=res_elf then
  205. begin
  206. hp:=tused_unit(usedunits.first);
  207. found:=false;
  208. Found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  209. If not found then
  210. While Assigned(hp) and not Found do
  211. begin
  212. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  213. hp:=tused_unit(hp.next);
  214. end;
  215. ResourceInfo:=TAsmList.Create;
  216. if found then
  217. begin
  218. { Valid pointer to resource information }
  219. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  220. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0));
  221. {$ifdef EXTERNALRESPTRS}
  222. current_module.linkotherofiles.add('resptrs.o',link_always);
  223. {$else EXTERNALRESPTRS}
  224. new_section(ResourceInfo,sec_fpc,'resptrs',4);
  225. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESSYMBOL',AT_DATA,0));
  226. For I:=1 to 32 do
  227. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  228. {$endif EXTERNALRESPTRS}
  229. end
  230. else
  231. begin
  232. { Nil pointer to resource information }
  233. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  234. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  235. end;
  236. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  237. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  238. ResourceInfo.free;
  239. end;
  240. end;
  241. Procedure InsertResourceTablesTable;
  242. var
  243. hp : tmodule;
  244. ResourceStringTables : tasmlist;
  245. count : longint;
  246. begin
  247. ResourceStringTables:=tasmlist.Create;
  248. count:=0;
  249. hp:=tmodule(loaded_units.first);
  250. while assigned(hp) do
  251. begin
  252. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  253. begin
  254. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  255. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  256. inc(count);
  257. end;
  258. hp:=tmodule(hp.next);
  259. end;
  260. { Insert TableCount at start }
  261. ResourceStringTables.insert(Tai_const.Create_aint(count));
  262. { Add to data segment }
  263. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  264. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(aint));
  265. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  266. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  267. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  268. ResourceStringTables.free;
  269. end;
  270. procedure InsertInitFinalTable;
  271. var
  272. hp : tused_unit;
  273. unitinits : TAsmList;
  274. count : longint;
  275. begin
  276. unitinits:=TAsmList.Create;
  277. count:=0;
  278. hp:=tused_unit(usedunits.first);
  279. while assigned(hp) do
  280. begin
  281. { call the unit init code and make it external }
  282. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  283. begin
  284. if (hp.u.flags and uf_init)<>0 then
  285. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  286. else
  287. unitinits.concat(Tai_const.Create_sym(nil));
  288. if (hp.u.flags and uf_finalize)<>0 then
  289. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  290. else
  291. unitinits.concat(Tai_const.Create_sym(nil));
  292. inc(count);
  293. end;
  294. hp:=tused_unit(hp.next);
  295. end;
  296. { Insert initialization/finalization of the program }
  297. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  298. begin
  299. if (current_module.flags and uf_init)<>0 then
  300. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  301. else
  302. unitinits.concat(Tai_const.Create_sym(nil));
  303. if (current_module.flags and uf_finalize)<>0 then
  304. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  305. else
  306. unitinits.concat(Tai_const.Create_sym(nil));
  307. inc(count);
  308. end;
  309. { Insert TableCount,InitCount at start }
  310. unitinits.insert(Tai_const.Create_32bit(0));
  311. unitinits.insert(Tai_const.Create_32bit(count));
  312. { Add to data segment }
  313. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  314. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(aint));
  315. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  316. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  317. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  318. unitinits.free;
  319. end;
  320. procedure insertmemorysizes;
  321. {$IFDEF POWERPC}
  322. var
  323. stkcookie: string;
  324. {$ENDIF POWERPC}
  325. begin
  326. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  327. { Insert Ident of the compiler in the .fpc.version section }
  328. current_asmdata.asmlists[al_globals].concat(Tai_section.create(sec_fpc,'version',0));
  329. current_asmdata.asmlists[al_globals].concat(Tai_align.Create(const_align(32)));
  330. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  331. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  332. { stacksize can be specified and is now simulated }
  333. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(aint));
  334. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(aint)));
  335. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(stacksize));
  336. {$IFDEF POWERPC}
  337. { AmigaOS4 "stack cookie" support }
  338. if ( target_info.system = system_powerpc_amiga ) then
  339. begin
  340. { this symbol is needed to ignite powerpc amigaos' }
  341. { stack allocation magic for us with the given stack size. }
  342. { note: won't work for m68k amigaos or morphos. (KB) }
  343. str(stacksize,stkcookie);
  344. stkcookie:='$STACK: '+stkcookie+#0;
  345. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  346. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  347. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  348. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  349. end;
  350. {$ENDIF POWERPC}
  351. { Initial heapsize }
  352. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  353. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(aint));
  354. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(aint)));
  355. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(heapsize));
  356. { Initial heapsize }
  357. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  358. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  359. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  360. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  361. end;
  362. procedure AddUnit(const s:string);
  363. var
  364. hp : tppumodule;
  365. unitsym : tunitsym;
  366. begin
  367. { load unit }
  368. hp:=registerunit(current_module,s,'');
  369. hp.loadppu;
  370. hp.adddependency(current_module);
  371. { add to symtable stack }
  372. symtablestack.push(hp.globalsymtable);
  373. if (m_mac in current_settings.modeswitches) and
  374. assigned(hp.globalmacrosymtable) then
  375. macrosymtablestack.push(hp.globalmacrosymtable);
  376. { insert unitsym }
  377. unitsym:=tunitsym.create(s,hp);
  378. inc(unitsym.refs);
  379. current_module.localsymtable.insert(unitsym);
  380. { add to used units }
  381. current_module.addusedunit(hp,false,unitsym);
  382. end;
  383. procedure maybeloadvariantsunit;
  384. var
  385. hp : tmodule;
  386. begin
  387. { Do we need the variants unit? Skip this
  388. for VarUtils unit for bootstrapping }
  389. if (current_module.flags and uf_uses_variants=0) or
  390. (current_module.modulename^='VARUTILS') then
  391. exit;
  392. { Variants unit already loaded? }
  393. hp:=tmodule(loaded_units.first);
  394. while assigned(hp) do
  395. begin
  396. if hp.modulename^='VARIANTS' then
  397. exit;
  398. hp:=tmodule(hp.next);
  399. end;
  400. { Variants unit is not loaded yet, load it now }
  401. Message(parser_w_implicit_uses_of_variants_unit);
  402. AddUnit('Variants');
  403. end;
  404. procedure loaddefaultunits;
  405. begin
  406. { we are going to rebuild the symtablestack, clear it first }
  407. symtablestack.clear;
  408. macrosymtablestack.clear;
  409. { macro symtable }
  410. macrosymtablestack.push(initialmacrosymtable);
  411. { are we compiling the system unit? }
  412. if (cs_compilesystem in current_settings.moduleswitches) then
  413. begin
  414. systemunit:=tglobalsymtable(current_module.localsymtable);
  415. { create system defines }
  416. create_intern_symbols;
  417. create_intern_types;
  418. { Set the owner of errorsym and errortype to symtable to
  419. prevent crashes when accessing .owner }
  420. generrorsym.owner:=systemunit;
  421. generrordef.owner:=systemunit;
  422. exit;
  423. end;
  424. { insert the system unit, it is allways the first. Load also the
  425. internal types from the system unit }
  426. AddUnit('System');
  427. systemunit:=tglobalsymtable(symtablestack.top);
  428. load_intern_types;
  429. { Set the owner of errorsym and errortype to symtable to
  430. prevent crashes when accessing .owner }
  431. generrorsym.owner:=systemunit;
  432. generrordef.owner:=systemunit;
  433. { Units only required for main module }
  434. if not(current_module.is_unit) then
  435. begin
  436. { Heaptrc unit, load heaptrace before any other units especially objpas }
  437. if (cs_use_heaptrc in current_settings.globalswitches) then
  438. AddUnit('HeapTrc');
  439. { Lineinfo unit }
  440. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  441. if (paratargetdbg = dbg_stabs) then
  442. AddUnit('lineinfo')
  443. else
  444. AddUnit('lnfodwrf');
  445. end;
  446. { Valgrind requires c memory manager }
  447. if (cs_gdb_valgrind in current_settings.globalswitches) then
  448. AddUnit('CMem');
  449. {$ifdef cpufpemu}
  450. { Floating point emulation unit?
  451. softfpu must be in the system unit anyways (FK)
  452. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  453. AddUnit('SoftFpu');
  454. }
  455. {$endif cpufpemu}
  456. end;
  457. { Objpas unit? }
  458. if m_objpas in current_settings.modeswitches then
  459. AddUnit('ObjPas');
  460. { Macpas unit? }
  461. if m_mac in current_settings.modeswitches then
  462. AddUnit('MacPas');
  463. { Profile unit? Needed for go32v2 only }
  464. if (cs_profile in current_settings.moduleswitches) and
  465. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  466. AddUnit('Profile');
  467. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  468. begin
  469. AddUnit('FPCylix');
  470. AddUnit('DynLibs');
  471. end;
  472. end;
  473. procedure loadautounits;
  474. var
  475. hs,s : string;
  476. begin
  477. hs:=autoloadunits;
  478. repeat
  479. s:=GetToken(hs,',');
  480. if s='' then
  481. break;
  482. AddUnit(s);
  483. until false;
  484. end;
  485. procedure loadunits;
  486. var
  487. s,sorg : TIDString;
  488. fn : string;
  489. pu : tused_unit;
  490. hp2 : tmodule;
  491. unitsym : tunitsym;
  492. begin
  493. consume(_USES);
  494. repeat
  495. s:=pattern;
  496. sorg:=orgpattern;
  497. consume(_ID);
  498. { support "<unit> in '<file>'" construct, but not for tp7 }
  499. fn:='';
  500. if not(m_tp7 in current_settings.modeswitches) and
  501. try_to_consume(_OP_IN) then
  502. fn:=FixFileName(get_stringconst);
  503. { Give a warning if lineinfo is loaded }
  504. if s='LINEINFO' then begin
  505. Message(parser_w_no_lineinfo_use_switch);
  506. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  507. s := 'LNFODWRF';
  508. sorg := s;
  509. end;
  510. { Give a warning if objpas is loaded }
  511. if s='OBJPAS' then
  512. Message(parser_w_no_objpas_use_mode);
  513. { Using the unit itself is not possible }
  514. if (s<>current_module.modulename^) then
  515. begin
  516. { check if the unit is already used }
  517. hp2:=nil;
  518. pu:=tused_unit(current_module.used_units.first);
  519. while assigned(pu) do
  520. begin
  521. if (pu.u.modulename^=s) then
  522. begin
  523. hp2:=pu.u;
  524. break;
  525. end;
  526. pu:=tused_unit(pu.next);
  527. end;
  528. if not assigned(hp2) then
  529. hp2:=registerunit(current_module,sorg,fn)
  530. else
  531. Message1(sym_e_duplicate_id,s);
  532. { Create unitsym, we need to use the name as specified, we
  533. can not use the modulename because that can be different
  534. when -Un is used }
  535. unitsym:=tunitsym.create(sorg,nil);
  536. current_module.localsymtable.insert(unitsym);
  537. { the current module uses the unit hp2 }
  538. current_module.addusedunit(hp2,true,unitsym);
  539. end
  540. else
  541. Message1(sym_e_duplicate_id,s);
  542. if token=_COMMA then
  543. begin
  544. pattern:='';
  545. consume(_COMMA);
  546. end
  547. else
  548. break;
  549. until false;
  550. { Load the units }
  551. pu:=tused_unit(current_module.used_units.first);
  552. while assigned(pu) do
  553. begin
  554. { Only load the units that are in the current
  555. (interface/implementation) uses clause }
  556. if pu.in_uses and
  557. (pu.in_interface=current_module.in_interface) then
  558. begin
  559. tppumodule(pu.u).loadppu;
  560. { is our module compiled? then we can stop }
  561. if current_module.state=ms_compiled then
  562. exit;
  563. { add this unit to the dependencies }
  564. pu.u.adddependency(current_module);
  565. { save crc values }
  566. pu.checksum:=pu.u.crc;
  567. pu.interface_checksum:=pu.u.interface_crc;
  568. { connect unitsym to the module }
  569. pu.unitsym.module:=pu.u;
  570. { add to symtable stack }
  571. symtablestack.push(pu.u.globalsymtable);
  572. if (m_mac in current_settings.modeswitches) and
  573. assigned(pu.u.globalmacrosymtable) then
  574. macrosymtablestack.push(pu.u.globalmacrosymtable);
  575. end;
  576. pu:=tused_unit(pu.next);
  577. end;
  578. consume(_SEMICOLON);
  579. end;
  580. procedure reset_all_defs;
  581. procedure reset_used_unit_defs(hp:tmodule);
  582. var
  583. pu : tused_unit;
  584. begin
  585. pu:=tused_unit(hp.used_units.first);
  586. while assigned(pu) do
  587. begin
  588. if not pu.u.is_reset then
  589. begin
  590. { prevent infinte loop for circular dependencies }
  591. pu.u.is_reset:=true;
  592. if assigned(pu.u.globalsymtable) then
  593. begin
  594. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  595. reset_used_unit_defs(pu.u);
  596. end;
  597. end;
  598. pu:=tused_unit(pu.next);
  599. end;
  600. end;
  601. var
  602. hp2 : tmodule;
  603. begin
  604. hp2:=tmodule(loaded_units.first);
  605. while assigned(hp2) do
  606. begin
  607. hp2.is_reset:=false;
  608. hp2:=tmodule(hp2.next);
  609. end;
  610. reset_used_unit_defs(current_module);
  611. end;
  612. procedure free_localsymtables(st:TSymtable);
  613. var
  614. i : longint;
  615. def : tstoreddef;
  616. pd : tprocdef;
  617. begin
  618. for i:=0 to st.DefList.Count-1 do
  619. begin
  620. def:=tstoreddef(st.DefList[i]);
  621. if def.typ=procdef then
  622. begin
  623. pd:=tprocdef(def);
  624. if assigned(pd.localst) and
  625. (pd.localst.symtabletype<>staticsymtable) and
  626. not(po_inline in pd.procoptions) then
  627. begin
  628. free_localsymtables(pd.localst);
  629. pd.localst.free;
  630. pd.localst:=nil;
  631. end;
  632. end;
  633. end;
  634. end;
  635. procedure parse_implementation_uses;
  636. begin
  637. if token=_USES then
  638. loadunits;
  639. end;
  640. procedure setupglobalswitches;
  641. begin
  642. if (cs_create_pic in current_settings.moduleswitches) then
  643. begin
  644. def_system_macro('FPC_PIC');
  645. def_system_macro('PIC');
  646. end;
  647. end;
  648. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  649. var
  650. ps : tprocsym;
  651. pd : tprocdef;
  652. begin
  653. { there should be no current_procinfo available }
  654. if assigned(current_procinfo) then
  655. internalerror(200304275);
  656. {Generate a procsym for main}
  657. ps:=tprocsym.create('$'+name);
  658. { main are allways used }
  659. inc(ps.refs);
  660. st.insert(ps);
  661. pd:=tprocdef.create(main_program_level);
  662. include(pd.procoptions,po_global);
  663. pd.procsym:=ps;
  664. ps.ProcdefList.Add(pd);
  665. { set procdef options }
  666. pd.proctypeoption:=potype;
  667. pd.proccalloption:=pocall_default;
  668. pd.forwarddef:=false;
  669. pd.setmangledname(target_info.cprefix+name);
  670. pd.aliasnames.insert(pd.mangledname);
  671. handle_calling_convention(pd);
  672. { We don't need is a local symtable. Change it into the static
  673. symtable }
  674. pd.localst.free;
  675. pd.localst:=st;
  676. { set procinfo and current_procinfo.procdef }
  677. result:=tcgprocinfo(cprocinfo.create(nil));
  678. result.procdef:=pd;
  679. { main proc does always a call e.g. to init system unit }
  680. include(result.flags,pi_do_call);
  681. end;
  682. procedure release_main_proc(pi:tcgprocinfo);
  683. begin
  684. { remove localst as it was replaced by staticsymtable }
  685. pi.procdef.localst:=nil;
  686. { remove procinfo }
  687. current_module.procinfo:=nil;
  688. pi.free;
  689. pi:=nil;
  690. end;
  691. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  692. begin
  693. { update module flags }
  694. current_module.flags:=current_module.flags or flag;
  695. { create procdef }
  696. case flag of
  697. uf_init :
  698. begin
  699. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  700. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  701. end;
  702. uf_finalize :
  703. begin
  704. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  705. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  706. end;
  707. else
  708. internalerror(200304253);
  709. end;
  710. result.code:=cnothingnode.create;
  711. end;
  712. procedure copy_macro(p:TObject; arg:pointer);
  713. begin
  714. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  715. end;
  716. procedure proc_unit;
  717. function is_assembler_generated:boolean;
  718. var
  719. hal : tasmlisttype;
  720. begin
  721. result:=false;
  722. if Errorcount=0 then
  723. begin
  724. for hal:=low(TasmlistType) to high(TasmlistType) do
  725. if not current_asmdata.asmlists[hal].empty then
  726. begin
  727. result:=true;
  728. exit;
  729. end;
  730. end;
  731. end;
  732. var
  733. main_file: tinputfile;
  734. {$ifdef EXTDEBUG}
  735. store_crc,
  736. {$endif EXTDEBUG}
  737. store_interface_crc : cardinal;
  738. s1,s2 : ^string; {Saves stack space}
  739. force_init_final : boolean;
  740. init_procinfo,
  741. finalize_procinfo : tcgprocinfo;
  742. unitname8 : string[8];
  743. has_impl,ag: boolean;
  744. {$ifdef i386}
  745. gotvarsym : tstaticvarsym;
  746. {$endif i386}
  747. begin
  748. init_procinfo:=nil;
  749. finalize_procinfo:=nil;
  750. if m_mac in current_settings.modeswitches then
  751. current_module.mode_switch_allowed:= false;
  752. consume(_UNIT);
  753. if compile_level=1 then
  754. Status.IsExe:=false;
  755. if token=_ID then
  756. begin
  757. { create filenames and unit name }
  758. main_file := current_scanner.inputfile;
  759. while assigned(main_file.next) do
  760. main_file := main_file.next;
  761. new(s1);
  762. s1^:=current_module.modulename^;
  763. current_module.SetFileName(main_file.path^+main_file.name^,true);
  764. current_module.SetModuleName(orgpattern);
  765. { check for system unit }
  766. new(s2);
  767. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  768. unitname8:=copy(current_module.modulename^,1,8);
  769. if (cs_check_unit_name in current_settings.globalswitches) and
  770. (
  771. not(
  772. (current_module.modulename^=s2^) or
  773. (
  774. (length(current_module.modulename^)>8) and
  775. (unitname8=s2^)
  776. )
  777. )
  778. or
  779. (
  780. (length(s1^)>8) and
  781. (s1^<>current_module.modulename^)
  782. )
  783. ) then
  784. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  785. if (current_module.modulename^='SYSTEM') then
  786. include(current_settings.moduleswitches,cs_compilesystem);
  787. dispose(s2);
  788. dispose(s1);
  789. end;
  790. if (target_info.system in system_unit_program_exports) then
  791. exportlib.preparelib(current_module.realmodulename^);
  792. consume(_ID);
  793. consume(_SEMICOLON);
  794. consume(_INTERFACE);
  795. { global switches are read, so further changes aren't allowed }
  796. current_module.in_global:=false;
  797. { handle the global switches }
  798. setupglobalswitches;
  799. message1(unit_u_loading_interface_units,current_module.modulename^);
  800. { update status }
  801. status.currentmodule:=current_module.realmodulename^;
  802. { maybe turn off m_objpas if we are compiling objpas }
  803. if (current_module.modulename^='OBJPAS') then
  804. exclude(current_settings.modeswitches,m_objpas);
  805. { maybe turn off m_mac if we are compiling macpas }
  806. if (current_module.modulename^='MACPAS') then
  807. exclude(current_settings.modeswitches,m_mac);
  808. parse_only:=true;
  809. { generate now the global symboltable,
  810. define first as local to overcome dependency conflicts }
  811. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  812. { insert unitsym of this unit to prevent other units having
  813. the same name }
  814. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  815. { load default units, like the system unit }
  816. loaddefaultunits;
  817. { insert qualifier for the system unit (allows system.writeln) }
  818. if not(cs_compilesystem in current_settings.moduleswitches) and
  819. (token=_USES) then
  820. begin
  821. loadunits;
  822. { has it been compiled at a higher level ?}
  823. if current_module.state=ms_compiled then
  824. exit;
  825. end;
  826. { move the global symtable from the temporary local to global }
  827. current_module.globalsymtable:=current_module.localsymtable;
  828. current_module.localsymtable:=nil;
  829. reset_all_defs;
  830. { number all units, so we know if a unit is used by this unit or
  831. needs to be added implicitly }
  832. current_module.updatemaps;
  833. { ... parse the declarations }
  834. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  835. symtablestack.push(current_module.globalsymtable);
  836. read_interface_declarations;
  837. symtablestack.pop(current_module.globalsymtable);
  838. { Export macros defined in the interface for macpas. The macros
  839. are put in the globalmacrosymtable that will only be used by other
  840. units. The current unit continues to use the localmacrosymtable }
  841. if (m_mac in current_settings.modeswitches) then
  842. begin
  843. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  844. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  845. end;
  846. { leave when we got an error }
  847. if (Errorcount>0) and not status.skip_error then
  848. begin
  849. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  850. status.skip_error:=true;
  851. exit;
  852. end;
  853. { Our interface is compiled, generate CRC and switch to implementation }
  854. if not(cs_compilesystem in current_settings.moduleswitches) and
  855. (Errorcount=0) then
  856. tppumodule(current_module).getppucrc;
  857. current_module.in_interface:=false;
  858. current_module.interface_compiled:=true;
  859. { First reload all units depending on our interface, we need to do this
  860. in the implementation part to prevent errorneous circular references }
  861. reload_flagged_units;
  862. { Parse the implementation section }
  863. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  864. has_impl:= false
  865. else
  866. has_impl:= true;
  867. parse_only:=false;
  868. { generates static symbol table }
  869. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  870. {$ifdef i386}
  871. if cs_create_pic in current_settings.moduleswitches then
  872. begin
  873. { insert symbol for got access in assembler code}
  874. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  875. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  876. current_module.localsymtable.insert(gotvarsym);
  877. { avoid unnecessary warnings }
  878. gotvarsym.varstate:=vs_read;
  879. gotvarsym.refs:=1;
  880. end;
  881. {$endif i386}
  882. if has_impl then
  883. begin
  884. consume(_IMPLEMENTATION);
  885. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  886. { Read the implementation units }
  887. parse_implementation_uses;
  888. end;
  889. if current_module.state=ms_compiled then
  890. exit;
  891. { reset ranges/stabs in exported definitions }
  892. reset_all_defs;
  893. { All units are read, now give them a number }
  894. current_module.updatemaps;
  895. symtablestack.push(current_module.globalsymtable);
  896. symtablestack.push(current_module.localsymtable);
  897. if has_impl then
  898. begin
  899. Message1(parser_u_parsing_implementation,current_module.modulename^);
  900. if current_module.in_interface then
  901. internalerror(200212285);
  902. { Compile the unit }
  903. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  904. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  905. init_procinfo.parse_body;
  906. { save file pos for debuginfo }
  907. current_module.mainfilepos:=init_procinfo.entrypos;
  908. end;
  909. { Generate specializations of objectdefs methods }
  910. generate_specialization_procs;
  911. { if the unit contains ansi/widestrings, initialization and
  912. finalization code must be forced }
  913. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  914. tstaticsymtable(current_module.localsymtable).needs_init_final;
  915. { should we force unit initialization? }
  916. { this is a hack, but how can it be done better ? }
  917. if force_init_final and ((current_module.flags and uf_init)=0) then
  918. begin
  919. { first release the not used init procinfo }
  920. if assigned(init_procinfo) then
  921. release_main_proc(init_procinfo);
  922. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  923. end;
  924. { finalize? }
  925. if has_impl and (token=_FINALIZATION) then
  926. begin
  927. { set module options }
  928. current_module.flags:=current_module.flags or uf_finalize;
  929. { Compile the finalize }
  930. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  931. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  932. finalize_procinfo.parse_body;
  933. end
  934. else if force_init_final then
  935. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  936. { Now both init and finalize bodies are read and it is known
  937. which variables are used in both init and finalize we can now
  938. generate the code. This is required to prevent putting a variable in
  939. a register that is also used in the finalize body (PFV) }
  940. if assigned(init_procinfo) then
  941. begin
  942. init_procinfo.generate_code;
  943. init_procinfo.resetprocdef;
  944. release_main_proc(init_procinfo);
  945. end;
  946. if assigned(finalize_procinfo) then
  947. begin
  948. finalize_procinfo.generate_code;
  949. finalize_procinfo.resetprocdef;
  950. release_main_proc(finalize_procinfo);
  951. end;
  952. symtablestack.pop(current_module.localsymtable);
  953. symtablestack.pop(current_module.globalsymtable);
  954. { the last char should always be a point }
  955. consume(_POINT);
  956. if (Errorcount=0) then
  957. begin
  958. { tests, if all (interface) forwards are resolved }
  959. tstoredsymtable(current_module.globalsymtable).check_forwards;
  960. { check if all private fields are used }
  961. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  962. { remove cross unit overloads }
  963. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  964. { test static symtable }
  965. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  966. tstoredsymtable(current_module.localsymtable).allprivatesused;
  967. tstoredsymtable(current_module.localsymtable).check_forwards;
  968. tstoredsymtable(current_module.localsymtable).checklabels;
  969. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  970. { used units }
  971. current_module.allunitsused;
  972. end;
  973. { leave when we got an error }
  974. if (Errorcount>0) and not status.skip_error then
  975. begin
  976. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  977. status.skip_error:=true;
  978. exit;
  979. end;
  980. { do we need to add the variants unit? }
  981. maybeloadvariantsunit;
  982. { generate wrappers for interfaces }
  983. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  984. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  985. { generate pic helpers to load eip if necessary }
  986. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  987. { generate rtti/init tables }
  988. write_persistent_type_info(current_module.globalsymtable);
  989. write_persistent_type_info(current_module.localsymtable);
  990. { Tables }
  991. insertThreadVars;
  992. { Resource strings }
  993. GenerateResourceStrings;
  994. { generate debuginfo }
  995. if (cs_debuginfo in current_settings.moduleswitches) then
  996. debuginfo.inserttypeinfo;
  997. { generate imports }
  998. if current_module.ImportLibraryList.Count>0 then
  999. importlib.generatelib;
  1000. { insert own objectfile, or say that it's in a library
  1001. (no check for an .o when loading) }
  1002. ag:=is_assembler_generated;
  1003. if ag then
  1004. insertobjectfile
  1005. else
  1006. begin
  1007. current_module.flags:=current_module.flags or uf_no_link;
  1008. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1009. end;
  1010. if ag then
  1011. begin
  1012. { create dwarf debuginfo }
  1013. create_dwarf;
  1014. { assemble }
  1015. create_objectfile;
  1016. end;
  1017. { Write out the ppufile after the object file has been created }
  1018. store_interface_crc:=current_module.interface_crc;
  1019. {$ifdef EXTDEBUG}
  1020. store_crc:=current_module.crc;
  1021. {$endif EXTDEBUG}
  1022. if (Errorcount=0) then
  1023. tppumodule(current_module).writeppu;
  1024. if not(cs_compilesystem in current_settings.moduleswitches) then
  1025. if store_interface_crc<>current_module.interface_crc then
  1026. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1027. {$ifdef EXTDEBUG}
  1028. if not(cs_compilesystem in current_settings.moduleswitches) then
  1029. if (store_crc<>current_module.crc) and simplify_ppu then
  1030. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1031. {$endif EXTDEBUG}
  1032. { release all overload references and local symtables that
  1033. are not needed anymore }
  1034. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1035. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1036. free_localsymtables(current_module.globalsymtable);
  1037. free_localsymtables(current_module.localsymtable);
  1038. { leave when we got an error }
  1039. if (Errorcount>0) and not status.skip_error then
  1040. begin
  1041. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1042. status.skip_error:=true;
  1043. exit;
  1044. end;
  1045. Message1(unit_u_finished_compiling,current_module.modulename^);
  1046. end;
  1047. procedure proc_program(islibrary : boolean);
  1048. var
  1049. main_file : tinputfile;
  1050. hp,hp2 : tmodule;
  1051. finalize_procinfo,
  1052. init_procinfo,
  1053. main_procinfo : tcgprocinfo;
  1054. force_init_final : boolean;
  1055. begin
  1056. DLLsource:=islibrary;
  1057. Status.IsLibrary:=IsLibrary;
  1058. Status.IsExe:=true;
  1059. parse_only:=false;
  1060. main_procinfo:=nil;
  1061. init_procinfo:=nil;
  1062. finalize_procinfo:=nil;
  1063. { DLL defaults to create reloc info }
  1064. if islibrary then
  1065. begin
  1066. if not RelocSectionSetExplicitly then
  1067. RelocSection:=true;
  1068. end;
  1069. { relocation works only without stabs under win32 !! PM }
  1070. { internal assembler uses rva for stabs info
  1071. so it should work with relocated DLLs }
  1072. if RelocSection and
  1073. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1074. (target_info.assem<>as_i386_pecoff) then
  1075. begin
  1076. include(current_settings.globalswitches,cs_link_strip);
  1077. { Warning stabs info does not work with reloc section !! }
  1078. if cs_debuginfo in current_settings.moduleswitches then
  1079. begin
  1080. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1081. Message(parser_w_parser_win32_debug_needs_WN);
  1082. exclude(current_settings.moduleswitches,cs_debuginfo);
  1083. end;
  1084. end;
  1085. { get correct output names }
  1086. main_file := current_scanner.inputfile;
  1087. while assigned(main_file.next) do
  1088. main_file := main_file.next;
  1089. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1090. if islibrary then
  1091. begin
  1092. consume(_LIBRARY);
  1093. stringdispose(current_module.modulename);
  1094. stringdispose(current_module.realmodulename);
  1095. current_module.modulename:=stringdup(pattern);
  1096. current_module.realmodulename:=stringdup(orgpattern);
  1097. current_module.islibrary:=true;
  1098. exportlib.preparelib(orgpattern);
  1099. if tf_library_needs_pic in target_info.flags then
  1100. include(current_settings.moduleswitches,cs_create_pic);
  1101. consume(_ID);
  1102. consume(_SEMICOLON);
  1103. end
  1104. else
  1105. { is there an program head ? }
  1106. if token=_PROGRAM then
  1107. begin
  1108. consume(_PROGRAM);
  1109. stringdispose(current_module.modulename);
  1110. stringdispose(current_module.realmodulename);
  1111. current_module.modulename:=stringdup(pattern);
  1112. current_module.realmodulename:=stringdup(orgpattern);
  1113. if (target_info.system in system_unit_program_exports) then
  1114. exportlib.preparelib(orgpattern);
  1115. consume(_ID);
  1116. if token=_LKLAMMER then
  1117. begin
  1118. consume(_LKLAMMER);
  1119. repeat
  1120. consume(_ID);
  1121. until not try_to_consume(_COMMA);
  1122. consume(_RKLAMMER);
  1123. end;
  1124. consume(_SEMICOLON);
  1125. end
  1126. else if (target_info.system in system_unit_program_exports) then
  1127. exportlib.preparelib(current_module.realmodulename^);
  1128. { global switches are read, so further changes aren't allowed }
  1129. current_module.in_global:=false;
  1130. { setup things using the switches }
  1131. setupglobalswitches;
  1132. { set implementation flag }
  1133. current_module.in_interface:=false;
  1134. current_module.interface_compiled:=true;
  1135. { insert after the unit symbol tables the static symbol table }
  1136. { of the program }
  1137. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1138. { load standard units (system,objpas,profile unit) }
  1139. loaddefaultunits;
  1140. { Load units provided on the command line }
  1141. loadautounits;
  1142. {Load the units used by the program we compile.}
  1143. if token=_USES then
  1144. loadunits;
  1145. { reset ranges/stabs in exported definitions }
  1146. reset_all_defs;
  1147. { All units are read, now give them a number }
  1148. current_module.updatemaps;
  1149. {Insert the name of the main program into the symbol table.}
  1150. if current_module.realmodulename^<>'' then
  1151. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1152. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1153. symtablestack.push(current_module.localsymtable);
  1154. { The program intialization needs an alias, so it can be called
  1155. from the bootstrap code.}
  1156. if islibrary then
  1157. begin
  1158. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1159. { Win32 startup code needs a single name }
  1160. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1161. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1162. else
  1163. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1164. end
  1165. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1166. begin
  1167. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1168. end
  1169. else
  1170. begin
  1171. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1172. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1173. end;
  1174. main_procinfo.parse_body;
  1175. { save file pos for debuginfo }
  1176. current_module.mainfilepos:=main_procinfo.entrypos;
  1177. { Generate specializations of objectdefs methods }
  1178. generate_specialization_procs;
  1179. { should we force unit initialization? }
  1180. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1181. if force_init_final then
  1182. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1183. { Add symbol to the exports section for win32 so smartlinking a
  1184. DLL will include the edata section }
  1185. if assigned(exportlib) and
  1186. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1187. ((current_module.flags and uf_has_exports)<>0) then
  1188. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1189. { finalize? }
  1190. if token=_FINALIZATION then
  1191. begin
  1192. { set module options }
  1193. current_module.flags:=current_module.flags or uf_finalize;
  1194. { Parse the finalize }
  1195. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1196. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1197. finalize_procinfo.parse_body;
  1198. end
  1199. else
  1200. if force_init_final then
  1201. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1202. { See remark in unit init/final }
  1203. main_procinfo.generate_code;
  1204. main_procinfo.resetprocdef;
  1205. release_main_proc(main_procinfo);
  1206. if assigned(init_procinfo) then
  1207. begin
  1208. init_procinfo.generate_code;
  1209. init_procinfo.resetprocdef;
  1210. release_main_proc(init_procinfo);
  1211. end;
  1212. if assigned(finalize_procinfo) then
  1213. begin
  1214. finalize_procinfo.generate_code;
  1215. finalize_procinfo.resetprocdef;
  1216. release_main_proc(finalize_procinfo);
  1217. end;
  1218. symtablestack.pop(current_module.localsymtable);
  1219. { consume the last point }
  1220. consume(_POINT);
  1221. if (Errorcount=0) then
  1222. begin
  1223. { test static symtable }
  1224. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1225. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1226. tstoredsymtable(current_module.localsymtable).check_forwards;
  1227. tstoredsymtable(current_module.localsymtable).checklabels;
  1228. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1229. current_module.allunitsused;
  1230. end;
  1231. { leave when we got an error }
  1232. if (Errorcount>0) and not status.skip_error then
  1233. begin
  1234. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1235. status.skip_error:=true;
  1236. exit;
  1237. end;
  1238. { remove all unused units, this happends when units are removed
  1239. from the uses clause in the source and the ppu was already being loaded }
  1240. hp:=tmodule(loaded_units.first);
  1241. while assigned(hp) do
  1242. begin
  1243. hp2:=hp;
  1244. hp:=tmodule(hp.next);
  1245. if hp2.is_unit and
  1246. not assigned(hp2.globalsymtable) then
  1247. loaded_units.remove(hp2);
  1248. end;
  1249. { do we need to add the variants unit? }
  1250. maybeloadvariantsunit;
  1251. linker.initsysinitunitname;
  1252. if target_info.system in system_internal_sysinit then
  1253. begin
  1254. { add start/halt unit }
  1255. AddUnit(linker.sysinitunit);
  1256. end;
  1257. {$ifdef arm}
  1258. { Insert .pdata section for arm-wince.
  1259. It is needed for exception handling. }
  1260. if target_info.system in [system_arm_wince] then
  1261. InsertPData;
  1262. {$endif arm}
  1263. InsertThreadvars;
  1264. { generate pic helpers to load eip if necessary }
  1265. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1266. { generate rtti/init tables }
  1267. write_persistent_type_info(current_module.localsymtable);
  1268. { generate wrappers for interfaces }
  1269. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1270. { generate imports }
  1271. if current_module.ImportLibraryList.Count>0 then
  1272. importlib.generatelib;
  1273. { generate debuginfo }
  1274. if (cs_debuginfo in current_settings.moduleswitches) then
  1275. debuginfo.inserttypeinfo;
  1276. if islibrary or (target_info.system in system_unit_program_exports) then
  1277. exportlib.generatelib;
  1278. { Reference all DEBUGINFO sections from the main .fpc section }
  1279. if (cs_debuginfo in current_settings.moduleswitches) then
  1280. debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1281. { Resource strings }
  1282. GenerateResourceStrings;
  1283. { insert Tables and StackLength }
  1284. insertinitfinaltable;
  1285. InsertThreadvarTablesTable;
  1286. InsertResourceTablesTable;
  1287. insertmemorysizes;
  1288. { Insert symbol to resource info }
  1289. InsertResourceInfo;
  1290. { create dwarf debuginfo }
  1291. create_dwarf;
  1292. { insert own objectfile }
  1293. insertobjectfile;
  1294. { assemble and link }
  1295. create_objectfile;
  1296. { release all local symtables that are not needed anymore }
  1297. free_localsymtables(current_module.localsymtable);
  1298. { leave when we got an error }
  1299. if (Errorcount>0) and not status.skip_error then
  1300. begin
  1301. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1302. status.skip_error:=true;
  1303. exit;
  1304. end;
  1305. if (not current_module.is_unit) then
  1306. begin
  1307. { create the executable when we are at level 1 }
  1308. if (compile_level=1) then
  1309. begin
  1310. { write .def file }
  1311. if (cs_link_deffile in current_settings.globalswitches) then
  1312. deffile.writefile;
  1313. { insert all .o files from all loaded units and
  1314. unload the units, we don't need them anymore.
  1315. Keep the current_module because that is still needed }
  1316. hp:=tmodule(loaded_units.first);
  1317. while assigned(hp) do
  1318. begin
  1319. linker.AddModuleFiles(hp);
  1320. hp2:=tmodule(hp.next);
  1321. if hp<>current_module then
  1322. begin
  1323. loaded_units.remove(hp);
  1324. hp.free;
  1325. end;
  1326. hp:=hp2;
  1327. end;
  1328. { finally we can create a executable }
  1329. if DLLSource then
  1330. linker.MakeSharedLibrary
  1331. else
  1332. linker.MakeExecutable;
  1333. end;
  1334. { Give Fatal with error count for linker errors }
  1335. if (Errorcount>0) and not status.skip_error then
  1336. begin
  1337. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1338. status.skip_error:=true;
  1339. end;
  1340. end;
  1341. end;
  1342. end.