ax-gdb.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662
  1. /* GDB-specific functions for operating on agent expressions.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  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 3 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, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "symtab.h"
  16. #include "symfile.h"
  17. #include "gdbtypes.h"
  18. #include "language.h"
  19. #include "value.h"
  20. #include "expression.h"
  21. #include "command.h"
  22. #include "gdbcmd.h"
  23. #include "frame.h"
  24. #include "target.h"
  25. #include "ax.h"
  26. #include "ax-gdb.h"
  27. #include "block.h"
  28. #include "regcache.h"
  29. #include "user-regs.h"
  30. #include "dictionary.h"
  31. #include "breakpoint.h"
  32. #include "tracepoint.h"
  33. #include "cp-support.h"
  34. #include "arch-utils.h"
  35. #include "cli/cli-utils.h"
  36. #include "linespec.h"
  37. #include "location.h"
  38. #include "objfiles.h"
  39. #include "typeprint.h"
  40. #include "valprint.h"
  41. #include "c-lang.h"
  42. #include "expop.h"
  43. #include "gdbsupport/format.h"
  44. /* To make sense of this file, you should read doc/agentexpr.texi.
  45. Then look at the types and enums in ax-gdb.h. For the code itself,
  46. look at gen_expr, towards the bottom; that's the main function that
  47. looks at the GDB expressions and calls everything else to generate
  48. code.
  49. I'm beginning to wonder whether it wouldn't be nicer to internally
  50. generate trees, with types, and then spit out the bytecode in
  51. linear form afterwards; we could generate fewer `swap', `ext', and
  52. `zero_ext' bytecodes that way; it would make good constant folding
  53. easier, too. But at the moment, I think we should be willing to
  54. pay for the simplicity of this code with less-than-optimal bytecode
  55. strings.
  56. Remember, "GBD" stands for "Great Britain, Dammit!" So be careful. */
  57. /* Prototypes for local functions. */
  58. /* There's a standard order to the arguments of these functions:
  59. struct agent_expr * --- agent expression buffer to generate code into
  60. struct axs_value * --- describes value left on top of stack */
  61. static void gen_traced_pop (struct agent_expr *, struct axs_value *);
  62. static void gen_sign_extend (struct agent_expr *, struct type *);
  63. static void gen_extend (struct agent_expr *, struct type *);
  64. static void gen_fetch (struct agent_expr *, struct type *);
  65. static void gen_left_shift (struct agent_expr *, int);
  66. static void gen_frame_args_address (struct agent_expr *);
  67. static void gen_frame_locals_address (struct agent_expr *);
  68. static void gen_offset (struct agent_expr *ax, int offset);
  69. static void gen_sym_offset (struct agent_expr *, struct symbol *);
  70. static void gen_var_ref (struct agent_expr *ax, struct axs_value *value,
  71. struct symbol *var);
  72. static void gen_int_literal (struct agent_expr *ax,
  73. struct axs_value *value,
  74. LONGEST k, struct type *type);
  75. static void gen_usual_unary (struct agent_expr *ax, struct axs_value *value);
  76. static int type_wider_than (struct type *type1, struct type *type2);
  77. static struct type *max_type (struct type *type1, struct type *type2);
  78. static void gen_conversion (struct agent_expr *ax,
  79. struct type *from, struct type *to);
  80. static int is_nontrivial_conversion (struct type *from, struct type *to);
  81. static void gen_usual_arithmetic (struct agent_expr *ax,
  82. struct axs_value *value1,
  83. struct axs_value *value2);
  84. static void gen_integral_promotions (struct agent_expr *ax,
  85. struct axs_value *value);
  86. static void gen_cast (struct agent_expr *ax,
  87. struct axs_value *value, struct type *type);
  88. static void gen_scale (struct agent_expr *ax,
  89. enum agent_op op, struct type *type);
  90. static void gen_ptradd (struct agent_expr *ax, struct axs_value *value,
  91. struct axs_value *value1, struct axs_value *value2);
  92. static void gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
  93. struct axs_value *value1, struct axs_value *value2);
  94. static void gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
  95. struct axs_value *value1, struct axs_value *value2,
  96. struct type *result_type);
  97. static void gen_binop (struct agent_expr *ax,
  98. struct axs_value *value,
  99. struct axs_value *value1,
  100. struct axs_value *value2,
  101. enum agent_op op,
  102. enum agent_op op_unsigned, int may_carry,
  103. const char *name);
  104. static void gen_logical_not (struct agent_expr *ax, struct axs_value *value,
  105. struct type *result_type);
  106. static void gen_complement (struct agent_expr *ax, struct axs_value *value);
  107. static void gen_deref (struct axs_value *);
  108. static void gen_address_of (struct axs_value *);
  109. static void gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
  110. struct type *type, int start, int end);
  111. static void gen_primitive_field (struct agent_expr *ax,
  112. struct axs_value *value,
  113. int offset, int fieldno, struct type *type);
  114. static int gen_struct_ref_recursive (struct agent_expr *ax,
  115. struct axs_value *value,
  116. const char *field, int offset,
  117. struct type *type);
  118. static void gen_struct_ref (struct agent_expr *ax,
  119. struct axs_value *value,
  120. const char *field,
  121. const char *operator_name,
  122. const char *operand_name);
  123. static void gen_static_field (struct agent_expr *ax, struct axs_value *value,
  124. struct type *type, int fieldno);
  125. static void gen_expr_binop_rest (struct expression *exp,
  126. enum exp_opcode op,
  127. struct agent_expr *ax,
  128. struct axs_value *value,
  129. struct axs_value *value1,
  130. struct axs_value *value2);
  131. /* Generating bytecode from GDB expressions: general assumptions */
  132. /* Here are a few general assumptions made throughout the code; if you
  133. want to make a change that contradicts one of these, then you'd
  134. better scan things pretty thoroughly.
  135. - We assume that all values occupy one stack element. For example,
  136. sometimes we'll swap to get at the left argument to a binary
  137. operator. If we decide that void values should occupy no stack
  138. elements, or that synthetic arrays (whose size is determined at
  139. run time, created by the `@' operator) should occupy two stack
  140. elements (address and length), then this will cause trouble.
  141. - We assume the stack elements are infinitely wide, and that we
  142. don't have to worry what happens if the user requests an
  143. operation that is wider than the actual interpreter's stack.
  144. That is, it's up to the interpreter to handle directly all the
  145. integer widths the user has access to. (Woe betide the language
  146. with bignums!)
  147. - We don't support side effects. Thus, we don't have to worry about
  148. GCC's generalized lvalues, function calls, etc.
  149. - We don't support floating point. Many places where we switch on
  150. some type don't bother to include cases for floating point; there
  151. may be even more subtle ways this assumption exists. For
  152. example, the arguments to % must be integers.
  153. - We assume all subexpressions have a static, unchanging type. If
  154. we tried to support convenience variables, this would be a
  155. problem.
  156. - All values on the stack should always be fully zero- or
  157. sign-extended.
  158. (I wasn't sure whether to choose this or its opposite --- that
  159. only addresses are assumed extended --- but it turns out that
  160. neither convention completely eliminates spurious extend
  161. operations (if everything is always extended, then you have to
  162. extend after add, because it could overflow; if nothing is
  163. extended, then you end up producing extends whenever you change
  164. sizes), and this is simpler.) */
  165. /* Scan for all static fields in the given class, including any base
  166. classes, and generate tracing bytecodes for each. */
  167. static void
  168. gen_trace_static_fields (struct agent_expr *ax,
  169. struct type *type)
  170. {
  171. int i, nbases = TYPE_N_BASECLASSES (type);
  172. struct axs_value value;
  173. type = check_typedef (type);
  174. for (i = type->num_fields () - 1; i >= nbases; i--)
  175. {
  176. if (field_is_static (&type->field (i)))
  177. {
  178. gen_static_field (ax, &value, type, i);
  179. if (value.optimized_out)
  180. continue;
  181. switch (value.kind)
  182. {
  183. case axs_lvalue_memory:
  184. {
  185. /* Initialize the TYPE_LENGTH if it is a typedef. */
  186. check_typedef (value.type);
  187. ax_const_l (ax, TYPE_LENGTH (value.type));
  188. ax_simple (ax, aop_trace);
  189. }
  190. break;
  191. case axs_lvalue_register:
  192. /* We don't actually need the register's value to be pushed,
  193. just note that we need it to be collected. */
  194. ax_reg_mask (ax, value.u.reg);
  195. default:
  196. break;
  197. }
  198. }
  199. }
  200. /* Now scan through base classes recursively. */
  201. for (i = 0; i < nbases; i++)
  202. {
  203. struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
  204. gen_trace_static_fields (ax, basetype);
  205. }
  206. }
  207. /* Trace the lvalue on the stack, if it needs it. In either case, pop
  208. the value. Useful on the left side of a comma, and at the end of
  209. an expression being used for tracing. */
  210. static void
  211. gen_traced_pop (struct agent_expr *ax, struct axs_value *value)
  212. {
  213. int string_trace = 0;
  214. if (ax->trace_string
  215. && value->type->code () == TYPE_CODE_PTR
  216. && c_textual_element_type (check_typedef (TYPE_TARGET_TYPE (value->type)),
  217. 's'))
  218. string_trace = 1;
  219. if (ax->tracing)
  220. switch (value->kind)
  221. {
  222. case axs_rvalue:
  223. if (string_trace)
  224. {
  225. ax_const_l (ax, ax->trace_string);
  226. ax_simple (ax, aop_tracenz);
  227. }
  228. else
  229. /* We don't trace rvalues, just the lvalues necessary to
  230. produce them. So just dispose of this value. */
  231. ax_simple (ax, aop_pop);
  232. break;
  233. case axs_lvalue_memory:
  234. {
  235. /* Initialize the TYPE_LENGTH if it is a typedef. */
  236. check_typedef (value->type);
  237. if (string_trace)
  238. {
  239. gen_fetch (ax, value->type);
  240. ax_const_l (ax, ax->trace_string);
  241. ax_simple (ax, aop_tracenz);
  242. }
  243. else
  244. {
  245. /* There's no point in trying to use a trace_quick bytecode
  246. here, since "trace_quick SIZE pop" is three bytes, whereas
  247. "const8 SIZE trace" is also three bytes, does the same
  248. thing, and the simplest code which generates that will also
  249. work correctly for objects with large sizes. */
  250. ax_const_l (ax, TYPE_LENGTH (value->type));
  251. ax_simple (ax, aop_trace);
  252. }
  253. }
  254. break;
  255. case axs_lvalue_register:
  256. /* We don't actually need the register's value to be on the
  257. stack, and the target will get heartburn if the register is
  258. larger than will fit in a stack, so just mark it for
  259. collection and be done with it. */
  260. ax_reg_mask (ax, value->u.reg);
  261. /* But if the register points to a string, assume the value
  262. will fit on the stack and push it anyway. */
  263. if (string_trace)
  264. {
  265. ax_reg (ax, value->u.reg);
  266. ax_const_l (ax, ax->trace_string);
  267. ax_simple (ax, aop_tracenz);
  268. }
  269. break;
  270. }
  271. else
  272. /* If we're not tracing, just pop the value. */
  273. ax_simple (ax, aop_pop);
  274. /* To trace C++ classes with static fields stored elsewhere. */
  275. if (ax->tracing
  276. && (value->type->code () == TYPE_CODE_STRUCT
  277. || value->type->code () == TYPE_CODE_UNION))
  278. gen_trace_static_fields (ax, value->type);
  279. }
  280. /* Generating bytecode from GDB expressions: helper functions */
  281. /* Assume that the lower bits of the top of the stack is a value of
  282. type TYPE, and the upper bits are zero. Sign-extend if necessary. */
  283. static void
  284. gen_sign_extend (struct agent_expr *ax, struct type *type)
  285. {
  286. /* Do we need to sign-extend this? */
  287. if (!type->is_unsigned ())
  288. ax_ext (ax, TYPE_LENGTH (type) * TARGET_CHAR_BIT);
  289. }
  290. /* Assume the lower bits of the top of the stack hold a value of type
  291. TYPE, and the upper bits are garbage. Sign-extend or truncate as
  292. needed. */
  293. static void
  294. gen_extend (struct agent_expr *ax, struct type *type)
  295. {
  296. int bits = TYPE_LENGTH (type) * TARGET_CHAR_BIT;
  297. /* I just had to. */
  298. ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, bits));
  299. }
  300. /* Assume that the top of the stack contains a value of type "pointer
  301. to TYPE"; generate code to fetch its value. Note that TYPE is the
  302. target type, not the pointer type. */
  303. static void
  304. gen_fetch (struct agent_expr *ax, struct type *type)
  305. {
  306. if (ax->tracing)
  307. {
  308. /* Record the area of memory we're about to fetch. */
  309. ax_trace_quick (ax, TYPE_LENGTH (type));
  310. }
  311. if (type->code () == TYPE_CODE_RANGE)
  312. type = TYPE_TARGET_TYPE (type);
  313. switch (type->code ())
  314. {
  315. case TYPE_CODE_PTR:
  316. case TYPE_CODE_REF:
  317. case TYPE_CODE_RVALUE_REF:
  318. case TYPE_CODE_ENUM:
  319. case TYPE_CODE_INT:
  320. case TYPE_CODE_CHAR:
  321. case TYPE_CODE_BOOL:
  322. /* It's a scalar value, so we know how to dereference it. How
  323. many bytes long is it? */
  324. switch (TYPE_LENGTH (type))
  325. {
  326. case 8 / TARGET_CHAR_BIT:
  327. ax_simple (ax, aop_ref8);
  328. break;
  329. case 16 / TARGET_CHAR_BIT:
  330. ax_simple (ax, aop_ref16);
  331. break;
  332. case 32 / TARGET_CHAR_BIT:
  333. ax_simple (ax, aop_ref32);
  334. break;
  335. case 64 / TARGET_CHAR_BIT:
  336. ax_simple (ax, aop_ref64);
  337. break;
  338. /* Either our caller shouldn't have asked us to dereference
  339. that pointer (other code's fault), or we're not
  340. implementing something we should be (this code's fault).
  341. In any case, it's a bug the user shouldn't see. */
  342. default:
  343. internal_error (__FILE__, __LINE__,
  344. _("gen_fetch: strange size"));
  345. }
  346. gen_sign_extend (ax, type);
  347. break;
  348. default:
  349. /* Our caller requested us to dereference a pointer from an unsupported
  350. type. Error out and give callers a chance to handle the failure
  351. gracefully. */
  352. error (_("gen_fetch: Unsupported type code `%s'."),
  353. type->name ());
  354. }
  355. }
  356. /* Generate code to left shift the top of the stack by DISTANCE bits, or
  357. right shift it by -DISTANCE bits if DISTANCE < 0. This generates
  358. unsigned (logical) right shifts. */
  359. static void
  360. gen_left_shift (struct agent_expr *ax, int distance)
  361. {
  362. if (distance > 0)
  363. {
  364. ax_const_l (ax, distance);
  365. ax_simple (ax, aop_lsh);
  366. }
  367. else if (distance < 0)
  368. {
  369. ax_const_l (ax, -distance);
  370. ax_simple (ax, aop_rsh_unsigned);
  371. }
  372. }
  373. /* Generating bytecode from GDB expressions: symbol references */
  374. /* Generate code to push the base address of the argument portion of
  375. the top stack frame. */
  376. static void
  377. gen_frame_args_address (struct agent_expr *ax)
  378. {
  379. int frame_reg;
  380. LONGEST frame_offset;
  381. gdbarch_virtual_frame_pointer (ax->gdbarch,
  382. ax->scope, &frame_reg, &frame_offset);
  383. ax_reg (ax, frame_reg);
  384. gen_offset (ax, frame_offset);
  385. }
  386. /* Generate code to push the base address of the locals portion of the
  387. top stack frame. */
  388. static void
  389. gen_frame_locals_address (struct agent_expr *ax)
  390. {
  391. int frame_reg;
  392. LONGEST frame_offset;
  393. gdbarch_virtual_frame_pointer (ax->gdbarch,
  394. ax->scope, &frame_reg, &frame_offset);
  395. ax_reg (ax, frame_reg);
  396. gen_offset (ax, frame_offset);
  397. }
  398. /* Generate code to add OFFSET to the top of the stack. Try to
  399. generate short and readable code. We use this for getting to
  400. variables on the stack, and structure members. If we were
  401. programming in ML, it would be clearer why these are the same
  402. thing. */
  403. static void
  404. gen_offset (struct agent_expr *ax, int offset)
  405. {
  406. /* It would suffice to simply push the offset and add it, but this
  407. makes it easier to read positive and negative offsets in the
  408. bytecode. */
  409. if (offset > 0)
  410. {
  411. ax_const_l (ax, offset);
  412. ax_simple (ax, aop_add);
  413. }
  414. else if (offset < 0)
  415. {
  416. ax_const_l (ax, -offset);
  417. ax_simple (ax, aop_sub);
  418. }
  419. }
  420. /* In many cases, a symbol's value is the offset from some other
  421. address (stack frame, base register, etc.) Generate code to add
  422. VAR's value to the top of the stack. */
  423. static void
  424. gen_sym_offset (struct agent_expr *ax, struct symbol *var)
  425. {
  426. gen_offset (ax, SYMBOL_VALUE (var));
  427. }
  428. /* Generate code for a variable reference to AX. The variable is the
  429. symbol VAR. Set VALUE to describe the result. */
  430. static void
  431. gen_var_ref (struct agent_expr *ax, struct axs_value *value, struct symbol *var)
  432. {
  433. /* Dereference any typedefs. */
  434. value->type = check_typedef (var->type ());
  435. value->optimized_out = 0;
  436. if (SYMBOL_COMPUTED_OPS (var) != NULL)
  437. {
  438. SYMBOL_COMPUTED_OPS (var)->tracepoint_var_ref (var, ax, value);
  439. return;
  440. }
  441. /* I'm imitating the code in read_var_value. */
  442. switch (var->aclass ())
  443. {
  444. case LOC_CONST: /* A constant, like an enum value. */
  445. ax_const_l (ax, (LONGEST) SYMBOL_VALUE (var));
  446. value->kind = axs_rvalue;
  447. break;
  448. case LOC_LABEL: /* A goto label, being used as a value. */
  449. ax_const_l (ax, (LONGEST) SYMBOL_VALUE_ADDRESS (var));
  450. value->kind = axs_rvalue;
  451. break;
  452. case LOC_CONST_BYTES:
  453. internal_error (__FILE__, __LINE__,
  454. _("gen_var_ref: LOC_CONST_BYTES "
  455. "symbols are not supported"));
  456. /* Variable at a fixed location in memory. Easy. */
  457. case LOC_STATIC:
  458. /* Push the address of the variable. */
  459. ax_const_l (ax, SYMBOL_VALUE_ADDRESS (var));
  460. value->kind = axs_lvalue_memory;
  461. break;
  462. case LOC_ARG: /* var lives in argument area of frame */
  463. gen_frame_args_address (ax);
  464. gen_sym_offset (ax, var);
  465. value->kind = axs_lvalue_memory;
  466. break;
  467. case LOC_REF_ARG: /* As above, but the frame slot really
  468. holds the address of the variable. */
  469. gen_frame_args_address (ax);
  470. gen_sym_offset (ax, var);
  471. /* Don't assume any particular pointer size. */
  472. gen_fetch (ax, builtin_type (ax->gdbarch)->builtin_data_ptr);
  473. value->kind = axs_lvalue_memory;
  474. break;
  475. case LOC_LOCAL: /* var lives in locals area of frame */
  476. gen_frame_locals_address (ax);
  477. gen_sym_offset (ax, var);
  478. value->kind = axs_lvalue_memory;
  479. break;
  480. case LOC_TYPEDEF:
  481. error (_("Cannot compute value of typedef `%s'."),
  482. var->print_name ());
  483. break;
  484. case LOC_BLOCK:
  485. ax_const_l (ax, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (var)));
  486. value->kind = axs_rvalue;
  487. break;
  488. case LOC_REGISTER:
  489. /* Don't generate any code at all; in the process of treating
  490. this as an lvalue or rvalue, the caller will generate the
  491. right code. */
  492. value->kind = axs_lvalue_register;
  493. value->u.reg
  494. = SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch);
  495. break;
  496. /* A lot like LOC_REF_ARG, but the pointer lives directly in a
  497. register, not on the stack. Simpler than LOC_REGISTER
  498. because it's just like any other case where the thing
  499. has a real address. */
  500. case LOC_REGPARM_ADDR:
  501. ax_reg (ax,
  502. SYMBOL_REGISTER_OPS (var)->register_number (var, ax->gdbarch));
  503. value->kind = axs_lvalue_memory;
  504. break;
  505. case LOC_UNRESOLVED:
  506. {
  507. struct bound_minimal_symbol msym
  508. = lookup_minimal_symbol (var->linkage_name (), NULL, NULL);
  509. if (!msym.minsym)
  510. error (_("Couldn't resolve symbol `%s'."), var->print_name ());
  511. /* Push the address of the variable. */
  512. ax_const_l (ax, BMSYMBOL_VALUE_ADDRESS (msym));
  513. value->kind = axs_lvalue_memory;
  514. }
  515. break;
  516. case LOC_COMPUTED:
  517. gdb_assert_not_reached ("LOC_COMPUTED variable missing a method");
  518. case LOC_OPTIMIZED_OUT:
  519. /* Flag this, but don't say anything; leave it up to callers to
  520. warn the user. */
  521. value->optimized_out = 1;
  522. break;
  523. default:
  524. error (_("Cannot find value of botched symbol `%s'."),
  525. var->print_name ());
  526. break;
  527. }
  528. }
  529. /* Generate code for a minimal symbol variable reference to AX. The
  530. variable is the symbol MINSYM, of OBJFILE. Set VALUE to describe
  531. the result. */
  532. static void
  533. gen_msym_var_ref (agent_expr *ax, axs_value *value,
  534. minimal_symbol *msymbol, objfile *objf)
  535. {
  536. CORE_ADDR address;
  537. type *t = find_minsym_type_and_address (msymbol, objf, &address);
  538. value->type = t;
  539. value->optimized_out = false;
  540. ax_const_l (ax, address);
  541. value->kind = axs_lvalue_memory;
  542. }
  543. /* Generating bytecode from GDB expressions: literals */
  544. static void
  545. gen_int_literal (struct agent_expr *ax, struct axs_value *value, LONGEST k,
  546. struct type *type)
  547. {
  548. ax_const_l (ax, k);
  549. value->kind = axs_rvalue;
  550. value->type = check_typedef (type);
  551. }
  552. /* Generating bytecode from GDB expressions: unary conversions, casts */
  553. /* Take what's on the top of the stack (as described by VALUE), and
  554. try to make an rvalue out of it. Signal an error if we can't do
  555. that. */
  556. void
  557. require_rvalue (struct agent_expr *ax, struct axs_value *value)
  558. {
  559. /* Only deal with scalars, structs and such may be too large
  560. to fit in a stack entry. */
  561. value->type = check_typedef (value->type);
  562. if (value->type->code () == TYPE_CODE_ARRAY
  563. || value->type->code () == TYPE_CODE_STRUCT
  564. || value->type->code () == TYPE_CODE_UNION
  565. || value->type->code () == TYPE_CODE_FUNC)
  566. error (_("Value not scalar: cannot be an rvalue."));
  567. switch (value->kind)
  568. {
  569. case axs_rvalue:
  570. /* It's already an rvalue. */
  571. break;
  572. case axs_lvalue_memory:
  573. /* The top of stack is the address of the object. Dereference. */
  574. gen_fetch (ax, value->type);
  575. break;
  576. case axs_lvalue_register:
  577. /* There's nothing on the stack, but value->u.reg is the
  578. register number containing the value.
  579. When we add floating-point support, this is going to have to
  580. change. What about SPARC register pairs, for example? */
  581. ax_reg (ax, value->u.reg);
  582. gen_extend (ax, value->type);
  583. break;
  584. }
  585. value->kind = axs_rvalue;
  586. }
  587. /* Assume the top of the stack is described by VALUE, and perform the
  588. usual unary conversions. This is motivated by ANSI 6.2.2, but of
  589. course GDB expressions are not ANSI; they're the mishmash union of
  590. a bunch of languages. Rah.
  591. NOTE! This function promises to produce an rvalue only when the
  592. incoming value is of an appropriate type. In other words, the
  593. consumer of the value this function produces may assume the value
  594. is an rvalue only after checking its type.
  595. The immediate issue is that if the user tries to use a structure or
  596. union as an operand of, say, the `+' operator, we don't want to try
  597. to convert that structure to an rvalue; require_rvalue will bomb on
  598. structs and unions. Rather, we want to simply pass the struct
  599. lvalue through unchanged, and let `+' raise an error. */
  600. static void
  601. gen_usual_unary (struct agent_expr *ax, struct axs_value *value)
  602. {
  603. /* We don't have to generate any code for the usual integral
  604. conversions, since values are always represented as full-width on
  605. the stack. Should we tweak the type? */
  606. /* Some types require special handling. */
  607. switch (value->type->code ())
  608. {
  609. /* Functions get converted to a pointer to the function. */
  610. case TYPE_CODE_FUNC:
  611. value->type = lookup_pointer_type (value->type);
  612. value->kind = axs_rvalue; /* Should always be true, but just in case. */
  613. break;
  614. /* Arrays get converted to a pointer to their first element, and
  615. are no longer an lvalue. */
  616. case TYPE_CODE_ARRAY:
  617. {
  618. struct type *elements = TYPE_TARGET_TYPE (value->type);
  619. value->type = lookup_pointer_type (elements);
  620. value->kind = axs_rvalue;
  621. /* We don't need to generate any code; the address of the array
  622. is also the address of its first element. */
  623. }
  624. break;
  625. /* Don't try to convert structures and unions to rvalues. Let the
  626. consumer signal an error. */
  627. case TYPE_CODE_STRUCT:
  628. case TYPE_CODE_UNION:
  629. return;
  630. }
  631. /* If the value is an lvalue, dereference it. */
  632. require_rvalue (ax, value);
  633. }
  634. /* Return non-zero iff the type TYPE1 is considered "wider" than the
  635. type TYPE2, according to the rules described in gen_usual_arithmetic. */
  636. static int
  637. type_wider_than (struct type *type1, struct type *type2)
  638. {
  639. return (TYPE_LENGTH (type1) > TYPE_LENGTH (type2)
  640. || (TYPE_LENGTH (type1) == TYPE_LENGTH (type2)
  641. && type1->is_unsigned ()
  642. && !type2->is_unsigned ()));
  643. }
  644. /* Return the "wider" of the two types TYPE1 and TYPE2. */
  645. static struct type *
  646. max_type (struct type *type1, struct type *type2)
  647. {
  648. return type_wider_than (type1, type2) ? type1 : type2;
  649. }
  650. /* Generate code to convert a scalar value of type FROM to type TO. */
  651. static void
  652. gen_conversion (struct agent_expr *ax, struct type *from, struct type *to)
  653. {
  654. /* Perhaps there is a more graceful way to state these rules. */
  655. /* If we're converting to a narrower type, then we need to clear out
  656. the upper bits. */
  657. if (TYPE_LENGTH (to) < TYPE_LENGTH (from))
  658. gen_extend (ax, to);
  659. /* If the two values have equal width, but different signednesses,
  660. then we need to extend. */
  661. else if (TYPE_LENGTH (to) == TYPE_LENGTH (from))
  662. {
  663. if (from->is_unsigned () != to->is_unsigned ())
  664. gen_extend (ax, to);
  665. }
  666. /* If we're converting to a wider type, and becoming unsigned, then
  667. we need to zero out any possible sign bits. */
  668. else if (TYPE_LENGTH (to) > TYPE_LENGTH (from))
  669. {
  670. if (to->is_unsigned ())
  671. gen_extend (ax, to);
  672. }
  673. }
  674. /* Return non-zero iff the type FROM will require any bytecodes to be
  675. emitted to be converted to the type TO. */
  676. static int
  677. is_nontrivial_conversion (struct type *from, struct type *to)
  678. {
  679. agent_expr_up ax (new agent_expr (NULL, 0));
  680. int nontrivial;
  681. /* Actually generate the code, and see if anything came out. At the
  682. moment, it would be trivial to replicate the code in
  683. gen_conversion here, but in the future, when we're supporting
  684. floating point and the like, it may not be. Doing things this
  685. way allows this function to be independent of the logic in
  686. gen_conversion. */
  687. gen_conversion (ax.get (), from, to);
  688. nontrivial = ax->len > 0;
  689. return nontrivial;
  690. }
  691. /* Generate code to perform the "usual arithmetic conversions" (ANSI C
  692. 6.2.1.5) for the two operands of an arithmetic operator. This
  693. effectively finds a "least upper bound" type for the two arguments,
  694. and promotes each argument to that type. *VALUE1 and *VALUE2
  695. describe the values as they are passed in, and as they are left. */
  696. static void
  697. gen_usual_arithmetic (struct agent_expr *ax, struct axs_value *value1,
  698. struct axs_value *value2)
  699. {
  700. /* Do the usual binary conversions. */
  701. if (value1->type->code () == TYPE_CODE_INT
  702. && value2->type->code () == TYPE_CODE_INT)
  703. {
  704. /* The ANSI integral promotions seem to work this way: Order the
  705. integer types by size, and then by signedness: an n-bit
  706. unsigned type is considered "wider" than an n-bit signed
  707. type. Promote to the "wider" of the two types, and always
  708. promote at least to int. */
  709. struct type *target = max_type (builtin_type (ax->gdbarch)->builtin_int,
  710. max_type (value1->type, value2->type));
  711. /* Deal with value2, on the top of the stack. */
  712. gen_conversion (ax, value2->type, target);
  713. /* Deal with value1, not on the top of the stack. Don't
  714. generate the `swap' instructions if we're not actually going
  715. to do anything. */
  716. if (is_nontrivial_conversion (value1->type, target))
  717. {
  718. ax_simple (ax, aop_swap);
  719. gen_conversion (ax, value1->type, target);
  720. ax_simple (ax, aop_swap);
  721. }
  722. value1->type = value2->type = check_typedef (target);
  723. }
  724. }
  725. /* Generate code to perform the integral promotions (ANSI 6.2.1.1) on
  726. the value on the top of the stack, as described by VALUE. Assume
  727. the value has integral type. */
  728. static void
  729. gen_integral_promotions (struct agent_expr *ax, struct axs_value *value)
  730. {
  731. const struct builtin_type *builtin = builtin_type (ax->gdbarch);
  732. if (!type_wider_than (value->type, builtin->builtin_int))
  733. {
  734. gen_conversion (ax, value->type, builtin->builtin_int);
  735. value->type = builtin->builtin_int;
  736. }
  737. else if (!type_wider_than (value->type, builtin->builtin_unsigned_int))
  738. {
  739. gen_conversion (ax, value->type, builtin->builtin_unsigned_int);
  740. value->type = builtin->builtin_unsigned_int;
  741. }
  742. }
  743. /* Generate code for a cast to TYPE. */
  744. static void
  745. gen_cast (struct agent_expr *ax, struct axs_value *value, struct type *type)
  746. {
  747. /* GCC does allow casts to yield lvalues, so this should be fixed
  748. before merging these changes into the trunk. */
  749. require_rvalue (ax, value);
  750. /* Dereference typedefs. */
  751. type = check_typedef (type);
  752. switch (type->code ())
  753. {
  754. case TYPE_CODE_PTR:
  755. case TYPE_CODE_REF:
  756. case TYPE_CODE_RVALUE_REF:
  757. /* It's implementation-defined, and I'll bet this is what GCC
  758. does. */
  759. break;
  760. case TYPE_CODE_ARRAY:
  761. case TYPE_CODE_STRUCT:
  762. case TYPE_CODE_UNION:
  763. case TYPE_CODE_FUNC:
  764. error (_("Invalid type cast: intended type must be scalar."));
  765. case TYPE_CODE_ENUM:
  766. case TYPE_CODE_BOOL:
  767. /* We don't have to worry about the size of the value, because
  768. all our integral values are fully sign-extended, and when
  769. casting pointers we can do anything we like. Is there any
  770. way for us to know what GCC actually does with a cast like
  771. this? */
  772. break;
  773. case TYPE_CODE_INT:
  774. gen_conversion (ax, value->type, type);
  775. break;
  776. case TYPE_CODE_VOID:
  777. /* We could pop the value, and rely on everyone else to check
  778. the type and notice that this value doesn't occupy a stack
  779. slot. But for now, leave the value on the stack, and
  780. preserve the "value == stack element" assumption. */
  781. break;
  782. default:
  783. error (_("Casts to requested type are not yet implemented."));
  784. }
  785. value->type = type;
  786. }
  787. /* Generating bytecode from GDB expressions: arithmetic */
  788. /* Scale the integer on the top of the stack by the size of the target
  789. of the pointer type TYPE. */
  790. static void
  791. gen_scale (struct agent_expr *ax, enum agent_op op, struct type *type)
  792. {
  793. struct type *element = TYPE_TARGET_TYPE (type);
  794. if (TYPE_LENGTH (element) != 1)
  795. {
  796. ax_const_l (ax, TYPE_LENGTH (element));
  797. ax_simple (ax, op);
  798. }
  799. }
  800. /* Generate code for pointer arithmetic PTR + INT. */
  801. static void
  802. gen_ptradd (struct agent_expr *ax, struct axs_value *value,
  803. struct axs_value *value1, struct axs_value *value2)
  804. {
  805. gdb_assert (value1->type->is_pointer_or_reference ());
  806. gdb_assert (value2->type->code () == TYPE_CODE_INT);
  807. gen_scale (ax, aop_mul, value1->type);
  808. ax_simple (ax, aop_add);
  809. gen_extend (ax, value1->type); /* Catch overflow. */
  810. value->type = value1->type;
  811. value->kind = axs_rvalue;
  812. }
  813. /* Generate code for pointer arithmetic PTR - INT. */
  814. static void
  815. gen_ptrsub (struct agent_expr *ax, struct axs_value *value,
  816. struct axs_value *value1, struct axs_value *value2)
  817. {
  818. gdb_assert (value1->type->is_pointer_or_reference ());
  819. gdb_assert (value2->type->code () == TYPE_CODE_INT);
  820. gen_scale (ax, aop_mul, value1->type);
  821. ax_simple (ax, aop_sub);
  822. gen_extend (ax, value1->type); /* Catch overflow. */
  823. value->type = value1->type;
  824. value->kind = axs_rvalue;
  825. }
  826. /* Generate code for pointer arithmetic PTR - PTR. */
  827. static void
  828. gen_ptrdiff (struct agent_expr *ax, struct axs_value *value,
  829. struct axs_value *value1, struct axs_value *value2,
  830. struct type *result_type)
  831. {
  832. gdb_assert (value1->type->is_pointer_or_reference ());
  833. gdb_assert (value2->type->is_pointer_or_reference ());
  834. if (TYPE_LENGTH (TYPE_TARGET_TYPE (value1->type))
  835. != TYPE_LENGTH (TYPE_TARGET_TYPE (value2->type)))
  836. error (_("\
  837. First argument of `-' is a pointer, but second argument is neither\n\
  838. an integer nor a pointer of the same type."));
  839. ax_simple (ax, aop_sub);
  840. gen_scale (ax, aop_div_unsigned, value1->type);
  841. value->type = result_type;
  842. value->kind = axs_rvalue;
  843. }
  844. static void
  845. gen_equal (struct agent_expr *ax, struct axs_value *value,
  846. struct axs_value *value1, struct axs_value *value2,
  847. struct type *result_type)
  848. {
  849. if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
  850. ax_simple (ax, aop_equal);
  851. else
  852. gen_binop (ax, value, value1, value2,
  853. aop_equal, aop_equal, 0, "equal");
  854. value->type = result_type;
  855. value->kind = axs_rvalue;
  856. }
  857. static void
  858. gen_less (struct agent_expr *ax, struct axs_value *value,
  859. struct axs_value *value1, struct axs_value *value2,
  860. struct type *result_type)
  861. {
  862. if (value1->type->is_pointer_or_reference () || value2->type->is_pointer_or_reference ())
  863. ax_simple (ax, aop_less_unsigned);
  864. else
  865. gen_binop (ax, value, value1, value2,
  866. aop_less_signed, aop_less_unsigned, 0, "less than");
  867. value->type = result_type;
  868. value->kind = axs_rvalue;
  869. }
  870. /* Generate code for a binary operator that doesn't do pointer magic.
  871. We set VALUE to describe the result value; we assume VALUE1 and
  872. VALUE2 describe the two operands, and that they've undergone the
  873. usual binary conversions. MAY_CARRY should be non-zero iff the
  874. result needs to be extended. NAME is the English name of the
  875. operator, used in error messages */
  876. static void
  877. gen_binop (struct agent_expr *ax, struct axs_value *value,
  878. struct axs_value *value1, struct axs_value *value2,
  879. enum agent_op op, enum agent_op op_unsigned,
  880. int may_carry, const char *name)
  881. {
  882. /* We only handle INT op INT. */
  883. if ((value1->type->code () != TYPE_CODE_INT)
  884. || (value2->type->code () != TYPE_CODE_INT))
  885. error (_("Invalid combination of types in %s."), name);
  886. ax_simple (ax, value1->type->is_unsigned () ? op_unsigned : op);
  887. if (may_carry)
  888. gen_extend (ax, value1->type); /* catch overflow */
  889. value->type = value1->type;
  890. value->kind = axs_rvalue;
  891. }
  892. static void
  893. gen_logical_not (struct agent_expr *ax, struct axs_value *value,
  894. struct type *result_type)
  895. {
  896. if (value->type->code () != TYPE_CODE_INT
  897. && value->type->code () != TYPE_CODE_PTR)
  898. error (_("Invalid type of operand to `!'."));
  899. ax_simple (ax, aop_log_not);
  900. value->type = result_type;
  901. }
  902. static void
  903. gen_complement (struct agent_expr *ax, struct axs_value *value)
  904. {
  905. if (value->type->code () != TYPE_CODE_INT)
  906. error (_("Invalid type of operand to `~'."));
  907. ax_simple (ax, aop_bit_not);
  908. gen_extend (ax, value->type);
  909. }
  910. /* Generating bytecode from GDB expressions: * & . -> @ sizeof */
  911. /* Dereference the value on the top of the stack. */
  912. static void
  913. gen_deref (struct axs_value *value)
  914. {
  915. /* The caller should check the type, because several operators use
  916. this, and we don't know what error message to generate. */
  917. if (!value->type->is_pointer_or_reference ())
  918. internal_error (__FILE__, __LINE__,
  919. _("gen_deref: expected a pointer"));
  920. /* We've got an rvalue now, which is a pointer. We want to yield an
  921. lvalue, whose address is exactly that pointer. So we don't
  922. actually emit any code; we just change the type from "Pointer to
  923. T" to "T", and mark the value as an lvalue in memory. Leave it
  924. to the consumer to actually dereference it. */
  925. value->type = check_typedef (TYPE_TARGET_TYPE (value->type));
  926. if (value->type->code () == TYPE_CODE_VOID)
  927. error (_("Attempt to dereference a generic pointer."));
  928. value->kind = ((value->type->code () == TYPE_CODE_FUNC)
  929. ? axs_rvalue : axs_lvalue_memory);
  930. }
  931. /* Produce the address of the lvalue on the top of the stack. */
  932. static void
  933. gen_address_of (struct axs_value *value)
  934. {
  935. /* Special case for taking the address of a function. The ANSI
  936. standard describes this as a special case, too, so this
  937. arrangement is not without motivation. */
  938. if (value->type->code () == TYPE_CODE_FUNC)
  939. /* The value's already an rvalue on the stack, so we just need to
  940. change the type. */
  941. value->type = lookup_pointer_type (value->type);
  942. else
  943. switch (value->kind)
  944. {
  945. case axs_rvalue:
  946. error (_("Operand of `&' is an rvalue, which has no address."));
  947. case axs_lvalue_register:
  948. error (_("Operand of `&' is in a register, and has no address."));
  949. case axs_lvalue_memory:
  950. value->kind = axs_rvalue;
  951. value->type = lookup_pointer_type (value->type);
  952. break;
  953. }
  954. }
  955. /* Generate code to push the value of a bitfield of a structure whose
  956. address is on the top of the stack. START and END give the
  957. starting and one-past-ending *bit* numbers of the field within the
  958. structure. */
  959. static void
  960. gen_bitfield_ref (struct agent_expr *ax, struct axs_value *value,
  961. struct type *type, int start, int end)
  962. {
  963. /* Note that ops[i] fetches 8 << i bits. */
  964. static enum agent_op ops[]
  965. = {aop_ref8, aop_ref16, aop_ref32, aop_ref64};
  966. static int num_ops = (sizeof (ops) / sizeof (ops[0]));
  967. /* We don't want to touch any byte that the bitfield doesn't
  968. actually occupy; we shouldn't make any accesses we're not
  969. explicitly permitted to. We rely here on the fact that the
  970. bytecode `ref' operators work on unaligned addresses.
  971. It takes some fancy footwork to get the stack to work the way
  972. we'd like. Say we're retrieving a bitfield that requires three
  973. fetches. Initially, the stack just contains the address:
  974. addr
  975. For the first fetch, we duplicate the address
  976. addr addr
  977. then add the byte offset, do the fetch, and shift and mask as
  978. needed, yielding a fragment of the value, properly aligned for
  979. the final bitwise or:
  980. addr frag1
  981. then we swap, and repeat the process:
  982. frag1 addr --- address on top
  983. frag1 addr addr --- duplicate it
  984. frag1 addr frag2 --- get second fragment
  985. frag1 frag2 addr --- swap again
  986. frag1 frag2 frag3 --- get third fragment
  987. Notice that, since the third fragment is the last one, we don't
  988. bother duplicating the address this time. Now we have all the
  989. fragments on the stack, and we can simply `or' them together,
  990. yielding the final value of the bitfield. */
  991. /* The first and one-after-last bits in the field, but rounded down
  992. and up to byte boundaries. */
  993. int bound_start = (start / TARGET_CHAR_BIT) * TARGET_CHAR_BIT;
  994. int bound_end = (((end + TARGET_CHAR_BIT - 1)
  995. / TARGET_CHAR_BIT)
  996. * TARGET_CHAR_BIT);
  997. /* current bit offset within the structure */
  998. int offset;
  999. /* The index in ops of the opcode we're considering. */
  1000. int op;
  1001. /* The number of fragments we generated in the process. Probably
  1002. equal to the number of `one' bits in bytesize, but who cares? */
  1003. int fragment_count;
  1004. /* Dereference any typedefs. */
  1005. type = check_typedef (type);
  1006. /* Can we fetch the number of bits requested at all? */
  1007. if ((end - start) > ((1 << num_ops) * 8))
  1008. internal_error (__FILE__, __LINE__,
  1009. _("gen_bitfield_ref: bitfield too wide"));
  1010. /* Note that we know here that we only need to try each opcode once.
  1011. That may not be true on machines with weird byte sizes. */
  1012. offset = bound_start;
  1013. fragment_count = 0;
  1014. for (op = num_ops - 1; op >= 0; op--)
  1015. {
  1016. /* number of bits that ops[op] would fetch */
  1017. int op_size = 8 << op;
  1018. /* The stack at this point, from bottom to top, contains zero or
  1019. more fragments, then the address. */
  1020. /* Does this fetch fit within the bitfield? */
  1021. if (offset + op_size <= bound_end)
  1022. {
  1023. /* Is this the last fragment? */
  1024. int last_frag = (offset + op_size == bound_end);
  1025. if (!last_frag)
  1026. ax_simple (ax, aop_dup); /* keep a copy of the address */
  1027. /* Add the offset. */
  1028. gen_offset (ax, offset / TARGET_CHAR_BIT);
  1029. if (ax->tracing)
  1030. {
  1031. /* Record the area of memory we're about to fetch. */
  1032. ax_trace_quick (ax, op_size / TARGET_CHAR_BIT);
  1033. }
  1034. /* Perform the fetch. */
  1035. ax_simple (ax, ops[op]);
  1036. /* Shift the bits we have to their proper position.
  1037. gen_left_shift will generate right shifts when the operand
  1038. is negative.
  1039. A big-endian field diagram to ponder:
  1040. byte 0 byte 1 byte 2 byte 3 byte 4 byte 5 byte 6 byte 7
  1041. +------++------++------++------++------++------++------++------+
  1042. xxxxAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCxxxxxxxxxxx
  1043. ^ ^ ^ ^
  1044. bit number 16 32 48 53
  1045. These are bit numbers as supplied by GDB. Note that the
  1046. bit numbers run from right to left once you've fetched the
  1047. value!
  1048. A little-endian field diagram to ponder:
  1049. byte 7 byte 6 byte 5 byte 4 byte 3 byte 2 byte 1 byte 0
  1050. +------++------++------++------++------++------++------++------+
  1051. xxxxxxxxxxxAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCCCCCCCCCCCCCxxxx
  1052. ^ ^ ^ ^ ^
  1053. bit number 48 32 16 4 0
  1054. In both cases, the most significant end is on the left
  1055. (i.e. normal numeric writing order), which means that you
  1056. don't go crazy thinking about `left' and `right' shifts.
  1057. We don't have to worry about masking yet:
  1058. - If they contain garbage off the least significant end, then we
  1059. must be looking at the low end of the field, and the right
  1060. shift will wipe them out.
  1061. - If they contain garbage off the most significant end, then we
  1062. must be looking at the most significant end of the word, and
  1063. the sign/zero extension will wipe them out.
  1064. - If we're in the interior of the word, then there is no garbage
  1065. on either end, because the ref operators zero-extend. */
  1066. if (gdbarch_byte_order (ax->gdbarch) == BFD_ENDIAN_BIG)
  1067. gen_left_shift (ax, end - (offset + op_size));
  1068. else
  1069. gen_left_shift (ax, offset - start);
  1070. if (!last_frag)
  1071. /* Bring the copy of the address up to the top. */
  1072. ax_simple (ax, aop_swap);
  1073. offset += op_size;
  1074. fragment_count++;
  1075. }
  1076. }
  1077. /* Generate enough bitwise `or' operations to combine all the
  1078. fragments we left on the stack. */
  1079. while (fragment_count-- > 1)
  1080. ax_simple (ax, aop_bit_or);
  1081. /* Sign- or zero-extend the value as appropriate. */
  1082. ((type->is_unsigned () ? ax_zero_ext : ax_ext) (ax, end - start));
  1083. /* This is *not* an lvalue. Ugh. */
  1084. value->kind = axs_rvalue;
  1085. value->type = type;
  1086. }
  1087. /* Generate bytecodes for field number FIELDNO of type TYPE. OFFSET
  1088. is an accumulated offset (in bytes), will be nonzero for objects
  1089. embedded in other objects, like C++ base classes. Behavior should
  1090. generally follow value_primitive_field. */
  1091. static void
  1092. gen_primitive_field (struct agent_expr *ax, struct axs_value *value,
  1093. int offset, int fieldno, struct type *type)
  1094. {
  1095. /* Is this a bitfield? */
  1096. if (TYPE_FIELD_PACKED (type, fieldno))
  1097. gen_bitfield_ref (ax, value, type->field (fieldno).type (),
  1098. (offset * TARGET_CHAR_BIT
  1099. + type->field (fieldno).loc_bitpos ()),
  1100. (offset * TARGET_CHAR_BIT
  1101. + type->field (fieldno).loc_bitpos ()
  1102. + TYPE_FIELD_BITSIZE (type, fieldno)));
  1103. else
  1104. {
  1105. gen_offset (ax, offset
  1106. + type->field (fieldno).loc_bitpos () / TARGET_CHAR_BIT);
  1107. value->kind = axs_lvalue_memory;
  1108. value->type = type->field (fieldno).type ();
  1109. }
  1110. }
  1111. /* Search for the given field in either the given type or one of its
  1112. base classes. Return 1 if found, 0 if not. */
  1113. static int
  1114. gen_struct_ref_recursive (struct agent_expr *ax, struct axs_value *value,
  1115. const char *field, int offset, struct type *type)
  1116. {
  1117. int i, rslt;
  1118. int nbases = TYPE_N_BASECLASSES (type);
  1119. type = check_typedef (type);
  1120. for (i = type->num_fields () - 1; i >= nbases; i--)
  1121. {
  1122. const char *this_name = type->field (i).name ();
  1123. if (this_name)
  1124. {
  1125. if (strcmp (field, this_name) == 0)
  1126. {
  1127. /* Note that bytecodes for the struct's base (aka
  1128. "this") will have been generated already, which will
  1129. be unnecessary but not harmful if the static field is
  1130. being handled as a global. */
  1131. if (field_is_static (&type->field (i)))
  1132. {
  1133. gen_static_field (ax, value, type, i);
  1134. if (value->optimized_out)
  1135. error (_("static field `%s' has been "
  1136. "optimized out, cannot use"),
  1137. field);
  1138. return 1;
  1139. }
  1140. gen_primitive_field (ax, value, offset, i, type);
  1141. return 1;
  1142. }
  1143. #if 0 /* is this right? */
  1144. if (this_name[0] == '\0')
  1145. internal_error (__FILE__, __LINE__,
  1146. _("find_field: anonymous unions not supported"));
  1147. #endif
  1148. }
  1149. }
  1150. /* Now scan through base classes recursively. */
  1151. for (i = 0; i < nbases; i++)
  1152. {
  1153. struct type *basetype = check_typedef (TYPE_BASECLASS (type, i));
  1154. rslt = gen_struct_ref_recursive (ax, value, field,
  1155. offset + TYPE_BASECLASS_BITPOS (type, i)
  1156. / TARGET_CHAR_BIT,
  1157. basetype);
  1158. if (rslt)
  1159. return 1;
  1160. }
  1161. /* Not found anywhere, flag so caller can complain. */
  1162. return 0;
  1163. }
  1164. /* Generate code to reference the member named FIELD of a structure or
  1165. union. The top of the stack, as described by VALUE, should have
  1166. type (pointer to a)* struct/union. OPERATOR_NAME is the name of
  1167. the operator being compiled, and OPERAND_NAME is the kind of thing
  1168. it operates on; we use them in error messages. */
  1169. static void
  1170. gen_struct_ref (struct agent_expr *ax, struct axs_value *value,
  1171. const char *field, const char *operator_name,
  1172. const char *operand_name)
  1173. {
  1174. struct type *type;
  1175. int found;
  1176. /* Follow pointers until we reach a non-pointer. These aren't the C
  1177. semantics, but they're what the normal GDB evaluator does, so we
  1178. should at least be consistent. */
  1179. while (value->type->is_pointer_or_reference ())
  1180. {
  1181. require_rvalue (ax, value);
  1182. gen_deref (value);
  1183. }
  1184. type = check_typedef (value->type);
  1185. /* This must yield a structure or a union. */
  1186. if (type->code () != TYPE_CODE_STRUCT
  1187. && type->code () != TYPE_CODE_UNION)
  1188. error (_("The left operand of `%s' is not a %s."),
  1189. operator_name, operand_name);
  1190. /* And it must be in memory; we don't deal with structure rvalues,
  1191. or structures living in registers. */
  1192. if (value->kind != axs_lvalue_memory)
  1193. error (_("Structure does not live in memory."));
  1194. /* Search through fields and base classes recursively. */
  1195. found = gen_struct_ref_recursive (ax, value, field, 0, type);
  1196. if (!found)
  1197. error (_("Couldn't find member named `%s' in struct/union/class `%s'"),
  1198. field, type->name ());
  1199. }
  1200. static int
  1201. gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
  1202. const struct type *curtype, const char *name);
  1203. static int
  1204. gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
  1205. const struct type *curtype, const char *name);
  1206. static void
  1207. gen_static_field (struct agent_expr *ax, struct axs_value *value,
  1208. struct type *type, int fieldno)
  1209. {
  1210. if (type->field (fieldno).loc_kind () == FIELD_LOC_KIND_PHYSADDR)
  1211. {
  1212. ax_const_l (ax, type->field (fieldno).loc_physaddr ());
  1213. value->kind = axs_lvalue_memory;
  1214. value->type = type->field (fieldno).type ();
  1215. value->optimized_out = 0;
  1216. }
  1217. else
  1218. {
  1219. const char *phys_name = type->field (fieldno).loc_physname ();
  1220. struct symbol *sym = lookup_symbol (phys_name, 0, VAR_DOMAIN, 0).symbol;
  1221. if (sym)
  1222. {
  1223. gen_var_ref (ax, value, sym);
  1224. /* Don't error if the value was optimized out, we may be
  1225. scanning all static fields and just want to pass over this
  1226. and continue with the rest. */
  1227. }
  1228. else
  1229. {
  1230. /* Silently assume this was optimized out; class printing
  1231. will let the user know why the data is missing. */
  1232. value->optimized_out = 1;
  1233. }
  1234. }
  1235. }
  1236. static int
  1237. gen_struct_elt_for_reference (struct agent_expr *ax, struct axs_value *value,
  1238. struct type *type, const char *fieldname)
  1239. {
  1240. struct type *t = type;
  1241. int i;
  1242. if (t->code () != TYPE_CODE_STRUCT
  1243. && t->code () != TYPE_CODE_UNION)
  1244. internal_error (__FILE__, __LINE__,
  1245. _("non-aggregate type to gen_struct_elt_for_reference"));
  1246. for (i = t->num_fields () - 1; i >= TYPE_N_BASECLASSES (t); i--)
  1247. {
  1248. const char *t_field_name = t->field (i).name ();
  1249. if (t_field_name && strcmp (t_field_name, fieldname) == 0)
  1250. {
  1251. if (field_is_static (&t->field (i)))
  1252. {
  1253. gen_static_field (ax, value, t, i);
  1254. if (value->optimized_out)
  1255. error (_("static field `%s' has been "
  1256. "optimized out, cannot use"),
  1257. fieldname);
  1258. return 1;
  1259. }
  1260. if (TYPE_FIELD_PACKED (t, i))
  1261. error (_("pointers to bitfield members not allowed"));
  1262. /* FIXME we need a way to do "want_address" equivalent */
  1263. error (_("Cannot reference non-static field \"%s\""), fieldname);
  1264. }
  1265. }
  1266. /* FIXME add other scoped-reference cases here */
  1267. /* Do a last-ditch lookup. */
  1268. return gen_maybe_namespace_elt (ax, value, type, fieldname);
  1269. }
  1270. /* C++: Return the member NAME of the namespace given by the type
  1271. CURTYPE. */
  1272. static int
  1273. gen_namespace_elt (struct agent_expr *ax, struct axs_value *value,
  1274. const struct type *curtype, const char *name)
  1275. {
  1276. int found = gen_maybe_namespace_elt (ax, value, curtype, name);
  1277. if (!found)
  1278. error (_("No symbol \"%s\" in namespace \"%s\"."),
  1279. name, curtype->name ());
  1280. return found;
  1281. }
  1282. /* A helper function used by value_namespace_elt and
  1283. value_struct_elt_for_reference. It looks up NAME inside the
  1284. context CURTYPE; this works if CURTYPE is a namespace or if CURTYPE
  1285. is a class and NAME refers to a type in CURTYPE itself (as opposed
  1286. to, say, some base class of CURTYPE). */
  1287. static int
  1288. gen_maybe_namespace_elt (struct agent_expr *ax, struct axs_value *value,
  1289. const struct type *curtype, const char *name)
  1290. {
  1291. const char *namespace_name = curtype->name ();
  1292. struct block_symbol sym;
  1293. sym = cp_lookup_symbol_namespace (namespace_name, name,
  1294. block_for_pc (ax->scope),
  1295. VAR_DOMAIN);
  1296. if (sym.symbol == NULL)
  1297. return 0;
  1298. gen_var_ref (ax, value, sym.symbol);
  1299. if (value->optimized_out)
  1300. error (_("`%s' has been optimized out, cannot use"),
  1301. sym.symbol->print_name ());
  1302. return 1;
  1303. }
  1304. static int
  1305. gen_aggregate_elt_ref (struct agent_expr *ax, struct axs_value *value,
  1306. struct type *type, const char *field)
  1307. {
  1308. switch (type->code ())
  1309. {
  1310. case TYPE_CODE_STRUCT:
  1311. case TYPE_CODE_UNION:
  1312. return gen_struct_elt_for_reference (ax, value, type, field);
  1313. break;
  1314. case TYPE_CODE_NAMESPACE:
  1315. return gen_namespace_elt (ax, value, type, field);
  1316. break;
  1317. default:
  1318. internal_error (__FILE__, __LINE__,
  1319. _("non-aggregate type in gen_aggregate_elt_ref"));
  1320. }
  1321. return 0;
  1322. }
  1323. namespace expr
  1324. {
  1325. void
  1326. operation::generate_ax (struct expression *exp,
  1327. struct agent_expr *ax,
  1328. struct axs_value *value,
  1329. struct type *cast_type)
  1330. {
  1331. if (constant_p ())
  1332. {
  1333. struct value *v = evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
  1334. ax_const_l (ax, value_as_long (v));
  1335. value->kind = axs_rvalue;
  1336. value->type = check_typedef (value_type (v));
  1337. }
  1338. else
  1339. {
  1340. do_generate_ax (exp, ax, value, cast_type);
  1341. if (cast_type != nullptr)
  1342. gen_cast (ax, value, cast_type);
  1343. }
  1344. }
  1345. void
  1346. scope_operation::do_generate_ax (struct expression *exp,
  1347. struct agent_expr *ax,
  1348. struct axs_value *value,
  1349. struct type *cast_type)
  1350. {
  1351. struct type *type = std::get<0> (m_storage);
  1352. const std::string &name = std::get<1> (m_storage);
  1353. int found = gen_aggregate_elt_ref (ax, value, type, name.c_str ());
  1354. if (!found)
  1355. error (_("There is no field named %s"), name.c_str ());
  1356. }
  1357. void
  1358. long_const_operation::do_generate_ax (struct expression *exp,
  1359. struct agent_expr *ax,
  1360. struct axs_value *value,
  1361. struct type *cast_type)
  1362. {
  1363. gen_int_literal (ax, value, std::get<1> (m_storage),
  1364. std::get<0> (m_storage));
  1365. }
  1366. void
  1367. var_msym_value_operation::do_generate_ax (struct expression *exp,
  1368. struct agent_expr *ax,
  1369. struct axs_value *value,
  1370. struct type *cast_type)
  1371. {
  1372. const bound_minimal_symbol &b = std::get<0> (m_storage);
  1373. gen_msym_var_ref (ax, value, b.minsym, b.objfile);
  1374. if (value->type->code () == TYPE_CODE_ERROR)
  1375. {
  1376. if (cast_type == nullptr)
  1377. error_unknown_type (b.minsym->linkage_name ());
  1378. value->type = cast_type;
  1379. }
  1380. }
  1381. void
  1382. register_operation::do_generate_ax (struct expression *exp,
  1383. struct agent_expr *ax,
  1384. struct axs_value *value,
  1385. struct type *cast_type)
  1386. {
  1387. const char *name = std::get<0> (m_storage).c_str ();
  1388. int len = std::get<0> (m_storage).size ();
  1389. int reg;
  1390. reg = user_reg_map_name_to_regnum (ax->gdbarch, name, len);
  1391. if (reg == -1)
  1392. internal_error (__FILE__, __LINE__,
  1393. _("Register $%s not available"), name);
  1394. /* No support for tracing user registers yet. */
  1395. if (reg >= gdbarch_num_cooked_regs (ax->gdbarch))
  1396. error (_("'%s' is a user-register; "
  1397. "GDB cannot yet trace user-register contents."),
  1398. name);
  1399. value->kind = axs_lvalue_register;
  1400. value->u.reg = reg;
  1401. value->type = register_type (ax->gdbarch, reg);
  1402. }
  1403. void
  1404. internalvar_operation::do_generate_ax (struct expression *exp,
  1405. struct agent_expr *ax,
  1406. struct axs_value *value,
  1407. struct type *cast_type)
  1408. {
  1409. struct internalvar *var = std::get<0> (m_storage);
  1410. const char *name = internalvar_name (var);
  1411. struct trace_state_variable *tsv;
  1412. tsv = find_trace_state_variable (name);
  1413. if (tsv)
  1414. {
  1415. ax_tsv (ax, aop_getv, tsv->number);
  1416. if (ax->tracing)
  1417. ax_tsv (ax, aop_tracev, tsv->number);
  1418. /* Trace state variables are always 64-bit integers. */
  1419. value->kind = axs_rvalue;
  1420. value->type = builtin_type (ax->gdbarch)->builtin_long_long;
  1421. }
  1422. else if (! compile_internalvar_to_ax (var, ax, value))
  1423. error (_("$%s is not a trace state variable; GDB agent "
  1424. "expressions cannot use convenience variables."), name);
  1425. }
  1426. void
  1427. ternop_cond_operation::do_generate_ax (struct expression *exp,
  1428. struct agent_expr *ax,
  1429. struct axs_value *value,
  1430. struct type *cast_type)
  1431. {
  1432. struct axs_value value1, value2, value3;
  1433. int if1, end;
  1434. std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
  1435. gen_usual_unary (ax, &value1);
  1436. /* For (A ? B : C), it's easiest to generate subexpression
  1437. bytecodes in order, but if_goto jumps on true, so we invert
  1438. the sense of A. Then we can do B by dropping through, and
  1439. jump to do C. */
  1440. gen_logical_not (ax, &value1, builtin_type (ax->gdbarch)->builtin_int);
  1441. if1 = ax_goto (ax, aop_if_goto);
  1442. std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
  1443. gen_usual_unary (ax, &value2);
  1444. end = ax_goto (ax, aop_goto);
  1445. ax_label (ax, if1, ax->len);
  1446. std::get<2> (m_storage)->generate_ax (exp, ax, &value3);
  1447. gen_usual_unary (ax, &value3);
  1448. ax_label (ax, end, ax->len);
  1449. /* This is arbitrary - what if B and C are incompatible types? */
  1450. value->type = value2.type;
  1451. value->kind = value2.kind;
  1452. }
  1453. /* Generate code for GDB's magical `repeat' operator.
  1454. LVALUE @ INT creates an array INT elements long, and whose elements
  1455. have the same type as LVALUE, located in memory so that LVALUE is
  1456. its first element. For example, argv[0]@argc gives you the array
  1457. of command-line arguments.
  1458. Unfortunately, because we have to know the types before we actually
  1459. have a value for the expression, we can't implement this perfectly
  1460. without changing the type system, having values that occupy two
  1461. stack slots, doing weird things with sizeof, etc. So we require
  1462. the right operand to be a constant expression. */
  1463. void
  1464. repeat_operation::do_generate_ax (struct expression *exp,
  1465. struct agent_expr *ax,
  1466. struct axs_value *value,
  1467. struct type *cast_type)
  1468. {
  1469. struct axs_value value1;
  1470. /* We don't want to turn this into an rvalue, so no conversions
  1471. here. */
  1472. std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
  1473. if (value1.kind != axs_lvalue_memory)
  1474. error (_("Left operand of `@' must be an object in memory."));
  1475. /* Evaluate the length; it had better be a constant. */
  1476. if (!std::get<1> (m_storage)->constant_p ())
  1477. error (_("Right operand of `@' must be a "
  1478. "constant, in agent expressions."));
  1479. struct value *v
  1480. = std::get<1> (m_storage)->evaluate (nullptr, exp,
  1481. EVAL_AVOID_SIDE_EFFECTS);
  1482. if (value_type (v)->code () != TYPE_CODE_INT)
  1483. error (_("Right operand of `@' must be an integer."));
  1484. int length = value_as_long (v);
  1485. if (length <= 0)
  1486. error (_("Right operand of `@' must be positive."));
  1487. /* The top of the stack is already the address of the object, so
  1488. all we need to do is frob the type of the lvalue. */
  1489. /* FIXME-type-allocation: need a way to free this type when we are
  1490. done with it. */
  1491. struct type *array
  1492. = lookup_array_range_type (value1.type, 0, length - 1);
  1493. value->kind = axs_lvalue_memory;
  1494. value->type = array;
  1495. }
  1496. void
  1497. comma_operation::do_generate_ax (struct expression *exp,
  1498. struct agent_expr *ax,
  1499. struct axs_value *value,
  1500. struct type *cast_type)
  1501. {
  1502. /* Note that we need to be a little subtle about generating code
  1503. for comma. In C, we can do some optimizations here because
  1504. we know the left operand is only being evaluated for effect.
  1505. However, if the tracing kludge is in effect, then we always
  1506. need to evaluate the left hand side fully, so that all the
  1507. variables it mentions get traced. */
  1508. struct axs_value value1;
  1509. std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
  1510. /* Don't just dispose of the left operand. We might be tracing,
  1511. in which case we want to emit code to trace it if it's an
  1512. lvalue. */
  1513. gen_traced_pop (ax, &value1);
  1514. std::get<1> (m_storage)->generate_ax (exp, ax, value);
  1515. /* It's the consumer's responsibility to trace the right operand. */
  1516. }
  1517. void
  1518. unop_sizeof_operation::do_generate_ax (struct expression *exp,
  1519. struct agent_expr *ax,
  1520. struct axs_value *value,
  1521. struct type *cast_type)
  1522. {
  1523. /* We don't care about the value of the operand expression; we only
  1524. care about its type. However, in the current arrangement, the
  1525. only way to find an expression's type is to generate code for it.
  1526. So we generate code for the operand, and then throw it away,
  1527. replacing it with code that simply pushes its size. */
  1528. int start = ax->len;
  1529. std::get<0> (m_storage)->generate_ax (exp, ax, value);
  1530. /* Throw away the code we just generated. */
  1531. ax->len = start;
  1532. ax_const_l (ax, TYPE_LENGTH (value->type));
  1533. value->kind = axs_rvalue;
  1534. value->type = builtin_type (ax->gdbarch)->builtin_int;
  1535. }
  1536. void
  1537. unop_cast_operation::do_generate_ax (struct expression *exp,
  1538. struct agent_expr *ax,
  1539. struct axs_value *value,
  1540. struct type *cast_type)
  1541. {
  1542. std::get<0> (m_storage)->generate_ax (exp, ax, value,
  1543. std::get<1> (m_storage));
  1544. }
  1545. void
  1546. unop_extract_operation::do_generate_ax (struct expression *exp,
  1547. struct agent_expr *ax,
  1548. struct axs_value *value,
  1549. struct type *cast_type)
  1550. {
  1551. std::get<0> (m_storage)->generate_ax (exp, ax, value);
  1552. struct type *to_type = get_type ();
  1553. if (!is_scalar_type (to_type))
  1554. error (_("can't generate agent expression to extract non-scalar type"));
  1555. if (to_type->is_unsigned ())
  1556. gen_extend (ax, to_type);
  1557. else
  1558. gen_sign_extend (ax, to_type);
  1559. }
  1560. void
  1561. unop_memval_operation::do_generate_ax (struct expression *exp,
  1562. struct agent_expr *ax,
  1563. struct axs_value *value,
  1564. struct type *cast_type)
  1565. {
  1566. std::get<0> (m_storage)->generate_ax (exp, ax, value);
  1567. /* If we have an axs_rvalue or an axs_lvalue_memory, then we
  1568. already have the right value on the stack. For
  1569. axs_lvalue_register, we must convert. */
  1570. if (value->kind == axs_lvalue_register)
  1571. require_rvalue (ax, value);
  1572. value->type = std::get<1> (m_storage);
  1573. value->kind = axs_lvalue_memory;
  1574. }
  1575. void
  1576. unop_memval_type_operation::do_generate_ax (struct expression *exp,
  1577. struct agent_expr *ax,
  1578. struct axs_value *value,
  1579. struct type *cast_type)
  1580. {
  1581. struct value *val
  1582. = std::get<0> (m_storage)->evaluate (nullptr, exp,
  1583. EVAL_AVOID_SIDE_EFFECTS);
  1584. struct type *type = value_type (val);
  1585. std::get<1> (m_storage)->generate_ax (exp, ax, value);
  1586. /* If we have an axs_rvalue or an axs_lvalue_memory, then we
  1587. already have the right value on the stack. For
  1588. axs_lvalue_register, we must convert. */
  1589. if (value->kind == axs_lvalue_register)
  1590. require_rvalue (ax, value);
  1591. value->type = type;
  1592. value->kind = axs_lvalue_memory;
  1593. }
  1594. void
  1595. op_this_operation::do_generate_ax (struct expression *exp,
  1596. struct agent_expr *ax,
  1597. struct axs_value *value,
  1598. struct type *cast_type)
  1599. {
  1600. struct symbol *sym, *func;
  1601. const struct block *b;
  1602. const struct language_defn *lang;
  1603. b = block_for_pc (ax->scope);
  1604. func = block_linkage_function (b);
  1605. lang = language_def (func->language ());
  1606. sym = lookup_language_this (lang, b).symbol;
  1607. if (!sym)
  1608. error (_("no `%s' found"), lang->name_of_this ());
  1609. gen_var_ref (ax, value, sym);
  1610. if (value->optimized_out)
  1611. error (_("`%s' has been optimized out, cannot use"),
  1612. sym->print_name ());
  1613. }
  1614. void
  1615. assign_operation::do_generate_ax (struct expression *exp,
  1616. struct agent_expr *ax,
  1617. struct axs_value *value,
  1618. struct type *cast_type)
  1619. {
  1620. operation *subop = std::get<0> (m_storage).get ();
  1621. if (subop->opcode () != OP_INTERNALVAR)
  1622. error (_("May only assign to trace state variables"));
  1623. internalvar_operation *ivarop
  1624. = dynamic_cast<internalvar_operation *> (subop);
  1625. gdb_assert (ivarop != nullptr);
  1626. const char *name = internalvar_name (ivarop->get_internalvar ());
  1627. struct trace_state_variable *tsv;
  1628. std::get<1> (m_storage)->generate_ax (exp, ax, value);
  1629. tsv = find_trace_state_variable (name);
  1630. if (tsv)
  1631. {
  1632. ax_tsv (ax, aop_setv, tsv->number);
  1633. if (ax->tracing)
  1634. ax_tsv (ax, aop_tracev, tsv->number);
  1635. }
  1636. else
  1637. error (_("$%s is not a trace state variable, "
  1638. "may not assign to it"), name);
  1639. }
  1640. void
  1641. assign_modify_operation::do_generate_ax (struct expression *exp,
  1642. struct agent_expr *ax,
  1643. struct axs_value *value,
  1644. struct type *cast_type)
  1645. {
  1646. operation *subop = std::get<1> (m_storage).get ();
  1647. if (subop->opcode () != OP_INTERNALVAR)
  1648. error (_("May only assign to trace state variables"));
  1649. internalvar_operation *ivarop
  1650. = dynamic_cast<internalvar_operation *> (subop);
  1651. gdb_assert (ivarop != nullptr);
  1652. const char *name = internalvar_name (ivarop->get_internalvar ());
  1653. struct trace_state_variable *tsv;
  1654. tsv = find_trace_state_variable (name);
  1655. if (tsv)
  1656. {
  1657. /* The tsv will be the left half of the binary operation. */
  1658. ax_tsv (ax, aop_getv, tsv->number);
  1659. if (ax->tracing)
  1660. ax_tsv (ax, aop_tracev, tsv->number);
  1661. /* Trace state variables are always 64-bit integers. */
  1662. struct axs_value value1, value2;
  1663. value1.kind = axs_rvalue;
  1664. value1.type = builtin_type (ax->gdbarch)->builtin_long_long;
  1665. /* Now do right half of expression. */
  1666. std::get<2> (m_storage)->generate_ax (exp, ax, &value2);
  1667. gen_expr_binop_rest (exp, std::get<0> (m_storage), ax,
  1668. value, &value1, &value2);
  1669. /* We have a result of the binary op, set the tsv. */
  1670. ax_tsv (ax, aop_setv, tsv->number);
  1671. if (ax->tracing)
  1672. ax_tsv (ax, aop_tracev, tsv->number);
  1673. }
  1674. else
  1675. error (_("$%s is not a trace state variable, "
  1676. "may not assign to it"), name);
  1677. }
  1678. void
  1679. unop_cast_type_operation::do_generate_ax (struct expression *exp,
  1680. struct agent_expr *ax,
  1681. struct axs_value *value,
  1682. struct type *cast_type)
  1683. {
  1684. struct value *val
  1685. = std::get<0> (m_storage)->evaluate (nullptr, exp,
  1686. EVAL_AVOID_SIDE_EFFECTS);
  1687. std::get<1> (m_storage)->generate_ax (exp, ax, value, value_type (val));
  1688. }
  1689. void
  1690. var_value_operation::do_generate_ax (struct expression *exp,
  1691. struct agent_expr *ax,
  1692. struct axs_value *value,
  1693. struct type *cast_type)
  1694. {
  1695. gen_var_ref (ax, value, std::get<0> (m_storage).symbol);
  1696. if (value->optimized_out)
  1697. error (_("`%s' has been optimized out, cannot use"),
  1698. std::get<0> (m_storage).symbol->print_name ());
  1699. if (value->type->code () == TYPE_CODE_ERROR)
  1700. {
  1701. if (cast_type == nullptr)
  1702. error_unknown_type (std::get<0> (m_storage).symbol->print_name ());
  1703. value->type = cast_type;
  1704. }
  1705. }
  1706. void
  1707. logical_and_operation::do_generate_ax (struct expression *exp,
  1708. struct agent_expr *ax,
  1709. struct axs_value *value,
  1710. struct type *cast_type)
  1711. {
  1712. struct axs_value value1, value2;
  1713. int if1, go1, if2, go2, end;
  1714. /* Generate the obvious sequence of tests and jumps. */
  1715. std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
  1716. gen_usual_unary (ax, &value1);
  1717. if1 = ax_goto (ax, aop_if_goto);
  1718. go1 = ax_goto (ax, aop_goto);
  1719. ax_label (ax, if1, ax->len);
  1720. std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
  1721. gen_usual_unary (ax, &value2);
  1722. if2 = ax_goto (ax, aop_if_goto);
  1723. go2 = ax_goto (ax, aop_goto);
  1724. ax_label (ax, if2, ax->len);
  1725. ax_const_l (ax, 1);
  1726. end = ax_goto (ax, aop_goto);
  1727. ax_label (ax, go1, ax->len);
  1728. ax_label (ax, go2, ax->len);
  1729. ax_const_l (ax, 0);
  1730. ax_label (ax, end, ax->len);
  1731. value->kind = axs_rvalue;
  1732. value->type = builtin_type (ax->gdbarch)->builtin_int;
  1733. }
  1734. void
  1735. logical_or_operation::do_generate_ax (struct expression *exp,
  1736. struct agent_expr *ax,
  1737. struct axs_value *value,
  1738. struct type *cast_type)
  1739. {
  1740. struct axs_value value1, value2;
  1741. int if1, if2, end;
  1742. /* Generate the obvious sequence of tests and jumps. */
  1743. std::get<0> (m_storage)->generate_ax (exp, ax, &value1);
  1744. gen_usual_unary (ax, &value1);
  1745. if1 = ax_goto (ax, aop_if_goto);
  1746. std::get<1> (m_storage)->generate_ax (exp, ax, &value2);
  1747. gen_usual_unary (ax, &value2);
  1748. if2 = ax_goto (ax, aop_if_goto);
  1749. ax_const_l (ax, 0);
  1750. end = ax_goto (ax, aop_goto);
  1751. ax_label (ax, if1, ax->len);
  1752. ax_label (ax, if2, ax->len);
  1753. ax_const_l (ax, 1);
  1754. ax_label (ax, end, ax->len);
  1755. value->kind = axs_rvalue;
  1756. value->type = builtin_type (ax->gdbarch)->builtin_int;
  1757. }
  1758. }
  1759. /* This handles the middle-to-right-side of code generation for binary
  1760. expressions, which is shared between regular binary operations and
  1761. assign-modify (+= and friends) expressions. */
  1762. static void
  1763. gen_expr_binop_rest (struct expression *exp,
  1764. enum exp_opcode op,
  1765. struct agent_expr *ax, struct axs_value *value,
  1766. struct axs_value *value1, struct axs_value *value2)
  1767. {
  1768. struct type *int_type = builtin_type (ax->gdbarch)->builtin_int;
  1769. gen_usual_unary (ax, value2);
  1770. gen_usual_arithmetic (ax, value1, value2);
  1771. switch (op)
  1772. {
  1773. case BINOP_ADD:
  1774. if (value1->type->code () == TYPE_CODE_INT
  1775. && value2->type->is_pointer_or_reference ())
  1776. {
  1777. /* Swap the values and proceed normally. */
  1778. ax_simple (ax, aop_swap);
  1779. gen_ptradd (ax, value, value2, value1);
  1780. }
  1781. else if (value1->type->is_pointer_or_reference ()
  1782. && value2->type->code () == TYPE_CODE_INT)
  1783. gen_ptradd (ax, value, value1, value2);
  1784. else
  1785. gen_binop (ax, value, value1, value2,
  1786. aop_add, aop_add, 1, "addition");
  1787. break;
  1788. case BINOP_SUB:
  1789. if (value1->type->is_pointer_or_reference ()
  1790. && value2->type->code () == TYPE_CODE_INT)
  1791. gen_ptrsub (ax,value, value1, value2);
  1792. else if (value1->type->is_pointer_or_reference ()
  1793. && value2->type->is_pointer_or_reference ())
  1794. /* FIXME --- result type should be ptrdiff_t */
  1795. gen_ptrdiff (ax, value, value1, value2,
  1796. builtin_type (ax->gdbarch)->builtin_long);
  1797. else
  1798. gen_binop (ax, value, value1, value2,
  1799. aop_sub, aop_sub, 1, "subtraction");
  1800. break;
  1801. case BINOP_MUL:
  1802. gen_binop (ax, value, value1, value2,
  1803. aop_mul, aop_mul, 1, "multiplication");
  1804. break;
  1805. case BINOP_DIV:
  1806. gen_binop (ax, value, value1, value2,
  1807. aop_div_signed, aop_div_unsigned, 1, "division");
  1808. break;
  1809. case BINOP_REM:
  1810. gen_binop (ax, value, value1, value2,
  1811. aop_rem_signed, aop_rem_unsigned, 1, "remainder");
  1812. break;
  1813. case BINOP_LSH:
  1814. gen_binop (ax, value, value1, value2,
  1815. aop_lsh, aop_lsh, 1, "left shift");
  1816. break;
  1817. case BINOP_RSH:
  1818. gen_binop (ax, value, value1, value2,
  1819. aop_rsh_signed, aop_rsh_unsigned, 1, "right shift");
  1820. break;
  1821. case BINOP_SUBSCRIPT:
  1822. {
  1823. struct type *type;
  1824. if (binop_types_user_defined_p (op, value1->type, value2->type))
  1825. {
  1826. error (_("cannot subscript requested type: "
  1827. "cannot call user defined functions"));
  1828. }
  1829. else
  1830. {
  1831. /* If the user attempts to subscript something that is not
  1832. an array or pointer type (like a plain int variable for
  1833. example), then report this as an error. */
  1834. type = check_typedef (value1->type);
  1835. if (type->code () != TYPE_CODE_ARRAY
  1836. && type->code () != TYPE_CODE_PTR)
  1837. {
  1838. if (type->name ())
  1839. error (_("cannot subscript something of type `%s'"),
  1840. type->name ());
  1841. else
  1842. error (_("cannot subscript requested type"));
  1843. }
  1844. }
  1845. if (!is_integral_type (value2->type))
  1846. error (_("Argument to arithmetic operation "
  1847. "not a number or boolean."));
  1848. gen_ptradd (ax, value, value1, value2);
  1849. gen_deref (value);
  1850. break;
  1851. }
  1852. case BINOP_BITWISE_AND:
  1853. gen_binop (ax, value, value1, value2,
  1854. aop_bit_and, aop_bit_and, 0, "bitwise and");
  1855. break;
  1856. case BINOP_BITWISE_IOR:
  1857. gen_binop (ax, value, value1, value2,
  1858. aop_bit_or, aop_bit_or, 0, "bitwise or");
  1859. break;
  1860. case BINOP_BITWISE_XOR:
  1861. gen_binop (ax, value, value1, value2,
  1862. aop_bit_xor, aop_bit_xor, 0, "bitwise exclusive-or");
  1863. break;
  1864. case BINOP_EQUAL:
  1865. gen_equal (ax, value, value1, value2, int_type);
  1866. break;
  1867. case BINOP_NOTEQUAL:
  1868. gen_equal (ax, value, value1, value2, int_type);
  1869. gen_logical_not (ax, value, int_type);
  1870. break;
  1871. case BINOP_LESS:
  1872. gen_less (ax, value, value1, value2, int_type);
  1873. break;
  1874. case BINOP_GTR:
  1875. ax_simple (ax, aop_swap);
  1876. gen_less (ax, value, value1, value2, int_type);
  1877. break;
  1878. case BINOP_LEQ:
  1879. ax_simple (ax, aop_swap);
  1880. gen_less (ax, value, value1, value2, int_type);
  1881. gen_logical_not (ax, value, int_type);
  1882. break;
  1883. case BINOP_GEQ:
  1884. gen_less (ax, value, value1, value2, int_type);
  1885. gen_logical_not (ax, value, int_type);
  1886. break;
  1887. default:
  1888. /* We should only list operators in the outer case statement
  1889. that we actually handle in the inner case statement. */
  1890. internal_error (__FILE__, __LINE__,
  1891. _("gen_expr: op case sets don't match"));
  1892. }
  1893. }
  1894. /* A helper function that emits a binop based on two operations. */
  1895. void
  1896. gen_expr_binop (struct expression *exp,
  1897. enum exp_opcode op,
  1898. expr::operation *lhs, expr::operation *rhs,
  1899. struct agent_expr *ax, struct axs_value *value)
  1900. {
  1901. struct axs_value value1, value2;
  1902. lhs->generate_ax (exp, ax, &value1);
  1903. gen_usual_unary (ax, &value1);
  1904. rhs->generate_ax (exp, ax, &value2);
  1905. gen_expr_binop_rest (exp, op, ax, value, &value1, &value2);
  1906. }
  1907. /* A helper function that emits a structop based on an operation and a
  1908. member name. */
  1909. void
  1910. gen_expr_structop (struct expression *exp,
  1911. enum exp_opcode op,
  1912. expr::operation *lhs,
  1913. const char *name,
  1914. struct agent_expr *ax, struct axs_value *value)
  1915. {
  1916. lhs->generate_ax (exp, ax, value);
  1917. if (op == STRUCTOP_STRUCT)
  1918. gen_struct_ref (ax, value, name, ".", "structure or union");
  1919. else if (op == STRUCTOP_PTR)
  1920. gen_struct_ref (ax, value, name, "->",
  1921. "pointer to a structure or union");
  1922. else
  1923. /* If this `if' chain doesn't handle it, then the case list
  1924. shouldn't mention it, and we shouldn't be here. */
  1925. internal_error (__FILE__, __LINE__,
  1926. _("gen_expr: unhandled struct case"));
  1927. }
  1928. /* A helper function that emits a unary operation. */
  1929. void
  1930. gen_expr_unop (struct expression *exp,
  1931. enum exp_opcode op,
  1932. expr::operation *lhs,
  1933. struct agent_expr *ax, struct axs_value *value)
  1934. {
  1935. struct axs_value value1, value2;
  1936. switch (op)
  1937. {
  1938. case UNOP_NEG:
  1939. gen_int_literal (ax, &value1, 0,
  1940. builtin_type (ax->gdbarch)->builtin_int);
  1941. gen_usual_unary (ax, &value1); /* shouldn't do much */
  1942. lhs->generate_ax (exp, ax, &value2);
  1943. gen_usual_unary (ax, &value2);
  1944. gen_usual_arithmetic (ax, &value1, &value2);
  1945. gen_binop (ax, value, &value1, &value2, aop_sub, aop_sub, 1, "negation");
  1946. break;
  1947. case UNOP_PLUS:
  1948. /* + FOO is equivalent to 0 + FOO, which can be optimized. */
  1949. lhs->generate_ax (exp, ax, value);
  1950. gen_usual_unary (ax, value);
  1951. break;
  1952. case UNOP_LOGICAL_NOT:
  1953. lhs->generate_ax (exp, ax, value);
  1954. gen_usual_unary (ax, value);
  1955. gen_logical_not (ax, value, builtin_type (ax->gdbarch)->builtin_int);
  1956. break;
  1957. case UNOP_COMPLEMENT:
  1958. lhs->generate_ax (exp, ax, value);
  1959. gen_usual_unary (ax, value);
  1960. gen_integral_promotions (ax, value);
  1961. gen_complement (ax, value);
  1962. break;
  1963. case UNOP_IND:
  1964. lhs->generate_ax (exp, ax, value);
  1965. gen_usual_unary (ax, value);
  1966. if (!value->type->is_pointer_or_reference ())
  1967. error (_("Argument of unary `*' is not a pointer."));
  1968. gen_deref (value);
  1969. break;
  1970. case UNOP_ADDR:
  1971. lhs->generate_ax (exp, ax, value);
  1972. gen_address_of (value);
  1973. break;
  1974. default:
  1975. gdb_assert_not_reached ("invalid case in gen_expr_unop");
  1976. }
  1977. }
  1978. /* Given a single variable and a scope, generate bytecodes to trace
  1979. its value. This is for use in situations where we have only a
  1980. variable's name, and no parsed expression; for instance, when the
  1981. name comes from a list of local variables of a function. */
  1982. agent_expr_up
  1983. gen_trace_for_var (CORE_ADDR scope, struct gdbarch *gdbarch,
  1984. struct symbol *var, int trace_string)
  1985. {
  1986. agent_expr_up ax (new agent_expr (gdbarch, scope));
  1987. struct axs_value value;
  1988. ax->tracing = 1;
  1989. ax->trace_string = trace_string;
  1990. gen_var_ref (ax.get (), &value, var);
  1991. /* If there is no actual variable to trace, flag it by returning
  1992. an empty agent expression. */
  1993. if (value.optimized_out)
  1994. return agent_expr_up ();
  1995. /* Make sure we record the final object, and get rid of it. */
  1996. gen_traced_pop (ax.get (), &value);
  1997. /* Oh, and terminate. */
  1998. ax_simple (ax.get (), aop_end);
  1999. return ax;
  2000. }
  2001. /* Generating bytecode from GDB expressions: driver */
  2002. /* Given a GDB expression EXPR, return bytecode to trace its value.
  2003. The result will use the `trace' and `trace_quick' bytecodes to
  2004. record the value of all memory touched by the expression. The
  2005. caller can then use the ax_reqs function to discover which
  2006. registers it relies upon. */
  2007. agent_expr_up
  2008. gen_trace_for_expr (CORE_ADDR scope, struct expression *expr,
  2009. int trace_string)
  2010. {
  2011. agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
  2012. struct axs_value value;
  2013. ax->tracing = 1;
  2014. ax->trace_string = trace_string;
  2015. value.optimized_out = 0;
  2016. expr->op->generate_ax (expr, ax.get (), &value);
  2017. /* Make sure we record the final object, and get rid of it. */
  2018. gen_traced_pop (ax.get (), &value);
  2019. /* Oh, and terminate. */
  2020. ax_simple (ax.get (), aop_end);
  2021. return ax;
  2022. }
  2023. /* Given a GDB expression EXPR, return a bytecode sequence that will
  2024. evaluate and return a result. The bytecodes will do a direct
  2025. evaluation, using the current data on the target, rather than
  2026. recording blocks of memory and registers for later use, as
  2027. gen_trace_for_expr does. The generated bytecode sequence leaves
  2028. the result of expression evaluation on the top of the stack. */
  2029. agent_expr_up
  2030. gen_eval_for_expr (CORE_ADDR scope, struct expression *expr)
  2031. {
  2032. agent_expr_up ax (new agent_expr (expr->gdbarch, scope));
  2033. struct axs_value value;
  2034. ax->tracing = 0;
  2035. value.optimized_out = 0;
  2036. expr->op->generate_ax (expr, ax.get (), &value);
  2037. require_rvalue (ax.get (), &value);
  2038. /* Oh, and terminate. */
  2039. ax_simple (ax.get (), aop_end);
  2040. return ax;
  2041. }
  2042. agent_expr_up
  2043. gen_trace_for_return_address (CORE_ADDR scope, struct gdbarch *gdbarch,
  2044. int trace_string)
  2045. {
  2046. agent_expr_up ax (new agent_expr (gdbarch, scope));
  2047. struct axs_value value;
  2048. ax->tracing = 1;
  2049. ax->trace_string = trace_string;
  2050. gdbarch_gen_return_address (gdbarch, ax.get (), &value, scope);
  2051. /* Make sure we record the final object, and get rid of it. */
  2052. gen_traced_pop (ax.get (), &value);
  2053. /* Oh, and terminate. */
  2054. ax_simple (ax.get (), aop_end);
  2055. return ax;
  2056. }
  2057. /* Given a collection of printf-style arguments, generate code to
  2058. evaluate the arguments and pass everything to a special
  2059. bytecode. */
  2060. agent_expr_up
  2061. gen_printf (CORE_ADDR scope, struct gdbarch *gdbarch,
  2062. CORE_ADDR function, LONGEST channel,
  2063. const char *format, int fmtlen,
  2064. int nargs, struct expression **exprs)
  2065. {
  2066. agent_expr_up ax (new agent_expr (gdbarch, scope));
  2067. struct axs_value value;
  2068. int tem;
  2069. /* We're computing values, not doing side effects. */
  2070. ax->tracing = 0;
  2071. /* Evaluate and push the args on the stack in reverse order,
  2072. for simplicity of collecting them on the target side. */
  2073. for (tem = nargs - 1; tem >= 0; --tem)
  2074. {
  2075. value.optimized_out = 0;
  2076. exprs[tem]->op->generate_ax (exprs[tem], ax.get (), &value);
  2077. require_rvalue (ax.get (), &value);
  2078. }
  2079. /* Push function and channel. */
  2080. ax_const_l (ax.get (), channel);
  2081. ax_const_l (ax.get (), function);
  2082. /* Issue the printf bytecode proper. */
  2083. ax_simple (ax.get (), aop_printf);
  2084. ax_raw_byte (ax.get (), nargs);
  2085. ax_string (ax.get (), format, fmtlen);
  2086. /* And terminate. */
  2087. ax_simple (ax.get (), aop_end);
  2088. return ax;
  2089. }
  2090. static void
  2091. agent_eval_command_one (const char *exp, int eval, CORE_ADDR pc)
  2092. {
  2093. const char *arg;
  2094. int trace_string = 0;
  2095. if (!eval)
  2096. {
  2097. if (*exp == '/')
  2098. exp = decode_agent_options (exp, &trace_string);
  2099. }
  2100. agent_expr_up agent;
  2101. arg = exp;
  2102. if (!eval && strcmp (arg, "$_ret") == 0)
  2103. {
  2104. agent = gen_trace_for_return_address (pc, get_current_arch (),
  2105. trace_string);
  2106. }
  2107. else
  2108. {
  2109. expression_up expr = parse_exp_1 (&arg, pc, block_for_pc (pc), 0);
  2110. if (eval)
  2111. {
  2112. gdb_assert (trace_string == 0);
  2113. agent = gen_eval_for_expr (pc, expr.get ());
  2114. }
  2115. else
  2116. agent = gen_trace_for_expr (pc, expr.get (), trace_string);
  2117. }
  2118. ax_reqs (agent.get ());
  2119. ax_print (gdb_stdout, agent.get ());
  2120. /* It would be nice to call ax_reqs here to gather some general info
  2121. about the expression, and then print out the result. */
  2122. dont_repeat ();
  2123. }
  2124. static void
  2125. maint_agent_command_1 (const char *exp, int eval)
  2126. {
  2127. /* We don't deal with overlay debugging at the moment. We need to
  2128. think more carefully about this. If you copy this code into
  2129. another command, change the error message; the user shouldn't
  2130. have to know anything about agent expressions. */
  2131. if (overlay_debugging)
  2132. error (_("GDB can't do agent expression translation with overlays."));
  2133. if (exp == 0)
  2134. error_no_arg (_("expression to translate"));
  2135. if (check_for_argument (&exp, "-at", sizeof ("-at") - 1))
  2136. {
  2137. struct linespec_result canonical;
  2138. event_location_up location
  2139. = new_linespec_location (&exp, symbol_name_match_type::WILD);
  2140. decode_line_full (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
  2141. NULL, 0, &canonical,
  2142. NULL, NULL);
  2143. exp = skip_spaces (exp);
  2144. if (exp[0] == ',')
  2145. {
  2146. exp++;
  2147. exp = skip_spaces (exp);
  2148. }
  2149. for (const auto &lsal : canonical.lsals)
  2150. for (const auto &sal : lsal.sals)
  2151. agent_eval_command_one (exp, eval, sal.pc);
  2152. }
  2153. else
  2154. agent_eval_command_one (exp, eval, get_frame_pc (get_current_frame ()));
  2155. dont_repeat ();
  2156. }
  2157. static void
  2158. maint_agent_command (const char *exp, int from_tty)
  2159. {
  2160. maint_agent_command_1 (exp, 0);
  2161. }
  2162. /* Parse the given expression, compile it into an agent expression
  2163. that does direct evaluation, and display the resulting
  2164. expression. */
  2165. static void
  2166. maint_agent_eval_command (const char *exp, int from_tty)
  2167. {
  2168. maint_agent_command_1 (exp, 1);
  2169. }
  2170. /* Parse the given expression, compile it into an agent expression
  2171. that does a printf, and display the resulting expression. */
  2172. static void
  2173. maint_agent_printf_command (const char *cmdrest, int from_tty)
  2174. {
  2175. struct frame_info *fi = get_current_frame (); /* need current scope */
  2176. const char *format_start, *format_end;
  2177. /* We don't deal with overlay debugging at the moment. We need to
  2178. think more carefully about this. If you copy this code into
  2179. another command, change the error message; the user shouldn't
  2180. have to know anything about agent expressions. */
  2181. if (overlay_debugging)
  2182. error (_("GDB can't do agent expression translation with overlays."));
  2183. if (cmdrest == 0)
  2184. error_no_arg (_("expression to translate"));
  2185. cmdrest = skip_spaces (cmdrest);
  2186. if (*cmdrest++ != '"')
  2187. error (_("Must start with a format string."));
  2188. format_start = cmdrest;
  2189. format_pieces fpieces (&cmdrest);
  2190. format_end = cmdrest;
  2191. if (*cmdrest++ != '"')
  2192. error (_("Bad format string, non-terminated '\"'."));
  2193. cmdrest = skip_spaces (cmdrest);
  2194. if (*cmdrest != ',' && *cmdrest != 0)
  2195. error (_("Invalid argument syntax"));
  2196. if (*cmdrest == ',')
  2197. cmdrest++;
  2198. cmdrest = skip_spaces (cmdrest);
  2199. std::vector<struct expression *> argvec;
  2200. while (*cmdrest != '\0')
  2201. {
  2202. const char *cmd1;
  2203. cmd1 = cmdrest;
  2204. expression_up expr = parse_exp_1 (&cmd1, 0, (struct block *) 0, 1);
  2205. argvec.push_back (expr.release ());
  2206. cmdrest = cmd1;
  2207. if (*cmdrest == ',')
  2208. ++cmdrest;
  2209. /* else complain? */
  2210. }
  2211. agent_expr_up agent = gen_printf (get_frame_pc (fi), get_current_arch (),
  2212. 0, 0,
  2213. format_start, format_end - format_start,
  2214. argvec.size (), argvec.data ());
  2215. ax_reqs (agent.get ());
  2216. ax_print (gdb_stdout, agent.get ());
  2217. /* It would be nice to call ax_reqs here to gather some general info
  2218. about the expression, and then print out the result. */
  2219. dont_repeat ();
  2220. }
  2221. /* Initialization code. */
  2222. void _initialize_ax_gdb ();
  2223. void
  2224. _initialize_ax_gdb ()
  2225. {
  2226. add_cmd ("agent", class_maintenance, maint_agent_command,
  2227. _("\
  2228. Translate an expression into remote agent bytecode for tracing.\n\
  2229. Usage: maint agent [-at LOCATION,] EXPRESSION\n\
  2230. If -at is given, generate remote agent bytecode for this location.\n\
  2231. If not, generate remote agent bytecode for current frame pc address."),
  2232. &maintenancelist);
  2233. add_cmd ("agent-eval", class_maintenance, maint_agent_eval_command,
  2234. _("\
  2235. Translate an expression into remote agent bytecode for evaluation.\n\
  2236. Usage: maint agent-eval [-at LOCATION,] EXPRESSION\n\
  2237. If -at is given, generate remote agent bytecode for this location.\n\
  2238. If not, generate remote agent bytecode for current frame pc address."),
  2239. &maintenancelist);
  2240. add_cmd ("agent-printf", class_maintenance, maint_agent_printf_command,
  2241. _("Translate an expression into remote "
  2242. "agent bytecode for evaluation and display the bytecodes."),
  2243. &maintenancelist);
  2244. }