expr.c 28 KB

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