authorgravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-09-08 14:39:47-04:00
committergravatar for noam@pixelhero.devNoam Preil <noam@pixelhero.dev> 2020-10-06 15:09:57-04:00
logea7b2750c823e7160db790582a1dcb96a9feebc7
tree841841bd51f53c8403cdd81bfe6b6054f023cc23
parente06ba9e86e53797d90d74e87724b897858fe2d77
signaturelock-open Commit is signed but in an unrecognized format.

CBE: addition and subtraction


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

src/codegen/c.zig+15
......@@ -160,6 +160,8 @@ fn genFn(file: *C, decl: *Decl) !void {
160160 if (switch (inst.tag) {
161161 .assembly => try genAsm(&ctx, inst.castTag(.assembly).?),
162162 .call => try genCall(&ctx, inst.castTag(.call).?),
163 .add => try genBinOp(&ctx, inst.cast(Inst.BinOp).?, "+"),
164 .sub => try genBinOp(&ctx, inst.cast(Inst.BinOp).?, "-"),
163165 .ret => try genRet(&ctx, inst.castTag(.ret).?),
164166 .retvoid => try genRetVoid(&ctx),
165167 .arg => try genArg(&ctx),
......@@ -207,6 +209,19 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 {
207209 return name;
208210}
209211
212fn genBinOp(ctx: *Context, inst: *Inst.BinOp, comptime operator: []const u8) !?[]u8 {
213 if (inst.base.isUnused())
214 return null;
215 const lhs = ctx.resolveInst(inst.lhs);
216 const rhs = ctx.resolveInst(inst.rhs);
217 const writer = ctx.file.main.writer();
218 const name = try ctx.name();
219 try writer.writeAll(indentation ++ "const ");
220 try renderType(ctx, writer, inst.base.ty);
221 try writer.print(" {} = {} " ++ operator ++ " {};\n", .{ name, lhs, rhs });
222 return name;
223}
224
210225fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 {
211226 const writer = ctx.file.main.writer();
212227 const header = ctx.file.header.writer();
test/stage2/cbe.zig+48
......@@ -147,4 +147,52 @@ pub fn addCases(ctx: *TestContext) !void {
147147 \\}
148148 \\
149149 );
150 ctx.c("exit with u8 arithmetic", linux_x64,
151 \\export fn _start() noreturn {
152 \\ exitMath(1);
153 \\}
154 \\
155 \\fn exitMath(a: u8) noreturn {
156 \\ exit(0 + a - a);
157 \\}
158 \\
159 \\fn exit(code: u8) noreturn {
160 \\ asm volatile ("syscall"
161 \\ :
162 \\ : [number] "{rax}" (231),
163 \\ [arg1] "{rdi}" (code)
164 \\ );
165 \\ unreachable;
166 \\}
167 \\
168 ,
169 \\#include <stddef.h>
170 \\#include <stdint.h>
171 \\
172 \\zig_noreturn void exitMath(uint8_t arg0);
173 \\zig_noreturn void exit(uint8_t arg0);
174 \\
175 \\const char *const exit__anon_0 = "{rax}";
176 \\const char *const exit__anon_1 = "{rdi}";
177 \\const char *const exit__anon_2 = "syscall";
178 \\
179 \\zig_noreturn void _start(void) {
180 \\ exitMath(1);
181 \\}
182 \\
183 \\zig_noreturn void exitMath(uint8_t arg0) {
184 \\ const uint8_t __temp_0 = 0 + arg0;
185 \\ const uint8_t __temp_1 = __temp_0 - arg0;
186 \\ exit(__temp_1);
187 \\}
188 \\
189 \\zig_noreturn void exit(uint8_t arg0) {
190 \\ const size_t __temp_0 = (size_t)arg0;
191 \\ register size_t rax_constant __asm__("rax") = 231;
192 \\ register size_t rdi_constant __asm__("rdi") = __temp_0;
193 \\ __asm volatile ("syscall" :: ""(rax_constant), ""(rdi_constant));
194 \\ zig_unreachable();
195 \\}
196 \\
197 );
150198}