verilog_expr.c 13.5 KB
Newer Older
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
/*
** 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.
*/

/*
饶先宏's avatar
饶先宏 已提交
33
* verilog_expr.c
34 35
  修改记录:
    202106021610: rxh, initial version
饶先宏's avatar
饶先宏 已提交
36
	202106061502: rxh, modify from expr to expr
37 38 39 40 41 42 43
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

#include "object.h"
#include "dlist.h"
44
#include "bignumber.h"
45
#include "conststring.h"
46
#include "verilog_parsetree.h"
47
#include "verilog_module.h"
48
#include "verilog_varsel.h"
49
#define IMPLEMENT_GUID
饶先宏's avatar
饶先宏 已提交
50
#include "verilog_expr.h"
51 52
#undef IMPLEMENT_GUID

饶先宏's avatar
饶先宏 已提交
53
typedef struct _sExpr {
54 55 56 57 58
	OBJECT_HEADER
	INTERFACE_DECLARE(IVerilogNode)
	VERILOGNODE_VARDECLARE
	DLIST_VARDECLARE

59
	verilogExpr data;
饶先宏's avatar
饶先宏 已提交
60
}sExpr;
61

饶先宏's avatar
饶先宏 已提交
62
OBJECT_FUNCDECLARE(expr, CLSID_VERILOG_EXPR);
63

饶先宏's avatar
饶先宏 已提交
64 65 66
VERILOGNODE_FUNCDECLARE(expr, CLSID_VERILOG_EXPR, sExpr);
DLIST_FUNCIMPL(expr, CLSID_VERILOG_EXPR, sExpr);
OBJECT_FUNCIMPL(expr, sExpr, CLSID_VERILOG_EXPR);
67

饶先宏's avatar
饶先宏 已提交
68 69 70
QUERYINTERFACE_BEGIN(expr, CLSID_VERILOG_EXPR)
QUERYINTERFACE_ITEM(IID_VERILOG_NODE, IVerilogNode, sExpr)
QUERYINTERFACE_ITEM(IID_DLIST, IDList, sExpr)
71 72
QUERYINTERFACE_END

饶先宏's avatar
饶先宏 已提交
73
static const char *exprModuleInfo()
74
{
75
	return "1.1.0-20210606.1504 Expression ";
76 77
}

饶先宏's avatar
饶先宏 已提交
78
static int exprCreate(const PARAMITEM * pParams, int paramcount, HOBJECT * pObject)
79
{
饶先宏's avatar
饶先宏 已提交
80 81
	sExpr * pobj;
	pobj = (sExpr *)malloc(sizeof(sExpr));
82 83 84
	if (pobj == NULL)
		return -1;

饶先宏's avatar
饶先宏 已提交
85
	memset(pobj, 0, sizeof(sExpr));
86 87
	
	*pObject = 0;
饶先宏's avatar
饶先宏 已提交
88 89 90
	DLIST_VARINIT(pobj, expr);
	VERILOGNODE_VARINIT(pobj, CLSID_VERILOG_EXPR);
	INTERFACE_INIT(IVerilogNode, pobj, expr, verilognode);
91 92
  	
	/*返回生成的对象*/
饶先宏's avatar
饶先宏 已提交
93
	OBJECT_RETURN_GEN(expr, pobj, pObject, CLSID_VERILOG_EXPR);
94 95 96 97
	return EIID_OK;
}


饶先宏's avatar
饶先宏 已提交
98
static void exprDestroy(HOBJECT object)
99
{
饶先宏's avatar
饶先宏 已提交
100 101
	sExpr * pobj;
	pobj = (sExpr *)objectThis(object);
102 103
	objectRelease(pobj->data.value);
	objectRelease(pobj->data.expr0);
104 105 106 107 108 109 110 111
	objectRelease(pobj->data.expr1);
	objectRelease(pobj->data.expr2);
	objectRelease(pobj->data.range0);
	objectRelease(pobj->data.range1);
	dlistRemoveAll(pobj->data.attributes);
	objectRelease(pobj->data.attributes);
	dlistRemoveAll(pobj->data.elementselect);
	objectRelease(pobj->data.elementselect);
112 113 114 115 116 117 118 119 120 121 122
	free(pobj);
}

/*
    功能:判断对象是否是一个有效对象
    参数:
        object -- 对象数据指针
    返回值:
        0 -- 对象是无效的
        1 -- 对象是有效的
*/
饶先宏's avatar
饶先宏 已提交
123
static int exprValid(HOBJECT object)
124 125 126 127
{
    return 1;
}

饶先宏's avatar
饶先宏 已提交
128 129 130
static int output_operator(FILE* pFile, int opt, int op)
{
	fprintf(pFile, operator_name[op]);
131
	return 0;
饶先宏's avatar
饶先宏 已提交
132 133 134
}


饶先宏's avatar
饶先宏 已提交
135
static int output_attributes(FILE* pFile, int opt, sExpr* pobj)
饶先宏's avatar
饶先宏 已提交
136
{
137
	if (dlistItemCount(pobj->data.attributes) > 0) {
饶先宏's avatar
饶先宏 已提交
138
		fprintf(pFile, "(* ");
139
		verilog_dump_node_list(pobj->data.attributes, pFile, opt, ", ", 5);
饶先宏's avatar
饶先宏 已提交
140 141 142 143 144
		fprintf(pFile, " *) ");
	}
	return 0;
}

饶先宏's avatar
饶先宏 已提交
145
static int expr_verilognode_dump(HOBJECT object, FILE * pFile, int opt)
146
{
饶先宏's avatar
饶先宏 已提交
147 148
	sExpr * pobj;
	pobj = (sExpr *)objectThis(object);
149
	switch (pobj->data.exprtype) {
饶先宏's avatar
饶先宏 已提交
150 151
		case EXPRTYPE_NUMBER:
		case EXPRTYPE_STRING:
152
			fprintf(pFile, conststringFromVar(pobj->data.value));
153
			break;
饶先宏's avatar
饶先宏 已提交
154 155
		case EXPRTYPE_PARAM:
		case EXPRTYPE_LOCALPARAM:
156
			fprintf(pFile, conststringFromVar(pobj->data.value));
饶先宏's avatar
饶先宏 已提交
157
			break;
饶先宏's avatar
饶先宏 已提交
158
		case EXPRTYPE_BINOP:
159 160
			objectCall2(pobj->data.expr0, dump, pFile, opt);
			output_operator(pFile, opt, pobj->data.op);
饶先宏's avatar
饶先宏 已提交
161
			output_attributes(pFile, opt, pobj);
162
			objectCall2(pobj->data.expr1, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
163
			break;
饶先宏's avatar
饶先宏 已提交
164
		case EXPRTYPE_UNOP:
165
			output_operator(pFile, opt, pobj->data.op);
饶先宏's avatar
饶先宏 已提交
166
			output_attributes(pFile, opt, pobj);
167
			objectCall2(pobj->data.expr0, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
168
			break;
饶先宏's avatar
饶先宏 已提交
169
		case EXPRTYPE_IFOP:
170
			objectCall2(pobj->data.expr0, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
171 172
			fprintf(pFile, "?");
			output_attributes(pFile, opt, pobj);
173
			objectCall2(pobj->data.expr1, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
174
			fprintf(pFile, ":");
175
			objectCall2(pobj->data.expr2, dump, pFile, opt);
饶先宏's avatar
饶先宏 已提交
176
			break;
177 178 179 180 181
		case EXPRTYPE_HIERARCHICAL_IDENT:
			if (dlistItemCount(pobj->data.elementselect) > 0) {
				verilog_dump_node_list(pobj->data.elementselect, pFile, opt, ".", 5);
			}
			break;
182
		default:
183
			fprintf(pFile, "<type=%d>", pobj->data.exprtype);
184 185 186 187 188
			break;
	}
	return 0;
}

饶先宏's avatar
饶先宏 已提交
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int expr_calc_binop(int op, IBigNumber** dst, IBigNumber** src0, IBigNumber** src1)
{
	switch (op) {
	case OP_L_NOT: return -1;
	case OP_B_NOT: return -1;
	case OP_B_AND: return -1;
	case OP_B_NAND: return -1;
	case OP_B_OR: return -1;
	case OP_B_NOR: return -1;
	case OP_B_XOR: return -1;
	case OP_B_EQU: return -1;
	case OP_PLUS: objectCall2(dst, Add, src0, src1); return 0;
	case OP_MINUS: objectCall2(dst, Sub, src0, src1); return 0;
	case OP_MUL: objectCall2(dst, Mul, src0, src1); return 0;
	case OP_DIV: return -1;
	case OP_MOD: return -1;
	case OP_POW: return -1;
	case OP_L_EQ: return -1;
	case OP_L_NEQ: return -1;
	case OP_C_EQ: return -1;
	case OP_C_NEQ: return -1;
	case OP_L_AND: return -1;
	case OP_L_OR: return -1;
	case OP_LT: return -1;
	case OP_LTE: return -1;
	case OP_GT: return -1;
	case OP_GTE: return -1;
	case OP_LSR: return -1;
	case OP_LSL: return -1;
	case OP_ASR: return -1;
	case OP_ASL: return -1;
	}
	return -1;
}

static int expr_calc_unop(int op, IBigNumber** dst, IBigNumber** src)
{
	switch (op) {
	case OP_L_NOT: return -1;
	case OP_B_NOT: return -1;
	case OP_B_AND: return -1;
	case OP_B_NAND: return -1;
	case OP_B_OR: return -1;
	case OP_B_NOR: return -1;
	case OP_B_XOR: return -1;
	case OP_B_EQU: return -1;
	case OP_PLUS: 
		if (dst != src)
			objectCall1(dst, Assign, src); 
		return 0;
	case OP_MINUS: 
		objectCall1(dst, Neg, src); 
		return 0;
	}
	return -1;
}

static int expr_setto_number(sExpr* pobj, IBigNumber** number)
{
	objectRelease(pobj->data.bignumber);
	pobj->data.bignumber = NULL;
	if (0 == objectQueryInterface(number, IID_BIGNUMBER, &pobj->data.bignumber)) {
		pobj->data.exprtype = EXPRTYPE_BIGNUMBER;
	}
	return 0;
}

饶先宏's avatar
饶先宏 已提交
256
static int expr_verilognode_procheck(HOBJECT object, HOBJECT module, void * param)
257 258 259
{
	sExpr* pobj;
	pobj = (sExpr*)objectThis(object);
饶先宏's avatar
饶先宏 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	switch (pobj->data.exprtype) {
	case EXPRTYPE_NUMBER:
	{
		int ret = 0;
		IBigNumber** number = NULL;
		number = bigintegerCreate(32);
		if (0 != objectCall3(number, AssignStr, pobj->data.value->string, NULL, 1)) {
			goto endof_number;
		}
		if (0 != expr_setto_number(pobj, number)) {
			goto endof_number;
		}
		ret = 1;
	endof_number:
		objectRelease(number);
		return ret;
	}
	case EXPRTYPE_BIGNUMBER:
		return 1;
	case EXPRTYPE_STRING:
		return 0;
		break;
	case EXPRTYPE_PARAM:
	case EXPRTYPE_LOCALPARAM:
		return 0;
		break;
	case EXPRTYPE_BINOP:
	{
		int cancalc = 0;
		IBigNumber** left = NULL;
		IBigNumber** right = NULL;
		verilogExpr* pexpr;
		if (1 != objectCall2(pobj->data.expr0, procheck, module, param))
			return 0;
		if (1 != objectCall2(pobj->data.expr1, procheck, module, param))
			return 0;
		left = bigintegerCreate(32);
		pexpr = verilogExprGetData(pobj->data.expr0);
		if (pexpr == NULL)
			goto endofbinop;
		if (pexpr->exprtype != EXPRTYPE_BIGNUMBER)
			goto endofbinop;
		if (0 != objectCall1(left, Clone, pexpr->bignumber))
			goto endofbinop;
		right = bigintegerCreate(32);
		pexpr = verilogExprGetData(pobj->data.expr1);
		if (pexpr == NULL) 
			goto endofbinop;
		if (pexpr->exprtype != EXPRTYPE_BIGNUMBER)
			goto endofbinop;
		if (0 != objectCall1(right, Clone, pexpr->bignumber))
			goto endofbinop;
		if (0 != expr_calc_binop(pobj->data.op, left, left, right))
			goto endofbinop;
		if (0 != expr_setto_number(pobj, left)) {
			goto endofbinop;
		}
		cancalc = 1;
	endofbinop:
		objectRelease(left);
		objectRelease(right);
		return cancalc;
	}
		break;
	case EXPRTYPE_UNOP:
	{
		int cancalc = 0;
		IBigNumber** left = NULL;
		verilogExpr* pexpr;
		if (1 != objectCall2(pobj->data.expr0, procheck, module, param))
			return 0;
		left = bigintegerCreate(32);
		pexpr = verilogExprGetData(pobj->data.expr0);
		if (pexpr == NULL)
			goto endofunop;
		if (pexpr->exprtype != EXPRTYPE_BIGNUMBER)
			goto endofunop;
		if (0 != objectCall1(left, Clone, pexpr->bignumber))
			goto endofunop;
		if (0 != expr_calc_unop(pobj->data.op, left, left))
			goto endofunop;
		if (0 != expr_setto_number(pobj, left)) {
			goto endofunop;
		}
		cancalc = 1;
	endofunop:
		objectRelease(left);
		return cancalc;
	}
	break;
	case EXPRTYPE_IFOP:
		break;
	case EXPRTYPE_HIERARCHICAL_IDENT:
		break;
	default:
		break;
	}
357 358 359 360 361 362 363 364 365 366
	return 0;
}

static int expr_verilognode_gencode(HOBJECT object, FILE * pFile, const char * modulename, void * opt)
{
	sExpr* pobj;
	pobj = (sExpr*)objectThis(object);
	return 0;
}

饶先宏's avatar
饶先宏 已提交
367
HOBJECT verilogparseCreateExpr(
368
	int exprtype,
饶先宏's avatar
饶先宏 已提交
369
	IConstStringVar* value,
370 371 372 373 374 375
	HOBJECT range0,
	HOBJECT range1,
	int op,
	HOBJECT expr0,
	HOBJECT expr1,
	HOBJECT expr2,
饶先宏's avatar
饶先宏 已提交
376 377
	IDListVarPtr attributes,
	IDListVarPtr elementselect
378 379
)
{
饶先宏's avatar
饶先宏 已提交
380 381 382 383 384
	HOBJECT expr = NULL;
	sExpr * pobj;
	A_u_t_o_registor_expr();
	objectCreate(CLSID_VERILOG_EXPR, NULL, 0, &expr);
	if (expr == NULL)
385
		return NULL;
饶先宏's avatar
饶先宏 已提交
386
	pobj = (sExpr *)objectThis(expr);
387
	objectQueryInterface(value, IID_CONSTSTRING, (void**)&pobj->data.value);
388 389 390 391 392
	pobj->data.exprtype = exprtype;
	pobj->data.op = op;
	pobj->data.attributes = attributes;
	pobj->data.elementselect = elementselect;
	objectQueryInterface(expr0, IID_VERILOG_NODE, (void**)&pobj->data.expr0);
饶先宏's avatar
饶先宏 已提交
393
	objectRelease(expr0);
394
	objectQueryInterface(expr1, IID_VERILOG_NODE, (void**)&pobj->data.expr1);
饶先宏's avatar
饶先宏 已提交
395
	objectRelease(expr1);
396
	objectQueryInterface(expr2, IID_VERILOG_NODE, (void**)&pobj->data.expr2);
饶先宏's avatar
饶先宏 已提交
397
	objectRelease(expr2);
398
	objectQueryInterface(range0, IID_VERILOG_NODE, (void**)&pobj->data.range0);
饶先宏's avatar
饶先宏 已提交
399
	objectRelease(range0);
400
	objectQueryInterface(range1, IID_VERILOG_NODE, (void**)&pobj->data.range1);
饶先宏's avatar
饶先宏 已提交
401
	objectRelease(range1);
饶先宏's avatar
饶先宏 已提交
402
	return expr;
403 404
}

405 406 407 408 409 410 411
verilogExpr* verilogExprGetData(HOBJECT object)
{
	sExpr* pobj;
	if (!(objectIsClass(object, CLSID_VERILOG_EXPR)))
		return NULL;
	pobj = (sExpr*)objectThis(object);
	return &pobj->data;
412 413
}

414 415 416 417 418 419 420 421 422 423
int verilogparseGenConstExpr(FILE* pFile, const char* pre, HOBJECT expr, HOBJECT module)
{
	int count;
	sExpr* pobj;
	pobj = (sExpr*)objectThis(expr);
	if (pobj == NULL)
		return 0;
	count = 1;
	switch (pobj->data.exprtype) {
	case EXPRTYPE_NUMBER:
424
	{
425 426
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_NUMBER, .value=\"%s\" }, \n", pre, pobj->data.value->string);
		break;
427
	}
饶先宏's avatar
饶先宏 已提交
428
	case EXPRTYPE_BIGNUMBER:
429
	{
饶先宏's avatar
饶先宏 已提交
430 431 432
		objectCall3(pobj->data.bignumber, GetStr, 16, hdl4se_parse_logbuf(), 1024);
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_NUMBER, .value=\"%s\" }, \n", pre, hdl4se_parse_logbuf());
		break;
433
	}
434
	case EXPRTYPE_STRING:
435
	{
436 437
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_STRING, .value=%s},\n", pre, pobj->data.value->string);
		break;
438
	}
439
	case EXPRTYPE_PARAM:
440
	{
饶先宏's avatar
饶先宏 已提交
441
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_PARAM, .index=%d},/* %s */\n", pre, verilogModuleGetParamIndex(module, pobj->data.value->string), pobj->data.value->string);
442
		break;
443
	}
444
	case EXPRTYPE_LOCALPARAM:
445
	{
饶先宏's avatar
饶先宏 已提交
446
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_LOCALPARAM, .index=%d},/* %s */\n", pre, verilogModuleGetLocalParamIndex(module, pobj->data.value->string), pobj->data.value->string);
447
		break;
448
	}
449
	case EXPRTYPE_BINOP:
450
	{
451 452 453 454 455
		count = verilogparseGenConstExpr(pFile, pre, pobj->data.expr0, module);
		count += verilogparseGenConstExpr(pFile, pre, pobj->data.expr1, module);
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_BINOP, .op=%s},\n", pre, OPERATOR_name[pobj->data.op]);
		count++;
		break;
456
	}
457
	case EXPRTYPE_UNOP:
458
	{
459 460 461 462
		count = verilogparseGenConstExpr(pFile, pre, pobj->data.expr0, module);
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_UNOP, .op=%s},\n", pre, OPERATOR_name[pobj->data.op]);
		count++;
		break;
463
	}
464
	case EXPRTYPE_IFOP:
465
	{
466 467 468 469 470 471
		count = verilogparseGenConstExpr(pFile, pre, pobj->data.expr0, module);
		count += verilogparseGenConstExpr(pFile, pre, pobj->data.expr1, module);
		count += verilogparseGenConstExpr(pFile, pre, pobj->data.expr2, module);
		fprintf(pFile, "%s{.exprtype=EXPRTYPE_IFOP, .op=0},\n", pre);
		count++;
		break;
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
	}
	case EXPRTYPE_HIERARCHICAL_IDENT:
	{
		int index = -1;
		/* 目前只取第一个名称 */
		verilogVarSel* select;
		select = verilogVarSelGetData(pobj->data.elementselect->__dlist_pNext);
		if (select != NULL) {
			index = verilogModuleGetParamIndex(module, select->name->string);
		}
		if (index >= 0)
			fprintf(pFile, "%s{.exprtype=EXPRTYPE_PARAM, .index=%d},/* %s */\n", pre, index, select->name->string);
		else 
			fprintf(pFile, "%s{error: can not find parameter %s}\n", pre, select->name->string);
		break;
	}
488
	default:
489 490
	{
		fprintf(pFile, "%s error type %d,\n", pre, pobj->data.exprtype);
491
		count = 0;
492 493
		break; 
	}
494 495 496
	}
	return count;
}