No longer directly accessing debuggee memory.
[wine] / debugger / expr.c
1 /*
2  * File expr.c - expression handling for Wine internal debugger.
3  *
4  * Copyright (C) 1997, Eric Youngdale.
5  *
6  */
7
8 #include "config.h"
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <assert.h>
13 #include "winbase.h"
14 #include "wine/winbase16.h"
15 #include "task.h"
16 #include "debugger.h"
17 #include "expr.h"
18
19 #include <stdarg.h>
20
21 struct expr
22 {
23   unsigned int  perm;
24   unsigned int  type:31;
25   union
26   {
27     struct
28     {
29       int value;
30     } constant;
31
32     struct
33     {
34       const char * str;
35     } string;
36
37     struct
38     {
39       unsigned int value;
40     } u_const;
41
42     struct
43     {
44       const char * name;
45     } symbol;
46
47     struct
48     {
49       enum debug_regs reg;
50       int             result;
51     } rgister;
52
53     struct
54     {
55       int unop_type;
56       struct expr * exp1;
57       int             result;
58     } unop;
59
60     struct
61     {
62       int binop_type;
63       int result;
64       struct expr * exp1;
65       struct expr * exp2;
66     } binop;
67
68     struct
69     {
70       struct datatype * cast;
71       struct expr     * expr;
72     } cast;
73
74     struct
75     {
76       struct expr * exp1;
77       const char * element_name;
78       int result;
79     } structure;
80
81     struct
82     {
83       struct expr * base;
84       struct expr * index;
85     } array;
86
87     struct
88     {
89       const char  * funcname;
90       int           nargs;
91       int           result;
92       struct expr * arg[5];
93     } call;
94
95   } un;
96 };
97
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
110
111 static char expr_list[4096];
112 static int next_expr_free = 0;
113
114 /*
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
117  * 16 bit world.
118  */
119 #define VAL(_exp)       DEBUG_GetExprValue(&_exp, NULL)
120
121 static
122 struct expr *
123 DEBUG_GetFreeExpr()
124 {
125   struct expr * rtn;
126
127   rtn =  (struct expr *) &expr_list[next_expr_free];
128
129   next_expr_free += sizeof(struct expr);
130   assert(next_expr_free < sizeof(expr_list));
131
132   return rtn;
133 }
134
135 void
136 DEBUG_FreeExprMem()
137 {
138   next_expr_free = 0;
139 }
140
141 struct expr *
142 DEBUG_TypeCastExpr(struct datatype * dt, struct expr * exp)
143 {
144   struct expr * ex;
145
146   ex = DEBUG_GetFreeExpr();
147
148   ex->type        = EXPR_TYPE_CAST;
149   ex->un.cast.cast = dt;
150   ex->un.cast.expr = exp;
151   return ex;
152 }
153
154 struct expr *
155 DEBUG_RegisterExpr(enum debug_regs regno)
156 {
157   struct expr * ex;
158
159   ex = DEBUG_GetFreeExpr();
160
161   ex->type        = EXPR_TYPE_REGISTER;
162   ex->un.rgister.reg = regno;
163   return ex;
164 }
165
166 struct expr *
167 DEBUG_SymbolExpr(const char * name)
168 {
169   struct expr * ex;
170
171   ex = DEBUG_GetFreeExpr();
172
173   ex->type        = EXPR_TYPE_SYMBOL;
174   ex->un.symbol.name = name;
175   return ex;
176 }
177
178 struct expr *
179 DEBUG_ConstExpr(int value)
180 {
181   struct expr * ex;
182
183   ex = DEBUG_GetFreeExpr();
184
185   ex->type        = EXPR_TYPE_CONST;
186   ex->un.constant.value = value;
187   return ex;
188 }
189
190 struct expr *
191 DEBUG_StringExpr(const char * str)
192 {
193   struct expr * ex;
194   char * pnt;
195   ex = DEBUG_GetFreeExpr();
196
197   ex->type        = EXPR_TYPE_STRING;
198   ex->un.string.str = str+1;
199   pnt = strrchr(ex->un.string.str, '"');
200   if( pnt != NULL )
201     {
202       *pnt =  '\0';
203     }
204   return ex;
205 }
206
207 struct expr *
208 DEBUG_USConstExpr(unsigned int value)
209 {
210   struct expr * ex;
211
212   ex = DEBUG_GetFreeExpr();
213
214   ex->type           = EXPR_TYPE_CONST;
215   ex->un.u_const.value = value;
216   return ex;
217 }
218
219 struct expr *
220 DEBUG_BinopExpr(int operator_type, struct expr * exp1, struct expr * exp2)
221 {
222   struct expr * ex;
223
224   ex = DEBUG_GetFreeExpr();
225
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;
230   return ex;
231 }
232
233 struct expr *
234 DEBUG_UnopExpr(int operator_type, struct expr * exp1)
235 {
236   struct expr * ex;
237
238   ex = DEBUG_GetFreeExpr();
239
240   ex->type           = EXPR_TYPE_UNOP;
241   ex->un.unop.unop_type = operator_type;
242   ex->un.unop.exp1      = exp1;
243   return ex;
244 }
245
246 struct expr *
247 DEBUG_StructExpr(struct expr * exp, const char * element)
248 {
249   struct expr * ex;
250
251   ex = DEBUG_GetFreeExpr();
252
253   ex->type           = EXPR_TYPE_STRUCT;
254   ex->un.structure.exp1 = exp;
255   ex->un.structure.element_name = element;
256   return ex;
257 }
258
259 struct expr *
260 DEBUG_StructPExpr(struct expr * exp, const char * element)
261 {
262   struct expr * ex;
263
264   ex = DEBUG_GetFreeExpr();
265
266   ex->type           = EXPR_TYPE_PSTRUCT;
267   ex->un.structure.exp1 = exp;
268   ex->un.structure.element_name = element;
269   return ex;
270 }
271
272 struct expr *
273 DEBUG_CallExpr(const char * funcname, int nargs, ...)
274 {
275   struct expr * ex;
276   va_list ap;
277   int i;
278
279   ex = DEBUG_GetFreeExpr();
280
281   ex->type           = EXPR_TYPE_CALL;
282   ex->un.call.funcname = funcname;
283   ex->un.call.nargs = nargs;
284
285   va_start(ap, nargs);
286   for(i=0; i < nargs; i++)
287     {
288       ex->un.call.arg[i] = va_arg(ap, struct expr *);
289     }
290   va_end(ap);
291   return ex;
292 }
293
294 DBG_ADDR
295 DEBUG_EvalExpr(struct expr * exp)
296 {
297   DBG_ADDR      rtn;
298   int           i;
299   DBG_ADDR      exp1;
300   DBG_ADDR      exp2;
301   unsigned int  cexp[5];
302   int               scale1;
303   int               scale2;
304   int               scale3;
305   struct datatype * type1;
306   struct datatype * type2;
307
308   rtn.type = NULL;
309   rtn.off = NULL;
310   rtn.seg = NULL;
311
312   switch(exp->type)
313     {
314     case EXPR_TYPE_CAST:
315       rtn = DEBUG_EvalExpr(exp->un.cast.expr);
316       rtn.type = exp->un.cast.cast;
317       break;
318     case EXPR_TYPE_STRING:
319       rtn.type = DEBUG_TypeString;
320       rtn.off = (unsigned int) &exp->un.string.str;
321       rtn.seg = 0;
322       break;
323     case EXPR_TYPE_CONST:
324       rtn.type = DEBUG_TypeIntConst;
325       rtn.off = (unsigned int) &exp->un.constant.value;
326       rtn.seg = 0;
327       break;
328     case EXPR_TYPE_US_CONST:
329       rtn.type = DEBUG_TypeUSInt;
330       rtn.off = (unsigned int) &exp->un.u_const.value;
331       rtn.seg = 0;
332       break;
333     case EXPR_TYPE_SYMBOL:
334       if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE) )
335         {    
336 #if 1
337            RaiseException(DEBUG_STATUS_NO_SYMBOL, 0, 0, NULL);
338 #else
339            static        char    ret[128];
340
341            /* FIXME: this is an ugly hack... but at least we know
342             * the symbol is not defined
343             */
344            sprintf(ret, "\"Symbol %s is not defined.\"", exp->un.symbol.name);
345            rtn = DEBUG_EvalExpr(DEBUG_StringExpr(ret));
346 #endif
347         }
348       break;
349     case EXPR_TYPE_PSTRUCT:
350       exp1 =  DEBUG_EvalExpr(exp->un.structure.exp1);
351       if( exp1.type == NULL )
352         {
353           break;
354         }
355       rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
356       if( type1 == NULL )
357         {
358           break;
359         }
360       rtn.type = type1;
361       DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
362                               &exp->un.structure.result);
363       break;
364     case EXPR_TYPE_STRUCT:
365       exp1 =  DEBUG_EvalExpr(exp->un.structure.exp1);
366       if( exp1.type == NULL )
367         {
368           break;
369         }
370       rtn = exp1;
371       DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
372                               &exp->un.structure.result);
373       break;
374     case EXPR_TYPE_CALL:
375       /*
376        * First, evaluate all of the arguments.  If any of them are not
377        * evaluable, then bail.
378        */
379       for(i=0; i < exp->un.call.nargs; i++)
380         {
381           exp1  = DEBUG_EvalExpr(exp->un.call.arg[i]);
382           if( exp1.type == NULL )
383             {
384               return rtn;
385             }
386           cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
387         }
388
389       /*
390        * Now look up the address of the function itself.
391        */
392       if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
393         {
394           fprintf(stderr, "Failed to find symbol\n");
395           break;
396         }
397
398 #if 0
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)
402        */
403       int               (*fptr)();
404
405       fptr = (int (*)()) rtn.off;
406       switch(exp->un.call.nargs)
407         {
408         case 0:
409           exp->un.call.result = (*fptr)();
410           break;
411         case 1:
412           exp->un.call.result = (*fptr)(cexp[0]);
413           break;
414         case 2:
415           exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
416           break;
417         case 3:
418           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
419           break;
420         case 4:
421           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
422           break;
423         case 5:
424           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
425           break;
426         }
427 #else
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...
431        */
432       exp->un.call.result = 0;
433 #endif
434       rtn.type = DEBUG_TypeInt;
435       rtn.off = (unsigned int) &exp->un.call.result;
436
437       break;
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;
442 #ifdef __i386__
443       if( exp->un.rgister.reg == REG_EIP )
444           rtn.seg = DEBUG_context.SegCs;
445       else
446           rtn.seg = DEBUG_context.SegDs;
447 #endif
448       DEBUG_FixAddress( &rtn, 0 );
449       break;
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 )
454         {
455           break;
456         }
457       if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
458         {
459           rtn.type = exp1.type;
460         }
461       else
462         {
463           rtn.type = DEBUG_TypeInt;
464         }
465       rtn.off = (unsigned int) &exp->un.binop.result;
466       switch(exp->un.binop.binop_type)
467         {
468         case EXP_OP_ADD:
469           type1 = DEBUG_GetPointerType(exp1.type);
470           type2 = DEBUG_GetPointerType(exp2.type);
471           scale1 = 1;
472           scale2 = 1;
473           if( type1 != NULL && type2 != NULL )
474             {
475               break;
476             }
477           else if( type1 != NULL )
478             {
479               scale2 = DEBUG_GetObjectSize(type1);
480               rtn.type = exp1.type;
481             }
482           else if( type2 != NULL )
483             {
484               scale1 = DEBUG_GetObjectSize(type2);
485               rtn.type = exp2.type;
486             }
487           rtn.seg = 0;
488           exp->un.binop.result = (VAL(exp1) * scale1  + scale2 * VAL(exp2));
489           break;
490         case EXP_OP_SUB:
491           type1 = DEBUG_GetPointerType(exp1.type);
492           type2 = DEBUG_GetPointerType(exp2.type);
493           scale1 = 1;
494           scale2 = 1;
495           scale3 = 1;
496           if( type1 != NULL && type2 != NULL )
497             {
498               if( type1 != type2 )
499                 {
500                   break;
501                 }
502               scale3 = DEBUG_GetObjectSize(type1);
503             }
504           else if( type1 != NULL )
505             {
506               scale2 = DEBUG_GetObjectSize(type1);
507               rtn.type = exp1.type;
508             }
509
510           else if( type2 != NULL )
511             {
512               scale1 = DEBUG_GetObjectSize(type2);
513               rtn.type = exp2.type;
514             }
515           rtn.seg = 0;
516           exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
517           break;
518         case EXP_OP_SEG:
519           rtn.seg = VAL(exp1);
520           exp->un.binop.result = VAL(exp2);
521 #ifdef __i386__
522           DEBUG_FixSegment(&rtn);
523 #endif
524           break;
525         case EXP_OP_LOR:
526           rtn.seg = 0;
527           exp->un.binop.result = (VAL(exp1) || VAL(exp2));
528           break;
529         case EXP_OP_LAND:
530           rtn.seg = 0;
531           exp->un.binop.result = (VAL(exp1) && VAL(exp2));
532           break;
533         case EXP_OP_OR:
534           rtn.seg = 0;
535           exp->un.binop.result = (VAL(exp1) | VAL(exp2));
536           break;
537         case EXP_OP_AND:
538           rtn.seg = 0;
539           exp->un.binop.result = (VAL(exp1) & VAL(exp2));
540           break;
541         case EXP_OP_XOR:
542           rtn.seg = 0;
543           exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
544           break;
545         case EXP_OP_EQ:
546           rtn.seg = 0;
547           exp->un.binop.result = (VAL(exp1) == VAL(exp2));
548           break;
549         case EXP_OP_GT:
550           rtn.seg = 0;
551           exp->un.binop.result = (VAL(exp1) > VAL(exp2));
552           break;
553         case EXP_OP_LT:
554           rtn.seg = 0;
555           exp->un.binop.result = (VAL(exp1) < VAL(exp2));
556           break;
557         case EXP_OP_GE:
558           rtn.seg = 0;
559           exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
560           break;
561         case EXP_OP_LE:
562           rtn.seg = 0;
563           exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
564           break;
565         case EXP_OP_NE:
566           rtn.seg = 0;
567           exp->un.binop.result = (VAL(exp1) != VAL(exp2));
568           break;
569         case EXP_OP_SHL:
570           rtn.seg = 0;
571           exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
572           break;
573         case EXP_OP_SHR:
574           rtn.seg = 0;
575           exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
576           break;
577         case EXP_OP_MUL:
578           rtn.seg = 0;
579           exp->un.binop.result = (VAL(exp1) * VAL(exp2));
580           break;
581         case EXP_OP_DIV:
582           if( VAL(exp2) != 0 )
583             {
584               rtn.seg = 0;
585               exp->un.binop.result = (VAL(exp1) / VAL(exp2));
586             }
587           else
588             {
589               rtn.seg = 0;
590               rtn.type = NULL;
591               rtn.off = 0;
592             }
593           break;
594         case EXP_OP_REM:
595           if( VAL(exp2) != 0 )
596             {
597               rtn.seg = 0;
598               exp->un.binop.result = (VAL(exp1) % VAL(exp2));
599             }
600           else
601             {
602               rtn.seg = 0;
603               rtn.type = NULL;
604               rtn.off = 0;
605             }
606           break;
607         case EXP_OP_ARR:
608           DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
609           break;
610         default:
611           break;
612         }
613       break;
614     case EXPR_TYPE_UNOP:
615       exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
616       if( exp1.type == NULL )
617         {
618           break;
619         }
620       rtn.off = (unsigned int) &exp->un.unop.result;
621       if( exp1.type == DEBUG_TypeIntConst )
622         {
623           rtn.type = exp1.type;
624         }
625       else
626         {
627           rtn.type = DEBUG_TypeInt;
628         }
629       switch(exp->un.unop.unop_type)
630         {
631         case EXP_OP_NEG:
632           rtn.seg = 0;
633           exp->un.unop.result = -VAL(exp1);
634           break;
635         case EXP_OP_NOT:
636           rtn.seg = 0;
637           exp->un.unop.result = !VAL(exp1);
638           break;
639         case EXP_OP_LNOT:
640           rtn.seg = 0;
641           exp->un.unop.result = ~VAL(exp1);
642           break;
643         case EXP_OP_DEREF:
644           rtn.seg = 0;
645           rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
646           break;
647         case EXP_OP_FORCE_DEREF:
648           rtn.seg = exp1.seg;
649           rtn.off = DEBUG_READ_MEM((void*)exp1.off, &rtn.off, sizeof(rtn.off));
650           break;
651         case EXP_OP_ADDR:
652           rtn.seg = 0;
653           rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
654           exp->un.unop.result = exp1.off;
655           break;
656         }
657       break;
658     default:
659       fprintf(stderr,"Unexpected expression.\n");
660       DEBUG_Exit(123);
661       break;
662     }
663
664   return rtn;
665 }
666
667
668 int
669 DEBUG_DisplayExpr(struct expr * exp)
670 {
671   int           i;
672
673
674   switch(exp->type)
675     {
676     case EXPR_TYPE_CAST:
677       fprintf(stderr, "((");
678       DEBUG_PrintTypeCast(exp->un.cast.cast);
679       fprintf(stderr, ")");
680       DEBUG_DisplayExpr(exp->un.cast.expr);
681       fprintf(stderr, ")");
682       break;
683     case EXPR_TYPE_REGISTER:
684       DEBUG_PrintRegister(exp->un.rgister.reg);
685       break;
686     case EXPR_TYPE_US_CONST:
687       fprintf(stderr, "%ud", exp->un.u_const.value);
688       break;
689     case EXPR_TYPE_CONST:
690       fprintf(stderr, "%d", exp->un.u_const.value);
691       break;
692     case EXPR_TYPE_STRING:
693       fprintf(stderr, "\"%s\"", exp->un.string.str);
694       break;
695     case EXPR_TYPE_SYMBOL:
696       fprintf(stderr, "%s" , exp->un.symbol.name);
697       break;
698     case EXPR_TYPE_PSTRUCT:
699       DEBUG_DisplayExpr(exp->un.structure.exp1);
700       fprintf(stderr, "->%s", exp->un.structure.element_name);
701       break;
702     case EXPR_TYPE_STRUCT:
703       DEBUG_DisplayExpr(exp->un.structure.exp1);
704       fprintf(stderr, ".%s", exp->un.structure.element_name);
705       break;
706     case EXPR_TYPE_CALL:
707       fprintf(stderr, "%s(",exp->un.call.funcname);
708       for(i=0; i < exp->un.call.nargs; i++)
709         {
710           DEBUG_DisplayExpr(exp->un.call.arg[i]);
711           if( i != exp->un.call.nargs - 1 )
712             {
713               fprintf(stderr, ", ");
714             }
715         }
716       fprintf(stderr, ")");
717       break;
718     case EXPR_TYPE_BINOP:
719       fprintf(stderr, "( ");
720       DEBUG_DisplayExpr(exp->un.binop.exp1);
721       switch(exp->un.binop.binop_type)
722         {
723         case EXP_OP_ADD:
724           fprintf(stderr, " + ");
725           break;
726         case EXP_OP_SUB:
727           fprintf(stderr, " - ");
728           break;
729         case EXP_OP_SEG:
730           fprintf(stderr, ":");
731           break;
732         case EXP_OP_LOR:
733           fprintf(stderr, " || ");
734           break;
735         case EXP_OP_LAND:
736           fprintf(stderr, " && ");
737           break;
738         case EXP_OP_OR:
739           fprintf(stderr, " | ");
740           break;
741         case EXP_OP_AND:
742           fprintf(stderr, " & ");
743           break;
744         case EXP_OP_XOR:
745           fprintf(stderr, " ^ ");
746           break;
747         case EXP_OP_EQ:
748           fprintf(stderr, " == ");
749           break;
750         case EXP_OP_GT:
751           fprintf(stderr, " > ");
752           break;
753         case EXP_OP_LT:
754           fprintf(stderr, " < ");
755           break;
756         case EXP_OP_GE:
757           fprintf(stderr, " >= ");
758           break;
759         case EXP_OP_LE:
760           fprintf(stderr, " <= ");
761           break;
762         case EXP_OP_NE:
763           fprintf(stderr, " != ");
764           break;
765         case EXP_OP_SHL:
766           fprintf(stderr, " << ");
767           break;
768         case EXP_OP_SHR:
769           fprintf(stderr, " >> ");
770           break;
771         case EXP_OP_MUL:
772           fprintf(stderr, " * ");
773           break;
774         case EXP_OP_DIV:
775           fprintf(stderr, " / ");
776           break;
777         case EXP_OP_REM:
778           fprintf(stderr, " %% ");
779           break;
780         case EXP_OP_ARR:
781           fprintf(stderr, "[");
782           break;
783         default:
784           break;
785         }
786       DEBUG_DisplayExpr(exp->un.binop.exp2);
787       if( exp->un.binop.binop_type == EXP_OP_ARR )
788         {
789           fprintf(stderr, "]");
790         }
791       fprintf(stderr, " )");
792       break;
793     case EXPR_TYPE_UNOP:
794       switch(exp->un.unop.unop_type)
795         {
796         case EXP_OP_NEG:
797           fprintf(stderr, "-");
798           break;
799         case EXP_OP_NOT:
800           fprintf(stderr, "!");
801           break;
802         case EXP_OP_LNOT:
803           fprintf(stderr, "~");
804           break;
805         case EXP_OP_DEREF:
806           fprintf(stderr, "*");
807           break;
808         case EXP_OP_ADDR:
809           fprintf(stderr, "&");
810           break;
811         }
812       DEBUG_DisplayExpr(exp->un.unop.exp1);
813       break;
814     default:
815       fprintf(stderr,"Unexpected expression.\n");
816       DEBUG_Exit(123);
817       break;
818     }
819
820   return TRUE;
821 }
822
823 struct expr *
824 DEBUG_CloneExpr(struct expr * exp)
825 {
826   int           i;
827   struct expr * rtn;
828
829   rtn = (struct expr *) DBG_alloc(sizeof(struct expr));
830
831   /*
832    * First copy the contents of the expression itself.
833    */
834   *rtn = *exp;
835
836
837   switch(exp->type)
838     {
839     case EXPR_TYPE_CAST:
840       rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
841       break;
842     case EXPR_TYPE_REGISTER:
843     case EXPR_TYPE_US_CONST:
844     case EXPR_TYPE_CONST:
845       break;
846     case EXPR_TYPE_STRING:
847       rtn->un.string.str = DBG_strdup(exp->un.string.str);
848       break;
849     case EXPR_TYPE_SYMBOL:
850       rtn->un.symbol.name = DBG_strdup(exp->un.symbol.name);
851       break;
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);
856       break;
857     case EXPR_TYPE_CALL:
858       for(i=0; i < exp->un.call.nargs; i++)
859         {
860           rtn->un.call.arg[i]  = DEBUG_CloneExpr(exp->un.call.arg[i]);
861         }
862       rtn->un.call.funcname = DBG_strdup(exp->un.call.funcname);
863       break;
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);
867       break;
868     case EXPR_TYPE_UNOP:
869       rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
870       break;
871     default:
872       fprintf(stderr,"Unexpected expression.\n");
873       DEBUG_Exit(123);
874       break;
875     }
876
877   return rtn;
878 }
879
880
881 /*
882  * Recursively go through an expression tree and free all memory associated
883  * with it.
884  */
885 int
886 DEBUG_FreeExpr(struct expr * exp)
887 {
888   int i;
889
890   switch(exp->type)
891     {
892     case EXPR_TYPE_CAST:
893       DEBUG_FreeExpr(exp->un.cast.expr);
894       break;
895     case EXPR_TYPE_REGISTER:
896     case EXPR_TYPE_US_CONST:
897     case EXPR_TYPE_CONST:
898       break;
899     case EXPR_TYPE_STRING:
900       DBG_free((char *) exp->un.string.str);
901       break;
902     case EXPR_TYPE_SYMBOL:
903       DBG_free((char *) exp->un.symbol.name);
904       break;
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);
909       break;
910     case EXPR_TYPE_CALL:
911       for(i=0; i < exp->un.call.nargs; i++)
912         {
913           DEBUG_FreeExpr(exp->un.call.arg[i]);
914         }
915       DBG_free((char *) exp->un.call.funcname);
916       break;
917     case EXPR_TYPE_BINOP:
918       DEBUG_FreeExpr(exp->un.binop.exp1);
919       DEBUG_FreeExpr(exp->un.binop.exp2);
920       break;
921     case EXPR_TYPE_UNOP:
922       DEBUG_FreeExpr(exp->un.unop.exp1);
923       break;
924     default:
925       fprintf(stderr,"Unexpected expression.\n");
926       DEBUG_Exit(123);
927       break;
928     }
929
930   DBG_free(exp);
931   return TRUE;
932 }