2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
12 #include <sys/types.h>
16 #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 )
422 rtn.seg = CS_reg(&DEBUG_context);
424 rtn.seg = DS_reg(&DEBUG_context);
425 DBG_FIX_ADDR_SEG( &rtn, 0 );
427 case EXPR_TYPE_BINOP:
428 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
429 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
430 if( exp1.type == NULL || exp2.type == NULL )
434 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
436 rtn.type = exp1.type;
440 rtn.type = DEBUG_TypeInt;
442 rtn.off = (unsigned int) &exp->un.binop.result;
443 switch(exp->un.binop.binop_type)
446 type1 = DEBUG_GetPointerType(exp1.type);
447 type2 = DEBUG_GetPointerType(exp2.type);
450 if( type1 != NULL && type2 != NULL )
454 else if( type1 != NULL )
456 scale2 = DEBUG_GetObjectSize(type1);
457 rtn.type = exp1.type;
459 else if( type2 != NULL )
461 scale1 = DEBUG_GetObjectSize(type2);
462 rtn.type = exp2.type;
465 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
468 type1 = DEBUG_GetPointerType(exp1.type);
469 type2 = DEBUG_GetPointerType(exp2.type);
473 if( type1 != NULL && type2 != NULL )
479 scale3 = DEBUG_GetObjectSize(type1);
481 else if( type1 != NULL )
483 scale2 = DEBUG_GetObjectSize(type1);
484 rtn.type = exp1.type;
487 else if( type2 != NULL )
489 scale1 = DEBUG_GetObjectSize(type2);
490 rtn.type = exp2.type;
493 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
497 exp->un.binop.result = VAL(exp2);
498 if (ISV86(&DEBUG_context)) {
499 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
500 rtn.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
501 GlobalUnlock16( GetCurrentTask() );
506 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
510 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
514 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
518 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
522 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
526 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
530 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
534 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
538 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
542 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
546 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
550 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
554 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
558 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
564 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
577 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
587 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
594 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
595 if( exp1.type == NULL )
599 rtn.off = (unsigned int) &exp->un.unop.result;
600 if( exp1.type == DEBUG_TypeIntConst )
602 rtn.type = exp1.type;
606 rtn.type = DEBUG_TypeInt;
608 switch(exp->un.binop.binop_type)
612 exp->un.unop.result = -VAL(exp1);
616 exp->un.unop.result = !VAL(exp1);
620 exp->un.unop.result = ~VAL(exp1);
624 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
626 case EXP_OP_FORCE_DEREF:
628 rtn.off = *(unsigned int *) exp1.off;
632 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
633 exp->un.unop.result = exp1.off;
638 fprintf(stderr,"Unexpected expression.\n");
648 DEBUG_DisplayExpr(struct expr * exp)
656 fprintf(stderr, "((");
657 DEBUG_PrintTypeCast(exp->un.cast.cast);
658 fprintf(stderr, ")");
659 DEBUG_DisplayExpr(exp->un.cast.expr);
660 fprintf(stderr, ")");
662 case EXPR_TYPE_REGISTER:
663 DEBUG_PrintRegister(exp->un.rgister.reg);
665 case EXPR_TYPE_US_CONST:
666 fprintf(stderr, "%ud", exp->un.u_const.value);
668 case EXPR_TYPE_CONST:
669 fprintf(stderr, "%d", exp->un.u_const.value);
671 case EXPR_TYPE_STRING:
672 fprintf(stderr, "\"%s\"", exp->un.string.str);
674 case EXPR_TYPE_SYMBOL:
675 fprintf(stderr, "%s" , exp->un.symbol.name);
677 case EXPR_TYPE_PSTRUCT:
678 DEBUG_DisplayExpr(exp->un.structure.exp1);
679 fprintf(stderr, "->%s", exp->un.structure.element_name);
681 case EXPR_TYPE_STRUCT:
682 DEBUG_DisplayExpr(exp->un.structure.exp1);
683 fprintf(stderr, ".%s", exp->un.structure.element_name);
687 * First, evaluate all of the arguments. If any of them are not
688 * evaluable, then bail.
690 fprintf(stderr, "%s(",exp->un.call.funcname);
691 for(i=0; i < exp->un.call.nargs; i++)
693 DEBUG_DisplayExpr(exp->un.call.arg[i]);
694 if( i != exp->un.call.nargs - 1 )
696 fprintf(stderr, ", ");
699 fprintf(stderr, ")");
701 case EXPR_TYPE_BINOP:
702 fprintf(stderr, "( ");
703 DEBUG_DisplayExpr(exp->un.binop.exp1);
704 switch(exp->un.binop.binop_type)
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, " * ");
758 fprintf(stderr, " / ");
761 fprintf(stderr, " %% ");
764 fprintf(stderr, "[");
769 DEBUG_DisplayExpr(exp->un.binop.exp2);
770 if( exp->un.binop.binop_type == EXP_OP_ARR )
772 fprintf(stderr, "]");
774 fprintf(stderr, " )");
777 switch(exp->un.binop.binop_type)
780 fprintf(stderr, "-");
783 fprintf(stderr, "!");
786 fprintf(stderr, "~");
789 fprintf(stderr, "*");
792 fprintf(stderr, "&");
795 DEBUG_DisplayExpr(exp->un.unop.exp1);
798 fprintf(stderr,"Unexpected expression.\n");
807 DEBUG_CloneExpr(struct expr * exp)
812 rtn = (struct expr *) xmalloc(sizeof(struct expr));
815 * First copy the contents of the expression itself.
823 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
825 case EXPR_TYPE_REGISTER:
826 case EXPR_TYPE_US_CONST:
827 case EXPR_TYPE_CONST:
829 case EXPR_TYPE_STRING:
830 rtn->un.string.str = xstrdup(exp->un.string.str);
832 case EXPR_TYPE_SYMBOL:
833 rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
835 case EXPR_TYPE_PSTRUCT:
836 case EXPR_TYPE_STRUCT:
837 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
838 rtn->un.structure.element_name = xstrdup(exp->un.structure.element_name);
842 * First, evaluate all of the arguments. If any of them are not
843 * evaluable, then bail.
845 for(i=0; i < exp->un.call.nargs; i++)
847 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
849 rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
851 case EXPR_TYPE_BINOP:
852 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
853 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
856 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
859 fprintf(stderr,"Unexpected expression.\n");
869 * Recursively go through an expression tree and free all memory associated
873 DEBUG_FreeExpr(struct expr * exp)
880 DEBUG_FreeExpr(exp->un.cast.expr);
882 case EXPR_TYPE_REGISTER:
883 case EXPR_TYPE_US_CONST:
884 case EXPR_TYPE_CONST:
886 case EXPR_TYPE_STRING:
887 free((char *) exp->un.string.str);
889 case EXPR_TYPE_SYMBOL:
890 free((char *) exp->un.symbol.name);
892 case EXPR_TYPE_PSTRUCT:
893 case EXPR_TYPE_STRUCT:
894 DEBUG_FreeExpr(exp->un.structure.exp1);
895 free((char *) exp->un.structure.element_name);
899 * First, evaluate all of the arguments. If any of them are not
900 * evaluable, then bail.
902 for(i=0; i < exp->un.call.nargs; i++)
904 DEBUG_FreeExpr(exp->un.call.arg[i]);
906 free((char *) exp->un.call.funcname);
908 case EXPR_TYPE_BINOP:
909 DEBUG_FreeExpr(exp->un.binop.exp1);
910 DEBUG_FreeExpr(exp->un.binop.exp2);
913 DEBUG_FreeExpr(exp->un.unop.exp1);
916 fprintf(stderr,"Unexpected expression.\n");