pmodules.pas 41 KB

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