expr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /*
  2. * File expr.c - expression handling for Wine internal debugger.
  3. *
  4. * Copyright (C) 1997, Eric Youngdale.
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdarg.h>
  23. #include "debugger.h"
  24. #include "expr.h"
  25. #include "wine/debug.h"
  26. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  27. struct expr
  28. {
  29. unsigned int type;
  30. union
  31. {
  32. struct
  33. {
  34. dbg_lgint_t value;
  35. } s_const;
  36. struct
  37. {
  38. dbg_lguint_t value;
  39. } u_const;
  40. struct
  41. {
  42. const char* str;
  43. } string;
  44. struct
  45. {
  46. const char* name;
  47. } symbol;
  48. struct
  49. {
  50. const char* name;
  51. } intvar;
  52. struct
  53. {
  54. int unop_type;
  55. struct expr* exp1;
  56. dbg_lgint_t result;
  57. } unop;
  58. struct
  59. {
  60. int binop_type;
  61. struct expr* exp1;
  62. struct expr* exp2;
  63. dbg_lgint_t result;
  64. } binop;
  65. struct
  66. {
  67. struct type_expr_t cast_to;
  68. struct expr* expr;
  69. } cast;
  70. struct
  71. {
  72. struct expr* exp1;
  73. const char* element_name;
  74. } structure;
  75. struct
  76. {
  77. const char* funcname;
  78. int nargs;
  79. struct expr* arg[5];
  80. dbg_lguint_t result;
  81. } call;
  82. } un;
  83. };
  84. #define EXPR_TYPE_S_CONST 0
  85. #define EXPR_TYPE_U_CONST 1
  86. #define EXPR_TYPE_SYMBOL 2
  87. #define EXPR_TYPE_INTVAR 3
  88. #define EXPR_TYPE_BINOP 4
  89. #define EXPR_TYPE_UNOP 5
  90. #define EXPR_TYPE_STRUCT 6
  91. #define EXPR_TYPE_PSTRUCT 7
  92. #define EXPR_TYPE_CALL 8
  93. #define EXPR_TYPE_STRING 9
  94. #define EXPR_TYPE_CAST 10
  95. static char expr_list[4096];
  96. static unsigned int next_expr_free = 0;
  97. static struct expr* expr_alloc(void)
  98. {
  99. struct expr* rtn;
  100. rtn = (struct expr*)&expr_list[next_expr_free];
  101. next_expr_free += sizeof(struct expr);
  102. assert(next_expr_free < sizeof(expr_list));
  103. return rtn;
  104. }
  105. void expr_free_all(void)
  106. {
  107. next_expr_free = 0;
  108. }
  109. struct expr* expr_alloc_typecast(struct type_expr_t* tet, struct expr* exp)
  110. {
  111. struct expr* ex;
  112. ex = expr_alloc();
  113. ex->type = EXPR_TYPE_CAST;
  114. ex->un.cast.cast_to = *tet;
  115. ex->un.cast.expr = exp;
  116. return ex;
  117. }
  118. struct expr* expr_alloc_internal_var(const char* name)
  119. {
  120. struct expr* ex;
  121. ex = expr_alloc();
  122. ex->type = EXPR_TYPE_INTVAR;
  123. ex->un.intvar.name = name;
  124. return ex;
  125. }
  126. struct expr* expr_alloc_symbol(const char* name)
  127. {
  128. struct expr* ex;
  129. ex = expr_alloc();
  130. ex->type = EXPR_TYPE_SYMBOL;
  131. ex->un.symbol.name = name;
  132. return ex;
  133. }
  134. struct expr* expr_alloc_sconstant(dbg_lgint_t value)
  135. {
  136. struct expr* ex;
  137. ex = expr_alloc();
  138. ex->type = EXPR_TYPE_S_CONST;
  139. ex->un.s_const.value = value;
  140. return ex;
  141. }
  142. struct expr* expr_alloc_uconstant(dbg_lguint_t value)
  143. {
  144. struct expr* ex;
  145. ex = expr_alloc();
  146. ex->type = EXPR_TYPE_U_CONST;
  147. ex->un.u_const.value = value;
  148. return ex;
  149. }
  150. struct expr* expr_alloc_string(const char* str)
  151. {
  152. struct expr* ex;
  153. ex = expr_alloc();
  154. ex->type = EXPR_TYPE_STRING;
  155. ex->un.string.str = str;
  156. return ex;
  157. }
  158. struct expr* expr_alloc_binary_op(int op_type, struct expr* exp1, struct expr* exp2)
  159. {
  160. struct expr* ex;
  161. ex = expr_alloc();
  162. ex->type = EXPR_TYPE_BINOP;
  163. ex->un.binop.binop_type = op_type;
  164. ex->un.binop.exp1 = exp1;
  165. ex->un.binop.exp2 = exp2;
  166. return ex;
  167. }
  168. struct expr* expr_alloc_unary_op(int op_type, struct expr* exp1)
  169. {
  170. struct expr* ex;
  171. ex = expr_alloc();
  172. ex->type = EXPR_TYPE_UNOP;
  173. ex->un.unop.unop_type = op_type;
  174. ex->un.unop.exp1 = exp1;
  175. return ex;
  176. }
  177. struct expr* expr_alloc_struct(struct expr* exp, const char* element)
  178. {
  179. struct expr* ex;
  180. ex = expr_alloc();
  181. ex->type = EXPR_TYPE_STRUCT;
  182. ex->un.structure.exp1 = exp;
  183. ex->un.structure.element_name = element;
  184. return ex;
  185. }
  186. struct expr* expr_alloc_pstruct(struct expr* exp, const char* element)
  187. {
  188. struct expr* ex;
  189. ex = expr_alloc();
  190. ex->type = EXPR_TYPE_PSTRUCT;
  191. ex->un.structure.exp1 = exp;
  192. ex->un.structure.element_name = element;
  193. return ex;
  194. }
  195. struct expr* WINAPIV expr_alloc_func_call(const char* funcname, int nargs, ...)
  196. {
  197. struct expr* ex;
  198. va_list ap;
  199. int i;
  200. ex = expr_alloc();
  201. ex->type = EXPR_TYPE_CALL;
  202. ex->un.call.funcname = funcname;
  203. ex->un.call.nargs = nargs;
  204. va_start(ap, nargs);
  205. for (i = 0; i < nargs; i++)
  206. {
  207. ex->un.call.arg[i] = va_arg(ap, struct expr*);
  208. }
  209. va_end(ap);
  210. return ex;
  211. }
  212. /******************************************************************
  213. * expr_eval
  214. *
  215. */
  216. struct dbg_lvalue expr_eval(struct expr* exp)
  217. {
  218. struct dbg_lvalue rtn;
  219. int i;
  220. struct dbg_lvalue exp1;
  221. struct dbg_lvalue exp2;
  222. DWORD64 scale1, scale2, scale3;
  223. struct dbg_type type1, type2;
  224. DWORD tag;
  225. const struct dbg_internal_var* div;
  226. init_lvalue_in_debugger(&rtn, dbg_itype_none, NULL);
  227. switch (exp->type)
  228. {
  229. case EXPR_TYPE_CAST:
  230. /* this is really brute force, we simply change the type... without
  231. * checking if this is right or not
  232. */
  233. rtn = expr_eval(exp->un.cast.expr);
  234. switch (exp->un.cast.cast_to.type)
  235. {
  236. case type_expr_type_id:
  237. if (exp->un.cast.cast_to.u.type.id == dbg_itype_none)
  238. {
  239. dbg_printf("Can't cast to unknown type\n");
  240. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  241. }
  242. rtn.type = exp->un.cast.cast_to.u.type;
  243. break;
  244. case type_expr_udt_class:
  245. case type_expr_udt_struct:
  246. case type_expr_udt_union:
  247. rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
  248. SymTagUDT);
  249. if (rtn.type.id == dbg_itype_none)
  250. {
  251. dbg_printf("Can't cast to UDT %s\n", exp->un.cast.cast_to.u.name);
  252. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  253. }
  254. break;
  255. case type_expr_enumeration:
  256. rtn.type = types_find_type(rtn.type.module, exp->un.cast.cast_to.u.name,
  257. SymTagEnum);
  258. if (rtn.type.id == dbg_itype_none)
  259. {
  260. dbg_printf("Can't cast to enumeration %s\n", exp->un.cast.cast_to.u.name);
  261. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  262. }
  263. break;
  264. default:
  265. dbg_printf("Unsupported cast type %u\n", exp->un.cast.cast_to.type);
  266. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  267. }
  268. for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
  269. {
  270. rtn.type = types_find_pointer(&rtn.type);
  271. if (rtn.type.id == dbg_itype_none)
  272. {
  273. dbg_printf("Cannot find pointer type\n");
  274. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  275. }
  276. }
  277. break;
  278. case EXPR_TYPE_STRING:
  279. init_lvalue_in_debugger(&rtn, dbg_itype_astring, &exp->un.string.str);
  280. break;
  281. case EXPR_TYPE_U_CONST:
  282. init_lvalue_in_debugger(&rtn, dbg_itype_lguint, &exp->un.u_const.value);
  283. break;
  284. case EXPR_TYPE_S_CONST:
  285. init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.s_const.value);
  286. break;
  287. case EXPR_TYPE_SYMBOL:
  288. switch (symbol_get_lvalue(exp->un.symbol.name, -1, &rtn, FALSE))
  289. {
  290. case sglv_found:
  291. break;
  292. case sglv_unknown:
  293. RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
  294. /* should never be here */
  295. case sglv_aborted:
  296. RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
  297. /* should never be here */
  298. }
  299. break;
  300. case EXPR_TYPE_PSTRUCT:
  301. exp1 = expr_eval(exp->un.structure.exp1);
  302. if (exp1.type.id == dbg_itype_none || !types_array_index(&exp1, 0, &rtn) ||
  303. rtn.type.id == dbg_itype_none)
  304. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  305. if (!types_udt_find_element(&rtn, exp->un.structure.element_name))
  306. {
  307. dbg_printf("%s\n", exp->un.structure.element_name);
  308. RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
  309. }
  310. break;
  311. case EXPR_TYPE_STRUCT:
  312. exp1 = expr_eval(exp->un.structure.exp1);
  313. if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  314. rtn = exp1;
  315. if (!types_udt_find_element(&rtn, exp->un.structure.element_name))
  316. {
  317. dbg_printf("%s\n", exp->un.structure.element_name);
  318. RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
  319. }
  320. break;
  321. case EXPR_TYPE_CALL:
  322. #if 0
  323. /*
  324. * First, evaluate all of the arguments. If any of them are not
  325. * evaluable, then bail.
  326. */
  327. for (i = 0; i < exp->un.call.nargs; i++)
  328. {
  329. exp1 = expr_eval(exp->un.call.arg[i]);
  330. if (exp1.type.id == dbg_itype_none)
  331. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  332. cexp[i] = types_extract_as_integer(&exp1);
  333. }
  334. /*
  335. * Now look up the address of the function itself.
  336. */
  337. switch (symbol_get_lvalue(exp->un.call.funcname, -1, &rtn, FALSE))
  338. {
  339. case sglv_found:
  340. break;
  341. case sglv_unknown:
  342. RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
  343. /* should never be here */
  344. case sglv_aborted:
  345. RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
  346. /* should never be here */
  347. }
  348. /* FIXME: NEWDBG NIY */
  349. /* Anyway, I wonder how this could work depending on the calling order of
  350. * the function (cdecl vs pascal for example)
  351. */
  352. int (*fptr)();
  353. fptr = (int (*)()) rtn.addr.off;
  354. switch (exp->un.call.nargs)
  355. {
  356. case 0:
  357. exp->un.call.result = (*fptr)();
  358. break;
  359. case 1:
  360. exp->un.call.result = (*fptr)(cexp[0]);
  361. break;
  362. case 2:
  363. exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
  364. break;
  365. case 3:
  366. exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
  367. break;
  368. case 4:
  369. exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
  370. break;
  371. case 5:
  372. exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
  373. break;
  374. }
  375. #else
  376. dbg_printf("Function call no longer implemented\n");
  377. /* would need to set up a call to this function, and then restore the current
  378. * context afterwards...
  379. */
  380. exp->un.call.result = 0;
  381. #endif
  382. init_lvalue_in_debugger(&rtn, dbg_itype_none, &exp->un.call.result);
  383. /* get return type from function signature type */
  384. /* FIXME rtn.type.module should be set to function's module... */
  385. types_get_info(&rtn.type, TI_GET_TYPE, &rtn.type.id);
  386. break;
  387. case EXPR_TYPE_INTVAR:
  388. if (!(div = dbg_get_internal_var(exp->un.intvar.name)))
  389. RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
  390. init_lvalue_in_debugger(&rtn, div->typeid, div->pval);
  391. break;
  392. case EXPR_TYPE_BINOP:
  393. exp1 = expr_eval(exp->un.binop.exp1);
  394. exp2 = expr_eval(exp->un.binop.exp2);
  395. if (exp1.type.id == dbg_itype_none || exp2.type.id == dbg_itype_none)
  396. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  397. init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.binop.result);
  398. type1 = exp1.type;
  399. type2 = exp2.type;
  400. switch (exp->un.binop.binop_type)
  401. {
  402. case EXP_OP_ADD:
  403. if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
  404. tag != SymTagPointerType ||
  405. !types_get_info(&exp1.type, TI_GET_TYPE, &type1.id))
  406. type1.id = dbg_itype_none;
  407. if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
  408. tag != SymTagPointerType ||
  409. !types_get_info(&exp2.type, TI_GET_TYPE, &type2.id))
  410. type2.id = dbg_itype_none;
  411. scale1 = 1;
  412. scale2 = 1;
  413. if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
  414. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  415. if (type1.id != dbg_itype_none)
  416. {
  417. types_get_info(&type1, TI_GET_LENGTH, &scale2);
  418. rtn.type = exp1.type;
  419. }
  420. else if (type2.id != dbg_itype_none)
  421. {
  422. types_get_info(&type2, TI_GET_LENGTH, &scale1);
  423. rtn.type = exp2.type;
  424. }
  425. exp->un.binop.result = types_extract_as_integer(&exp1) * (dbg_lguint_t)scale1 +
  426. (dbg_lguint_t)scale2 * types_extract_as_integer(&exp2);
  427. break;
  428. case EXP_OP_SUB:
  429. if (!types_get_info(&exp1.type, TI_GET_SYMTAG, &tag) ||
  430. tag != SymTagPointerType ||
  431. !types_get_info(&exp1.type, TI_GET_TYPE, &type1.id))
  432. type1.id = dbg_itype_none;
  433. if (!types_get_info(&exp2.type, TI_GET_SYMTAG, &tag) ||
  434. tag != SymTagPointerType ||
  435. !types_get_info(&exp2.type, TI_GET_TYPE, &type2.id))
  436. type2.id = dbg_itype_none;
  437. scale1 = 1;
  438. scale2 = 1;
  439. scale3 = 1;
  440. if (type1.id != dbg_itype_none && type2.id != dbg_itype_none)
  441. {
  442. WINE_FIXME("This may fail (if module base address are wrongly calculated)\n");
  443. if (memcmp(&type1, &type2, sizeof(struct dbg_type)))
  444. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  445. types_get_info(&type1, TI_GET_LENGTH, &scale3);
  446. }
  447. else if (type1.id != dbg_itype_none)
  448. {
  449. types_get_info(&type1, TI_GET_LENGTH, &scale2);
  450. rtn.type = exp1.type;
  451. }
  452. else if (type2.id != dbg_itype_none)
  453. {
  454. types_get_info(&type2, TI_GET_LENGTH, &scale1);
  455. rtn.type = exp2.type;
  456. }
  457. exp->un.binop.result = (types_extract_as_integer(&exp1) * (dbg_lguint_t)scale1 -
  458. types_extract_as_integer(&exp2) * (dbg_lguint_t)scale2) / (dbg_lguint_t)scale3;
  459. break;
  460. case EXP_OP_SEG:
  461. rtn.type.id = dbg_itype_segptr;
  462. rtn.type.module = 0;
  463. dbg_curr_process->be_cpu->build_addr(dbg_curr_thread->handle, &dbg_context, &rtn.addr,
  464. types_extract_as_integer(&exp1), types_extract_as_integer(&exp2));
  465. break;
  466. case EXP_OP_LOR:
  467. exp->un.binop.result = (types_extract_as_integer(&exp1) || types_extract_as_integer(&exp2));
  468. break;
  469. case EXP_OP_LAND:
  470. exp->un.binop.result = (types_extract_as_integer(&exp1) && types_extract_as_integer(&exp2));
  471. break;
  472. case EXP_OP_OR:
  473. exp->un.binop.result = (types_extract_as_integer(&exp1) | types_extract_as_integer(&exp2));
  474. break;
  475. case EXP_OP_AND:
  476. exp->un.binop.result = (types_extract_as_integer(&exp1) & types_extract_as_integer(&exp2));
  477. break;
  478. case EXP_OP_XOR:
  479. exp->un.binop.result = (types_extract_as_integer(&exp1) ^ types_extract_as_integer(&exp2));
  480. break;
  481. case EXP_OP_EQ:
  482. exp->un.binop.result = (types_extract_as_integer(&exp1) == types_extract_as_integer(&exp2));
  483. break;
  484. case EXP_OP_GT:
  485. exp->un.binop.result = (types_extract_as_integer(&exp1) > types_extract_as_integer(&exp2));
  486. break;
  487. case EXP_OP_LT:
  488. exp->un.binop.result = (types_extract_as_integer(&exp1) < types_extract_as_integer(&exp2));
  489. break;
  490. case EXP_OP_GE:
  491. exp->un.binop.result = (types_extract_as_integer(&exp1) >= types_extract_as_integer(&exp2));
  492. break;
  493. case EXP_OP_LE:
  494. exp->un.binop.result = (types_extract_as_integer(&exp1) <= types_extract_as_integer(&exp2));
  495. break;
  496. case EXP_OP_NE:
  497. exp->un.binop.result = (types_extract_as_integer(&exp1) != types_extract_as_integer(&exp2));
  498. break;
  499. case EXP_OP_SHL:
  500. exp->un.binop.result = types_extract_as_integer(&exp1) << types_extract_as_integer(&exp2);
  501. break;
  502. case EXP_OP_SHR:
  503. exp->un.binop.result = types_extract_as_integer(&exp1) >> types_extract_as_integer(&exp2);
  504. break;
  505. case EXP_OP_MUL:
  506. exp->un.binop.result = (types_extract_as_integer(&exp1) * types_extract_as_integer(&exp2));
  507. break;
  508. case EXP_OP_DIV:
  509. if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
  510. exp->un.binop.result = (types_extract_as_integer(&exp1) / types_extract_as_integer(&exp2));
  511. break;
  512. case EXP_OP_REM:
  513. if (types_extract_as_integer(&exp2) == 0) RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
  514. exp->un.binop.result = (types_extract_as_integer(&exp1) % types_extract_as_integer(&exp2));
  515. break;
  516. case EXP_OP_ARR:
  517. if (!types_array_index(&exp1, types_extract_as_integer(&exp2), &rtn))
  518. RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
  519. break;
  520. default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  521. }
  522. break;
  523. case EXPR_TYPE_UNOP:
  524. exp1 = expr_eval(exp->un.unop.exp1);
  525. if (exp1.type.id == dbg_itype_none) RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  526. init_lvalue_in_debugger(&rtn, dbg_itype_lgint, &exp->un.unop.result);
  527. switch (exp->un.unop.unop_type)
  528. {
  529. case EXP_OP_NEG:
  530. exp->un.unop.result = -types_extract_as_integer(&exp1);
  531. break;
  532. case EXP_OP_NOT:
  533. exp->un.unop.result = !types_extract_as_integer(&exp1);
  534. break;
  535. case EXP_OP_LNOT:
  536. exp->un.unop.result = ~types_extract_as_integer(&exp1);
  537. break;
  538. case EXP_OP_DEREF:
  539. if (!types_array_index(&exp1, 0, &rtn))
  540. RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
  541. break;
  542. case EXP_OP_FORCE_DEREF:
  543. rtn = exp1;
  544. if (exp1.in_debuggee)
  545. dbg_read_memory(memory_to_linear_addr(&exp1.addr), &rtn.addr.Offset, sizeof(rtn.addr.Offset));
  546. break;
  547. case EXP_OP_ADDR:
  548. /* only do it on linear addresses */
  549. if (exp1.addr.Mode != AddrModeFlat)
  550. RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
  551. exp->un.unop.result = (ULONG_PTR)memory_to_linear_addr(&exp1.addr);
  552. rtn.type = types_find_pointer(&exp1.type);
  553. if (rtn.type.id == dbg_itype_none)
  554. RaiseException(DEBUG_STATUS_CANT_DEREF, 0, 0, NULL);
  555. break;
  556. default: RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  557. }
  558. break;
  559. default:
  560. WINE_FIXME("Unexpected expression (%d).\n", exp->type);
  561. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  562. break;
  563. }
  564. return rtn;
  565. }
  566. BOOL expr_print(const struct expr* exp)
  567. {
  568. int i;
  569. struct dbg_type type;
  570. switch (exp->type)
  571. {
  572. case EXPR_TYPE_CAST:
  573. WINE_FIXME("No longer supported (missing module base)\n");
  574. dbg_printf("((");
  575. switch (exp->un.cast.cast_to.type)
  576. {
  577. case type_expr_type_id:
  578. type.module = 0;
  579. type.id = exp->un.cast.cast_to.type;
  580. types_print_type(&type, FALSE); break;
  581. case type_expr_udt_class:
  582. dbg_printf("class %s", exp->un.cast.cast_to.u.name); break;
  583. case type_expr_udt_struct:
  584. dbg_printf("struct %s", exp->un.cast.cast_to.u.name); break;
  585. case type_expr_udt_union:
  586. dbg_printf("union %s", exp->un.cast.cast_to.u.name); break;
  587. case type_expr_enumeration:
  588. dbg_printf("enum %s", exp->un.cast.cast_to.u.name); break;
  589. }
  590. for (i = 0; i < exp->un.cast.cast_to.deref_count; i++)
  591. dbg_printf("*");
  592. dbg_printf(")");
  593. expr_print(exp->un.cast.expr);
  594. dbg_printf(")");
  595. break;
  596. case EXPR_TYPE_INTVAR:
  597. dbg_printf("$%s", exp->un.intvar.name);
  598. break;
  599. case EXPR_TYPE_U_CONST:
  600. dbg_printf("%I64u", exp->un.u_const.value);
  601. break;
  602. case EXPR_TYPE_S_CONST:
  603. dbg_printf("%I64d", exp->un.s_const.value);
  604. break;
  605. case EXPR_TYPE_STRING:
  606. dbg_printf("\"%s\"", exp->un.string.str);
  607. break;
  608. case EXPR_TYPE_SYMBOL:
  609. dbg_printf("%s" , exp->un.symbol.name);
  610. break;
  611. case EXPR_TYPE_PSTRUCT:
  612. expr_print(exp->un.structure.exp1);
  613. dbg_printf("->%s", exp->un.structure.element_name);
  614. break;
  615. case EXPR_TYPE_STRUCT:
  616. expr_print(exp->un.structure.exp1);
  617. dbg_printf(".%s", exp->un.structure.element_name);
  618. break;
  619. case EXPR_TYPE_CALL:
  620. dbg_printf("%s(",exp->un.call.funcname);
  621. for (i = 0; i < exp->un.call.nargs; i++)
  622. {
  623. expr_print(exp->un.call.arg[i]);
  624. if (i != exp->un.call.nargs - 1) dbg_printf(", ");
  625. }
  626. dbg_printf(")");
  627. break;
  628. case EXPR_TYPE_BINOP:
  629. dbg_printf("(");
  630. expr_print(exp->un.binop.exp1);
  631. switch (exp->un.binop.binop_type)
  632. {
  633. case EXP_OP_ADD: dbg_printf(" + "); break;
  634. case EXP_OP_SUB: dbg_printf(" - "); break;
  635. case EXP_OP_SEG: dbg_printf(":"); break;
  636. case EXP_OP_LOR: dbg_printf(" || "); break;
  637. case EXP_OP_LAND: dbg_printf(" && "); break;
  638. case EXP_OP_OR: dbg_printf(" | "); break;
  639. case EXP_OP_AND: dbg_printf(" & "); break;
  640. case EXP_OP_XOR: dbg_printf(" ^ "); break;
  641. case EXP_OP_EQ: dbg_printf(" == "); break;
  642. case EXP_OP_GT: dbg_printf(" > "); break;
  643. case EXP_OP_LT: dbg_printf(" < "); break;
  644. case EXP_OP_GE: dbg_printf(" >= "); break;
  645. case EXP_OP_LE: dbg_printf(" <= "); break;
  646. case EXP_OP_NE: dbg_printf(" != "); break;
  647. case EXP_OP_SHL: dbg_printf(" << "); break;
  648. case EXP_OP_SHR: dbg_printf(" >> "); break;
  649. case EXP_OP_MUL: dbg_printf(" * "); break;
  650. case EXP_OP_DIV: dbg_printf(" / "); break;
  651. case EXP_OP_REM: dbg_printf(" %% "); break;
  652. case EXP_OP_ARR: dbg_printf("["); break;
  653. default: break;
  654. }
  655. expr_print(exp->un.binop.exp2);
  656. if (exp->un.binop.binop_type == EXP_OP_ARR) dbg_printf("]");
  657. dbg_printf(")");
  658. break;
  659. case EXPR_TYPE_UNOP:
  660. switch (exp->un.unop.unop_type)
  661. {
  662. case EXP_OP_NEG: dbg_printf("-"); break;
  663. case EXP_OP_NOT: dbg_printf("!"); break;
  664. case EXP_OP_LNOT: dbg_printf("~"); break;
  665. case EXP_OP_DEREF: dbg_printf("*"); break;
  666. case EXP_OP_ADDR: dbg_printf("&"); break;
  667. }
  668. expr_print(exp->un.unop.exp1);
  669. break;
  670. default:
  671. WINE_FIXME("Unexpected expression (%u).\n", exp->type);
  672. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  673. break;
  674. }
  675. return TRUE;
  676. }
  677. struct expr* expr_clone(const struct expr* exp, BOOL *local_binding)
  678. {
  679. int i;
  680. struct expr* rtn;
  681. rtn = HeapAlloc(GetProcessHeap(), 0, sizeof(struct expr));
  682. /*
  683. * First copy the contents of the expression itself.
  684. */
  685. *rtn = *exp;
  686. switch (exp->type)
  687. {
  688. case EXPR_TYPE_CAST:
  689. rtn->un.cast.expr = expr_clone(exp->un.cast.expr, local_binding);
  690. break;
  691. case EXPR_TYPE_INTVAR:
  692. rtn->un.intvar.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.intvar.name) + 1), exp->un.intvar.name);
  693. break;
  694. case EXPR_TYPE_U_CONST:
  695. case EXPR_TYPE_S_CONST:
  696. break;
  697. case EXPR_TYPE_STRING:
  698. rtn->un.string.str = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.string.str) + 1), exp->un.string.str);
  699. break;
  700. case EXPR_TYPE_SYMBOL:
  701. rtn->un.symbol.name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.symbol.name) + 1), exp->un.symbol.name);
  702. if (local_binding && symbol_is_local(exp->un.symbol.name))
  703. *local_binding = TRUE;
  704. break;
  705. case EXPR_TYPE_PSTRUCT:
  706. case EXPR_TYPE_STRUCT:
  707. rtn->un.structure.exp1 = expr_clone(exp->un.structure.exp1, local_binding);
  708. rtn->un.structure.element_name = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.structure.element_name) + 1), exp->un.structure.element_name);
  709. break;
  710. case EXPR_TYPE_CALL:
  711. for (i = 0; i < exp->un.call.nargs; i++)
  712. {
  713. rtn->un.call.arg[i] = expr_clone(exp->un.call.arg[i], local_binding);
  714. }
  715. rtn->un.call.funcname = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(exp->un.call.funcname) + 1), exp->un.call.funcname);
  716. break;
  717. case EXPR_TYPE_BINOP:
  718. rtn->un.binop.exp1 = expr_clone(exp->un.binop.exp1, local_binding);
  719. rtn->un.binop.exp2 = expr_clone(exp->un.binop.exp2, local_binding);
  720. break;
  721. case EXPR_TYPE_UNOP:
  722. rtn->un.unop.exp1 = expr_clone(exp->un.unop.exp1, local_binding);
  723. break;
  724. default:
  725. WINE_FIXME("Unexpected expression (%u).\n", exp->type);
  726. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  727. break;
  728. }
  729. return rtn;
  730. }
  731. /*
  732. * Recursively go through an expression tree and free all memory associated
  733. * with it.
  734. */
  735. BOOL expr_free(struct expr* exp)
  736. {
  737. int i;
  738. switch (exp->type)
  739. {
  740. case EXPR_TYPE_CAST:
  741. expr_free(exp->un.cast.expr);
  742. break;
  743. case EXPR_TYPE_INTVAR:
  744. HeapFree(GetProcessHeap(), 0, (char*)exp->un.intvar.name);
  745. break;
  746. case EXPR_TYPE_U_CONST:
  747. case EXPR_TYPE_S_CONST:
  748. break;
  749. case EXPR_TYPE_STRING:
  750. HeapFree(GetProcessHeap(), 0, (char*)exp->un.string.str);
  751. break;
  752. case EXPR_TYPE_SYMBOL:
  753. HeapFree(GetProcessHeap(), 0, (char*)exp->un.symbol.name);
  754. break;
  755. case EXPR_TYPE_PSTRUCT:
  756. case EXPR_TYPE_STRUCT:
  757. expr_free(exp->un.structure.exp1);
  758. HeapFree(GetProcessHeap(), 0, (char*)exp->un.structure.element_name);
  759. break;
  760. case EXPR_TYPE_CALL:
  761. for (i = 0; i < exp->un.call.nargs; i++)
  762. {
  763. expr_free(exp->un.call.arg[i]);
  764. }
  765. HeapFree(GetProcessHeap(), 0, (char*)exp->un.call.funcname);
  766. break;
  767. case EXPR_TYPE_BINOP:
  768. expr_free(exp->un.binop.exp1);
  769. expr_free(exp->un.binop.exp2);
  770. break;
  771. case EXPR_TYPE_UNOP:
  772. expr_free(exp->un.unop.exp1);
  773. break;
  774. default:
  775. WINE_FIXME("Unexpected expression (%u).\n", exp->type);
  776. RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
  777. break;
  778. }
  779. HeapFree(GetProcessHeap(), 0, exp);
  780. return TRUE;
  781. }