2011-02-07 05:55:31 +01:00
|
|
|
#ifndef __vtype_H
|
|
|
|
|
#define __vtype_H
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2011 Stephen Williams (steve@icarus.com)
|
|
|
|
|
*
|
|
|
|
|
* This source code is free software; you can redistribute it
|
|
|
|
|
* and/or modify it in source code form 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2011-02-14 04:01:21 +01:00
|
|
|
# include <iostream>
|
2011-10-02 02:04:04 +02:00
|
|
|
# include <list>
|
2011-02-14 01:37:10 +01:00
|
|
|
# include <vector>
|
|
|
|
|
# include <climits>
|
2011-04-18 02:19:09 +02:00
|
|
|
# include <inttypes.h>
|
2011-02-07 05:55:31 +01:00
|
|
|
# include "StringHeap.h"
|
|
|
|
|
|
2011-10-16 02:41:48 +02:00
|
|
|
class Expression;
|
2011-11-05 23:55:17 +01:00
|
|
|
class prange_t;
|
2011-10-16 02:41:48 +02:00
|
|
|
|
2011-02-14 01:37:10 +01:00
|
|
|
/*
|
|
|
|
|
* A description of a VHDL type consists of a graph of VType
|
|
|
|
|
* objects. Derived types are specific kinds of types, and those that
|
|
|
|
|
* are compound may in turn reference other types.
|
|
|
|
|
*/
|
2011-02-07 05:55:31 +01:00
|
|
|
class VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VType() { }
|
|
|
|
|
virtual ~VType() =0;
|
2011-02-14 04:01:21 +01:00
|
|
|
|
2011-07-20 04:19:27 +02:00
|
|
|
// This virtual method writes a VHDL-accurate representation
|
|
|
|
|
// of this type to the designated stream. This is used for
|
|
|
|
|
// writing parsed types to library files.
|
|
|
|
|
virtual void write_to_stream(std::ostream&fd) const;
|
|
|
|
|
|
2011-10-02 02:04:04 +02:00
|
|
|
// This virtual method writes a human-readable version of the
|
|
|
|
|
// type to a given file for debug purposes. (Question: is this
|
|
|
|
|
// really necessary given the write_to_stream method?)
|
2011-02-14 04:01:21 +01:00
|
|
|
virtual void show(std::ostream&) const;
|
2011-03-27 21:01:58 +02:00
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
// This virtual method emits a definition for the specific
|
|
|
|
|
// type. It is used to emit typedef's.
|
|
|
|
|
virtual int emit_def(std::ostream&out, perm_string name) const =0;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
friend class decl_t;
|
|
|
|
|
// This virtual method is called to emit the declaration. This
|
|
|
|
|
// is used by the decl_t object to emit variable/wire/port declarations.
|
|
|
|
|
virtual int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const =0;
|
|
|
|
|
|
2011-03-27 21:01:58 +02:00
|
|
|
public:
|
2011-10-02 19:56:00 +02:00
|
|
|
// A couple places use the VType along with a few
|
|
|
|
|
// per-declaration details, so provide a common structure for
|
|
|
|
|
// holding that stuff together.
|
2011-03-27 21:01:58 +02:00
|
|
|
struct decl_t {
|
2011-10-02 19:56:00 +02:00
|
|
|
decl_t() : type(0), reg_flag(false) { }
|
2011-03-27 21:01:58 +02:00
|
|
|
int emit(std::ostream&out, perm_string name) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
|
|
|
|
|
const VType*type;
|
2011-05-16 01:17:51 +02:00
|
|
|
bool reg_flag;
|
2011-03-27 21:01:58 +02:00
|
|
|
};
|
2011-10-02 19:56:00 +02:00
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
};
|
|
|
|
|
|
2011-02-14 04:01:21 +01:00
|
|
|
inline std::ostream&operator << (std::ostream&out, const VType&item)
|
|
|
|
|
{
|
|
|
|
|
item.show(out);
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
extern void preload_global_types(void);
|
2011-02-19 22:08:26 +01:00
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
/*
|
2012-05-14 18:58:49 +02:00
|
|
|
* This class represents the primitive types that are available to the
|
2011-02-07 05:55:31 +01:00
|
|
|
* type subsystem.
|
|
|
|
|
*/
|
|
|
|
|
class VTypePrimitive : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
enum type_t { BOOLEAN, BIT, INTEGER, STDLOGIC };
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VTypePrimitive(type_t);
|
|
|
|
|
~VTypePrimitive();
|
|
|
|
|
|
2011-07-20 04:19:27 +02:00
|
|
|
void write_to_stream(std::ostream&fd) const;
|
2011-02-14 04:01:21 +01:00
|
|
|
void show(std::ostream&) const;
|
|
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
type_t type() const { return type_; }
|
|
|
|
|
|
2011-10-02 19:56:00 +02:00
|
|
|
int emit_primitive_type(std::ostream&fd) const;
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
private:
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
private:
|
|
|
|
|
type_t type_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-07-20 14:04:43 +02:00
|
|
|
extern const VTypePrimitive* primitive_BOOLEAN;
|
|
|
|
|
extern const VTypePrimitive* primitive_BIT;
|
|
|
|
|
extern const VTypePrimitive* primitive_INTEGER;
|
|
|
|
|
extern const VTypePrimitive* primitive_STDLOGIC;
|
2011-02-07 05:55:31 +01:00
|
|
|
|
2011-02-14 01:37:10 +01:00
|
|
|
/*
|
|
|
|
|
* An array is a compound N-dimensional array of element type. The
|
|
|
|
|
* construction of the array is from an element type and a vector of
|
|
|
|
|
* ranges. The array type can be left incomplete by leaving some
|
|
|
|
|
* ranges as "box" ranges, meaning present but not defined.
|
|
|
|
|
*/
|
2011-02-07 05:55:31 +01:00
|
|
|
class VTypeArray : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
2011-02-14 01:37:10 +01:00
|
|
|
class range_t {
|
|
|
|
|
public:
|
2011-10-16 02:41:48 +02:00
|
|
|
range_t() : msb_(0), lsb_(0) { }
|
|
|
|
|
range_t(Expression*m, Expression*l) : msb_(m), lsb_(l) { }
|
2011-02-14 01:37:10 +01:00
|
|
|
|
2011-10-16 02:41:48 +02:00
|
|
|
bool is_box() const { return msb_==0 && lsb_==0; }
|
2011-02-14 01:37:10 +01:00
|
|
|
|
2011-10-16 02:41:48 +02:00
|
|
|
Expression* msb() const { return msb_; }
|
|
|
|
|
Expression* lsb() const { return lsb_; }
|
2011-02-14 01:37:10 +01:00
|
|
|
|
|
|
|
|
private:
|
2011-10-16 02:41:48 +02:00
|
|
|
Expression* msb_;
|
|
|
|
|
Expression* lsb_;
|
2011-02-14 01:37:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
2011-02-26 05:09:31 +01:00
|
|
|
VTypeArray(const VType*etype, const std::vector<range_t>&r, bool signed_vector =false);
|
2011-11-05 23:55:17 +01:00
|
|
|
VTypeArray(const VType*etype, std::list<prange_t*>*r, bool signed_vector =false);
|
2011-02-07 05:55:31 +01:00
|
|
|
~VTypeArray();
|
|
|
|
|
|
2011-07-20 04:19:27 +02:00
|
|
|
void write_to_stream(std::ostream&fd) const;
|
2011-02-14 04:01:21 +01:00
|
|
|
void show(std::ostream&) const;
|
|
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
size_t dimensions() const;
|
2011-02-14 04:01:21 +01:00
|
|
|
const range_t&dimension(size_t idx) const
|
|
|
|
|
{ return ranges_[idx]; }
|
|
|
|
|
|
2011-02-26 05:09:31 +01:00
|
|
|
bool signed_vector() const { return signed_flag_; }
|
|
|
|
|
|
2011-02-14 01:37:10 +01:00
|
|
|
const VType* element_type() const;
|
2011-02-07 05:55:31 +01:00
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
|
|
|
|
|
2011-10-02 19:56:00 +02:00
|
|
|
private:
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
private:
|
2011-02-14 01:37:10 +01:00
|
|
|
const VType*etype_;
|
|
|
|
|
|
|
|
|
|
std::vector<range_t> ranges_;
|
2011-02-26 05:09:31 +01:00
|
|
|
bool signed_flag_;
|
2011-02-07 05:55:31 +01:00
|
|
|
};
|
|
|
|
|
|
2011-04-18 02:19:09 +02:00
|
|
|
class VTypeRange : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VTypeRange(const VType*base, int64_t max_val, int64_t min_val);
|
|
|
|
|
~VTypeRange();
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
private:
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
2011-04-18 02:19:09 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const VType*base_;
|
|
|
|
|
int64_t max_, min_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-02 02:04:04 +02:00
|
|
|
class VTypeEnum : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VTypeEnum(const std::list<perm_string>*names);
|
|
|
|
|
~VTypeEnum();
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
void show(std::ostream&) const;
|
|
|
|
|
|
|
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
2011-10-02 19:56:00 +02:00
|
|
|
private:
|
2011-10-10 00:25:35 +02:00
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
2011-10-02 02:04:04 +02:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<perm_string>names_;
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-17 23:03:19 +01:00
|
|
|
class VTypeRecord : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
class element_t {
|
|
|
|
|
public:
|
|
|
|
|
element_t(perm_string name, const VType*type);
|
|
|
|
|
|
|
|
|
|
void show(std::ostream&) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
perm_string name_;
|
|
|
|
|
const VType*type_;
|
|
|
|
|
|
|
|
|
|
private:// Not implement
|
|
|
|
|
element_t(const element_t&);
|
|
|
|
|
element_t& operator= (const element_t);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
explicit VTypeRecord(std::list<element_t*>*elements);
|
|
|
|
|
~VTypeRecord();
|
|
|
|
|
|
|
|
|
|
void show(std::ostream&) const;
|
|
|
|
|
|
|
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
|
|
|
|
private:
|
|
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::vector<element_t*> elements_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
class VTypeDef : public VType {
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
VTypeDef(perm_string name, const VType*is);
|
|
|
|
|
~VTypeDef();
|
|
|
|
|
|
|
|
|
|
int emit_typedef(std::ostream&out) const;
|
|
|
|
|
|
|
|
|
|
int emit_def(std::ostream&out, perm_string name) const;
|
|
|
|
|
private:
|
|
|
|
|
int emit_decl(std::ostream&out, perm_string name, bool reg_flag) const;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
perm_string name_;
|
|
|
|
|
const VType*type_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-02-07 05:55:31 +01:00
|
|
|
#endif
|