symsym.pas 77 KB

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