pmodules.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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,
  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. begin
  905. consume(_UNIT);
  906. if Compile_Level=1 then
  907. IsExe:=false;
  908. if token=_ID then
  909. begin
  910. { create filenames and unit name }
  911. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  912. stringdispose(current_module^.modulename);
  913. current_module^.modulename:=stringdup(upper(pattern));
  914. { check for system unit }
  915. new(s1);
  916. new(s2);
  917. s1^:=upper(target_info.system_unit);
  918. s2^:=upper(SplitName(current_scanner^.inputfile^.name^));
  919. if (cs_compilesystem in aktmoduleswitches) then
  920. begin
  921. if ((length(current_module^.modulename^)>8) or
  922. (current_module^.modulename^<>s1^) or
  923. (current_module^.modulename^<>s2^)) then
  924. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  925. end
  926. else
  927. begin
  928. if (cs_check_unit_name in aktglobalswitches) and
  929. not((current_module^.modulename^=s2^) or
  930. ((length(current_module^.modulename^)>8) and
  931. (copy(current_module^.modulename^,1,8)=s2^))) then
  932. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  933. if (current_module^.modulename^=s1^) then
  934. Message(unit_w_switch_us_missed);
  935. end;
  936. dispose(s2);
  937. dispose(s1);
  938. end;
  939. consume(_ID);
  940. consume(_SEMICOLON);
  941. consume(_INTERFACE);
  942. { global switches are read, so further changes aren't allowed }
  943. current_module^.in_global:=false;
  944. { handle the global switches }
  945. setupglobalswitches;
  946. Message1(unit_u_start_parse_interface,current_module^.modulename^);
  947. { update status }
  948. status.currentmodule:=current_module^.modulename^;
  949. { maybe turn off m_objpas if we are compiling objpas }
  950. if (current_module^.modulename^='OBJPAS') then
  951. aktmodeswitches:=aktmodeswitches-[m_objpas];
  952. { this should be placed after uses !!}
  953. {$ifndef UseNiceNames}
  954. procprefix:='_'+current_module^.modulename^+'$$';
  955. {$else UseNiceNames}
  956. procprefix:='_'+tostr(length(current_module^.modulename^))+lowercase(current_module^.modulename^)+'_';
  957. {$endif UseNiceNames}
  958. parse_only:=true;
  959. { generate now the global symboltable }
  960. st:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  961. refsymtable:=st;
  962. unitst:=punitsymtable(st);
  963. { define first as local to overcome dependency conflicts }
  964. current_module^.localsymtable:=st;
  965. { the unit name must be usable as a unit specifier }
  966. { inside the unit itself (PM) }
  967. { this also forbids to have another symbol }
  968. { with the same name as the unit }
  969. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  970. { a unit compiled at command line must be inside the loaded_unit list }
  971. if (compile_level=1) then
  972. loaded_units.insert(current_module);
  973. { load default units, like the system unit }
  974. loaddefaultunits;
  975. { reset }
  976. make_ref:=true;
  977. lexlevel:=0;
  978. { insert qualifier for the system unit (allows system.writeln) }
  979. if not(cs_compilesystem in aktmoduleswitches) then
  980. begin
  981. if token=_USES then
  982. begin
  983. unitst^.symtabletype:=unitsymtable;
  984. loadunits;
  985. { has it been compiled at a higher level ?}
  986. if current_module^.compiled then
  987. begin
  988. { this unit symtable is obsolete }
  989. { dispose(unitst,done);
  990. disposed as localsymtable !! }
  991. exit;
  992. end;
  993. unitst^.symtabletype:=globalsymtable;
  994. end;
  995. { ... but insert the symbol table later }
  996. st^.next:=symtablestack;
  997. symtablestack:=st;
  998. end
  999. else
  1000. { while compiling a system unit, some types are directly inserted }
  1001. begin
  1002. st^.next:=symtablestack;
  1003. symtablestack:=st;
  1004. insert_intern_types(st);
  1005. end;
  1006. { now we know the place to insert the constants }
  1007. constsymtable:=symtablestack;
  1008. { move the global symtab from the temporary local to global }
  1009. current_module^.globalsymtable:=current_module^.localsymtable;
  1010. current_module^.localsymtable:=nil;
  1011. reset_global_defs;
  1012. { number all units, so we know if a unit is used by this unit or
  1013. needs to be added implicitly }
  1014. numberunits;
  1015. { ... parse the declarations }
  1016. Message1(parser_u_parsing_interface,current_module^.modulename^);
  1017. read_interface_declarations;
  1018. { leave when we got an error }
  1019. if (Errorcount>0) and not status.skip_error then
  1020. begin
  1021. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1022. status.skip_error:=true;
  1023. exit;
  1024. end;
  1025. {$ifdef New_GDB}
  1026. write_gdb_info;
  1027. {$endIf Def New_GDB}
  1028. {$ifndef Dont_use_double_checksum}
  1029. if not(cs_compilesystem in aktmoduleswitches) then
  1030. if (Errorcount=0) then
  1031. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),true);
  1032. {$endif Test_Double_checksum}
  1033. { Parse the implementation section }
  1034. consume(_IMPLEMENTATION);
  1035. current_module^.in_implementation:=true;
  1036. Message1(unit_u_start_parse_implementation,current_module^.modulename^);
  1037. parse_only:=false;
  1038. { generates static symbol table }
  1039. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1040. current_module^.localsymtable:=st;
  1041. { remove the globalsymtable from the symtable stack }
  1042. { to reinsert it after loading the implementation units }
  1043. symtablestack:=unitst^.next;
  1044. { we don't want implementation units symbols in unitsymtable !! PM }
  1045. refsymtable:=st;
  1046. { Read the implementation units }
  1047. parse_implementation_uses(unitst);
  1048. if current_module^.compiled then
  1049. begin
  1050. exit;
  1051. end;
  1052. { reset ranges/stabs in exported definitions }
  1053. reset_global_defs;
  1054. { All units are read, now give them a number }
  1055. numberunits;
  1056. { now we can change refsymtable }
  1057. refsymtable:=st;
  1058. { but reinsert the global symtable as lasts }
  1059. unitst^.next:=symtablestack;
  1060. symtablestack:=unitst;
  1061. {$ifdef DEBUG}
  1062. test_symtablestack;
  1063. {$endif DEBUG}
  1064. constsymtable:=symtablestack;
  1065. {$ifdef Splitheap}
  1066. if testsplit then
  1067. begin
  1068. Split_Heap;
  1069. allow_special:=true;
  1070. Switch_to_temp_heap;
  1071. end;
  1072. { it will report all crossings }
  1073. allow_special:=false;
  1074. {$endif Splitheap}
  1075. Message1(parser_u_parsing_implementation,current_module^.modulename^);
  1076. { Compile the unit }
  1077. codegen_newprocedure;
  1078. gen_main_procsym(current_module^.modulename^+'_init',potype_unitinit,st);
  1079. {$ifdef fixLeaksOnError}
  1080. new(names,init);
  1081. strContStack.push(names);
  1082. names^.insert('INIT$$'+current_module^.modulename^);
  1083. names^.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1084. compile_proc_body(names^,true,false);
  1085. if names <> PstringContainer(strContStack.pop) then
  1086. writeln('Problem with strContStack in pmodules (1)');
  1087. dispose(names,done);
  1088. {$else fixLeaksOnError}
  1089. names.init;
  1090. names.insert('INIT$$'+current_module^.modulename^);
  1091. names.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1092. compile_proc_body(names,true,false);
  1093. names.done;
  1094. {$endif fixLeaksOnError}
  1095. codegen_doneprocedure;
  1096. { avoid self recursive destructor call !! PM }
  1097. aktprocsym^.definition^.localst:=nil;
  1098. { finalize? }
  1099. if token=_FINALIZATION then
  1100. begin
  1101. { set module options }
  1102. current_module^.flags:=current_module^.flags or uf_finalize;
  1103. { Compile the finalize }
  1104. codegen_newprocedure;
  1105. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1106. {$ifdef fixLeaksOnError}
  1107. new(names,init);
  1108. strContStack.push(names);
  1109. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1110. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1111. compile_proc_body(names^,true,false);
  1112. if names <> PstringContainer(strContStack.pop) then
  1113. writeln('Problem with strContStack in pmodules (2)');
  1114. dispose(names,done);
  1115. {$else fixLeaksOnError}
  1116. names.init;
  1117. names.insert('FINALIZE$$'+current_module^.modulename^);
  1118. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1119. compile_proc_body(names,true,false);
  1120. names.done;
  1121. {$endif fixLeaksOnError}
  1122. codegen_doneprocedure;
  1123. end;
  1124. { the last char should always be a point }
  1125. consume(_POINT);
  1126. If ResourceStringList<>Nil then
  1127. begin
  1128. insertresourcestrings;
  1129. current_module^.flags:=current_module^.flags or uf_has_resources;
  1130. WriteResourceFile(Current_module^.ModuleName^);
  1131. end;
  1132. { avoid self recursive destructor call !! PM }
  1133. aktprocsym^.definition^.localst:=nil;
  1134. { absence does not matter here !! }
  1135. aktprocsym^.definition^.forwarddef:=false;
  1136. { test static symtable }
  1137. if (Errorcount=0) then
  1138. begin
  1139. st^.allsymbolsused;
  1140. st^.allprivatesused;
  1141. end;
  1142. { size of the static data }
  1143. datasize:=st^.datasize;
  1144. {$ifdef GDB}
  1145. { add all used definitions even for implementation}
  1146. if (cs_debuginfo in aktmoduleswitches) then
  1147. begin
  1148. {$IfnDef New_GDB}
  1149. if assigned(current_module^.globalsymtable) then
  1150. begin
  1151. { all types }
  1152. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  1153. { and all local symbols}
  1154. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  1155. end;
  1156. { all local types }
  1157. punitsymtable(st)^.concattypestabto(debuglist);
  1158. { and all local symbols}
  1159. st^.concatstabto(debuglist);
  1160. {$else New_GDB}
  1161. write_gdb_info;
  1162. {$endIf Def New_GDB}
  1163. end;
  1164. {$endif GDB}
  1165. reset_global_defs;
  1166. { tests, if all (interface) forwards are resolved }
  1167. if (Errorcount=0) then
  1168. begin
  1169. symtablestack^.check_forwards;
  1170. symtablestack^.allprivatesused;
  1171. end;
  1172. { now we have a correct unit, change the symtable type }
  1173. current_module^.in_implementation:=false;
  1174. symtablestack^.symtabletype:=unitsymtable;
  1175. {$ifdef GDB}
  1176. punitsymtable(symtablestack)^.is_stab_written:=false;
  1177. {$endif GDB}
  1178. { leave when we got an error }
  1179. if (Errorcount>0) and not status.skip_error then
  1180. begin
  1181. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1182. status.skip_error:=true;
  1183. exit;
  1184. end;
  1185. { generate imports }
  1186. if current_module^.uses_imports then
  1187. importlib^.generatelib;
  1188. { insert own objectfile, or say that it's in a library
  1189. (no check for an .o when loading) }
  1190. if is_assembler_generated then
  1191. insertobjectfile
  1192. else
  1193. current_module^.flags:=current_module^.flags or uf_no_link;
  1194. if cs_local_browser in aktmoduleswitches then
  1195. current_module^.localsymtable:=refsymtable;
  1196. { Write out the ppufile }
  1197. {$ifndef Dont_use_double_checksum}
  1198. store_interface_crc:=current_module^.interface_crc;
  1199. store_crc:=current_module^.crc;
  1200. {$endif Test_Double_checksum}
  1201. if (Errorcount=0) then
  1202. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),false);
  1203. {$ifndef Dont_use_double_checksum}
  1204. if not(cs_compilesystem in aktmoduleswitches) then
  1205. if store_interface_crc<>current_module^.interface_crc then
  1206. Do_comment(V_Warning,current_module^.ppufilename^+' Interface CRC changed '+
  1207. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1208. {$ifdef EXTDEBUG}
  1209. if not(cs_compilesystem in aktmoduleswitches) then
  1210. if (store_crc<>current_module^.crc) and simplify_ppu then
  1211. Do_comment(V_Warning,current_module^.ppufilename^+' implementation CRC changed '+
  1212. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1213. {$endif EXTDEBUG}
  1214. {$endif ndef Dont_use_Double_checksum}
  1215. { must be done only after local symtable ref stores !! }
  1216. closecurrentppu;
  1217. {$ifdef GDB}
  1218. pu:=pused_unit(usedunits.first);
  1219. while assigned(pu) do
  1220. begin
  1221. if assigned(pu^.u^.globalsymtable) then
  1222. punitsymtable(pu^.u^.globalsymtable)^.is_stab_written:=false;
  1223. pu:=pused_unit(pu^.next);
  1224. end;
  1225. {$endif GDB}
  1226. { remove static symtable (=refsymtable) here to save some mem }
  1227. if not (cs_local_browser in aktmoduleswitches) then
  1228. begin
  1229. dispose(st,done);
  1230. current_module^.localsymtable:=nil;
  1231. end;
  1232. if is_assembler_generated then
  1233. begin
  1234. { finish asmlist by adding segment starts }
  1235. insertsegment;
  1236. { assemble }
  1237. create_objectfile;
  1238. end;
  1239. { leave when we got an error }
  1240. if (Errorcount>0) and not status.skip_error then
  1241. begin
  1242. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1243. status.skip_error:=true;
  1244. exit;
  1245. end;
  1246. end;
  1247. procedure proc_program(islibrary : boolean);
  1248. var
  1249. st : psymtable;
  1250. hp : pmodule;
  1251. {$ifdef fixLeaksOnError}
  1252. names : Pstringcontainer;
  1253. {$else fixLeaksOnError}
  1254. names : Tstringcontainer;
  1255. {$endif fixLeaksOnError}
  1256. begin
  1257. DLLsource:=islibrary;
  1258. IsExe:=true;
  1259. parse_only:=false;
  1260. { relocation works only without stabs under win32 !! PM }
  1261. { internal assembler uses rva for stabs info
  1262. so it should work with relocated DLLs }
  1263. if RelocSection and
  1264. (target_info.target=target_i386_win32) and
  1265. (target_info.assem<>as_i386_pecoff) then
  1266. begin
  1267. aktglobalswitches:=aktglobalswitches+[cs_link_strip];
  1268. { Warning stabs info does not work with reloc section !! }
  1269. if cs_debuginfo in aktmoduleswitches then
  1270. begin
  1271. Message1(parser_w_parser_reloc_no_debug,current_module^.mainsource^);
  1272. Message(parser_w_parser_win32_debug_needs_WN);
  1273. aktmoduleswitches:=aktmoduleswitches-[cs_debuginfo];
  1274. end;
  1275. end;
  1276. if islibrary then
  1277. begin
  1278. consume(_LIBRARY);
  1279. stringdispose(current_module^.modulename);
  1280. current_module^.modulename:=stringdup(pattern);
  1281. current_module^.islibrary:=true;
  1282. exportlib^.preparelib(pattern);
  1283. consume(_ID);
  1284. consume(_SEMICOLON);
  1285. end
  1286. else
  1287. { is there an program head ? }
  1288. if token=_PROGRAM then
  1289. begin
  1290. consume(_PROGRAM);
  1291. stringdispose(current_module^.modulename);
  1292. current_module^.modulename:=stringdup(pattern);
  1293. if (target_info.target=target_i386_WIN32) then
  1294. exportlib^.preparelib(pattern);
  1295. consume(_ID);
  1296. if token=_LKLAMMER then
  1297. begin
  1298. consume(_LKLAMMER);
  1299. idlist;
  1300. consume(_RKLAMMER);
  1301. end;
  1302. consume(_SEMICOLON);
  1303. end
  1304. else if (target_info.target=target_i386_WIN32) then
  1305. exportlib^.preparelib(current_module^.modulename^);
  1306. { global switches are read, so further changes aren't allowed }
  1307. current_module^.in_global:=false;
  1308. { get correct output names }
  1309. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  1310. { setup things using the global switches }
  1311. setupglobalswitches;
  1312. { set implementation flag }
  1313. current_module^.in_implementation:=true;
  1314. { insert after the unit symbol tables the static symbol table }
  1315. { of the program }
  1316. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1317. current_module^.localsymtable:=st;
  1318. symtablestack:=st;
  1319. refsymtable:=st;
  1320. { necessary for browser }
  1321. loaded_units.insert(current_module);
  1322. { load standard units (system,objpas,profile unit) }
  1323. loaddefaultunits;
  1324. { reset }
  1325. lexlevel:=0;
  1326. {Load the units used by the program we compile.}
  1327. if token=_USES then
  1328. loadunits;
  1329. { reset ranges/stabs in exported definitions }
  1330. reset_global_defs;
  1331. { All units are read, now give them a number }
  1332. numberunits;
  1333. {Insert the name of the main program into the symbol table.}
  1334. if current_module^.modulename^<>'' then
  1335. {st^.insert(new(pprogramsym,init(current_module^.modulename^)));}
  1336. st^.insert(new(punitsym,init(current_module^.modulename^,punitsymtable(st))));
  1337. { ...is also constsymtable, this is the symtable where }
  1338. { the elements of enumeration types are inserted }
  1339. constsymtable:=st;
  1340. Message1(parser_u_parsing_implementation,current_module^.mainsource^);
  1341. { reset }
  1342. procprefix:='';
  1343. {The program intialization needs an alias, so it can be called
  1344. from the bootstrap code.}
  1345. codegen_newprocedure;
  1346. gen_main_procsym('main',potype_proginit,st);
  1347. {$ifdef fixLeaksOnError}
  1348. new(names,init);
  1349. strContStack.push(names);
  1350. names^.insert('program_init');
  1351. names^.insert('PASCALMAIN');
  1352. names^.insert(target_os.cprefix+'main');
  1353. {$ifdef m68k}
  1354. if target_info.target=target_m68k_PalmOS then
  1355. names^.insert('PilotMain');
  1356. {$endif m68k}
  1357. compile_proc_body(names^,true,false);
  1358. if names <> PstringContainer(strContStack.pop) then
  1359. writeln('Problem with strContStack in pmodules (1)');
  1360. dispose(names,done);
  1361. {$else fixLeaksOnError}
  1362. names.init;
  1363. names.insert('program_init');
  1364. names.insert('PASCALMAIN');
  1365. names.insert(target_os.cprefix+'main');
  1366. {$ifdef m68k}
  1367. if target_info.target=target_m68k_PalmOS then
  1368. names.insert('PilotMain');
  1369. {$endif m68k}
  1370. compile_proc_body(names,true,false);
  1371. names.done;
  1372. {$endif fixLeaksOnError}
  1373. { avoid self recursive destructor call !! PM }
  1374. aktprocsym^.definition^.localst:=nil;
  1375. { consider these symbols as global ones }
  1376. { for browser }
  1377. current_module^.globalsymtable:=current_module^.localsymtable;
  1378. current_module^.localsymtable:=nil;
  1379. If ResourceStringList<>Nil then
  1380. begin
  1381. insertresourcestrings;
  1382. WriteResourceFile(Current_module^.ModuleName^);
  1383. end;
  1384. codegen_doneprocedure;
  1385. { finalize? }
  1386. if token=_FINALIZATION then
  1387. begin
  1388. { set module options }
  1389. current_module^.flags:=current_module^.flags or uf_finalize;
  1390. { Compile the finalize }
  1391. codegen_newprocedure;
  1392. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1393. {$ifdef fixLeaksOnError}
  1394. new(names,init);
  1395. strContStack.push(names);
  1396. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1397. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1398. compile_proc_body(names^,true,false);
  1399. if names <> PstringContainer(strContStack.pop) then
  1400. writeln('Problem with strContStack in pmodules (1)');
  1401. dispose(names,done);
  1402. {$else fixLeaksOnError}
  1403. names.init;
  1404. names.insert('FINALIZE$$'+current_module^.modulename^);
  1405. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1406. compile_proc_body(names,true,false);
  1407. names.done;
  1408. {$endif fixLeaksOnError}
  1409. codegen_doneprocedure;
  1410. end;
  1411. { consume the last point }
  1412. consume(_POINT);
  1413. {$ifdef New_GDB}
  1414. write_gdb_info;
  1415. {$endIf Def New_GDB}
  1416. { leave when we got an error }
  1417. if (Errorcount>0) and not status.skip_error then
  1418. begin
  1419. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1420. status.skip_error:=true;
  1421. exit;
  1422. end;
  1423. { test static symtable }
  1424. if (Errorcount=0) then
  1425. begin
  1426. st^.allsymbolsused;
  1427. st^.allprivatesused;
  1428. end;
  1429. { generate imports }
  1430. if current_module^.uses_imports then
  1431. importlib^.generatelib;
  1432. if islibrary or
  1433. (target_info.target=target_i386_WIN32) then
  1434. exportlib^.generatelib;
  1435. { insert heap }
  1436. insertResourceTablesTable;
  1437. insertinitfinaltable;
  1438. insertheap;
  1439. inserttargetspecific;
  1440. datasize:=symtablestack^.datasize;
  1441. { finish asmlist by adding segment starts }
  1442. insertsegment;
  1443. { insert own objectfile }
  1444. insertobjectfile;
  1445. { assemble and link }
  1446. create_objectfile;
  1447. { leave when we got an error }
  1448. if (Errorcount>0) and not status.skip_error then
  1449. begin
  1450. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1451. status.skip_error:=true;
  1452. exit;
  1453. end;
  1454. { create the executable when we are at level 1 }
  1455. if (compile_level=1) then
  1456. begin
  1457. { insert all .o files from all loaded units }
  1458. hp:=pmodule(loaded_units.first);
  1459. while assigned(hp) do
  1460. begin
  1461. Linker^.AddModuleFiles(hp);
  1462. hp:=pmodule(hp^.next);
  1463. end;
  1464. { write .def file }
  1465. if (cs_link_deffile in aktglobalswitches) then
  1466. deffile.writefile;
  1467. { finally we can create a executable }
  1468. if (not current_module^.is_unit) then
  1469. begin
  1470. if DLLSource then
  1471. Linker^.MakeSharedLibrary
  1472. else
  1473. Linker^.MakeExecutable;
  1474. end;
  1475. end;
  1476. end;
  1477. end.
  1478. {
  1479. $Log$
  1480. Revision 1.185 2000-02-09 13:22:57 peter
  1481. * log truncated
  1482. Revision 1.184 2000/02/06 17:20:53 peter
  1483. * -gl switch for auto lineinfo including
  1484. Revision 1.183 2000/01/16 22:17:12 peter
  1485. * renamed call_offset to para_offset
  1486. Revision 1.182 2000/01/16 14:15:33 jonas
  1487. * changed "with object_type" construct because of bug in the
  1488. compiler
  1489. Revision 1.181 2000/01/12 10:30:15 peter
  1490. * align codesegment at the end after main proc
  1491. Revision 1.180 2000/01/11 17:16:05 jonas
  1492. * removed a lot of memory leaks when an error is encountered (caused by
  1493. procinfo and pstringcontainers). There are still plenty left though :)
  1494. Revision 1.179 2000/01/11 09:52:07 peter
  1495. * fixed placing of .sl directories
  1496. * use -b again for base-file selection
  1497. * fixed group writing for linux with smartlinking
  1498. Revision 1.178 2000/01/07 01:14:29 peter
  1499. * updated copyright to 2000
  1500. Revision 1.177 1999/12/20 22:29:26 pierre
  1501. * relocation with debug info in rva (only with internal compiler)
  1502. Revision 1.176 1999/12/10 10:02:53 peter
  1503. * only check relocsection for win32
  1504. Revision 1.175 1999/11/30 10:40:44 peter
  1505. + ttype, tsymlist
  1506. Revision 1.174 1999/11/29 16:24:52 pierre
  1507. * bug in previous commit corrected
  1508. Revision 1.173 1999/11/29 15:18:27 pierre
  1509. + allow exports in win32 executables
  1510. Revision 1.172 1999/11/24 11:41:05 pierre
  1511. * defaultsymtablestack is now restored after parser.compile
  1512. Revision 1.171 1999/11/22 22:21:46 pierre
  1513. * Compute correct Exe Filenam
  1514. Revision 1.170 1999/11/22 00:23:09 pierre
  1515. * also complain about unused functions in program
  1516. Revision 1.169 1999/11/20 01:19:10 pierre
  1517. * DLL index used for win32 target with DEF file
  1518. + DLL initialization/finalization support
  1519. Revision 1.168 1999/11/18 23:35:40 pierre
  1520. * avoid double warnings
  1521. Revision 1.167 1999/11/18 15:34:47 pierre
  1522. * Notes/Hints for local syms changed to
  1523. Set_varstate function
  1524. Revision 1.166 1999/11/17 17:05:02 pierre
  1525. * Notes/hints changes
  1526. Revision 1.165 1999/11/15 15:03:47 pierre
  1527. * Pavel's changes for reloc section in executable
  1528. + warning that -g needs -WN under win32
  1529. }