1 /*
2 * Copyright (c) 2001-2016 Stephen Williams (steve@icarus.com)
3 *
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20 # include "codes.h"
21 # include "statistics.h"
22 # include "config.h"
23 #ifdef CHECK_WITH_VALGRIND
24 # include "vvp_cleanup.h"
25 #endif
26 # include <cstring>
27 # include <cassert>
28
29 /*
30 * The code space is broken into chunks, to make for efficient
31 * allocation of large amounts. Each chunk is an array of vvp_code_s
32 * structures, with the last opcode loaded with an of_CHUNK_LINK
33 * instruction to branch to the next chunk. This handles the case
34 * where the program counter steps off the end of a chunk.
35 */
36 const unsigned code_chunk_size = 1024;
37
38 static struct vvp_code_s *first_chunk = 0;
39 static struct vvp_code_s *current_chunk = 0;
40 static unsigned current_within_chunk = 0;
41
42 /*
43 * This initializes the code space. It sets up the first code chunk,
44 * and places at address 0 a ZOMBIE instruction.
45 */
codespace_init(void)46 void codespace_init(void)
47 {
48 assert(current_chunk == 0);
49 first_chunk = new struct vvp_code_s [code_chunk_size];
50 current_chunk = first_chunk;
51
52 current_chunk[0].opcode = &of_ZOMBIE;
53
54 current_chunk[code_chunk_size-1].opcode = &of_CHUNK_LINK;
55 current_chunk[code_chunk_size-1].cptr = 0;
56
57 current_within_chunk = 1;
58
59 count_opcodes = 0;
60 size_opcodes += code_chunk_size * sizeof (struct vvp_code_s);
61 }
62
codespace_next(void)63 vvp_code_t codespace_next(void)
64 {
65 if (current_within_chunk == (code_chunk_size-1)) {
66 current_chunk[code_chunk_size-1].cptr
67 = new struct vvp_code_s [code_chunk_size];
68 current_chunk = current_chunk[code_chunk_size-1].cptr;
69
70 /* Put a link opcode on the end of the chunk. */
71 current_chunk[code_chunk_size-1].opcode = &of_CHUNK_LINK;
72 current_chunk[code_chunk_size-1].cptr = 0;
73
74 current_within_chunk = 0;
75
76 size_opcodes += code_chunk_size * sizeof (struct vvp_code_s);
77 }
78
79 vvp_code_t res = current_chunk + current_within_chunk;
80 return res;
81 }
82
codespace_allocate(void)83 vvp_code_t codespace_allocate(void)
84 {
85 vvp_code_t res = codespace_next();
86 current_within_chunk += 1;
87 count_opcodes += 1;
88
89 memset(res, 0, sizeof(*res));
90
91 return res;
92 }
93
codespace_null(void)94 vvp_code_t codespace_null(void)
95 {
96 return first_chunk + 0;
97 }
98
99 #ifdef CHECK_WITH_VALGRIND
codespace_delete(void)100 void codespace_delete(void)
101 {
102 vvp_code_t cur = first_chunk;
103
104 /* If there are no opcodes then just delete the code space. */
105 if (count_opcodes == 0) {
106 delete [] cur;
107 return;
108 }
109
110 do {
111 vvp_code_t next = cur[code_chunk_size-1].cptr;
112 for (unsigned idx = 0 ; idx < code_chunk_size; idx += 1) {
113 count_opcodes -= 1;
114 if ((cur+idx)->opcode == &of_VPI_CALL) {
115 vpi_call_delete((cur+idx)->handle);
116 } else if (((cur+idx)->opcode == &of_EXEC_UFUNC_REAL) ||
117 ((cur+idx)->opcode == &of_EXEC_UFUNC_VEC4)) {
118 exec_ufunc_delete((cur+idx));
119 } else if ((cur+idx)->opcode == &of_FILE_LINE) {
120 delete((cur+idx)->handle);
121 } else if (((cur+idx)->opcode == &of_CONCATI_STR) ||
122 ((cur+idx)->opcode == &of_NEW_DARRAY) ||
123 ((cur+idx)->opcode == &of_PUSHI_STR)) {
124 delete [] ((cur+idx)->text);
125 }
126 if (count_opcodes == 0) break;
127 }
128 /* Don't count the &of_CHUNK_LINK opcode. */
129 if (count_opcodes != 0) count_opcodes += 1;
130 delete [] cur;
131 cur = next;
132 } while (cur != 0);
133 }
134 #endif
135