1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
|
/* expression.c: A numeric expression
Copyright (c) 2003-2008 Philip Kendall
$Id: expression.c 4633 2012-01-19 23:26:10Z pak21 $
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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
Author contact information:
E-mail: philip-fuse@shadowmagic.org.uk
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debugger_internals.h"
#include "fuse.h"
#include "mempool.h"
#include "ui/ui.h"
#include "utils.h"
typedef enum expression_type {
DEBUGGER_EXPRESSION_TYPE_INTEGER,
DEBUGGER_EXPRESSION_TYPE_REGISTER,
DEBUGGER_EXPRESSION_TYPE_UNARYOP,
DEBUGGER_EXPRESSION_TYPE_BINARYOP,
DEBUGGER_EXPRESSION_TYPE_VARIABLE,
} expression_type;
enum precedence_t {
/* Lowest precedence */
PRECEDENCE_LOGICAL_OR,
PRECEDENCE_LOGICAL_AND,
PRECEDENCE_BITWISE_OR,
PRECEDENCE_BITWISE_XOR,
PRECEDENCE_BITWISE_AND,
PRECEDENCE_EQUALITY,
PRECEDENCE_COMPARISON,
PRECEDENCE_ADDITION,
PRECEDENCE_MULTIPLICATION,
PRECEDENCE_NEGATE,
PRECEDENCE_ATOMIC,
/* Highest precedence */
};
struct unaryop_type {
int operation;
debugger_expression *op;
};
struct binaryop_type {
int operation;
debugger_expression *op1, *op2;
};
struct debugger_expression {
expression_type type;
enum precedence_t precedence;
union {
int integer;
int reg;
struct unaryop_type unaryop;
struct binaryop_type binaryop;
char *variable;
} types;
};
static libspectrum_dword evaluate_unaryop( struct unaryop_type *unaryop );
static libspectrum_dword evaluate_binaryop( struct binaryop_type *binary );
static int deparse_unaryop( char *buffer, size_t length,
const struct unaryop_type *unaryop );
static int deparse_binaryop( char *buffer, size_t length,
const struct binaryop_type *binaryop );
static int
brackets_necessary( int top_operation, debugger_expression *operand );
static int is_non_associative( int operation );
static enum precedence_t
unaryop_precedence( int operation )
{
switch( operation ) {
case '!': case '~': case '-': return PRECEDENCE_NEGATE;
default:
ui_error( UI_ERROR_ERROR, "unknown unary operator %d", operation );
fuse_abort();
}
}
static enum precedence_t
binaryop_precedence( int operation )
{
switch( operation ) {
case DEBUGGER_TOKEN_LOGICAL_OR: return PRECEDENCE_LOGICAL_OR;
case DEBUGGER_TOKEN_LOGICAL_AND: return PRECEDENCE_LOGICAL_AND;
case '|': return PRECEDENCE_BITWISE_OR;
case '^': return PRECEDENCE_BITWISE_XOR;
case '&': return PRECEDENCE_BITWISE_AND;
case '+': case '-': return PRECEDENCE_ADDITION;
case '*': case '/': return PRECEDENCE_MULTIPLICATION;
case DEBUGGER_TOKEN_EQUAL_TO:
case DEBUGGER_TOKEN_NOT_EQUAL_TO:
return PRECEDENCE_EQUALITY;
case '<': case '>':
case DEBUGGER_TOKEN_LESS_THAN_OR_EQUAL_TO:
case DEBUGGER_TOKEN_GREATER_THAN_OR_EQUAL_TO:
return PRECEDENCE_COMPARISON;
default:
ui_error( UI_ERROR_ERROR, "unknown binary operator %d", operation );
fuse_abort();
}
}
debugger_expression*
debugger_expression_new_number( libspectrum_dword number, int pool )
{
debugger_expression *exp;
exp = mempool_alloc( pool, sizeof( *exp ) );
if( !exp ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
exp->type = DEBUGGER_EXPRESSION_TYPE_INTEGER;
exp->precedence = PRECEDENCE_ATOMIC;
exp->types.integer = number;
return exp;
}
debugger_expression*
debugger_expression_new_register( int which, int pool )
{
debugger_expression *exp;
exp = mempool_alloc( pool, sizeof( *exp ) );
if( !exp ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
exp->type = DEBUGGER_EXPRESSION_TYPE_REGISTER;
exp->precedence = PRECEDENCE_ATOMIC;
exp->types.reg = which;
return exp;
}
debugger_expression*
debugger_expression_new_binaryop( int operation, debugger_expression *operand1,
debugger_expression *operand2, int pool )
{
debugger_expression *exp;
exp = mempool_alloc( pool, sizeof( *exp ) );
if( !exp ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
exp->type = DEBUGGER_EXPRESSION_TYPE_BINARYOP;
exp->precedence = binaryop_precedence( operation );
exp->types.binaryop.operation = operation;
exp->types.binaryop.op1 = operand1;
exp->types.binaryop.op2 = operand2;
return exp;
}
debugger_expression*
debugger_expression_new_unaryop( int operation, debugger_expression *operand,
int pool )
{
debugger_expression *exp;
exp = mempool_alloc( pool, sizeof( *exp ) );
if( !exp ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
exp->type = DEBUGGER_EXPRESSION_TYPE_UNARYOP;
exp->precedence = unaryop_precedence( operation );
exp->types.unaryop.operation = operation;
exp->types.unaryop.op = operand;
return exp;
}
debugger_expression*
debugger_expression_new_variable( const char *name, int pool )
{
debugger_expression *exp;
exp = mempool_alloc( pool, sizeof( *exp ) );
if( !exp ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
exp->type = DEBUGGER_EXPRESSION_TYPE_VARIABLE;
exp->precedence = PRECEDENCE_ATOMIC;
exp->types.variable = mempool_strdup( pool, name );
if( !exp->types.variable ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return NULL;
}
return exp;
}
void
debugger_expression_delete( debugger_expression *exp )
{
switch( exp->type ) {
case DEBUGGER_EXPRESSION_TYPE_INTEGER:
case DEBUGGER_EXPRESSION_TYPE_REGISTER:
break;
case DEBUGGER_EXPRESSION_TYPE_UNARYOP:
debugger_expression_delete( exp->types.unaryop.op );
break;
case DEBUGGER_EXPRESSION_TYPE_BINARYOP:
debugger_expression_delete( exp->types.binaryop.op1 );
debugger_expression_delete( exp->types.binaryop.op2 );
break;
case DEBUGGER_EXPRESSION_TYPE_VARIABLE:
free( exp->types.variable );
break;
}
free( exp );
}
debugger_expression*
debugger_expression_copy( debugger_expression *src )
{
debugger_expression *dest;
dest = malloc( sizeof( *dest ) );
if( !dest ) return NULL;
dest->type = src->type;
dest->precedence = src->precedence;
switch( dest->type ) {
case DEBUGGER_EXPRESSION_TYPE_INTEGER:
dest->types.integer = src->types.integer;
break;
case DEBUGGER_EXPRESSION_TYPE_REGISTER:
dest->types.reg = src->types.reg;
break;
case DEBUGGER_EXPRESSION_TYPE_UNARYOP:
dest->types.unaryop.operation = src->types.unaryop.operation;
dest->types.unaryop.op = debugger_expression_copy( src->types.unaryop.op );
if( !dest->types.unaryop.op ) {
free( dest );
return NULL;
}
break;
case DEBUGGER_EXPRESSION_TYPE_BINARYOP:
dest->types.binaryop.operation = src->types.binaryop.operation;
dest->types.binaryop.op1 =
debugger_expression_copy( src->types.binaryop.op1 );
if( !dest->types.binaryop.op1 ) {
free( dest );
return NULL;
}
dest->types.binaryop.op2 =
debugger_expression_copy( src->types.binaryop.op2 );
if( !dest->types.binaryop.op2 ) {
debugger_expression_delete( dest->types.binaryop.op1 );
free( dest );
return NULL;
}
break;
case DEBUGGER_EXPRESSION_TYPE_VARIABLE:
dest->types.variable = utils_safe_strdup( src->types.variable );
break;
}
return dest;
}
libspectrum_dword
debugger_expression_evaluate( debugger_expression *exp )
{
switch( exp->type ) {
case DEBUGGER_EXPRESSION_TYPE_INTEGER:
return exp->types.integer;
case DEBUGGER_EXPRESSION_TYPE_REGISTER:
return debugger_register_get( exp->types.reg );
case DEBUGGER_EXPRESSION_TYPE_UNARYOP:
return evaluate_unaryop( &( exp->types.unaryop ) );
case DEBUGGER_EXPRESSION_TYPE_BINARYOP:
return evaluate_binaryop( &( exp->types.binaryop ) );
case DEBUGGER_EXPRESSION_TYPE_VARIABLE:
return debugger_variable_get( exp->types.variable );
}
ui_error( UI_ERROR_ERROR, "unknown expression type %d", exp->type );
fuse_abort();
}
static libspectrum_dword
evaluate_unaryop( struct unaryop_type *unary )
{
switch( unary->operation ) {
case '!': return !debugger_expression_evaluate( unary->op );
case '~': return ~debugger_expression_evaluate( unary->op );
case '-': return -debugger_expression_evaluate( unary->op );
}
ui_error( UI_ERROR_ERROR, "unknown unary operator %d", unary->operation );
fuse_abort();
}
static libspectrum_dword
evaluate_binaryop( struct binaryop_type *binary )
{
switch( binary->operation ) {
case '+': return debugger_expression_evaluate( binary->op1 ) +
debugger_expression_evaluate( binary->op2 );
case '-': return debugger_expression_evaluate( binary->op1 ) -
debugger_expression_evaluate( binary->op2 );
case '*': return debugger_expression_evaluate( binary->op1 ) *
debugger_expression_evaluate( binary->op2 );
case '/': return debugger_expression_evaluate( binary->op1 ) /
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_EQUAL_TO:
return debugger_expression_evaluate( binary->op1 ) ==
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_NOT_EQUAL_TO:
return debugger_expression_evaluate( binary->op1 ) !=
debugger_expression_evaluate( binary->op2 );
case '>': return debugger_expression_evaluate( binary->op1 ) >
debugger_expression_evaluate( binary->op2 );
case '<': return debugger_expression_evaluate( binary->op1 ) <
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_LESS_THAN_OR_EQUAL_TO:
return debugger_expression_evaluate( binary->op1 ) <=
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_GREATER_THAN_OR_EQUAL_TO:
return debugger_expression_evaluate( binary->op1 ) >=
debugger_expression_evaluate( binary->op2 );
case '&': return debugger_expression_evaluate( binary->op1 ) &
debugger_expression_evaluate( binary->op2 );
case '^': return debugger_expression_evaluate( binary->op1 ) ^
debugger_expression_evaluate( binary->op2 );
case '|': return debugger_expression_evaluate( binary->op1 ) |
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_LOGICAL_AND:
return debugger_expression_evaluate( binary->op1 ) &&
debugger_expression_evaluate( binary->op2 );
case DEBUGGER_TOKEN_LOGICAL_OR:
return debugger_expression_evaluate( binary->op1 ) ||
debugger_expression_evaluate( binary->op2 );
}
ui_error( UI_ERROR_ERROR, "unknown binary operator %d", binary->operation );
fuse_abort();
}
int
debugger_expression_deparse( char *buffer, size_t length,
const debugger_expression *exp )
{
switch( exp->type ) {
case DEBUGGER_EXPRESSION_TYPE_INTEGER:
if( debugger_output_base == 10 ) {
snprintf( buffer, length, "%d", exp->types.integer );
} else {
snprintf( buffer, length, "0x%x", exp->types.integer );
}
return 0;
case DEBUGGER_EXPRESSION_TYPE_REGISTER:
snprintf( buffer, length, "%s", debugger_register_text( exp->types.reg ) );
return 0;
case DEBUGGER_EXPRESSION_TYPE_UNARYOP:
return deparse_unaryop( buffer, length, &( exp->types.unaryop ) );
case DEBUGGER_EXPRESSION_TYPE_BINARYOP:
return deparse_binaryop( buffer, length, &( exp->types.binaryop ) );
case DEBUGGER_EXPRESSION_TYPE_VARIABLE:
snprintf( buffer, length, "$%s", exp->types.variable );
return 0;
}
ui_error( UI_ERROR_ERROR, "unknown expression type %d", exp->type );
fuse_abort();
}
static int
deparse_unaryop( char *buffer, size_t length,
const struct unaryop_type *unaryop )
{
char *operand_buffer; const char *operation_string = NULL;
int brackets;
int error;
operand_buffer = malloc( length );
if( !operand_buffer ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return 1;
}
error = debugger_expression_deparse( operand_buffer, length, unaryop->op );
if( error ) { free( operand_buffer ); return error; }
switch( unaryop->operation ) {
case '!': operation_string = "!"; break;
case '~': operation_string = "~"; break;
case '-': operation_string = "-"; break;
default:
ui_error( UI_ERROR_ERROR, "unknown unary operation %d",
unaryop->operation );
fuse_abort();
}
brackets = ( unaryop->op->precedence <
unaryop_precedence( unaryop->operation ) );
snprintf( buffer, length, "%s%s%s%s", operation_string,
brackets ? "( " : "", operand_buffer,
brackets ? " )" : "" );
free( operand_buffer );
return 0;
}
static int
deparse_binaryop( char *buffer, size_t length,
const struct binaryop_type *binaryop )
{
char *operand1_buffer, *operand2_buffer; const char *operation_string = NULL;
int brackets_necessary1, brackets_necessary2;
int error;
operand1_buffer = malloc( 2 * length );
if( !operand1_buffer ) {
ui_error( UI_ERROR_ERROR, "out of memory at %s:%d", __FILE__, __LINE__ );
return 1;
}
operand2_buffer = &operand1_buffer[ length ];
error = debugger_expression_deparse( operand1_buffer, length,
binaryop->op1 );
if( error ) { free( operand1_buffer ); return error; }
error = debugger_expression_deparse( operand2_buffer, length,
binaryop->op2 );
if( error ) { free( operand1_buffer ); return error; }
switch( binaryop->operation ) {
case '+': operation_string = "+"; break;
case '-': operation_string = "-"; break;
case '*': operation_string = "*"; break;
case '/': operation_string = "/"; break;
case DEBUGGER_TOKEN_EQUAL_TO: operation_string = "=="; break;
case DEBUGGER_TOKEN_NOT_EQUAL_TO: operation_string = "!="; break;
case '<': operation_string = "<"; break;
case '>': operation_string = ">"; break;
case DEBUGGER_TOKEN_LESS_THAN_OR_EQUAL_TO: operation_string = "<="; break;
case DEBUGGER_TOKEN_GREATER_THAN_OR_EQUAL_TO: operation_string = ">="; break;
case '&': operation_string = "&"; break;
case '^': operation_string = "^"; break;
case '|': operation_string = "|"; break;
case DEBUGGER_TOKEN_LOGICAL_AND: operation_string = "&&"; break;
case DEBUGGER_TOKEN_LOGICAL_OR: operation_string = "||"; break;
default:
ui_error( UI_ERROR_ERROR, "unknown binary operation %d",
binaryop->operation );
fuse_abort();
}
brackets_necessary1 = brackets_necessary( binaryop->operation,
binaryop->op1 );
brackets_necessary2 = brackets_necessary( binaryop->operation,
binaryop->op2 );
snprintf( buffer, length, "%s%s%s %s %s%s%s",
brackets_necessary1 ? "( " : "", operand1_buffer,
brackets_necessary1 ? " )" : "",
operation_string,
brackets_necessary2 ? "( " : "", operand2_buffer,
brackets_necessary2 ? " )" : "" );
free( operand1_buffer );
return 0;
}
/* When deparsing, do we need to put brackets around `operand' when
being used as an operand of the binary operation `top_operation'? */
static int
brackets_necessary( int top_operation, debugger_expression *operand )
{
enum precedence_t top_precedence, bottom_precedence;
top_precedence = binaryop_precedence( top_operation );
bottom_precedence = operand->precedence;
/* If the top level operation has a higher precedence than the
bottom level operation, we always need brackets */
if( top_precedence > bottom_precedence ) return 1;
/* If the two operations are of equal precedence, we need brackets
i) if the top level operation is non-associative, or
ii) if the operand is a non-associative operation
Note the assumption here that all things with a precedence equal to
a binary operator are also binary operators
Strictly, we don't need brackets in either of these cases, but
otherwise the user is going to have to remember operator
left-right associativity; I think things are clearer with
brackets in.
*/
if( top_precedence == bottom_precedence ) {
if( is_non_associative( top_operation ) ) return 1;
/* Sanity check */
if( operand->type != DEBUGGER_EXPRESSION_TYPE_BINARYOP ) {
ui_error( UI_ERROR_ERROR,
"binary operator has same precedence as non-binary operator" );
fuse_abort();
}
return is_non_associative( operand->types.binaryop.operation );
}
/* Otherwise (ie if the top level operation is of lower precedence
than the bottom, or both operators have equal precedence and
everything is associative) we don't need brackets */
return 0;
}
/* Is a binary operator non-associative? */
static int
is_non_associative( int operation )
{
switch( operation ) {
/* Simple cases */
case '+': case '*': return 0;
case '-': case '/': return 1;
/* None of the comparison operators are associative due to them
returning truth values */
case DEBUGGER_TOKEN_EQUAL_TO:
case DEBUGGER_TOKEN_NOT_EQUAL_TO:
case '<': case '>':
case DEBUGGER_TOKEN_LESS_THAN_OR_EQUAL_TO:
case DEBUGGER_TOKEN_GREATER_THAN_OR_EQUAL_TO:
return 1;
/* The logical operators are associative */
case DEBUGGER_TOKEN_LOGICAL_AND: return 0;
case DEBUGGER_TOKEN_LOGICAL_OR: return 0;
/* The bitwise operators are also associative (consider them as
vectorised logical operators) */
case '&': return 0;
case '^': return 0;
case '|': return 0;
}
/* Should never get here */
ui_error( UI_ERROR_ERROR, "unknown binary operation %d", operation );
fuse_abort();
}
|