pmodules.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  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. {define TEST_IMPL does not work well }
  20. interface
  21. uses
  22. files;
  23. procedure loadsystemunit;
  24. procedure proc_unit;
  25. procedure proc_program(islibrary : boolean);
  26. implementation
  27. uses
  28. cobjects,verbose,comphook,systems,globals,
  29. symtable,aasm,hcodegen,
  30. link,assemble,import
  31. {$ifndef OLDPPU}
  32. ,ppu
  33. {$endif OLDPPU}
  34. {$ifdef i386}
  35. ,i386
  36. {$endif}
  37. {$ifdef m68k}
  38. ,m68k
  39. {$endif}
  40. ,scanner,pbase,psystem,pdecl,psub,parser;
  41. procedure setlinkerfile;
  42. begin
  43. { Add Object File }
  44. if (cs_smartlink in aktmoduleswitches) then
  45. current_module^.linkstaticlibs.insert(current_module^.libfilename^)
  46. else
  47. current_module^.linkofiles.insert(current_module^.objfilename^);
  48. end;
  49. procedure insertsegment;
  50. procedure fixseg(p:paasmoutput;sec:tsection);
  51. begin
  52. p^.insert(new(pai_section,init(sec)));
  53. if (cs_smartlink in aktmoduleswitches) then
  54. p^.insert(new(pai_cut,init));
  55. p^.concat(new(pai_section,init(sec_none)));
  56. end;
  57. begin
  58. {Insert Ident of the compiler}
  59. if (not (cs_smartlink in aktmoduleswitches))
  60. {$ifndef EXTDEBUG}
  61. and (not current_module^.is_unit)
  62. {$endif}
  63. then
  64. begin
  65. datasegment^.insert(new(pai_align,init(4)));
  66. datasegment^.insert(new(pai_string,init('FPC '+version_string+' for '+target_string+' - '+target_info.short_name)));
  67. end;
  68. { Insert start and end of sections }
  69. fixseg(codesegment,sec_code);
  70. fixseg(datasegment,sec_data);
  71. fixseg(bsssegment,sec_bss);
  72. fixseg(consts,sec_data);
  73. end;
  74. procedure insertheap;
  75. begin
  76. if (cs_smartlink in aktmoduleswitches) then
  77. begin
  78. bsssegment^.concat(new(pai_cut,init));
  79. datasegment^.concat(new(pai_cut,init));
  80. end;
  81. { On the Macintosh Classic M68k Architecture
  82. The Heap variable is simply a POINTER to the
  83. real HEAP. The HEAP must be set up by the RTL
  84. and must store the pointer in this value.
  85. On OS/2 the heap is also intialized by the RTL. We do
  86. not output a pointer }
  87. case target_info.target of
  88. {$ifdef i386}
  89. target_OS2 : ;
  90. {$endif i386}
  91. {$ifdef m68k}
  92. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  93. {$endif m68k}
  94. else
  95. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  96. end;
  97. {$ifdef i386}
  98. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  99. {$endif i386}
  100. {$ifdef m68k}
  101. datasegment^.concat(new(pai_symbol,init_global('HEAP_SIZE')));
  102. {$endif m68k}
  103. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  104. end;
  105. procedure inserttargetspecific;
  106. begin
  107. {$ifdef i386}
  108. case target_info.target of
  109. target_GO32V2 : begin
  110. { stacksize can be specified }
  111. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  112. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  113. end;
  114. target_WIN32 : begin
  115. { Generate an external entry to be sure that _mainCRTStarup will be
  116. linked, can't use concat_external because those aren't written for
  117. asw (PFV) }
  118. datasegment^.concat(new(pai_const,init_symbol('_mainCRTStartup')));
  119. end;
  120. end;
  121. {$endif i386}
  122. end;
  123. {$ifndef OLDPPU}
  124. function loadunit(const s : string;compile_system:boolean) : pmodule;forward;
  125. procedure load_usedunits(compile_system:boolean);
  126. var
  127. pu : pused_unit;
  128. loaded_unit : pmodule;
  129. nextmapentry : longint;
  130. begin
  131. { init the map }
  132. new(current_module^.map);
  133. nextmapentry:=1;
  134. { load the used units from interface }
  135. pu:=pused_unit(current_module^.used_units.first);
  136. while assigned(pu) do
  137. begin
  138. if (not pu^.loaded) and (pu^.in_interface) then
  139. begin
  140. loaded_unit:=loadunit(pu^.name^,false);
  141. if current_module^.compiled then
  142. exit;
  143. { register unit in used units }
  144. pu^.u:=loaded_unit;
  145. pu^.loaded:=true;
  146. { need to recompile the current unit ? }
  147. if loaded_unit^.crc<>pu^.checksum then
  148. begin
  149. current_module^.do_compile:=true;
  150. exit;
  151. end;
  152. { setup the map entry for deref }
  153. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  154. inc(nextmapentry);
  155. if nextmapentry>maxunits then
  156. Message(unit_f_too_much_units);
  157. end;
  158. pu:=pused_unit(pu^.next);
  159. end;
  160. { ok, now load the unit }
  161. current_module^.symtable:=new(punitsymtable,loadasunit);
  162. { if this is the system unit insert the intern symbols }
  163. if compile_system then
  164. begin
  165. make_ref:=false;
  166. insertinternsyms(psymtable(current_module^.symtable));
  167. make_ref:=true;
  168. end;
  169. { now only read the implementation part }
  170. current_module^.in_implementation:=true;
  171. { load the used units from implementation }
  172. pu:=pused_unit(current_module^.used_units.first);
  173. while assigned(pu) do
  174. begin
  175. if (not pu^.loaded) and (not pu^.in_interface) then
  176. begin
  177. loaded_unit:=loadunit(pu^.name^,false);
  178. if current_module^.compiled then
  179. exit;
  180. { register unit in used units }
  181. pu^.u:=loaded_unit;
  182. pu^.loaded:=true;
  183. {$ifdef TEST_IMPL}
  184. { need to recompile the current unit ? }
  185. if loaded_unit^.crc<>pu^.checksum then
  186. begin
  187. current_module^.do_compile:=true;
  188. exit;
  189. end;
  190. { setup the map entry for deref }
  191. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  192. inc(nextmapentry);
  193. if nextmapentry>maxunits then
  194. Message(unit_f_too_much_units);
  195. {$endif TEST_IMPL}
  196. end;
  197. pu:=pused_unit(pu^.next);
  198. end;
  199. { remove the map, it's not needed anymore }
  200. dispose(current_module^.map);
  201. current_module^.map:=nil;
  202. end;
  203. function loadunit(const s : string;compile_system:boolean) : pmodule;
  204. var
  205. st : punitsymtable;
  206. old_current_ppu : pppufile;
  207. old_current_module,hp : pmodule;
  208. procedure loadppufile;
  209. begin
  210. { load interface section }
  211. if not current_module^.do_compile then
  212. load_interface;
  213. { only load units when we don't recompile }
  214. if not current_module^.do_compile then
  215. load_usedunits(compile_system);
  216. { recompile if set }
  217. if current_module^.do_compile then
  218. begin
  219. { we needn't the ppufile }
  220. if assigned(current_module^.ppufile) then
  221. begin
  222. dispose(current_module^.ppufile,done);
  223. current_module^.ppufile:=nil;
  224. end;
  225. { recompile the unit or give a fatal error if sources not available }
  226. if not(current_module^.sources_avail) then
  227. Message1(unit_f_cant_compile_unit,current_module^.modulename^)
  228. else
  229. begin
  230. current_scanner^.close;
  231. compile(current_module^.mainsource^,compile_system);
  232. if (not old_current_module^.compiled) then
  233. current_scanner^.reopen;
  234. end;
  235. end
  236. else
  237. begin
  238. { only reassemble ? }
  239. if (current_module^.do_assemble) then
  240. OnlyAsm(current_module^.asmfilename^);
  241. { add the files for the linker }
  242. Linker.AddModuleFiles(current_module);
  243. end;
  244. if assigned(current_module^.ppufile) then
  245. begin
  246. dispose(current_module^.ppufile,done);
  247. current_module^.ppufile:=nil;
  248. end;
  249. end;
  250. begin
  251. old_current_module:=current_module;
  252. old_current_ppu:=current_ppu;
  253. { be sure not to mix lines from different files }
  254. { update_line; }
  255. { unit not found }
  256. st:=nil;
  257. { search all loaded units }
  258. hp:=pmodule(loaded_units.first);
  259. while assigned(hp) do
  260. begin
  261. if hp^.modulename^=s then
  262. begin
  263. { the unit is already registered }
  264. { and this means that the unit }
  265. { is already compiled }
  266. { else there is a cyclic unit use }
  267. if assigned(hp^.symtable) then
  268. st:=punitsymtable(hp^.symtable)
  269. else
  270. begin
  271. { recompile the unit ? }
  272. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  273. Message(unit_f_circular_unit_reference);
  274. end;
  275. break;
  276. end;
  277. { the next unit }
  278. hp:=pmodule(hp^.next);
  279. end;
  280. { the unit is not in the symtable stack }
  281. if (not assigned(st)) then
  282. begin
  283. { if the unit is loaded remove it first }
  284. if assigned(hp) then
  285. begin
  286. { remove the old unit }
  287. loaded_units.remove(hp);
  288. dispose(hp,done);
  289. end;
  290. { generates a new unit info record }
  291. current_module:=new(pmodule,init(s,true));
  292. current_ppu:=current_module^.ppufile;
  293. { now we can register the unit }
  294. loaded_units.insert(current_module);
  295. { now realy load the ppu }
  296. loadppufile;
  297. { set compiled flag }
  298. current_module^.compiled:=true;
  299. { register the unit _once_ }
  300. usedunits.concat(new(pused_unit,init(current_module,true)));
  301. { load return pointer }
  302. hp:=current_module;
  303. end;
  304. { set the old module }
  305. current_ppu:=old_current_ppu;
  306. current_module:=old_current_module;
  307. loadunit:=hp;
  308. end;
  309. {$else OLDPPU}
  310. {*****************************************************************************
  311. Old PPU
  312. *****************************************************************************}
  313. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;forward;
  314. procedure load_ppu(oldhp,hp : pmodule;compile_system : boolean);
  315. var
  316. loaded_unit : pmodule;
  317. b : byte;
  318. checksum,
  319. count,
  320. nextmapentry : longint;
  321. hs : string;
  322. begin
  323. { init the map }
  324. new(hp^.map);
  325. nextmapentry:=1;
  326. { load the used units from interface }
  327. hp^.ppufile^.read_data(b,1,count);
  328. while (b=ibloadunit) do
  329. begin
  330. { read unit name }
  331. hp^.ppufile^.read_data(hs[0],1,count);
  332. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  333. hp^.ppufile^.read_data(checksum,4,count);
  334. loaded_unit:=loadunit(hs,false,false);
  335. if hp^.compiled then
  336. exit;
  337. { if the crc of a used unit is the same as }
  338. { written to the PPU file, we needn't to }
  339. { recompile the current unit }
  340. if (loaded_unit^.crc<>checksum) then
  341. begin
  342. { we have to compile the current unit }
  343. { remove stuff which isn't needed }
  344. { forget the map }
  345. dispose(hp^.map);
  346. hp^.map:=nil;
  347. hp^.ppufile^.close;
  348. dispose(hp^.ppufile,done);
  349. hp^.ppufile:=nil;
  350. if not(hp^.sources_avail) then
  351. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  352. else
  353. begin
  354. if assigned(oldhp^.current_inputfile) then
  355. oldhp^.current_inputfile^.tempclose;
  356. compile(hp^.mainsource^,compile_system);
  357. if (not oldhp^.compiled) and assigned(oldhp^.current_inputfile) then
  358. oldhp^.current_inputfile^.tempreopen;
  359. end;
  360. exit;
  361. end;
  362. { setup the map entry for deref }
  363. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  364. inc(nextmapentry);
  365. if nextmapentry>maxunits then
  366. Message(unit_f_too_much_units);
  367. { read until ibend }
  368. hp^.ppufile^.read_data(b,1,count);
  369. end;
  370. { ok, now load the unit }
  371. hp^.symtable:=new(punitsymtable,load(hp^.modulename^));
  372. { if this is the system unit insert the intern }
  373. { symbols }
  374. make_ref:=false;
  375. if compile_system then
  376. insertinternsyms(psymtable(hp^.symtable));
  377. make_ref:=true;
  378. { now only read the implementation part }
  379. hp^.in_implementation:=true;
  380. { load the used units from implementation }
  381. hp^.ppufile^.read_data(b,1,count);
  382. while (b<>ibend) and (b=ibloadunit) do
  383. begin
  384. { read unit name }
  385. hp^.ppufile^.read_data(hs[0],1,count);
  386. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  387. hp^.ppufile^.read_data(checksum,4,count);
  388. loaded_unit:=loadunit(hs,false,false);
  389. if hp^.compiled then exit;
  390. { if the crc of a used unit is the same as }
  391. { written to the PPU file, we needn't to }
  392. { recompile the current unit }
  393. { but for the implementation part }
  394. { the written crc is false, because }
  395. { not defined when writing the ppufile !! }
  396. {$ifdef TEST_IMPL}
  397. if (checksum<>0) and (loaded_unit^.crc<>checksum) then
  398. begin
  399. { we have to compile the current unit }
  400. { remove stuff which isn't needed }
  401. { forget the map }
  402. dispose(hp^.map);
  403. hp^.map:=nil;
  404. hp^.ppufile^.close;
  405. dispose(hp^.ppufile,done);
  406. hp^.ppufile:=nil;
  407. if not(hp^.sources_avail) then
  408. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  409. else
  410. begin
  411. oldhp^.current_inputfile^.tempclose;
  412. compile(hp^.mainsource^,compile_system);
  413. oldhp^.current_inputfile^.tempclose;
  414. end;
  415. exit;
  416. end;
  417. {$endif TEST_IMPL}
  418. { read until ibend }
  419. hp^.ppufile^.read_data(b,1,count);
  420. end;
  421. hp^.ppufile^.close;
  422. dispose(hp^.map);
  423. hp^.map:=nil;
  424. end;
  425. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  426. var
  427. st : punitsymtable;
  428. old_current_module,hp,nextmodule : pmodule;
  429. pu : pused_unit;
  430. hs : pstring;
  431. begin
  432. old_current_module:=current_module;
  433. { be sure not to mix lines from different files }
  434. { update_line; }
  435. { unit not found }
  436. st:=nil;
  437. { search all loaded units }
  438. hp:=pmodule(loaded_units.first);
  439. while assigned(hp) do
  440. begin
  441. if hp^.modulename^=s then
  442. begin
  443. { the unit is already registered }
  444. { and this means that the unit }
  445. { is already compiled }
  446. { else there is a cyclic unit use }
  447. if assigned(hp^.symtable) then
  448. st:=punitsymtable(hp^.symtable)
  449. else
  450. begin
  451. { recompile the unit ? }
  452. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  453. Message(unit_f_circular_unit_reference);
  454. end;
  455. break;
  456. end;
  457. { the next unit }
  458. hp:=pmodule(hp^.next);
  459. end;
  460. { no error and the unit isn't loaded }
  461. if not(assigned(hp)) and (st=nil) then
  462. begin
  463. { generates a new unit info record }
  464. hp:=new(pmodule,init(s,true));
  465. { now we can register the unit }
  466. loaded_units.insert(hp);
  467. current_module:=hp;
  468. { force build ? }
  469. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  470. begin
  471. { we needn't the ppufile }
  472. if assigned(hp^.ppufile) then
  473. begin
  474. dispose(hp^.ppufile,done);
  475. hp^.ppufile:=nil;
  476. end;
  477. if not(hp^.sources_avail) then
  478. Message1(unit_f_cant_compile_unit,hp^.modulename^)
  479. else
  480. begin
  481. if assigned(old_current_module^.current_inputfile) then
  482. old_current_module^.current_inputfile^.tempclose;
  483. compile(hp^.mainsource^,compile_system);
  484. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  485. old_current_module^.current_inputfile^.tempreopen;
  486. end;
  487. end
  488. else
  489. begin
  490. { only reassemble ? }
  491. if (hp^.do_assemble) then
  492. OnlyAsm(hp^.asmfilename^);
  493. { we should know there the PPU file else it's an error and
  494. we can't load the unit }
  495. if hp^.ppufile^.name^<>'' then
  496. load_ppu(old_current_module,hp,compile_system);
  497. { add the files for the linker }
  498. addlinkerfiles(hp);
  499. end;
  500. { register the unit _once_ }
  501. usedunits.concat(new(pused_unit,init(hp,0)));
  502. { the unit is written, so we can set the symtable type }
  503. { to unitsymtable, else we get some dupid errors }
  504. { this is not the right place because of the }
  505. { ready label }
  506. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  507. { placed at this end of proc_unit }
  508. psymtable(hp^.symtable)^.unitid:=0;
  509. { reset the unitnumbers for the other units }
  510. pu:=pused_unit(old_current_module^.used_units.first);
  511. while assigned(pu) do
  512. begin
  513. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  514. pu:=pused_unit(pu^.next);
  515. end;
  516. end
  517. else
  518. if assigned(hp) and (st=nil) then
  519. begin
  520. { we have to compile the unit again, but it is already inserted !!}
  521. { we may have problem with the lost symtable !! }
  522. current_module:=hp;
  523. { we must preserve the unit chain }
  524. nextmodule:=pmodule(hp^.next);
  525. { we have to cleanup a little }
  526. hp^.special_done;
  527. new(hs);
  528. hs^:=hp^.mainsource^;
  529. hp^.init(hs^,true);
  530. dispose(hs);
  531. { we must preserve the unit chain }
  532. hp^.next:=nextmodule;
  533. if assigned(hp^.ppufile) then
  534. load_ppu(old_current_module,hp,compile_system)
  535. else
  536. begin
  537. {$ifdef UseBrowser}
  538. { here we need to remove the names ! }
  539. hp^.sourcefiles.done;
  540. hp^.sourcefiles.init;
  541. {$endif UseBrowser}
  542. if assigned(old_current_module^.current_inputfile) then
  543. old_current_module^.current_inputfile^.tempclose;
  544. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  545. compile(hp^.mainsource^,compile_system);
  546. if (not old_current_module^.compiled) and assigned(old_current_module^.current_inputfile) then
  547. old_current_module^.current_inputfile^.tempreopen;
  548. end;
  549. current_module^.compiled:=true;
  550. end;
  551. { set the old module }
  552. current_module:=old_current_module;
  553. { the current module uses the unit hp }
  554. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  555. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  556. if in_uses and not current_module^.in_implementation then
  557. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  558. loadunit:=hp;
  559. end;
  560. {$endif OLDPPU}
  561. procedure loadsystemunit;
  562. var
  563. hp : pmodule;
  564. begin
  565. { if the current file isn't a system unit the the system unit
  566. will be loaded }
  567. if not(cs_compilesystem in aktmoduleswitches) then
  568. begin
  569. {$ifndef OLDPPU}
  570. hp:=loadunit(upper(target_info.system_unit),true);
  571. systemunit:=hp^.symtable;
  572. { add to the used units }
  573. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  574. {$else OLDPPU}
  575. hp:=loadunit(upper(target_info.system_unit),true,true);
  576. systemunit:=hp^.symtable;
  577. { add to the used units }
  578. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  579. {$endif OLDPPU}
  580. { read default constant definitions }
  581. make_ref:=false;
  582. readconstdefs;
  583. { we could try to overload caret by default }
  584. symtablestack:=systemunit;
  585. { if POWER is defined in the RTL then use it for starstar overloading }
  586. getsym('POWER',false);
  587. if assigned(srsym) and (srsym^.typ=procsym) and
  588. (overloaded_operators[STARSTAR]=nil) then
  589. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  590. make_ref:=true;
  591. end
  592. else
  593. begin
  594. createconstdefs;
  595. systemunit:=nil;
  596. end;
  597. end;
  598. procedure loadunits;
  599. var
  600. s : stringid;
  601. hp : pused_unit;
  602. hp2 : pmodule;
  603. hp3 : psymtable;
  604. oldprocsym:Pprocsym;
  605. begin
  606. oldprocsym:=aktprocsym;
  607. consume(_USES);
  608. {$ifdef DEBUG}
  609. test_symtablestack;
  610. {$endif DEBUG}
  611. repeat
  612. s:=pattern;
  613. consume(ID);
  614. {$ifndef OLDPPU}
  615. hp2:=loadunit(s,false);
  616. { the current module uses the unit hp2 }
  617. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  618. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  619. {$else OLDPPU}
  620. hp2:=loadunit(s,false,true);
  621. {$endif OLDPPU}
  622. if current_module^.compiled then
  623. exit;
  624. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  625. if token=COMMA then
  626. begin
  627. pattern:='';
  628. consume(COMMA);
  629. end
  630. else
  631. break;
  632. until false;
  633. consume(SEMICOLON);
  634. { set the symtable to systemunit so it gets reorderd correctly }
  635. symtablestack:=systemunit;
  636. { now insert the units in the symtablestack }
  637. hp:=pused_unit(current_module^.used_units.first);
  638. while assigned(hp) do
  639. begin
  640. {$IfDef GDB}
  641. if (cs_debuginfo in aktmoduleswitches) and
  642. not hp^.is_stab_written then
  643. begin
  644. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  645. hp^.is_stab_written:=true;
  646. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  647. end;
  648. {$EndIf GDB}
  649. if hp^.in_uses then
  650. begin
  651. hp3:=symtablestack;
  652. while assigned(hp3) do
  653. begin
  654. { insert units only once ! }
  655. if hp^.u^.symtable=hp3 then
  656. break;
  657. hp3:=hp3^.next;
  658. { unit isn't inserted }
  659. if hp3=nil then
  660. begin
  661. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  662. symtablestack:=psymtable(hp^.u^.symtable);
  663. {$ifdef CHAINPROCSYMS}
  664. symtablestack^.chainprocsyms;
  665. {$endif CHAINPROCSYMS}
  666. {$ifdef DEBUG}
  667. test_symtablestack;
  668. {$endif DEBUG}
  669. end;
  670. end;
  671. end;
  672. hp:=pused_unit(hp^.next);
  673. end;
  674. aktprocsym:=oldprocsym;
  675. end;
  676. procedure parse_implementation_uses(symt:Psymtable);
  677. var
  678. old_module_in_implementation : boolean;
  679. begin
  680. if token=_USES then
  681. begin
  682. old_module_in_implementation:=module_in_implementation;
  683. module_in_implementation:=true;
  684. current_module^.in_implementation:=true;
  685. symt^.symtabletype:=unitsymtable;
  686. loadunits;
  687. symt^.symtabletype:=globalsymtable;
  688. {$ifdef DEBUG}
  689. test_symtablestack;
  690. {$endif DEBUG}
  691. module_in_implementation:=old_module_in_implementation;
  692. end;
  693. end;
  694. procedure proc_unit;
  695. var
  696. { unitname : stringid; }
  697. names : Tstringcontainer;
  698. p : psymtable;
  699. unitst : punitsymtable;
  700. pu : pused_unit;
  701. i : longint;
  702. s1,s2 : ^string; {Saves stack space}
  703. begin
  704. consume(_UNIT);
  705. if token=ID then
  706. begin
  707. { create filenames and unit name }
  708. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
  709. stringdispose(current_module^.modulename);
  710. current_module^.modulename:=stringdup(upper(pattern));
  711. { check for system unit }
  712. new(s1);
  713. new(s2);
  714. s1^:=upper(target_info.system_unit);
  715. s2^:=upper(current_scanner^.inputfile^.name^);
  716. { strip extension, there could only be one dot }
  717. i:=pos('.',s2^);
  718. if i>0 then
  719. s2^:=Copy(s2^,1,i-1);
  720. if (cs_compilesystem in aktmoduleswitches) then
  721. begin
  722. if (cs_check_unit_name in aktglobalswitches) and
  723. ((length(current_module^.modulename^)>8) or
  724. (current_module^.modulename^<>s1^) or
  725. (current_module^.modulename^<>s2^)) then
  726. Message1(unit_e_illegal_unit_name,s1^);
  727. end
  728. else
  729. if (current_module^.modulename^=s1^) then
  730. Message(unit_w_switch_us_missed);
  731. dispose(s2);
  732. dispose(s1);
  733. end;
  734. consume(ID);
  735. consume(SEMICOLON);
  736. consume(_INTERFACE);
  737. { update status }
  738. status.currentmodule:=current_module^.modulename^;
  739. { this should be placed after uses !!}
  740. {$ifndef UseNiceNames}
  741. procprefix:='_'+current_module^.modulename^+'$$';
  742. {$else UseNiceNames}
  743. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  744. {$endif UseNiceNames}
  745. parse_only:=true;
  746. { generate now the global symboltable }
  747. p:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  748. refsymtable:=p;
  749. unitst:=punitsymtable(p);
  750. { the unit name must be usable as a unit specifier }
  751. { inside the unit itself (PM) }
  752. { this also forbids to have another symbol }
  753. { with the same name as the unit }
  754. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  755. { set the symbol table for the current unit }
  756. { this must be set later for interdependency }
  757. { current_module^.symtable:=psymtable(p); }
  758. { a unit compiled at command line must be inside the loaded_unit list }
  759. if (compile_level=1) then
  760. begin
  761. loaded_units.insert(current_module);
  762. if cs_createlib in initmoduleswitches then
  763. begin
  764. current_module^.flags:=current_module^.flags or uf_in_library;
  765. if cs_shared_lib in initmoduleswitches then
  766. current_module^.flags:=current_module^.flags or uf_shared_library;
  767. end;
  768. end;
  769. { insert qualifier for the system unit (allows system.writeln) }
  770. if not(cs_compilesystem in aktmoduleswitches) then
  771. begin
  772. { insert the system unit }
  773. { it is allways the first }
  774. systemunit^.next:=nil;
  775. symtablestack:=systemunit;
  776. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  777. if token=_USES then
  778. begin
  779. unitst^.symtabletype:=unitsymtable;
  780. loadunits;
  781. { has it been compiled at a higher level ?}
  782. if current_module^.compiled then
  783. exit;
  784. unitst^.symtabletype:=globalsymtable;
  785. end;
  786. { ... but insert the symbol table later }
  787. p^.next:=symtablestack;
  788. symtablestack:=p;
  789. end
  790. else
  791. { while compiling a system unit, some types are directly inserted }
  792. begin
  793. p^.next:=symtablestack;
  794. symtablestack:=p;
  795. insert_intern_types(p);
  796. end;
  797. { displaced for inter-dependency considerations }
  798. current_module^.symtable:=psymtable(p);
  799. constsymtable:=symtablestack;
  800. { ... parse the declarations }
  801. read_interface_declarations;
  802. consume(_IMPLEMENTATION);
  803. parse_only:=false;
  804. {$ifdef GDB}
  805. { add all used definitions even for implementation}
  806. if (cs_debuginfo in aktmoduleswitches) then
  807. begin
  808. { all types }
  809. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  810. { and all local symbols}
  811. refsymtable^.concatstabto(debuglist);
  812. end;
  813. {$endif GDB}
  814. { generates static symbol table }
  815. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  816. {Generate a procsym.}
  817. make_ref:=false;
  818. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  819. aktprocsym^.definition:=new(Pprocdef,init);
  820. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  821. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  822. make_ref:=true;
  823. {The generated procsym has a local symtable. Discard it and turn
  824. it into the static one.}
  825. dispose(aktprocsym^.definition^.localst,done);
  826. aktprocsym^.definition^.localst:=p;
  827. { remove the globalsymtable from the symtable stack }
  828. { to reinsert it after loading the implementation units }
  829. symtablestack:=unitst^.next;
  830. { number the definitions, so a deref from other units works }
  831. refsymtable^.number_defs;
  832. { Read the implementation units }
  833. parse_implementation_uses(unitst);
  834. numberunits;
  835. { now we can change refsymtable }
  836. refsymtable:=p;
  837. { but reinsert the global symtable as lasts }
  838. unitst^.next:=symtablestack;
  839. symtablestack:=unitst;
  840. {$ifdef DEBUG}
  841. test_symtablestack;
  842. {$endif DEBUG}
  843. constsymtable:=symtablestack;
  844. {$ifdef Splitheap}
  845. if testsplit then
  846. begin
  847. Split_Heap;
  848. allow_special:=true;
  849. Switch_to_temp_heap;
  850. end;
  851. { it will report all crossings }
  852. allow_special:=false;
  853. {$endif Splitheap}
  854. { set some informations }
  855. procinfo.retdef:=voiddef;
  856. procinfo._class:=nil;
  857. procinfo.call_offset:=8;
  858. { for temporary values }
  859. procinfo.framepointer:=frame_pointer;
  860. { clear flags }
  861. procinfo.flags:=0;
  862. { Create a new procedure }
  863. codegen_newprocedure;
  864. { Compile the unit }
  865. names.init;
  866. names.insert(current_module^.modulename^+'_init');
  867. names.insert('INIT$$'+current_module^.modulename^);
  868. compile_proc_body(names,true,false);
  869. names.done;
  870. { Shutdown the codegen for this procedure }
  871. codegen_doneprocedure;
  872. {$ifdef dummy}
  873. if token=_FINALIZATION then
  874. begin
  875. current_module^.flags:=current_module^.flags or uf_finalize;
  876. { clear flags }
  877. procinfo.flags:=0;
  878. {Reset the codegenerator.}
  879. codegen_newprocedure;
  880. names.init;
  881. names.insert(current_module^.modulename^+'_finalize');
  882. names.insert('FINALIZE$$'+current_module^.modulename^);
  883. compile_proc_body(names,true,false);
  884. names.done;
  885. codegen_doneprocedure;
  886. end;
  887. {$endif dummy}
  888. consume(POINT);
  889. { add files which need to be linked }
  890. setlinkerfile;
  891. { size of the static data }
  892. datasize:=symtablestack^.datasize;
  893. { unsed static symbols ? }
  894. symtablestack^.allsymbolsused;
  895. {$ifdef GDB}
  896. { add all used definitions even for implementation}
  897. if (cs_debuginfo in aktmoduleswitches) then
  898. begin
  899. { all types }
  900. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  901. { and all local symbols}
  902. symtablestack^.concatstabto(debuglist);
  903. end;
  904. {$endif GDB}
  905. current_module^.in_implementation:=false;
  906. { deletes all symtables generated in the implementation part }
  907. while symtablestack^.symtabletype<>globalsymtable do
  908. dellexlevel;
  909. { tests, if all forwards are resolved }
  910. symtablestack^.check_forwards;
  911. symtablestack^.symtabletype:=unitsymtable;
  912. punitsymtable(symtablestack)^.is_stab_written:=false;
  913. {Write out the unit if the compile was succesfull.}
  914. if status.errorcount=0 then
  915. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  916. pu:=pused_unit(usedunits.first);
  917. while assigned(pu) do
  918. begin
  919. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  920. pu:=pused_unit(pu^.next);
  921. end;
  922. inc(datasize,symtablestack^.datasize);
  923. { generate imports }
  924. if current_module^.uses_imports then
  925. importlib^.generatelib;
  926. { finish asmlist by adding segment starts }
  927. insertsegment;
  928. end;
  929. procedure proc_program(islibrary : boolean);
  930. var
  931. st : psymtable;
  932. names : Tstringcontainer;
  933. begin
  934. parse_only:=false;
  935. if islibrary then
  936. begin
  937. consume(_LIBRARY);
  938. stringdispose(current_module^.modulename);
  939. current_module^.modulename:=stringdup(pattern);
  940. consume(ID);
  941. consume(SEMICOLON);
  942. end
  943. else
  944. { is there an program head ? }
  945. if token=_PROGRAM then
  946. begin
  947. consume(_PROGRAM);
  948. stringdispose(current_module^.modulename);
  949. current_module^.modulename:=stringdup(pattern);
  950. consume(ID);
  951. if token=LKLAMMER then
  952. begin
  953. consume(LKLAMMER);
  954. idlist;
  955. consume(RKLAMMER);
  956. end;
  957. consume(SEMICOLON);
  958. end;
  959. { insert after the unit symbol tables the static symbol table }
  960. { of the program }
  961. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  962. {Generate a procsym.}
  963. make_ref:=false;
  964. aktprocsym:=new(Pprocsym,init('main'));
  965. aktprocsym^.definition:=new(Pprocdef,init);
  966. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  967. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  968. make_ref:=true;
  969. {The localst is a local symtable. Change it into the static
  970. symtable.}
  971. dispose(aktprocsym^.definition^.localst,done);
  972. aktprocsym^.definition^.localst:=st;
  973. refsymtable:=st;
  974. { necessary for browser }
  975. loaded_units.insert(current_module);
  976. {Insert the symbols of the system unit into the stack of symbol
  977. tables.}
  978. symtablestack:=systemunit;
  979. systemunit^.next:=nil;
  980. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  981. {Load the units used by the program we compile.}
  982. if token=_USES then
  983. loadunits;
  984. {Insert the name of the main program into the symbol table.}
  985. if current_module^.modulename^<>'' then
  986. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  987. { ...is also constsymtable, this is the symtable where }
  988. { the elements of enumeration types are inserted }
  989. constsymtable:=st;
  990. { set some informations about the main program }
  991. with procinfo do
  992. begin
  993. retdef:=voiddef;
  994. _class:=nil;
  995. call_offset:=8;
  996. framepointer:=frame_pointer;
  997. flags:=0;
  998. end;
  999. procprefix:='';
  1000. in_except_block:=false;
  1001. codegen_newprocedure;
  1002. {The program intialization needs an alias, so it can be called
  1003. from the bootstrap code.}
  1004. names.init;
  1005. names.insert('program_init');
  1006. names.insert('PASCALMAIN');
  1007. names.insert(target_os.cprefix+'main');
  1008. compile_proc_body(names,true,false);
  1009. names.done;
  1010. codegen_doneprocedure;
  1011. consume(POINT);
  1012. setlinkerfile;
  1013. { insert heap }
  1014. insertheap;
  1015. { generate imports }
  1016. if current_module^.uses_imports then
  1017. importlib^.generatelib;
  1018. inserttargetspecific;
  1019. datasize:=symtablestack^.datasize;
  1020. { finish asmlist by adding segment starts }
  1021. insertsegment;
  1022. end;
  1023. end.
  1024. {
  1025. $Log$
  1026. Revision 1.39 1998-08-14 21:56:37 peter
  1027. * setting the outputfile using -o works now to create static libs
  1028. Revision 1.38 1998/08/10 14:50:13 peter
  1029. + localswitches, moduleswitches, globalswitches splitting
  1030. Revision 1.37 1998/08/10 10:18:31 peter
  1031. + Compiler,Comphook unit which are the new interface units to the
  1032. compiler
  1033. Revision 1.36 1998/07/14 14:46:54 peter
  1034. * released NEWINPUT
  1035. Revision 1.35 1998/07/08 12:39:38 peter
  1036. * heap_size for m68k
  1037. Revision 1.34 1998/07/07 11:20:03 peter
  1038. + NEWINPUT for a better inputfile and scanner object
  1039. Revision 1.33 1998/06/25 11:15:34 pierre
  1040. * ppu files where not closed in newppu !!
  1041. second compilation was impossible due to too many opened files
  1042. (not visible in 'make cycle' as we remove all the ppu files)
  1043. Revision 1.32 1998/06/25 08:48:16 florian
  1044. * first version of rtti support
  1045. Revision 1.31 1998/06/24 14:48:35 peter
  1046. * ifdef newppu -> ifndef oldppu
  1047. Revision 1.30 1998/06/17 14:10:16 peter
  1048. * small os2 fixes
  1049. * fixed interdependent units with newppu (remake3 under linux works now)
  1050. Revision 1.29 1998/06/16 08:56:25 peter
  1051. + targetcpu
  1052. * cleaner pmodules for newppu
  1053. Revision 1.28 1998/06/13 00:10:10 peter
  1054. * working browser and newppu
  1055. * some small fixes against crashes which occured in bp7 (but not in
  1056. fpc?!)
  1057. Revision 1.27 1998/06/11 13:58:08 peter
  1058. * small fix to let newppu compile
  1059. Revision 1.26 1998/06/09 16:01:47 pierre
  1060. + added procedure directive parsing for procvars
  1061. (accepted are popstack cdecl and pascal)
  1062. + added C vars with the following syntax
  1063. var C calias 'true_c_name';(can be followed by external)
  1064. reason is that you must add the Cprefix
  1065. which is target dependent
  1066. Revision 1.25 1998/06/08 22:59:49 peter
  1067. * smartlinking works for win32
  1068. * some defines to exclude some compiler parts
  1069. Revision 1.24 1998/06/08 13:13:44 pierre
  1070. + temporary variables now in temp_gen.pas unit
  1071. because it is processor independent
  1072. * mppc68k.bat modified to undefine i386 and support_mmx
  1073. (which are defaults for i386)
  1074. Revision 1.23 1998/06/05 17:47:29 peter
  1075. * some better uses clauses
  1076. Revision 1.22 1998/06/05 14:37:34 pierre
  1077. * fixes for inline for operators
  1078. * inline procedure more correctly restricted
  1079. Revision 1.21 1998/06/04 23:51:53 peter
  1080. * m68k compiles
  1081. + .def file creation moved to gendef.pas so it could also be used
  1082. for win32
  1083. Revision 1.20 1998/06/04 09:55:42 pierre
  1084. * demangled name of procsym reworked to become independant of the mangling scheme
  1085. Revision 1.19 1998/06/03 23:40:38 peter
  1086. + unlimited file support, release tempclose
  1087. Revision 1.18 1998/06/03 22:49:00 peter
  1088. + wordbool,longbool
  1089. * rename bis,von -> high,low
  1090. * moved some systemunit loading/creating to psystem.pas
  1091. Revision 1.17 1998/05/28 14:40:25 peter
  1092. * fixes for newppu, remake3 works now with it
  1093. Revision 1.16 1998/05/27 19:45:06 peter
  1094. * symtable.pas splitted into includefiles
  1095. * symtable adapted for $ifdef NEWPPU
  1096. Revision 1.15 1998/05/23 01:21:22 peter
  1097. + aktasmmode, aktoptprocessor, aktoutputformat
  1098. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1099. + $LIBNAME to set the library name where the unit will be put in
  1100. * splitted cgi386 a bit (codeseg to large for bp7)
  1101. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1102. Revision 1.14 1998/05/20 09:42:35 pierre
  1103. + UseTokenInfo now default
  1104. * unit in interface uses and implementation uses gives error now
  1105. * only one error for unknown symbol (uses lastsymknown boolean)
  1106. the problem came from the label code !
  1107. + first inlined procedures and function work
  1108. (warning there might be allowed cases were the result is still wrong !!)
  1109. * UseBrower updated gives a global list of all position of all used symbols
  1110. with switch -gb
  1111. Revision 1.13 1998/05/12 10:47:00 peter
  1112. * moved printstatus to verb_def
  1113. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1114. prefix like error: warning: and is included in V_Default
  1115. * fixed some messages
  1116. * first time parameter scan is only for -v and -T
  1117. - removed old style messages
  1118. Revision 1.12 1998/05/11 13:07:56 peter
  1119. + $ifdef NEWPPU for the new ppuformat
  1120. + $define GDB not longer required
  1121. * removed all warnings and stripped some log comments
  1122. * no findfirst/findnext anymore to remove smartlink *.o files
  1123. Revision 1.11 1998/05/06 18:36:53 peter
  1124. * tai_section extended with code,data,bss sections and enumerated type
  1125. * ident 'compiled by FPC' moved to pmodules
  1126. * small fix for smartlink
  1127. Revision 1.10 1998/05/04 17:54:28 peter
  1128. + smartlinking works (only case jumptable left todo)
  1129. * redesign of systems.pas to support assemblers and linkers
  1130. + Unitname is now also in the PPU-file, increased version to 14
  1131. Revision 1.9 1998/05/01 16:38:45 florian
  1132. * handling of private and protected fixed
  1133. + change_keywords_to_tp implemented to remove
  1134. keywords which aren't supported by tp
  1135. * break and continue are now symbols of the system unit
  1136. + widestring, longstring and ansistring type released
  1137. Revision 1.8 1998/04/30 15:59:41 pierre
  1138. * GDB works again better :
  1139. correct type info in one pass
  1140. + UseTokenInfo for better source position
  1141. * fixed one remaining bug in scanner for line counts
  1142. * several little fixes
  1143. Revision 1.7 1998/04/29 10:33:59 pierre
  1144. + added some code for ansistring (not complete nor working yet)
  1145. * corrected operator overloading
  1146. * corrected nasm output
  1147. + started inline procedures
  1148. + added starstarn : use ** for exponentiation (^ gave problems)
  1149. + started UseTokenInfo cond to get accurate positions
  1150. Revision 1.6 1998/04/27 23:10:28 peter
  1151. + new scanner
  1152. * $makelib -> if smartlink
  1153. * small filename fixes pmodule.setfilename
  1154. * moved import from files.pas -> import.pas
  1155. Revision 1.5 1998/04/14 23:27:03 florian
  1156. + exclude/include with constant second parameter added
  1157. Revision 1.4 1998/04/10 14:41:43 peter
  1158. * removed some Hints
  1159. * small speed optimization for AsmLn
  1160. Revision 1.3 1998/04/03 09:51:00 daniel
  1161. * Fixed heap allocation for OS/2.
  1162. }