symsym.pas 63 KB

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