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