pmodules.pas 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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. interface
  20. uses
  21. dos,strings,
  22. cobjects,globals,scanner,symtable,aasm,tree,pass_1,
  23. types,hcodegen,files,verbose,systems,link,assemble
  24. {$ifdef GDB}
  25. ,gdb
  26. {$endif GDB}
  27. { parser specific stuff }
  28. ,pbase,pdecl,pstatmnt,psub
  29. { processor specific stuff }
  30. {$ifdef i386}
  31. ,i386
  32. ,cgai386
  33. ,tgeni386
  34. ,cgi386
  35. ,aopt386
  36. {$endif}
  37. {$ifdef m68k}
  38. ,m68k
  39. ,cga68k
  40. ,tgen68k
  41. ,cg68k
  42. {$endif}
  43. ;
  44. procedure addlinkerfiles(hp:pmodule);
  45. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  46. procedure proc_unit;
  47. procedure proc_program(islibrary : boolean);
  48. implementation
  49. uses
  50. parser;
  51. {$I innr.inc}
  52. procedure addlinkerfiles(hp:pmodule);
  53. begin
  54. with hp^ do
  55. begin
  56. while not linkofiles.empty do
  57. Linker.AddObject(linkofiles.Get);
  58. while not linksharedlibs.empty do
  59. Linker.AddSharedLibrary(linksharedlibs.Get);
  60. while not linkstaticlibs.empty do
  61. Linker.AddStaticLibrary(linkstaticlibs.Get);
  62. end;
  63. end;
  64. procedure insertsegment;
  65. begin
  66. {Insert Ident of the compiler}
  67. if not smartlink then
  68. begin
  69. datasegment^.insert(new(pai_align,init(4)));
  70. datasegment^.insert(new(pai_string,init('FPC '+version_string+' - '+target_info.short_name)));
  71. end;
  72. bsssegment^.insert(new(pai_section,init(sec_bss)));
  73. codesegment^.insert(new(pai_section,init(sec_code)));
  74. datasegment^.insert(new(pai_section,init(sec_data)));
  75. end;
  76. procedure insertheap;
  77. begin
  78. if smartlink then
  79. begin
  80. bsssegment^.concat(new(pai_cut,init));
  81. datasegment^.concat(new(pai_cut,init));
  82. end;
  83. { On the Macintosh Classic M68k Architecture
  84. The Heap variable is simply a POINTER to the
  85. real HEAP. The HEAP must be set up by the RTL
  86. and must store the pointer in this value.
  87. On OS/2 the heap is also intialized by the RTL. We do
  88. not output a pointer }
  89. case target_info.target of
  90. target_OS2 : ;
  91. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  92. else
  93. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  94. end;
  95. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  96. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  97. end;
  98. procedure inserttargetspecific;
  99. var
  100. i : longint;
  101. begin
  102. case target_info.target of
  103. target_GO32V2 : begin
  104. { stacksize can be specified }
  105. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  106. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  107. end;
  108. target_WIN32 : begin
  109. { generate the last entry for the imports directory }
  110. if not(assigned(importssection)) then
  111. importssection:=new(paasmoutput,init);
  112. { $3 ensure that it is the last entry, all other entries }
  113. { are written to $2 }
  114. importssection^.concat(new(pai_section,init_idata(3)));
  115. for i:=1 to 5 do
  116. importssection^.concat(new(pai_const,init_32bit(0)));
  117. end;
  118. end;
  119. end;
  120. { all intern procedures for system unit }
  121. procedure insertinternsyms(p : psymtable);
  122. begin
  123. p^.insert(new(psyssym,init('CONCAT',in_concat_x)));
  124. p^.insert(new(psyssym,init('WRITE',in_write_x)));
  125. p^.insert(new(psyssym,init('WRITELN',in_writeln_x)));
  126. p^.insert(new(psyssym,init('ASSIGNED',in_assigned_x)));
  127. p^.insert(new(psyssym,init('READ',in_read_x)));
  128. p^.insert(new(psyssym,init('READLN',in_readln_x)));
  129. p^.insert(new(psyssym,init('OFS',in_ofs_x)));
  130. p^.insert(new(psyssym,init('SIZEOF',in_sizeof_x)));
  131. p^.insert(new(psyssym,init('TYPEOF',in_typeof_x)));
  132. p^.insert(new(psyssym,init('LOW',in_low_x)));
  133. p^.insert(new(psyssym,init('HIGH',in_high_x)));
  134. p^.insert(new(psyssym,init('SEG',in_seg_x)));
  135. p^.insert(new(psyssym,init('ORD',in_ord_x)));
  136. p^.insert(new(psyssym,init('PRED',in_pred_x)));
  137. p^.insert(new(psyssym,init('SUCC',in_succ_x)));
  138. p^.insert(new(psyssym,init('EXCLUDE',in_exclude_x_y)));
  139. p^.insert(new(psyssym,init('INCLUDE',in_include_x_y)));
  140. p^.insert(new(psyssym,init('BREAK',in_break)));
  141. p^.insert(new(psyssym,init('CONTINUE',in_continue)));
  142. { for testing purpose }
  143. p^.insert(new(psyssym,init('DECI',in_dec_x)));
  144. p^.insert(new(psyssym,init('INCI',in_inc_x)));
  145. p^.insert(new(psyssym,init('STR',in_str_x_string)));
  146. end;
  147. { all the types inserted into the system unit }
  148. procedure insert_intern_types(p : psymtable);
  149. {$ifdef GDB}
  150. var
  151. { several defs to simulate more or less C++ objects for GDB }
  152. vmtdef : precdef;
  153. pvmtdef : ppointerdef;
  154. vmtarraydef : parraydef;
  155. vmtsymtable : psymtable;
  156. {$endif GDB}
  157. begin
  158. p^.insert(new(ptypesym,init('longint',s32bitdef)));
  159. p^.insert(new(ptypesym,init('ulong',u32bitdef)));
  160. p^.insert(new(ptypesym,init('void',voiddef)));
  161. p^.insert(new(ptypesym,init('char',cchardef)));
  162. {$ifdef i386}
  163. p^.insert(new(ptypesym,init('s64real',c64floatdef)));
  164. {$endif i386}
  165. p^.insert(new(ptypesym,init('s80real',s80floatdef)));
  166. p^.insert(new(ptypesym,init('cs32fixed',s32fixeddef)));
  167. p^.insert(new(ptypesym,init('byte',u8bitdef)));
  168. p^.insert(new(ptypesym,init('string',cstringdef)));
  169. p^.insert(new(ptypesym,init('longstring',clongstringdef)));
  170. p^.insert(new(ptypesym,init('ansistring',cansistringdef)));
  171. p^.insert(new(ptypesym,init('widestring',cwidestringdef)));
  172. p^.insert(new(ptypesym,init('word',u16bitdef)));
  173. p^.insert(new(ptypesym,init('boolean',booldef)));
  174. p^.insert(new(ptypesym,init('void_pointer',voidpointerdef)));
  175. p^.insert(new(ptypesym,init('file',cfiledef)));
  176. {$ifdef i386}
  177. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s64real)))));
  178. p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s64bit)))));
  179. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  180. {$endif}
  181. {$ifdef m68k}
  182. { internal definitions }
  183. p^.insert(new(ptypesym,init('s32real',c64floatdef)));
  184. { mappings... }
  185. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s32real)))));
  186. if (cs_fp_emulation) in aktswitches then
  187. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s32real)))))
  188. else
  189. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s64real)))));
  190. { p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s32real)))));}
  191. if (cs_fp_emulation) in aktswitches then
  192. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s32real)))))
  193. else
  194. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  195. {$endif}
  196. p^.insert(new(ptypesym,init('SINGLE',new(pfloatdef,init(s32real)))));
  197. p^.insert(new(ptypesym,init('POINTER',new(ppointerdef,init(voiddef)))));
  198. p^.insert(new(ptypesym,init('STRING',cstringdef)));
  199. p^.insert(new(ptypesym,init('LONGSTRING',clongstringdef)));
  200. p^.insert(new(ptypesym,init('ANSISTRING',cansistringdef)));
  201. p^.insert(new(ptypesym,init('WIDESTRING',cwidestringdef)));
  202. p^.insert(new(ptypesym,init('BOOLEAN',new(porddef,init(bool8bit,0,1)))));
  203. p^.insert(new(ptypesym,init('CHAR',new(porddef,init(uchar,0,255)))));
  204. p^.insert(new(ptypesym,init('TEXT',new(pfiledef,init(ft_text,nil)))));
  205. p^.insert(new(ptypesym,init('CARDINAL',new(porddef,init(u32bit,0,$ffffffff)))));
  206. p^.insert(new(ptypesym,init('FIXED',new(pfloatdef,init(f32bit)))));
  207. p^.insert(new(ptypesym,init('FIXED16',new(pfloatdef,init(f16bit)))));
  208. p^.insert(new(ptypesym,init('TYPEDFILE',new(pfiledef,init(ft_typed,voiddef)))));
  209. { !!!!!
  210. p^.insert(new(ptypesym,init('COMP',new(porddef,init(s64bit,0,0)))));
  211. p^.insert(new(ptypesym,init('SINGLE',new(porddef,init(s32real,0,0)))));
  212. p^.insert(new(ptypesym,init('EXTENDED',new(porddef,init(s80real,0,0)))));
  213. p^.insert(new(ptypesym,init('FILE',new(pfiledef,init(ft_untyped,nil)))));
  214. }
  215. { Add a type for virtual method tables in lowercase }
  216. { so it isn't reachable! }
  217. {$ifdef GDB}
  218. vmtsymtable:=new(psymtable,init(recordsymtable));
  219. vmtdef:=new(precdef,init(vmtsymtable));
  220. pvmtdef:=new(ppointerdef,init(vmtdef));
  221. vmtsymtable^.insert(new(pvarsym,init('parent',pvmtdef)));
  222. vmtsymtable^.insert(new(pvarsym,init('length',globaldef('longint'))));
  223. vmtsymtable^.insert(new(pvarsym,init('mlength',globaldef('longint'))));
  224. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  225. vmtarraydef^.definition := voidpointerdef;
  226. vmtsymtable^.insert(new(pvarsym,init('__pfn',vmtarraydef)));
  227. p^.insert(new(ptypesym,init('__vtbl_ptr_type',vmtdef)));
  228. p^.insert(new(ptypesym,init('pvmt',pvmtdef)));
  229. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  230. vmtarraydef^.definition := pvmtdef;
  231. p^.insert(new(ptypesym,init('vtblarray',vmtarraydef)));
  232. {$endif GDB}
  233. insertinternsyms(p);
  234. end;
  235. procedure load_ppu(hp : pmodule;compile_system : boolean);
  236. var
  237. loaded_unit : pmodule;
  238. b : byte;
  239. checksum,
  240. count,
  241. nextmapentry : longint;
  242. hs : string;
  243. begin
  244. { init the map }
  245. new(hp^.map);
  246. nextmapentry:=1;
  247. { load the used units from interface }
  248. hp^.ppufile^.read_data(b,1,count);
  249. while (b=ibloadunit) do
  250. begin
  251. { read unit name }
  252. hp^.ppufile^.read_data(hs[0],1,count);
  253. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  254. hp^.ppufile^.read_data(checksum,4,count);
  255. loaded_unit:=loadunit(hs,false,false);
  256. if hp^.compiled then
  257. exit;
  258. { if the crc of a used unit is the same as }
  259. { written to the PPU file, we needn't to }
  260. { recompile the current unit }
  261. if (loaded_unit^.crc<>checksum) then
  262. begin
  263. { we have to compile the current unit }
  264. { remove stuff which isn't needed }
  265. { forget the map }
  266. dispose(hp^.map);
  267. hp^.map:=nil;
  268. hp^.ppufile^.close;
  269. dispose(hp^.ppufile,done);
  270. hp^.ppufile:=nil;
  271. if not(hp^.sources_avail) then
  272. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  273. else
  274. compile(hp^.mainsource^,compile_system);
  275. exit;
  276. end;
  277. { setup the map entry for deref }
  278. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  279. inc(nextmapentry);
  280. if nextmapentry>maxunits then
  281. Message(unit_f_too_much_units);
  282. { read until ibend }
  283. hp^.ppufile^.read_data(b,1,count);
  284. end;
  285. { ok, now load the unit }
  286. hp^.symtable:=new(punitsymtable,load(hp^.unitname^));
  287. { if this is the system unit insert the intern }
  288. { symbols }
  289. if compile_system then
  290. insertinternsyms(psymtable(hp^.symtable));
  291. { now only read the implementation part }
  292. hp^.in_implementation:=true;
  293. { load the used units from implementation }
  294. hp^.ppufile^.read_data(b,1,count);
  295. while (b<>ibend) and (b=ibloadunit) do
  296. begin
  297. { read unit name }
  298. hp^.ppufile^.read_data(hs[0],1,count);
  299. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  300. hp^.ppufile^.read_data(checksum,4,count);
  301. loaded_unit:=loadunit(hs,false,false);
  302. if hp^.compiled then exit;
  303. { if the crc of a used unit is the same as }
  304. { written to the PPU file, we needn't to }
  305. { recompile the current unit }
  306. { but for the implementation part }
  307. { the written crc is false, because }
  308. { not defined when writing the ppufile !! }
  309. (* if {(loaded_unit^.crc<>checksum) or}
  310. (do_build and loaded_unit^.sources_avail) then
  311. begin
  312. { we have to compile the current unit }
  313. { remove stuff which isn't needed }
  314. { forget the map }
  315. dispose(hp^.map);
  316. hp^.map:=nil;
  317. hp^.ppufile^.close;
  318. dispose(hp^.ppufile,done);
  319. hp^.ppufile:=nil;
  320. if not(hp^.sources_avail) then
  321. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  322. else
  323. compile(hp^.mainsource^,compile_system);
  324. exit;
  325. end; *)
  326. { read until ibend }
  327. hp^.ppufile^.read_data(b,1,count);
  328. end;
  329. hp^.ppufile^.close;
  330. dispose(hp^.map);
  331. hp^.map:=nil;
  332. end;
  333. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  334. var
  335. st : punitsymtable;
  336. old_current_module,hp,nextmodule : pmodule;
  337. pu : pused_unit;
  338. hs : pstring;
  339. begin
  340. old_current_module:=current_module;
  341. { be sure not to mix lines from different files }
  342. { update_line; }
  343. { unit not found }
  344. st:=nil;
  345. { search all loaded units }
  346. hp:=pmodule(loaded_units.first);
  347. while assigned(hp) do
  348. begin
  349. if hp^.unitname^=s then
  350. begin
  351. { the unit is already registered }
  352. { and this means that the unit }
  353. { is already compiled }
  354. { else there is a cyclic unit use }
  355. if assigned(hp^.symtable) then
  356. st:=punitsymtable(hp^.symtable)
  357. else
  358. begin
  359. { recompile the unit ? }
  360. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  361. Message(unit_f_circular_unit_reference);
  362. end;
  363. break;
  364. end;
  365. { the next unit }
  366. hp:=pmodule(hp^.next);
  367. end;
  368. { no error and the unit isn't loaded }
  369. if not(assigned(hp)) and (st=nil) then
  370. begin
  371. { generates a new unit info record }
  372. hp:=new(pmodule,init(s,true));
  373. { now we can register the unit }
  374. loaded_units.insert(hp);
  375. current_module:=hp;
  376. { force build ? }
  377. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  378. begin
  379. { we needn't the ppufile }
  380. if assigned(hp^.ppufile) then
  381. begin
  382. dispose(hp^.ppufile,done);
  383. hp^.ppufile:=nil;
  384. end;
  385. if not(hp^.sources_avail) then
  386. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  387. else
  388. compile(hp^.mainsource^,compile_system);
  389. end
  390. else
  391. begin
  392. { only reassemble ? }
  393. if (hp^.do_assemble) then
  394. OnlyAsm(hp^.asmfilename^);
  395. { we should know there the PPU file else it's an error and
  396. we can't load the unit }
  397. if hp^.ppufile^.name^<>'' then
  398. load_ppu(hp,compile_system);
  399. { add the files for the linker }
  400. addlinkerfiles(hp);
  401. end;
  402. { register the unit _once_ }
  403. usedunits.concat(new(pused_unit,init(hp,0)));
  404. { the unit is written, so we can set the symtable type }
  405. { to unitsymtable, else we get some dupid errors }
  406. { this is not the right place because of the }
  407. { ready label }
  408. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  409. { placed at this end of proc_unit }
  410. psymtable(hp^.symtable)^.unitid:=0;
  411. { reset the unitnumbers for the other units }
  412. pu:=pused_unit(old_current_module^.used_units.first);
  413. while assigned(pu) do
  414. begin
  415. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  416. pu:=pused_unit(pu^.next);
  417. end;
  418. end
  419. else
  420. if assigned(hp) and (st=nil) then
  421. begin
  422. { we have to compile the unit again, but it is already inserted !!}
  423. { we may have problem with the lost symtable !! }
  424. current_module:=hp;
  425. { we must preserve the unit chain }
  426. nextmodule:=pmodule(hp^.next);
  427. { we have to cleanup a little }
  428. hp^.special_done;
  429. new(hs);
  430. hs^:=hp^.mainsource^;
  431. hp^.init(hs^,true);
  432. dispose(hs);
  433. { we must preserve the unit chain }
  434. hp^.next:=nextmodule;
  435. if assigned(hp^.ppufile) then
  436. load_ppu(hp,compile_system)
  437. else
  438. begin
  439. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  440. compile(hp^.mainsource^,compile_system);
  441. end;
  442. current_module^.compiled:=true;
  443. end;
  444. { set the old module }
  445. current_module:=old_current_module;
  446. { the current module uses the unit hp }
  447. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  448. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  449. if in_uses and not current_module^.in_implementation then
  450. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  451. loadunit:=hp;
  452. end;
  453. procedure loadunits;
  454. var
  455. s : stringid;
  456. hp : pused_unit;
  457. hp2 : pmodule;
  458. hp3 : psymtable;
  459. oldprocsym:Pprocsym;
  460. begin
  461. oldprocsym:=aktprocsym;
  462. consume(_USES);
  463. {$ifdef DEBUG}
  464. test_symtablestack;
  465. {$endif DEBUG}
  466. repeat
  467. s:=pattern;
  468. consume(ID);
  469. hp2:=loadunit(s,false,true);
  470. if current_module^.compiled then
  471. exit;
  472. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  473. if token=COMMA then
  474. begin
  475. pattern:='';
  476. consume(COMMA);
  477. end
  478. else
  479. break;
  480. until false;
  481. consume(SEMICOLON);
  482. { now insert the units in the symtablestack }
  483. hp:=pused_unit(current_module^.used_units.first);
  484. { set the symtable to systemunit so it gets reorderd correctly }
  485. symtablestack:=systemunit;
  486. while assigned(hp) do
  487. begin
  488. {$IfDef GDB}
  489. if (cs_debuginfo in aktswitches) and
  490. not hp^.is_stab_written then
  491. begin
  492. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  493. hp^.is_stab_written:=true;
  494. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  495. end;
  496. {$EndIf GDB}
  497. if hp^.in_uses then
  498. begin
  499. hp3:=symtablestack;
  500. while assigned(hp3) do
  501. begin
  502. { insert units only once ! }
  503. if hp^.u^.symtable=hp3 then
  504. break;
  505. hp3:=hp3^.next;
  506. { unit isn't inserted }
  507. if hp3=nil then
  508. begin
  509. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  510. symtablestack:=psymtable(hp^.u^.symtable);
  511. {$ifdef CHAINPROCSYMS}
  512. symtablestack^.chainprocsyms;
  513. {$endif CHAINPROCSYMS}
  514. {$ifdef DEBUG}
  515. test_symtablestack;
  516. {$endif DEBUG}
  517. end;
  518. end;
  519. end;
  520. hp:=pused_unit(hp^.next);
  521. end;
  522. aktprocsym:=oldprocsym;
  523. end;
  524. procedure parse_implementation_uses(symt:Psymtable);
  525. var
  526. old_module_in_implementation : boolean;
  527. begin
  528. if token=_USES then
  529. begin
  530. old_module_in_implementation:=module_in_implementation;
  531. module_in_implementation:=true;
  532. current_module^.in_implementation:=true;
  533. symt^.symtabletype:=unitsymtable;
  534. loadunits;
  535. symt^.symtabletype:=globalsymtable;
  536. {$ifdef DEBUG}
  537. test_symtablestack;
  538. {$endif DEBUG}
  539. module_in_implementation:=old_module_in_implementation;
  540. end;
  541. end;
  542. procedure proc_unit;
  543. var
  544. { unitname : stringid; }
  545. names:Tstringcontainer;
  546. p : psymtable;
  547. unitst : punitsymtable;
  548. pu : pused_unit;
  549. s1,s2 : ^string; {Saves stack space}
  550. begin
  551. consume(_UNIT);
  552. if token=ID then
  553. begin
  554. { create filenames and unit name }
  555. current_module^.SetFileName(current_module^.current_inputfile^.path^,current_module^.current_inputfile^.name^);
  556. current_module^.unitname:=stringdup(upper(pattern));
  557. { check for system unit }
  558. new(s1);
  559. new(s2);
  560. s1^:=upper(target_info.system_unit);
  561. s2^:=upper(current_module^.current_inputfile^.name^);
  562. if (cs_compilesystem in aktswitches) then
  563. begin
  564. if (cs_check_unit_name in aktswitches) and
  565. ((length(current_module^.unitname^)>8) or
  566. (current_module^.unitname^<>s1^) or
  567. (current_module^.unitname^<>s2^)) then
  568. Message1(unit_e_illegal_unit_name,s1^);
  569. end
  570. else
  571. if (current_module^.unitname^=s1^) then
  572. Message(unit_w_switch_us_missed);
  573. dispose(s2);
  574. dispose(s1);
  575. { Add Object File }
  576. if smartlink then
  577. current_module^.linkstaticlibs.insert(current_module^.arfilename^)
  578. else
  579. current_module^.linkofiles.insert(current_module^.objfilename^);
  580. end;
  581. consume(ID);
  582. consume(SEMICOLON);
  583. consume(_INTERFACE);
  584. { this should be placed after uses !!}
  585. {$ifndef UseNiceNames}
  586. procprefix:='_'+current_module^.unitname^+'$$';
  587. {$else UseNiceNames}
  588. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  589. {$endif UseNiceNames}
  590. parse_only:=true;
  591. { generate now the global symboltable }
  592. p:=new(punitsymtable,init(globalsymtable,current_module^.unitname^));
  593. refsymtable:=p;
  594. unitst:=punitsymtable(p);
  595. { the unit name must be usable as a unit specifier }
  596. { inside the unit itself (PM) }
  597. { this also forbids to have another symbol }
  598. { with the same name as the unit }
  599. refsymtable^.insert(new(punitsym,init(current_module^.unitname^,unitst)));
  600. { set the symbol table for the current unit }
  601. { this must be set later for interdependency }
  602. { current_module^.symtable:=psymtable(p); }
  603. { a unit compiled at command line must be inside the loaded_unit list }
  604. if (compile_level=1) then
  605. begin
  606. loaded_units.insert(current_module);
  607. if cs_unit_to_lib in initswitches then
  608. begin
  609. current_module^.flags:=current_module^.flags or uf_in_library;
  610. if cs_shared_lib in initswitches then
  611. current_module^.flags:=current_module^.flags or uf_shared_library;
  612. end;
  613. end;
  614. { insert qualifier for the system unit (allows system.writeln) }
  615. if not(cs_compilesystem in aktswitches) then
  616. begin
  617. { insert the system unit }
  618. { it is allways the first }
  619. systemunit^.next:=nil;
  620. symtablestack:=systemunit;
  621. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  622. if token=_USES then
  623. begin
  624. unitst^.symtabletype:=unitsymtable;
  625. loadunits;
  626. { has it been compiled at a higher level ?}
  627. if current_module^.compiled then
  628. exit;
  629. unitst^.symtabletype:=globalsymtable;
  630. end;
  631. { ... but insert the symbol table later }
  632. p^.next:=symtablestack;
  633. symtablestack:=p;
  634. end
  635. else
  636. { while compiling a system unit, some types are directly inserted }
  637. begin
  638. p^.next:=symtablestack;
  639. symtablestack:=p;
  640. insert_intern_types(p);
  641. end;
  642. { displaced for inter-dependency considerations }
  643. current_module^.symtable:=psymtable(p);
  644. constsymtable:=symtablestack;
  645. { ... parse the declarations }
  646. read_interface_declarations;
  647. consume(_IMPLEMENTATION);
  648. parse_only:=false;
  649. refsymtable^.number_defs;
  650. {$ifdef GDB}
  651. { add all used definitions even for implementation}
  652. if (cs_debuginfo in aktswitches) then
  653. begin
  654. { all types }
  655. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  656. { and all local symbols}
  657. refsymtable^.concatstabto(debuglist);
  658. end;
  659. {$endif GDB}
  660. { for interdependent units
  661. the crc is included in the ppufile
  662. but it is not known when writing the first ppufile
  663. so I tried to add a fake writing of the ppu
  664. just to get the CRC
  665. but the result is different for the real CRC
  666. it calculates after, I don't know why
  667. Answer:
  668. -------
  669. When reading the interface part, the compiler assumes
  670. that all registers are modified by a procedure
  671. usedinproc:=$ff !
  672. If the definition is read, the compiler determines
  673. the used registers and write the correct value
  674. to usedinproc
  675. only_calculate_crc:=true;
  676. writeunitas(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^+
  677. +'.PPS',punitsymtable(symtablestack));
  678. only_calculate_crc:=false;
  679. }
  680. { generates static symbol table }
  681. p:=new(punitsymtable,init(staticsymtable,current_module^.unitname^));
  682. refsymtable:=p;
  683. {Generate a procsym.}
  684. aktprocsym:=new(Pprocsym,init(current_module^.unitname^+'_init'));
  685. aktprocsym^.definition:=new(Pprocdef,init);
  686. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  687. aktprocsym^.definition^.setmangledname(current_module^.unitname^+'_init');
  688. {The generated procsym has a local symtable. Discard it and turn
  689. it into the static one.}
  690. dispose(aktprocsym^.definition^.localst,done);
  691. aktprocsym^.definition^.localst:=p;
  692. { testing !!!!!!!!! }
  693. { we set the interface part as a unitsymtable }
  694. { for the case we need to compile another unit }
  695. { remove the globalsymtable from the symtable stack }
  696. { to reinsert it after loading the implementation units }
  697. symtablestack:=unitst^.next;
  698. parse_implementation_uses(unitst);
  699. { but reinsert the global symtable as lasts }
  700. unitst^.next:=symtablestack;
  701. symtablestack:=unitst;
  702. {$ifdef DEBUG}
  703. test_symtablestack;
  704. {$endif DEBUG}
  705. constsymtable:=symtablestack;
  706. {$ifdef Splitheap}
  707. if testsplit then
  708. begin
  709. Split_Heap;
  710. allow_special:=true;
  711. Switch_to_temp_heap;
  712. end;
  713. { it will report all crossings }
  714. allow_special:=false;
  715. {$endif Splitheap}
  716. { set some informations }
  717. procinfo.retdef:=voiddef;
  718. procinfo._class:=nil;
  719. procinfo.call_offset:=8;
  720. { for temporary values }
  721. procinfo.framepointer:=frame_pointer;
  722. { clear flags }
  723. procinfo.flags:=0;
  724. {Reset the codegenerator.}
  725. codegen_newprocedure;
  726. names.init;
  727. names.insert(current_module^.unitname^+'_init');
  728. names.insert('INIT$$'+current_module^.unitname^);
  729. compile_proc_body(names,true,false);
  730. names.done;
  731. codegen_doneprocedure;
  732. consume(POINT);
  733. { size of the static data }
  734. datasize:=symtablestack^.datasize;
  735. { unsed static symbols ? }
  736. symtablestack^.allsymbolsused;
  737. {$ifdef GDB}
  738. { add all used definitions even for implementation}
  739. if (cs_debuginfo in aktswitches) then
  740. begin
  741. { all types }
  742. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  743. { and all local symbols}
  744. symtablestack^.concatstabto(debuglist);
  745. end;
  746. {$endif GDB}
  747. current_module^.in_implementation:=false;
  748. { deletes all symtables generated in the implementation part }
  749. while symtablestack^.symtabletype<>globalsymtable do
  750. dellexlevel;
  751. { tests, if all forwards are resolved }
  752. symtablestack^.check_forwards;
  753. symtablestack^.symtabletype:=unitsymtable;
  754. punitsymtable(symtablestack)^.is_stab_written:=false;
  755. {Write out the unit if the compile was succesfull.}
  756. if errorcount=0 then
  757. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  758. pu:=pused_unit(usedunits.first);
  759. while assigned(pu) do
  760. begin
  761. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  762. pu:=pused_unit(pu^.next);
  763. end;
  764. inc(datasize,symtablestack^.datasize);
  765. { finish asmlist by adding segment starts }
  766. insertsegment;
  767. end;
  768. procedure proc_program(islibrary : boolean);
  769. var
  770. st : psymtable;
  771. programname : stringid;
  772. names:Tstringcontainer;
  773. begin
  774. { Trying to compile the system unit... }
  775. { if no unit defined... then issue a }
  776. { fatal error (avoids pointer problems)}
  777. { when referencing the non-existant }
  778. { system unit. }
  779. { System Unit should be compiled using proc_unit !! (PFV) }
  780. { if (cs_compilesystem in aktswitches) then
  781. Begin
  782. if token<>_UNIT then
  783. Message1(scan_f_syn_expected,'UNIT');
  784. consume(_UNIT);
  785. end;}
  786. parse_only:=false;
  787. programname:='';
  788. if islibrary then
  789. begin
  790. consume(_LIBRARY);
  791. programname:=pattern;
  792. consume(ID);
  793. consume(SEMICOLON);
  794. end
  795. else
  796. { is there an program head ? }
  797. if token=_PROGRAM then
  798. begin
  799. consume(_PROGRAM);
  800. programname:=pattern;
  801. consume(ID);
  802. if token=LKLAMMER then
  803. begin
  804. consume(LKLAMMER);
  805. idlist;
  806. consume(RKLAMMER);
  807. end;
  808. consume(SEMICOLON);
  809. end;
  810. { insert after the unit symbol tables the static symbol table }
  811. { of the program }
  812. st:=new(punitsymtable,init(staticsymtable,programname));
  813. {Generate a procsym.}
  814. aktprocsym:=new(Pprocsym,init('main'));
  815. aktprocsym^.definition:=new(Pprocdef,init);
  816. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  817. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  818. {The localst is a local symtable. Change it into the static
  819. symtable.}
  820. dispose(aktprocsym^.definition^.localst,done);
  821. aktprocsym^.definition^.localst:=st;
  822. refsymtable:=st;
  823. {Insert the symbols of the system unit into the stack of symbol
  824. tables.}
  825. symtablestack:=systemunit;
  826. systemunit^.next:=nil;
  827. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  828. {Load the units used by the program we compile.}
  829. if token=_USES then
  830. loadunits;
  831. {Insert the name of the main program into the symbol table.}
  832. if programname<>'' then
  833. st^.insert(new(pprogramsym,init(programname)));
  834. { ...is also constsymtable, this is the symtable where }
  835. { the elements of enumeration types are inserted }
  836. constsymtable:=st;
  837. { set some informations about the main program }
  838. procinfo.retdef:=voiddef;
  839. procinfo._class:=nil;
  840. procinfo.call_offset:=8;
  841. {Set the framepointer of the program initialization to the
  842. default framepointer (EBP on i386).}
  843. procinfo.framepointer:=frame_pointer;
  844. { clear flags }
  845. procinfo.flags:=0;
  846. procprefix:='';
  847. in_except_block:=false;
  848. codegen_newprocedure;
  849. {The program intialization needs an alias, so it can be called
  850. from the bootstrap code.}
  851. names.init;
  852. names.insert('program_init');
  853. names.insert('PASCALMAIN');
  854. names.insert(target_os.cprefix+'main');
  855. compile_proc_body(names,true,false);
  856. names.done;
  857. codegen_doneprocedure;
  858. consume(POINT);
  859. if smartlink then
  860. current_module^.linkstaticlibs.insert(current_module^.arfilename^)
  861. else
  862. current_module^.linkofiles.insert(current_module^.objfilename^);
  863. insertheap;
  864. inserttargetspecific;
  865. datasize:=symtablestack^.datasize;
  866. symtablestack^.check_forwards;
  867. symtablestack^.allsymbolsused;
  868. { finish asmlist by adding segment starts }
  869. insertsegment;
  870. end;
  871. end.
  872. {
  873. $Log$
  874. Revision 1.11 1998-05-06 18:36:53 peter
  875. * tai_section extended with code,data,bss sections and enumerated type
  876. * ident 'compiled by FPC' moved to pmodules
  877. * small fix for smartlink
  878. Revision 1.10 1998/05/04 17:54:28 peter
  879. + smartlinking works (only case jumptable left todo)
  880. * redesign of systems.pas to support assemblers and linkers
  881. + Unitname is now also in the PPU-file, increased version to 14
  882. Revision 1.9 1998/05/01 16:38:45 florian
  883. * handling of private and protected fixed
  884. + change_keywords_to_tp implemented to remove
  885. keywords which aren't supported by tp
  886. * break and continue are now symbols of the system unit
  887. + widestring, longstring and ansistring type released
  888. Revision 1.8 1998/04/30 15:59:41 pierre
  889. * GDB works again better :
  890. correct type info in one pass
  891. + UseTokenInfo for better source position
  892. * fixed one remaining bug in scanner for line counts
  893. * several little fixes
  894. Revision 1.7 1998/04/29 10:33:59 pierre
  895. + added some code for ansistring (not complete nor working yet)
  896. * corrected operator overloading
  897. * corrected nasm output
  898. + started inline procedures
  899. + added starstarn : use ** for exponentiation (^ gave problems)
  900. + started UseTokenInfo cond to get accurate positions
  901. Revision 1.6 1998/04/27 23:10:28 peter
  902. + new scanner
  903. * $makelib -> if smartlink
  904. * small filename fixes pmodule.setfilename
  905. * moved import from files.pas -> import.pas
  906. Revision 1.5 1998/04/14 23:27:03 florian
  907. + exclude/include with constant second parameter added
  908. Revision 1.4 1998/04/10 14:41:43 peter
  909. * removed some Hints
  910. * small speed optimization for AsmLn
  911. Revision 1.3 1998/04/03 09:51:00 daniel
  912. * Fixed heap allocation for OS/2.
  913. }