pmodules.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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. aasmbase,aasmtai,aasmcpu,
  31. cgbase,cpuinfo,
  32. ncgutil,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,cpubase,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure create_objectfile;
  40. var
  41. DLLScanner : TDLLScanner;
  42. s : string;
  43. KeepShared : TStringList;
  44. begin
  45. { try to create import entries from system dlls }
  46. if target_info.DllScanSupported and
  47. (not current_module.linkOtherSharedLibs.Empty) then
  48. begin
  49. { Init DLLScanner }
  50. if assigned(CDLLScanner[target_info.system]) then
  51. DLLScanner:=CDLLScanner[target_info.system].Create
  52. else
  53. internalerror(200104121);
  54. KeepShared:=TStringList.Create;
  55. { Walk all shared libs }
  56. While not current_module.linkOtherSharedLibs.Empty do
  57. begin
  58. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  59. if not DLLScanner.scan(s) then
  60. KeepShared.Concat(s);
  61. end;
  62. DLLscanner.Free;
  63. { Recreate import section }
  64. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  65. begin
  66. if assigned(importssection)then
  67. importssection.clear
  68. else
  69. importssection:=taasmoutput.Create;
  70. importlib.generatelib;
  71. end;
  72. { Readd the not processed files }
  73. while not KeepShared.Empty do
  74. begin
  75. s:=KeepShared.GetFirst;
  76. current_module.linkOtherSharedLibs.add(s,link_allways);
  77. end;
  78. KeepShared.Free;
  79. end;
  80. { create the .s file and assemble it }
  81. GenerateAsm(false);
  82. { Also create a smartlinked version ? }
  83. if (cs_create_smart in aktmoduleswitches) then
  84. begin
  85. { regenerate the importssection for win32 }
  86. if assigned(importssection) and
  87. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  88. begin
  89. importsSection.clear;
  90. importlib.generatesmartlib;
  91. end;
  92. GenerateAsm(true);
  93. if target_asm.needar then
  94. Linker.MakeStaticLibrary;
  95. end;
  96. { resource files }
  97. CompileResourceFiles;
  98. end;
  99. procedure insertobjectfile;
  100. { Insert the used object file for this unit in the used list for this unit }
  101. begin
  102. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  103. current_module.flags:=current_module.flags or uf_static_linked;
  104. if (cs_create_smart in aktmoduleswitches) then
  105. begin
  106. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  107. current_module.flags:=current_module.flags or uf_smart_linked;
  108. end;
  109. end;
  110. procedure insertsegment;
  111. procedure fixseg(p:TAAsmoutput;sec:TSection);
  112. begin
  113. p.insert(Tai_section.Create(sec));
  114. if (cs_create_smart in aktmoduleswitches) then
  115. p.insert(Tai_cut.Create);
  116. p.concat(Tai_section.Create(sec_none));
  117. end;
  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(hp.u.modulename^+'_$THREADVARLIST'));
  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(current_module.modulename^+'_$THREADVARLIST'));
  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. if count > 0 then
  187. have_local_threadvars := true;
  188. end;
  189. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  190. var
  191. ltvTable : taasmoutput;
  192. begin
  193. ltvTable:=taasmoutput(arg);
  194. if (tsym(p).typ=varsym) and
  195. (vo_is_thread_var in tvarsym(p).varoptions) then
  196. begin
  197. { address of threadvar }
  198. ltvTable.concat(tai_const_symbol.createdataname(tvarsym(p).mangledname));
  199. { size of threadvar }
  200. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  201. end;
  202. end;
  203. procedure InsertThreadvars;
  204. var
  205. ltvTable : TAAsmoutput;
  206. begin
  207. ltvTable:=TAAsmoutput.create;
  208. if assigned(current_module.globalsymtable) then
  209. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  211. if ltvTable.first<>nil then
  212. begin
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$THREADVARLIST',0));
  215. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$THREADVARLIST'));
  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(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  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(current_module.modulename^+'_RESOURCESTRINGLIST'));
  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('INIT$$'+hp.u.modulename^))
  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('FINALIZE$$'+hp.u.modulename^))
  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('INIT$$'+current_module.modulename^))
  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('FINALIZE$$'+current_module.modulename^))
  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. {$ifdef x86_64}
  325. system_x86_64_linux:
  326. ;
  327. {$endif x86_64}
  328. {$ifdef i386}
  329. system_i386_OS2,system_i386_EMX:
  330. ;
  331. {$endif i386}
  332. {$ifdef powerpc}
  333. system_powerpc_macos:
  334. ;
  335. {$endif powerpc}
  336. {$ifdef alpha}
  337. system_alpha_linux:
  338. ;
  339. {$endif alpha}
  340. {$ifdef m68k}
  341. system_m68k_Mac:
  342. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  343. system_m68k_PalmOS:
  344. ;
  345. {$endif m68k}
  346. {$IFDEF SPARC}
  347. system_SPARC_Linux:
  348. ;
  349. {$ENDIF SPARC}
  350. else
  351. begin
  352. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  353. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  354. end;
  355. end;
  356. {$ifdef m68k}
  357. if target_info.system<>system_m68k_PalmOS then
  358. begin
  359. dataSegment.concat(Tai_align.Create(const_align(4)));
  360. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  361. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  362. end;
  363. {$else m68k}
  364. dataSegment.concat(Tai_align.Create(const_align(4)));
  365. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  366. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  367. {$endif m68k}
  368. end;
  369. procedure insertstacklength;
  370. begin
  371. { stacksize can be specified and is now simulated }
  372. dataSegment.concat(Tai_align.Create(const_align(4)));
  373. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  374. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  375. end;
  376. procedure loaddefaultunits;
  377. procedure AddUnit(const s:string);
  378. var
  379. hp : tppumodule;
  380. unitsym : tunitsym;
  381. begin
  382. { load unit }
  383. hp:=registerunit(current_module,s,'');
  384. hp.loadppu;
  385. hp.adddependency(current_module);
  386. current_module.addusedunit(hp,false);
  387. { add to symtable stack }
  388. tsymtable(hp.globalsymtable).next:=symtablestack;
  389. symtablestack:=hp.globalsymtable;
  390. { insert unitsym }
  391. unitsym:=tunitsym.create(s,hp.globalsymtable);
  392. inc(unitsym.refs);
  393. refsymtable.insert(unitsym);
  394. end;
  395. begin
  396. { are we compiling the system unit? }
  397. if (cs_compilesystem in aktmoduleswitches) then
  398. begin
  399. { create system defines }
  400. createconstdefs;
  401. { we don't need to reset anything, it's already done in parser.pas }
  402. exit;
  403. end;
  404. { insert the system unit, it is allways the first }
  405. Symtablestack:=nil;
  406. AddUnit('System');
  407. SystemUnit:=TGlobalSymtable(Symtablestack);
  408. { read default constant definitions }
  409. make_ref:=false;
  410. readconstdefs;
  411. make_ref:=true;
  412. {$ifdef cpufpemu}
  413. { Floating point emulation unit? }
  414. if (cs_fp_emulation in aktmoduleswitches) then
  415. AddUnit('SoftFpu');
  416. {$endif cpufpemu}
  417. { Thread support unit? }
  418. if (cs_threading in aktmoduleswitches) then
  419. AddUnit('SysThrds');
  420. { Objpas unit? }
  421. if m_objpas in aktmodeswitches then
  422. AddUnit('ObjPas');
  423. { Profile unit? Needed for go32v2 only }
  424. if (cs_profile in aktmoduleswitches) and
  425. (target_info.system=system_i386_go32v2) then
  426. AddUnit('Profile');
  427. { Units only required for main module }
  428. if not(current_module.is_unit) then
  429. begin
  430. { Heaptrc unit }
  431. if (cs_gdb_heaptrc in aktglobalswitches) then
  432. AddUnit('HeapTrc');
  433. { Lineinfo unit }
  434. if (cs_gdb_lineinfo in aktglobalswitches) then
  435. AddUnit('LineInfo');
  436. end;
  437. { save default symtablestack }
  438. defaultsymtablestack:=symtablestack;
  439. end;
  440. procedure loadunits;
  441. var
  442. s,sorg : stringid;
  443. fn : string;
  444. pu : tused_unit;
  445. hp2 : tmodule;
  446. hp3 : tsymtable;
  447. oldprocsym:tprocsym;
  448. oldprocdef:tprocdef;
  449. unitsym : tunitsym;
  450. begin
  451. oldprocsym:=aktprocsym;
  452. oldprocdef:=aktprocdef;
  453. consume(_USES);
  454. {$ifdef DEBUG}
  455. test_symtablestack;
  456. {$endif DEBUG}
  457. repeat
  458. s:=pattern;
  459. sorg:=orgpattern;
  460. consume(_ID);
  461. { support "<unit> in '<file>'" construct, but not for tp7 }
  462. if not(m_tp7 in aktmodeswitches) then
  463. begin
  464. if try_to_consume(_OP_IN) then
  465. fn:=FixFileName(get_stringconst)
  466. else
  467. fn:='';
  468. end;
  469. { Give a warning if objpas is loaded }
  470. if s='OBJPAS' then
  471. Message(parser_w_no_objpas_use_mode);
  472. { check if the unit is already used }
  473. pu:=tused_unit(current_module.used_units.first);
  474. while assigned(pu) do
  475. begin
  476. if (pu.u.modulename^=s) then
  477. break;
  478. pu:=tused_unit(pu.next);
  479. end;
  480. { avoid uses of itself }
  481. if not assigned(pu) and (s<>current_module.modulename^) then
  482. begin
  483. { register the unit }
  484. hp2:=registerunit(current_module,sorg,fn);
  485. { the current module uses the unit hp2 }
  486. current_module.addusedunit(hp2,true);
  487. end
  488. else
  489. Message1(sym_e_duplicate_id,s);
  490. if token=_COMMA then
  491. begin
  492. pattern:='';
  493. consume(_COMMA);
  494. end
  495. else
  496. break;
  497. until false;
  498. consume(_SEMICOLON);
  499. { Load the units }
  500. pu:=tused_unit(current_module.used_units.first);
  501. while assigned(pu) do
  502. begin
  503. if pu.in_uses then
  504. begin
  505. { store the original name to create the unitsym }
  506. sorg:=pu.u.realmodulename^;
  507. tppumodule(pu.u).loadppu;
  508. { is our module compiled? then we can stop }
  509. if current_module.state=ms_compiled then
  510. exit;
  511. { add this unit to the dependencies }
  512. pu.u.adddependency(current_module);
  513. pu.checksum:=pu.u.crc;
  514. pu.interface_checksum:=pu.u.interface_crc;
  515. { Create unitsym, we need to use the name as specified, we
  516. can not use the modulename because that can be different
  517. when -Un is used }
  518. unitsym:=tunitsym.create(sorg,pu.u.globalsymtable);
  519. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  520. inc(unitsym.refs);
  521. refsymtable.insert(unitsym);
  522. end;
  523. pu:=tused_unit(pu.next);
  524. end;
  525. { set the symtable to systemunit so it gets reorderd correctly,
  526. then insert the units in the symtablestack }
  527. pu:=tused_unit(current_module.used_units.first);
  528. symtablestack:=defaultsymtablestack;
  529. while assigned(pu) do
  530. begin
  531. {$IfDef GDB}
  532. if (cs_debuginfo in aktmoduleswitches) and
  533. (cs_gdb_dbx in aktglobalswitches) and
  534. not pu.is_stab_written then
  535. begin
  536. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  537. pu.is_stab_written:=true;
  538. pu.unitid:=tsymtable(pu.u.globalsymtable).unitid;
  539. end;
  540. {$EndIf GDB}
  541. if pu.in_uses then
  542. begin
  543. { Reinsert in symtablestack }
  544. hp3:=symtablestack;
  545. while assigned(hp3) do
  546. begin
  547. { insert units only once ! }
  548. if pu.u.globalsymtable=hp3 then
  549. break;
  550. hp3:=hp3.next;
  551. { unit isn't inserted }
  552. if hp3=nil then
  553. begin
  554. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  555. symtablestack:=tsymtable(pu.u.globalsymtable);
  556. {$ifdef DEBUG}
  557. test_symtablestack;
  558. {$endif DEBUG}
  559. end;
  560. end;
  561. end;
  562. pu:=tused_unit(pu.next);
  563. end;
  564. aktprocsym:=oldprocsym;
  565. aktprocdef:=oldprocdef;
  566. end;
  567. procedure write_gdb_info;
  568. {$IfDef GDB}
  569. var
  570. hp : tused_unit;
  571. begin
  572. if not (cs_debuginfo in aktmoduleswitches) then
  573. exit;
  574. { now insert the units in the symtablestack }
  575. hp:=tused_unit(current_module.used_units.first);
  576. while assigned(hp) do
  577. begin
  578. if (cs_debuginfo in aktmoduleswitches) and
  579. not hp.is_stab_written then
  580. begin
  581. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  582. hp.is_stab_written:=true;
  583. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  584. end;
  585. hp:=tused_unit(hp.next);
  586. end;
  587. if (not current_module.in_interface) and
  588. assigned(current_module.localsymtable) then
  589. begin
  590. { all types }
  591. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  592. { and all local symbols}
  593. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  594. end
  595. else if assigned(current_module.globalsymtable) then
  596. begin
  597. { all types }
  598. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  599. { and all local symbols}
  600. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  601. end;
  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. end;
  615. {$Else GDB}
  616. begin
  617. end;
  618. {$EndIf GDB}
  619. procedure parse_implementation_uses(symt:tsymtable);
  620. begin
  621. if token=_USES then
  622. begin
  623. loadunits;
  624. {$ifdef DEBUG}
  625. test_symtablestack;
  626. {$endif DEBUG}
  627. end;
  628. end;
  629. procedure setupglobalswitches;
  630. begin
  631. { can't have local browser when no global browser }
  632. if (cs_local_browser in aktmoduleswitches) and
  633. not(cs_browser in aktmoduleswitches) then
  634. exclude(aktmoduleswitches,cs_local_browser);
  635. { define a symbol in delphi,objfpc,tp,gpc mode }
  636. if (m_delphi in aktmodeswitches) then
  637. current_scanner.def_macro('FPC_DELPHI')
  638. else
  639. if (m_tp7 in aktmodeswitches) then
  640. current_scanner.def_macro('FPC_TP')
  641. else
  642. if (m_objfpc in aktmodeswitches) then
  643. current_scanner.def_macro('FPC_OBJFPC')
  644. else
  645. if (m_gpc in aktmodeswitches) then
  646. current_scanner.def_macro('FPC_GPC');
  647. end;
  648. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  649. var
  650. stt : tsymtable;
  651. begin
  652. {Generate a procsym for main}
  653. make_ref:=false;
  654. aktprocsym:=tprocsym.create('$'+name);
  655. { main are allways used }
  656. inc(aktprocsym.refs);
  657. {Try to insert in in static symtable ! }
  658. stt:=symtablestack;
  659. symtablestack:=st;
  660. aktprocdef:=tprocdef.create;
  661. aktprocsym.addprocdef(aktprocdef);
  662. aktprocdef.procsym:=aktprocsym;
  663. symtablestack:=stt;
  664. aktprocdef.proctypeoption:=options;
  665. aktprocdef.setmangledname(target_info.cprefix+name);
  666. aktprocdef.forwarddef:=false;
  667. make_ref:=true;
  668. { The localst is a local symtable. Change it into the static
  669. symtable }
  670. aktprocdef.localst.free;
  671. aktprocdef.localst:=st;
  672. { and insert the procsym in symtable }
  673. st.insert(aktprocsym);
  674. { set some informations about the main program }
  675. with procinfo do
  676. begin
  677. _class:=nil;
  678. para_offset:=target_info.first_parm_offset;
  679. framepointer.enum:=R_INTREGISTER;
  680. framepointer.number:=NR_FRAME_POINTER_REG;
  681. flags:=0;
  682. procdef:=aktprocdef;
  683. end;
  684. end;
  685. procedure proc_unit;
  686. function is_assembler_generated:boolean;
  687. begin
  688. is_assembler_generated:=(Errorcount=0) and
  689. not(
  690. codeSegment.empty and
  691. dataSegment.empty and
  692. bssSegment.empty and
  693. ((importssection=nil) or importsSection.empty) and
  694. ((resourcesection=nil) or resourceSection.empty) and
  695. ((resourcestringlist=nil) or resourcestringList.empty)
  696. );
  697. end;
  698. var
  699. main_file: tinputfile;
  700. st : tsymtable;
  701. unitst : tglobalsymtable;
  702. {$ifdef GDB}
  703. pu : tused_unit;
  704. {$endif GDB}
  705. store_crc,store_interface_crc : cardinal;
  706. s2 : ^string; {Saves stack space}
  707. force_init_final : boolean;
  708. begin
  709. consume(_UNIT);
  710. if compile_level=1 then
  711. Status.IsExe:=false;
  712. if token=_ID then
  713. begin
  714. { create filenames and unit name }
  715. main_file := current_scanner.inputfile;
  716. while assigned(main_file.next) do
  717. main_file := main_file.next;
  718. current_module.SetFileName(main_file.path^+main_file.name^,true);
  719. stringdispose(current_module.modulename);
  720. stringdispose(current_module.realmodulename);
  721. current_module.modulename:=stringdup(pattern);
  722. current_module.realmodulename:=stringdup(orgpattern);
  723. { check for system unit }
  724. new(s2);
  725. s2^:=upper(SplitName(main_file.name^));
  726. if (cs_check_unit_name in aktglobalswitches) and
  727. not((current_module.modulename^=s2^) or
  728. ((length(current_module.modulename^)>8) and
  729. (copy(current_module.modulename^,1,8)=s2^))) then
  730. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  731. if (current_module.modulename^='SYSTEM') then
  732. include(aktmoduleswitches,cs_compilesystem);
  733. dispose(s2);
  734. end;
  735. consume(_ID);
  736. consume(_SEMICOLON);
  737. consume(_INTERFACE);
  738. { global switches are read, so further changes aren't allowed }
  739. current_module.in_global:=false;
  740. { handle the global switches }
  741. setupglobalswitches;
  742. Comment(V_Used,'Loading interface units from '+current_module.modulename^);
  743. // Message1(unit_u_start_parse_interface,current_module.modulename^);
  744. { update status }
  745. status.currentmodule:=current_module.realmodulename^;
  746. { maybe turn off m_objpas if we are compiling objpas }
  747. if (current_module.modulename^='OBJPAS') then
  748. exclude(aktmodeswitches,m_objpas);
  749. parse_only:=true;
  750. { generate now the global symboltable }
  751. st:=tglobalsymtable.create(current_module.modulename^);
  752. refsymtable:=st;
  753. unitst:=tglobalsymtable(st);
  754. { define first as local to overcome dependency conflicts }
  755. current_module.localsymtable:=st;
  756. { the unit name must be usable as a unit specifier }
  757. { inside the unit itself (PM) }
  758. { this also forbids to have another symbol }
  759. { with the same name as the unit }
  760. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  761. { load default units, like the system unit }
  762. loaddefaultunits;
  763. { reset }
  764. make_ref:=true;
  765. lexlevel:=0;
  766. { insert qualifier for the system unit (allows system.writeln) }
  767. if not(cs_compilesystem in aktmoduleswitches) then
  768. begin
  769. if token=_USES then
  770. begin
  771. loadunits;
  772. { has it been compiled at a higher level ?}
  773. if current_module.state=ms_compiled then
  774. begin
  775. { this unit symtable is obsolete }
  776. { dispose(unitst,done);
  777. disposed as localsymtable !! }
  778. RestoreUnitSyms;
  779. exit;
  780. end;
  781. end;
  782. { ... but insert the symbol table later }
  783. st.next:=symtablestack;
  784. symtablestack:=st;
  785. end
  786. else
  787. { while compiling a system unit, some types are directly inserted }
  788. begin
  789. st.next:=symtablestack;
  790. symtablestack:=st;
  791. insert_intern_types(st);
  792. end;
  793. { now we know the place to insert the constants }
  794. constsymtable:=symtablestack;
  795. { move the global symtab from the temporary local to global }
  796. current_module.globalsymtable:=current_module.localsymtable;
  797. current_module.localsymtable:=nil;
  798. reset_global_defs;
  799. { number all units, so we know if a unit is used by this unit or
  800. needs to be added implicitly }
  801. current_module.numberunits;
  802. { ... parse the declarations }
  803. Comment(V_Used,'Parsing interface of '+current_module.modulename^);
  804. // Message1(parser_u_parsing_interface,current_module.realmodulename^);
  805. read_interface_declarations;
  806. { leave when we got an error }
  807. if (Errorcount>0) and not status.skip_error then
  808. begin
  809. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  810. status.skip_error:=true;
  811. exit;
  812. end;
  813. {else in inteface its somatimes necessary even if unused
  814. st^.allunitsused; }
  815. {$ifdef New_GDB}
  816. write_gdb_info;
  817. {$endIf Def New_GDB}
  818. if not(cs_compilesystem in aktmoduleswitches) then
  819. if (Errorcount=0) then
  820. tppumodule(current_module).getppucrc;
  821. { Parse the implementation section }
  822. consume(_IMPLEMENTATION);
  823. current_module.in_interface:=false;
  824. Comment(V_Used,'Loading implementation units from '+current_module.modulename^);
  825. // Message1(unit_u_start_parse_implementation,current_module.modulename^);
  826. parse_only:=false;
  827. { generates static symbol table }
  828. st:=tstaticsymtable.create(current_module.modulename^);
  829. current_module.localsymtable:=st;
  830. { remove the globalsymtable from the symtable stack }
  831. { to reinsert it after loading the implementation units }
  832. symtablestack:=unitst.next;
  833. { we don't want implementation units symbols in unitsymtable !! PM }
  834. refsymtable:=st;
  835. { Read the implementation units }
  836. parse_implementation_uses(unitst);
  837. if current_module.state=ms_compiled then
  838. begin
  839. RestoreUnitSyms;
  840. exit;
  841. end;
  842. { reset ranges/stabs in exported definitions }
  843. reset_global_defs;
  844. { All units are read, now give them a number }
  845. current_module.numberunits;
  846. { now we can change refsymtable }
  847. refsymtable:=st;
  848. { but reinsert the global symtable as lasts }
  849. unitst.next:=symtablestack;
  850. symtablestack:=unitst;
  851. tstoredsymtable(symtablestack).chainoperators;
  852. {$ifdef DEBUG}
  853. test_symtablestack;
  854. {$endif DEBUG}
  855. constsymtable:=symtablestack;
  856. {$ifdef Splitheap}
  857. if testsplit then
  858. begin
  859. Split_Heap;
  860. allow_special:=true;
  861. Switch_to_temp_heap;
  862. end;
  863. { it will report all crossings }
  864. allow_special:=false;
  865. {$endif Splitheap}
  866. Comment(V_Used,'Parsing implementation of '+current_module.modulename^);
  867. if current_module.in_interface then
  868. internalerror(200212285);
  869. // Message1(parser_u_parsing_implementation,current_module.modulename^);
  870. { Compile the unit }
  871. codegen_newprocedure;
  872. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  873. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  874. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  875. compile_proc_body(true,false);
  876. codegen_doneprocedure;
  877. { avoid self recursive destructor call !! PM }
  878. aktprocdef.localst:=nil;
  879. { if the unit contains ansi/widestrings, initialization and
  880. finalization code must be forced }
  881. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  882. tstaticsymtable(current_module.localsymtable).needs_init_final;
  883. { should we force unit initialization? }
  884. { this is a hack, but how can it be done better ? }
  885. if force_init_final and ((current_module.flags and uf_init)=0) then
  886. begin
  887. current_module.flags:=current_module.flags or uf_init;
  888. { now we can insert a cut }
  889. if (cs_create_smart in aktmoduleswitches) then
  890. codeSegment.concat(Tai_cut.Create);
  891. genimplicitunitinit(codesegment);
  892. end;
  893. { finalize? }
  894. if token=_FINALIZATION then
  895. begin
  896. { set module options }
  897. current_module.flags:=current_module.flags or uf_finalize;
  898. { Compile the finalize }
  899. codegen_newprocedure;
  900. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  901. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  902. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  903. compile_proc_body(true,false);
  904. codegen_doneprocedure;
  905. end
  906. else if force_init_final then
  907. begin
  908. current_module.flags:=current_module.flags or uf_finalize;
  909. { now we can insert a cut }
  910. if (cs_create_smart in aktmoduleswitches) then
  911. codeSegment.concat(Tai_cut.Create);
  912. genimplicitunitfinal(codesegment);
  913. end;
  914. { the last char should always be a point }
  915. consume(_POINT);
  916. { generate a list of threadvars }
  917. InsertThreadvars;
  918. { Generate resoucestrings }
  919. If ResourceStrings.ResStrCount>0 then
  920. begin
  921. ResourceStrings.CreateResourceStringList;
  922. current_module.flags:=current_module.flags or uf_has_resources;
  923. { only write if no errors found }
  924. if (Errorcount=0) then
  925. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  926. end;
  927. { avoid self recursive destructor call !! PM }
  928. aktprocdef.localst:=nil;
  929. { absence does not matter here !! }
  930. aktprocdef.forwarddef:=false;
  931. { test static symtable }
  932. if (Errorcount=0) then
  933. begin
  934. tstoredsymtable(st).allsymbolsused;
  935. tstoredsymtable(st).allunitsused;
  936. tstoredsymtable(st).allprivatesused;
  937. end;
  938. { size of the static data }
  939. datasize:=st.datasize;
  940. {$ifdef GDB}
  941. { add all used definitions even for implementation}
  942. if (cs_debuginfo in aktmoduleswitches) then
  943. begin
  944. {$IfnDef New_GDB}
  945. if assigned(current_module.globalsymtable) then
  946. begin
  947. { all types }
  948. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  949. { and all local symbols}
  950. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  951. end;
  952. { all local types }
  953. tglobalsymtable(st)^.concattypestabto(debuglist);
  954. { and all local symbols}
  955. st^.concatstabto(debuglist);
  956. {$else New_GDB}
  957. write_gdb_info;
  958. {$endIf Def New_GDB}
  959. end;
  960. {$endif GDB}
  961. reset_global_defs;
  962. if (Errorcount=0) then
  963. begin
  964. { tests, if all (interface) forwards are resolved }
  965. tstoredsymtable(symtablestack).check_forwards;
  966. { check if all private fields are used }
  967. tstoredsymtable(symtablestack).allprivatesused;
  968. { remove cross unit overloads }
  969. tstoredsymtable(symtablestack).unchain_overloaded;
  970. end;
  971. {$ifdef GDB}
  972. tglobalsymtable(symtablestack).is_stab_written:=false;
  973. {$endif GDB}
  974. { leave when we got an error }
  975. if (Errorcount>0) and not status.skip_error then
  976. begin
  977. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  978. status.skip_error:=true;
  979. exit;
  980. end;
  981. { generate imports }
  982. if current_module.uses_imports then
  983. importlib.generatelib;
  984. { insert own objectfile, or say that it's in a library
  985. (no check for an .o when loading) }
  986. if is_assembler_generated then
  987. insertobjectfile
  988. else
  989. current_module.flags:=current_module.flags or uf_no_link;
  990. if cs_local_browser in aktmoduleswitches then
  991. current_module.localsymtable:=refsymtable;
  992. {$ifdef GDB}
  993. pu:=tused_unit(usedunits.first);
  994. while assigned(pu) do
  995. begin
  996. if assigned(pu.u.globalsymtable) then
  997. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  998. pu:=tused_unit(pu.next);
  999. end;
  1000. {$endif GDB}
  1001. if is_assembler_generated then
  1002. begin
  1003. { finish asmlist by adding segment starts }
  1004. insertsegment;
  1005. { assemble }
  1006. create_objectfile;
  1007. end;
  1008. { Write out the ppufile after the object file has been created }
  1009. store_interface_crc:=current_module.interface_crc;
  1010. store_crc:=current_module.crc;
  1011. if (Errorcount=0) then
  1012. tppumodule(current_module).writeppu;
  1013. if not(cs_compilesystem in aktmoduleswitches) then
  1014. if store_interface_crc<>current_module.interface_crc then
  1015. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1016. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  1017. {$ifdef EXTDEBUG}
  1018. if not(cs_compilesystem in aktmoduleswitches) then
  1019. if (store_crc<>current_module.crc) and simplify_ppu then
  1020. Comment(V_Note,current_module.ppufilename^+' implementation CRC changed '+
  1021. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1022. {$endif EXTDEBUG}
  1023. { remove static symtable (=refsymtable) here to save some mem }
  1024. if not (cs_local_browser in aktmoduleswitches) then
  1025. begin
  1026. st.free;
  1027. current_module.localsymtable:=nil;
  1028. end;
  1029. RestoreUnitSyms;
  1030. { leave when we got an error }
  1031. if (Errorcount>0) and not status.skip_error then
  1032. begin
  1033. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1034. status.skip_error:=true;
  1035. exit;
  1036. end;
  1037. Comment(V_Used,'Finished compiling module '+current_module.modulename^);
  1038. end;
  1039. procedure proc_program(islibrary : boolean);
  1040. var
  1041. main_file: tinputfile;
  1042. st : tsymtable;
  1043. hp : tmodule;
  1044. begin
  1045. DLLsource:=islibrary;
  1046. Status.IsLibrary:=IsLibrary;
  1047. Status.IsExe:=true;
  1048. parse_only:=false;
  1049. { relocation works only without stabs under win32 !! PM }
  1050. { internal assembler uses rva for stabs info
  1051. so it should work with relocated DLLs }
  1052. if RelocSection and
  1053. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1054. (target_info.assem<>as_i386_pecoff) then
  1055. begin
  1056. include(aktglobalswitches,cs_link_strip);
  1057. { Warning stabs info does not work with reloc section !! }
  1058. if cs_debuginfo in aktmoduleswitches then
  1059. begin
  1060. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1061. Message(parser_w_parser_win32_debug_needs_WN);
  1062. exclude(aktmoduleswitches,cs_debuginfo);
  1063. end;
  1064. end;
  1065. { get correct output names }
  1066. main_file := current_scanner.inputfile;
  1067. while assigned(main_file.next) do
  1068. main_file := main_file.next;
  1069. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1070. if islibrary then
  1071. begin
  1072. consume(_LIBRARY);
  1073. stringdispose(current_module.modulename);
  1074. current_module.modulename:=stringdup(pattern);
  1075. current_module.islibrary:=true;
  1076. exportlib.preparelib(pattern);
  1077. consume(_ID);
  1078. consume(_SEMICOLON);
  1079. end
  1080. else
  1081. { is there an program head ? }
  1082. if token=_PROGRAM then
  1083. begin
  1084. consume(_PROGRAM);
  1085. stringdispose(current_module.modulename);
  1086. stringdispose(current_module.realmodulename);
  1087. current_module.modulename:=stringdup(pattern);
  1088. current_module.realmodulename:=stringdup(orgpattern);
  1089. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1090. exportlib.preparelib(pattern);
  1091. consume(_ID);
  1092. if token=_LKLAMMER then
  1093. begin
  1094. consume(_LKLAMMER);
  1095. repeat
  1096. consume(_ID);
  1097. until not try_to_consume(_COMMA);
  1098. consume(_RKLAMMER);
  1099. end;
  1100. consume(_SEMICOLON);
  1101. end
  1102. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1103. exportlib.preparelib(current_module.modulename^);
  1104. { global switches are read, so further changes aren't allowed }
  1105. current_module.in_global:=false;
  1106. { setup things using the global switches }
  1107. setupglobalswitches;
  1108. { set implementation flag }
  1109. current_module.in_interface:=false;
  1110. { insert after the unit symbol tables the static symbol table }
  1111. { of the program }
  1112. st:=tstaticsymtable.create(current_module.modulename^);;
  1113. current_module.localsymtable:=st;
  1114. refsymtable:=st;
  1115. { load standard units (system,objpas,profile unit) }
  1116. loaddefaultunits;
  1117. { reset }
  1118. lexlevel:=0;
  1119. {Load the units used by the program we compile.}
  1120. if token=_USES then
  1121. loadunits;
  1122. tstoredsymtable(symtablestack).chainoperators;
  1123. { reset ranges/stabs in exported definitions }
  1124. reset_global_defs;
  1125. { All units are read, now give them a number }
  1126. current_module.numberunits;
  1127. {Insert the name of the main program into the symbol table.}
  1128. if current_module.realmodulename^<>'' then
  1129. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1130. { ...is also constsymtable, this is the symtable where }
  1131. { the elements of enumeration types are inserted }
  1132. constsymtable:=st;
  1133. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1134. {The program intialization needs an alias, so it can be called
  1135. from the bootstrap code.}
  1136. codegen_newprocedure;
  1137. if islibrary then
  1138. begin
  1139. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1140. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1141. { Win32 startup code needs a single name }
  1142. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1143. aktprocdef.aliasnames.insert('PASCALMAIN');
  1144. { this code is called from C so we need to save some
  1145. registers }
  1146. include(aktprocdef.procoptions,po_savestdregs);
  1147. end
  1148. else
  1149. begin
  1150. gen_main_procsym('main',potype_proginit,st);
  1151. aktprocdef.aliasnames.insert('program_init');
  1152. aktprocdef.aliasnames.insert('PASCALMAIN');
  1153. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1154. end;
  1155. {$IFDEF SPARC}
  1156. ProcInfo.After_Header;
  1157. {main function is declared as
  1158. PROCEDURE main(ArgC:Integer;ArgV,EnvP:ARRAY OF PChar):Integer;CDECL;
  1159. So, all parameters are passerd into registers in sparc architecture.}
  1160. {$ENDIF SPARC}
  1161. compile_proc_body(true,false);
  1162. { should we force unit initialization? }
  1163. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1164. begin
  1165. current_module.flags:=current_module.flags or (uf_init or uf_finalize);
  1166. { Add initialize section }
  1167. if (cs_create_smart in aktmoduleswitches) then
  1168. codeSegment.concat(Tai_cut.Create);
  1169. genimplicitunitinit(codesegment);
  1170. { Add finalize section }
  1171. if (cs_create_smart in aktmoduleswitches) then
  1172. codeSegment.concat(Tai_cut.Create);
  1173. genimplicitunitfinal(codesegment);
  1174. end;
  1175. { Add symbol to the exports section for win32 so smartlinking a
  1176. DLL will include the edata section }
  1177. if assigned(exportlib) and
  1178. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1179. assigned(current_module._exports.first) then
  1180. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1181. { avoid self recursive destructor call !! PM }
  1182. aktprocdef.localst:=nil;
  1183. { consider these symbols as global ones for browser
  1184. but the typecasting of the globalsymtable with tglobalsymtable
  1185. can then lead to problems (PFV)
  1186. current_module.globalsymtable:=current_module.localsymtable;
  1187. current_module.localsymtable:=nil;}
  1188. If ResourceStrings.ResStrCount>0 then
  1189. begin
  1190. ResourceStrings.CreateResourceStringList;
  1191. { only write if no errors found }
  1192. if (Errorcount=0) then
  1193. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1194. end;
  1195. codegen_doneprocedure;
  1196. { finalize? }
  1197. if token=_FINALIZATION then
  1198. begin
  1199. { set module options }
  1200. current_module.flags:=current_module.flags or uf_finalize;
  1201. { Compile the finalize }
  1202. codegen_newprocedure;
  1203. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1204. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1205. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1206. compile_proc_body(true,false);
  1207. codegen_doneprocedure;
  1208. end;
  1209. { consume the last point }
  1210. consume(_POINT);
  1211. {$ifdef New_GDB}
  1212. write_gdb_info;
  1213. {$endIf Def New_GDB}
  1214. { leave when we got an error }
  1215. if (Errorcount>0) and not status.skip_error then
  1216. begin
  1217. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1218. status.skip_error:=true;
  1219. exit;
  1220. end;
  1221. { test static symtable }
  1222. if (Errorcount=0) then
  1223. begin
  1224. tstoredsymtable(st).allsymbolsused;
  1225. tstoredsymtable(st).allunitsused;
  1226. tstoredsymtable(st).allprivatesused;
  1227. end;
  1228. { generate a list of threadvars }
  1229. InsertThreadvars;
  1230. { generate imports }
  1231. if current_module.uses_imports then
  1232. importlib.generatelib;
  1233. if islibrary or
  1234. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1235. (target_info.system=system_i386_NETWARE) then
  1236. exportlib.generatelib;
  1237. { insert Tables and Heap }
  1238. insertThreadVarTablesTable;
  1239. insertResourceTablesTable;
  1240. insertinitfinaltable;
  1241. insertheap;
  1242. insertstacklength;
  1243. datasize:=symtablestack.datasize;
  1244. { finish asmlist by adding segment starts }
  1245. insertsegment;
  1246. { insert own objectfile }
  1247. insertobjectfile;
  1248. { assemble and link }
  1249. create_objectfile;
  1250. { leave when we got an error }
  1251. if (Errorcount>0) and not status.skip_error then
  1252. begin
  1253. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1254. status.skip_error:=true;
  1255. exit;
  1256. end;
  1257. { create the executable when we are at level 1 }
  1258. if (compile_level=1) then
  1259. begin
  1260. { insert all .o files from all loaded units }
  1261. hp:=tmodule(loaded_units.first);
  1262. while assigned(hp) do
  1263. begin
  1264. linker.AddModuleFiles(hp);
  1265. hp:=tmodule(hp.next);
  1266. end;
  1267. { write .def file }
  1268. if (cs_link_deffile in aktglobalswitches) then
  1269. deffile.writefile;
  1270. { finally we can create a executable }
  1271. if (not current_module.is_unit) then
  1272. begin
  1273. if DLLSource then
  1274. linker.MakeSharedLibrary
  1275. else
  1276. linker.MakeExecutable;
  1277. end;
  1278. end;
  1279. end;
  1280. end.
  1281. {
  1282. $Log$
  1283. Revision 1.99 2003-03-23 23:21:42 hajny
  1284. + emx target added
  1285. Revision 1.98 2003/03/17 22:20:08 peter
  1286. *** empty log message ***
  1287. Revision 1.97 2003/03/17 13:36:39 peter
  1288. * fix import linking under linux
  1289. Revision 1.96 2003/02/19 22:00:14 daniel
  1290. * Code generator converted to new register notation
  1291. - Horribily outdated todo.txt removed
  1292. Revision 1.95 2003/02/06 22:36:55 mazen
  1293. * fixing bug related to errornous program main entry stack frame
  1294. Revision 1.94 2003/01/30 21:46:20 peter
  1295. * tai_const_symbol.createdataname added
  1296. Revision 1.93 2003/01/11 11:19:54 hajny
  1297. * correction from rev. 1.88 put back
  1298. Revision 1.92 2003/01/08 18:43:56 daniel
  1299. * Tregister changed into a record
  1300. Revision 1.91 2003/01/05 13:36:53 florian
  1301. * x86-64 compiles
  1302. + very basic support for float128 type (x86-64 only)
  1303. Revision 1.90 2002/12/29 18:17:23 peter
  1304. * insert unitsym with the name as specified in the uses list
  1305. Revision 1.89 2002/12/29 14:57:50 peter
  1306. * unit loading changed to first register units and load them
  1307. afterwards. This is needed to support uses xxx in yyy correctly
  1308. * unit dependency check fixed
  1309. Revision 1.88 2002/12/27 19:09:33 hajny
  1310. * another (hopefully final ;-) ) fix for not linked import libraries for units with no code
  1311. Revision 1.87 2002/12/24 23:32:56 peter
  1312. * Use FixFilename for specified unit sourcefile in uses
  1313. Revision 1.86 2002/12/06 16:56:58 peter
  1314. * only compile cs_fp_emulation support when cpufpuemu is defined
  1315. * define cpufpuemu for m68k only
  1316. Revision 1.85 2002/11/30 21:32:24 carl
  1317. + Add loading of softfpu in emulation mode
  1318. + Correct routine call for softfpu
  1319. * Extended type must also be defined even with softfpu
  1320. Revision 1.84 2002/11/15 01:58:53 peter
  1321. * merged changes from 1.0.7 up to 04-11
  1322. - -V option for generating bug report tracing
  1323. - more tracing for option parsing
  1324. - errors for cdecl and high()
  1325. - win32 import stabs
  1326. - win32 records<=8 are returned in eax:edx (turned off by default)
  1327. - heaptrc update
  1328. - more info for temp management in .s file with EXTDEBUG
  1329. Revision 1.83 2002/11/09 15:33:26 carl
  1330. * major alignment updates
  1331. Revision 1.82 2002/10/16 06:32:52 michael
  1332. + Renamed thread unit to systhrds
  1333. Revision 1.81 2002/10/14 19:42:34 peter
  1334. * only use init tables for threadvars
  1335. Revision 1.80 2002/10/06 19:41:30 peter
  1336. * Add finalization of typed consts
  1337. * Finalization of globals in the main program
  1338. Revision 1.79 2002/09/09 17:34:15 peter
  1339. * tdicationary.replace added to replace and item in a dictionary. This
  1340. is only allowed for the same name
  1341. * varsyms are inserted in symtable before the types are parsed. This
  1342. fixes the long standing "var longint : longint" bug
  1343. - consume_idlist and idstringlist removed. The loops are inserted
  1344. at the callers place and uses the symtable for duplicate id checking
  1345. Revision 1.78 2002/09/07 15:25:07 peter
  1346. * old logs removed and tabs fixed
  1347. Revision 1.77 2002/09/03 16:26:27 daniel
  1348. * Make Tprocdef.defs protected
  1349. Revision 1.76 2002/09/02 18:46:26 peter
  1350. * insert PASCALMAIN in library for Win32 only
  1351. Revision 1.75 2002/08/31 15:59:30 florian
  1352. + HEAP* stuff must be generated for Linux/PPC as well
  1353. + direct assembler reader searches now global and static symtables as well
  1354. Revision 1.74 2002/08/25 19:25:20 peter
  1355. * sym.insert_in_data removed
  1356. * symtable.insertvardata/insertconstdata added
  1357. * removed insert_in_data call from symtable.insert, it needs to be
  1358. called separatly. This allows to deref the address calculation
  1359. * procedures now calculate the parast addresses after the procedure
  1360. directives are parsed. This fixes the cdecl parast problem
  1361. * push_addr_param has an extra argument that specifies if cdecl is used
  1362. or not
  1363. Revision 1.73 2002/08/18 20:06:25 peter
  1364. * inlining is now also allowed in interface
  1365. * renamed write/load to ppuwrite/ppuload
  1366. * tnode storing in ppu
  1367. * nld,ncon,nbas are already updated for storing in ppu
  1368. Revision 1.72 2002/08/17 09:23:39 florian
  1369. * first part of procinfo rewrite
  1370. Revision 1.71 2002/08/11 13:24:12 peter
  1371. * saving of asmsymbols in ppu supported
  1372. * asmsymbollist global is removed and moved into a new class
  1373. tasmlibrarydata that will hold the info of a .a file which
  1374. corresponds with a single module. Added librarydata to tmodule
  1375. to keep the library info stored for the module. In the future the
  1376. objectfiles will also be stored to the tasmlibrarydata class
  1377. * all getlabel/newasmsymbol and friends are moved to the new class
  1378. Revision 1.70 2002/08/10 14:46:29 carl
  1379. + moved target_cpu_string to cpuinfo
  1380. * renamed asmmode enum.
  1381. * assembler reader has now less ifdef's
  1382. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1383. Revision 1.69 2002/07/26 21:15:41 florian
  1384. * rewrote the system handling
  1385. Revision 1.68 2002/07/04 20:43:01 florian
  1386. * first x86-64 patches
  1387. Revision 1.67 2002/07/01 18:46:25 peter
  1388. * internal linker
  1389. * reorganized aasm layer
  1390. Revision 1.66 2002/05/16 19:46:43 carl
  1391. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1392. + try to fix temp allocation (still in ifdef)
  1393. + generic constructor calls
  1394. + start of tassembler / tmodulebase class cleanup
  1395. Revision 1.65 2002/05/14 19:34:49 peter
  1396. * removed old logs and updated copyright year
  1397. Revision 1.64 2002/05/12 16:53:09 peter
  1398. * moved entry and exitcode to ncgutil and cgobj
  1399. * foreach gets extra argument for passing local data to the
  1400. iterator function
  1401. * -CR checks also class typecasts at runtime by changing them
  1402. into as
  1403. * fixed compiler to cycle with the -CR option
  1404. * fixed stabs with elf writer, finally the global variables can
  1405. be watched
  1406. * removed a lot of routines from cga unit and replaced them by
  1407. calls to cgobj
  1408. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1409. u32bit then the other is typecasted also to u32bit without giving
  1410. a rangecheck warning/error.
  1411. * fixed pascal calling method with reversing also the high tree in
  1412. the parast, detected by tcalcst3 test
  1413. Revision 1.63 2002/05/06 19:54:50 carl
  1414. + added more patches from Mazen for SPARC port
  1415. Revision 1.62 2002/04/20 21:32:24 carl
  1416. + generic FPC_CHECKPOINTER
  1417. + first parameter offset in stack now portable
  1418. * rename some constants
  1419. + move some cpu stuff to other units
  1420. - remove unused constents
  1421. * fix stacksize for some targets
  1422. * fix generic size problems which depend now on EXTEND_SIZE constant
  1423. Revision 1.61 2002/04/19 15:46:02 peter
  1424. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1425. in most cases and not written to the ppu
  1426. * add mangeledname_prefix() routine to generate the prefix of
  1427. manglednames depending on the current procedure, object and module
  1428. * removed static procprefix since the mangledname is now build only
  1429. on demand from tprocdef.mangledname
  1430. Revision 1.60 2002/04/14 16:53:10 carl
  1431. + align code section and data section according to alignment rules
  1432. Revision 1.59 2002/04/07 17:58:38 carl
  1433. + generic stack checking
  1434. Revision 1.58 2002/04/04 19:06:03 peter
  1435. * removed unused units
  1436. * use tlocation.size in cg.a_*loc*() routines
  1437. }