void three_params_second(int i, t_data *data, t_instruc *inst, int *order) { if (g_op[i].param_type[*order] == T_REG) { if (is_register(inst->trim, data, inst, order) == 1) return ; else error_line(data, "Incorrect input parameters"); } else if (g_op[i].param_type[*order] == (T_REG | T_DIR | T_IND)) { if (three_params_second_cut(data, inst, order) == 1) return ; } else if (g_op[i].param_type[*order] == (T_DIR | T_REG)) { if (is_direct(inst->trim, data, inst, order) == 1) return ; else if (is_register(inst->trim, data, inst, order) == 1) return ; else error_line(data, "Incorrect input parameters"); } else error_line(data, "Incorrect input parameters"); }
int get_symbol_address(const Symbol * sym, ContextAddress * addr) { SYMBOL_INFO * info = NULL; assert(sym->magic == SYMBOL_MAGIC); if (sym->address != 0) { *addr = sym->address; return 0; } if (sym->base || sym->info) { errno = ERR_INV_CONTEXT; return -1; } if (get_sym_info(sym, sym->index, &info) < 0) return -1; if (is_register(info)) { set_errno(ERR_INV_CONTEXT, "Register variable"); return -1; } *addr = (ContextAddress)info->Address; if (is_frame_relative(info)) { StackFrame * frame_info; int frame = sym->frame + STACK_NO_FRAME; if (get_frame_info(sym->ctx, frame, &frame_info) < 0) return -1; *addr += frame_info->fp - sizeof(ContextAddress) * 2; } return 0; }
bool token::is_register(registers _register) const { if (is_register()) { return get_register() == _register; } return false; }
int three_params_second_cut(t_data *data, t_instruc *inst, int *order) { if (is_register(inst->trim, data, inst, order) == 1) return (1); else if (is_direct(inst->trim, data, inst, order) == 1) return (1); else if (is_indirect(inst->trim, data, inst, order) == 1) return (1); else error_line(data, "Incorrect input parameters"); return (0); }
static void syminfo2symbol(Context * ctx, int frame, SYMBOL_INFO * info, Symbol * sym) { sym->module = info->ModBase; sym->index = info->Index; if (is_frame_relative(info) || is_register(info)) { assert(frame >= 0); assert(ctx != ctx->mem); sym->frame = frame - STACK_NO_FRAME; } else { assert(sym->frame == 0); ctx = ctx->mem; } sym->ctx = ctx; tag2symclass(sym, info->Tag); }
int get_symbol_register(const Symbol * sym, Context ** ctx, int * frame, RegisterDefinition ** reg) { RegisterDefinition * def = NULL; SYMBOL_INFO * info = NULL; assert(sym->magic == SYMBOL_MAGIC); if (sym->address != 0 || sym->base || sym->info) { errno = ERR_INV_CONTEXT; return -1; } if (get_sym_info(sym, sym->index, &info) < 0) return -1; if (!is_register(info)) { errno = ERR_INV_CONTEXT; return -1; } /* Register numbers are defined in cvconst.h */ if (info->Register < 40) { static const char * reg_names[] = { NULL, "AL", "CL", "DL", "BL", "AH", "CH", "DH", "BH", "AX", "CX", "DX", "BX", "SP", "BP", "SI", "DI", "EAX", "ECX", "EDX", "EBX", "ESP", "EBP", "ESI", "EDI", "ES", "CS", "SS", "DS", "FS", "GS", "IP", "FLAGS", "EIP", "EFLAGS", NULL, NULL, NULL, NULL, NULL, }; def = find_register(sym->ctx, reg_names[info->Register]); } else if (info->Register >= 328 && info->Register <= 343) { static const char * reg_names[] = { "RAX", "RBX", "RCX", "RDX", "RSI", "RDI", "RBP", "RSP", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15" }; def = find_register(sym->ctx, reg_names[info->Register - 328]); } if (def != NULL) { *ctx = sym->ctx; *frame = sym->frame + STACK_NO_FRAME; *reg = def; return 0; } errno = ERR_INV_CONTEXT; return -1; }
int my_st(t_corewar *core, t_champions *champions, t_instruction *instruction) { int value_to_store; int index; value_to_store = 0; if (is_good_register(instruction->params[0]) == 1) value_to_store = champions->reg[instruction->params[0]]; if (is_register(instruction->type, 2) == 1) { if (is_good_register(instruction->params[1]) == 1) champions->reg[instruction->params[1]] = value_to_store; } else if (is_indirect(instruction->type, 2) == 1) { index = champions->pc - 5 + (instruction->params[1] % IDX_MOD); write_arena_four(core, champions, value_to_store, index); } return (0); }
dcpu16operand parseoperand(list *labels) { dcpu16operand op = {0}; dcpu16token tok; op.addressing = IMMEDIATE; if ((tok = nexttoken()) == T_IDENTIFIER) { /* label */ op.type = LABEL; op.label = getnewlabel(labels, cur_tok.string)->label; } else if (tok == T_NUMBER) { /* numeric */ op.type = LITERAL; op.numeric = cur_tok.number; } else if (is_register(tok) || is_status_register(tok) || is_stack_operation(tok)) { /* register */ /* * For all intents and purposes, stack operations, registers and * status registers are equivalent in usage for immediate addressing, * so we might as well classify them all as REGISTER */ op.type = REGISTER; op.token = tok; } else if (tok == T_LBRACK) { /* [reference] */ dcpu16token a = nexttoken(); op.addressing = REFERENCE; if (a == T_NUMBER) { /* [numeric] */ op.type = LITERAL; op.numeric = cur_tok.number; } else if (is_register(a)) { /* [register] */ op.type = REGISTER; op.token = a; } else if (a == T_IDENTIFIER) { /* [label] */ op.type = LABEL; op.label = getnewlabel(labels, cur_tok.string)->label; } else { error("Expected numeric, register or label, got %s", toktostr(a)); } /* First part parsed correctly so far, now either ']' or '+' * follows */ if (((tok = nexttoken()) != T_RBRACK) && (tok != T_PLUS)) error("Expected '+' or ']', got %s", toktostr(tok)); if (tok == T_RBRACK) { /* [numeric], [register], [label] checks out */ return op; } else { dcpu16token b = nexttoken(); op.type = REGISTER_OFFSET; if (is_register(a)) { /* [register + label], [register + numeric] */ if (b == T_IDENTIFIER) { op.register_offset.type = LABEL; op.register_offset.register_index = a; op.register_offset.label = getnewlabel(labels, cur_tok.string)->label; } else if (b == T_NUMBER) { op.register_offset.type = LITERAL; op.register_offset.register_index = a; op.register_offset.offset = cur_tok.number; } else { error("Expected numeric or label, got %s", toktostr(b)); } } else { /* [numeric + register], [label + register] */ if (is_register(b)) { op.register_offset.register_index = b; if (a == T_NUMBER) { op.register_offset.type = LITERAL; op.register_offset.offset = cur_tok.number; } else { op.register_offset.type = LABEL; op.register_offset.label = getnewlabel(labels, cur_tok.string)->label; } } else { error("Expected register, got %s", toktostr(b)); } } if (nexttoken() != T_RBRACK) error("Expected ']'"); } } else { error("Expected register, label, numeric or '[', got %s", toktostr(tok)); } return op; }
int dispcmd(interpreteur inter, memory mem) { DEBUG_MSG("Chaine : %s", inter->input); char* token = NULL; if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("no argument given to command %s\n", "dispcmd"); return 1; } if (strcmp(token, "mem") == 0) { token = get_next_token(inter); if (token == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "dispcmd"); return 1; } while (token != NULL) { if (strcmp(token, "map") == 0) { if (get_next_token(inter) != NULL) { WARNING_MSG("too many arguments %s\n", "dispcmd"); return 1; } printf("Virtual memory map (8 segments)\n"); printf(".text \tr-x\tVaddr: %zx\tSize: %zd bytes\n", mem->txt->vaddr, mem->txt->size); printf(".data \trw-\tVaddr: %zx\tSize: %zd bytes\n", mem->data->vaddr, mem->data->size); printf(".bss \trw-\tVaddr: %zx\tSize: %zd bytes\n", mem->bss->vaddr, mem->bss->size); printf("[stack]\trw-\tVaddr: %zx\tSize: %zd bytes\n", mem->stack->vaddr, mem->stack->size); return 0; } char* tok_mem1 = token; char* tok_mem2 = get_next_token(inter); tok_mem2 = get_next_token(inter); if (is_adress(tok_mem1)) { if (tok_mem2 == NULL) { WARNING_MSG("second argument is not an adress range %s\n", "dispcmd"); return 1; } if (is_adress(tok_mem2)) { size_t adress1 = strtol(tok_mem1,NULL,16); size_t adress2 = strtol(tok_mem2,NULL,16); if ((adress2 - adress1) > 17) { WARNING_MSG("adress range must not contain more than 16 adresses %s\n","dispcmd"); return 1; } int j = 0; byte value; value = read_memory_value(adress1, mem); printf("%zx\t%02x ", adress1, value); for(j = 1; adress1 + j <= adress2; j++) { value = read_memory_value(adress1 + j, mem); printf("%02x ", value); } } else { WARNING_MSG("second argument is not an adress range %s\n","dispcmd"); return 1; } } printf("\n"); if((token = get_next_token(inter) == NULL)) return 0; } WARNING_MSG("first argument not a valid memory range or command%s\n", "dispcmd"); return 1; } if (strcmp(token, "reg") == 0) { if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "dispcmd"); return 1; } int i = 0; if (!strcmp(token, "all")) { for (i=0; i<=15; i++) { if (i%4 == 0) printf("\n"); printf("r%i: %02x\t", i, mem->reg[i]); } printf("apsr: %02x\t\n", mem->reg[reg_index("apsr")]); return 0; } while ((token != NULL) && !is_register(token)) { if (i%4 == 0) printf("\n"); printf("%s: %02x\t", token, mem->reg[reg_index(token)]); i++; token = get_next_token(inter); } if (token == NULL) { printf("\n"); return 0; } else { WARNING_MSG("argument not a valid register %s\n", "dispcmd"); return 1; } } else { WARNING_MSG("value %s is not a valid argument of command %s\n", token, "dispcmd"); return 1; } return 1; }
int assertcmd(interpreteur inter, memory mem) { DEBUG_MSG("Chaine : %s", inter->input); char* token = NULL; if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("no argument given to command %s\n", "assertcmd"); return 1; } if (strcmp(token, "reg") == 0) { if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "assertcmd"); return 1; } if (!is_register(token)) { char* token1; if((token1 = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if (get_type(token1) == INT) { if (atoi(token1) == mem->reg[reg_index(token)]) { printf("Ok\n"); return 0; } return 1; } else if (is_hexa(token1)) { if (strtol(token1, NULL, 16) == mem->reg[reg_index(token)]) { printf("Ok\n"); return 0; } return 1; } else { WARNING_MSG("second argument is not an integer %s\n", "assertcmd"); return 1; } } else { WARNING_MSG("first argument not a valid register%s\n", "assertcmd"); return 1; } } if (strcmp(token, "word") == 0) { if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "assertcmd"); return 1; } if (is_adress(token)) { char* token1; if ((token1 = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "assertcmd"); return 1; } word value; if (get_type(token1) == INT) value = atoi(token1); else if (is_hexa(token1)) value = strtol(token1, NULL, 16); else{ WARNING_MSG("second argument is not an integer %s\n", "assertcmd"); return 1; } byte bValue[4]; bValue[0] = (byte)((value & 0xff000000) >> 24); bValue[1] = (byte)((value & 0x00ff0000) >> 16); bValue[2] = (byte)((value & 0x0000ff00) >> 8); bValue[3] = (byte)((value & 0x000000ff)); size_t addr = strtol(token, NULL, 16); if (mem->endianness == LSB && bValue[0] == read_memory_value(addr, mem) && bValue[1] == read_memory_value(addr+1, mem) && bValue[2] == read_memory_value(addr+2, mem) && bValue[3] == read_memory_value(addr+3, mem)) { printf("Ok\n"); return 0; } else if (mem->endianness == MSB && bValue[0] == read_memory_value(addr+3, mem) && bValue[1] == read_memory_value(addr+2, mem) && bValue[2] == read_memory_value(addr+1, mem) && bValue[3] == read_memory_value(addr, mem)) { printf("Ok\n"); return 0; } return 1; } else { WARNING_MSG("first argument not a valid adress %s\n","assertcmd"); return 1; } }
int setcmd(interpreteur inter, memory mem) { DEBUG_MSG("Chaine : %s", inter->input); char* token = NULL; if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("no argument given to command %s\n", "setcmd"); return 1; } if (strcmp(token, "mem") == 0) { if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if (!strcmp(token, "byte")) { if((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if(!is_adress(token)) { WARNING_MSG("third argument is not an adress range %s\n", "setcmd"); return 1; } size_t adress = strtol(token, NULL, 16); if((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if(!is_hexa(token)) { WARNING_MSG("fourth argument is not an hex %s\n", "setcmd"); return 1; } byte value = strtol(token, NULL, 16); if(write_memory_value(adress, value, mem)) { WARNING_MSG("address is not valid %s\n", "setcmd"); return 1; } return 0; } if (!strcmp(token, "word")) { if((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if(!is_adress(token)) { WARNING_MSG("third argument is not an adress range %s\n", "setcmd"); return 1; } size_t adress = strtol(token, NULL, 16); if((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if(!is_hexa(token)) { WARNING_MSG("fourth argument is not an hex %s\n", "setcmd"); return 1; } word wValue = strtol(token, NULL, 16); byte bValue[4]; bValue[0] = (byte)((wValue & 0xff000000) >> 24); bValue[1] = (byte)((wValue & 0x00ff0000) >> 16); bValue[2] = (byte)((wValue & 0x0000ff00) >> 8); bValue[3] = (byte)((wValue & 0x000000ff)); int i; if (mem->endianness == LSB) { for (i=0;i<=3;i++) { if (write_memory_value(adress+i, bValue[i], mem)) { WARNING_MSG("address is not valid %s\n", "setcmd"); return 1; } } } else if (mem->endianness == MSB) { for (i=0;i<=3;i++) { if (write_memory_value(adress+i, bValue[3-i], mem)) { WARNING_MSG("address is not valid %s\n", "setcmd"); return 1; } } } else ERROR_MSG("Endianness invaid\n"); return 0; } } if (strcmp(token, "reg") == 0) { if ((token = get_next_token(inter)) == NULL) { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } if (!is_register(token)) { char* value; if ((value = get_next_token(inter)) != NULL) { if (get_type(value) == INT) mem->reg[reg_index(token)] = atoi(value); else if (is_hexa(value)) mem->reg[reg_index(token)] = strtol(value, NULL, 16); else { WARNING_MSG("value is not an integer %s\n", "setcmd"); return 1; } return 0; } else { WARNING_MSG("not enough arguments given to command %s\n", "setcmd"); return 1; } } else { WARNING_MSG("argument not a valid register%s\n", "setcmd"); return 1; } } else { WARNING_MSG("value %s is not a valid argument of command %s\n", token, "setcmd"); return 1; } return 1; }