Skip to content

Commit 36e50d5

Browse files
committed
improve module parameters (overrides for CON declarations)
1 parent 04989e9 commit 36e50d5

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

Changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Version 7.4.4
22
- Added some missing libc.a files (thanks to Ada)
3+
- Improved module constant overrides
34

45
Version 7.4.3
56
- Remove some unused debug strings when debugging is not enabled

frontends/common.c

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,61 @@ DeclareConstants(Module *P, AST **conlist_ptr)
684684
*conlist_ptr = completed_declarations;
685685
}
686686

687+
static AST *
688+
ReplaceIdentifierFrom(AST *id, AST *varlist)
689+
{
690+
const char *name = GetIdentifierName(id);
691+
AST *item, *val;
692+
while (varlist) {
693+
item = varlist->left;
694+
varlist = varlist->right;
695+
while (item && item->kind == AST_COMMENTEDNODE)
696+
item = item->left;
697+
if (item && item->kind == AST_ASSIGN) {
698+
const char *itemname;
699+
val = item->right;
700+
item = item->left;
701+
itemname = GetIdentifierName(item);
702+
if (itemname && !strcasecmp(itemname, name)) {
703+
return DupAST(val);
704+
}
705+
}
706+
}
707+
return id;
708+
}
709+
static AST *
710+
SimplifyOneObjParam(AST *param)
711+
{
712+
if (!param)
713+
return param;
714+
switch(param->kind) {
715+
case AST_IDENTIFIER:
716+
return ReplaceIdentifierFrom(param, current->objparams);
717+
default:
718+
break;
719+
}
720+
if (param->left)
721+
param->left = SimplifyOneObjParam(param->left);
722+
if (param->right)
723+
param->right = SimplifyOneObjParam(param->right);
724+
return param;
725+
}
726+
727+
AST *
728+
SimplifyObjParams(AST *params)
729+
{
730+
AST *orig_params = params;
731+
while (params) {
732+
AST *node = params->left;
733+
while (node && node->kind == AST_COMMENTEDNODE)
734+
node = node->left;
735+
if (node && node->kind == AST_ASSIGN)
736+
node->right = SimplifyOneObjParam(node->right);
737+
params = params->right;
738+
}
739+
return orig_params;
740+
}
741+
687742
void
688743
ProcessConstantOverrides(Module *P)
689744
{
@@ -709,8 +764,10 @@ ProcessConstantOverrides(Module *P)
709764
if (ident->kind == AST_IDENTIFIER) {
710765
const char *name = ident->d.string;
711766
Symbol *sym = LookupSymbolInTable(&P->objsyms, name);
712-
if (!sym || sym->kind != SYM_CONSTANT) {
713-
ERROR(ident, "object parameter %s not found or not a constant", name);
767+
if (!sym) {
768+
ERROR(ident, "object parameter %s not found", name);
769+
} else if (sym->kind != SYM_CONSTANT) {
770+
ERROR(ident, "object parameter %s is not a constant", name);
714771
} else if (!IsConstExpr(valast)) {
715772
ERROR(ident, "new value for %s is not constant", name);
716773
} else {

frontends/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,10 @@ int ParseWarnString(AST *lineNum, const char *str, int *flags);
11061106
/* declare constants */
11071107
void DeclareConstants(Module *P, AST **conlist);
11081108

1109+
/* simplify a list of parameters to objects (replacing names with
1110+
constants where possible */
1111+
AST *SimplifyObjParams(AST *params);
1112+
11091113
/* process constants to set clock frequency and such */
11101114
void ProcessConstantOverrides(Module *P);
11111115

spinc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ doParseFile(const char *name, Module *P, int *is_dup, AST *paramlist)
684684
if (paramlist) {
685685
P->basename = NewTemporaryVariable(P->basename, NULL);
686686
P->classname = NewTemporaryVariable(P->classname, NULL);
687-
P->objparams = paramlist;
687+
P->objparams = SimplifyObjParams(paramlist);
688688
}
689689
}
690690
P->curLanguage = language;

0 commit comments

Comments
 (0)