2
0

pmodules.pas 43 KB

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