Exemplo n.º 1
0
leaf *build(char *expr, hashtab *h)
{
  lstack s;
  char opr;
  int i = -1, num;
  char str[MAX_STR];
  leaf *l = NULL, *r = NULL, *tmp = NULL;
  
  lstack_init(&s, 1024);
  
  while (expr[++i])
    {
      if (is_digit(expr[i]))
	{
	  num = get_number(expr, &i);
	  l = new_num_leaf(num, INT, NULL, NULL, -1);
	  lpush(&s, l);
	}
      else
	if (is_opr(expr[i]))
	  {
	    lpop(&s, &r);
	    lpop(&s, &l);
	    
	    //Optimization : reducing the amount of registers !!
	    if ((expr[i] == '+' || expr[i] == '*') && l->typ == INT && r->typ != INT) 
	      tmp = l, l = r, r = tmp;

	    l = new_num_leaf(expr[i], CHAR, l, r, get_reg_num(l, r));
	    lpush(&s, l);
	  }
	else
	  if (is_alpha(expr[i]))
	    {
	      get_string(expr, str, &i);
	      
	      if (search(h, str))
		{
		  lpop(&s, &r);	  
		  r->reg = 1; //Premice to register allocation !!!
		  l = new_str_leaf(str, FUNC, NULL, r, 1);
		  lpush(&s, l);
		}
	      else //If not a function then a variable identifier ! [extend the grammar]
		{
		  l = new_str_leaf(str, VAR, NULL, NULL, -1);
		  lpush(&s, l);
		}
	    }
    }
   
  lstack_free(&s);

  return l;
}
Exemplo n.º 2
0
char get_modif(char str[]) {
    DBGPRINT("Got str: %s\n", str);

    if (get_reg_num(str) >= 0)
        return 'r';

    for (int i = 0; i < strlen(str); i++)
        if (!isdigit(str[i]))
            return 'w';

    return 'n';
}
Exemplo n.º 3
0
void execute_program(UM_state um)
{
        int ra =0;
        int rb =0;
        int rc =0;         
        //uint32_t val= 0;
        bool proceed = true;
        int num_instructions = get_words_in_seg(um->memory, 0);
        int i = um->instr_ctr;

        while (i < num_instructions && proceed) {
                uint32_t word = get_word(um->memory, 0, i);
                uint32_t op_code = get_op_code(word);

                /*if (op_code == 13) {
                        int ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        (void) val;
                }

                else {*/
                        ra = get_reg_num(word, RA_LSB);
                        rb = get_reg_num(word, RB_LSB);
                        rc = get_reg_num(word, RC_LSB);

                        proceed &= valid_reg(ra);
                        proceed &= valid_reg(rb);
                        proceed &= valid_reg(rc);
                //}

                if (!proceed) {
                        break;
                }
                        
                if (op_code == 0) {
                        /* Conditional move - op code 0 */

                        if (val_in_reg(um, rc) != 0) {
                                uint32_t val = val_in_reg(um, rb);
                                set_reg_val(um, ra, val);
                        }
                                        
                } else if (op_code == 1) {
                        /* Segmented load - op code 1 */
                        uint32_t val = get_word(um->memory, val_in_reg(um, rb),
                                       val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 2) {

                        /* Segmented store - op code 2 */

                        uint32_t ID = val_in_reg(um, ra);
                        uint32_t offset = val_in_reg(um, rb);
                        uint32_t val = val_in_reg(um, rc);

                        put_word(um->memory, ID, offset, val);
                                        
                } else if (op_code == 3) {

                        /* Add - op code 3 */
                        uint32_t val = (val_in_reg(um, rb) + val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);

                                        
                } else if (op_code == 4) {
                        /* Multiply - op code 4 */
                        uint32_t val = (val_in_reg(um, rb) * val_in_reg(um, rc)) % UINT_MAX;
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 5) {
                        /* Divide - op code 5 */

                        uint32_t val = val_in_reg(um, rb) / val_in_reg(um, rc);
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 6) {
                        /* Bitwise NAND - op code 6 */

                        uint32_t val = ~(val_in_reg(um, rb) & val_in_reg(um, rc));
                        set_reg_val(um, ra, val);
                                        
                } else if (op_code == 7) {
                        /* Halt */
                        proceed = false;
                                        
                } else if (op_code == 8) {
                        /* Map segment - op code 8 */

                        if (Stack_empty(um->unmapped_IDs)) {
                              
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= create_segment(um->memory, num_words);             
                                set_reg_val(um, rb, (get_num_segs(um->memory) - 1));
                        }

                        else {
                                uint32_t ID = get_unmapped_ID(um);
                                uint32_t num_words = val_in_reg(um, rc);

                                proceed &= resize(um->memory, ID, num_words);

                                set_reg_val(um, rb, ID);
                        }

                } else if (op_code == 9) {
                        /* Unmap segment - op code 9 */
                        
                        uint32_t ID = val_in_reg(um, rc);
                        proceed &= clear_seg(um->memory, ID);
                        proceed &= add_unmapped_ID(um, ID);
                                        
                } else if (op_code == 10) {
                        /* Output - op code 10 */
                        uint32_t val = val_in_reg(um, rc);

                        if (val < 256) {
                                fprintf(stdout, "%c", (char) val);
                        } else {
                                proceed &= false;

                        }
                                        
                } else if (op_code == 11) {
                        /* Input - op code 11 */

                        uint32_t val = getc(stdin);

                        if ((int) val == EOF) {
                                val = ~0;

                        } else if (val > 255) {
                                proceed &= false;
                        }

                        set_reg_val(um, rc, val);
                                        
                } else if (op_code == 12) {
                        /* Load program - op code 12 */

                        uint32_t ID = val_in_reg(um, rb);

                        if (ID != 0) {
                                proceed &= clear_seg(um->memory, 0);

                                int num_words = get_words_in_seg(um->memory, ID);
                                
                                resize(um->memory, 0, num_words);

                                for (int i = 0; i < num_words; i++) {
                                        proceed &= put_word(um->memory, 0, i,
                                                               get_word(um->memory, ID, i));
                                }
                        }

                        um->instr_ctr = val_in_reg(um, rc);

                        num_instructions = 
                                get_words_in_seg(um->memory, 0);
                        i = um->instr_ctr;
                                        
                } else if (op_code == 13) {

                       /* Load value - op code 13 */
                        ra = get_reg_num(word, LOAD_RA_LSB);
                        proceed &= valid_reg(ra);

                        uint32_t val = get_val(word);
                        set_reg_val(um, ra, val);
                        
                } else {

                        fprintf(stderr, "op code doesn't exist\n");
                        proceed = false;
                }
                
                if (op_code != 12) {
                        i++;
                }
        }

        return;
}
Exemplo n.º 4
0
void put_cmd(vararray* binary, line_t* cur_line, int j, vararray* labels, vararray* funcs, int step) {
    int i = j, argc = 0, to_push = 0, pos = 0;
    char cmd_num = 0;

    char* keyword = cur_line->words[j];
    char modif[4], words[2][MAX_WORD_LENGTH];
    memset(modif, 0, sizeof(modif));
    memset(words, 0, sizeof(words));

    for (i = j+1; i < cur_line->num; (argc++, i++)) {
        DBGPRINT("1/Argument %d: %s\n", 0, words[0]);//What's this debugoutput tell you?
        strcpy(words[argc], cur_line->words[i]);
        DBGPRINT("2/Argument %d: %s\n", 0, words[0]);
        DBGPRINT("Argument %d: %s\n", argc, words[argc]);//QUESTION: Why you repeat the same dbgoutput here
        modif[argc] = get_modif(words[argc]);
        DBGPRINT("Argument %d: %s\n", 0, words[0]);// and here?
    }

    DBGPRINT("Argument %d: %s\n", 0, words[0]);
    DBGPRINT("Pre-define: Number: %d, keyword: \"%s\" MODIF: \"%s\" argc: %d\n", cmd_num, keyword, modif, argc);

#define CMD_(number_, keyword_, argc_, modif_, code_)\
    if ((!strcmp(keyword, #keyword_)) && (modif_cmp(modif, modif_)) && (argc_ == argc)) {\
        strcpy(modif, modif_);\
        cmd_num = number_;\
    } else

#include "../s_common/s_cmdlist.h"

#undef CMD_
    USERERR("No command found for \"%s\"\n", cur_line->words[0]);


    DBGPRINT("Number: %d, keyword: \"%s\" MODIF: \"%s\" argc: %d\n", cmd_num, keyword, modif, argc);
    pos = binary->nmax;//QUESTION: What's purpose of it?

    var_push(binary, &cmd_num);
    for (i = 0; i < argc; i++) {
        switch (modif[i]) {
        case 'r': {
            DBGPRINT("Call of get_reg_num.\n");
            to_push = get_reg_num(words[i]);
            break;
        }
        case 'n': {
            to_push = get_digit(words[i]);
            break;
        }
        case 'f': {
            to_push = get_lf(words[i], funcs, pos, step);
            break;
        }
        case 'l': {
            to_push = get_lf(words[i], labels, pos, step);
            break;
        }

        default: {
            FATALERR("Modif failure. Act_modif: %s\n", modif);//I think it's debug error. User shouldn't know it
            break;
        }
        }
        DBGPRINT("ARGUMENT %d: %d\n", i, to_push);
        var_pushn(binary, &to_push, sizeof(int));
    }

    DBGPRINT("Num of elems in bin: %d;\n"\
             "-----------------------------\n", (int)binary->nmax);
}