Exemplo n.º 1
0
/*------------------------------------------------------------------------
 * (function: connect_nodes)
 * 	Connect one output node to the inputs of the input node
 *----------------------------------------------------------------------*/
void connect_nodes(nnode_t *out_node, int out_idx, nnode_t *in_node, int in_idx)
{
	npin_t *new_in_pin;

	oassert(out_node->num_output_pins > out_idx);
	oassert(in_node->num_input_pins > in_idx);

	new_in_pin = allocate_npin();
	
	/* create the pin that hooks up to the input */
	add_input_pin_to_node(in_node, new_in_pin, in_idx);
	
	if (out_node->output_pins[out_idx] == NULL)
	{
		/* IF - this node has no output net or pin */
		npin_t *new_out_pin;
		nnet_t *new_net;
		new_net = allocate_nnet();
		new_out_pin = allocate_npin();
	
		/* create the pin that hooks up to the input */
		add_output_pin_to_node(out_node, new_out_pin, out_idx);
		/* hook up in pin out of the new net */
		add_fanout_pin_to_net(new_net, new_in_pin);
		/* hook up the new pin 2 to this new net */
		add_driver_pin_to_net(new_net, new_out_pin);
	}
	else
	{
		/* ELSE - there is a net so we just add a fanout */
		/* hook up in pin out of the new net */
		add_fanout_pin_to_net(out_node->output_pins[out_idx]->net, new_in_pin);
	}
}
Exemplo n.º 2
0
address Da_op_calc_adr_of_op (struct Da_op* op, const CONTEXT * ctx, struct MemoryCache *mem, unsigned ins_prefixes, address FS)
{
    address adr=0;
    address rt;

    oassert (op->type==DA_OP_TYPE_VALUE_IN_MEMORY);
    oassert (op->adr.adr_index_mult!=0);

    if (op->adr.adr_base != R_ABSENT)
        adr=adr+X86_register_get_value_as_u64 (op->adr.adr_base, ctx);

    if (op->adr.adr_index != R_ABSENT)
        adr=adr+X86_register_get_value_as_u64(op->adr.adr_index, ctx) * op->adr.adr_index_mult;

/*
    if (__WORDSIZE==64 && op->value_width_in_bits==32)
    {
        rt=adr+(int64_t)(int32_t)op->adr.adr_disp; // negative values of adr_disp must work! (to be checked)
        L ("%s() line=%d (int64_t)(int32_t)op->adr.adr_disp=0x" PRI_REG_HEX "\n", __func__, __LINE__, (int64_t)(int32_t)op->adr.adr_disp);
    }
    else
*/
        rt=adr+op->adr.adr_disp; // negative values of adr_disp must work! (to be checked)

    if (IS_SET(ins_prefixes, PREFIX_FS))
        rt+=FS;
    
    return rt;
};
Exemplo n.º 3
0
void strbuf_vaddf (strbuf *sb, const char *fmt, va_list va)
{
	va_list va2;
	va_copy (va2, va);

#if defined(TARGET_IS_WINDOWS_2000) || defined (__APPLE__)
	// temporary (?) kludge
	char tmpbuf[1024];
	size_t sz=vsnprintf (tmpbuf, sizeof(tmpbuf), fmt, va2);
	oassert (sz<sizeof(tmpbuf)-1);
// FIXME: this
#elif __linux__ || __CYGWIN__
	size_t sz=vsnprintf (NULL, 0, fmt, va2);
#else
	size_t sz=_vscprintf (fmt, va2); // MSVC-specific? absent in Windows 2000 msvcrt.dll :( f**k.
#endif

	strbuf_grow(sb, sz);

#ifdef _MSC_VER
	if (vsnprintf_s (sb->buf + sb->strlen, sz+1, sz, fmt, va)==-1) // MSVC-specific
#else
		if (vsnprintf (sb->buf + sb->strlen, sz+1, fmt, va)==-1)
#endif
		{
			oassert(0);
			fatal_error();
		};
	sb->strlen+=sz;
};
Exemplo n.º 4
0
/*---------------------------------------------------------------------------------------------
 * (function: instantiate_logical_logic )
 *-------------------------------------------------------------------------------------------*/
void instantiate_logical_logic(nnode_t *node, operation_list op, short mark, netlist_t *netlist)
{
	int i;
	int port_B_offset;
	int width_a;
	int width_b;
	nnode_t *new_logic_cell;
	nnode_t *reduction1;
	nnode_t *reduction2;

	oassert(node->num_input_pins > 0);
	oassert(node->num_input_port_sizes == 2);
	oassert(node->num_output_pins == 1);
	/* setup the calculations for padding and indexing */
	width_a = node->input_port_sizes[0];
	width_b = node->input_port_sizes[1];
	port_B_offset = width_a;

	/* instantiate the cells */
	new_logic_cell = make_1port_logic_gate(op, 2, node, mark);
	reduction1 = make_1port_logic_gate(BITWISE_OR, width_a, node, mark);
	reduction2 = make_1port_logic_gate(BITWISE_OR, width_b, node, mark);

	/* connect inputs.  In the case that a signal is smaller than the other then zero pad */
	for(i = 0; i < width_a; i++)
	{
		/* Joining the inputs to the input 1 of that gate */
		if (i < width_a)
		{
			remap_pin_to_new_node(node->input_pins[i], reduction1, i);
		}
		else
		{
			/* ELSE - the B input does not exist, so this answer goes right through */
			add_a_input_pin_to_node_spot_idx(reduction1, get_a_zero_pin(netlist), i);
		}
	}
	for(i = 0; i < width_b; i++)
	{
		/* Joining the inputs to the input 1 of that gate */
		if (i < width_b)
		{
			remap_pin_to_new_node(node->input_pins[i+port_B_offset], reduction2, i);
		}
		else
		{
			/* ELSE - the B input does not exist, so this answer goes right through */
			add_a_input_pin_to_node_spot_idx(reduction2, get_a_zero_pin(netlist), i);
		}
	}

	connect_nodes(reduction1, 0, new_logic_cell, 0);
	connect_nodes(reduction2, 0, new_logic_cell, 1);

	instantiate_bitwise_reduction(reduction1, BITWISE_OR, mark, netlist);
	instantiate_bitwise_reduction(reduction2, BITWISE_OR, mark, netlist);

	remap_pin_to_new_node(node->output_pins[0], new_logic_cell, 0);
}
Exemplo n.º 5
0
enum obj_type X86_register_get_type (X86_register r)
{
    switch (r)
    {
    case R_AL: case R_BL: case R_CL: case R_DL:
    case R_AH: case R_BH: case R_CH: case R_DH:
    case R_R8L:  case R_R9L:  case R_R10L: case R_R11L: case R_R12L: 
    case R_R13L: case R_R14L: case R_R15L: 
    case R_SPL:  case R_BPL:  case R_SIL:  case R_DIL: 
        return OBJ_BYTE;

    case R_AX: case R_BX: case R_CX: case R_DX: 
    case R_BP: case R_DI: case R_SI: case R_SP: 
    case R_FS: case R_GS: case R_SS: case R_ES: case R_DS: case R_CS: 
    case R_R8W:  case R_R9W:  case R_R10W: case R_R11W: case R_R12W: 
    case R_R13W: case R_R14W: case R_R15W: 
        return OBJ_WYDE;

    case R_EAX: case R_EBX: case R_ECX: case R_EDI: case R_EDX: case R_ESI: 
    case R_ESP: case R_EBP: case R_EIP:
    case R_R8D:  case R_R9D:  case R_R10D: case R_R11D: case R_R12D: 
    case R_R13D: case R_R14D: case R_R15D: 
        return OBJ_TETRABYTE;

    case R_RAX: case R_RBX: case R_RCX: case R_RDI: case R_RDX: case R_RSI:
    case R_RSP: case R_RBP: case R_RIP:
    case R_R8:  case R_R9:  case R_R10: case R_R11: case R_R12:
    case R_R13: case R_R14: case R_R15:
        return OBJ_OCTABYTE;

    case R_ST0: case R_ST1: case R_ST2: case R_ST3: 
    case R_ST4: case R_ST5: case R_ST6: case R_ST7: 
        return OBJ_DOUBLE; // not very precious, but OK.

    case R_XMM0: case R_XMM1: case R_XMM2: case R_XMM3: case R_XMM4: 
    case R_XMM5: case R_XMM6: case R_XMM7: case R_XMM8: case R_XMM9: 
    case R_XMM10: case R_XMM11: case R_XMM12: case R_XMM13: case R_XMM14: case R_XMM15:
        return OBJ_XMM;

    case R_MM0: case R_MM1: case R_MM2: case R_MM3:
    case R_MM4: case R_MM5: case R_MM6: case R_MM7:
        oassert (!"MMX registers are not handled here");
        fatal_error();

    case R_PF: case R_SF: case R_AF: case R_ZF: 
    case R_OF: case R_CF: case R_DF: case R_TF: 
        oassert (!"flag registers are not handled here");
        fatal_error();

    case R_ABSENT:
        oassert (!"R_ABSENT isn't handled here");
        fatal_error();

    default: 
        oassert(!"unknown register");
        fatal_error();
    };
};
Exemplo n.º 6
0
/*-------------------------------------------------------------------------
 * (function: split_multiplier_a)
 *
 * This function works to split the "a" input of a multiplier into 
 *  several smaller multipliers to better "fit" with the available 
 *  resources in a targeted FPGA architecture.
 *
 * This function is at the lowest level since it simply receives
 *  a multiplier and is told how to split it. The end result is:
 *
 *  a1a0 * b => a0 * b + a1 * b => c
 *  
 * Note that for the addition we need to perform sign extension,
 * but this should not be a problem since the sign extension is always
 * extending NOT contracting.
 *
 *-----------------------------------------------------------------------*/
void split_multiplier_a(nnode_t *node, int a0, int a1, int b)
{
	nnode_t *a0b, *a1b, *addsmall;
	int i;

	/* Check for a legitimate split */
	oassert(node->input_port_sizes[0] == (a0 + a1));
	oassert(node->input_port_sizes[1] == b);
	
	/* New node for a0b multiply */
	a0b = allocate_nnode();
	a0b->name = (char *)malloc(strlen(node->name) + 3);
	strcpy(a0b->name, node->name);
	strcat(a0b->name, "-0");
	init_split_multiplier(node, a0b, 0, a0, 0, b);
	mult_list = insert_in_vptr_list(mult_list, a0b);

	/* New node for a1b multiply */
	a1b = allocate_nnode();
	a1b->name = (char *)malloc(strlen(node->name) + 3);
	strcpy(a1b->name, node->name);
	strcat(a1b->name, "-1");
	init_split_multiplier(node, a1b, a0, a1, 0, b);
	mult_list = insert_in_vptr_list(mult_list, a1b);

	/* New node for the add */
	addsmall = allocate_nnode();
	addsmall->name = (char *)malloc(strlen(node->name) + 6);
	strcpy(addsmall->name, node->name);
	strcat(addsmall->name, "-add0");
	init_cascade_adder(addsmall, a1b, a1 + b);
	
	/* Connect pins for addsmall */
	for (i = a0; i < a0b->output_port_sizes[0]; i++)
		connect_nodes(a0b, i, addsmall, i-a0);
	for (i = a0b->output_port_sizes[0] - a0; i < a1+b; i++) /* Sign extend */
		connect_nodes(a0b, a0b->output_port_sizes[0]-1, addsmall, i);
	for (i = b+a1; i < (2 * (a1 + b)); i++)
		connect_nodes(a1b, i-(b+a1), addsmall, i);

	/* Move original output pins for multiply to new outputs */
	for (i = 0; i < a0; i++)
		remap_pin_to_new_node(node->output_pins[i], a0b, i);

	for (i = a0; i < node->num_output_pins; i++)
		remap_pin_to_new_node(node->output_pins[i], addsmall, i-a0);

	/* Probably more to do here in freeing the old node! */
	free(node->name);
	free(node->input_port_sizes);
	free(node->output_port_sizes);

	/* Free arrays NOT the pins since relocated! */
	free(node->input_pins); 
	free(node->output_pins); 
	free(node);
	return;
}
Exemplo n.º 7
0
/*-------------------------------------------------------------------------
 * (function: split_multiplier_b)
 *
 * This function works to split the "b" input of a multiplier into 
 *  several smaller multipliers to better "fit" with the available 
 *  resources in a targeted FPGA architecture.
 *
 * This function is at the lowest level since it simply receives
 *  a multiplier and is told how to split it. The end result is:
 *
 *  a * b1b0 => a * b1 + a * b0 => c
 *  
 * Note that for the addition we need to perform sign extension,
 * but this should not be a problem since the sign extension is always
 * extending NOT contracting.
 *
 *-----------------------------------------------------------------------*/
void split_multiplier_b(nnode_t *node, int a, int b1, int b0)
{
	nnode_t *ab0, *ab1, *addsmall;
	int i;

	/* Check for a legitimate split */
	oassert(node->input_port_sizes[0] == a);
	oassert(node->input_port_sizes[1] == (b0 + b1));
	
	/* New node for ab0 multiply */
	ab0 = allocate_nnode();
	ab0->name = (char *)malloc(strlen(node->name) + 3);
	strcpy(ab0->name, node->name);
	strcat(ab0->name, "-0");
	init_split_multiplier(node, ab0, 0, a, 0, b0);
	mult_list = insert_in_vptr_list(mult_list, ab0);

	/* New node for ab1 multiply */
	ab1 = allocate_nnode();
	ab1->name = (char *)malloc(strlen(node->name) + 3);
	strcpy(ab1->name, node->name);
	strcat(ab1->name, "-1");
	init_split_multiplier(node, ab1, 0, a, b0, b1);
	mult_list = insert_in_vptr_list(mult_list, ab1);

	/* New node for the add */
	addsmall = allocate_nnode();
	addsmall->name = (char *)malloc(strlen(node->name) + 6);
	strcpy(addsmall->name, node->name);
	strcat(addsmall->name, "-add0");
	init_cascade_adder(addsmall, ab1, a + b1);
	
	/* Connect pins for addsmall */
	for (i = b0; i < ab0->output_port_sizes[0]; i++)
		connect_nodes(ab0, i, addsmall, i-b0);
	for (i = ab0->output_port_sizes[0] - b0; i < a+b1; i++) /* Sign extend */
		connect_nodes(ab0, ab0->output_port_sizes[0]-1, addsmall, i);
	for (i = b1+a; i < (2 * (a + b1)); i++)
		connect_nodes(ab1, i-(b1+a), addsmall, i);

	/* Move original output pins for multiply to new outputs */
	for (i = 0; i < b0; i++)
		remap_pin_to_new_node(node->output_pins[i], ab0, i);

	for (i = b0; i < node->num_output_pins; i++)
		remap_pin_to_new_node(node->output_pins[i], addsmall, i-b0);

	/* Probably more to do here in freeing the old node! */
	free(node->name);
	free(node->input_port_sizes);
	free(node->output_port_sizes);

	/* Free arrays NOT the pins since relocated! */
	free(node->input_pins); 
	free(node->output_pins); 
	free(node);
	return;
}
Exemplo n.º 8
0
bool Da_op_set_value_of_op (struct Da_op* op, obj *val, CONTEXT * ctx, struct MemoryCache *mem, unsigned ins_prefixes, address FS, bool clear_high_tetra_if_ExX) 
{
    address adr;

    switch (op->type)
    {
        case DA_OP_TYPE_REGISTER:
            //L ("%s() line %d clear_high_tetra_if_ExX=%d\n", __FUNCTION__, __LINE__, clear_high_tetra_if_ExX);
            X86_register_set_value (op->reg, ctx, val, clear_high_tetra_if_ExX);
            return true;

        case DA_OP_TYPE_VALUE_IN_MEMORY:
            adr=(address)Da_op_calc_adr_of_op(op, ctx, mem, ins_prefixes, FS);

            switch (op->value_width_in_bits)
            {
                case 8:
                    if (MC_WriteByte (mem, adr, obj_get_as_byte(val))==false)
                        goto COPY_FAILED;
                    return true;
                case 16:
                    if (MC_WriteWyde (mem, adr, obj_get_as_wyde(val))==false)
                        goto COPY_FAILED;
                    return true;
                case 32:
                    if (MC_WriteTetrabyte (mem, adr, obj_get_as_tetra(val))==false)
                        goto COPY_FAILED;
                    return true;
                case 64:
                    if (MC_WriteOctabyte (mem, adr, obj_get_as_octa(val))==false)
                        goto COPY_FAILED;
                    return true;
                case 128:
                    {
                        //val.dump();
                        byte * xmm=obj_get_as_xmm(val);
                        //L ("%s(). writing to adr=0x%x\n", __FUNCTION__, adr);
                        //L_print_buf (xmm, 16);
                        if (MC_WriteBuffer (mem, adr, 16, xmm)==false)
                            goto COPY_FAILED;
                        return true;
                    }

                default:
                    oassert(!"unsupported value_width_in_bits");
                    fatal_error();
            };

        default:
            oassert(!"unsupported type");
            fatal_error();
    };
COPY_FAILED:
    //L ("%s(): Error writing at 0x" PRI_ADR_HEX ". Copy failed.\n", __FUNCTION__, adr);
    return false;
};
Exemplo n.º 9
0
/**
 * Recursively resolves an IDENTIFIER to a parameter into its actual value,
 * by looking it up in the global_param_table_sc
 * Also try and fold any BINARY_OPERATIONs now that an IDENTIFIER has been
 * resolved
 */
ast_node_t *resolve_node(char *module_name, ast_node_t *node)
{
    if (node)
    {
        long sc_spot;
        int i;
        info_ast_visit_t *node_details;
        STRING_CACHE *local_param_table_sc;

        ast_node_t *node_copy;
        node_copy = (ast_node_t *)malloc(sizeof(ast_node_t));
        memcpy(node_copy, node, sizeof(ast_node_t));
        node_copy->children = malloc(sizeof(ast_node_t*) * node_copy->num_children);

        for (i = 0; i < node->num_children; i++)
        {
            node_copy->children[i] = resolve_node(module_name, node->children[i]);
        }

        switch (node->type)
        {
        case IDENTIFIERS:
            oassert(module_name);
            sc_spot = sc_lookup_string(global_param_table_sc, module_name);
            oassert(sc_spot != -1);
            local_param_table_sc = (STRING_CACHE *)global_param_table_sc->data[sc_spot];
            sc_spot = sc_lookup_string(local_param_table_sc, node->types.identifier);
            if (sc_spot != -1)
            {
                node = ((ast_node_t *)local_param_table_sc->data[sc_spot]);
                oassert(node->type == NUMBERS);
            }
            break;

        case BINARY_OPERATION:
            node_copy->shared_node = TRUE;
            node_details = constantFold(node_copy);
            node_copy->shared_node = FALSE;
            if (node_details && node_details->is_constant_folded == TRUE)
            {
                node = node_details->from;
                free(node_details);
                oassert(node->type == NUMBERS);
            }
            break;

        default:
            break;
        }

        free(node_copy->children);
        free(node_copy);
    }
    return node;
}
Exemplo n.º 10
0
/*---------------------------------------------------------------------------------------------
 * (function: add_a_output_pin_to_spot_idx)
 *-------------------------------------------------------------------------------------------*/
void add_driver_pin_to_net(nnet_t *net, npin_t *pin)
{
	oassert(net != NULL);
	oassert(pin != NULL);
	oassert(pin->type != INPUT);
	/* assumes the pin spots have been allocated and the pin */
	net->driver_pin = pin;
	/* record the node and pin spot in the pin */
	pin->net = net;
	pin->type = OUTPUT;
}
Exemplo n.º 11
0
/*
 * Pads the width of a single port memory to that specified in the arch file.
 */
void pad_sp_memory_width(nnode_t *node, netlist_t *netlist)
{
	oassert(node->type == MEMORY);
	oassert(single_port_rams != NULL);

	pad_memory_input_port (node, netlist, single_port_rams, "data");

	pad_memory_output_port(node, netlist, single_port_rams, "out");

	sp_memory_list = insert_in_vptr_list(sp_memory_list, node);
}
Exemplo n.º 12
0
/*---------------------------------------------------------------------------------------------
 * (function: add_a_output_pin_to_node_spot_idx)
 *-------------------------------------------------------------------------------------------*/
void add_output_pin_to_node(nnode_t *node, npin_t *pin, int pin_idx)
{
	oassert(node != NULL);
	oassert(pin != NULL);
	oassert(pin_idx < node->num_output_pins);
	/* assumes the pin spots have been allocated and the pin */
	node->output_pins[pin_idx] = pin;
	/* record the node and pin spot in the pin */
	pin->type = OUTPUT;
	pin->node = node;
	pin->pin_node_idx = pin_idx;
}
Exemplo n.º 13
0
void set_or_update_DRx_for_thread(thread *t, BP *bp, unsigned DRx_no)
{
    CONTEXT ctx;
    ctx.ContextFlags = CONTEXT_ALL;
    DWORD rt;
    rt=GetThreadContext (t->THDL, &ctx); oassert (rt!=FALSE);      

    if (utils_c_debug)
        L ("%s() going to call set_or_update_DRx_breakpoint for TID %d\n", __func__, t->TID);
    set_or_update_DRx_breakpoint(bp, &ctx, DRx_no);

    rt=SetThreadContext (t->THDL, &ctx); oassert (rt!=FALSE);
};
Exemplo n.º 14
0
/*---------------------------------------------------------------------------
 * (function: record_mult_distribution)
 *-------------------------------------------------------------------------*/
void record_mult_distribution(nnode_t *node)
{
	int a, b;

	oassert(hard_multipliers != NULL);
	oassert(node != NULL);

	a = node->input_port_sizes[0];
	b = node->input_port_sizes[1];

	mults[a * hard_multipliers->inputs->size + b] += 1;
	return;
}
Exemplo n.º 15
0
/*
 * Pads the width of a dual port memory to that specified in the arch file.
 */
void pad_dp_memory_width(nnode_t *node, netlist_t *netlist)
{
	oassert(node->type == MEMORY);
	oassert(dual_port_rams != NULL);

	pad_memory_input_port(node, netlist, dual_port_rams, "data1");
	pad_memory_input_port(node, netlist, dual_port_rams, "data2");

	pad_memory_output_port(node, netlist, dual_port_rams, "out1");
	pad_memory_output_port(node, netlist, dual_port_rams, "out2");

	dp_memory_list = insert_in_vptr_list(dp_memory_list, node);
}
Exemplo n.º 16
0
/*---------------------------------------------------------------------------------------------
 * (function: add_a_input_pin_to_spot_idx)
 *-------------------------------------------------------------------------------------------*/
void add_fanout_pin_to_net(nnet_t *net, npin_t *pin)
{
	oassert(net != NULL);
	oassert(pin != NULL);
	oassert(pin->type != OUTPUT);
	/* assumes the pin spots have been allocated and the pin */
	net->fanout_pins = (npin_t**)realloc(net->fanout_pins, sizeof(npin_t*)*(net->num_fanout_pins+1));
	net->fanout_pins[net->num_fanout_pins] = pin;
	net->num_fanout_pins++;
	/* record the node and pin spot in the pin */
	pin->net = net;
	pin->pin_net_idx = net->num_fanout_pins-1;
	pin->type = INPUT;
}
Exemplo n.º 17
0
/*---------------------------------------------------------------------------------------------
 * (function: cleanup_activation)
 *-------------------------------------------------------------------------------------------*/
void cleanup_activation(netlist_t *netlist)
{
	int i, j;

	for (i = 0; i < netlist->num_forward_levels; i++)
	{
		for (j = 0; j < netlist->num_at_forward_level[i]; j++)
		{
			/* initialize the activation data */
			nnode_t *current_node = netlist->forward_levels[i][j];
			activation_t *act_data = (activation_t*)current_node->node_data;
			oassert(act_data != NULL);

			if (act_data->static_probability != NULL)
				free(act_data->static_probability);
			if (act_data->transition_density != NULL)
				free(act_data->transition_density);
			if (act_data->transition_probability != NULL)
				free(act_data->transition_probability);

			free(act_data);
			current_node->unique_node_data_id = RESET;
		}
	}
}
Exemplo n.º 18
0
X86_register X86_register_get_32bit_part_of(X86_register r)
{
    switch (r)
    {
        case R_EAX: case R_AX: case R_AL: case R_AH: 
            return R_EAX;
        case R_EBX: case R_BX: case R_BL: case R_BH: 
            return R_EBX;
        case R_ECX: case R_CX: case R_CL: case R_CH: 
            return R_ECX;
        case R_EDX: case R_DX: case R_DL: case R_DH: 
            return R_EDX;
        case R_ESI: case R_SI: 
            return R_ESI;
        case R_EDI: case R_DI: 
            return R_EDI;
        case R_EBP: case R_BP: 
            return R_EBP;
        case R_ESP: case R_SP: 
            return R_ESP;
        default:
            oassert(0);
            fatal_error();
    };
};
Exemplo n.º 19
0
enum X86_register _32_bit_X86_register_is_part_of_64_bit_reg (enum X86_register r)
{
    switch (r)
    {
    case R_EAX: return R_RAX;
    case R_EBX: return R_RBX;
    case R_ECX: return R_RCX;
    case R_EDX: return R_RDX;
    case R_ESI: return R_RSI;
    case R_EDI: return R_RDI;
    case R_EBP: return R_RBP;
    case R_ESP: return R_RSP;
    case R_EIP: return R_RIP;
    
    case R_R8D:  return R_R8;
    case R_R9D:  return R_R9;
    case R_R10D: return R_R10;
    case R_R11D: return R_R11;
    case R_R12D: return R_R12;
    case R_R13D: return R_R13;
    case R_R14D: return R_R14;
    case R_R15D: return R_R15;
    default: oassert(0);
    };
}
/*---------------------------------------------------------------------------------------------
 *  (function: make_simple_name )
 *-------------------------------------------------------------------------------------------*/
char *make_simple_name(char *input, const char *flatten_string, char flatten_char)
{
	unsigned int i;
	unsigned int j;
	char *return_string = NULL;
	oassert(input != NULL);

	return_string = (char*)malloc(sizeof(char)*(strlen(input)+1));

	for (i = 0; i < strlen(input); i++)
	{ 
		return_string[i] = input[i];
		for (j = 0; j < strlen(flatten_string); j++)
		{
			if (input[i] == flatten_string[j])
			{
				return_string[i] = flatten_char;
				break;
			}
		}
	}

	return_string[strlen(input)] = '\0';	

	return return_string;
}
Exemplo n.º 21
0
/*---------------------------------------------------------------------------------------------
 * (function: make_output_pins_for_existing_node)
 * 	Looks at a node and extracts the output pins into a signal list so they can be accessed
 * 	in this form
 *-------------------------------------------------------------------------------------------*/
signal_list_t *make_output_pins_for_existing_node(nnode_t* node, int width)
{
	signal_list_t *return_list = init_signal_list();
	int i; 

	oassert(node->num_output_pins == width);

	for (i = 0; i < width; i++)
	{
		npin_t *new_pin1;
		npin_t *new_pin2;
		nnet_t *new_net;
		new_pin1 = allocate_npin();
		new_pin2 = allocate_npin();
		new_net = allocate_nnet();
		new_net->name = node->name;
		/* hook the output pin into the node */
		add_output_pin_to_node(node, new_pin1, i);
		/* hook up new pin 1 into the new net */
		add_driver_pin_to_net(new_net, new_pin1);
		/* hook up the new pin 2 to this new net */
		add_fanout_pin_to_net(new_net, new_pin2);
		
		/* add the new_pin2 to the list of pins */
		add_pin_to_signal_list(return_list, new_pin2);
	}

	return return_list;
}
Exemplo n.º 22
0
/*---------------------------------------------------------------------------
 * (function: move_a_input_pin)
 *-------------------------------------------------------------------------*/
void move_input_pin(nnode_t *node, int old_idx, int new_idx)
{
	npin_t *pin;

	oassert(node != NULL);
	oassert(((old_idx >= 0) && (old_idx < node->num_input_pins)));
	oassert(((new_idx >= 0) && (new_idx < node->num_input_pins)));
	/* assumes the pin spots have been allocated and the pin */
	node->input_pins[new_idx] = node->input_pins[old_idx];
	pin = node->input_pins[old_idx];
	node->input_pins[old_idx] = NULL;
	/* record the node and pin spot in the pin */
	pin->type = INPUT;
	pin->node = node;
	pin->pin_node_idx = new_idx;
}
Exemplo n.º 23
0
thread *find_thread (DWORD PID, DWORD TID)
{
    process *p=find_process(PID);
    thread *t=(thread*)rbtree_lookup (p->threads, (void*)TID);
    oassert (t!=NULL && "TID not found in threads table");
    return t;
};
Exemplo n.º 24
0
/*---------------------------------------------------------------------------------------------
 * (function: boolean_difference)
 *-------------------------------------------------------------------------------------------*/
short *boolean_difference(nnode_t *node, int variable_spot)
{
	int i, l;
	short *return_function;
	int function_size;
	long long skip_size = 1 << variable_spot;
	int index;

	oassert(node->num_input_pins < sizeof (long long int)*8);
	oassert(node->associated_function != NULL);
	oassert(node->num_input_pins > 1);

	/* calculate the size of the boolean difference */
	function_size = pow2(node->num_input_pins-1);

	return_function = (short*)calloc(sizeof(short), function_size);

	for (i = 0; i < function_size; i++)
	{
		long long int place = 1;
		long long int index_plus = 1;
		index = 0;
	
		/* IF - this function value is on then calculate the probability */
		for (l = 0; l < node->num_input_pins-1; l++)
		{
			/* move the bit collumn */
			if (l == variable_spot)
			{
				index_plus = index_plus << 1;
			}

			if ((i & place) > 0) // if this bit is high
			{
				index += index_plus;
			}

			place = place << 1;
			index_plus = index_plus << 1;
		}

		/* do the boolean xor of this element */
		return_function[i] = node->associated_function[index] ^ node->associated_function[index+skip_size];
	}

	return return_function;
}
Exemplo n.º 25
0
static void dump_stack_EBP_frame (process *p, thread *t, CONTEXT * ctx, MemoryCache *mem)
{
    HANDLE THDL=t->THDL;
    address stack_top=TIB_get_stack_top (THDL, mem);
    address stack_bottom=TIB_get_stack_bottom (THDL, mem);
    strbuf sb=STRBUF_INIT;

    L ("Call stack:\n");

    address next_BP=CONTEXT_get_BP (ctx);

    if (TIB_is_ptr_in_stack_limits (THDL, next_BP, mem)==false)
    {
        L ("Current frame pointer %s (0x" PRI_ADR_HEX ") is not within current stack limits (top=0x" PRI_ADR_HEX ", bottom=0x" PRI_ADR_HEX ")\n", 
                BP_REGISTER_NAME,
                CONTEXT_get_BP (ctx), stack_top, stack_bottom);
        goto exit;
    };

    while (next_BP<=stack_top && next_BP>=stack_bottom)
    {
        REG tmp;
        bool b=MC_ReadREG(mem, next_BP, &tmp);
        oassert (b);
        address ret_adr;
        b=MC_ReadREG(mem, next_BP+sizeof(REG), &ret_adr);
        oassert (b);

        if (ret_adr==0)
            break;

        strbuf_reinit(&sb, 0);
        process_get_sym (p, ret_adr, true, true, &sb);

        L ("return address=0x" PRI_ADR_HEX " (%s)\n", ret_adr, sb.buf);

        if (next_BP==tmp)
            break;
        
        next_BP=tmp;
    };

exit:
    strbuf_deinit (&sb);
};
Exemplo n.º 26
0
void add_symbol (address a, char *name, add_symbol_params *params)
{
    module *m=params->m;
    rbtree *symtbl=m->symbols;
    oassert(symtbl && "symbols=NULL in module");
    MemoryCache *mc=params->mc;

    if (one_time_int3_bp_re && params->t==SYM_TYPE_PE_EXPORT && module_adr_in_executable_section (m, a))
    {
        strbuf sb=STRBUF_INIT;
        strbuf_addstr (&sb, get_module_name(m));
        strbuf_addc (&sb, '!');
        strbuf_addstr (&sb, name);

        if (regexec (one_time_int3_bp_re, sb.buf, 0, NULL, 0)==0)
            set_onetime_INT3_BP(a, params->p, m, name, mc);

        strbuf_deinit (&sb);
    };

    if (dump_seh && string_is_ends_with (name, "security_cookie"))
    {
        m->security_cookie_adr=a;
        m->security_cookie_adr_known=true;
        if (symbol_c_debug)
            L ("%s() got address of security_cookie (0x" PRI_REG_HEX ") for %s!%s\n", __FUNCTION__, a, get_module_name(m), name);
    };

    bool dump_symbol=false;
    if (dump_all_symbols_re)
    {
        strbuf sb=STRBUF_INIT;
        strbuf_addstr (&sb, get_module_name(m));
        strbuf_addc (&sb, '!');
        strbuf_addstr (&sb, name);

        if (regexec (dump_all_symbols_re, sb.buf, 0, NULL, 0)==0)
            dump_symbol=true;

        strbuf_deinit (&sb);
    };

    if (dump_symbol || (dump_all_symbols_re==NULL && dump_all_symbols))
    {
        dump_PID_if_need(params->p);
        L("New symbol. Module=[%s], address=[0x" PRI_ADR_HEX "], name=[%s]\n", get_module_name(m), a, name);
    };

    symbol *new_sym=create_symbol(params->t, name);
    symbol *first_sym=(symbol*)rbtree_lookup(symtbl, (void*)a);

    if (first_sym)
        new_sym->next=first_sym; // insert at beginning of list

    rbtree_insert(symtbl, (void*)a, (void*)new_sym);
};
Exemplo n.º 27
0
MemoryCache* MC_MemoryCache_ctor_testing(BYTE *testing_memory, SIZE_T testing_memory_size)
{
	oassert ((testing_memory_size & (PAGE_SIZE-1))==0);
	MemoryCache* rt=DCALLOC(MemoryCache, 1, "MemoryCache");
	rt->_cache=rbtree_create(true, "MemoryCache._cache", compare_size_t);
	rt->testing=true;
	rt->testing_memory=testing_memory;
	rt->testing_memory_size=testing_memory_size;
	return rt;
};
Exemplo n.º 28
0
/*---------------------------------------------------------------------------
 * (function: init_mult_distribution)
 *-------------------------------------------------------------------------*/
void init_mult_distribution()
{
	int i, j;

	oassert(hard_multipliers != NULL);
	mults = (int *)malloc(sizeof(int) * (hard_multipliers->inputs->size + 1) * (1 + hard_multipliers->inputs->next->size));
	for (i = 0; i <= hard_multipliers->inputs->size; i++)
		for (j = 0; j <= hard_multipliers->inputs->next->size; j++)
			mults[i * hard_multipliers->inputs->size + j] = 0;
	return;
}
Exemplo n.º 29
0
void strbuf_addstr_range_be (strbuf *sb, const char *s, unsigned begin, unsigned end)
{
	int i;

	oassert (begin<end);

	// FIXME: to be optimized

	for (i=begin; i<end; i++)
		strbuf_addc (sb, s[i]); 
};
Exemplo n.º 30
0
/**
 * Make a unique name for a module based on its parameter list
 * e.g. for a "mod #(0,1,2,3) a(b,c,d)" instantiation you get name___0_1_2_3
 */
char *make_module_param_name(ast_node_t *module_param_list, char *module_name)
{
    char *module_param_name = (char*)malloc((strlen(module_name)+1024) * sizeof(char));
    strcpy(module_param_name, module_name);

    if (module_param_list)
    {
        int i;
        oassert(module_param_list->num_children > 0);
        strcat(module_param_name, "___");
        for (i = 0; i < module_param_list->num_children; i++)
        {
            oassert(module_param_list->children[i]->children[5]->type == NUMBERS);

            sprintf(module_param_name, "%s_%lld", module_param_name, module_param_list->children[i]->children[5]->types.number.value);
        }
    }

    return module_param_name;
}