verilog_vardecl.c 11.1 KB
Newer Older
饶先宏's avatar
饶先宏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
** HDL4SE: 软件Verilog综合仿真平台
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** LCOM: 轻量级组件对象模型
** Copyright (C) 2021-2021, raoxianhong<raoxianhong@163.net>
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are met:
**
** * Redistributions of source code must retain the above copyright notice,
**   this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright notice,
**   this list of conditions and the following disclaimer in the documentation
**   and/or other materials provided with the distribution.
** * The name of the author may be used to endorse or promote products
**   derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
** LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
** THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
* verilog_vardecl.c
  修改记录:
    202106061102: rxh, initial version
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "object.h"
#include "dlist.h"
43
#include "bignumber.h"
44
#include "conststring.h"
饶先宏's avatar
饶先宏 已提交
45
#include "verilog_parsetree.h"
饶先宏's avatar
饶先宏 已提交
46 47
#include "verilog_module.h"
#include "verilog_expr.h"
饶先宏's avatar
饶先宏 已提交
48 49
#include "verilog_varsel.h"
#include "verilog_assignment.h"
饶先宏's avatar
饶先宏 已提交
50 51 52 53 54 55 56 57 58 59
#define IMPLEMENT_GUID
#include "verilog_vardecl.h"
#undef IMPLEMENT_GUID

typedef struct _sVarDecl {
    OBJECT_HEADER
    INTERFACE_DECLARE(IVerilogNode)
    VERILOGNODE_VARDECLARE
    DLIST_VARDECLARE
    
60
	verilogVarDecl data;
饶先宏's avatar
饶先宏 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}sVarDecl;

OBJECT_FUNCDECLARE(vardecl, CLSID_VERILOG_VARDECL);

VERILOGNODE_FUNCDECLARE(vardecl, CLSID_VERILOG_VARDECL, sVarDecl);
DLIST_FUNCIMPL(vardecl, CLSID_VERILOG_VARDECL, sVarDecl);
OBJECT_FUNCIMPL(vardecl, sVarDecl, CLSID_VERILOG_VARDECL);

QUERYINTERFACE_BEGIN(vardecl, CLSID_VERILOG_VARDECL)
QUERYINTERFACE_ITEM(IID_VERILOG_NODE, IVerilogNode, sVarDecl)
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sVarDecl)
QUERYINTERFACE_END

static const char *vardeclModuleInfo()
{
	return "1.0.0-20210606.1108 Verilog Var Declaration ";
}

static int vardeclCreate(const PARAMITEM * pParams, int paramcount, HOBJECT * pObject)
{
	sVarDecl * pobj;
	pobj = (sVarDecl *)malloc(sizeof(sVarDecl));
	if (pobj == NULL)
		return -1;

	memset(pobj, 0, sizeof(sVarDecl));
	
	*pObject = 0;
	DLIST_VARINIT(pobj, vardecl);
	VERILOGNODE_VARINIT(pobj, CLSID_VERILOG_VARDECL);
	INTERFACE_INIT(IVerilogNode, pobj, vardecl, verilognode);
  	
	/*返回生成的对象*/
	OBJECT_RETURN_GEN(vardecl, pobj, pObject, CLSID_VERILOG_VARDECL);
	return EIID_OK;
}


static void vardeclDestroy(HOBJECT object)
{
	sVarDecl * pobj;
	pobj = (sVarDecl *)objectThis(object);
103 104 105

	objectRelease(pobj->data.name);
	objectRelease(pobj->data.assignexpr);
106 107 108 109 110 111
	objectRelease(pobj->data.range_msb);
	objectRelease(pobj->data.range_lsb);
	dlistRemoveAll(pobj->data.attributes);
	objectRelease(pobj->data.attributes);
	dlistRemoveAll(pobj->data.dimensions);
	objectRelease(pobj->data.dimensions);
饶先宏's avatar
饶先宏 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
	memset(pobj, 0, sizeof(sVarDecl));
	free(pobj);
}

/*
    功能:判断对象是否是一个有效对象
    参数:
        object -- 对象数据指针
    返回值:
        0 -- 对象是无效的
        1 -- 对象是有效的
*/
static int vardeclValid(HOBJECT object)
{
    return 1;
}

饶先宏's avatar
饶先宏 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
static int output_range(FILE* pFile, int opt, int type, IVerilogNode** msb, IVerilogNode** lsb)
{
	if (type == RANGE_TYPE_NONE)
		return 0;
	if (type == RANGE_TYPE_BITSELECT) {
		fprintf(pFile, " [");
		objectCall2(msb, dump, pFile, opt);
		fprintf(pFile, "] ");
		return 0;
	}
	fprintf(pFile, " [");
	objectCall2(msb, dump, pFile, opt);
	if (type == RANGE_TYPE_PARTSELECT)
		fprintf(pFile, ":");
	else if (type == RANGE_TYPE_STARTPLUSWIDTH)
		fprintf(pFile, "+:");
	else if (type == RANGE_TYPE_STARTMINUSWIDTH)
		fprintf(pFile, "-:");
	objectCall2(lsb, dump, pFile, opt);
	fprintf(pFile, "] ");
	return 0;

}

饶先宏's avatar
饶先宏 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
static const char* var_type_name[] = {
	"",
	"supply0",
	"supply1",
	"tri",
	"triand",
	"trior",
	"tri0",
	"tri1",
	"uwire",
	"wire",
	"wand",
	"wor",
	"integer",
	"time",
	"reg",
};


饶先宏's avatar
饶先宏 已提交
172 173 174 175
static int vardecl_verilognode_dump(HOBJECT object, FILE * pFile, int opt)
{
	sVarDecl * pobj;
	pobj = (sVarDecl *)objectThis(object);
饶先宏's avatar
饶先宏 已提交
176 177 178 179 180
	/*
	attribute_instance_list net_type drive_strength_option vectored_or_scalared_option signed_option
		range_option delay3_option list_of_net
	*/
	fprintf(pFile, "  ");
181
	if (dlistItemCount(pobj->data.attributes) > 0) {
饶先宏's avatar
饶先宏 已提交
182
		fprintf(pFile, "(* ");
183
		verilog_dump_node_list(pobj->data.attributes, pFile, opt, ", ", 5);
饶先宏's avatar
饶先宏 已提交
184 185
		fprintf(pFile, " *) ");
	}
186 187
	fprintf(pFile, var_type_name[pobj->data.type]);
	output_range(pFile, opt, pobj->data.range_type, pobj->data.range_msb, pobj->data.range_lsb);
饶先宏's avatar
饶先宏 已提交
188
	fprintf(pFile, " %s", conststringFromVar(pobj->data.name));
189
	if (pobj->data.assignexpr != NULL) {
饶先宏's avatar
饶先宏 已提交
190
		fprintf(pFile, " = ");
191
		objectCall2(pobj->data.assignexpr, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
192 193 194 195 196
	}
	fprintf(pFile, ";\n");
	return 0;
}

饶先宏's avatar
饶先宏 已提交
197
static int vardecl_verilognode_procheck(HOBJECT object, HOBJECT module, void * param)
198 199
{
	sVarDecl* pobj;
饶先宏's avatar
饶先宏 已提交
200
	verilogModule* pmodule = verilogModuleGetData(module);
201
	pobj = (sVarDecl*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
202 203 204
	objectCall2(pobj->data.assignexpr, procheck, module, param);
	objectCall2(pobj->data.range_msb, procheck, module, param);
	objectCall2(pobj->data.range_lsb, procheck, module, param);
饶先宏's avatar
饶先宏 已提交
205 206 207 208 209 210 211 212 213 214 215 216
	{/*如果声明中带赋值,则将赋值加入到持续性赋值中去*/
		if (pobj->data.assignexpr != NULL) {
			IDListVarPtr lvalue, itemlist;
			HOBJECT continuous_assign;
			lvalue = dlistCreate();
			dlistAppendItem(lvalue, verilogparseCreateVarSel(
				pobj->data.name, //const char * name,
				RANGE_TYPE_NONE, //int range_type,
				NULL, //HOBJECT range_msb,
				NULL  //HOBJECT range_lsb
			));
			continuous_assign = verilogparseCreateAssignment(
饶先宏's avatar
饶先宏 已提交
217
				0,
饶先宏's avatar
饶先宏 已提交
218
				lvalue,
饶先宏's avatar
饶先宏 已提交
219 220
				NULL, 
				RANGE_TYPE_NONE,
饶先宏's avatar
饶先宏 已提交
221 222 223 224 225 226 227 228 229 230 231
				0,
				pobj->data.assignexpr,
				pobj->data.attributes
			);
			itemlist = dlistCreate();
			dlistAppendItem(itemlist, continuous_assign);
			verilogparseAddModuleItems(module, itemlist, MODULE_ITEM_TYPE_CONTINUOUS_ASSIGNMENT);
			//objectRelease(pobj->data.assignexpr);
			pobj->data.assignexpr = NULL;
		}
	}
饶先宏's avatar
饶先宏 已提交
232
	pobj->data.index = pmodule->paramindex++;
233 234 235
	return 0;
}

饶先宏's avatar
饶先宏 已提交
236
static int vardecl_verilognode_gencode(HOBJECT object, FILE * pFile, HOBJECT module, void * param)
237 238
{
	sVarDecl* pobj;
饶先宏's avatar
饶先宏 已提交
239 240 241 242
	int paramcount;
	verilogModule* moduleinfo;
	moduleinfo = verilogModuleGetData(module);
	paramcount = dlistItemCount(moduleinfo->module_parameters);
243
	pobj = (sVarDecl*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
244
	if (pobj->data.range_type == RANGE_TYPE_NONE) {
饶先宏's avatar
饶先宏 已提交
245
		fprintf(pFile, "\tnets[%3d] = hdl4seCreateUnit(module, CLSID_HDL4SE_WIRE, \"  1\", \"%s\");\n",
饶先宏's avatar
饶先宏 已提交
246 247
			pobj->data.index,
			pobj->data.name->string);
饶先宏's avatar
饶先宏 已提交
248
		fprintf(pFile, "\t__netswidth[%d] = 1;\n", pobj->data.index);
饶先宏's avatar
饶先宏 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261
	}
	else if (pobj->data.range_type == RANGE_TYPE_PARTSELECT) {
		verilogExpr* lsb;
		verilogExpr* msb;
		lsb = verilogExprGetData(pobj->data.range_lsb);
		msb = verilogExprGetData(pobj->data.range_msb);
		if (lsb != NULL && lsb->exprtype == EXPRTYPE_BIGNUMBER && msb != NULL && msb->exprtype == EXPRTYPE_BIGNUMBER) {
			int width;
			IBigNumber** temp = bigintegerCreate(32);
			objectCall2(temp, Sub, msb->bignumber, lsb->bignumber);
			objectCall2(temp, AddInt32, temp, 1);
			objectCall1(temp, GetInt32, &width);
			objectRelease(temp);
饶先宏's avatar
饶先宏 已提交
262
			fprintf(pFile, "\tnets[%3d] = hdl4seCreateUnit(module, CLSID_HDL4SE_WIRE, \"%3d\", \"%s\");\n",
饶先宏's avatar
饶先宏 已提交
263 264 265
				pobj->data.index,
				width,
				pobj->data.name->string);
饶先宏's avatar
饶先宏 已提交
266
			fprintf(pFile, "\t__netswidth[%d] = %d;\n", pobj->data.index, width);
饶先宏's avatar
饶先宏 已提交
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
		}
		else {
			int exprcount;
			fprintf(pFile, "\t\t{/*%3d %s */\n",
				pobj->data.index,
				pobj->data.name->string);
			fprintf(pFile, "\t\t\tint wirewidth;\n");
			fprintf(pFile, "\t\t\tchar instanceparam[256];\n");
			fprintf(pFile, "\t\t\tconst expr_code expr_info[] = {\n");
			exprcount = verilogparseGenConstExpr(pFile, "\t\t\t\t", pobj->data.range_msb, module);
			exprcount += verilogparseGenConstExpr(pFile, "\t\t\t\t", pobj->data.range_lsb, module);
			fprintf(pFile, "\t\t\t\t{.exprtype=EXPRTYPE_BINOP, .op=OP_MINUS},\n");
			fprintf(pFile, "\t\t\t\t{.exprtype=EXPRTYPE_NUMBER, .value=\"1\"},\n");
			fprintf(pFile, "\t\t\t\t{.exprtype=EXPRTYPE_BINOP, .op=OP_PLUS},\n");
			exprcount += 3;
			fprintf(pFile, "\t\t\t};\n");
			fprintf(pFile, "\t\t\twirewidth = const_expr_eval(%d, expr_info, %d, %s);\n",
				exprcount,
				paramcount,
				paramcount > 0 ? "parameters" : "NULL");
			fprintf(pFile, "\t\t\tsprintf(instanceparam, \"%%d\", wirewidth);\n");
			fprintf(pFile, "\t\t\tnets[%3d] = hdl4seCreateUnit(module, CLSID_HDL4SE_WIRE, instanceparam, \"%s\");\n",
				pobj->data.index,
				pobj->data.name->string);
饶先宏's avatar
饶先宏 已提交
291
			fprintf(pFile, "\t__netswidth[%d] = wirewidth;\n", pobj->data.index);
饶先宏's avatar
饶先宏 已提交
292 293 294 295
			fprintf(pFile, "\t\t};\n");
		}
	}

296 297 298
	return 0;
}

饶先宏's avatar
饶先宏 已提交
299
HOBJECT verilogparseCreateVarDecl(IConstStringVar* name)
饶先宏's avatar
饶先宏 已提交
300 301 302 303 304 305 306 307
{
	HOBJECT vardecl = NULL;
	sVarDecl * pobj;
	A_u_t_o_registor_vardecl();
	objectCreate(CLSID_VERILOG_VARDECL, NULL, 0, &vardecl);
	if (vardecl == NULL)
		return NULL;
	pobj = (sVarDecl *)objectThis(vardecl);
饶先宏's avatar
饶先宏 已提交
308
	objectQueryInterface(name, IID_CONSTSTRING, (void**)&pobj->data.name);
饶先宏's avatar
饶先宏 已提交
309 310 311 312 313 314 315 316 317
	return vardecl;
}

int verilogparseVarDeclSetAssignExpr(HOBJECT object, HOBJECT expr)
{
	sVarDecl* pobj;
	if (!objectIsClass(object, CLSID_VERILOG_VARDECL))
		return -1;
	pobj = (sVarDecl*)objectThis(object);
318 319
	objectRelease(pobj->data.assignexpr);
	objectQueryInterface(expr, IID_VERILOG_NODE, (void**)&pobj->data.assignexpr);
饶先宏's avatar
饶先宏 已提交
320 321 322 323 324 325 326 327 328
	return 0;
}

int verilogparseVarDeclSetDimensions(HOBJECT object, IDListVar* dimensions)
{
	sVarDecl* pobj;
	if (!objectIsClass(object, CLSID_VERILOG_VARDECL))
		return -1;
	pobj = (sVarDecl*)objectThis(object);
329 330
	pobj->data.dimensions = dimensions;
	objectAddRef(pobj->data.dimensions);
饶先宏's avatar
饶先宏 已提交
331 332 333 334 335 336 337 338 339 340
	return 0;
}

int verilogparseVarDeclSetOptions(HOBJECT object,
	int type,
	IDListVar* attributes,
	int vectored_or_scalared,
	int issigned,
	int range_type,
	HOBJECT range_msb,
饶先宏's avatar
饶先宏 已提交
341
	HOBJECT range_lsb
饶先宏's avatar
饶先宏 已提交
342 343 344
)
{
#define ASSIGN_MEMBER(m) \
345 346 347
	objectRelease(pobj->data.m); \
	pobj->data.m = NULL; \
	objectQueryInterface(m, IID_VERILOG_NODE, (void**)&pobj->data.m);
饶先宏's avatar
饶先宏 已提交
348 349 350 351 352

	sVarDecl* pobj;
	if (!objectIsClass(object, CLSID_VERILOG_VARDECL))
		return -1;
	pobj = (sVarDecl*)objectThis(object);
353 354 355 356 357 358
	pobj->data.attributes = attributes;
	objectAddRef(pobj->data.attributes);
	pobj->data.type = type;
	pobj->data.vectored_or_scalared = vectored_or_scalared;
	pobj->data.issigned = issigned;
	pobj->data.range_type = range_type;
饶先宏's avatar
饶先宏 已提交
359 360 361 362 363
	ASSIGN_MEMBER(range_msb);
	ASSIGN_MEMBER(range_lsb);
	return 0;
}

364 365 366 367 368 369 370 371
verilogVarDecl* verilogVarDeclGetData(HOBJECT object)
{
	sVarDecl* pobj;
	if (!objectIsClass(object, CLSID_VERILOG_VARDECL))
		return NULL;
	pobj = (sVarDecl*)objectThis(object);
	return &pobj->data;
}