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"
74 const char * element_name;
86 const char * funcname;
95 #define EXPR_TYPE_CONST 0
96 #define EXPR_TYPE_US_CONST 1
97 #define EXPR_TYPE_SYMBOL 2
98 #define EXPR_TYPE_REGISTER 3
99 #define EXPR_TYPE_BINOP 4
100 #define EXPR_TYPE_UNOP 5
101 #define EXPR_TYPE_STRUCT 6
102 #define EXPR_TYPE_PSTRUCT 7
103 #define EXPR_TYPE_ARRAY 8
104 #define EXPR_TYPE_CALL 9
105 #define EXPR_TYPE_STRING 10
107 static char expr_list[4096];
108 static int next_expr_free = 0;
111 * This is how we turn an expression address into the actual value.
112 * This works well in the 32 bit domain - not sure at all about the
115 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
123 rtn = (struct expr *) &expr_list[next_expr_free];
125 next_expr_free += sizeof(struct expr);
137 DEBUG_RegisterExpr(enum debug_regs regno)
141 ex = DEBUG_GetFreeExpr();
143 ex->type = EXPR_TYPE_REGISTER;
144 ex->un.rgister.reg = regno;
149 DEBUG_SymbolExpr(const char * name)
153 ex = DEBUG_GetFreeExpr();
155 ex->type = EXPR_TYPE_SYMBOL;
156 ex->un.symbol.name = name;
161 DEBUG_ConstExpr(int value)
165 ex = DEBUG_GetFreeExpr();
167 ex->type = EXPR_TYPE_CONST;
168 ex->un.constant.value = value;
173 DEBUG_StringExpr(const char * str)
177 ex = DEBUG_GetFreeExpr();
179 ex->type = EXPR_TYPE_STRING;
180 ex->un.string.str = str+1;
181 pnt = strrchr(ex->un.string.str, '"');
190 DEBUG_USConstExpr(unsigned int value)
194 ex = DEBUG_GetFreeExpr();
196 ex->type = EXPR_TYPE_CONST;
197 ex->un.u_const.value = value;
202 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
206 ex = DEBUG_GetFreeExpr();
208 ex->type = EXPR_TYPE_BINOP;
209 ex->un.binop.binop_type = operator_type;
210 ex->un.binop.exp1 = exp1;
211 ex->un.binop.exp2 = exp2;
216 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
220 ex = DEBUG_GetFreeExpr();
222 ex->type = EXPR_TYPE_UNOP;
223 ex->un.unop.unop_type = operator_type;
224 ex->un.unop.exp1 = exp1;
229 DEBUG_StructExpr(struct expr * exp, const char * element)
233 ex = DEBUG_GetFreeExpr();
235 ex->type = EXPR_TYPE_STRUCT;
236 ex->un.structure.exp1 = exp;
237 ex->un.structure.element_name = element;
242 DEBUG_StructPExpr(struct expr * exp, const char * element)
246 ex = DEBUG_GetFreeExpr();
248 ex->type = EXPR_TYPE_PSTRUCT;
249 ex->un.structure.exp1 = exp;
250 ex->un.structure.element_name = element;
255 DEBUG_CallExpr(const char * funcname, int nargs, ...)
261 ex = DEBUG_GetFreeExpr();
263 ex->type = EXPR_TYPE_CALL;
264 ex->un.call.funcname = funcname;
265 ex->un.call.nargs = nargs;
268 for(i=0; i < nargs; i++)
270 ex->un.call.arg[i] = va_arg(ap, struct expr *);
277 DEBUG_EvalExpr(struct expr * exp)
283 unsigned int cexp[5];
288 struct datatype * type1;
289 struct datatype * type2;
297 case EXPR_TYPE_STRING:
298 rtn.type = DEBUG_TypeString;
299 rtn.off = (unsigned int) &exp->un.string.str;
302 case EXPR_TYPE_CONST:
303 rtn.type = DEBUG_TypeIntConst;
304 rtn.off = (unsigned int) &exp->un.constant.value;
307 case EXPR_TYPE_US_CONST:
308 rtn.type = DEBUG_TypeUSInt;
309 rtn.off = (unsigned int) &exp->un.u_const.value;
312 case EXPR_TYPE_SYMBOL:
313 if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
320 case EXPR_TYPE_PSTRUCT:
321 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
322 if( exp1.type == NULL )
326 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
332 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
333 &exp->un.structure.result);
335 case EXPR_TYPE_STRUCT:
336 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
337 if( exp1.type == NULL )
342 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
343 &exp->un.structure.result);
347 * First, evaluate all of the arguments. If any of them are not
348 * evaluable, then bail.
350 for(i=0; i < exp->un.call.nargs; i++)
352 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
353 if( exp1.type == NULL )
357 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
361 * Now look up the address of the function itself.
363 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
365 fprintf(stderr, "Failed to find symbol\n");
369 fptr = (int (*)()) rtn.off;
370 switch(exp->un.call.nargs)
373 exp->un.call.result = (*fptr)();
376 exp->un.call.result = (*fptr)(cexp[0]);
379 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
382 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
385 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
388 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
391 rtn.type = DEBUG_TypeInt;
392 rtn.off = (unsigned int) &exp->un.call.result;
394 case EXPR_TYPE_REGISTER:
395 rtn.type = DEBUG_TypeIntConst;
396 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
397 rtn.off = (unsigned int) &exp->un.rgister.result;
400 case EXPR_TYPE_BINOP:
401 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
402 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
403 if( exp1.type == NULL || exp2.type == NULL )
407 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
409 rtn.type = exp1.type;
413 rtn.type = DEBUG_TypeInt;
415 rtn.off = (unsigned int) &exp->un.binop.result;
416 switch(exp->un.binop.binop_type)
419 type1 = DEBUG_GetPointerType(exp1.type);
420 type2 = DEBUG_GetPointerType(exp2.type);
423 if( type1 != NULL && type2 != NULL )
427 else if( type1 != NULL )
429 scale2 = DEBUG_GetObjectSize(type1);
430 rtn.type = exp1.type;
432 else if( type2 != NULL )
434 scale1 = DEBUG_GetObjectSize(type2);
435 rtn.type = exp2.type;
438 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
441 type1 = DEBUG_GetPointerType(exp1.type);
442 type2 = DEBUG_GetPointerType(exp2.type);
446 if( type1 != NULL && type2 != NULL )
452 scale3 = DEBUG_GetObjectSize(type1);
454 else if( type1 != NULL )
456 scale2 = DEBUG_GetObjectSize(type1);
457 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) - VAL(exp2)) / scale3;
470 exp->un.binop.result = VAL(exp2);
474 exp->un.binop.result = (VAL(exp1) || VAL(exp2));
478 exp->un.binop.result = (VAL(exp1) && VAL(exp2));
482 exp->un.binop.result = (VAL(exp1) | VAL(exp2));
486 exp->un.binop.result = (VAL(exp1) & VAL(exp2));
490 exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
494 exp->un.binop.result = (VAL(exp1) == VAL(exp2));
498 exp->un.binop.result = (VAL(exp1) > VAL(exp2));
502 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
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 = ((unsigned) VAL(exp1) << VAL(exp2));
522 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
526 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
532 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
545 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
555 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
562 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
563 if( exp1.type == NULL )
567 rtn.off = (unsigned int) &exp->un.unop.result;
568 if( exp1.type == DEBUG_TypeIntConst )
570 rtn.type = exp1.type;
574 rtn.type = DEBUG_TypeInt;
576 switch(exp->un.binop.binop_type)
580 exp->un.unop.result = -VAL(exp1);
584 exp->un.unop.result = !VAL(exp1);
588 exp->un.unop.result = ~VAL(exp1);
592 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
594 case EXP_OP_FORCE_DEREF:
596 rtn.off = *(unsigned int *) exp1.off;
600 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
601 exp->un.unop.result = exp1.off;
606 fprintf(stderr,"Unexpected expression.\n");
616 DEBUG_DisplayExpr(struct expr * exp)
623 case EXPR_TYPE_REGISTER:
624 DEBUG_PrintRegister(exp->un.rgister.reg);
626 case EXPR_TYPE_US_CONST:
627 fprintf(stderr, "%ud", exp->un.u_const.value);
629 case EXPR_TYPE_CONST:
630 fprintf(stderr, "%d", exp->un.u_const.value);
632 case EXPR_TYPE_STRING:
633 fprintf(stderr, "\"%s\"", exp->un.string.str);
635 case EXPR_TYPE_SYMBOL:
636 fprintf(stderr, "%s" , exp->un.symbol.name);
638 case EXPR_TYPE_PSTRUCT:
639 DEBUG_DisplayExpr(exp->un.structure.exp1);
640 fprintf(stderr, "->%s", exp->un.structure.element_name);
642 case EXPR_TYPE_STRUCT:
643 DEBUG_DisplayExpr(exp->un.structure.exp1);
644 fprintf(stderr, ".%s", exp->un.structure.element_name);
648 * First, evaluate all of the arguments. If any of them are not
649 * evaluable, then bail.
651 fprintf(stderr, "%s(",exp->un.call.funcname);
652 for(i=0; i < exp->un.call.nargs; i++)
654 DEBUG_DisplayExpr(exp->un.call.arg[i]);
655 if( i != exp->un.call.nargs - 1 )
657 fprintf(stderr, ", ");
660 fprintf(stderr, ")");
662 case EXPR_TYPE_BINOP:
663 fprintf(stderr, "( ");
664 DEBUG_DisplayExpr(exp->un.binop.exp1);
665 switch(exp->un.binop.binop_type)
668 fprintf(stderr, " + ");
671 fprintf(stderr, " - ");
674 fprintf(stderr, ":");
677 fprintf(stderr, " || ");
680 fprintf(stderr, " && ");
683 fprintf(stderr, " | ");
686 fprintf(stderr, " & ");
689 fprintf(stderr, " ^ ");
692 fprintf(stderr, " == ");
695 fprintf(stderr, " > ");
698 fprintf(stderr, " < ");
701 fprintf(stderr, " >= ");
704 fprintf(stderr, " <= ");
707 fprintf(stderr, " != ");
710 fprintf(stderr, " << ");
713 fprintf(stderr, " >> ");
716 fprintf(stderr, " * ");
719 fprintf(stderr, " / ");
722 fprintf(stderr, " %% ");
725 fprintf(stderr, "[");
730 DEBUG_DisplayExpr(exp->un.binop.exp2);
731 if( exp->un.binop.binop_type == EXP_OP_ARR )
733 fprintf(stderr, "]");
735 fprintf(stderr, " )");
738 switch(exp->un.binop.binop_type)
741 fprintf(stderr, "-");
744 fprintf(stderr, "!");
747 fprintf(stderr, "~");
750 fprintf(stderr, "*");
753 fprintf(stderr, "&");
756 DEBUG_DisplayExpr(exp->un.unop.exp1);
759 fprintf(stderr,"Unexpected expression.\n");
768 DEBUG_CloneExpr(struct expr * exp)
773 rtn = (struct expr *) xmalloc(sizeof(struct expr));
776 * First copy the contents of the expression itself.
783 case EXPR_TYPE_REGISTER:
784 case EXPR_TYPE_US_CONST:
785 case EXPR_TYPE_CONST:
787 case EXPR_TYPE_STRING:
788 rtn->un.string.str = xstrdup(exp->un.string.str);
790 case EXPR_TYPE_SYMBOL:
791 rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
793 case EXPR_TYPE_PSTRUCT:
794 case EXPR_TYPE_STRUCT:
795 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
796 rtn->un.structure.element_name = xstrdup(exp->un.structure.element_name);
800 * First, evaluate all of the arguments. If any of them are not
801 * evaluable, then bail.
803 for(i=0; i < exp->un.call.nargs; i++)
805 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
807 rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
809 case EXPR_TYPE_BINOP:
810 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
811 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
814 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
817 fprintf(stderr,"Unexpected expression.\n");
827 * Recursively go through an expression tree and free all memory associated
831 DEBUG_FreeExpr(struct expr * exp)
837 case EXPR_TYPE_REGISTER:
838 case EXPR_TYPE_US_CONST:
839 case EXPR_TYPE_CONST:
841 case EXPR_TYPE_STRING:
842 free((char *) exp->un.string.str);
844 case EXPR_TYPE_SYMBOL:
845 free((char *) exp->un.symbol.name);
847 case EXPR_TYPE_PSTRUCT:
848 case EXPR_TYPE_STRUCT:
849 DEBUG_FreeExpr(exp->un.structure.exp1);
850 free((char *) exp->un.structure.element_name);
854 * First, evaluate all of the arguments. If any of them are not
855 * evaluable, then bail.
857 for(i=0; i < exp->un.call.nargs; i++)
859 DEBUG_FreeExpr(exp->un.call.arg[i]);
861 free((char *) exp->un.call.funcname);
863 case EXPR_TYPE_BINOP:
864 DEBUG_FreeExpr(exp->un.binop.exp1);
865 DEBUG_FreeExpr(exp->un.binop.exp2);
868 DEBUG_FreeExpr(exp->un.unop.exp1);
871 fprintf(stderr,"Unexpected expression.\n");