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"
72 struct datatype
* cast
;
79 const char * element_name
;
91 const char * funcname
;
100 #define EXPR_TYPE_CONST 0
101 #define EXPR_TYPE_US_CONST 1
102 #define EXPR_TYPE_SYMBOL 2
103 #define EXPR_TYPE_REGISTER 3
104 #define EXPR_TYPE_BINOP 4
105 #define EXPR_TYPE_UNOP 5
106 #define EXPR_TYPE_STRUCT 6
107 #define EXPR_TYPE_PSTRUCT 7
108 #define EXPR_TYPE_ARRAY 8
109 #define EXPR_TYPE_CALL 9
110 #define EXPR_TYPE_STRING 10
111 #define EXPR_TYPE_CAST 11
113 static char expr_list
[4096];
114 static int next_expr_free
= 0;
117 * This is how we turn an expression address into the actual value.
118 * This works well in the 32 bit domain - not sure at all about the
121 #define VAL(_exp) DEBUG_GetExprValue(&_exp, NULL)
129 rtn
= (struct expr
*) &expr_list
[next_expr_free
];
131 next_expr_free
+= sizeof(struct expr
);
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];
306 struct datatype
* type1
;
307 struct datatype
* type2
;
316 rtn
= DEBUG_EvalExpr(exp
->un
.cast
.expr
);
317 rtn
.type
= exp
->un
.cast
.cast
;
319 case EXPR_TYPE_STRING
:
320 rtn
.type
= DEBUG_TypeString
;
321 rtn
.off
= (unsigned int) &exp
->un
.string
.str
;
324 case EXPR_TYPE_CONST
:
325 rtn
.type
= DEBUG_TypeIntConst
;
326 rtn
.off
= (unsigned int) &exp
->un
.constant
.value
;
329 case EXPR_TYPE_US_CONST
:
330 rtn
.type
= DEBUG_TypeUSInt
;
331 rtn
.off
= (unsigned int) &exp
->un
.u_const
.value
;
334 case EXPR_TYPE_SYMBOL
:
335 if( !DEBUG_GetSymbolValue(exp
->un
.symbol
.name
, -1, &rtn
, FALSE
) )
342 case EXPR_TYPE_PSTRUCT
:
343 exp1
= DEBUG_EvalExpr(exp
->un
.structure
.exp1
);
344 if( exp1
.type
== NULL
)
348 rtn
.off
= DEBUG_TypeDerefPointer(&exp1
, &type1
);
354 DEBUG_FindStructElement(&rtn
, exp
->un
.structure
.element_name
,
355 &exp
->un
.structure
.result
);
357 case EXPR_TYPE_STRUCT
:
358 exp1
= DEBUG_EvalExpr(exp
->un
.structure
.exp1
);
359 if( exp1
.type
== NULL
)
364 DEBUG_FindStructElement(&rtn
, exp
->un
.structure
.element_name
,
365 &exp
->un
.structure
.result
);
369 * First, evaluate all of the arguments. If any of them are not
370 * evaluable, then bail.
372 for(i
=0; i
< exp
->un
.call
.nargs
; i
++)
374 exp1
= DEBUG_EvalExpr(exp
->un
.call
.arg
[i
]);
375 if( exp1
.type
== NULL
)
379 cexp
[i
] = DEBUG_GetExprValue(&exp1
, NULL
);
383 * Now look up the address of the function itself.
385 if( !DEBUG_GetSymbolValue(exp
->un
.call
.funcname
, -1, &rtn
, FALSE
) )
387 fprintf(stderr
, "Failed to find symbol\n");
391 fptr
= (int (*)()) rtn
.off
;
392 switch(exp
->un
.call
.nargs
)
395 exp
->un
.call
.result
= (*fptr
)();
398 exp
->un
.call
.result
= (*fptr
)(cexp
[0]);
401 exp
->un
.call
.result
= (*fptr
)(cexp
[0], cexp
[1]);
404 exp
->un
.call
.result
= (*fptr
)(cexp
[0], cexp
[1], cexp
[2]);
407 exp
->un
.call
.result
= (*fptr
)(cexp
[0], cexp
[1], cexp
[2], cexp
[3]);
410 exp
->un
.call
.result
= (*fptr
)(cexp
[0], cexp
[1], cexp
[2], cexp
[3], cexp
[4]);
413 rtn
.type
= DEBUG_TypeInt
;
414 rtn
.off
= (unsigned int) &exp
->un
.call
.result
;
416 case EXPR_TYPE_REGISTER
:
417 rtn
.type
= DEBUG_TypeIntConst
;
418 exp
->un
.rgister
.result
= DEBUG_GetRegister(exp
->un
.rgister
.reg
);
419 rtn
.off
= (unsigned int) &exp
->un
.rgister
.result
;
420 if( exp
->un
.rgister
.reg
== REG_EIP
)
422 rtn
.seg
= CS_reg(&DEBUG_context
);
426 rtn
.seg
= DS_reg(&DEBUG_context
);
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
);
503 exp
->un
.binop
.result
= (VAL(exp1
) || VAL(exp2
));
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
= ((unsigned) VAL(exp1
) << VAL(exp2
));
551 exp
->un
.binop
.result
= ((unsigned) VAL(exp1
) << VAL(exp2
));
555 exp
->un
.binop
.result
= (VAL(exp1
) * VAL(exp2
));
561 exp
->un
.binop
.result
= (VAL(exp1
) / VAL(exp2
));
574 exp
->un
.binop
.result
= (VAL(exp1
) % VAL(exp2
));
584 DEBUG_ArrayIndex(&exp1
, &rtn
, VAL(exp2
));
591 exp1
= DEBUG_EvalExpr(exp
->un
.unop
.exp1
);
592 if( exp1
.type
== NULL
)
596 rtn
.off
= (unsigned int) &exp
->un
.unop
.result
;
597 if( exp1
.type
== DEBUG_TypeIntConst
)
599 rtn
.type
= exp1
.type
;
603 rtn
.type
= DEBUG_TypeInt
;
605 switch(exp
->un
.binop
.binop_type
)
609 exp
->un
.unop
.result
= -VAL(exp1
);
613 exp
->un
.unop
.result
= !VAL(exp1
);
617 exp
->un
.unop
.result
= ~VAL(exp1
);
621 rtn
.off
= (unsigned int) DEBUG_TypeDerefPointer(&exp1
, &rtn
.type
);
623 case EXP_OP_FORCE_DEREF
:
625 rtn
.off
= *(unsigned int *) exp1
.off
;
629 rtn
.type
= DEBUG_FindOrMakePointerType(exp1
.type
);
630 exp
->un
.unop
.result
= exp1
.off
;
635 fprintf(stderr
,"Unexpected expression.\n");
645 DEBUG_DisplayExpr(struct expr
* exp
)
653 fprintf(stderr
, "((");
654 DEBUG_PrintTypeCast(exp
->un
.cast
.cast
);
655 fprintf(stderr
, ")");
656 DEBUG_DisplayExpr(exp
->un
.cast
.expr
);
657 fprintf(stderr
, ")");
659 case EXPR_TYPE_REGISTER
:
660 DEBUG_PrintRegister(exp
->un
.rgister
.reg
);
662 case EXPR_TYPE_US_CONST
:
663 fprintf(stderr
, "%ud", exp
->un
.u_const
.value
);
665 case EXPR_TYPE_CONST
:
666 fprintf(stderr
, "%d", exp
->un
.u_const
.value
);
668 case EXPR_TYPE_STRING
:
669 fprintf(stderr
, "\"%s\"", exp
->un
.string
.str
);
671 case EXPR_TYPE_SYMBOL
:
672 fprintf(stderr
, "%s" , exp
->un
.symbol
.name
);
674 case EXPR_TYPE_PSTRUCT
:
675 DEBUG_DisplayExpr(exp
->un
.structure
.exp1
);
676 fprintf(stderr
, "->%s", exp
->un
.structure
.element_name
);
678 case EXPR_TYPE_STRUCT
:
679 DEBUG_DisplayExpr(exp
->un
.structure
.exp1
);
680 fprintf(stderr
, ".%s", exp
->un
.structure
.element_name
);
684 * First, evaluate all of the arguments. If any of them are not
685 * evaluable, then bail.
687 fprintf(stderr
, "%s(",exp
->un
.call
.funcname
);
688 for(i
=0; i
< exp
->un
.call
.nargs
; i
++)
690 DEBUG_DisplayExpr(exp
->un
.call
.arg
[i
]);
691 if( i
!= exp
->un
.call
.nargs
- 1 )
693 fprintf(stderr
, ", ");
696 fprintf(stderr
, ")");
698 case EXPR_TYPE_BINOP
:
699 fprintf(stderr
, "( ");
700 DEBUG_DisplayExpr(exp
->un
.binop
.exp1
);
701 switch(exp
->un
.binop
.binop_type
)
704 fprintf(stderr
, " + ");
707 fprintf(stderr
, " - ");
710 fprintf(stderr
, ":");
713 fprintf(stderr
, " || ");
716 fprintf(stderr
, " && ");
719 fprintf(stderr
, " | ");
722 fprintf(stderr
, " & ");
725 fprintf(stderr
, " ^ ");
728 fprintf(stderr
, " == ");
731 fprintf(stderr
, " > ");
734 fprintf(stderr
, " < ");
737 fprintf(stderr
, " >= ");
740 fprintf(stderr
, " <= ");
743 fprintf(stderr
, " != ");
746 fprintf(stderr
, " << ");
749 fprintf(stderr
, " >> ");
752 fprintf(stderr
, " * ");
755 fprintf(stderr
, " / ");
758 fprintf(stderr
, " %% ");
761 fprintf(stderr
, "[");
766 DEBUG_DisplayExpr(exp
->un
.binop
.exp2
);
767 if( exp
->un
.binop
.binop_type
== EXP_OP_ARR
)
769 fprintf(stderr
, "]");
771 fprintf(stderr
, " )");
774 switch(exp
->un
.binop
.binop_type
)
777 fprintf(stderr
, "-");
780 fprintf(stderr
, "!");
783 fprintf(stderr
, "~");
786 fprintf(stderr
, "*");
789 fprintf(stderr
, "&");
792 DEBUG_DisplayExpr(exp
->un
.unop
.exp1
);
795 fprintf(stderr
,"Unexpected expression.\n");
804 DEBUG_CloneExpr(struct expr
* exp
)
809 rtn
= (struct expr
*) xmalloc(sizeof(struct expr
));
812 * First copy the contents of the expression itself.
820 rtn
->un
.cast
.expr
= DEBUG_CloneExpr(exp
->un
.cast
.expr
);
822 case EXPR_TYPE_REGISTER
:
823 case EXPR_TYPE_US_CONST
:
824 case EXPR_TYPE_CONST
:
826 case EXPR_TYPE_STRING
:
827 rtn
->un
.string
.str
= xstrdup(exp
->un
.string
.str
);
829 case EXPR_TYPE_SYMBOL
:
830 rtn
->un
.symbol
.name
= xstrdup(exp
->un
.symbol
.name
);
832 case EXPR_TYPE_PSTRUCT
:
833 case EXPR_TYPE_STRUCT
:
834 rtn
->un
.structure
.exp1
= DEBUG_CloneExpr(exp
->un
.structure
.exp1
);
835 rtn
->un
.structure
.element_name
= xstrdup(exp
->un
.structure
.element_name
);
839 * First, evaluate all of the arguments. If any of them are not
840 * evaluable, then bail.
842 for(i
=0; i
< exp
->un
.call
.nargs
; i
++)
844 rtn
->un
.call
.arg
[i
] = DEBUG_CloneExpr(exp
->un
.call
.arg
[i
]);
846 rtn
->un
.call
.funcname
= xstrdup(exp
->un
.call
.funcname
);
848 case EXPR_TYPE_BINOP
:
849 rtn
->un
.binop
.exp1
= DEBUG_CloneExpr(exp
->un
.binop
.exp1
);
850 rtn
->un
.binop
.exp2
= DEBUG_CloneExpr(exp
->un
.binop
.exp2
);
853 rtn
->un
.unop
.exp1
= DEBUG_CloneExpr(exp
->un
.unop
.exp1
);
856 fprintf(stderr
,"Unexpected expression.\n");
866 * Recursively go through an expression tree and free all memory associated
870 DEBUG_FreeExpr(struct expr
* exp
)
877 DEBUG_FreeExpr(exp
->un
.cast
.expr
);
879 case EXPR_TYPE_REGISTER
:
880 case EXPR_TYPE_US_CONST
:
881 case EXPR_TYPE_CONST
:
883 case EXPR_TYPE_STRING
:
884 free((char *) exp
->un
.string
.str
);
886 case EXPR_TYPE_SYMBOL
:
887 free((char *) exp
->un
.symbol
.name
);
889 case EXPR_TYPE_PSTRUCT
:
890 case EXPR_TYPE_STRUCT
:
891 DEBUG_FreeExpr(exp
->un
.structure
.exp1
);
892 free((char *) exp
->un
.structure
.element_name
);
896 * First, evaluate all of the arguments. If any of them are not
897 * evaluable, then bail.
899 for(i
=0; i
< exp
->un
.call
.nargs
; i
++)
901 DEBUG_FreeExpr(exp
->un
.call
.arg
[i
]);
903 free((char *) exp
->un
.call
.funcname
);
905 case EXPR_TYPE_BINOP
:
906 DEBUG_FreeExpr(exp
->un
.binop
.exp1
);
907 DEBUG_FreeExpr(exp
->un
.binop
.exp2
);
910 DEBUG_FreeExpr(exp
->un
.unop
.exp1
);
913 fprintf(stderr
,"Unexpected expression.\n");