X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=trace_model.hpp;h=a74508efc5e0a0fc2411f9fe19b3e1aea070beda;hb=8f98c3a529e7ef88e4111ef22cf8411916a9a065;hp=8a87e0f65ed8443eef840252ce4dee7627817586;hpb=3495713d02b8f9ef28981ac54c22c4c8ee9b2299;p=apitrace diff --git a/trace_model.hpp b/trace_model.hpp index 8a87e0f..a74508e 100644 --- a/trace_model.hpp +++ b/trace_model.hpp @@ -23,15 +23,17 @@ * **************************************************************************/ +/* + * Object hierarchy for describing the traces in memory. + */ + #ifndef _TRACE_MODEL_HPP_ #define _TRACE_MODEL_HPP_ -#include +#include -#include #include -#include #include #include @@ -39,218 +41,315 @@ namespace Trace { -class Visitor; -class Dumper; -class UInt; +typedef unsigned Id; -class Value -{ -public: - virtual void visit(Visitor &visitor) = 0; +struct FunctionSig { + Id id; + const char *name; + unsigned num_args; + const char **arg_names; +}; - operator bool (void) const; - operator signed long long (void) const; - operator unsigned long long (void) const; - operator double (void) const; - void *blob(void) const; - const char *string(void) const; +struct StructSig { + Id id; + const char *name; + unsigned num_members; + const char **member_names; +}; - inline operator signed char (void) const { - return static_cast(*this); - } - inline operator unsigned char (void) const { - return static_cast(*this); - } +struct EnumSig { + Id id; + const char *name; + signed long long value; +}; - inline operator signed short (void) const { - return static_cast(*this); - } - inline operator unsigned short (void) const { - return static_cast(*this); - } +struct BitmaskFlag { + const char *name; + unsigned long long value; +}; - inline operator signed (void) const { - return static_cast(*this); - } - inline operator unsigned (void) const { - return static_cast(*this); - } +struct BitmaskSig { + Id id; + unsigned num_flags; + const BitmaskFlag *flags; +}; - inline operator signed long (void) const { - return static_cast(*this); - } - inline operator unsigned long (void) const { - return static_cast(*this); - } +class Visitor; - inline operator float (void) const { - return static_cast(*this); - } - const Value & operator[](size_t index) const; +class Value +{ +public: + virtual ~Value() {} + virtual void visit(Visitor &visitor) = 0; + + virtual bool toBool(void) const = 0; + virtual signed long long toSInt(void) const; + virtual unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + + virtual void *toPointer(void) const; + virtual void *toPointer(bool bind); + virtual unsigned long long toUIntPtr(void) const; + virtual const char *toString(void) const; + + const Value & operator[](size_t index) const; + + void dump(std::ostream &os, bool color=true); }; class Null : public Value { public: - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void *toPointer(void) const; + void *toPointer(bool bind); + unsigned long long toUIntPtr(void) const; + const char *toString(void) const; + void visit(Visitor &visitor); }; class Bool : public Value { public: - Bool(bool _value) : value(_value) {} + Bool(bool _value) : value(_value) {} - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void visit(Visitor &visitor); - bool value; + bool value; }; class SInt : public Value { public: - SInt(signed long long _value) : value(_value) {} + SInt(signed long long _value) : value(_value) {} - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void visit(Visitor &visitor); - signed long long value; + signed long long value; }; class UInt : public Value { public: - UInt(unsigned long long _value) : value(_value) {} + UInt(unsigned long long _value) : value(_value) {} - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void visit(Visitor &visitor); - unsigned long long value; + unsigned long long value; }; class Float : public Value { public: - Float(double _value) : value(_value) {} + Float(double _value) : value(_value) {} - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void visit(Visitor &visitor); - double value; + double value; }; class String : public Value { public: - String(std::string _value) : value(_value) {} + String(const char * _value) : value(_value) {} + ~String(); - void visit(Visitor &visitor); + bool toBool(void) const; + const char *toString(void) const; + void visit(Visitor &visitor); - std::string value; + const char * value; }; -class Const : public Value +class Enum : public Value { public: - Const(std::string _name, Value *_value) : name(_name), value(_value) {} + Enum(const EnumSig *_sig) : sig(_sig) {} - void visit(Visitor &visitor); + bool toBool(void) const; + signed long long toSInt(void) const; + unsigned long long toUInt(void) const; + virtual float toFloat(void) const; + virtual double toDouble(void) const; + void visit(Visitor &visitor); - std::string name; - Value *value; + const EnumSig *sig; +}; + + +class Bitmask : public UInt +{ +public: + Bitmask(const BitmaskSig *_sig, unsigned long long _value) : UInt(_value), sig(_sig) {} + + void visit(Visitor &visitor); + + const BitmaskSig *sig; +}; + + +class Struct : public Value +{ +public: + Struct(StructSig *_sig) : sig(_sig), members(_sig->num_members) { } + ~Struct(); + + bool toBool(void) const; + void visit(Visitor &visitor); + + const StructSig *sig; + std::vector members; }; class Array : public Value { public: - Array(size_t len) : values(len) {} + Array(size_t len) : values(len) {} + ~Array(); - void visit(Visitor &visitor); + bool toBool(void) const; + void visit(Visitor &visitor); - std::vector values; + std::vector values; }; class Blob : public Value { public: - Blob(size_t _size) { - size = _size; - buf = new char[_size]; - } + Blob(size_t _size) { + size = _size; + buf = new char[_size]; + bound = false; + } + + ~Blob(); + + bool toBool(void) const; + void *toPointer(void) const; + void *toPointer(bool bind); + void visit(Visitor &visitor); + + size_t size; + char *buf; + bool bound; +}; - ~Blob() { - delete [] buf; - } - void visit(Visitor &visitor); +class Pointer : public UInt +{ +public: + Pointer(unsigned long long value) : UInt(value) {} - size_t size; - char *buf; + bool toBool(void) const; + void *toPointer(void) const; + void *toPointer(bool bind); + unsigned long long toUIntPtr(void) const; + void visit(Visitor &visitor); }; class Visitor { public: - virtual void visit(Null *) {assert(0);} - virtual void visit(Bool *) {assert(0);} - virtual void visit(SInt *) {assert(0);} - virtual void visit(UInt *) {assert(0);} - virtual void visit(Float *) {assert(0);} - virtual void visit(String *) {assert(0);} - virtual void visit(Const *) {assert(0);} - virtual void visit(Array *) {assert(0);} - virtual void visit(Blob *) {assert(0);} + virtual void visit(Null *); + virtual void visit(Bool *); + virtual void visit(SInt *); + virtual void visit(UInt *); + virtual void visit(Float *); + virtual void visit(String *); + virtual void visit(Enum *); + virtual void visit(Bitmask *); + virtual void visit(Struct *); + virtual void visit(Array *); + virtual void visit(Blob *); + virtual void visit(Pointer *); protected: - inline void _visit(Value *value) { - if (value) { - value->visit(*this); - } - } + inline void _visit(Value *value) { + if (value) { + value->visit(*this); + } + } }; -std::ostream & operator <<(std::ostream &os, Value *value); - +inline std::ostream & operator <<(std::ostream &os, Value *value) { + if (value) { + value->dump(os); + } + return os; +} -signed long long asSInt(const Value &node); -unsigned long long asUInt(const Value &node); -double asFloat(const Value &node); - - -typedef std::pair Arg; class Call { public: - unsigned no; - std::string name; - std::vector args; - Value *ret; + unsigned no; + const FunctionSig *sig; + std::vector args; + Value *ret; - Call() : ret(0) { } + Call(FunctionSig *_sig) : sig(_sig), args(_sig->num_args), ret(0) { } + ~Call(); - inline Value & arg(unsigned index) { - return *(args[index].second); - } -}; + inline const char * name(void) const { + return sig->name; + } + inline Value & arg(unsigned index) { + assert(index < args.size()); + return *(args[index]); + } + + void dump(std::ostream &os, bool color=true); +}; -std::ostream & operator <<(std::ostream &os, Call &call); +inline std::ostream & operator <<(std::ostream &os, Call &call) { + call.dump(os); + return os; +} } /* namespace Trace */