-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgen_plugin.cpp
182 lines (165 loc) · 5.51 KB
/
gen_plugin.cpp
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
/*
* Copyright (c) 2020 Romain Dolbeau <[email protected]>
* MIT License
* See the LICENSE file at the top level of this software distribution for details.
*/
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include "inst.hpp"
#include "unparse.hpp"
#include "gen_plugin.hpp"
#include <cstring>
#include <unistd.h>
// yucc
extern "C" {
#include "inst_par.h"
extern FILE *yyin, *yyout;
extern int yydebug;
}
std::set<const instruction*> instructions;
std::map<std::string, std::string> semantics;
std::map<std::string, int> em_widths;
std::map<std::string, std::string> mem_semantics;
std::vector<std::string> prologues;
std::vector<std::string> extras;
void add_inst0(const char* name, const char* opname, const char* key, const char* group) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
instructions.insert(i);
}
void add_inst1(const char* name, const char* opname, const char* key, const char* group, const char* e1) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
i->addExt(e1);
instructions.insert(i);
}
void add_inst2(const char* name, const char* opname, const char* key, const char* group, const char* e1, const char* e2) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
i->addExt(e1);
i->addExt(e2);
instructions.insert(i);
}
void add_inst3(const char* name, const char* opname, const char* key, const char* group, const char* e1, const char* e2, const char *e3) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
i->addExt(e1);
i->addExt(e2);
i->addExt(e3);
instructions.insert(i);
}
void add_inst4(const char* name, const char* opname, const char* key, const char* group, const char* e1, const char* e2, const char *e3, const char *e4) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
i->addExt(e1);
i->addExt(e2);
i->addExt(e3);
i->addExt(e4);
instructions.insert(i);
}
void add_inst5(const char* name, const char* opname, const char* key, const char* group, const char* e1, const char* e2, const char *e3, const char *e4, const char *e5) {
if (strlen(key) != 32) { fprintf(stderr, "For %s / %s: %s isn't 32 characters long\n", name, opname, key); }
instruction* i = new instruction(name, opname, key, group);
i->addExt(e1);
i->addExt(e2);
i->addExt(e3);
i->addExt(e4);
i->addExt(e5);
instructions.insert(i);
}
void add_sem(const char* name, const char* sem) {
const std::string key(name);
semantics[key] = std::string(sem);
}
void add_memsem(const char* name, const int em_width, const char* memsem) {
const std::string key(name);
if ((em_width > 0) && (memsem != NULL)) {
em_widths[key] = em_width;
mem_semantics[key] = std::string(memsem);
}
}
void add_prol(const char *prol) {
prologues.push_back(std::string(prol));
}
void add_extra(const char *extra) {
extras.push_back(std::string(extra));
}
int main(int argc, char **argv) {
int c, done = 0, wide = 0;
extern char *optarg;
extern int optind, optopt;
FILE *myfile;
char *pluginName = NULL;
#define MAX_INSTS 1024
char* insts[MAX_INSTS];
int instidx = 0;
while ((c = getopt(argc, argv, "n:i:I:w")) != -1) {
switch(c) {
default:
fprintf(stderr, "usage: %s [-w] -n <plugin_name> -i <input_file> -I <instructions> \n", argv[0]);
break;
case 'i':
myfile = fopen(optarg, "r");
if (!myfile) {
fprintf(stderr, "no file\n");
return -1;
}
yyin = myfile;
do {
yyparse();
} while (!feof(yyin));
fclose(myfile);
done = 1;
break;
case 'n':
pluginName = strndup(optarg, 512);
break;
case 'I':
if (instidx < MAX_INSTS) {
insts[instidx] = strndup(optarg, 512);
instidx ++;
}
break;
case 'w':
wide = 1;
break;
}
}
if (!done || !pluginName || (instidx == 0)) {
std::cerr << "Should have at least a plugin name & a datafile & some instructions enabled" << std::endl;
exit(-1);
}
std::set<const instruction*> filtered_instructions;
for (const instruction* inst : instructions) {
// if (inst->isWord())
// continue;
if (semantics[inst->opname] == "")
continue;
bool addinst = false;
for (int i = 0 ; i < instidx; i++) {
if (inst->match(insts[i]) || (strncmp(insts[i], "*", 1)==0)) {
// printf("%s is in %s\n", inst->name.c_str(), insts[i]);
addinst = true;
} else {
// printf("%s is NOT in %s\n", inst->name.c_str(), insts[i]);
}
}
if (addinst)
filtered_instructions.insert(inst);
//printf("adding %s\n", inst->name.c_str());
}
if ((em_widths.size() != 0) || (mem_semantics.size() !=0)) {
if (em_widths.size() != mem_semantics.size()) {
std::cerr << "Multicycle error: emwidths.size() != mem_semantics.size()" << std::endl;
exit(-1);
}
if (semantics.size() != mem_semantics.size()) {
std::cerr << "Multicycle error: semantics.size() != mem_semantics.size() (all instructions should have the same nuber of cycles in the same plugin)" << std::endl;
exit(-1);
}
}
unparse(std::cout, pluginName, filtered_instructions, semantics, em_widths, mem_semantics, prologues, extras, wide == 1);
return 0;
}