Skip to content

Commit 952e4b7

Browse files
committed
added more calculate function
1 parent 57df9ee commit 952e4b7

File tree

9 files changed

+107
-30
lines changed

9 files changed

+107
-30
lines changed

ODIN_II/SRC/ast_elaborate.c

100755100644
File mode changed.

ODIN_II/SRC/ast_elaborate.h

100755100644
File mode changed.

ODIN_II/SRC/ast_optimizations.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright (c) 2009 Peter Andrew Jamieson ([email protected])
3-
43
Permission is hereby granted, free of charge, to any person
54
obtaining a copy of this software and associated documentation
65
files (the "Software"), to deal in the Software without
@@ -9,10 +8,8 @@ copy, modify, merge, publish, distribute, sublicense, and/or sell
98
copies of the Software, and to permit persons to whom the
109
Software is furnished to do so, subject to the following
1110
conditions:
12-
1311
The above copyright notice and this permission notice shall be
1412
included in all copies or substantial portions of the Software.
15-
1613
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1714
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
1815
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
@@ -342,4 +339,4 @@ info_ast_visit_t *constantFold(ast_node_t *node)
342339
}
343340

344341
return node_details;
345-
}
342+
}

ODIN_II/SRC/ast_util.c

Lines changed: 96 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -970,32 +970,6 @@ char *make_module_param_name(ast_node_t *module_param_list, char *module_name)
970970
}
971971

972972

973-
/*---------------------------------------------------------------------------------------------
974-
* (function: calculate)
975-
* Calculate binary operations
976-
*-------------------------------------------------------------------------------------------*/
977-
long calculate(long operand0, long operand1, short type)
978-
{
979-
long result = 0;
980-
switch(type){
981-
case ADD:
982-
result = operand0 + operand1;
983-
break;
984-
case MINUS:
985-
result = operand0 - operand1;
986-
break;
987-
case MULTIPLY:
988-
result = operand0 * operand1;
989-
break;
990-
case DIVIDE:
991-
result = operand0 / operand1;
992-
break;
993-
default:
994-
break;
995-
}
996-
return result;
997-
}
998-
999973
/*---------------------------------------------------------------------------------------------
1000974
* (function: move_ast_node)
1001975
* move node from src to dest
@@ -1048,3 +1022,99 @@ ast_node_t *ast_node_deep_copy(ast_node_t *node){
10481022

10491023
return node_copy;
10501024
}
1025+
1026+
1027+
/*---------------------------------------------------------------------------------------------
1028+
* (function: calculate)
1029+
* Calculate binary operations
1030+
*-------------------------------------------------------------------------------------------*/
1031+
long calculate(long operand0, long operand1, short type){
1032+
long result = 0;
1033+
long long temp =calculate_binary((long long) operand0, (long long) operand1,type);
1034+
if(!(temp & WRONG_CALCULATION)){
1035+
return (long) temp;
1036+
}
1037+
return result;
1038+
}
1039+
1040+
1041+
/*---------------------------------------------------------------------------------------------
1042+
* (function: calculate_unary)
1043+
*-------------------------------------------------------------------------------------------*/
1044+
long long calculate_unary(long long operand_0, short type_of_ops){
1045+
if(operand_0 != WRONG_CALCULATION){
1046+
switch (type_of_ops){
1047+
case LOGICAL_NOT:
1048+
return !operand_0;
1049+
case BITWISE_NOT:
1050+
return ~operand_0;
1051+
case MINUS:
1052+
return -operand_0;
1053+
default:
1054+
break;
1055+
}
1056+
}
1057+
return WRONG_CALCULATION;
1058+
}
1059+
1060+
/*---------------------------------------------------------------------------------------------
1061+
* (function: calculate_binary)
1062+
*-------------------------------------------------------------------------------------------*/
1063+
long long calculate_binary(long long operand_0, long long operand_1, short type_of_ops){
1064+
if(!(operand_0 & WRONG_CALCULATION) && !(operand_1 & WRONG_CALCULATION)){
1065+
switch (type_of_ops){
1066+
case ADD:
1067+
return operand_0 + operand_1;
1068+
case MINUS:
1069+
return operand_0 - operand_1;
1070+
case MULTIPLY:
1071+
return operand_0 * operand_1;
1072+
case DIVIDE:
1073+
return operand_0 / operand_1;
1074+
case BITWISE_XOR:
1075+
return operand_0 ^ operand_1;
1076+
case BITWISE_XNOR:
1077+
return ~(operand_0 ^ operand_1);
1078+
case BITWISE_AND:
1079+
return operand_0 & operand_1;
1080+
case BITWISE_NAND:
1081+
return ~(operand_0 & operand_1);
1082+
case BITWISE_OR:
1083+
return operand_0 | operand_1;
1084+
case BITWISE_NOR:
1085+
return ~(operand_0 | operand_1);
1086+
case SL:
1087+
return operand_0 << operand_1;
1088+
case SR:
1089+
return operand_0 >> operand_1;
1090+
case LOGICAL_AND:
1091+
return operand_0 && operand_1;
1092+
case LOGICAL_OR:
1093+
return operand_0 || operand_1;
1094+
default:
1095+
break;
1096+
}
1097+
}
1098+
return WRONG_CALCULATION;
1099+
}
1100+
1101+
/*---------------------------------------------------------------------------------------------
1102+
* (function: node_is_constant)
1103+
*-------------------------------------------------------------------------------------------*/
1104+
long long node_is_constant(ast_node_t *node){
1105+
if (node != NULL && node->type == NUMBERS){
1106+
/* check if it's a constant depending on the number type */
1107+
switch (node->types.number.base){
1108+
case DEC: case HEX: case OCT: case BIN:
1109+
if (node->types.number.value == -1){
1110+
break;
1111+
}
1112+
case(LONG_LONG):
1113+
return (long long) node->types.number.value;
1114+
break;
1115+
default:
1116+
break;
1117+
}
1118+
}
1119+
return WRONG_CALCULATION;
1120+
}

ODIN_II/SRC/ast_util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ char_list_t *get_name_of_pins_with_prefix(ast_node_t *var_node, char *instance_n
2424
ast_node_t *resolve_node(char *module_name, ast_node_t *node);
2525
char *make_module_param_name(ast_node_t *module_param_list, char *module_name);
2626
long calculate(long operand0, long operand1, short type);
27+
long long calculate_unary(long long operand_0, short type_of_ops);
28+
long long calculate_binary(long long operand_0,long long operand_1 , short type_of_ops);
29+
2730
void move_ast_node(ast_node_t *src, ast_node_t *dest, ast_node_t *node);
2831

2932
ast_node_t *ast_node_deep_copy(ast_node_t *node);
33+
long long node_is_constant(ast_node_t *node);

ODIN_II/SRC/netlist_create_from_ast.c

100755100644
File mode changed.

ODIN_II/SRC/netlist_utils.c

100755100644
File mode changed.

ODIN_II/SRC/odin_ii_func.c

100755100644
File mode changed.

ODIN_II/SRC/types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ typedef struct chain_information_t_t chain_information_t;
6868
#define ACTIVATION_ERROR -7
6969
/* for errors in the netlist simulation */
7070
#define SIMULATION_ERROR -8
71+
/* for errors in the calculation
72+
* largest hex value it can take
73+
* avoids using include limit.h since it may (or may not) conflict with naming.
74+
* also keep it "portable" is long long reall necessary tho? long would suffice and c99 wouldnt be essential
75+
*/
76+
# define WRONG_CALCULATION (long long)1 << (sizeof(long long)*8-1)
7177

7278
/* unique numbers to mark the nodes as we DFS traverse the netlist */
7379
#define PARTIAL_MAP_TRAVERSE_VALUE 10

0 commit comments

Comments
 (0)