pmodules.pas 41 KB

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