pmodules.pas 36 KB

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