pmodules.pas 42 KB

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