1 /******************************************************************************
2 Copyright (c) 1992, 1995, 1996 Xerox Corporation. All rights reserved.
3 Portions of this code were written by Stephen White, aka ghond.
4 Use and copying of this software and preparation of derivative works based
5 upon this software are permitted. Any distribution of this software or
6 derivative works must comply with all applicable United States export
7 control laws. This software is made available AS IS, and Xerox Corporation
8 makes no warranty about the software, its performance or its conformity to
9 any specification. Any person obtaining a copy of this software is requested
10 to send their name and post office or electronic mail address to:
11 Pavel Curtis
12 Xerox PARC
13 3333 Coyote Hill Rd.
14 Palo Alto, CA 94304
15 Pavel@Xerox.Com
16 *****************************************************************************/
17
18 #include "ast.h"
19 #include "exceptions.h"
20 #include "list.h"
21 #include "parser.h"
22 #include "program.h"
23 #include "storage.h"
24 #include "structures.h"
25 #include "utils.h"
26
27 Program *
new_program(void)28 new_program(void)
29 {
30 Program *p = (Program *) mymalloc(sizeof(Program), M_PROGRAM);
31
32 p->ref_count = 1;
33 p->first_lineno = 1;
34 p->cached_lineno = 1;
35 p->cached_lineno_pc = 0;
36 return p;
37 }
38
39 Program *
null_program(void)40 null_program(void)
41 {
42 static Program *p = 0;
43 Var code, errors;
44
45 if (!p) {
46 code = new_list(0);
47 p = parse_list_as_program(code, &errors);
48 if (!p)
49 panic("Can't create the null program!");
50 free_var(code);
51 free_var(errors);
52 }
53 return p;
54 }
55
56 Program *
program_ref(Program * p)57 program_ref(Program * p)
58 {
59 p->ref_count++;
60
61 return p;
62 }
63
64 int
program_bytes(Program * p)65 program_bytes(Program * p)
66 {
67 int i, count;
68
69 count = sizeof(Program);
70 count += p->main_vector.size;
71
72 for (i = 0; i < p->num_literals; i++)
73 count += value_bytes(p->literals[i]);
74
75 count += sizeof(Bytecodes) * p->fork_vectors_size;
76 for (i = 0; i < p->fork_vectors_size; i++)
77 count += p->fork_vectors[i].size;
78
79 count += sizeof(const char *) * p->num_var_names;
80 for (i = 0; i < p->num_var_names; i++)
81 count += strlen(p->var_names[i]) + 1;
82
83 return count;
84 }
85
86 void
free_program(Program * p)87 free_program(Program * p)
88 {
89 unsigned i;
90
91 p->ref_count--;
92 if (p->ref_count == 0) {
93
94 for (i = 0; i < p->num_literals; i++)
95 /* can't be a list--strings and floats need to be freed, though. */
96 free_var(p->literals[i]);
97 if (p->literals)
98 myfree(p->literals, M_LIT_LIST);
99
100 for (i = 0; i < p->fork_vectors_size; i++)
101 myfree(p->fork_vectors[i].vector, M_BYTECODES);
102 if (p->fork_vectors_size)
103 myfree(p->fork_vectors, M_FORK_VECTORS);
104
105 for (i = 0; i < p->num_var_names; i++)
106 free_str(p->var_names[i]);
107 myfree(p->var_names, M_NAMES);
108
109 myfree(p->main_vector.vector, M_BYTECODES);
110
111 myfree(p, M_PROGRAM);
112 }
113 }
114
115 char rcsid_program[] = "$Id: program.c,v 1.5 1998/12/14 13:18:48 nop Exp $";
116
117 /*
118 * $Log: program.c,v $
119 * Revision 1.5 1998/12/14 13:18:48 nop
120 * Merge UNSAFE_OPTS (ref fixups); fix Log tag placement to fit CVS whims
121 *
122 * Revision 1.4 1997/07/07 03:24:54 nop
123 * Merge UNSAFE_OPTS (r5) after extensive testing.
124 *
125 * Revision 1.3.2.1 1997/06/05 09:00:00 bjj
126 * Cache one pc/lineno pair with each Program. Hopefully most programs that
127 * fail multiple times usually do it on the same line!
128 *
129 * Revision 1.3 1997/03/08 06:25:42 nop
130 * 1.8.0p6 merge by hand.
131 *
132 * Revision 1.2 1997/03/03 04:19:17 nop
133 * GNU Indent normalization
134 *
135 * Revision 1.1.1.1 1997/03/03 03:45:01 nop
136 * LambdaMOO 1.8.0p5
137 *
138 * Revision 2.4 1997/03/04 04:36:18 eostrom
139 * Fixed memory leak in free_program().
140 *
141 * Revision 2.3 1996/04/08 00:41:16 pavel
142 * Corrected an error in the computation of `program_bytes()'.
143 * Release 1.8.0p3.
144 *
145 * Revision 2.2 1996/02/08 06:54:31 pavel
146 * Updated copyright notice for 1996. Release 1.8.0beta1.
147 *
148 * Revision 2.1 1995/12/11 08:04:20 pavel
149 * Added `null_program()' and `program_bytes()'. Release 1.8.0alpha2.
150 *
151 * Revision 2.0 1995/11/30 04:29:58 pavel
152 * New baseline version, corresponding to release 1.8.0alpha1.
153 *
154 * Revision 1.6 1992/10/23 23:03:47 pavel
155 * Added copyright notice.
156 *
157 * Revision 1.5 1992/08/28 16:17:41 pjames
158 * Changed `ak_dealloc_string()' to `free_str()'.
159 *
160 * Revision 1.4 1992/08/10 16:54:22 pjames
161 * Updated #includes.
162 *
163 * Revision 1.3 1992/07/27 18:16:27 pjames
164 * Changed name of ct_env to var_names, const_env to literals, and
165 * f_vectors to fork_vectors.
166 *
167 * Revision 1.2 1992/07/21 00:05:45 pavel
168 * Added rcsid_<filename-root> declaration to hold the RCS ident. string.
169 *
170 * Revision 1.1 1992/07/20 23:23:12 pavel
171 * Initial RCS-controlled version.
172 */
173