symsym.pas 77 KB

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