2
0

pmodules.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  45. procedure proc_unit;
  46. procedure proc_program(islibrary : boolean);
  47. implementation
  48. uses
  49. parser;
  50. {$I innr.inc}
  51. procedure insertinternsyms(p : psymtable);
  52. begin
  53. p^.insert(new(psyssym,init('CONCAT',in_concat_x)));
  54. p^.insert(new(psyssym,init('WRITE',in_write_x)));
  55. p^.insert(new(psyssym,init('WRITELN',in_writeln_x)));
  56. p^.insert(new(psyssym,init('ASSIGNED',in_assigned_x)));
  57. p^.insert(new(psyssym,init('READ',in_read_x)));
  58. p^.insert(new(psyssym,init('READLN',in_readln_x)));
  59. p^.insert(new(psyssym,init('OFS',in_ofs_x)));
  60. p^.insert(new(psyssym,init('SIZEOF',in_sizeof_x)));
  61. p^.insert(new(psyssym,init('TYPEOF',in_typeof_x)));
  62. p^.insert(new(psyssym,init('LOW',in_low_x)));
  63. p^.insert(new(psyssym,init('HIGH',in_high_x)));
  64. p^.insert(new(psyssym,init('SEG',in_seg_x)));
  65. p^.insert(new(psyssym,init('ORD',in_ord_x)));
  66. p^.insert(new(psyssym,init('PRED',in_pred_x)));
  67. p^.insert(new(psyssym,init('SUCC',in_succ_x)));
  68. { for testing purpose }
  69. p^.insert(new(psyssym,init('DECI',in_dec_x)));
  70. p^.insert(new(psyssym,init('INCI',in_inc_x)));
  71. p^.insert(new(psyssym,init('STR',in_str_x_string)));
  72. end;
  73. procedure load_ppu(hp : pmodule;compile_system : boolean);
  74. var
  75. loaded_unit : pmodule;
  76. b : byte;
  77. checksum,
  78. count,
  79. nextmapentry : longint;
  80. hs : string;
  81. begin
  82. { init the map }
  83. new(hp^.map);
  84. nextmapentry:=1;
  85. { load the used units from interface }
  86. hp^.ppufile^.read_data(b,1,count);
  87. while (b=ibloadunit) do
  88. begin
  89. { read unit name }
  90. hp^.ppufile^.read_data(hs[0],1,count);
  91. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  92. hp^.ppufile^.read_data(checksum,4,count);
  93. loaded_unit:=loadunit(hs,false,false);
  94. if hp^.compiled then
  95. exit;
  96. { if the crc of a used unit is the same as }
  97. { written to the PPU file, we needn't to }
  98. { recompile the current unit }
  99. if (loaded_unit^.crc<>checksum) or
  100. (do_build and loaded_unit^.sources_avail) then
  101. begin
  102. { we have to compile the current unit }
  103. { remove stuff which isn't needed }
  104. { forget the map }
  105. dispose(hp^.map);
  106. hp^.map:=nil;
  107. hp^.ppufile^.close;
  108. dispose(hp^.ppufile,done);
  109. hp^.ppufile:=nil;
  110. compile(hp^.mainsource^,compile_system);
  111. exit;
  112. end;
  113. { setup the map entry for deref }
  114. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  115. inc(nextmapentry);
  116. if nextmapentry>maxunits then
  117. Message(unit_f_too_much_units);
  118. { read until ibend }
  119. hp^.ppufile^.read_data(b,1,count);
  120. end;
  121. { ok, now load the unit }
  122. hp^.symtable:=new(punitsymtable,load(hp^.unitname^));
  123. { if this is the system unit insert the intern }
  124. { symbols }
  125. if compile_system then
  126. insertinternsyms(psymtable(hp^.symtable));
  127. { now only read the implementation part }
  128. hp^.in_implementation:=true;
  129. { load the used units from implementation }
  130. hp^.ppufile^.read_data(b,1,count);
  131. while (b<>ibend) and (b=ibloadunit) do
  132. begin
  133. { read unit name }
  134. hp^.ppufile^.read_data(hs[0],1,count);
  135. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  136. hp^.ppufile^.read_data(checksum,4,count);
  137. loaded_unit:=loadunit(hs,false,false);
  138. if hp^.compiled then exit;
  139. { if the crc of a used unit is the same as }
  140. { written to the PPU file, we needn't to }
  141. { recompile the current unit }
  142. { but for the implementation part }
  143. { the written crc is false, because }
  144. { not defined when writing the ppufile !! }
  145. if {(loaded_unit^.crc<>checksum) or}
  146. (do_build and loaded_unit^.sources_avail) then
  147. begin
  148. { we have to compile the current unit }
  149. { remove stuff which isn't needed }
  150. { forget the map }
  151. dispose(hp^.map);
  152. hp^.map:=nil;
  153. hp^.ppufile^.close;
  154. dispose(hp^.ppufile,done);
  155. hp^.ppufile:=nil;
  156. compile(hp^.mainsource^,compile_system);
  157. exit;
  158. end;
  159. { read until ibend }
  160. hp^.ppufile^.read_data(b,1,count);
  161. end;
  162. hp^.ppufile^.close;
  163. dispose(hp^.map);
  164. hp^.map:=nil;
  165. end;
  166. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  167. var
  168. st : punitsymtable;
  169. old_current_module,hp,nextmodule : pmodule;
  170. pu : pused_unit;
  171. a : pasmfile;
  172. hs : pstring;
  173. i : longint;
  174. begin
  175. old_current_module:=current_module;
  176. { be sure not to mix lines from different files }
  177. { update_line; }
  178. { unit not found }
  179. st:=nil;
  180. { search all loaded units }
  181. hp:=pmodule(loaded_units.first);
  182. while assigned(hp) do
  183. begin
  184. if hp^.unitname^=s then
  185. begin
  186. { the unit is already registered }
  187. { and this means that the unit }
  188. { is already compiled }
  189. { else there is a cyclic unit use }
  190. if assigned(hp^.symtable) then
  191. st:=punitsymtable(hp^.symtable)
  192. else
  193. begin
  194. { recompile the unit ? }
  195. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  196. Message(unit_f_circular_unit_reference);
  197. end;
  198. break;
  199. end;
  200. { the next unit }
  201. hp:=pmodule(hp^.next);
  202. end;
  203. { no error and the unit isn't loaded }
  204. if not(assigned(hp)) and (st=nil) then
  205. begin
  206. { generates a new unit info record }
  207. hp:=new(pmodule,init(s,true));
  208. { now we can register the unit }
  209. loaded_units.insert(hp);
  210. current_module:=hp;
  211. { force build ? }
  212. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  213. begin
  214. { we needn't the ppufile }
  215. if assigned(hp^.ppufile) then
  216. begin
  217. dispose(hp^.ppufile,done);
  218. hp^.ppufile:=nil;
  219. end;
  220. if not(hp^.sources_avail) then
  221. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  222. else
  223. compile(hp^.mainsource^,compile_system);
  224. end
  225. else
  226. begin
  227. { only reassemble ? }
  228. if (hp^.do_assemble) then
  229. begin
  230. a:=new(PAsmFile,Init(hp^.asmfilename^));
  231. a^.DoAssemble;
  232. dispose(a,Done);
  233. end;
  234. { we should know there the PPU file else it's an error and
  235. we can't load the unit }
  236. if hp^.ppufile^.name^<>'' then
  237. begin
  238. if (hp^.flags and uf_in_library)=0 then
  239. Linker.AddObjectFile(hp^.objfilename^);
  240. load_ppu(hp,compile_system);
  241. end;
  242. end;
  243. { register the unit _once_ }
  244. usedunits.concat(new(pused_unit,init(hp,0)));
  245. { the unit is written, so we can set the symtable type }
  246. { to unitsymtable, else we get some dupid errors }
  247. { this is not the right place because of the }
  248. { ready label }
  249. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  250. { placed at this end of proc_unit }
  251. psymtable(hp^.symtable)^.unitid:=0;
  252. { reset the unitnumbers for the other units }
  253. pu:=pused_unit(old_current_module^.used_units.first);
  254. while assigned(pu) do
  255. begin
  256. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  257. pu:=pused_unit(pu^.next);
  258. end;
  259. end
  260. else
  261. if assigned(hp) and (st=nil) then
  262. begin
  263. { we have to compile the unit again, but it is already inserted !!}
  264. { we may have problem with the lost symtable !! }
  265. current_module:=hp;
  266. { we must preserve the unit chain }
  267. nextmodule:=pmodule(hp^.next);
  268. { we have to cleanup a little }
  269. hp^.special_done;
  270. new(hs);
  271. hs^:=hp^.mainsource^;
  272. hp^.init(hs^,true);
  273. dispose(hs);
  274. { we must preserve the unit chain }
  275. hp^.next:=nextmodule;
  276. if assigned(hp^.ppufile) then
  277. load_ppu(hp,compile_system)
  278. else
  279. begin
  280. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  281. compile(hp^.mainsource^,compile_system);
  282. end;
  283. current_module^.compiled:=true;
  284. end;
  285. { set the old module }
  286. current_module:=old_current_module;
  287. { the current module uses the unit hp }
  288. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  289. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  290. if in_uses and not current_module^.in_implementation then
  291. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  292. loadunit:=hp;
  293. end;
  294. procedure loadunits;
  295. var
  296. s : stringid;
  297. hp : pused_unit;
  298. hp2 : pmodule;
  299. hp3 : psymtable;
  300. oldprocsym:Pprocsym;
  301. begin
  302. oldprocsym:=aktprocsym;
  303. consume(_USES);
  304. {$ifdef DEBUG}
  305. test_symtablestack;
  306. {$endif DEBUG}
  307. repeat
  308. s:=pattern;
  309. consume(ID);
  310. hp2:=loadunit(s,false,true);
  311. if current_module^.compiled then
  312. exit;
  313. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  314. if token=COMMA then
  315. begin
  316. pattern:='';
  317. consume(COMMA);
  318. end
  319. else
  320. break;
  321. until false;
  322. consume(SEMICOLON);
  323. { now insert the units in the symtablestack }
  324. hp:=pused_unit(current_module^.used_units.first);
  325. { set the symtable to systemunit so it gets reorderd correctly }
  326. symtablestack:=systemunit;
  327. while assigned(hp) do
  328. begin
  329. {$IfDef GDB}
  330. if (cs_debuginfo in aktswitches) and
  331. not hp^.is_stab_written then
  332. begin
  333. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  334. hp^.is_stab_written:=true;
  335. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  336. end;
  337. {$EndIf GDB}
  338. if hp^.in_uses then
  339. begin
  340. hp3:=symtablestack;
  341. while assigned(hp3) do
  342. begin
  343. { insert units only once ! }
  344. if hp^.u^.symtable=hp3 then
  345. break;
  346. hp3:=hp3^.next;
  347. { unit isn't inserted }
  348. if hp3=nil then
  349. begin
  350. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  351. symtablestack:=psymtable(hp^.u^.symtable);
  352. {$ifdef CHAINPROCSYMS}
  353. symtablestack^.chainprocsyms;
  354. {$endif CHAINPROCSYMS}
  355. {$ifdef DEBUG}
  356. test_symtablestack;
  357. {$endif DEBUG}
  358. end;
  359. end;
  360. end;
  361. hp:=pused_unit(hp^.next);
  362. end;
  363. aktprocsym:=oldprocsym;
  364. end;
  365. procedure parse_uses(symt:Psymtable);
  366. begin
  367. if token=_USES then
  368. begin
  369. current_module^.in_implementation:=true;
  370. symt^.symtabletype:=unitsymtable;
  371. loadunits;
  372. symt^.symtabletype:=globalsymtable;
  373. {$ifdef DEBUG}
  374. test_symtablestack;
  375. {$endif DEBUG}
  376. end;
  377. end;
  378. procedure proc_unit;
  379. var
  380. unitname : stringid;
  381. {$ifdef GDB}
  382. { several defs to simulate more or less C++ objects for GDB }
  383. vmtdef : precdef;
  384. pvmtdef : ppointerdef;
  385. vmtarraydef : parraydef;
  386. vmtsymtable : psymtable;
  387. {$endif GDB}
  388. names:Tstringcontainer;
  389. p : psymtable;
  390. unitst : punitsymtable;
  391. pu : pused_unit;
  392. { the output ppufile is written to this path }
  393. s1,s2,s3:^string; {Saves stack space, but only eats heap
  394. space when there is a lot of heap free.}
  395. begin
  396. consume(_UNIT);
  397. stringdispose(current_module^.objfilename);
  398. stringdispose(current_module^.ppufilename);
  399. { create filenames and check unit name }
  400. new(s1);
  401. new(s2);
  402. new(s3);
  403. s1^:=FixFileName(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^);
  404. current_module^.objfilename:=stringdup(s1^+target_info.objext);
  405. current_module^.ppufilename:=stringdup(s1^+target_info.unitext);
  406. s1^:=upper(pattern);
  407. s2^:=upper(target_info.system_unit);
  408. s3^:=upper(current_module^.current_inputfile^.name^);
  409. if (cs_compilesystem in aktswitches) then
  410. begin
  411. if (cs_check_unit_name in aktswitches) and
  412. ((length(pattern)>8) or (s1^<>s2^) or (s1^<>s3^)) then
  413. Message1(unit_e_illegal_unit_name,s1^);
  414. end
  415. else
  416. if (s1^=s2^) then
  417. Message(unit_w_switch_us_missed);
  418. dispose(s3);
  419. dispose(s2);
  420. dispose(s1);
  421. { add object }
  422. Linker.AddObjectFile(current_module^.objfilename^);
  423. unitname:=pattern;
  424. consume(ID);
  425. consume(SEMICOLON);
  426. consume(_INTERFACE);
  427. { this should be placed after uses !!}
  428. {$ifndef UseNiceNames}
  429. procprefix:='_'+unitname+'$$';
  430. {$else UseNiceNames}
  431. procprefix:='_'+tostr(length(unitname))+lowercase(unitname)+'_';
  432. {$endif UseNiceNames}
  433. parse_only:=true;
  434. { generate now the global symboltable }
  435. p:=new(punitsymtable,init(globalsymtable,unitname));
  436. refsymtable:=p;
  437. unitst:=punitsymtable(p);
  438. { set the symbol table for the current unit }
  439. { this must be set later for interdependency }
  440. { current_module^.symtable:=psymtable(p); }
  441. { a unit compiled at command line must be inside the loaded_unit list }
  442. if (compile_level=1) then
  443. begin
  444. current_module^.unitname:=stringdup(unitname);
  445. loaded_units.insert(current_module);
  446. if cs_unit_to_lib in initswitches then
  447. begin
  448. current_module^.flags:=current_module^.flags or uf_in_library;
  449. if cs_shared_lib in initswitches then
  450. current_module^.flags:=current_module^.flags or uf_shared_library;
  451. end;
  452. end;
  453. { insert qualifier for the system unit (allows system.writeln) }
  454. if not(cs_compilesystem in aktswitches) then
  455. begin
  456. { insert the system unit }
  457. { it is allways the first }
  458. systemunit^.next:=nil;
  459. symtablestack:=systemunit;
  460. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  461. if token=_USES then
  462. begin
  463. unitst^.symtabletype:=unitsymtable;
  464. loadunits;
  465. { has it been compiled at a higher level ?}
  466. if current_module^.compiled then
  467. exit;
  468. unitst^.symtabletype:=globalsymtable;
  469. end;
  470. { ... but insert the symbol table later }
  471. p^.next:=symtablestack;
  472. symtablestack:=p;
  473. end
  474. else
  475. { while compiling a system unit, some types are directly inserted }
  476. begin
  477. p^.next:=symtablestack;
  478. symtablestack:=p;
  479. p^.insert(new(ptypesym,init('longint',s32bitdef)));
  480. p^.insert(new(ptypesym,init('ulong',u32bitdef)));
  481. p^.insert(new(ptypesym,init('void',voiddef)));
  482. p^.insert(new(ptypesym,init('char',cchardef)));
  483. {$ifdef i386}
  484. p^.insert(new(ptypesym,init('s64real',c64floatdef)));
  485. {$endif i386}
  486. p^.insert(new(ptypesym,init('s80real',s80floatdef)));
  487. p^.insert(new(ptypesym,init('cs32fixed',s32fixeddef)));
  488. p^.insert(new(ptypesym,init('byte',u8bitdef)));
  489. p^.insert(new(ptypesym,init('string',cstringdef)));
  490. p^.insert(new(ptypesym,init('word',u16bitdef)));
  491. p^.insert(new(ptypesym,init('boolean',booldef)));
  492. p^.insert(new(ptypesym,init('void_pointer',voidpointerdef)));
  493. p^.insert(new(ptypesym,init('file',cfiledef)));
  494. {$ifdef i386}
  495. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s64real)))));
  496. p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s64bit)))));
  497. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  498. {$endif}
  499. {$ifdef m68k}
  500. { internal definitions }
  501. p^.insert(new(ptypesym,init('s32real',c64floatdef)));
  502. { mappings... }
  503. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s32real)))));
  504. if (cs_fp_emulation) in aktswitches then
  505. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s32real)))))
  506. else
  507. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s64real)))));
  508. { p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s32real)))));}
  509. if (cs_fp_emulation) in aktswitches then
  510. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s32real)))))
  511. else
  512. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  513. {$endif}
  514. p^.insert(new(ptypesym,init('SINGLE',new(pfloatdef,init(s32real)))));
  515. p^.insert(new(ptypesym,init('POINTER',new(ppointerdef,init(voiddef)))));
  516. p^.insert(new(ptypesym,init('STRING',cstringdef)));
  517. p^.insert(new(ptypesym,init('BOOLEAN',new(porddef,init(bool8bit,0,1)))));
  518. p^.insert(new(ptypesym,init('CHAR',new(porddef,init(uchar,0,255)))));
  519. p^.insert(new(ptypesym,init('TEXT',new(pfiledef,init(ft_text,nil)))));
  520. p^.insert(new(ptypesym,init('CARDINAL',new(porddef,init(u32bit,0,$ffffffff)))));
  521. p^.insert(new(ptypesym,init('FIXED',new(pfloatdef,init(f32bit)))));
  522. p^.insert(new(ptypesym,init('FIXED16',new(pfloatdef,init(f16bit)))));
  523. p^.insert(new(ptypesym,init('TYPEDFILE',new(pfiledef,init(ft_typed,voiddef)))));
  524. { !!!!!
  525. p^.insert(new(ptypesym,init('COMP',new(porddef,init(s64bit,0,0)))));
  526. p^.insert(new(ptypesym,init('SINGLE',new(porddef,init(s32real,0,0)))));
  527. p^.insert(new(ptypesym,init('EXTENDED',new(porddef,init(s80real,0,0)))));
  528. p^.insert(new(ptypesym,init('FILE',new(pfiledef,init(ft_untyped,nil)))));
  529. }
  530. { Add a type for virtual method tables in lowercase }
  531. { so it isn't reachable! }
  532. {$ifdef GDB}
  533. vmtsymtable:=new(psymtable,init(recordsymtable));
  534. vmtdef:=new(precdef,init(vmtsymtable));
  535. pvmtdef:=new(ppointerdef,init(vmtdef));
  536. vmtsymtable^.insert(new(pvarsym,init('parent',pvmtdef)));
  537. vmtsymtable^.insert(new(pvarsym,init('length',globaldef('longint'))));
  538. vmtsymtable^.insert(new(pvarsym,init('mlength',globaldef('longint'))));
  539. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  540. vmtarraydef^.definition := voidpointerdef;
  541. vmtsymtable^.insert(new(pvarsym,init('__pfn',vmtarraydef)));
  542. p^.insert(new(ptypesym,init('__vtbl_ptr_type',vmtdef)));
  543. p^.insert(new(ptypesym,init('pvmt',pvmtdef)));
  544. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  545. vmtarraydef^.definition := pvmtdef;
  546. p^.insert(new(ptypesym,init('vtblarray',vmtarraydef)));
  547. insertinternsyms(p);
  548. {$endif GDB}
  549. end;
  550. { displaced for inter-dependency considerations }
  551. current_module^.symtable:=psymtable(p);
  552. constsymtable:=symtablestack;
  553. { ... parse the declarations }
  554. read_interface_declarations;
  555. consume(_IMPLEMENTATION);
  556. parse_only:=false;
  557. refsymtable^.number_defs;
  558. {$ifdef GDB}
  559. { add all used definitions even for implementation}
  560. if (cs_debuginfo in aktswitches) then
  561. begin
  562. { all types }
  563. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  564. { and all local symbols}
  565. refsymtable^.concatstabto(debuglist);
  566. end;
  567. {$endif GDB}
  568. { for interdependent units
  569. the crc is included in the ppufile
  570. but it is not known when writing the first ppufile
  571. so I tried to add a fake writing of the ppu
  572. just to get the CRC
  573. but the result is different for the real CRC
  574. it calculates after, I don't know why
  575. Answer:
  576. -------
  577. When reading the interface part, the compiler assumes
  578. that all registers are modified by a procedure
  579. usedinproc:=$ff !
  580. If the definition is read, the compiler determines
  581. the used registers and write the correct value
  582. to usedinproc
  583. only_calculate_crc:=true;
  584. writeunitas(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^+
  585. +'.PPS',punitsymtable(symtablestack));
  586. only_calculate_crc:=false;
  587. }
  588. { generates static symbol table }
  589. p:=new(punitsymtable,init(staticsymtable,unitname));
  590. refsymtable:=p;
  591. {Generate a procsym.}
  592. aktprocsym:=new(Pprocsym,init(unitname+'_init'));
  593. aktprocsym^.definition:=new(Pprocdef,init);
  594. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  595. aktprocsym^.definition^.setmangledname(unitname+'_init');
  596. {The generated procsym has a local symtable. Discard it and turn
  597. it into the static one.}
  598. dispose(aktprocsym^.definition^.localst,done);
  599. aktprocsym^.definition^.localst:=p;
  600. names.init;
  601. names.insert(unitname+'_init');
  602. { testing !!!!!!!!! }
  603. { we set the interface part as a unitsymtable }
  604. { for the case we need to compile another unit }
  605. { remove the globalsymtable from the symtable stack }
  606. { to reinsert it after loading the implementation units }
  607. symtablestack:=unitst^.next;
  608. parse_uses(unitst);
  609. { duplicated here to be sure }
  610. {$ifndef UseNiceNames}
  611. procprefix:='_'+unitname+'$$';
  612. {$else UseNiceNames}
  613. procprefix:='_'+tostr(length(unitname))+lowercase(unitname)+'_';
  614. {$endif UseNiceNames}
  615. { but reinsert the global symtable as lasts }
  616. unitst^.next:=symtablestack;
  617. symtablestack:=unitst;
  618. {$ifdef DEBUG}
  619. test_symtablestack;
  620. {$endif DEBUG}
  621. constsymtable:=symtablestack;
  622. {$ifdef Splitheap}
  623. if testsplit then
  624. begin
  625. Split_Heap;
  626. allow_special:=true;
  627. Switch_to_temp_heap;
  628. end;
  629. {$endif Splitheap}
  630. {$ifdef Splitheap}
  631. { it will report all crossings }
  632. allow_special:=false;
  633. {$endif Splitheap}
  634. { set some informations }
  635. procinfo.retdef:=voiddef;
  636. procinfo._class:=nil;
  637. procinfo.call_offset:=8;
  638. { for temporary values }
  639. procinfo.framepointer:=frame_pointer;
  640. { clear flags }
  641. procinfo.flags:=0;
  642. {Reset the codegenerator.}
  643. codegen_newprocedure;
  644. names.insert('INIT$$'+unitname);
  645. compile_proc_body(names,true,false);
  646. codegen_doneprocedure;
  647. consume(POINT);
  648. names.done;
  649. { size of the static data }
  650. datasize:=symtablestack^.datasize;
  651. { unsed static symbols ? }
  652. symtablestack^.allsymbolsused;
  653. {$ifdef GDB}
  654. { add all used definitions even for implementation}
  655. if (cs_debuginfo in aktswitches) then
  656. begin
  657. { all types }
  658. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  659. { and all local symbols}
  660. symtablestack^.concatstabto(debuglist);
  661. end;
  662. {$endif GDB}
  663. current_module^.in_implementation:=false;
  664. { deletes all symtables generated in the implementation part }
  665. while symtablestack^.symtabletype<>globalsymtable do
  666. dellexlevel;
  667. { tests, if all forwards are resolved }
  668. symtablestack^.check_forwards;
  669. symtablestack^.symtabletype:=unitsymtable;
  670. punitsymtable(symtablestack)^.is_stab_written:=false;
  671. {Write out the unit if the compile was succesfull.}
  672. if errorcount=0 then
  673. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  674. pu:=pused_unit(usedunits.first);
  675. while assigned(pu) do
  676. begin
  677. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  678. pu:=pused_unit(pu^.next);
  679. end;
  680. inc(datasize,symtablestack^.datasize);
  681. end;
  682. procedure proc_program(islibrary : boolean);
  683. var
  684. i : longint;
  685. st : psymtable;
  686. programname : stringid;
  687. names:Tstringcontainer;
  688. begin
  689. { Trying to compile the system unit... }
  690. { if no unit defined... then issue a }
  691. { fatal error (avoids pointer problems)}
  692. { when referencing the non-existant }
  693. { system unit. }
  694. if (cs_compilesystem in aktswitches) then
  695. Begin
  696. if token<>_UNIT then
  697. Message1(scan_f_syn_expected,'UNIT');
  698. consume(_UNIT);
  699. end;
  700. parse_only:=false;
  701. programname:='';
  702. if islibrary then
  703. begin
  704. consume(_LIBRARY);
  705. programname:=pattern;
  706. consume(ID);
  707. consume(SEMICOLON);
  708. end
  709. else
  710. { is there an program head ? }
  711. if token=_PROGRAM then
  712. begin
  713. consume(_PROGRAM);
  714. programname:=pattern;
  715. consume(ID);
  716. if token=LKLAMMER then
  717. begin
  718. consume(LKLAMMER);
  719. idlist;
  720. consume(RKLAMMER);
  721. end;
  722. consume(SEMICOLON);
  723. end;
  724. { insert after the unit symbol tables the static symbol table }
  725. { of the program }
  726. st:=new(punitsymtable,init(staticsymtable,programname));
  727. {Generate a procsym.}
  728. aktprocsym:=new(Pprocsym,init('program_init'));
  729. aktprocsym^.definition:=new(Pprocdef,init);
  730. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  731. aktprocsym^.definition^.setmangledname('program_init');
  732. {The localst is a local symtable. Change it into the static
  733. symtable.}
  734. dispose(aktprocsym^.definition^.localst,done);
  735. aktprocsym^.definition^.localst:=st;
  736. names.init;
  737. names.insert('program_init');
  738. refsymtable:=st;
  739. {Insert the symbols of the system unit into the stack of symbol
  740. tables.}
  741. symtablestack:=systemunit;
  742. systemunit^.next:=nil;
  743. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  744. {Load the units used by the program we compile.}
  745. if token=_USES then loadunits;
  746. {Insert the name of the main program into the symbol table.}
  747. if programname<>'' then
  748. st^.insert(new(pprogramsym,init(programname)));
  749. { ...is also constsymtable, this is the symtable where }
  750. { the elements of enumeration types are inserted }
  751. constsymtable:=st;
  752. codegen_newprocedure;
  753. { set some informations about the main program }
  754. procinfo.retdef:=voiddef;
  755. procinfo._class:=nil;
  756. procinfo.call_offset:=8;
  757. {Set the framepointer of the program initialization to the
  758. default framepointer (EBP on i386).}
  759. procinfo.framepointer:=frame_pointer;
  760. { clear flags }
  761. procinfo.flags:=0;
  762. procprefix:='';
  763. in_except_block:=false;
  764. {The program intialization needs an alias, so it can be called
  765. from the bootstrap code.}
  766. case target_info.target of
  767. target_GO32V1,
  768. target_GO32V2,
  769. target_OS2,
  770. target_WIN32:
  771. names.insert('_main');
  772. target_LINUX:
  773. names.insert('main');
  774. end;
  775. names.insert('PASCALMAIN');
  776. compile_proc_body(names,true,false);
  777. codegen_doneprocedure;
  778. Linker.AddObjectFile(current_module^.unitname^);
  779. current_module^.linkofiles.insert(current_module^.unitname^);
  780. { On the Macintosh Classic M68k Architecture }
  781. { The Heap variable is simply a POINTER to the }
  782. { real HEAP. The HEAP must be set up by the RTL }
  783. { and must store the pointer in this value. }
  784. if (target_info.target = target_MAC68k) then
  785. bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)))
  786. else
  787. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  788. if target_info.target=target_GO32V2 then
  789. begin
  790. { stacksize can be specified }
  791. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  792. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  793. end;
  794. if (target_info.target=target_WIN32) then
  795. begin
  796. { generate the last entry for the imports directory }
  797. if not(assigned(importssection)) then
  798. importssection:=new(paasmoutput,init);
  799. { $3 ensure that it is the last entry, all other entries }
  800. { are written to $2 }
  801. importssection^.concat(new(pai_section,init('.idata$3')));
  802. for i:=1 to 5 do
  803. importssection^.concat(new(pai_const,init_32bit(0)));
  804. end;
  805. {I prefer starting with a heapsize of 256K in OS/2. The heap can
  806. grow later until the size specified on the command line. Allocating
  807. four megs at once can hurt performance when more programs are in
  808. memory.}
  809. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  810. if target_info.target=target_OS2 then
  811. heapsize:=256*1024;
  812. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  813. datasize:=symtablestack^.datasize;
  814. names.done;
  815. consume(POINT);
  816. symtablestack^.check_forwards;
  817. symtablestack^.allsymbolsused;
  818. end;
  819. end.
  820. {
  821. $Log$
  822. Revision 1.1 1998-03-25 11:18:15 root
  823. Initial revision
  824. Revision 1.43 1998/03/20 23:31:34 florian
  825. * bug0113 fixed
  826. * problem with interdepened units fixed ("options.pas problem")
  827. * two small extensions for future AMD 3D support
  828. Revision 1.42 1998/03/11 22:22:52 florian
  829. * Fixed circular unit uses, when the units are not in the current dir (from Peter)
  830. * -i shows correct info, not <lf> anymore (from Peter)
  831. * linking with shared libs works again (from Peter)
  832. Revision 1.41 1998/03/10 17:19:29 peter
  833. * fixed bug0108
  834. * better linebreak scanning (concentrated in nextchar(), it supports
  835. #10, #13, #10#13, #13#10
  836. Revision 1.40 1998/03/10 16:27:42 pierre
  837. * better line info in stabs debug
  838. * symtabletype and lexlevel separated into two fields of tsymtable
  839. + ifdef MAKELIB for direct library output, not complete
  840. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  841. working
  842. + ifdef TESTFUNCRET for setting func result in underfunction, not
  843. working
  844. Revision 1.39 1998/03/10 01:17:24 peter
  845. * all files have the same header
  846. * messages are fully implemented, EXTDEBUG uses Comment()
  847. + AG... files for the Assembler generation
  848. Revision 1.38 1998/03/05 22:43:50 florian
  849. * some win32 support stuff added
  850. Revision 1.37 1998/03/04 01:35:08 peter
  851. * messages for unit-handling and assembler/linker
  852. * the compiler compiles without -dGDB, but doesn't work yet
  853. + -vh for Hint
  854. Revision 1.36 1998/03/03 23:18:45 florian
  855. * ret $8 problem with unit init/main program fixed
  856. Revision 1.35 1998/03/02 13:38:48 peter
  857. + importlib object
  858. * doesn't crash on a systemunit anymore
  859. * updated makefile and depend
  860. Revision 1.34 1998/03/02 01:49:04 peter
  861. * renamed target_DOS to target_GO32V1
  862. + new verbose system, merged old errors and verbose units into one new
  863. verbose.pas, so errors.pas is obsolete
  864. Revision 1.33 1998/03/01 22:46:20 florian
  865. + some win95 linking stuff
  866. * a couple of bugs fixed:
  867. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  868. Revision 1.32 1998/02/28 09:30:58 florian
  869. + writing of win32 import section added
  870. Revision 1.31 1998/02/28 03:57:08 carl
  871. + replaced target_info.short_name by target_info.target (a bit faster)
  872. Revision 1.30 1998/02/27 09:25:58 daniel
  873. * Changed symtable handling so no junk symtable is put on the symtablestack.
  874. Revision 1.28 1998/02/24 14:20:54 peter
  875. + tstringcontainer.empty
  876. * ld -T option restored for linux
  877. * libraries are placed before the objectfiles in a .PPU file
  878. * removed 'uses link' from files.pas
  879. Revision 1.27 1998/02/24 00:19:19 peter
  880. * makefile works again (btw. linux does like any char after a \ )
  881. * removed circular unit with assemble and files
  882. * fixed a sigsegv in pexpr
  883. * pmodule init unit/program is the almost the same, merged them
  884. Revision 1.26 1998/02/22 23:55:18 peter
  885. * small fix
  886. Revision 1.25 1998/02/22 23:03:28 peter
  887. * renamed msource->mainsource and name->unitname
  888. * optimized filename handling, filename is not seperate anymore with
  889. path+name+ext, this saves stackspace and a lot of fsplit()'s
  890. * recompiling of some units in libraries fixed
  891. * shared libraries are working again
  892. + $LINKLIB <lib> to support automatic linking to libraries
  893. + libraries are saved/read from the ppufile, also allows more libraries
  894. per ppufile
  895. Revision 1.24 1998/02/22 18:51:06 carl
  896. * where the heck did the HEAP for m68k go??????? REINSTATED
  897. Revision 1.23 1998/02/21 05:50:14 carl
  898. * bugfix of crash with Us switch
  899. Revision 1.22 1998/02/19 00:11:08 peter
  900. * fixed -g to work again
  901. * fixed some typos with the scriptobject
  902. Revision 1.21 1998/02/17 21:20:57 peter
  903. + Script unit
  904. + __EXIT is called again to exit a program
  905. - target_info.link/assembler calls
  906. * linking works again for dos
  907. * optimized a few filehandling functions
  908. * fixed stabs generation for procedures
  909. Revision 1.20 1998/02/16 12:51:38 michael
  910. + Implemented linker object
  911. Revision 1.19 1998/02/14 01:45:29 peter
  912. * more fixes
  913. - pmode target is removed
  914. - search_as_ld is removed, this is done in the link.pas/assemble.pas
  915. + findexe() to search for an executable (linker,assembler,binder)
  916. Revision 1.18 1998/02/13 22:26:37 peter
  917. * fixed a few SigSegv's
  918. * INIT$$ was not written for linux!
  919. * assembling and linking works again for linux and dos
  920. + assembler object, only attasmi3 supported yet
  921. * restore pp.pas with AddPath etc.
  922. Revision 1.17 1998/02/13 10:35:27 daniel
  923. * Made Motorola version compilable.
  924. * Fixed optimizer
  925. Revision 1.16 1998/02/12 17:19:22 florian
  926. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  927. also that aktswitches isn't a pointer)
  928. Revision 1.15 1998/02/12 11:50:28 daniel
  929. Yes! Finally! After three retries, my patch!
  930. Changes:
  931. Complete rewrite of psub.pas.
  932. Added support for DLL's.
  933. Compiler requires less memory.
  934. Platform units for each platform.
  935. Revision 1.14 1998/01/30 17:31:26 pierre
  936. * bug of cyclic symtablestack fixed
  937. Revision 1.13 1998/01/28 13:48:49 michael
  938. + Initial implementation for making libs from within FPC. Not tested, as compiler does not run
  939. Revision 1.12 1998/01/19 15:46:25 peter
  940. * fixed INIT$$lowercase generation
  941. Revision 1.11 1998/01/19 09:32:28 michael
  942. * Shared Lib and GDB/RHIDE Bufixes from Peter Vreman.
  943. Revision 1.10 1998/01/17 01:57:39 michael
  944. + Start of shared library support. First working version.
  945. Revision 1.9 1998/01/16 18:03:17 florian
  946. * small bug fixes, some stuff of delphi styled constructores added
  947. Revision 1.8 1998/01/13 23:11:15 florian
  948. + class methods
  949. Revision 1.7 1998/01/13 17:13:09 michael
  950. * File time handling and file searching is now done in an OS-independent way,
  951. using the new file treating functions in globals.pas.
  952. Revision 1.6 1998/01/13 16:16:03 pierre
  953. * bug in interdependent units handling
  954. - primary unit was not in loaded_units list
  955. - current_module^.symtable was assigned too early
  956. - donescanner must not call error if the compilation
  957. of the unit was done at a higher level.
  958. Revision 1.5 1998/01/12 13:03:32 florian
  959. + parsing of class methods implemented
  960. Revision 1.4 1998/01/11 10:54:24 florian
  961. + generic library support
  962. Revision 1.3 1998/01/11 04:17:36 carl
  963. + floating point support for m68k
  964. Revision 1.2 1998/01/09 09:10:01 michael
  965. + Initial implementation, second try
  966. }