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