pmodules.pas 41 KB

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