pmodules.pas 37 KB

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