2001-03-11 01:29:38 +01:00
|
|
|
/*
|
2012-12-18 20:08:30 +01:00
|
|
|
* Copyright (c) 2001-2012 Stephen Williams (steve@icarus.com)
|
2001-03-11 01:29:38 +01:00
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form under the terms of the GNU
|
|
|
|
|
* General Public License as published by the Free Software
|
|
|
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
|
|
|
* any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2001-03-11 01:29:38 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
# include "codes.h"
|
2002-07-05 04:50:57 +02:00
|
|
|
# include "statistics.h"
|
2009-01-30 02:23:09 +01:00
|
|
|
# include "config.h"
|
|
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
|
|
|
# include "vvp_cleanup.h"
|
|
|
|
|
#endif
|
2010-05-31 22:12:06 +02:00
|
|
|
# include <cstring>
|
|
|
|
|
# include <cassert>
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
/*
|
|
|
|
|
* The code space is broken into chunks, to make for efficient
|
|
|
|
|
* allocation of large amounts. Each chunk is an array of vvp_code_s
|
|
|
|
|
* structures, with the last opcode loaded with an of_CHUNK_LINK
|
|
|
|
|
* instruction to branch to the next chunk. This handles the case
|
|
|
|
|
* where the program counter steps off the end of a chunk.
|
|
|
|
|
*/
|
|
|
|
|
const unsigned code_chunk_size = 1024;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
static struct vvp_code_s *first_chunk = 0;
|
|
|
|
|
static struct vvp_code_s *current_chunk = 0;
|
|
|
|
|
static unsigned current_within_chunk = 0;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2001-04-13 05:55:18 +02:00
|
|
|
/*
|
2003-07-03 22:03:36 +02:00
|
|
|
* This initializes the code space. It sets up the first code chunk,
|
|
|
|
|
* and places at address 0 a ZOMBIE instruction.
|
2001-04-13 05:55:18 +02:00
|
|
|
*/
|
2001-03-11 01:29:38 +01:00
|
|
|
void codespace_init(void)
|
|
|
|
|
{
|
2003-07-03 22:03:36 +02:00
|
|
|
assert(current_chunk == 0);
|
|
|
|
|
first_chunk = new struct vvp_code_s [code_chunk_size];
|
|
|
|
|
current_chunk = first_chunk;
|
2001-04-13 05:55:18 +02:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
current_chunk[0].opcode = &of_ZOMBIE;
|
2001-04-13 05:55:18 +02:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
current_chunk[code_chunk_size-1].opcode = &of_CHUNK_LINK;
|
|
|
|
|
current_chunk[code_chunk_size-1].cptr = 0;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
current_within_chunk = 1;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
count_opcodes = 0;
|
|
|
|
|
size_opcodes += code_chunk_size * sizeof (struct vvp_code_s);
|
|
|
|
|
}
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t codespace_next(void)
|
|
|
|
|
{
|
|
|
|
|
if (current_within_chunk == (code_chunk_size-1)) {
|
|
|
|
|
current_chunk[code_chunk_size-1].cptr
|
|
|
|
|
= new struct vvp_code_s [code_chunk_size];
|
|
|
|
|
current_chunk = current_chunk[code_chunk_size-1].cptr;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
/* Put a link opcode on the end of the chunk. */
|
|
|
|
|
current_chunk[code_chunk_size-1].opcode = &of_CHUNK_LINK;
|
|
|
|
|
current_chunk[code_chunk_size-1].cptr = 0;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
current_within_chunk = 0;
|
2001-03-11 01:29:38 +01:00
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
size_opcodes += code_chunk_size * sizeof (struct vvp_code_s);
|
2001-03-11 01:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t res = current_chunk + current_within_chunk;
|
2001-03-11 01:29:38 +01:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t codespace_allocate(void)
|
2001-04-01 08:40:44 +02:00
|
|
|
{
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t res = codespace_next();
|
|
|
|
|
current_within_chunk += 1;
|
|
|
|
|
count_opcodes += 1;
|
2001-04-01 08:40:44 +02:00
|
|
|
|
2003-08-01 02:58:03 +02:00
|
|
|
memset(res, 0, sizeof(*res));
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
return res;
|
2002-07-05 04:50:57 +02:00
|
|
|
}
|
|
|
|
|
|
2003-07-03 22:03:36 +02:00
|
|
|
vvp_code_t codespace_null(void)
|
2001-03-11 01:29:38 +01:00
|
|
|
{
|
2003-07-03 22:03:36 +02:00
|
|
|
return first_chunk + 0;
|
2001-03-11 01:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
2009-01-30 02:23:09 +01:00
|
|
|
#ifdef CHECK_WITH_VALGRIND
|
|
|
|
|
void codespace_delete(void)
|
|
|
|
|
{
|
|
|
|
|
vvp_code_t cur = first_chunk;
|
|
|
|
|
|
2011-10-18 18:05:54 +02:00
|
|
|
/* If there are no opcodes then just delete the code space. */
|
|
|
|
|
if (count_opcodes == 0) {
|
|
|
|
|
delete [] cur;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-30 02:23:09 +01:00
|
|
|
do {
|
|
|
|
|
vvp_code_t next = cur[code_chunk_size-1].cptr;
|
|
|
|
|
for (unsigned idx = 0 ; idx < code_chunk_size; idx += 1) {
|
|
|
|
|
count_opcodes -= 1;
|
|
|
|
|
if ((cur+idx)->opcode == &of_VPI_CALL) {
|
|
|
|
|
vpi_call_delete((cur+idx)->handle);
|
2009-10-14 03:03:39 +02:00
|
|
|
} else if ((cur+idx)->opcode == &of_EXEC_UFUNC) {
|
|
|
|
|
exec_ufunc_delete((cur+idx));
|
2011-02-28 04:20:16 +01:00
|
|
|
} else if ((cur+idx)->opcode == &of_FILE_LINE) {
|
|
|
|
|
delete((cur+idx)->handle);
|
2012-12-18 20:08:30 +01:00
|
|
|
} else if (((cur+idx)->opcode == &of_CONCATI_STR) ||
|
|
|
|
|
((cur+idx)->opcode == &of_NEW_DARRAY) ||
|
|
|
|
|
((cur+idx)->opcode == &of_PUSHI_STR)) {
|
|
|
|
|
delete [] ((cur+idx)->text);
|
2009-01-30 02:23:09 +01:00
|
|
|
}
|
|
|
|
|
if (count_opcodes == 0) break;
|
|
|
|
|
}
|
|
|
|
|
/* Don't count the &of_CHUNK_LINK opcode. */
|
|
|
|
|
if (count_opcodes != 0) count_opcodes += 1;
|
|
|
|
|
delete [] cur;
|
|
|
|
|
cur = next;
|
|
|
|
|
} while (cur != 0);
|
|
|
|
|
}
|
|
|
|
|
#endif
|