pmodules.pas 38 KB

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