pmodules.pas 60 KB

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