| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2007 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* JIT operations. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <string.h> |
| 12 |
#include <sys/types.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
#include <signal.h> |
| 15 |
#include <fcntl.h> |
| 16 |
#include <assert.h> |
| 17 |
|
| 18 |
#include "cpu.h" |
| 19 |
#include "jit_op.h" |
| 20 |
|
| 21 |
u_int jit_op_blk_sizes[JIT_OP_POOL_NR] = { |
| 22 |
0, 32, 64, 128, 256, 384, 512, 1024, |
| 23 |
}; |
| 24 |
|
| 25 |
/* Get a JIT op (allocate one if necessary) */ |
| 26 |
jit_op_t *jit_op_get(cpu_gen_t *cpu,int size_index,u_int opcode) |
| 27 |
{ |
| 28 |
jit_op_t *op; |
| 29 |
size_t len; |
| 30 |
|
| 31 |
assert(size_index < JIT_OP_POOL_NR); |
| 32 |
op = cpu->jit_op_pool[size_index]; |
| 33 |
|
| 34 |
if (op != NULL) { |
| 35 |
assert(op->ob_size_index == size_index); |
| 36 |
cpu->jit_op_pool[size_index] = op->next; |
| 37 |
} else { |
| 38 |
/* no block found, allocate one */ |
| 39 |
len = sizeof(*op) + jit_op_blk_sizes[size_index]; |
| 40 |
|
| 41 |
op = malloc(len); |
| 42 |
assert(op != NULL); |
| 43 |
op->ob_size_index = size_index; |
| 44 |
} |
| 45 |
|
| 46 |
op->opcode = opcode; |
| 47 |
op->param[0] = op->param[1] = op->param[2] = -1; |
| 48 |
op->next = NULL; |
| 49 |
op->ob_ptr = op->ob_data; |
| 50 |
op->arg_ptr = NULL; |
| 51 |
op->insn_name = NULL; |
| 52 |
return op; |
| 53 |
} |
| 54 |
|
| 55 |
/* Release a JIT op */ |
| 56 |
void jit_op_free(cpu_gen_t *cpu,jit_op_t *op) |
| 57 |
{ |
| 58 |
assert(op->ob_size_index < JIT_OP_POOL_NR); |
| 59 |
op->next = cpu->jit_op_pool[op->ob_size_index]; |
| 60 |
cpu->jit_op_pool[op->ob_size_index] = op; |
| 61 |
} |
| 62 |
|
| 63 |
/* Free a list of JIT ops */ |
| 64 |
void jit_op_free_list(cpu_gen_t *cpu,jit_op_t *op_list) |
| 65 |
{ |
| 66 |
jit_op_t *op,*opn; |
| 67 |
|
| 68 |
for(op=op_list;op;op=opn) { |
| 69 |
opn = op->next; |
| 70 |
jit_op_free(cpu,op); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
/* Initialize JIT op pools for the specified CPU */ |
| 75 |
int jit_op_init_cpu(cpu_gen_t *cpu) |
| 76 |
{ |
| 77 |
cpu->jit_op_array = calloc(cpu->jit_op_array_size,sizeof(jit_op_t *)); |
| 78 |
|
| 79 |
if (!cpu->jit_op_array) |
| 80 |
return(-1); |
| 81 |
|
| 82 |
memset(cpu->jit_op_pool,0,sizeof(cpu->jit_op_pool)); |
| 83 |
return(0); |
| 84 |
} |
| 85 |
|
| 86 |
/* Free memory used by pools */ |
| 87 |
void jit_op_free_pools(cpu_gen_t *cpu) |
| 88 |
{ |
| 89 |
jit_op_t *op,*opn; |
| 90 |
int i; |
| 91 |
|
| 92 |
for(i=0;i<JIT_OP_POOL_NR;i++) { |
| 93 |
for(op=cpu->jit_op_pool[i];op;op=opn) { |
| 94 |
opn = op->next; |
| 95 |
free(op); |
| 96 |
} |
| 97 |
|
| 98 |
cpu->jit_op_pool[i] = NULL; |
| 99 |
} |
| 100 |
} |