pmodules.pas 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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. punitsymtable(symtablestack)^.is_stab_written:=false;
  669. { insert own objectfile }
  670. insertobjectfile;
  671. {Write out the unit if the compile was succesfull.}
  672. if status.errorcount=0 then
  673. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack));
  674. pu:=pused_unit(usedunits.first);
  675. while assigned(pu) do
  676. begin
  677. punitsymtable(pu^.u^.symtable)^.is_stab_written:=false;
  678. pu:=pused_unit(pu^.next);
  679. end;
  680. inc(datasize,symtablestack^.datasize);
  681. { leave when we got an error }
  682. if status.errorcount>0 then
  683. exit;
  684. { generate imports }
  685. if current_module^.uses_imports then
  686. importlib^.generatelib;
  687. { finish asmlist by adding segment starts }
  688. insertsegment;
  689. { assemble }
  690. create_objectfile;
  691. end;
  692. procedure proc_program(islibrary : boolean);
  693. var
  694. st : psymtable;
  695. names : Tstringcontainer;
  696. begin
  697. parse_only:=false;
  698. if islibrary then
  699. begin
  700. consume(_LIBRARY);
  701. stringdispose(current_module^.modulename);
  702. current_module^.modulename:=stringdup(pattern);
  703. consume(ID);
  704. consume(SEMICOLON);
  705. end
  706. else
  707. { is there an program head ? }
  708. if token=_PROGRAM then
  709. begin
  710. consume(_PROGRAM);
  711. stringdispose(current_module^.modulename);
  712. current_module^.modulename:=stringdup(pattern);
  713. consume(ID);
  714. if token=LKLAMMER then
  715. begin
  716. consume(LKLAMMER);
  717. idlist;
  718. consume(RKLAMMER);
  719. end;
  720. consume(SEMICOLON);
  721. end;
  722. { global switches are read, so further changes aren't allowed }
  723. current_module^.in_global:=false;
  724. { set implementation flag }
  725. current_module^.in_implementation:=true;
  726. { insert after the unit symbol tables the static symbol table }
  727. { of the program }
  728. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  729. {Generate a procsym.}
  730. make_ref:=false;
  731. aktprocsym:=new(Pprocsym,init('main'));
  732. aktprocsym^.definition:=new(Pprocdef,init);
  733. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or poproginit;
  734. aktprocsym^.definition^.setmangledname(target_os.Cprefix+'main');
  735. make_ref:=true;
  736. {The localst is a local symtable. Change it into the static
  737. symtable.}
  738. dispose(aktprocsym^.definition^.localst,done);
  739. aktprocsym^.definition^.localst:=st;
  740. refsymtable:=st;
  741. { necessary for browser }
  742. loaded_units.insert(current_module);
  743. {Insert the symbols of the system unit into the stack of symbol
  744. tables.}
  745. symtablestack:=systemunit;
  746. systemunit^.next:=nil;
  747. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  748. {Load the units used by the program we compile.}
  749. if token=_USES then
  750. loadunits;
  751. {Insert the name of the main program into the symbol table.}
  752. if current_module^.modulename^<>'' then
  753. st^.insert(new(pprogramsym,init(current_module^.modulename^)));
  754. { ...is also constsymtable, this is the symtable where }
  755. { the elements of enumeration types are inserted }
  756. constsymtable:=st;
  757. { set some informations about the main program }
  758. with procinfo do
  759. begin
  760. retdef:=voiddef;
  761. _class:=nil;
  762. call_offset:=8;
  763. framepointer:=frame_pointer;
  764. flags:=0;
  765. end;
  766. procprefix:='';
  767. in_except_block:=false;
  768. codegen_newprocedure;
  769. {The program intialization needs an alias, so it can be called
  770. from the bootstrap code.}
  771. names.init;
  772. names.insert('program_init');
  773. names.insert('PASCALMAIN');
  774. names.insert(target_os.cprefix+'main');
  775. {$ifdef m68k}
  776. if target_info.target=target_PalmOS then
  777. names.insert('PilotMain');
  778. {$endif}
  779. compile_proc_body(names,true,false);
  780. names.done;
  781. codegen_doneprocedure;
  782. consume(POINT);
  783. { leave when we got an error }
  784. if status.errorcount>0 then
  785. exit;
  786. { insert heap }
  787. insertheap;
  788. { generate imports }
  789. if current_module^.uses_imports then
  790. importlib^.generatelib;
  791. inserttargetspecific;
  792. datasize:=symtablestack^.datasize;
  793. { finish asmlist by adding segment starts }
  794. insertsegment;
  795. { insert own objectfile }
  796. insertobjectfile;
  797. { assemble and link }
  798. create_objectfile;
  799. { create the executable when we are at level 1 }
  800. if (compile_level=1) then
  801. begin
  802. if (cs_link_deffile in aktglobalswitches) then
  803. deffile.writefile;
  804. if (not current_module^.is_unit) then
  805. Linker.MakeExecutable;
  806. end;
  807. end;
  808. end.
  809. {
  810. $Log$
  811. Revision 1.50 1998-09-21 08:45:17 pierre
  812. + added vmt_offset in tobjectdef.write for fututre use
  813. (first steps to have objects without vmt if no virtual !!)
  814. + added fpu_used field for tabstractprocdef :
  815. sets this level to 2 if the functions return with value in FPU
  816. (is then set to correct value at parsing of implementation)
  817. THIS MIGHT refuse some code with FPU expression too complex
  818. that were accepted before and even in some cases
  819. that don't overflow in fact
  820. ( like if f : float; is a forward that finally in implementation
  821. only uses one fpu register !!)
  822. Nevertheless I think that it will improve security on
  823. FPU operations !!
  824. * most other changes only for UseBrowser code
  825. (added symtable references for record and objects)
  826. local switch for refs to args and local of each function
  827. (static symtable still missing)
  828. UseBrowser still not stable and probably broken by
  829. the definition hash array !!
  830. Revision 1.49 1998/09/18 08:01:36 pierre
  831. + improvement on the usebrowser part
  832. (does not work correctly for now)
  833. Revision 1.48 1998/09/09 15:33:07 peter
  834. * fixed in_global to allow directives also after interface token
  835. Revision 1.47 1998/09/09 11:50:55 pierre
  836. * forward def are not put in record or objects
  837. + added check for forwards also in record and objects
  838. * dummy parasymtable for unit initialization removed from
  839. symtable stack
  840. Revision 1.46 1998/09/03 11:24:01 peter
  841. * moved more inputfile things from tscannerfile to tinputfile
  842. * changed ifdef Sourceline to cs_asm_source
  843. Revision 1.45 1998/08/31 12:26:28 peter
  844. * m68k and palmos updates from surebugfixes
  845. Revision 1.44 1998/08/26 15:35:33 peter
  846. * fixed scannerfiles for macros
  847. + $I %<environment>%
  848. Revision 1.43 1998/08/26 10:08:47 peter
  849. * fixed problem with libprefix at the wrong place
  850. * fixed lib generation with smartlinking and no -CS used
  851. Revision 1.42 1998/08/19 18:04:54 peter
  852. * fixed current_module^.in_implementation flag
  853. Revision 1.41 1998/08/17 10:10:08 peter
  854. - removed OLDPPU
  855. Revision 1.40 1998/08/17 09:17:50 peter
  856. * static/shared linking updates
  857. Revision 1.39 1998/08/14 21:56:37 peter
  858. * setting the outputfile using -o works now to create static libs
  859. Revision 1.38 1998/08/10 14:50:13 peter
  860. + localswitches, moduleswitches, globalswitches splitting
  861. Revision 1.37 1998/08/10 10:18:31 peter
  862. + Compiler,Comphook unit which are the new interface units to the
  863. compiler
  864. Revision 1.36 1998/07/14 14:46:54 peter
  865. * released NEWINPUT
  866. Revision 1.35 1998/07/08 12:39:38 peter
  867. * heap_size for m68k
  868. Revision 1.34 1998/07/07 11:20:03 peter
  869. + NEWINPUT for a better inputfile and scanner object
  870. Revision 1.33 1998/06/25 11:15:34 pierre
  871. * ppu files where not closed in newppu !!
  872. second compilation was impossible due to too many opened files
  873. (not visible in 'make cycle' as we remove all the ppu files)
  874. Revision 1.32 1998/06/25 08:48:16 florian
  875. * first version of rtti support
  876. Revision 1.31 1998/06/24 14:48:35 peter
  877. * ifdef newppu -> ifndef oldppu
  878. Revision 1.30 1998/06/17 14:10:16 peter
  879. * small os2 fixes
  880. * fixed interdependent units with newppu (remake3 under linux works now)
  881. Revision 1.29 1998/06/16 08:56:25 peter
  882. + targetcpu
  883. * cleaner pmodules for newppu
  884. Revision 1.28 1998/06/13 00:10:10 peter
  885. * working browser and newppu
  886. * some small fixes against crashes which occured in bp7 (but not in
  887. fpc?!)
  888. Revision 1.27 1998/06/11 13:58:08 peter
  889. * small fix to let newppu compile
  890. Revision 1.26 1998/06/09 16:01:47 pierre
  891. + added procedure directive parsing for procvars
  892. (accepted are popstack cdecl and pascal)
  893. + added C vars with the following syntax
  894. var C calias 'true_c_name';(can be followed by external)
  895. reason is that you must add the Cprefix
  896. which is target dependent
  897. Revision 1.25 1998/06/08 22:59:49 peter
  898. * smartlinking works for win32
  899. * some defines to exclude some compiler parts
  900. Revision 1.24 1998/06/08 13:13:44 pierre
  901. + temporary variables now in temp_gen.pas unit
  902. because it is processor independent
  903. * mppc68k.bat modified to undefine i386 and support_mmx
  904. (which are defaults for i386)
  905. Revision 1.23 1998/06/05 17:47:29 peter
  906. * some better uses clauses
  907. Revision 1.22 1998/06/05 14:37:34 pierre
  908. * fixes for inline for operators
  909. * inline procedure more correctly restricted
  910. Revision 1.21 1998/06/04 23:51:53 peter
  911. * m68k compiles
  912. + .def file creation moved to gendef.pas so it could also be used
  913. for win32
  914. Revision 1.20 1998/06/04 09:55:42 pierre
  915. * demangled name of procsym reworked to become independant of the mangling scheme
  916. Revision 1.19 1998/06/03 23:40:38 peter
  917. + unlimited file support, release tempclose
  918. Revision 1.18 1998/06/03 22:49:00 peter
  919. + wordbool,longbool
  920. * rename bis,von -> high,low
  921. * moved some systemunit loading/creating to psystem.pas
  922. Revision 1.17 1998/05/28 14:40:25 peter
  923. * fixes for newppu, remake3 works now with it
  924. Revision 1.16 1998/05/27 19:45:06 peter
  925. * symtable.pas splitted into includefiles
  926. * symtable adapted for $ifdef NEWPPU
  927. Revision 1.15 1998/05/23 01:21:22 peter
  928. + aktasmmode, aktoptprocessor, aktoutputformat
  929. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  930. + $LIBNAME to set the library name where the unit will be put in
  931. * splitted cgi386 a bit (codeseg to large for bp7)
  932. * nasm, tasm works again. nasm moved to ag386nsm.pas
  933. Revision 1.14 1998/05/20 09:42:35 pierre
  934. + UseTokenInfo now default
  935. * unit in interface uses and implementation uses gives error now
  936. * only one error for unknown symbol (uses lastsymknown boolean)
  937. the problem came from the label code !
  938. + first inlined procedures and function work
  939. (warning there might be allowed cases were the result is still wrong !!)
  940. * UseBrower updated gives a global list of all position of all used symbols
  941. with switch -gb
  942. Revision 1.13 1998/05/12 10:47:00 peter
  943. * moved printstatus to verb_def
  944. + V_Normal which is between V_Error and V_Warning and doesn't have a
  945. prefix like error: warning: and is included in V_Default
  946. * fixed some messages
  947. * first time parameter scan is only for -v and -T
  948. - removed old style messages
  949. Revision 1.12 1998/05/11 13:07:56 peter
  950. + $ifdef NEWPPU for the new ppuformat
  951. + $define GDB not longer required
  952. * removed all warnings and stripped some log comments
  953. * no findfirst/findnext anymore to remove smartlink *.o files
  954. Revision 1.11 1998/05/06 18:36:53 peter
  955. * tai_section extended with code,data,bss sections and enumerated type
  956. * ident 'compiled by FPC' moved to pmodules
  957. * small fix for smartlink
  958. Revision 1.10 1998/05/04 17:54:28 peter
  959. + smartlinking works (only case jumptable left todo)
  960. * redesign of systems.pas to support assemblers and linkers
  961. + Unitname is now also in the PPU-file, increased version to 14
  962. Revision 1.9 1998/05/01 16:38:45 florian
  963. * handling of private and protected fixed
  964. + change_keywords_to_tp implemented to remove
  965. keywords which aren't supported by tp
  966. * break and continue are now symbols of the system unit
  967. + widestring, longstring and ansistring type released
  968. Revision 1.8 1998/04/30 15:59:41 pierre
  969. * GDB works again better :
  970. correct type info in one pass
  971. + UseTokenInfo for better source position
  972. * fixed one remaining bug in scanner for line counts
  973. * several little fixes
  974. Revision 1.7 1998/04/29 10:33:59 pierre
  975. + added some code for ansistring (not complete nor working yet)
  976. * corrected operator overloading
  977. * corrected nasm output
  978. + started inline procedures
  979. + added starstarn : use ** for exponentiation (^ gave problems)
  980. + started UseTokenInfo cond to get accurate positions
  981. Revision 1.6 1998/04/27 23:10:28 peter
  982. + new scanner
  983. * $makelib -> if smartlink
  984. * small filename fixes pmodule.setfilename
  985. * moved import from files.pas -> import.pas
  986. Revision 1.5 1998/04/14 23:27:03 florian
  987. + exclude/include with constant second parameter added
  988. Revision 1.4 1998/04/10 14:41:43 peter
  989. * removed some Hints
  990. * small speed optimization for AsmLn
  991. Revision 1.3 1998/04/03 09:51:00 daniel
  992. * Fixed heap allocation for OS/2.
  993. }