fxos/include/fxos/operands.h

104 lines
2.4 KiB
C++

#ifndef LIBFXOS_OPERANDS_H
#define LIBFXOS_OPERANDS_H
#include <fxos/lang.h>
namespace FxOS {
//---
// Data type representation
//
// The abstract interpreter supports the following data types when analyzing
// data movement and access:
// Integers i8 u8 i16 u16 i32 u32 (regs, mem)
// Bit fields over ints T { <fields } (regs, mem)
// Arrays of any type T[] T[n] (mem)
// Strings char[] char[n] (mem)
// Structures struct { <fields> } (mem)
//---
class DataType
{
public:
enum DataKind {
/* Base types */
Integral,
/* Bit fields over integers */
BitField,
/* Structures (can only reside in memory) */
Struct,
/* Arrays (can only reside in memory) */
Array,
};
/* Type kind */
DataKind kind;
/* Type size in bytes, as would be returned by sizeof(). Must be 1, 2
or 4 for integral types and bit fields. Might be 0 for arrays if the
size of the array is unknown */
uint16_t size;
/* Type alignment, can only be 1, 2 or 4 */
uint16_t align;
private:
/* Type of fields in bit fields */
using Field = std::pair<int, std::string>;
union {
/* For integer types of size 1, whether to display as char
(might be extended to more attributes later) */
bool ischar;
/* For bit field types */
std::vector<Field> fields;
/* For struct types */
std::vector<DataType> attributes;
/* For array types, number of elements (0 if unknown or
variable-size NUL-terminated strings) */
int elements;
};
};
enum class OperandKind {
/* CPU-held registers accessed with instructions */
CpuRegister,
/* Standard randomly-addressable memory */
Memory,
/* Memory-mapped module registers with specific access */
MappedModule,
};
class Operand
{
public:
/* Returns the operand kind (which is also the subclass identity) */
virtual OperandKind type() const noexcept = 0;
/* String representation */
virtual std::string str() const noexcept = 0;
};
class RegisterOperand: public Operand
{
public:
RegisterOperand(CpuRegister name): m_name(name) {}
OperandKind type() const noexcept override {
return OperandKind::CpuRegister;
}
CpuRegister name() const noexcept {
return m_name;
}
std::string str() const noexcept override {
return m_name.str();
}
private:
/* Register name for assembler listings */
CpuRegister m_name;
};
} /* namespace FxOS */
#endif /* LIBFXOS_OPERANDS_H */