pmodules.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i fpcdefs.inc}
  20. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmcpu,aasmbase,
  30. cgbase,cpuinfo,cgobj,
  31. nbas,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,procinfo,
  34. dwarf,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure fixseg(p:TAAsmoutput; sec:TAsmSectionType; secname: string);
  40. begin
  41. maybe_new_object_file(p);
  42. if target_info.system <> system_powerpc_macos then
  43. p.insert(Tai_section.Create(sec,'',0))
  44. else
  45. p.insert(Tai_section.Create(sec,secname,0));
  46. end;
  47. procedure create_objectfile;
  48. var
  49. DLLScanner : TDLLScanner;
  50. s : string;
  51. KeepShared : TStringList;
  52. begin
  53. { try to create import entries from system dlls }
  54. if target_info.DllScanSupported and
  55. (not current_module.linkOtherSharedLibs.Empty) then
  56. begin
  57. { Init DLLScanner }
  58. if assigned(CDLLScanner[target_info.system]) then
  59. DLLScanner:=CDLLScanner[target_info.system].Create
  60. else
  61. internalerror(200104121);
  62. KeepShared:=TStringList.Create;
  63. { Walk all shared libs }
  64. While not current_module.linkOtherSharedLibs.Empty do
  65. begin
  66. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  67. if not DLLScanner.scan(s) then
  68. KeepShared.Concat(s);
  69. end;
  70. DLLscanner.Free;
  71. { Recreate import section }
  72. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  73. begin
  74. if assigned(importssection)then
  75. importssection.clear
  76. else
  77. importssection:=taasmoutput.Create;
  78. importlib.generatelib;
  79. end;
  80. { Readd the not processed files }
  81. while not KeepShared.Empty do
  82. begin
  83. s:=KeepShared.GetFirst;
  84. current_module.linkOtherSharedLibs.add(s,link_allways);
  85. end;
  86. KeepShared.Free;
  87. end;
  88. { create the .s file and assemble it }
  89. GenerateAsm(false);
  90. { Also create a smartlinked version ? }
  91. if (cs_create_smart in aktmoduleswitches) then
  92. begin
  93. { regenerate the importssection for win32 }
  94. if assigned(importssection) and
  95. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  96. begin
  97. importsSection.clear;
  98. importlib.generatesmartlib;
  99. end;
  100. GenerateAsm(true);
  101. if (af_needar in target_asm.flags) then
  102. Linker.MakeStaticLibrary;
  103. end;
  104. { resource files }
  105. CompileResourceFiles;
  106. end;
  107. procedure insertobjectfile;
  108. { Insert the used object file for this unit in the used list for this unit }
  109. begin
  110. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  111. current_module.flags:=current_module.flags or uf_static_linked;
  112. if (cs_create_smart in aktmoduleswitches) then
  113. begin
  114. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  115. current_module.flags:=current_module.flags or uf_smart_linked;
  116. end;
  117. end;
  118. procedure create_dwarf;
  119. begin
  120. dwarflist:=taasmoutput.create;
  121. { Call frame information }
  122. if (tf_needs_dwarf_cfi in target_info.flags) and
  123. (af_supports_dwarf in target_asm.flags) then
  124. dwarfcfi.generate_code(dwarflist);
  125. end;
  126. procedure insertsegment;
  127. var
  128. oldaktfilepos : tfileposinfo;
  129. {Note: Sections get names in macos only.}
  130. begin
  131. { Insert Ident of the compiler }
  132. if (not (cs_create_smart in aktmoduleswitches))
  133. {$ifndef EXTDEBUG}
  134. and (not current_module.is_unit)
  135. {$endif}
  136. then
  137. begin
  138. { align the first data }
  139. dataSegment.insert(Tai_align.Create(const_align(32)));
  140. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  141. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  142. end;
  143. { align code segment }
  144. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  145. { Insert start and end of sections }
  146. fixseg(codesegment,sec_code,'____seg_code');
  147. fixseg(datasegment,sec_data,'____seg_data');
  148. fixseg(bsssegment,sec_bss,'____seg_bss');
  149. { we should use .rdata section for these two no ?
  150. .rdata is a read only data section (PM) }
  151. fixseg(rttilist,sec_data,'____seg_rtti');
  152. fixseg(consts,sec_data,'____seg_consts');
  153. fixseg(picdata,sec_data,'____seg_picdata');
  154. if assigned(resourcestringlist) then
  155. fixseg(resourcestringlist,sec_data,'____seg_resstrings');
  156. {$ifdef GDB}
  157. if assigned(debuglist) then
  158. begin
  159. oldaktfilepos:=aktfilepos;
  160. aktfilepos.line:=0;
  161. debugList.insert(Tai_symbol.Createname('gcc2_compiled',AT_DATA,0));
  162. debugList.insert(Tai_symbol.Createname('fpc_compiled',AT_DATA,0));
  163. fixseg(debuglist,sec_code,'____seg_debug');
  164. aktfilepos:=oldaktfilepos;
  165. end;
  166. {$endif GDB}
  167. end;
  168. procedure InsertThreadvarTablesTable;
  169. var
  170. hp : tused_unit;
  171. ltvTables : taasmoutput;
  172. count : longint;
  173. begin
  174. ltvTables:=TAAsmOutput.Create;
  175. count:=0;
  176. hp:=tused_unit(usedunits.first);
  177. while assigned(hp) do
  178. begin
  179. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  180. begin
  181. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,0));
  182. inc(count);
  183. end;
  184. hp:=tused_unit(hp.next);
  185. end;
  186. { Add program threadvars, if any }
  187. If (current_module.flags and uf_threadvars)=uf_threadvars then
  188. begin
  189. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,0));
  190. inc(count);
  191. end;
  192. { TableCount }
  193. ltvTables.insert(Tai_const.Create_32bit(count));
  194. ltvTables.insert(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  195. ltvTables.insert(Tai_align.Create(const_align(sizeof(aint))));
  196. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  197. { insert in data segment }
  198. maybe_new_object_file(dataSegment);
  199. dataSegment.concatlist(ltvTables);
  200. ltvTables.free;
  201. end;
  202. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  203. var
  204. ltvTable : taasmoutput;
  205. begin
  206. ltvTable:=taasmoutput(arg);
  207. if (tsym(p).typ=varsym) and
  208. (vo_is_thread_var in tvarsym(p).varoptions) then
  209. begin
  210. { address of threadvar }
  211. ltvTable.concat(tai_const.Createname(tvarsym(p).mangledname,AT_DATA,0));
  212. { size of threadvar }
  213. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  214. end;
  215. end;
  216. procedure InsertThreadvars;
  217. var
  218. s : string;
  219. ltvTable : TAAsmoutput;
  220. begin
  221. ltvTable:=TAAsmoutput.create;
  222. if assigned(current_module.globalsymtable) then
  223. current_module.globalsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  224. current_module.localsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  225. if ltvTable.first<>nil then
  226. begin
  227. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  228. { add begin and end of the list }
  229. ltvTable.insert(tai_symbol.Createname_global(s,AT_DATA,0));
  230. ltvTable.concat(tai_const.create_sym(nil)); { end of list marker }
  231. ltvTable.concat(tai_symbol_end.createname(s));
  232. maybe_new_object_file(dataSegment);
  233. dataSegment.concatlist(ltvTable);
  234. current_module.flags:=current_module.flags or uf_threadvars;
  235. end;
  236. ltvTable.Free;
  237. end;
  238. Procedure InsertResourceTablesTable;
  239. var
  240. hp : tused_unit;
  241. ResourceStringTables : taasmoutput;
  242. count : longint;
  243. begin
  244. ResourceStringTables:=TAAsmOutput.Create;
  245. count:=0;
  246. hp:=tused_unit(usedunits.first);
  247. while assigned(hp) do
  248. begin
  249. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  250. begin
  251. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  252. inc(count);
  253. end;
  254. hp:=tused_unit(hp.next);
  255. end;
  256. { Add program resources, if any }
  257. If ResourceStringList<>Nil then
  258. begin
  259. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  260. Inc(Count);
  261. end;
  262. { TableCount }
  263. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  264. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  265. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  266. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  267. { insert in data segment }
  268. maybe_new_object_file(dataSegment);
  269. dataSegment.concatlist(ResourceStringTables);
  270. ResourceStringTables.free;
  271. end;
  272. procedure InsertInitFinalTable;
  273. var
  274. hp : tused_unit;
  275. unitinits : taasmoutput;
  276. count : longint;
  277. begin
  278. unitinits:=TAAsmOutput.Create;
  279. count:=0;
  280. hp:=tused_unit(usedunits.first);
  281. while assigned(hp) do
  282. begin
  283. { call the unit init code and make it external }
  284. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  285. begin
  286. if (hp.u.flags and uf_init)<>0 then
  287. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  288. else
  289. unitinits.concat(Tai_const.Create_sym(nil));
  290. if (hp.u.flags and uf_finalize)<>0 then
  291. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  292. else
  293. unitinits.concat(Tai_const.Create_sym(nil));
  294. inc(count);
  295. end;
  296. hp:=tused_unit(hp.next);
  297. end;
  298. { Insert initialization/finalization of the program }
  299. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  300. begin
  301. if (current_module.flags and uf_init)<>0 then
  302. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  303. else
  304. unitinits.concat(Tai_const.Create_sym(nil));
  305. if (current_module.flags and uf_finalize)<>0 then
  306. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  307. else
  308. unitinits.concat(Tai_const.Create_sym(nil));
  309. inc(count);
  310. end;
  311. { TableCount,InitCount }
  312. unitinits.insert(Tai_const.Create_32bit(0));
  313. unitinits.insert(Tai_const.Create_32bit(count));
  314. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  315. unitinits.insert(Tai_align.Create(const_align(4)));
  316. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  317. { insert in data segment }
  318. maybe_new_object_file(dataSegment);
  319. dataSegment.concatlist(unitinits);
  320. unitinits.free;
  321. end;
  322. procedure insertmemorysizes;
  323. begin
  324. { stacksize can be specified and is now simulated }
  325. dataSegment.concat(Tai_align.Create(const_align(4)));
  326. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  327. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  328. dataSegment.concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,4));
  329. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  330. end;
  331. procedure loaddefaultunits;
  332. procedure AddUnit(const s:string);
  333. var
  334. hp : tppumodule;
  335. unitsym : tunitsym;
  336. begin
  337. { load unit }
  338. hp:=registerunit(current_module,s,'');
  339. hp.loadppu;
  340. hp.adddependency(current_module);
  341. { add to symtable stack }
  342. tsymtable(hp.globalsymtable).next:=symtablestack;
  343. symtablestack:=hp.globalsymtable;
  344. { insert unitsym }
  345. unitsym:=tunitsym.create(s,hp.globalsymtable);
  346. inc(unitsym.refs);
  347. refsymtable.insert(unitsym);
  348. { add to used units }
  349. current_module.addusedunit(hp,false,unitsym);
  350. end;
  351. begin
  352. { are we compiling the system unit? }
  353. if (cs_compilesystem in aktmoduleswitches) then
  354. begin
  355. { create system defines }
  356. createconstdefs;
  357. { we don't need to reset anything, it's already done in parser.pas }
  358. exit;
  359. end;
  360. { insert the system unit, it is allways the first }
  361. Symtablestack:=nil;
  362. AddUnit('System');
  363. SystemUnit:=TGlobalSymtable(Symtablestack);
  364. { read default constant definitions }
  365. make_ref:=false;
  366. readconstdefs;
  367. make_ref:=true;
  368. { Set the owner of errorsym and errortype to symtable to
  369. prevent crashes when accessing .owner }
  370. generrorsym.owner:=systemunit;
  371. generrortype.def.owner:=systemunit;
  372. {$ifdef cpufpemu}
  373. { Floating point emulation unit? }
  374. if (cs_fp_emulation in aktmoduleswitches) then
  375. AddUnit('SoftFpu');
  376. {$endif cpufpemu}
  377. { Thread support unit? }
  378. if (cs_threading in aktmoduleswitches) then
  379. AddUnit('SysThrds');
  380. { Objpas unit? }
  381. if m_objpas in aktmodeswitches then
  382. AddUnit('ObjPas');
  383. { Macpas unit? }
  384. if m_mac in aktmodeswitches then
  385. AddUnit('MacPas');
  386. { Profile unit? Needed for go32v2 only }
  387. if (cs_profile in aktmoduleswitches) and
  388. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  389. AddUnit('Profile');
  390. { Units only required for main module }
  391. if not(current_module.is_unit) then
  392. begin
  393. { Heaptrc unit }
  394. if (cs_gdb_heaptrc in aktglobalswitches) then
  395. AddUnit('HeapTrc');
  396. { Lineinfo unit }
  397. if (cs_gdb_lineinfo in aktglobalswitches) then
  398. AddUnit('LineInfo');
  399. { Lineinfo unit }
  400. if (cs_gdb_valgrind in aktglobalswitches) then
  401. AddUnit('CMem');
  402. end;
  403. { save default symtablestack }
  404. defaultsymtablestack:=symtablestack;
  405. end;
  406. procedure loadunits;
  407. var
  408. s,sorg : stringid;
  409. fn : string;
  410. pu : tused_unit;
  411. hp2 : tmodule;
  412. hp3 : tsymtable;
  413. unitsym : tunitsym;
  414. begin
  415. consume(_USES);
  416. {$ifdef DEBUG}
  417. test_symtablestack;
  418. {$endif DEBUG}
  419. repeat
  420. s:=pattern;
  421. sorg:=orgpattern;
  422. consume(_ID);
  423. { support "<unit> in '<file>'" construct, but not for tp7 }
  424. if not(m_tp7 in aktmodeswitches) then
  425. begin
  426. if try_to_consume(_OP_IN) then
  427. fn:=FixFileName(get_stringconst)
  428. else
  429. fn:='';
  430. end;
  431. { Give a warning if objpas is loaded }
  432. if s='OBJPAS' then
  433. Message(parser_w_no_objpas_use_mode);
  434. { Using the unit itself is not possible }
  435. if (s<>current_module.modulename^) then
  436. begin
  437. { check if the unit is already used }
  438. hp2:=nil;
  439. pu:=tused_unit(current_module.used_units.first);
  440. while assigned(pu) do
  441. begin
  442. if (pu.u.modulename^=s) then
  443. begin
  444. hp2:=pu.u;
  445. break;
  446. end;
  447. pu:=tused_unit(pu.next);
  448. end;
  449. if not assigned(hp2) then
  450. hp2:=registerunit(current_module,sorg,fn)
  451. else
  452. Message1(sym_e_duplicate_id,s);
  453. { Create unitsym, we need to use the name as specified, we
  454. can not use the modulename because that can be different
  455. when -Un is used }
  456. unitsym:=tunitsym.create(sorg,nil);
  457. refsymtable.insert(unitsym);
  458. { the current module uses the unit hp2 }
  459. current_module.addusedunit(hp2,true,unitsym);
  460. end
  461. else
  462. Message1(sym_e_duplicate_id,s);
  463. if token=_COMMA then
  464. begin
  465. pattern:='';
  466. consume(_COMMA);
  467. end
  468. else
  469. break;
  470. until false;
  471. consume(_SEMICOLON);
  472. { Load the units }
  473. pu:=tused_unit(current_module.used_units.first);
  474. while assigned(pu) do
  475. begin
  476. { Only load the units that are in the current
  477. (interface/implementation) uses clause }
  478. if pu.in_uses and
  479. (pu.in_interface=current_module.in_interface) then
  480. begin
  481. tppumodule(pu.u).loadppu;
  482. { is our module compiled? then we can stop }
  483. if current_module.state=ms_compiled then
  484. exit;
  485. { add this unit to the dependencies }
  486. pu.u.adddependency(current_module);
  487. { save crc values }
  488. pu.checksum:=pu.u.crc;
  489. pu.interface_checksum:=pu.u.interface_crc;
  490. { connect unitsym to the globalsymtable of the unit }
  491. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  492. { increase refs of the unitsym when the unit contains
  493. initialization/finalization code so it doesn't trigger
  494. the unit not used hint }
  495. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  496. inc(pu.unitsym.refs);
  497. end;
  498. pu:=tused_unit(pu.next);
  499. end;
  500. { set the symtable to systemunit so it gets reorderd correctly,
  501. then insert the units in the symtablestack }
  502. pu:=tused_unit(current_module.used_units.first);
  503. symtablestack:=defaultsymtablestack;
  504. while assigned(pu) do
  505. begin
  506. if pu.in_uses then
  507. begin
  508. { Reinsert in symtablestack }
  509. hp3:=symtablestack;
  510. while assigned(hp3) do
  511. begin
  512. { insert units only once ! }
  513. if pu.u.globalsymtable=hp3 then
  514. break;
  515. hp3:=hp3.next;
  516. { unit isn't inserted }
  517. if hp3=nil then
  518. begin
  519. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  520. symtablestack:=tsymtable(pu.u.globalsymtable);
  521. {$ifdef DEBUG}
  522. test_symtablestack;
  523. {$endif DEBUG}
  524. end;
  525. end;
  526. end;
  527. pu:=tused_unit(pu.next);
  528. end;
  529. end;
  530. {$IfDef GDB}
  531. procedure write_gdb_info;
  532. procedure reset_unit_type_info;
  533. var
  534. hp : tmodule;
  535. begin
  536. hp:=tmodule(loaded_units.first);
  537. while assigned(hp) do
  538. begin
  539. hp.is_stab_written:=false;
  540. hp:=tmodule(hp.next);
  541. end;
  542. end;
  543. procedure write_used_unit_type_info(hp:tmodule);
  544. var
  545. pu : tused_unit;
  546. begin
  547. pu:=tused_unit(hp.used_units.first);
  548. while assigned(pu) do
  549. begin
  550. if not pu.u.is_stab_written then
  551. begin
  552. { prevent infinte loop for circular dependencies }
  553. pu.u.is_stab_written:=true;
  554. { write type info from used units, use a depth first
  555. strategy to reduce the recursion in writing all
  556. dependent stabs }
  557. write_used_unit_type_info(pu.u);
  558. if assigned(pu.u.globalsymtable) then
  559. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  560. end;
  561. pu:=tused_unit(pu.next);
  562. end;
  563. end;
  564. var
  565. vardebuglist : taasmoutput;
  566. storefilepos : tfileposinfo;
  567. begin
  568. if not (cs_debuginfo in aktmoduleswitches) then
  569. exit;
  570. storefilepos:=aktfilepos;
  571. aktfilepos:=current_module.mainfilepos;
  572. { include symbol that will be referenced from the program to be sure to
  573. include this debuginfo .o file }
  574. if current_module.is_unit then
  575. begin
  576. current_module.flags:=current_module.flags or uf_has_debuginfo;
  577. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.globalsymtable,''),AT_DATA,0));
  578. end
  579. else
  580. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  581. { first write all global/local symbols again to a temp list. This will flag
  582. all required tdefs. After that the temp list can be removed since the debuginfo is already
  583. written to the stabs when the variables/consts were written }
  584. {$warning Hack to get all needed types}
  585. vardebuglist:=taasmoutput.create;
  586. new_section(vardebuglist,sec_data,'',0);
  587. if assigned(current_module.globalsymtable) then
  588. tglobalsymtable(current_module.globalsymtable).concatstabto(vardebuglist);
  589. if assigned(current_module.localsymtable) then
  590. tstaticsymtable(current_module.localsymtable).concatstabto(vardebuglist);
  591. vardebuglist.free;
  592. { reset unit type info flag }
  593. reset_unit_type_info;
  594. { write used types from the used units }
  595. write_used_unit_type_info(current_module);
  596. { last write the types from this unit }
  597. if assigned(current_module.globalsymtable) then
  598. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  599. if assigned(current_module.localsymtable) then
  600. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  601. { include files }
  602. if (cs_gdb_dbx in aktglobalswitches) then
  603. begin
  604. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  605. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  606. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  607. debugList.concat(Tai_stabs.Create(strpnew('"'+
  608. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  609. tostr(N_EINCL)+',0,0,0')));
  610. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  611. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  612. do_count_dbx:=false;
  613. end;
  614. aktfilepos:=storefilepos;
  615. end;
  616. {$EndIf GDB}
  617. procedure reset_all_defs;
  618. procedure reset_used_unit_defs(hp:tmodule);
  619. var
  620. pu : tused_unit;
  621. begin
  622. pu:=tused_unit(hp.used_units.first);
  623. while assigned(pu) do
  624. begin
  625. if not pu.u.is_reset then
  626. begin
  627. { prevent infinte loop for circular dependencies }
  628. pu.u.is_reset:=true;
  629. if assigned(pu.u.globalsymtable) then
  630. begin
  631. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  632. reset_used_unit_defs(pu.u);
  633. end;
  634. end;
  635. pu:=tused_unit(pu.next);
  636. end;
  637. end;
  638. var
  639. hp2 : tmodule;
  640. begin
  641. hp2:=tmodule(loaded_units.first);
  642. while assigned(hp2) do
  643. begin
  644. hp2.is_reset:=false;
  645. hp2:=tmodule(hp2.next);
  646. end;
  647. reset_used_unit_defs(current_module);
  648. end;
  649. procedure free_localsymtables(st:tsymtable);
  650. var
  651. def : tstoreddef;
  652. pd : tprocdef;
  653. begin
  654. def:=tstoreddef(st.defindex.first);
  655. while assigned(def) do
  656. begin
  657. if def.deftype=procdef then
  658. begin
  659. pd:=tprocdef(def);
  660. if assigned(pd.localst) and
  661. (pd.localst.symtabletype<>staticsymtable) and
  662. not((pd.proccalloption=pocall_inline) or
  663. ((current_module.flags and uf_local_browser)<>0)) then
  664. begin
  665. free_localsymtables(pd.localst);
  666. pd.localst.free;
  667. pd.localst:=nil;
  668. end;
  669. end;
  670. def:=tstoreddef(def.indexnext);
  671. end;
  672. end;
  673. procedure parse_implementation_uses;
  674. begin
  675. if token=_USES then
  676. begin
  677. loadunits;
  678. {$ifdef DEBUG}
  679. test_symtablestack;
  680. {$endif DEBUG}
  681. end;
  682. end;
  683. procedure setupglobalswitches;
  684. begin
  685. { can't have local browser when no global browser }
  686. if (cs_local_browser in aktmoduleswitches) and
  687. not(cs_browser in aktmoduleswitches) then
  688. exclude(aktmoduleswitches,cs_local_browser);
  689. { define a symbol in delphi,objfpc,tp,gpc mode }
  690. if (m_delphi in aktmodeswitches) then
  691. current_scanner.def_macro('FPC_DELPHI')
  692. else
  693. if (m_tp7 in aktmodeswitches) then
  694. current_scanner.def_macro('FPC_TP')
  695. else
  696. if (m_objfpc in aktmodeswitches) then
  697. current_scanner.def_macro('FPC_OBJFPC')
  698. else
  699. if (m_gpc in aktmodeswitches) then
  700. current_scanner.def_macro('FPC_GPC')
  701. else
  702. if (m_mac in aktmodeswitches) then
  703. current_scanner.def_macro('FPC_MACPAS');
  704. end;
  705. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  706. var
  707. stt : tsymtable;
  708. ps : tprocsym;
  709. pd : tprocdef;
  710. begin
  711. { there should be no current_procinfo available }
  712. if assigned(current_procinfo) then
  713. internalerror(200304275);
  714. {Generate a procsym for main}
  715. make_ref:=false;
  716. { try to insert in in static symtable ! }
  717. stt:=symtablestack;
  718. symtablestack:=st;
  719. { generate procsym }
  720. ps:=tprocsym.create('$'+name);
  721. { main are allways used }
  722. inc(ps.refs);
  723. symtablestack.insert(ps);
  724. pd:=tprocdef.create(main_program_level);
  725. include(pd.procoptions,po_public);
  726. pd.procsym:=ps;
  727. ps.addprocdef(pd);
  728. { restore symtable }
  729. make_ref:=true;
  730. symtablestack:=stt;
  731. { set procdef options }
  732. pd.proctypeoption:=potype;
  733. pd.proccalloption:=pocall_default;
  734. pd.forwarddef:=false;
  735. pd.setmangledname(target_info.cprefix+name);
  736. pd.aliasnames.insert(pd.mangledname);
  737. { We don't need is a local symtable. Change it into the static
  738. symtable }
  739. pd.localst.free;
  740. pd.localst:=st;
  741. { set procinfo and current_procinfo.procdef }
  742. current_procinfo:=cprocinfo.create(nil);
  743. current_module.procinfo:=current_procinfo;
  744. current_procinfo.procdef:=pd;
  745. { return procdef }
  746. create_main_proc:=pd;
  747. { main proc does always a call e.g. to init system unit }
  748. include(current_procinfo.flags,pi_do_call);
  749. end;
  750. procedure release_main_proc(pd:tprocdef);
  751. begin
  752. { this is a main proc, so there should be no parent }
  753. if not(assigned(current_procinfo)) or
  754. assigned(current_procinfo.parent) or
  755. not(current_procinfo.procdef=pd) then
  756. internalerror(200304276);
  757. { remove procinfo }
  758. current_module.procinfo:=nil;
  759. current_procinfo.free;
  760. current_procinfo:=nil;
  761. { remove localst as it was replaced by staticsymtable }
  762. pd.localst:=nil;
  763. end;
  764. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  765. var
  766. pd : tprocdef;
  767. begin
  768. { update module flags }
  769. current_module.flags:=current_module.flags or flag;
  770. { create procdef }
  771. case flag of
  772. uf_init :
  773. begin
  774. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  775. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  776. end;
  777. uf_finalize :
  778. begin
  779. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  780. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  781. end;
  782. else
  783. internalerror(200304253);
  784. end;
  785. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  786. tcgprocinfo(current_procinfo).generate_code;
  787. release_main_proc(pd);
  788. end;
  789. procedure proc_unit;
  790. function is_assembler_generated:boolean;
  791. begin
  792. is_assembler_generated:=(Errorcount=0) and
  793. not(
  794. codeSegment.empty and
  795. dataSegment.empty and
  796. bssSegment.empty and
  797. ((importssection=nil) or importsSection.empty) and
  798. ((resourcesection=nil) or resourceSection.empty) and
  799. ((resourcestringlist=nil) or resourcestringList.empty)
  800. );
  801. end;
  802. var
  803. main_file: tinputfile;
  804. st : tsymtable;
  805. unitst : tglobalsymtable;
  806. store_crc,store_interface_crc : cardinal;
  807. s1,s2 : ^string; {Saves stack space}
  808. force_init_final : boolean;
  809. pd : tprocdef;
  810. unitname8 : string[8];
  811. has_impl: boolean;
  812. begin
  813. consume(_UNIT);
  814. if compile_level=1 then
  815. Status.IsExe:=false;
  816. if token=_ID then
  817. begin
  818. { create filenames and unit name }
  819. main_file := current_scanner.inputfile;
  820. while assigned(main_file.next) do
  821. main_file := main_file.next;
  822. new(s1);
  823. s1^:=current_module.modulename^;
  824. current_module.SetFileName(main_file.path^+main_file.name^,true);
  825. current_module.SetModuleName(orgpattern);
  826. { check for system unit }
  827. new(s2);
  828. s2^:=upper(SplitName(main_file.name^));
  829. unitname8:=copy(current_module.modulename^,1,8);
  830. if (cs_check_unit_name in aktglobalswitches) and
  831. (
  832. not(
  833. (current_module.modulename^=s2^) or
  834. (
  835. (length(current_module.modulename^)>8) and
  836. (unitname8=s2^)
  837. )
  838. )
  839. or
  840. (
  841. (length(s1^)>8) and
  842. (s1^<>current_module.modulename^)
  843. )
  844. ) then
  845. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  846. if (current_module.modulename^='SYSTEM') then
  847. include(aktmoduleswitches,cs_compilesystem);
  848. dispose(s2);
  849. dispose(s1);
  850. end;
  851. consume(_ID);
  852. consume(_SEMICOLON);
  853. consume(_INTERFACE);
  854. { global switches are read, so further changes aren't allowed }
  855. current_module.in_global:=false;
  856. { handle the global switches }
  857. setupglobalswitches;
  858. message1(unit_u_loading_interface_units,current_module.modulename^);
  859. { update status }
  860. status.currentmodule:=current_module.realmodulename^;
  861. { maybe turn off m_objpas if we are compiling objpas }
  862. if (current_module.modulename^='OBJPAS') then
  863. exclude(aktmodeswitches,m_objpas);
  864. { maybe turn off m_mac if we are compiling macpas }
  865. if (current_module.modulename^='MACPAS') then
  866. exclude(aktmodeswitches,m_mac);
  867. parse_only:=true;
  868. { generate now the global symboltable }
  869. st:=tglobalsymtable.create(current_module.modulename^);
  870. refsymtable:=st;
  871. unitst:=tglobalsymtable(st);
  872. { define first as local to overcome dependency conflicts }
  873. current_module.localsymtable:=st;
  874. { the unit name must be usable as a unit specifier }
  875. { inside the unit itself (PM) }
  876. { this also forbids to have another symbol }
  877. { with the same name as the unit }
  878. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  879. { load default units, like the system unit }
  880. loaddefaultunits;
  881. { reset }
  882. make_ref:=true;
  883. { insert qualifier for the system unit (allows system.writeln) }
  884. if not(cs_compilesystem in aktmoduleswitches) then
  885. begin
  886. if token=_USES then
  887. begin
  888. loadunits;
  889. { has it been compiled at a higher level ?}
  890. if current_module.state=ms_compiled then
  891. exit;
  892. end;
  893. { ... but insert the symbol table later }
  894. st.next:=symtablestack;
  895. symtablestack:=st;
  896. end
  897. else
  898. { while compiling a system unit, some types are directly inserted }
  899. begin
  900. st.next:=symtablestack;
  901. symtablestack:=st;
  902. insert_intern_types(st);
  903. end;
  904. { now we know the place to insert the constants }
  905. constsymtable:=symtablestack;
  906. { move the global symtab from the temporary local to global }
  907. current_module.globalsymtable:=current_module.localsymtable;
  908. current_module.localsymtable:=nil;
  909. reset_all_defs;
  910. { number all units, so we know if a unit is used by this unit or
  911. needs to be added implicitly }
  912. current_module.numberunits;
  913. { ... parse the declarations }
  914. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  915. read_interface_declarations;
  916. { leave when we got an error }
  917. if (Errorcount>0) and not status.skip_error then
  918. begin
  919. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  920. status.skip_error:=true;
  921. exit;
  922. end;
  923. { Our interface is compiled, generate CRC and switch to implementation }
  924. if not(cs_compilesystem in aktmoduleswitches) and
  925. (Errorcount=0) then
  926. tppumodule(current_module).getppucrc;
  927. current_module.in_interface:=false;
  928. current_module.interface_compiled:=true;
  929. { First reload all units depending on our interface, we need to do this
  930. in the implementation part to prevent errorneous circular references }
  931. reload_flagged_units;
  932. { Parse the implementation section }
  933. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  934. has_impl:= false
  935. else
  936. begin
  937. consume(_IMPLEMENTATION);
  938. has_impl:= true;
  939. end;
  940. if has_impl then
  941. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  942. parse_only:=false;
  943. { generates static symbol table }
  944. st:=tstaticsymtable.create(current_module.modulename^);
  945. current_module.localsymtable:=st;
  946. { remove the globalsymtable from the symtable stack }
  947. { to reinsert it after loading the implementation units }
  948. symtablestack:=unitst.next;
  949. { we don't want implementation units symbols in unitsymtable !! PM }
  950. refsymtable:=st;
  951. { Read the implementation units }
  952. if has_impl then
  953. parse_implementation_uses;
  954. if current_module.state=ms_compiled then
  955. exit;
  956. { reset ranges/stabs in exported definitions }
  957. reset_all_defs;
  958. { All units are read, now give them a number }
  959. current_module.numberunits;
  960. { now we can change refsymtable }
  961. refsymtable:=st;
  962. { but reinsert the global symtable as lasts }
  963. unitst.next:=symtablestack;
  964. symtablestack:=unitst;
  965. {$ifdef DEBUG}
  966. test_symtablestack;
  967. {$endif DEBUG}
  968. constsymtable:=symtablestack;
  969. if has_impl then
  970. begin
  971. Message1(parser_u_parsing_implementation,current_module.modulename^);
  972. if current_module.in_interface then
  973. internalerror(200212285);
  974. { Compile the unit }
  975. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  976. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  977. tcgprocinfo(current_procinfo).parse_body;
  978. tcgprocinfo(current_procinfo).generate_code;
  979. tcgprocinfo(current_procinfo).resetprocdef;
  980. { save file pos for debuginfo }
  981. current_module.mainfilepos:=current_procinfo.entrypos;
  982. release_main_proc(pd);
  983. end;
  984. { if the unit contains ansi/widestrings, initialization and
  985. finalization code must be forced }
  986. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  987. tstaticsymtable(current_module.localsymtable).needs_init_final;
  988. { should we force unit initialization? }
  989. { this is a hack, but how can it be done better ? }
  990. if force_init_final and ((current_module.flags and uf_init)=0) then
  991. gen_implicit_initfinal(uf_init,st);
  992. { finalize? }
  993. if has_impl and (token=_FINALIZATION) then
  994. begin
  995. { set module options }
  996. current_module.flags:=current_module.flags or uf_finalize;
  997. { Compile the finalize }
  998. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  999. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1000. tcgprocinfo(current_procinfo).parse_body;
  1001. tcgprocinfo(current_procinfo).generate_code;
  1002. tcgprocinfo(current_procinfo).resetprocdef;
  1003. release_main_proc(pd);
  1004. end
  1005. else if force_init_final then
  1006. gen_implicit_initfinal(uf_finalize,st);
  1007. { the last char should always be a point }
  1008. consume(_POINT);
  1009. { Generate resoucestrings }
  1010. If ResourceStrings.ResStrCount>0 then
  1011. begin
  1012. ResourceStrings.CreateResourceStringList;
  1013. current_module.flags:=current_module.flags or uf_has_resources;
  1014. { only write if no errors found }
  1015. if (Errorcount=0) then
  1016. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1017. end;
  1018. if (Errorcount=0) then
  1019. begin
  1020. { tests, if all (interface) forwards are resolved }
  1021. tstoredsymtable(symtablestack).check_forwards;
  1022. { check if all private fields are used }
  1023. tstoredsymtable(symtablestack).allprivatesused;
  1024. { remove cross unit overloads }
  1025. tstoredsymtable(symtablestack).unchain_overloaded;
  1026. { test static symtable }
  1027. tstoredsymtable(st).allsymbolsused;
  1028. tstoredsymtable(st).allprivatesused;
  1029. tstoredsymtable(st).check_forwards;
  1030. tstoredsymtable(st).checklabels;
  1031. tstoredsymtable(st).unchain_overloaded;
  1032. { used units }
  1033. current_module.allunitsused;
  1034. end;
  1035. { leave when we got an error }
  1036. if (Errorcount>0) and not status.skip_error then
  1037. begin
  1038. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1039. status.skip_error:=true;
  1040. exit;
  1041. end;
  1042. { generate debuginfo }
  1043. {$ifdef GDB}
  1044. write_gdb_info;
  1045. {$endif GDB}
  1046. { generate a list of threadvars }
  1047. InsertThreadvars;
  1048. { generate imports }
  1049. if current_module.uses_imports then
  1050. importlib.generatelib;
  1051. { insert own objectfile, or say that it's in a library
  1052. (no check for an .o when loading) }
  1053. if is_assembler_generated then
  1054. insertobjectfile
  1055. else
  1056. current_module.flags:=current_module.flags or uf_no_link;
  1057. if cs_local_browser in aktmoduleswitches then
  1058. current_module.localsymtable:=refsymtable;
  1059. if is_assembler_generated then
  1060. begin
  1061. { create dwarf debuginfo }
  1062. create_dwarf;
  1063. { finish asmlist by adding segment starts }
  1064. insertsegment;
  1065. { assemble }
  1066. create_objectfile;
  1067. end;
  1068. { Write out the ppufile after the object file has been created }
  1069. store_interface_crc:=current_module.interface_crc;
  1070. store_crc:=current_module.crc;
  1071. if (Errorcount=0) then
  1072. tppumodule(current_module).writeppu;
  1073. if not(cs_compilesystem in aktmoduleswitches) then
  1074. if store_interface_crc<>current_module.interface_crc then
  1075. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1076. {$ifdef EXTDEBUG}
  1077. if not(cs_compilesystem in aktmoduleswitches) then
  1078. if (store_crc<>current_module.crc) and simplify_ppu then
  1079. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1080. {$endif EXTDEBUG}
  1081. { release all local symtables that are not needed anymore }
  1082. free_localsymtables(current_module.globalsymtable);
  1083. free_localsymtables(current_module.localsymtable);
  1084. { remove static symtable (=refsymtable) here to save some mem }
  1085. if not (cs_local_browser in aktmoduleswitches) then
  1086. begin
  1087. st.free;
  1088. current_module.localsymtable:=nil;
  1089. end;
  1090. { leave when we got an error }
  1091. if (Errorcount>0) and not status.skip_error then
  1092. begin
  1093. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1094. status.skip_error:=true;
  1095. exit;
  1096. end;
  1097. Message1(unit_u_finished_compiling,current_module.modulename^);
  1098. end;
  1099. procedure proc_program(islibrary : boolean);
  1100. var
  1101. main_file : tinputfile;
  1102. st : tsymtable;
  1103. hp,hp2 : tmodule;
  1104. pd : tprocdef;
  1105. begin
  1106. DLLsource:=islibrary;
  1107. Status.IsLibrary:=IsLibrary;
  1108. Status.IsExe:=true;
  1109. parse_only:=false;
  1110. { DLL defaults to create reloc info }
  1111. if islibrary then
  1112. begin
  1113. if not RelocSectionSetExplicitly then
  1114. RelocSection:=true;
  1115. end;
  1116. { relocation works only without stabs under win32 !! PM }
  1117. { internal assembler uses rva for stabs info
  1118. so it should work with relocated DLLs }
  1119. if RelocSection and
  1120. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1121. (target_info.assem<>as_i386_pecoff) then
  1122. begin
  1123. include(aktglobalswitches,cs_link_strip);
  1124. { Warning stabs info does not work with reloc section !! }
  1125. if cs_debuginfo in aktmoduleswitches then
  1126. begin
  1127. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1128. Message(parser_w_parser_win32_debug_needs_WN);
  1129. exclude(aktmoduleswitches,cs_debuginfo);
  1130. end;
  1131. end;
  1132. { get correct output names }
  1133. main_file := current_scanner.inputfile;
  1134. while assigned(main_file.next) do
  1135. main_file := main_file.next;
  1136. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1137. if islibrary then
  1138. begin
  1139. consume(_LIBRARY);
  1140. stringdispose(current_module.modulename);
  1141. current_module.modulename:=stringdup(pattern);
  1142. current_module.islibrary:=true;
  1143. exportlib.preparelib(pattern);
  1144. consume(_ID);
  1145. consume(_SEMICOLON);
  1146. end
  1147. else
  1148. { is there an program head ? }
  1149. if token=_PROGRAM then
  1150. begin
  1151. consume(_PROGRAM);
  1152. stringdispose(current_module.modulename);
  1153. stringdispose(current_module.realmodulename);
  1154. current_module.modulename:=stringdup(pattern);
  1155. current_module.realmodulename:=stringdup(orgpattern);
  1156. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1157. exportlib.preparelib(pattern);
  1158. consume(_ID);
  1159. if token=_LKLAMMER then
  1160. begin
  1161. consume(_LKLAMMER);
  1162. repeat
  1163. consume(_ID);
  1164. until not try_to_consume(_COMMA);
  1165. consume(_RKLAMMER);
  1166. end;
  1167. consume(_SEMICOLON);
  1168. end
  1169. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1170. exportlib.preparelib(current_module.modulename^);
  1171. { global switches are read, so further changes aren't allowed }
  1172. current_module.in_global:=false;
  1173. { setup things using the global switches }
  1174. setupglobalswitches;
  1175. { set implementation flag }
  1176. current_module.in_interface:=false;
  1177. current_module.interface_compiled:=true;
  1178. { insert after the unit symbol tables the static symbol table }
  1179. { of the program }
  1180. st:=tstaticsymtable.create(current_module.modulename^);;
  1181. current_module.localsymtable:=st;
  1182. refsymtable:=st;
  1183. { load standard units (system,objpas,profile unit) }
  1184. loaddefaultunits;
  1185. {Load the units used by the program we compile.}
  1186. if token=_USES then
  1187. loadunits;
  1188. { reset ranges/stabs in exported definitions }
  1189. reset_all_defs;
  1190. { All units are read, now give them a number }
  1191. current_module.numberunits;
  1192. {Insert the name of the main program into the symbol table.}
  1193. if current_module.realmodulename^<>'' then
  1194. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1195. { ...is also constsymtable, this is the symtable where }
  1196. { the elements of enumeration types are inserted }
  1197. constsymtable:=st;
  1198. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1199. { The program intialization needs an alias, so it can be called
  1200. from the bootstrap code.}
  1201. if islibrary or
  1202. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1203. begin
  1204. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1205. { Win32 startup code needs a single name }
  1206. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1207. pd.aliasnames.insert('PASCALMAIN');
  1208. end
  1209. else
  1210. begin
  1211. if (target_info.system = system_i386_netware) or
  1212. (target_info.system = system_i386_netwlibc) then
  1213. begin
  1214. pd:=create_main_proc('PASCALMAIN',potype_proginit,st); { main is need by the netware rtl }
  1215. end else
  1216. begin
  1217. pd:=create_main_proc('main',potype_proginit,st);
  1218. pd.aliasnames.insert('PASCALMAIN');
  1219. end;
  1220. end;
  1221. tcgprocinfo(current_procinfo).parse_body;
  1222. tcgprocinfo(current_procinfo).generate_code;
  1223. tcgprocinfo(current_procinfo).resetprocdef;
  1224. { save file pos for debuginfo }
  1225. current_module.mainfilepos:=current_procinfo.entrypos;
  1226. release_main_proc(pd);
  1227. { should we force unit initialization? }
  1228. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1229. begin
  1230. { initialize section }
  1231. gen_implicit_initfinal(uf_init,st);
  1232. { finalize section }
  1233. gen_implicit_initfinal(uf_finalize,st);
  1234. end;
  1235. { Add symbol to the exports section for win32 so smartlinking a
  1236. DLL will include the edata section }
  1237. if assigned(exportlib) and
  1238. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1239. assigned(current_module._exports.first) then
  1240. codesegment.concat(tai_const.create_sym(exportlib.edatalabel));
  1241. If ResourceStrings.ResStrCount>0 then
  1242. begin
  1243. ResourceStrings.CreateResourceStringList;
  1244. { only write if no errors found }
  1245. if (Errorcount=0) then
  1246. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1247. end;
  1248. { finalize? }
  1249. if token=_FINALIZATION then
  1250. begin
  1251. { set module options }
  1252. current_module.flags:=current_module.flags or uf_finalize;
  1253. { Compile the finalize }
  1254. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1255. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1256. tcgprocinfo(current_procinfo).parse_body;
  1257. tcgprocinfo(current_procinfo).generate_code;
  1258. tcgprocinfo(current_procinfo).resetprocdef;
  1259. release_main_proc(pd);
  1260. end;
  1261. { consume the last point }
  1262. consume(_POINT);
  1263. if (Errorcount=0) then
  1264. begin
  1265. { test static symtable }
  1266. tstoredsymtable(st).allsymbolsused;
  1267. tstoredsymtable(st).allprivatesused;
  1268. tstoredsymtable(st).check_forwards;
  1269. tstoredsymtable(st).checklabels;
  1270. tstoredsymtable(st).unchain_overloaded;
  1271. current_module.allunitsused;
  1272. end;
  1273. { leave when we got an error }
  1274. if (Errorcount>0) and not status.skip_error then
  1275. begin
  1276. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1277. status.skip_error:=true;
  1278. exit;
  1279. end;
  1280. { generate debuginfo }
  1281. {$ifdef GDB}
  1282. write_gdb_info;
  1283. {$endif GDB}
  1284. { generate a list of threadvars }
  1285. InsertThreadvars;
  1286. { generate imports }
  1287. if current_module.uses_imports then
  1288. importlib.generatelib;
  1289. if islibrary or
  1290. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1291. (target_info.system=system_i386_NETWARE) then
  1292. exportlib.generatelib;
  1293. { insert Tables and StackLength }
  1294. insertThreadVarTablesTable;
  1295. insertResourceTablesTable;
  1296. insertinitfinaltable;
  1297. insertmemorysizes;
  1298. { create dwarf debuginfo }
  1299. create_dwarf;
  1300. { finish asmlist by adding segment starts }
  1301. insertsegment;
  1302. { insert own objectfile }
  1303. insertobjectfile;
  1304. { assemble and link }
  1305. create_objectfile;
  1306. { release all local symtables that are not needed anymore }
  1307. free_localsymtables(current_module.localsymtable);
  1308. { leave when we got an error }
  1309. if (Errorcount>0) and not status.skip_error then
  1310. begin
  1311. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1312. status.skip_error:=true;
  1313. exit;
  1314. end;
  1315. { create the executable when we are at level 1 }
  1316. if (compile_level=1) then
  1317. begin
  1318. { remove all unused units, this happends when units are removed
  1319. from the uses clause in the source and the ppu was already being loaded }
  1320. hp:=tmodule(loaded_units.first);
  1321. while assigned(hp) do
  1322. begin
  1323. hp2:=hp;
  1324. hp:=tmodule(hp.next);
  1325. if hp2.is_unit and
  1326. not assigned(hp2.globalsymtable) then
  1327. loaded_units.remove(hp2);
  1328. end;
  1329. { insert all .o files from all loaded units }
  1330. hp:=tmodule(loaded_units.first);
  1331. while assigned(hp) do
  1332. begin
  1333. linker.AddModuleFiles(hp);
  1334. hp:=tmodule(hp.next);
  1335. end;
  1336. { write .def file }
  1337. if (cs_link_deffile in aktglobalswitches) then
  1338. deffile.writefile;
  1339. { finally we can create a executable }
  1340. if (not current_module.is_unit) then
  1341. begin
  1342. if DLLSource then
  1343. linker.MakeSharedLibrary
  1344. else
  1345. linker.MakeExecutable;
  1346. end;
  1347. end;
  1348. end;
  1349. end.
  1350. {
  1351. $Log$
  1352. Revision 1.171 2004-11-04 23:59:13 peter
  1353. use filepos of main when generating the module stabs
  1354. Revision 1.170 2004/11/04 17:09:54 peter
  1355. fixed debuginfo for variables in staticsymtable
  1356. Revision 1.169 2004/10/31 15:29:39 olle
  1357. + All sections get names in macos
  1358. Revision 1.168 2004/10/26 15:11:01 peter
  1359. * -Ch for heapsize added again
  1360. * __heapsize contains the heapsize
  1361. Revision 1.167 2004/10/25 15:38:41 peter
  1362. * heap and heapsize removed
  1363. * checkpointer fixes
  1364. Revision 1.166 2004/10/15 09:14:17 mazen
  1365. - remove $IFDEF DELPHI and related code
  1366. - remove $IFDEF FPCPROCVAR and related code
  1367. Revision 1.165 2004/10/04 18:26:51 peter
  1368. * debuginfo fixes
  1369. Revision 1.164 2004/09/14 16:33:46 peter
  1370. * release localsymtables when module is compiled
  1371. Revision 1.163 2004/09/04 21:18:47 armin
  1372. * target netwlibc added (libc is preferred for newer netware versions)
  1373. Revision 1.162 2004/09/03 16:12:32 armin
  1374. * dont create main for netware (only PASCALMAIN)
  1375. Revision 1.161 2004/08/16 22:52:35 olle
  1376. + Added automatic use of unit macpas under mode macpas
  1377. Revision 1.160 2004/07/06 20:23:25 peter
  1378. * remove unused and not loaded units before linking
  1379. Revision 1.159 2004/06/29 21:00:08 peter
  1380. * only enable dwarf for supported platforms
  1381. Revision 1.158 2004/06/20 08:55:30 florian
  1382. * logs truncated
  1383. Revision 1.157 2004/06/18 15:16:27 peter
  1384. * fixed debuginfo symbol
  1385. Revision 1.156 2004/06/16 20:07:09 florian
  1386. * dwarf branch merged
  1387. Revision 1.155 2004/05/23 20:56:42 peter
  1388. * initialize errorsym/errortype.def.owner to prevent crashes
  1389. Revision 1.154 2004/05/23 15:06:21 peter
  1390. * implicit_finally flag must be set in pass1
  1391. * add check whether the implicit frame is generated when expected
  1392. Revision 1.153 2004/05/19 21:16:13 peter
  1393. * add DEBUGINFO symbol to reference the .o file that includes the
  1394. stabs info for types and global/static variables
  1395. * debuginfo flag added to ppu to indicate whether debuginfo is
  1396. generated or not
  1397. }