summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/debug/express.h
blob: 1caee590d5a28911ae49c295f3171e07290ed16e (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************

    express.h

    Generic expressions engine.

***************************************************************************/

#ifndef MAME_EMU_DEBUG_EXPRESS_H
#define MAME_EMU_DEBUG_EXPRESS_H

#pragma once

#include "emucore.h"

#include <deque>
#include <functional>
#include <list>
#include <unordered_map>



//**************************************************************************
//  CONSTANTS
//**************************************************************************

// values for the address space passed to external_read/write_memory
enum expression_space
{
	EXPSPACE_INVALID,
	EXPSPACE_PROGRAM_LOGICAL,
	EXPSPACE_DATA_LOGICAL,
	EXPSPACE_IO_LOGICAL,
	EXPSPACE_SPACE3_LOGICAL,
	EXPSPACE_PROGRAM_PHYSICAL,
	EXPSPACE_DATA_PHYSICAL,
	EXPSPACE_IO_PHYSICAL,
	EXPSPACE_SPACE3_PHYSICAL,
	EXPSPACE_OPCODE,
	EXPSPACE_RAMWRITE,
	EXPSPACE_REGION
};



//**************************************************************************
//  TYPE DEFINITIONS
//**************************************************************************

using offs_t = u32;

// ======================> expression_error

// an expression_error holds an error code and a string offset
class expression_error
{
public:
	// codes
	enum error_code
	{
		NONE,
		NOT_LVAL,
		NOT_RVAL,
		SYNTAX,
		UNKNOWN_SYMBOL,
		INVALID_NUMBER,
		INVALID_TOKEN,
		STACK_OVERFLOW,
		STACK_UNDERFLOW,
		UNBALANCED_PARENS,
		DIVIDE_BY_ZERO,
		OUT_OF_MEMORY,
		INVALID_PARAM_COUNT,
		UNBALANCED_QUOTES,
		TOO_MANY_STRINGS,
		INVALID_MEMORY_SIZE,
		INVALID_MEMORY_SPACE,
		NO_SUCH_MEMORY_SPACE,
		INVALID_MEMORY_NAME,
		MISSING_MEMORY_NAME
	};

	// construction/destruction
	expression_error(error_code code, int offset = 0)
		: m_code(code),
			m_offset(offset) { }

	// operators
	operator error_code() const { return m_code; }

	// getters
	error_code code() const { return m_code; }
	int offset() const { return m_offset; }
	const char *code_string() const;

private:
	// internal state
	error_code          m_code;
	int                 m_offset;
};


// ======================> symbol_entry

// symbol_entry describes a symbol in a symbol table
class symbol_entry
{
protected:
	// symbol types
	enum symbol_type
	{
		SMT_INTEGER,
		SMT_FUNCTION
	};

	// construction/destruction
	symbol_entry(symbol_table &table, symbol_type type, const char *name, const std::string &format);
public:
	virtual ~symbol_entry();

	// getters
	const char *name() const { return m_name.c_str(); }
	const std::string &format() const { return m_format; }

	// type checking
	bool is_function() const { return (m_type == SMT_FUNCTION); }

	// symbol access
	virtual bool is_lval() const = 0;
	virtual u64 value() const = 0;
	virtual void set_value(u64 newvalue) = 0;

protected:
	// internal state
	symbol_table &  m_table;                    // pointer back to the owning table
	symbol_type     m_type;                     // type of symbol
	std::string     m_name;                     // name of the symbol
	std::string     m_format;                   // format of symbol (or empty if unspecified)
};


// ======================> symbol_table

// a symbol_table holds symbols of various types which the expression engine
// queries to look up symbols
class symbol_table
{
public:
	// callback functions for getting/setting a symbol value
	typedef std::function<u64()> getter_func;
	typedef std::function<void(u64 value)> setter_func;

	// callback functions for function execution
	typedef std::function<u64(int numparams, const u64 *paramlist)> execute_func;

	// callback functions for memory reads/writes
	typedef std::function<void()> memory_modified_func;

	enum read_write
	{
		READ_ONLY = 0,
		READ_WRITE
	};

	// construction/destruction
	symbol_table(running_machine &machine, symbol_table *parent = nullptr, device_t *device = nullptr);

	// getters
	const std::unordered_map<std::string, std::unique_ptr<symbol_entry>> &entries() const { return m_symlist; }
	symbol_table *parent() const { return m_parent; }

	// setters
	void set_memory_modified_func(memory_modified_func modified);

	// symbol access
	void add(const char *name, read_write rw, u64 *ptr = nullptr);
	void add(const char *name, u64 constvalue);
	void add(const char *name, getter_func getter, setter_func setter = nullptr, const std::string &format_string = "");
	void add(const char *name, int minparams, int maxparams, execute_func execute);
	symbol_entry *find(const char *name) const { if (name) { auto search = m_symlist.find(name); if (search != m_symlist.end()) return search->second.get(); else return nullptr; } else return nullptr; }
	symbol_entry *find_deep(const char *name);

	// value getter/setter
	u64 value(const char *symbol);
	void set_value(const char *symbol, u64 value);

	// memory accessors
	expression_error::error_code memory_valid(const char *name, expression_space space);
	u64 memory_value(const char *name, expression_space space, u32 offset, int size, bool disable_se);
	void set_memory_value(const char *name, expression_space space, u32 offset, int size, u64 value, bool disable_se);
	u64 read_memory(address_space &space, offs_t address, int size, bool apply_translation);
	void write_memory(address_space &space, offs_t address, u64 data, int size, bool apply_translation);

private:
	// memory helpers
	u64 read_program_direct(address_space &space, int opcode, offs_t address, int size);
	u64 read_memory_region(const char *rgntag, offs_t address, int size);
	void write_program_direct(address_space &space, int opcode, offs_t address, int size, u64 data);
	void write_memory_region(const char *rgntag, offs_t address, int size, u64 data);
	device_t *expression_get_device(const char *tag);
	void notify_memory_modified();

	// internal state
	running_machine &       m_machine;          // reference to the machine
	symbol_table *          m_parent;           // pointer to the parent symbol table
	std::unordered_map<std::string,std::unique_ptr<symbol_entry>> m_symlist;        // list of symbols
	device_memory_interface *const m_memintf;   // pointer to the local memory interface (if any)
	memory_modified_func    m_memory_modified;  // memory modified callback
};


// ======================> parsed_expression

// a parsed_expression holds a pre-parsed expression that can be
// efficiently executed at a later time
class parsed_expression
{
public:
	// construction/destruction
	parsed_expression(symbol_table &symtable, const char *expression = nullptr, int default_base = 16);
	parsed_expression(const parsed_expression &src);
	parsed_expression(parsed_expression &&src) = default;

	// operators
	parsed_expression &operator=(const parsed_expression &src) { copy(src); return *this; }
	parsed_expression &operator=(parsed_expression &&src) = default;

	// getters
	bool is_empty() const { return (m_tokenlist.count() == 0); }
	const char *original_string() const { return m_original_string.c_str(); }
	symbol_table &symbols() const { return m_symtable.get(); }

	// setters
	void set_symbols(symbol_table &symtable) { m_symtable = std::reference_wrapper<symbol_table>(symtable); }
	void set_default_base(int base) { assert(base == 8 || base == 10 || base == 16); m_default_base = base; }

	// execution
	void parse(const char *string);
	u64 execute() { return execute_tokens(); }

private:
	// a single token
	class parse_token
	{
		friend class simple_list<parse_token>;

		// operator flags
		enum
		{
			TIN_OPTYPE_SHIFT        = 0,        // 8 bits (0-7)
			TIN_OPTYPE_MASK         = 0xff << TIN_OPTYPE_SHIFT,
			TIN_RIGHT_TO_LEFT_SHIFT = 16,       // 1 bit  (16)
			TIN_RIGHT_TO_LEFT_MASK  = 1 << TIN_RIGHT_TO_LEFT_SHIFT,
			TIN_FUNCTION_SHIFT      = 17,       // 1 bit  (17)
			TIN_FUNCTION_MASK       = 1 << TIN_FUNCTION_SHIFT,
			TIN_MEMORY_SIZE_SHIFT   = 18,       // 2 bits (18-19)
			TIN_MEMORY_SIZE_MASK    = 3 << TIN_MEMORY_SIZE_SHIFT,
			TIN_MEMORY_SPACE_SHIFT  = 20,       // 4 bits (20-23)
			TIN_MEMORY_SPACE_MASK   = 0xf << TIN_MEMORY_SPACE_SHIFT,
			TIN_PRECEDENCE_SHIFT    = 24,       // 5 bits (24-28)
			TIN_PRECEDENCE_MASK     = 0x1f << TIN_PRECEDENCE_SHIFT,
			TIN_SIDE_EFFECT_SHIFT   = 29,       // 1 bit  (29)
			TIN_SIDE_EFFECT_MASK    = 1 << TIN_SIDE_EFFECT_SHIFT
		};

		// types of tokens
		enum token_type
		{
			INVALID = 0,
			NUMBER,
			STRING,
			MEMORY,
			SYMBOL,
			OPERATOR
		};

	public:
		// construction/destruction
		parse_token(int offset = 0);

		// getters
		parse_token *next() const { return m_next; }
		int offset() const { return m_offset; }
		bool is_number() const { return (m_type == NUMBER); }
		bool is_string() const { return (m_type == STRING); }
		bool is_memory() const { return (m_type == MEMORY); }
		bool is_symbol() const { return (m_type == SYMBOL); }
		bool is_operator() const { return (m_type == OPERATOR); }
		bool is_operator(u8 type) const { return (m_type == OPERATOR && optype() == type); }
		bool is_lval() const { return ((m_type == SYMBOL && m_symbol->is_lval()) || m_type == MEMORY); }

		u64 value() const { assert(m_type == NUMBER); return m_value; }
		u32 address() const { assert(m_type == MEMORY); return m_value; }
		symbol_entry *symbol() const { assert(m_type == SYMBOL); return m_symbol; }

		u8 optype() const { assert(m_type == OPERATOR); return (m_flags & TIN_OPTYPE_MASK) >> TIN_OPTYPE_SHIFT; }
		u8 precedence() const { assert(m_type == OPERATOR); return (m_flags & TIN_PRECEDENCE_MASK) >> TIN_PRECEDENCE_SHIFT; }
		bool is_function_separator() const { assert(m_type == OPERATOR); return ((m_flags & TIN_FUNCTION_MASK) != 0); }
		bool right_to_left() const { assert(m_type == OPERATOR); return ((m_flags & TIN_RIGHT_TO_LEFT_MASK) != 0); }
		expression_space memory_space() const { assert(m_type == OPERATOR || m_type == MEMORY); return expression_space((m_flags & TIN_MEMORY_SPACE_MASK) >> TIN_MEMORY_SPACE_SHIFT); }
		int memory_size() const { assert(m_type == OPERATOR || m_type == MEMORY); return (m_flags & TIN_MEMORY_SIZE_MASK) >> TIN_MEMORY_SIZE_SHIFT; }
		bool memory_side_effects() const { assert(m_type == OPERATOR || m_type == MEMORY); return (m_flags & TIN_SIDE_EFFECT_MASK) >> TIN_SIDE_EFFECT_SHIFT; }

		// setters
		parse_token &set_offset(int offset) { m_offset = offset; return *this; }
		parse_token &set_offset(const parse_token &src) { m_offset = src.m_offset; return *this; }
		parse_token &set_offset(const parse_token &src1, const parse_token &src2) { m_offset = std::min(src1.m_offset, src2.m_offset); return *this; }
		parse_token &configure_number(u64 value) { m_type = NUMBER; m_value = value; return *this; }
		parse_token &configure_string(const char *string) { m_type = STRING; m_string = string; return *this; }
		parse_token &configure_memory(u32 address, parse_token &memoryat) { m_type = MEMORY; m_value = address; m_flags = memoryat.m_flags; m_string = memoryat.m_string; return *this; }
		parse_token &configure_symbol(symbol_entry &symbol) { m_type = SYMBOL; m_symbol = &symbol; return *this; }
		parse_token &configure_operator(u8 optype, u8 precedence)
			{ m_type = OPERATOR; m_flags = ((optype << TIN_OPTYPE_SHIFT) & TIN_OPTYPE_MASK) | ((precedence << TIN_PRECEDENCE_SHIFT) & TIN_PRECEDENCE_MASK); return *this; }

		parse_token &set_function_separator() { assert(m_type == OPERATOR); m_flags |= TIN_FUNCTION_MASK; return *this; }
		parse_token &set_right_to_left() { assert(m_type == OPERATOR); m_flags |= TIN_RIGHT_TO_LEFT_MASK; return *this; }
		parse_token &set_memory_space(expression_space space) { assert(m_type == OPERATOR || m_type == MEMORY); m_flags = (m_flags & ~TIN_MEMORY_SPACE_MASK) | ((space << TIN_MEMORY_SPACE_SHIFT) & TIN_MEMORY_SPACE_MASK); return *this; }
		parse_token &set_memory_size(int log2ofbits) { assert(m_type == OPERATOR || m_type == MEMORY); m_flags = (m_flags & ~TIN_MEMORY_SIZE_MASK) | ((log2ofbits << TIN_MEMORY_SIZE_SHIFT) & TIN_MEMORY_SIZE_MASK); return *this; }
		parse_token &set_memory_side_effects(bool disable_se) { assert(m_type == OPERATOR || m_type == MEMORY); m_flags = disable_se ? m_flags | TIN_SIDE_EFFECT_MASK : m_flags & ~TIN_SIDE_EFFECT_MASK; return *this; }
		parse_token &set_memory_source(const char *string) { assert(m_type == OPERATOR || m_type == MEMORY); m_string = string; return *this; }

		// access
		u64 get_lval_value(symbol_table &symtable);
		void set_lval_value(symbol_table &symtable, u64 value);

	private:
		// internal state
		parse_token *           m_next;             // next token in list
		token_type              m_type;             // type of token
		int                     m_offset;           // offset within the string
		u64                     m_value;            // integral value
		u32                     m_flags;            // additional flags/info
		const char *            m_string;           // associated string
		symbol_entry *          m_symbol;           // symbol pointer
	};

	// internal helpers
	void copy(const parsed_expression &src);
	void print_tokens(FILE *out);

	// parsing helpers
	void parse_string_into_tokens();
	void parse_symbol_or_number(parse_token &token, const char *&string);
	void parse_number(parse_token &token, const char *string, int base, expression_error::error_code errcode);
	void parse_quoted_char(parse_token &token, const char *&string);
	void parse_quoted_string(parse_token &token, const char *&string);
	void parse_memory_operator(parse_token &token, const char *string, bool disable_se);
	void normalize_operator(parse_token *prevtoken, parse_token &thistoken);
	void infix_to_postfix();

	// execution helpers
	void push_token(parse_token &token);
	void pop_token(parse_token &token);
	void pop_token_lval(parse_token &token);
	void pop_token_rval(parse_token &token);
	u64 execute_tokens();
	void execute_function(parse_token &token);

	// constants
	static const int MAX_FUNCTION_PARAMS = 16;

	// internal state
	std::reference_wrapper<symbol_table> m_symtable;    // symbol table
	int                 m_default_base;                 // default base
	std::string         m_original_string;              // original string (prior to parsing)
	simple_list<parse_token> m_tokenlist;               // token list
	std::list<std::string> m_stringlist;                // string list
	std::deque<parse_token> m_token_stack;              // token stack (used during execution)
};

#endif // MAME_EMU_DEBUG_EXPRESS_H