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;
421 if( exp->un.rgister.reg == REG_EIP )
423 rtn.seg = CS_reg(&DEBUG_context);
427 rtn.seg = DS_reg(&DEBUG_context);
430 case EXPR_TYPE_BINOP:
431 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
432 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
433 if( exp1.type == NULL || exp2.type == NULL )
437 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
439 rtn.type = exp1.type;
443 rtn.type = DEBUG_TypeInt;
445 rtn.off = (unsigned int) &exp->un.binop.result;
446 switch(exp->un.binop.binop_type)
449 type1 = DEBUG_GetPointerType(exp1.type);
450 type2 = DEBUG_GetPointerType(exp2.type);
453 if( type1 != NULL && type2 != NULL )
457 else if( type1 != NULL )
459 scale2 = DEBUG_GetObjectSize(type1);
460 rtn.type = exp1.type;
462 else if( type2 != NULL )
464 scale1 = DEBUG_GetObjectSize(type2);
465 rtn.type = exp2.type;
468 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
471 type1 = DEBUG_GetPointerType(exp1.type);
472 type2 = DEBUG_GetPointerType(exp2.type);
476 if( type1 != NULL && type2 != NULL )
482 scale3 = DEBUG_GetObjectSize(type1);
484 else if( type1 != NULL )
486 scale2 = DEBUG_GetObjectSize(type1);
487 rtn.type = exp1.type;
490 else if( type2 != NULL )
492 scale1 = DEBUG_GetObjectSize(type2);
493 rtn.type = exp2.type;
496 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
500 exp->un.binop.result = VAL(exp2);
504 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
508 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
512 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
516 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
520 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
524 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
528 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
532 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
536 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
540 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
544 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
548 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
552 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
556 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
562 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
575 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
585 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
592 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
593 if( exp1.type == NULL )
597 rtn.off = (unsigned int) &exp->un.unop.result;
598 if( exp1.type == DEBUG_TypeIntConst )
600 rtn.type = exp1.type;
604 rtn.type = DEBUG_TypeInt;
606 switch(exp->un.binop.binop_type)
610 exp->un.unop.result = -VAL(exp1);
614 exp->un.unop.result = !VAL(exp1);
618 exp->un.unop.result = ~VAL(exp1);
622 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
624 case EXP_OP_FORCE_DEREF:
626 rtn.off = *(unsigned int *) exp1.off;
630 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
631 exp->un.unop.result = exp1.off;
636 fprintf(stderr,"Unexpected expression.\n");
646 DEBUG_DisplayExpr(struct expr * exp)
654 fprintf(stderr, "((");
655 DEBUG_PrintTypeCast(exp->un.cast.cast);
656 fprintf(stderr, ")");
657 DEBUG_DisplayExpr(exp->un.cast.expr);
658 fprintf(stderr, ")");
660 case EXPR_TYPE_REGISTER:
661 DEBUG_PrintRegister(exp->un.rgister.reg);
663 case EXPR_TYPE_US_CONST:
664 fprintf(stderr, "%ud", exp->un.u_const.value);
666 case EXPR_TYPE_CONST:
667 fprintf(stderr, "%d", exp->un.u_const.value);
669 case EXPR_TYPE_STRING:
670 fprintf(stderr, "\"%s\"", exp->un.string.str);
672 case EXPR_TYPE_SYMBOL:
673 fprintf(stderr, "%s" , exp->un.symbol.name);
675 case EXPR_TYPE_PSTRUCT:
676 DEBUG_DisplayExpr(exp->un.structure.exp1);
677 fprintf(stderr, "->%s", exp->un.structure.element_name);
679 case EXPR_TYPE_STRUCT:
680 DEBUG_DisplayExpr(exp->un.structure.exp1);
681 fprintf(stderr, ".%s", exp->un.structure.element_name);
685 * First, evaluate all of the arguments. If any of them are not
686 * evaluable, then bail.
688 fprintf(stderr, "%s(",exp->un.call.funcname);
689 for(i=0; i < exp->un.call.nargs; i++)
691 DEBUG_DisplayExpr(exp->un.call.arg[i]);
692 if( i != exp->un.call.nargs - 1 )
694 fprintf(stderr, ", ");
697 fprintf(stderr, ")");
699 case EXPR_TYPE_BINOP:
700 fprintf(stderr, "( ");
701 DEBUG_DisplayExpr(exp->un.binop.exp1);
702 switch(exp->un.binop.binop_type)
705 fprintf(stderr, " + ");
708 fprintf(stderr, " - ");
711 fprintf(stderr, ":");
714 fprintf(stderr, " || ");
717 fprintf(stderr, " && ");
720 fprintf(stderr, " | ");
723 fprintf(stderr, " & ");
726 fprintf(stderr, " ^ ");
729 fprintf(stderr, " == ");
732 fprintf(stderr, " > ");
735 fprintf(stderr, " < ");
738 fprintf(stderr, " >= ");
741 fprintf(stderr, " <= ");
744 fprintf(stderr, " != ");
747 fprintf(stderr, " << ");
750 fprintf(stderr, " >> ");
753 fprintf(stderr, " * ");
756 fprintf(stderr, " / ");
759 fprintf(stderr, " %% ");
762 fprintf(stderr, "[");
767 DEBUG_DisplayExpr(exp->un.binop.exp2);
768 if( exp->un.binop.binop_type == EXP_OP_ARR )
770 fprintf(stderr, "]");
772 fprintf(stderr, " )");
775 switch(exp->un.binop.binop_type)
778 fprintf(stderr, "-");
781 fprintf(stderr, "!");
784 fprintf(stderr, "~");
787 fprintf(stderr, "*");
790 fprintf(stderr, "&");
793 DEBUG_DisplayExpr(exp->un.unop.exp1);
796 fprintf(stderr,"Unexpected expression.\n");
805 DEBUG_CloneExpr(struct expr * exp)
810 rtn = (struct expr *) xmalloc(sizeof(struct expr));
813 * First copy the contents of the expression itself.
821 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
823 case EXPR_TYPE_REGISTER:
824 case EXPR_TYPE_US_CONST:
825 case EXPR_TYPE_CONST:
827 case EXPR_TYPE_STRING:
828 rtn->un.string.str = xstrdup(exp->un.string.str);
830 case EXPR_TYPE_SYMBOL:
831 rtn->un.symbol.name = xstrdup(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 = xstrdup(exp->un.structure.element_name);
840 * First, evaluate all of the arguments. If any of them are not
841 * evaluable, then bail.
843 for(i=0; i < exp->un.call.nargs; i++)
845 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
847 rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
849 case EXPR_TYPE_BINOP:
850 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
851 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
854 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
857 fprintf(stderr,"Unexpected expression.\n");
867 * Recursively go through an expression tree and free all memory associated
871 DEBUG_FreeExpr(struct expr * exp)
878 DEBUG_FreeExpr(exp->un.cast.expr);
880 case EXPR_TYPE_REGISTER:
881 case EXPR_TYPE_US_CONST:
882 case EXPR_TYPE_CONST:
884 case EXPR_TYPE_STRING:
885 free((char *) exp->un.string.str);
887 case EXPR_TYPE_SYMBOL:
888 free((char *) exp->un.symbol.name);
890 case EXPR_TYPE_PSTRUCT:
891 case EXPR_TYPE_STRUCT:
892 DEBUG_FreeExpr(exp->un.structure.exp1);
893 free((char *) exp->un.structure.element_name);
897 * First, evaluate all of the arguments. If any of them are not
898 * evaluable, then bail.
900 for(i=0; i < exp->un.call.nargs; i++)
902 DEBUG_FreeExpr(exp->un.call.arg[i]);
904 free((char *) exp->un.call.funcname);
906 case EXPR_TYPE_BINOP:
907 DEBUG_FreeExpr(exp->un.binop.exp1);
908 DEBUG_FreeExpr(exp->un.binop.exp2);
911 DEBUG_FreeExpr(exp->un.unop.exp1);
914 fprintf(stderr,"Unexpected expression.\n");