pmodules.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. {define TEST_IMPL does not work well }
  20. interface
  21. procedure loadsystemunit;
  22. procedure proc_unit;
  23. procedure proc_program(islibrary : boolean);
  24. implementation
  25. uses
  26. cobjects,comphook,systems,globals,
  27. symtable,aasm,files,
  28. hcodegen,verbose, { don't use hcodegen.message !! }
  29. link,assemble,import,gendef,ppu
  30. {$ifdef i386}
  31. ,i386
  32. {$endif}
  33. {$ifdef m68k}
  34. ,m68k
  35. {$endif}
  36. ,scanner,pbase,psystem,pdecl,psub,parser;
  37. procedure create_objectfile;
  38. begin
  39. { create the .s file and assemble it }
  40. GenerateAsm;
  41. { When creating a library call the linker. And insert the output
  42. of the linker files }
  43. if (cs_create_sharedlib in aktmoduleswitches) then
  44. Linker.MakeSharedLibrary
  45. else
  46. if (cs_create_staticlib in aktmoduleswitches) or
  47. (cs_smartlink in aktmoduleswitches) then
  48. Linker.MakeStaticLibrary(SmartLinkFilesCnt);
  49. { add the files for the linker from current_module }
  50. Linker.AddModuleFiles(current_module);
  51. end;
  52. procedure insertobjectfile;
  53. { Insert the used object file for this unit in the used list for this unit }
  54. begin
  55. if (cs_create_sharedlib in aktmoduleswitches) then
  56. current_module^.linksharedlibs.insert(current_module^.sharedlibfilename^)
  57. else
  58. begin
  59. if (cs_create_staticlib in aktmoduleswitches) or
  60. (cs_smartlink in aktmoduleswitches) then
  61. current_module^.linkstaticlibs.insert(current_module^.staticlibfilename^)
  62. else
  63. current_module^.linkofiles.insert(current_module^.objfilename^);
  64. end;
  65. end;
  66. procedure insertsegment;
  67. procedure fixseg(p:paasmoutput;sec:tsection);
  68. begin
  69. p^.insert(new(pai_section,init(sec)));
  70. if (cs_smartlink in aktmoduleswitches) then
  71. p^.insert(new(pai_cut,init));
  72. p^.concat(new(pai_section,init(sec_none)));
  73. end;
  74. begin
  75. {Insert Ident of the compiler}
  76. if (not (cs_smartlink in aktmoduleswitches))
  77. {$ifndef EXTDEBUG}
  78. and (not current_module^.is_unit)
  79. {$endif}
  80. then
  81. begin
  82. datasegment^.insert(new(pai_align,init(4)));
  83. datasegment^.insert(new(pai_string,init('FPC '+version_string+' for '+target_string+' - '+target_info.short_name)));
  84. end;
  85. { Insert start and end of sections }
  86. fixseg(codesegment,sec_code);
  87. fixseg(datasegment,sec_data);
  88. fixseg(bsssegment,sec_bss);
  89. fixseg(consts,sec_data);
  90. end;
  91. procedure insertheap;
  92. begin
  93. if (cs_smartlink in aktmoduleswitches) then
  94. begin
  95. bsssegment^.concat(new(pai_cut,init));
  96. datasegment^.concat(new(pai_cut,init));
  97. end;
  98. { On the Macintosh Classic M68k Architecture
  99. The Heap variable is simply a POINTER to the
  100. real HEAP. The HEAP must be set up by the RTL
  101. and must store the pointer in this value.
  102. On OS/2 the heap is also intialized by the RTL. We do
  103. not output a pointer }
  104. case target_info.target of
  105. {$ifdef i386}
  106. target_OS2 : ;
  107. {$endif i386}
  108. {$ifdef m68k}
  109. target_Mac68K : bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  110. {$endif m68k}
  111. else
  112. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  113. end;
  114. {$ifdef i386}
  115. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  116. {$endif i386}
  117. {$ifdef m68k}
  118. datasegment^.concat(new(pai_symbol,init_global('HEAP_SIZE')));
  119. {$endif m68k}
  120. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  121. end;
  122. procedure inserttargetspecific;
  123. begin
  124. case target_info.target of
  125. {$ifdef i386}
  126. target_GO32V2 : begin
  127. { stacksize can be specified }
  128. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  129. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  130. end;
  131. target_WIN32 : begin
  132. { Generate an external entry to be sure that _mainCRTStarup will be
  133. linked, can't use concat_external because those aren't written for
  134. asw (PFV) }
  135. datasegment^.concat(new(pai_const,init_symbol('_mainCRTStartup')));
  136. end;
  137. {$endif i386}
  138. {$ifdef m68k}
  139. target_Atari : begin
  140. { stacksize can be specified }
  141. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  142. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  143. end;
  144. {$endif m68k}
  145. end;
  146. end;
  147. function loadunit(const s : string;compile_system:boolean) : pmodule;forward;
  148. procedure load_usedunits(compile_system:boolean);
  149. var
  150. pu : pused_unit;
  151. loaded_unit : pmodule;
  152. nextmapentry : longint;
  153. begin
  154. { init the map }
  155. new(current_module^.map);
  156. fillchar(current_module^.map^,sizeof(tunitmap),#0);
  157. nextmapentry:=1;
  158. { load the used units from interface }
  159. pu:=pused_unit(current_module^.used_units.first);
  160. while assigned(pu) do
  161. begin
  162. if (not pu^.loaded) and (pu^.in_interface) then
  163. begin
  164. loaded_unit:=loadunit(pu^.name^,false);
  165. if current_module^.compiled then
  166. exit;
  167. { register unit in used units }
  168. pu^.u:=loaded_unit;
  169. pu^.loaded:=true;
  170. { need to recompile the current unit ? }
  171. if loaded_unit^.crc<>pu^.checksum then
  172. begin
  173. current_module^.do_compile:=true;
  174. exit;
  175. end;
  176. { setup the map entry for deref }
  177. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  178. inc(nextmapentry);
  179. if nextmapentry>maxunits then
  180. Message(unit_f_too_much_units);
  181. end;
  182. pu:=pused_unit(pu^.next);
  183. end;
  184. { ok, now load the unit }
  185. current_module^.symtable:=new(punitsymtable,loadasunit);
  186. { if this is the system unit insert the intern symbols }
  187. if compile_system then
  188. begin
  189. make_ref:=false;
  190. insertinternsyms(psymtable(current_module^.symtable));
  191. make_ref:=true;
  192. end;
  193. { now only read the implementation part }
  194. current_module^.in_implementation:=true;
  195. { load the used units from implementation }
  196. pu:=pused_unit(current_module^.used_units.first);
  197. while assigned(pu) do
  198. begin
  199. if (not pu^.loaded) and (not pu^.in_interface) then
  200. begin
  201. loaded_unit:=loadunit(pu^.name^,false);
  202. if current_module^.compiled then
  203. exit;
  204. { register unit in used units }
  205. pu^.u:=loaded_unit;
  206. pu^.loaded:=true;
  207. {$ifdef TEST_IMPL}
  208. { need to recompile the current unit ? }
  209. if loaded_unit^.crc<>pu^.checksum then
  210. begin
  211. current_module^.do_compile:=true;
  212. exit;
  213. end;
  214. {$endif TEST_IMPL}
  215. { setup the map entry for deref }
  216. current_module^.map^[nextmapentry]:=loaded_unit^.symtable;
  217. inc(nextmapentry);
  218. if nextmapentry>maxunits then
  219. Message(unit_f_too_much_units);
  220. end;
  221. pu:=pused_unit(pu^.next);
  222. end;
  223. {$ifdef UseBrowser}
  224. if cs_browser in aktmoduleswitches then
  225. begin
  226. punitsymtable(current_module^.symtable)^.
  227. load_symtable_refs;
  228. end;
  229. if ((current_module^.flags and uf_has_browser)<>0) and
  230. (cs_local_browser in aktmoduleswitches) then
  231. begin
  232. current_module^.implsymtable:=new(psymtable,load);
  233. psymtable(current_module^.implsymtable)^.name:=
  234. stringdup('implementation of '+psymtable(current_module^.symtable)^.name^);
  235. psymtable(current_module^.implsymtable)^.load_browser;
  236. end;
  237. {$endif UseBrowser}
  238. { remove the map, it's not needed anymore }
  239. dispose(current_module^.map);
  240. current_module^.map:=nil;
  241. end;
  242. function loadunit(const s : string;compile_system:boolean) : pmodule;
  243. var
  244. st : punitsymtable;
  245. old_current_ppu : pppufile;
  246. old_current_module,hp : pmodule;
  247. procedure loadppufile;
  248. begin
  249. { load interface section }
  250. if not current_module^.do_compile then
  251. load_interface;
  252. { only load units when we don't recompile }
  253. if not current_module^.do_compile then
  254. load_usedunits(compile_system);
  255. { recompile if set }
  256. if current_module^.do_compile then
  257. begin
  258. { we needn't the ppufile }
  259. if assigned(current_module^.ppufile) then
  260. begin
  261. dispose(current_module^.ppufile,done);
  262. current_module^.ppufile:=nil;
  263. end;
  264. { recompile the unit or give a fatal error if sources not available }
  265. if not(current_module^.sources_avail) then
  266. Message1(unit_f_cant_compile_unit,current_module^.modulename^)
  267. else
  268. begin
  269. current_scanner^.tempcloseinputfile;
  270. compile(current_module^.mainsource^,compile_system);
  271. if (not old_current_module^.compiled) then
  272. current_scanner^.tempopeninputfile;
  273. end;
  274. end
  275. else
  276. begin
  277. { only reassemble ? }
  278. if (current_module^.do_assemble) then
  279. OnlyAsm;
  280. { add the files for the linker }
  281. Linker.AddModuleFiles(current_module);
  282. end;
  283. if assigned(current_module^.ppufile) then
  284. begin
  285. dispose(current_module^.ppufile,done);
  286. current_module^.ppufile:=nil;
  287. end;
  288. end;
  289. begin
  290. old_current_module:=current_module;
  291. old_current_ppu:=current_ppu;
  292. { be sure not to mix lines from different files }
  293. { update_line; }
  294. { unit not found }
  295. st:=nil;
  296. { search all loaded units }
  297. hp:=pmodule(loaded_units.first);
  298. while assigned(hp) do
  299. begin
  300. if hp^.modulename^=s then
  301. begin
  302. { the unit is already registered }
  303. { and this means that the unit }
  304. { is already compiled }
  305. { else there is a cyclic unit use }
  306. if assigned(hp^.symtable) then
  307. st:=punitsymtable(hp^.symtable)
  308. else
  309. begin
  310. { recompile the unit ? }
  311. if (not current_module^.in_implementation) and (hp^.in_implementation) then
  312. Message(unit_f_circular_unit_reference);
  313. end;
  314. break;
  315. end;
  316. { the next unit }
  317. hp:=pmodule(hp^.next);
  318. end;
  319. { the unit is not in the symtable stack }
  320. if (not assigned(st)) then
  321. begin
  322. { if the unit is loaded remove it first }
  323. { this creates problem with the browser !! }
  324. if assigned(hp) then
  325. begin
  326. { remove the old unit }
  327. loaded_units.remove(hp);
  328. dispose(hp,done);
  329. end;
  330. { generates a new unit info record }
  331. current_module:=new(pmodule,init(s,true));
  332. current_ppu:=current_module^.ppufile;
  333. { now we can register the unit }
  334. loaded_units.insert(current_module);
  335. { now realy load the ppu }
  336. loadppufile;
  337. { set compiled flag }
  338. current_module^.compiled:=true;
  339. { register the unit _once_ }
  340. usedunits.concat(new(pused_unit,init(current_module,true)));
  341. { load return pointer }
  342. hp:=current_module;
  343. end;
  344. { set the old module }
  345. current_ppu:=old_current_ppu;
  346. current_module:=old_current_module;
  347. loadunit:=hp;
  348. end;
  349. procedure loadsystemunit;
  350. var
  351. hp : pmodule;
  352. begin
  353. { if the current file isn't a system unit the the system unit
  354. will be loaded }
  355. if not(cs_compilesystem in aktmoduleswitches) then
  356. begin
  357. hp:=loadunit(upper(target_info.system_unit),true);
  358. systemunit:=hp^.symtable;
  359. { add to the used units }
  360. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  361. { read default constant definitions }
  362. make_ref:=false;
  363. readconstdefs;
  364. { we could try to overload caret by default }
  365. symtablestack:=systemunit;
  366. { if POWER is defined in the RTL then use it for starstar overloading }
  367. getsym('POWER',false);
  368. if assigned(srsym) and (srsym^.typ=procsym) and
  369. (overloaded_operators[STARSTAR]=nil) then
  370. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  371. make_ref:=true;
  372. end
  373. else
  374. begin
  375. createconstdefs;
  376. systemunit:=nil;
  377. end;
  378. end;
  379. procedure loadunits;
  380. var
  381. s : stringid;
  382. hp : pused_unit;
  383. hp2 : pmodule;
  384. hp3 : psymtable;
  385. oldprocsym:Pprocsym;
  386. begin
  387. oldprocsym:=aktprocsym;
  388. consume(_USES);
  389. {$ifdef DEBUG}
  390. test_symtablestack;
  391. {$endif DEBUG}
  392. repeat
  393. s:=pattern;
  394. consume(ID);
  395. hp2:=loadunit(s,false);
  396. { the current module uses the unit hp2 }
  397. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  398. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  399. if current_module^.compiled then
  400. exit;
  401. refsymtable^.insert(new(punitsym,init(s,hp2^.symtable)));
  402. if token=COMMA then
  403. begin
  404. pattern:='';
  405. consume(COMMA);
  406. end
  407. else
  408. break;
  409. until false;
  410. consume(SEMICOLON);
  411. { set the symtable to systemunit so it gets reorderd correctly }
  412. symtablestack:=systemunit;
  413. { now insert the units in the symtablestack }
  414. hp:=pused_unit(current_module^.used_units.first);
  415. while assigned(hp) do
  416. begin
  417. {$IfDef GDB}
  418. if (cs_debuginfo in aktmoduleswitches) and
  419. not hp^.is_stab_written then
  420. begin
  421. punitsymtable(hp^.u^.symtable)^.concattypestabto(debuglist);
  422. hp^.is_stab_written:=true;
  423. hp^.unitid:=psymtable(hp^.u^.symtable)^.unitid;
  424. end;
  425. {$EndIf GDB}
  426. if hp^.in_uses then
  427. begin
  428. hp3:=symtablestack;
  429. while assigned(hp3) do
  430. begin
  431. { insert units only once ! }
  432. if hp^.u^.symtable=hp3 then
  433. break;
  434. hp3:=hp3^.next;
  435. { unit isn't inserted }
  436. if hp3=nil then
  437. begin
  438. psymtable(hp^.u^.symtable)^.next:=symtablestack;
  439. symtablestack:=psymtable(hp^.u^.symtable);
  440. {$ifdef CHAINPROCSYMS}
  441. symtablestack^.chainprocsyms;
  442. {$endif CHAINPROCSYMS}
  443. {$ifdef DEBUG}
  444. test_symtablestack;
  445. {$endif DEBUG}
  446. end;
  447. end;
  448. end;
  449. hp:=pused_unit(hp^.next);
  450. end;
  451. aktprocsym:=oldprocsym;
  452. end;
  453. procedure parse_implementation_uses(symt:Psymtable);
  454. begin
  455. if token=_USES then
  456. begin
  457. symt^.symtabletype:=unitsymtable;
  458. loadunits;
  459. symt^.symtabletype:=globalsymtable;
  460. {$ifdef DEBUG}
  461. test_symtablestack;
  462. {$endif DEBUG}
  463. end;
  464. end;
  465. procedure proc_unit;
  466. var
  467. { unitname : stringid; }
  468. names : Tstringcontainer;
  469. p : psymtable;
  470. unitst : punitsymtable;
  471. pu : pused_unit;
  472. i : longint;
  473. s1,s2 : ^string; {Saves stack space}
  474. begin
  475. consume(_UNIT);
  476. if token=ID then
  477. begin
  478. { create filenames and unit name }
  479. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^);
  480. stringdispose(current_module^.modulename);
  481. current_module^.modulename:=stringdup(upper(pattern));
  482. { check for system unit }
  483. new(s1);
  484. new(s2);
  485. s1^:=upper(target_info.system_unit);
  486. s2^:=upper(current_scanner^.inputfile^.name^);
  487. { strip extension, there could only be one dot }
  488. i:=pos('.',s2^);
  489. if i>0 then
  490. s2^:=Copy(s2^,1,i-1);
  491. if (cs_compilesystem in aktmoduleswitches) then
  492. begin
  493. if (cs_check_unit_name in aktglobalswitches) and
  494. ((length(current_module^.modulename^)>8) or
  495. (current_module^.modulename^<>s1^) or
  496. (current_module^.modulename^<>s2^)) then
  497. Message1(unit_e_illegal_unit_name,s1^);
  498. end
  499. else
  500. if (current_module^.modulename^=s1^) then
  501. Message(unit_w_switch_us_missed);
  502. dispose(s2);
  503. dispose(s1);
  504. end;
  505. consume(ID);
  506. consume(SEMICOLON);
  507. consume(_INTERFACE);
  508. { global switches are read, so further changes aren't allowed }
  509. current_module^.in_global:=false;
  510. { update status }
  511. status.currentmodule:=current_module^.modulename^;
  512. { this should be placed after uses !!}
  513. {$ifndef UseNiceNames}
  514. procprefix:='_'+current_module^.modulename^+'$$';
  515. {$else UseNiceNames}
  516. procprefix:='_'+tostr(length(current_module^.unitname^))+lowercase(current_module^.unitname^)+'_';
  517. {$endif UseNiceNames}
  518. parse_only:=true;
  519. { generate now the global symboltable }
  520. p:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  521. refsymtable:=p;
  522. unitst:=punitsymtable(p);
  523. { the unit name must be usable as a unit specifier }
  524. { inside the unit itself (PM) }
  525. { this also forbids to have another symbol }
  526. { with the same name as the unit }
  527. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  528. { set the symbol table for the current unit }
  529. { this must be set later for interdependency }
  530. { current_module^.symtable:=psymtable(p); }
  531. { a unit compiled at command line must be inside the loaded_unit list }
  532. if (compile_level=1) then
  533. loaded_units.insert(current_module);
  534. { insert qualifier for the system unit (allows system.writeln) }
  535. if not(cs_compilesystem in aktmoduleswitches) then
  536. begin
  537. { insert the system unit }
  538. { it is allways the first }
  539. systemunit^.next:=nil;
  540. symtablestack:=systemunit;
  541. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  542. if token=_USES then
  543. begin
  544. unitst^.symtabletype:=unitsymtable;
  545. loadunits;
  546. { has it been compiled at a higher level ?}
  547. if current_module^.compiled then
  548. exit;
  549. unitst^.symtabletype:=globalsymtable;
  550. end;
  551. { ... but insert the symbol table later }
  552. p^.next:=symtablestack;
  553. symtablestack:=p;
  554. end
  555. else
  556. { while compiling a system unit, some types are directly inserted }
  557. begin
  558. p^.next:=symtablestack;
  559. symtablestack:=p;
  560. insert_intern_types(p);
  561. end;
  562. { displaced for inter-dependency considerations }
  563. current_module^.symtable:=psymtable(p);
  564. constsymtable:=symtablestack;
  565. { ... parse the declarations }
  566. read_interface_declarations;
  567. {$ifdef GDB}
  568. { add all used definitions}
  569. if (cs_debuginfo in aktmoduleswitches) then
  570. begin
  571. { all types }
  572. punitsymtable(refsymtable)^.concattypestabto(debuglist);
  573. { and all local symbols}
  574. refsymtable^.concatstabto(debuglist);
  575. end;
  576. {$endif GDB}
  577. { Parse the implementation section }
  578. consume(_IMPLEMENTATION);
  579. current_module^.in_implementation:=true;
  580. parse_only:=false;
  581. { generates static symbol table }
  582. p:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  583. {Generate a procsym.}
  584. make_ref:=false;
  585. aktprocsym:=new(Pprocsym,init(current_module^.modulename^+'_init'));
  586. aktprocsym^.definition:=new(Pprocdef,init);
  587. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or pounitinit;
  588. aktprocsym^.definition^.setmangledname(current_module^.modulename^+'_init');
  589. make_ref:=true;
  590. {The generated procsym has a local symtable. Discard it and turn
  591. it into the static one.}
  592. dispose(aktprocsym^.definition^.localst,done);
  593. aktprocsym^.definition^.localst:=p;
  594. { remove the globalsymtable from the symtable stack }
  595. { to reinsert it after loading the implementation units }
  596. symtablestack:=unitst^.next;
  597. { number the definitions, so a deref from other units works }
  598. refsymtable^.number_defs;
  599. {$ifdef UseBrowser}
  600. refsymtable^.number_symbols;
  601. { we don't want implementation units symbols in unitsymtable !! PM }
  602. refsymtable:=p;
  603. {$endif UseBrowser}
  604. { Read the implementation units }
  605. parse_implementation_uses(unitst);
  606. numberunits;
  607. { now we can change refsymtable }
  608. refsymtable:=p;
  609. { but reinsert the global symtable as lasts }
  610. unitst^.next:=symtablestack;
  611. symtablestack:=unitst;
  612. {$ifdef DEBUG}
  613. test_symtablestack;
  614. {$endif DEBUG}
  615. constsymtable:=symtablestack;
  616. {$ifdef Splitheap}
  617. if testsplit then
  618. begin
  619. Split_Heap;
  620. allow_special:=true;
  621. Switch_to_temp_heap;
  622. end;
  623. { it will report all crossings }
  624. allow_special:=false;
  625. {$endif Splitheap}
  626. { set some informations }
  627. procinfo.retdef:=voiddef;
  628. procinfo._class:=nil;
  629. procinfo.call_offset:=8;
  630. { for temporary values }
  631. procinfo.framepointer:=frame_pointer;
  632. { clear flags }
  633. procinfo.flags:=0;
  634. { Create a new procedure }
  635. codegen_newprocedure;
  636. { Compile the unit }
  637. names.init;
  638. names.insert(current_module^.modulename^+'_init');
  639. names.insert('INIT$$'+current_module^.modulename^);
  640. compile_proc_body(names,true,false);
  641. names.done;
  642. { Shutdown the codegen for this procedure }
  643. codegen_doneprocedure;
  644. {$ifdef dummy}
  645. if token=_FINALIZATION then
  646. begin
  647. current_module^.flags:=current_module^.flags or uf_finalize;
  648. { clear flags }
  649. procinfo.flags:=0;
  650. {Reset the codegenerator.}
  651. codegen_newprocedure;
  652. names.init;
  653. names.insert(current_module^.modulename^+'_finalize');
  654. names.insert('FINALIZE$$'+current_module^.modulename^);
  655. compile_proc_body(names,true,false);
  656. names.done;
  657. codegen_doneprocedure;
  658. end;
  659. {$endif dummy}
  660. consume(POINT);
  661. { size of the static data }
  662. datasize:=symtablestack^.datasize;
  663. { avoid self recursive destructor call !! PM }
  664. aktprocsym^.definition^.localst:=nil;
  665. { unsed static symbols ? }
  666. symtablestack^.allsymbolsused;
  667. {$ifdef GDB}
  668. { add all used definitions even for implementation}
  669. if (cs_debuginfo in aktmoduleswitches) then
  670. begin
  671. { all types }
  672. punitsymtable(symtablestack)^.concattypestabto(debuglist);
  673. { and all local symbols}
  674. symtablestack^.concatstabto(debuglist);
  675. end;
  676. {$endif GDB}
  677. current_module^.in_implementation:=false;
  678. { deletes all symtables generated in the implementation part }
  679. while symtablestack^.symtabletype<>globalsymtable do
  680. if cs_local_browser in aktmoduleswitches then
  681. symtablestack:=symtablestack^.next
  682. else
  683. dellexlevel;
  684. { tests, if all forwards are resolved }
  685. symtablestack^.check_forwards;
  686. symtablestack^.symtabletype:=unitsymtable;
  687. {$ifdef GDB}
  688. punitsymtable(symtablestack)^.is_stab_written:=false;
  689. {$endif GDB}
  690. { insert own objectfile }
  691. insertobjectfile;
  692. {Write out the unit if the compile was succesfull.}
  693. if status.errorcount=0 then
  694. begin
  695. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  696. {$ifdef UseBrowser}
  697. if cs_local_browser in aktmoduleswitches then
  698. begin
  699. current_module^.implsymtable:=refsymtable;
  700. refsymtable^.write;
  701. refsymtable^.write_browser;
  702. end;
  703. {$endif UseBrowser}
  704. end;
  705. {$ifdef GDB}
  706. pu:=pused_unit(usedunits.first);
  707. while assigned(pu) do
  708. begin
  709. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  710. pu:=pused_unit(pu^.next);
  711. end;
  712. {$endif GDB}
  713. inc(datasize,symtablestack^.datasize);
  714. { leave when we got an error }
  715. if status.errorcount>0 then
  716. exit;
  717. { generate imports }
  718. if current_module^.uses_imports then
  719. importlib^.generatelib;
  720. { finish asmlist by adding segment starts }
  721. insertsegment;
  722. { assemble }
  723. create_objectfile;
  724. end;
  725. procedure proc_program(islibrary : boolean);
  726. var
  727. st : psymtable;
  728. names : Tstringcontainer;
  729. begin
  730. parse_only:=false;
  731. if islibrary then
  732. begin
  733. consume(_LIBRARY);
  734. stringdispose(current_module^.modulename);
  735. current_module^.modulename:=stringdup(pattern);
  736. consume(ID);
  737. consume(SEMICOLON);
  738. end
  739. else
  740. { is there an program head ? }
  741. if token=_PROGRAM then
  742. begin
  743. consume(_PROGRAM);
  744. stringdispose(current_module^.modulename);
  745. current_module^.modulename:=stringdup(pattern);
  746. consume(ID);
  747. if token=LKLAMMER then
  748. begin
  749. consume(LKLAMMER);
  750. idlist;
  751. consume(RKLAMMER);
  752. end;
  753. consume(SEMICOLON);
  754. end;
  755. { global switches are read, so further changes aren't allowed }
  756. current_module^.in_global:=false;
  757. { set implementation flag }
  758. current_module^.in_implementation:=true;
  759. { insert after the unit symbol tables the static symbol table }
  760. { of the program }
  761. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  762. current_module^.symtable:=st;
  763. { necessary for browser }
  764. loaded_units.insert(current_module);
  765. {Generate a procsym.}
  766. make_ref:=false;
  767. { this was missing !!
  768. the procdef was registered to systemunit
  769. after the defhasharray was set !!
  770. so the defhasharray became wrong !! PM }
  771. symtablestack:=st;
  772. aktprocsym:=new(Pprocsym,init('main'));
  773. aktprocsym^.definition:=new(Pprocdef,init);
  774. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  775. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  776. make_ref:=true;
  777. {The localst is a local symtable. Change it into the static
  778. symtable.}
  779. dispose(aktprocsym^.definition^.localst,done);
  780. aktprocsym^.definition^.localst:=st;
  781. refsymtable:=st;
  782. {Insert the symbols of the system unit into the stack of symbol
  783. tables.}
  784. symtablestack:=systemunit;
  785. systemunit^.next:=nil;
  786. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  787. {Load the units used by the program we compile.}
  788. if token=_USES then
  789. loadunits;
  790. {Insert the name of the main program into the symbol table.}
  791. if current_module^.modulename^<>'' then
  792. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  793. { ...is also constsymtable, this is the symtable where }
  794. { the elements of enumeration types are inserted }
  795. constsymtable:=st;
  796. { set some informations about the main program }
  797. with procinfo do
  798. begin
  799. retdef:=voiddef;
  800. _class:=nil;
  801. call_offset:=8;
  802. framepointer:=frame_pointer;
  803. flags:=0;
  804. end;
  805. procprefix:='';
  806. in_except_block:=false;
  807. codegen_newprocedure;
  808. {The program intialization needs an alias, so it can be called
  809. from the bootstrap code.}
  810. names.init;
  811. names.insert('program_init');
  812. names.insert('PASCALMAIN');
  813. names.insert(target_os.cprefix+'main');
  814. {$ifdef m68k}
  815. if target_info.target=target_PalmOS then
  816. names.insert('PilotMain');
  817. {$endif}
  818. compile_proc_body(names,true,false);
  819. names.done;
  820. { avoid self recursive destructor call !! PM }
  821. aktprocsym^.definition^.localst:=nil;
  822. codegen_doneprocedure;
  823. consume(POINT);
  824. { leave when we got an error }
  825. if status.errorcount>0 then
  826. exit;
  827. { insert heap }
  828. insertheap;
  829. { generate imports }
  830. if current_module^.uses_imports then
  831. importlib^.generatelib;
  832. inserttargetspecific;
  833. datasize:=symtablestack^.datasize;
  834. { finish asmlist by adding segment starts }
  835. insertsegment;
  836. { insert own objectfile }
  837. insertobjectfile;
  838. { assemble and link }
  839. create_objectfile;
  840. { create the executable when we are at level 1 }
  841. if (compile_level=1) then
  842. begin
  843. if (cs_link_deffile in aktglobalswitches) then
  844. deffile.writefile;
  845. if (not current_module^.is_unit) then
  846. Linker.MakeExecutable;
  847. end;
  848. end;
  849. end.
  850. {
  851. $Log$
  852. Revision 1.53 1998-09-23 12:20:50 pierre
  853. * main program tmodule had no symtable (crashed browser)
  854. * unit symbols problem fixed !!
  855. Revision 1.52 1998/09/22 17:13:49 pierre
  856. + browsing updated and developed
  857. records and objects fields are also stored
  858. Revision 1.51 1998/09/22 15:40:55 peter
  859. * some extra ifdef GDB
  860. Revision 1.50 1998/09/21 08:45:17 pierre
  861. + added vmt_offset in tobjectdef.write for fututre use
  862. (first steps to have objects without vmt if no virtual !!)
  863. + added fpu_used field for tabstractprocdef :
  864. sets this level to 2 if the functions return with value in FPU
  865. (is then set to correct value at parsing of implementation)
  866. THIS MIGHT refuse some code with FPU expression too complex
  867. that were accepted before and even in some cases
  868. that don't overflow in fact
  869. ( like if f : float; is a forward that finally in implementation
  870. only uses one fpu register !!)
  871. Nevertheless I think that it will improve security on
  872. FPU operations !!
  873. * most other changes only for UseBrowser code
  874. (added symtable references for record and objects)
  875. local switch for refs to args and local of each function
  876. (static symtable still missing)
  877. UseBrowser still not stable and probably broken by
  878. the definition hash array !!
  879. Revision 1.49 1998/09/18 08:01:36 pierre
  880. + improvement on the usebrowser part
  881. (does not work correctly for now)
  882. Revision 1.48 1998/09/09 15:33:07 peter
  883. * fixed in_global to allow directives also after interface token
  884. Revision 1.47 1998/09/09 11:50:55 pierre
  885. * forward def are not put in record or objects
  886. + added check for forwards also in record and objects
  887. * dummy parasymtable for unit initialization removed from
  888. symtable stack
  889. Revision 1.46 1998/09/03 11:24:01 peter
  890. * moved more inputfile things from tscannerfile to tinputfile
  891. * changed ifdef Sourceline to cs_asm_source
  892. Revision 1.45 1998/08/31 12:26:28 peter
  893. * m68k and palmos updates from surebugfixes
  894. Revision 1.44 1998/08/26 15:35:33 peter
  895. * fixed scannerfiles for macros
  896. + $I %<environment>%
  897. Revision 1.43 1998/08/26 10:08:47 peter
  898. * fixed problem with libprefix at the wrong place
  899. * fixed lib generation with smartlinking and no -CS used
  900. Revision 1.42 1998/08/19 18:04:54 peter
  901. * fixed current_module^.in_implementation flag
  902. Revision 1.41 1998/08/17 10:10:08 peter
  903. - removed OLDPPU
  904. Revision 1.40 1998/08/17 09:17:50 peter
  905. * static/shared linking updates
  906. Revision 1.39 1998/08/14 21:56:37 peter
  907. * setting the outputfile using -o works now to create static libs
  908. Revision 1.38 1998/08/10 14:50:13 peter
  909. + localswitches, moduleswitches, globalswitches splitting
  910. Revision 1.37 1998/08/10 10:18:31 peter
  911. + Compiler,Comphook unit which are the new interface units to the
  912. compiler
  913. Revision 1.36 1998/07/14 14:46:54 peter
  914. * released NEWINPUT
  915. Revision 1.35 1998/07/08 12:39:38 peter
  916. * heap_size for m68k
  917. Revision 1.34 1998/07/07 11:20:03 peter
  918. + NEWINPUT for a better inputfile and scanner object
  919. Revision 1.33 1998/06/25 11:15:34 pierre
  920. * ppu files where not closed in newppu !!
  921. second compilation was impossible due to too many opened files
  922. (not visible in 'make cycle' as we remove all the ppu files)
  923. Revision 1.32 1998/06/25 08:48:16 florian
  924. * first version of rtti support
  925. Revision 1.31 1998/06/24 14:48:35 peter
  926. * ifdef newppu -> ifndef oldppu
  927. Revision 1.30 1998/06/17 14:10:16 peter
  928. * small os2 fixes
  929. * fixed interdependent units with newppu (remake3 under linux works now)
  930. Revision 1.29 1998/06/16 08:56:25 peter
  931. + targetcpu
  932. * cleaner pmodules for newppu
  933. Revision 1.28 1998/06/13 00:10:10 peter
  934. * working browser and newppu
  935. * some small fixes against crashes which occured in bp7 (but not in
  936. fpc?!)
  937. Revision 1.27 1998/06/11 13:58:08 peter
  938. * small fix to let newppu compile
  939. Revision 1.26 1998/06/09 16:01:47 pierre
  940. + added procedure directive parsing for procvars
  941. (accepted are popstack cdecl and pascal)
  942. + added C vars with the following syntax
  943. var C calias 'true_c_name';(can be followed by external)
  944. reason is that you must add the Cprefix
  945. which is target dependent
  946. Revision 1.25 1998/06/08 22:59:49 peter
  947. * smartlinking works for win32
  948. * some defines to exclude some compiler parts
  949. Revision 1.24 1998/06/08 13:13:44 pierre
  950. + temporary variables now in temp_gen.pas unit
  951. because it is processor independent
  952. * mppc68k.bat modified to undefine i386 and support_mmx
  953. (which are defaults for i386)
  954. Revision 1.23 1998/06/05 17:47:29 peter
  955. * some better uses clauses
  956. Revision 1.22 1998/06/05 14:37:34 pierre
  957. * fixes for inline for operators
  958. * inline procedure more correctly restricted
  959. Revision 1.21 1998/06/04 23:51:53 peter
  960. * m68k compiles
  961. + .def file creation moved to gendef.pas so it could also be used
  962. for win32
  963. Revision 1.20 1998/06/04 09:55:42 pierre
  964. * demangled name of procsym reworked to become independant of the mangling scheme
  965. Revision 1.19 1998/06/03 23:40:38 peter
  966. + unlimited file support, release tempclose
  967. Revision 1.18 1998/06/03 22:49:00 peter
  968. + wordbool,longbool
  969. * rename bis,von -> high,low
  970. * moved some systemunit loading/creating to psystem.pas
  971. Revision 1.17 1998/05/28 14:40:25 peter
  972. * fixes for newppu, remake3 works now with it
  973. Revision 1.16 1998/05/27 19:45:06 peter
  974. * symtable.pas splitted into includefiles
  975. * symtable adapted for $ifdef NEWPPU
  976. Revision 1.15 1998/05/23 01:21:22 peter
  977. + aktasmmode, aktoptprocessor, aktoutputformat
  978. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  979. + $LIBNAME to set the library name where the unit will be put in
  980. * splitted cgi386 a bit (codeseg to large for bp7)
  981. * nasm, tasm works again. nasm moved to ag386nsm.pas
  982. Revision 1.14 1998/05/20 09:42:35 pierre
  983. + UseTokenInfo now default
  984. * unit in interface uses and implementation uses gives error now
  985. * only one error for unknown symbol (uses lastsymknown boolean)
  986. the problem came from the label code !
  987. + first inlined procedures and function work
  988. (warning there might be allowed cases were the result is still wrong !!)
  989. * UseBrower updated gives a global list of all position of all used symbols
  990. with switch -gb
  991. Revision 1.13 1998/05/12 10:47:00 peter
  992. * moved printstatus to verb_def
  993. + V_Normal which is between V_Error and V_Warning and doesn't have a
  994. prefix like error: warning: and is included in V_Default
  995. * fixed some messages
  996. * first time parameter scan is only for -v and -T
  997. - removed old style messages
  998. Revision 1.12 1998/05/11 13:07:56 peter
  999. + $ifdef NEWPPU for the new ppuformat
  1000. + $define GDB not longer required
  1001. * removed all warnings and stripped some log comments
  1002. * no findfirst/findnext anymore to remove smartlink *.o files
  1003. Revision 1.11 1998/05/06 18:36:53 peter
  1004. * tai_section extended with code,data,bss sections and enumerated type
  1005. * ident 'compiled by FPC' moved to pmodules
  1006. * small fix for smartlink
  1007. Revision 1.10 1998/05/04 17:54:28 peter
  1008. + smartlinking works (only case jumptable left todo)
  1009. * redesign of systems.pas to support assemblers and linkers
  1010. + Unitname is now also in the PPU-file, increased version to 14
  1011. Revision 1.9 1998/05/01 16:38:45 florian
  1012. * handling of private and protected fixed
  1013. + change_keywords_to_tp implemented to remove
  1014. keywords which aren't supported by tp
  1015. * break and continue are now symbols of the system unit
  1016. + widestring, longstring and ansistring type released
  1017. Revision 1.8 1998/04/30 15:59:41 pierre
  1018. * GDB works again better :
  1019. correct type info in one pass
  1020. + UseTokenInfo for better source position
  1021. * fixed one remaining bug in scanner for line counts
  1022. * several little fixes
  1023. Revision 1.7 1998/04/29 10:33:59 pierre
  1024. + added some code for ansistring (not complete nor working yet)
  1025. * corrected operator overloading
  1026. * corrected nasm output
  1027. + started inline procedures
  1028. + added starstarn : use ** for exponentiation (^ gave problems)
  1029. + started UseTokenInfo cond to get accurate positions
  1030. Revision 1.6 1998/04/27 23:10:28 peter
  1031. + new scanner
  1032. * $makelib -> if smartlink
  1033. * small filename fixes pmodule.setfilename
  1034. * moved import from files.pas -> import.pas
  1035. Revision 1.5 1998/04/14 23:27:03 florian
  1036. + exclude/include with constant second parameter added
  1037. Revision 1.4 1998/04/10 14:41:43 peter
  1038. * removed some Hints
  1039. * small speed optimization for AsmLn
  1040. Revision 1.3 1998/04/03 09:51:00 daniel
  1041. * Fixed heap allocation for OS/2.
  1042. }