2
0

pmodules.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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. {$ifdef NEWPPU}
  28. ,ppu
  29. {$endif}
  30. { parser specific stuff }
  31. ,pbase,pdecl,pstatmnt,psub
  32. { processor specific stuff }
  33. {$ifdef i386}
  34. ,i386
  35. ,cgai386
  36. ,tgeni386
  37. ,cgi386
  38. ,aopt386
  39. {$endif}
  40. {$ifdef m68k}
  41. ,m68k
  42. ,cga68k
  43. ,tgen68k
  44. ,cg68k
  45. {$endif}
  46. ;
  47. procedure addlinkerfiles(hp:pmodule);
  48. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  49. procedure proc_unit;
  50. procedure proc_program(islibrary : boolean);
  51. implementation
  52. uses
  53. parser;
  54. {$I innr.inc}
  55. procedure addlinkerfiles(hp:pmodule);
  56. begin
  57. with hp^ do
  58. begin
  59. while not linkofiles.empty do
  60. Linker.AddObject(linkofiles.Get);
  61. while not linksharedlibs.empty do
  62. Linker.AddSharedLibrary(linksharedlibs.Get);
  63. while not linkstaticlibs.empty do
  64. Linker.AddStaticLibrary(linkstaticlibs.Get);
  65. end;
  66. end;
  67. procedure insertsegment;
  68. begin
  69. {Insert Ident of the compiler}
  70. if (not smartlink)
  71. {$ifndef EXTDEBUG}
  72. and (not current_module^.is_unit)
  73. {$endif}
  74. then
  75. begin
  76. datasegment^.insert(new(pai_align,init(4)));
  77. datasegment^.insert(new(pai_string,init('FPC '+version_string+' for '+target_string+' - '+target_info.short_name)));
  78. end;
  79. codesegment^.insert(new(pai_section,init(sec_code)));
  80. datasegment^.insert(new(pai_section,init(sec_data)));
  81. bsssegment^.insert(new(pai_section,init(sec_bss)));
  82. consts^.insert(new(pai_section,init(sec_data)));
  83. consts^.insert(new(pai_asm_comment,init('Constants')));
  84. end;
  85. procedure insertheap;
  86. begin
  87. if smartlink then
  88. begin
  89. bsssegment^.concat(new(pai_cut,init));
  90. datasegment^.concat(new(pai_cut,init));
  91. end;
  92. { On the Macintosh Classic M68k Architecture
  93. The Heap variable is simply a POINTER to the
  94. real HEAP. The HEAP must be set up by the RTL
  95. and must store the pointer in this value.
  96. On OS/2 the heap is also intialized by the RTL. We do
  97. not output a pointer }
  98. case target_info.target of
  99. target_OS2 : ;
  100. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  101. else
  102. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  103. end;
  104. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  105. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  106. end;
  107. procedure inserttargetspecific;
  108. var
  109. i : longint;
  110. begin
  111. case target_info.target of
  112. target_GO32V2 : begin
  113. { stacksize can be specified }
  114. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  115. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  116. end;
  117. target_WIN32 : begin
  118. { generate the last entry for the imports directory }
  119. if not(assigned(importssection)) then
  120. importssection:=new(paasmoutput,init);
  121. { $3 ensure that it is the last entry, all other entries }
  122. { are written to $2 }
  123. importssection^.concat(new(pai_section,init_idata(3)));
  124. for i:=1 to 5 do
  125. importssection^.concat(new(pai_const,init_32bit(0)));
  126. end;
  127. end;
  128. end;
  129. { all intern procedures for system unit }
  130. procedure insertinternsyms(p : psymtable);
  131. begin
  132. p^.insert(new(psyssym,init('CONCAT',in_concat_x)));
  133. p^.insert(new(psyssym,init('WRITE',in_write_x)));
  134. p^.insert(new(psyssym,init('WRITELN',in_writeln_x)));
  135. p^.insert(new(psyssym,init('ASSIGNED',in_assigned_x)));
  136. p^.insert(new(psyssym,init('READ',in_read_x)));
  137. p^.insert(new(psyssym,init('READLN',in_readln_x)));
  138. p^.insert(new(psyssym,init('OFS',in_ofs_x)));
  139. p^.insert(new(psyssym,init('SIZEOF',in_sizeof_x)));
  140. p^.insert(new(psyssym,init('TYPEOF',in_typeof_x)));
  141. p^.insert(new(psyssym,init('LOW',in_low_x)));
  142. p^.insert(new(psyssym,init('HIGH',in_high_x)));
  143. p^.insert(new(psyssym,init('SEG',in_seg_x)));
  144. p^.insert(new(psyssym,init('ORD',in_ord_x)));
  145. p^.insert(new(psyssym,init('PRED',in_pred_x)));
  146. p^.insert(new(psyssym,init('SUCC',in_succ_x)));
  147. p^.insert(new(psyssym,init('EXCLUDE',in_exclude_x_y)));
  148. p^.insert(new(psyssym,init('INCLUDE',in_include_x_y)));
  149. p^.insert(new(psyssym,init('BREAK',in_break)));
  150. p^.insert(new(psyssym,init('CONTINUE',in_continue)));
  151. { for testing purpose }
  152. p^.insert(new(psyssym,init('DECI',in_dec_x)));
  153. p^.insert(new(psyssym,init('INCI',in_inc_x)));
  154. p^.insert(new(psyssym,init('STR',in_str_x_string)));
  155. end;
  156. { all the types inserted into the system unit }
  157. procedure insert_intern_types(p : psymtable);
  158. {$ifdef GDB}
  159. var
  160. { several defs to simulate more or less C++ objects for GDB }
  161. vmtdef : precdef;
  162. pvmtdef : ppointerdef;
  163. vmtarraydef : parraydef;
  164. vmtsymtable : psymtable;
  165. {$endif GDB}
  166. begin
  167. p^.insert(new(ptypesym,init('longint',s32bitdef)));
  168. p^.insert(new(ptypesym,init('ulong',u32bitdef)));
  169. p^.insert(new(ptypesym,init('void',voiddef)));
  170. p^.insert(new(ptypesym,init('char',cchardef)));
  171. {$ifdef i386}
  172. p^.insert(new(ptypesym,init('s64real',c64floatdef)));
  173. {$endif i386}
  174. p^.insert(new(ptypesym,init('s80real',s80floatdef)));
  175. p^.insert(new(ptypesym,init('cs32fixed',s32fixeddef)));
  176. p^.insert(new(ptypesym,init('byte',u8bitdef)));
  177. p^.insert(new(ptypesym,init('string',cstringdef)));
  178. p^.insert(new(ptypesym,init('longstring',clongstringdef)));
  179. p^.insert(new(ptypesym,init('ansistring',cansistringdef)));
  180. p^.insert(new(ptypesym,init('widestring',cwidestringdef)));
  181. p^.insert(new(ptypesym,init('word',u16bitdef)));
  182. p^.insert(new(ptypesym,init('boolean',booldef)));
  183. p^.insert(new(ptypesym,init('void_pointer',voidpointerdef)));
  184. p^.insert(new(ptypesym,init('file',cfiledef)));
  185. {$ifdef i386}
  186. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s64real)))));
  187. p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s64bit)))));
  188. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  189. {$endif}
  190. {$ifdef m68k}
  191. { internal definitions }
  192. p^.insert(new(ptypesym,init('s32real',c64floatdef)));
  193. { mappings... }
  194. p^.insert(new(ptypesym,init('REAL',new(pfloatdef,init(s32real)))));
  195. if (cs_fp_emulation) in aktswitches then
  196. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s32real)))))
  197. else
  198. p^.insert(new(ptypesym,init('DOUBLE',new(pfloatdef,init(s64real)))));
  199. { p^.insert(new(ptypesym,init('COMP',new(pfloatdef,init(s32real)))));}
  200. if (cs_fp_emulation) in aktswitches then
  201. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s32real)))))
  202. else
  203. p^.insert(new(ptypesym,init('EXTENDED',new(pfloatdef,init(s80real)))));
  204. {$endif}
  205. p^.insert(new(ptypesym,init('SINGLE',new(pfloatdef,init(s32real)))));
  206. p^.insert(new(ptypesym,init('POINTER',new(ppointerdef,init(voiddef)))));
  207. p^.insert(new(ptypesym,init('STRING',cstringdef)));
  208. p^.insert(new(ptypesym,init('LONGSTRING',clongstringdef)));
  209. p^.insert(new(ptypesym,init('ANSISTRING',cansistringdef)));
  210. p^.insert(new(ptypesym,init('WIDESTRING',cwidestringdef)));
  211. p^.insert(new(ptypesym,init('BOOLEAN',new(porddef,init(bool8bit,0,1)))));
  212. p^.insert(new(ptypesym,init('CHAR',new(porddef,init(uchar,0,255)))));
  213. p^.insert(new(ptypesym,init('TEXT',new(pfiledef,init(ft_text,nil)))));
  214. p^.insert(new(ptypesym,init('CARDINAL',new(porddef,init(u32bit,0,$ffffffff)))));
  215. p^.insert(new(ptypesym,init('FIXED',new(pfloatdef,init(f32bit)))));
  216. p^.insert(new(ptypesym,init('FIXED16',new(pfloatdef,init(f16bit)))));
  217. p^.insert(new(ptypesym,init('TYPEDFILE',new(pfiledef,init(ft_typed,voiddef)))));
  218. { !!!!!
  219. p^.insert(new(ptypesym,init('COMP',new(porddef,init(s64bit,0,0)))));
  220. p^.insert(new(ptypesym,init('SINGLE',new(porddef,init(s32real,0,0)))));
  221. p^.insert(new(ptypesym,init('EXTENDED',new(porddef,init(s80real,0,0)))));
  222. p^.insert(new(ptypesym,init('FILE',new(pfiledef,init(ft_untyped,nil)))));
  223. }
  224. { Add a type for virtual method tables in lowercase }
  225. { so it isn't reachable! }
  226. {$ifdef GDB}
  227. vmtsymtable:=new(psymtable,init(recordsymtable));
  228. vmtdef:=new(precdef,init(vmtsymtable));
  229. pvmtdef:=new(ppointerdef,init(vmtdef));
  230. vmtsymtable^.insert(new(pvarsym,init('parent',pvmtdef)));
  231. vmtsymtable^.insert(new(pvarsym,init('length',globaldef('longint'))));
  232. vmtsymtable^.insert(new(pvarsym,init('mlength',globaldef('longint'))));
  233. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  234. vmtarraydef^.definition := voidpointerdef;
  235. vmtsymtable^.insert(new(pvarsym,init('__pfn',vmtarraydef)));
  236. p^.insert(new(ptypesym,init('__vtbl_ptr_type',vmtdef)));
  237. p^.insert(new(ptypesym,init('pvmt',pvmtdef)));
  238. vmtarraydef:=new(parraydef,init(0,1,s32bitdef));
  239. vmtarraydef^.definition := pvmtdef;
  240. p^.insert(new(ptypesym,init('vtblarray',vmtarraydef)));
  241. {$endif GDB}
  242. insertinternsyms(p);
  243. end;
  244. procedure load_ppu(oldhp,hp : pmodule;compile_system : boolean);
  245. var
  246. loaded_unit : pmodule;
  247. b : byte;
  248. checksum,
  249. {$ifndef NEWPPU}
  250. count,
  251. {$endif NEWPPU}
  252. nextmapentry : longint;
  253. hs : string;
  254. begin
  255. { init the map }
  256. new(hp^.map);
  257. nextmapentry:=1;
  258. {$ifdef NEWPPU}
  259. { load the used units from interface }
  260. b:=hp^.ppufile^.readentry;
  261. if b=ibloadunit_int then
  262. begin
  263. while not hp^.ppufile^.endofentry do
  264. begin
  265. hs:=hp^.ppufile^.getstring;
  266. checksum:=hp^.ppufile^.getlongint;
  267. loaded_unit:=loadunit(hs,false,false);
  268. if hp^.compiled then
  269. exit;
  270. { if the crc of a used unit is the same as written to the
  271. PPU file, we needn't to recompile the current unit }
  272. if (loaded_unit^.crc<>checksum) then
  273. begin
  274. { we have to compile the current unit remove stuff which isn't
  275. needed }
  276. { forget the map }
  277. dispose(hp^.map);
  278. hp^.map:=nil;
  279. { remove the ppufile }
  280. dispose(hp^.ppufile,done);
  281. hp^.ppufile:=nil;
  282. { recompile or give an fatal error }
  283. if not(hp^.sources_avail) then
  284. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  285. else
  286. begin
  287. {$ifdef TEST_TEMPCLOSE}
  288. if assigned(oldhp^.current_inputfile) then
  289. oldhp^.current_inputfile^.tempclose;
  290. {$endif TEST_TEMPCLOSE}
  291. compile(hp^.mainsource^,compile_system);
  292. {$ifdef TEST_TEMPCLOSE}
  293. if not oldhp^.compiled then
  294. oldhp^.current_inputfile^.tempreopen;
  295. {$endif TEST_TEMPCLOSE}
  296. end;
  297. exit;
  298. end;
  299. { setup the map entry for deref }
  300. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  301. inc(nextmapentry);
  302. if nextmapentry>maxunits then
  303. Message(unit_f_too_much_units);
  304. end;
  305. { ok, now load the unit }
  306. hp^.symtable:=new(punitsymtable,load(hp^.unitname^));
  307. { if this is the system unit insert the intern symbols }
  308. make_ref:=false;
  309. if compile_system then
  310. insertinternsyms(psymtable(hp^.symtable));
  311. make_ref:=true;
  312. end;
  313. { now only read the implementation part }
  314. hp^.in_implementation:=true;
  315. { load the used units from implementation }
  316. b:=hp^.ppufile^.readentry;
  317. if b=ibloadunit_imp then
  318. begin
  319. while not hp^.ppufile^.endofentry do
  320. begin
  321. hs:=hp^.ppufile^.getstring;
  322. checksum:=hp^.ppufile^.getlongint;
  323. loaded_unit:=loadunit(hs,false,false);
  324. if hp^.compiled then
  325. exit;
  326. end;
  327. end;
  328. hp^.ppufile^.close;
  329. {! dispose(hp^.ppufile,done);}
  330. {$else}
  331. { load the used units from interface }
  332. hp^.ppufile^.read_data(b,1,count);
  333. while (b=ibloadunit) do
  334. begin
  335. { read unit name }
  336. hp^.ppufile^.read_data(hs[0],1,count);
  337. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  338. hp^.ppufile^.read_data(checksum,4,count);
  339. loaded_unit:=loadunit(hs,false,false);
  340. if hp^.compiled then
  341. exit;
  342. { if the crc of a used unit is the same as }
  343. { written to the PPU file, we needn't to }
  344. { recompile the current unit }
  345. if (loaded_unit^.crc<>checksum) then
  346. begin
  347. { we have to compile the current unit }
  348. { remove stuff which isn't needed }
  349. { forget the map }
  350. dispose(hp^.map);
  351. hp^.map:=nil;
  352. hp^.ppufile^.close;
  353. dispose(hp^.ppufile,done);
  354. hp^.ppufile:=nil;
  355. if not(hp^.sources_avail) then
  356. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  357. else
  358. begin
  359. {$ifdef TEST_TEMPCLOSE}
  360. if assigned(oldhp^.current_inputfile) then
  361. oldhp^.current_inputfile^.tempclose;
  362. {$endif TEST_TEMPCLOSE}
  363. compile(hp^.mainsource^,compile_system);
  364. {$ifdef TEST_TEMPCLOSE}
  365. if not oldhp^.compiled then
  366. oldhp^.current_inputfile^.tempreopen;
  367. {$endif TEST_TEMPCLOSE}
  368. end;
  369. exit;
  370. end;
  371. { setup the map entry for deref }
  372. hp^.map^[nextmapentry]:=loaded_unit^.symtable;
  373. inc(nextmapentry);
  374. if nextmapentry>maxunits then
  375. Message(unit_f_too_much_units);
  376. { read until ibend }
  377. hp^.ppufile^.read_data(b,1,count);
  378. end;
  379. { ok, now load the unit }
  380. hp^.symtable:=new(punitsymtable,load(hp^.unitname^));
  381. { if this is the system unit insert the intern }
  382. { symbols }
  383. make_ref:=false;
  384. if compile_system then
  385. insertinternsyms(psymtable(hp^.symtable));
  386. make_ref:=true;
  387. { now only read the implementation part }
  388. hp^.in_implementation:=true;
  389. { load the used units from implementation }
  390. hp^.ppufile^.read_data(b,1,count);
  391. while (b<>ibend) and (b=ibloadunit) do
  392. begin
  393. { read unit name }
  394. hp^.ppufile^.read_data(hs[0],1,count);
  395. hp^.ppufile^.read_data(hs[1],byte(hs[0]),count);
  396. hp^.ppufile^.read_data(checksum,4,count);
  397. loaded_unit:=loadunit(hs,false,false);
  398. if hp^.compiled then exit;
  399. { if the crc of a used unit is the same as }
  400. { written to the PPU file, we needn't to }
  401. { recompile the current unit }
  402. { but for the implementation part }
  403. { the written crc is false, because }
  404. { not defined when writing the ppufile !! }
  405. (* if {(loaded_unit^.crc<>checksum) or}
  406. (do_build and loaded_unit^.sources_avail) then
  407. begin
  408. { we have to compile the current unit }
  409. { remove stuff which isn't needed }
  410. { forget the map }
  411. dispose(hp^.map);
  412. hp^.map:=nil;
  413. hp^.ppufile^.close;
  414. dispose(hp^.ppufile,done);
  415. hp^.ppufile:=nil;
  416. if not(hp^.sources_avail) then
  417. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  418. else
  419. begin
  420. {ifdef TEST_TEMPCLOSE}
  421. oldhp^.current_inputfile^.tempclose;
  422. {endif TEST_TEMPCLOSE}
  423. compile(hp^.mainsource^,compile_system);
  424. {ifdef TEST_TEMPCLOSE}
  425. oldhp^.current_inputfile^.tempclose;
  426. {endif TEST_TEMPCLOSE}
  427. end;
  428. exit;
  429. end; *)
  430. { read until ibend }
  431. hp^.ppufile^.read_data(b,1,count);
  432. end;
  433. hp^.ppufile^.close;
  434. {$endif}
  435. dispose(hp^.map);
  436. hp^.map:=nil;
  437. end;
  438. function loadunit(const s : string;compile_system, in_uses : boolean) : pmodule;
  439. var
  440. st : punitsymtable;
  441. old_current_module,hp,nextmodule : pmodule;
  442. pu : pused_unit;
  443. hs : pstring;
  444. begin
  445. old_current_module:=current_module;
  446. { be sure not to mix lines from different files }
  447. { update_line; }
  448. { unit not found }
  449. st:=nil;
  450. { search all loaded units }
  451. hp:=pmodule(loaded_units.first);
  452. while assigned(hp) do
  453. begin
  454. if hp^.unitname^=s then
  455. begin
  456. { the unit is already registered }
  457. { and this means that the unit }
  458. { is already compiled }
  459. { else there is a cyclic unit use }
  460. if assigned(hp^.symtable) then
  461. st:=punitsymtable(hp^.symtable)
  462. else
  463. begin
  464. { recompile the unit ? }
  465. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  466. Message(unit_f_circular_unit_reference);
  467. end;
  468. break;
  469. end;
  470. { the next unit }
  471. hp:=pmodule(hp^.next);
  472. end;
  473. { no error and the unit isn't loaded }
  474. if not(assigned(hp)) and (st=nil) then
  475. begin
  476. { generates a new unit info record }
  477. hp:=new(pmodule,init(s,true));
  478. { now we can register the unit }
  479. loaded_units.insert(hp);
  480. current_module:=hp;
  481. { force build ? }
  482. if (hp^.do_compile) or (hp^.sources_avail and do_build) then
  483. begin
  484. { we needn't the ppufile }
  485. if assigned(hp^.ppufile) then
  486. begin
  487. dispose(hp^.ppufile,done);
  488. hp^.ppufile:=nil;
  489. end;
  490. if not(hp^.sources_avail) then
  491. Message1(unit_f_cant_compile_unit,hp^.unitname^)
  492. else
  493. begin
  494. {$ifdef TEST_TEMPCLOSE}
  495. if assigned(old_current_module^.current_inputfile) then
  496. old_current_module^.current_inputfile^.tempclose;
  497. {$endif TEST_TEMPCLOSE}
  498. compile(hp^.mainsource^,compile_system);
  499. {$ifdef TEST_TEMPCLOSE}
  500. if not old_current_module^.compiled then
  501. old_current_module^.current_inputfile^.tempreopen;
  502. {$endif TEST_TEMPCLOSE}
  503. end;
  504. end
  505. else
  506. begin
  507. { only reassemble ? }
  508. if (hp^.do_assemble) then
  509. OnlyAsm(hp^.asmfilename^);
  510. { we should know there the PPU file else it's an error and
  511. we can't load the unit }
  512. {$ifdef NEWPPU}
  513. { if hp^.ppufile^.name^<>'' then}
  514. {$else}
  515. if hp^.ppufile^.name^<>'' then
  516. {$endif}
  517. load_ppu(old_current_module,hp,compile_system);
  518. { add the files for the linker }
  519. addlinkerfiles(hp);
  520. end;
  521. { register the unit _once_ }
  522. usedunits.concat(new(pused_unit,init(hp,0)));
  523. { the unit is written, so we can set the symtable type }
  524. { to unitsymtable, else we get some dupid errors }
  525. { this is not the right place because of the }
  526. { ready label }
  527. { psymtable(hp^.symtable)^.symtabletype:=unitsymtable; }
  528. { placed at this end of proc_unit }
  529. psymtable(hp^.symtable)^.unitid:=0;
  530. { reset the unitnumbers for the other units }
  531. pu:=pused_unit(old_current_module^.used_units.first);
  532. while assigned(pu) do
  533. begin
  534. psymtable(pu^.u^.symtable)^.unitid:=pu^.unitid;
  535. pu:=pused_unit(pu^.next);
  536. end;
  537. end
  538. else
  539. if assigned(hp) and (st=nil) then
  540. begin
  541. { we have to compile the unit again, but it is already inserted !!}
  542. { we may have problem with the lost symtable !! }
  543. current_module:=hp;
  544. { we must preserve the unit chain }
  545. nextmodule:=pmodule(hp^.next);
  546. { we have to cleanup a little }
  547. hp^.special_done;
  548. new(hs);
  549. hs^:=hp^.mainsource^;
  550. hp^.init(hs^,true);
  551. dispose(hs);
  552. { we must preserve the unit chain }
  553. hp^.next:=nextmodule;
  554. if assigned(hp^.ppufile) then
  555. load_ppu(old_current_module,hp,compile_system)
  556. else
  557. begin
  558. {$ifdef UseBrowser}
  559. { here we need to remove the names ! }
  560. hp^.sourcefiles.done;
  561. hp^.sourcefiles.init;
  562. {$endif not UseBrowser}
  563. {$ifdef TEST_TEMPCLOSE}
  564. if assigned(old_current_module^.current_inputfile) then
  565. old_current_module^.current_inputfile^.tempclose;
  566. {$endif TEST_TEMPCLOSE}
  567. Message1(parser_d_compiling_second_time,hp^.mainsource^);
  568. compile(hp^.mainsource^,compile_system);
  569. {$ifdef TEST_TEMPCLOSE}
  570. if not old_current_module^.compiled then
  571. old_current_module^.current_inputfile^.tempreopen;
  572. {$endif TEST_TEMPCLOSE}
  573. end;
  574. current_module^.compiled:=true;
  575. end;
  576. { set the old module }
  577. current_module:=old_current_module;
  578. { the current module uses the unit hp }
  579. current_module^.used_units.concat(new(pused_unit,init(hp,0)));
  580. pused_unit(current_module^.used_units.last)^.in_uses:=in_uses;
  581. if in_uses and not current_module^.in_implementation then
  582. pused_unit(current_module^.used_units.last)^.in_interface:=true;
  583. loadunit:=hp;
  584. end;
  585. procedure loadunits;
  586. var
  587. s : stringid;
  588. hp : pused_unit;
  589. hp2 : pmodule;
  590. hp3 : psymtable;
  591. oldprocsym:Pprocsym;
  592. begin
  593. oldprocsym:=aktprocsym;
  594. consume(_USES);
  595. {$ifdef DEBUG}
  596. test_symtablestack;
  597. {$endif DEBUG}
  598. repeat
  599. s:=pattern;
  600. consume(ID);
  601. hp2:=loadunit(s,false,true);
  602. if current_module^.compiled then
  603. exit;
  604. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  605. if token=COMMA then
  606. begin
  607. pattern:='';
  608. consume(COMMA);
  609. end
  610. else
  611. break;
  612. until false;
  613. consume(SEMICOLON);
  614. { now insert the units in the symtablestack }
  615. hp:=pused_unit(current_module^.used_units.first);
  616. { set the symtable to systemunit so it gets reorderd correctly }
  617. symtablestack:=systemunit;
  618. while assigned(hp) do
  619. begin
  620. {$IfDef GDB}
  621. if (cs_debuginfo in aktswitches) and
  622. not hp^.is_stab_written then
  623. begin
  624. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  625. hp^.is_stab_written:=true;
  626. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  627. end;
  628. {$EndIf GDB}
  629. if hp^.in_uses then
  630. begin
  631. hp3:=symtablestack;
  632. while assigned(hp3) do
  633. begin
  634. { insert units only once ! }
  635. if hp^.u^.symtable=hp3 then
  636. break;
  637. hp3:=hp3^.next;
  638. { unit isn't inserted }
  639. if hp3=nil then
  640. begin
  641. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  642. symtablestack:=psymtable(hp^.u^.symtable);
  643. {$ifdef CHAINPROCSYMS}
  644. symtablestack^.chainprocsyms;
  645. {$endif CHAINPROCSYMS}
  646. {$ifdef DEBUG}
  647. test_symtablestack;
  648. {$endif DEBUG}
  649. end;
  650. end;
  651. end;
  652. hp:=pused_unit(hp^.next);
  653. end;
  654. aktprocsym:=oldprocsym;
  655. end;
  656. procedure parse_implementation_uses(symt:Psymtable);
  657. var
  658. old_module_in_implementation : boolean;
  659. begin
  660. if token=_USES then
  661. begin
  662. old_module_in_implementation:=module_in_implementation;
  663. module_in_implementation:=true;
  664. current_module^.in_implementation:=true;
  665. symt^.symtabletype:=unitsymtable;
  666. loadunits;
  667. symt^.symtabletype:=globalsymtable;
  668. {$ifdef DEBUG}
  669. test_symtablestack;
  670. {$endif DEBUG}
  671. module_in_implementation:=old_module_in_implementation;
  672. end;
  673. end;
  674. procedure proc_unit;
  675. var
  676. { unitname : stringid; }
  677. names:Tstringcontainer;
  678. p : psymtable;
  679. unitst : punitsymtable;
  680. pu : pused_unit;
  681. s1,s2 : ^string; {Saves stack space}
  682. begin
  683. consume(_UNIT);
  684. if token=ID then
  685. begin
  686. { create filenames and unit name }
  687. current_module^.SetFileName(current_module^.current_inputfile^.path^,current_module^.current_inputfile^.name^);
  688. current_module^.unitname:=stringdup(upper(pattern));
  689. { check for system unit }
  690. new(s1);
  691. new(s2);
  692. s1^:=upper(target_info.system_unit);
  693. s2^:=upper(current_module^.current_inputfile^.name^);
  694. if (cs_compilesystem in aktswitches) then
  695. begin
  696. if (cs_check_unit_name in aktswitches) and
  697. ((length(current_module^.unitname^)>8) or
  698. (current_module^.unitname^<>s1^) or
  699. (current_module^.unitname^<>s2^)) then
  700. Message1(unit_e_illegal_unit_name,s1^);
  701. end
  702. else
  703. if (current_module^.unitname^=s1^) then
  704. Message(unit_w_switch_us_missed);
  705. dispose(s2);
  706. dispose(s1);
  707. { Add Object File }
  708. if smartlink then
  709. current_module^.linkstaticlibs.insert(current_module^.arfilename^)
  710. else
  711. current_module^.linkofiles.insert(current_module^.objfilename^);
  712. end;
  713. consume(ID);
  714. consume(SEMICOLON);
  715. consume(_INTERFACE);
  716. { this should be placed after uses !!}
  717. {$ifndef UseNiceNames}
  718. procprefix:='_'+current_module^.unitname^+'$$';
  719. {$else UseNiceNames}
  720. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  721. {$endif UseNiceNames}
  722. parse_only:=true;
  723. { generate now the global symboltable }
  724. p:=new(punitsymtable,init(globalsymtable,current_module^.unitname^));
  725. refsymtable:=p;
  726. unitst:=punitsymtable(p);
  727. { the unit name must be usable as a unit specifier }
  728. { inside the unit itself (PM) }
  729. { this also forbids to have another symbol }
  730. { with the same name as the unit }
  731. refsymtable^.insert(new(punitsym,init(current_module^.unitname^,unitst)));
  732. { set the symbol table for the current unit }
  733. { this must be set later for interdependency }
  734. { current_module^.symtable:=psymtable(p); }
  735. { a unit compiled at command line must be inside the loaded_unit list }
  736. if (compile_level=1) then
  737. begin
  738. loaded_units.insert(current_module);
  739. if cs_unit_to_lib in initswitches then
  740. begin
  741. current_module^.flags:=current_module^.flags or uf_in_library;
  742. if cs_shared_lib in initswitches then
  743. current_module^.flags:=current_module^.flags or uf_shared_library;
  744. end;
  745. end;
  746. { insert qualifier for the system unit (allows system.writeln) }
  747. if not(cs_compilesystem in aktswitches) then
  748. begin
  749. { insert the system unit }
  750. { it is allways the first }
  751. systemunit^.next:=nil;
  752. symtablestack:=systemunit;
  753. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  754. if token=_USES then
  755. begin
  756. unitst^.symtabletype:=unitsymtable;
  757. loadunits;
  758. { has it been compiled at a higher level ?}
  759. if current_module^.compiled then
  760. exit;
  761. unitst^.symtabletype:=globalsymtable;
  762. end;
  763. { ... but insert the symbol table later }
  764. p^.next:=symtablestack;
  765. symtablestack:=p;
  766. end
  767. else
  768. { while compiling a system unit, some types are directly inserted }
  769. begin
  770. p^.next:=symtablestack;
  771. symtablestack:=p;
  772. insert_intern_types(p);
  773. end;
  774. { displaced for inter-dependency considerations }
  775. current_module^.symtable:=psymtable(p);
  776. constsymtable:=symtablestack;
  777. { ... parse the declarations }
  778. read_interface_declarations;
  779. consume(_IMPLEMENTATION);
  780. parse_only:=false;
  781. refsymtable^.number_defs;
  782. {$ifdef GDB}
  783. { add all used definitions even for implementation}
  784. if (cs_debuginfo in aktswitches) then
  785. begin
  786. { all types }
  787. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  788. { and all local symbols}
  789. refsymtable^.concatstabto(debuglist);
  790. end;
  791. {$endif GDB}
  792. { for interdependent units
  793. the crc is included in the ppufile
  794. but it is not known when writing the first ppufile
  795. so I tried to add a fake writing of the ppu
  796. just to get the CRC
  797. but the result is different for the real CRC
  798. it calculates after, I don't know why
  799. Answer:
  800. -------
  801. When reading the interface part, the compiler assumes
  802. that all registers are modified by a procedure
  803. usedinproc:=$ff !
  804. If the definition is read, the compiler determines
  805. the used registers and write the correct value
  806. to usedinproc
  807. only_calculate_crc:=true;
  808. writeunitas(current_module^.current_inputfile^.path^+current_module^.current_inputfile^.name^+
  809. +'.PPS',punitsymtable(symtablestack));
  810. only_calculate_crc:=false;
  811. }
  812. { generates static symbol table }
  813. p:=new(punitsymtable,init(staticsymtable,current_module^.unitname^));
  814. { must be done only after _USES !! (PM)
  815. refsymtable:=p;}
  816. {Generate a procsym.}
  817. aktprocsym:=new(Pprocsym,init(current_module^.unitname^+'_init'));
  818. aktprocsym^.definition:=new(Pprocdef,init);
  819. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  820. aktprocsym^.definition^.setmangledname(current_module^.unitname^+'_init');
  821. {The generated procsym has a local symtable. Discard it and turn
  822. it into the static one.}
  823. dispose(aktprocsym^.definition^.localst,done);
  824. aktprocsym^.definition^.localst:=p;
  825. { testing !!!!!!!!! }
  826. { we set the interface part as a unitsymtable }
  827. { for the case we need to compile another unit }
  828. { remove the globalsymtable from the symtable stack }
  829. { to reinsert it after loading the implementation units }
  830. symtablestack:=unitst^.next;
  831. parse_implementation_uses(unitst);
  832. { now we can change refsymtable }
  833. refsymtable:=p;
  834. { but reinsert the global symtable as lasts }
  835. unitst^.next:=symtablestack;
  836. symtablestack:=unitst;
  837. {$ifdef DEBUG}
  838. test_symtablestack;
  839. {$endif DEBUG}
  840. constsymtable:=symtablestack;
  841. {$ifdef Splitheap}
  842. if testsplit then
  843. begin
  844. Split_Heap;
  845. allow_special:=true;
  846. Switch_to_temp_heap;
  847. end;
  848. { it will report all crossings }
  849. allow_special:=false;
  850. {$endif Splitheap}
  851. { set some informations }
  852. procinfo.retdef:=voiddef;
  853. procinfo._class:=nil;
  854. procinfo.call_offset:=8;
  855. { for temporary values }
  856. procinfo.framepointer:=frame_pointer;
  857. { clear flags }
  858. procinfo.flags:=0;
  859. {Reset the codegenerator.}
  860. codegen_newprocedure;
  861. names.init;
  862. names.insert(current_module^.unitname^+'_init');
  863. names.insert('INIT$$'+current_module^.unitname^);
  864. compile_proc_body(names,true,false);
  865. names.done;
  866. codegen_doneprocedure;
  867. consume(POINT);
  868. { size of the static data }
  869. datasize:=symtablestack^.datasize;
  870. { unsed static symbols ? }
  871. symtablestack^.allsymbolsused;
  872. {$ifdef GDB}
  873. { add all used definitions even for implementation}
  874. if (cs_debuginfo in aktswitches) then
  875. begin
  876. { all types }
  877. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  878. { and all local symbols}
  879. symtablestack^.concatstabto(debuglist);
  880. end;
  881. {$endif GDB}
  882. current_module^.in_implementation:=false;
  883. { deletes all symtables generated in the implementation part }
  884. while symtablestack^.symtabletype<>globalsymtable do
  885. dellexlevel;
  886. { tests, if all forwards are resolved }
  887. symtablestack^.check_forwards;
  888. symtablestack^.symtabletype:=unitsymtable;
  889. punitsymtable(symtablestack)^.is_stab_written:=false;
  890. {Write out the unit if the compile was succesfull.}
  891. if errorcount=0 then
  892. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  893. pu:=pused_unit(usedunits.first);
  894. while assigned(pu) do
  895. begin
  896. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  897. pu:=pused_unit(pu^.next);
  898. end;
  899. inc(datasize,symtablestack^.datasize);
  900. { finish asmlist by adding segment starts }
  901. insertsegment;
  902. end;
  903. procedure proc_program(islibrary : boolean);
  904. var
  905. st : psymtable;
  906. programname : stringid;
  907. names:Tstringcontainer;
  908. begin
  909. { Trying to compile the system unit... }
  910. { if no unit defined... then issue a }
  911. { fatal error (avoids pointer problems)}
  912. { when referencing the non-existant }
  913. { system unit. }
  914. { System Unit should be compiled using proc_unit !! (PFV) }
  915. { if (cs_compilesystem in aktswitches) then
  916. Begin
  917. if token<>_UNIT then
  918. Message1(scan_f_syn_expected,'UNIT');
  919. consume(_UNIT);
  920. end;}
  921. parse_only:=false;
  922. programname:='';
  923. if islibrary then
  924. begin
  925. consume(_LIBRARY);
  926. programname:=pattern;
  927. consume(ID);
  928. consume(SEMICOLON);
  929. end
  930. else
  931. { is there an program head ? }
  932. if token=_PROGRAM then
  933. begin
  934. consume(_PROGRAM);
  935. programname:=pattern;
  936. consume(ID);
  937. if token=LKLAMMER then
  938. begin
  939. consume(LKLAMMER);
  940. idlist;
  941. consume(RKLAMMER);
  942. end;
  943. consume(SEMICOLON);
  944. end;
  945. { insert after the unit symbol tables the static symbol table }
  946. { of the program }
  947. st:=new(punitsymtable,init(staticsymtable,programname));
  948. {Generate a procsym.}
  949. aktprocsym:=new(Pprocsym,init('main'));
  950. aktprocsym^.definition:=new(Pprocdef,init);
  951. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  952. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  953. {The localst is a local symtable. Change it into the static
  954. symtable.}
  955. dispose(aktprocsym^.definition^.localst,done);
  956. aktprocsym^.definition^.localst:=st;
  957. refsymtable:=st;
  958. { necessary for browser }
  959. loaded_units.insert(current_module);
  960. {Insert the symbols of the system unit into the stack of symbol
  961. tables.}
  962. symtablestack:=systemunit;
  963. systemunit^.next:=nil;
  964. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  965. {Load the units used by the program we compile.}
  966. if token=_USES then
  967. loadunits;
  968. {Insert the name of the main program into the symbol table.}
  969. if programname<>'' then
  970. st^.insert(new(pprogramsym,init(programname)));
  971. { ...is also constsymtable, this is the symtable where }
  972. { the elements of enumeration types are inserted }
  973. constsymtable:=st;
  974. { set some informations about the main program }
  975. procinfo.retdef:=voiddef;
  976. procinfo._class:=nil;
  977. procinfo.call_offset:=8;
  978. {Set the framepointer of the program initialization to the
  979. default framepointer (EBP on i386).}
  980. procinfo.framepointer:=frame_pointer;
  981. { clear flags }
  982. procinfo.flags:=0;
  983. procprefix:='';
  984. in_except_block:=false;
  985. codegen_newprocedure;
  986. {The program intialization needs an alias, so it can be called
  987. from the bootstrap code.}
  988. names.init;
  989. names.insert('program_init');
  990. names.insert('PASCALMAIN');
  991. names.insert(target_os.cprefix+'main');
  992. compile_proc_body(names,true,false);
  993. names.done;
  994. codegen_doneprocedure;
  995. consume(POINT);
  996. if smartlink then
  997. current_module^.linkstaticlibs.insert(current_module^.arfilename^)
  998. else
  999. current_module^.linkofiles.insert(current_module^.objfilename^);
  1000. insertheap;
  1001. inserttargetspecific;
  1002. datasize:=symtablestack^.datasize;
  1003. { symtablestack^.check_forwards;
  1004. symtablestack^.allsymbolsused;
  1005. done in compile_proc_body }
  1006. { finish asmlist by adding segment starts }
  1007. insertsegment;
  1008. end;
  1009. end.
  1010. {
  1011. $Log$
  1012. Revision 1.14 1998-05-20 09:42:35 pierre
  1013. + UseTokenInfo now default
  1014. * unit in interface uses and implementation uses gives error now
  1015. * only one error for unknown symbol (uses lastsymknown boolean)
  1016. the problem came from the label code !
  1017. + first inlined procedures and function work
  1018. (warning there might be allowed cases were the result is still wrong !!)
  1019. * UseBrower updated gives a global list of all position of all used symbols
  1020. with switch -gb
  1021. Revision 1.13 1998/05/12 10:47:00 peter
  1022. * moved printstatus to verb_def
  1023. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1024. prefix like error: warning: and is included in V_Default
  1025. * fixed some messages
  1026. * first time parameter scan is only for -v and -T
  1027. - removed old style messages
  1028. Revision 1.12 1998/05/11 13:07:56 peter
  1029. + $ifdef NEWPPU for the new ppuformat
  1030. + $define GDB not longer required
  1031. * removed all warnings and stripped some log comments
  1032. * no findfirst/findnext anymore to remove smartlink *.o files
  1033. Revision 1.11 1998/05/06 18:36:53 peter
  1034. * tai_section extended with code,data,bss sections and enumerated type
  1035. * ident 'compiled by FPC' moved to pmodules
  1036. * small fix for smartlink
  1037. Revision 1.10 1998/05/04 17:54:28 peter
  1038. + smartlinking works (only case jumptable left todo)
  1039. * redesign of systems.pas to support assemblers and linkers
  1040. + Unitname is now also in the PPU-file, increased version to 14
  1041. Revision 1.9 1998/05/01 16:38:45 florian
  1042. * handling of private and protected fixed
  1043. + change_keywords_to_tp implemented to remove
  1044. keywords which aren't supported by tp
  1045. * break and continue are now symbols of the system unit
  1046. + widestring, longstring and ansistring type released
  1047. Revision 1.8 1998/04/30 15:59:41 pierre
  1048. * GDB works again better :
  1049. correct type info in one pass
  1050. + UseTokenInfo for better source position
  1051. * fixed one remaining bug in scanner for line counts
  1052. * several little fixes
  1053. Revision 1.7 1998/04/29 10:33:59 pierre
  1054. + added some code for ansistring (not complete nor working yet)
  1055. * corrected operator overloading
  1056. * corrected nasm output
  1057. + started inline procedures
  1058. + added starstarn : use ** for exponentiation (^ gave problems)
  1059. + started UseTokenInfo cond to get accurate positions
  1060. Revision 1.6 1998/04/27 23:10:28 peter
  1061. + new scanner
  1062. * $makelib -> if smartlink
  1063. * small filename fixes pmodule.setfilename
  1064. * moved import from files.pas -> import.pas
  1065. Revision 1.5 1998/04/14 23:27:03 florian
  1066. + exclude/include with constant second parameter added
  1067. Revision 1.4 1998/04/10 14:41:43 peter
  1068. * removed some Hints
  1069. * small speed optimization for AsmLn
  1070. Revision 1.3 1998/04/03 09:51:00 daniel
  1071. * Fixed heap allocation for OS/2.
  1072. }