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