2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "wine/winbase16.h"
81 struct datatype * cast;
88 const char * element_name;
100 const char * funcname;
103 struct expr * arg[5];
109 #define EXPR_TYPE_CONST 0
110 #define EXPR_TYPE_US_CONST 1
111 #define EXPR_TYPE_SYMBOL 2
112 #define EXPR_TYPE_INTVAR 3
113 #define EXPR_TYPE_BINOP 4
114 #define EXPR_TYPE_UNOP 5
115 #define EXPR_TYPE_STRUCT 6
116 #define EXPR_TYPE_PSTRUCT 7
117 #define EXPR_TYPE_ARRAY 8
118 #define EXPR_TYPE_CALL 9
119 #define EXPR_TYPE_STRING 10
120 #define EXPR_TYPE_CAST 11
122 static char expr_list[4096];
123 static unsigned int next_expr_free = 0;
126 * This is how we turn an expression address into the actual value.
127 * This works well in the 32 bit domain - not sure at all about the
130 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
134 DEBUG_GetFreeExpr(void)
138 rtn = (struct expr *) &expr_list[next_expr_free];
140 next_expr_free += sizeof(struct expr);
141 assert(next_expr_free < sizeof(expr_list));
147 DEBUG_FreeExprMem(void)
153 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
157 ex = DEBUG_GetFreeExpr();
159 ex->type = EXPR_TYPE_CAST;
160 ex->un.cast.cast = dt;
161 ex->un.cast.expr = exp;
166 DEBUG_IntVarExpr(const char* name)
170 ex = DEBUG_GetFreeExpr();
172 ex->type = EXPR_TYPE_INTVAR;
173 ex->un.intvar.name = name;
178 DEBUG_SymbolExpr(const char * name)
182 ex = DEBUG_GetFreeExpr();
184 ex->type = EXPR_TYPE_SYMBOL;
185 ex->un.symbol.name = name;
190 DEBUG_ConstExpr(int value)
194 ex = DEBUG_GetFreeExpr();
196 ex->type = EXPR_TYPE_CONST;
197 ex->un.constant.value = value;
202 DEBUG_StringExpr(const char * str)
206 ex = DEBUG_GetFreeExpr();
208 ex->type = EXPR_TYPE_STRING;
209 ex->un.string.str = str+1;
210 pnt = strrchr(ex->un.string.str, '"');
219 DEBUG_USConstExpr(unsigned int value)
223 ex = DEBUG_GetFreeExpr();
225 ex->type = EXPR_TYPE_CONST;
226 ex->un.u_const.value = value;
231 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
235 ex = DEBUG_GetFreeExpr();
237 ex->type = EXPR_TYPE_BINOP;
238 ex->un.binop.binop_type = operator_type;
239 ex->un.binop.exp1 = exp1;
240 ex->un.binop.exp2 = exp2;
245 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
249 ex = DEBUG_GetFreeExpr();
251 ex->type = EXPR_TYPE_UNOP;
252 ex->un.unop.unop_type = operator_type;
253 ex->un.unop.exp1 = exp1;
258 DEBUG_StructExpr(struct expr * exp, const char * element)
262 ex = DEBUG_GetFreeExpr();
264 ex->type = EXPR_TYPE_STRUCT;
265 ex->un.structure.exp1 = exp;
266 ex->un.structure.element_name = element;
271 DEBUG_StructPExpr(struct expr * exp, const char * element)
275 ex = DEBUG_GetFreeExpr();
277 ex->type = EXPR_TYPE_PSTRUCT;
278 ex->un.structure.exp1 = exp;
279 ex->un.structure.element_name = element;
284 DEBUG_CallExpr(const char * funcname, int nargs, ...)
290 ex = DEBUG_GetFreeExpr();
292 ex->type = EXPR_TYPE_CALL;
293 ex->un.call.funcname = funcname;
294 ex->un.call.nargs = nargs;
297 for(i=0; i < nargs; i++)
299 ex->un.call.arg[i] = va_arg(ap, struct expr *);
305 DBG_VALUE DEBUG_EvalExpr(struct expr * exp)
311 unsigned int cexp[5];
315 struct datatype * type1;
316 struct datatype * type2;
319 rtn.cookie = DV_INVALID;
326 if (!exp->un.cast.cast)
328 DEBUG_Printf("Can't cast to unknown type\n");
329 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
331 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
332 rtn.type = exp->un.cast.cast;
334 case EXPR_TYPE_STRING:
335 rtn.type = DEBUG_GetBasicType(DT_BASIC_STRING);
336 rtn.cookie = DV_HOST;
337 rtn.addr.off = (unsigned int) &exp->un.string.str;
340 case EXPR_TYPE_CONST:
341 rtn.type = DEBUG_GetBasicType(DT_BASIC_CONST_INT);
342 rtn.cookie = DV_HOST;
343 rtn.addr.off = (unsigned int) &exp->un.constant.value;
346 case EXPR_TYPE_US_CONST:
347 rtn.type = DEBUG_GetBasicType(DT_BASIC_USHORTINT);
348 rtn.cookie = DV_HOST;
349 rtn.addr.off = (unsigned int) &exp->un.u_const.value;
352 case EXPR_TYPE_SYMBOL:
353 switch (DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE))
358 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
359 /* should never be here */
361 RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
362 /* should never be here */
365 case EXPR_TYPE_PSTRUCT:
366 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
367 if( exp1.type == NULL )
369 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
371 rtn.cookie = DV_TARGET;
372 rtn.addr.off = DEBUG_TypeDerefPointer(&exp1, &rtn.type);
373 if( rtn.type == NULL )
375 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
377 if (!DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
378 &exp->un.structure.result))
380 DEBUG_Printf("%s\n", exp->un.structure.element_name);
381 RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
385 case EXPR_TYPE_STRUCT:
386 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
387 if( exp1.type == NULL )
389 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
392 if (!DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
393 &exp->un.structure.result))
395 DEBUG_Printf("%s\n", exp->un.structure.element_name);
396 RaiseException(DEBUG_STATUS_NO_FIELD, 0, 0, NULL);
401 * First, evaluate all of the arguments. If any of them are not
402 * evaluable, then bail.
404 for(i=0; i < exp->un.call.nargs; i++)
406 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
407 if( exp1.type == NULL )
411 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
415 * Now look up the address of the function itself.
417 switch (DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ))
422 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
423 /* should never be here */
425 RaiseException(DEBUG_STATUS_ABORT, 0, 0, NULL);
426 /* should never be here */
430 /* FIXME: NEWDBG NIY */
431 /* Anyway, I wonder how this could work depending on the calling order of
432 * the function (cdecl vs pascal for example)
436 fptr = (int (*)()) rtn.addr.off;
437 switch(exp->un.call.nargs)
440 exp->un.call.result = (*fptr)();
443 exp->un.call.result = (*fptr)(cexp[0]);
446 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
449 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
452 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
455 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
459 DEBUG_Printf("Function call no longer implemented\n");
460 /* would need to set up a call to this function, and then restore the current
461 * context afterwards...
463 exp->un.call.result = 0;
465 rtn.type = DEBUG_GetBasicType(DT_BASIC_INT);
466 rtn.cookie = DV_HOST;
467 rtn.addr.off = (unsigned int) &exp->un.call.result;
470 case EXPR_TYPE_INTVAR:
473 DBG_INTVAR* div = DEBUG_GetIntVar(exp->un.intvar.name);
475 if (!div) RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
476 rtn.cookie = DV_HOST;
477 rtn.type = div->type;
478 rtn.addr.off = (unsigned int)div->pval;
479 /* EPP FIXME rtn.addr.seg = ?? */
482 case EXPR_TYPE_BINOP:
483 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
484 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
485 rtn.cookie = DV_HOST;
486 if( exp1.type == NULL || exp2.type == NULL )
488 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
490 if( exp1.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) &&
491 exp2.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
493 rtn.type = exp1.type;
497 rtn.type = DEBUG_GetBasicType(DT_BASIC_INT);
500 rtn.addr.off = (unsigned int) &exp->un.binop.result;
501 switch(exp->un.binop.binop_type)
504 type1 = DEBUG_GetPointerType(exp1.type);
505 type2 = DEBUG_GetPointerType(exp2.type);
508 if( type1 != NULL && type2 != NULL )
510 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
512 else if( type1 != NULL )
514 scale2 = DEBUG_GetObjectSize(type1);
515 rtn.type = exp1.type;
517 else if( type2 != NULL )
519 scale1 = DEBUG_GetObjectSize(type2);
520 rtn.type = exp2.type;
522 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
525 type1 = DEBUG_GetPointerType(exp1.type);
526 type2 = DEBUG_GetPointerType(exp2.type);
530 if( type1 != NULL && type2 != NULL )
534 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
536 scale3 = DEBUG_GetObjectSize(type1);
538 else if( type1 != NULL )
540 scale2 = DEBUG_GetObjectSize(type1);
541 rtn.type = exp1.type;
544 else if( type2 != NULL )
546 scale1 = DEBUG_GetObjectSize(type2);
547 rtn.type = exp2.type;
549 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
552 rtn.cookie = DV_TARGET;
554 rtn.addr.seg = VAL(exp1);
555 rtn.addr.off = VAL(exp2);
558 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
561 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
564 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
567 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
570 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
573 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
576 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
579 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
582 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
585 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
588 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
591 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
594 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
597 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
602 RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
604 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
609 RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
611 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
614 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
617 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
622 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
623 rtn.cookie = DV_HOST;
624 if( exp1.type == NULL )
626 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
629 rtn.addr.off = (unsigned int) &exp->un.unop.result;
630 if( exp1.type == DEBUG_GetBasicType(DT_BASIC_CONST_INT) )
632 rtn.type = exp1.type;
636 rtn.type = DEBUG_GetBasicType(DT_BASIC_INT);
638 switch(exp->un.unop.unop_type)
641 exp->un.unop.result = -VAL(exp1);
644 exp->un.unop.result = !VAL(exp1);
647 exp->un.unop.result = ~VAL(exp1);
650 /* FIXME: this is currently buggy.
651 * there is no way to tell were the deref:ed value is...
653 * x is a pointer to struct s, x being on the stack
654 * => exp1 is target, result is target
655 * x is a pointer to struct s, x being optimized into a reg
656 * => exp1 is host, result is target
657 * x is a pointer to internal variable x
658 * => exp1 is host, result is host
659 * so we force DV_TARGET, because dereferencing pointers to
660 * internal variables is very unlikely. a correct fix would be
663 rtn.cookie = DV_TARGET;
664 rtn.addr.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
667 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
670 case EXP_OP_FORCE_DEREF:
671 rtn.cookie = exp1.cookie;
672 rtn.addr.seg = exp1.addr.seg;
673 if (exp1.cookie == DV_TARGET)
674 DEBUG_READ_MEM((void*)exp1.addr.off, &rtn.addr.off, sizeof(rtn.addr.off));
676 memcpy(&rtn.addr.off, (void*)exp1.addr.off, sizeof(rtn.addr.off));
679 /* FIXME: even for a 16 bit entity ? */
680 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
681 exp->un.unop.result = exp1.addr.off;
684 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
688 DEBUG_Printf("Unexpected expression (%d).\n", exp->type);
689 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
693 assert(rtn.cookie == DV_TARGET || rtn.cookie == DV_HOST);
700 DEBUG_DisplayExpr(const struct expr * exp)
708 DEBUG_PrintTypeCast(exp->un.cast.cast);
710 DEBUG_DisplayExpr(exp->un.cast.expr);
713 case EXPR_TYPE_INTVAR:
714 DEBUG_Printf("$%s", exp->un.intvar.name);
716 case EXPR_TYPE_US_CONST:
717 DEBUG_Printf("%ud", exp->un.u_const.value);
719 case EXPR_TYPE_CONST:
720 DEBUG_Printf("%d", exp->un.u_const.value);
722 case EXPR_TYPE_STRING:
723 DEBUG_Printf("\"%s\"", exp->un.string.str);
725 case EXPR_TYPE_SYMBOL:
726 DEBUG_Printf("%s" , exp->un.symbol.name);
728 case EXPR_TYPE_PSTRUCT:
729 DEBUG_DisplayExpr(exp->un.structure.exp1);
730 DEBUG_Printf("->%s", exp->un.structure.element_name);
732 case EXPR_TYPE_STRUCT:
733 DEBUG_DisplayExpr(exp->un.structure.exp1);
734 DEBUG_Printf(".%s", exp->un.structure.element_name);
737 DEBUG_Printf("%s(",exp->un.call.funcname);
738 for(i=0; i < exp->un.call.nargs; i++)
740 DEBUG_DisplayExpr(exp->un.call.arg[i]);
741 if( i != exp->un.call.nargs - 1 )
748 case EXPR_TYPE_BINOP:
750 DEBUG_DisplayExpr(exp->un.binop.exp1);
751 switch(exp->un.binop.binop_type)
763 DEBUG_Printf(" || ");
766 DEBUG_Printf(" && ");
778 DEBUG_Printf(" == ");
787 DEBUG_Printf(" >= ");
790 DEBUG_Printf(" <= ");
793 DEBUG_Printf(" != ");
796 DEBUG_Printf(" << ");
799 DEBUG_Printf(" >> ");
808 DEBUG_Printf(" %% ");
816 DEBUG_DisplayExpr(exp->un.binop.exp2);
817 if( exp->un.binop.binop_type == EXP_OP_ARR )
824 switch(exp->un.unop.unop_type)
842 DEBUG_DisplayExpr(exp->un.unop.exp1);
845 DEBUG_Printf("Unexpected expression.\n");
846 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
854 DEBUG_CloneExpr(const struct expr * exp)
859 rtn = (struct expr *) DBG_alloc(sizeof(struct expr));
862 * First copy the contents of the expression itself.
870 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
872 case EXPR_TYPE_INTVAR:
873 rtn->un.intvar.name = DBG_strdup(exp->un.intvar.name);
875 case EXPR_TYPE_US_CONST:
876 case EXPR_TYPE_CONST:
878 case EXPR_TYPE_STRING:
879 rtn->un.string.str = DBG_strdup(exp->un.string.str);
881 case EXPR_TYPE_SYMBOL:
882 rtn->un.symbol.name = DBG_strdup(exp->un.symbol.name);
884 case EXPR_TYPE_PSTRUCT:
885 case EXPR_TYPE_STRUCT:
886 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
887 rtn->un.structure.element_name = DBG_strdup(exp->un.structure.element_name);
890 for(i=0; i < exp->un.call.nargs; i++)
892 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
894 rtn->un.call.funcname = DBG_strdup(exp->un.call.funcname);
896 case EXPR_TYPE_BINOP:
897 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
898 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
901 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
904 DEBUG_Printf("Unexpected expression.\n");
905 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
914 * Recursively go through an expression tree and free all memory associated
918 DEBUG_FreeExpr(struct expr * exp)
925 DEBUG_FreeExpr(exp->un.cast.expr);
927 case EXPR_TYPE_INTVAR:
928 DBG_free((char *) exp->un.intvar.name);
930 case EXPR_TYPE_US_CONST:
931 case EXPR_TYPE_CONST:
933 case EXPR_TYPE_STRING:
934 DBG_free((char *) exp->un.string.str);
936 case EXPR_TYPE_SYMBOL:
937 DBG_free((char *) exp->un.symbol.name);
939 case EXPR_TYPE_PSTRUCT:
940 case EXPR_TYPE_STRUCT:
941 DEBUG_FreeExpr(exp->un.structure.exp1);
942 DBG_free((char *) exp->un.structure.element_name);
945 for(i=0; i < exp->un.call.nargs; i++)
947 DEBUG_FreeExpr(exp->un.call.arg[i]);
949 DBG_free((char *) exp->un.call.funcname);
951 case EXPR_TYPE_BINOP:
952 DEBUG_FreeExpr(exp->un.binop.exp1);
953 DEBUG_FreeExpr(exp->un.binop.exp2);
956 DEBUG_FreeExpr(exp->un.unop.exp1);
959 DEBUG_Printf("Unexpected expression.\n");
960 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);