pmodules.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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. {$define New_GDB}
  21. interface
  22. procedure proc_unit;
  23. procedure proc_program(islibrary : boolean);
  24. implementation
  25. uses
  26. globtype,version,systems,tokens,
  27. cutils,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. aasmtai,aasmcpu,
  31. cgbase,cpuinfo,cgobj,
  32. nbas,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,procinfo,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure fixseg(p:TAAsmoutput;sec:TSection);
  40. begin
  41. p.insert(Tai_section.Create(sec));
  42. if (cs_create_smart in aktmoduleswitches) then
  43. p.insert(Tai_cut.Create);
  44. p.concat(Tai_section.Create(sec_none));
  45. end;
  46. procedure create_objectfile;
  47. var
  48. DLLScanner : TDLLScanner;
  49. s : string;
  50. KeepShared : TStringList;
  51. begin
  52. { try to create import entries from system dlls }
  53. if target_info.DllScanSupported and
  54. (not current_module.linkOtherSharedLibs.Empty) then
  55. begin
  56. { Init DLLScanner }
  57. if assigned(CDLLScanner[target_info.system]) then
  58. DLLScanner:=CDLLScanner[target_info.system].Create
  59. else
  60. internalerror(200104121);
  61. KeepShared:=TStringList.Create;
  62. { Walk all shared libs }
  63. While not current_module.linkOtherSharedLibs.Empty do
  64. begin
  65. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  66. if not DLLScanner.scan(s) then
  67. KeepShared.Concat(s);
  68. end;
  69. DLLscanner.Free;
  70. { Recreate import section }
  71. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  72. begin
  73. if assigned(importssection)then
  74. importssection.clear
  75. else
  76. importssection:=taasmoutput.Create;
  77. importlib.generatelib;
  78. end;
  79. { Readd the not processed files }
  80. while not KeepShared.Empty do
  81. begin
  82. s:=KeepShared.GetFirst;
  83. current_module.linkOtherSharedLibs.add(s,link_allways);
  84. end;
  85. KeepShared.Free;
  86. end;
  87. { create the .s file and assemble it }
  88. GenerateAsm(false);
  89. { Also create a smartlinked version ? }
  90. if (cs_create_smart in aktmoduleswitches) then
  91. begin
  92. { regenerate the importssection for win32 }
  93. if assigned(importssection) and
  94. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  95. begin
  96. importsSection.clear;
  97. importlib.generatesmartlib;
  98. end;
  99. GenerateAsm(true);
  100. if target_asm.needar then
  101. Linker.MakeStaticLibrary;
  102. end;
  103. { resource files }
  104. CompileResourceFiles;
  105. end;
  106. procedure insertobjectfile;
  107. { Insert the used object file for this unit in the used list for this unit }
  108. begin
  109. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  110. current_module.flags:=current_module.flags or uf_static_linked;
  111. if (cs_create_smart in aktmoduleswitches) then
  112. begin
  113. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  114. current_module.flags:=current_module.flags or uf_smart_linked;
  115. end;
  116. end;
  117. procedure insertsegment;
  118. begin
  119. { Insert Ident of the compiler }
  120. if (not (cs_create_smart in aktmoduleswitches))
  121. {$ifndef EXTDEBUG}
  122. and (not current_module.is_unit)
  123. {$endif}
  124. then
  125. begin
  126. { align the first data }
  127. dataSegment.insert(Tai_align.Create(const_align(32)));
  128. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  129. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  130. end;
  131. { align code segment }
  132. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  133. { Insert start and end of sections }
  134. fixseg(codesegment,sec_code);
  135. fixseg(datasegment,sec_data);
  136. fixseg(bsssegment,sec_bss);
  137. { we should use .rdata section for these two no ? }
  138. { .rdata is a read only data section (PM) }
  139. fixseg(rttilist,sec_data);
  140. fixseg(consts,sec_data);
  141. if assigned(resourcestringlist) then
  142. fixseg(resourcestringlist,sec_data);
  143. {$ifdef GDB}
  144. if assigned(debuglist) then
  145. begin
  146. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  147. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  148. fixseg(debuglist,sec_code);
  149. end;
  150. {$endif GDB}
  151. end;
  152. procedure InsertThreadvarTablesTable;
  153. var
  154. hp : tused_unit;
  155. ltvTables : taasmoutput;
  156. count : longint;
  157. begin
  158. ltvTables:=TAAsmOutput.Create;
  159. count:=0;
  160. hp:=tused_unit(usedunits.first);
  161. while assigned(hp) do
  162. begin
  163. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  164. begin
  165. ltvTables.concat(Tai_const_symbol.Createdataname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,'')));
  166. inc(count);
  167. end;
  168. hp:=tused_unit(hp.next);
  169. end;
  170. { Add program threadvars, if any }
  171. If (current_module.flags and uf_threadvars)=uf_threadvars then
  172. begin
  173. ltvTables.concat(Tai_const_symbol.Createdataname(make_mangledname('THREADVARLIST',current_module.localsymtable,'')));
  174. inc(count);
  175. end;
  176. { TableCount }
  177. ltvTables.insert(Tai_const.Create_32bit(count));
  178. ltvTables.insert(Tai_symbol.Createdataname_global('FPC_THREADVARTABLES',0));
  179. ltvTables.insert(Tai_align.Create(const_align(pointer_size)));
  180. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  181. { insert in data segment }
  182. if (cs_create_smart in aktmoduleswitches) then
  183. dataSegment.concat(Tai_cut.Create);
  184. dataSegment.concatlist(ltvTables);
  185. ltvTables.free;
  186. end;
  187. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  188. var
  189. ltvTable : taasmoutput;
  190. begin
  191. ltvTable:=taasmoutput(arg);
  192. if (tsym(p).typ=varsym) and
  193. (vo_is_thread_var in tvarsym(p).varoptions) then
  194. begin
  195. { address of threadvar }
  196. ltvTable.concat(tai_const_symbol.createdataname(tvarsym(p).mangledname));
  197. { size of threadvar }
  198. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  199. end;
  200. end;
  201. procedure InsertThreadvars;
  202. var
  203. s : string;
  204. ltvTable : TAAsmoutput;
  205. begin
  206. ltvTable:=TAAsmoutput.create;
  207. if assigned(current_module.globalsymtable) then
  208. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  209. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. if ltvTable.first<>nil then
  211. begin
  212. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.createdataname_global(s,0));
  215. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(s));
  217. if (cs_create_smart in aktmoduleswitches) then
  218. dataSegment.concat(Tai_cut.Create);
  219. dataSegment.concatlist(ltvTable);
  220. current_module.flags:=current_module.flags or uf_threadvars;
  221. end;
  222. ltvTable.Free;
  223. end;
  224. Procedure InsertResourceTablesTable;
  225. var
  226. hp : tused_unit;
  227. ResourceStringTables : taasmoutput;
  228. count : longint;
  229. begin
  230. ResourceStringTables:=TAAsmOutput.Create;
  231. count:=0;
  232. hp:=tused_unit(usedunits.first);
  233. while assigned(hp) do
  234. begin
  235. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  236. begin
  237. ResourceStringTables.concat(Tai_const_symbol.Createdataname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,'')));
  238. inc(count);
  239. end;
  240. hp:=tused_unit(hp.next);
  241. end;
  242. { Add program resources, if any }
  243. If ResourceStringList<>Nil then
  244. begin
  245. ResourceStringTables.concat(Tai_const_symbol.Createdataname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,'')));
  246. Inc(Count);
  247. end;
  248. { TableCount }
  249. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  250. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  251. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  252. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  253. { insert in data segment }
  254. if (cs_create_smart in aktmoduleswitches) then
  255. dataSegment.concat(Tai_cut.Create);
  256. dataSegment.concatlist(ResourceStringTables);
  257. ResourceStringTables.free;
  258. end;
  259. procedure InsertInitFinalTable;
  260. var
  261. hp : tused_unit;
  262. unitinits : taasmoutput;
  263. count : longint;
  264. begin
  265. unitinits:=TAAsmOutput.Create;
  266. count:=0;
  267. hp:=tused_unit(usedunits.first);
  268. while assigned(hp) do
  269. begin
  270. { call the unit init code and make it external }
  271. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  272. begin
  273. if (hp.u.flags and uf_init)<>0 then
  274. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('INIT$',hp.u.globalsymtable,'')))
  275. else
  276. unitinits.concat(Tai_const.Create_32bit(0));
  277. if (hp.u.flags and uf_finalize)<>0 then
  278. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,'')))
  279. else
  280. unitinits.concat(Tai_const.Create_32bit(0));
  281. inc(count);
  282. end;
  283. hp:=tused_unit(hp.next);
  284. end;
  285. { Insert initialization/finalization of the program }
  286. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  287. begin
  288. if (current_module.flags and uf_init)<>0 then
  289. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('INIT$',current_module.localsymtable,'')))
  290. else
  291. unitinits.concat(Tai_const.Create_32bit(0));
  292. if (current_module.flags and uf_finalize)<>0 then
  293. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,'')))
  294. else
  295. unitinits.concat(Tai_const.Create_32bit(0));
  296. inc(count);
  297. end;
  298. { TableCount,InitCount }
  299. unitinits.insert(Tai_const.Create_32bit(0));
  300. unitinits.insert(Tai_const.Create_32bit(count));
  301. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  302. unitinits.insert(Tai_align.Create(const_align(4)));
  303. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  304. { insert in data segment }
  305. if (cs_create_smart in aktmoduleswitches) then
  306. dataSegment.concat(Tai_cut.Create);
  307. dataSegment.concatlist(unitinits);
  308. unitinits.free;
  309. end;
  310. procedure insertheap;
  311. begin
  312. if (cs_create_smart in aktmoduleswitches) then
  313. begin
  314. bssSegment.concat(Tai_cut.Create);
  315. dataSegment.concat(Tai_cut.Create);
  316. end;
  317. { On the Macintosh Classic M68k Architecture
  318. The Heap variable is simply a POINTER to the
  319. real HEAP. The HEAP must be set up by the RTL
  320. and must store the pointer in this value.
  321. On OS/2 the heap is also intialized by the RTL. We do
  322. not output a pointer }
  323. case target_info.system of
  324. system_i386_OS2,system_i386_EMX:
  325. ;
  326. system_powerpc_macos:
  327. ;
  328. system_i386_watcom:
  329. ;
  330. system_alpha_linux:
  331. ;
  332. system_m68k_Mac:
  333. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  334. system_m68k_PalmOS:
  335. ;
  336. else
  337. begin
  338. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  339. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  340. end;
  341. end;
  342. {$ifdef m68k}
  343. if target_info.system<>system_m68k_PalmOS then
  344. begin
  345. dataSegment.concat(Tai_align.Create(const_align(4)));
  346. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  347. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  348. end;
  349. {$else m68k}
  350. dataSegment.concat(Tai_align.Create(const_align(4)));
  351. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  352. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  353. {$endif m68k}
  354. end;
  355. procedure insertstacklength;
  356. begin
  357. { stacksize can be specified and is now simulated }
  358. dataSegment.concat(Tai_align.Create(const_align(4)));
  359. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  360. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  361. end;
  362. procedure loaddefaultunits;
  363. procedure AddUnit(const s:string);
  364. var
  365. hp : tppumodule;
  366. unitsym : tunitsym;
  367. begin
  368. { load unit }
  369. hp:=registerunit(current_module,s,'');
  370. hp.loadppu;
  371. hp.adddependency(current_module);
  372. { add to symtable stack }
  373. tsymtable(hp.globalsymtable).next:=symtablestack;
  374. symtablestack:=hp.globalsymtable;
  375. { insert unitsym }
  376. unitsym:=tunitsym.create(s,hp.globalsymtable);
  377. inc(unitsym.refs);
  378. refsymtable.insert(unitsym);
  379. { add to used units }
  380. current_module.addusedunit(hp,false,unitsym);
  381. end;
  382. begin
  383. { are we compiling the system unit? }
  384. if (cs_compilesystem in aktmoduleswitches) then
  385. begin
  386. { create system defines }
  387. createconstdefs;
  388. { we don't need to reset anything, it's already done in parser.pas }
  389. exit;
  390. end;
  391. { insert the system unit, it is allways the first }
  392. Symtablestack:=nil;
  393. AddUnit('System');
  394. SystemUnit:=TGlobalSymtable(Symtablestack);
  395. { read default constant definitions }
  396. make_ref:=false;
  397. readconstdefs;
  398. make_ref:=true;
  399. {$ifdef cpufpemu}
  400. { Floating point emulation unit? }
  401. if (cs_fp_emulation in aktmoduleswitches) then
  402. AddUnit('SoftFpu');
  403. {$endif cpufpemu}
  404. { Thread support unit? }
  405. if (cs_threading in aktmoduleswitches) then
  406. AddUnit('SysThrds');
  407. { Objpas unit? }
  408. if m_objpas in aktmodeswitches then
  409. AddUnit('ObjPas');
  410. { Profile unit? Needed for go32v2 only }
  411. if (cs_profile in aktmoduleswitches) and
  412. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  413. AddUnit('Profile');
  414. { Units only required for main module }
  415. if not(current_module.is_unit) then
  416. begin
  417. { Heaptrc unit }
  418. if (cs_gdb_heaptrc in aktglobalswitches) then
  419. AddUnit('HeapTrc');
  420. { Lineinfo unit }
  421. if (cs_gdb_lineinfo in aktglobalswitches) then
  422. AddUnit('LineInfo');
  423. end;
  424. { save default symtablestack }
  425. defaultsymtablestack:=symtablestack;
  426. end;
  427. procedure loadunits;
  428. var
  429. s,sorg : stringid;
  430. fn : string;
  431. pu : tused_unit;
  432. hp2 : tmodule;
  433. hp3 : tsymtable;
  434. unitsym : tunitsym;
  435. begin
  436. consume(_USES);
  437. {$ifdef DEBUG}
  438. test_symtablestack;
  439. {$endif DEBUG}
  440. repeat
  441. s:=pattern;
  442. sorg:=orgpattern;
  443. consume(_ID);
  444. { support "<unit> in '<file>'" construct, but not for tp7 }
  445. if not(m_tp7 in aktmodeswitches) then
  446. begin
  447. if try_to_consume(_OP_IN) then
  448. fn:=FixFileName(get_stringconst)
  449. else
  450. fn:='';
  451. end;
  452. { Give a warning if objpas is loaded }
  453. if s='OBJPAS' then
  454. Message(parser_w_no_objpas_use_mode);
  455. { Using the unit itself is not possible }
  456. if (s<>current_module.modulename^) then
  457. begin
  458. { check if the unit is already used }
  459. hp2:=nil;
  460. pu:=tused_unit(current_module.used_units.first);
  461. while assigned(pu) do
  462. begin
  463. if (pu.u.modulename^=s) then
  464. begin
  465. hp2:=pu.u;
  466. break;
  467. end;
  468. pu:=tused_unit(pu.next);
  469. end;
  470. if not assigned(hp2) then
  471. hp2:=registerunit(current_module,sorg,fn)
  472. else
  473. Message1(sym_e_duplicate_id,s);
  474. { Create unitsym, we need to use the name as specified, we
  475. can not use the modulename because that can be different
  476. when -Un is used }
  477. unitsym:=tunitsym.create(sorg,nil);
  478. refsymtable.insert(unitsym);
  479. { the current module uses the unit hp2 }
  480. current_module.addusedunit(hp2,true,unitsym);
  481. end
  482. else
  483. Message1(sym_e_duplicate_id,s);
  484. if token=_COMMA then
  485. begin
  486. pattern:='';
  487. consume(_COMMA);
  488. end
  489. else
  490. break;
  491. until false;
  492. consume(_SEMICOLON);
  493. { Load the units }
  494. pu:=tused_unit(current_module.used_units.first);
  495. while assigned(pu) do
  496. begin
  497. { Only load the units that are in the current
  498. (interface/implementation) uses clause }
  499. if pu.in_uses and
  500. (pu.in_interface=current_module.in_interface) then
  501. begin
  502. tppumodule(pu.u).loadppu;
  503. { is our module compiled? then we can stop }
  504. if current_module.state=ms_compiled then
  505. exit;
  506. { add this unit to the dependencies }
  507. pu.u.adddependency(current_module);
  508. { save crc values }
  509. pu.checksum:=pu.u.crc;
  510. pu.interface_checksum:=pu.u.interface_crc;
  511. { connect unitsym to the globalsymtable of the unit }
  512. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  513. { increase refs of the unitsym when the unit contains
  514. initialization/finalization code so it doesn't trigger
  515. the unit not used hint }
  516. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  517. inc(pu.unitsym.refs);
  518. end;
  519. pu:=tused_unit(pu.next);
  520. end;
  521. { set the symtable to systemunit so it gets reorderd correctly,
  522. then insert the units in the symtablestack }
  523. pu:=tused_unit(current_module.used_units.first);
  524. symtablestack:=defaultsymtablestack;
  525. while assigned(pu) do
  526. begin
  527. {$IfDef GDB}
  528. if (cs_debuginfo in aktmoduleswitches) and
  529. (cs_gdb_dbx in aktglobalswitches) and
  530. not pu.is_stab_written then
  531. begin
  532. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  533. pu.is_stab_written:=true;
  534. pu.unitid:=tsymtable(pu.u.globalsymtable).unitid;
  535. end;
  536. {$EndIf GDB}
  537. if pu.in_uses then
  538. begin
  539. { Reinsert in symtablestack }
  540. hp3:=symtablestack;
  541. while assigned(hp3) do
  542. begin
  543. { insert units only once ! }
  544. if pu.u.globalsymtable=hp3 then
  545. break;
  546. hp3:=hp3.next;
  547. { unit isn't inserted }
  548. if hp3=nil then
  549. begin
  550. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  551. symtablestack:=tsymtable(pu.u.globalsymtable);
  552. {$ifdef DEBUG}
  553. test_symtablestack;
  554. {$endif DEBUG}
  555. end;
  556. end;
  557. end;
  558. pu:=tused_unit(pu.next);
  559. end;
  560. end;
  561. procedure write_gdb_info;
  562. {$IfDef GDB}
  563. var
  564. hp : tused_unit;
  565. begin
  566. if not (cs_debuginfo in aktmoduleswitches) then
  567. exit;
  568. { now insert the units in the symtablestack }
  569. hp:=tused_unit(current_module.used_units.first);
  570. while assigned(hp) do
  571. begin
  572. if (cs_debuginfo in aktmoduleswitches) and
  573. not hp.is_stab_written then
  574. begin
  575. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  576. hp.is_stab_written:=true;
  577. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  578. end;
  579. hp:=tused_unit(hp.next);
  580. end;
  581. if (not current_module.in_interface) and
  582. assigned(current_module.localsymtable) then
  583. begin
  584. { all types }
  585. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  586. { and all local symbols}
  587. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  588. end
  589. else if assigned(current_module.globalsymtable) then
  590. begin
  591. { all types }
  592. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  593. { and all local symbols}
  594. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  595. end;
  596. if (cs_gdb_dbx in aktglobalswitches) then
  597. begin
  598. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  599. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  600. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  601. debugList.concat(Tai_stabs.Create(strpnew('"'+
  602. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  603. tostr(N_EINCL)+',0,0,0')));
  604. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  605. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  606. do_count_dbx:=false;
  607. end;
  608. end;
  609. {$Else GDB}
  610. begin
  611. end;
  612. {$EndIf GDB}
  613. procedure parse_implementation_uses;
  614. begin
  615. if token=_USES then
  616. begin
  617. loadunits;
  618. {$ifdef DEBUG}
  619. test_symtablestack;
  620. {$endif DEBUG}
  621. end;
  622. end;
  623. procedure setupglobalswitches;
  624. begin
  625. { can't have local browser when no global browser }
  626. if (cs_local_browser in aktmoduleswitches) and
  627. not(cs_browser in aktmoduleswitches) then
  628. exclude(aktmoduleswitches,cs_local_browser);
  629. { define a symbol in delphi,objfpc,tp,gpc mode }
  630. if (m_delphi in aktmodeswitches) then
  631. current_scanner.def_macro('FPC_DELPHI')
  632. else
  633. if (m_tp7 in aktmodeswitches) then
  634. current_scanner.def_macro('FPC_TP')
  635. else
  636. if (m_objfpc in aktmodeswitches) then
  637. current_scanner.def_macro('FPC_OBJFPC')
  638. else
  639. if (m_gpc in aktmodeswitches) then
  640. current_scanner.def_macro('FPC_GPC');
  641. end;
  642. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  643. var
  644. stt : tsymtable;
  645. ps : tprocsym;
  646. pd : tprocdef;
  647. begin
  648. { there should be no current_procinfo available }
  649. if assigned(current_procinfo) then
  650. internalerror(200304275);
  651. {Generate a procsym for main}
  652. make_ref:=false;
  653. { try to insert in in static symtable ! }
  654. stt:=symtablestack;
  655. symtablestack:=st;
  656. { generate procsym }
  657. ps:=tprocsym.create('$'+name);
  658. { main are allways used }
  659. inc(ps.refs);
  660. symtablestack.insert(ps);
  661. pd:=tprocdef.create(main_program_level);
  662. include(pd.procoptions,po_public);
  663. pd.procsym:=ps;
  664. ps.addprocdef(pd);
  665. { restore symtable }
  666. make_ref:=true;
  667. symtablestack:=stt;
  668. { set procdef options }
  669. pd.proctypeoption:=potype;
  670. pd.proccalloption:=pocall_default;
  671. pd.forwarddef:=false;
  672. pd.setmangledname(target_info.cprefix+name);
  673. pd.aliasnames.insert(pd.mangledname);
  674. { We don't need is a local symtable. Change it into the static
  675. symtable }
  676. pd.localst.free;
  677. pd.localst:=st;
  678. { set procinfo and current_procinfo.procdef }
  679. current_procinfo:=cprocinfo.create(nil);
  680. current_module.procinfo:=current_procinfo;
  681. current_procinfo.procdef:=pd;
  682. { return procdef }
  683. create_main_proc:=pd;
  684. { main proc does always a call e.g. to init system unit }
  685. include(current_procinfo.flags,pi_do_call);
  686. end;
  687. procedure release_main_proc(pd:tprocdef);
  688. begin
  689. { this is a main proc, so there should be no parent }
  690. if not(assigned(current_procinfo)) or
  691. assigned(current_procinfo.parent) or
  692. not(current_procinfo.procdef=pd) then
  693. internalerror(200304276);
  694. { remove procinfo }
  695. current_module.procinfo:=nil;
  696. current_procinfo.free;
  697. current_procinfo:=nil;
  698. { remove localst as it was replaced by staticsymtable }
  699. pd.localst:=nil;
  700. end;
  701. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  702. var
  703. pd : tprocdef;
  704. begin
  705. { update module flags }
  706. current_module.flags:=current_module.flags or flag;
  707. { create procdef }
  708. case flag of
  709. uf_init :
  710. begin
  711. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  712. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  713. end;
  714. uf_finalize :
  715. begin
  716. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  717. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  718. end;
  719. else
  720. internalerror(200304253);
  721. end;
  722. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  723. tcgprocinfo(current_procinfo).add_entry_exit_code;
  724. tcgprocinfo(current_procinfo).generate_code;
  725. release_main_proc(pd);
  726. end;
  727. procedure proc_unit;
  728. function is_assembler_generated:boolean;
  729. begin
  730. is_assembler_generated:=(Errorcount=0) and
  731. not(
  732. codeSegment.empty and
  733. dataSegment.empty and
  734. bssSegment.empty and
  735. ((importssection=nil) or importsSection.empty) and
  736. ((resourcesection=nil) or resourceSection.empty) and
  737. ((resourcestringlist=nil) or resourcestringList.empty)
  738. );
  739. end;
  740. var
  741. main_file: tinputfile;
  742. st : tsymtable;
  743. unitst : tglobalsymtable;
  744. {$ifdef GDB}
  745. pu : tused_unit;
  746. {$endif GDB}
  747. store_crc,store_interface_crc : cardinal;
  748. s1,s2 : ^string; {Saves stack space}
  749. force_init_final : boolean;
  750. pd : tprocdef;
  751. begin
  752. consume(_UNIT);
  753. if compile_level=1 then
  754. Status.IsExe:=false;
  755. if token=_ID then
  756. begin
  757. { create filenames and unit name }
  758. main_file := current_scanner.inputfile;
  759. while assigned(main_file.next) do
  760. main_file := main_file.next;
  761. new(s1);
  762. s1^:=current_module.modulename^;
  763. current_module.SetFileName(main_file.path^+main_file.name^,true);
  764. current_module.SetModuleName(orgpattern);
  765. { check for system unit }
  766. new(s2);
  767. s2^:=upper(SplitName(main_file.name^));
  768. if (cs_check_unit_name in aktglobalswitches) and
  769. (
  770. not(
  771. (current_module.modulename^=s2^) or
  772. (
  773. (length(current_module.modulename^)>8) and
  774. (copy(current_module.modulename^,1,8)=s2^)
  775. )
  776. )
  777. or
  778. (
  779. (length(s1^)>8) and
  780. (s1^<>current_module.modulename^)
  781. )
  782. ) then
  783. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  784. if (current_module.modulename^='SYSTEM') then
  785. include(aktmoduleswitches,cs_compilesystem);
  786. dispose(s2);
  787. dispose(s1);
  788. end;
  789. consume(_ID);
  790. consume(_SEMICOLON);
  791. consume(_INTERFACE);
  792. { global switches are read, so further changes aren't allowed }
  793. current_module.in_global:=false;
  794. { handle the global switches }
  795. setupglobalswitches;
  796. message1(unit_u_loading_interface_units,current_module.modulename^);
  797. { update status }
  798. status.currentmodule:=current_module.realmodulename^;
  799. { maybe turn off m_objpas if we are compiling objpas }
  800. if (current_module.modulename^='OBJPAS') then
  801. exclude(aktmodeswitches,m_objpas);
  802. parse_only:=true;
  803. { generate now the global symboltable }
  804. st:=tglobalsymtable.create(current_module.modulename^);
  805. refsymtable:=st;
  806. unitst:=tglobalsymtable(st);
  807. { define first as local to overcome dependency conflicts }
  808. current_module.localsymtable:=st;
  809. { the unit name must be usable as a unit specifier }
  810. { inside the unit itself (PM) }
  811. { this also forbids to have another symbol }
  812. { with the same name as the unit }
  813. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  814. { load default units, like the system unit }
  815. loaddefaultunits;
  816. { reset }
  817. make_ref:=true;
  818. { insert qualifier for the system unit (allows system.writeln) }
  819. if not(cs_compilesystem in aktmoduleswitches) then
  820. begin
  821. if token=_USES then
  822. begin
  823. loadunits;
  824. { has it been compiled at a higher level ?}
  825. if current_module.state=ms_compiled then
  826. exit;
  827. end;
  828. { ... but insert the symbol table later }
  829. st.next:=symtablestack;
  830. symtablestack:=st;
  831. end
  832. else
  833. { while compiling a system unit, some types are directly inserted }
  834. begin
  835. st.next:=symtablestack;
  836. symtablestack:=st;
  837. insert_intern_types(st);
  838. end;
  839. { now we know the place to insert the constants }
  840. constsymtable:=symtablestack;
  841. { move the global symtab from the temporary local to global }
  842. current_module.globalsymtable:=current_module.localsymtable;
  843. current_module.localsymtable:=nil;
  844. reset_global_defs;
  845. { number all units, so we know if a unit is used by this unit or
  846. needs to be added implicitly }
  847. current_module.numberunits;
  848. { ... parse the declarations }
  849. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  850. read_interface_declarations;
  851. { leave when we got an error }
  852. if (Errorcount>0) and not status.skip_error then
  853. begin
  854. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  855. status.skip_error:=true;
  856. exit;
  857. end;
  858. {$ifdef New_GDB}
  859. write_gdb_info;
  860. {$endIf Def New_GDB}
  861. { Our interface is compiled, generate CRC and switch to implementation }
  862. if not(cs_compilesystem in aktmoduleswitches) and
  863. (Errorcount=0) then
  864. tppumodule(current_module).getppucrc;
  865. current_module.in_interface:=false;
  866. current_module.interface_compiled:=true;
  867. { First reload all units depending on our interface, we need to do this
  868. in the implementation part to prevent errorneous circular references }
  869. reload_flagged_units;
  870. { Parse the implementation section }
  871. consume(_IMPLEMENTATION);
  872. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  873. parse_only:=false;
  874. { generates static symbol table }
  875. st:=tstaticsymtable.create(current_module.modulename^);
  876. current_module.localsymtable:=st;
  877. { remove the globalsymtable from the symtable stack }
  878. { to reinsert it after loading the implementation units }
  879. symtablestack:=unitst.next;
  880. { we don't want implementation units symbols in unitsymtable !! PM }
  881. refsymtable:=st;
  882. { Read the implementation units }
  883. parse_implementation_uses;
  884. if current_module.state=ms_compiled then
  885. exit;
  886. { reset ranges/stabs in exported definitions }
  887. reset_global_defs;
  888. { All units are read, now give them a number }
  889. current_module.numberunits;
  890. { now we can change refsymtable }
  891. refsymtable:=st;
  892. { but reinsert the global symtable as lasts }
  893. unitst.next:=symtablestack;
  894. symtablestack:=unitst;
  895. tstoredsymtable(symtablestack).chainoperators;
  896. {$ifdef DEBUG}
  897. test_symtablestack;
  898. {$endif DEBUG}
  899. constsymtable:=symtablestack;
  900. {$ifdef Splitheap}
  901. if testsplit then
  902. begin
  903. Split_Heap;
  904. allow_special:=true;
  905. Switch_to_temp_heap;
  906. end;
  907. { it will report all crossings }
  908. allow_special:=false;
  909. {$endif Splitheap}
  910. Message1(parser_u_parsing_implementation,current_module.modulename^);
  911. if current_module.in_interface then
  912. internalerror(200212285);
  913. { Compile the unit }
  914. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  915. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  916. tcgprocinfo(current_procinfo).parse_body;
  917. tcgprocinfo(current_procinfo).generate_code;
  918. tcgprocinfo(current_procinfo).resetprocdef;
  919. release_main_proc(pd);
  920. { if the unit contains ansi/widestrings, initialization and
  921. finalization code must be forced }
  922. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  923. tstaticsymtable(current_module.localsymtable).needs_init_final;
  924. { should we force unit initialization? }
  925. { this is a hack, but how can it be done better ? }
  926. if force_init_final and ((current_module.flags and uf_init)=0) then
  927. gen_implicit_initfinal(uf_init,st);
  928. { finalize? }
  929. if token=_FINALIZATION then
  930. begin
  931. { set module options }
  932. current_module.flags:=current_module.flags or uf_finalize;
  933. { Compile the finalize }
  934. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  935. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  936. tcgprocinfo(current_procinfo).parse_body;
  937. tcgprocinfo(current_procinfo).generate_code;
  938. tcgprocinfo(current_procinfo).resetprocdef;
  939. release_main_proc(pd);
  940. end
  941. else if force_init_final then
  942. gen_implicit_initfinal(uf_finalize,st);
  943. { the last char should always be a point }
  944. consume(_POINT);
  945. { generate a list of threadvars }
  946. InsertThreadvars;
  947. { Generate resoucestrings }
  948. If ResourceStrings.ResStrCount>0 then
  949. begin
  950. ResourceStrings.CreateResourceStringList;
  951. current_module.flags:=current_module.flags or uf_has_resources;
  952. { only write if no errors found }
  953. if (Errorcount=0) then
  954. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  955. end;
  956. if (Errorcount=0) then
  957. begin
  958. { test static symtable }
  959. tstoredsymtable(st).allsymbolsused;
  960. tstoredsymtable(st).allprivatesused;
  961. current_module.allunitsused;
  962. end;
  963. {$ifdef GDB}
  964. { add all used definitions even for implementation}
  965. if (cs_debuginfo in aktmoduleswitches) then
  966. begin
  967. {$IfnDef New_GDB}
  968. if assigned(current_module.globalsymtable) then
  969. begin
  970. { all types }
  971. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  972. { and all local symbols}
  973. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  974. end;
  975. { all local types }
  976. tglobalsymtable(st)^.concattypestabto(debuglist);
  977. { and all local symbols}
  978. st^.concatstabto(debuglist);
  979. {$else New_GDB}
  980. write_gdb_info;
  981. {$endIf Def New_GDB}
  982. end;
  983. {$endif GDB}
  984. reset_global_defs;
  985. if (Errorcount=0) then
  986. begin
  987. { tests, if all (interface) forwards are resolved }
  988. tstoredsymtable(symtablestack).check_forwards;
  989. { check if all private fields are used }
  990. tstoredsymtable(symtablestack).allprivatesused;
  991. { remove cross unit overloads }
  992. tstoredsymtable(symtablestack).unchain_overloaded;
  993. end;
  994. {$ifdef GDB}
  995. tglobalsymtable(symtablestack).is_stab_written:=false;
  996. {$endif GDB}
  997. { leave when we got an error }
  998. if (Errorcount>0) and not status.skip_error then
  999. begin
  1000. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1001. status.skip_error:=true;
  1002. exit;
  1003. end;
  1004. { generate imports }
  1005. if current_module.uses_imports then
  1006. importlib.generatelib;
  1007. { insert own objectfile, or say that it's in a library
  1008. (no check for an .o when loading) }
  1009. if is_assembler_generated then
  1010. insertobjectfile
  1011. else
  1012. current_module.flags:=current_module.flags or uf_no_link;
  1013. if cs_local_browser in aktmoduleswitches then
  1014. current_module.localsymtable:=refsymtable;
  1015. {$ifdef GDB}
  1016. pu:=tused_unit(usedunits.first);
  1017. while assigned(pu) do
  1018. begin
  1019. if assigned(pu.u.globalsymtable) then
  1020. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  1021. pu:=tused_unit(pu.next);
  1022. end;
  1023. {$endif GDB}
  1024. if is_assembler_generated then
  1025. begin
  1026. { finish asmlist by adding segment starts }
  1027. insertsegment;
  1028. { assemble }
  1029. create_objectfile;
  1030. end;
  1031. { Write out the ppufile after the object file has been created }
  1032. store_interface_crc:=current_module.interface_crc;
  1033. store_crc:=current_module.crc;
  1034. if (Errorcount=0) then
  1035. tppumodule(current_module).writeppu;
  1036. if not(cs_compilesystem in aktmoduleswitches) then
  1037. if store_interface_crc<>current_module.interface_crc then
  1038. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1039. {$ifdef EXTDEBUG}
  1040. if not(cs_compilesystem in aktmoduleswitches) then
  1041. if (store_crc<>current_module.crc) and simplify_ppu then
  1042. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1043. {$endif EXTDEBUG}
  1044. { remove static symtable (=refsymtable) here to save some mem }
  1045. if not (cs_local_browser in aktmoduleswitches) then
  1046. begin
  1047. st.free;
  1048. current_module.localsymtable:=nil;
  1049. end;
  1050. { leave when we got an error }
  1051. if (Errorcount>0) and not status.skip_error then
  1052. begin
  1053. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1054. status.skip_error:=true;
  1055. exit;
  1056. end;
  1057. Message1(unit_u_finished_compiling,current_module.modulename^);
  1058. end;
  1059. procedure proc_program(islibrary : boolean);
  1060. var
  1061. main_file: tinputfile;
  1062. st : tsymtable;
  1063. hp : tmodule;
  1064. pd : tprocdef;
  1065. begin
  1066. DLLsource:=islibrary;
  1067. Status.IsLibrary:=IsLibrary;
  1068. Status.IsExe:=true;
  1069. parse_only:=false;
  1070. { relocation works only without stabs under win32 !! PM }
  1071. { internal assembler uses rva for stabs info
  1072. so it should work with relocated DLLs }
  1073. if RelocSection and
  1074. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1075. (target_info.assem<>as_i386_pecoff) then
  1076. begin
  1077. include(aktglobalswitches,cs_link_strip);
  1078. { Warning stabs info does not work with reloc section !! }
  1079. if cs_debuginfo in aktmoduleswitches then
  1080. begin
  1081. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1082. Message(parser_w_parser_win32_debug_needs_WN);
  1083. exclude(aktmoduleswitches,cs_debuginfo);
  1084. end;
  1085. end;
  1086. { get correct output names }
  1087. main_file := current_scanner.inputfile;
  1088. while assigned(main_file.next) do
  1089. main_file := main_file.next;
  1090. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1091. if islibrary then
  1092. begin
  1093. consume(_LIBRARY);
  1094. stringdispose(current_module.modulename);
  1095. current_module.modulename:=stringdup(pattern);
  1096. current_module.islibrary:=true;
  1097. exportlib.preparelib(pattern);
  1098. consume(_ID);
  1099. consume(_SEMICOLON);
  1100. end
  1101. else
  1102. { is there an program head ? }
  1103. if token=_PROGRAM then
  1104. begin
  1105. consume(_PROGRAM);
  1106. stringdispose(current_module.modulename);
  1107. stringdispose(current_module.realmodulename);
  1108. current_module.modulename:=stringdup(pattern);
  1109. current_module.realmodulename:=stringdup(orgpattern);
  1110. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1111. exportlib.preparelib(pattern);
  1112. consume(_ID);
  1113. if token=_LKLAMMER then
  1114. begin
  1115. consume(_LKLAMMER);
  1116. repeat
  1117. consume(_ID);
  1118. until not try_to_consume(_COMMA);
  1119. consume(_RKLAMMER);
  1120. end;
  1121. consume(_SEMICOLON);
  1122. end
  1123. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1124. exportlib.preparelib(current_module.modulename^);
  1125. { global switches are read, so further changes aren't allowed }
  1126. current_module.in_global:=false;
  1127. { setup things using the global switches }
  1128. setupglobalswitches;
  1129. { set implementation flag }
  1130. current_module.in_interface:=false;
  1131. current_module.interface_compiled:=true;
  1132. { insert after the unit symbol tables the static symbol table }
  1133. { of the program }
  1134. st:=tstaticsymtable.create(current_module.modulename^);;
  1135. current_module.localsymtable:=st;
  1136. refsymtable:=st;
  1137. { load standard units (system,objpas,profile unit) }
  1138. loaddefaultunits;
  1139. {Load the units used by the program we compile.}
  1140. if token=_USES then
  1141. loadunits;
  1142. tstoredsymtable(symtablestack).chainoperators;
  1143. { reset ranges/stabs in exported definitions }
  1144. reset_global_defs;
  1145. { All units are read, now give them a number }
  1146. current_module.numberunits;
  1147. {Insert the name of the main program into the symbol table.}
  1148. if current_module.realmodulename^<>'' then
  1149. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1150. { ...is also constsymtable, this is the symtable where }
  1151. { the elements of enumeration types are inserted }
  1152. constsymtable:=st;
  1153. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1154. { The program intialization needs an alias, so it can be called
  1155. from the bootstrap code.}
  1156. if islibrary or
  1157. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1158. begin
  1159. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1160. { Win32 startup code needs a single name }
  1161. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1162. pd.aliasnames.insert('PASCALMAIN');
  1163. end
  1164. else
  1165. begin
  1166. pd:=create_main_proc('main',potype_proginit,st);
  1167. pd.aliasnames.insert('PASCALMAIN');
  1168. end;
  1169. tcgprocinfo(current_procinfo).parse_body;
  1170. tcgprocinfo(current_procinfo).generate_code;
  1171. tcgprocinfo(current_procinfo).resetprocdef;
  1172. release_main_proc(pd);
  1173. { should we force unit initialization? }
  1174. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1175. begin
  1176. { initialize section }
  1177. gen_implicit_initfinal(uf_init,st);
  1178. { finalize section }
  1179. gen_implicit_initfinal(uf_finalize,st);
  1180. end;
  1181. { Add symbol to the exports section for win32 so smartlinking a
  1182. DLL will include the edata section }
  1183. if assigned(exportlib) and
  1184. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1185. assigned(current_module._exports.first) then
  1186. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1187. If ResourceStrings.ResStrCount>0 then
  1188. begin
  1189. ResourceStrings.CreateResourceStringList;
  1190. { only write if no errors found }
  1191. if (Errorcount=0) then
  1192. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1193. end;
  1194. { finalize? }
  1195. if token=_FINALIZATION then
  1196. begin
  1197. { set module options }
  1198. current_module.flags:=current_module.flags or uf_finalize;
  1199. { Compile the finalize }
  1200. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1201. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1202. tcgprocinfo(current_procinfo).parse_body;
  1203. tcgprocinfo(current_procinfo).generate_code;
  1204. tcgprocinfo(current_procinfo).resetprocdef;
  1205. release_main_proc(pd);
  1206. end;
  1207. { consume the last point }
  1208. consume(_POINT);
  1209. {$ifdef New_GDB}
  1210. write_gdb_info;
  1211. {$endIf Def New_GDB}
  1212. { leave when we got an error }
  1213. if (Errorcount>0) and not status.skip_error then
  1214. begin
  1215. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1216. status.skip_error:=true;
  1217. exit;
  1218. end;
  1219. if (Errorcount=0) then
  1220. begin
  1221. { test static symtable }
  1222. tstoredsymtable(st).allsymbolsused;
  1223. tstoredsymtable(st).allprivatesused;
  1224. current_module.allunitsused;
  1225. end;
  1226. { generate a list of threadvars }
  1227. InsertThreadvars;
  1228. { generate imports }
  1229. if current_module.uses_imports then
  1230. importlib.generatelib;
  1231. if islibrary or
  1232. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1233. (target_info.system=system_i386_NETWARE) then
  1234. exportlib.generatelib;
  1235. { insert Tables and Heap }
  1236. insertThreadVarTablesTable;
  1237. insertResourceTablesTable;
  1238. insertinitfinaltable;
  1239. insertheap;
  1240. insertstacklength;
  1241. // datasize:=symtablestack.datasize;
  1242. { finish asmlist by adding segment starts }
  1243. insertsegment;
  1244. { insert own objectfile }
  1245. insertobjectfile;
  1246. { assemble and link }
  1247. create_objectfile;
  1248. { leave when we got an error }
  1249. if (Errorcount>0) and not status.skip_error then
  1250. begin
  1251. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1252. status.skip_error:=true;
  1253. exit;
  1254. end;
  1255. { create the executable when we are at level 1 }
  1256. if (compile_level=1) then
  1257. begin
  1258. { insert all .o files from all loaded units }
  1259. hp:=tmodule(loaded_units.first);
  1260. while assigned(hp) do
  1261. begin
  1262. linker.AddModuleFiles(hp);
  1263. hp:=tmodule(hp.next);
  1264. end;
  1265. { write .def file }
  1266. if (cs_link_deffile in aktglobalswitches) then
  1267. deffile.writefile;
  1268. { finally we can create a executable }
  1269. if (not current_module.is_unit) then
  1270. begin
  1271. if DLLSource then
  1272. linker.MakeSharedLibrary
  1273. else
  1274. linker.MakeExecutable;
  1275. end;
  1276. end;
  1277. end;
  1278. end.
  1279. {
  1280. $Log$
  1281. Revision 1.136 2004-01-04 21:08:09 jonas
  1282. * Never generate a "main" symbol for PASCALMAIN for Darwin and classic
  1283. Mac OS, they use a C-main in their system unit
  1284. Revision 1.135 2003/12/12 19:42:21 peter
  1285. * check unit name when expected unitname > 8 chars
  1286. Revision 1.134 2003/12/08 22:33:43 peter
  1287. * don't allow duplicate uses
  1288. * fix wrong circular dependency
  1289. Revision 1.133 2003/11/29 20:13:25 florian
  1290. * fixed several pi_do_call problems
  1291. Revision 1.132 2003/10/29 19:48:51 peter
  1292. * renamed mangeldname_prefix to make_mangledname and made it more
  1293. generic
  1294. * make_mangledname is now also used for internal threadvar/resstring
  1295. lists
  1296. * Add P$ in front of program modulename to prevent duplicated symbols
  1297. at assembler level, because the main program can have the same name
  1298. as a unit, see webtbs/tw1251b
  1299. Revision 1.131 2003/10/24 17:40:23 peter
  1300. * cleanup of the entry and exit code insertion
  1301. Revision 1.130 2003/10/22 15:22:33 peter
  1302. * fixed unitsym-globalsymtable relation so the uses of a unit
  1303. is counted correctly
  1304. Revision 1.129 2003/10/21 15:14:33 peter
  1305. * fixed memleak for initfinalcode
  1306. * exit from generatecode when there are already errors
  1307. Revision 1.128 2003/10/01 20:34:49 peter
  1308. * procinfo unit contains tprocinfo
  1309. * cginfo renamed to cgbase
  1310. * moved cgmessage to verbose
  1311. * fixed ppc and sparc compiles
  1312. Revision 1.127 2003/09/30 08:39:50 michael
  1313. + Patch from Wiktor Sywula for watcom support
  1314. Revision 1.126 2003/09/23 18:03:08 peter
  1315. * add missing release of main_proc
  1316. Revision 1.125 2003/09/23 17:56:05 peter
  1317. * locals and paras are allocated in the code generation
  1318. * tvarsym.localloc contains the location of para/local when
  1319. generating code for the current procedure
  1320. Revision 1.124 2003/09/09 20:59:27 daniel
  1321. * Adding register allocation order
  1322. Revision 1.123 2003/09/09 15:55:44 peter
  1323. * use register with least interferences in spillregister
  1324. Revision 1.122 2003/09/07 22:09:35 peter
  1325. * preparations for different default calling conventions
  1326. * various RA fixes
  1327. Revision 1.121 2003/09/05 17:41:12 florian
  1328. * merged Wiktor's Watcom patches in 1.1
  1329. Revision 1.120 2003/08/23 22:29:24 peter
  1330. * reload flagged units when interface is loaded
  1331. Revision 1.119 2003/08/21 14:47:41 peter
  1332. * remove convert_registers
  1333. Revision 1.118 2003/08/20 17:48:49 peter
  1334. * fixed stackalloc to not allocate localst.datasize twice
  1335. * order of stackalloc code fixed for implicit init/final
  1336. Revision 1.117 2003/08/20 09:07:00 daniel
  1337. * New register coding now mandatory, some more convert_registers calls
  1338. removed.
  1339. Revision 1.116 2003/07/23 11:04:15 jonas
  1340. * split en_exit_code into a part that may allocate a register and a part
  1341. that doesn't, so the former can be done before the register colouring
  1342. has been performed
  1343. Revision 1.115 2003/07/06 17:58:22 peter
  1344. * framepointer fixes for sparc
  1345. * parent framepointer code more generic
  1346. Revision 1.114 2003/06/13 21:19:31 peter
  1347. * current_procdef removed, use current_procinfo.procdef instead
  1348. Revision 1.113 2003/06/09 12:23:30 peter
  1349. * init/final of procedure data splitted from genentrycode
  1350. * use asmnode getposition to insert final at the correct position
  1351. als for the implicit try...finally
  1352. Revision 1.112 2003/06/07 20:26:32 peter
  1353. * re-resolving added instead of reloading from ppu
  1354. * tderef object added to store deref info for resolving
  1355. Revision 1.111 2003/06/03 20:21:45 mazen
  1356. - removed unneeded ifdefs
  1357. - removed unneeded cases for sparc and x86_64
  1358. Revision 1.110 2003/06/03 13:01:59 daniel
  1359. * Register allocator finished
  1360. Revision 1.109 2003/05/26 21:17:17 peter
  1361. * procinlinenode removed
  1362. * aktexit2label removed, fast exit removed
  1363. + tcallnode.inlined_pass_2 added
  1364. Revision 1.108 2003/05/25 10:27:12 peter
  1365. * moved Comment calls to messge file
  1366. Revision 1.107 2003/05/22 21:31:35 peter
  1367. * defer codegeneration for nested procedures
  1368. Revision 1.106 2003/05/15 18:58:53 peter
  1369. * removed selfpointer_offset, vmtpointer_offset
  1370. * tvarsym.adjusted_address
  1371. * address in localsymtable is now in the real direction
  1372. * removed some obsolete globals
  1373. Revision 1.105 2003/05/11 19:31:28 florian
  1374. * fixed implicit init/final code for units, stack frame was wrong for ppc
  1375. Revision 1.104 2003/04/28 21:19:02 peter
  1376. * fix stabs generation for implicit initfinal
  1377. Revision 1.103 2003/04/27 11:21:34 peter
  1378. * aktprocdef renamed to current_procinfo.procdef
  1379. * procinfo renamed to current_procinfo
  1380. * procinfo will now be stored in current_module so it can be
  1381. cleaned up properly
  1382. * gen_main_procsym changed to create_main_proc and release_main_proc
  1383. to also generate a tprocinfo structure
  1384. * fixed unit implicit initfinal
  1385. Revision 1.102 2003/04/27 07:29:50 peter
  1386. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1387. a new procdef declaration
  1388. * aktprocsym removed
  1389. * lexlevel removed, use symtable.symtablelevel instead
  1390. * implicit init/final code uses the normal genentry/genexit
  1391. * funcret state checking updated for new funcret handling
  1392. Revision 1.101 2003/04/23 12:35:34 florian
  1393. * fixed several issues with powerpc
  1394. + applied a patch from Jonas for nested function calls (PowerPC only)
  1395. * ...
  1396. Revision 1.100 2003/04/12 15:13:03 peter
  1397. * Use the original unitname when defining a unitsym
  1398. Revision 1.99 2003/03/23 23:21:42 hajny
  1399. + emx target added
  1400. Revision 1.98 2003/03/17 22:20:08 peter
  1401. *** empty log message ***
  1402. Revision 1.97 2003/03/17 13:36:39 peter
  1403. * fix import linking under linux
  1404. Revision 1.96 2003/02/19 22:00:14 daniel
  1405. * Code generator converted to new register notation
  1406. - Horribily outdated todo.txt removed
  1407. Revision 1.95 2003/02/06 22:36:55 mazen
  1408. * fixing bug related to errornous program main entry stack frame
  1409. Revision 1.94 2003/01/30 21:46:20 peter
  1410. * tai_const_symbol.createdataname added
  1411. Revision 1.93 2003/01/11 11:19:54 hajny
  1412. * correction from rev. 1.88 put back
  1413. Revision 1.92 2003/01/08 18:43:56 daniel
  1414. * Tregister changed into a record
  1415. Revision 1.91 2003/01/05 13:36:53 florian
  1416. * x86-64 compiles
  1417. + very basic support for float128 type (x86-64 only)
  1418. Revision 1.90 2002/12/29 18:17:23 peter
  1419. * insert unitsym with the name as specified in the uses list
  1420. Revision 1.89 2002/12/29 14:57:50 peter
  1421. * unit loading changed to first register units and load them
  1422. afterwards. This is needed to support uses xxx in yyy correctly
  1423. * unit dependency check fixed
  1424. Revision 1.88 2002/12/27 19:09:33 hajny
  1425. * another (hopefully final ;-) ) fix for not linked import libraries for units with no code
  1426. Revision 1.87 2002/12/24 23:32:56 peter
  1427. * Use FixFilename for specified unit sourcefile in uses
  1428. Revision 1.86 2002/12/06 16:56:58 peter
  1429. * only compile cs_fp_emulation support when cpufpuemu is defined
  1430. * define cpufpuemu for m68k only
  1431. Revision 1.85 2002/11/30 21:32:24 carl
  1432. + Add loading of softfpu in emulation mode
  1433. + Correct routine call for softfpu
  1434. * Extended type must also be defined even with softfpu
  1435. Revision 1.84 2002/11/15 01:58:53 peter
  1436. * merged changes from 1.0.7 up to 04-11
  1437. - -V option for generating bug report tracing
  1438. - more tracing for option parsing
  1439. - errors for cdecl and high()
  1440. - win32 import stabs
  1441. - win32 records<=8 are returned in eax:edx (turned off by default)
  1442. - heaptrc update
  1443. - more info for temp management in .s file with EXTDEBUG
  1444. Revision 1.83 2002/11/09 15:33:26 carl
  1445. * major alignment updates
  1446. Revision 1.82 2002/10/16 06:32:52 michael
  1447. + Renamed thread unit to systhrds
  1448. Revision 1.81 2002/10/14 19:42:34 peter
  1449. * only use init tables for threadvars
  1450. Revision 1.80 2002/10/06 19:41:30 peter
  1451. * Add finalization of typed consts
  1452. * Finalization of globals in the main program
  1453. Revision 1.79 2002/09/09 17:34:15 peter
  1454. * tdicationary.replace added to replace and item in a dictionary. This
  1455. is only allowed for the same name
  1456. * varsyms are inserted in symtable before the types are parsed. This
  1457. fixes the long standing "var longint : longint" bug
  1458. - consume_idlist and idstringlist removed. The loops are inserted
  1459. at the callers place and uses the symtable for duplicate id checking
  1460. Revision 1.78 2002/09/07 15:25:07 peter
  1461. * old logs removed and tabs fixed
  1462. Revision 1.77 2002/09/03 16:26:27 daniel
  1463. * Make Tprocdef.defs protected
  1464. Revision 1.76 2002/09/02 18:46:26 peter
  1465. * insert PASCALMAIN in library for Win32 only
  1466. Revision 1.75 2002/08/31 15:59:30 florian
  1467. + HEAP* stuff must be generated for Linux/PPC as well
  1468. + direct assembler reader searches now global and static symtables as well
  1469. Revision 1.74 2002/08/25 19:25:20 peter
  1470. * sym.insert_in_data removed
  1471. * symtable.insertvardata/insertconstdata added
  1472. * removed insert_in_data call from symtable.insert, it needs to be
  1473. called separatly. This allows to deref the address calculation
  1474. * procedures now calculate the parast addresses after the procedure
  1475. directives are parsed. This fixes the cdecl parast problem
  1476. * push_addr_param has an extra argument that specifies if cdecl is used
  1477. or not
  1478. Revision 1.73 2002/08/18 20:06:25 peter
  1479. * inlining is now also allowed in interface
  1480. * renamed write/load to ppuwrite/ppuload
  1481. * tnode storing in ppu
  1482. * nld,ncon,nbas are already updated for storing in ppu
  1483. Revision 1.72 2002/08/17 09:23:39 florian
  1484. * first part of procinfo rewrite
  1485. Revision 1.71 2002/08/11 13:24:12 peter
  1486. * saving of asmsymbols in ppu supported
  1487. * asmsymbollist global is removed and moved into a new class
  1488. tasmlibrarydata that will hold the info of a .a file which
  1489. corresponds with a single module. Added librarydata to tmodule
  1490. to keep the library info stored for the module. In the future the
  1491. objectfiles will also be stored to the tasmlibrarydata class
  1492. * all getlabel/newasmsymbol and friends are moved to the new class
  1493. Revision 1.70 2002/08/10 14:46:29 carl
  1494. + moved target_cpu_string to cpuinfo
  1495. * renamed asmmode enum.
  1496. * assembler reader has now less ifdef's
  1497. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1498. Revision 1.69 2002/07/26 21:15:41 florian
  1499. * rewrote the system handling
  1500. Revision 1.68 2002/07/04 20:43:01 florian
  1501. * first x86-64 patches
  1502. Revision 1.67 2002/07/01 18:46:25 peter
  1503. * internal linker
  1504. * reorganized aasm layer
  1505. Revision 1.66 2002/05/16 19:46:43 carl
  1506. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1507. + try to fix temp allocation (still in ifdef)
  1508. + generic constructor calls
  1509. + start of tassembler / tmodulebase class cleanup
  1510. Revision 1.65 2002/05/14 19:34:49 peter
  1511. * removed old logs and updated copyright year
  1512. Revision 1.64 2002/05/12 16:53:09 peter
  1513. * moved entry and exitcode to ncgutil and cgobj
  1514. * foreach gets extra argument for passing local data to the
  1515. iterator function
  1516. * -CR checks also class typecasts at runtime by changing them
  1517. into as
  1518. * fixed compiler to cycle with the -CR option
  1519. * fixed stabs with elf writer, finally the global variables can
  1520. be watched
  1521. * removed a lot of routines from cga unit and replaced them by
  1522. calls to cgobj
  1523. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1524. u32bit then the other is typecasted also to u32bit without giving
  1525. a rangecheck warning/error.
  1526. * fixed pascal calling method with reversing also the high tree in
  1527. the parast, detected by tcalcst3 test
  1528. Revision 1.63 2002/05/06 19:54:50 carl
  1529. + added more patches from Mazen for SPARC port
  1530. Revision 1.62 2002/04/20 21:32:24 carl
  1531. + generic FPC_CHECKPOINTER
  1532. + first parameter offset in stack now portable
  1533. * rename some constants
  1534. + move some cpu stuff to other units
  1535. - remove unused constents
  1536. * fix stacksize for some targets
  1537. * fix generic size problems which depend now on EXTEND_SIZE constant
  1538. Revision 1.61 2002/04/19 15:46:02 peter
  1539. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1540. in most cases and not written to the ppu
  1541. * add mangeledname_prefix() routine to generate the prefix of
  1542. manglednames depending on the current procedure, object and module
  1543. * removed static procprefix since the mangledname is now build only
  1544. on demand from tprocdef.mangledname
  1545. Revision 1.60 2002/04/14 16:53:10 carl
  1546. + align code section and data section according to alignment rules
  1547. Revision 1.59 2002/04/07 17:58:38 carl
  1548. + generic stack checking
  1549. Revision 1.58 2002/04/04 19:06:03 peter
  1550. * removed unused units
  1551. * use tlocation.size in cg.a_*loc*() routines
  1552. }