pmodules.pas 39 KB

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