2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
12 #include "wine/winbase16.h"
67 struct datatype * cast;
74 const char * element_name;
86 const char * funcname;
95 #define EXPR_TYPE_CONST 0
96 #define EXPR_TYPE_US_CONST 1
97 #define EXPR_TYPE_SYMBOL 2
98 #define EXPR_TYPE_INTVAR 3
99 #define EXPR_TYPE_BINOP 4
100 #define EXPR_TYPE_UNOP 5
101 #define EXPR_TYPE_STRUCT 6
102 #define EXPR_TYPE_PSTRUCT 7
103 #define EXPR_TYPE_ARRAY 8
104 #define EXPR_TYPE_CALL 9
105 #define EXPR_TYPE_STRING 10
106 #define EXPR_TYPE_CAST 11
108 static char expr_list[4096];
109 static int next_expr_free = 0;
112 * This is how we turn an expression address into the actual value.
113 * This works well in the 32 bit domain - not sure at all about the
116 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
120 DEBUG_GetFreeExpr(void)
124 rtn = (struct expr *) &expr_list[next_expr_free];
126 next_expr_free += sizeof(struct expr);
127 assert(next_expr_free < sizeof(expr_list));
133 DEBUG_FreeExprMem(void)
139 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
143 ex = DEBUG_GetFreeExpr();
145 ex->type = EXPR_TYPE_CAST;
146 ex->un.cast.cast = dt;
147 ex->un.cast.expr = exp;
152 DEBUG_IntVarExpr(const char* name)
156 ex = DEBUG_GetFreeExpr();
158 ex->type = EXPR_TYPE_INTVAR;
159 ex->un.intvar.name = name;
164 DEBUG_SymbolExpr(const char * name)
168 ex = DEBUG_GetFreeExpr();
170 ex->type = EXPR_TYPE_SYMBOL;
171 ex->un.symbol.name = name;
176 DEBUG_ConstExpr(int value)
180 ex = DEBUG_GetFreeExpr();
182 ex->type = EXPR_TYPE_CONST;
183 ex->un.constant.value = value;
188 DEBUG_StringExpr(const char * str)
192 ex = DEBUG_GetFreeExpr();
194 ex->type = EXPR_TYPE_STRING;
195 ex->un.string.str = str+1;
196 pnt = strrchr(ex->un.string.str, '"');
205 DEBUG_USConstExpr(unsigned int value)
209 ex = DEBUG_GetFreeExpr();
211 ex->type = EXPR_TYPE_CONST;
212 ex->un.u_const.value = value;
217 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
221 ex = DEBUG_GetFreeExpr();
223 ex->type = EXPR_TYPE_BINOP;
224 ex->un.binop.binop_type = operator_type;
225 ex->un.binop.exp1 = exp1;
226 ex->un.binop.exp2 = exp2;
231 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
235 ex = DEBUG_GetFreeExpr();
237 ex->type = EXPR_TYPE_UNOP;
238 ex->un.unop.unop_type = operator_type;
239 ex->un.unop.exp1 = exp1;
244 DEBUG_StructExpr(struct expr * exp, const char * element)
248 ex = DEBUG_GetFreeExpr();
250 ex->type = EXPR_TYPE_STRUCT;
251 ex->un.structure.exp1 = exp;
252 ex->un.structure.element_name = element;
257 DEBUG_StructPExpr(struct expr * exp, const char * element)
261 ex = DEBUG_GetFreeExpr();
263 ex->type = EXPR_TYPE_PSTRUCT;
264 ex->un.structure.exp1 = exp;
265 ex->un.structure.element_name = element;
270 DEBUG_CallExpr(const char * funcname, int nargs, ...)
276 ex = DEBUG_GetFreeExpr();
278 ex->type = EXPR_TYPE_CALL;
279 ex->un.call.funcname = funcname;
280 ex->un.call.nargs = nargs;
283 for(i=0; i < nargs; i++)
285 ex->un.call.arg[i] = va_arg(ap, struct expr *);
291 DBG_VALUE DEBUG_EvalExpr(struct expr * exp)
297 unsigned int cexp[5];
301 struct datatype * type1;
302 struct datatype * type2;
311 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
312 rtn.type = exp->un.cast.cast;
313 if (DEBUG_GetType(rtn.type) == DT_POINTER)
314 rtn.cookie = DV_TARGET;
316 case EXPR_TYPE_STRING:
317 rtn.type = DEBUG_TypeString;
318 rtn.cookie = DV_HOST;
319 rtn.addr.off = (unsigned int) &exp->un.string.str;
322 case EXPR_TYPE_CONST:
323 rtn.type = DEBUG_TypeIntConst;
324 rtn.cookie = DV_HOST;
325 rtn.addr.off = (unsigned int) &exp->un.constant.value;
328 case EXPR_TYPE_US_CONST:
329 rtn.type = DEBUG_TypeUSInt;
330 rtn.cookie = DV_HOST;
331 rtn.addr.off = (unsigned int) &exp->un.u_const.value;
334 case EXPR_TYPE_SYMBOL:
335 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE) )
337 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
340 case EXPR_TYPE_PSTRUCT:
341 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
342 if( exp1.type == NULL )
344 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
346 rtn.cookie = DV_TARGET;
347 rtn.addr.off = DEBUG_TypeDerefPointer(&exp1, &type1);
350 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
353 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
354 &exp->un.structure.result);
356 case EXPR_TYPE_STRUCT:
357 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
358 if( exp1.type == NULL )
360 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
363 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
364 &exp->un.structure.result);
368 * First, evaluate all of the arguments. If any of them are not
369 * evaluable, then bail.
371 for(i=0; i < exp->un.call.nargs; i++)
373 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
374 if( exp1.type == NULL )
378 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
382 * Now look up the address of the function itself.
384 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
386 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
390 /* FIXME: NEWDBG NIY */
391 /* Anyway, I wonder how this could work depending on the calling order of
392 * the function (cdecl vs pascal for example)
396 fptr = (int (*)()) rtn.addr.off;
397 switch(exp->un.call.nargs)
400 exp->un.call.result = (*fptr)();
403 exp->un.call.result = (*fptr)(cexp[0]);
406 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
409 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
412 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
415 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
419 DEBUG_Printf(DBG_CHN_MESG, "Function call no longer implemented\n");
420 /* would need to set up a call to this function, and then restore the current
421 * context afterwards...
423 exp->un.call.result = 0;
425 rtn.type = DEBUG_TypeInt;
426 rtn.cookie = DV_HOST;
427 rtn.addr.off = (unsigned int) &exp->un.call.result;
430 case EXPR_TYPE_INTVAR:
433 DBG_INTVAR* div = DEBUG_GetIntVar(exp->un.intvar.name);
435 if (!div) RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
436 rtn.cookie = DV_HOST;
437 rtn.type = div->type;
438 rtn.addr.off = (unsigned int)div->pval;
439 /* EPP FIXME rtn.addr.seg = ?? */
442 case EXPR_TYPE_BINOP:
443 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
444 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
445 rtn.cookie = DV_HOST;
446 if( exp1.type == NULL || exp2.type == NULL )
448 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
450 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
452 rtn.type = exp1.type;
456 rtn.type = DEBUG_TypeInt;
459 rtn.addr.off = (unsigned int) &exp->un.binop.result;
460 switch(exp->un.binop.binop_type)
463 type1 = DEBUG_GetPointerType(exp1.type);
464 type2 = DEBUG_GetPointerType(exp2.type);
467 if( type1 != NULL && type2 != NULL )
469 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
471 else if( type1 != NULL )
473 scale2 = DEBUG_GetObjectSize(type1);
474 rtn.type = exp1.type;
476 else if( type2 != NULL )
478 scale1 = DEBUG_GetObjectSize(type2);
479 rtn.type = exp2.type;
481 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
484 type1 = DEBUG_GetPointerType(exp1.type);
485 type2 = DEBUG_GetPointerType(exp2.type);
489 if( type1 != NULL && type2 != NULL )
493 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
495 scale3 = DEBUG_GetObjectSize(type1);
497 else if( type1 != NULL )
499 scale2 = DEBUG_GetObjectSize(type1);
500 rtn.type = exp1.type;
503 else if( type2 != NULL )
505 scale1 = DEBUG_GetObjectSize(type2);
506 rtn.type = exp2.type;
508 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
511 rtn.cookie = DV_TARGET;
512 rtn.addr.seg = VAL(exp1);
513 exp->un.binop.result = VAL(exp2);
515 DEBUG_FixSegment(&rtn.addr);
519 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
522 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
525 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
528 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
531 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
534 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
537 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
540 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
543 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
546 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
549 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
552 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
555 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
558 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
563 RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
565 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
570 RaiseException(DEBUG_STATUS_DIV_BY_ZERO, 0, 0, NULL);
572 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
575 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
578 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
583 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
584 rtn.cookie = DV_HOST;
585 if( exp1.type == NULL )
587 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
590 rtn.addr.off = (unsigned int) &exp->un.unop.result;
591 if( exp1.type == DEBUG_TypeIntConst )
593 rtn.type = exp1.type;
597 rtn.type = DEBUG_TypeInt;
599 switch(exp->un.unop.unop_type)
602 exp->un.unop.result = -VAL(exp1);
605 exp->un.unop.result = !VAL(exp1);
608 exp->un.unop.result = ~VAL(exp1);
611 rtn.cookie = exp1.cookie;
612 rtn.addr.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
615 RaiseException(DEBUG_STATUS_BAD_TYPE, 0, 0, NULL);
618 case EXP_OP_FORCE_DEREF:
619 rtn.cookie = exp1.cookie;
620 rtn.addr.seg = exp1.addr.seg;
621 if (exp1.cookie == DV_TARGET)
622 DEBUG_READ_MEM((void*)exp1.addr.off, &rtn.addr.off, sizeof(rtn.addr.off));
624 memcpy(&rtn.addr.off, (void*)exp1.addr.off, sizeof(rtn.addr.off));
627 /* FIXME: even for a 16 bit entity ? */
628 rtn.cookie = DV_TARGET;
629 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
630 exp->un.unop.result = exp1.addr.off;
633 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
637 DEBUG_Printf(DBG_CHN_MESG,"Unexpected expression (%d).\n", exp->type);
638 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
642 assert(rtn.cookie == DV_TARGET || rtn.cookie == DV_HOST);
649 DEBUG_DisplayExpr(const struct expr * exp)
656 DEBUG_Printf(DBG_CHN_MESG, "((");
657 DEBUG_PrintTypeCast(exp->un.cast.cast);
658 DEBUG_Printf(DBG_CHN_MESG, ")");
659 DEBUG_DisplayExpr(exp->un.cast.expr);
660 DEBUG_Printf(DBG_CHN_MESG, ")");
662 case EXPR_TYPE_INTVAR:
663 DEBUG_Printf(DBG_CHN_MESG, "$%s", exp->un.intvar.name);
665 case EXPR_TYPE_US_CONST:
666 DEBUG_Printf(DBG_CHN_MESG, "%ud", exp->un.u_const.value);
668 case EXPR_TYPE_CONST:
669 DEBUG_Printf(DBG_CHN_MESG, "%d", exp->un.u_const.value);
671 case EXPR_TYPE_STRING:
672 DEBUG_Printf(DBG_CHN_MESG, "\"%s\"", exp->un.string.str);
674 case EXPR_TYPE_SYMBOL:
675 DEBUG_Printf(DBG_CHN_MESG, "%s" , exp->un.symbol.name);
677 case EXPR_TYPE_PSTRUCT:
678 DEBUG_DisplayExpr(exp->un.structure.exp1);
679 DEBUG_Printf(DBG_CHN_MESG, "->%s", exp->un.structure.element_name);
681 case EXPR_TYPE_STRUCT:
682 DEBUG_DisplayExpr(exp->un.structure.exp1);
683 DEBUG_Printf(DBG_CHN_MESG, ".%s", exp->un.structure.element_name);
686 DEBUG_Printf(DBG_CHN_MESG, "%s(",exp->un.call.funcname);
687 for(i=0; i < exp->un.call.nargs; i++)
689 DEBUG_DisplayExpr(exp->un.call.arg[i]);
690 if( i != exp->un.call.nargs - 1 )
692 DEBUG_Printf(DBG_CHN_MESG, ", ");
695 DEBUG_Printf(DBG_CHN_MESG, ")");
697 case EXPR_TYPE_BINOP:
698 DEBUG_Printf(DBG_CHN_MESG, "( ");
699 DEBUG_DisplayExpr(exp->un.binop.exp1);
700 switch(exp->un.binop.binop_type)
703 DEBUG_Printf(DBG_CHN_MESG, " + ");
706 DEBUG_Printf(DBG_CHN_MESG, " - ");
709 DEBUG_Printf(DBG_CHN_MESG, ":");
712 DEBUG_Printf(DBG_CHN_MESG, " || ");
715 DEBUG_Printf(DBG_CHN_MESG, " && ");
718 DEBUG_Printf(DBG_CHN_MESG, " | ");
721 DEBUG_Printf(DBG_CHN_MESG, " & ");
724 DEBUG_Printf(DBG_CHN_MESG, " ^ ");
727 DEBUG_Printf(DBG_CHN_MESG, " == ");
730 DEBUG_Printf(DBG_CHN_MESG, " > ");
733 DEBUG_Printf(DBG_CHN_MESG, " < ");
736 DEBUG_Printf(DBG_CHN_MESG, " >= ");
739 DEBUG_Printf(DBG_CHN_MESG, " <= ");
742 DEBUG_Printf(DBG_CHN_MESG, " != ");
745 DEBUG_Printf(DBG_CHN_MESG, " << ");
748 DEBUG_Printf(DBG_CHN_MESG, " >> ");
751 DEBUG_Printf(DBG_CHN_MESG, " * ");
754 DEBUG_Printf(DBG_CHN_MESG, " / ");
757 DEBUG_Printf(DBG_CHN_MESG, " %% ");
760 DEBUG_Printf(DBG_CHN_MESG, "[");
765 DEBUG_DisplayExpr(exp->un.binop.exp2);
766 if( exp->un.binop.binop_type == EXP_OP_ARR )
768 DEBUG_Printf(DBG_CHN_MESG, "]");
770 DEBUG_Printf(DBG_CHN_MESG, " )");
773 switch(exp->un.unop.unop_type)
776 DEBUG_Printf(DBG_CHN_MESG, "-");
779 DEBUG_Printf(DBG_CHN_MESG, "!");
782 DEBUG_Printf(DBG_CHN_MESG, "~");
785 DEBUG_Printf(DBG_CHN_MESG, "*");
788 DEBUG_Printf(DBG_CHN_MESG, "&");
791 DEBUG_DisplayExpr(exp->un.unop.exp1);
794 DEBUG_Printf(DBG_CHN_MESG,"Unexpected expression.\n");
795 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
803 DEBUG_CloneExpr(const struct expr * exp)
808 rtn = (struct expr *) DBG_alloc(sizeof(struct expr));
811 * First copy the contents of the expression itself.
819 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
821 case EXPR_TYPE_INTVAR:
822 rtn->un.intvar.name = DBG_strdup(exp->un.intvar.name);
824 case EXPR_TYPE_US_CONST:
825 case EXPR_TYPE_CONST:
827 case EXPR_TYPE_STRING:
828 rtn->un.string.str = DBG_strdup(exp->un.string.str);
830 case EXPR_TYPE_SYMBOL:
831 rtn->un.symbol.name = DBG_strdup(exp->un.symbol.name);
833 case EXPR_TYPE_PSTRUCT:
834 case EXPR_TYPE_STRUCT:
835 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
836 rtn->un.structure.element_name = DBG_strdup(exp->un.structure.element_name);
839 for(i=0; i < exp->un.call.nargs; i++)
841 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
843 rtn->un.call.funcname = DBG_strdup(exp->un.call.funcname);
845 case EXPR_TYPE_BINOP:
846 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
847 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
850 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
853 DEBUG_Printf(DBG_CHN_MESG,"Unexpected expression.\n");
854 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);
863 * Recursively go through an expression tree and free all memory associated
867 DEBUG_FreeExpr(struct expr * exp)
874 DEBUG_FreeExpr(exp->un.cast.expr);
876 case EXPR_TYPE_INTVAR:
877 DBG_free((char *) exp->un.intvar.name);
879 case EXPR_TYPE_US_CONST:
880 case EXPR_TYPE_CONST:
882 case EXPR_TYPE_STRING:
883 DBG_free((char *) exp->un.string.str);
885 case EXPR_TYPE_SYMBOL:
886 DBG_free((char *) exp->un.symbol.name);
888 case EXPR_TYPE_PSTRUCT:
889 case EXPR_TYPE_STRUCT:
890 DEBUG_FreeExpr(exp->un.structure.exp1);
891 DBG_free((char *) exp->un.structure.element_name);
894 for(i=0; i < exp->un.call.nargs; i++)
896 DEBUG_FreeExpr(exp->un.call.arg[i]);
898 DBG_free((char *) exp->un.call.funcname);
900 case EXPR_TYPE_BINOP:
901 DEBUG_FreeExpr(exp->un.binop.exp1);
902 DEBUG_FreeExpr(exp->un.binop.exp2);
905 DEBUG_FreeExpr(exp->un.unop.exp1);
908 DEBUG_Printf(DBG_CHN_MESG,"Unexpected expression.\n");
909 RaiseException(DEBUG_STATUS_INTERNAL_ERROR, 0, 0, NULL);