2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
13 #include "wine/winbase16.h"
69 struct datatype * cast;
76 const char * element_name;
88 const char * funcname;
97 #define EXPR_TYPE_CONST 0
98 #define EXPR_TYPE_US_CONST 1
99 #define EXPR_TYPE_SYMBOL 2
100 #define EXPR_TYPE_REGISTER 3
101 #define EXPR_TYPE_BINOP 4
102 #define EXPR_TYPE_UNOP 5
103 #define EXPR_TYPE_STRUCT 6
104 #define EXPR_TYPE_PSTRUCT 7
105 #define EXPR_TYPE_ARRAY 8
106 #define EXPR_TYPE_CALL 9
107 #define EXPR_TYPE_STRING 10
108 #define EXPR_TYPE_CAST 11
110 static char expr_list[4096];
111 static int next_expr_free = 0;
114 * This is how we turn an expression address into the actual value.
115 * This works well in the 32 bit domain - not sure at all about the
118 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
126 rtn = (struct expr *) &expr_list[next_expr_free];
128 next_expr_free += sizeof(struct expr);
129 assert(next_expr_free < sizeof(expr_list));
141 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
145 ex = DEBUG_GetFreeExpr();
147 ex->type = EXPR_TYPE_CAST;
148 ex->un.cast.cast = dt;
149 ex->un.cast.expr = exp;
154 DEBUG_RegisterExpr(enum debug_regs regno)
158 ex = DEBUG_GetFreeExpr();
160 ex->type = EXPR_TYPE_REGISTER;
161 ex->un.rgister.reg = regno;
166 DEBUG_SymbolExpr(const char * name)
170 ex = DEBUG_GetFreeExpr();
172 ex->type = EXPR_TYPE_SYMBOL;
173 ex->un.symbol.name = name;
178 DEBUG_ConstExpr(int value)
182 ex = DEBUG_GetFreeExpr();
184 ex->type = EXPR_TYPE_CONST;
185 ex->un.constant.value = value;
190 DEBUG_StringExpr(const char * str)
194 ex = DEBUG_GetFreeExpr();
196 ex->type = EXPR_TYPE_STRING;
197 ex->un.string.str = str+1;
198 pnt = strrchr(ex->un.string.str, '"');
207 DEBUG_USConstExpr(unsigned int value)
211 ex = DEBUG_GetFreeExpr();
213 ex->type = EXPR_TYPE_CONST;
214 ex->un.u_const.value = value;
219 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
223 ex = DEBUG_GetFreeExpr();
225 ex->type = EXPR_TYPE_BINOP;
226 ex->un.binop.binop_type = operator_type;
227 ex->un.binop.exp1 = exp1;
228 ex->un.binop.exp2 = exp2;
233 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
237 ex = DEBUG_GetFreeExpr();
239 ex->type = EXPR_TYPE_UNOP;
240 ex->un.unop.unop_type = operator_type;
241 ex->un.unop.exp1 = exp1;
246 DEBUG_StructExpr(struct expr * exp, const char * element)
250 ex = DEBUG_GetFreeExpr();
252 ex->type = EXPR_TYPE_STRUCT;
253 ex->un.structure.exp1 = exp;
254 ex->un.structure.element_name = element;
259 DEBUG_StructPExpr(struct expr * exp, const char * element)
263 ex = DEBUG_GetFreeExpr();
265 ex->type = EXPR_TYPE_PSTRUCT;
266 ex->un.structure.exp1 = exp;
267 ex->un.structure.element_name = element;
272 DEBUG_CallExpr(const char * funcname, int nargs, ...)
278 ex = DEBUG_GetFreeExpr();
280 ex->type = EXPR_TYPE_CALL;
281 ex->un.call.funcname = funcname;
282 ex->un.call.nargs = nargs;
285 for(i=0; i < nargs; i++)
287 ex->un.call.arg[i] = va_arg(ap, struct expr *);
294 DEBUG_EvalExpr(struct expr * exp)
300 unsigned int cexp[5];
305 struct datatype * type1;
306 struct datatype * type2;
315 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
316 rtn.type = exp->un.cast.cast;
318 case EXPR_TYPE_STRING:
319 rtn.type = DEBUG_TypeString;
320 rtn.off = (unsigned int) &exp->un.string.str;
323 case EXPR_TYPE_CONST:
324 rtn.type = DEBUG_TypeIntConst;
325 rtn.off = (unsigned int) &exp->un.constant.value;
328 case EXPR_TYPE_US_CONST:
329 rtn.type = DEBUG_TypeUSInt;
330 rtn.off = (unsigned int) &exp->un.u_const.value;
333 case EXPR_TYPE_SYMBOL:
334 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
341 case EXPR_TYPE_PSTRUCT:
342 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
343 if( exp1.type == NULL )
347 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
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 )
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 fprintf(stderr, "Failed to find symbol\n");
390 fptr = (int (*)()) rtn.off;
391 switch(exp->un.call.nargs)
394 exp->un.call.result = (*fptr)();
397 exp->un.call.result = (*fptr)(cexp[0]);
400 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
403 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
406 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
409 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
412 rtn.type = DEBUG_TypeInt;
413 rtn.off = (unsigned int) &exp->un.call.result;
415 case EXPR_TYPE_REGISTER:
416 rtn.type = DEBUG_TypeIntConst;
417 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
418 rtn.off = (unsigned int) &exp->un.rgister.result;
419 if( exp->un.rgister.reg == REG_EIP )
420 rtn.seg = CS_reg(&DEBUG_context);
422 rtn.seg = DS_reg(&DEBUG_context);
423 DBG_FIX_ADDR_SEG( &rtn, 0 );
425 case EXPR_TYPE_BINOP:
426 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
427 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
428 if( exp1.type == NULL || exp2.type == NULL )
432 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
434 rtn.type = exp1.type;
438 rtn.type = DEBUG_TypeInt;
440 rtn.off = (unsigned int) &exp->un.binop.result;
441 switch(exp->un.binop.binop_type)
444 type1 = DEBUG_GetPointerType(exp1.type);
445 type2 = DEBUG_GetPointerType(exp2.type);
448 if( type1 != NULL && type2 != NULL )
452 else if( type1 != NULL )
454 scale2 = DEBUG_GetObjectSize(type1);
455 rtn.type = exp1.type;
457 else if( type2 != NULL )
459 scale1 = DEBUG_GetObjectSize(type2);
460 rtn.type = exp2.type;
463 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
466 type1 = DEBUG_GetPointerType(exp1.type);
467 type2 = DEBUG_GetPointerType(exp2.type);
471 if( type1 != NULL && type2 != NULL )
477 scale3 = DEBUG_GetObjectSize(type1);
479 else if( type1 != NULL )
481 scale2 = DEBUG_GetObjectSize(type1);
482 rtn.type = exp1.type;
485 else if( type2 != NULL )
487 scale1 = DEBUG_GetObjectSize(type2);
488 rtn.type = exp2.type;
491 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
495 exp->un.binop.result = VAL(exp2);
496 if (ISV86(&DEBUG_context)) {
497 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
498 rtn.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
499 GlobalUnlock16( GetCurrentTask() );
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.unop.unop_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.unop.unop_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 *) DBG_alloc(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 = 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);
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 = DBG_strdup(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 DBG_free((char *) exp->un.string.str);
887 case EXPR_TYPE_SYMBOL:
888 DBG_free((char *) exp->un.symbol.name);
890 case EXPR_TYPE_PSTRUCT:
891 case EXPR_TYPE_STRUCT:
892 DEBUG_FreeExpr(exp->un.structure.exp1);
893 DBG_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 DBG_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");