2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
12 #include <sys/types.h>
15 #include "selectors.h"
73 struct datatype * cast;
80 const char * element_name;
92 const char * funcname;
101 #define EXPR_TYPE_CONST 0
102 #define EXPR_TYPE_US_CONST 1
103 #define EXPR_TYPE_SYMBOL 2
104 #define EXPR_TYPE_REGISTER 3
105 #define EXPR_TYPE_BINOP 4
106 #define EXPR_TYPE_UNOP 5
107 #define EXPR_TYPE_STRUCT 6
108 #define EXPR_TYPE_PSTRUCT 7
109 #define EXPR_TYPE_ARRAY 8
110 #define EXPR_TYPE_CALL 9
111 #define EXPR_TYPE_STRING 10
112 #define EXPR_TYPE_CAST 11
114 static char expr_list[4096];
115 static int next_expr_free = 0;
118 * This is how we turn an expression address into the actual value.
119 * This works well in the 32 bit domain - not sure at all about the
122 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
130 rtn = (struct expr *) &expr_list[next_expr_free];
132 next_expr_free += sizeof(struct expr);
143 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
147 ex = DEBUG_GetFreeExpr();
149 ex->type = EXPR_TYPE_CAST;
150 ex->un.cast.cast = dt;
151 ex->un.cast.expr = exp;
156 DEBUG_RegisterExpr(enum debug_regs regno)
160 ex = DEBUG_GetFreeExpr();
162 ex->type = EXPR_TYPE_REGISTER;
163 ex->un.rgister.reg = regno;
168 DEBUG_SymbolExpr(const char * name)
172 ex = DEBUG_GetFreeExpr();
174 ex->type = EXPR_TYPE_SYMBOL;
175 ex->un.symbol.name = name;
180 DEBUG_ConstExpr(int value)
184 ex = DEBUG_GetFreeExpr();
186 ex->type = EXPR_TYPE_CONST;
187 ex->un.constant.value = value;
192 DEBUG_StringExpr(const char * str)
196 ex = DEBUG_GetFreeExpr();
198 ex->type = EXPR_TYPE_STRING;
199 ex->un.string.str = str+1;
200 pnt = strrchr(ex->un.string.str, '"');
209 DEBUG_USConstExpr(unsigned int value)
213 ex = DEBUG_GetFreeExpr();
215 ex->type = EXPR_TYPE_CONST;
216 ex->un.u_const.value = value;
221 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
225 ex = DEBUG_GetFreeExpr();
227 ex->type = EXPR_TYPE_BINOP;
228 ex->un.binop.binop_type = operator_type;
229 ex->un.binop.exp1 = exp1;
230 ex->un.binop.exp2 = exp2;
235 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
239 ex = DEBUG_GetFreeExpr();
241 ex->type = EXPR_TYPE_UNOP;
242 ex->un.unop.unop_type = operator_type;
243 ex->un.unop.exp1 = exp1;
248 DEBUG_StructExpr(struct expr * exp, const char * element)
252 ex = DEBUG_GetFreeExpr();
254 ex->type = EXPR_TYPE_STRUCT;
255 ex->un.structure.exp1 = exp;
256 ex->un.structure.element_name = element;
261 DEBUG_StructPExpr(struct expr * exp, const char * element)
265 ex = DEBUG_GetFreeExpr();
267 ex->type = EXPR_TYPE_PSTRUCT;
268 ex->un.structure.exp1 = exp;
269 ex->un.structure.element_name = element;
274 DEBUG_CallExpr(const char * funcname, int nargs, ...)
280 ex = DEBUG_GetFreeExpr();
282 ex->type = EXPR_TYPE_CALL;
283 ex->un.call.funcname = funcname;
284 ex->un.call.nargs = nargs;
287 for(i=0; i < nargs; i++)
289 ex->un.call.arg[i] = va_arg(ap, struct expr *);
296 DEBUG_EvalExpr(struct expr * exp)
302 unsigned int cexp[5];
307 struct datatype * type1;
308 struct datatype * type2;
317 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
318 rtn.type = exp->un.cast.cast;
320 case EXPR_TYPE_STRING:
321 rtn.type = DEBUG_TypeString;
322 rtn.off = (unsigned int) &exp->un.string.str;
325 case EXPR_TYPE_CONST:
326 rtn.type = DEBUG_TypeIntConst;
327 rtn.off = (unsigned int) &exp->un.constant.value;
330 case EXPR_TYPE_US_CONST:
331 rtn.type = DEBUG_TypeUSInt;
332 rtn.off = (unsigned int) &exp->un.u_const.value;
335 case EXPR_TYPE_SYMBOL:
336 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
343 case EXPR_TYPE_PSTRUCT:
344 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
345 if( exp1.type == NULL )
349 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
355 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
356 &exp->un.structure.result);
358 case EXPR_TYPE_STRUCT:
359 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
360 if( exp1.type == NULL )
365 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
366 &exp->un.structure.result);
370 * First, evaluate all of the arguments. If any of them are not
371 * evaluable, then bail.
373 for(i=0; i < exp->un.call.nargs; i++)
375 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
376 if( exp1.type == NULL )
380 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
384 * Now look up the address of the function itself.
386 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
388 fprintf(stderr, "Failed to find symbol\n");
392 fptr = (int (*)()) rtn.off;
393 switch(exp->un.call.nargs)
396 exp->un.call.result = (*fptr)();
399 exp->un.call.result = (*fptr)(cexp[0]);
402 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
405 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
408 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
411 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
414 rtn.type = DEBUG_TypeInt;
415 rtn.off = (unsigned int) &exp->un.call.result;
417 case EXPR_TYPE_REGISTER:
418 rtn.type = DEBUG_TypeIntConst;
419 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
420 rtn.off = (unsigned int) &exp->un.rgister.result;
423 case EXPR_TYPE_BINOP:
424 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
425 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
426 if( exp1.type == NULL || exp2.type == NULL )
430 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
432 rtn.type = exp1.type;
436 rtn.type = DEBUG_TypeInt;
438 rtn.off = (unsigned int) &exp->un.binop.result;
439 switch(exp->un.binop.binop_type)
442 type1 = DEBUG_GetPointerType(exp1.type);
443 type2 = DEBUG_GetPointerType(exp2.type);
446 if( type1 != NULL && type2 != NULL )
450 else if( type1 != NULL )
452 scale2 = DEBUG_GetObjectSize(type1);
453 rtn.type = exp1.type;
455 else if( type2 != NULL )
457 scale1 = DEBUG_GetObjectSize(type2);
458 rtn.type = exp2.type;
461 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
464 type1 = DEBUG_GetPointerType(exp1.type);
465 type2 = DEBUG_GetPointerType(exp2.type);
469 if( type1 != NULL && type2 != NULL )
475 scale3 = DEBUG_GetObjectSize(type1);
477 else if( type1 != NULL )
479 scale2 = DEBUG_GetObjectSize(type1);
480 rtn.type = exp1.type;
483 else if( type2 != NULL )
485 scale1 = DEBUG_GetObjectSize(type2);
486 rtn.type = exp2.type;
489 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
493 exp->un.binop.result = VAL(exp2);
497 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
501 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
505 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
509 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
513 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
517 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
521 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
525 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
529 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
533 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
537 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
541 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
545 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
549 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
555 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
568 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
578 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
585 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
586 if( exp1.type == NULL )
590 rtn.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.binop.binop_type)
603 exp->un.unop.result = -VAL(exp1);
607 exp->un.unop.result = !VAL(exp1);
611 exp->un.unop.result = ~VAL(exp1);
615 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
617 case EXP_OP_FORCE_DEREF:
619 rtn.off = *(unsigned int *) exp1.off;
623 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
624 exp->un.unop.result = exp1.off;
629 fprintf(stderr,"Unexpected expression.\n");
639 DEBUG_DisplayExpr(struct expr * exp)
647 fprintf(stderr, "((");
648 DEBUG_PrintTypeCast(exp->un.cast.cast);
649 fprintf(stderr, ")");
650 DEBUG_DisplayExpr(exp->un.cast.expr);
651 fprintf(stderr, ")");
653 case EXPR_TYPE_REGISTER:
654 DEBUG_PrintRegister(exp->un.rgister.reg);
656 case EXPR_TYPE_US_CONST:
657 fprintf(stderr, "%ud", exp->un.u_const.value);
659 case EXPR_TYPE_CONST:
660 fprintf(stderr, "%d", exp->un.u_const.value);
662 case EXPR_TYPE_STRING:
663 fprintf(stderr, "\"%s\"", exp->un.string.str);
665 case EXPR_TYPE_SYMBOL:
666 fprintf(stderr, "%s" , exp->un.symbol.name);
668 case EXPR_TYPE_PSTRUCT:
669 DEBUG_DisplayExpr(exp->un.structure.exp1);
670 fprintf(stderr, "->%s", exp->un.structure.element_name);
672 case EXPR_TYPE_STRUCT:
673 DEBUG_DisplayExpr(exp->un.structure.exp1);
674 fprintf(stderr, ".%s", exp->un.structure.element_name);
678 * First, evaluate all of the arguments. If any of them are not
679 * evaluable, then bail.
681 fprintf(stderr, "%s(",exp->un.call.funcname);
682 for(i=0; i < exp->un.call.nargs; i++)
684 DEBUG_DisplayExpr(exp->un.call.arg[i]);
685 if( i != exp->un.call.nargs - 1 )
687 fprintf(stderr, ", ");
690 fprintf(stderr, ")");
692 case EXPR_TYPE_BINOP:
693 fprintf(stderr, "( ");
694 DEBUG_DisplayExpr(exp->un.binop.exp1);
695 switch(exp->un.binop.binop_type)
698 fprintf(stderr, " + ");
701 fprintf(stderr, " - ");
704 fprintf(stderr, ":");
707 fprintf(stderr, " || ");
710 fprintf(stderr, " && ");
713 fprintf(stderr, " | ");
716 fprintf(stderr, " & ");
719 fprintf(stderr, " ^ ");
722 fprintf(stderr, " == ");
725 fprintf(stderr, " > ");
728 fprintf(stderr, " < ");
731 fprintf(stderr, " >= ");
734 fprintf(stderr, " <= ");
737 fprintf(stderr, " != ");
740 fprintf(stderr, " << ");
743 fprintf(stderr, " >> ");
746 fprintf(stderr, " * ");
749 fprintf(stderr, " / ");
752 fprintf(stderr, " %% ");
755 fprintf(stderr, "[");
760 DEBUG_DisplayExpr(exp->un.binop.exp2);
761 if( exp->un.binop.binop_type == EXP_OP_ARR )
763 fprintf(stderr, "]");
765 fprintf(stderr, " )");
768 switch(exp->un.binop.binop_type)
771 fprintf(stderr, "-");
774 fprintf(stderr, "!");
777 fprintf(stderr, "~");
780 fprintf(stderr, "*");
783 fprintf(stderr, "&");
786 DEBUG_DisplayExpr(exp->un.unop.exp1);
789 fprintf(stderr,"Unexpected expression.\n");
798 DEBUG_CloneExpr(struct expr * exp)
803 rtn = (struct expr *) xmalloc(sizeof(struct expr));
806 * First copy the contents of the expression itself.
814 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
816 case EXPR_TYPE_REGISTER:
817 case EXPR_TYPE_US_CONST:
818 case EXPR_TYPE_CONST:
820 case EXPR_TYPE_STRING:
821 rtn->un.string.str = xstrdup(exp->un.string.str);
823 case EXPR_TYPE_SYMBOL:
824 rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
826 case EXPR_TYPE_PSTRUCT:
827 case EXPR_TYPE_STRUCT:
828 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
829 rtn->un.structure.element_name = xstrdup(exp->un.structure.element_name);
833 * First, evaluate all of the arguments. If any of them are not
834 * evaluable, then bail.
836 for(i=0; i < exp->un.call.nargs; i++)
838 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
840 rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
842 case EXPR_TYPE_BINOP:
843 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
844 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
847 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
850 fprintf(stderr,"Unexpected expression.\n");
860 * Recursively go through an expression tree and free all memory associated
864 DEBUG_FreeExpr(struct expr * exp)
871 DEBUG_FreeExpr(exp->un.cast.expr);
873 case EXPR_TYPE_REGISTER:
874 case EXPR_TYPE_US_CONST:
875 case EXPR_TYPE_CONST:
877 case EXPR_TYPE_STRING:
878 free((char *) exp->un.string.str);
880 case EXPR_TYPE_SYMBOL:
881 free((char *) exp->un.symbol.name);
883 case EXPR_TYPE_PSTRUCT:
884 case EXPR_TYPE_STRUCT:
885 DEBUG_FreeExpr(exp->un.structure.exp1);
886 free((char *) exp->un.structure.element_name);
890 * First, evaluate all of the arguments. If any of them are not
891 * evaluable, then bail.
893 for(i=0; i < exp->un.call.nargs; i++)
895 DEBUG_FreeExpr(exp->un.call.arg[i]);
897 free((char *) exp->un.call.funcname);
899 case EXPR_TYPE_BINOP:
900 DEBUG_FreeExpr(exp->un.binop.exp1);
901 DEBUG_FreeExpr(exp->un.binop.exp2);
904 DEBUG_FreeExpr(exp->un.unop.exp1);
907 fprintf(stderr,"Unexpected expression.\n");