pmodules.pas 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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('main'));
  729. aktprocsym^.definition:=new(Pprocdef,init);
  730. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  731. aktprocsym^.definition^.setmangledname(target_info.Cprefix+'main');
  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. {On OS/2 the heap is also intialized by the RTL. We do
  785. not output a pointer.}
  786. if target_info.target<>target_OS2 then
  787. if (target_info.target = target_MAC68k) then
  788. bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)))
  789. else
  790. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  791. if target_info.target=target_GO32V2 then
  792. begin
  793. { stacksize can be specified }
  794. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  795. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  796. end;
  797. if (target_info.target=target_WIN32) then
  798. begin
  799. { generate the last entry for the imports directory }
  800. if not(assigned(importssection)) then
  801. importssection:=new(paasmoutput,init);
  802. { $3 ensure that it is the last entry, all other entries }
  803. { are written to $2 }
  804. importssection^.concat(new(pai_section,init('.idata$3')));
  805. for i:=1 to 5 do
  806. importssection^.concat(new(pai_const,init_32bit(0)));
  807. end;
  808. {I prefer starting with a heapsize of 256K in OS/2. The heap can
  809. grow later until the size specified on the command line. Allocating
  810. four megs at once can hurt performance when more programs are in
  811. memory.}
  812. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  813. if target_info.target=target_OS2 then
  814. heapsize:=256*1024;
  815. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  816. datasize:=symtablestack^.datasize;
  817. names.done;
  818. consume(POINT);
  819. symtablestack^.check_forwards;
  820. symtablestack^.allsymbolsused;
  821. end;
  822. end.
  823. {
  824. $Log$
  825. Revision 1.3 1998-04-03 09:51:00 daniel
  826. * Fixed heap allocation for OS/2.
  827. Revision 1.2 1998/03/30 15:53:01 florian
  828. * last changes before release:
  829. - gdb fixed
  830. - ratti386 warning removed (about unset function result)
  831. Revision 1.1.1.1 1998/03/25 11:18:15 root
  832. * Restored version
  833. Revision 1.43 1998/03/20 23:31:34 florian
  834. * bug0113 fixed
  835. * problem with interdepened units fixed ("options.pas problem")
  836. * two small extensions for future AMD 3D support
  837. Revision 1.42 1998/03/11 22:22:52 florian
  838. * Fixed circular unit uses, when the units are not in the current dir (from Peter)
  839. * -i shows correct info, not <lf> anymore (from Peter)
  840. * linking with shared libs works again (from Peter)
  841. Revision 1.41 1998/03/10 17:19:29 peter
  842. * fixed bug0108
  843. * better linebreak scanning (concentrated in nextchar(), it supports
  844. #10, #13, #10#13, #13#10
  845. Revision 1.40 1998/03/10 16:27:42 pierre
  846. * better line info in stabs debug
  847. * symtabletype and lexlevel separated into two fields of tsymtable
  848. + ifdef MAKELIB for direct library output, not complete
  849. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  850. working
  851. + ifdef TESTFUNCRET for setting func result in underfunction, not
  852. working
  853. Revision 1.39 1998/03/10 01:17:24 peter
  854. * all files have the same header
  855. * messages are fully implemented, EXTDEBUG uses Comment()
  856. + AG... files for the Assembler generation
  857. Revision 1.38 1998/03/05 22:43:50 florian
  858. * some win32 support stuff added
  859. Revision 1.37 1998/03/04 01:35:08 peter
  860. * messages for unit-handling and assembler/linker
  861. * the compiler compiles without -dGDB, but doesn't work yet
  862. + -vh for Hint
  863. Revision 1.36 1998/03/03 23:18:45 florian
  864. * ret $8 problem with unit init/main program fixed
  865. Revision 1.35 1998/03/02 13:38:48 peter
  866. + importlib object
  867. * doesn't crash on a systemunit anymore
  868. * updated makefile and depend
  869. Revision 1.34 1998/03/02 01:49:04 peter
  870. * renamed target_DOS to target_GO32V1
  871. + new verbose system, merged old errors and verbose units into one new
  872. verbose.pas, so errors.pas is obsolete
  873. Revision 1.33 1998/03/01 22:46:20 florian
  874. + some win95 linking stuff
  875. * a couple of bugs fixed:
  876. bug0055,bug0058,bug0059,bug0064,bug0072,bug0093,bug0095,bug0098
  877. Revision 1.32 1998/02/28 09:30:58 florian
  878. + writing of win32 import section added
  879. Revision 1.31 1998/02/28 03:57:08 carl
  880. + replaced target_info.short_name by target_info.target (a bit faster)
  881. Revision 1.30 1998/02/27 09:25:58 daniel
  882. * Changed symtable handling so no junk symtable is put on the symtablestack.
  883. Revision 1.28 1998/02/24 14:20:54 peter
  884. + tstringcontainer.empty
  885. * ld -T option restored for linux
  886. * libraries are placed before the objectfiles in a .PPU file
  887. * removed 'uses link' from files.pas
  888. Revision 1.27 1998/02/24 00:19:19 peter
  889. * makefile works again (btw. linux does like any char after a \ )
  890. * removed circular unit with assemble and files
  891. * fixed a sigsegv in pexpr
  892. * pmodule init unit/program is the almost the same, merged them
  893. Revision 1.26 1998/02/22 23:55:18 peter
  894. * small fix
  895. Revision 1.25 1998/02/22 23:03:28 peter
  896. * renamed msource->mainsource and name->unitname
  897. * optimized filename handling, filename is not seperate anymore with
  898. path+name+ext, this saves stackspace and a lot of fsplit()'s
  899. * recompiling of some units in libraries fixed
  900. * shared libraries are working again
  901. + $LINKLIB <lib> to support automatic linking to libraries
  902. + libraries are saved/read from the ppufile, also allows more libraries
  903. per ppufile
  904. Revision 1.24 1998/02/22 18:51:06 carl
  905. * where the heck did the HEAP for m68k go??????? REINSTATED
  906. Revision 1.23 1998/02/21 05:50:14 carl
  907. * bugfix of crash with Us switch
  908. Revision 1.22 1998/02/19 00:11:08 peter
  909. * fixed -g to work again
  910. * fixed some typos with the scriptobject
  911. Revision 1.21 1998/02/17 21:20:57 peter
  912. + Script unit
  913. + __EXIT is called again to exit a program
  914. - target_info.link/assembler calls
  915. * linking works again for dos
  916. * optimized a few filehandling functions
  917. * fixed stabs generation for procedures
  918. Revision 1.20 1998/02/16 12:51:38 michael
  919. + Implemented linker object
  920. Revision 1.19 1998/02/14 01:45:29 peter
  921. * more fixes
  922. - pmode target is removed
  923. - search_as_ld is removed, this is done in the link.pas/assemble.pas
  924. + findexe() to search for an executable (linker,assembler,binder)
  925. Revision 1.18 1998/02/13 22:26:37 peter
  926. * fixed a few SigSegv's
  927. * INIT$$ was not written for linux!
  928. * assembling and linking works again for linux and dos
  929. + assembler object, only attasmi3 supported yet
  930. * restore pp.pas with AddPath etc.
  931. Revision 1.17 1998/02/13 10:35:27 daniel
  932. * Made Motorola version compilable.
  933. * Fixed optimizer
  934. Revision 1.16 1998/02/12 17:19:22 florian
  935. * fixed to get remake3 work, but needs additional fixes (output, I don't like
  936. also that aktswitches isn't a pointer)
  937. Revision 1.15 1998/02/12 11:50:28 daniel
  938. Yes! Finally! After three retries, my patch!
  939. Changes:
  940. Complete rewrite of psub.pas.
  941. Added support for DLL's.
  942. Compiler requires less memory.
  943. Platform units for each platform.
  944. Revision 1.14 1998/01/30 17:31:26 pierre
  945. * bug of cyclic symtablestack fixed
  946. Revision 1.13 1998/01/28 13:48:49 michael
  947. + Initial implementation for making libs from within FPC. Not tested, as compiler does not run
  948. Revision 1.12 1998/01/19 15:46:25 peter
  949. * fixed INIT$$lowercase generation
  950. Revision 1.11 1998/01/19 09:32:28 michael
  951. * Shared Lib and GDB/RHIDE Bufixes from Peter Vreman.
  952. Revision 1.10 1998/01/17 01:57:39 michael
  953. + Start of shared library support. First working version.
  954. Revision 1.9 1998/01/16 18:03:17 florian
  955. * small bug fixes, some stuff of delphi styled constructores added
  956. Revision 1.8 1998/01/13 23:11:15 florian
  957. + class methods
  958. Revision 1.7 1998/01/13 17:13:09 michael
  959. * File time handling and file searching is now done in an OS-independent way,
  960. using the new file treating functions in globals.pas.
  961. Revision 1.6 1998/01/13 16:16:03 pierre
  962. * bug in interdependent units handling
  963. - primary unit was not in loaded_units list
  964. - current_module^.symtable was assigned too early
  965. - donescanner must not call error if the compilation
  966. of the unit was done at a higher level.
  967. Revision 1.5 1998/01/12 13:03:32 florian
  968. + parsing of class methods implemented
  969. Revision 1.4 1998/01/11 10:54:24 florian
  970. + generic library support
  971. Revision 1.3 1998/01/11 04:17:36 carl
  972. + floating point support for m68k
  973. Revision 1.2 1998/01/09 09:10:01 michael
  974. + Initial implementation, second try
  975. }