bool token::is_instruction(opcodes opcode) const { if (is_instruction()) { return get_instruction().opcode == opcode; } return false; }
/* Finds a node in the linked list of source lines, returning NULL if it is not present Multiple source lines may have this addr, but only ones with instructions and/or .long declarations are returned, not the label name */ SourceLine *find_source_line(uint16 addr) { SourceLine *cur = source_lines; while (cur != NULL) { if (cur->addr == addr && (is_instruction(cur) || strncmp(cur->line, ".long", 5) == 0)) return cur; cur = cur->next; } return NULL; }
static void set_champ_labels(t_labels **labels, char **file_content) { int cur_case; cur_case = -1; while (file_content[++cur_case]) { if (is_instruction(file_content[cur_case]) != -1 && is_label(file_content[cur_case]) != -1 && !(*labels)) add_label(labels, "."); if (is_label(file_content[cur_case]) != -1) add_label(labels, get_label(file_content[cur_case])); } }
static void set_labels_instruction(t_labels **labels, char **file_content) { int cur_case; t_labels *labls; cur_case = -1; labls = *labels; while (file_content[++cur_case]) { if (is_label(file_content[cur_case]) != -1) labls = search_in_labels(labls, get_label(file_content[cur_case])); if (is_instruction(file_content[cur_case]) != -1) add_instruction(&(labls->lst_cmd), get_name_instruction(file_content[cur_case]), get_params(get_instruction(file_content[cur_case]))); } }
void parseline(list *instructions, list *labels) { start: ;; dcpu16token tok = nexttoken(); if (tok == T_COLON) { /* Label definition */ if ((tok = nexttoken()) == T_IDENTIFIER) { dcpu16label *l = getnewlabel(labels, cur_tok.string); if (l->defined) error("Redefinition of label '%s' (%04X -> %04X) forbidden", l->label, l->pc, pc); l->pc = pc; l->defined = 1; goto start; } else { error("Expected label, got %s", toktostr(tok)); } } else if (is_instruction(tok)) { dcpu16instruction instr, *newinstr; instr.opcode = tok; instr.a = parseoperand(labels); if (!is_nonbasic_instruction(tok)) { if ((tok = nexttoken()) == T_COMMA) { instr.b = parseoperand(labels); } else { error("Expected ',', got %s", toktostr(tok)); } } if ((tok = nexttoken()) != T_NEWLINE) error("Expected EOL, got %s", toktostr(tok)); /* * All tokens valid, store instructions, advance PC */ newinstr = malloc(sizeof(dcpu16instruction)); memcpy(newinstr, &instr, sizeof(dcpu16instruction)); newinstr->pc = pc; newinstr->line = curline; list_push_back(instructions, newinstr); pc += instruction_length(newinstr); } else if (is_macro(tok)) { if (tok == T_ORG) { if ((tok = nexttoken()) == T_NUMBER) { if (flag_paranoid && (cur_tok.number < pc)) { warning("new origin precedes old origin! " "This could cause already written code to be " "overriden."); } pc = cur_tok.number; } else { error("Expected numeric, got %s", toktostr(tok)); } } else if (tok == T_DAT) { /* All of the stuff we are about to read, neatly packed * into words */ list_node *p = NULL; list *data = list_create(); do { if ((tok = nexttoken()) == T_STRING) { char *ptr = cur_tok.string; while (*ptr != '\0') { uint16_t *dat = malloc(sizeof(uint16_t)); *dat = *ptr++; list_push_back(data, dat); } } else if (tok == T_NUMBER) { uint16_t *dat = malloc(sizeof(uint16_t)); *dat = cur_tok.number; list_push_back(data, dat); } else { error("Expected string or numeric, got %s", toktostr(tok)); } if ((tok = nexttoken()) == T_COMMA) continue; else if (tok == T_NEWLINE) break; else error("Expected ',' or EOL, got %s", toktostr(tok)); } while (1); /* ram[pc++] = cur_tok.number; */ for (p = list_get_root(data); p != NULL; p = p->next) { ram[pc++] = *((uint16_t*)p->data); } list_dispose(&data, &free); } } else if (tok == T_NEWLINE) { return; } else { error("Expected label-definition or opcode, got %s", toktostr(tok)); } }
int is_nonbasic_instruction(dcpu16token t) { return is_instruction(t) && (t == T_JSR); }