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