pmodules.pas 60 KB

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