authorgravatar for 81774659+gracefuu@users.noreply.github.comgracefu <81774659+gracefuu@users.noreply.github.com> 2021-04-08 05:24:40+08:00
committergravatar for 81774659+gracefuu@users.noreply.github.comgracefu <81774659+gracefuu@users.noreply.github.com> 2021-04-08 05:27:00+08:00
loge4a60b63f2c3551440f9b58e8c556545497827bc
treec31ae2d6e96742826b7796c99de5c4542bad04a2
parent4c71942f84261e9872cd27139c45b6d5fb1ab6c7
signaturelock-open Commit is signed but in an unrecognized format.

stage2 wasm: Add bitwise/boolean ops &, |, ^, and, or


2 files changed, 107 insertions(+), 0 deletions(-)

src/codegen/wasm.zig+7
...@@ -657,6 +657,10 @@ pub const Context = struct {...@@ -657,6 +657,10 @@ pub const Context = struct {
657 .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?),657 .breakpoint => self.genBreakpoint(inst.castTag(.breakpoint).?),
658 .br => self.genBr(inst.castTag(.br).?),658 .br => self.genBr(inst.castTag(.br).?),
659 .call => self.genCall(inst.castTag(.call).?),659 .call => self.genCall(inst.castTag(.call).?),
660 .bit_or => self.genBinOp(inst.castTag(.bit_or).?, .@"or"),
661 .bit_and => self.genBinOp(inst.castTag(.bit_and).?, .@"and"),
662 .bool_or => self.genBinOp(inst.castTag(.bool_or).?, .@"or"),
663 .bool_and => self.genBinOp(inst.castTag(.bool_and).?, .@"and"),
660 .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq),664 .cmp_eq => self.genCmp(inst.castTag(.cmp_eq).?, .eq),
661 .cmp_gte => self.genCmp(inst.castTag(.cmp_gte).?, .gte),665 .cmp_gte => self.genCmp(inst.castTag(.cmp_gte).?, .gte),
662 .cmp_gt => self.genCmp(inst.castTag(.cmp_gt).?, .gt),666 .cmp_gt => self.genCmp(inst.castTag(.cmp_gt).?, .gt),
...@@ -669,6 +673,8 @@ pub const Context = struct {...@@ -669,6 +673,8 @@ pub const Context = struct {
669 .load => self.genLoad(inst.castTag(.load).?),673 .load => self.genLoad(inst.castTag(.load).?),
670 .loop => self.genLoop(inst.castTag(.loop).?),674 .loop => self.genLoop(inst.castTag(.loop).?),
671 .mul => self.genBinOp(inst.castTag(.mul).?, .mul),675 .mul => self.genBinOp(inst.castTag(.mul).?, .mul),
676 .div => self.genBinOp(inst.castTag(.div).?, .div),
677 .xor => self.genBinOp(inst.castTag(.xor).?, .xor),
672 .not => self.genNot(inst.castTag(.not).?),678 .not => self.genNot(inst.castTag(.not).?),
673 .ret => self.genRet(inst.castTag(.ret).?),679 .ret => self.genRet(inst.castTag(.ret).?),
674 .retvoid => WValue.none,680 .retvoid => WValue.none,
...@@ -764,6 +770,7 @@ pub const Context = struct {...@@ -764,6 +770,7 @@ pub const Context = struct {
764 const opcode: wasm.Opcode = buildOpcode(.{770 const opcode: wasm.Opcode = buildOpcode(.{
765 .op = op,771 .op = op,
766 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),772 .valtype1 = try self.typeToValtype(inst.base.src, inst.base.ty),
773 .signedness = if (inst.base.ty.isSignedInt()) .signed else .unsigned,
767 });774 });
768 try self.code.append(wasm.opcode(opcode));775 try self.code.append(wasm.opcode(opcode));
769 return .none;776 return .none;
test/stage2/wasm.zig+100
...@@ -153,6 +153,106 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -153,6 +153,106 @@ pub fn addCases(ctx: *TestContext) !void {
153 \\ return x * y;153 \\ return x * y;
154 \\}154 \\}
155 , "350\n");155 , "350\n");
156
157 case.addCompareOutput(
158 \\export fn _start() u32 {
159 \\ var i: u32 = 352;
160 \\ i /= 7; // i = 50
161 \\ var result: u32 = foo(i, 7);
162 \\ return result;
163 \\}
164 \\fn foo(x: u32, y: u32) u32 {
165 \\ return x / y;
166 \\}
167 , "7\n");
168
169 case.addCompareOutput(
170 \\export fn _start() u32 {
171 \\ var i: u32 = 5;
172 \\ i &= 6;
173 \\ return i;
174 \\}
175 , "4\n");
176
177 case.addCompareOutput(
178 \\export fn _start() u32 {
179 \\ var i: u32 = 5;
180 \\ i |= 6;
181 \\ return i;
182 \\}
183 , "7\n");
184
185 case.addCompareOutput(
186 \\export fn _start() u32 {
187 \\ var i: u32 = 5;
188 \\ i ^= 6;
189 \\ return i;
190 \\}
191 , "3\n");
192
193 case.addCompareOutput(
194 \\export fn _start() bool {
195 \\ var b: bool = false;
196 \\ b = b or false;
197 \\ return b;
198 \\}
199 , "0\n");
200
201 case.addCompareOutput(
202 \\export fn _start() bool {
203 \\ var b: bool = true;
204 \\ b = b or false;
205 \\ return b;
206 \\}
207 , "1\n");
208
209 case.addCompareOutput(
210 \\export fn _start() bool {
211 \\ var b: bool = false;
212 \\ b = b or true;
213 \\ return b;
214 \\}
215 , "1\n");
216
217 case.addCompareOutput(
218 \\export fn _start() bool {
219 \\ var b: bool = true;
220 \\ b = b or true;
221 \\ return b;
222 \\}
223 , "1\n");
224
225 case.addCompareOutput(
226 \\export fn _start() bool {
227 \\ var b: bool = false;
228 \\ b = b and false;
229 \\ return b;
230 \\}
231 , "0\n");
232
233 case.addCompareOutput(
234 \\export fn _start() bool {
235 \\ var b: bool = true;
236 \\ b = b and false;
237 \\ return b;
238 \\}
239 , "0\n");
240
241 case.addCompareOutput(
242 \\export fn _start() bool {
243 \\ var b: bool = false;
244 \\ b = b and true;
245 \\ return b;
246 \\}
247 , "0\n");
248
249 case.addCompareOutput(
250 \\export fn _start() bool {
251 \\ var b: bool = true;
252 \\ b = b and true;
253 \\ return b;
254 \\}
255 , "1\n");
156 }256 }
157257
158 {258 {