pmodules.pas 44 KB

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