Esempio n. 1
0
int jit_function_apply_vararg
	(jit_function_t func, jit_type_t signature, void **args, void *return_area)
{
	struct jit_backtrace call_trace;
	void *entry;
	jit_jmp_buf jbuf;

	/* Establish a "setjmp" point here so that we can unwind the
	   stack to this point when an exception occurs and then prevent
	   the exception from propagating further up the stack */
	_jit_unwind_push_setjmp(&jbuf);
	if(setjmp(jbuf.buf))
	{
		_jit_unwind_pop_setjmp();
		return 0;
	}

	/* Create a backtrace entry that blocks exceptions from
	   flowing further than this up the stack */
	_jit_backtrace_push(&call_trace, 0);

	/* Get the function's entry point */
	if(!func)
	{
		jit_exception_builtin(JIT_RESULT_NULL_FUNCTION);
		return 0;
	}
	if(func->nested_parent)
	{
		jit_exception_builtin(JIT_RESULT_CALLED_NESTED);
		return 0;
	}
	if(func->is_compiled)
	{
		entry = func->entry_point;
	}
	else
	{
		entry = (*func->context->on_demand_driver)(func);
	}

	/* Get the default signature if necessary */
	if(!signature)
	{
		signature = func->signature;
	}

	/* Clear the exception state */
	jit_exception_clear_last();

	/* Apply the function.  If it returns, then there is no exception */
	jit_apply(signature, func->entry_point, args,
			  jit_type_num_params(func->signature), return_area);

	/* Restore the backtrace and "setjmp" contexts and exit */
	_jit_unwind_pop_setjmp();
	return 1;
}
/*
 * Create the slots for the fixed parameters of the current function.
 * Returns 0 on error else 1.
 */
static int _ILJitParamsCreate(ILJITCoder *coder)
{
	ILJitType signature = jit_function_get_signature(coder->jitFunction);

	if(signature)
	{
		ILJitLocalSlot *param = 0;
		ILUInt32 numParams = jit_type_num_params(signature);

#ifdef IL_JIT_THREAD_IN_SIGNATURE
		/* We don't include the ILExecThread in the params. */
		if(numParams > 1)
		{
			ILInt32 current = 0;

			/* Allocate the locals for the parameters */
			_ILJitLocalsAlloc(coder->jitParams, numParams - 1);

			for(current = 1; current < numParams; ++current)
			{
				param = _ILJitParamGet(coder, current - 1);

				param->value = jit_value_get_param(coder->jitFunction, current);
				param->flags = 0;
				param->refValue = 0;
			}
			coder->jitParams.numSlots = numParams - 1;
		}
#else
		if(numParams > 0)
		{
			ILInt32 current = 0;

			/* Allocate the locals for the parameters */
			_ILJitLocalsAlloc(coder->jitParams, numParams);

			for(current = 0; current < numParams; ++current)
			{
				param = _ILJitParamGet(coder, current);

				param->value = jit_value_get_param(coder->jitFunction, current);
				param->flags = 0;
				param->refValue = 0;
			}
			coder->jitParams.numSlots = numParams;
		}
#endif
		else
		{
			coder->jitParams.numSlots = 0;
		}
		return 1;
	}
	return 0;
}
Esempio n. 3
0
/*@
 * @deftypefun jit_value_t jit_value_get_param (jit_function_t @var{func}, unsigned int @var{param})
 * Get the value that corresponds to a specified function parameter.
 * Returns NULL if out of memory or @var{param} is invalid.
 * @end deftypefun
@*/
jit_value_t
jit_value_get_param(jit_function_t func, unsigned int param)
{
	unsigned int num_params, current;

	/* Ensure that we have a builder for this function */
	if(!_jit_function_ensure_builder(func))
	{
		return 0;
	}

	/* Ensure valid param number. */
	jit_type_t signature = func->signature;
	num_params = jit_type_num_params(signature);
	if(param >= num_params)
	{
		return 0;
	}

	/* If we have already created the values, then exit immediately */
	jit_value_t *values = func->builder->param_values;
	if(values)
	{
		return values[param];
	}

	/* Create the values for the first time */
	values = (jit_value_t *) jit_calloc(num_params, sizeof(jit_value_t));
	if(!values)
	{
		return 0;
	}
	func->builder->param_values = values;
	for(current = 0; current < num_params; ++current)
	{
		jit_type_t type = jit_type_get_param(signature, current);
		values[current] = jit_value_create(func, type);
		if(values[current])
		{
			/* The value belongs to the entry block, no matter
			   where it happens to be created */
			values[current]->block = func->builder->entry_block;
			values[current]->is_parameter = 1;
		}
	}

	/* Return the value block for the desired parameter */
	return values[param];
}
Esempio n. 4
0
/* TODO: seems to be unused */
void _jit_value_ref_params(jit_function_t func)
{
	unsigned int num_params;
	unsigned int param;
	if(func->builder->param_values)
	{
		num_params = jit_type_num_params(func->signature);
		for(param = 0; param < num_params; ++param)
		{
			jit_value_ref(func, func->builder->param_values[param]);
		}
	}
	jit_value_ref(func, func->builder->struct_return);
	jit_value_ref(func, func->builder->parent_frame);
}
Esempio n. 5
0
/*
 * Calculate the size of the argument area for an interpreted function.
 */
unsigned int _jit_interp_calculate_arg_size
		(jit_function_t func, jit_type_t signature)
{
	unsigned int size = 0;
	jit_type_t type;
	unsigned int num_params;
	unsigned int param;

	/* Determine if we need nested parameter information */
	if(func->nested_parent)
	{
		size += 2 * sizeof(jit_item);
	}

	/* Determine if we need a structure pointer argument */
	type = jit_type_get_return(signature);
	if(jit_type_return_via_pointer(type))
	{
		size += sizeof(jit_item);
	}

	/* Calculate the total size of the regular arguments */
	num_params = jit_type_num_params(signature);
	for(param = 0; param < num_params; ++param)
	{
		type = jit_type_get_param(signature, param);
		type = jit_type_remove_tags(type);
		if(type->kind == JIT_TYPE_STRUCT || type->kind == JIT_TYPE_UNION)
		{
			size += JIT_NUM_ITEMS_IN_STRUCT(jit_type_get_size(type)) *
					sizeof(jit_item);
		}
		else
		{
			size += sizeof(jit_item);
		}
	}

	/* Return the final size to the caller */
	return size;
}
Esempio n. 6
0
/*@
 * @deftypefun int _jit_create_entry_insns (jit_function_t @var{func})
 * Create instructions in the entry block to initialize the
 * registers and frame offsets that contain the parameters.
 * Returns zero if out of memory.
 *
 * This function is called when a builder is initialized.  It should
 * scan the signature and decide which register or frame position
 * contains each of the parameters and then call either
 * @code{jit_insn_incoming_reg} or @code{jit_insn_incoming_frame_posn}
 * to notify @code{libjit} of the location.
 * @end deftypefun
@*/
int _jit_create_entry_insns(jit_function_t func)
{
	jit_type_t signature = func->signature;
	jit_type_t type;
	jit_nint offset;
	jit_value_t value;
	unsigned int num_params;
	unsigned int param;

	/* Reset the frame size for this function */
	func->builder->frame_size = 0;

	/* The starting parameter offset.  We use negative offsets to indicate
	   an offset into the "args" block, and positive offsets to indicate
	   an offset into the "frame" block.  The negative values will be
	   flipped when we output the argument opcodes for interpretation */
	offset = -1;

	/* If the function is nested, then we need two extra parameters
	   to pass the pointer to the parent's local variables and arguments */
	if(func->nested_parent)
	{
		offset -= 2;
	}

	/* Allocate the structure return pointer */
	value = jit_value_get_struct_pointer(func);
	if(value)
	{
		if(!jit_insn_incoming_frame_posn(func, value, offset))
		{
			return 0;
		}
		--offset;
	}

	/* Allocate the parameter offsets */
	num_params = jit_type_num_params(signature);
	for(param = 0; param < num_params; ++param)
	{
		value = jit_value_get_param(func, param);
		if(!value)
		{
			continue;
		}

		type = jit_type_remove_tags(jit_value_get_type(value));
		switch(type->kind)
		{
		case JIT_TYPE_SBYTE:
		case JIT_TYPE_UBYTE:
			if(!jit_insn_incoming_frame_posn(func, value,
							 offset - _jit_int_lowest_byte()))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_SHORT:
		case JIT_TYPE_USHORT:
			if(!jit_insn_incoming_frame_posn(func, value,
							 offset - _jit_int_lowest_short()))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_INT:
		case JIT_TYPE_UINT:
		case JIT_TYPE_NINT:
		case JIT_TYPE_NUINT:
		case JIT_TYPE_SIGNATURE:
		case JIT_TYPE_PTR:
		case JIT_TYPE_LONG:
		case JIT_TYPE_ULONG:
		case JIT_TYPE_FLOAT32:
		case JIT_TYPE_FLOAT64:
		case JIT_TYPE_NFLOAT:
			if(!jit_insn_incoming_frame_posn(func, value, offset))
			{
				return 0;
			}
			--offset;
			break;

		case JIT_TYPE_STRUCT:
		case JIT_TYPE_UNION:
			if(!jit_insn_incoming_frame_posn(func, value, offset))
			{
				return 0;
			}
			offset -= JIT_NUM_ITEMS_IN_STRUCT(jit_type_get_size(type));
			break;
		}
	}
	return 1;
}
Esempio n. 7
0
/*@
 * @deftypefun void _jit_gen_insn (jit_gencode_t @var{gen}, jit_function_t @var{func}, jit_block_t @var{block}, jit_insn_t @var{insn})
 * Generate native code for the specified @var{insn}.  This function should
 * call the appropriate register allocation routines, output the instruction,
 * and then arrange for the result to be placed in an appropriate register
 * or memory destination.
 * @end deftypefun
@*/
void _jit_gen_insn(jit_gencode_t gen, jit_function_t func,
				   jit_block_t block, jit_insn_t insn)
{
	jit_label_t label;
	void **pc;
	jit_nint offset;
	jit_nint size;

	switch(insn->opcode)
	{
	case JIT_OP_BR_IEQ:
	case JIT_OP_BR_INE:
	case JIT_OP_BR_ILT:
	case JIT_OP_BR_ILT_UN:
	case JIT_OP_BR_ILE:
	case JIT_OP_BR_ILE_UN:
	case JIT_OP_BR_IGT:
	case JIT_OP_BR_IGT_UN:
	case JIT_OP_BR_IGE:
	case JIT_OP_BR_IGE_UN:
	case JIT_OP_BR_LEQ:
	case JIT_OP_BR_LNE:
	case JIT_OP_BR_LLT:
	case JIT_OP_BR_LLT_UN:
	case JIT_OP_BR_LLE:
	case JIT_OP_BR_LLE_UN:
	case JIT_OP_BR_LGT:
	case JIT_OP_BR_LGT_UN:
	case JIT_OP_BR_LGE:
	case JIT_OP_BR_LGE_UN:
	case JIT_OP_BR_FEQ:
	case JIT_OP_BR_FNE:
	case JIT_OP_BR_FLT:
	case JIT_OP_BR_FLE:
	case JIT_OP_BR_FGT:
	case JIT_OP_BR_FGE:
	case JIT_OP_BR_FLT_INV:
	case JIT_OP_BR_FLE_INV:
	case JIT_OP_BR_FGT_INV:
	case JIT_OP_BR_FGE_INV:
	case JIT_OP_BR_DEQ:
	case JIT_OP_BR_DNE:
	case JIT_OP_BR_DLT:
	case JIT_OP_BR_DLE:
	case JIT_OP_BR_DGT:
	case JIT_OP_BR_DGE:
	case JIT_OP_BR_DLT_INV:
	case JIT_OP_BR_DLE_INV:
	case JIT_OP_BR_DGT_INV:
	case JIT_OP_BR_DGE_INV:
	case JIT_OP_BR_NFEQ:
	case JIT_OP_BR_NFNE:
	case JIT_OP_BR_NFLT:
	case JIT_OP_BR_NFLE:
	case JIT_OP_BR_NFGT:
	case JIT_OP_BR_NFGE:
	case JIT_OP_BR_NFLT_INV:
	case JIT_OP_BR_NFLE_INV:
	case JIT_OP_BR_NFGT_INV:
	case JIT_OP_BR_NFGE_INV:
		/* Binary branch */
		load_value(gen, insn->value2, 2);
		/* Fall through */

	case JIT_OP_BR_IFALSE:
	case JIT_OP_BR_ITRUE:
	case JIT_OP_BR_LFALSE:
	case JIT_OP_BR_LTRUE:
		/* Unary branch */
		load_value(gen, insn->value1, 1);
		/* Fall through */

	case JIT_OP_BR:
	case JIT_OP_CALL_FINALLY:
		/* Unconditional branch */
	branch:
		label = (jit_label_t)(insn->dest);
		pc = (void **)(gen->ptr);
		jit_cache_opcode(gen, insn->opcode);
		block = jit_block_from_label(func, label);
		if(!block)
		{
			break;
		}
		if(block->address)
		{
			/* We already know the address of the block */
			jit_cache_native(gen, ((void **)(block->address)) - pc);
		}
		else
		{
			/* Record this position on the block's fixup list */
			jit_cache_native(gen, block->fixup_list);
			block->fixup_list = (void *)pc;
		}
		break;

	case JIT_OP_CALL_FILTER:
		/* Branch to a filter subroutine, load the filter
		   parameter to the r0 register */
		load_value(gen, insn->value1, 0);
		goto branch;

	case JIT_OP_JUMP_TABLE:
	{
		jit_label_t *labels;
		jit_nint num_labels;
		jit_nint index;

		load_value(gen, insn->dest, 0);

		labels = (jit_label_t *) insn->value1->address;
		num_labels = insn->value2->address;

		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, num_labels);
		for(index = 0; index < num_labels; index++)
		{
			block = jit_block_from_label(func, labels[index]);
			if(!block)
			{
				return;
			}
			if(block->address)
			{
				/* We already know the address of the block */
				jit_cache_native(gen, block->address);
			}
			else
			{
				/* Record this position on the block's fixup list */
				pc = (void **)(gen->ptr);
				jit_cache_native(gen, block->fixup_absolute_list);
				block->fixup_absolute_list = pc;
			}
		}
		break;
	}

	case JIT_OP_ADDRESS_OF_LABEL:
		/* Get the address of a particular label */
		label = (jit_label_t)(insn->value1);
		block = jit_block_from_label(func, label);
		if(!block)
		{
			break;
		}
		pc = (void **)(gen->ptr);
		jit_cache_opcode(gen, insn->opcode);
		if(block->address)
		{
			/* We already know the address of the block */
			jit_cache_native(gen, ((void **)(block->address)) - pc);
		}
		else
		{
			/* Record this position on the block's fixup list */
			jit_cache_native(gen, block->fixup_list);
			block->fixup_list = (void *)pc;
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_CALL:
	case JIT_OP_CALL_TAIL:
		/* Call a function, whose pointer is supplied explicitly */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->dest));
		break;

	case JIT_OP_CALL_INDIRECT:
	case JIT_OP_CALL_INDIRECT_TAIL:
		/* Call a function, whose pointer is supplied in the register */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->value2));
		jit_cache_native(gen, (jit_nint)
				 (jit_type_num_params((jit_type_t)(insn->value2))));
		break;

	case JIT_OP_CALL_VTABLE_PTR:
	case JIT_OP_CALL_VTABLE_PTR_TAIL:
		/* Call a function, whose vtable pointer is supplied in the register */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_CALL_EXTERNAL:
	case JIT_OP_CALL_EXTERNAL_TAIL:
		/* Call a native function, whose pointer is supplied explicitly */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, (jit_nint)(insn->value2));
		jit_cache_native(gen, (jit_nint)(insn->dest));
		jit_cache_native(gen, (jit_nint)
				 (jit_type_num_params((jit_type_t)(insn->value2))));
		break;

	case JIT_OP_RETURN:
		/* Return from the current function with no result */
		jit_cache_opcode(gen, JIT_OP_RETURN);
		break;

	case JIT_OP_RETURN_INT:
	case JIT_OP_RETURN_LONG:
	case JIT_OP_RETURN_FLOAT32:
	case JIT_OP_RETURN_FLOAT64:
	case JIT_OP_RETURN_NFLOAT:
		/* Return from the current function with a specific result */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_RETURN_SMALL_STRUCT:
		/* Return from current function with a small structure result */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		break;

	case JIT_OP_SETUP_FOR_NESTED:
		/* TODO!!! */
		/* Set up to call a nested child */
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 2);
		break;

	case JIT_OP_SETUP_FOR_SIBLING:
		/* TODO!!! */
		/* Set up to call a nested sibling */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, jit_value_get_nint_constant(insn->value1));
		adjust_working(gen, 2);
		break;

	case JIT_OP_IMPORT:
		/* Import a local variable from an outer nested scope */
		_jit_gen_fix_value(insn->value1);
		if(insn->value1->frame_offset >= 0)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_IMPORT_LOCAL);
			jit_cache_native(gen, insn->value1->frame_offset);
			jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		}
		else
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_IMPORT_ARG);
			jit_cache_native(gen, -(insn->value1->frame_offset + 1));
			jit_cache_native(gen, jit_value_get_nint_constant(insn->value2));
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_THROW:
		/* Throw an exception */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_LOAD_PC:
	case JIT_OP_LOAD_EXCEPTION_PC:
		/* Load the current program counter onto the stack */
		jit_cache_opcode(gen, insn->opcode);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_CALL_FILTER_RETURN:
		/* The r0 register currently contains "dest" */
		store_value(gen, insn->dest);
		break;

	case JIT_OP_ENTER_FINALLY:
		/* Record that the finally return address is on the stack */
		++(gen->extra_working_space);
		break;

	case JIT_OP_LEAVE_FINALLY:
		/* Leave a finally clause */
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_ENTER_FILTER:
		/* The top of the stack contains the return address,
		   the r0 register contains the "dest" (filter parameter). */
		++(gen->extra_working_space);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_LEAVE_FILTER:
		/* Leave a filter clause, returning a particular value */
		load_value(gen, insn->value1, 0);
		jit_cache_opcode(gen, insn->opcode);
		break;

	case JIT_OP_INCOMING_REG:
		/* Store incoming value (in interpreter this is used to
		   pass an exception object to the catcher) */
		store_value(gen, insn->value1);
		break;

	case JIT_OP_RETURN_REG:
		/* Push a function return value back onto the stack */
		switch(jit_type_normalize(insn->value1->type)->kind)
		{
		case JIT_TYPE_SBYTE:
		case JIT_TYPE_UBYTE:
		case JIT_TYPE_SHORT:
		case JIT_TYPE_USHORT:
		case JIT_TYPE_INT:
		case JIT_TYPE_UINT:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_INT);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_LONG:
		case JIT_TYPE_ULONG:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_LONG);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_FLOAT32:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_FLOAT32);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_FLOAT64:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_FLOAT64);
			store_value(gen, insn->value1);
			break;

		case JIT_TYPE_NFLOAT:
			jit_cache_opcode(gen, JIT_INTERP_OP_LDR_0_NFLOAT);
			store_value(gen, insn->value1);
			break;
		}
		break;

	case JIT_OP_COPY_LOAD_SBYTE:
	case JIT_OP_COPY_LOAD_UBYTE:
	case JIT_OP_COPY_LOAD_SHORT:
	case JIT_OP_COPY_LOAD_USHORT:
	case JIT_OP_COPY_INT:
	case JIT_OP_COPY_LONG:
	case JIT_OP_COPY_FLOAT32:
	case JIT_OP_COPY_FLOAT64:
	case JIT_OP_COPY_NFLOAT:
	case JIT_OP_COPY_STORE_BYTE:
	case JIT_OP_COPY_STORE_SHORT:
		/* Copy a value from one temporary variable to another */
		load_value(gen, insn->value1, 0);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_COPY_STRUCT:
		/* Copy a struct from one address to another */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->dest));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_ADDRESS_OF:
		/* Get the address of a local variable */
		_jit_gen_fix_value(insn->value1);
		if(insn->value1->frame_offset >= 0)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_LDLA_0);
			jit_cache_native(gen, insn->value1->frame_offset);
		}
		else
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_LDAA_0);
			jit_cache_native(gen, -(insn->value1->frame_offset + 1));
		}
		store_value(gen, insn->dest);
		break;

	case JIT_OP_PUSH_INT:
	case JIT_OP_PUSH_LONG:
	case JIT_OP_PUSH_FLOAT32:
	case JIT_OP_PUSH_FLOAT64:
	case JIT_OP_PUSH_NFLOAT:
		/* Push an item onto the stack, ready for a function call */
		load_value(gen, insn->value1, 1);
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 1);
		break;

	case JIT_OP_PUSH_STRUCT:
		/* Load the pointer value */
		load_value(gen, insn->value1, 1);
		/* Push the structure at the designated pointer */
		size = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		adjust_working(gen, JIT_NUM_ITEMS_IN_STRUCT(size));
		break;

	case JIT_OP_PUSH_RETURN_AREA_PTR:
		/* Push the address of the interpreter's return area */
		jit_cache_opcode(gen, insn->opcode);
		adjust_working(gen, 1);
		break;

	case JIT_OP_POP_STACK:
		/* Pop parameter values from the stack after a function returns */
		size = jit_value_get_nint_constant(insn->value1);
		if(size == 1)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP);
		}
		else if(size == 2)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP_2);
		}
		else if(size == 3)
		{
			jit_cache_opcode(gen, JIT_INTERP_OP_POP_3);
		}
		else if(size != 0)
		{
			jit_cache_opcode(gen, JIT_OP_POP_STACK);
			jit_cache_native(gen, size);
		}
		break;

	case JIT_OP_FLUSH_SMALL_STRUCT:
		/* Flush a small structure return value back into the frame */
		load_value(gen, insn->value1, 0);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->value1));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_LOAD_RELATIVE_SBYTE:
	case JIT_OP_LOAD_RELATIVE_UBYTE:
	case JIT_OP_LOAD_RELATIVE_SHORT:
	case JIT_OP_LOAD_RELATIVE_USHORT:
	case JIT_OP_LOAD_RELATIVE_INT:
	case JIT_OP_LOAD_RELATIVE_LONG:
	case JIT_OP_LOAD_RELATIVE_FLOAT32:
	case JIT_OP_LOAD_RELATIVE_FLOAT64:
	case JIT_OP_LOAD_RELATIVE_NFLOAT:
		/* Load a value from a relative pointer */
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		store_value(gen, insn->dest);
		break;

	case JIT_OP_LOAD_RELATIVE_STRUCT:
		/* Load a structured value from a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->dest));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_STORE_RELATIVE_BYTE:
	case JIT_OP_STORE_RELATIVE_SHORT:
	case JIT_OP_STORE_RELATIVE_INT:
	case JIT_OP_STORE_RELATIVE_LONG:
	case JIT_OP_STORE_RELATIVE_FLOAT32:
	case JIT_OP_STORE_RELATIVE_FLOAT64:
	case JIT_OP_STORE_RELATIVE_NFLOAT:
		/* Store a value to a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		break;

	case JIT_OP_STORE_RELATIVE_STRUCT:
		/* Store a structured value to a relative pointer */
		load_value(gen, insn->dest, 0);
		load_value(gen, insn->value1, 1);
		offset = jit_value_get_nint_constant(insn->value2);
		size = (jit_nint)jit_type_get_size(jit_value_get_type(insn->value1));
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, offset);
		jit_cache_native(gen, size);
		break;

	case JIT_OP_ADD_RELATIVE:
		/* Add a relative offset to a pointer */
		offset = jit_value_get_nint_constant(insn->value2);
		if(offset != 0)
		{
			load_value(gen, insn->value1, 1);
			jit_cache_opcode(gen, insn->opcode);
			jit_cache_native(gen, offset);
			store_value(gen, insn->dest);
		}
		else
		{
			load_value(gen, insn->value1, 0);
			store_value(gen, insn->dest);
		}
		break;

	case JIT_OP_MARK_BREAKPOINT:
		/* Mark the current location as a potential breakpoint */
		jit_cache_opcode(gen, insn->opcode);
		jit_cache_native(gen, insn->value1->address);
		jit_cache_native(gen, insn->value2->address);
		break;

	default:
		if(insn->dest && (insn->flags & JIT_INSN_DEST_IS_VALUE) != 0)
		{
			load_value(gen, insn->dest, 0);
		}
		if(insn->value1)
		{
			load_value(gen, insn->value1, 1);
		}
		if(insn->value2)
		{
			load_value(gen, insn->value2, 2);
		}
		jit_cache_opcode(gen, insn->opcode);
		if(insn->dest && (insn->flags & JIT_INSN_DEST_IS_VALUE) == 0)
		{
			store_value(gen, insn->dest);
		}
		break;
	}
}
Esempio n. 8
0
void _jit_gen_epilog(jit_gencode_t gen, jit_function_t func)
{
	jit_nint pop_bytes = 0;
	int reg, offset;
	unsigned char *inst;
	int struct_return_offset = 0;
	void **fixup;
	void **next;

	/* Check if there is sufficient space for the epilog */
	_jit_cache_check_space(&gen->posn, 48);

#if JIT_APPLY_X86_FASTCALL == 1
	/* Determine the number of parameter bytes to pop when we return */
	{
		jit_type_t signature;
		unsigned int num_params;
		unsigned int param;
		signature = func->signature;
		if(jit_type_get_abi(signature) == jit_abi_stdcall ||
            jit_type_get_abi(signature) == jit_abi_thiscall||
            jit_type_get_abi(signature) == jit_abi_fastcall)
		{
			if(func->nested_parent)
			{
				pop_bytes += sizeof(void *);
			}
			if(jit_type_return_via_pointer(jit_type_get_return(signature)))
			{
				struct_return_offset = 2 * sizeof(void *) + pop_bytes;
				pop_bytes += sizeof(void *);
			}
			num_params = jit_type_num_params(signature);
			for(param = 0; param < num_params; ++param)
			{
				pop_bytes += ROUND_STACK
						(jit_type_get_size
							(jit_type_get_param(signature, param)));
			}
			if(jit_type_get_abi(signature) == jit_abi_fastcall)
			{
				/* The first two words are in fastcall registers */
				if(pop_bytes > (2 * sizeof(void *)))
				{
					pop_bytes -= 2 * sizeof(void *);
				}
				else
				{
					pop_bytes = 0;
				}
				struct_return_offset = 0;
			}
            else if(jit_type_get_abi(signature) == jit_abi_thiscall)
            {
                /* The this is in ECX register */
                if(pop_bytes > (1 * sizeof(void *)))
                {
                    pop_bytes -= 1 * sizeof(void *);
                }
                else
                {
                    pop_bytes = 0;
                }
                struct_return_offset = 0;
            }
		}
		else if(!(func->nested_parent) &&
				jit_type_return_via_pointer(jit_type_get_return(signature)))
		{
#if JIT_APPLY_X86_POP_STRUCT_RETURN == 1
			pop_bytes += sizeof(void *);
#endif
			struct_return_offset = 2 * sizeof(void *);
		}
	}
#else
	{
		/* We only need to pop structure pointers in non-nested functions */
		jit_type_t signature;
		signature = func->signature;
		if(!(func->nested_parent) &&
		   jit_type_return_via_pointer(jit_type_get_return(signature)))
		{
#if JIT_APPLY_X86_POP_STRUCT_RETURN == 1
			pop_bytes += sizeof(void *);
#endif
			struct_return_offset = 2 * sizeof(void *);
		}
	}
#endif

	/* Perform fixups on any blocks that jump to the epilog */
	inst = gen->posn.ptr;
	fixup = (void **)(gen->epilog_fixup);
	while(fixup != 0)
	{
		next = (void **)(fixup[0]);
		fixup[0] = (void *)(((jit_nint)inst) - ((jit_nint)fixup) - 4);
		fixup = next;
	}
	gen->epilog_fixup = 0;

	/* If we are returning a structure via a pointer, then copy
	   the pointer value into EAX when we return */
	if(struct_return_offset != 0)
	{
		x86_mov_reg_membase(inst, X86_EAX, X86_EBP, struct_return_offset, 4);
	}

	/* Restore the callee save registers that we used */
	if(gen->stack_changed)
	{
		offset = -(func->builder->frame_size);
		for(reg = 0; reg <= 7; ++reg)
		{
			if(jit_reg_is_used(gen->touched, reg) &&
			   (_jit_reg_info[reg].flags & JIT_REG_CALL_USED) == 0)
			{
				offset -= sizeof(void *);
				x86_mov_reg_membase(inst, _jit_reg_info[reg].cpu_reg,
									X86_EBP, offset, sizeof(void *));
			}
		}
	}
	else
	{
		for(reg = 7; reg >= 0; --reg)
		{
			if(jit_reg_is_used(gen->touched, reg) &&
			   (_jit_reg_info[reg].flags & JIT_REG_CALL_USED) == 0)
			{
				x86_pop_reg(inst, _jit_reg_info[reg].cpu_reg);
			}
		}
	}

	/* Pop the stack frame and restore the saved copy of ebp */
	if(gen->stack_changed || func->builder->frame_size > 0)
	{
		x86_mov_reg_reg(inst, X86_ESP, X86_EBP, sizeof(void *));
	}
	x86_pop_reg(inst, X86_EBP);

	/* Return from the current function */
	if(pop_bytes > 0)
	{
		x86_ret_imm(inst, pop_bytes);
	}
	else
	{
		x86_ret(inst);
	}
	gen->posn.ptr = inst;
}