static void handle_text(char *s) { char *opstart = s; operand *op; dblock *db = NULL; if (db = parse_string(&opstart,*s,8)) { add_atom(0,new_data_atom(db,1)); s = opstart; } if (!db) { op = new_operand(); s = skip_operand(s); if (parse_operand(opstart,s-opstart,op,DATA_OPERAND(8))) { atom *a; a = new_datadef_atom(8,op); a->align = 1; add_atom(0,a); } else syntax_error(8); /* invalid data operand */ } eol(s); }
static void handle_datadef(char *s,int sz) { for (;;) { char *opstart = s; operand *op; dblock *db = NULL; if (OPSZ_BITS(sz)==8 && (*s=='\"' || *s=='\'')) { if (db = parse_string(&opstart,*s,8)) { add_atom(0,new_data_atom(db,1)); s = opstart; } } if (!db) { op = new_operand(); s = skip_operand(s); if (parse_operand(opstart,s-opstart,op,DATA_OPERAND(sz))) add_atom(0,new_datadef_atom(OPSZ_BITS(sz),op)); else syntax_error(8); /* invalid data operand */ } s = skip(s); if (*s == ',') s = skip(s+1); else break; } }
static void handle_data_offset(char *s,int size,int offset) { for (;;) { char *opstart = s; operand *op; dblock *db = NULL; if (size==8 && (*s=='\"' || *s=='\'')) { if (db = parse_string(&opstart,*s,8)) { if (offset != 0) { int i; for (i=0; i<db->size; i++) db->data[i] = db->data[i] + offset; } add_atom(0,new_data_atom(db,1)); s = opstart; } } if (!db) { op = new_operand(); s = skip_operand(s); if (parse_operand(opstart,s-opstart,op,DATA_OPERAND(size))) { atom *a; if (offset != 0) op->value = make_expr(ADD,number_expr(offset),op->value); a = new_datadef_atom(abs(size),op); a->align = 1; add_atom(0,a); } else syntax_error(8); /* invalid data operand */ } s = skip(s); if (*s == ',') { s = skip(s+1); } else if (*s == commentchar) { break; } else if (*s) { syntax_error(9); /* , expected */ return; } else break; } eol(s); }
static void stab_entry(char *name,int type,int othr,int desc,char *s) { section *stabs; if (!(stabs = find_section(stabname,stabattr))) { section *str; dblock *db; stabs = new_section(stabname,stabattr,4); if (!(str = find_section(stabstrname,stabstrattr))) { str = new_section(stabstrname,stabstrattr,1); } else { if (str->pc != 0) ierror(0); } /* first byte of .stabstr is 0 */ add_atom(str,new_space_atom(number_expr(1),1,0)); /* compilation unit header has to be patched by output module */ new_stabstr(getfilename()); db = new_dblock(); db->size = 12; db->data = mymalloc(12); add_atom(stabs,new_data_atom(db,1)); } add_const_datadef(stabs,name?new_stabstr(name):0,32,1); add_const_datadef(stabs,type,8,1); add_const_datadef(stabs,othr,8,1); add_const_datadef(stabs,desc,16,1); if (s) { operand *op = new_operand(); int len = oplen(skip_operand(s),s); if (parse_operand(s,len,op,DATA_OPERAND(32))) { atom *a = new_datadef_atom(32,op); a->align = 1; add_atom(stabs,a); } else syntax_error(8); } else add_atom(stabs,new_space_atom(number_expr(4),1,0)); /* no value */ }
static void handle_data(char *s,int size,int noalign) { for (;;) { char *opstart = s; operand *op; dblock *db = NULL; if ((size==8 || size==16) && *s=='\"') { if (db = parse_string(&opstart,*s,size)) { add_atom(0,new_data_atom(db,1)); s = opstart; } } if (!db) { op = new_operand(); s = skip_operand(s); if (parse_operand(opstart,s-opstart,op,DATA_OPERAND(size))) { atom *a; a = new_datadef_atom(size,op); if (noalign) a->align=1; add_atom(0,a); } else syntax_error(8); /* invalid data operand */ } s = skip(s); if (*s == ',') { s = skip(s+1); } else if (*s==commentchar) break; else if (*s) { syntax_error(9); /* , expected */ return; } else break; } eol(s); }
static void add_const_datadef(section *s,taddr val,int size,int align) { char buf[32]; int len; operand *op; if (size <= 32) { len = sprintf(buf,"%ld",(long)val); op = new_operand(); if (parse_operand(buf,len,op,DATA_OPERAND(size))) { atom *a = new_datadef_atom(size,op); a->align = align; add_atom(s,a); } else syntax_error(8); } else ierror(0); }
static void handle_data(char *s,int size,int noalign,int zeroterm) { expr *tree; dblock *db; do{ char *opstart=s; operand *op; if(size==8&&*s=='\"'){ s=string(s,&db); add_atom(0,new_data_atom(db,1)); }else{ op=new_operand(); s=skip_operand(s); if(!parse_operand(opstart,s-opstart,op,DATA_OPERAND(size))){ syntax_error(8); }else{ atom *a=new_datadef_atom(size,op); if(noalign) a->align=1; add_atom(0,a); } } s=skip(s); if(*s==','){ s=skip(s+1); }else if(*s) syntax_error(9); }while(*s); if(zeroterm){ if(size!=8) ierror(0); db=new_dblock(); db->size=1; db->data=mymalloc(1); *db->data=0; add_atom(0,new_data_atom(db,1)); } eol(s); }
void process_instruction(char *line) { const char *p = strchr(line, ' '); char inst[32]; char param[32]; memset(&inst,0,sizeof(inst)); memset(¶m,0,sizeof(param)); int end = strlen(line); if(p) { end = p - line; } strncpy(inst, line, end); EShaderInstruction instruction = find_instruction_by_name(inst); EOperandInfo oper; memset(&oper,0,sizeof(oper)); printf("Inst: %s\n", inst); char operandtxt[128]; int idx = 1; EOperandInfo operand; memset(&operand,0,sizeof(operand)); InstructionModifiers modifiers; memset(&modifiers, 0, sizeof(modifiers)); parse_result_modifiers(line, &modifiers); while(true) { memset(&operandtxt,0,sizeof(operand)); find_nth((char *)line, idx++, operandtxt, sizeof(operand)); if(strlen(operandtxt) < 1 || idx-1 > MAX_OPERANDS) break; parse_operand(operandtxt, &operand, idx-2, instruction); printf("oper: %s\n", operandtxt); } write_instruction(instruction, &operand, &modifiers); }
int main(int argc, char* argv[]) { Registers registers; Decoder decoder; Reader reader; int memory[1001]; init_values(®isters, memory); read_lines(&reader, argv[1]); while(registers.regs[eip] != 0) { char* line; line = fetch_instruction(®isters, &reader); parse(®isters, &decoder, line, memory); parse_operand(®isters, &decoder, memory); printf(" eip: %*i", 3, registers.regs[eip]); printf(" eax: %*i", 3, registers.regs[eax]); printf(" ebp: %*i", 3, registers.regs[ebp]); printf(" esp: %i\n", registers.regs[esp]); } // while () return 0; } // main()
uint16_t parse_expression(debugger_state_t *state, const char *string) { uint16_t value_stack[20]; int value_stack_pos = 0; char operator_stack[20]; int operator_stack_pos = 0; while (isspace(*string)) { string++; } while (*string != 0) { if (strchr("+-*/%(){}", *string)) { int op = precedence(*string); if (op == -2 || op == -4) { // left parenthesis operator_stack[operator_stack_pos++] = *string; } else if (op == -3) { // right parenthesis while (operator_stack_pos > 0) { char ch = operator_stack[operator_stack_pos - 1]; int prec = precedence(ch); if (prec != -2 && value_stack_pos < 2) { state->print(state, "ERROR: Missing values!\n"); } else if (prec != -2 && value_stack_pos > 1) { uint16_t first = value_stack[value_stack_pos - 1]; uint16_t second = value_stack[value_stack_pos - 2]; value_stack_pos -= 2; // popped values value_stack[value_stack_pos++] = run_operator(ch, first, second); operator_stack_pos--; } else { operator_stack_pos--; break; } } } else if (op == -5) { // right dereference while (operator_stack_pos > 0) { char ch = operator_stack[operator_stack_pos - 1]; int prec = precedence(ch); if (prec != -4 && value_stack_pos < 2) { state->print(state, "ERROR: Missing values!\n"); } else if (prec != -2 && value_stack_pos > 1) { uint16_t first = value_stack[value_stack_pos - 1]; uint16_t second = value_stack[value_stack_pos - 2]; value_stack_pos -= 2; // popped values value_stack[value_stack_pos++] = run_operator(ch, first, second); operator_stack_pos--; } else { operator_stack_pos--; break; } } if (value_stack_pos < 1) { state->print(state, "ERROR: Dereferencing failed!\n"); } else { uint16_t memory = value_stack[value_stack_pos - 1]; value_stack[value_stack_pos - 1] = state->asic->cpu->read_byte(state->asic->cpu->memory, memory); } } else { while (operator_stack_pos > 0) { char ch = operator_stack[operator_stack_pos - 1]; int prec = precedence(ch); if (prec > op && value_stack_pos < 2) { state->print(state, "ERROR: Missing values!\n"); } else if (prec > op) { uint16_t first = value_stack[value_stack_pos - 1]; uint16_t second = value_stack[value_stack_pos - 2]; value_stack_pos -= 2; // popped values value_stack[value_stack_pos++] = run_operator(ch, first, second); operator_stack_pos--; } else { break; } } operator_stack[operator_stack_pos++] = *string; } string++; } else { value_stack[value_stack_pos++] = parse_operand(state, string, &string); if (string == 0) { return 0; } } while (isspace(*string)) { string++; } } // print_stack(value_stack, value_stack_pos, operator_stack, operator_stack_pos); while (operator_stack_pos > 0) { char ch = operator_stack[operator_stack_pos - 1]; int prec = precedence(ch); if(prec == -2) { state->print(state, "ERROR: Mismatched parentheses!\n"); return 0; } else if(prec == -4) { state->print(state, "ERROR: Mismatched dereference!\n"); return 0; } else if(value_stack_pos < 2) { state->print(state, "ERROR: Missing values!\n"); return 0; } else { uint16_t first = value_stack[value_stack_pos - 1]; uint16_t second = value_stack[value_stack_pos - 2]; value_stack_pos -= 2; // popped values value_stack[value_stack_pos++] = run_operator(ch, first, second); operator_stack_pos--; } } // print_stack(value_stack, value_stack_pos, operator_stack, operator_stack_pos); if (value_stack_pos > 0) { return value_stack[value_stack_pos - 1]; } else { return 0; } }
/* >> TOK_KEYWORD '=' operand */ static int parse_assignment (section_t **section,scan_t *sc,char **lookahead,int why) { int result; char *keyword; stmt_t *stmt = (*section)->stmt; /* * wtf is lookahead and why don't you parse TOK_KEYWORD '=' when it's not * NULL, I hear you say (; * * The reason for this hack is because if we parse a section (i.e. not the main * section), we've already looked at the keyword and the '=' sign (it's the only * way to distinguish between an array of enumerations and a section, so we need * to pass this information along and make this one exception. * * There is another instance where we need to peek at the next token and that is * to look for the '}' at the end of nested sections. We use why to distinguish * between the two cases. * * See parse_array_or_section() for more details. */ if (lookahead == NULL) { SCAN(); if (!result) return (FIN); if (result != TOK_KEYWORD) { parse_error (sc,"TOK_KEYWORD",result); return (ERR); } keyword = sc->token.keyword; } else keyword = *lookahead; if (stmt != NULL) do { if (!strcmp (stmt->name,keyword)) { log_printf (LOG_ERROR,"Parse error on line %d: Duplicate keyword: %s\n",sc->line,keyword); mem_free (keyword); return (ERR); } stmt = stmt->next; } while (stmt != (*section)->stmt); if (lookahead == NULL || why != SECTION_START) { /* we can't use SCAN() here since we need to free keyword */ if ((result = scan (sc)) < 0) { mem_free (keyword); return (ERR); } if (result != '=') { parse_error (sc,"'='",result); mem_free (keyword); return (ERR); } } result = parse_operand (section,sc,&keyword); if (result != OK) return (ERR); return (result); }
int parse_expression(char *expr) { if (strlen(strtrim(expr)) == 0) return BREAK; char *token; static const char *operators = "+/*-%^"; static const char *mfuncs = "|ln|sqrt|sin|cos|tan|asin|acos|atan|"; static const char *constants = "|e|pi|"; double operand; char *ptr; while ((token = strsep(&expr, " \n"))) { if (strlen(token) == 0) continue; if (*token == ':') /* precision specified */ parse_precision(++token); else if (strchr(operators, *token) && strlen(token) == 1) { /* operator */ if (stackptr - opstack < 2) { fprintf(stderr, "!! Malformed expression -- too few operands.\n"); return CONTINUE; } if (parse_operator(*token) > 0) { return CONTINUE; } } else if ((ptr = strstr(mfuncs, strlower(token))) != NULL && *(ptr - 1) == '|' && *(ptr + strlen(token)) == '|') { /* validated trig function */ if (stackptr - opstack < 1) { fprintf(stderr, "!! Malformed expression -- too few operands.\n"); return CONTINUE; } parse_mfunc(token); } else if ((ptr = strstr(constants, strlower(token))) != NULL && *(ptr - 1) == '|' && *(ptr + strlen(token)) == '|') { /* validated constant */ parse_constant(token); } else { /* it's an operand, or it's bad input */ if (parse_operand(token, &operand) > 0) /* parsing failed on bad input */ return CONTINUE; if (stackptr == &opstack[STACK_SIZE]) { /* stack overflow */ fprintf(stderr, "!! Stack overflow. Expression too large.\n"); return CONTINUE; } *stackptr++ = operand; } } if (stackptr - opstack > 1) fprintf(stderr, "!! Malformed expression -- too many operands.\n"); else if (stackptr - opstack == 1) { if (verbose >= 1) printf(" = %.*f\n", precision, *--stackptr); else printf("%.*f\n", precision, *--stackptr); } return CONTINUE; }