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