pmodules.pas 61 KB

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