<commit_before>/*
 *  VHDL code generation for scopes.
 *
 *  Copyright (C) 2008  <NAME> (<EMAIL>)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it 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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "vhdl_target.h"
#include "vhdl_element.hh"

#include <iostream>
#include <sstream>
#include <cassert>

/*
 * Create a VHDL entity for scopes of type IVL_SCT_MODULE.
 */
static vhdl_entity *create_entity_for(ivl_scope_t scope)
{
   assert(ivl_scope_type(scope) == IVL_SCT_MODULE);

   // The type name will become the entity name
   const char *tname = ivl_scope_tname(scope);

   // Remember the scope name this entity was derived from so
   // the correct processes can be added later
   const char *derived_from = ivl_scope_name(scope);
   
   // Verilog does not have the entity/architecture distinction
   // so we always create a pair and associate the architecture
   // with the entity for convenience (this also means that we
   // retain a 1-to-1 mapping of scope to VHDL element)
   vhdl_arch *arch = new vhdl_arch(tname);
   vhdl_entity *ent = new vhdl_entity(tname, derived_from, arch);

   // Build a comment to add to the entity/architecture
   std::ostringstream ss;
   ss << "Generated from " << ivl_scope_name(scope);
   ss << " (" << ivl_scope_def_file(scope) << ":";
   ss << ivl_scope_def_lineno(scope) << ")";
   
   arch->set_comment(ss.str());
   ent->set_comment(ss.str());

   std::cout << "Generated entity " << tname;
   std::cout << " from " << ivl_scope_name(scope) << std::endl;
   
   remember_entity(ent);
   return ent;
}

/*
 * Instantiate an entity in the hierarchy, and possibly create
 * that entity if it hasn't been encountered yet.
 */
static int draw_module(ivl_scope_t scope, ivl_scope_t parent)
{
   assert(ivl_scope_type(scope) == IVL_SCT_MODULE);

   // Maybe we need to create this entity first?
   vhdl_entity *ent = find_entity(ivl_scope_tname(scope)); 
   if (NULL == ent)
      ent = create_entity_for(scope);
   assert(ent);

   // Is this module instantiated inside another?
   if (parent != NULL) {
      vhdl_entity *parent_ent = find_entity(ivl_scope_tname(parent));
      assert(parent_ent != NULL);
      vhdl_arch *parent_arch = parent_ent->get_arch();
      assert(parent_arch != NULL);
      
      // Create a forward declaration for it
      if (!parent_arch->have_declared_component(ent->get_name())) {
         vhdl_decl *comp_decl = vhdl_component_decl::component_decl_for(ent);
         parent_arch->add_decl(comp_decl);
      }

      // And an instantiation statement
      const char *inst_name = ivl_scope_basename(scope);
      vhdl_comp_inst *inst = new vhdl_comp_inst(inst_name, ent->get_name().c_str());
      std::ostringstream ss;
      ss << "Scope name " << ivl_scope_name(scope);
      inst->set_comment(ss.str());
      parent_arch->add_stmt(inst);      
   }
   
   return 0;
}

int draw_scope(ivl_scope_t scope, void *_parent)
{
   ivl_scope_t parent = static_cast<ivl_scope_t>(_parent);
   
   const char *name = ivl_scope_name(scope);
   const char *basename = ivl_scope_basename(scope);

   std::cout << "scope " << name << " (" << basename << ")" << std::endl;

   ivl_scope_type_t type = ivl_scope_type(scope);
   int rc = 0;
   switch (type) {
   case IVL_SCT_MODULE:
      rc = draw_module(scope, parent);
      break;
   default:
      error("No VHDL conversion for %s (at %s)",
            ivl_scope_tname(scope),
            ivl_scope_name(scope));
      break;
   }
   if (rc != 0)
      return rc;
   
   rc = ivl_scope_children(scope, draw_scope, scope);
   if (rc != 0)
      return rc;
   
   return 0;
}

<commit_msg>Fix a bug where the same instantiation appeared multiple times<commit_after>/*
 *  VHDL code generation for scopes.
 *
 *  Copyright (C) 2008  <NAME> (<EMAIL>)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it 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.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "vhdl_target.h"
#include "vhdl_element.hh"

#include <iostream>
#include <sstream>
#include <cassert>

/*
 * Create a VHDL entity for scopes of type IVL_SCT_MODULE.
 */
static vhdl_entity *create_entity_for(ivl_scope_t scope)
{
   assert(ivl_scope_type(scope) == IVL_SCT_MODULE);

   // The type name will become the entity name
   const char *tname = ivl_scope_tname(scope);

   // Remember the scope name this entity was derived from so
   // the correct processes can be added later
   const char *derived_from = ivl_scope_name(scope);
   
   // Verilog does not have the entity/architecture distinction
   // so we always create a pair and associate the architecture
   // with the entity for convenience (this also means that we
   // retain a 1-to-1 mapping of scope to VHDL element)
   vhdl_arch *arch = new vhdl_arch(tname);
   vhdl_entity *ent = new vhdl_entity(tname, derived_from, arch);

   // Build a comment to add to the entity/architecture
   std::ostringstream ss;
   ss << "Generated from " << ivl_scope_name(scope);
   ss << " (" << ivl_scope_def_file(scope) << ":";
   ss << ivl_scope_def_lineno(scope) << ")";
   
   arch->set_comment(ss.str());
   ent->set_comment(ss.str());

   std::cout << "Generated entity " << tname;
   std::cout << " from " << ivl_scope_name(scope) << std::endl;
   
   remember_entity(ent);
   return ent;
}

/*
 * Instantiate an entity in the hierarchy, and possibly create
 * that entity if it hasn't been encountered yet.
 */
static int draw_module(ivl_scope_t scope, ivl_scope_t parent)
{
   assert(ivl_scope_type(scope) == IVL_SCT_MODULE);

   // Maybe we need to create this entity first?
   vhdl_entity *ent = find_entity(ivl_scope_tname(scope)); 
   if (NULL == ent)
      ent = create_entity_for(scope);
   assert(ent);

   // Is this module instantiated inside another?
   if (parent != NULL) {
      vhdl_entity *parent_ent = find_entity(ivl_scope_tname(parent));
      assert(parent_ent != NULL);

      
      // Make sure we only collect instantiations from *one*
      // example of this module in the hieararchy
      if (parent_ent->get_derived_from() == ivl_scope_name(parent)) {

         vhdl_arch *parent_arch = parent_ent->get_arch();
         assert(parent_arch != NULL);
         
         // Create a forward declaration for it
         if (!parent_arch->have_declared_component(ent->get_name())) {
            vhdl_decl *comp_decl = vhdl_component_decl::component_decl_for(ent);
            parent_arch->add_decl(comp_decl);
         }
         
         // And an instantiation statement
         const char *inst_name = ivl_scope_basename(scope);
         vhdl_comp_inst *inst = new vhdl_comp_inst(inst_name, ent->get_name().c_str());
         std::ostringstream ss;
         ss << "Generated from " << ivl_scope_name(scope);
         inst->set_comment(ss.str());
         parent_arch->add_stmt(inst);
      }
      else {
         std::cout << "Ignoring instantiation " << ivl_scope_name(scope);
         std::cout << " (already accounted for)" << std::endl;
      }
   }
   
   return 0;
}

int draw_scope(ivl_scope_t scope, void *_parent)
{
   ivl_scope_t parent = static_cast<ivl_scope_t>(_parent);
   
   const char *name = ivl_scope_name(scope);
   const char *basename = ivl_scope_basename(scope);

   std::cout << "scope " << name << " (" << basename << ")" << std::endl;

   ivl_scope_type_t type = ivl_scope_type(scope);
   int rc = 0;
   switch (type) {
   case IVL_SCT_MODULE:
      rc = draw_module(scope, parent);
      break;
   default:
      error("No VHDL conversion for %s (at %s)",
            ivl_scope_tname(scope),
            ivl_scope_name(scope));
      break;
   }
   if (rc != 0)
      return rc;
   
   rc = ivl_scope_children(scope, draw_scope, scope);
   if (rc != 0)
      return rc;
   
   return 0;
}


{'fraction_non_alphanumeric': 0.10740947075208913, 'fraction_numerical': 0.005125348189415042, 'mean_word_length': 3.200280767430978, 'pattern_counts': {'":': 2, '<': 103, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '304356', 'n_tokens_mistral': 2960, 'n_tokens_neox': 2855, 'n_words': 871}