-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.h
247 lines (222 loc) · 8.29 KB
/
common.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
/*
Copyright (C) 2018, The Connectal Project
This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h> // atol
#include <string.h>
#include <assert.h>
#include "AtomiccIR.h"
#define SUBSCRIPT_MARKER "@["
#define PARAMETER_MARKER "@{"
#define SUBSCRIPT_CLOSE "@]"
#define PARAMETER_CLOSE "@}"
#define ASSIGN_SIZE_LIMIT 6
#define GENVAR_NAME "__inst$Genvar"
#define BLOCK_NAME "BasicBlockCond_"
#define COUNT_LIMIT 0//10
#define VARIANT "_OC_"
#define VARIANTP "_IC_"
#define SUFFIX_FOR_GENERIC VARIANT "__"
#define LOCAL_VARIABLE_PREFIX "_"
#define ARRAY_ELEMENT_MARKER "_"
#define DOLLAR "$"
#define PERIOD "."
#define GENERIC_INT_TEMPLATE_FLAG 999999
#define GENERIC_INT_TEMPLATE_FLAG_STRING "999999"
typedef struct {
std::string argName;
std::string value;
std::string type;
bool moduleStart;
std::string clockValue;
int out;
bool inout;
bool trigger;
std::string isparam; // initial value for parameters
std::string vecCount;
} ModData;
typedef std::list<ModData> ModList;
typedef struct {
std::string name;
std::string base;
std::string type;
bool alias;
uint64_t offset;
} FieldItem;
typedef struct {
ACCExpr *value;
std::string type;
bool noRecursion;
int size;
} AssignItem;
enum {PIN_MODULE=1, PIN_OBJECT, PIN_REG, PIN_WIRE, PIN_CONSTANT};
typedef struct {
int count;
std::string type;
bool out;
bool inout;
int pin;
bool done;
std::string vecCount;
bool isArgument;
std::string clockName;
} RefItem;
typedef struct {
long upper;
ACCExpr *value;
std::string type;
} BitfieldPart;
typedef struct {
ACCExpr *dest;
ACCExpr *value;
} CondInfo;
typedef struct {
ACCExpr * cond;
std::list<CondInfo> info;
} CondGroupInfo;
typedef struct {
ACCExpr *guard;
std::map<std::string, CondGroupInfo> info;
} CondGroup;
typedef struct {
std::string format;
std::list<int> width;
} PrintfInfo;
typedef struct {
ACCExpr *cond;
ACCExpr *value;
} AssertVerilog;
typedef struct {
std::map<std::string, CondGroup> cond;
} AlwaysGroup;
typedef struct {
std::map<std::string, AlwaysGroup> always; // index is 'always variety'
std::list<AssertVerilog> assertList;
} CondLineType;
#define ALWAYS_CLOCKED "always @( posedge "
#define ALWAYS_STAR "always @(*)"
#define ALWAYS_COMB "always_comb"
typedef struct {
ACCExpr *phi;
std::string defaultValue;
bool isParam;
} MuxValueElement;
typedef ModuleIR *(^CBFun)(FieldElement &item);
typedef std::map<std::string, std::string> MapNameValue;
// util.cpp
std::string baseMethodName(std::string pname);
std::string getRdyName(std::string basename, bool isAsync = false);
std::string getEnaName(std::string basename);
bool isRdyName(std::string name);
bool isEnaName(std::string name);
void extractParam(std::string debugName, std::string replaceList, MapNameValue &mapValue);
std::string instantiateType(std::string arg, MapNameValue &mapValue);
std::string genericModuleParam(std::string name, std::string param, MapNameValue *mapValue);
std::string convertType(std::string arg, int arrayProcess = 0);
ModuleIR *lookupIR(std::string ind);
ModuleIR *lookupInterface(std::string ind);
std::string sizeProcess(std::string type);
std::string typeDeclaration(std::string type);
MethodInfo *lookupMethod(ModuleIR *IR, std::string name);
MethodInfo *lookupQualName(ModuleIR *searchIR, std::string searchStr, std::string &vecCount, MapNameValue &mapValue);
ModuleIR *allocIR(std::string name, bool isInterface = false);
MethodInfo *allocMethod(std::string name);
bool addMethod(ModuleIR *IR, MethodInfo *MI);
void dumpMethod(std::string name, MethodInfo *MI);
void dumpModule(std::string name, ModuleIR *IR);
std::string findType(std::string name);
std::string CBEMangle(const std::string &S);
char *getExecutionFilename(char *buf, int buflen);
void walkReplaceBuiltin(ACCExpr *expr, std::string phiDefault);
std::string exprWidth(ACCExpr *expr, bool forceNumeric = false);
std::string makeSection(std::string var, ACCExpr *init, ACCExpr *limit, ACCExpr *incr);
typedef const char *CCharPointer;
std::string getBoundedString(CCharPointer *bufpp, char terminator = 0);
void extractSubscript(std::string &source, int index, std::string &sub);
void buildAccessible(ModuleIR *IR);
void fixupAccessible(std::string &name);
std::string cleanupModuleType(std::string type);
void normalizeIdentifier(ACCExpr *expr);
// expr.cpp
bool bitOp(std::string s);
bool plainInteger(std::string val);
std::string tree2str(ACCExpr *expr, bool addSpaces = true);
ACCExpr *allocExpr(std::string value, ACCExpr *argl = nullptr, ACCExpr *argr = nullptr, ACCExpr *argt = nullptr);
bool isIdChar(char ch);
bool isParen(std::string ch);
void dumpExpr(std::string tag, ACCExpr *next);
ACCExpr *cleanupExpr(ACCExpr *expr, bool preserveParen = false);
ACCExpr *cleanupExprBuiltin(ACCExpr *expr, std::string phiDefault = "0", bool preserveParen = false);
ACCExpr *cleanupInteger(ACCExpr *expr);
ACCExpr *cleanupBool(ACCExpr *expr);
ACCExpr *str2tree(std::string arg, bool allowRangeParam = false);
bool checkOperand(std::string s);
ACCExpr *invertExpr(ACCExpr *expr);
void updateWidth(ACCExpr *item, std::string clen);
ACCExpr *getRHS(ACCExpr *expr, int match = 1);
bool matchExpr(ACCExpr *lhs, ACCExpr *rhs);
int walkCount (ACCExpr *expr);
ACCExpr *dupExpr(ACCExpr *expr);
bool checkIntegerString(std::string val, std::string pattern);
bool checkInteger(ACCExpr *expr, std::string pattern);
bool booleanBinop(std::string s);
bool arithOp(std::string s);
bool relationalOp(std::string s);
bool isConstExpr(ACCExpr *expr);
std::string replacePeriod(std::string value);
ACCExpr *cleanupModuleParam(std::string param);
extern int trace_expr, trace_interface;
// readIR.cpp
void readIR(std::list<ModuleIR *> &irSeq, std::list<std::string> &fileList, std::string OutputDir);
// interfaces.cpp
void processInterfaces(std::list<ModuleIR *> &irSeq);
ACCExpr *printfArgs(ACCExpr *listp);
// software.cpp
int generateSoftware(std::list<ModuleIR *> &irSeq, const char *exename, std::string outName);
// metaGen.cpp
void metaGenerateModule(ModuleIR *IR, FILE *OStr);
// preprocessIR.cpp
void preprocessIR(std::list<ModuleIR *> &irSeq);
void cleanupIR(std::list<ModuleIR *> &irSeq);
// verilog.cpp
void generateModuleDef(ModuleIR *IR, ModList &modLineTop);
void connectMethodList(ModuleIR *IIR, ACCExpr *targetTree, ACCExpr *sourceTree, bool isForward);
// filegen.cpp
void generateModuleHeader(FILE *OStr, ModList &modLine, bool isTopModule);
void generateVerilogOutput(FILE *OStr);
// kami.cpp
void generateKami(std::list<ModuleIR *> &irSeq, std::string myName, std::string OutputDir);
extern int trace_assign;
extern int trace_expand;
extern int trace_skipped;
extern std::map<std::string, RefItem> refList;
extern std::map<std::string, AssignItem> assignList;
extern std::map<std::string, std::map<std::string, AssignItem>> condAssignList;
extern std::map<std::string, ModuleIR *> mapIndex, interfaceIndex, mapAllModule;
extern std::map<std::string, int> replaceBlock;
extern ModList modNew;
extern std::map<std::string, CondLineType> condLines;
extern std::list<PrintfInfo> printfFormat;
extern std::map<std::string, int> genericModule;
extern int globalExprCleanup;
extern int flagErrorsCleanup;
typedef struct {
std::string name;
std::string instance;
bool out;
MethodInfo *MI;
} SyncPinInfo;
extern std::map<std::string, SyncPinInfo> syncPins; // SyncFF items needed for PipeInSync instances
extern int implementPrintf;
extern std::string myGlobalName;
extern std::map<std::string, std::map<std::string, MuxValueElement>> muxValueList;