fix dangling pointer issue. If the node being processed is replaced,
[xorcyst.git] / RCS / unit.c,v
blob27a56acc1070c71e150a3898e3ea13100e55174b
1 head    1.4;
2 access;
3 symbols;
4 locks; strict;
5 comment @ * @;
8 1.4
9 date    2007.07.22.13.33.26;    author khansen; state Exp;
10 branches;
11 next    1.3;
13 1.3
14 date    2004.12.18.17.00.35;    author kenth;   state Exp;
15 branches;
16 next    1.2;
18 1.2
19 date    2004.12.16.13.20.07;    author kenth;   state Exp;
20 branches;
21 next    1.1;
23 1.1
24 date    2004.06.30.07.56.00;    author kenth;   state Exp;
25 branches;
26 next    ;
29 desc
33 1.4
34 log
35 @convert tabs to whitespaces
37 text
38 @/*
39  * $Id: unit.c,v 1.3 2004/12/18 17:00:35 kenth Exp $
40  * $Log: unit.c,v $
41  * Revision 1.3  2004/12/18 17:00:35  kenth
42  * improved error reporting slightly
43  *
44  * Revision 1.2  2004/12/16 13:20:07  kenth
45  * xorcyst 1.3.5
46  *
47  * Revision 1.1  2004/06/30 07:56:00  kenth
48  * Initial revision
49  *
50  */
52 /**
53  *    (C) 2004 Kent Hansen
54  *
55  *    The XORcyst is free software; you can redistribute it and/or modify
56  *    it under the terms of the GNU General Public License as published by
57  *    the Free Software Foundation; either version 2 of the License, or
58  *    (at your option) any later version.
59  *
60  *    The XORcyst is distributed in the hope that it will be useful,
61  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
62  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
63  *    GNU General Public License for more details.
64  *
65  *    You should have received a copy of the GNU General Public License
66  *    along with The XORcyst; if not, write to the Free Software
67  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
68  */
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include "unit.h"
73 #include "objdef.h"
75 #define SAFE_FREE(m) if ((m) != NULL) { free(m); m = NULL; }
77 /*---------------------------------------------------------------------------*/
79 /* Reads a byte */
80 #define get_1(f) (unsigned char)fgetc(fp)
81 /* Reads a short (big-endian) */
82 static unsigned short get_2(FILE *fp)
84     unsigned short result;
85     result = get_1(fp) << 8;    /* High byte */
86     result |= get_1(fp);        /* Low byte */
87     return result;
89 /* Reads a 24-bit integer (big-endian) */
90 static unsigned int get_3(FILE *fp)
92     unsigned int result;
93     result = get_2(fp) << 8;    /* High 16 bits */
94     result |= get_1(fp);        /* Low byte */
95     return result;
97 /* Reads an int (big-endian) */
98 static unsigned int get_4(FILE *fp)
100     unsigned int result;
101     /* Assumes little-endian machine!! */
102     result = get_2(fp) << 16;   /* High 16 bits */
103     result |= get_2(fp);        /* Low 16 bits */
104     return result;
106 /* Reads a string prepended by 8-bit length */
107 static char *get_str_8(FILE *fp)
109     char *s;
110     /* Read length */
111     int len = get_1(fp) + 1;
112     /* Allocate space for string */
113     s = (char *)malloc(len + 1);
114     if (s != NULL) {
115         /* Read string from file */
116         fread(s, 1, len, fp);
117         /* 0-terminate string */
118         s[len] = '\0';
119     }
120     /* Return the new string */
121     return s;
123 /* Reads a string prepended by 16-bit length */
124 static char *get_str_16(FILE *fp)
126     char *s;
127     /* Read length */
128     int len = get_2(fp) + 1;
129     /* Allocate space for string */
130     s = (char *)malloc(len + 1);
131     if (s != NULL) {
132         /* Read string from file */
133         fread(s, 1, len, fp);
134     }
135     /* Return the new string */
136     return s;
138 #define get_bytes_8(f) (unsigned char *)get_str_8(f)
139 #define get_bytes_16(f) (unsigned char *)get_str_16(f)
141 /*--------------------------------------------------------------------------*/
144  * Reads a constant from file.
145  * @@param fp File handle
146  * @@param u Unit in which constant is loaded
147  * @@param i Index of constant in unit's constant array
148  */
149 static void get_const(FILE *fp, unit *u, int i)
151     constant *cnst = &u->constants[i];
152     /* Read name */
153     cnst->name = get_str_8(fp);
154     /* Read type */
155     cnst->type = get_1(fp);
156     /* Read value */
157     switch (cnst->type) {
158         case INT_8: cnst->integer = get_1(fp);  cnst->type = INTEGER_CONSTANT;  break;
159         case INT_16:    cnst->integer = get_2(fp);  cnst->type = INTEGER_CONSTANT;  break;
160         case INT_24:    cnst->integer = get_3(fp);  cnst->type = INTEGER_CONSTANT;  break;
161         case INT_32:    cnst->integer = get_4(fp);  cnst->type = INTEGER_CONSTANT;  break;
162         case STR_8: cnst->string = get_str_8(fp);   cnst->type = STRING_CONSTANT;   break;
163         case STR_16:    cnst->string = get_str_16(fp);  cnst->type = STRING_CONSTANT;   break;
165         default:
166         /* Error, invalid type */
167         fprintf(stderr, "%s(0x%lx): get_const(): bad constant type (%.2X)\n", u->name, ftell(fp), cnst->type);
168         break;
169     }
170     /* Set owner */
171     cnst->unit = u;
175  * Reads constant array from file.
176  * @@param fp File handle
177  * @@param u Unit whose constants array will be populated
178  */
179 static void get_constants(FILE *fp, unit *u)
181     int i;
182     /* Read the number of constants */
183     int count = get_2(fp);
184     /* Allocate the list */
185     if (count > 0) {
186         u->constants = (constant *)malloc(sizeof(constant) * count);
187     }
188     else {
189         u->constants = NULL;
190     }
191     /* Read constants */
192     for (i=0; i<count; i++) {
193         get_const(fp, u, i);
194     }
195     /* Set count field */
196     u->const_count = count;
200  * Reads imported symbol from file.
201  * @@param fp File handle
202  * @@param u Unit
203  * @@param i External index
204  */
205 static void get_ext(FILE *fp, unit *u, int i)
207     external *ext = &u->externals[i];
208     /* Read unit # */
209     ext->unit = get_1(fp);
210     /* Read name */
211     ext->name = get_str_8(fp);
212     /* Set owner */
213     ext->from = u;
217  * Reads imported symbol array from file.
218  * @@param fp File handle
219  * @@param u Unit whose externals array will be populated
220  */
221 static void get_externals(FILE *fp, unit *u)
223     int i;
224     int count;
225     /* Read the number of imported symbols */
226     count = get_2(fp);
227     /* Allocate the list */
228     if (count > 0) {
229         u->externals = (external *)malloc(sizeof(external) * count);
230     }
231     else {
232         u->externals = NULL;
233     }
234     /* Read imported symbols */
235     for (i=0; i<count; i++) {
236         get_ext(fp, u, i);
237     }
238     /* Set count field */
239     u->ext_count = count;
243  * Reads an expression from file.
244  * @@param fp File handle
245  * @@param dest Pointer to pointer where expression should be stored
246  * @@param u Owner unit
247  */
248 static void get_expr_recursive(FILE *fp, expression **dest, unit *u)
250     unsigned char type;
251     /* Allocate space for expression */
252     expression *exp = (expression *)malloc( sizeof(expression) );
253     if (exp != NULL) {
254         /* Read expression type */
255         type = get_1(fp);
256         /* Read the value */
257         switch (type) {
258             /* Literals */
259             case INT_8: exp->integer = get_1(fp);   exp->type = INTEGER_EXPRESSION; break;
260             case INT_16:    exp->integer = get_2(fp);   exp->type = INTEGER_EXPRESSION; break;
261             case INT_24:    exp->integer = get_3(fp);   exp->type = INTEGER_EXPRESSION; break;
262             case INT_32:    exp->integer = get_4(fp);   exp->type = INTEGER_EXPRESSION; break;
263             case STR_8: exp->string = get_str_8(fp);    exp->type = STRING_EXPRESSION;  break;
264             case STR_16:    exp->string = get_str_16(fp);   exp->type = STRING_EXPRESSION;  break;
266             /* Identifiers */
267             case LOCAL: exp->local_id = get_2(fp);  exp->type = LOCAL_EXPRESSION;   break;
268             case EXTRN: exp->extrn_id = get_2(fp);  exp->type = EXTERNAL_EXPRESSION;break;
270             /* Current address */
271             case PC:    ;               exp->type = PC_EXPRESSION;  break;
273             /* Arithmetic, binary */
274             case OP_PLUS:
275             case OP_MINUS:
276             case OP_MUL:
277             case OP_DIV:
278             case OP_MOD:
279             case OP_SHL:
280             case OP_SHR:
281             case OP_AND:
282             case OP_OR:
283             case OP_XOR:
284             case OP_EQ:
285             case OP_NE:
286             case OP_LT:
287             case OP_GT:
288             case OP_LE:
289             case OP_GE:
290             get_expr_recursive(fp, &exp->op_expr.lhs, u);
291             get_expr_recursive(fp, &exp->op_expr.rhs, u);
292             exp->op_expr.operator = type;
293             exp->type = OPERATOR_EXPRESSION;
294             break;
296             /* Arithmetic, unary */
297             case OP_NOT:
298             case OP_NEG:
299             case OP_LO:
300             case OP_HI:
301             case OP_UMINUS:
302             case OP_BANK:
303             get_expr_recursive(fp, &exp->op_expr.lhs, u);
304             exp->op_expr.rhs = NULL;
305             exp->op_expr.operator = type;
306             exp->type = OPERATOR_EXPRESSION;
307             break;
309             default:
310             fprintf(stderr, "%s(0x%lx): get_expr(): invalid expression type (%.2X)\n", u->name, ftell(fp), type);
311             exp->integer = 0;
312             exp->type = INTEGER_EXPRESSION;
313             break;
314         }
315     }
316     /* Set owner */
317     exp->unit = u;
318     /* Set result pointer */
319     *dest = exp;
323  * Reads expressions from file.
324  * @@param fp File handle
325  * @@param u Unit whose expressions array to populate
326  */
327 static void get_expressions(FILE *fp, unit *u)
329     int i;
330     int count;
331     /* Read the number of expressions */
332     count = get_2(fp);
333     /* Allocate the list */
334     if (count > 0) {
335         u->expressions = (expression **)malloc(sizeof(expression *) * count);
336     }
337     else {
338         u->expressions = NULL;
339     }
340     /* Read expressions */
341     for (i=0; i<count; i++) {
342         get_expr_recursive(fp, &u->expressions[i], u);
343     }
344     /* Store count */
345     u->expr_count = count;
349  * Reads a segment from file.
350  * @@param fp File handle
351  * @@param seg Where to store segment
352  */
353 static void get_segment(FILE *fp, segment *seg)
355     /* Read size */
356     seg->size = get_3(fp);
357     /* Allocate space */
358     if (seg->size > 0) {
359         /* Allocate mem for bytecodes */
360         seg->bytes = (unsigned char *)malloc(seg->size);
361         if (seg->bytes != NULL) {
362             /* Read contents */
363             fread(seg->bytes, 1, seg->size, fp);
364         }
365     }
366     else {
367         seg->bytes = NULL;
368     }
371 /*--------------------------------------------------------------------------*/
374  * Reads a unit from file.
375  * @@param filename Name of the unit
376  * @@param u Pointer to struct to fill in
377  */
378 int unit_read(char *filename, unit *u)
380     FILE *fp;
381     int count, i;
382     unsigned short magic;
383     unsigned char version;
385     /* Store name */
386     u->name = filename;
387     /* Attempt to open file */
388     fp = fopen(filename, "rb");
389     if (fp == NULL) {
390         /* Error, couldn't open it */
391         u->const_count = 0;
392         u->ext_count = 0;
393         u->expr_count = 0;
394         u->dataseg.size = 0;
395         u->dataseg.bytes = NULL;
396         u->codeseg.size = 0;
397         u->codeseg.bytes = NULL;
398         return 0;
399     }
400     /* Read magic number */
401     magic = get_2(fp);
402     /* Verify magic number */
403     if (magic != A_MAGIC) {
404         /* Error, bad magic number */
405         return 0;
406     }
407     /* Read version */
408     version = get_1(fp);
409     /* Verify version: should be 1.0 */
410     if (version != A_VERSION) {
411         /* Error, bad version */
412         return 0;
413     }
414     /* Read exported constants */
415     get_constants(fp, u);
416     /* Read # of units explicitly imported from */
417     count = get_1(fp);
418     /* Read unit names */
419     for (i=0; i<count; i++) {
420         get_str_8(fp);
421     }
422     /* Read imported symbols */
423     get_externals(fp, u);
424     /* Read dataseg */
425     get_segment(fp, &u->dataseg);
426     /* Read codeseg */
427     get_segment(fp, &u->codeseg);
428     /* Read expressions */
429     get_expressions(fp, u);
431     /* Close the file */
432     fclose(fp);
434     /* Success */
435     return 1;
438 /*--------------------------------------------------------------------------*/
441  * Finalizes a constant.
442  * @@param c Constant to finalize
443  */
444 static void finalize_constant(constant *c)
446     if (c->type == STRING_CONSTANT) {
447         SAFE_FREE(c->string);
448     }
449     SAFE_FREE(c->name);
453  * Finalizes an external.
454  * @@param e External to finalize
455  */
456 static void finalize_external(external *e)
458     SAFE_FREE(e->name);
462  * Finalizes an expression.
463  * @@param e Expression to finalize
464  */
465 static void finalize_expression(expression *e)
467     if (e == NULL) { return; }
468     switch (e->type) {
469         case STRING_EXPRESSION:
470         SAFE_FREE(e->string);
471         break;
473         case OPERATOR_EXPRESSION:
474         finalize_expression(e->op_expr.lhs);
475         finalize_expression(e->op_expr.rhs);
476         break;
478         default:
479         /* Nada */
480         break;
481     }
482     SAFE_FREE(e);
486  * Finalizes a bytecode segment.
487  * @@param s Segment to finalize
488  */
489 static void finalize_segment(segment *s)
491     SAFE_FREE(s->bytes);
494 /*--------------------------------------------------------------------------*/
497  * Finalizes a unit.
498  * @@param u Unit to finalize
499  */
500 void unit_finalize(unit *u)
502     int i;
503     /* Finalize the constants */
504     for (i=0; i<u->const_count; i++) {
505         finalize_constant(&u->constants[i]);
506     }
507     SAFE_FREE(u->constants);
508     /* Finalize the externals */
509     for (i=0; i<u->ext_count; i++) {
510         finalize_external(&u->externals[i]);
511     }
512     SAFE_FREE(u->externals);
513     /* Finalize expressions */
514     for (i=0; i<u->expr_count; i++) {
515         finalize_expression(u->expressions[i]);
516     }
517     SAFE_FREE(u->expressions);
518     /* Finalize bytecode segments */
519     finalize_segment(&u->dataseg);
520     finalize_segment(&u->codeseg);
527 @improved error reporting slightly
529 text
530 @d2 1
531 a2 1
532  * $Id: unit.c,v 1.2 2004/12/16 13:20:07 kenth Exp kenth $
533 d4 3
534 d47 4
535 a50 4
536         unsigned short result;
537         result = get_1(fp) << 8;        /* High byte */
538         result |= get_1(fp);            /* Low byte */
539         return result;
540 d55 4
541 a58 4
542         unsigned int result;
543         result = get_2(fp) << 8;        /* High 16 bits */
544         result |= get_1(fp);            /* Low byte */
545         return result;
546 d63 5
547 a67 5
548         unsigned int result;
549         /* Assumes little-endian machine!! */
550         result = get_2(fp) << 16;       /* High 16 bits */
551         result |= get_2(fp);            /* Low 16 bits */
552         return result;
553 d72 13
554 a84 13
555         char *s;
556         /* Read length */
557         int len = get_1(fp) + 1;
558         /* Allocate space for string */
559         s = (char *)malloc(len + 1);
560         if (s != NULL) {
561                 /* Read string from file */
562                 fread(s, 1, len, fp);
563                 /* 0-terminate string */
564                 s[len] = '\0';
565         }
566         /* Return the new string */
567         return s;
568 d89 11
569 a99 11
570         char *s;
571         /* Read length */
572         int len = get_2(fp) + 1;
573         /* Allocate space for string */
574         s = (char *)malloc(len + 1);
575         if (s != NULL) {
576                 /* Read string from file */
577                 fread(s, 1, len, fp);
578         }
579         /* Return the new string */
580         return s;
581 d114 21
582 a134 21
583         constant *cnst = &u->constants[i];
584         /* Read name */
585         cnst->name = get_str_8(fp);
586         /* Read type */
587         cnst->type = get_1(fp);
588         /* Read value */
589         switch (cnst->type) {
590                 case INT_8:     cnst->integer = get_1(fp);      cnst->type = INTEGER_CONSTANT;  break;
591                 case INT_16:    cnst->integer = get_2(fp);      cnst->type = INTEGER_CONSTANT;  break;
592                 case INT_24:    cnst->integer = get_3(fp);      cnst->type = INTEGER_CONSTANT;  break;
593                 case INT_32:    cnst->integer = get_4(fp);      cnst->type = INTEGER_CONSTANT;  break;
594                 case STR_8:     cnst->string = get_str_8(fp);   cnst->type = STRING_CONSTANT;   break;
595                 case STR_16:    cnst->string = get_str_16(fp);  cnst->type = STRING_CONSTANT;   break;
597                 default:
598                 /* Error, invalid type */
599                 fprintf(stderr, "%s(0x%lx): get_const(): bad constant type (%.2X)\n", u->name, ftell(fp), cnst->type);
600                 break;
601         }
602         /* Set owner */
603         cnst->unit = u;
604 d144 16
605 a159 16
606         int i;
607         /* Read the number of constants */
608         int count = get_2(fp);
609         /* Allocate the list */
610         if (count > 0) {
611                 u->constants = (constant *)malloc(sizeof(constant) * count);
612         }
613         else {
614                 u->constants = NULL;
615         }
616         /* Read constants */
617         for (i=0; i<count; i++) {
618                 get_const(fp, u, i);
619         }
620         /* Set count field */
621         u->const_count = count;
622 d170 7
623 a176 7
624         external *ext = &u->externals[i];
625         /* Read unit # */
626         ext->unit = get_1(fp);
627         /* Read name */
628         ext->name = get_str_8(fp);
629         /* Set owner */
630         ext->from = u;
631 d186 17
632 a202 17
633         int i;
634         int count;
635         /* Read the number of imported symbols */
636         count = get_2(fp);
637         /* Allocate the list */
638         if (count > 0) {
639                 u->externals = (external *)malloc(sizeof(external) * count);
640         }
641         else {
642                 u->externals = NULL;
643         }
644         /* Read imported symbols */
645         for (i=0; i<count; i++) {
646                 get_ext(fp, u, i);
647         }
648         /* Set count field */
649         u->ext_count = count;
650 d213 70
651 a282 70
652         unsigned char type;
653         /* Allocate space for expression */
654         expression *exp = (expression *)malloc( sizeof(expression) );
655         if (exp != NULL) {
656                 /* Read expression type */
657                 type = get_1(fp);
658                 /* Read the value */
659                 switch (type) {
660                         /* Literals */
661                         case INT_8:     exp->integer = get_1(fp);       exp->type = INTEGER_EXPRESSION; break;
662                         case INT_16:    exp->integer = get_2(fp);       exp->type = INTEGER_EXPRESSION; break;
663                         case INT_24:    exp->integer = get_3(fp);       exp->type = INTEGER_EXPRESSION; break;
664                         case INT_32:    exp->integer = get_4(fp);       exp->type = INTEGER_EXPRESSION; break;
665                         case STR_8:     exp->string = get_str_8(fp);    exp->type = STRING_EXPRESSION;  break;
666                         case STR_16:    exp->string = get_str_16(fp);   exp->type = STRING_EXPRESSION;  break;
668                         /* Identifiers */
669                         case LOCAL:     exp->local_id = get_2(fp);      exp->type = LOCAL_EXPRESSION;   break;
670                         case EXTRN:     exp->extrn_id = get_2(fp);      exp->type = EXTERNAL_EXPRESSION;break;
672                         /* Current address */
673                         case PC:        ;                               exp->type = PC_EXPRESSION;      break;
675                         /* Arithmetic, binary */
676                         case OP_PLUS:
677                         case OP_MINUS:
678                         case OP_MUL:
679                         case OP_DIV:
680                         case OP_MOD:
681                         case OP_SHL:
682                         case OP_SHR:
683                         case OP_AND:
684                         case OP_OR:
685                         case OP_XOR:
686                         case OP_EQ:
687                         case OP_NE:
688                         case OP_LT:
689                         case OP_GT:
690                         case OP_LE:
691                         case OP_GE:
692                         get_expr_recursive(fp, &exp->op_expr.lhs, u);
693                         get_expr_recursive(fp, &exp->op_expr.rhs, u);
694                         exp->op_expr.operator = type;
695                         exp->type = OPERATOR_EXPRESSION;
696                         break;
698                         /* Arithmetic, unary */
699                         case OP_NOT:
700                         case OP_NEG:
701                         case OP_LO:
702                         case OP_HI:
703                         case OP_UMINUS:
704                         case OP_BANK:
705                         get_expr_recursive(fp, &exp->op_expr.lhs, u);
706                         exp->op_expr.rhs = NULL;
707                         exp->op_expr.operator = type;
708                         exp->type = OPERATOR_EXPRESSION;
709                         break;
711                         default:
712                         fprintf(stderr, "%s(0x%lx): get_expr(): invalid expression type (%.2X)\n", u->name, ftell(fp), type);
713                         exp->integer = 0;
714                         exp->type = INTEGER_EXPRESSION;
715                         break;
716                 }
717         }
718         /* Set owner */
719         exp->unit = u;
720         /* Set result pointer */
721         *dest = exp;
722 d292 17
723 a308 17
724         int i;
725         int count;
726         /* Read the number of expressions */
727         count = get_2(fp);
728         /* Allocate the list */
729         if (count > 0) {
730                 u->expressions = (expression **)malloc(sizeof(expression *) * count);
731         }
732         else {
733                 u->expressions = NULL;
734         }
735         /* Read expressions */
736         for (i=0; i<count; i++) {
737                 get_expr_recursive(fp, &u->expressions[i], u);
738         }
739         /* Store count */
740         u->expr_count = count;
741 d318 14
742 a331 14
743         /* Read size */
744         seg->size = get_3(fp);
745         /* Allocate space */
746         if (seg->size > 0) {
747                 /* Allocate mem for bytecodes */
748                 seg->bytes = (unsigned char *)malloc(seg->size);
749                 if (seg->bytes != NULL) {
750                         /* Read contents */
751                         fread(seg->bytes, 1, seg->size, fp);
752                 }
753         }
754         else {
755                 seg->bytes = NULL;
756         }
757 d343 50
758 a392 50
759         FILE *fp;
760         int count, i;
761         unsigned short magic;
762         unsigned char version;
764         /* Store name */
765         u->name = filename;
766         /* Attempt to open file */
767         fp = fopen(filename, "rb");
768         if (fp == NULL) {
769                 /* Error, couldn't open it */
770                 u->const_count = 0;
771                 u->ext_count = 0;
772                 u->expr_count = 0;
773                 u->dataseg.size = 0;
774                 u->dataseg.bytes = NULL;
775                 u->codeseg.size = 0;
776                 u->codeseg.bytes = NULL;
777                 return 0;
778         }
779         /* Read magic number */
780         magic = get_2(fp);
781         /* Verify magic number */
782         if (magic != A_MAGIC) {
783                 /* Error, bad magic number */
784                 return 0;
785         }
786         /* Read version */
787         version = get_1(fp);
788         /* Verify version: should be 1.0 */
789         if (version != A_VERSION) {
790                 /* Error, bad version */
791                 return 0;
792         }
793         /* Read exported constants */
794         get_constants(fp, u);
795         /* Read # of units explicitly imported from */
796         count = get_1(fp);
797         /* Read unit names */
798         for (i=0; i<count; i++) {
799                 get_str_8(fp);
800         }
801         /* Read imported symbols */
802         get_externals(fp, u);
803         /* Read dataseg */
804         get_segment(fp, &u->dataseg);
805         /* Read codeseg */
806         get_segment(fp, &u->codeseg);
807         /* Read expressions */
808         get_expressions(fp, u);
809 d394 2
810 a395 2
811         /* Close the file */
812         fclose(fp);
813 d397 2
814 a398 2
815         /* Success */
816         return 1;
817 d409 4
818 a412 4
819         if (c->type == STRING_CONSTANT) {
820                 SAFE_FREE(c->string);
821         }
822         SAFE_FREE(c->name);
823 d421 1
824 a421 1
825         SAFE_FREE(e->name);
826 d430 16
827 a445 16
828         if (e == NULL) { return; }
829         switch (e->type) {
830                 case STRING_EXPRESSION:
831                 SAFE_FREE(e->string);
832                 break;
834                 case OPERATOR_EXPRESSION:
835                 finalize_expression(e->op_expr.lhs);
836                 finalize_expression(e->op_expr.rhs);
837                 break;
839                 default:
840                 /* Nada */
841                 break;
842         }
843         SAFE_FREE(e);
844 d454 1
845 a454 1
846         SAFE_FREE(s->bytes);
847 d465 19
848 a483 19
849         int i;
850         /* Finalize the constants */
851         for (i=0; i<u->const_count; i++) {
852                 finalize_constant(&u->constants[i]);
853         }
854         SAFE_FREE(u->constants);
855         /* Finalize the externals */
856         for (i=0; i<u->ext_count; i++) {
857                 finalize_external(&u->externals[i]);
858         }
859         SAFE_FREE(u->externals);
860         /* Finalize expressions */
861         for (i=0; i<u->expr_count; i++) {
862                 finalize_expression(u->expressions[i]);
863         }
864         SAFE_FREE(u->expressions);
865         /* Finalize bytecode segments */
866         finalize_segment(&u->dataseg);
867         finalize_segment(&u->codeseg);
873 @xorcyst 1.3.5
875 text
876 @d2 1
877 a2 1
878  * $Id: unit.c,v 1.1 2004/06/30 07:56:00 kenth Exp kenth $
879 d4 3
880 d127 1
881 a127 1
882                 printf("get_const(): bad type\n");
883 d270 3
884 a272 1
885                         printf("get_expr: invalid expression type\n");
891 @Initial revision
893 text
894 @d2 5
895 a6 2
896  * $Id$
897  * $Log$
898 d103 2
899 a104 1
900  * @@param cnst Pointer to struct where constant will be loaded
901 d106 1
902 a106 1
903 static void get_const(FILE *fp, constant *cnst)
904 d108 1
905 d127 2
906 d134 1
907 a134 2
908  * @@param list Pointer to array where constants will be stored
909  * @@return Number of constants read
910 d136 1
911 a136 1
912 static int get_constants(FILE *fp, constant **list)
913 d143 1
914 a143 1
915                 *list = (constant *)malloc(sizeof(constant) * count);
916 d146 1
917 a146 1
918                 *list = NULL;
919 d150 1
920 a150 1
921                 get_const(fp, &((*list)[i]));
922 d152 2
923 a153 2
924         /* Return the number of constants read */
925         return count;
926 d159 2
927 a160 1
928  * @@param ext Pointer to struct where external info will be stored
929 d162 1
930 a162 1
931 static void get_ext(FILE *fp, external *ext)
932 d164 1
933 d169 2
934 d176 1
935 a176 1
936  * @@param list Pointer to array where external infos will be stored
937 d178 1
938 a178 1
939 static int get_externals(FILE *fp, external **list)
940 d186 1
941 a186 1
942                 *list = (external *)malloc(sizeof(external) * count);
943 d189 1
944 a189 1
945                 *list = NULL;
946 d193 1
947 a193 1
948                 get_ext(fp, &((*list)[i]));
949 d195 2
950 a196 2
951         /* Return the number of imported symbols read */
952         return count;
953 d203 1
954 d205 1
955 a205 1
956 static void get_expr(FILE *fp, expression **dest)
957 d247 2
958 a248 2
959                         get_expr(fp, &exp->op_expr.lhs);
960                         get_expr(fp, &exp->op_expr.rhs);
961 d260 1
962 a260 1
963                         get_expr(fp, &exp->op_expr.lhs);
964 d268 1
965 d271 2
966 d280 1
967 a280 2
968  * @@param list Pointer to array of pointers where expressions will be stored
969  * @@return Number of expressions read
970 d282 1
971 a282 1
972 static int get_expressions(FILE *fp, expression ***list)
973 d290 1
974 a290 1
975                 *list = (expression **)malloc(sizeof(expression *) * count);
976 d293 1
977 a293 1
978                 *list = NULL;
979 d297 1
980 a297 1
981                 get_expr(fp, &((*list)[i]));
982 d299 2
983 a300 2
984         /* Return the number of expressions read */
985         return count;
986 d370 1
987 a370 1
988         u->const_count = get_constants(fp, &u->constants);
989 d378 1
990 a378 1
991         u->ext_count = get_externals(fp, &u->externals);
992 d384 1
993 a384 1
994         u->expr_count = get_expressions(fp, &u->expressions);