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"
74 struct datatype * cast;
81 const char * element_name;
93 const char * funcname;
102 #define EXPR_TYPE_CONST 0
103 #define EXPR_TYPE_US_CONST 1
104 #define EXPR_TYPE_SYMBOL 2
105 #define EXPR_TYPE_REGISTER 3
106 #define EXPR_TYPE_BINOP 4
107 #define EXPR_TYPE_UNOP 5
108 #define EXPR_TYPE_STRUCT 6
109 #define EXPR_TYPE_PSTRUCT 7
110 #define EXPR_TYPE_ARRAY 8
111 #define EXPR_TYPE_CALL 9
112 #define EXPR_TYPE_STRING 10
113 #define EXPR_TYPE_CAST 11
115 static char expr_list[4096];
116 static int next_expr_free = 0;
119 * This is how we turn an expression address into the actual value.
120 * This works well in the 32 bit domain - not sure at all about the
123 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
131 rtn = (struct expr *) &expr_list[next_expr_free];
133 next_expr_free += sizeof(struct expr);
144 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
148 ex = DEBUG_GetFreeExpr();
150 ex->type = EXPR_TYPE_CAST;
151 ex->un.cast.cast = dt;
152 ex->un.cast.expr = exp;
157 DEBUG_RegisterExpr(enum debug_regs regno)
161 ex = DEBUG_GetFreeExpr();
163 ex->type = EXPR_TYPE_REGISTER;
164 ex->un.rgister.reg = regno;
169 DEBUG_SymbolExpr(const char * name)
173 ex = DEBUG_GetFreeExpr();
175 ex->type = EXPR_TYPE_SYMBOL;
176 ex->un.symbol.name = name;
181 DEBUG_ConstExpr(int value)
185 ex = DEBUG_GetFreeExpr();
187 ex->type = EXPR_TYPE_CONST;
188 ex->un.constant.value = value;
193 DEBUG_StringExpr(const char * str)
197 ex = DEBUG_GetFreeExpr();
199 ex->type = EXPR_TYPE_STRING;
200 ex->un.string.str = str+1;
201 pnt = strrchr(ex->un.string.str, '"');
210 DEBUG_USConstExpr(unsigned int value)
214 ex = DEBUG_GetFreeExpr();
216 ex->type = EXPR_TYPE_CONST;
217 ex->un.u_const.value = value;
222 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
226 ex = DEBUG_GetFreeExpr();
228 ex->type = EXPR_TYPE_BINOP;
229 ex->un.binop.binop_type = operator_type;
230 ex->un.binop.exp1 = exp1;
231 ex->un.binop.exp2 = exp2;
236 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
240 ex = DEBUG_GetFreeExpr();
242 ex->type = EXPR_TYPE_UNOP;
243 ex->un.unop.unop_type = operator_type;
244 ex->un.unop.exp1 = exp1;
249 DEBUG_StructExpr(struct expr * exp, const char * element)
253 ex = DEBUG_GetFreeExpr();
255 ex->type = EXPR_TYPE_STRUCT;
256 ex->un.structure.exp1 = exp;
257 ex->un.structure.element_name = element;
262 DEBUG_StructPExpr(struct expr * exp, const char * element)
266 ex = DEBUG_GetFreeExpr();
268 ex->type = EXPR_TYPE_PSTRUCT;
269 ex->un.structure.exp1 = exp;
270 ex->un.structure.element_name = element;
275 DEBUG_CallExpr(const char * funcname, int nargs, ...)
281 ex = DEBUG_GetFreeExpr();
283 ex->type = EXPR_TYPE_CALL;
284 ex->un.call.funcname = funcname;
285 ex->un.call.nargs = nargs;
288 for(i=0; i < nargs; i++)
290 ex->un.call.arg[i] = va_arg(ap, struct expr *);
297 DEBUG_EvalExpr(struct expr * exp)
303 unsigned int cexp[5];
308 struct datatype * type1;
309 struct datatype * type2;
318 rtn = DEBUG_EvalExpr(exp->un.cast.expr);
319 rtn.type = exp->un.cast.cast;
321 case EXPR_TYPE_STRING:
322 rtn.type = DEBUG_TypeString;
323 rtn.off = (unsigned int) &exp->un.string.str;
326 case EXPR_TYPE_CONST:
327 rtn.type = DEBUG_TypeIntConst;
328 rtn.off = (unsigned int) &exp->un.constant.value;
331 case EXPR_TYPE_US_CONST:
332 rtn.type = DEBUG_TypeUSInt;
333 rtn.off = (unsigned int) &exp->un.u_const.value;
336 case EXPR_TYPE_SYMBOL:
337 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
344 case EXPR_TYPE_PSTRUCT:
345 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
346 if( exp1.type == NULL )
350 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
356 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
357 &exp->un.structure.result);
359 case EXPR_TYPE_STRUCT:
360 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
361 if( exp1.type == NULL )
366 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
367 &exp->un.structure.result);
371 * First, evaluate all of the arguments. If any of them are not
372 * evaluable, then bail.
374 for(i=0; i < exp->un.call.nargs; i++)
376 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
377 if( exp1.type == NULL )
381 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
385 * Now look up the address of the function itself.
387 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
389 fprintf(stderr, "Failed to find symbol\n");
393 fptr = (int (*)()) rtn.off;
394 switch(exp->un.call.nargs)
397 exp->un.call.result = (*fptr)();
400 exp->un.call.result = (*fptr)(cexp[0]);
403 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
406 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
409 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
412 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
415 rtn.type = DEBUG_TypeInt;
416 rtn.off = (unsigned int) &exp->un.call.result;
418 case EXPR_TYPE_REGISTER:
419 rtn.type = DEBUG_TypeIntConst;
420 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
421 rtn.off = (unsigned int) &exp->un.rgister.result;
422 if( exp->un.rgister.reg == REG_EIP )
423 rtn.seg = CS_reg(&DEBUG_context);
425 rtn.seg = DS_reg(&DEBUG_context);
426 DBG_FIX_ADDR_SEG( &rtn, 0 );
428 case EXPR_TYPE_BINOP:
429 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
430 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
431 if( exp1.type == NULL || exp2.type == NULL )
435 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
437 rtn.type = exp1.type;
441 rtn.type = DEBUG_TypeInt;
443 rtn.off = (unsigned int) &exp->un.binop.result;
444 switch(exp->un.binop.binop_type)
447 type1 = DEBUG_GetPointerType(exp1.type);
448 type2 = DEBUG_GetPointerType(exp2.type);
451 if( type1 != NULL && type2 != NULL )
455 else if( type1 != NULL )
457 scale2 = DEBUG_GetObjectSize(type1);
458 rtn.type = exp1.type;
460 else if( type2 != NULL )
462 scale1 = DEBUG_GetObjectSize(type2);
463 rtn.type = exp2.type;
466 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
469 type1 = DEBUG_GetPointerType(exp1.type);
470 type2 = DEBUG_GetPointerType(exp2.type);
474 if( type1 != NULL && type2 != NULL )
480 scale3 = DEBUG_GetObjectSize(type1);
482 else if( type1 != NULL )
484 scale2 = DEBUG_GetObjectSize(type1);
485 rtn.type = exp1.type;
488 else if( type2 != NULL )
490 scale1 = DEBUG_GetObjectSize(type2);
491 rtn.type = exp2.type;
494 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
498 exp->un.binop.result = VAL(exp2);
499 if (ISV86(&DEBUG_context)) {
500 TDB *pTask = (TDB*)GlobalLock16( GetCurrentTask() );
501 rtn.seg |= (DWORD)(pTask?(pTask->hModule):0)<<16;
502 GlobalUnlock16( GetCurrentTask() );
507 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
511 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
515 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
519 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
523 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
527 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
531 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
535 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
539 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
543 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
547 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
551 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
555 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
559 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
565 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
578 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
588 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
595 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
596 if( exp1.type == NULL )
600 rtn.off = (unsigned int) &exp->un.unop.result;
601 if( exp1.type == DEBUG_TypeIntConst )
603 rtn.type = exp1.type;
607 rtn.type = DEBUG_TypeInt;
609 switch(exp->un.binop.binop_type)
613 exp->un.unop.result = -VAL(exp1);
617 exp->un.unop.result = !VAL(exp1);
621 exp->un.unop.result = ~VAL(exp1);
625 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
627 case EXP_OP_FORCE_DEREF:
629 rtn.off = *(unsigned int *) exp1.off;
633 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
634 exp->un.unop.result = exp1.off;
639 fprintf(stderr,"Unexpected expression.\n");
649 DEBUG_DisplayExpr(struct expr * exp)
657 fprintf(stderr, "((");
658 DEBUG_PrintTypeCast(exp->un.cast.cast);
659 fprintf(stderr, ")");
660 DEBUG_DisplayExpr(exp->un.cast.expr);
661 fprintf(stderr, ")");
663 case EXPR_TYPE_REGISTER:
664 DEBUG_PrintRegister(exp->un.rgister.reg);
666 case EXPR_TYPE_US_CONST:
667 fprintf(stderr, "%ud", exp->un.u_const.value);
669 case EXPR_TYPE_CONST:
670 fprintf(stderr, "%d", exp->un.u_const.value);
672 case EXPR_TYPE_STRING:
673 fprintf(stderr, "\"%s\"", exp->un.string.str);
675 case EXPR_TYPE_SYMBOL:
676 fprintf(stderr, "%s" , exp->un.symbol.name);
678 case EXPR_TYPE_PSTRUCT:
679 DEBUG_DisplayExpr(exp->un.structure.exp1);
680 fprintf(stderr, "->%s", exp->un.structure.element_name);
682 case EXPR_TYPE_STRUCT:
683 DEBUG_DisplayExpr(exp->un.structure.exp1);
684 fprintf(stderr, ".%s", exp->un.structure.element_name);
688 * First, evaluate all of the arguments. If any of them are not
689 * evaluable, then bail.
691 fprintf(stderr, "%s(",exp->un.call.funcname);
692 for(i=0; i < exp->un.call.nargs; i++)
694 DEBUG_DisplayExpr(exp->un.call.arg[i]);
695 if( i != exp->un.call.nargs - 1 )
697 fprintf(stderr, ", ");
700 fprintf(stderr, ")");
702 case EXPR_TYPE_BINOP:
703 fprintf(stderr, "( ");
704 DEBUG_DisplayExpr(exp->un.binop.exp1);
705 switch(exp->un.binop.binop_type)
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, " %% ");
765 fprintf(stderr, "[");
770 DEBUG_DisplayExpr(exp->un.binop.exp2);
771 if( exp->un.binop.binop_type == EXP_OP_ARR )
773 fprintf(stderr, "]");
775 fprintf(stderr, " )");
778 switch(exp->un.binop.binop_type)
781 fprintf(stderr, "-");
784 fprintf(stderr, "!");
787 fprintf(stderr, "~");
790 fprintf(stderr, "*");
793 fprintf(stderr, "&");
796 DEBUG_DisplayExpr(exp->un.unop.exp1);
799 fprintf(stderr,"Unexpected expression.\n");
808 DEBUG_CloneExpr(struct expr * exp)
813 rtn = (struct expr *) DBG_alloc(sizeof(struct expr));
816 * First copy the contents of the expression itself.
824 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
826 case EXPR_TYPE_REGISTER:
827 case EXPR_TYPE_US_CONST:
828 case EXPR_TYPE_CONST:
830 case EXPR_TYPE_STRING:
831 rtn->un.string.str = DBG_strdup(exp->un.string.str);
833 case EXPR_TYPE_SYMBOL:
834 rtn->un.symbol.name = DBG_strdup(exp->un.symbol.name);
836 case EXPR_TYPE_PSTRUCT:
837 case EXPR_TYPE_STRUCT:
838 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
839 rtn->un.structure.element_name = DBG_strdup(exp->un.structure.element_name);
843 * First, evaluate all of the arguments. If any of them are not
844 * evaluable, then bail.
846 for(i=0; i < exp->un.call.nargs; i++)
848 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
850 rtn->un.call.funcname = DBG_strdup(exp->un.call.funcname);
852 case EXPR_TYPE_BINOP:
853 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
854 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
857 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
860 fprintf(stderr,"Unexpected expression.\n");
870 * Recursively go through an expression tree and free all memory associated
874 DEBUG_FreeExpr(struct expr * exp)
881 DEBUG_FreeExpr(exp->un.cast.expr);
883 case EXPR_TYPE_REGISTER:
884 case EXPR_TYPE_US_CONST:
885 case EXPR_TYPE_CONST:
887 case EXPR_TYPE_STRING:
888 DBG_free((char *) exp->un.string.str);
890 case EXPR_TYPE_SYMBOL:
891 DBG_free((char *) exp->un.symbol.name);
893 case EXPR_TYPE_PSTRUCT:
894 case EXPR_TYPE_STRUCT:
895 DEBUG_FreeExpr(exp->un.structure.exp1);
896 DBG_free((char *) exp->un.structure.element_name);
900 * First, evaluate all of the arguments. If any of them are not
901 * evaluable, then bail.
903 for(i=0; i < exp->un.call.nargs; i++)
905 DEBUG_FreeExpr(exp->un.call.arg[i]);
907 DBG_free((char *) exp->un.call.funcname);
909 case EXPR_TYPE_BINOP:
910 DEBUG_FreeExpr(exp->un.binop.exp1);
911 DEBUG_FreeExpr(exp->un.binop.exp2);
914 DEBUG_FreeExpr(exp->un.unop.exp1);
917 fprintf(stderr,"Unexpected expression.\n");