Release 970305
[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         {
422           rtn.seg = CS_reg(&DEBUG_context);
423         }
424       else
425         {
426           rtn.seg = DS_reg(&DEBUG_context);
427         }
428       break;
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 )
433         {
434           break;
435         }
436       if( exp1.type == DEBUG_TypeIntConst && exp2.type == DEBUG_TypeIntConst )
437         {
438           rtn.type = exp1.type;
439         }
440       else
441         {
442           rtn.type = DEBUG_TypeInt;
443         }
444       rtn.off = (unsigned int) &exp->un.binop.result;
445       switch(exp->un.binop.binop_type)
446         {
447         case EXP_OP_ADD:
448           type1 = DEBUG_GetPointerType(exp1.type);
449           type2 = DEBUG_GetPointerType(exp2.type);
450           scale1 = 1;
451           scale2 = 1;
452           if( type1 != NULL && type2 != NULL )
453             {
454               break;
455             }
456           else if( type1 != NULL )
457             {
458               scale2 = DEBUG_GetObjectSize(type1);
459               rtn.type = exp1.type;
460             }
461           else if( type2 != NULL )
462             {
463               scale1 = DEBUG_GetObjectSize(type2);
464               rtn.type = exp2.type;
465             }
466           rtn.seg = 0;
467           exp->un.binop.result = (VAL(exp1) * scale1  + scale2 * VAL(exp2));
468           break;
469         case EXP_OP_SUB:
470           type1 = DEBUG_GetPointerType(exp1.type);
471           type2 = DEBUG_GetPointerType(exp2.type);
472           scale1 = 1;
473           scale2 = 1;
474           scale3 = 1;
475           if( type1 != NULL && type2 != NULL )
476             {
477               if( type1 != type2 )
478                 {
479                   break;
480                 }
481               scale3 = DEBUG_GetObjectSize(type1);
482             }
483           else if( type1 != NULL )
484             {
485               scale2 = DEBUG_GetObjectSize(type1);
486               rtn.type = exp1.type;
487             }
488
489           else if( type2 != NULL )
490             {
491               scale1 = DEBUG_GetObjectSize(type2);
492               rtn.type = exp2.type;
493             }
494           rtn.seg = 0;
495           exp->un.binop.result = (VAL(exp1) - VAL(exp2)) / scale3;
496           break;
497         case EXP_OP_SEG:
498           rtn.seg = VAL(exp1);
499           exp->un.binop.result = VAL(exp2);
500           break;
501         case EXP_OP_LOR:
502           rtn.seg = 0;
503           exp->un.binop.result = (VAL(exp1) || VAL(exp2));
504           break;
505         case EXP_OP_LAND:
506           rtn.seg = 0;
507           exp->un.binop.result = (VAL(exp1) && VAL(exp2));
508           break;
509         case EXP_OP_OR:
510           rtn.seg = 0;
511           exp->un.binop.result = (VAL(exp1) | VAL(exp2));
512           break;
513         case EXP_OP_AND:
514           rtn.seg = 0;
515           exp->un.binop.result = (VAL(exp1) & VAL(exp2));
516           break;
517         case EXP_OP_XOR:
518           rtn.seg = 0;
519           exp->un.binop.result = (VAL(exp1) ^ VAL(exp2));
520           break;
521         case EXP_OP_EQ:
522           rtn.seg = 0;
523           exp->un.binop.result = (VAL(exp1) == VAL(exp2));
524           break;
525         case EXP_OP_GT:
526           rtn.seg = 0;
527           exp->un.binop.result = (VAL(exp1) > VAL(exp2));
528           break;
529         case EXP_OP_LT:
530           rtn.seg = 0;
531           exp->un.binop.result = (VAL(exp1) < VAL(exp2));
532           break;
533         case EXP_OP_GE:
534           rtn.seg = 0;
535           exp->un.binop.result = (VAL(exp1) >= VAL(exp2));
536           break;
537         case EXP_OP_LE:
538           rtn.seg = 0;
539           exp->un.binop.result = (VAL(exp1) <= VAL(exp2));
540           break;
541         case EXP_OP_NE:
542           rtn.seg = 0;
543           exp->un.binop.result = (VAL(exp1) != VAL(exp2));
544           break;
545         case EXP_OP_SHL:
546           rtn.seg = 0;
547           exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
548           break;
549         case EXP_OP_SHR:
550           rtn.seg = 0;
551           exp->un.binop.result = ((unsigned) VAL(exp1) << VAL(exp2));
552           break;
553         case EXP_OP_MUL:
554           rtn.seg = 0;
555           exp->un.binop.result = (VAL(exp1) * VAL(exp2));
556           break;
557         case EXP_OP_DIV:
558           if( VAL(exp2) != 0 )
559             {
560               rtn.seg = 0;
561               exp->un.binop.result = (VAL(exp1) / VAL(exp2));
562             }
563           else
564             {
565               rtn.seg = 0;
566               rtn.type = NULL;
567               rtn.off = 0;
568             }
569           break;
570         case EXP_OP_REM:
571           if( VAL(exp2) != 0 )
572             {
573               rtn.seg = 0;
574               exp->un.binop.result = (VAL(exp1) % VAL(exp2));
575             }
576           else
577             {
578               rtn.seg = 0;
579               rtn.type = NULL;
580               rtn.off = 0;
581             }
582           break;
583         case EXP_OP_ARR:
584           DEBUG_ArrayIndex(&exp1, &rtn, VAL(exp2));
585           break;
586         default:
587           break;
588         }
589       break;
590     case EXPR_TYPE_UNOP:
591       exp1 = DEBUG_EvalExpr(exp->un.unop.exp1);
592       if( exp1.type == NULL )
593         {
594           break;
595         }
596       rtn.off = (unsigned int) &exp->un.unop.result;
597       if( exp1.type == DEBUG_TypeIntConst )
598         {
599           rtn.type = exp1.type;
600         }
601       else
602         {
603           rtn.type = DEBUG_TypeInt;
604         }
605       switch(exp->un.binop.binop_type)
606         {
607         case EXP_OP_NEG:
608           rtn.seg = 0;
609           exp->un.unop.result = -VAL(exp1);
610           break;
611         case EXP_OP_NOT:
612           rtn.seg = 0;
613           exp->un.unop.result = !VAL(exp1);
614           break;
615         case EXP_OP_LNOT:
616           rtn.seg = 0;
617           exp->un.unop.result = ~VAL(exp1);
618           break;
619         case EXP_OP_DEREF:
620           rtn.seg = 0;
621           rtn.off = (unsigned int) DEBUG_TypeDerefPointer(&exp1, &rtn.type);
622           break;
623         case EXP_OP_FORCE_DEREF:
624           rtn.seg = 0;
625           rtn.off = *(unsigned int *) exp1.off;
626           break;
627         case EXP_OP_ADDR:
628           rtn.seg = 0;
629           rtn.type = DEBUG_FindOrMakePointerType(exp1.type);
630           exp->un.unop.result = exp1.off;
631           break;
632         }
633       break;
634     default:
635       fprintf(stderr,"Unexpected expression.\n");
636       exit(123);
637       break;
638     }
639
640   return rtn;
641 }
642
643
644 int
645 DEBUG_DisplayExpr(struct expr * exp)
646 {
647   int           i;
648
649
650   switch(exp->type)
651     {
652     case EXPR_TYPE_CAST:
653       fprintf(stderr, "((");
654       DEBUG_PrintTypeCast(exp->un.cast.cast);
655       fprintf(stderr, ")");
656       DEBUG_DisplayExpr(exp->un.cast.expr);
657       fprintf(stderr, ")");
658       break;
659     case EXPR_TYPE_REGISTER:
660       DEBUG_PrintRegister(exp->un.rgister.reg);
661       break;
662     case EXPR_TYPE_US_CONST:
663       fprintf(stderr, "%ud", exp->un.u_const.value);
664       break;
665     case EXPR_TYPE_CONST:
666       fprintf(stderr, "%d", exp->un.u_const.value);
667       break;
668     case EXPR_TYPE_STRING:
669       fprintf(stderr, "\"%s\"", exp->un.string.str);
670       break;
671     case EXPR_TYPE_SYMBOL:
672       fprintf(stderr, "%s" , exp->un.symbol.name);
673       break;
674     case EXPR_TYPE_PSTRUCT:
675       DEBUG_DisplayExpr(exp->un.structure.exp1);
676       fprintf(stderr, "->%s", exp->un.structure.element_name);
677       break;
678     case EXPR_TYPE_STRUCT:
679       DEBUG_DisplayExpr(exp->un.structure.exp1);
680       fprintf(stderr, ".%s", exp->un.structure.element_name);
681       break;
682     case EXPR_TYPE_CALL:
683       /*
684        * First, evaluate all of the arguments.  If any of them are not
685        * evaluable, then bail.
686        */
687       fprintf(stderr, "%s(",exp->un.call.funcname);
688       for(i=0; i < exp->un.call.nargs; i++)
689         {
690           DEBUG_DisplayExpr(exp->un.call.arg[i]);
691           if( i != exp->un.call.nargs - 1 )
692             {
693               fprintf(stderr, ", ");
694             }
695         }
696       fprintf(stderr, ")");
697       break;
698     case EXPR_TYPE_BINOP:
699       fprintf(stderr, "( ");
700       DEBUG_DisplayExpr(exp->un.binop.exp1);
701       switch(exp->un.binop.binop_type)
702         {
703         case EXP_OP_ADD:
704           fprintf(stderr, " + ");
705           break;
706         case EXP_OP_SUB:
707           fprintf(stderr, " - ");
708           break;
709         case EXP_OP_SEG:
710           fprintf(stderr, ":");
711           break;
712         case EXP_OP_LOR:
713           fprintf(stderr, " || ");
714           break;
715         case EXP_OP_LAND:
716           fprintf(stderr, " && ");
717           break;
718         case EXP_OP_OR:
719           fprintf(stderr, " | ");
720           break;
721         case EXP_OP_AND:
722           fprintf(stderr, " & ");
723           break;
724         case EXP_OP_XOR:
725           fprintf(stderr, " ^ ");
726           break;
727         case EXP_OP_EQ:
728           fprintf(stderr, " == ");
729           break;
730         case EXP_OP_GT:
731           fprintf(stderr, " > ");
732           break;
733         case EXP_OP_LT:
734           fprintf(stderr, " < ");
735           break;
736         case EXP_OP_GE:
737           fprintf(stderr, " >= ");
738           break;
739         case EXP_OP_LE:
740           fprintf(stderr, " <= ");
741           break;
742         case EXP_OP_NE:
743           fprintf(stderr, " != ");
744           break;
745         case EXP_OP_SHL:
746           fprintf(stderr, " << ");
747           break;
748         case EXP_OP_SHR:
749           fprintf(stderr, " >> ");
750           break;
751         case EXP_OP_MUL:
752           fprintf(stderr, " * ");
753           break;
754         case EXP_OP_DIV:
755           fprintf(stderr, " / ");
756           break;
757         case EXP_OP_REM:
758           fprintf(stderr, " %% ");
759           break;
760         case EXP_OP_ARR:
761           fprintf(stderr, "[");
762           break;
763         default:
764           break;
765         }
766       DEBUG_DisplayExpr(exp->un.binop.exp2);
767       if( exp->un.binop.binop_type == EXP_OP_ARR )
768         {
769           fprintf(stderr, "]");
770         }
771       fprintf(stderr, " )");
772       break;
773     case EXPR_TYPE_UNOP:
774       switch(exp->un.binop.binop_type)
775         {
776         case EXP_OP_NEG:
777           fprintf(stderr, "-");
778           break;
779         case EXP_OP_NOT:
780           fprintf(stderr, "!");
781           break;
782         case EXP_OP_LNOT:
783           fprintf(stderr, "~");
784           break;
785         case EXP_OP_DEREF:
786           fprintf(stderr, "*");
787           break;
788         case EXP_OP_ADDR:
789           fprintf(stderr, "&");
790           break;
791         }
792       DEBUG_DisplayExpr(exp->un.unop.exp1);
793       break;
794     default:
795       fprintf(stderr,"Unexpected expression.\n");
796       exit(123);
797       break;
798     }
799
800   return TRUE;
801 }
802
803 struct expr *
804 DEBUG_CloneExpr(struct expr * exp)
805 {
806   int           i;
807   struct expr * rtn;
808
809   rtn = (struct expr *) xmalloc(sizeof(struct expr));
810
811   /*
812    * First copy the contents of the expression itself.
813    */
814   *rtn = *exp;
815
816
817   switch(exp->type)
818     {
819     case EXPR_TYPE_CAST:
820       rtn->un.cast.expr = DEBUG_CloneExpr(exp->un.cast.expr);
821       break;
822     case EXPR_TYPE_REGISTER:
823     case EXPR_TYPE_US_CONST:
824     case EXPR_TYPE_CONST:
825       break;
826     case EXPR_TYPE_STRING:
827       rtn->un.string.str = xstrdup(exp->un.string.str);
828       break;
829     case EXPR_TYPE_SYMBOL:
830       rtn->un.symbol.name = xstrdup(exp->un.symbol.name);
831       break;
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);
836       break;
837     case EXPR_TYPE_CALL:
838       /*
839        * First, evaluate all of the arguments.  If any of them are not
840        * evaluable, then bail.
841        */
842       for(i=0; i < exp->un.call.nargs; i++)
843         {
844           rtn->un.call.arg[i]  = DEBUG_CloneExpr(exp->un.call.arg[i]);
845         }
846       rtn->un.call.funcname = xstrdup(exp->un.call.funcname);
847       break;
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);
851       break;
852     case EXPR_TYPE_UNOP:
853       rtn->un.unop.exp1 = DEBUG_CloneExpr(exp->un.unop.exp1);
854       break;
855     default:
856       fprintf(stderr,"Unexpected expression.\n");
857       exit(123);
858       break;
859     }
860
861   return rtn;
862 }
863
864
865 /*
866  * Recursively go through an expression tree and free all memory associated
867  * with it.
868  */
869 int
870 DEBUG_FreeExpr(struct expr * exp)
871 {
872   int i;
873
874   switch(exp->type)
875     {
876     case EXPR_TYPE_CAST:
877       DEBUG_FreeExpr(exp->un.cast.expr);
878       break;
879     case EXPR_TYPE_REGISTER:
880     case EXPR_TYPE_US_CONST:
881     case EXPR_TYPE_CONST:
882       break;
883     case EXPR_TYPE_STRING:
884       free((char *) exp->un.string.str);
885       break;
886     case EXPR_TYPE_SYMBOL:
887       free((char *) exp->un.symbol.name);
888       break;
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);
893       break;
894     case EXPR_TYPE_CALL:
895       /*
896        * First, evaluate all of the arguments.  If any of them are not
897        * evaluable, then bail.
898        */
899       for(i=0; i < exp->un.call.nargs; i++)
900         {
901           DEBUG_FreeExpr(exp->un.call.arg[i]);
902         }
903       free((char *) exp->un.call.funcname);
904       break;
905     case EXPR_TYPE_BINOP:
906       DEBUG_FreeExpr(exp->un.binop.exp1);
907       DEBUG_FreeExpr(exp->un.binop.exp2);
908       break;
909     case EXPR_TYPE_UNOP:
910       DEBUG_FreeExpr(exp->un.unop.exp1);
911       break;
912     default:
913       fprintf(stderr,"Unexpected expression.\n");
914       exit(123);
915       break;
916     }
917
918   free(exp);
919   return TRUE;
920 }