symsym.pas 78 KB

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