pmodules.pas 64 KB

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