forked from Komodo2006/Hackathon-Nov25
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen2.cpp
More file actions
201 lines (201 loc) · 5.49 KB
/
codegen2.cpp
File metadata and controls
201 lines (201 loc) · 5.49 KB
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
#include<bits/stdc++.h>
using namespace std;
#define N 1000010
struct node{
int op,x,y,p0,p1;
}d[N];
string names[N];
int dcnt = 5;
int S = 1, I = 2, O0 = 3, O1 = 4;
int newnode(int op,int x,int y,int p0,int p1){
int ret = dcnt++;
names[ret]=to_string(ret);
d[ret]=(node){op,x,y,p0,p1};
return ret;
}
#define PUSH 0
#define SWAP 1
#define NIL SWAP,0,0
void connect0(int x,int y){
d[x].p0=y;
}
void connect1(int x,int y){
d[x].p1=y;
}
struct Bit{
int f,r0,r1,b,bp0,bp1;
void init0(){
bp0 = newnode(NIL,0,-1);
bp1 = newnode(NIL,0,-1);
b = newnode(NIL,bp0,bp1);
r1 = newnode(PUSH,b,bp1,0,-1);
r0 = newnode(PUSH,b,bp0,r1,-1);
f = newnode(SWAP,b,0,0,-1);
}
void init1(){
bp0 = newnode(NIL,0,-1);
bp1 = newnode(NIL,0,-1);
b = newnode(NIL,bp1,bp0);
r1 = newnode(PUSH,b,bp1,0,-1);
r0 = newnode(PUSH,b,bp0,r1,-1);
f = newnode(SWAP,b,0,0,-1);
}
};
namespace Logic{
Bit B0,B1;
void init(){
B0.init0();
B1.init1();
}
struct XorGate{
int s,t;
int _r1,_r2;
void init(Bit x, Bit y, Bit z){
s = newnode(NIL,x.b,-1);
connect0(x.bp0,y.b);
_r2 = newnode(SWAP,y.bp0,0,y.b,-1);
_r1 = newnode(SWAP,y.bp1,0,_r2,-1);
connect0(x.bp1,_r1);
//now, set the primary and secondary output of y.bp0 and y.bp1
connect0(y.bp0,z.r0);
connect0(y.bp1,z.f);
connect1(y.bp0,z.f);
connect1(y.bp1,z.r0);
t = newnode(NIL,0,-1);
connect0(z.r1,t);
connect0(z.f,t);
}
};
struct AndGate{
int s,t;
void init(Bit x, Bit y, Bit z){
s = newnode(NIL,x.b,-1);
connect0(x.bp0,z.r0);
connect0(x.bp1,y.b);
connect0(y.bp0,z.r0);
connect0(y.bp1,z.f);
t = newnode(NIL,0,-1);
connect0(z.r1,t);
connect0(z.f,t);
}
};
struct NotGate{
int s,t;
void init(Bit x,Bit y){
s = newnode(NIL,x.b,-1);
connect0(x.bp0,y.f);
connect0(x.bp1,y.r0);
t = newnode(NIL,0,-1);
connect0(y.r1,t);
connect0(y.f,t);
}
};
struct OrGate{
int s,t;
Bit nx,ny,nz;
NotGate ngx,ngy,ngz;
AndGate a;
void init(Bit x,Bit y,Bit z){
nx.init0();ny.init0();nz.init0();
ngx.init(x,nx); ngy.init(y,ny);
a.init(nx,ny,nz);
ngz.init(nz,z);
s = newnode(NIL,ngx.s,-1);
t = newnode(NIL,0,-1);
connect0(ngx.t,ngy.s);
connect0(ngy.t,a.s);
connect0(a.t,ngz.s);
connect0(ngz.t,t);
}
};
struct DuplicateGate{
int s,t;
void init(Bit x,Bit y, Bit z){// The r1&f of y&z are occupied, meaning they aren't writable.
s = newnode(NIL,x.b,-1);
connect0(x.bp0,y.r0);
connect0(x.bp1,y.f);
t = newnode(NIL,0,-1);
connect0(y.r1,z.r0);
connect0(y.f,z.f);
connect0(z.r1,t);
connect0(z.f,t);
}
};
struct HalfAdder{
int s,t;
Bit i0_d1,i1_d1,i0_d2,i1_d2;
DuplicateGate d0,d1;
XorGate x;
AndGate a;
void init(Bit i0,Bit i1,Bit o0,Bit o1){
i0_d1.init0();i1_d1.init0();
i0_d2.init0();i1_d2.init0();
d0.init(i0,i0_d1,i0_d2);
d1.init(i1,i1_d1,i1_d2);
s = newnode(NIL,d0.s,-1);
connect0(d0.t,d1.s);
x.init(i0_d1,i1_d1,o0);
a.init(i0_d2,i1_d2,o1);
connect0(d1.t,x.s);
connect0(x.t,a.s);
t = newnode(NIL,0,-1);
connect0(a.t,t);
}
};
};
namespace IO{
struct Reader{
int s,t;
void init(Bit x){
int c1 = newnode(PUSH,I,x.r0,-1,0);
int c2 = newnode(PUSH,I,x.f,I,0);
s = newnode(NIL,c1,0);
t = newnode(NIL,0,0);
connect0(c1,c2);
connect0(x.r1,t);
connect0(x.f,t);
}
};
struct Writer{
int s,t;
void init(Bit x){
int s = newnode(NIL,0,0);
int t = newnode(NIL,0,0);
int c1 = newnode(PUSH,O0,t,O0,0);
int c2 = newnode(PUSH,O0,0,c1,0);
int c3 = newnode(PUSH,O1,t,O1,0);
int c4 = newnode(PUSH,O1,0,c3,0);
connect0(s,x.b);
connect0(x.bp0,c2);
connect0(x.bp1,c4);
}
};
};
void print(){
for(int i=1;i<dcnt;i++){
printf("NODE %s(",names[i].c_str());
if(d[i].p1==-1)d[i].p1=0;
if(d[i].op==SWAP)printf("SWAP %s) ",names[d[i].x].c_str());
else printf("PUSH %s %s) ",names[d[i].x].c_str(),names[d[i].y].c_str());
printf("%s %s\n",names[d[i].p0].c_str(),names[d[i].p1].c_str());
}
}
int main(){
names[0]="*";
names[S]="S";names[I]="I";names[O0]="O0";names[O1]="O1";
Logic::init();
Bit x,y,z,w;
x.init0();y.init0();z.init0();w.init0();
IO :: Reader rx,ry;
rx.init(x);ry.init(y);
connect0(S,rx.s); connect0(rx.t,ry.s);
Logic::HalfAdder ha; ha.init(x,y,z,w);
connect0(ry.t,ha.s);
connect0(ha.t,z.b);
connect0(z.bp0,O0); connect0(z.bp1,O1);
int c5 = newnode(PUSH,O0,0,w.b,0);
int c6 = newnode(PUSH,O1,0,c5,0);
connect0(O0,c6);connect0(O1,c6);
connect0(w.bp0,O0); connect0(w.bp1,O1);
print();
}