pmodules.pas 53 KB

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