2 * File expr.c - expression handling for Wine internal debugger.
4 * Copyright (C) 1997, Eric Youngdale.
14 #include "wine/winbase16.h"
70 struct datatype * cast;
77 const char * element_name;
89 const char * funcname;
98 #define EXPR_TYPE_CONST 0
99 #define EXPR_TYPE_US_CONST 1
100 #define EXPR_TYPE_SYMBOL 2
101 #define EXPR_TYPE_REGISTER 3
102 #define EXPR_TYPE_BINOP 4
103 #define EXPR_TYPE_UNOP 5
104 #define EXPR_TYPE_STRUCT 6
105 #define EXPR_TYPE_PSTRUCT 7
106 #define EXPR_TYPE_ARRAY 8
107 #define EXPR_TYPE_CALL 9
108 #define EXPR_TYPE_STRING 10
109 #define EXPR_TYPE_CAST 11
111 static char expr_list[4096];
112 static int next_expr_free = 0;
115 * This is how we turn an expression address into the actual value.
116 * This works well in the 32 bit domain - not sure at all about the
119 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
127 rtn = (struct expr *) &expr_list[next_expr_free];
129 next_expr_free += sizeof(struct expr);
130 assert(next_expr_free < sizeof(expr_list));
142 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
146 ex = DEBUG_GetFreeExpr();
148 ex->type = EXPR_TYPE_CAST;
149 ex->un.cast.cast = dt;
150 ex->un.cast.expr = exp;
155 DEBUG_RegisterExpr(enum debug_regs regno)
159 ex = DEBUG_GetFreeExpr();
161 ex->type = EXPR_TYPE_REGISTER;
162 ex->un.rgister.reg = regno;
167 DEBUG_SymbolExpr(const char * name)
171 ex = DEBUG_GetFreeExpr();
173 ex->type = EXPR_TYPE_SYMBOL;
174 ex->un.symbol.name = name;
179 DEBUG_ConstExpr(int value)
183 ex = DEBUG_GetFreeExpr();
185 ex->type = EXPR_TYPE_CONST;
186 ex->un.constant.value = value;
191 DEBUG_StringExpr(const char * str)
195 ex = DEBUG_GetFreeExpr();
197 ex->type = EXPR_TYPE_STRING;
198 ex->un.string.str = str+1;
199 pnt = strrchr(ex->un.string.str, '"');
208 DEBUG_USConstExpr(unsigned int value)
212 ex = DEBUG_GetFreeExpr();
214 ex->type = EXPR_TYPE_CONST;
215 ex->un.u_const.value = value;
220 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
224 ex = DEBUG_GetFreeExpr();
226 ex->type = EXPR_TYPE_BINOP;
227 ex->un.binop.binop_type = operator_type;
228 ex->un.binop.exp1 = exp1;
229 ex->un.binop.exp2 = exp2;
234 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
238 ex = DEBUG_GetFreeExpr();
240 ex->type = EXPR_TYPE_UNOP;
241 ex->un.unop.unop_type = operator_type;
242 ex->un.unop.exp1 = exp1;
247 DEBUG_StructExpr(struct expr * exp, const char * element)
251 ex = DEBUG_GetFreeExpr();
253 ex->type = EXPR_TYPE_STRUCT;
254 ex->un.structure.exp1 = exp;
255 ex->un.structure.element_name = element;
260 DEBUG_StructPExpr(struct expr * exp, const char * element)
264 ex = DEBUG_GetFreeExpr();
266 ex->type = EXPR_TYPE_PSTRUCT;
267 ex->un.structure.exp1 = exp;
268 ex->un.structure.element_name = element;
273 DEBUG_CallExpr(const char * funcname, int nargs, ...)
279 ex = DEBUG_GetFreeExpr();
281 ex->type = EXPR_TYPE_CALL;
282 ex->un.call.funcname = funcname;
283 ex->un.call.nargs = nargs;
286 for(i=0; i < nargs; i++)
288 ex->un.call.arg[i] = va_arg(ap, struct expr *);
295 DEBUG_EvalExpr(struct expr * exp)
301 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) )
337 RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
339 static char ret[128];
341 /* FIXME: this is an ugly hack... but at least we know
342 * the symbol is not defined
344 sprintf(ret, "\"Symbol %s is not defined.\"", exp->un.symbol.name);
345 rtn = DEBUG_EvalExpr(DEBUG_StringExpr(ret));
349 case EXPR_TYPE_PSTRUCT:
350 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
351 if( exp1.type == NULL )
355 rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
361 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
362 &exp->un.structure.result);
364 case EXPR_TYPE_STRUCT:
365 exp1 = DEBUG_EvalExpr(exp->un.structure.exp1);
366 if( exp1.type == NULL )
371 DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
372 &exp->un.structure.result);
376 * First, evaluate all of the arguments. If any of them are not
377 * evaluable, then bail.
379 for(i=0; i < exp->un.call.nargs; i++)
381 exp1 = DEBUG_EvalExpr(exp->un.call.arg[i]);
382 if( exp1.type == NULL )
386 cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
390 * Now look up the address of the function itself.
392 if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
394 fprintf(stderr, "Failed to find symbol\n");
399 /* FIXME: NEWDBG NIY */
400 /* Anyway, I wonder how this could work depending on the calling order of
401 * the function (cdecl vs pascal for example)
405 fptr = (int (*)()) rtn.off;
406 switch(exp->un.call.nargs)
409 exp->un.call.result = (*fptr)();
412 exp->un.call.result = (*fptr)(cexp[0]);
415 exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
418 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
421 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
424 exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
428 fprintf(stderr, "Function call no longer implemented\n");
429 /* would need to set up a call to this function, and then restore the current
430 * context afterwards...
432 exp->un.call.result = 0;
434 rtn.type = DEBUG_TypeInt;
435 rtn.off = (unsigned int) &exp->un.call.result;
438 case EXPR_TYPE_REGISTER:
439 rtn.type = DEBUG_TypeIntConst;
440 exp->un.rgister.result = DEBUG_GetRegister(exp->un.rgister.reg);
441 rtn.off = (unsigned int) &exp->un.rgister.result;
443 if( exp->un.rgister.reg == REG_EIP )
444 rtn.seg = DEBUG_context.SegCs;
446 rtn.seg = DEBUG_context.SegDs;
448 DEBUG_FixAddress( &rtn, 0 );
450 case EXPR_TYPE_BINOP:
451 exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
452 exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
453 if( exp1.type == NULL || exp2.type == NULL )
457 if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
459 rtn.type = exp1.type;
463 rtn.type = DEBUG_TypeInt;
465 rtn.off = (unsigned int) &exp->un.binop.result;
466 switch(exp->un.binop.binop_type)
469 type1 = DEBUG_GetPointerType(exp1.type);
470 type2 = DEBUG_GetPointerType(exp2.type);
473 if( type1 != NULL && type2 != NULL )
477 else if( type1 != NULL )
479 scale2 = DEBUG_GetObjectSize(type1);
480 rtn.type = exp1.type;
482 else if( type2 != NULL )
484 scale1 = DEBUG_GetObjectSize(type2);
485 rtn.type = exp2.type;
488 exp->un.binop.result = (VAL(exp1) * scale1 + scale2 * VAL(exp2));
491 type1 = DEBUG_GetPointerType(exp1.type);
492 type2 = DEBUG_GetPointerType(exp2.type);
496 if( type1 != NULL && type2 != NULL )
502 scale3 = DEBUG_GetObjectSize(type1);
504 else if( type1 != NULL )
506 scale2 = DEBUG_GetObjectSize(type1);
507 rtn.type = exp1.type;
510 else if( type2 != NULL )
512 scale1 = DEBUG_GetObjectSize(type2);
513 rtn.type = exp2.type;
516 exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
520 exp->un.binop.result = VAL(exp2);
522 DEBUG_FixSegment(&rtn);
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 = (VAL(exp1) > VAL(exp2));
555 exp->un.binop.result = (VAL(exp1) < VAL(exp2));
559 exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
563 exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
567 exp->un.binop.result = (VAL(exp1) != VAL(exp2));
571 exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
575 exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
579 exp->un.binop.result = (VAL(exp1) * VAL(exp2));
585 exp->un.binop.result = (VAL(exp1) / VAL(exp2));
598 exp->un.binop.result = (VAL(exp1) % VAL(exp2));
608 DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
615 exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
616 if( exp1.type == NULL )
620 rtn.off = (unsigned int) &exp->un.unop.result;
621 if( exp1.type == DEBUG_TypeIntConst )
623 rtn.type = exp1.type;
627 rtn.type = DEBUG_TypeInt;
629 switch(exp->un.unop.unop_type)
633 exp->un.unop.result = -VAL(exp1);
637 exp->un.unop.result = !VAL(exp1);
641 exp->un.unop.result = ~VAL(exp1);
645 rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
647 case EXP_OP_FORCE_DEREF:
649 rtn.off = DEBUG_READ_MEM((void*)exp1.off, &rtn.off, sizeof(rtn.off));
653 rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
654 exp->un.unop.result = exp1.off;
659 fprintf(stderr,"Unexpected expression.\n");
669 DEBUG_DisplayExpr(struct expr * exp)
677 fprintf(stderr, "((");
678 DEBUG_PrintTypeCast(exp->un.cast.cast);
679 fprintf(stderr, ")");
680 DEBUG_DisplayExpr(exp->un.cast.expr);
681 fprintf(stderr, ")");
683 case EXPR_TYPE_REGISTER:
684 DEBUG_PrintRegister(exp->un.rgister.reg);
686 case EXPR_TYPE_US_CONST:
687 fprintf(stderr, "%ud", exp->un.u_const.value);
689 case EXPR_TYPE_CONST:
690 fprintf(stderr, "%d", exp->un.u_const.value);
692 case EXPR_TYPE_STRING:
693 fprintf(stderr, "\"%s\"", exp->un.string.str);
695 case EXPR_TYPE_SYMBOL:
696 fprintf(stderr, "%s" , exp->un.symbol.name);
698 case EXPR_TYPE_PSTRUCT:
699 DEBUG_DisplayExpr(exp->un.structure.exp1);
700 fprintf(stderr, "->%s", exp->un.structure.element_name);
702 case EXPR_TYPE_STRUCT:
703 DEBUG_DisplayExpr(exp->un.structure.exp1);
704 fprintf(stderr, ".%s", exp->un.structure.element_name);
707 fprintf(stderr, "%s(",exp->un.call.funcname);
708 for(i=0; i < exp->un.call.nargs; i++)
710 DEBUG_DisplayExpr(exp->un.call.arg[i]);
711 if( i != exp->un.call.nargs - 1 )
713 fprintf(stderr, ", ");
716 fprintf(stderr, ")");
718 case EXPR_TYPE_BINOP:
719 fprintf(stderr, "( ");
720 DEBUG_DisplayExpr(exp->un.binop.exp1);
721 switch(exp->un.binop.binop_type)
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, " << ");
769 fprintf(stderr, " >> ");
772 fprintf(stderr, " * ");
775 fprintf(stderr, " / ");
778 fprintf(stderr, " %% ");
781 fprintf(stderr, "[");
786 DEBUG_DisplayExpr(exp->un.binop.exp2);
787 if( exp->un.binop.binop_type == EXP_OP_ARR )
789 fprintf(stderr, "]");
791 fprintf(stderr, " )");
794 switch(exp->un.unop.unop_type)
797 fprintf(stderr, "-");
800 fprintf(stderr, "!");
803 fprintf(stderr, "~");
806 fprintf(stderr, "*");
809 fprintf(stderr, "&");
812 DEBUG_DisplayExpr(exp->un.unop.exp1);
815 fprintf(stderr,"Unexpected expression.\n");
824 DEBUG_CloneExpr(struct expr * exp)
829 rtn = (struct expr *) DBG_alloc(sizeof(struct expr));
832 * First copy the contents of the expression itself.
840 rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
842 case EXPR_TYPE_REGISTER:
843 case EXPR_TYPE_US_CONST:
844 case EXPR_TYPE_CONST:
846 case EXPR_TYPE_STRING:
847 rtn->un.string.str = DBG_strdup(exp->un.string.str);
849 case EXPR_TYPE_SYMBOL:
850 rtn->un.symbol.name = DBG_strdup(exp->un.symbol.name);
852 case EXPR_TYPE_PSTRUCT:
853 case EXPR_TYPE_STRUCT:
854 rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
855 rtn->un.structure.element_name = DBG_strdup(exp->un.structure.element_name);
858 for(i=0; i < exp->un.call.nargs; i++)
860 rtn->un.call.arg[i] = DEBUG_CloneExpr(exp->un.call.arg[i]);
862 rtn->un.call.funcname = DBG_strdup(exp->un.call.funcname);
864 case EXPR_TYPE_BINOP:
865 rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
866 rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
869 rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
872 fprintf(stderr,"Unexpected expression.\n");
882 * Recursively go through an expression tree and free all memory associated
886 DEBUG_FreeExpr(struct expr * exp)
893 DEBUG_FreeExpr(exp->un.cast.expr);
895 case EXPR_TYPE_REGISTER:
896 case EXPR_TYPE_US_CONST:
897 case EXPR_TYPE_CONST:
899 case EXPR_TYPE_STRING:
900 DBG_free((char *) exp->un.string.str);
902 case EXPR_TYPE_SYMBOL:
903 DBG_free((char *) exp->un.symbol.name);
905 case EXPR_TYPE_PSTRUCT:
906 case EXPR_TYPE_STRUCT:
907 DEBUG_FreeExpr(exp->un.structure.exp1);
908 DBG_free((char *) exp->un.structure.element_name);
911 for(i=0; i < exp->un.call.nargs; i++)
913 DEBUG_FreeExpr(exp->un.call.arg[i]);
915 DBG_free((char *) exp->un.call.funcname);
917 case EXPR_TYPE_BINOP:
918 DEBUG_FreeExpr(exp->un.binop.exp1);
919 DEBUG_FreeExpr(exp->un.binop.exp2);
922 DEBUG_FreeExpr(exp->un.unop.exp1);
925 fprintf(stderr,"Unexpected expression.\n");