pmodules.pas 62 KB

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