symsym.pas 78 KB

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