symsym.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  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,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. { ppu }
  28. ppu,
  29. cclasses,symnot,
  30. { aasm }
  31. aasmbase,aasmtai,
  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(const n : string);
  39. constructor ppuload(ppufile:tcompilerppufile);
  40. destructor destroy;override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  42. {$ifdef GDB}
  43. function get_var_value(const s:string):string;
  44. function stabstr_evaluate(const s:string;vars:array of string):Pchar;
  45. procedure concatstabto(asmlist : taasmoutput);
  46. {$endif GDB}
  47. function mangledname : string; virtual;
  48. end;
  49. tlabelsym = class(tstoredsym)
  50. used,
  51. defined : boolean;
  52. { points to the matching node, only valid resulttype pass is run and
  53. the goto<->label relation in the node tree is created, should
  54. be a tnode }
  55. code : pointer;
  56. { when the label is defined in an asm block, this points to the
  57. generated asmlabel }
  58. asmblocklabel : tasmlabel;
  59. constructor create(const n : string);
  60. constructor ppuload(ppufile:tcompilerppufile);
  61. procedure ppuwrite(ppufile:tcompilerppufile);override;
  62. {$ifdef GDB}
  63. function stabstring : pchar;override;
  64. {$endif GDB}
  65. end;
  66. tunitsym = class(Tstoredsym)
  67. unitsymtable : tsymtable;
  68. constructor create(const n : string;ref : tsymtable);
  69. constructor ppuload(ppufile:tcompilerppufile);
  70. destructor destroy;override;
  71. procedure ppuwrite(ppufile:tcompilerppufile);override;
  72. end;
  73. terrorsym = class(Tsym)
  74. constructor create;
  75. end;
  76. Tprocdefcallback = procedure(p:Tprocdef;arg:pointer);
  77. tprocsym = class(tstoredsym)
  78. protected
  79. pdlistfirst,
  80. pdlistlast : pprocdeflist; { linked list of overloaded procdefs }
  81. function getprocdef(nr:cardinal):Tprocdef;
  82. public
  83. procdef_count : byte;
  84. {$ifdef GDB}
  85. is_global : boolean;
  86. {$endif GDB}
  87. overloadchecked : boolean;
  88. property procdef[nr:cardinal]:Tprocdef read getprocdef;
  89. constructor create(const n : string);
  90. constructor ppuload(ppufile:tcompilerppufile);
  91. destructor destroy;override;
  92. { writes all declarations except the specified one }
  93. procedure write_parameter_lists(skipdef:tprocdef);
  94. { tests, if all procedures definitions are defined and not }
  95. { only forward }
  96. procedure check_forward;
  97. procedure unchain_overload;
  98. procedure ppuwrite(ppufile:tcompilerppufile);override;
  99. procedure buildderef;override;
  100. procedure deref;override;
  101. procedure addprocdef(p:tprocdef);
  102. procedure addprocdef_deref(const d:tderef);
  103. procedure add_para_match_to(Aprocsym:Tprocsym;cpoptions:tcompare_paras_options);
  104. procedure concat_procdefs_to(s:Tprocsym);
  105. procedure foreach_procdef_static(proc2call:Tprocdefcallback;arg:pointer);
  106. function first_procdef:Tprocdef;
  107. function last_procdef:Tprocdef;
  108. function search_procdef_nopara_boolret:Tprocdef;
  109. function search_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  110. function search_procdef_bypara(para:tlist;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  111. function search_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  112. function search_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  113. function write_references(ppufile:tcompilerppufile;locals:boolean):boolean;override;
  114. { currobjdef is the object def to assume, this is necessary for protected and
  115. private,
  116. context is the object def we're really in, this is for the strict stuff
  117. }
  118. function is_visible_for_object(currobjdef:tdef;context:tdef):boolean;override;
  119. {$ifdef GDB}
  120. function stabstring : pchar;override;
  121. {$endif GDB}
  122. end;
  123. ttypesym = class(Tstoredsym)
  124. restype : ttype;
  125. constructor create(const n : string;const tt : ttype);
  126. constructor ppuload(ppufile:tcompilerppufile);
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;
  128. procedure buildderef;override;
  129. procedure deref;override;
  130. function gettypedef:tdef;override;
  131. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  132. function write_references(ppufile:tcompilerppufile;locals:boolean):boolean;override;
  133. {$ifdef GDB}
  134. function stabstring : pchar;override;
  135. {$endif GDB}
  136. end;
  137. tabstractvarsym = class(tstoredsym)
  138. varoptions : tvaroptions;
  139. varspez : tvarspez; { sets the type of access }
  140. varregable : tvarregable;
  141. varstate : tvarstate;
  142. notifications : Tlinkedlist;
  143. constructor create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  144. constructor ppuload(ppufile:tcompilerppufile);
  145. destructor destroy;override;
  146. procedure ppuwrite(ppufile:tcompilerppufile);override;
  147. procedure buildderef;override;
  148. procedure deref;override;
  149. function getsize : longint;
  150. function is_regvar:boolean;
  151. procedure trigger_notifications(what:Tnotification_flag);
  152. function register_notification(flags:Tnotification_flags;
  153. callback:Tnotification_callback):cardinal;
  154. procedure unregister_notification(id:cardinal);
  155. private
  156. procedure setvartype(const newtype: ttype);
  157. _vartype : ttype;
  158. public
  159. property vartype: ttype read _vartype write setvartype;
  160. end;
  161. tfieldvarsym = class(tabstractvarsym)
  162. fieldoffset : aint; { offset in record/object }
  163. constructor create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  164. constructor ppuload(ppufile:tcompilerppufile);
  165. procedure ppuwrite(ppufile:tcompilerppufile);override;
  166. {$ifdef GDB}
  167. function stabstring : pchar;override;
  168. {$endif GDB}
  169. end;
  170. tabstractnormalvarsym = class(tabstractvarsym)
  171. defaultconstsym : tsym;
  172. defaultconstsymderef : tderef;
  173. localloc : TLocation; { register/reference for local var }
  174. constructor create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  175. constructor ppuload(ppufile:tcompilerppufile);
  176. procedure ppuwrite(ppufile:tcompilerppufile);override;
  177. procedure buildderef;override;
  178. procedure deref;override;
  179. end;
  180. tlocalvarsym = class(tabstractnormalvarsym)
  181. constructor create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  182. constructor ppuload(ppufile:tcompilerppufile);
  183. procedure ppuwrite(ppufile:tcompilerppufile);override;
  184. {$ifdef GDB}
  185. function stabstring : pchar;override;
  186. {$endif GDB}
  187. end;
  188. tparavarsym = class(tabstractnormalvarsym)
  189. paraloc : array[tcallercallee] of TCGPara;
  190. paranr : word; { position of this parameter }
  191. {$ifdef EXTDEBUG}
  192. eqval : tequaltype;
  193. {$endif EXTDEBUG}
  194. constructor create(const n : string;nr:word;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  195. constructor ppuload(ppufile:tcompilerppufile);
  196. destructor destroy;override;
  197. procedure ppuwrite(ppufile:tcompilerppufile);override;
  198. {$ifdef GDB}
  199. function stabstring : pchar;override;
  200. {$endif GDB}
  201. end;
  202. tglobalvarsym = class(tabstractnormalvarsym)
  203. private
  204. _mangledname : pstring;
  205. public
  206. constructor create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  207. constructor create_dll(const n : string;vsp:tvarspez;const tt : ttype);
  208. constructor create_C(const n,mangled : string;vsp:tvarspez;const tt : ttype);
  209. constructor ppuload(ppufile:tcompilerppufile);
  210. destructor destroy;override;
  211. procedure ppuwrite(ppufile:tcompilerppufile);override;
  212. function mangledname:string;override;
  213. procedure set_mangledname(const s:string);
  214. {$ifdef GDB}
  215. function stabstring : pchar;override;
  216. {$endif GDB}
  217. end;
  218. tabsolutevarsym = class(tabstractvarsym)
  219. public
  220. abstyp : absolutetyp;
  221. {$ifdef i386}
  222. absseg : boolean;
  223. {$endif i386}
  224. asmname : pstring;
  225. addroffset : aint;
  226. ref : tsymlist;
  227. constructor create(const n : string;const tt : ttype);
  228. constructor create_ref(const n : string;const tt : ttype;_ref:tsymlist);
  229. destructor destroy;override;
  230. constructor ppuload(ppufile:tcompilerppufile);
  231. procedure buildderef;override;
  232. procedure deref;override;
  233. function mangledname : string;override;
  234. procedure ppuwrite(ppufile:tcompilerppufile);override;
  235. {$ifdef gdb}
  236. function stabstring:Pchar;override;
  237. {$endif gdb}
  238. end;
  239. tpropertysym = class(Tstoredsym)
  240. propoptions : tpropertyoptions;
  241. propoverriden : tpropertysym;
  242. propoverridenderef : tderef;
  243. proptype,
  244. indextype : ttype;
  245. index,
  246. default : longint;
  247. readaccess,
  248. writeaccess,
  249. storedaccess : tsymlist;
  250. constructor create(const n : string);
  251. destructor destroy;override;
  252. constructor ppuload(ppufile:tcompilerppufile);
  253. function getsize : longint;
  254. procedure ppuwrite(ppufile:tcompilerppufile);override;
  255. function gettypedef:tdef;override;
  256. procedure buildderef;override;
  257. procedure deref;override;
  258. procedure dooverride(overriden:tpropertysym);
  259. end;
  260. ttypedconstsym = class(tstoredsym)
  261. private
  262. _mangledname : pstring;
  263. public
  264. typedconsttype : ttype;
  265. is_writable : boolean;
  266. constructor create(const n : string;p : tdef;writable : boolean);
  267. constructor createtype(const n : string;const tt : ttype;writable : boolean);
  268. constructor ppuload(ppufile:tcompilerppufile);
  269. destructor destroy;override;
  270. function mangledname : string;override;
  271. procedure ppuwrite(ppufile:tcompilerppufile);override;
  272. procedure buildderef;override;
  273. procedure deref;override;
  274. function getsize:longint;
  275. {$ifdef GDB}
  276. function stabstring : pchar;override;
  277. {$endif GDB}
  278. end;
  279. tconstvalue = record
  280. case integer of
  281. 0: (valueord : tconstexprint);
  282. 1: (valueordptr : tconstptruint);
  283. 2: (valueptr : pointer; len : longint);
  284. end;
  285. tconstsym = class(tstoredsym)
  286. consttype : ttype;
  287. consttyp : tconsttyp;
  288. value : tconstvalue;
  289. resstrindex : longint; { needed for resource strings }
  290. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;const tt:ttype);
  291. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;const tt:ttype);
  292. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;const tt:ttype);
  293. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  294. constructor ppuload(ppufile:tcompilerppufile);
  295. destructor destroy;override;
  296. procedure buildderef;override;
  297. procedure deref;override;
  298. procedure ppuwrite(ppufile:tcompilerppufile);override;
  299. {$ifdef GDB}
  300. function stabstring : pchar;override;
  301. {$endif GDB}
  302. end;
  303. tenumsym = class(Tstoredsym)
  304. value : longint;
  305. definition : tenumdef;
  306. definitionderef : tderef;
  307. nextenum : tenumsym;
  308. constructor create(const n : string;def : tenumdef;v : longint);
  309. constructor ppuload(ppufile:tcompilerppufile);
  310. procedure ppuwrite(ppufile:tcompilerppufile);override;
  311. procedure buildderef;override;
  312. procedure deref;override;
  313. procedure order;
  314. end;
  315. tsyssym = class(Tstoredsym)
  316. number : longint;
  317. constructor create(const n : string;l : longint);
  318. constructor ppuload(ppufile:tcompilerppufile);
  319. destructor destroy;override;
  320. procedure ppuwrite(ppufile:tcompilerppufile);override;
  321. end;
  322. const
  323. maxmacrolen=16*1024;
  324. type
  325. pmacrobuffer = ^tmacrobuffer;
  326. tmacrobuffer = array[0..maxmacrolen-1] of char;
  327. tmacro = class(tstoredsym)
  328. {Normally true, but false when a previously defined macro is undef-ed}
  329. defined : boolean;
  330. {True if this is a mac style compiler variable, in which case no macro
  331. substitutions shall be done.}
  332. is_compiler_var : boolean;
  333. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  334. {e. g. an IFDEF which returns false, will not be registered as used,}
  335. {since there is no place to register its use. }
  336. is_used : boolean;
  337. buftext : pchar;
  338. buflen : longint;
  339. constructor create(const n : string);
  340. constructor ppuload(ppufile:tcompilerppufile);
  341. procedure ppuwrite(ppufile:tcompilerppufile);override;
  342. destructor destroy;override;
  343. end;
  344. { compiler generated symbol to point to rtti and init/finalize tables }
  345. trttisym = class(tstoredsym)
  346. private
  347. _mangledname : pstring;
  348. public
  349. lab : tasmsymbol;
  350. rttityp : trttitype;
  351. constructor create(const n:string;rt:trttitype);
  352. constructor ppuload(ppufile:tcompilerppufile);
  353. destructor destroy;override;
  354. procedure ppuwrite(ppufile:tcompilerppufile);override;
  355. function mangledname:string;override;
  356. function get_label:tasmsymbol;
  357. end;
  358. var
  359. generrorsym : tsym;
  360. implementation
  361. uses
  362. { global }
  363. verbose,
  364. { target }
  365. systems,
  366. { symtable }
  367. defutil,symtable,
  368. { tree }
  369. node,
  370. { aasm }
  371. {$ifdef gdb}
  372. gdb,
  373. {$endif gdb}
  374. { codegen }
  375. paramgr,cresstr,
  376. procinfo
  377. ;
  378. {****************************************************************************
  379. Helpers
  380. ****************************************************************************}
  381. {****************************************************************************
  382. TSYM (base for all symtypes)
  383. ****************************************************************************}
  384. constructor tstoredsym.create(const n : string);
  385. begin
  386. inherited create(n);
  387. end;
  388. constructor tstoredsym.ppuload(ppufile:tcompilerppufile);
  389. var
  390. nr : word;
  391. s : string;
  392. begin
  393. nr:=ppufile.getword;
  394. s:=ppufile.getstring;
  395. if s[1]='$' then
  396. inherited createname(copy(s,2,255))
  397. else
  398. inherited createname(upper(s));
  399. _realname:=stringdup(s);
  400. typ:=abstractsym;
  401. { force the correct indexnr. must be after create! }
  402. indexnr:=nr;
  403. ppufile.getposinfo(fileinfo);
  404. ppufile.getsmallset(symoptions);
  405. lastref:=nil;
  406. defref:=nil;
  407. refs:=0;
  408. lastwritten:=nil;
  409. refcount:=0;
  410. {$ifdef GDB}
  411. isstabwritten := false;
  412. {$endif GDB}
  413. end;
  414. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  415. begin
  416. ppufile.putword(indexnr);
  417. ppufile.putstring(_realname^);
  418. ppufile.putposinfo(fileinfo);
  419. ppufile.putsmallset(symoptions);
  420. end;
  421. destructor tstoredsym.destroy;
  422. begin
  423. if assigned(defref) then
  424. begin
  425. {$ifdef MEMDEBUG}
  426. membrowser.start;
  427. {$endif MEMDEBUG}
  428. defref.freechain;
  429. defref.free;
  430. {$ifdef MEMDEBUG}
  431. membrowser.stop;
  432. {$endif MEMDEBUG}
  433. end;
  434. inherited destroy;
  435. end;
  436. {$ifdef GDB}
  437. function Tstoredsym.get_var_value(const s:string):string;
  438. begin
  439. if s='mangledname' then
  440. get_var_value:=mangledname
  441. else
  442. get_var_value:=inherited get_var_value(s);
  443. end;
  444. function Tstoredsym.stabstr_evaluate(const s:string;vars:array of string):Pchar;
  445. begin
  446. stabstr_evaluate:=string_evaluate(s,@get_var_value,vars);
  447. end;
  448. procedure tstoredsym.concatstabto(asmlist : taasmoutput);
  449. var
  450. stabstr : Pchar;
  451. begin
  452. stabstr:=stabstring;
  453. if stabstr<>nil then
  454. asmlist.concat(Tai_stabs.create(stabstr));
  455. end;
  456. {$endif GDB}
  457. function tstoredsym.mangledname : string;
  458. begin
  459. internalerror(200204171);
  460. end;
  461. {****************************************************************************
  462. TLABELSYM
  463. ****************************************************************************}
  464. constructor tlabelsym.create(const n : string);
  465. begin
  466. inherited create(n);
  467. typ:=labelsym;
  468. used:=false;
  469. defined:=false;
  470. code:=nil;
  471. end;
  472. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  473. begin
  474. inherited ppuload(ppufile);
  475. typ:=labelsym;
  476. code:=nil;
  477. used:=false;
  478. defined:=true;
  479. end;
  480. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  481. begin
  482. if owner.symtabletype=globalsymtable then
  483. Message(sym_e_ill_label_decl)
  484. else
  485. begin
  486. inherited ppuwrite(ppufile);
  487. ppufile.writeentry(iblabelsym);
  488. end;
  489. end;
  490. {$ifdef GDB}
  491. function Tlabelsym.stabstring : pchar;
  492. begin
  493. stabstring:=stabstr_evaluate('"${name}",${N_LSYM},0,${line},0',[]);
  494. end;
  495. {$endif GDB}
  496. {****************************************************************************
  497. TUNITSYM
  498. ****************************************************************************}
  499. constructor tunitsym.create(const n : string;ref : tsymtable);
  500. var
  501. old_make_ref : boolean;
  502. begin
  503. old_make_ref:=make_ref;
  504. make_ref:=false;
  505. inherited create(n);
  506. make_ref:=old_make_ref;
  507. typ:=unitsym;
  508. unitsymtable:=ref;
  509. end;
  510. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  511. begin
  512. inherited ppuload(ppufile);
  513. typ:=unitsym;
  514. unitsymtable:=nil;
  515. end;
  516. destructor tunitsym.destroy;
  517. begin
  518. inherited destroy;
  519. end;
  520. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  521. begin
  522. inherited ppuwrite(ppufile);
  523. ppufile.writeentry(ibunitsym);
  524. end;
  525. {****************************************************************************
  526. TPROCSYM
  527. ****************************************************************************}
  528. constructor tprocsym.create(const n : string);
  529. begin
  530. inherited create(n);
  531. typ:=procsym;
  532. pdlistfirst:=nil;
  533. pdlistlast:=nil;
  534. owner:=nil;
  535. {$ifdef GDB}
  536. is_global:=false;
  537. {$endif GDB}
  538. { the tprocdef have their own symoptions, make the procsym
  539. always visible }
  540. symoptions:=[sp_public];
  541. overloadchecked:=false;
  542. procdef_count:=0;
  543. end;
  544. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  545. var
  546. pdderef : tderef;
  547. i,n : longint;
  548. begin
  549. inherited ppuload(ppufile);
  550. typ:=procsym;
  551. pdlistfirst:=nil;
  552. pdlistlast:=nil;
  553. procdef_count:=0;
  554. n:=ppufile.getword;
  555. for i:=1to n do
  556. begin
  557. ppufile.getderef(pdderef);
  558. addprocdef_deref(pdderef);
  559. end;
  560. {$ifdef GDB}
  561. is_global:=false;
  562. {$endif GDB}
  563. overloadchecked:=false;
  564. end;
  565. destructor tprocsym.destroy;
  566. var
  567. hp,p : pprocdeflist;
  568. begin
  569. p:=pdlistfirst;
  570. while assigned(p) do
  571. begin
  572. hp:=p^.next;
  573. dispose(p);
  574. p:=hp;
  575. end;
  576. inherited destroy;
  577. end;
  578. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  579. var
  580. p : pprocdeflist;
  581. n : word;
  582. begin
  583. inherited ppuwrite(ppufile);
  584. { count procdefs }
  585. n:=0;
  586. p:=pdlistfirst;
  587. while assigned(p) do
  588. begin
  589. { only write the proc definitions that belong
  590. to this procsym and are in the global symtable }
  591. if p^.own and
  592. (p^.def.owner.symtabletype in [globalsymtable,objectsymtable]) then
  593. inc(n);
  594. p:=p^.next;
  595. end;
  596. ppufile.putword(n);
  597. { write procdefs }
  598. p:=pdlistfirst;
  599. while assigned(p) do
  600. begin
  601. { only write the proc definitions that belong
  602. to this procsym and are in the global symtable }
  603. if p^.own and
  604. (p^.def.owner.symtabletype in [globalsymtable,objectsymtable]) then
  605. ppufile.putderef(p^.defderef);
  606. p:=p^.next;
  607. end;
  608. ppufile.writeentry(ibprocsym);
  609. end;
  610. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  611. var
  612. p : pprocdeflist;
  613. begin
  614. p:=pdlistfirst;
  615. while assigned(p) do
  616. begin
  617. if p^.def<>skipdef then
  618. MessagePos1(p^.def.fileinfo,sym_h_param_list,p^.def.fullprocname(false));
  619. p:=p^.next;
  620. end;
  621. end;
  622. {Makes implicit externals (procedures declared in the interface
  623. section which do not have a counterpart in the implementation)
  624. to be an imported procedure. For mode macpas.}
  625. procedure import_implict_external(pd:tabstractprocdef);
  626. begin
  627. tprocdef(pd).forwarddef:=false;
  628. tprocdef(pd).setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  629. end;
  630. procedure tprocsym.check_forward;
  631. var
  632. p : pprocdeflist;
  633. begin
  634. p:=pdlistfirst;
  635. while assigned(p) do
  636. begin
  637. if p^.own and (p^.def.forwarddef) then
  638. begin
  639. if (m_mac in aktmodeswitches) and (p^.def.interfacedef) then
  640. import_implict_external(p^.def)
  641. else
  642. begin
  643. MessagePos1(p^.def.fileinfo,sym_e_forward_not_resolved,p^.def.fullprocname(false));
  644. { Turn further error messages off }
  645. p^.def.forwarddef:=false;
  646. end
  647. end;
  648. p:=p^.next;
  649. end;
  650. end;
  651. procedure tprocsym.buildderef;
  652. var
  653. p : pprocdeflist;
  654. begin
  655. p:=pdlistfirst;
  656. while assigned(p) do
  657. begin
  658. if p^.own then
  659. p^.defderef.build(p^.def);
  660. p:=p^.next;
  661. end;
  662. end;
  663. procedure tprocsym.deref;
  664. var
  665. p : pprocdeflist;
  666. begin
  667. { We have removed the overloaded entries, because they
  668. are not valid anymore and we can't deref them because
  669. the unit were they come from is not necessary in
  670. our uses clause (PFV) }
  671. unchain_overload;
  672. { Deref our own procdefs }
  673. p:=pdlistfirst;
  674. while assigned(p) do
  675. begin
  676. if not p^.own then
  677. internalerror(200310291);
  678. p^.def:=tprocdef(p^.defderef.resolve);
  679. p:=p^.next;
  680. end;
  681. end;
  682. procedure tprocsym.addprocdef(p:tprocdef);
  683. var
  684. pd : pprocdeflist;
  685. begin
  686. new(pd);
  687. pd^.def:=p;
  688. pd^.defderef.reset;
  689. pd^.next:=nil;
  690. pd^.own:=(pd^.def.procsym=self);
  691. { Add at end of list to keep always
  692. a correct order, also after loading from ppu }
  693. if assigned(pdlistlast) then
  694. begin
  695. pdlistlast^.next:=pd;
  696. pdlistlast:=pd;
  697. end
  698. else
  699. begin
  700. pdlistfirst:=pd;
  701. pdlistlast:=pd;
  702. end;
  703. inc(procdef_count);
  704. end;
  705. procedure tprocsym.addprocdef_deref(const d:tderef);
  706. var
  707. pd : pprocdeflist;
  708. begin
  709. new(pd);
  710. pd^.def:=nil;
  711. pd^.defderef:=d;
  712. pd^.next:=nil;
  713. pd^.own:=true;
  714. { Add at end of list to keep always
  715. a correct order, also after loading from ppu }
  716. if assigned(pdlistlast) then
  717. begin
  718. pdlistlast^.next:=pd;
  719. pdlistlast:=pd;
  720. end
  721. else
  722. begin
  723. pdlistfirst:=pd;
  724. pdlistlast:=pd;
  725. end;
  726. inc(procdef_count);
  727. end;
  728. function Tprocsym.getprocdef(nr:cardinal):Tprocdef;
  729. var
  730. i : cardinal;
  731. pd : pprocdeflist;
  732. begin
  733. pd:=pdlistfirst;
  734. for i:=2 to nr do
  735. begin
  736. if not assigned(pd) then
  737. internalerror(200209051);
  738. pd:=pd^.next;
  739. end;
  740. getprocdef:=pd^.def;
  741. end;
  742. procedure Tprocsym.add_para_match_to(Aprocsym:Tprocsym;cpoptions:tcompare_paras_options);
  743. var
  744. pd:pprocdeflist;
  745. begin
  746. pd:=pdlistfirst;
  747. while assigned(pd) do
  748. begin
  749. if Aprocsym.search_procdef_bypara(pd^.def.paras,nil,cpoptions)=nil then
  750. Aprocsym.addprocdef(pd^.def);
  751. pd:=pd^.next;
  752. end;
  753. end;
  754. procedure Tprocsym.concat_procdefs_to(s:Tprocsym);
  755. var
  756. pd : pprocdeflist;
  757. begin
  758. pd:=pdlistfirst;
  759. while assigned(pd) do
  760. begin
  761. s.addprocdef(pd^.def);
  762. pd:=pd^.next;
  763. end;
  764. end;
  765. function Tprocsym.first_procdef:Tprocdef;
  766. begin
  767. if assigned(pdlistfirst) then
  768. first_procdef:=pdlistfirst^.def
  769. else
  770. first_procdef:=nil;
  771. end;
  772. function Tprocsym.last_procdef:Tprocdef;
  773. begin
  774. if assigned(pdlistlast) then
  775. last_procdef:=pdlistlast^.def
  776. else
  777. last_procdef:=nil;
  778. end;
  779. procedure Tprocsym.foreach_procdef_static(proc2call:Tprocdefcallback;arg:pointer);
  780. var
  781. p : pprocdeflist;
  782. begin
  783. p:=pdlistfirst;
  784. while assigned(p) do
  785. begin
  786. proc2call(p^.def,arg);
  787. p:=p^.next;
  788. end;
  789. end;
  790. function Tprocsym.search_procdef_nopara_boolret:Tprocdef;
  791. var
  792. p : pprocdeflist;
  793. begin
  794. search_procdef_nopara_boolret:=nil;
  795. p:=pdlistfirst;
  796. while p<>nil do
  797. begin
  798. if (p^.def.maxparacount=0) and
  799. is_boolean(p^.def.rettype.def) then
  800. begin
  801. search_procdef_nopara_boolret:=p^.def;
  802. break;
  803. end;
  804. p:=p^.next;
  805. end;
  806. end;
  807. function Tprocsym.search_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  808. var
  809. p : pprocdeflist;
  810. begin
  811. search_procdef_bytype:=nil;
  812. p:=pdlistfirst;
  813. while p<>nil do
  814. begin
  815. if p^.def.proctypeoption=pt then
  816. begin
  817. search_procdef_bytype:=p^.def;
  818. break;
  819. end;
  820. p:=p^.next;
  821. end;
  822. end;
  823. function Tprocsym.search_procdef_bypara(para:tlist;retdef:tdef;
  824. cpoptions:tcompare_paras_options):Tprocdef;
  825. var
  826. pd : pprocdeflist;
  827. eq : tequaltype;
  828. begin
  829. search_procdef_bypara:=nil;
  830. pd:=pdlistfirst;
  831. while assigned(pd) do
  832. begin
  833. if assigned(retdef) then
  834. eq:=compare_defs(retdef,pd^.def.rettype.def,nothingn)
  835. else
  836. eq:=te_equal;
  837. if (eq>=te_equal) or
  838. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  839. begin
  840. eq:=compare_paras(para,pd^.def.paras,cp_value_equal_const,cpoptions);
  841. if (eq>=te_equal) or
  842. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  843. begin
  844. search_procdef_bypara:=pd^.def;
  845. break;
  846. end;
  847. end;
  848. pd:=pd^.next;
  849. end;
  850. end;
  851. function Tprocsym.search_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  852. var
  853. pd : pprocdeflist;
  854. eq,besteq : tequaltype;
  855. bestpd : tprocdef;
  856. begin
  857. { This function will return the pprocdef of pprocsym that
  858. is the best match for procvardef. When there are multiple
  859. matches it returns nil.}
  860. search_procdef_byprocvardef:=nil;
  861. bestpd:=nil;
  862. besteq:=te_incompatible;
  863. pd:=pdlistfirst;
  864. while assigned(pd) do
  865. begin
  866. eq:=proc_to_procvar_equal(pd^.def,d,false);
  867. if eq>=te_equal then
  868. begin
  869. { multiple procvars with the same equal level }
  870. if assigned(bestpd) and
  871. (besteq=eq) then
  872. exit;
  873. if eq>besteq then
  874. begin
  875. besteq:=eq;
  876. bestpd:=pd^.def;
  877. end;
  878. end;
  879. pd:=pd^.next;
  880. end;
  881. search_procdef_byprocvardef:=bestpd;
  882. end;
  883. function Tprocsym.search_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  884. var
  885. convtyp : tconverttype;
  886. pd : pprocdeflist;
  887. bestpd : tprocdef;
  888. eq : tequaltype;
  889. hpd : tprocdef;
  890. i : byte;
  891. begin
  892. result:=nil;
  893. bestpd:=nil;
  894. besteq:=te_incompatible;
  895. pd:=pdlistfirst;
  896. while assigned(pd) do
  897. begin
  898. if equal_defs(todef,pd^.def.rettype.def) and
  899. { the result type must be always really equal and not an alias,
  900. if you mess with this code, check tw4093 }
  901. ((todef=pd^.def.rettype.def) or
  902. (
  903. not(df_unique in todef.defoptions) and
  904. not(df_unique in pd^.def.rettype.def.defoptions)
  905. )
  906. ) then
  907. begin
  908. i:=0;
  909. { ignore vs_hidden parameters }
  910. while (i<pd^.def.paras.count) and
  911. assigned(pd^.def.paras[i]) and
  912. (vo_is_hidden_para in tparavarsym(pd^.def.paras[i]).varoptions) do
  913. inc(i);
  914. if (i<pd^.def.paras.count) and
  915. assigned(pd^.def.paras[i]) then
  916. begin
  917. eq:=compare_defs_ext(fromdef,tparavarsym(pd^.def.paras[i]).vartype.def,nothingn,convtyp,hpd,[]);
  918. { alias? if yes, only l1 choice,
  919. if you mess with this code, check tw4093 }
  920. if (eq=te_exact) and
  921. (fromdef<>tparavarsym(pd^.def.paras[i]).vartype.def) and
  922. ((df_unique in fromdef.defoptions) or
  923. (df_unique in tparavarsym(pd^.def.paras[i]).vartype.def.defoptions)) then
  924. eq:=te_convert_l1;
  925. if eq=te_exact then
  926. begin
  927. besteq:=eq;
  928. result:=pd^.def;
  929. exit;
  930. end;
  931. if eq>besteq then
  932. begin
  933. bestpd:=pd^.def;
  934. besteq:=eq;
  935. end;
  936. end;
  937. end;
  938. pd:=pd^.next;
  939. end;
  940. result:=bestpd;
  941. end;
  942. function tprocsym.write_references(ppufile:tcompilerppufile;locals:boolean) : boolean;
  943. var
  944. p : pprocdeflist;
  945. begin
  946. write_references:=false;
  947. if not inherited write_references(ppufile,locals) then
  948. exit;
  949. write_references:=true;
  950. p:=pdlistfirst;
  951. while assigned(p) do
  952. begin
  953. if p^.own then
  954. p^.def.write_references(ppufile,locals);
  955. p:=p^.next;
  956. end;
  957. end;
  958. procedure tprocsym.unchain_overload;
  959. var
  960. p,hp : pprocdeflist;
  961. begin
  962. { remove all overloaded procdefs from the
  963. procdeflist that are not in the current symtable }
  964. overloadchecked:=false;
  965. p:=pdlistfirst;
  966. { reset new lists }
  967. pdlistfirst:=nil;
  968. pdlistlast:=nil;
  969. while assigned(p) do
  970. begin
  971. hp:=p^.next;
  972. if p^.own then
  973. begin
  974. { keep, add to list }
  975. if assigned(pdlistlast) then
  976. begin
  977. pdlistlast^.next:=p;
  978. pdlistlast:=p;
  979. end
  980. else
  981. begin
  982. pdlistfirst:=p;
  983. pdlistlast:=p;
  984. end;
  985. p^.next:=nil;
  986. end
  987. else
  988. begin
  989. { remove }
  990. dispose(p);
  991. dec(procdef_count);
  992. end;
  993. p:=hp;
  994. end;
  995. end;
  996. function tprocsym.is_visible_for_object(currobjdef:tdef;context:tdef):boolean;
  997. var
  998. p : pprocdeflist;
  999. begin
  1000. { This procsym is visible, when there is at least
  1001. one of the procdefs visible }
  1002. result:=false;
  1003. p:=pdlistfirst;
  1004. while assigned(p) do
  1005. begin
  1006. if p^.own and
  1007. p^.def.is_visible_for_object(tobjectdef(currobjdef)) then
  1008. begin
  1009. result:=true;
  1010. exit;
  1011. end;
  1012. p:=p^.next;
  1013. end;
  1014. end;
  1015. {$ifdef GDB}
  1016. function tprocsym.stabstring : pchar;
  1017. begin
  1018. internalerror(200111171);
  1019. result:=nil;
  1020. end;
  1021. {$endif GDB}
  1022. {****************************************************************************
  1023. TERRORSYM
  1024. ****************************************************************************}
  1025. constructor terrorsym.create;
  1026. begin
  1027. inherited create('');
  1028. typ:=errorsym;
  1029. end;
  1030. {****************************************************************************
  1031. TPROPERTYSYM
  1032. ****************************************************************************}
  1033. constructor tpropertysym.create(const n : string);
  1034. begin
  1035. inherited create(n);
  1036. typ:=propertysym;
  1037. propoptions:=[];
  1038. index:=0;
  1039. default:=0;
  1040. proptype.reset;
  1041. indextype.reset;
  1042. readaccess:=tsymlist.create;
  1043. writeaccess:=tsymlist.create;
  1044. storedaccess:=tsymlist.create;
  1045. end;
  1046. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  1047. begin
  1048. inherited ppuload(ppufile);
  1049. typ:=propertysym;
  1050. ppufile.getsmallset(propoptions);
  1051. if (ppo_is_override in propoptions) then
  1052. begin
  1053. ppufile.getderef(propoverridenderef);
  1054. { we need to have these objects initialized }
  1055. readaccess:=tsymlist.create;
  1056. writeaccess:=tsymlist.create;
  1057. storedaccess:=tsymlist.create;
  1058. end
  1059. else
  1060. begin
  1061. ppufile.gettype(proptype);
  1062. index:=ppufile.getlongint;
  1063. default:=ppufile.getlongint;
  1064. ppufile.gettype(indextype);
  1065. readaccess:=ppufile.getsymlist;
  1066. writeaccess:=ppufile.getsymlist;
  1067. storedaccess:=ppufile.getsymlist;
  1068. end;
  1069. end;
  1070. destructor tpropertysym.destroy;
  1071. begin
  1072. readaccess.free;
  1073. writeaccess.free;
  1074. storedaccess.free;
  1075. inherited destroy;
  1076. end;
  1077. function tpropertysym.gettypedef:tdef;
  1078. begin
  1079. gettypedef:=proptype.def;
  1080. end;
  1081. procedure tpropertysym.buildderef;
  1082. begin
  1083. if (ppo_is_override in propoptions) then
  1084. begin
  1085. propoverridenderef.build(propoverriden);
  1086. end
  1087. else
  1088. begin
  1089. proptype.buildderef;
  1090. indextype.buildderef;
  1091. readaccess.buildderef;
  1092. writeaccess.buildderef;
  1093. storedaccess.buildderef;
  1094. end;
  1095. end;
  1096. procedure tpropertysym.deref;
  1097. begin
  1098. if (ppo_is_override in propoptions) then
  1099. begin
  1100. propoverriden:=tpropertysym(propoverridenderef.resolve);
  1101. dooverride(propoverriden);
  1102. end
  1103. else
  1104. begin
  1105. proptype.resolve;
  1106. indextype.resolve;
  1107. readaccess.resolve;
  1108. writeaccess.resolve;
  1109. storedaccess.resolve;
  1110. end;
  1111. end;
  1112. function tpropertysym.getsize : longint;
  1113. begin
  1114. getsize:=0;
  1115. end;
  1116. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  1117. begin
  1118. inherited ppuwrite(ppufile);
  1119. ppufile.putsmallset(propoptions);
  1120. if (ppo_is_override in propoptions) then
  1121. ppufile.putderef(propoverridenderef)
  1122. else
  1123. begin
  1124. ppufile.puttype(proptype);
  1125. ppufile.putlongint(index);
  1126. ppufile.putlongint(default);
  1127. ppufile.puttype(indextype);
  1128. ppufile.putsymlist(readaccess);
  1129. ppufile.putsymlist(writeaccess);
  1130. ppufile.putsymlist(storedaccess);
  1131. end;
  1132. ppufile.writeentry(ibpropertysym);
  1133. end;
  1134. procedure tpropertysym.dooverride(overriden:tpropertysym);
  1135. begin
  1136. propoverriden:=overriden;
  1137. proptype:=overriden.proptype;
  1138. propoptions:=overriden.propoptions+[ppo_is_override];
  1139. index:=overriden.index;
  1140. default:=overriden.default;
  1141. indextype:=overriden.indextype;
  1142. readaccess.free;
  1143. readaccess:=overriden.readaccess.getcopy;
  1144. writeaccess.free;
  1145. writeaccess:=overriden.writeaccess.getcopy;
  1146. storedaccess.free;
  1147. storedaccess:=overriden.storedaccess.getcopy;
  1148. end;
  1149. {****************************************************************************
  1150. TABSTRACTVARSYM
  1151. ****************************************************************************}
  1152. constructor tabstractvarsym.create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1153. begin
  1154. inherited create(n);
  1155. vartype:=tt;
  1156. varspez:=vsp;
  1157. varstate:=vs_declared;
  1158. varoptions:=vopts;
  1159. end;
  1160. constructor tabstractvarsym.ppuload(ppufile:tcompilerppufile);
  1161. begin
  1162. inherited ppuload(ppufile);
  1163. varstate:=vs_used;
  1164. varspez:=tvarspez(ppufile.getbyte);
  1165. varregable:=tvarregable(ppufile.getbyte);
  1166. ppufile.gettype(_vartype);
  1167. ppufile.getsmallset(varoptions);
  1168. end;
  1169. destructor tabstractvarsym.destroy;
  1170. begin
  1171. if assigned(notifications) then
  1172. notifications.destroy;
  1173. inherited destroy;
  1174. end;
  1175. procedure tabstractvarsym.buildderef;
  1176. begin
  1177. vartype.buildderef;
  1178. end;
  1179. procedure tabstractvarsym.deref;
  1180. begin
  1181. vartype.resolve;
  1182. end;
  1183. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  1184. var
  1185. oldintfcrc : boolean;
  1186. begin
  1187. inherited ppuwrite(ppufile);
  1188. ppufile.putbyte(byte(varspez));
  1189. oldintfcrc:=ppufile.do_crc;
  1190. ppufile.do_crc:=false;
  1191. ppufile.putbyte(byte(varregable));
  1192. ppufile.do_crc:=oldintfcrc;
  1193. ppufile.puttype(vartype);
  1194. ppufile.putsmallset(varoptions);
  1195. end;
  1196. function tabstractvarsym.getsize : longint;
  1197. begin
  1198. if assigned(vartype.def) and
  1199. ((vartype.def.deftype<>arraydef) or
  1200. tarraydef(vartype.def).isDynamicArray or
  1201. (tarraydef(vartype.def).highrange>=tarraydef(vartype.def).lowrange)) then
  1202. result:=vartype.def.size
  1203. else
  1204. result:=0;
  1205. end;
  1206. function tabstractvarsym.is_regvar:boolean;
  1207. begin
  1208. { Register variables are not allowed in the following cases:
  1209. - regvars are disabled
  1210. - exceptions are used (after an exception is raised the contents of the
  1211. registers is not valid anymore)
  1212. - it has a local copy
  1213. - the value needs to be in memory (i.e. reference counted) }
  1214. result:=(cs_regvars in aktglobalswitches) and
  1215. not(pi_has_assembler_block in current_procinfo.flags) and
  1216. not(pi_uses_exceptions in current_procinfo.flags) and
  1217. not(vo_has_local_copy in varoptions) and
  1218. (varregable<>vr_none);
  1219. end;
  1220. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  1221. var n:Tnotification;
  1222. begin
  1223. if assigned(notifications) then
  1224. begin
  1225. n:=Tnotification(notifications.first);
  1226. while assigned(n) do
  1227. begin
  1228. if what in n.flags then
  1229. n.callback(what,self);
  1230. n:=Tnotification(n.next);
  1231. end;
  1232. end;
  1233. end;
  1234. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  1235. Tnotification_callback):cardinal;
  1236. var n:Tnotification;
  1237. begin
  1238. if not assigned(notifications) then
  1239. notifications:=Tlinkedlist.create;
  1240. n:=Tnotification.create(flags,callback);
  1241. register_notification:=n.id;
  1242. notifications.concat(n);
  1243. end;
  1244. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  1245. var n:Tnotification;
  1246. begin
  1247. if not assigned(notifications) then
  1248. internalerror(200212311)
  1249. else
  1250. begin
  1251. n:=Tnotification(notifications.first);
  1252. while assigned(n) do
  1253. begin
  1254. if n.id=id then
  1255. begin
  1256. notifications.remove(n);
  1257. n.destroy;
  1258. exit;
  1259. end;
  1260. n:=Tnotification(n.next);
  1261. end;
  1262. internalerror(200212311)
  1263. end;
  1264. end;
  1265. procedure tabstractvarsym.setvartype(const newtype: ttype);
  1266. begin
  1267. _vartype := newtype;
  1268. { can we load the value into a register ? }
  1269. if not assigned(owner) or
  1270. (owner.symtabletype in [localsymtable,parasymtable]) or
  1271. (
  1272. (owner.symtabletype=staticsymtable) and
  1273. not(cs_create_pic in aktmoduleswitches)
  1274. ) then
  1275. begin
  1276. if tstoreddef(vartype.def).is_intregable then
  1277. varregable:=vr_intreg
  1278. else
  1279. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  1280. if {(
  1281. not assigned(owner) or
  1282. (owner.symtabletype<>staticsymtable)
  1283. ) and }
  1284. tstoreddef(vartype.def).is_fpuregable then
  1285. begin
  1286. {$ifdef x86}
  1287. if use_sse(vartype.def) then
  1288. varregable:=vr_mmreg
  1289. else
  1290. {$else x86}
  1291. varregable:=vr_fpureg;
  1292. {$endif x86}
  1293. end;
  1294. end;
  1295. end;
  1296. {****************************************************************************
  1297. TFIELDVARSYM
  1298. ****************************************************************************}
  1299. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1300. begin
  1301. inherited create(n,vsp,tt,vopts);
  1302. typ:=fieldvarsym;
  1303. fieldoffset:=0;
  1304. end;
  1305. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1306. begin
  1307. inherited ppuload(ppufile);
  1308. typ:=fieldvarsym;
  1309. fieldoffset:=ppufile.getaint;
  1310. end;
  1311. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1312. begin
  1313. inherited ppuwrite(ppufile);
  1314. ppufile.putaint(fieldoffset);
  1315. ppufile.writeentry(ibfieldvarsym);
  1316. end;
  1317. {$ifdef GDB}
  1318. function tfieldvarsym.stabstring:Pchar;
  1319. var
  1320. st : string;
  1321. begin
  1322. stabstring:=nil;
  1323. case owner.symtabletype of
  1324. objectsymtable :
  1325. begin
  1326. if (sp_static in symoptions) then
  1327. begin
  1328. st:=tstoreddef(vartype.def).numberstring;
  1329. if (cs_gdb_gsym in aktglobalswitches) then
  1330. st:='G'+st
  1331. else
  1332. st:='S'+st;
  1333. stabstring:=stabstr_evaluate('"${ownername}__${name}:$1",${N_LCSYM},0,${line},${mangledname}',[st]);
  1334. end;
  1335. end;
  1336. end;
  1337. end;
  1338. {$endif GDB}
  1339. {****************************************************************************
  1340. TABSTRACTNORMALVARSYM
  1341. ****************************************************************************}
  1342. constructor tabstractnormalvarsym.create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1343. begin
  1344. inherited create(n,vsp,tt,vopts);
  1345. fillchar(localloc,sizeof(localloc),0);
  1346. defaultconstsym:=nil;
  1347. end;
  1348. constructor tabstractnormalvarsym.ppuload(ppufile:tcompilerppufile);
  1349. begin
  1350. inherited ppuload(ppufile);
  1351. fillchar(localloc,sizeof(localloc),0);
  1352. ppufile.getderef(defaultconstsymderef);
  1353. end;
  1354. procedure tabstractnormalvarsym.buildderef;
  1355. begin
  1356. inherited buildderef;
  1357. defaultconstsymderef.build(defaultconstsym);
  1358. end;
  1359. procedure tabstractnormalvarsym.deref;
  1360. begin
  1361. inherited deref;
  1362. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1363. end;
  1364. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1365. begin
  1366. inherited ppuwrite(ppufile);
  1367. ppufile.putderef(defaultconstsymderef);
  1368. end;
  1369. {****************************************************************************
  1370. TGLOBALVARSYM
  1371. ****************************************************************************}
  1372. constructor tglobalvarsym.create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1373. begin
  1374. inherited create(n,vsp,tt,vopts);
  1375. typ:=globalvarsym;
  1376. _mangledname:=nil;
  1377. end;
  1378. constructor tglobalvarsym.create_dll(const n : string;vsp:tvarspez;const tt : ttype);
  1379. begin
  1380. tglobalvarsym(self).create(n,vsp,tt,[vo_is_dll_var]);
  1381. end;
  1382. constructor tglobalvarsym.create_C(const n,mangled : string;vsp:tvarspez;const tt : ttype);
  1383. begin
  1384. tglobalvarsym(self).create(n,vsp,tt,[]);
  1385. set_mangledname(mangled);
  1386. end;
  1387. constructor tglobalvarsym.ppuload(ppufile:tcompilerppufile);
  1388. begin
  1389. inherited ppuload(ppufile);
  1390. typ:=globalvarsym;
  1391. if vo_has_mangledname in varoptions then
  1392. _mangledname:=stringdup(ppufile.getstring)
  1393. else
  1394. _mangledname:=nil;
  1395. end;
  1396. destructor tglobalvarsym.destroy;
  1397. begin
  1398. if assigned(_mangledname) then
  1399. begin
  1400. {$ifdef MEMDEBUG}
  1401. memmanglednames.start;
  1402. {$endif MEMDEBUG}
  1403. stringdispose(_mangledname);
  1404. {$ifdef MEMDEBUG}
  1405. memmanglednames.stop;
  1406. {$endif MEMDEBUG}
  1407. end;
  1408. inherited destroy;
  1409. end;
  1410. procedure tglobalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1411. begin
  1412. inherited ppuwrite(ppufile);
  1413. if vo_has_mangledname in varoptions then
  1414. ppufile.putstring(_mangledname^);
  1415. ppufile.writeentry(ibglobalvarsym);
  1416. end;
  1417. function tglobalvarsym.mangledname:string;
  1418. begin
  1419. if not assigned(_mangledname) then
  1420. begin
  1421. {$ifdef compress}
  1422. _mangledname:=stringdup(minilzw_encode(make_mangledname('U',owner,name)));
  1423. {$else}
  1424. _mangledname:=stringdup(make_mangledname('U',owner,name));
  1425. {$endif}
  1426. end;
  1427. result:=_mangledname^;
  1428. end;
  1429. procedure tglobalvarsym.set_mangledname(const s:string);
  1430. begin
  1431. stringdispose(_mangledname);
  1432. {$ifdef compress}
  1433. _mangledname:=stringdup(minilzw_encode(s));
  1434. {$else}
  1435. _mangledname:=stringdup(s);
  1436. {$endif}
  1437. include(varoptions,vo_has_mangledname);
  1438. end;
  1439. {$ifdef GDB}
  1440. function Tglobalvarsym.stabstring:Pchar;
  1441. var st:string;
  1442. threadvaroffset:string;
  1443. regidx:Tregisterindex;
  1444. begin
  1445. result:=nil;
  1446. st:=tstoreddef(vartype.def).numberstring;
  1447. case localloc.loc of
  1448. LOC_REGISTER,
  1449. LOC_CREGISTER,
  1450. LOC_MMREGISTER,
  1451. LOC_CMMREGISTER,
  1452. LOC_FPUREGISTER,
  1453. LOC_CFPUREGISTER :
  1454. begin
  1455. regidx:=findreg_by_number(localloc.register);
  1456. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1457. { this is the register order for GDB}
  1458. if regidx<>0 then
  1459. stabstring:=stabstr_evaluate('"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1460. end;
  1461. else
  1462. begin
  1463. if (vo_is_thread_var in varoptions) then
  1464. threadvaroffset:='+'+tostr(sizeof(aint))
  1465. else
  1466. threadvaroffset:='';
  1467. { Here we used S instead of
  1468. because with G GDB doesn't look at the address field
  1469. but searches the same name or with a leading underscore
  1470. but these names don't exist in pascal !}
  1471. if (cs_gdb_gsym in aktglobalswitches) then
  1472. st:='G'+st
  1473. else
  1474. st:='S'+st;
  1475. stabstring:=stabstr_evaluate('"${name}:$1",${N_LCSYM},0,${line},${mangledname}$2',[st,threadvaroffset]);
  1476. end;
  1477. end;
  1478. end;
  1479. {$endif GDB}
  1480. {****************************************************************************
  1481. TLOCALVARSYM
  1482. ****************************************************************************}
  1483. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1484. begin
  1485. inherited create(n,vsp,tt,vopts);
  1486. typ:=localvarsym;
  1487. end;
  1488. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1489. begin
  1490. inherited ppuload(ppufile);
  1491. typ:=localvarsym;
  1492. end;
  1493. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1494. begin
  1495. inherited ppuwrite(ppufile);
  1496. ppufile.writeentry(iblocalvarsym);
  1497. end;
  1498. {$ifdef GDB}
  1499. function tlocalvarsym.stabstring:Pchar;
  1500. var st:string;
  1501. regidx:Tregisterindex;
  1502. begin
  1503. stabstring:=nil;
  1504. { There is no space allocated for not referenced locals }
  1505. if (owner.symtabletype=localsymtable) and (refs=0) then
  1506. exit;
  1507. st:=tstoreddef(vartype.def).numberstring;
  1508. case localloc.loc of
  1509. LOC_REGISTER,
  1510. LOC_CREGISTER,
  1511. LOC_MMREGISTER,
  1512. LOC_CMMREGISTER,
  1513. LOC_FPUREGISTER,
  1514. LOC_CFPUREGISTER :
  1515. begin
  1516. regidx:=findreg_by_number(localloc.register);
  1517. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1518. { this is the register order for GDB}
  1519. if regidx<>0 then
  1520. stabstring:=stabstr_evaluate('"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(regstabs_table[regidx])]);
  1521. end;
  1522. LOC_REFERENCE :
  1523. { offset to ebp => will not work if the framepointer is esp
  1524. so some optimizing will make things harder to debug }
  1525. stabstring:=stabstr_evaluate('"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(localloc.reference.offset)])
  1526. else
  1527. internalerror(2003091814);
  1528. end;
  1529. end;
  1530. {$endif GDB}
  1531. {****************************************************************************
  1532. TPARAVARSYM
  1533. ****************************************************************************}
  1534. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;const tt : ttype;vopts:tvaroptions);
  1535. begin
  1536. inherited create(n,vsp,tt,vopts);
  1537. typ:=paravarsym;
  1538. paranr:=nr;
  1539. paraloc[calleeside].init;
  1540. paraloc[callerside].init;
  1541. end;
  1542. destructor tparavarsym.destroy;
  1543. begin
  1544. paraloc[calleeside].done;
  1545. paraloc[callerside].done;
  1546. inherited destroy;
  1547. end;
  1548. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1549. var
  1550. b : byte;
  1551. begin
  1552. inherited ppuload(ppufile);
  1553. paranr:=ppufile.getword;
  1554. paraloc[calleeside].init;
  1555. paraloc[callerside].init;
  1556. if vo_has_explicit_paraloc in varoptions then
  1557. begin
  1558. b:=ppufile.getbyte;
  1559. if b<>sizeof(paraloc[callerside].location^) then
  1560. internalerror(200411154);
  1561. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1562. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1563. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1564. end;
  1565. typ:=paravarsym;
  1566. end;
  1567. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1568. begin
  1569. inherited ppuwrite(ppufile);
  1570. ppufile.putword(paranr);
  1571. if vo_has_explicit_paraloc in varoptions then
  1572. begin
  1573. paraloc[callerside].check_simple_location;
  1574. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1575. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1576. end;
  1577. ppufile.writeentry(ibparavarsym);
  1578. end;
  1579. {$ifdef GDB}
  1580. function tparavarsym.stabstring:Pchar;
  1581. var st:string;
  1582. regidx:Tregisterindex;
  1583. c:char;
  1584. begin
  1585. result:=nil;
  1586. { set loc to LOC_REFERENCE to get somewhat usable debugging info for -Or }
  1587. { while stabs aren't adapted for regvars yet }
  1588. if (vo_is_self in varoptions) then
  1589. begin
  1590. case localloc.loc of
  1591. LOC_REGISTER,
  1592. LOC_CREGISTER:
  1593. regidx:=findreg_by_number(localloc.register);
  1594. LOC_REFERENCE: ;
  1595. else
  1596. internalerror(2003091815);
  1597. end;
  1598. if (po_classmethod in current_procinfo.procdef.procoptions) or
  1599. (po_staticmethod in current_procinfo.procdef.procoptions) then
  1600. begin
  1601. if (localloc.loc=LOC_REFERENCE) then
  1602. stabstring:=stabstr_evaluate('"pvmt:p$1",${N_TSYM},0,0,$2',
  1603. [Tstoreddef(pvmttype.def).numberstring,tostr(localloc.reference.offset)]);
  1604. (* else
  1605. stabstring:=stabstr_evaluate('"pvmt:r$1",${N_RSYM},0,0,$2',
  1606. [Tstoreddef(pvmttype.def).numberstring,tostr(regstabs_table[regidx])]) *)
  1607. end
  1608. else
  1609. begin
  1610. if not(is_class(current_procinfo.procdef._class)) then
  1611. c:='v'
  1612. else
  1613. c:='p';
  1614. if (localloc.loc=LOC_REFERENCE) then
  1615. stabstring:=stabstr_evaluate('"$$t:$1",${N_TSYM},0,0,$2',
  1616. [c+current_procinfo.procdef._class.numberstring,tostr(localloc.reference.offset)]);
  1617. (* else
  1618. stabstring:=stabstr_evaluate('"$$t:r$1",${N_RSYM},0,0,$2',
  1619. [c+current_procinfo.procdef._class.numberstring,tostr(regstabs_table[regidx])]); *)
  1620. end;
  1621. end
  1622. else
  1623. begin
  1624. st:=tstoreddef(vartype.def).numberstring;
  1625. if paramanager.push_addr_param(varspez,vartype.def,tprocdef(owner.defowner).proccalloption) and
  1626. not(vo_has_local_copy in varoptions) and
  1627. not is_open_string(vartype.def) then
  1628. st := 'v'+st { should be 'i' but 'i' doesn't work }
  1629. else
  1630. st := 'p'+st;
  1631. case localloc.loc of
  1632. LOC_REGISTER,
  1633. LOC_CREGISTER,
  1634. LOC_MMREGISTER,
  1635. LOC_CMMREGISTER,
  1636. LOC_FPUREGISTER,
  1637. LOC_CFPUREGISTER :
  1638. begin
  1639. regidx:=findreg_by_number(localloc.register);
  1640. { "eax", "ecx", "edx", "ebx", "esp", "ebp", "esi", "edi", "eip", "ps", "cs", "ss", "ds", "es", "fs", "gs", }
  1641. { this is the register order for GDB}
  1642. if regidx<>0 then
  1643. stabstring:=stabstr_evaluate('"${name}:r$1",${N_RSYM},0,${line},$2',[st,tostr(longint(regstabs_table[regidx]))]);
  1644. end;
  1645. LOC_REFERENCE :
  1646. { offset to ebp => will not work if the framepointer is esp
  1647. so some optimizing will make things harder to debug }
  1648. stabstring:=stabstr_evaluate('"${name}:$1",${N_TSYM},0,${line},$2',[st,tostr(localloc.reference.offset)])
  1649. else
  1650. internalerror(2003091814);
  1651. end;
  1652. end;
  1653. end;
  1654. {$endif GDB}
  1655. {****************************************************************************
  1656. TABSOLUTEVARSYM
  1657. ****************************************************************************}
  1658. constructor tabsolutevarsym.create(const n : string;const tt : ttype);
  1659. begin
  1660. inherited create(n,vs_value,tt,[]);
  1661. typ:=absolutevarsym;
  1662. ref:=nil;
  1663. end;
  1664. constructor tabsolutevarsym.create_ref(const n : string;const tt : ttype;_ref:tsymlist);
  1665. begin
  1666. inherited create(n,vs_value,tt,[]);
  1667. typ:=absolutevarsym;
  1668. ref:=_ref;
  1669. end;
  1670. destructor tabsolutevarsym.destroy;
  1671. begin
  1672. if assigned(ref) then
  1673. ref.free;
  1674. inherited destroy;
  1675. end;
  1676. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1677. begin
  1678. inherited ppuload(ppufile);
  1679. typ:=absolutevarsym;
  1680. ref:=nil;
  1681. asmname:=nil;
  1682. abstyp:=absolutetyp(ppufile.getbyte);
  1683. {$ifdef i386}
  1684. absseg:=false;
  1685. {$endif i386}
  1686. case abstyp of
  1687. tovar :
  1688. ref:=ppufile.getsymlist;
  1689. toasm :
  1690. asmname:=stringdup(ppufile.getstring);
  1691. toaddr :
  1692. begin
  1693. addroffset:=ppufile.getaint;
  1694. {$ifdef i386}
  1695. absseg:=boolean(ppufile.getbyte);
  1696. {$endif i386}
  1697. end;
  1698. end;
  1699. end;
  1700. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1701. begin
  1702. inherited ppuwrite(ppufile);
  1703. ppufile.putbyte(byte(abstyp));
  1704. case abstyp of
  1705. tovar :
  1706. ppufile.putsymlist(ref);
  1707. toasm :
  1708. ppufile.putstring(asmname^);
  1709. toaddr :
  1710. begin
  1711. ppufile.putaint(addroffset);
  1712. {$ifdef i386}
  1713. ppufile.putbyte(byte(absseg));
  1714. {$endif i386}
  1715. end;
  1716. end;
  1717. ppufile.writeentry(ibabsolutevarsym);
  1718. end;
  1719. procedure tabsolutevarsym.buildderef;
  1720. begin
  1721. inherited buildderef;
  1722. if (abstyp=tovar) then
  1723. ref.buildderef;
  1724. end;
  1725. procedure tabsolutevarsym.deref;
  1726. begin
  1727. inherited deref;
  1728. { own absolute deref }
  1729. if (abstyp=tovar) then
  1730. ref.resolve;
  1731. end;
  1732. function tabsolutevarsym.mangledname : string;
  1733. begin
  1734. case abstyp of
  1735. toasm :
  1736. mangledname:=asmname^;
  1737. toaddr :
  1738. mangledname:='$'+tostr(addroffset);
  1739. else
  1740. internalerror(200411061);
  1741. end;
  1742. end;
  1743. {$ifdef GDB}
  1744. function tabsolutevarsym.stabstring:Pchar;
  1745. begin
  1746. stabstring:=nil;
  1747. end;
  1748. {$endif GDB}
  1749. {****************************************************************************
  1750. TTYPEDCONSTSYM
  1751. *****************************************************************************}
  1752. constructor ttypedconstsym.create(const n : string;p : tdef;writable : boolean);
  1753. begin
  1754. inherited create(n);
  1755. typ:=typedconstsym;
  1756. typedconsttype.setdef(p);
  1757. is_writable:=writable;
  1758. end;
  1759. constructor ttypedconstsym.createtype(const n : string;const tt : ttype;writable : boolean);
  1760. begin
  1761. inherited create(n);
  1762. typ:=typedconstsym;
  1763. typedconsttype:=tt;
  1764. is_writable:=writable;
  1765. end;
  1766. constructor ttypedconstsym.ppuload(ppufile:tcompilerppufile);
  1767. begin
  1768. inherited ppuload(ppufile);
  1769. typ:=typedconstsym;
  1770. ppufile.gettype(typedconsttype);
  1771. is_writable:=boolean(ppufile.getbyte);
  1772. end;
  1773. destructor ttypedconstsym.destroy;
  1774. begin
  1775. if assigned(_mangledname) then
  1776. begin
  1777. {$ifdef MEMDEBUG}
  1778. memmanglednames.start;
  1779. {$endif MEMDEBUG}
  1780. stringdispose(_mangledname);
  1781. {$ifdef MEMDEBUG}
  1782. memmanglednames.stop;
  1783. {$endif MEMDEBUG}
  1784. end;
  1785. inherited destroy;
  1786. end;
  1787. function ttypedconstsym.mangledname:string;
  1788. begin
  1789. if not assigned(_mangledname) then
  1790. begin
  1791. {$ifdef compress}
  1792. _mangledname:=stringdup(make_mangledname('TC',owner,name));
  1793. {$else}
  1794. _mangledname:=stringdup(make_mangledname('TC',owner,name));
  1795. {$endif}
  1796. end;
  1797. result:=_mangledname^;
  1798. end;
  1799. function ttypedconstsym.getsize : longint;
  1800. begin
  1801. if assigned(typedconsttype.def) then
  1802. getsize:=typedconsttype.def.size
  1803. else
  1804. getsize:=0;
  1805. end;
  1806. procedure ttypedconstsym.buildderef;
  1807. begin
  1808. typedconsttype.buildderef;
  1809. end;
  1810. procedure ttypedconstsym.deref;
  1811. begin
  1812. typedconsttype.resolve;
  1813. end;
  1814. procedure ttypedconstsym.ppuwrite(ppufile:tcompilerppufile);
  1815. begin
  1816. inherited ppuwrite(ppufile);
  1817. ppufile.puttype(typedconsttype);
  1818. ppufile.putbyte(byte(is_writable));
  1819. ppufile.writeentry(ibtypedconstsym);
  1820. end;
  1821. {$ifdef GDB}
  1822. function ttypedconstsym.stabstring : pchar;
  1823. var st:char;
  1824. begin
  1825. if (cs_gdb_gsym in aktglobalswitches) and (owner.symtabletype=globalsymtable) then
  1826. st:='G'
  1827. else
  1828. st:='S';
  1829. stabstring:=stabstr_evaluate('"${name}:$1$2",${N_STSYM},0,${line},${mangledname}',
  1830. [st,Tstoreddef(typedconsttype.def).numberstring]);
  1831. end;
  1832. {$endif GDB}
  1833. {****************************************************************************
  1834. TCONSTSYM
  1835. ****************************************************************************}
  1836. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;const tt:ttype);
  1837. begin
  1838. inherited create(n);
  1839. fillchar(value, sizeof(value), #0);
  1840. typ:=constsym;
  1841. consttyp:=t;
  1842. value.valueord:=v;
  1843. ResStrIndex:=0;
  1844. consttype:=tt;
  1845. end;
  1846. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;const tt:ttype);
  1847. begin
  1848. inherited create(n);
  1849. fillchar(value, sizeof(value), #0);
  1850. typ:=constsym;
  1851. consttyp:=t;
  1852. value.valueordptr:=v;
  1853. ResStrIndex:=0;
  1854. consttype:=tt;
  1855. end;
  1856. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;const tt:ttype);
  1857. begin
  1858. inherited create(n);
  1859. fillchar(value, sizeof(value), #0);
  1860. typ:=constsym;
  1861. consttyp:=t;
  1862. value.valueptr:=v;
  1863. ResStrIndex:=0;
  1864. consttype:=tt;
  1865. end;
  1866. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1867. begin
  1868. inherited create(n);
  1869. fillchar(value, sizeof(value), #0);
  1870. typ:=constsym;
  1871. consttyp:=t;
  1872. value.valueptr:=str;
  1873. consttype.reset;
  1874. value.len:=l;
  1875. if t=constresourcestring then
  1876. ResStrIndex:=ResourceStrings.Register(name,pchar(value.valueptr),value.len);
  1877. end;
  1878. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1879. var
  1880. pd : pbestreal;
  1881. ps : pnormalset;
  1882. pc : pchar;
  1883. begin
  1884. inherited ppuload(ppufile);
  1885. typ:=constsym;
  1886. consttype.reset;
  1887. consttyp:=tconsttyp(ppufile.getbyte);
  1888. fillchar(value, sizeof(value), #0);
  1889. case consttyp of
  1890. constord :
  1891. begin
  1892. ppufile.gettype(consttype);
  1893. value.valueord:=ppufile.getexprint;
  1894. end;
  1895. constpointer :
  1896. begin
  1897. ppufile.gettype(consttype);
  1898. value.valueordptr:=ppufile.getptruint;
  1899. end;
  1900. conststring,
  1901. constresourcestring :
  1902. begin
  1903. value.len:=ppufile.getlongint;
  1904. getmem(pc,value.len+1);
  1905. ppufile.getdata(pc^,value.len);
  1906. if consttyp=constresourcestring then
  1907. ResStrIndex:=ppufile.getlongint;
  1908. value.valueptr:=pc;
  1909. end;
  1910. constreal :
  1911. begin
  1912. new(pd);
  1913. pd^:=ppufile.getreal;
  1914. value.valueptr:=pd;
  1915. end;
  1916. constset :
  1917. begin
  1918. ppufile.gettype(consttype);
  1919. new(ps);
  1920. ppufile.getnormalset(ps^);
  1921. value.valueptr:=ps;
  1922. end;
  1923. constguid :
  1924. begin
  1925. new(pguid(value.valueptr));
  1926. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1927. end;
  1928. constnil : ;
  1929. else
  1930. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1931. end;
  1932. end;
  1933. destructor tconstsym.destroy;
  1934. begin
  1935. case consttyp of
  1936. conststring,
  1937. constresourcestring :
  1938. freemem(pchar(value.valueptr),value.len+1);
  1939. constreal :
  1940. dispose(pbestreal(value.valueptr));
  1941. constset :
  1942. dispose(pnormalset(value.valueptr));
  1943. constguid :
  1944. dispose(pguid(value.valueptr));
  1945. end;
  1946. inherited destroy;
  1947. end;
  1948. procedure tconstsym.buildderef;
  1949. begin
  1950. if consttyp in [constord,constpointer,constset] then
  1951. consttype.buildderef;
  1952. end;
  1953. procedure tconstsym.deref;
  1954. begin
  1955. if consttyp in [constord,constpointer,constset] then
  1956. consttype.resolve;
  1957. end;
  1958. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1959. begin
  1960. inherited ppuwrite(ppufile);
  1961. ppufile.putbyte(byte(consttyp));
  1962. case consttyp of
  1963. constnil : ;
  1964. constord :
  1965. begin
  1966. ppufile.puttype(consttype);
  1967. ppufile.putexprint(value.valueord);
  1968. end;
  1969. constpointer :
  1970. begin
  1971. ppufile.puttype(consttype);
  1972. ppufile.putptruint(value.valueordptr);
  1973. end;
  1974. conststring,
  1975. constresourcestring :
  1976. begin
  1977. ppufile.putlongint(value.len);
  1978. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1979. if consttyp=constresourcestring then
  1980. ppufile.putlongint(ResStrIndex);
  1981. end;
  1982. constreal :
  1983. ppufile.putreal(pbestreal(value.valueptr)^);
  1984. constset :
  1985. begin
  1986. ppufile.puttype(consttype);
  1987. ppufile.putnormalset(value.valueptr^);
  1988. end;
  1989. constguid :
  1990. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1991. else
  1992. internalerror(13);
  1993. end;
  1994. ppufile.writeentry(ibconstsym);
  1995. end;
  1996. {$ifdef GDB}
  1997. function Tconstsym.stabstring:Pchar;
  1998. var st : string;
  1999. begin
  2000. {even GDB v4.16 only now 'i' 'r' and 'e' !!!}
  2001. case consttyp of
  2002. conststring:
  2003. st:='s'''+backspace_quote(octal_quote(strpas(pchar(value.valueptr)),[#0..#9,#11,#12,#14..#31,'''']),['"','\',#10,#13])+'''';
  2004. constord:
  2005. st:='i'+tostr(value.valueord);
  2006. constpointer:
  2007. st:='i'+tostr(value.valueordptr);
  2008. constreal:
  2009. begin
  2010. system.str(pbestreal(value.valueptr)^,st);
  2011. st := 'r'+st;
  2012. end;
  2013. { if we don't know just put zero !! }
  2014. else st:='i0';
  2015. {***SETCONST}
  2016. {constset:;} {*** I don't know what to do with a set.}
  2017. { sets are not recognized by GDB}
  2018. {***}
  2019. end;
  2020. { valgrind does not support constants }
  2021. if cs_gdb_valgrind in aktglobalswitches then
  2022. stabstring:=nil
  2023. else
  2024. stabstring:=stabstr_evaluate('"${name}:c=$1;",${N_FUNCTION},0,${line},0',[st]);
  2025. end;
  2026. {$endif GDB}
  2027. {****************************************************************************
  2028. TENUMSYM
  2029. ****************************************************************************}
  2030. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  2031. begin
  2032. inherited create(n);
  2033. typ:=enumsym;
  2034. definition:=def;
  2035. value:=v;
  2036. { First entry? Then we need to set the minval }
  2037. if def.firstenum=nil then
  2038. begin
  2039. if v>0 then
  2040. def.has_jumps:=true;
  2041. def.setmin(v);
  2042. def.setmax(v);
  2043. end
  2044. else
  2045. begin
  2046. { check for jumps }
  2047. if v>def.max+1 then
  2048. def.has_jumps:=true;
  2049. { update low and high }
  2050. if def.min>v then
  2051. def.setmin(v);
  2052. if def.max<v then
  2053. def.setmax(v);
  2054. end;
  2055. order;
  2056. end;
  2057. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  2058. begin
  2059. inherited ppuload(ppufile);
  2060. typ:=enumsym;
  2061. ppufile.getderef(definitionderef);
  2062. value:=ppufile.getlongint;
  2063. nextenum := Nil;
  2064. end;
  2065. procedure tenumsym.buildderef;
  2066. begin
  2067. definitionderef.build(definition);
  2068. end;
  2069. procedure tenumsym.deref;
  2070. begin
  2071. definition:=tenumdef(definitionderef.resolve);
  2072. order;
  2073. end;
  2074. procedure tenumsym.order;
  2075. var
  2076. sym : tenumsym;
  2077. begin
  2078. sym := tenumsym(definition.firstenum);
  2079. if sym = nil then
  2080. begin
  2081. definition.firstenum := self;
  2082. nextenum := nil;
  2083. exit;
  2084. end;
  2085. { reorder the symbols in increasing value }
  2086. if value < sym.value then
  2087. begin
  2088. nextenum := sym;
  2089. definition.firstenum := self;
  2090. end
  2091. else
  2092. begin
  2093. while (sym.value <= value) and assigned(sym.nextenum) do
  2094. sym := sym.nextenum;
  2095. nextenum := sym.nextenum;
  2096. sym.nextenum := self;
  2097. end;
  2098. end;
  2099. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  2100. begin
  2101. inherited ppuwrite(ppufile);
  2102. ppufile.putderef(definitionderef);
  2103. ppufile.putlongint(value);
  2104. ppufile.writeentry(ibenumsym);
  2105. end;
  2106. {****************************************************************************
  2107. TTYPESYM
  2108. ****************************************************************************}
  2109. constructor ttypesym.create(const n : string;const tt : ttype);
  2110. begin
  2111. inherited create(n);
  2112. typ:=typesym;
  2113. restype:=tt;
  2114. { register the typesym for the definition }
  2115. if assigned(restype.def) and
  2116. (restype.def.deftype<>errordef) and
  2117. not(assigned(restype.def.typesym)) then
  2118. restype.def.typesym:=self;
  2119. end;
  2120. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  2121. begin
  2122. inherited ppuload(ppufile);
  2123. typ:=typesym;
  2124. ppufile.gettype(restype);
  2125. end;
  2126. function ttypesym.gettypedef:tdef;
  2127. begin
  2128. gettypedef:=restype.def;
  2129. end;
  2130. procedure ttypesym.buildderef;
  2131. begin
  2132. restype.buildderef;
  2133. end;
  2134. procedure ttypesym.deref;
  2135. begin
  2136. restype.resolve;
  2137. end;
  2138. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  2139. begin
  2140. inherited ppuwrite(ppufile);
  2141. ppufile.puttype(restype);
  2142. ppufile.writeentry(ibtypesym);
  2143. end;
  2144. procedure ttypesym.load_references(ppufile:tcompilerppufile;locals:boolean);
  2145. begin
  2146. inherited load_references(ppufile,locals);
  2147. if (restype.def.deftype=recorddef) then
  2148. tstoredsymtable(trecorddef(restype.def).symtable).load_references(ppufile,locals);
  2149. if (restype.def.deftype=objectdef) then
  2150. tstoredsymtable(tobjectdef(restype.def).symtable).load_references(ppufile,locals);
  2151. end;
  2152. function ttypesym.write_references(ppufile:tcompilerppufile;locals:boolean):boolean;
  2153. var
  2154. d : tderef;
  2155. begin
  2156. d.reset;
  2157. if not inherited write_references(ppufile,locals) then
  2158. begin
  2159. { write address of this symbol if record or object
  2160. even if no real refs are there
  2161. because we need it for the symtable }
  2162. if (restype.def.deftype in [recorddef,objectdef]) then
  2163. begin
  2164. d.build(self);
  2165. ppufile.putderef(d);
  2166. ppufile.writeentry(ibsymref);
  2167. end;
  2168. end;
  2169. write_references:=true;
  2170. if (restype.def.deftype=recorddef) then
  2171. tstoredsymtable(trecorddef(restype.def).symtable).write_references(ppufile,locals);
  2172. if (restype.def.deftype=objectdef) then
  2173. tstoredsymtable(tobjectdef(restype.def).symtable).write_references(ppufile,locals);
  2174. end;
  2175. {$ifdef GDB}
  2176. function ttypesym.stabstring : pchar;
  2177. var stabchar:string[2];
  2178. begin
  2179. stabstring:=nil;
  2180. if restype.def<>nil then
  2181. begin
  2182. if restype.def.deftype in tagtypes then
  2183. stabchar:='Tt'
  2184. else
  2185. stabchar:='t';
  2186. stabstring:=stabstr_evaluate('"${name}:$1$2",${N_LSYM},0,${line},0',[stabchar,tstoreddef(restype.def).numberstring]);
  2187. end;
  2188. end;
  2189. {$endif GDB}
  2190. {****************************************************************************
  2191. TSYSSYM
  2192. ****************************************************************************}
  2193. constructor tsyssym.create(const n : string;l : longint);
  2194. begin
  2195. inherited create(n);
  2196. typ:=syssym;
  2197. number:=l;
  2198. end;
  2199. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  2200. begin
  2201. inherited ppuload(ppufile);
  2202. typ:=syssym;
  2203. number:=ppufile.getlongint;
  2204. end;
  2205. destructor tsyssym.destroy;
  2206. begin
  2207. inherited destroy;
  2208. end;
  2209. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  2210. begin
  2211. inherited ppuwrite(ppufile);
  2212. ppufile.putlongint(number);
  2213. ppufile.writeentry(ibsyssym);
  2214. end;
  2215. {*****************************************************************************
  2216. TMacro
  2217. *****************************************************************************}
  2218. constructor tmacro.create(const n : string);
  2219. begin
  2220. inherited create(n);
  2221. typ:= macrosym;
  2222. owner:= nil;
  2223. defined:=false;
  2224. is_used:=false;
  2225. is_compiler_var:= false;
  2226. buftext:=nil;
  2227. buflen:=0;
  2228. end;
  2229. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  2230. begin
  2231. inherited ppuload(ppufile);
  2232. typ:=macrosym;
  2233. name:=ppufile.getstring;
  2234. defined:=boolean(ppufile.getbyte);
  2235. is_compiler_var:=boolean(ppufile.getbyte);
  2236. is_used:=false;
  2237. buflen:= ppufile.getlongint;
  2238. if buflen > 0 then
  2239. begin
  2240. getmem(buftext, buflen);
  2241. ppufile.getdata(buftext^, buflen)
  2242. end
  2243. else
  2244. buftext:=nil;
  2245. end;
  2246. destructor tmacro.destroy;
  2247. begin
  2248. if assigned(buftext) then
  2249. freemem(buftext,buflen);
  2250. inherited destroy;
  2251. end;
  2252. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  2253. begin
  2254. inherited ppuwrite(ppufile);
  2255. ppufile.putstring(name);
  2256. ppufile.putbyte(byte(defined));
  2257. ppufile.putbyte(byte(is_compiler_var));
  2258. ppufile.putlongint(buflen);
  2259. if buflen > 0 then
  2260. ppufile.putdata(buftext^,buflen);
  2261. ppufile.writeentry(ibmacrosym);
  2262. end;
  2263. {****************************************************************************
  2264. TRTTISYM
  2265. ****************************************************************************}
  2266. constructor trttisym.create(const n:string;rt:trttitype);
  2267. const
  2268. prefix : array[trttitype] of string[5]=('$rtti','$init');
  2269. begin
  2270. inherited create(prefix[rt]+n);
  2271. include(symoptions,sp_internal);
  2272. typ:=rttisym;
  2273. lab:=nil;
  2274. rttityp:=rt;
  2275. end;
  2276. destructor trttisym.destroy;
  2277. begin
  2278. if assigned(_mangledname) then
  2279. begin
  2280. {$ifdef MEMDEBUG}
  2281. memmanglednames.start;
  2282. {$endif MEMDEBUG}
  2283. stringdispose(_mangledname);
  2284. {$ifdef MEMDEBUG}
  2285. memmanglednames.stop;
  2286. {$endif MEMDEBUG}
  2287. end;
  2288. inherited destroy;
  2289. end;
  2290. constructor trttisym.ppuload(ppufile:tcompilerppufile);
  2291. begin
  2292. inherited ppuload(ppufile);
  2293. typ:=rttisym;
  2294. lab:=nil;
  2295. rttityp:=trttitype(ppufile.getbyte);
  2296. end;
  2297. procedure trttisym.ppuwrite(ppufile:tcompilerppufile);
  2298. begin
  2299. inherited ppuwrite(ppufile);
  2300. ppufile.putbyte(byte(rttityp));
  2301. ppufile.writeentry(ibrttisym);
  2302. end;
  2303. function trttisym.mangledname : string;
  2304. const
  2305. prefix : array[trttitype] of string[5]=('RTTI_','INIT_');
  2306. begin
  2307. if not assigned(_mangledname) then
  2308. _mangledname:=stringdup(make_mangledname(prefix[rttityp],owner,Copy(name,5,255)));
  2309. result:=_mangledname^;
  2310. end;
  2311. function trttisym.get_label:tasmsymbol;
  2312. begin
  2313. { the label is always a global label }
  2314. if not assigned(lab) then
  2315. lab:=objectlibrary.newasmsymbol(mangledname,AB_EXTERNAL,AT_DATA);
  2316. get_label:=lab;
  2317. end;
  2318. end.