Added a memset() to prevent a debugger segfault caused by
[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 <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <limits.h>
12 #include <sys/types.h>
13 #include <neexe.h>
14 #include "module.h"
15 #include "selectors.h"
16 #include "debugger.h"
17 #include "xmalloc.h"
18
19 #include "expr.h"
20
21 #include <stdarg.h>
22
23 struct expr
24 {
25   unsigned int  perm;
26   unsigned int  type:31;
27   union
28   {
29     struct
30     {
31       int value;
32     } constant;
33
34     struct
35     {
36       const char * str;
37     } string;
38
39     struct
40     {
41       unsigned int value;
42     } u_const;
43
44     struct
45     {
46       const char * name;
47     } symbol;
48
49     struct
50     {
51       enum debug_regs reg;
52       int             result;
53     } rgister;
54
55     struct
56     {
57       int unop_type;
58       struct expr * exp1;
59       int             result;
60     } unop;
61
62     struct
63     {
64       int binop_type;
65       int result;
66       struct expr * exp1;
67       struct expr * exp2;
68     } binop;
69
70     struct
71     {
72       struct datatype * cast;
73       struct expr     * expr;
74     } cast;
75
76     struct
77     {
78       struct expr * exp1;
79       const char * element_name;
80       int result;
81     } structure;
82
83     struct
84     {
85       struct expr * base;
86       struct expr * index;
87     } array;
88
89     struct
90     {
91       const char  * funcname;
92       int           nargs;
93       int           result;
94       struct expr * arg[5];
95     } call;
96
97   } un;
98 };
99
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
112
113 static char expr_list[4096];
114 static int next_expr_free = 0;
115
116 /*
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
119  * 16 bit world.
120  */
121 #define VAL(_exp)       DEBUG_GetExprValue(&_exp, NULL)
122
123 static
124 struct expr *
125 DEBUG_GetFreeExpr()
126 {
127   struct expr * rtn;
128
129   rtn =  (struct expr *) &expr_list[next_expr_free];
130
131   next_expr_free += sizeof(struct expr);
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           (*fptr)();
303   int               scale1;
304   int               scale2;
305   int               scale3;
306   struct datatype * type1;
307   struct datatype * type2;
308
309   rtn.type = NULL;
310   rtn.off = NULL;
311   rtn.seg = NULL;
312
313   switch(exp->type)
314     {
315     case EXPR_TYPE_CAST:
316       rtn = DEBUG_EvalExpr(exp->un.cast.expr);
317       rtn.type = exp->un.cast.cast;
318       break;
319     case EXPR_TYPE_STRING:
320       rtn.type = DEBUG_TypeString;
321       rtn.off = (unsigned int) &exp->un.string.str;
322       rtn.seg = 0;
323       break;
324     case EXPR_TYPE_CONST:
325       rtn.type = DEBUG_TypeIntConst;
326       rtn.off = (unsigned int) &exp->un.constant.value;
327       rtn.seg = 0;
328       break;
329     case EXPR_TYPE_US_CONST:
330       rtn.type = DEBUG_TypeUSInt;
331       rtn.off = (unsigned int) &exp->un.u_const.value;
332       rtn.seg = 0;
333       break;
334     case EXPR_TYPE_SYMBOL:
335       if( !DEBUG_GetSymbolValue(exp->un.symbol.name, -1, &rtn, FALSE ) )
336         {
337           rtn.type = NULL;
338           rtn.off = 0;
339           rtn.seg = 0;
340         };
341       break;
342     case EXPR_TYPE_PSTRUCT:
343       exp1 =  DEBUG_EvalExpr(exp->un.structure.exp1);
344       if( exp1.type == NULL )
345         {
346           break;
347         }
348       rtn.off = DEBUG_TypeDerefPointer(&exp1, &type1);
349       if( type1 == NULL )
350         {
351           break;
352         }
353       rtn.type = type1;
354       DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
355                               &exp->un.structure.result);
356       break;
357     case EXPR_TYPE_STRUCT:
358       exp1 =  DEBUG_EvalExpr(exp->un.structure.exp1);
359       if( exp1.type == NULL )
360         {
361           break;
362         }
363       rtn = exp1;
364       DEBUG_FindStructElement(&rtn, exp->un.structure.element_name,
365                               &exp->un.structure.result);
366       break;
367     case EXPR_TYPE_CALL:
368       /*
369        * First, evaluate all of the arguments.  If any of them are not
370        * evaluable, then bail.
371        */
372       for(i=0; i < exp->un.call.nargs; i++)
373         {
374           exp1  = DEBUG_EvalExpr(exp->un.call.arg[i]);
375           if( exp1.type == NULL )
376             {
377               return rtn;
378             }
379           cexp[i] = DEBUG_GetExprValue(&exp1, NULL);
380         }
381
382       /*
383        * Now look up the address of the function itself.
384        */
385       if( !DEBUG_GetSymbolValue(exp->un.call.funcname, -1, &rtn, FALSE ) )
386         {
387           fprintf(stderr, "Failed to find symbol\n");
388           break;
389         };
390
391       fptr = (int (*)()) rtn.off;
392       switch(exp->un.call.nargs)
393         {
394         case 0:
395           exp->un.call.result = (*fptr)();
396           break;
397         case 1:
398           exp->un.call.result = (*fptr)(cexp[0]);
399           break;
400         case 2:
401           exp->un.call.result = (*fptr)(cexp[0], cexp[1]);
402           break;
403         case 3:
404           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2]);
405           break;
406         case 4:
407           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3]);
408           break;
409         case 5:
410           exp->un.call.result = (*fptr)(cexp[0], cexp[1], cexp[2], cexp[3], cexp[4]);
411           break;
412         }
413       rtn.type = DEBUG_TypeInt;
414       rtn.off = (unsigned int) &exp->un.call.result;
415       break;
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 )
421           rtn.seg = CS_reg(&DEBUG_context);
422       else
423           rtn.seg = DS_reg(&DEBUG_context);
424       DBG_FIX_ADDR_SEG( &rtn, 0 );
425       break;
426     case EXPR_TYPE_BINOP:
427       exp1 = DEBUG_EvalExpr(exp->un.binop.exp1);
428       exp2 = DEBUG_EvalExpr(exp->un.binop.exp2);
429       if( exp1.type == NULL || exp2.type == NULL )
430         {
431           break;
432         }
433       if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
434         {
435           rtn.type = exp1.type;
436         }
437       else
438         {
439           rtn.type = DEBUG_TypeInt;
440         }
441       rtn.off = (unsigned int) &exp->un.binop.result;
442       switch(exp->un.binop.binop_type)
443         {
444         case EXP_OP_ADD:
445           type1 = DEBUG_GetPointerType(exp1.type);
446           type2 = DEBUG_GetPointerType(exp2.type);
447           scale1 = 1;
448           scale2 = 1;
449           if( type1 != NULL && type2 != NULL )
450             {
451               break;
452             }
453           else if( type1 != NULL )
454             {
455               scale2 = DEBUG_GetObjectSize(type1);
456               rtn.type = exp1.type;
457             }
458           else if( type2 != NULL )
459             {
460               scale1 = DEBUG_GetObjectSize(type2);
461               rtn.type = exp2.type;
462             }
463           rtn.seg = 0;
464           exp->un.binop.result = (VAL(exp1) * scale1  + scale2 * VAL(exp2));
465           break;
466         case EXP_OP_SUB:
467           type1 = DEBUG_GetPointerType(exp1.type);
468           type2 = DEBUG_GetPointerType(exp2.type);
469           scale1 = 1;
470           scale2 = 1;
471           scale3 = 1;
472           if( type1 != NULL && type2 != NULL )
473             {
474               if( type1 != type2 )
475                 {
476                   break;
477                 }
478               scale3 = DEBUG_GetObjectSize(type1);
479             }
480           else if( type1 != NULL )
481             {
482               scale2 = DEBUG_GetObjectSize(type1);
483               rtn.type = exp1.type;
484             }
485
486           else if( type2 != NULL )
487             {
488               scale1 = DEBUG_GetObjectSize(type2);
489               rtn.type = exp2.type;
490             }
491           rtn.seg = 0;
492           exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
493           break;
494         case EXP_OP_SEG:
495           rtn.seg = VAL(exp1);
496           exp->un.binop.result = VAL(exp2);
497           break;
498         case EXP_OP_LOR:
499           rtn.seg = 0;
500           exp->un.binop.result = (VAL(exp1) || VAL(exp2));
501           break;
502         case EXP_OP_LAND:
503           rtn.seg = 0;
504           exp->un.binop.result = (VAL(exp1) && VAL(exp2));
505           break;
506         case EXP_OP_OR:
507           rtn.seg = 0;
508           exp->un.binop.result = (VAL(exp1) | VAL(exp2));
509           break;
510         case EXP_OP_AND:
511           rtn.seg = 0;
512           exp->un.binop.result = (VAL(exp1) & VAL(exp2));
513           break;
514         case EXP_OP_XOR:
515           rtn.seg = 0;
516           exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
517           break;
518         case EXP_OP_EQ:
519           rtn.seg = 0;
520           exp->un.binop.result = (VAL(exp1) == VAL(exp2));
521           break;
522         case EXP_OP_GT:
523           rtn.seg = 0;
524           exp->un.binop.result = (VAL(exp1) > VAL(exp2));
525           break;
526         case EXP_OP_LT:
527           rtn.seg = 0;
528           exp->un.binop.result = (VAL(exp1) < VAL(exp2));
529           break;
530         case EXP_OP_GE:
531           rtn.seg = 0;
532           exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
533           break;
534         case EXP_OP_LE:
535           rtn.seg = 0;
536           exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
537           break;
538         case EXP_OP_NE:
539           rtn.seg = 0;
540           exp->un.binop.result = (VAL(exp1) != VAL(exp2));
541           break;
542         case EXP_OP_SHL:
543           rtn.seg = 0;
544           exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
545           break;
546         case EXP_OP_SHR:
547           rtn.seg = 0;
548           exp->un.binop.result = ((unsigned) VAL(exp1) >> VAL(exp2));
549           break;
550         case EXP_OP_MUL:
551           rtn.seg = 0;
552           exp->un.binop.result = (VAL(exp1) * VAL(exp2));
553           break;
554         case EXP_OP_DIV:
555           if( VAL(exp2) != 0 )
556             {
557               rtn.seg = 0;
558               exp->un.binop.result = (VAL(exp1) / VAL(exp2));
559             }
560           else
561             {
562               rtn.seg = 0;
563               rtn.type = NULL;
564               rtn.off = 0;
565             }
566           break;
567         case EXP_OP_REM:
568           if( VAL(exp2) != 0 )
569             {
570               rtn.seg = 0;
571               exp->un.binop.result = (VAL(exp1) % VAL(exp2));
572             }
573           else
574             {
575               rtn.seg = 0;
576               rtn.type = NULL;
577               rtn.off = 0;
578             }
579           break;
580         case EXP_OP_ARR:
581           DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
582           break;
583         default:
584           break;
585         }
586       break;
587     case EXPR_TYPE_UNOP:
588       exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
589       if( exp1.type == NULL )
590         {
591           break;
592         }
593       rtn.off = (unsigned int) &exp->un.unop.result;
594       if( exp1.type == DEBUG_TypeIntConst )
595         {
596           rtn.type = exp1.type;
597         }
598       else
599         {
600           rtn.type = DEBUG_TypeInt;
601         }
602       switch(exp->un.binop.binop_type)
603         {
604         case EXP_OP_NEG:
605           rtn.seg = 0;
606           exp->un.unop.result = -VAL(exp1);
607           break;
608         case EXP_OP_NOT:
609           rtn.seg = 0;
610           exp->un.unop.result = !VAL(exp1);
611           break;
612         case EXP_OP_LNOT:
613           rtn.seg = 0;
614           exp->un.unop.result = ~VAL(exp1);
615           break;
616         case EXP_OP_DEREF:
617           rtn.seg = 0;
618           rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
619           break;
620         case EXP_OP_FORCE_DEREF:
621           rtn.seg = 0;
622           rtn.off = *(unsigned int *) exp1.off;
623           break;
624         case EXP_OP_ADDR:
625           rtn.seg = 0;
626           rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
627           exp->un.unop.result = exp1.off;
628           break;
629         }
630       break;
631     default:
632       fprintf(stderr,"Unexpected expression.\n");
633       exit(123);
634       break;
635     }
636
637   return rtn;
638 }
639
640
641 int
642 DEBUG_DisplayExpr(struct expr * exp)
643 {
644   int           i;
645
646
647   switch(exp->type)
648     {
649     case EXPR_TYPE_CAST:
650       fprintf(stderr, "((");
651       DEBUG_PrintTypeCast(exp->un.cast.cast);
652       fprintf(stderr, ")");
653       DEBUG_DisplayExpr(exp->un.cast.expr);
654       fprintf(stderr, ")");
655       break;
656     case EXPR_TYPE_REGISTER:
657       DEBUG_PrintRegister(exp->un.rgister.reg);
658       break;
659     case EXPR_TYPE_US_CONST:
660       fprintf(stderr, "%ud", exp->un.u_const.value);
661       break;
662     case EXPR_TYPE_CONST:
663       fprintf(stderr, "%d", exp->un.u_const.value);
664       break;
665     case EXPR_TYPE_STRING:
666       fprintf(stderr, "\"%s\"", exp->un.string.str);
667       break;
668     case EXPR_TYPE_SYMBOL:
669       fprintf(stderr, "%s" , exp->un.symbol.name);
670       break;
671     case EXPR_TYPE_PSTRUCT:
672       DEBUG_DisplayExpr(exp->un.structure.exp1);
673       fprintf(stderr, "->%s", exp->un.structure.element_name);
674       break;
675     case EXPR_TYPE_STRUCT:
676       DEBUG_DisplayExpr(exp->un.structure.exp1);
677       fprintf(stderr, ".%s", exp->un.structure.element_name);
678       break;
679     case EXPR_TYPE_CALL:
680       /*
681        * First, evaluate all of the arguments.  If any of them are not
682        * evaluable, then bail.
683        */
684       fprintf(stderr, "%s(",exp->un.call.funcname);
685       for(i=0; i < exp->un.call.nargs; i++)
686         {
687           DEBUG_DisplayExpr(exp->un.call.arg[i]);
688           if( i != exp->un.call.nargs - 1 )
689             {
690               fprintf(stderr, ", ");
691             }
692         }
693       fprintf(stderr, ")");
694       break;
695     case EXPR_TYPE_BINOP:
696       fprintf(stderr, "( ");
697       DEBUG_DisplayExpr(exp->un.binop.exp1);
698       switch(exp->un.binop.binop_type)
699         {
700         case EXP_OP_ADD:
701           fprintf(stderr, " + ");
702           break;
703         case EXP_OP_SUB:
704           fprintf(stderr, " - ");
705           break;
706         case EXP_OP_SEG:
707           fprintf(stderr, ":");
708           break;
709         case EXP_OP_LOR:
710           fprintf(stderr, " || ");
711           break;
712         case EXP_OP_LAND:
713           fprintf(stderr, " && ");
714           break;
715         case EXP_OP_OR:
716           fprintf(stderr, " | ");
717           break;
718         case EXP_OP_AND:
719           fprintf(stderr, " & ");
720           break;
721         case EXP_OP_XOR:
722           fprintf(stderr, " ^ ");
723           break;
724         case EXP_OP_EQ:
725           fprintf(stderr, " == ");
726           break;
727         case EXP_OP_GT:
728           fprintf(stderr, " > ");
729           break;
730         case EXP_OP_LT:
731           fprintf(stderr, " < ");
732           break;
733         case EXP_OP_GE:
734           fprintf(stderr, " >= ");
735           break;
736         case EXP_OP_LE:
737           fprintf(stderr, " <= ");
738           break;
739         case EXP_OP_NE:
740           fprintf(stderr, " != ");
741           break;
742         case EXP_OP_SHL:
743           fprintf(stderr, " << ");
744           break;
745         case EXP_OP_SHR:
746           fprintf(stderr, " >> ");
747           break;
748         case EXP_OP_MUL:
749           fprintf(stderr, " * ");
750           break;
751         case EXP_OP_DIV:
752           fprintf(stderr, " / ");
753           break;
754         case EXP_OP_REM:
755           fprintf(stderr, " %% ");
756           break;
757         case EXP_OP_ARR:
758           fprintf(stderr, "[");
759           break;
760         default:
761           break;
762         }
763       DEBUG_DisplayExpr(exp->un.binop.exp2);
764       if( exp->un.binop.binop_type == EXP_OP_ARR )
765         {
766           fprintf(stderr, "]");
767         }
768       fprintf(stderr, " )");
769       break;
770     case EXPR_TYPE_UNOP:
771       switch(exp->un.binop.binop_type)
772         {
773         case EXP_OP_NEG:
774           fprintf(stderr, "-");
775           break;
776         case EXP_OP_NOT:
777           fprintf(stderr, "!");
778           break;
779         case EXP_OP_LNOT:
780           fprintf(stderr, "~");
781           break;
782         case EXP_OP_DEREF:
783           fprintf(stderr, "*");
784           break;
785         case EXP_OP_ADDR:
786           fprintf(stderr, "&");
787           break;
788         }
789       DEBUG_DisplayExpr(exp->un.unop.exp1);
790       break;
791     default:
792       fprintf(stderr,"Unexpected expression.\n");
793       exit(123);
794       break;
795     }
796
797   return TRUE;
798 }
799
800 struct expr *
801 DEBUG_CloneExpr(struct expr * exp)
802 {
803   int           i;
804   struct expr * rtn;
805
806   rtn = (struct expr *) xmalloc(sizeof(struct expr));
807
808   /*
809    * First copy the contents of the expression itself.
810    */
811   *rtn = *exp;
812
813
814   switch(exp->type)
815     {
816     case EXPR_TYPE_CAST:
817       rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
818       break;
819     case EXPR_TYPE_REGISTER:
820     case EXPR_TYPE_US_CONST:
821     case EXPR_TYPE_CONST:
822       break;
823     case EXPR_TYPE_STRING:
824       rtn->un.string.str = xstrdup(exp->un.string.str);
825       break;
826     case EXPR_TYPE_SYMBOL:
827       rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
828       break;
829     case EXPR_TYPE_PSTRUCT:
830     case EXPR_TYPE_STRUCT:
831       rtn->un.structure.exp1 = DEBUG_CloneExpr(exp->un.structure.exp1);
832       rtn->un.structure.element_name = xstrdup(exp->un.structure.element_name);
833       break;
834     case EXPR_TYPE_CALL:
835       /*
836        * First, evaluate all of the arguments.  If any of them are not
837        * evaluable, then bail.
838        */
839       for(i=0; i < exp->un.call.nargs; i++)
840         {
841           rtn->un.call.arg[i]  = DEBUG_CloneExpr(exp->un.call.arg[i]);
842         }
843       rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
844       break;
845     case EXPR_TYPE_BINOP:
846       rtn->un.binop.exp1 = DEBUG_CloneExpr(exp->un.binop.exp1);
847       rtn->un.binop.exp2 = DEBUG_CloneExpr(exp->un.binop.exp2);
848       break;
849     case EXPR_TYPE_UNOP:
850       rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
851       break;
852     default:
853       fprintf(stderr,"Unexpected expression.\n");
854       exit(123);
855       break;
856     }
857
858   return rtn;
859 }
860
861
862 /*
863  * Recursively go through an expression tree and free all memory associated
864  * with it.
865  */
866 int
867 DEBUG_FreeExpr(struct expr * exp)
868 {
869   int i;
870
871   switch(exp->type)
872     {
873     case EXPR_TYPE_CAST:
874       DEBUG_FreeExpr(exp->un.cast.expr);
875       break;
876     case EXPR_TYPE_REGISTER:
877     case EXPR_TYPE_US_CONST:
878     case EXPR_TYPE_CONST:
879       break;
880     case EXPR_TYPE_STRING:
881       free((char *) exp->un.string.str);
882       break;
883     case EXPR_TYPE_SYMBOL:
884       free((char *) exp->un.symbol.name);
885       break;
886     case EXPR_TYPE_PSTRUCT:
887     case EXPR_TYPE_STRUCT:
888       DEBUG_FreeExpr(exp->un.structure.exp1);
889       free((char *) exp->un.structure.element_name);
890       break;
891     case EXPR_TYPE_CALL:
892       /*
893        * First, evaluate all of the arguments.  If any of them are not
894        * evaluable, then bail.
895        */
896       for(i=0; i < exp->un.call.nargs; i++)
897         {
898           DEBUG_FreeExpr(exp->un.call.arg[i]);
899         }
900       free((char *) exp->un.call.funcname);
901       break;
902     case EXPR_TYPE_BINOP:
903       DEBUG_FreeExpr(exp->un.binop.exp1);
904       DEBUG_FreeExpr(exp->un.binop.exp2);
905       break;
906     case EXPR_TYPE_UNOP:
907       DEBUG_FreeExpr(exp->un.unop.exp1);
908       break;
909     default:
910       fprintf(stderr,"Unexpected expression.\n");
911       exit(123);
912       break;
913     }
914
915   free(exp);
916   return TRUE;
917 }