pmodules.pas 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  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. { necessary for browser }
  763. loaded_units.insert(current_module);
  764. {Generate a procsym.}
  765. make_ref:=false;
  766. { this was missing !!
  767. the procdef was registered to systemunit
  768. after the defhasharray was set !!
  769. so the defhasharray became wrong !! PM }
  770. symtablestack:=st;
  771. aktprocsym:=new(Pprocsym,init('main'));
  772. aktprocsym^.definition:=new(Pprocdef,init);
  773. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  774. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  775. make_ref:=true;
  776. {The localst is a local symtable. Change it into the static
  777. symtable.}
  778. dispose(aktprocsym^.definition^.localst,done);
  779. aktprocsym^.definition^.localst:=st;
  780. refsymtable:=st;
  781. {Insert the symbols of the system unit into the stack of symbol
  782. tables.}
  783. symtablestack:=systemunit;
  784. systemunit^.next:=nil;
  785. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  786. {Load the units used by the program we compile.}
  787. if token=_USES then
  788. loadunits;
  789. {Insert the name of the main program into the symbol table.}
  790. if current_module^.modulename^<>'' then
  791. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  792. { ...is also constsymtable, this is the symtable where }
  793. { the elements of enumeration types are inserted }
  794. constsymtable:=st;
  795. { set some informations about the main program }
  796. with procinfo do
  797. begin
  798. retdef:=voiddef;
  799. _class:=nil;
  800. call_offset:=8;
  801. framepointer:=frame_pointer;
  802. flags:=0;
  803. end;
  804. procprefix:='';
  805. in_except_block:=false;
  806. codegen_newprocedure;
  807. {The program intialization needs an alias, so it can be called
  808. from the bootstrap code.}
  809. names.init;
  810. names.insert('program_init');
  811. names.insert('PASCALMAIN');
  812. names.insert(target_os.cprefix+'main');
  813. {$ifdef m68k}
  814. if target_info.target=target_PalmOS then
  815. names.insert('PilotMain');
  816. {$endif}
  817. compile_proc_body(names,true,false);
  818. names.done;
  819. { avoid self recursive destructor call !! PM }
  820. aktprocsym^.definition^.localst:=nil;
  821. codegen_doneprocedure;
  822. consume(POINT);
  823. { leave when we got an error }
  824. if status.errorcount>0 then
  825. exit;
  826. { insert heap }
  827. insertheap;
  828. { generate imports }
  829. if current_module^.uses_imports then
  830. importlib^.generatelib;
  831. inserttargetspecific;
  832. datasize:=symtablestack^.datasize;
  833. { finish asmlist by adding segment starts }
  834. insertsegment;
  835. { insert own objectfile }
  836. insertobjectfile;
  837. { assemble and link }
  838. create_objectfile;
  839. { create the executable when we are at level 1 }
  840. if (compile_level=1) then
  841. begin
  842. if (cs_link_deffile in aktglobalswitches) then
  843. deffile.writefile;
  844. if (not current_module^.is_unit) then
  845. Linker.MakeExecutable;
  846. end;
  847. end;
  848. end.
  849. {
  850. $Log$
  851. Revision 1.52 1998-09-22 17:13:49 pierre
  852. + browsing updated and developed
  853. records and objects fields are also stored
  854. Revision 1.51 1998/09/22 15:40:55 peter
  855. * some extra ifdef GDB
  856. Revision 1.50 1998/09/21 08:45:17 pierre
  857. + added vmt_offset in tobjectdef.write for fututre use
  858. (first steps to have objects without vmt if no virtual !!)
  859. + added fpu_used field for tabstractprocdef :
  860. sets this level to 2 if the functions return with value in FPU
  861. (is then set to correct value at parsing of implementation)
  862. THIS MIGHT refuse some code with FPU expression too complex
  863. that were accepted before and even in some cases
  864. that don't overflow in fact
  865. ( like if f : float; is a forward that finally in implementation
  866. only uses one fpu register !!)
  867. Nevertheless I think that it will improve security on
  868. FPU operations !!
  869. * most other changes only for UseBrowser code
  870. (added symtable references for record and objects)
  871. local switch for refs to args and local of each function
  872. (static symtable still missing)
  873. UseBrowser still not stable and probably broken by
  874. the definition hash array !!
  875. Revision 1.49 1998/09/18 08:01:36 pierre
  876. + improvement on the usebrowser part
  877. (does not work correctly for now)
  878. Revision 1.48 1998/09/09 15:33:07 peter
  879. * fixed in_global to allow directives also after interface token
  880. Revision 1.47 1998/09/09 11:50:55 pierre
  881. * forward def are not put in record or objects
  882. + added check for forwards also in record and objects
  883. * dummy parasymtable for unit initialization removed from
  884. symtable stack
  885. Revision 1.46 1998/09/03 11:24:01 peter
  886. * moved more inputfile things from tscannerfile to tinputfile
  887. * changed ifdef Sourceline to cs_asm_source
  888. Revision 1.45 1998/08/31 12:26:28 peter
  889. * m68k and palmos updates from surebugfixes
  890. Revision 1.44 1998/08/26 15:35:33 peter
  891. * fixed scannerfiles for macros
  892. + $I %<environment>%
  893. Revision 1.43 1998/08/26 10:08:47 peter
  894. * fixed problem with libprefix at the wrong place
  895. * fixed lib generation with smartlinking and no -CS used
  896. Revision 1.42 1998/08/19 18:04:54 peter
  897. * fixed current_module^.in_implementation flag
  898. Revision 1.41 1998/08/17 10:10:08 peter
  899. - removed OLDPPU
  900. Revision 1.40 1998/08/17 09:17:50 peter
  901. * static/shared linking updates
  902. Revision 1.39 1998/08/14 21:56:37 peter
  903. * setting the outputfile using -o works now to create static libs
  904. Revision 1.38 1998/08/10 14:50:13 peter
  905. + localswitches, moduleswitches, globalswitches splitting
  906. Revision 1.37 1998/08/10 10:18:31 peter
  907. + Compiler,Comphook unit which are the new interface units to the
  908. compiler
  909. Revision 1.36 1998/07/14 14:46:54 peter
  910. * released NEWINPUT
  911. Revision 1.35 1998/07/08 12:39:38 peter
  912. * heap_size for m68k
  913. Revision 1.34 1998/07/07 11:20:03 peter
  914. + NEWINPUT for a better inputfile and scanner object
  915. Revision 1.33 1998/06/25 11:15:34 pierre
  916. * ppu files where not closed in newppu !!
  917. second compilation was impossible due to too many opened files
  918. (not visible in 'make cycle' as we remove all the ppu files)
  919. Revision 1.32 1998/06/25 08:48:16 florian
  920. * first version of rtti support
  921. Revision 1.31 1998/06/24 14:48:35 peter
  922. * ifdef newppu -> ifndef oldppu
  923. Revision 1.30 1998/06/17 14:10:16 peter
  924. * small os2 fixes
  925. * fixed interdependent units with newppu (remake3 under linux works now)
  926. Revision 1.29 1998/06/16 08:56:25 peter
  927. + targetcpu
  928. * cleaner pmodules for newppu
  929. Revision 1.28 1998/06/13 00:10:10 peter
  930. * working browser and newppu
  931. * some small fixes against crashes which occured in bp7 (but not in
  932. fpc?!)
  933. Revision 1.27 1998/06/11 13:58:08 peter
  934. * small fix to let newppu compile
  935. Revision 1.26 1998/06/09 16:01:47 pierre
  936. + added procedure directive parsing for procvars
  937. (accepted are popstack cdecl and pascal)
  938. + added C vars with the following syntax
  939. var C calias 'true_c_name';(can be followed by external)
  940. reason is that you must add the Cprefix
  941. which is target dependent
  942. Revision 1.25 1998/06/08 22:59:49 peter
  943. * smartlinking works for win32
  944. * some defines to exclude some compiler parts
  945. Revision 1.24 1998/06/08 13:13:44 pierre
  946. + temporary variables now in temp_gen.pas unit
  947. because it is processor independent
  948. * mppc68k.bat modified to undefine i386 and support_mmx
  949. (which are defaults for i386)
  950. Revision 1.23 1998/06/05 17:47:29 peter
  951. * some better uses clauses
  952. Revision 1.22 1998/06/05 14:37:34 pierre
  953. * fixes for inline for operators
  954. * inline procedure more correctly restricted
  955. Revision 1.21 1998/06/04 23:51:53 peter
  956. * m68k compiles
  957. + .def file creation moved to gendef.pas so it could also be used
  958. for win32
  959. Revision 1.20 1998/06/04 09:55:42 pierre
  960. * demangled name of procsym reworked to become independant of the mangling scheme
  961. Revision 1.19 1998/06/03 23:40:38 peter
  962. + unlimited file support, release tempclose
  963. Revision 1.18 1998/06/03 22:49:00 peter
  964. + wordbool,longbool
  965. * rename bis,von -> high,low
  966. * moved some systemunit loading/creating to psystem.pas
  967. Revision 1.17 1998/05/28 14:40:25 peter
  968. * fixes for newppu, remake3 works now with it
  969. Revision 1.16 1998/05/27 19:45:06 peter
  970. * symtable.pas splitted into includefiles
  971. * symtable adapted for $ifdef NEWPPU
  972. Revision 1.15 1998/05/23 01:21:22 peter
  973. + aktasmmode, aktoptprocessor, aktoutputformat
  974. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  975. + $LIBNAME to set the library name where the unit will be put in
  976. * splitted cgi386 a bit (codeseg to large for bp7)
  977. * nasm, tasm works again. nasm moved to ag386nsm.pas
  978. Revision 1.14 1998/05/20 09:42:35 pierre
  979. + UseTokenInfo now default
  980. * unit in interface uses and implementation uses gives error now
  981. * only one error for unknown symbol (uses lastsymknown boolean)
  982. the problem came from the label code !
  983. + first inlined procedures and function work
  984. (warning there might be allowed cases were the result is still wrong !!)
  985. * UseBrower updated gives a global list of all position of all used symbols
  986. with switch -gb
  987. Revision 1.13 1998/05/12 10:47:00 peter
  988. * moved printstatus to verb_def
  989. + V_Normal which is between V_Error and V_Warning and doesn't have a
  990. prefix like error: warning: and is included in V_Default
  991. * fixed some messages
  992. * first time parameter scan is only for -v and -T
  993. - removed old style messages
  994. Revision 1.12 1998/05/11 13:07:56 peter
  995. + $ifdef NEWPPU for the new ppuformat
  996. + $define GDB not longer required
  997. * removed all warnings and stripped some log comments
  998. * no findfirst/findnext anymore to remove smartlink *.o files
  999. Revision 1.11 1998/05/06 18:36:53 peter
  1000. * tai_section extended with code,data,bss sections and enumerated type
  1001. * ident 'compiled by FPC' moved to pmodules
  1002. * small fix for smartlink
  1003. Revision 1.10 1998/05/04 17:54:28 peter
  1004. + smartlinking works (only case jumptable left todo)
  1005. * redesign of systems.pas to support assemblers and linkers
  1006. + Unitname is now also in the PPU-file, increased version to 14
  1007. Revision 1.9 1998/05/01 16:38:45 florian
  1008. * handling of private and protected fixed
  1009. + change_keywords_to_tp implemented to remove
  1010. keywords which aren't supported by tp
  1011. * break and continue are now symbols of the system unit
  1012. + widestring, longstring and ansistring type released
  1013. Revision 1.8 1998/04/30 15:59:41 pierre
  1014. * GDB works again better :
  1015. correct type info in one pass
  1016. + UseTokenInfo for better source position
  1017. * fixed one remaining bug in scanner for line counts
  1018. * several little fixes
  1019. Revision 1.7 1998/04/29 10:33:59 pierre
  1020. + added some code for ansistring (not complete nor working yet)
  1021. * corrected operator overloading
  1022. * corrected nasm output
  1023. + started inline procedures
  1024. + added starstarn : use ** for exponentiation (^ gave problems)
  1025. + started UseTokenInfo cond to get accurate positions
  1026. Revision 1.6 1998/04/27 23:10:28 peter
  1027. + new scanner
  1028. * $makelib -> if smartlink
  1029. * small filename fixes pmodule.setfilename
  1030. * moved import from files.pas -> import.pas
  1031. Revision 1.5 1998/04/14 23:27:03 florian
  1032. + exclude/include with constant second parameter added
  1033. Revision 1.4 1998/04/10 14:41:43 peter
  1034. * removed some Hints
  1035. * small speed optimization for AsmLn
  1036. Revision 1.3 1998/04/03 09:51:00 daniel
  1037. * Fixed heap allocation for OS/2.
  1038. }