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