2011-03-27 21:01:58 +02:00
|
|
|
/*
|
2012-04-09 04:06:58 +02:00
|
|
|
* Copyright (c) 2011-2012 Stephen Williams (steve@icarus.com)
|
2014-08-06 15:00:35 +02:00
|
|
|
* Copyright (c) 2014 CERN / Maciej Suminski (maciej.suminski@cern.ch)
|
2011-03-27 21:01:58 +02:00
|
|
|
*
|
|
|
|
|
* 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
|
2012-08-29 03:41:23 +02:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2011-03-27 21:01:58 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# include "vtype.h"
|
2011-10-16 02:41:48 +02:00
|
|
|
# include "expression.h"
|
2014-08-22 10:49:55 +02:00
|
|
|
# include <sstream>
|
2011-03-27 21:01:58 +02:00
|
|
|
# include <typeinfo>
|
|
|
|
|
# include <cassert>
|
|
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
2011-10-02 19:56:00 +02:00
|
|
|
|
2011-03-27 21:01:58 +02:00
|
|
|
int VType::decl_t::emit(ostream&out, perm_string name) const
|
|
|
|
|
{
|
2011-10-10 00:25:35 +02:00
|
|
|
return type->emit_decl(out, name, reg_flag);
|
2011-10-02 19:56:00 +02:00
|
|
|
}
|
|
|
|
|
|
2012-04-29 03:37:22 +02:00
|
|
|
int VType::emit_decl(ostream&out, perm_string name, bool reg_flag) const
|
2011-10-02 19:56:00 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
2011-11-06 18:01:02 +01:00
|
|
|
|
2012-04-29 03:37:22 +02:00
|
|
|
if (!reg_flag)
|
|
|
|
|
out << "wire ";
|
2011-10-02 19:56:00 +02:00
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
errors += emit_def(out, name);
|
|
|
|
|
out << " ";
|
2011-10-02 19:56:00 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 03:38:49 +02:00
|
|
|
int VType::emit_typedef(std::ostream&, typedef_context_t&) const
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeERROR::emit_def(ostream&out, perm_string) const
|
2012-11-03 03:30:12 +01:00
|
|
|
{
|
|
|
|
|
out << "/* ERROR */";
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeArray::emit_def(ostream&out, perm_string name) const
|
2011-10-02 19:56:00 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
2012-04-29 03:37:22 +02:00
|
|
|
|
|
|
|
|
const VTypeArray*cur = this;
|
|
|
|
|
while (const VTypeArray*sub = dynamic_cast<const VTypeArray*> (cur->etype_)) {
|
|
|
|
|
cur = sub;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 01:55:29 +02:00
|
|
|
const VType*raw_base = cur->etype_;
|
|
|
|
|
const VTypePrimitive*base = dynamic_cast<const VTypePrimitive*> (raw_base);
|
|
|
|
|
|
|
|
|
|
if (base) {
|
|
|
|
|
assert(dimensions() == 1);
|
|
|
|
|
|
2014-08-25 16:57:27 +02:00
|
|
|
base->emit_def(out, empty_perm_string);
|
2012-05-06 01:55:29 +02:00
|
|
|
if (signed_flag_)
|
2014-08-25 16:57:27 +02:00
|
|
|
out << " signed";
|
2014-08-22 10:49:55 +02:00
|
|
|
} else {
|
2014-08-25 16:57:27 +02:00
|
|
|
raw_base->emit_def(out, empty_perm_string);
|
2014-08-22 10:49:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(raw_base->can_be_packed()) {
|
|
|
|
|
errors += emit_dimensions(out);
|
2014-08-25 16:57:27 +02:00
|
|
|
emit_name(out, name);
|
2012-05-06 01:55:29 +02:00
|
|
|
} else {
|
2014-08-25 16:57:27 +02:00
|
|
|
emit_name(out, name);
|
2014-08-22 10:49:55 +02:00
|
|
|
errors += emit_dimensions(out);
|
2012-05-06 01:55:29 +02:00
|
|
|
}
|
2012-04-29 03:37:22 +02:00
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VTypeArray::emit_typedef(std::ostream&out, typedef_context_t&ctx) const
|
|
|
|
|
{
|
|
|
|
|
return etype_->emit_typedef(out, ctx);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int VTypeArray::emit_dimensions(std::ostream&out) const
|
|
|
|
|
{
|
|
|
|
|
int errors = 0;
|
|
|
|
|
|
|
|
|
|
list<const VTypeArray*> dims;
|
|
|
|
|
const VTypeArray*cur = this;
|
|
|
|
|
while (const VTypeArray*sub = dynamic_cast<const VTypeArray*> (cur->etype_)) {
|
|
|
|
|
dims.push_back(cur);
|
|
|
|
|
cur = sub;
|
|
|
|
|
}
|
2012-04-29 03:37:22 +02:00
|
|
|
dims.push_back(cur);
|
2011-10-10 00:25:35 +02:00
|
|
|
|
2012-04-29 03:37:22 +02:00
|
|
|
while (! dims.empty()) {
|
|
|
|
|
cur = dims.front();
|
|
|
|
|
dims.pop_front();
|
2014-08-08 17:17:47 +02:00
|
|
|
|
2012-04-29 03:37:22 +02:00
|
|
|
out << "[";
|
2014-08-08 17:17:47 +02:00
|
|
|
if (cur->dimension(0).msb() && cur->dimension(0).lsb()) {
|
|
|
|
|
// bounded array, unbounded arrays have msb() & lsb() nullified
|
2013-05-12 05:00:00 +02:00
|
|
|
errors += cur->dimension(0).msb()->emit(out, 0, 0);
|
2014-08-08 17:17:47 +02:00
|
|
|
out << ":";
|
2013-05-12 05:00:00 +02:00
|
|
|
errors += cur->dimension(0).lsb()->emit(out, 0, 0);
|
2014-08-08 17:17:47 +02:00
|
|
|
}
|
2012-04-29 03:37:22 +02:00
|
|
|
out << "]";
|
|
|
|
|
}
|
2011-10-10 00:25:35 +02:00
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeEnum::emit_def(ostream&out, perm_string name) const
|
2011-10-10 00:25:35 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
|
|
|
|
out << "enum {";
|
|
|
|
|
assert(names_.size() >= 1);
|
|
|
|
|
out << "\\" << names_[0] << " ";
|
|
|
|
|
for (size_t idx = 1 ; idx < names_.size() ; idx += 1)
|
|
|
|
|
out << ", \\" << names_[idx] << " ";
|
|
|
|
|
|
2014-08-25 16:57:27 +02:00
|
|
|
out << "}";
|
|
|
|
|
emit_name(out, name);
|
2011-10-10 00:25:35 +02:00
|
|
|
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-02 19:56:00 +02:00
|
|
|
int VTypePrimitive::emit_primitive_type(ostream&out) const
|
|
|
|
|
{
|
|
|
|
|
int errors = 0;
|
|
|
|
|
switch (type_) {
|
|
|
|
|
case BOOLEAN:
|
|
|
|
|
case BIT:
|
2012-04-29 03:37:22 +02:00
|
|
|
out << "bool";
|
2011-03-27 21:01:58 +02:00
|
|
|
break;
|
2011-10-02 19:56:00 +02:00
|
|
|
case STDLOGIC:
|
2012-04-29 03:37:22 +02:00
|
|
|
out << "logic";
|
2011-03-27 21:01:58 +02:00
|
|
|
break;
|
2011-10-02 19:56:00 +02:00
|
|
|
case INTEGER:
|
2014-08-25 16:51:35 +02:00
|
|
|
out << "bool[31:0]";
|
|
|
|
|
break;
|
|
|
|
|
case REAL:
|
|
|
|
|
out << "real";
|
2011-10-02 19:56:00 +02:00
|
|
|
break;
|
2013-05-12 05:00:00 +02:00
|
|
|
case CHARACTER:
|
|
|
|
|
out << "char";
|
|
|
|
|
break;
|
2011-10-02 19:56:00 +02:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
2011-03-27 21:01:58 +02:00
|
|
|
break;
|
|
|
|
|
}
|
2011-10-02 19:56:00 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypePrimitive::emit_def(ostream&out, perm_string name) const
|
2011-10-10 00:25:35 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
|
|
|
|
errors += emit_primitive_type(out);
|
2014-08-25 16:57:27 +02:00
|
|
|
emit_name(out, name);
|
2011-10-10 00:25:35 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
2011-03-27 21:01:58 +02:00
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeRange::emit_def(ostream&out, perm_string name) const
|
2011-10-10 00:25:35 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
2012-05-06 01:55:29 +02:00
|
|
|
out << "/* Internal error: Don't know how to emit range */";
|
2014-08-22 10:49:55 +02:00
|
|
|
errors += base_->emit_def(out, name);
|
2011-10-02 19:56:00 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeRecord::emit_def(ostream&out, perm_string name) const
|
2012-03-17 23:03:19 +01:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
2012-04-09 04:06:58 +02:00
|
|
|
out << "struct packed {";
|
|
|
|
|
|
|
|
|
|
for (vector<element_t*>::const_iterator cur = elements_.begin()
|
|
|
|
|
; cur != elements_.end() ; ++cur) {
|
|
|
|
|
perm_string element_name = (*cur)->peek_name();
|
|
|
|
|
const VType*element_type = (*cur)->peek_type();
|
2014-08-22 10:49:55 +02:00
|
|
|
element_type->emit_def(out, empty_perm_string);
|
2012-04-29 03:37:22 +02:00
|
|
|
out << " \\" << element_name << " ; ";
|
2012-04-09 04:06:58 +02:00
|
|
|
}
|
|
|
|
|
|
2014-08-25 16:57:27 +02:00
|
|
|
out << "}";
|
|
|
|
|
emit_name(out, name);
|
2012-03-17 23:03:19 +01:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 03:38:49 +02:00
|
|
|
/*
|
|
|
|
|
* For VTypeDef objects, use the name of the defined type as the
|
|
|
|
|
* type. (We are defining a variable here, not the type itself.) The
|
|
|
|
|
* emit_typedef() method was presumably called to define type already.
|
|
|
|
|
*/
|
2014-08-22 10:49:55 +02:00
|
|
|
int VTypeDef::emit_def(ostream&out, perm_string) const
|
2011-10-02 19:56:00 +02:00
|
|
|
{
|
|
|
|
|
int errors = 0;
|
2014-08-25 16:57:27 +02:00
|
|
|
emit_name(out, name_);
|
2011-10-02 19:56:00 +02:00
|
|
|
return errors;
|
2011-03-27 21:01:58 +02:00
|
|
|
}
|
2011-10-10 00:25:35 +02:00
|
|
|
|
|
|
|
|
int VTypeDef::emit_decl(ostream&out, perm_string name, bool reg_flag) const
|
|
|
|
|
{
|
|
|
|
|
int errors = 0;
|
|
|
|
|
if (reg_flag)
|
|
|
|
|
out << "reg ";
|
|
|
|
|
else
|
|
|
|
|
out << "wire ";
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
errors += type_->emit_def(out, name);
|
2011-10-10 00:25:35 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 03:38:49 +02:00
|
|
|
int VTypeDef::emit_typedef(ostream&out, typedef_context_t&ctx) const
|
2011-10-10 00:25:35 +02:00
|
|
|
{
|
2012-05-13 03:38:49 +02:00
|
|
|
// The typedef_context_t is used by me to determine if this
|
|
|
|
|
// typedef has already been emitted in this architecture. If
|
|
|
|
|
// it has, then it is MARKED, give up. Otherwise, recurse the
|
|
|
|
|
// emit_typedef to make sure all sub-types that I use have
|
|
|
|
|
// been emitted, then emit my typedef.
|
|
|
|
|
typedef_topo_t&flag = ctx[this];
|
|
|
|
|
switch (flag) {
|
|
|
|
|
case MARKED:
|
|
|
|
|
return 0;
|
|
|
|
|
case PENDING:
|
|
|
|
|
out << "typedef \\" << name_ << " ; /* typedef cycle? */" << endl;
|
|
|
|
|
return 0;
|
|
|
|
|
case NONE:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
flag = PENDING;
|
|
|
|
|
int errors = type_->emit_typedef(out, ctx);
|
|
|
|
|
flag = MARKED;
|
|
|
|
|
|
2014-08-22 10:49:55 +02:00
|
|
|
// Array types are used directly anyway and typedefs for unpacked
|
|
|
|
|
// arrays do not work currently
|
|
|
|
|
if(dynamic_cast<const VTypeArray*>(type_))
|
|
|
|
|
out << "// ";
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
out << "typedef ";
|
2014-08-22 10:49:55 +02:00
|
|
|
errors += type_->emit_def(out, name_);
|
|
|
|
|
out << " ;" << endl;
|
|
|
|
|
|
2011-10-10 00:25:35 +02:00
|
|
|
return errors;
|
|
|
|
|
}
|