//F
void fetch()
{
    int i;
    char str[32];
    fprintf(stderr, "[IIU]:Instruction fetch\n");

    if (feof(_src))
        return;
   
    for (i = 0; i < 4; i++) {
        fscanf(_src, "%[^\n]\n", str);
        _ci[i] = new_instruction();
        _ci[i]->string = strdup(str);
    }
}
Ejemplo n.º 2
0
static void generate_return(code_block *parent, return_statement *ret) {
  function *fn = ret->target;

  bool non_void = !is_void(fn->return_type);
  if (non_void == !ret->value) {
    fprintf(stderr, "return from void function with return or non-void without "
      "return\n");
    abort();
  }

  size_t param_count = 1 + non_void;

  if (non_void) {
    parent = generate_expression(parent, ret->value);
  }

  parent->tail.type = GOTO;
  parent->tail.parameter_count = param_count;
  parent->tail.parameters = xmalloc(sizeof(size_t) * param_count);

  if (non_void) {
    parent->tail.parameters[1] = last_instruction(parent);
  }

  parent->tail.parameters[0] = 0;
  parent->tail.first_block = next_instruction(parent);

  size_t return_index = instruction_type(parent, 0)->struct_index;
  code_struct *return_struct = get_code_struct(parent->system, return_index);
  type *return_block_type = return_struct->fields[0].field_type;

  code_instruction *unwrap = new_instruction(parent, 2);
  unwrap->operation.type = O_GET_FIELD;
  unwrap->type = copy_type(return_block_type);
  unwrap->parameters[0] = 0; // return structs are always the first argument
  unwrap->parameters[1] = 0; // blockref position in all return structs
}
Ejemplo n.º 3
0
void Dag::output_quadcode(InsCtrl& ins)
{
	std::vector<unsigned int> oper_stack;
	std::map<unsigned int, symbol*> obj_operand;

	//get the reverse order of the all operators, will be put in the oper_stack
	while(!operators.empty())
	{
		bool find=false;
		unsigned int _index=0;
		//first find the node index which do not have a parent
		for(std::map<Quadcode::Quad_type,num_set>::iterator it=operators.begin();it!=operators.end();it++)
		{
			num_set& node_set=it->second;	//the list of node index which the operator is in
			for(num_set::iterator it1=node_set.begin();it1!=node_set.end();it1++)
			{
				if(is_no_parent(dag_nodes,*it1)&&(*it1>_index||!find))	//the dag tree can have multiple branches, so we find the one with maximum index
				{
					_index=*it1;
					find=true;
				}
			}
		}
		
		if(find)	//we have find the index
		{
			do
			{
				std::map<Quadcode::Quad_type,num_set>::iterator it=operators.find(dag_nodes[_index]->typ); //find the given operator set
				assert(it!=operators.end());
				num_set& node_set=it->second; //the node set of the operation
				num_set::iterator it1=node_set.find(_index);
				node_set.erase(it1);	//erase the find node
				if(node_set.empty()) operators.erase(it);

				oper_stack.push_back(_index);
				dag_nodes[_index]->enable=false;
				_index=dag_nodes[_index]->left;	//traverse the left branch
			}
			while(!dag_nodes[_index]->is_leaf&&is_no_parent(dag_nodes,_index));	//while the operation node have no parent
		}
		else
		{assert(operators.empty());}
	}

	//already get the operator stack , output the code in the reverse order

	node_assign(ins,obj_operand,0);
	for(int i=oper_stack.size();i>0;i--)
	{
		DNode* node=dag_nodes[oper_stack[i-1]];	//in reverse order
		Quadcode::Quad_type typ=node->typ;
		Quadcode* code=NULL;
		symbol* src_arg1=NULL;
		symbol* src_arg2=NULL;
		symbol* obj_arg=NULL;

		assert(!node->is_leaf);
		src_arg1=get_rep_operand(obj_operand,node->left);
		if(node->two_children) src_arg2=get_rep_operand(obj_operand,node->right);
		obj_arg=get_rep_operand(obj_operand,oper_stack[i-1]);
		update_origin(obj_arg,oper_stack[i-1]);		//if not leave, strip it out
		if(src_arg2!=NULL) code=new_instruction(typ,obj_arg,src_arg1,src_arg2);
		else code=new_instruction(typ,obj_arg,src_arg1,src_arg1);
		ins.new_instruction(code);
		node_assign(ins,obj_operand,oper_stack[i-1]+1);		//plus one because basic index of assign_info is 1
	}
	flush();
}
Ejemplo n.º 4
0
void test_instruction_1(void) {
    Instruction* i = new_instruction(op("nop", NOP));
    TEST_ASSERT_EQUAL_INT(I_TYPE_NONE, i->type);
    free(i);
}
Ejemplo n.º 5
0
Archivo: pass.c Proyecto: ephesus/Zasm2
/** Parse text file and create
 * list elements for each instruction
 * appends instructions to the end of the list it gets
 * so that you can call this more than once, to include
 * more than one file
 * if you call it passing a root that is null, it will return
 * the beginning of the list
 * if you pass it something other than null, it returns a pointer
 * to the last link of the list
 */
struct instruction *parse_source(FILE *infile, struct instruction* initial_root, struct tab_entry *tabroot)
{

    struct instruction *cur_old =NULL, *cur = NULL;
    struct instruction *inst_root;
    char buffer[INSTRUCTION_BUFFER_SIZE];
    char b[INSTRUCTION_BUFFER_SIZE];
    char *buf, *ptr;
    int instructions = 0;
    int cur_op_num;

    inst_root = initial_root;
    buf = b;

    while (fgets(buffer, INSTRUCTION_BUFFER_SIZE, infile))
    {
        strip_comment(buffer);
        linenumber++;

        if (cur == NULL)
            cur = new_instruction();

        if (isblank(buffer[0]) || (buffer[0] == '\n') || (buffer[0] == '#') || (buffer[0] == '.')) {
            if (buffer[0] == '#') //make preprocessor directives .<directive>
                buffer[0] = '.';

            /** split line, get instruction and operands */
            if ((buf = (char *) strtok(buffer, whitespace))) {
                instructions++;

                if (!inst_root)
                    inst_root = cur;

                if (cur_old)
                    cur_old->next = cur;

                capitalize(buf);

                strncpy(cur->mnumonic, buf, MNUMONIC_TXT_LENGTH);

                get_operands(cur);
            }

            calculate_opcode(tabroot, cur);

            cur_old = cur;
            cur = new_instruction();

        } else {
            /* see if it's a valid label */
            if (strlen(buffer) > 0) {
              if ((buf = (char *) strtok(buffer, whitespace))) {
                  instructions++;

                  if (!inst_root)
                      inst_root = cur;

                  if (cur_old)
                      cur_old->next = cur;

                  capitalize(buf);

                  strncpy(cur->mnumonic, buf, MNUMONIC_TXT_LENGTH);

                  get_operands(cur);
              }
              // buffer holds untokenized string with charcter at *[0]
                if (validate_label(buffer)) {
                    add_label(buffer, cur);
                } else {
                    do_error_msg(ERR_BADLABEL);
                }

            }
        }
    }

    /* return either the head or the tail */
    return initial_root == NULL ? inst_root : cur;
}
Ejemplo n.º 6
0
static void mirror_instruction(code_block *parent, size_t src) {
  code_instruction *mirror = new_instruction(parent, 1);
  mirror->operation.type = O_GET_SYMBOL;
  mirror->type = copy_type(instruction_type(parent, src));
  mirror->parameters[0] = src;
}