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. { 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. begin
  579. { are we compiling the system unit? }
  580. if (cs_compilesystem in aktmoduleswitches) then
  581. begin
  582. { create system defines }
  583. createconstdefs;
  584. { we don't need to reset anything, it's already done in parser.pas }
  585. exit;
  586. end;
  587. { insert the system unit, it is allways the first }
  588. hp:=loadunit(upper(target_info.system_unit),true);
  589. systemunit:=hp^.globalsymtable;
  590. { it's always the first unit }
  591. systemunit^.next:=nil;
  592. symtablestack:=systemunit;
  593. { add to the used units }
  594. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  595. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  596. { read default constant definitions }
  597. make_ref:=false;
  598. readconstdefs;
  599. { if POWER is defined in the RTL then use it for starstar overloading }
  600. getsym('POWER',false);
  601. make_ref:=true;
  602. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[_STARSTAR]=nil) then
  603. overloaded_operators[_STARSTAR]:=pprocsym(srsym);
  604. { Objpas unit? }
  605. if m_objpas in aktmodeswitches then
  606. begin
  607. hp:=loadunit('OBJPAS',false);
  608. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  609. symtablestack:=hp^.globalsymtable;
  610. { add to the used units }
  611. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  612. refsymtable^.insert(new(punitsym,init('OBJPAS',hp^.globalsymtable)));
  613. end;
  614. { Profile unit? Needed for go32v2 only }
  615. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  616. begin
  617. hp:=loadunit('PROFILE',false);
  618. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  619. symtablestack:=hp^.globalsymtable;
  620. { add to the used units }
  621. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  622. refsymtable^.insert(new(punitsym,init('PROFILE',hp^.globalsymtable)));
  623. end;
  624. { Units only required for main module }
  625. if not(current_module^.is_unit) then
  626. begin
  627. { Heaptrc unit }
  628. if (cs_gdb_heaptrc in aktglobalswitches) then
  629. begin
  630. hp:=loadunit('HEAPTRC',false);
  631. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  632. symtablestack:=hp^.globalsymtable;
  633. { add to the used units }
  634. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  635. refsymtable^.insert(new(punitsym,init('HEAPTRC',hp^.globalsymtable)));
  636. end;
  637. { Lineinfo unit }
  638. if (cs_gdb_lineinfo in aktglobalswitches) then
  639. begin
  640. hp:=loadunit('LINEINFO',false);
  641. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  642. symtablestack:=hp^.globalsymtable;
  643. { add to the used units }
  644. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  645. refsymtable^.insert(new(punitsym,init('LINEINFO',hp^.globalsymtable)));
  646. end;
  647. end;
  648. { save default symtablestack }
  649. defaultsymtablestack:=symtablestack;
  650. end;
  651. procedure loadunits;
  652. var
  653. s : stringid;
  654. pu,
  655. hp : pused_unit;
  656. hp2 : pmodule;
  657. hp3 : psymtable;
  658. oldprocsym:Pprocsym;
  659. begin
  660. oldprocsym:=aktprocsym;
  661. consume(_USES);
  662. {$ifdef DEBUG}
  663. test_symtablestack;
  664. {$endif DEBUG}
  665. repeat
  666. s:=pattern;
  667. consume(_ID);
  668. { Give a warning if objpas is loaded }
  669. if s='OBJPAS' then
  670. Message(parser_w_no_objpas_use_mode);
  671. { check if the unit is already used }
  672. pu:=pused_unit(current_module^.used_units.first);
  673. while assigned(pu) do
  674. begin
  675. if (pu^.name^=s) then
  676. break;
  677. pu:=pused_unit(pu^.next);
  678. end;
  679. { avoid uses of itself }
  680. if not assigned(pu) and (s<>current_module^.modulename^) then
  681. begin
  682. { load the unit }
  683. hp2:=loadunit(s,false);
  684. { the current module uses the unit hp2 }
  685. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  686. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  687. if current_module^.compiled then
  688. exit;
  689. refsymtable^.insert(new(punitsym,init(s,hp2^.globalsymtable)));
  690. end
  691. else
  692. Message1(sym_e_duplicate_id,s);
  693. if token=_COMMA then
  694. begin
  695. pattern:='';
  696. consume(_COMMA);
  697. end
  698. else
  699. break;
  700. until false;
  701. consume(_SEMICOLON);
  702. { set the symtable to systemunit so it gets reorderd correctly }
  703. symtablestack:=defaultsymtablestack;
  704. { now insert the units in the symtablestack }
  705. hp:=pused_unit(current_module^.used_units.first);
  706. while assigned(hp) do
  707. begin
  708. {$IfDef GDB}
  709. if (cs_debuginfo in aktmoduleswitches) and
  710. (cs_gdb_dbx in aktglobalswitches) and
  711. not hp^.is_stab_written then
  712. begin
  713. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  714. hp^.is_stab_written:=true;
  715. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  716. end;
  717. {$EndIf GDB}
  718. if hp^.in_uses then
  719. begin
  720. hp3:=symtablestack;
  721. while assigned(hp3) do
  722. begin
  723. { insert units only once ! }
  724. if hp^.u^.globalsymtable=hp3 then
  725. break;
  726. hp3:=hp3^.next;
  727. { unit isn't inserted }
  728. if hp3=nil then
  729. begin
  730. psymtable(hp^.u^.globalsymtable)^.next:=symtablestack;
  731. symtablestack:=psymtable(hp^.u^.globalsymtable);
  732. {$ifdef CHAINPROCSYMS}
  733. symtablestack^.chainprocsyms;
  734. {$endif CHAINPROCSYMS}
  735. {$ifdef DEBUG}
  736. test_symtablestack;
  737. {$endif DEBUG}
  738. end;
  739. end;
  740. end;
  741. hp:=pused_unit(hp^.next);
  742. end;
  743. {$ifndef DONOTCHAINOPERATORS}
  744. symtablestack^.chainoperators;
  745. {$endif DONOTCHAINOPERATORS}
  746. aktprocsym:=oldprocsym;
  747. end;
  748. procedure write_gdb_info;
  749. {$IfDef GDB}
  750. var
  751. hp : pused_unit;
  752. begin
  753. if not (cs_debuginfo in aktmoduleswitches) then
  754. exit;
  755. if (cs_gdb_dbx in aktglobalswitches) then
  756. begin
  757. debuglist^.concat(new(pai_asm_comment,init(strpnew('EINCL of global '+
  758. punitsymtable(current_module^.globalsymtable)^.name^+' has index '+
  759. tostr(punitsymtable(current_module^.globalsymtable)^.unitid)))));
  760. debuglist^.concat(new(pai_stabs,init(strpnew('"'+
  761. punitsymtable(current_module^.globalsymtable)^.name^+'",'+
  762. tostr(N_EINCL)+',0,0,0'))));
  763. punitsymtable(current_module^.globalsymtable)^.dbx_count_ok:=true;
  764. dbx_counter:=punitsymtable(current_module^.globalsymtable)^.prev_dbx_counter;
  765. do_count_dbx:=false;
  766. end;
  767. { now insert the units in the symtablestack }
  768. hp:=pused_unit(current_module^.used_units.first);
  769. while assigned(hp) do
  770. begin
  771. if (cs_debuginfo in aktmoduleswitches) and
  772. not hp^.is_stab_written then
  773. begin
  774. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  775. hp^.is_stab_written:=true;
  776. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  777. end;
  778. hp:=pused_unit(hp^.next);
  779. end;
  780. if current_module^.in_implementation then
  781. begin
  782. if assigned(current_module^.localsymtable) then
  783. begin
  784. { all types }
  785. punitsymtable(current_module^.localsymtable)^.concattypestabto(debuglist);
  786. { and all local symbols}
  787. punitsymtable(current_module^.localsymtable)^.concatstabto(debuglist);
  788. end;
  789. end
  790. else
  791. begin
  792. if assigned(current_module^.globalsymtable) then
  793. begin
  794. { all types }
  795. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  796. { and all local symbols}
  797. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  798. end;
  799. end;
  800. end;
  801. {$Else GDB}
  802. begin
  803. end;
  804. {$EndIf GDB}
  805. procedure parse_implementation_uses(symt:Psymtable);
  806. begin
  807. if token=_USES then
  808. begin
  809. symt^.symtabletype:=unitsymtable;
  810. loadunits;
  811. symt^.symtabletype:=globalsymtable;
  812. {$ifdef DEBUG}
  813. test_symtablestack;
  814. {$endif DEBUG}
  815. end;
  816. end;
  817. procedure setupglobalswitches;
  818. procedure def_symbol(const s:string);
  819. var
  820. mac : pmacrosym;
  821. begin
  822. mac:=new(pmacrosym,init(s));
  823. mac^.defined:=true;
  824. Message1(parser_m_macro_defined,mac^.name);
  825. macros^.insert(mac);
  826. end;
  827. begin
  828. { can't have local browser when no global browser }
  829. if (cs_local_browser in aktmoduleswitches) and
  830. not(cs_browser in aktmoduleswitches) then
  831. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  832. { define a symbol in delphi,objfpc,tp,gpc mode }
  833. if (m_delphi in aktmodeswitches) then
  834. def_symbol('FPC_DELPHI')
  835. else
  836. if (m_tp in aktmodeswitches) then
  837. def_symbol('FPC_TP')
  838. else
  839. if (m_objfpc in aktmodeswitches) then
  840. def_symbol('FPC_OBJFPC')
  841. else
  842. if (m_gpc in aktmodeswitches) then
  843. def_symbol('FPC_GPC');
  844. { turn ansistrings on by default ? }
  845. if (m_default_ansistring in aktmodeswitches) then
  846. aktlocalswitches:=aktlocalswitches+[cs_ansistrings];
  847. end;
  848. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:psymtable);
  849. var
  850. stt : psymtable;
  851. begin
  852. {Generate a procsym for main}
  853. make_ref:=false;
  854. aktprocsym:=new(Pprocsym,init(name));
  855. { main are allways used }
  856. inc(aktprocsym^.refs);
  857. {Try to insert in in static symtable ! }
  858. stt:=symtablestack;
  859. symtablestack:=st;
  860. aktprocsym^.definition:=new(Pprocdef,init);
  861. symtablestack:=stt;
  862. aktprocsym^.definition^.proctypeoption:=options;
  863. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  864. aktprocsym^.definition^.forwarddef:=false;
  865. make_ref:=true;
  866. { The localst is a local symtable. Change it into the static
  867. symtable }
  868. dispose(aktprocsym^.definition^.localst,done);
  869. aktprocsym^.definition^.localst:=st;
  870. { and insert the procsym in symtable }
  871. st^.insert(aktprocsym);
  872. { set some informations about the main program }
  873. with procinfo^ do
  874. begin
  875. returntype.setdef(voiddef);
  876. _class:=nil;
  877. para_offset:=8;
  878. framepointer:=frame_pointer;
  879. flags:=0;
  880. end;
  881. end;
  882. procedure proc_unit;
  883. function is_assembler_generated:boolean;
  884. begin
  885. is_assembler_generated:=(Errorcount=0) and
  886. not(
  887. codesegment^.empty and
  888. datasegment^.empty and
  889. bsssegment^.empty and
  890. ((importssection=nil) or importssection^.empty) and
  891. ((resourcesection=nil) or resourcesection^.empty) and
  892. ((resourcestringlist=nil) or resourcestringlist^.empty)
  893. );
  894. end;
  895. var
  896. {$ifdef fixLeaksOnError}
  897. names : Pstringcontainer;
  898. {$else fixLeaksOnError}
  899. names : Tstringcontainer;
  900. {$endif fixLeaksOnError}
  901. st : psymtable;
  902. unitst : punitsymtable;
  903. {$ifdef GDB}
  904. pu : pused_unit;
  905. {$endif GDB}
  906. {$ifndef Dont_use_double_checksum}
  907. store_crc,store_interface_crc : longint;
  908. {$endif}
  909. s1,s2 : ^string; {Saves stack space}
  910. force_init_final : boolean;
  911. begin
  912. consume(_UNIT);
  913. if Compile_Level=1 then
  914. IsExe:=false;
  915. if token=_ID then
  916. begin
  917. { create filenames and unit name }
  918. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  919. stringdispose(current_module^.modulename);
  920. current_module^.modulename:=stringdup(upper(pattern));
  921. { check for system unit }
  922. new(s1);
  923. new(s2);
  924. s1^:=upper(target_info.system_unit);
  925. s2^:=upper(SplitName(current_scanner^.inputfile^.name^));
  926. if (cs_compilesystem in aktmoduleswitches) then
  927. begin
  928. if ((length(current_module^.modulename^)>8) or
  929. (current_module^.modulename^<>s1^) or
  930. (current_module^.modulename^<>s2^)) then
  931. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  932. end
  933. else
  934. begin
  935. if (cs_check_unit_name in aktglobalswitches) and
  936. not((current_module^.modulename^=s2^) or
  937. ((length(current_module^.modulename^)>8) and
  938. (copy(current_module^.modulename^,1,8)=s2^))) then
  939. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  940. if (current_module^.modulename^=s1^) then
  941. Message(unit_w_switch_us_missed);
  942. end;
  943. dispose(s2);
  944. dispose(s1);
  945. end;
  946. consume(_ID);
  947. consume(_SEMICOLON);
  948. consume(_INTERFACE);
  949. { global switches are read, so further changes aren't allowed }
  950. current_module^.in_global:=false;
  951. { handle the global switches }
  952. setupglobalswitches;
  953. Message1(unit_u_start_parse_interface,current_module^.modulename^);
  954. { update status }
  955. status.currentmodule:=current_module^.modulename^;
  956. { maybe turn off m_objpas if we are compiling objpas }
  957. if (current_module^.modulename^='OBJPAS') then
  958. aktmodeswitches:=aktmodeswitches-[m_objpas];
  959. { this should be placed after uses !!}
  960. {$ifndef UseNiceNames}
  961. procprefix:='_'+current_module^.modulename^+'$$';
  962. {$else UseNiceNames}
  963. procprefix:='_'+tostr(length(current_module^.modulename^))+lowercase(current_module^.modulename^)+'_';
  964. {$endif UseNiceNames}
  965. parse_only:=true;
  966. { generate now the global symboltable }
  967. st:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  968. refsymtable:=st;
  969. unitst:=punitsymtable(st);
  970. { define first as local to overcome dependency conflicts }
  971. current_module^.localsymtable:=st;
  972. { the unit name must be usable as a unit specifier }
  973. { inside the unit itself (PM) }
  974. { this also forbids to have another symbol }
  975. { with the same name as the unit }
  976. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  977. { a unit compiled at command line must be inside the loaded_unit list }
  978. if (compile_level=1) then
  979. loaded_units.insert(current_module);
  980. { load default units, like the system unit }
  981. loaddefaultunits;
  982. { reset }
  983. make_ref:=true;
  984. lexlevel:=0;
  985. { insert qualifier for the system unit (allows system.writeln) }
  986. if not(cs_compilesystem in aktmoduleswitches) then
  987. begin
  988. if token=_USES then
  989. begin
  990. unitst^.symtabletype:=unitsymtable;
  991. loadunits;
  992. { has it been compiled at a higher level ?}
  993. if current_module^.compiled then
  994. begin
  995. { this unit symtable is obsolete }
  996. { dispose(unitst,done);
  997. disposed as localsymtable !! }
  998. exit;
  999. end;
  1000. unitst^.symtabletype:=globalsymtable;
  1001. end;
  1002. { ... but insert the symbol table later }
  1003. st^.next:=symtablestack;
  1004. symtablestack:=st;
  1005. end
  1006. else
  1007. { while compiling a system unit, some types are directly inserted }
  1008. begin
  1009. st^.next:=symtablestack;
  1010. symtablestack:=st;
  1011. insert_intern_types(st);
  1012. end;
  1013. { now we know the place to insert the constants }
  1014. constsymtable:=symtablestack;
  1015. { move the global symtab from the temporary local to global }
  1016. current_module^.globalsymtable:=current_module^.localsymtable;
  1017. current_module^.localsymtable:=nil;
  1018. reset_global_defs;
  1019. { number all units, so we know if a unit is used by this unit or
  1020. needs to be added implicitly }
  1021. numberunits;
  1022. { ... parse the declarations }
  1023. Message1(parser_u_parsing_interface,current_module^.modulename^);
  1024. read_interface_declarations;
  1025. { leave when we got an error }
  1026. if (Errorcount>0) and not status.skip_error then
  1027. begin
  1028. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1029. status.skip_error:=true;
  1030. exit;
  1031. end;
  1032. {else in inteface its somatimes necessary even if unused
  1033. st^.allunitsused; }
  1034. {$ifdef New_GDB}
  1035. write_gdb_info;
  1036. {$endIf Def New_GDB}
  1037. {$ifndef Dont_use_double_checksum}
  1038. if not(cs_compilesystem in aktmoduleswitches) then
  1039. if (Errorcount=0) then
  1040. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),true);
  1041. {$endif Test_Double_checksum}
  1042. { Parse the implementation section }
  1043. consume(_IMPLEMENTATION);
  1044. current_module^.in_implementation:=true;
  1045. Message1(unit_u_start_parse_implementation,current_module^.modulename^);
  1046. parse_only:=false;
  1047. { generates static symbol table }
  1048. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1049. current_module^.localsymtable:=st;
  1050. { remove the globalsymtable from the symtable stack }
  1051. { to reinsert it after loading the implementation units }
  1052. symtablestack:=unitst^.next;
  1053. { we don't want implementation units symbols in unitsymtable !! PM }
  1054. refsymtable:=st;
  1055. { Read the implementation units }
  1056. parse_implementation_uses(unitst);
  1057. if current_module^.compiled then
  1058. begin
  1059. exit;
  1060. end;
  1061. { reset ranges/stabs in exported definitions }
  1062. reset_global_defs;
  1063. { All units are read, now give them a number }
  1064. numberunits;
  1065. { now we can change refsymtable }
  1066. refsymtable:=st;
  1067. { but reinsert the global symtable as lasts }
  1068. unitst^.next:=symtablestack;
  1069. symtablestack:=unitst;
  1070. {$ifdef DEBUG}
  1071. test_symtablestack;
  1072. {$endif DEBUG}
  1073. constsymtable:=symtablestack;
  1074. {$ifdef Splitheap}
  1075. if testsplit then
  1076. begin
  1077. Split_Heap;
  1078. allow_special:=true;
  1079. Switch_to_temp_heap;
  1080. end;
  1081. { it will report all crossings }
  1082. allow_special:=false;
  1083. {$endif Splitheap}
  1084. Message1(parser_u_parsing_implementation,current_module^.modulename^);
  1085. { Compile the unit }
  1086. codegen_newprocedure;
  1087. gen_main_procsym(current_module^.modulename^+'_init',potype_unitinit,st);
  1088. {$ifdef fixLeaksOnError}
  1089. new(names,init);
  1090. strContStack.push(names);
  1091. names^.insert('INIT$$'+current_module^.modulename^);
  1092. names^.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1093. compile_proc_body(names^,true,false);
  1094. if names <> PstringContainer(strContStack.pop) then
  1095. writeln('Problem with strContStack in pmodules (1)');
  1096. dispose(names,done);
  1097. {$else fixLeaksOnError}
  1098. names.init;
  1099. names.insert('INIT$$'+current_module^.modulename^);
  1100. names.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1101. compile_proc_body(names,true,false);
  1102. names.done;
  1103. {$endif fixLeaksOnError}
  1104. codegen_doneprocedure;
  1105. { avoid self recursive destructor call !! PM }
  1106. aktprocsym^.definition^.localst:=nil;
  1107. { if the unit contains ansi/widestrings, initialization and
  1108. finalization code must be forced }
  1109. force_init_final:=needs_init_final(current_module^.globalsymtable)
  1110. or needs_init_final(current_module^.localsymtable);
  1111. { should we force unit initialization? }
  1112. { this is a hack, but how can it be done better ? }
  1113. if force_init_final and ((current_module^.flags and uf_init)=0) then
  1114. begin
  1115. current_module^.flags:=current_module^.flags or uf_init;
  1116. { now we can insert a cut }
  1117. if (cs_create_smart in aktmoduleswitches) then
  1118. codesegment^.concat(new(pai_cut,init));
  1119. genimplicitunitinit(codesegment);
  1120. end;
  1121. { finalize? }
  1122. if token=_FINALIZATION then
  1123. begin
  1124. { set module options }
  1125. current_module^.flags:=current_module^.flags or uf_finalize;
  1126. { Compile the finalize }
  1127. codegen_newprocedure;
  1128. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1129. {$ifdef fixLeaksOnError}
  1130. new(names,init);
  1131. strContStack.push(names);
  1132. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1133. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1134. compile_proc_body(names^,true,false);
  1135. if names <> PstringContainer(strContStack.pop) then
  1136. writeln('Problem with strContStack in pmodules (2)');
  1137. dispose(names,done);
  1138. {$else fixLeaksOnError}
  1139. names.init;
  1140. names.insert('FINALIZE$$'+current_module^.modulename^);
  1141. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1142. compile_proc_body(names,true,false);
  1143. names.done;
  1144. {$endif fixLeaksOnError}
  1145. codegen_doneprocedure;
  1146. end
  1147. else if force_init_final then
  1148. begin
  1149. current_module^.flags:=current_module^.flags or uf_finalize;
  1150. { now we can insert a cut }
  1151. if (cs_create_smart in aktmoduleswitches) then
  1152. codesegment^.concat(new(pai_cut,init));
  1153. genimplicitunitfinal(codesegment);
  1154. end;
  1155. { the last char should always be a point }
  1156. consume(_POINT);
  1157. If ResourceStringList<>Nil then
  1158. begin
  1159. insertresourcestrings;
  1160. current_module^.flags:=current_module^.flags or uf_has_resources;
  1161. WriteResourceFile(Current_module^.ModuleName^);
  1162. end;
  1163. { avoid self recursive destructor call !! PM }
  1164. aktprocsym^.definition^.localst:=nil;
  1165. { absence does not matter here !! }
  1166. aktprocsym^.definition^.forwarddef:=false;
  1167. { test static symtable }
  1168. if (Errorcount=0) then
  1169. begin
  1170. st^.allsymbolsused;
  1171. st^.allunitsused;
  1172. st^.allprivatesused;
  1173. end;
  1174. { size of the static data }
  1175. datasize:=st^.datasize;
  1176. {$ifdef GDB}
  1177. { add all used definitions even for implementation}
  1178. if (cs_debuginfo in aktmoduleswitches) then
  1179. begin
  1180. {$IfnDef New_GDB}
  1181. if assigned(current_module^.globalsymtable) then
  1182. begin
  1183. { all types }
  1184. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  1185. { and all local symbols}
  1186. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  1187. end;
  1188. { all local types }
  1189. punitsymtable(st)^.concattypestabto(debuglist);
  1190. { and all local symbols}
  1191. st^.concatstabto(debuglist);
  1192. {$else New_GDB}
  1193. write_gdb_info;
  1194. {$endIf Def New_GDB}
  1195. end;
  1196. {$endif GDB}
  1197. reset_global_defs;
  1198. { tests, if all (interface) forwards are resolved }
  1199. if (Errorcount=0) then
  1200. begin
  1201. symtablestack^.check_forwards;
  1202. symtablestack^.allprivatesused;
  1203. end;
  1204. { now we have a correct unit, change the symtable type }
  1205. current_module^.in_implementation:=false;
  1206. symtablestack^.symtabletype:=unitsymtable;
  1207. {$ifdef GDB}
  1208. punitsymtable(symtablestack)^.is_stab_written:=false;
  1209. {$endif GDB}
  1210. { leave when we got an error }
  1211. if (Errorcount>0) and not status.skip_error then
  1212. begin
  1213. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1214. status.skip_error:=true;
  1215. closecurrentppu;
  1216. exit;
  1217. end;
  1218. { generate imports }
  1219. if current_module^.uses_imports then
  1220. importlib^.generatelib;
  1221. { insert own objectfile, or say that it's in a library
  1222. (no check for an .o when loading) }
  1223. if is_assembler_generated then
  1224. insertobjectfile
  1225. else
  1226. current_module^.flags:=current_module^.flags or uf_no_link;
  1227. if cs_local_browser in aktmoduleswitches then
  1228. current_module^.localsymtable:=refsymtable;
  1229. { Write out the ppufile }
  1230. {$ifndef Dont_use_double_checksum}
  1231. store_interface_crc:=current_module^.interface_crc;
  1232. store_crc:=current_module^.crc;
  1233. {$endif Test_Double_checksum}
  1234. if (Errorcount=0) then
  1235. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),false);
  1236. {$ifndef Dont_use_double_checksum}
  1237. if not(cs_compilesystem in aktmoduleswitches) then
  1238. if store_interface_crc<>current_module^.interface_crc then
  1239. Do_comment(V_Warning,current_module^.ppufilename^+' Interface CRC changed '+
  1240. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1241. {$ifdef EXTDEBUG}
  1242. if not(cs_compilesystem in aktmoduleswitches) then
  1243. if (store_crc<>current_module^.crc) and simplify_ppu then
  1244. Do_comment(V_Warning,current_module^.ppufilename^+' implementation CRC changed '+
  1245. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1246. {$endif EXTDEBUG}
  1247. {$endif ndef Dont_use_Double_checksum}
  1248. { must be done only after local symtable ref stores !! }
  1249. closecurrentppu;
  1250. {$ifdef GDB}
  1251. pu:=pused_unit(usedunits.first);
  1252. while assigned(pu) do
  1253. begin
  1254. if assigned(pu^.u^.globalsymtable) then
  1255. punitsymtable(pu^.u^.globalsymtable)^.is_stab_written:=false;
  1256. pu:=pused_unit(pu^.next);
  1257. end;
  1258. {$endif GDB}
  1259. { remove static symtable (=refsymtable) here to save some mem }
  1260. if not (cs_local_browser in aktmoduleswitches) then
  1261. begin
  1262. dispose(st,done);
  1263. current_module^.localsymtable:=nil;
  1264. end;
  1265. if is_assembler_generated then
  1266. begin
  1267. { finish asmlist by adding segment starts }
  1268. insertsegment;
  1269. { assemble }
  1270. create_objectfile;
  1271. end;
  1272. { leave when we got an error }
  1273. if (Errorcount>0) and not status.skip_error then
  1274. begin
  1275. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1276. status.skip_error:=true;
  1277. exit;
  1278. end;
  1279. end;
  1280. procedure proc_program(islibrary : boolean);
  1281. var
  1282. st : psymtable;
  1283. hp : pmodule;
  1284. {$ifdef fixLeaksOnError}
  1285. names : Pstringcontainer;
  1286. {$else fixLeaksOnError}
  1287. names : Tstringcontainer;
  1288. {$endif fixLeaksOnError}
  1289. begin
  1290. DLLsource:=islibrary;
  1291. IsExe:=true;
  1292. parse_only:=false;
  1293. { relocation works only without stabs under win32 !! PM }
  1294. { internal assembler uses rva for stabs info
  1295. so it should work with relocated DLLs }
  1296. if RelocSection and
  1297. (target_info.target=target_i386_win32) and
  1298. (target_info.assem<>as_i386_pecoff) then
  1299. begin
  1300. aktglobalswitches:=aktglobalswitches+[cs_link_strip];
  1301. { Warning stabs info does not work with reloc section !! }
  1302. if cs_debuginfo in aktmoduleswitches then
  1303. begin
  1304. Message1(parser_w_parser_reloc_no_debug,current_module^.mainsource^);
  1305. Message(parser_w_parser_win32_debug_needs_WN);
  1306. aktmoduleswitches:=aktmoduleswitches-[cs_debuginfo];
  1307. end;
  1308. end;
  1309. if islibrary then
  1310. begin
  1311. consume(_LIBRARY);
  1312. stringdispose(current_module^.modulename);
  1313. current_module^.modulename:=stringdup(pattern);
  1314. current_module^.islibrary:=true;
  1315. exportlib^.preparelib(pattern);
  1316. consume(_ID);
  1317. consume(_SEMICOLON);
  1318. end
  1319. else
  1320. { is there an program head ? }
  1321. if token=_PROGRAM then
  1322. begin
  1323. consume(_PROGRAM);
  1324. stringdispose(current_module^.modulename);
  1325. current_module^.modulename:=stringdup(pattern);
  1326. if (target_info.target=target_i386_WIN32) then
  1327. exportlib^.preparelib(pattern);
  1328. consume(_ID);
  1329. if token=_LKLAMMER then
  1330. begin
  1331. consume(_LKLAMMER);
  1332. idlist;
  1333. consume(_RKLAMMER);
  1334. end;
  1335. consume(_SEMICOLON);
  1336. end
  1337. else if (target_info.target=target_i386_WIN32) then
  1338. exportlib^.preparelib(current_module^.modulename^);
  1339. { global switches are read, so further changes aren't allowed }
  1340. current_module^.in_global:=false;
  1341. { get correct output names }
  1342. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  1343. { setup things using the global switches }
  1344. setupglobalswitches;
  1345. { set implementation flag }
  1346. current_module^.in_implementation:=true;
  1347. { insert after the unit symbol tables the static symbol table }
  1348. { of the program }
  1349. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1350. current_module^.localsymtable:=st;
  1351. symtablestack:=st;
  1352. refsymtable:=st;
  1353. { necessary for browser }
  1354. loaded_units.insert(current_module);
  1355. { load standard units (system,objpas,profile unit) }
  1356. loaddefaultunits;
  1357. { reset }
  1358. lexlevel:=0;
  1359. {Load the units used by the program we compile.}
  1360. if token=_USES then
  1361. loadunits;
  1362. { reset ranges/stabs in exported definitions }
  1363. reset_global_defs;
  1364. { All units are read, now give them a number }
  1365. numberunits;
  1366. {Insert the name of the main program into the symbol table.}
  1367. if current_module^.modulename^<>'' then
  1368. {st^.insert(new(pprogramsym,init(current_module^.modulename^)));}
  1369. st^.insert(new(punitsym,init(current_module^.modulename^,punitsymtable(st))));
  1370. { ...is also constsymtable, this is the symtable where }
  1371. { the elements of enumeration types are inserted }
  1372. constsymtable:=st;
  1373. Message1(parser_u_parsing_implementation,current_module^.mainsource^);
  1374. { reset }
  1375. procprefix:='';
  1376. {The program intialization needs an alias, so it can be called
  1377. from the bootstrap code.}
  1378. codegen_newprocedure;
  1379. gen_main_procsym('main',potype_proginit,st);
  1380. {$ifdef fixLeaksOnError}
  1381. new(names,init);
  1382. strContStack.push(names);
  1383. names^.insert('program_init');
  1384. names^.insert('PASCALMAIN');
  1385. names^.insert(target_os.cprefix+'main');
  1386. {$ifdef m68k}
  1387. if target_info.target=target_m68k_PalmOS then
  1388. names^.insert('PilotMain');
  1389. {$endif m68k}
  1390. compile_proc_body(names^,true,false);
  1391. if names <> PstringContainer(strContStack.pop) then
  1392. writeln('Problem with strContStack in pmodules (1)');
  1393. dispose(names,done);
  1394. {$else fixLeaksOnError}
  1395. names.init;
  1396. names.insert('program_init');
  1397. names.insert('PASCALMAIN');
  1398. names.insert(target_os.cprefix+'main');
  1399. {$ifdef m68k}
  1400. if target_info.target=target_m68k_PalmOS then
  1401. names.insert('PilotMain');
  1402. {$endif m68k}
  1403. compile_proc_body(names,true,false);
  1404. names.done;
  1405. {$endif fixLeaksOnError}
  1406. { avoid self recursive destructor call !! PM }
  1407. aktprocsym^.definition^.localst:=nil;
  1408. { consider these symbols as global ones }
  1409. { for browser }
  1410. current_module^.globalsymtable:=current_module^.localsymtable;
  1411. current_module^.localsymtable:=nil;
  1412. If ResourceStringList<>Nil then
  1413. begin
  1414. insertresourcestrings;
  1415. WriteResourceFile(Current_module^.ModuleName^);
  1416. end;
  1417. codegen_doneprocedure;
  1418. { finalize? }
  1419. if token=_FINALIZATION then
  1420. begin
  1421. { set module options }
  1422. current_module^.flags:=current_module^.flags or uf_finalize;
  1423. { Compile the finalize }
  1424. codegen_newprocedure;
  1425. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1426. {$ifdef fixLeaksOnError}
  1427. new(names,init);
  1428. strContStack.push(names);
  1429. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1430. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1431. compile_proc_body(names^,true,false);
  1432. if names <> PstringContainer(strContStack.pop) then
  1433. writeln('Problem with strContStack in pmodules (1)');
  1434. dispose(names,done);
  1435. {$else fixLeaksOnError}
  1436. names.init;
  1437. names.insert('FINALIZE$$'+current_module^.modulename^);
  1438. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1439. compile_proc_body(names,true,false);
  1440. names.done;
  1441. {$endif fixLeaksOnError}
  1442. codegen_doneprocedure;
  1443. end;
  1444. { consume the last point }
  1445. consume(_POINT);
  1446. {$ifdef New_GDB}
  1447. write_gdb_info;
  1448. {$endIf Def New_GDB}
  1449. { leave when we got an error }
  1450. if (Errorcount>0) and not status.skip_error then
  1451. begin
  1452. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1453. status.skip_error:=true;
  1454. exit;
  1455. end;
  1456. { test static symtable }
  1457. if (Errorcount=0) then
  1458. begin
  1459. st^.allsymbolsused;
  1460. st^.allunitsused;
  1461. st^.allprivatesused;
  1462. end;
  1463. { generate imports }
  1464. if current_module^.uses_imports then
  1465. importlib^.generatelib;
  1466. if islibrary or
  1467. (target_info.target=target_i386_WIN32) then
  1468. exportlib^.generatelib;
  1469. { insert heap }
  1470. insertResourceTablesTable;
  1471. insertinitfinaltable;
  1472. insertheap;
  1473. inserttargetspecific;
  1474. datasize:=symtablestack^.datasize;
  1475. { finish asmlist by adding segment starts }
  1476. insertsegment;
  1477. { insert own objectfile }
  1478. insertobjectfile;
  1479. { assemble and link }
  1480. create_objectfile;
  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. { create the executable when we are at level 1 }
  1489. if (compile_level=1) then
  1490. begin
  1491. { insert all .o files from all loaded units }
  1492. hp:=pmodule(loaded_units.first);
  1493. while assigned(hp) do
  1494. begin
  1495. Linker^.AddModuleFiles(hp);
  1496. hp:=pmodule(hp^.next);
  1497. end;
  1498. { write .def file }
  1499. if (cs_link_deffile in aktglobalswitches) then
  1500. deffile.writefile;
  1501. { finally we can create a executable }
  1502. if (not current_module^.is_unit) then
  1503. begin
  1504. if DLLSource then
  1505. Linker^.MakeSharedLibrary
  1506. else
  1507. Linker^.MakeExecutable;
  1508. end;
  1509. end;
  1510. end;
  1511. end.
  1512. {
  1513. $Log$
  1514. Revision 1.189 2000-04-25 23:55:30 pierre
  1515. + Hint about unused unit
  1516. * Testop bug fixed !!
  1517. Now the operators are only applied if the unit is explicitly loaded
  1518. Revision 1.188 2000/04/14 08:15:05 pierre
  1519. * close ppu file if errors
  1520. Revision 1.187 2000/04/02 10:18:18 florian
  1521. * bug 701 fixed: ansistrings in interface and implementation part of the units
  1522. are now finalized correctly even if there are no explicit initialization/
  1523. finalization statements
  1524. Revision 1.186 2000/03/01 15:36:11 florian
  1525. * some new stuff for the new cg
  1526. Revision 1.185 2000/02/09 13:22:57 peter
  1527. * log truncated
  1528. Revision 1.184 2000/02/06 17:20:53 peter
  1529. * -gl switch for auto lineinfo including
  1530. Revision 1.183 2000/01/16 22:17:12 peter
  1531. * renamed call_offset to para_offset
  1532. Revision 1.182 2000/01/16 14:15:33 jonas
  1533. * changed "with object_type" construct because of bug in the
  1534. compiler
  1535. Revision 1.181 2000/01/12 10:30:15 peter
  1536. * align codesegment at the end after main proc
  1537. Revision 1.180 2000/01/11 17:16:05 jonas
  1538. * removed a lot of memory leaks when an error is encountered (caused by
  1539. procinfo and pstringcontainers). There are still plenty left though :)
  1540. Revision 1.179 2000/01/11 09:52:07 peter
  1541. * fixed placing of .sl directories
  1542. * use -b again for base-file selection
  1543. * fixed group writing for linux with smartlinking
  1544. Revision 1.178 2000/01/07 01:14:29 peter
  1545. * updated copyright to 2000
  1546. Revision 1.177 1999/12/20 22:29:26 pierre
  1547. * relocation with debug info in rva (only with internal compiler)
  1548. Revision 1.176 1999/12/10 10:02:53 peter
  1549. * only check relocsection for win32
  1550. Revision 1.175 1999/11/30 10:40:44 peter
  1551. + ttype, tsymlist
  1552. Revision 1.174 1999/11/29 16:24:52 pierre
  1553. * bug in previous commit corrected
  1554. Revision 1.173 1999/11/29 15:18:27 pierre
  1555. + allow exports in win32 executables
  1556. Revision 1.172 1999/11/24 11:41:05 pierre
  1557. * defaultsymtablestack is now restored after parser.compile
  1558. Revision 1.171 1999/11/22 22:21:46 pierre
  1559. * Compute correct Exe Filenam
  1560. Revision 1.170 1999/11/22 00:23:09 pierre
  1561. * also complain about unused functions in program
  1562. Revision 1.169 1999/11/20 01:19:10 pierre
  1563. * DLL index used for win32 target with DEF file
  1564. + DLL initialization/finalization support
  1565. Revision 1.168 1999/11/18 23:35:40 pierre
  1566. * avoid double warnings
  1567. Revision 1.167 1999/11/18 15:34:47 pierre
  1568. * Notes/Hints for local syms changed to
  1569. Set_varstate function
  1570. Revision 1.166 1999/11/17 17:05:02 pierre
  1571. * Notes/hints changes
  1572. Revision 1.165 1999/11/15 15:03:47 pierre
  1573. * Pavel's changes for reloc section in executable
  1574. + warning that -g needs -WN under win32
  1575. }