pmodules.pas 54 KB

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