symsym.pas 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. Implementation for the symbols types of the symtable
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symsym;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. { target }
  24. globtype,globals,widestr,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. { ppu }
  28. ppu,finput,
  29. cclasses,symnot,
  30. { aasm }
  31. aasmbase,
  32. cpuinfo,cpubase,cgbase,cgutils,parabase
  33. ;
  34. type
  35. { this class is the base for all symbol objects }
  36. tstoredsym = class(tsym)
  37. public
  38. constructor create(st:tsymtyp;const n : string);
  39. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  40. destructor destroy;override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  42. end;
  43. tlabelsym = class(tstoredsym)
  44. used,
  45. defined,
  46. nonlocal : boolean;
  47. { points to the matching node, only valid resultdef pass is run and
  48. the goto<->label relation in the node tree is created, should
  49. be a tnode }
  50. code : pointer;
  51. { when the label is defined in an asm block, this points to the
  52. generated asmlabel }
  53. asmblocklabel : tasmlabel;
  54. constructor create(const n : string);
  55. constructor ppuload(ppufile:tcompilerppufile);
  56. procedure ppuwrite(ppufile:tcompilerppufile);override;
  57. function mangledname:string;override;
  58. end;
  59. tunitsym = class(Tstoredsym)
  60. module : tobject; { tmodule }
  61. constructor create(const n : string;amodule : tobject);
  62. constructor ppuload(ppufile:tcompilerppufile);
  63. destructor destroy;override;
  64. procedure ppuwrite(ppufile:tcompilerppufile);override;
  65. end;
  66. terrorsym = class(Tsym)
  67. constructor create;
  68. end;
  69. { tprocsym }
  70. tprocsym = class(tstoredsym)
  71. protected
  72. FProcdefList : TFPObjectList;
  73. FProcdefDerefList : TFPList;
  74. public
  75. constructor create(const n : string);
  76. constructor ppuload(ppufile:tcompilerppufile);
  77. destructor destroy;override;
  78. { writes all declarations except the specified one }
  79. procedure write_parameter_lists(skipdef:tprocdef);
  80. { tests, if all procedures definitions are defined and not }
  81. { only forward }
  82. procedure check_forward;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. procedure buildderef;override;
  85. procedure deref;override;
  86. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  87. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  88. function find_procdef_byoptions(ops:tprocoptions): Tprocdef;
  89. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  90. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  91. function find_procdef_enumerator_operator(typedef:tdef;var besteq:tequaltype):Tprocdef;
  92. property ProcdefList:TFPObjectList read FProcdefList;
  93. end;
  94. ttypesym = class(Tstoredsym)
  95. typedef : tdef;
  96. typedefderef : tderef;
  97. constructor create(const n : string;def:tdef);
  98. constructor ppuload(ppufile:tcompilerppufile);
  99. procedure ppuwrite(ppufile:tcompilerppufile);override;
  100. procedure buildderef;override;
  101. procedure deref;override;
  102. end;
  103. tabstractvarsym = class(tstoredsym)
  104. varoptions : tvaroptions;
  105. notifications : Tlinkedlist;
  106. varspez : tvarspez; { sets the type of access }
  107. varregable : tvarregable;
  108. varstate : tvarstate;
  109. { Has the address of this variable potentially escaped the }
  110. { block in which is was declared? }
  111. { could also be part of tabstractnormalvarsym, but there's }
  112. { one byte left here till the next 4 byte alignment }
  113. addr_taken : boolean;
  114. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  115. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  116. destructor destroy;override;
  117. procedure ppuwrite(ppufile:tcompilerppufile);override;
  118. procedure buildderef;override;
  119. procedure deref;override;
  120. function getsize : aint;
  121. function getpackedbitsize : longint;
  122. function is_regvar(refpara: boolean):boolean;
  123. procedure trigger_notifications(what:Tnotification_flag);
  124. function register_notification(flags:Tnotification_flags;
  125. callback:Tnotification_callback):cardinal;
  126. procedure unregister_notification(id:cardinal);
  127. private
  128. _vardef : tdef;
  129. vardefderef : tderef;
  130. procedure setvardef(def:tdef);
  131. public
  132. property vardef: tdef read _vardef write setvardef;
  133. end;
  134. tfieldvarsym = class(tabstractvarsym)
  135. fieldoffset : aint; { offset in record/object }
  136. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  137. constructor ppuload(ppufile:tcompilerppufile);
  138. procedure ppuwrite(ppufile:tcompilerppufile);override;
  139. function mangledname:string;override;
  140. end;
  141. tabstractnormalvarsym = class(tabstractvarsym)
  142. defaultconstsym : tsym;
  143. defaultconstsymderef : tderef;
  144. localloc : TLocation; { register/reference for local var }
  145. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  146. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  147. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  148. procedure ppuwrite(ppufile:tcompilerppufile);override;
  149. procedure buildderef;override;
  150. procedure deref;override;
  151. end;
  152. tlocalvarsym = class(tabstractnormalvarsym)
  153. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  154. constructor ppuload(ppufile:tcompilerppufile);
  155. procedure ppuwrite(ppufile:tcompilerppufile);override;
  156. end;
  157. tparavarsym = class(tabstractnormalvarsym)
  158. paraloc : array[tcallercallee] of TCGPara;
  159. paranr : word; { position of this parameter }
  160. {$ifdef EXTDEBUG}
  161. eqval : tequaltype;
  162. {$endif EXTDEBUG}
  163. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  164. constructor ppuload(ppufile:tcompilerppufile);
  165. destructor destroy;override;
  166. procedure ppuwrite(ppufile:tcompilerppufile);override;
  167. end;
  168. tstaticvarsym = class(tabstractnormalvarsym)
  169. private
  170. _mangledname : pshortstring;
  171. public
  172. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  173. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  174. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  175. constructor ppuload(ppufile:tcompilerppufile);
  176. destructor destroy;override;
  177. procedure ppuwrite(ppufile:tcompilerppufile);override;
  178. function mangledname:string;override;
  179. procedure set_mangledname(const s:string);
  180. end;
  181. tabsolutevarsym = class(tabstractvarsym)
  182. public
  183. abstyp : absolutetyp;
  184. {$ifdef i386}
  185. absseg : boolean;
  186. {$endif i386}
  187. asmname : pshortstring;
  188. addroffset : aword;
  189. ref : tpropaccesslist;
  190. constructor create(const n : string;def:tdef);
  191. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  192. destructor destroy;override;
  193. constructor ppuload(ppufile:tcompilerppufile);
  194. procedure buildderef;override;
  195. procedure deref;override;
  196. function mangledname : string;override;
  197. procedure ppuwrite(ppufile:tcompilerppufile);override;
  198. end;
  199. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  200. tpropertysym = class(Tstoredsym)
  201. propoptions : tpropertyoptions;
  202. overridenpropsym : tpropertysym;
  203. overridenpropsymderef : tderef;
  204. propdef : tdef;
  205. propdefderef : tderef;
  206. indexdef : tdef;
  207. indexdefderef : tderef;
  208. index,
  209. default : longint;
  210. propaccesslist : array[tpropaccesslisttypes] of tpropaccesslist;
  211. constructor create(const n : string);
  212. destructor destroy;override;
  213. constructor ppuload(ppufile:tcompilerppufile);
  214. function getsize : aint;
  215. procedure ppuwrite(ppufile:tcompilerppufile);override;
  216. procedure buildderef;override;
  217. procedure deref;override;
  218. end;
  219. tconstvalue = record
  220. case integer of
  221. 0: (valueord : tconstexprint);
  222. 1: (valueordptr : tconstptruint);
  223. 2: (valueptr : pointer; len : longint);
  224. end;
  225. tconstsym = class(tstoredsym)
  226. constdef : tdef;
  227. constdefderef : tderef;
  228. consttyp : tconsttyp;
  229. value : tconstvalue;
  230. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  231. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  232. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  233. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  234. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  235. constructor ppuload(ppufile:tcompilerppufile);
  236. destructor destroy;override;
  237. procedure buildderef;override;
  238. procedure deref;override;
  239. procedure ppuwrite(ppufile:tcompilerppufile);override;
  240. end;
  241. tenumsym = class(Tstoredsym)
  242. value : longint;
  243. definition : tenumdef;
  244. definitionderef : tderef;
  245. nextenum : tenumsym;
  246. constructor create(const n : string;def : tenumdef;v : longint);
  247. constructor ppuload(ppufile:tcompilerppufile);
  248. procedure ppuwrite(ppufile:tcompilerppufile);override;
  249. procedure buildderef;override;
  250. procedure deref;override;
  251. procedure order;
  252. end;
  253. tsyssym = class(Tstoredsym)
  254. number : longint;
  255. constructor create(const n : string;l : longint);
  256. constructor ppuload(ppufile:tcompilerppufile);
  257. destructor destroy;override;
  258. procedure ppuwrite(ppufile:tcompilerppufile);override;
  259. end;
  260. const
  261. maxmacrolen=16*1024;
  262. type
  263. pmacrobuffer = ^tmacrobuffer;
  264. tmacrobuffer = array[0..maxmacrolen-1] of char;
  265. tmacro = class(tstoredsym)
  266. {Normally true, but false when a previously defined macro is undef-ed}
  267. defined : boolean;
  268. {True if this is a mac style compiler variable, in which case no macro
  269. substitutions shall be done.}
  270. is_compiler_var : boolean;
  271. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  272. {e. g. an IFDEF which returns false, will not be registered as used,}
  273. {since there is no place to register its use. }
  274. is_used : boolean;
  275. buftext : pchar;
  276. buflen : longint;
  277. constructor create(const n : string);
  278. constructor ppuload(ppufile:tcompilerppufile);
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. destructor destroy;override;
  281. function GetCopy:tmacro;
  282. end;
  283. var
  284. generrorsym : tsym;
  285. implementation
  286. uses
  287. { global }
  288. verbose,
  289. { target }
  290. systems,
  291. { symtable }
  292. defutil,symtable,
  293. fmodule,
  294. { tree }
  295. node,
  296. { aasm }
  297. aasmtai,aasmdata,
  298. { codegen }
  299. paramgr,
  300. procinfo
  301. ;
  302. {****************************************************************************
  303. Helpers
  304. ****************************************************************************}
  305. {****************************************************************************
  306. TSYM (base for all symtypes)
  307. ****************************************************************************}
  308. constructor tstoredsym.create(st:tsymtyp;const n : string);
  309. begin
  310. inherited create(st,n);
  311. { Register in current_module }
  312. if assigned(current_module) then
  313. begin
  314. current_module.symlist.Add(self);
  315. SymId:=current_module.symlist.Count-1;
  316. end;
  317. end;
  318. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  319. begin
  320. SymId:=ppufile.getlongint;
  321. inherited Create(st,ppufile.getstring);
  322. { Register symbol }
  323. current_module.symlist[SymId]:=self;
  324. ppufile.getposinfo(fileinfo);
  325. visibility:=tvisibility(ppufile.getbyte);
  326. ppufile.getsmallset(symoptions);
  327. if sp_has_deprecated_msg in symoptions then
  328. deprecatedmsg:=stringdup(ppufile.getstring)
  329. else
  330. deprecatedmsg:=nil;
  331. end;
  332. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  333. begin
  334. ppufile.putlongint(SymId);
  335. ppufile.putstring(realname);
  336. ppufile.putposinfo(fileinfo);
  337. ppufile.putbyte(byte(visibility));
  338. ppufile.putsmallset(symoptions);
  339. if sp_has_deprecated_msg in symoptions then
  340. ppufile.putstring(deprecatedmsg^);
  341. end;
  342. destructor tstoredsym.destroy;
  343. begin
  344. inherited destroy;
  345. end;
  346. {****************************************************************************
  347. TLABELSYM
  348. ****************************************************************************}
  349. constructor tlabelsym.create(const n : string);
  350. begin
  351. inherited create(labelsym,n);
  352. used:=false;
  353. defined:=false;
  354. nonlocal:=false;
  355. code:=nil;
  356. end;
  357. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  358. begin
  359. inherited ppuload(labelsym,ppufile);
  360. code:=nil;
  361. used:=false;
  362. nonlocal:=false;
  363. defined:=true;
  364. end;
  365. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  366. begin
  367. if owner.symtabletype=globalsymtable then
  368. Message(sym_e_ill_label_decl)
  369. else
  370. begin
  371. inherited ppuwrite(ppufile);
  372. ppufile.writeentry(iblabelsym);
  373. end;
  374. end;
  375. function tlabelsym.mangledname:string;
  376. begin
  377. if not(defined) then
  378. begin
  379. defined:=true;
  380. if nonlocal then
  381. current_asmdata.getglobaljumplabel(asmblocklabel)
  382. else
  383. current_asmdata.getjumplabel(asmblocklabel);
  384. end;
  385. result:=asmblocklabel.name;
  386. end;
  387. {****************************************************************************
  388. TUNITSYM
  389. ****************************************************************************}
  390. constructor tunitsym.create(const n : string;amodule : tobject);
  391. begin
  392. inherited create(unitsym,n);
  393. module:=amodule;
  394. end;
  395. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  396. begin
  397. inherited ppuload(unitsym,ppufile);
  398. module:=nil;
  399. end;
  400. destructor tunitsym.destroy;
  401. begin
  402. inherited destroy;
  403. end;
  404. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  405. begin
  406. inherited ppuwrite(ppufile);
  407. ppufile.writeentry(ibunitsym);
  408. end;
  409. {****************************************************************************
  410. TPROCSYM
  411. ****************************************************************************}
  412. constructor tprocsym.create(const n : string);
  413. begin
  414. inherited create(procsym,n);
  415. FProcdefList:=TFPObjectList.Create(false);
  416. FProcdefderefList:=nil;
  417. { the tprocdef have their own symoptions, make the procsym
  418. always visible }
  419. visibility:=vis_public;
  420. end;
  421. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  422. var
  423. pdderef : tderef;
  424. i,
  425. pdcnt : longint;
  426. begin
  427. inherited ppuload(procsym,ppufile);
  428. FProcdefList:=TFPObjectList.Create(false);
  429. FProcdefDerefList:=TFPList.Create;
  430. pdcnt:=ppufile.getword;
  431. for i:=1 to pdcnt do
  432. begin
  433. ppufile.getderef(pdderef);
  434. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  435. end;
  436. end;
  437. destructor tprocsym.destroy;
  438. begin
  439. FProcdefList.Free;
  440. if assigned(FProcdefDerefList) then
  441. FProcdefDerefList.Free;
  442. inherited destroy;
  443. end;
  444. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  445. var
  446. i : longint;
  447. d : tderef;
  448. begin
  449. inherited ppuwrite(ppufile);
  450. ppufile.putword(FProcdefDerefList.Count);
  451. for i:=0 to FProcdefDerefList.Count-1 do
  452. begin
  453. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  454. ppufile.putderef(d);
  455. end;
  456. ppufile.writeentry(ibprocsym);
  457. end;
  458. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  459. var
  460. i : longint;
  461. pd : tprocdef;
  462. begin
  463. for i:=0 to ProcdefList.Count-1 do
  464. begin
  465. pd:=tprocdef(ProcdefList[i]);
  466. if pd<>skipdef then
  467. MessagePos1(pd.fileinfo,sym_h_param_list,pd.fullprocname(false));
  468. end;
  469. end;
  470. procedure tprocsym.check_forward;
  471. var
  472. i : longint;
  473. pd : tprocdef;
  474. begin
  475. for i:=0 to ProcdefList.Count-1 do
  476. begin
  477. pd:=tprocdef(ProcdefList[i]);
  478. if (pd.owner=owner) and (pd.forwarddef) then
  479. begin
  480. { For mode macpas. Make implicit externals (procedures declared in the interface
  481. section which do not have a counterpart in the implementation)
  482. to be an imported procedure }
  483. if (m_mac in current_settings.modeswitches) and
  484. (pd.interfacedef) then
  485. begin
  486. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  487. if (not current_module.interface_only) then
  488. MessagePos1(pd.fileinfo,sym_w_forward_not_resolved,pd.fullprocname(false));
  489. end
  490. else
  491. begin
  492. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  493. end;
  494. { Turn further error messages off }
  495. pd.forwarddef:=false;
  496. end;
  497. end;
  498. end;
  499. procedure tprocsym.buildderef;
  500. var
  501. i : longint;
  502. pd : tprocdef;
  503. d : tderef;
  504. begin
  505. if not assigned(FProcdefDerefList) then
  506. FProcdefDerefList:=TFPList.Create
  507. else
  508. FProcdefDerefList.Clear;
  509. for i:=0 to ProcdefList.Count-1 do
  510. begin
  511. pd:=tprocdef(ProcdefList[i]);
  512. { only write the proc definitions that belong
  513. to this procsym and are in the global symtable }
  514. if pd.owner=owner then
  515. begin
  516. d.build(pd);
  517. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  518. end;
  519. end;
  520. end;
  521. procedure tprocsym.deref;
  522. var
  523. i : longint;
  524. pd : tprocdef;
  525. d : tderef;
  526. begin
  527. { Clear all procdefs }
  528. ProcdefList.Clear;
  529. if not assigned(FProcdefDerefList) then
  530. internalerror(200611031);
  531. for i:=0 to FProcdefDerefList.Count-1 do
  532. begin
  533. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  534. pd:=tprocdef(d.resolve);
  535. ProcdefList.Add(pd);
  536. end;
  537. end;
  538. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  539. var
  540. i : longint;
  541. pd : tprocdef;
  542. begin
  543. result:=nil;
  544. for i:=0 to ProcdefList.Count-1 do
  545. begin
  546. pd:=tprocdef(ProcdefList[i]);
  547. if pd.proctypeoption=pt then
  548. begin
  549. result:=pd;
  550. exit;
  551. end;
  552. end;
  553. end;
  554. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  555. cpoptions:tcompare_paras_options):Tprocdef;
  556. var
  557. i : longint;
  558. pd : tprocdef;
  559. eq : tequaltype;
  560. begin
  561. result:=nil;
  562. for i:=0 to ProcdefList.Count-1 do
  563. begin
  564. pd:=tprocdef(ProcdefList[i]);
  565. if assigned(retdef) then
  566. eq:=compare_defs(retdef,pd.returndef,nothingn)
  567. else
  568. eq:=te_equal;
  569. if (eq>=te_equal) or
  570. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  571. begin
  572. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  573. if (eq>=te_equal) or
  574. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  575. begin
  576. result:=pd;
  577. exit;
  578. end;
  579. end;
  580. end;
  581. end;
  582. function tprocsym.find_procdef_byoptions(ops: tprocoptions): Tprocdef;
  583. var
  584. i : longint;
  585. pd : tprocdef;
  586. begin
  587. result:=nil;
  588. for i:=0 to ProcdefList.Count-1 do
  589. begin
  590. pd:=tprocdef(ProcdefList[i]);
  591. if ops * pd.procoptions = ops then
  592. begin
  593. result:=pd;
  594. exit;
  595. end;
  596. end;
  597. end;
  598. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  599. var
  600. i : longint;
  601. bestpd,
  602. pd : tprocdef;
  603. eq,besteq : tequaltype;
  604. begin
  605. { This function will return the pprocdef of pprocsym that
  606. is the best match for procvardef. When there are multiple
  607. matches it returns nil.}
  608. result:=nil;
  609. bestpd:=nil;
  610. besteq:=te_incompatible;
  611. for i:=0 to ProcdefList.Count-1 do
  612. begin
  613. pd:=tprocdef(ProcdefList[i]);
  614. eq:=proc_to_procvar_equal(pd,d);
  615. if eq>=te_equal then
  616. begin
  617. { multiple procvars with the same equal level }
  618. if assigned(bestpd) and
  619. (besteq=eq) then
  620. exit;
  621. if eq>besteq then
  622. begin
  623. besteq:=eq;
  624. bestpd:=pd;
  625. end;
  626. end;
  627. end;
  628. result:=bestpd;
  629. end;
  630. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  631. var
  632. paraidx,
  633. i : longint;
  634. bestpd,
  635. hpd,
  636. pd : tprocdef;
  637. convtyp : tconverttype;
  638. eq : tequaltype;
  639. begin
  640. { This function will return the pprocdef of pprocsym that
  641. is the best match for procvardef. When there are multiple
  642. matches it returns nil.}
  643. result:=nil;
  644. bestpd:=nil;
  645. besteq:=te_incompatible;
  646. for i:=0 to ProcdefList.Count-1 do
  647. begin
  648. pd:=tprocdef(ProcdefList[i]);
  649. if (equal_defs(todef,pd.returndef) or
  650. { shortstrings of different lengths are ok as result }
  651. (is_shortstring(todef) and is_shortstring(pd.returndef))) and
  652. { the result type must be always really equal and not an alias,
  653. if you mess with this code, check tw4093 }
  654. ((todef=pd.returndef) or
  655. (
  656. not(df_unique in todef.defoptions) and
  657. not(df_unique in pd.returndef.defoptions)
  658. )
  659. ) then
  660. begin
  661. paraidx:=0;
  662. { ignore vs_hidden parameters }
  663. while (paraidx<pd.paras.count) and
  664. assigned(pd.paras[paraidx]) and
  665. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  666. inc(paraidx);
  667. if (paraidx<pd.paras.count) and
  668. assigned(pd.paras[paraidx]) then
  669. begin
  670. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  671. { alias? if yes, only l1 choice,
  672. if you mess with this code, check tw4093 }
  673. if (eq=te_exact) and
  674. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  675. ((df_unique in fromdef.defoptions) or
  676. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  677. eq:=te_convert_l1;
  678. if eq=te_exact then
  679. begin
  680. besteq:=eq;
  681. result:=pd;
  682. exit;
  683. end;
  684. if eq>besteq then
  685. begin
  686. bestpd:=pd;
  687. besteq:=eq;
  688. end;
  689. end;
  690. end;
  691. end;
  692. result:=bestpd;
  693. end;
  694. function Tprocsym.find_procdef_enumerator_operator(typedef:tdef;var besteq:tequaltype):Tprocdef;
  695. var
  696. paraidx, realparamcount,
  697. i, j : longint;
  698. bestpd,
  699. hpd,
  700. pd : tprocdef;
  701. convtyp : tconverttype;
  702. eq : tequaltype;
  703. begin
  704. { This function will return the pprocdef of pprocsym that
  705. is the best match for procvardef. When there are multiple
  706. matches it returns nil.}
  707. result:=nil;
  708. bestpd:=nil;
  709. besteq:=te_incompatible;
  710. for i:=0 to ProcdefList.Count-1 do
  711. begin
  712. pd:=tprocdef(ProcdefList[i]);
  713. paraidx:=0;
  714. { ignore vs_hidden parameters }
  715. while (paraidx<pd.paras.count) and
  716. assigned(pd.paras[paraidx]) and
  717. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  718. inc(paraidx);
  719. realparamcount:=0;
  720. for j := 0 to pd.paras.Count-1 do
  721. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  722. inc(realparamcount);
  723. if (paraidx<pd.paras.count) and
  724. assigned(pd.paras[paraidx]) and
  725. (realparamcount = 1) then
  726. begin
  727. eq:=compare_defs_ext(typedef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  728. { alias? if yes, only l1 choice,
  729. if you mess with this code, check tw4093 }
  730. if (eq=te_exact) and
  731. (typedef<>tparavarsym(pd.paras[paraidx]).vardef) and
  732. ((df_unique in typedef.defoptions) or
  733. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  734. eq:=te_convert_l1;
  735. if eq=te_exact then
  736. begin
  737. besteq:=eq;
  738. result:=pd;
  739. exit;
  740. end;
  741. if eq>besteq then
  742. begin
  743. bestpd:=pd;
  744. besteq:=eq;
  745. end;
  746. end;
  747. end;
  748. result:=bestpd;
  749. end;
  750. {****************************************************************************
  751. TERRORSYM
  752. ****************************************************************************}
  753. constructor terrorsym.create;
  754. begin
  755. inherited create(errorsym,'');
  756. end;
  757. {****************************************************************************
  758. TPROPERTYSYM
  759. ****************************************************************************}
  760. constructor tpropertysym.create(const n : string);
  761. var
  762. pap : tpropaccesslisttypes;
  763. begin
  764. inherited create(propertysym,n);
  765. propoptions:=[];
  766. index:=0;
  767. default:=0;
  768. propdef:=nil;
  769. indexdef:=nil;
  770. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  771. propaccesslist[pap]:=tpropaccesslist.create;
  772. end;
  773. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  774. var
  775. pap : tpropaccesslisttypes;
  776. begin
  777. inherited ppuload(propertysym,ppufile);
  778. ppufile.getsmallset(propoptions);
  779. ppufile.getderef(overridenpropsymderef);
  780. ppufile.getderef(propdefderef);
  781. index:=ppufile.getlongint;
  782. default:=ppufile.getlongint;
  783. ppufile.getderef(indexdefderef);
  784. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  785. propaccesslist[pap]:=ppufile.getpropaccesslist;
  786. end;
  787. destructor tpropertysym.destroy;
  788. var
  789. pap : tpropaccesslisttypes;
  790. begin
  791. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  792. propaccesslist[pap].free;
  793. inherited destroy;
  794. end;
  795. procedure tpropertysym.buildderef;
  796. var
  797. pap : tpropaccesslisttypes;
  798. begin
  799. overridenpropsymderef.build(overridenpropsym);
  800. propdefderef.build(propdef);
  801. indexdefderef.build(indexdef);
  802. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  803. propaccesslist[pap].buildderef;
  804. end;
  805. procedure tpropertysym.deref;
  806. var
  807. pap : tpropaccesslisttypes;
  808. begin
  809. overridenpropsym:=tpropertysym(overridenpropsymderef.resolve);
  810. indexdef:=tdef(indexdefderef.resolve);
  811. propdef:=tdef(propdefderef.resolve);
  812. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  813. propaccesslist[pap].resolve;
  814. end;
  815. function tpropertysym.getsize : aint;
  816. begin
  817. getsize:=0;
  818. end;
  819. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  820. var
  821. pap : tpropaccesslisttypes;
  822. begin
  823. inherited ppuwrite(ppufile);
  824. ppufile.putsmallset(propoptions);
  825. ppufile.putderef(overridenpropsymderef);
  826. ppufile.putderef(propdefderef);
  827. ppufile.putlongint(index);
  828. ppufile.putlongint(default);
  829. ppufile.putderef(indexdefderef);
  830. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  831. ppufile.putpropaccesslist(propaccesslist[pap]);
  832. ppufile.writeentry(ibpropertysym);
  833. end;
  834. {****************************************************************************
  835. TABSTRACTVARSYM
  836. ****************************************************************************}
  837. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  838. begin
  839. inherited create(st,n);
  840. vardef:=def;
  841. varspez:=vsp;
  842. varstate:=vs_declared;
  843. varoptions:=vopts;
  844. end;
  845. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  846. begin
  847. inherited ppuload(st,ppufile);
  848. varstate:=vs_readwritten;
  849. varspez:=tvarspez(ppufile.getbyte);
  850. varregable:=tvarregable(ppufile.getbyte);
  851. addr_taken:=boolean(ppufile.getbyte);
  852. ppufile.getderef(vardefderef);
  853. ppufile.getsmallset(varoptions);
  854. end;
  855. destructor tabstractvarsym.destroy;
  856. begin
  857. if assigned(notifications) then
  858. notifications.destroy;
  859. inherited destroy;
  860. end;
  861. procedure tabstractvarsym.buildderef;
  862. begin
  863. vardefderef.build(vardef);
  864. end;
  865. procedure tabstractvarsym.deref;
  866. var
  867. oldvarregable: tvarregable;
  868. begin
  869. { setting the vardef also updates varregable. We just loaded this }
  870. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  871. { tw7817b.pp: the address is taken of a local variable in an }
  872. { inlined procedure -> must remain non-regable when inlining) }
  873. oldvarregable:=varregable;
  874. vardef:=tdef(vardefderef.resolve);
  875. varregable:=oldvarregable;
  876. end;
  877. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  878. var
  879. oldintfcrc : boolean;
  880. begin
  881. inherited ppuwrite(ppufile);
  882. ppufile.putbyte(byte(varspez));
  883. oldintfcrc:=ppufile.do_crc;
  884. ppufile.do_crc:=false;
  885. ppufile.putbyte(byte(varregable));
  886. ppufile.putbyte(byte(addr_taken));
  887. ppufile.do_crc:=oldintfcrc;
  888. ppufile.putderef(vardefderef);
  889. ppufile.putsmallset(varoptions);
  890. end;
  891. function tabstractvarsym.getsize : aint;
  892. begin
  893. if assigned(vardef) and
  894. ((vardef.typ<>arraydef) or
  895. is_dynamic_array(vardef) or
  896. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  897. result:=vardef.size
  898. else
  899. result:=0;
  900. end;
  901. function tabstractvarsym.getpackedbitsize : longint;
  902. begin
  903. { bitpacking is only done for ordinals }
  904. if not is_ordinal(vardef) then
  905. internalerror(2006082010);
  906. result:=vardef.packedbitsize;
  907. end;
  908. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  909. begin
  910. { Register variables are not allowed in the following cases:
  911. - regvars are disabled
  912. - exceptions are used (after an exception is raised the contents of the
  913. registers is not valid anymore)
  914. - it has a local copy
  915. - the value needs to be in memory (i.e. reference counted) }
  916. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  917. not(pi_has_assembler_block in current_procinfo.flags) and
  918. not(pi_uses_exceptions in current_procinfo.flags) and
  919. not(vo_has_local_copy in varoptions) and
  920. ((refpara and
  921. (varregable <> vr_none)) or
  922. (not refpara and
  923. not(varregable in [vr_none,vr_addr])))
  924. {$if not defined(powerpc) and not defined(powerpc64)}
  925. and ((vardef.typ <> recorddef) or
  926. (varregable = vr_addr) or
  927. not(varstate in [vs_written,vs_readwritten]));
  928. {$endif}
  929. end;
  930. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  931. var n:Tnotification;
  932. begin
  933. if assigned(notifications) then
  934. begin
  935. n:=Tnotification(notifications.first);
  936. while assigned(n) do
  937. begin
  938. if what in n.flags then
  939. n.callback(what,self);
  940. n:=Tnotification(n.next);
  941. end;
  942. end;
  943. end;
  944. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  945. Tnotification_callback):cardinal;
  946. var n:Tnotification;
  947. begin
  948. if not assigned(notifications) then
  949. notifications:=Tlinkedlist.create;
  950. n:=Tnotification.create(flags,callback);
  951. register_notification:=n.id;
  952. notifications.concat(n);
  953. end;
  954. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  955. var n:Tnotification;
  956. begin
  957. if not assigned(notifications) then
  958. internalerror(200212311)
  959. else
  960. begin
  961. n:=Tnotification(notifications.first);
  962. while assigned(n) do
  963. begin
  964. if n.id=id then
  965. begin
  966. notifications.remove(n);
  967. n.destroy;
  968. exit;
  969. end;
  970. n:=Tnotification(n.next);
  971. end;
  972. internalerror(200212311)
  973. end;
  974. end;
  975. procedure tabstractvarsym.setvardef(def:tdef);
  976. begin
  977. _vardef := def;
  978. { can we load the value into a register ? }
  979. if not assigned(owner) or
  980. (owner.symtabletype in [localsymtable,parasymtable]) or
  981. (
  982. (owner.symtabletype=staticsymtable) and
  983. not(cs_create_pic in current_settings.moduleswitches)
  984. ) then
  985. begin
  986. if tstoreddef(vardef).is_intregable then
  987. varregable:=vr_intreg
  988. else
  989. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  990. if {(
  991. not assigned(owner) or
  992. (owner.symtabletype<>staticsymtable)
  993. ) and }
  994. tstoreddef(vardef).is_fpuregable then
  995. begin
  996. {$ifdef x86}
  997. if use_sse(vardef) then
  998. varregable:=vr_mmreg
  999. else
  1000. {$else x86}
  1001. varregable:=vr_fpureg;
  1002. {$endif x86}
  1003. end;
  1004. end;
  1005. end;
  1006. {****************************************************************************
  1007. TFIELDVARSYM
  1008. ****************************************************************************}
  1009. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1010. begin
  1011. inherited create(fieldvarsym,n,vsp,def,vopts);
  1012. fieldoffset:=-1;
  1013. end;
  1014. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1015. begin
  1016. inherited ppuload(fieldvarsym,ppufile);
  1017. fieldoffset:=ppufile.getaint;
  1018. end;
  1019. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1020. begin
  1021. inherited ppuwrite(ppufile);
  1022. ppufile.putaint(fieldoffset);
  1023. ppufile.writeentry(ibfieldvarsym);
  1024. end;
  1025. function tfieldvarsym.mangledname:string;
  1026. var
  1027. srsym : tsym;
  1028. srsymtable : tsymtable;
  1029. begin
  1030. if sp_static in symoptions then
  1031. begin
  1032. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1033. result:=srsym.mangledname
  1034. { when generating the debug info for the module in which the }
  1035. { symbol is defined, the localsymtable of that module is }
  1036. { already popped from the symtablestack }
  1037. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1038. result:=srsym.mangledname
  1039. else
  1040. internalerror(2007012501);
  1041. end
  1042. else
  1043. result:=inherited mangledname;
  1044. end;
  1045. {****************************************************************************
  1046. TABSTRACTNORMALVARSYM
  1047. ****************************************************************************}
  1048. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1049. begin
  1050. inherited create(st,n,vsp,def,vopts);
  1051. fillchar(localloc,sizeof(localloc),0);
  1052. fillchar(initialloc,sizeof(initialloc),0);
  1053. defaultconstsym:=nil;
  1054. end;
  1055. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1056. begin
  1057. inherited ppuload(st,ppufile);
  1058. fillchar(localloc,sizeof(localloc),0);
  1059. fillchar(initialloc,sizeof(initialloc),0);
  1060. ppufile.getderef(defaultconstsymderef);
  1061. end;
  1062. procedure tabstractnormalvarsym.buildderef;
  1063. begin
  1064. inherited buildderef;
  1065. defaultconstsymderef.build(defaultconstsym);
  1066. end;
  1067. procedure tabstractnormalvarsym.deref;
  1068. begin
  1069. inherited deref;
  1070. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1071. end;
  1072. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1073. begin
  1074. inherited ppuwrite(ppufile);
  1075. ppufile.putderef(defaultconstsymderef);
  1076. end;
  1077. {****************************************************************************
  1078. Tstaticvarsym
  1079. ****************************************************************************}
  1080. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1081. begin
  1082. inherited create(staticvarsym,n,vsp,def,vopts);
  1083. _mangledname:=nil;
  1084. end;
  1085. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1086. begin
  1087. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1088. end;
  1089. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1090. begin
  1091. tstaticvarsym(self).create(n,vsp,def,[]);
  1092. set_mangledname(mangled);
  1093. end;
  1094. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1095. begin
  1096. inherited ppuload(staticvarsym,ppufile);
  1097. if vo_has_mangledname in varoptions then
  1098. _mangledname:=stringdup(ppufile.getstring)
  1099. else
  1100. _mangledname:=nil;
  1101. end;
  1102. destructor tstaticvarsym.destroy;
  1103. begin
  1104. if assigned(_mangledname) then
  1105. begin
  1106. {$ifdef MEMDEBUG}
  1107. memmanglednames.start;
  1108. {$endif MEMDEBUG}
  1109. stringdispose(_mangledname);
  1110. {$ifdef MEMDEBUG}
  1111. memmanglednames.stop;
  1112. {$endif MEMDEBUG}
  1113. end;
  1114. inherited destroy;
  1115. end;
  1116. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1117. begin
  1118. inherited ppuwrite(ppufile);
  1119. if vo_has_mangledname in varoptions then
  1120. ppufile.putstring(_mangledname^);
  1121. ppufile.writeentry(ibstaticvarsym);
  1122. end;
  1123. function tstaticvarsym.mangledname:string;
  1124. var
  1125. prefix : string[2];
  1126. begin
  1127. if not assigned(_mangledname) then
  1128. begin
  1129. if (vo_is_typed_const in varoptions) then
  1130. prefix:='TC'
  1131. else
  1132. prefix:='U';
  1133. {$ifdef compress}
  1134. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1135. {$else}
  1136. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1137. {$endif}
  1138. end;
  1139. result:=_mangledname^;
  1140. end;
  1141. procedure tstaticvarsym.set_mangledname(const s:string);
  1142. begin
  1143. stringdispose(_mangledname);
  1144. {$ifdef compress}
  1145. _mangledname:=stringdup(minilzw_encode(s));
  1146. {$else}
  1147. _mangledname:=stringdup(s);
  1148. {$endif}
  1149. include(varoptions,vo_has_mangledname);
  1150. end;
  1151. {****************************************************************************
  1152. TLOCALVARSYM
  1153. ****************************************************************************}
  1154. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1155. begin
  1156. inherited create(localvarsym,n,vsp,def,vopts);
  1157. end;
  1158. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1159. begin
  1160. inherited ppuload(localvarsym,ppufile);
  1161. end;
  1162. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1163. begin
  1164. inherited ppuwrite(ppufile);
  1165. ppufile.writeentry(iblocalvarsym);
  1166. end;
  1167. {****************************************************************************
  1168. TPARAVARSYM
  1169. ****************************************************************************}
  1170. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1171. begin
  1172. inherited create(paravarsym,n,vsp,def,vopts);
  1173. if (vsp in [vs_var,vs_value,vs_const]) then
  1174. varstate := vs_initialised;
  1175. paranr:=nr;
  1176. paraloc[calleeside].init;
  1177. paraloc[callerside].init;
  1178. end;
  1179. destructor tparavarsym.destroy;
  1180. begin
  1181. paraloc[calleeside].done;
  1182. paraloc[callerside].done;
  1183. inherited destroy;
  1184. end;
  1185. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1186. var
  1187. b : byte;
  1188. begin
  1189. inherited ppuload(paravarsym,ppufile);
  1190. paranr:=ppufile.getword;
  1191. { The var state of parameter symbols is fixed after writing them so
  1192. we write them to the unit file.
  1193. This enables constant folding for inline procedures loaded from units
  1194. }
  1195. varstate:=tvarstate(ppufile.getbyte);
  1196. paraloc[calleeside].init;
  1197. paraloc[callerside].init;
  1198. if vo_has_explicit_paraloc in varoptions then
  1199. begin
  1200. paraloc[callerside].alignment:=ppufile.getbyte;
  1201. b:=ppufile.getbyte;
  1202. if b<>sizeof(paraloc[callerside].location^) then
  1203. internalerror(200411154);
  1204. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1205. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1206. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1207. end;
  1208. end;
  1209. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1210. var
  1211. oldintfcrc : boolean;
  1212. begin
  1213. inherited ppuwrite(ppufile);
  1214. ppufile.putword(paranr);
  1215. { The var state of parameter symbols is fixed after writing them so
  1216. we write them to the unit file.
  1217. This enables constant folding for inline procedures loaded from units
  1218. }
  1219. oldintfcrc:=ppufile.do_crc;
  1220. ppufile.do_crc:=false;
  1221. ppufile.putbyte(ord(varstate));
  1222. ppufile.do_crc:=oldintfcrc;
  1223. if vo_has_explicit_paraloc in varoptions then
  1224. begin
  1225. paraloc[callerside].check_simple_location;
  1226. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1227. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1228. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1229. end;
  1230. ppufile.writeentry(ibparavarsym);
  1231. end;
  1232. {****************************************************************************
  1233. TABSOLUTEVARSYM
  1234. ****************************************************************************}
  1235. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1236. begin
  1237. inherited create(absolutevarsym,n,vs_value,def,[]);
  1238. ref:=nil;
  1239. end;
  1240. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1241. begin
  1242. inherited create(absolutevarsym,n,vs_value,def,[]);
  1243. ref:=_ref;
  1244. end;
  1245. destructor tabsolutevarsym.destroy;
  1246. begin
  1247. if assigned(ref) then
  1248. ref.free;
  1249. inherited destroy;
  1250. end;
  1251. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1252. begin
  1253. inherited ppuload(absolutevarsym,ppufile);
  1254. ref:=nil;
  1255. asmname:=nil;
  1256. abstyp:=absolutetyp(ppufile.getbyte);
  1257. {$ifdef i386}
  1258. absseg:=false;
  1259. {$endif i386}
  1260. case abstyp of
  1261. tovar :
  1262. ref:=ppufile.getpropaccesslist;
  1263. toasm :
  1264. asmname:=stringdup(ppufile.getstring);
  1265. toaddr :
  1266. begin
  1267. addroffset:=ppufile.getaword;
  1268. {$ifdef i386}
  1269. absseg:=boolean(ppufile.getbyte);
  1270. {$endif i386}
  1271. end;
  1272. end;
  1273. end;
  1274. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1275. begin
  1276. inherited ppuwrite(ppufile);
  1277. ppufile.putbyte(byte(abstyp));
  1278. case abstyp of
  1279. tovar :
  1280. ppufile.putpropaccesslist(ref);
  1281. toasm :
  1282. ppufile.putstring(asmname^);
  1283. toaddr :
  1284. begin
  1285. ppufile.putaword(addroffset);
  1286. {$ifdef i386}
  1287. ppufile.putbyte(byte(absseg));
  1288. {$endif i386}
  1289. end;
  1290. end;
  1291. ppufile.writeentry(ibabsolutevarsym);
  1292. end;
  1293. procedure tabsolutevarsym.buildderef;
  1294. begin
  1295. inherited buildderef;
  1296. if (abstyp=tovar) then
  1297. ref.buildderef;
  1298. end;
  1299. procedure tabsolutevarsym.deref;
  1300. begin
  1301. inherited deref;
  1302. { own absolute deref }
  1303. if (abstyp=tovar) then
  1304. ref.resolve;
  1305. end;
  1306. function tabsolutevarsym.mangledname : string;
  1307. begin
  1308. case abstyp of
  1309. toasm :
  1310. mangledname:=asmname^;
  1311. toaddr :
  1312. mangledname:='$'+tostr(addroffset);
  1313. else
  1314. internalerror(200411062);
  1315. end;
  1316. end;
  1317. {****************************************************************************
  1318. TCONSTSYM
  1319. ****************************************************************************}
  1320. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1321. begin
  1322. inherited create(constsym,n);
  1323. fillchar(value, sizeof(value), #0);
  1324. consttyp:=t;
  1325. value.valueord:=v;
  1326. constdef:=def;
  1327. end;
  1328. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1329. begin
  1330. inherited create(constsym,n);
  1331. fillchar(value, sizeof(value), #0);
  1332. consttyp:=t;
  1333. value.valueordptr:=v;
  1334. constdef:=def;
  1335. end;
  1336. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1337. begin
  1338. inherited create(constsym,n);
  1339. fillchar(value, sizeof(value), #0);
  1340. consttyp:=t;
  1341. value.valueptr:=v;
  1342. constdef:=def;
  1343. end;
  1344. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1345. begin
  1346. inherited create(constsym,n);
  1347. fillchar(value, sizeof(value), #0);
  1348. consttyp:=t;
  1349. value.valueptr:=str;
  1350. constdef:=nil;
  1351. value.len:=l;
  1352. end;
  1353. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1354. begin
  1355. inherited create(constsym,n);
  1356. fillchar(value, sizeof(value), #0);
  1357. consttyp:=t;
  1358. pcompilerwidestring(value.valueptr):=pw;
  1359. constdef:=nil;
  1360. value.len:=getlengthwidestring(pw);
  1361. end;
  1362. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1363. var
  1364. pd : pbestreal;
  1365. ps : pnormalset;
  1366. pc : pchar;
  1367. pw : pcompilerwidestring;
  1368. i : longint;
  1369. begin
  1370. inherited ppuload(constsym,ppufile);
  1371. constdef:=nil;
  1372. consttyp:=tconsttyp(ppufile.getbyte);
  1373. fillchar(value, sizeof(value), #0);
  1374. case consttyp of
  1375. constord :
  1376. begin
  1377. ppufile.getderef(constdefderef);
  1378. value.valueord:=ppufile.getexprint;
  1379. end;
  1380. constpointer :
  1381. begin
  1382. ppufile.getderef(constdefderef);
  1383. value.valueordptr:=ppufile.getptruint;
  1384. end;
  1385. constwstring :
  1386. begin
  1387. initwidestring(pw);
  1388. setlengthwidestring(pw,ppufile.getlongint);
  1389. pw^.len:=pw^.maxlen;
  1390. { don't use getdata, because the compilerwidechars may have to
  1391. be byteswapped
  1392. }
  1393. {$if sizeof(tcompilerwidechar) = 2}
  1394. for i:=0 to pw^.len-1 do
  1395. pw^.data[i]:=ppufile.getword;
  1396. {$elseif sizeof(tcompilerwidechar) = 4}
  1397. for i:=0 to pw^.len-1 do
  1398. pw^.data[i]:=cardinal(ppufile.getlongint);
  1399. {$else}
  1400. {$error Unsupported tcompilerwidechar size}
  1401. {$endif}
  1402. pcompilerwidestring(value.valueptr):=pw;
  1403. end;
  1404. conststring,
  1405. constresourcestring :
  1406. begin
  1407. value.len:=ppufile.getlongint;
  1408. getmem(pc,value.len+1);
  1409. ppufile.getdata(pc^,value.len);
  1410. pc[value.len]:=#0;
  1411. value.valueptr:=pc;
  1412. end;
  1413. constreal :
  1414. begin
  1415. new(pd);
  1416. pd^:=ppufile.getreal;
  1417. value.valueptr:=pd;
  1418. end;
  1419. constset :
  1420. begin
  1421. ppufile.getderef(constdefderef);
  1422. new(ps);
  1423. ppufile.getnormalset(ps^);
  1424. value.valueptr:=ps;
  1425. end;
  1426. constguid :
  1427. begin
  1428. new(pguid(value.valueptr));
  1429. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1430. end;
  1431. constnil : ;
  1432. else
  1433. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1434. end;
  1435. end;
  1436. destructor tconstsym.destroy;
  1437. begin
  1438. case consttyp of
  1439. conststring,
  1440. constresourcestring :
  1441. freemem(pchar(value.valueptr),value.len+1);
  1442. constwstring :
  1443. donewidestring(pcompilerwidestring(value.valueptr));
  1444. constreal :
  1445. dispose(pbestreal(value.valueptr));
  1446. constset :
  1447. dispose(pnormalset(value.valueptr));
  1448. constguid :
  1449. dispose(pguid(value.valueptr));
  1450. end;
  1451. inherited destroy;
  1452. end;
  1453. procedure tconstsym.buildderef;
  1454. begin
  1455. if consttyp in [constord,constpointer,constset] then
  1456. constdefderef.build(constdef);
  1457. end;
  1458. procedure tconstsym.deref;
  1459. begin
  1460. if consttyp in [constord,constpointer,constset] then
  1461. constdef:=tdef(constdefderef.resolve);
  1462. end;
  1463. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1464. begin
  1465. inherited ppuwrite(ppufile);
  1466. ppufile.putbyte(byte(consttyp));
  1467. case consttyp of
  1468. constnil : ;
  1469. constord :
  1470. begin
  1471. ppufile.putderef(constdefderef);
  1472. ppufile.putexprint(value.valueord);
  1473. end;
  1474. constpointer :
  1475. begin
  1476. ppufile.putderef(constdefderef);
  1477. ppufile.putptruint(value.valueordptr);
  1478. end;
  1479. constwstring :
  1480. begin
  1481. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1482. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1483. end;
  1484. conststring,
  1485. constresourcestring :
  1486. begin
  1487. ppufile.putlongint(value.len);
  1488. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1489. end;
  1490. constreal :
  1491. ppufile.putreal(pbestreal(value.valueptr)^);
  1492. constset :
  1493. begin
  1494. ppufile.putderef(constdefderef);
  1495. ppufile.putnormalset(value.valueptr^);
  1496. end;
  1497. constguid :
  1498. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1499. else
  1500. internalerror(13);
  1501. end;
  1502. ppufile.writeentry(ibconstsym);
  1503. end;
  1504. {****************************************************************************
  1505. TENUMSYM
  1506. ****************************************************************************}
  1507. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1508. begin
  1509. inherited create(enumsym,n);
  1510. definition:=def;
  1511. value:=v;
  1512. { First entry? Then we need to set the minval }
  1513. if def.firstenum=nil then
  1514. begin
  1515. if v>0 then
  1516. def.has_jumps:=true;
  1517. def.setmin(v);
  1518. def.setmax(v);
  1519. end
  1520. else
  1521. begin
  1522. { check for jumps }
  1523. if v>def.max+1 then
  1524. def.has_jumps:=true;
  1525. { update low and high }
  1526. if def.min>v then
  1527. def.setmin(v);
  1528. if def.max<v then
  1529. def.setmax(v);
  1530. end;
  1531. order;
  1532. end;
  1533. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1534. begin
  1535. inherited ppuload(enumsym,ppufile);
  1536. ppufile.getderef(definitionderef);
  1537. value:=ppufile.getlongint;
  1538. nextenum := Nil;
  1539. end;
  1540. procedure tenumsym.buildderef;
  1541. begin
  1542. definitionderef.build(definition);
  1543. end;
  1544. procedure tenumsym.deref;
  1545. begin
  1546. definition:=tenumdef(definitionderef.resolve);
  1547. order;
  1548. end;
  1549. procedure tenumsym.order;
  1550. var
  1551. sym : tenumsym;
  1552. begin
  1553. sym := tenumsym(definition.firstenum);
  1554. if sym = nil then
  1555. begin
  1556. definition.firstenum := self;
  1557. nextenum := nil;
  1558. exit;
  1559. end;
  1560. { reorder the symbols in increasing value }
  1561. if value < sym.value then
  1562. begin
  1563. nextenum := sym;
  1564. definition.firstenum := self;
  1565. end
  1566. else
  1567. begin
  1568. while (sym.value <= value) and assigned(sym.nextenum) do
  1569. sym := sym.nextenum;
  1570. nextenum := sym.nextenum;
  1571. sym.nextenum := self;
  1572. end;
  1573. end;
  1574. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1575. begin
  1576. inherited ppuwrite(ppufile);
  1577. ppufile.putderef(definitionderef);
  1578. ppufile.putlongint(value);
  1579. ppufile.writeentry(ibenumsym);
  1580. end;
  1581. {****************************************************************************
  1582. TTYPESYM
  1583. ****************************************************************************}
  1584. constructor ttypesym.create(const n : string;def:tdef);
  1585. begin
  1586. inherited create(typesym,n);
  1587. typedef:=def;
  1588. { register the typesym for the definition }
  1589. if assigned(typedef) and
  1590. (typedef.typ<>errordef) and
  1591. not(assigned(typedef.typesym)) then
  1592. typedef.typesym:=self;
  1593. end;
  1594. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1595. begin
  1596. inherited ppuload(typesym,ppufile);
  1597. ppufile.getderef(typedefderef);
  1598. end;
  1599. procedure ttypesym.buildderef;
  1600. begin
  1601. typedefderef.build(typedef);
  1602. end;
  1603. procedure ttypesym.deref;
  1604. begin
  1605. typedef:=tdef(typedefderef.resolve);
  1606. end;
  1607. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1608. begin
  1609. inherited ppuwrite(ppufile);
  1610. ppufile.putderef(typedefderef);
  1611. ppufile.writeentry(ibtypesym);
  1612. end;
  1613. {****************************************************************************
  1614. TSYSSYM
  1615. ****************************************************************************}
  1616. constructor tsyssym.create(const n : string;l : longint);
  1617. begin
  1618. inherited create(syssym,n);
  1619. number:=l;
  1620. end;
  1621. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1622. begin
  1623. inherited ppuload(syssym,ppufile);
  1624. number:=ppufile.getlongint;
  1625. end;
  1626. destructor tsyssym.destroy;
  1627. begin
  1628. inherited destroy;
  1629. end;
  1630. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1631. begin
  1632. inherited ppuwrite(ppufile);
  1633. ppufile.putlongint(number);
  1634. ppufile.writeentry(ibsyssym);
  1635. end;
  1636. {*****************************************************************************
  1637. TMacro
  1638. *****************************************************************************}
  1639. constructor tmacro.create(const n : string);
  1640. begin
  1641. inherited create(macrosym,n);
  1642. owner:=nil;
  1643. defined:=false;
  1644. is_used:=false;
  1645. is_compiler_var:=false;
  1646. buftext:=nil;
  1647. buflen:=0;
  1648. end;
  1649. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1650. begin
  1651. inherited ppuload(macrosym,ppufile);
  1652. defined:=boolean(ppufile.getbyte);
  1653. is_compiler_var:=boolean(ppufile.getbyte);
  1654. is_used:=false;
  1655. buflen:= ppufile.getlongint;
  1656. if buflen > 0 then
  1657. begin
  1658. getmem(buftext, buflen);
  1659. ppufile.getdata(buftext^, buflen)
  1660. end
  1661. else
  1662. buftext:=nil;
  1663. end;
  1664. destructor tmacro.destroy;
  1665. begin
  1666. if assigned(buftext) then
  1667. freemem(buftext);
  1668. inherited destroy;
  1669. end;
  1670. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1671. begin
  1672. inherited ppuwrite(ppufile);
  1673. ppufile.putbyte(byte(defined));
  1674. ppufile.putbyte(byte(is_compiler_var));
  1675. ppufile.putlongint(buflen);
  1676. if buflen > 0 then
  1677. ppufile.putdata(buftext^,buflen);
  1678. ppufile.writeentry(ibmacrosym);
  1679. end;
  1680. function tmacro.GetCopy:tmacro;
  1681. var
  1682. p : tmacro;
  1683. begin
  1684. p:=tmacro.create(realname);
  1685. p.defined:=defined;
  1686. p.is_used:=is_used;
  1687. p.is_compiler_var:=is_compiler_var;
  1688. p.buflen:=buflen;
  1689. if assigned(buftext) then
  1690. begin
  1691. getmem(p.buftext,buflen);
  1692. move(buftext^,p.buftext^,buflen);
  1693. end;
  1694. Result:=p;
  1695. end;
  1696. end.