symsym.pas 79 KB

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