pmodules.pas 37 KB

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