2005-04-28 23:14:54 +00:00
|
|
|
/*
|
|
|
|
* ReactOS kernel
|
|
|
|
* Copyright (C) 2005 ReactOS Team
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2009-10-27 10:34:16 +00:00
|
|
|
* 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.
|
2005-04-28 23:14:54 +00:00
|
|
|
*/
|
2008-03-09 14:11:42 +00:00
|
|
|
/*
|
2005-04-28 23:14:54 +00:00
|
|
|
* PROJECT: ReactOS kernel
|
2015-10-04 11:54:25 +00:00
|
|
|
* FILE: ntoskrnl/kdbg/kdb_expr.c
|
2005-04-28 23:14:54 +00:00
|
|
|
* PURPOSE: Kernel debugger expression evaluation
|
|
|
|
* PROGRAMMER: Gregor Anich (blight@blight.eu.org)
|
|
|
|
* UPDATE HISTORY:
|
|
|
|
* Created 15/01/2005
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Note:
|
|
|
|
*
|
|
|
|
* The given expression is parsed and stored in reverse polish notation,
|
|
|
|
* then it is evaluated and the result is returned.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* INCLUDES ******************************************************************/
|
|
|
|
|
|
|
|
#include <ntoskrnl.h>
|
|
|
|
#define NDEBUG
|
2008-08-30 16:31:06 +00:00
|
|
|
#include <debug.h>
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
/* TYPES *********************************************************************/
|
|
|
|
typedef enum _RPN_OP_TYPE
|
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnOpNop,
|
|
|
|
RpnOpBinaryOperator,
|
|
|
|
RpnOpUnaryOperator,
|
|
|
|
RpnOpImmediate,
|
|
|
|
RpnOpRegister,
|
|
|
|
RpnOpDereference
|
2005-04-28 23:14:54 +00:00
|
|
|
} RPN_OP_TYPE;
|
|
|
|
|
|
|
|
typedef ULONGLONG (*RPN_BINARY_OPERATOR)(ULONGLONG a, ULONGLONG b);
|
|
|
|
|
|
|
|
typedef struct _RPN_OP
|
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
RPN_OP_TYPE Type;
|
|
|
|
ULONG CharacterOffset;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
/* RpnOpBinaryOperator */
|
|
|
|
RPN_BINARY_OPERATOR BinaryOperator;
|
|
|
|
/* RpnOpImmediate */
|
|
|
|
ULONGLONG Immediate;
|
|
|
|
/* RpnOpRegister */
|
|
|
|
UCHAR Register;
|
|
|
|
/* RpnOpDereference */
|
|
|
|
UCHAR DerefMemorySize;
|
|
|
|
}
|
|
|
|
Data;
|
|
|
|
}
|
|
|
|
RPN_OP, *PRPN_OP;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
typedef struct _RPN_STACK
|
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG Size; /* Number of RPN_OPs on Ops */
|
|
|
|
ULONG Sp; /* Stack pointer */
|
|
|
|
RPN_OP Ops[1]; /* Array of RPN_OPs */
|
|
|
|
}
|
|
|
|
RPN_STACK, *PRPN_STACK;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
/* DEFINES *******************************************************************/
|
|
|
|
#define stricmp _stricmp
|
|
|
|
|
|
|
|
#ifndef RTL_FIELD_SIZE
|
|
|
|
# define RTL_FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define CONST_STRCPY(dst, src) \
|
2009-09-09 13:49:04 +00:00
|
|
|
do { if ((dst)) { memcpy(dst, src, sizeof(src)); } } while (0);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
#define RPN_OP_STACK_SIZE 256
|
|
|
|
#define RPN_VALUE_STACK_SIZE 256
|
|
|
|
|
|
|
|
/* GLOBALS *******************************************************************/
|
2009-09-09 13:49:04 +00:00
|
|
|
static struct
|
|
|
|
{
|
|
|
|
ULONG Size;
|
|
|
|
ULONG Sp;
|
|
|
|
RPN_OP Ops[RPN_OP_STACK_SIZE];
|
|
|
|
}
|
|
|
|
RpnStack =
|
|
|
|
{
|
|
|
|
RPN_OP_STACK_SIZE,
|
|
|
|
0
|
|
|
|
};
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
PCHAR Name;
|
2019-10-18 22:12:41 +00:00
|
|
|
UCHAR Offset;
|
2009-09-09 13:49:04 +00:00
|
|
|
UCHAR Size;
|
|
|
|
}
|
|
|
|
RegisterToTrapFrame[] =
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2021-04-27 08:23:37 +00:00
|
|
|
/* FIXME: X86 only */
|
|
|
|
#ifdef _M_IX86
|
2020-04-08 21:08:57 +00:00
|
|
|
{"eip", FIELD_OFFSET(KDB_KTRAP_FRAME, Eip), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Eip)},
|
2021-04-27 08:23:37 +00:00
|
|
|
#else
|
|
|
|
{"rip", FIELD_OFFSET(KDB_KTRAP_FRAME, Rip), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rip)},
|
|
|
|
#endif
|
2020-04-08 21:08:57 +00:00
|
|
|
{"eflags", FIELD_OFFSET(KDB_KTRAP_FRAME, EFlags), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, EFlags)},
|
2021-04-27 08:23:37 +00:00
|
|
|
#ifdef _M_IX86
|
2020-04-08 21:08:57 +00:00
|
|
|
{"eax", FIELD_OFFSET(KDB_KTRAP_FRAME, Eax), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Eax)},
|
|
|
|
{"ebx", FIELD_OFFSET(KDB_KTRAP_FRAME, Ebx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Ebx)},
|
|
|
|
{"ecx", FIELD_OFFSET(KDB_KTRAP_FRAME, Ecx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Ecx)},
|
|
|
|
{"edx", FIELD_OFFSET(KDB_KTRAP_FRAME, Edx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Edx)},
|
|
|
|
{"esi", FIELD_OFFSET(KDB_KTRAP_FRAME, Esi), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Esi)},
|
|
|
|
{"edi", FIELD_OFFSET(KDB_KTRAP_FRAME, Edi), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Edi)},
|
|
|
|
{"esp", FIELD_OFFSET(KDB_KTRAP_FRAME, Esp), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Esp)},
|
|
|
|
{"ebp", FIELD_OFFSET(KDB_KTRAP_FRAME, Ebp), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Ebp)},
|
2021-04-27 08:23:37 +00:00
|
|
|
#else
|
|
|
|
{"rax", FIELD_OFFSET(KDB_KTRAP_FRAME, Rax), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rax)},
|
|
|
|
{"rbx", FIELD_OFFSET(KDB_KTRAP_FRAME, Rbx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rbx)},
|
|
|
|
{"rcx", FIELD_OFFSET(KDB_KTRAP_FRAME, Rcx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rcx)},
|
|
|
|
{"rdx", FIELD_OFFSET(KDB_KTRAP_FRAME, Rdx), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rdx)},
|
|
|
|
{"rsi", FIELD_OFFSET(KDB_KTRAP_FRAME, Rsi), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rsi)},
|
|
|
|
{"rdi", FIELD_OFFSET(KDB_KTRAP_FRAME, Rdi), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rdi)},
|
|
|
|
{"rsp", FIELD_OFFSET(KDB_KTRAP_FRAME, Rsp), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rsp)},
|
|
|
|
{"rbp", FIELD_OFFSET(KDB_KTRAP_FRAME, Rbp), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Rbp)},
|
|
|
|
#endif
|
2020-04-08 21:08:57 +00:00
|
|
|
{"cs", FIELD_OFFSET(KDB_KTRAP_FRAME, SegCs), 2 }, /* Use only the lower 2 bytes */
|
|
|
|
{"ds", FIELD_OFFSET(KDB_KTRAP_FRAME, SegDs), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, SegDs)},
|
|
|
|
{"es", FIELD_OFFSET(KDB_KTRAP_FRAME, SegEs), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, SegEs)},
|
|
|
|
{"fs", FIELD_OFFSET(KDB_KTRAP_FRAME, SegFs), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, SegFs)},
|
|
|
|
{"gs", FIELD_OFFSET(KDB_KTRAP_FRAME, SegGs), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, SegGs)},
|
|
|
|
{"ss", FIELD_OFFSET(KDB_KTRAP_FRAME, SegSs), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, SegSs)},
|
|
|
|
{"dr0", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr0), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr0)},
|
|
|
|
{"dr1", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr1), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr1)},
|
|
|
|
{"dr2", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr2), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr2)},
|
|
|
|
{"dr3", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr3), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr3)},
|
|
|
|
{"dr6", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr6), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr6)},
|
|
|
|
{"dr7", FIELD_OFFSET(KDB_KTRAP_FRAME, Dr7), RTL_FIELD_SIZE(KDB_KTRAP_FRAME, Dr7)}
|
2005-04-28 23:14:54 +00:00
|
|
|
};
|
2009-09-09 13:49:04 +00:00
|
|
|
static const INT RegisterToTrapFrameCount = sizeof (RegisterToTrapFrame) / sizeof (RegisterToTrapFrame[0]);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorAdd(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return a + b;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorSub(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return a - b;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorMul(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return a * b;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorDiv(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return a / b;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorMod(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return a % b;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorEquals(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a == b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorNotEquals(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a != b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorLessThan(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a < b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorLessThanOrEquals(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a <= b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorGreaterThan(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a > b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONGLONG
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnBinaryOperatorGreaterThanOrEquals(
|
|
|
|
ULONGLONG a,
|
|
|
|
ULONGLONG b)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
return (a >= b);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Dumps the given RPN stack content
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
*/
|
|
|
|
VOID
|
|
|
|
RpnpDumpStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PRPN_STACK Stack)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG ul;
|
|
|
|
|
|
|
|
ASSERT(Stack);
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("\nStack size: %ld\n", Stack->Sp);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
for (ul = 0; ul < Stack->Sp; ul++)
|
|
|
|
{
|
|
|
|
PRPN_OP Op = Stack->Ops + ul;
|
|
|
|
switch (Op->Type)
|
|
|
|
{
|
|
|
|
case RpnOpNop:
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("NOP,");
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpImmediate:
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("0x%I64x,", Op->Data.Immediate);
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpBinaryOperator:
|
|
|
|
if (Op->Data.BinaryOperator == RpnBinaryOperatorAdd)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("+,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorSub)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("-,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorMul)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("*,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorDiv)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("/,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorMod)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("%%,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorEquals)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("==,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorNotEquals)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("!=,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorLessThan)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("<,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorLessThanOrEquals)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("<=,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorGreaterThan)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf(">,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (Op->Data.BinaryOperator == RpnBinaryOperatorGreaterThanOrEquals)
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf(">=,");
|
2009-09-09 13:49:04 +00:00
|
|
|
else
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("UNKNOWN OP,");
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpRegister:
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("%s,", RegisterToTrapFrame[Op->Data.Register].Name);
|
2009-09-09 13:49:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpDereference:
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("[%s],",
|
2009-09-09 13:49:04 +00:00
|
|
|
(Op->Data.DerefMemorySize == 1) ? ("byte") :
|
|
|
|
((Op->Data.DerefMemorySize == 2) ? ("word") :
|
|
|
|
((Op->Data.DerefMemorySize == 4) ? ("dword") : ("qword"))));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("\nUnsupported Type: %d\n", Op->Type);
|
2009-09-09 13:49:04 +00:00
|
|
|
ul = Stack->Sp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-18 16:14:51 +00:00
|
|
|
KdpDprintf("\n");
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Clears the given RPN stack.
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static VOID
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpClearStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
OUT PRPN_STACK Stack)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Stack);
|
|
|
|
Stack->Sp = 0;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Pushes an RPN_OP onto the stack.
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
* \param Op RPN_OP to be copied onto the stack.
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpPushStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN OUT PRPN_STACK Stack,
|
|
|
|
IN PRPN_OP Op)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Stack);
|
|
|
|
ASSERT(Op);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (Stack->Sp >= Stack->Size)
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
memcpy(Stack->Ops + Stack->Sp, Op, sizeof (RPN_OP));
|
|
|
|
Stack->Sp++;
|
|
|
|
|
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Pops the top op from the stack.
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
* \param Op Pointer to an RPN_OP to store the popped op into (can be NULL).
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (stack empty)
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpPopStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN OUT PRPN_STACK Stack,
|
|
|
|
OUT PRPN_OP Op OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Stack);
|
|
|
|
|
|
|
|
if (Stack->Sp == 0)
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
Stack->Sp--;
|
|
|
|
if (Op)
|
|
|
|
memcpy(Op, Stack->Ops + Stack->Sp, sizeof (RPN_OP));
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Gets the top op from the stack (not popping it)
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
* \param Op Pointer to an RPN_OP to copy the top op into.
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure (stack empty)
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpTopStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PRPN_STACK Stack,
|
|
|
|
OUT PRPN_OP Op)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Stack);
|
|
|
|
ASSERT(Op);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (Stack->Sp == 0)
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
memcpy(Op, Stack->Ops + Stack->Sp - 1, sizeof (RPN_OP));
|
|
|
|
|
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Parses an expression.
|
|
|
|
*
|
|
|
|
* This functions parses the given expression until the end of string or a closing
|
|
|
|
* brace is found. As the function parses the string it pushes RPN_OPs onto the
|
|
|
|
* stack.
|
|
|
|
*
|
|
|
|
* Examples: 1+2*3 ; eax+10 ; (eax+16) * (ebx+4) ; dword[eax]
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
* \param Expression String to parse.
|
|
|
|
* \param CharacterOffset Character offset of the subexpression from the beginning of the expression.
|
|
|
|
* \param End On success End is set to the character at which parsing stopped.
|
|
|
|
* \param ErrOffset On failure this is set to the character offset at which the error occoured.
|
|
|
|
* \param ErrMsg On failure a message describing the problem is copied into this buffer (128 bytes)
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure.
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpParseExpression(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PRPN_STACK Stack,
|
|
|
|
IN PCHAR Expression,
|
|
|
|
OUT PCHAR *End OPTIONAL,
|
|
|
|
IN ULONG CharacterOffset,
|
|
|
|
OUT PLONG ErrOffset OPTIONAL,
|
|
|
|
OUT PCHAR ErrMsg OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PCHAR p = Expression;
|
|
|
|
PCHAR pend;
|
|
|
|
PCHAR Operator = NULL;
|
|
|
|
LONG OperatorOffset = -1;
|
|
|
|
RPN_OP RpnOp;
|
|
|
|
RPN_OP PoppedOperator;
|
|
|
|
BOOLEAN HavePoppedOperator = FALSE;
|
|
|
|
RPN_OP ComparativeOp;
|
|
|
|
BOOLEAN ComparativeOpFilled = FALSE;
|
|
|
|
BOOLEAN IsComparativeOp;
|
|
|
|
INT i, i2;
|
|
|
|
ULONG ul;
|
|
|
|
UCHAR MemorySize;
|
|
|
|
CHAR Buffer[16];
|
|
|
|
BOOLEAN First;
|
|
|
|
|
|
|
|
ASSERT(Stack);
|
|
|
|
ASSERT(Expression);
|
|
|
|
|
|
|
|
First = TRUE;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Skip whitespace */
|
|
|
|
while (isspace(*p))
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
CharacterOffset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for end of expression */
|
|
|
|
if (p[0] == '\0' || p[0] == ')' || p[0] == ']')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (!First)
|
|
|
|
{
|
|
|
|
/* Remember operator */
|
|
|
|
Operator = p++;
|
|
|
|
OperatorOffset = CharacterOffset++;
|
|
|
|
|
|
|
|
/* Pop operator (to get the right operator precedence) */
|
|
|
|
HavePoppedOperator = FALSE;
|
|
|
|
if (*Operator == '*' || *Operator == '/' || *Operator == '%')
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (RpnpTopStack(Stack, &PoppedOperator) &&
|
|
|
|
PoppedOperator.Type == RpnOpBinaryOperator &&
|
|
|
|
(PoppedOperator.Data.BinaryOperator == RpnBinaryOperatorAdd ||
|
|
|
|
PoppedOperator.Data.BinaryOperator == RpnBinaryOperatorSub))
|
|
|
|
{
|
|
|
|
RpnpPopStack(Stack, NULL);
|
|
|
|
HavePoppedOperator = TRUE;
|
|
|
|
}
|
|
|
|
else if (PoppedOperator.Type == RpnOpNop)
|
|
|
|
{
|
|
|
|
RpnpPopStack(Stack, NULL);
|
|
|
|
/* Discard the NOP - it was only pushed to indicate there was a
|
|
|
|
* closing brace, so the previous operator shouldn't be popped.
|
|
|
|
*/
|
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
else if ((Operator[0] == '=' && Operator[1] == '=') ||
|
|
|
|
(Operator[0] == '!' && Operator[1] == '=') ||
|
|
|
|
Operator[0] == '<' || Operator[0] == '>')
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (Operator[0] == '=' || Operator[0] == '!' ||
|
|
|
|
(Operator[0] == '<' && Operator[1] == '=') ||
|
|
|
|
(Operator[0] == '>' && Operator[1] == '='))
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
CharacterOffset++;
|
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
#if 0
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Parse rest of expression */
|
|
|
|
if (!RpnpParseExpression(Stack, p + 1, &pend, CharacterOffset + 1,
|
|
|
|
ErrOffset, ErrMsg))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (pend == p + 1)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Expression expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset + 1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto end_of_expression; /* return */
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (Operator[0] != '+' && Operator[0] != '-')
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
CONST_STRCPY(ErrMsg, "Operator expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = OperatorOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Skip whitespace */
|
|
|
|
while (isspace(*p))
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
p++;
|
|
|
|
CharacterOffset++;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Get operand */
|
|
|
|
MemorySize = sizeof(ULONG_PTR); /* default to pointer size */
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
get_operand:
|
2009-09-09 13:49:04 +00:00
|
|
|
i = strcspn(p, "+-*/%()[]<>!=");
|
|
|
|
if (i > 0)
|
|
|
|
{
|
|
|
|
i2 = i;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Copy register name/memory size */
|
|
|
|
while (isspace(p[--i2]));
|
|
|
|
|
|
|
|
i2 = min(i2 + 1, (INT)sizeof (Buffer) - 1);
|
|
|
|
strncpy(Buffer, p, i2);
|
|
|
|
Buffer[i2] = '\0';
|
|
|
|
|
|
|
|
/* Memory size prefix */
|
|
|
|
if (p[i] == '[')
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (stricmp(Buffer, "byte") == 0)
|
|
|
|
MemorySize = 1;
|
|
|
|
else if (stricmp(Buffer, "word") == 0)
|
|
|
|
MemorySize = 2;
|
|
|
|
else if (stricmp(Buffer, "dword") == 0)
|
|
|
|
MemorySize = 4;
|
|
|
|
else if (stricmp(Buffer, "qword") == 0)
|
|
|
|
MemorySize = 8;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Invalid memory size prefix");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += i;
|
|
|
|
CharacterOffset += i;
|
|
|
|
goto get_operand;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
/* Try to find register */
|
|
|
|
for (i = 0; i < RegisterToTrapFrameCount; i++)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (stricmp(RegisterToTrapFrame[i].Name, Buffer) == 0)
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
if (i < RegisterToTrapFrameCount)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnOp.Type = RpnOpRegister;
|
|
|
|
RpnOp.CharacterOffset = CharacterOffset;
|
|
|
|
RpnOp.Data.Register = i;
|
|
|
|
i = strlen(RegisterToTrapFrame[i].Name);
|
|
|
|
CharacterOffset += i;
|
|
|
|
p += i;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
else
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Immediate value */
|
|
|
|
/* FIXME: Need string to ULONGLONG function */
|
|
|
|
ul = strtoul(p, &pend, 0);
|
|
|
|
if (p != pend)
|
|
|
|
{
|
|
|
|
RpnOp.Type = RpnOpImmediate;
|
|
|
|
RpnOp.CharacterOffset = CharacterOffset;
|
|
|
|
RpnOp.Data.Immediate = (ULONGLONG)ul;
|
|
|
|
CharacterOffset += pend - p;
|
|
|
|
p = pend;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Operand expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Push operand */
|
|
|
|
if (!RpnpPushStack(Stack, &RpnOp))
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else if (i == 0)
|
|
|
|
{
|
|
|
|
if (p[0] == '(' || p[0] == '[') /* subexpression */
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!RpnpParseExpression(Stack, p + 1, &pend, CharacterOffset + 1,
|
|
|
|
ErrOffset, ErrMsg))
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if (pend == p + 1)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Expression expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset + 1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p[0] == '[') /* dereference */
|
|
|
|
{
|
|
|
|
ASSERT(MemorySize == 1 || MemorySize == 2 ||
|
|
|
|
MemorySize == 4 || MemorySize == 8);
|
|
|
|
|
|
|
|
if (pend[0] != ']')
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "']' expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset + (pend - p);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
RpnOp.Type = RpnOpDereference;
|
|
|
|
RpnOp.CharacterOffset = CharacterOffset;
|
|
|
|
RpnOp.Data.DerefMemorySize = MemorySize;
|
|
|
|
|
|
|
|
if (!RpnpPushStack(Stack, &RpnOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* p[0] == '(' */
|
|
|
|
{
|
|
|
|
if (pend[0] != ')')
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "')' expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset + (pend - p);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push a "nop" to prevent popping of the + operator (which would
|
|
|
|
* result in (10+10)/2 beeing evaluated as 15)
|
|
|
|
*/
|
|
|
|
RpnOp.Type = RpnOpNop;
|
|
|
|
if (!RpnpPushStack(Stack, &RpnOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip closing brace/bracket */
|
|
|
|
pend++;
|
|
|
|
|
|
|
|
CharacterOffset += pend - p;
|
|
|
|
p = pend;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (First && p[0] == '-') /* Allow expressions like "- eax" */
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnOp.Type = RpnOpImmediate;
|
|
|
|
RpnOp.CharacterOffset = CharacterOffset;
|
|
|
|
RpnOp.Data.Immediate = 0;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!RpnpPushStack(Stack, &RpnOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
CONST_STRCPY(ErrMsg, "Operand expected");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = CharacterOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "strcspn() failed");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
2005-04-28 23:14:54 +00:00
|
|
|
return FALSE;
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!First)
|
|
|
|
{
|
|
|
|
/* Push operator */
|
|
|
|
RpnOp.CharacterOffset = OperatorOffset;
|
|
|
|
RpnOp.Type = RpnOpBinaryOperator;
|
|
|
|
IsComparativeOp = FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
switch (*Operator)
|
|
|
|
{
|
|
|
|
case '+':
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorAdd;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '-':
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorSub;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '*':
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorMul;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '/':
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorDiv;
|
|
|
|
break;
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '%':
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorMod;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '=':
|
|
|
|
ASSERT(Operator[1] == '=');
|
|
|
|
IsComparativeOp = TRUE;
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorEquals;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '!':
|
|
|
|
ASSERT(Operator[1] == '=');
|
|
|
|
IsComparativeOp = TRUE;
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorNotEquals;
|
|
|
|
break;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
case '<':
|
|
|
|
IsComparativeOp = TRUE;
|
|
|
|
|
|
|
|
if (Operator[1] == '=')
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorLessThanOrEquals;
|
|
|
|
else
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorLessThan;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '>':
|
|
|
|
IsComparativeOp = TRUE;
|
|
|
|
|
|
|
|
if (Operator[1] == '=')
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorGreaterThanOrEquals;
|
|
|
|
else
|
|
|
|
RpnOp.Data.BinaryOperator = RpnBinaryOperatorGreaterThan;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsComparativeOp)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ComparativeOpFilled && !RpnpPushStack(Stack, &ComparativeOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(&ComparativeOp, &RpnOp, sizeof(RPN_OP));
|
|
|
|
ComparativeOpFilled = TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
else if (!RpnpPushStack(Stack, &RpnOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push popped operator */
|
|
|
|
if (HavePoppedOperator)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
if (!RpnpPushStack(Stack, &PoppedOperator))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
2009-09-09 13:49:04 +00:00
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
First = FALSE;
|
|
|
|
}
|
2005-05-09 01:38:29 +00:00
|
|
|
|
2005-04-28 23:14:54 +00:00
|
|
|
//end_of_expression:
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
if (ComparativeOpFilled && !RpnpPushStack(Stack, &ComparativeOp))
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "RPN op stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip whitespace */
|
|
|
|
while (isspace(*p))
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
CharacterOffset++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (End)
|
|
|
|
*End = p;
|
|
|
|
|
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Evaluates the RPN op stack and returns the result.
|
|
|
|
*
|
|
|
|
* \param Stack Pointer to a RPN_STACK structure.
|
|
|
|
* \param TrapFrame Register values.
|
|
|
|
* \param Result Pointer to an ULONG to store the result into.
|
|
|
|
* \param ErrOffset On failure this is set to the character offset at which the error occoured.
|
|
|
|
* \param ErrMsg Buffer which receives an error message on failure (128 bytes)
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure.
|
|
|
|
*/
|
2009-06-13 15:28:33 +00:00
|
|
|
static BOOLEAN
|
2005-04-28 23:14:54 +00:00
|
|
|
RpnpEvaluateStack(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PRPN_STACK Stack,
|
|
|
|
IN PKDB_KTRAP_FRAME TrapFrame,
|
|
|
|
OUT PULONGLONG Result,
|
|
|
|
OUT PLONG ErrOffset OPTIONAL,
|
|
|
|
OUT PCHAR ErrMsg OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONGLONG ValueStack[RPN_VALUE_STACK_SIZE];
|
|
|
|
ULONG ValueStackPointer = 0;
|
|
|
|
ULONG index;
|
|
|
|
ULONGLONG ull;
|
|
|
|
ULONG ul;
|
|
|
|
USHORT us;
|
|
|
|
UCHAR uc;
|
|
|
|
PVOID p;
|
|
|
|
BOOLEAN Ok;
|
2005-04-28 23:14:54 +00:00
|
|
|
#ifdef DEBUG_RPN
|
2009-09-09 13:49:04 +00:00
|
|
|
ULONG ValueStackPointerMax = 0;
|
2005-04-28 23:14:54 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Stack);
|
|
|
|
ASSERT(TrapFrame);
|
|
|
|
ASSERT(Result);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
for (index = 0; index < Stack->Sp; index++)
|
|
|
|
{
|
|
|
|
PRPN_OP Op = Stack->Ops + index;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_RPN
|
2009-09-09 13:49:04 +00:00
|
|
|
ValueStackPointerMax = max(ValueStackPointerMax, ValueStackPointer);
|
2005-04-28 23:14:54 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
switch (Op->Type)
|
|
|
|
{
|
|
|
|
case RpnOpNop:
|
|
|
|
/* No operation */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpImmediate:
|
|
|
|
if (ValueStackPointer == RPN_VALUE_STACK_SIZE)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Value stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueStack[ValueStackPointer++] = Op->Data.Immediate;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpRegister:
|
|
|
|
if (ValueStackPointer == RPN_VALUE_STACK_SIZE)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Value stack overflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ul = Op->Data.Register;
|
|
|
|
p = (PVOID)((ULONG_PTR)TrapFrame + RegisterToTrapFrame[ul].Offset);
|
|
|
|
|
|
|
|
switch (RegisterToTrapFrame[ul].Size)
|
|
|
|
{
|
|
|
|
case 1: ull = (ULONGLONG)(*(PUCHAR)p); break;
|
|
|
|
case 2: ull = (ULONGLONG)(*(PUSHORT)p); break;
|
|
|
|
case 4: ull = (ULONGLONG)(*(PULONG)p); break;
|
|
|
|
case 8: ull = (ULONGLONG)(*(PULONGLONG)p); break;
|
|
|
|
default: ASSERT(0); return FALSE; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueStack[ValueStackPointer++] = ull;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpDereference:
|
|
|
|
if (ValueStackPointer < 1)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Value stack underflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Print a warning when address is out of range */
|
|
|
|
p = (PVOID)(ULONG_PTR)ValueStack[ValueStackPointer - 1];
|
|
|
|
Ok = FALSE;
|
|
|
|
|
|
|
|
switch (Op->Data.DerefMemorySize)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
if (NT_SUCCESS(KdbpSafeReadMemory(&uc, p, sizeof (uc))))
|
|
|
|
{
|
|
|
|
Ok = TRUE;
|
|
|
|
ull = (ULONGLONG)uc;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
if (NT_SUCCESS(KdbpSafeReadMemory(&us, p, sizeof (us))))
|
|
|
|
{
|
|
|
|
Ok = TRUE;
|
|
|
|
ull = (ULONGLONG)us;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
if (NT_SUCCESS(KdbpSafeReadMemory(&ul, p, sizeof (ul))))
|
|
|
|
{
|
|
|
|
Ok = TRUE;
|
|
|
|
ull = (ULONGLONG)ul;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 8:
|
|
|
|
if (NT_SUCCESS(KdbpSafeReadMemory(&ull, p, sizeof (ull))))
|
|
|
|
{
|
|
|
|
Ok = TRUE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
return FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Ok)
|
|
|
|
{
|
2021-04-27 08:23:37 +00:00
|
|
|
_snprintf(ErrMsg, 128, "Couldn't access memory at 0x%p", p);
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = Op->CharacterOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueStack[ValueStackPointer - 1] = ull;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RpnOpBinaryOperator:
|
|
|
|
if (ValueStackPointer < 2)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Value stack underflow");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValueStackPointer--;
|
|
|
|
ull = ValueStack[ValueStackPointer];
|
|
|
|
|
2011-09-03 22:21:56 +00:00
|
|
|
if (ull == 0 && Op->Data.BinaryOperator == RpnBinaryOperatorDiv)
|
2009-09-09 13:49:04 +00:00
|
|
|
{
|
2011-09-03 22:21:56 +00:00
|
|
|
CONST_STRCPY(ErrMsg, "Division by zero");
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = Op->CharacterOffset;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
ull = Op->Data.BinaryOperator(ValueStack[ValueStackPointer - 1], ull);
|
|
|
|
ValueStack[ValueStackPointer - 1] = ull;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
ASSERT(0);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-04-28 23:14:54 +00:00
|
|
|
#ifdef DEBUG_RPN
|
2009-09-09 13:49:04 +00:00
|
|
|
DPRINT1("Max value stack pointer: %d\n", ValueStackPointerMax);
|
2005-04-28 23:14:54 +00:00
|
|
|
#endif
|
2009-09-09 13:49:04 +00:00
|
|
|
|
|
|
|
if (ValueStackPointer != 1)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Stack not empty after evaluation");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*Result = ValueStack[0];
|
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Evaluates the given expression
|
|
|
|
*
|
|
|
|
* \param Expression Expression to evaluate.
|
|
|
|
* \param TrapFrame Register values.
|
|
|
|
* \param Result Variable which receives the result on success.
|
|
|
|
* \param ErrOffset Variable which receives character offset on parse error (-1 on other errors)
|
|
|
|
* \param ErrMsg Buffer which receives an error message on failure (128 bytes)
|
|
|
|
*
|
|
|
|
* \retval TRUE Success.
|
|
|
|
* \retval FALSE Failure.
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpRpnEvaluateExpression(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PCHAR Expression,
|
|
|
|
IN PKDB_KTRAP_FRAME TrapFrame,
|
|
|
|
OUT PULONGLONG Result,
|
|
|
|
OUT PLONG ErrOffset OPTIONAL,
|
|
|
|
OUT PCHAR ErrMsg OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PRPN_STACK Stack = (PRPN_STACK)&RpnStack;
|
|
|
|
|
|
|
|
ASSERT(Expression);
|
|
|
|
ASSERT(TrapFrame);
|
|
|
|
ASSERT(Result);
|
|
|
|
|
|
|
|
/* Clear the stack and parse the expression */
|
|
|
|
RpnpClearStack(Stack);
|
|
|
|
if (!RpnpParseExpression(Stack, Expression, NULL, 0, ErrOffset, ErrMsg))
|
|
|
|
return FALSE;
|
|
|
|
|
2005-04-28 23:14:54 +00:00
|
|
|
#ifdef DEBUG_RPN
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnpDumpStack(Stack);
|
2005-04-28 23:14:54 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Evaluate the stack */
|
|
|
|
if (!RpnpEvaluateStack(Stack, TrapFrame, Result, ErrOffset, ErrMsg))
|
|
|
|
return FALSE;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
return TRUE;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Parses the given expression and returns a "handle" to it.
|
|
|
|
*
|
|
|
|
* \param Expression Expression to evaluate.
|
|
|
|
* \param ErrOffset Variable which receives character offset on parse error (-1 on other errors)
|
|
|
|
* \param ErrMsg Buffer which receives an error message on failure (128 bytes)
|
|
|
|
*
|
|
|
|
* \returns "Handle" for the expression, NULL on failure.
|
|
|
|
*
|
|
|
|
* \sa KdbpRpnEvaluateExpression
|
|
|
|
*/
|
|
|
|
PVOID
|
|
|
|
KdbpRpnParseExpression(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PCHAR Expression,
|
|
|
|
OUT PLONG ErrOffset OPTIONAL,
|
|
|
|
OUT PCHAR ErrMsg OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
LONG Size;
|
|
|
|
PRPN_STACK Stack = (PRPN_STACK)&RpnStack;
|
|
|
|
PRPN_STACK NewStack;
|
|
|
|
|
|
|
|
ASSERT(Expression);
|
|
|
|
|
|
|
|
/* Clear the stack and parse the expression */
|
|
|
|
RpnpClearStack(Stack);
|
|
|
|
if (!RpnpParseExpression(Stack, Expression, NULL, 0, ErrOffset, ErrMsg))
|
|
|
|
return FALSE;
|
|
|
|
|
2005-04-28 23:14:54 +00:00
|
|
|
#ifdef DEBUG_RPN
|
2009-09-09 13:49:04 +00:00
|
|
|
RpnpDumpStack(Stack);
|
2005-04-28 23:14:54 +00:00
|
|
|
#endif
|
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Duplicate the stack and return a pointer/handle to it */
|
|
|
|
ASSERT(Stack->Sp >= 1);
|
|
|
|
Size = sizeof (RPN_STACK) + (RTL_FIELD_SIZE(RPN_STACK, Ops[0]) * (Stack->Sp - 1));
|
|
|
|
NewStack = ExAllocatePoolWithTag(NonPagedPool, Size, TAG_KDBG);
|
|
|
|
|
|
|
|
if (!NewStack)
|
|
|
|
{
|
|
|
|
CONST_STRCPY(ErrMsg, "Out of memory");
|
|
|
|
|
|
|
|
if (ErrOffset)
|
|
|
|
*ErrOffset = -1;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(NewStack, Stack, Size);
|
|
|
|
NewStack->Size = NewStack->Sp;
|
|
|
|
|
|
|
|
return NewStack;
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!\brief Evaluates the given expression and returns the result.
|
|
|
|
*
|
|
|
|
* \param Expression Expression "handle" returned by KdbpRpnParseExpression.
|
|
|
|
* \param TrapFrame Register values.
|
|
|
|
* \param Result Variable which receives the result on success.
|
|
|
|
* \param ErrOffset Variable which receives character offset on parse error (-1 on other errors)
|
|
|
|
* \param ErrMsg Buffer which receives an error message on failure (128 bytes)
|
|
|
|
*
|
|
|
|
* \returns "Handle" for the expression, NULL on failure.
|
|
|
|
*
|
|
|
|
* \sa KdbpRpnParseExpression
|
|
|
|
*/
|
|
|
|
BOOLEAN
|
|
|
|
KdbpRpnEvaluateParsedExpression(
|
2009-09-09 13:49:04 +00:00
|
|
|
IN PVOID Expression,
|
|
|
|
IN PKDB_KTRAP_FRAME TrapFrame,
|
|
|
|
OUT PULONGLONG Result,
|
|
|
|
OUT PLONG ErrOffset OPTIONAL,
|
|
|
|
OUT PCHAR ErrMsg OPTIONAL)
|
2005-04-28 23:14:54 +00:00
|
|
|
{
|
2009-09-09 13:49:04 +00:00
|
|
|
PRPN_STACK Stack = (PRPN_STACK)Expression;
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
ASSERT(Expression);
|
|
|
|
ASSERT(TrapFrame);
|
|
|
|
ASSERT(Result);
|
2005-04-28 23:14:54 +00:00
|
|
|
|
2009-09-09 13:49:04 +00:00
|
|
|
/* Evaluate the stack */
|
|
|
|
return RpnpEvaluateStack(Stack, TrapFrame, Result, ErrOffset, ErrMsg);
|
2005-04-28 23:14:54 +00:00
|
|
|
}
|
|
|
|
|