pmodules.pas 37 KB

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