pmodules.pas 63 KB

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