pmodules.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.inc}
  20. { close old_current_ppu on system that are
  21. short on file handles like DOS system PM }
  22. {$ifdef GO32V1}
  23. {$define SHORT_ON_FILE_HANDLES}
  24. {$endif GO32V1}
  25. {$ifdef GO32V2}
  26. {$define SHORT_ON_FILE_HANDLES}
  27. {$endif GO32V2}
  28. {$define New_GDB}
  29. interface
  30. procedure proc_unit;
  31. procedure proc_program(islibrary : boolean);
  32. implementation
  33. uses
  34. globtype,version,systems,tokens,
  35. cutils,cobjects,comphook,
  36. globals,verbose,fmodule,finput,
  37. symconst,symbase,symppu,symdef,symsym,symtable,aasm,types,
  38. {$ifdef newcg}
  39. cgbase,
  40. {$else newcg}
  41. hcodegen,
  42. {$ifdef i386}
  43. cgai386,
  44. {$ifndef NOTARGETWIN32}
  45. t_win32,
  46. {$endif}
  47. {$endif i386}
  48. {$endif newcg}
  49. link,assemble,import,export,gendef,ppu,comprsrc,
  50. cresstr,cpubase,cpuasm,
  51. {$ifdef GDB}
  52. gdb,
  53. {$endif GDB}
  54. scanner,pbase,psystem,psub,parser;
  55. procedure create_objectfile;
  56. var
  57. DLLScanner : TDLLScanner;
  58. s : string;
  59. begin
  60. { try to create import entries from system dlls }
  61. if target_info.DllScanSupported and
  62. (not current_module.linkOtherSharedLibs.Empty) then
  63. begin
  64. { Init DLLScanner }
  65. case target_info.target of
  66. {$ifdef i386}
  67. {$ifndef NOTARGETWIN32}
  68. target_i386_win32 :
  69. DLLScanner:=tDLLscannerWin32.create;
  70. {$endif}
  71. {$endif}
  72. else
  73. internalerror(769795413);
  74. end;
  75. { Walk all shared libs }
  76. While not current_module.linkOtherSharedLibs.Empty do
  77. begin
  78. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  79. DLLScanner.scan(s)
  80. end;
  81. DLLscanner.Free;
  82. { Recreate import section }
  83. if (target_info.target=target_i386_win32) then
  84. begin
  85. if assigned(importssection)then
  86. importssection.clear
  87. else
  88. importssection:=taasmoutput.Create;
  89. importlib.generatelib;
  90. end;
  91. end;
  92. { create the .s file and assemble it }
  93. GenerateAsm(false);
  94. { Also create a smartlinked version ? }
  95. if (cs_create_smart in aktmoduleswitches) then
  96. begin
  97. { regenerate the importssection for win32 }
  98. if assigned(importssection) and
  99. (target_info.target=target_i386_win32) then
  100. begin
  101. importsSection.clear;
  102. importlib.generatesmartlib;
  103. end;
  104. GenerateAsm(true);
  105. if target_asm.needar then
  106. Linker.MakeStaticLibrary;
  107. end;
  108. { resource files }
  109. CompileResourceFiles;
  110. end;
  111. procedure insertobjectfile;
  112. { Insert the used object file for this unit in the used list for this unit }
  113. begin
  114. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  115. current_module.flags:=current_module.flags or uf_static_linked;
  116. if (cs_create_smart in aktmoduleswitches) then
  117. begin
  118. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  119. current_module.flags:=current_module.flags or uf_smart_linked;
  120. end;
  121. end;
  122. procedure insertsegment;
  123. procedure fixseg(p:TAAsmoutput;sec:tsection);
  124. begin
  125. p.insert(Tai_section.Create(sec));
  126. if (cs_create_smart in aktmoduleswitches) then
  127. p.insert(Tai_cut.Create);
  128. p.concat(Tai_section.Create(sec_none));
  129. end;
  130. begin
  131. { Insert Ident of the compiler }
  132. if (not (cs_create_smart in aktmoduleswitches))
  133. {$ifndef EXTDEBUG}
  134. and (not current_module.is_unit)
  135. {$endif}
  136. then
  137. begin
  138. dataSegment.insert(Tai_align.Create(4));
  139. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  140. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.short_name));
  141. end;
  142. { finish codesegment }
  143. codeSegment.concat(Tai_align.Create(16));
  144. { Insert start and end of sections }
  145. fixseg(codesegment,sec_code);
  146. fixseg(datasegment,sec_data);
  147. fixseg(bsssegment,sec_bss);
  148. { we should use .rdata section for these two no ? }
  149. { .rdata is a read only data section (PM) }
  150. fixseg(rttilist,sec_data);
  151. fixseg(consts,sec_data);
  152. if assigned(resourcestringlist) then
  153. fixseg(resourcestringlist,sec_data);
  154. {$ifdef GDB}
  155. if assigned(debuglist) then
  156. begin
  157. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  158. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  159. fixseg(debuglist,sec_code);
  160. end;
  161. {$endif GDB}
  162. end;
  163. Procedure InsertResourceTablesTable;
  164. var
  165. hp : tused_unit;
  166. ResourceStringTables : taasmoutput;
  167. count : longint;
  168. begin
  169. ResourceStringTables:=TAAsmOutput.Create;
  170. count:=0;
  171. hp:=tused_unit(usedunits.first);
  172. while assigned(hp) do
  173. begin
  174. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  175. begin
  176. ResourceStringTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  177. inc(count);
  178. end;
  179. hp:=tused_unit(hp.next);
  180. end;
  181. { Add program resources, if any }
  182. If ResourceStringList<>Nil then
  183. begin
  184. ResourceStringTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_RESOURCESTRINGLIST'));
  185. Inc(Count);
  186. end;
  187. { TableCount }
  188. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  189. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  190. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  191. { insert in data segment }
  192. if (cs_create_smart in aktmoduleswitches) then
  193. dataSegment.concat(Tai_cut.Create);
  194. dataSegment.concatlist(ResourceStringTables);
  195. ResourceStringTables.free;
  196. end;
  197. procedure InsertInitFinalTable;
  198. var
  199. hp : tused_unit;
  200. unitinits : taasmoutput;
  201. count : longint;
  202. begin
  203. unitinits:=TAAsmOutput.Create;
  204. count:=0;
  205. hp:=tused_unit(usedunits.first);
  206. while assigned(hp) do
  207. begin
  208. { call the unit init code and make it external }
  209. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  210. begin
  211. if (hp.u.flags and uf_init)<>0 then
  212. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+hp.u.modulename^))
  213. else
  214. unitinits.concat(Tai_const.Create_32bit(0));
  215. if (hp.u.flags and uf_finalize)<>0 then
  216. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+hp.u.modulename^))
  217. else
  218. unitinits.concat(Tai_const.Create_32bit(0));
  219. inc(count);
  220. end;
  221. hp:=tused_unit(hp.next);
  222. end;
  223. if current_module.islibrary then
  224. if (current_module.flags and uf_finalize)<>0 then
  225. begin
  226. { INIT code is done by PASCALMAIN calling }
  227. unitinits.concat(Tai_const.Create_32bit(0));
  228. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+current_module.modulename^));
  229. inc(count);
  230. end;
  231. { TableCount,InitCount }
  232. unitinits.insert(Tai_const.Create_32bit(0));
  233. unitinits.insert(Tai_const.Create_32bit(count));
  234. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  235. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  236. { insert in data segment }
  237. if (cs_create_smart in aktmoduleswitches) then
  238. dataSegment.concat(Tai_cut.Create);
  239. dataSegment.concatlist(unitinits);
  240. unitinits.free;
  241. end;
  242. procedure insertheap;
  243. begin
  244. if (cs_create_smart in aktmoduleswitches) then
  245. begin
  246. bssSegment.concat(Tai_cut.Create);
  247. dataSegment.concat(Tai_cut.Create);
  248. end;
  249. { On the Macintosh Classic M68k Architecture
  250. The Heap variable is simply a POINTER to the
  251. real HEAP. The HEAP must be set up by the RTL
  252. and must store the pointer in this value.
  253. On OS/2 the heap is also intialized by the RTL. We do
  254. not output a pointer }
  255. case target_info.target of
  256. {$ifdef i386}
  257. target_i386_OS2:
  258. ;
  259. {$endif i386}
  260. {$ifdef alpha}
  261. target_alpha_linux:
  262. ;
  263. {$endif alpha}
  264. {$ifdef powerpc}
  265. target_powerpc_linux:
  266. ;
  267. {$endif powerpc}
  268. {$ifdef m68k}
  269. target_m68k_Mac:
  270. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  271. target_m68k_PalmOS:
  272. ;
  273. {$endif m68k}
  274. else
  275. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  276. end;
  277. {$ifdef m68k}
  278. if target_info.target<>target_m68k_PalmOS then
  279. begin
  280. dataSegment.concat(Tai_symbol.Createdataname_global('HEAP_SIZE',0));
  281. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  282. end;
  283. {$else m68k}
  284. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  285. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  286. {$endif m68k}
  287. end;
  288. procedure inserttargetspecific;
  289. begin
  290. case target_info.target of
  291. {$ifdef alpha}
  292. target_alpha_linux:
  293. ;
  294. {$endif alpha}
  295. {$ifdef powerpc}
  296. target_powerpc_linux:
  297. ;
  298. {$endif powerpc}
  299. {$ifdef i386}
  300. target_i386_GO32V2 :
  301. begin
  302. { stacksize can be specified }
  303. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  304. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  305. end;
  306. {$endif i386}
  307. {$ifdef m68k}
  308. target_m68k_Atari :
  309. begin
  310. { stacksize can be specified }
  311. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  312. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  313. end;
  314. {$endif m68k}
  315. end;
  316. end;
  317. function loadunit(const s : string;compile_system:boolean) : tmodule;forward;
  318. procedure load_usedunits(compile_system:boolean);
  319. var
  320. pu : tused_unit;
  321. loaded_unit : tmodule;
  322. load_refs : boolean;
  323. nextmapentry : longint;
  324. begin
  325. load_refs:=true;
  326. { init the map }
  327. new(current_module.map);
  328. fillchar(current_module.map^,sizeof(tunitmap),#0);
  329. {$ifdef NEWMAP}
  330. current_module.map^[0]:=current_module;
  331. {$endif NEWMAP}
  332. nextmapentry:=1;
  333. { load the used units from interface }
  334. current_module.in_implementation:=false;
  335. pu:=tused_unit(current_module.used_units.first);
  336. while assigned(pu) do
  337. begin
  338. if (not pu.loaded) and (pu.in_interface) then
  339. begin
  340. loaded_unit:=loadunit(pu.name^,false);
  341. if current_module.compiled then
  342. exit;
  343. { register unit in used units }
  344. pu.u:=loaded_unit;
  345. pu.loaded:=true;
  346. { doubles are not important for that list PM }
  347. pu.u.dependent_units.concat(tdependent_unit.create(current_module));
  348. { need to recompile the current unit ? }
  349. if loaded_unit.crc<>pu.checksum then
  350. begin
  351. Message2(unit_u_recompile_crc_change,current_module.modulename^,pu.name^);
  352. current_module.recompile_reason:=rr_crcchanged;
  353. current_module.do_compile:=true;
  354. dispose(current_module.map);
  355. current_module.map:=nil;
  356. exit;
  357. end;
  358. { setup the map entry for deref }
  359. {$ifndef NEWMAP}
  360. current_module.map^[nextmapentry]:=loaded_unit.globalsymtable;
  361. {$else NEWMAP}
  362. current_module.map^[nextmapentry]:=loaded_unit;
  363. {$endif NEWMAP}
  364. inc(nextmapentry);
  365. if nextmapentry>maxunits then
  366. Message(unit_f_too_much_units);
  367. end;
  368. pu:=tused_unit(pu.next);
  369. end;
  370. { ok, now load the unit }
  371. current_module.globalsymtable:=new(punitsymtable,loadasunit);
  372. { now only read the implementation part }
  373. current_module.in_implementation:=true;
  374. { load the used units from implementation }
  375. pu:=tused_unit(current_module.used_units.first);
  376. while assigned(pu) do
  377. begin
  378. if (not pu.loaded) and (not pu.in_interface) then
  379. begin
  380. loaded_unit:=loadunit(pu.name^,false);
  381. if current_module.compiled then
  382. exit;
  383. { register unit in used units }
  384. pu.u:=loaded_unit;
  385. pu.loaded:=true;
  386. { need to recompile the current unit ? }
  387. if (loaded_unit.interface_crc<>pu.interface_checksum) {and
  388. not(current_module.in_second_compile) } then
  389. begin
  390. Message2(unit_u_recompile_crc_change,current_module.modulename^,pu.name^+' {impl}');
  391. current_module.recompile_reason:=rr_crcchanged;
  392. current_module.do_compile:=true;
  393. dispose(current_module.map);
  394. current_module.map:=nil;
  395. exit;
  396. end;
  397. { setup the map entry for deref }
  398. {$ifndef NEWMAP}
  399. current_module.map^[nextmapentry]:=loaded_unit.globalsymtable;
  400. {$else NEWMAP}
  401. current_module.map^[nextmapentry]:=loaded_unit;
  402. {$endif NEWMAP}
  403. inc(nextmapentry);
  404. if nextmapentry>maxunits then
  405. Message(unit_f_too_much_units);
  406. end;
  407. pu:=tused_unit(pu.next);
  408. end;
  409. { deref }
  410. punitsymtable(current_module.globalsymtable)^.deref;
  411. { load browser info if stored }
  412. if ((current_module.flags and uf_has_browser)<>0) and load_refs then
  413. punitsymtable(current_module.globalsymtable)^.load_symtable_refs;
  414. { remove the map, it's not needed anymore }
  415. dispose(current_module.map);
  416. current_module.map:=nil;
  417. end;
  418. function loadunit(const s : string;compile_system:boolean) : tmodule;
  419. const
  420. ImplIntf : array[boolean] of string[15]=('interface','implementation');
  421. var
  422. st : punitsymtable;
  423. second_time : boolean;
  424. old_current_ppu : pppufile;
  425. old_current_module,hp,hp2 : tmodule;
  426. name : string;{ necessary because current_module.mainsource^ is reset in compile !! }
  427. scanner : pscannerfile;
  428. procedure loadppufile;
  429. begin
  430. { load interface section }
  431. if not current_module.do_compile then
  432. load_interface;
  433. { only load units when we don't recompile }
  434. if not current_module.do_compile then
  435. load_usedunits(compile_system);
  436. { recompile if set }
  437. if current_module.do_compile then
  438. begin
  439. { we don't need the ppufile anymore }
  440. if assigned(current_module.ppufile) then
  441. begin
  442. dispose(current_module.ppufile,done);
  443. current_module.ppufile:=nil;
  444. current_ppu:=nil;
  445. end;
  446. { recompile the unit or give a fatal error if sources not available }
  447. if not(current_module.sources_avail) and
  448. not(current_module.sources_checked) then
  449. if (not current_module.search_unit(current_module.modulename^,true))
  450. and (length(current_module.modulename^)>8) then
  451. current_module.search_unit(copy(current_module.modulename^,1,8),true);
  452. if not(current_module.sources_avail) then
  453. begin
  454. hp:=current_module;
  455. current_module:=old_current_module;
  456. if hp.recompile_reason=rr_noppu then
  457. Message1(unit_f_cant_find_ppu,hp.modulename^)
  458. else
  459. Message1(unit_f_cant_compile_unit,hp.modulename^);
  460. current_module:=hp;
  461. end
  462. else
  463. begin
  464. if current_module.in_compile then
  465. begin
  466. current_module.in_second_compile:=true;
  467. Message1(parser_d_compiling_second_time,current_module.modulename^);
  468. end;
  469. current_scanner^.tempcloseinputfile;
  470. name:=current_module.mainsource^;
  471. if assigned(scanner) then
  472. scanner^.invalid:=true;
  473. compile(name,compile_system);
  474. current_module.in_second_compile:=false;
  475. if (not current_scanner^.invalid) then
  476. current_scanner^.tempopeninputfile;
  477. end;
  478. end
  479. else
  480. begin
  481. { only reassemble ? }
  482. if (current_module.do_assemble) then
  483. OnlyAsm;
  484. end;
  485. if assigned(current_module.ppufile) then
  486. begin
  487. dispose(current_module.ppufile,done);
  488. current_module.ppufile:=nil;
  489. current_ppu:=nil;
  490. end;
  491. end;
  492. var
  493. dummy : tmodule;
  494. begin
  495. old_current_module:=current_module;
  496. old_current_ppu:=current_ppu;
  497. { Info }
  498. Message3(unit_u_load_unit,current_module.modulename^,ImplIntf[current_module.in_implementation],s);
  499. { unit not found }
  500. st:=nil;
  501. dummy:=nil;
  502. { search all loaded units }
  503. hp:=tmodule(loaded_units.first);
  504. while assigned(hp) do
  505. begin
  506. if hp.modulename^=s then
  507. begin
  508. { forced to reload ? }
  509. if hp.do_reload then
  510. begin
  511. hp.do_reload:=false;
  512. break;
  513. end;
  514. { the unit is already registered }
  515. { and this means that the unit }
  516. { is already compiled }
  517. { else there is a cyclic unit use }
  518. if assigned(hp.globalsymtable) then
  519. st:=punitsymtable(hp.globalsymtable)
  520. else
  521. begin
  522. { both units in interface ? }
  523. if (not current_module.in_implementation) and (not hp.in_implementation) then
  524. begin
  525. { check for a cycle }
  526. hp2:=current_module.loaded_from;
  527. while assigned(hp2) and (hp2<>hp) do
  528. begin
  529. if hp2.in_implementation then
  530. hp2:=nil
  531. else
  532. hp2:=hp2.loaded_from;
  533. end;
  534. if assigned(hp2) then
  535. Message2(unit_f_circular_unit_reference,current_module.modulename^,hp.modulename^);
  536. end;
  537. end;
  538. break;
  539. end
  540. else if copy(hp.modulename^,1,8)=s then
  541. dummy:=hp;
  542. { the next unit }
  543. hp:=tmodule(hp.next);
  544. end;
  545. if assigned(dummy) and not assigned(hp) then
  546. Message2(unit_w_unit_name_error,s,dummy.modulename^);
  547. { the unit is not in the symtable stack }
  548. if (not assigned(st)) then
  549. begin
  550. if assigned(hp) then
  551. begin
  552. { remove the old unit }
  553. loaded_units.remove(hp);
  554. scanner:=hp.scanner;
  555. hp.reset;
  556. hp.scanner:=scanner;
  557. { try to reopen ppu }
  558. hp.search_unit(s,false);
  559. { try to load the unit a second time first }
  560. current_module:=hp;
  561. current_module.in_second_load:=true;
  562. Message1(unit_u_second_load_unit,current_module.modulename^);
  563. second_time:=true;
  564. end
  565. else
  566. { generates a new unit info record }
  567. begin
  568. current_module:=tmodule.create(s,true);
  569. scanner:=nil;
  570. second_time:=false;
  571. end;
  572. current_ppu:=current_module.ppufile;
  573. { close old_current_ppu on system that are
  574. short on file handles like DOS PM }
  575. {$ifdef SHORT_ON_FILE_HANDLES}
  576. if assigned(old_current_ppu) then
  577. old_current_ppu^.tempclose;
  578. {$endif SHORT_ON_FILE_HANDLES}
  579. { now we can register the unit }
  580. current_module.loaded_from:=old_current_module;
  581. loaded_units.insert(current_module);
  582. { now realy load the ppu }
  583. loadppufile;
  584. { set compiled flag }
  585. current_module.compiled:=true;
  586. { load return pointer }
  587. hp:=current_module;
  588. { for a second_time recompile reload all dependent units,
  589. for a first time compile register the unit _once_ }
  590. if second_time then
  591. begin
  592. { now reload all dependent units }
  593. hp2:=tmodule(loaded_units.first);
  594. while assigned(hp2) do
  595. begin
  596. if hp2.do_reload then
  597. dummy:=loadunit(hp2.modulename^,false);
  598. hp2:=tmodule(hp2.next);
  599. end;
  600. end
  601. else
  602. usedunits.concat(tused_unit.create(current_module,true));
  603. end;
  604. { set the old module }
  605. {$ifdef SHORT_ON_FILE_HANDLES}
  606. if assigned(old_current_ppu) then
  607. old_current_ppu^.tempopen;
  608. {$endif SHORT_ON_FILE_HANDLES}
  609. current_ppu:=old_current_ppu;
  610. current_module:=old_current_module;
  611. { we are back }
  612. SetCompileModule(current_module);
  613. loadunit:=hp;
  614. end;
  615. procedure loaddefaultunits;
  616. var
  617. hp : tmodule;
  618. unitsym : punitsym;
  619. begin
  620. { are we compiling the system unit? }
  621. if (cs_compilesystem in aktmoduleswitches) then
  622. begin
  623. { create system defines }
  624. createconstdefs;
  625. { we don't need to reset anything, it's already done in parser.pas }
  626. exit;
  627. end;
  628. { insert the system unit, it is allways the first }
  629. hp:=loadunit('SYSTEM',true);
  630. systemunit:=hp.globalsymtable;
  631. { it's always the first unit }
  632. systemunit^.next:=nil;
  633. symtablestack:=systemunit;
  634. { add to the used units }
  635. current_module.used_units.concat(tused_unit.create(hp,true));
  636. unitsym:=new(punitsym,init('System',systemunit));
  637. inc(unitsym^.refs);
  638. refsymtable^.insert(unitsym);
  639. { read default constant definitions }
  640. make_ref:=false;
  641. readconstdefs;
  642. { if POWER is defined in the RTL then use it for starstar overloading }
  643. {$ifdef DONOTCHAINOPERATORS}
  644. getsym('POWER',false);
  645. {$endif DONOTCHAINOPERATORS}
  646. make_ref:=true;
  647. {$ifdef DONOTCHAINOPERATORS}
  648. { Code now in chainoperators PM }
  649. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[_STARSTAR]=nil) then
  650. overloaded_operators[_STARSTAR]:=pprocsym(srsym);
  651. {$endif DONOTCHAINOPERATORS}
  652. { Objpas unit? }
  653. if m_objpas in aktmodeswitches then
  654. begin
  655. hp:=loadunit('ObjPas',false);
  656. psymtable(hp.globalsymtable)^.next:=symtablestack;
  657. symtablestack:=hp.globalsymtable;
  658. { add to the used units }
  659. current_module.used_units.concat(tused_unit.create(hp,true));
  660. unitsym:=new(punitsym,init('ObjPas',hp.globalsymtable));
  661. inc(unitsym^.refs);
  662. refsymtable^.insert(unitsym);
  663. end;
  664. { Profile unit? Needed for go32v2 only }
  665. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  666. begin
  667. hp:=loadunit('Profile',false);
  668. psymtable(hp.globalsymtable)^.next:=symtablestack;
  669. symtablestack:=hp.globalsymtable;
  670. { add to the used units }
  671. current_module.used_units.concat(tused_unit.create(hp,true));
  672. unitsym:=new(punitsym,init('Profile',hp.globalsymtable));
  673. inc(unitsym^.refs);
  674. refsymtable^.insert(unitsym);
  675. end;
  676. { Units only required for main module }
  677. if not(current_module.is_unit) then
  678. begin
  679. { Heaptrc unit }
  680. if (cs_gdb_heaptrc in aktglobalswitches) then
  681. begin
  682. hp:=loadunit('HeapTrc',false);
  683. psymtable(hp.globalsymtable)^.next:=symtablestack;
  684. symtablestack:=hp.globalsymtable;
  685. { add to the used units }
  686. current_module.used_units.concat(tused_unit.create(hp,true));
  687. unitsym:=new(punitsym,init('HeapTrc',hp.globalsymtable));
  688. inc(unitsym^.refs);
  689. refsymtable^.insert(unitsym);
  690. end;
  691. { Lineinfo unit }
  692. if (cs_gdb_lineinfo in aktglobalswitches) then
  693. begin
  694. hp:=loadunit('LineInfo',false);
  695. psymtable(hp.globalsymtable)^.next:=symtablestack;
  696. symtablestack:=hp.globalsymtable;
  697. { add to the used units }
  698. current_module.used_units.concat(tused_unit.create(hp,true));
  699. unitsym:=new(punitsym,init('LineInfo',hp.globalsymtable));
  700. inc(unitsym^.refs);
  701. refsymtable^.insert(unitsym);
  702. end;
  703. end;
  704. { save default symtablestack }
  705. defaultsymtablestack:=symtablestack;
  706. end;
  707. procedure loadunits;
  708. var
  709. s,sorg : stringid;
  710. pu,
  711. hp : tused_unit;
  712. hp2 : tmodule;
  713. hp3 : psymtable;
  714. oldprocsym:Pprocsym;
  715. unitsym : punitsym;
  716. begin
  717. oldprocsym:=aktprocsym;
  718. consume(_USES);
  719. {$ifdef DEBUG}
  720. test_symtablestack;
  721. {$endif DEBUG}
  722. repeat
  723. s:=pattern;
  724. sorg:=orgpattern;
  725. consume(_ID);
  726. { Give a warning if objpas is loaded }
  727. if s='OBJPAS' then
  728. Message(parser_w_no_objpas_use_mode);
  729. { check if the unit is already used }
  730. pu:=tused_unit(current_module.used_units.first);
  731. while assigned(pu) do
  732. begin
  733. if (pu.name^=s) then
  734. break;
  735. pu:=tused_unit(pu.next);
  736. end;
  737. { avoid uses of itself }
  738. if not assigned(pu) and (s<>current_module.modulename^) then
  739. begin
  740. { load the unit }
  741. hp2:=loadunit(s,false);
  742. { the current module uses the unit hp2 }
  743. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  744. tused_unit(current_module.used_units.last).in_uses:=true;
  745. if current_module.compiled then
  746. exit;
  747. unitsym:=new(punitsym,init(sorg,hp2.globalsymtable));
  748. { never claim about unused unit if
  749. there is init or finalize code PM }
  750. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  751. inc(unitsym^.refs);
  752. refsymtable^.insert(unitsym);
  753. end
  754. else
  755. Message1(sym_e_duplicate_id,s);
  756. if token=_COMMA then
  757. begin
  758. pattern:='';
  759. consume(_COMMA);
  760. end
  761. else
  762. break;
  763. until false;
  764. consume(_SEMICOLON);
  765. { set the symtable to systemunit so it gets reorderd correctly }
  766. symtablestack:=defaultsymtablestack;
  767. { now insert the units in the symtablestack }
  768. hp:=tused_unit(current_module.used_units.first);
  769. while assigned(hp) do
  770. begin
  771. {$IfDef GDB}
  772. if (cs_debuginfo in aktmoduleswitches) and
  773. (cs_gdb_dbx in aktglobalswitches) and
  774. not hp.is_stab_written then
  775. begin
  776. punitsymtable(hp.u.globalsymtable)^.concattypestabto(debuglist);
  777. hp.is_stab_written:=true;
  778. hp.unitid:=psymtable(hp.u.globalsymtable)^.unitid;
  779. end;
  780. {$EndIf GDB}
  781. if hp.in_uses then
  782. begin
  783. hp3:=symtablestack;
  784. while assigned(hp3) do
  785. begin
  786. { insert units only once ! }
  787. if hp.u.globalsymtable=hp3 then
  788. break;
  789. hp3:=hp3^.next;
  790. { unit isn't inserted }
  791. if hp3=nil then
  792. begin
  793. psymtable(hp.u.globalsymtable)^.next:=symtablestack;
  794. symtablestack:=psymtable(hp.u.globalsymtable);
  795. {$ifdef CHAINPROCSYMS}
  796. symtablestack^.chainprocsyms;
  797. {$endif CHAINPROCSYMS}
  798. {$ifdef DEBUG}
  799. test_symtablestack;
  800. {$endif DEBUG}
  801. end;
  802. end;
  803. end;
  804. hp:=tused_unit(hp.next);
  805. end;
  806. aktprocsym:=oldprocsym;
  807. end;
  808. procedure write_gdb_info;
  809. {$IfDef GDB}
  810. var
  811. hp : tused_unit;
  812. begin
  813. if not (cs_debuginfo in aktmoduleswitches) then
  814. exit;
  815. if (cs_gdb_dbx in aktglobalswitches) then
  816. begin
  817. debugList.concat(Tai_asm_comment.Create(strpnew('EINCL of global '+
  818. punitsymtable(current_module.globalsymtable)^.name^+' has index '+
  819. tostr(punitsymtable(current_module.globalsymtable)^.unitid))));
  820. debugList.concat(Tai_stabs.Create(strpnew('"'+
  821. punitsymtable(current_module.globalsymtable)^.name^+'",'+
  822. tostr(N_EINCL)+',0,0,0')));
  823. punitsymtable(current_module.globalsymtable)^.dbx_count_ok:={true}false;
  824. dbx_counter:=punitsymtable(current_module.globalsymtable)^.prev_dbx_counter;
  825. do_count_dbx:=false;
  826. end;
  827. { now insert the units in the symtablestack }
  828. hp:=tused_unit(current_module.used_units.first);
  829. while assigned(hp) do
  830. begin
  831. if (cs_debuginfo in aktmoduleswitches) and
  832. not hp.is_stab_written then
  833. begin
  834. punitsymtable(hp.u.globalsymtable)^.concattypestabto(debuglist);
  835. hp.is_stab_written:=true;
  836. hp.unitid:=psymtable(hp.u.globalsymtable)^.unitid;
  837. end;
  838. hp:=tused_unit(hp.next);
  839. end;
  840. if current_module.in_implementation and
  841. assigned(current_module.localsymtable) then
  842. begin
  843. { all types }
  844. punitsymtable(current_module.localsymtable)^.concattypestabto(debuglist);
  845. { and all local symbols}
  846. punitsymtable(current_module.localsymtable)^.concatstabto(debuglist);
  847. end
  848. else if assigned(current_module.globalsymtable) then
  849. begin
  850. { all types }
  851. punitsymtable(current_module.globalsymtable)^.concattypestabto(debuglist);
  852. { and all local symbols}
  853. punitsymtable(current_module.globalsymtable)^.concatstabto(debuglist);
  854. end;
  855. end;
  856. {$Else GDB}
  857. begin
  858. end;
  859. {$EndIf GDB}
  860. procedure parse_implementation_uses(symt:Psymtable);
  861. begin
  862. if token=_USES then
  863. begin
  864. symt^.symtabletype:=unitsymtable;
  865. loadunits;
  866. symt^.symtabletype:=globalsymtable;
  867. {$ifdef DEBUG}
  868. test_symtablestack;
  869. {$endif DEBUG}
  870. end;
  871. end;
  872. procedure setupglobalswitches;
  873. begin
  874. { can't have local browser when no global browser }
  875. if (cs_local_browser in aktmoduleswitches) and
  876. not(cs_browser in aktmoduleswitches) then
  877. exclude(aktmoduleswitches,cs_local_browser);
  878. { define a symbol in delphi,objfpc,tp,gpc mode }
  879. if (m_delphi in aktmodeswitches) then
  880. current_scanner^.def_macro('FPC_DELPHI')
  881. else
  882. if (m_tp in aktmodeswitches) then
  883. current_scanner^.def_macro('FPC_TP')
  884. else
  885. if (m_objfpc in aktmodeswitches) then
  886. current_scanner^.def_macro('FPC_OBJFPC')
  887. else
  888. if (m_gpc in aktmodeswitches) then
  889. current_scanner^.def_macro('FPC_GPC');
  890. end;
  891. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:psymtable);
  892. var
  893. stt : psymtable;
  894. begin
  895. {Generate a procsym for main}
  896. make_ref:=false;
  897. aktprocsym:=new(Pprocsym,init('$'+name));
  898. { main are allways used }
  899. inc(aktprocsym^.refs);
  900. {Try to insert in in static symtable ! }
  901. stt:=symtablestack;
  902. symtablestack:=st;
  903. aktprocsym^.definition:=new(Pprocdef,init);
  904. symtablestack:=stt;
  905. aktprocsym^.definition^.proctypeoption:=options;
  906. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  907. aktprocsym^.definition^.forwarddef:=false;
  908. make_ref:=true;
  909. { The localst is a local symtable. Change it into the static
  910. symtable }
  911. dispose(aktprocsym^.definition^.localst,done);
  912. aktprocsym^.definition^.localst:=st;
  913. { and insert the procsym in symtable }
  914. st^.insert(aktprocsym);
  915. { set some informations about the main program }
  916. with procinfo^ do
  917. begin
  918. returntype.setdef(voiddef);
  919. _class:=nil;
  920. para_offset:=8;
  921. framepointer:=frame_pointer;
  922. flags:=0;
  923. end;
  924. end;
  925. procedure proc_unit;
  926. function is_assembler_generated:boolean;
  927. begin
  928. is_assembler_generated:=(Errorcount=0) and
  929. not(
  930. codeSegment.empty and
  931. dataSegment.empty and
  932. bssSegment.empty and
  933. ((importssection=nil) or importsSection.empty) and
  934. ((resourcesection=nil) or resourceSection.empty) and
  935. ((resourcestringlist=nil) or resourcestringList.empty)
  936. );
  937. end;
  938. var
  939. main_file: tinputfile;
  940. st : psymtable;
  941. unitst : punitsymtable;
  942. {$ifdef GDB}
  943. pu : tused_unit;
  944. {$endif GDB}
  945. store_crc,store_interface_crc : longint;
  946. s2 : ^string; {Saves stack space}
  947. force_init_final : boolean;
  948. begin
  949. consume(_UNIT);
  950. if token=_ID then
  951. begin
  952. { create filenames and unit name }
  953. main_file := current_scanner^.inputfile;
  954. while assigned(main_file.next) do
  955. main_file := main_file.next;
  956. current_module.SetFileName(main_file.path^+main_file.name^,true);
  957. stringdispose(current_module.modulename);
  958. stringdispose(current_module.realmodulename);
  959. current_module.modulename:=stringdup(pattern);
  960. current_module.realmodulename:=stringdup(orgpattern);
  961. { check for system unit }
  962. new(s2);
  963. s2^:=upper(SplitName(main_file.name^));
  964. if (cs_compilesystem in aktmoduleswitches) then
  965. begin
  966. if ((length(current_module.modulename^)>8) or
  967. (current_module.modulename^<>'SYSTEM') or
  968. (current_module.modulename^<>s2^)) then
  969. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  970. end
  971. else
  972. begin
  973. if (cs_check_unit_name in aktglobalswitches) and
  974. not((current_module.modulename^=s2^) or
  975. ((length(current_module.modulename^)>8) and
  976. (copy(current_module.modulename^,1,8)=s2^))) then
  977. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  978. if (current_module.modulename^='SYSTEM') then
  979. Message(unit_w_switch_us_missed);
  980. end;
  981. dispose(s2);
  982. end;
  983. consume(_ID);
  984. consume(_SEMICOLON);
  985. consume(_INTERFACE);
  986. { global switches are read, so further changes aren't allowed }
  987. current_module.in_global:=false;
  988. { handle the global switches }
  989. setupglobalswitches;
  990. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  991. { update status }
  992. status.currentmodule:=current_module.realmodulename^;
  993. { maybe turn off m_objpas if we are compiling objpas }
  994. if (current_module.modulename^='OBJPAS') then
  995. aktmodeswitches:=aktmodeswitches-[m_objpas];
  996. { this should be placed after uses !!}
  997. {$ifndef UseNiceNames}
  998. procprefix:='_'+current_module.modulename^+'$$';
  999. {$else UseNiceNames}
  1000. procprefix:='_'+tostr(length(current_module.modulename^))+lowercase(current_module.modulename^)+'_';
  1001. {$endif UseNiceNames}
  1002. parse_only:=true;
  1003. { generate now the global symboltable }
  1004. st:=new(punitsymtable,init(globalsymtable,current_module.modulename^));
  1005. refsymtable:=st;
  1006. unitst:=punitsymtable(st);
  1007. { define first as local to overcome dependency conflicts }
  1008. current_module.localsymtable:=st;
  1009. { the unit name must be usable as a unit specifier }
  1010. { inside the unit itself (PM) }
  1011. { this also forbids to have another symbol }
  1012. { with the same name as the unit }
  1013. refsymtable^.insert(new(punitsym,init(current_module.realmodulename^,unitst)));
  1014. { a unit compiled at command line must be inside the loaded_unit list }
  1015. if (compile_level=1) then
  1016. loaded_units.insert(current_module);
  1017. { load default units, like the system unit }
  1018. loaddefaultunits;
  1019. { reset }
  1020. make_ref:=true;
  1021. lexlevel:=0;
  1022. { insert qualifier for the system unit (allows system.writeln) }
  1023. if not(cs_compilesystem in aktmoduleswitches) then
  1024. begin
  1025. if token=_USES then
  1026. begin
  1027. unitst^.symtabletype:=unitsymtable;
  1028. loadunits;
  1029. { has it been compiled at a higher level ?}
  1030. if current_module.compiled then
  1031. begin
  1032. { this unit symtable is obsolete }
  1033. { dispose(unitst,done);
  1034. disposed as localsymtable !! }
  1035. RestoreUnitSyms;
  1036. exit;
  1037. end;
  1038. unitst^.symtabletype:=globalsymtable;
  1039. end;
  1040. { ... but insert the symbol table later }
  1041. st^.next:=symtablestack;
  1042. symtablestack:=st;
  1043. end
  1044. else
  1045. { while compiling a system unit, some types are directly inserted }
  1046. begin
  1047. st^.next:=symtablestack;
  1048. symtablestack:=st;
  1049. insert_intern_types(st);
  1050. end;
  1051. { now we know the place to insert the constants }
  1052. constsymtable:=symtablestack;
  1053. { move the global symtab from the temporary local to global }
  1054. current_module.globalsymtable:=current_module.localsymtable;
  1055. current_module.localsymtable:=nil;
  1056. reset_global_defs;
  1057. { number all units, so we know if a unit is used by this unit or
  1058. needs to be added implicitly }
  1059. numberunits;
  1060. { ... parse the declarations }
  1061. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1062. read_interface_declarations;
  1063. { leave when we got an error }
  1064. if (Errorcount>0) and not status.skip_error then
  1065. begin
  1066. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1067. status.skip_error:=true;
  1068. exit;
  1069. end;
  1070. {else in inteface its somatimes necessary even if unused
  1071. st^.allunitsused; }
  1072. {$ifdef New_GDB}
  1073. write_gdb_info;
  1074. {$endIf Def New_GDB}
  1075. if not(cs_compilesystem in aktmoduleswitches) then
  1076. if (Errorcount=0) then
  1077. writeunitas(current_module.ppufilename^,punitsymtable(symtablestack),true);
  1078. { Parse the implementation section }
  1079. consume(_IMPLEMENTATION);
  1080. current_module.in_implementation:=true;
  1081. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  1082. parse_only:=false;
  1083. { generates static symbol table }
  1084. st:=new(punitsymtable,init(staticsymtable,current_module.modulename^));
  1085. current_module.localsymtable:=st;
  1086. { remove the globalsymtable from the symtable stack }
  1087. { to reinsert it after loading the implementation units }
  1088. symtablestack:=unitst^.next;
  1089. { we don't want implementation units symbols in unitsymtable !! PM }
  1090. refsymtable:=st;
  1091. { Read the implementation units }
  1092. parse_implementation_uses(unitst);
  1093. if current_module.compiled then
  1094. begin
  1095. RestoreUnitSyms;
  1096. exit;
  1097. end;
  1098. { reset ranges/stabs in exported definitions }
  1099. reset_global_defs;
  1100. { All units are read, now give them a number }
  1101. numberunits;
  1102. { now we can change refsymtable }
  1103. refsymtable:=st;
  1104. { but reinsert the global symtable as lasts }
  1105. unitst^.next:=symtablestack;
  1106. symtablestack:=unitst;
  1107. {$ifndef DONOTCHAINOPERATORS}
  1108. pstoredsymtable(symtablestack)^.chainoperators;
  1109. {$endif DONOTCHAINOPERATORS}
  1110. {$ifdef DEBUG}
  1111. test_symtablestack;
  1112. {$endif DEBUG}
  1113. constsymtable:=symtablestack;
  1114. {$ifdef Splitheap}
  1115. if testsplit then
  1116. begin
  1117. Split_Heap;
  1118. allow_special:=true;
  1119. Switch_to_temp_heap;
  1120. end;
  1121. { it will report all crossings }
  1122. allow_special:=false;
  1123. {$endif Splitheap}
  1124. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  1125. { Compile the unit }
  1126. codegen_newprocedure;
  1127. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  1128. aktprocsym^.definition^.aliasnames.insert('INIT$$'+current_module.modulename^);
  1129. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_init');
  1130. compile_proc_body(true,false);
  1131. codegen_doneprocedure;
  1132. { avoid self recursive destructor call !! PM }
  1133. aktprocsym^.definition^.localst:=nil;
  1134. { if the unit contains ansi/widestrings, initialization and
  1135. finalization code must be forced }
  1136. force_init_final:=needs_init_final(current_module.globalsymtable)
  1137. or needs_init_final(current_module.localsymtable);
  1138. { should we force unit initialization? }
  1139. { this is a hack, but how can it be done better ? }
  1140. if force_init_final and ((current_module.flags and uf_init)=0) then
  1141. begin
  1142. current_module.flags:=current_module.flags or uf_init;
  1143. { now we can insert a cut }
  1144. if (cs_create_smart in aktmoduleswitches) then
  1145. codeSegment.concat(Tai_cut.Create);
  1146. genimplicitunitinit(codesegment);
  1147. end;
  1148. { finalize? }
  1149. if token=_FINALIZATION then
  1150. begin
  1151. { set module options }
  1152. current_module.flags:=current_module.flags or uf_finalize;
  1153. { Compile the finalize }
  1154. codegen_newprocedure;
  1155. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1156. aktprocsym^.definition^.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1157. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_finalize');
  1158. compile_proc_body(true,false);
  1159. codegen_doneprocedure;
  1160. end
  1161. else if force_init_final then
  1162. begin
  1163. current_module.flags:=current_module.flags or uf_finalize;
  1164. { now we can insert a cut }
  1165. if (cs_create_smart in aktmoduleswitches) then
  1166. codeSegment.concat(Tai_cut.Create);
  1167. genimplicitunitfinal(codesegment);
  1168. end;
  1169. { the last char should always be a point }
  1170. consume(_POINT);
  1171. If ResourceStrings.ResStrCount>0 then
  1172. begin
  1173. ResourceStrings.CreateResourceStringList;
  1174. current_module.flags:=current_module.flags or uf_has_resources;
  1175. { only write if no errors found }
  1176. if (Errorcount=0) then
  1177. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1178. end;
  1179. { avoid self recursive destructor call !! PM }
  1180. aktprocsym^.definition^.localst:=nil;
  1181. { absence does not matter here !! }
  1182. aktprocsym^.definition^.forwarddef:=false;
  1183. { test static symtable }
  1184. if (Errorcount=0) then
  1185. begin
  1186. pstoredsymtable(st)^.allsymbolsused;
  1187. pstoredsymtable(st)^.allunitsused;
  1188. pstoredsymtable(st)^.allprivatesused;
  1189. end;
  1190. { size of the static data }
  1191. datasize:=st^.datasize;
  1192. {$ifdef GDB}
  1193. { add all used definitions even for implementation}
  1194. if (cs_debuginfo in aktmoduleswitches) then
  1195. begin
  1196. {$IfnDef New_GDB}
  1197. if assigned(current_module.globalsymtable) then
  1198. begin
  1199. { all types }
  1200. punitsymtable(current_module.globalsymtable)^.concattypestabto(debuglist);
  1201. { and all local symbols}
  1202. punitsymtable(current_module.globalsymtable)^.concatstabto(debuglist);
  1203. end;
  1204. { all local types }
  1205. punitsymtable(st)^.concattypestabto(debuglist);
  1206. { and all local symbols}
  1207. st^.concatstabto(debuglist);
  1208. {$else New_GDB}
  1209. write_gdb_info;
  1210. {$endIf Def New_GDB}
  1211. end;
  1212. {$endif GDB}
  1213. reset_global_defs;
  1214. { tests, if all (interface) forwards are resolved }
  1215. if (Errorcount=0) then
  1216. begin
  1217. pstoredsymtable(symtablestack)^.check_forwards;
  1218. pstoredsymtable(symtablestack)^.allprivatesused;
  1219. end;
  1220. { now we have a correct unit, change the symtable type }
  1221. current_module.in_implementation:=false;
  1222. symtablestack^.symtabletype:=unitsymtable;
  1223. {$ifdef GDB}
  1224. punitsymtable(symtablestack)^.is_stab_written:=false;
  1225. {$endif GDB}
  1226. { leave when we got an error }
  1227. if (Errorcount>0) and not status.skip_error then
  1228. begin
  1229. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1230. status.skip_error:=true;
  1231. closecurrentppu;
  1232. exit;
  1233. end;
  1234. { generate imports }
  1235. if current_module.uses_imports then
  1236. importlib.generatelib;
  1237. { insert own objectfile, or say that it's in a library
  1238. (no check for an .o when loading) }
  1239. if is_assembler_generated then
  1240. insertobjectfile
  1241. else
  1242. current_module.flags:=current_module.flags or uf_no_link;
  1243. if cs_local_browser in aktmoduleswitches then
  1244. current_module.localsymtable:=refsymtable;
  1245. { Write out the ppufile }
  1246. store_interface_crc:=current_module.interface_crc;
  1247. store_crc:=current_module.crc;
  1248. if (Errorcount=0) then
  1249. writeunitas(current_module.ppufilename^,punitsymtable(symtablestack),false);
  1250. if not(cs_compilesystem in aktmoduleswitches) then
  1251. if store_interface_crc<>current_module.interface_crc then
  1252. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1253. tostr(store_crc)+'<>'+tostr(current_module.interface_crc));
  1254. {$ifdef EXTDEBUG}
  1255. if not(cs_compilesystem in aktmoduleswitches) then
  1256. if (store_crc<>current_module.crc) and simplify_ppu then
  1257. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  1258. tostr(store_crc)+'<>'+tostr(current_module.interface_crc));
  1259. {$endif EXTDEBUG}
  1260. { must be done only after local symtable ref stores !! }
  1261. closecurrentppu;
  1262. {$ifdef GDB}
  1263. pu:=tused_unit(usedunits.first);
  1264. while assigned(pu) do
  1265. begin
  1266. if assigned(pu.u.globalsymtable) then
  1267. punitsymtable(pu.u.globalsymtable)^.is_stab_written:=false;
  1268. pu:=tused_unit(pu.next);
  1269. end;
  1270. {$endif GDB}
  1271. { remove static symtable (=refsymtable) here to save some mem }
  1272. if not (cs_local_browser in aktmoduleswitches) then
  1273. begin
  1274. dispose(st,done);
  1275. current_module.localsymtable:=nil;
  1276. end;
  1277. RestoreUnitSyms;
  1278. if is_assembler_generated then
  1279. begin
  1280. { finish asmlist by adding segment starts }
  1281. insertsegment;
  1282. { assemble }
  1283. create_objectfile;
  1284. end;
  1285. { leave when we got an error }
  1286. if (Errorcount>0) and not status.skip_error then
  1287. begin
  1288. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1289. status.skip_error:=true;
  1290. exit;
  1291. end;
  1292. end;
  1293. procedure proc_program(islibrary : boolean);
  1294. var
  1295. main_file: tinputfile;
  1296. st : psymtable;
  1297. hp : tmodule;
  1298. begin
  1299. DLLsource:=islibrary;
  1300. IsExe:=true;
  1301. parse_only:=false;
  1302. { relocation works only without stabs under win32 !! PM }
  1303. { internal assembler uses rva for stabs info
  1304. so it should work with relocated DLLs }
  1305. if RelocSection and
  1306. (target_info.target=target_i386_win32) and
  1307. (target_info.assem<>as_i386_pecoff) then
  1308. begin
  1309. aktglobalswitches:=aktglobalswitches+[cs_link_strip];
  1310. { Warning stabs info does not work with reloc section !! }
  1311. if cs_debuginfo in aktmoduleswitches then
  1312. begin
  1313. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1314. Message(parser_w_parser_win32_debug_needs_WN);
  1315. aktmoduleswitches:=aktmoduleswitches-[cs_debuginfo];
  1316. end;
  1317. end;
  1318. { get correct output names }
  1319. main_file := current_scanner^.inputfile;
  1320. while assigned(main_file.next) do
  1321. main_file := main_file.next;
  1322. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1323. if islibrary then
  1324. begin
  1325. consume(_LIBRARY);
  1326. stringdispose(current_module.modulename);
  1327. current_module.modulename:=stringdup(pattern);
  1328. current_module.islibrary:=true;
  1329. exportlib.preparelib(pattern);
  1330. consume(_ID);
  1331. consume(_SEMICOLON);
  1332. end
  1333. else
  1334. { is there an program head ? }
  1335. if token=_PROGRAM then
  1336. begin
  1337. consume(_PROGRAM);
  1338. stringdispose(current_module.modulename);
  1339. stringdispose(current_module.realmodulename);
  1340. current_module.modulename:=stringdup(pattern);
  1341. current_module.realmodulename:=stringdup(orgpattern);
  1342. if (target_info.target=target_i386_WIN32) then
  1343. exportlib.preparelib(pattern);
  1344. consume(_ID);
  1345. if token=_LKLAMMER then
  1346. begin
  1347. consume(_LKLAMMER);
  1348. idlist;
  1349. consume(_RKLAMMER);
  1350. end;
  1351. consume(_SEMICOLON);
  1352. end
  1353. else if (target_info.target=target_i386_WIN32) then
  1354. exportlib.preparelib(current_module.modulename^);
  1355. { global switches are read, so further changes aren't allowed }
  1356. current_module.in_global:=false;
  1357. { setup things using the global switches }
  1358. setupglobalswitches;
  1359. { set implementation flag }
  1360. current_module.in_implementation:=true;
  1361. { insert after the unit symbol tables the static symbol table }
  1362. { of the program }
  1363. st:=new(punitsymtable,init(staticsymtable,current_module.modulename^));
  1364. current_module.localsymtable:=st;
  1365. refsymtable:=st;
  1366. { load standard units (system,objpas,profile unit) }
  1367. loaddefaultunits;
  1368. { reset }
  1369. lexlevel:=0;
  1370. {Load the units used by the program we compile.}
  1371. if token=_USES then
  1372. loadunits;
  1373. { necessary for browser }
  1374. loaded_units.insert(current_module);
  1375. {$ifndef DONOTCHAINOPERATORS}
  1376. pstoredsymtable(symtablestack)^.chainoperators;
  1377. {$endif DONOTCHAINOPERATORS}
  1378. { reset ranges/stabs in exported definitions }
  1379. reset_global_defs;
  1380. { All units are read, now give them a number }
  1381. numberunits;
  1382. {Insert the name of the main program into the symbol table.}
  1383. if current_module.realmodulename^<>'' then
  1384. st^.insert(new(punitsym,init(current_module.realmodulename^,punitsymtable(st))));
  1385. { ...is also constsymtable, this is the symtable where }
  1386. { the elements of enumeration types are inserted }
  1387. constsymtable:=st;
  1388. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1389. { reset }
  1390. procprefix:='';
  1391. {The program intialization needs an alias, so it can be called
  1392. from the bootstrap code.}
  1393. codegen_newprocedure;
  1394. gen_main_procsym('main',potype_proginit,st);
  1395. aktprocsym^.definition^.aliasnames.insert('program_init');
  1396. aktprocsym^.definition^.aliasnames.insert('PASCALMAIN');
  1397. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+'main');
  1398. {$ifdef m68k}
  1399. if target_info.target=target_m68k_PalmOS then
  1400. aktprocsym^.definition^.aliasnames.insert('PilotMain');
  1401. {$endif m68k}
  1402. compile_proc_body(true,false);
  1403. { avoid self recursive destructor call !! PM }
  1404. aktprocsym^.definition^.localst:=nil;
  1405. { consider these symbols as global ones }
  1406. { for browser }
  1407. current_module.globalsymtable:=current_module.localsymtable;
  1408. current_module.localsymtable:=nil;
  1409. If ResourceStrings.ResStrCount>0 then
  1410. begin
  1411. ResourceStrings.CreateResourceStringList;
  1412. { only write if no errors found }
  1413. if (Errorcount=0) then
  1414. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1415. end;
  1416. codegen_doneprocedure;
  1417. { finalize? }
  1418. if token=_FINALIZATION then
  1419. begin
  1420. { set module options }
  1421. current_module.flags:=current_module.flags or uf_finalize;
  1422. { Compile the finalize }
  1423. codegen_newprocedure;
  1424. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1425. aktprocsym^.definition^.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1426. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_finalize');
  1427. compile_proc_body(true,false);
  1428. codegen_doneprocedure;
  1429. end;
  1430. { consume the last point }
  1431. consume(_POINT);
  1432. {$ifdef New_GDB}
  1433. write_gdb_info;
  1434. {$endIf Def New_GDB}
  1435. { leave when we got an error }
  1436. if (Errorcount>0) and not status.skip_error then
  1437. begin
  1438. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1439. status.skip_error:=true;
  1440. exit;
  1441. end;
  1442. { test static symtable }
  1443. if (Errorcount=0) then
  1444. begin
  1445. pstoredsymtable(st)^.allsymbolsused;
  1446. pstoredsymtable(st)^.allunitsused;
  1447. pstoredsymtable(st)^.allprivatesused;
  1448. end;
  1449. { generate imports }
  1450. if current_module.uses_imports then
  1451. importlib.generatelib;
  1452. if islibrary or
  1453. (target_info.target=target_i386_WIN32) then
  1454. exportlib.generatelib;
  1455. { insert heap }
  1456. insertResourceTablesTable;
  1457. insertinitfinaltable;
  1458. insertheap;
  1459. inserttargetspecific;
  1460. datasize:=symtablestack^.datasize;
  1461. { finish asmlist by adding segment starts }
  1462. insertsegment;
  1463. { insert own objectfile }
  1464. insertobjectfile;
  1465. { assemble and link }
  1466. create_objectfile;
  1467. { leave when we got an error }
  1468. if (Errorcount>0) and not status.skip_error then
  1469. begin
  1470. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1471. status.skip_error:=true;
  1472. exit;
  1473. end;
  1474. { create the executable when we are at level 1 }
  1475. if (compile_level=1) then
  1476. begin
  1477. { insert all .o files from all loaded units }
  1478. hp:=tmodule(loaded_units.first);
  1479. while assigned(hp) do
  1480. begin
  1481. linker.AddModuleFiles(hp);
  1482. hp:=tmodule(hp.next);
  1483. end;
  1484. { write .def file }
  1485. if (cs_link_deffile in aktglobalswitches) then
  1486. deffile.writefile;
  1487. { finally we can create a executable }
  1488. if (not current_module.is_unit) then
  1489. begin
  1490. if DLLSource then
  1491. linker.MakeSharedLibrary
  1492. else
  1493. linker.MakeExecutable;
  1494. end;
  1495. end;
  1496. end;
  1497. end.
  1498. {
  1499. $Log$
  1500. Revision 1.24 2001-03-06 18:28:02 peter
  1501. * patch from Pavel with a new and much faster DLL Scanner for
  1502. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1503. Revision 1.23 2001/02/24 10:44:56 peter
  1504. * generate .rst from ppufilename instead of modulename
  1505. Revision 1.22 2001/02/21 19:37:19 peter
  1506. * moved deref to be done after loading of implementation units. prederef
  1507. is still done directly after loading of symbols and definitions.
  1508. Revision 1.21 2001/01/14 22:13:52 peter
  1509. * fixed crash with program name as a important unit name
  1510. Revision 1.20 2000/12/25 00:07:27 peter
  1511. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1512. tlinkedlist objects)
  1513. Revision 1.19 2000/11/29 00:30:36 florian
  1514. * unused units removed from uses clause
  1515. * some changes for widestrings
  1516. Revision 1.18 2000/11/01 23:04:37 peter
  1517. * tprocdef.fullprocname added for better casesensitve writing of
  1518. procedures
  1519. Revision 1.17 2000/10/31 22:02:50 peter
  1520. * symtable splitted, no real code changes
  1521. Revision 1.16 2000/10/21 14:36:26 peter
  1522. * merged pierres fixes
  1523. Revision 1.15 2000/10/15 09:08:58 peter
  1524. * use System for the systemunit instead of target dependent
  1525. Revision 1.14 2000/10/15 07:47:51 peter
  1526. * unit names and procedure names are stored mixed case
  1527. Revision 1.13 2000/10/04 14:51:08 pierre
  1528. * IsExe restored
  1529. Revision 1.12 2000/09/30 16:07:40 peter
  1530. * filepos when unit not found (merged)
  1531. Revision 1.11 2000/09/24 21:33:47 peter
  1532. * message updates merges
  1533. Revision 1.10 2000/09/24 15:06:22 peter
  1534. * use defines.inc
  1535. Revision 1.9 2000/08/31 07:53:02 michael
  1536. + Applied patch from Peter
  1537. Revision 1.8 2000/08/29 19:00:01 peter
  1538. * _init and _finalize procsyms also need a $ prefix
  1539. Revision 1.7 2000/08/27 20:19:39 peter
  1540. * store strings with case in ppu, when an internal symbol is created
  1541. a '$' is prefixed so it's not automatic uppercased
  1542. Revision 1.6 2000/08/27 16:11:52 peter
  1543. * moved some util functions from globals,cobjects to cutils
  1544. * splitted files into finput,fmodule
  1545. Revision 1.5 2000/08/25 08:48:22 jonas
  1546. * fixed bug with include files at the very beginning of .pp/.pas files
  1547. (wrong name used for generating exe/checking unit name) (merged from
  1548. fixes branch)
  1549. Revision 1.4 2000/08/21 11:27:44 pierre
  1550. * fix the stabs problems
  1551. Revision 1.3 2000/07/13 12:08:26 michael
  1552. + patched to 1.1.0 with former 1.09patch from peter
  1553. Revision 1.2 2000/07/13 11:32:45 michael
  1554. + removed logs
  1555. }