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