2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
13 #include <sys/types.h>
15 #include "wine/winbase16.h"
18 #include "selectors.h"
75 struct datatype * cast;
82 const char * element_name;
94 const char * funcname;
103 #define EXPR_TYPE_CONST 0
104 #define EXPR_TYPE_US_CONST 1
105 #define EXPR_TYPE_SYMBOL 2
106 #define EXPR_TYPE_REGISTER 3
107 #define EXPR_TYPE_BINOP 4
108 #define EXPR_TYPE_UNOP 5
109 #define EXPR_TYPE_STRUCT 6
110 #define EXPR_TYPE_PSTRUCT 7
111 #define EXPR_TYPE_ARRAY 8
112 #define EXPR_TYPE_CALL 9
113 #define EXPR_TYPE_STRING 10
114 #define EXPR_TYPE_CAST 11
116 static char expr_list[4096];
117 static int next_expr_free = 0;
120 * This is how we turn an expression address into the actual value.
121 * This works well in the 32 bit domain - not sure at all about the
124 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
132 rtn = (struct expr *) &expr_list[next_expr_free];
134 next_expr_free += sizeof(struct expr);
145 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
149 ex = DEBUG_GetFreeExpr();
151 ex->type = EXPR_TYPE_CAST;
152 ex->un.cast.cast = dt;
153 ex->un.cast.expr = exp;
158 DEBUG_RegisterExpr(enum debug_regs regno)
162 ex = DEBUG_GetFreeExpr();
164 ex->type = EXPR_TYPE_REGISTER;
165 ex->un.rgister.reg = regno;
170 DEBUG_SymbolExpr(const char * name)
174 ex = DEBUG_GetFreeExpr();
176 ex->type = EXPR_TYPE_SYMBOL;
177 ex->un.symbol.name = name;
182 DEBUG_ConstExpr(int value)
186 ex = DEBUG_GetFreeExpr();
188 ex->type = EXPR_TYPE_CONST;
189 ex->un.constant.value = value;
194 DEBUG_StringExpr(const char * str)
198 ex = DEBUG_GetFreeExpr();
200 ex->type = EXPR_TYPE_STRING;
201 ex->un.string.str = str+1;
202 pnt = strrchr(ex->un.string.str, '"');
211 DEBUG_USConstExpr(unsigned int value)
215 ex = DEBUG_GetFreeExpr();
217 ex->type = EXPR_TYPE_CONST;
218 ex->un.u_const.value = value;
223 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
227 ex = DEBUG_GetFreeExpr();
229 ex->type = EXPR_TYPE_BINOP;
230 ex->un.binop.binop_type = operator_type;
231 ex->un.binop.exp1 = exp1;
232 ex->un.binop.exp2 = exp2;
237 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
241 ex = DEBUG_GetFreeExpr();
243 ex->type = EXPR_TYPE_UNOP;
244 ex->un.unop.unop_type = operator_type;
245 ex->un.unop.exp1 = exp1;
250 DEBUG_StructExpr(struct expr * exp, const char * element)
254 ex = DEBUG_GetFreeExpr();
256 ex->type = EXPR_TYPE_STRUCT;
257 ex->un.structure.exp1 = exp;
258 ex->un.structure.element_name = element;
263 DEBUG_StructPExpr(struct expr * exp, const char * element)
267 ex = DEBUG_GetFreeExpr();
269 ex->type = EXPR_TYPE_PSTRUCT;
270 ex->un.structure.exp1 = exp;
271 ex->un.structure.element_name = element;
276 DEBUG_CallExpr(const char * funcname, int nargs, ...)
282 ex = DEBUG_GetFreeExpr();
284 ex->type = EXPR_TYPE_CALL;
285 ex->un.call.funcname = funcname;
286 ex->un.call.nargs = nargs;
289 for(i=0; i < nargs; i++)
291 ex->un.call.arg[i] = va_arg(ap, struct expr *);
298 DEBUG_EvalExpr(struct expr * exp)
304 unsigned int cexp[5];
309 struct datatype * type1;
310 struct datatype * type2;
319 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
320 rtn.type = exp->un.cast.cast;
322 case EXPR_TYPE_STRING:
323 rtn.type = DEBUG_TypeString;
324 rtn.off = (unsigned int) &exp->un.string.str;
327 case EXPR_TYPE_CONST:
328 rtn.type = DEBUG_TypeIntConst;
329 rtn.off = (unsigned int) &exp->un.constant.value;
332 case EXPR_TYPE_US_CONST:
333 rtn.type = DEBUG_TypeUSInt;
334 rtn.off = (unsigned int) &exp->un.u_const.value;
337 case EXPR_TYPE_SYMBOL:
338 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
345 case EXPR_TYPE_PSTRUCT:
346 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
347 if( exp1.type == NULL )
351 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
357 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
358 &exp->un.structure.result);
360 case EXPR_TYPE_STRUCT:
361 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
362 if( exp1.type == NULL )
367 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
368 &exp->un.structure.result);
372 * First, evaluate all of the arguments. If any of them are not
373 * evaluable, then bail.
375 for(i=0; i < exp->un.call.nargs; i++)
377 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
378 if( exp1.type == NULL )
382 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
386 * Now look up the address of the function itself.
388 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
390 fprintf(stderr, "Failed to find symbol\n");
394 fptr = (int (*)()) rtn.off;
395 switch(exp->un.call.nargs)
398 exp->un.call.result = (*fptr)();
401 exp->un.call.result = (*fptr)(cexp[0]);
404 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
407 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
410 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
413 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
416 rtn.type = DEBUG_TypeInt;
417 rtn.off = (unsigned int) &exp->un.call.result;
419 case EXPR_TYPE_REGISTER:
420 rtn.type = DEBUG_TypeIntConst;
421 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
422 rtn.off = (unsigned int) &exp->un.rgister.result;
423 if( exp->un.rgister.reg == REG_EIP )
424 rtn.seg = CS_reg(&DEBUG_context);
426 rtn.seg = DS_reg(&DEBUG_context);
427 DBG_FIX_ADDR_SEG( &rtn, 0 );
429 case EXPR_TYPE_BINOP:
430 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
431 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
432 if( exp1.type == NULL || exp2.type == NULL )
436 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
438 rtn.type = exp1.type;
442 rtn.type = DEBUG_TypeInt;
444 rtn.off = (unsigned int) &exp->un.binop.result;
445 switch(exp->un.binop.binop_type)
448 type1 = DEBUG_GetPointerType(exp1.type);
449 type2 = DEBUG_GetPointerType(exp2.type);
452 if( type1 != NULL && type2 != NULL )
456 else if( type1 != NULL )
458 scale2 = DEBUG_GetObjectSize(type1);
459 rtn.type = exp1.type;
461 else if( type2 != NULL )
463 scale1 = DEBUG_GetObjectSize(type2);
464 rtn.type = exp2.type;
467 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
470 type1 = DEBUG_GetPointerType(exp1.type);
471 type2 = DEBUG_GetPointerType(exp2.type);
475 if( type1 != NULL && type2 != NULL )
481 scale3 = DEBUG_GetObjectSize(type1);
483 else if( type1 != NULL )
485 scale2 = DEBUG_GetObjectSize(type1);
486 rtn.type = exp1.type;
489 else if( type2 != NULL )
491 scale1 = DEBUG_GetObjectSize(type2);
492 rtn.type = exp2.type;
495 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
499 exp->un.binop.result = VAL(exp2);
500 if (ISV86(&DEBUG_context)) {
501 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
502 rtn.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
503 GlobalUnlock16( GetCurrentTask() );
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 = (VAL(exp1) != VAL(exp2));
552 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
556 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
560 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
566 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
579 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
589 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
596 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
597 if( exp1.type == NULL )
601 rtn.off = (unsigned int) &exp->un.unop.result;
602 if( exp1.type == DEBUG_TypeIntConst )
604 rtn.type = exp1.type;
608 rtn.type = DEBUG_TypeInt;
610 switch(exp->un.binop.binop_type)
614 exp->un.unop.result = -VAL(exp1);
618 exp->un.unop.result = !VAL(exp1);
622 exp->un.unop.result = ~VAL(exp1);
626 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
628 case EXP_OP_FORCE_DEREF:
630 rtn.off = *(unsigned int *) exp1.off;
634 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
635 exp->un.unop.result = exp1.off;
640 fprintf(stderr,"Unexpected expression.\n");
650 DEBUG_DisplayExpr(struct expr * exp)
658 fprintf(stderr, "((");
659 DEBUG_PrintTypeCast(exp->un.cast.cast);
660 fprintf(stderr, ")");
661 DEBUG_DisplayExpr(exp->un.cast.expr);
662 fprintf(stderr, ")");
664 case EXPR_TYPE_REGISTER:
665 DEBUG_PrintRegister(exp->un.rgister.reg);
667 case EXPR_TYPE_US_CONST:
668 fprintf(stderr, "%ud", exp->un.u_const.value);
670 case EXPR_TYPE_CONST:
671 fprintf(stderr, "%d", exp->un.u_const.value);
673 case EXPR_TYPE_STRING:
674 fprintf(stderr, "\"%s\"", exp->un.string.str);
676 case EXPR_TYPE_SYMBOL:
677 fprintf(stderr, "%s" , exp->un.symbol.name);
679 case EXPR_TYPE_PSTRUCT:
680 DEBUG_DisplayExpr(exp->un.structure.exp1);
681 fprintf(stderr, "->%s", exp->un.structure.element_name);
683 case EXPR_TYPE_STRUCT:
684 DEBUG_DisplayExpr(exp->un.structure.exp1);
685 fprintf(stderr, ".%s", exp->un.structure.element_name);
689 * First, evaluate all of the arguments. If any of them are not
690 * evaluable, then bail.
692 fprintf(stderr, "%s(",exp->un.call.funcname);
693 for(i=0; i < exp->un.call.nargs; i++)
695 DEBUG_DisplayExpr(exp->un.call.arg[i]);
696 if( i != exp->un.call.nargs - 1 )
698 fprintf(stderr, ", ");
701 fprintf(stderr, ")");
703 case EXPR_TYPE_BINOP:
704 fprintf(stderr, "( ");
705 DEBUG_DisplayExpr(exp->un.binop.exp1);
706 switch(exp->un.binop.binop_type)
709 fprintf(stderr, " + ");
712 fprintf(stderr, " - ");
715 fprintf(stderr, ":");
718 fprintf(stderr, " || ");
721 fprintf(stderr, " && ");
724 fprintf(stderr, " | ");
727 fprintf(stderr, " & ");
730 fprintf(stderr, " ^ ");
733 fprintf(stderr, " == ");
736 fprintf(stderr, " > ");
739 fprintf(stderr, " < ");
742 fprintf(stderr, " >= ");
745 fprintf(stderr, " <= ");
748 fprintf(stderr, " != ");
751 fprintf(stderr, " << ");
754 fprintf(stderr, " >> ");
757 fprintf(stderr, " * ");
760 fprintf(stderr, " / ");
763 fprintf(stderr, " %% ");
766 fprintf(stderr, "[");
771 DEBUG_DisplayExpr(exp->un.binop.exp2);
772 if( exp->un.binop.binop_type == EXP_OP_ARR )
774 fprintf(stderr, "]");
776 fprintf(stderr, " )");
779 switch(exp->un.binop.binop_type)
782 fprintf(stderr, "-");
785 fprintf(stderr, "!");
788 fprintf(stderr, "~");
791 fprintf(stderr, "*");
794 fprintf(stderr, "&");
797 DEBUG_DisplayExpr(exp->un.unop.exp1);
800 fprintf(stderr,"Unexpected expression.\n");
809 DEBUG_CloneExpr(struct expr * exp)
814 rtn = (struct expr *) xmalloc(sizeof(struct expr));
817 * First copy the contents of the expression itself.
825 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
827 case EXPR_TYPE_REGISTER:
828 case EXPR_TYPE_US_CONST:
829 case EXPR_TYPE_CONST:
831 case EXPR_TYPE_STRING:
832 rtn->un.string.str = xstrdup(exp->un.string.str);
834 case EXPR_TYPE_SYMBOL:
835 rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
837 case EXPR_TYPE_PSTRUCT:
838 case EXPR_TYPE_STRUCT:
839 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
840 rtn->un.structure.element_name = xstrdup(exp->un.structure.element_name);
844 * First, evaluate all of the arguments. If any of them are not
845 * evaluable, then bail.
847 for(i=0; i < exp->un.call.nargs; i++)
849 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
851 rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
853 case EXPR_TYPE_BINOP:
854 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
855 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
858 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
861 fprintf(stderr,"Unexpected expression.\n");
871 * Recursively go through an expression tree and free all memory associated
875 DEBUG_FreeExpr(struct expr * exp)
882 DEBUG_FreeExpr(exp->un.cast.expr);
884 case EXPR_TYPE_REGISTER:
885 case EXPR_TYPE_US_CONST:
886 case EXPR_TYPE_CONST:
888 case EXPR_TYPE_STRING:
889 free((char *) exp->un.string.str);
891 case EXPR_TYPE_SYMBOL:
892 free((char *) exp->un.symbol.name);
894 case EXPR_TYPE_PSTRUCT:
895 case EXPR_TYPE_STRUCT:
896 DEBUG_FreeExpr(exp->un.structure.exp1);
897 free((char *) exp->un.structure.element_name);
901 * First, evaluate all of the arguments. If any of them are not
902 * evaluable, then bail.
904 for(i=0; i < exp->un.call.nargs; i++)
906 DEBUG_FreeExpr(exp->un.call.arg[i]);
908 free((char *) exp->un.call.funcname);
910 case EXPR_TYPE_BINOP:
911 DEBUG_FreeExpr(exp->un.binop.exp1);
912 DEBUG_FreeExpr(exp->un.binop.exp2);
915 DEBUG_FreeExpr(exp->un.unop.exp1);
918 fprintf(stderr,"Unexpected expression.\n");