pmodules.pas 56 KB

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