pmodules.pas 41 KB

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