pmodules.pas 43 KB

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