Separated address expression parsing.

This commit is contained in:
Daniel Beer 2010-05-01 17:54:10 +12:00
parent 99b53f475b
commit 2794165dee
10 changed files with 342 additions and 284 deletions

View File

@ -41,7 +41,7 @@ install: mspdebug mspdebug.man
mspdebug: main.o fet.o rf2500.o dis.o uif.o ihex.o elf32.o stab.o util.o \
bsl.o sim.o symmap.o gdb.o btree.o device.o rtools.o sym.o devcmd.o \
cproc.o vector.o cproc_util.o
cproc.o vector.o cproc_util.o expr.o
$(CC) $(LDFLAGS) -o $@ $^ -lusb $(READLINE_LIBS)
.c.o:

View File

@ -28,6 +28,7 @@
#include <readline/history.h>
#endif
#include "expr.h"
#include "cproc.h"
#include "vector.h"
#include "stab.h"
@ -207,7 +208,7 @@ static int parse_option(struct cproc_option *o, const char *word)
break;
case CPROC_OPTION_NUMERIC:
return stab_exp(word, &o->data.numeric);
return expr_eval(word, &o->data.numeric);
case CPROC_OPTION_STRING:
strncpy(o->data.text, word, sizeof(o->data.text));

View File

@ -25,6 +25,7 @@
#include "device.h"
#include "binfile.h"
#include "stab.h"
#include "expr.h"
#include "cproc.h"
#include "cproc_util.h"
#include "util.h"
@ -61,13 +62,13 @@ static int cmd_md(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(off_text, &offset) < 0) {
if (expr_eval(off_text, &offset) < 0) {
fprintf(stderr, "md: can't parse offset: %s\n", off_text);
return -1;
}
if (len_text) {
if (stab_exp(len_text, &length) < 0) {
if (expr_eval(len_text, &length) < 0) {
fprintf(stderr, "md: can't parse length: %s\n",
len_text);
return -1;
@ -110,7 +111,7 @@ static int cmd_mw(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(off_text, &offset) < 0) {
if (expr_eval(off_text, &offset) < 0) {
fprintf(stderr, "md: can't parse offset: %s\n", off_text);
return -1;
}
@ -182,7 +183,7 @@ static int cmd_run(cproc_t cp, char **arg)
device_status_t status;
if (bp_text) {
if (stab_exp(bp_text, &bp_addr) < 0) {
if (expr_eval(bp_text, &bp_addr) < 0) {
fprintf(stderr, "run: can't parse breakpoint: %s\n",
bp_text);
return -1;
@ -236,7 +237,7 @@ static int cmd_set(cproc_t cp, char **arg)
reg_text++;
reg = atoi(reg_text);
if (stab_exp(val_text, &value) < 0) {
if (expr_eval(val_text, &value) < 0) {
fprintf(stderr, "set: can't parse value: %s\n", val_text);
return -1;
}
@ -270,13 +271,13 @@ static int cmd_dis(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(off_text, &offset) < 0) {
if (expr_eval(off_text, &offset) < 0) {
fprintf(stderr, "dis: can't parse offset: %s\n", off_text);
return -1;
}
if (len_text) {
if (stab_exp(len_text, &length) < 0) {
if (expr_eval(len_text, &length) < 0) {
fprintf(stderr, "dis: can't parse length: %s\n",
len_text);
return -1;
@ -395,8 +396,8 @@ static int cmd_hexout(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(off_text, &off) < 0 ||
stab_exp(len_text, &length) < 0)
if (expr_eval(off_text, &off) < 0 ||
expr_eval(len_text, &length) < 0)
return -1;
if (hexout_start(&hexout, filename) < 0)

291
expr.c Normal file
View File

@ -0,0 +1,291 @@
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <regex.h>
#include <errno.h>
#include <assert.h>
#include "expr.h"
#include "stab.h"
#include "util.h"
/************************************************************************
* Address expression parsing.
*/
struct addr_exp_state {
int last_operator;
int data_stack[32];
int data_stack_size;
int op_stack[32];
int op_stack_size;
};
static int addr_exp_data(struct addr_exp_state *s, const char *text)
{
int value;
if (!s->last_operator || s->last_operator == ')') {
fprintf(stderr, "syntax error at token %s\n", text);
return -1;
}
/* Hex value */
if (*text == '0' && text[1] == 'x')
value = strtoul(text + 2, NULL, 16);
else if (isdigit(*text))
value = atoi(text);
else if (stab_get(text, &value) < 0) {
fprintf(stderr, "can't parse token: %s\n", text);
return -1;
}
if (s->data_stack_size + 1 > ARRAY_LEN(s->data_stack)) {
fprintf(stderr, "data stack overflow at token %s\n", text);
return -1;
}
s->data_stack[s->data_stack_size++] = value;
s->last_operator = 0;
return 0;
}
static int addr_exp_pop(struct addr_exp_state *s)
{
char op = s->op_stack[--s->op_stack_size];
int data1 = s->data_stack[--s->data_stack_size];
int data2 = 0;
int result = 0;
if (op != 'N')
data2 = s->data_stack[--s->data_stack_size];
assert (s->op_stack_size >= 0);
assert (s->data_stack_size >= 0);
switch (op) {
case '+':
result = data2 + data1;
break;
case '-':
result = data2 - data1;
break;
case '*':
result = data2 * data1;
break;
case '/':
if (!data1)
goto divzero;
result = data2 / data1;
break;
case '%':
if (!data1)
goto divzero;
result = data2 % data1;
break;
case 'N':
result = -data1;
break;
}
s->data_stack[s->data_stack_size++] = result;
return 0;
divzero:
fprintf(stderr, "divide by zero\n");
return -1;
}
static int can_push(struct addr_exp_state *s, char op)
{
char top;
if (!s->op_stack_size || op == '(')
return 1;
top = s->op_stack[s->op_stack_size - 1];
if (top == '(')
return 1;
switch (op) {
case 'N':
return 1;
case '*':
case '%':
case '/':
return top == '+' || top == '-';
default:
break;
}
return 0;
}
static int addr_exp_op(struct addr_exp_state *s, char op)
{
if (op == '(') {
if (!s->last_operator || s->last_operator == ')')
goto syntax_error;
} else if (op == '-') {
if (s->last_operator && s->last_operator != ')')
op = 'N';
} else {
if (s->last_operator && s->last_operator != ')')
goto syntax_error;
}
if (op == ')') {
/* ) collapses the stack to the last matching ( */
while (s->op_stack_size &&
s->op_stack[s->op_stack_size - 1] != '(')
if (addr_exp_pop(s) < 0)
return -1;
if (!s->op_stack_size) {
fprintf(stderr, "parenthesis mismatch: )\n");
return -1;
}
s->op_stack_size--;
} else {
while (!can_push(s, op))
if (addr_exp_pop(s) < 0)
return -1;
if (s->op_stack_size + 1 > ARRAY_LEN(s->op_stack)) {
fprintf(stderr, "operator stack overflow: %c\n", op);
return -1;
}
s->op_stack[s->op_stack_size++] = op;
}
s->last_operator = op;
return 0;
syntax_error:
fprintf(stderr, "syntax error at operator %c\n", op);
return -1;
}
static int addr_exp_finish(struct addr_exp_state *s, int *ret)
{
if (s->last_operator && s->last_operator != ')') {
fprintf(stderr, "syntax error at end of expression\n");
return -1;
}
while (s->op_stack_size) {
if (s->op_stack[s->op_stack_size - 1] == '(') {
fprintf(stderr, "parenthesis mismatch: (\n");
return -1;
}
if (addr_exp_pop(s) < 0)
return -1;
}
if (s->data_stack_size != 1) {
fprintf(stderr, "no data: stack size is %d\n",
s->data_stack_size);
return -1;
}
if (ret)
*ret = s->data_stack[0];
return 0;
}
int expr_eval(const char *text, int *addr)
{
const char *text_save = text;
int last_cc = 1;
char token_buf[64];
int token_len = 0;
struct addr_exp_state s = {0};
s.last_operator = '(';
for (;;) {
int cc;
/* Figure out what class this character is */
if (*text == '+' || *text == '-' ||
*text == '*' || *text == '/' ||
*text == '%' || *text == '(' ||
*text == ')')
cc = 1;
else if (!*text || isspace(*text))
cc = 2;
else if (isalnum(*text) || *text == '.' || *text == '_' ||
*text == '$' || *text == ':')
cc = 3;
else {
fprintf(stderr, "illegal character in expression: %c\n",
*text);
return -1;
}
/* Accumulate and process token text */
if (cc == 3) {
if (token_len + 1 < sizeof(token_buf))
token_buf[token_len++] = *text;
} else if (token_len) {
token_buf[token_len] = 0;
token_len = 0;
if (addr_exp_data(&s, token_buf) < 0)
goto fail;
}
/* Process operators */
if (cc == 1) {
if (addr_exp_op(&s, *text) < 0)
goto fail;
}
if (!*text)
break;
last_cc = cc;
text++;
}
if (addr_exp_finish(&s, addr) < 0)
goto fail;
return 0;
fail:
fprintf(stderr, "bad address expression: %s\n", text_save);
return -1;
}

29
expr.h Normal file
View File

@ -0,0 +1,29 @@
/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef EXPR_H_
#define EXPR_H_
#include <sys/types.h>
/* Parse an address expression, storing the result in the integer
* pointed to. Returns 0 if parsed successfully, -1 if not.
*/
int expr_eval(const char *text, int *value);
#endif

3
main.c
View File

@ -32,6 +32,7 @@
#include "rtools.h"
#include "sym.h"
#include "devcmd.h"
#include "expr.h"
#include "sim.h"
#include "bsl.h"
@ -87,7 +88,7 @@ static int fetch_io(void *user_data, u_int16_t pc,
if (!len)
return 0;
if (!stab_exp(text, &data)) {
if (!expr_eval(text, &data)) {
if (data_ret)
*data_ret = data;
return 0;

View File

@ -27,6 +27,7 @@
#include "dis.h"
#include "rtools.h"
#include "stab.h"
#include "expr.h"
#include "cproc_util.h"
#define ISEARCH_OPCODE 0x0001
@ -133,7 +134,7 @@ static int isearch_addr(const char *term, char **arg,
return -1;
}
if (stab_exp(addr_text, &addr) < 0)
if (expr_eval(addr_text, &addr) < 0)
return -1;
q->flags |= which;
@ -390,8 +391,8 @@ static int cmd_isearch(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(addr_text, &addr) < 0 ||
stab_exp(len_text, &len) < 0)
if (expr_eval(addr_text, &addr) < 0 ||
expr_eval(len_text, &len) < 0)
return -1;
q.flags = 0;

262
stab.c
View File

@ -228,265 +228,3 @@ void stab_exit(void)
btree_free(sym_table);
btree_free(addr_table);
}
/************************************************************************
* Address expression parsing.
*/
struct addr_exp_state {
int last_operator;
int data_stack[32];
int data_stack_size;
int op_stack[32];
int op_stack_size;
};
static int addr_exp_data(struct addr_exp_state *s, const char *text)
{
int value;
if (!s->last_operator || s->last_operator == ')') {
fprintf(stderr, "syntax error at token %s\n", text);
return -1;
}
/* Hex value */
if (*text == '0' && text[1] == 'x')
value = strtoul(text + 2, NULL, 16);
else if (isdigit(*text))
value = atoi(text);
else if (stab_get(text, &value) < 0) {
fprintf(stderr, "can't parse token: %s\n", text);
return -1;
}
if (s->data_stack_size + 1 > ARRAY_LEN(s->data_stack)) {
fprintf(stderr, "data stack overflow at token %s\n", text);
return -1;
}
s->data_stack[s->data_stack_size++] = value;
s->last_operator = 0;
return 0;
}
static int addr_exp_pop(struct addr_exp_state *s)
{
char op = s->op_stack[--s->op_stack_size];
int data1 = s->data_stack[--s->data_stack_size];
int data2 = 0;
int result = 0;
if (op != 'N')
data2 = s->data_stack[--s->data_stack_size];
assert (s->op_stack_size >= 0);
assert (s->data_stack_size >= 0);
switch (op) {
case '+':
result = data2 + data1;
break;
case '-':
result = data2 - data1;
break;
case '*':
result = data2 * data1;
break;
case '/':
if (!data1)
goto divzero;
result = data2 / data1;
break;
case '%':
if (!data1)
goto divzero;
result = data2 % data1;
break;
case 'N':
result = -data1;
break;
}
s->data_stack[s->data_stack_size++] = result;
return 0;
divzero:
fprintf(stderr, "divide by zero\n");
return -1;
}
static int can_push(struct addr_exp_state *s, char op)
{
char top;
if (!s->op_stack_size || op == '(')
return 1;
top = s->op_stack[s->op_stack_size - 1];
if (top == '(')
return 1;
switch (op) {
case 'N':
return 1;
case '*':
case '%':
case '/':
return top == '+' || top == '-';
default:
break;
}
return 0;
}
static int addr_exp_op(struct addr_exp_state *s, char op)
{
if (op == '(') {
if (!s->last_operator || s->last_operator == ')')
goto syntax_error;
} else if (op == '-') {
if (s->last_operator && s->last_operator != ')')
op = 'N';
} else {
if (s->last_operator && s->last_operator != ')')
goto syntax_error;
}
if (op == ')') {
/* ) collapses the stack to the last matching ( */
while (s->op_stack_size &&
s->op_stack[s->op_stack_size - 1] != '(')
if (addr_exp_pop(s) < 0)
return -1;
if (!s->op_stack_size) {
fprintf(stderr, "parenthesis mismatch: )\n");
return -1;
}
s->op_stack_size--;
} else {
while (!can_push(s, op))
if (addr_exp_pop(s) < 0)
return -1;
if (s->op_stack_size + 1 > ARRAY_LEN(s->op_stack)) {
fprintf(stderr, "operator stack overflow: %c\n", op);
return -1;
}
s->op_stack[s->op_stack_size++] = op;
}
s->last_operator = op;
return 0;
syntax_error:
fprintf(stderr, "syntax error at operator %c\n", op);
return -1;
}
static int addr_exp_finish(struct addr_exp_state *s, int *ret)
{
if (s->last_operator && s->last_operator != ')') {
fprintf(stderr, "syntax error at end of expression\n");
return -1;
}
while (s->op_stack_size) {
if (s->op_stack[s->op_stack_size - 1] == '(') {
fprintf(stderr, "parenthesis mismatch: (\n");
return -1;
}
if (addr_exp_pop(s) < 0)
return -1;
}
if (s->data_stack_size != 1) {
fprintf(stderr, "no data: stack size is %d\n",
s->data_stack_size);
return -1;
}
if (ret)
*ret = s->data_stack[0];
return 0;
}
int stab_exp(const char *text, int *addr)
{
const char *text_save = text;
int last_cc = 1;
char token_buf[64];
int token_len = 0;
struct addr_exp_state s = {0};
s.last_operator = '(';
for (;;) {
int cc;
/* Figure out what class this character is */
if (*text == '+' || *text == '-' ||
*text == '*' || *text == '/' ||
*text == '%' || *text == '(' ||
*text == ')')
cc = 1;
else if (!*text || isspace(*text))
cc = 2;
else if (isalnum(*text) || *text == '.' || *text == '_' ||
*text == '$' || *text == ':')
cc = 3;
else {
fprintf(stderr, "illegal character in expression: %c\n",
*text);
return -1;
}
/* Accumulate and process token text */
if (cc == 3) {
if (token_len + 1 < sizeof(token_buf))
token_buf[token_len++] = *text;
} else if (token_len) {
token_buf[token_len] = 0;
token_len = 0;
if (addr_exp_data(&s, token_buf) < 0)
goto fail;
}
/* Process operators */
if (cc == 1) {
if (addr_exp_op(&s, *text) < 0)
goto fail;
}
if (!*text)
break;
last_cc = cc;
text++;
}
if (addr_exp_finish(&s, addr) < 0)
goto fail;
return 0;
fail:
fprintf(stderr, "bad address expression: %s\n", text_save);
return -1;
}

5
stab.h
View File

@ -57,9 +57,4 @@ typedef int (*stab_callback_t)(void *user_data,
int stab_enum(stab_callback_t cb, void *user_data);
/* Parse an address expression, storing the result in the integer
* pointed to. Returns 0 if parsed successfully, -1 if not.
*/
int stab_exp(const char *text, int *value);
#endif

5
sym.c
View File

@ -24,6 +24,7 @@
#include <regex.h>
#include <errno.h>
#include "stab.h"
#include "expr.h"
#include "binfile.h"
#include "util.h"
#include "vector.h"
@ -35,7 +36,7 @@ static int cmd_eval(cproc_t cp, char **arg)
u_int16_t offset;
char name[64];
if (stab_exp(*arg, &addr) < 0) {
if (expr_eval(*arg, &addr) < 0) {
fprintf(stderr, "=: can't parse: %s\n", *arg);
return -1;
}
@ -321,7 +322,7 @@ static int cmd_sym(cproc_t cp, char **arg)
return -1;
}
if (stab_exp(val_text, &value) < 0) {
if (expr_eval(val_text, &value) < 0) {
fprintf(stderr, "sym: can't parse value: %s\n",
val_text);
return -1;