| author | |
| committer | |
| log | ea7b2750c823e7160db790582a1dcb96a9feebc7 |
| tree | 841841bd51f53c8403cdd81bfe6b6054f023cc23 |
| parent | e06ba9e86e53797d90d74e87724b897858fe2d77 |
| signature | Commit is signed but in an unrecognized format. |
2 files changed, 63 insertions(+), 0 deletions(-)
src/codegen/c.zig+15| ... | @@ -160,6 +160,8 @@ fn genFn(file: *C, decl: *Decl) !void { | ... | @@ -160,6 +160,8 @@ fn genFn(file: *C, decl: *Decl) !void { |
| 160 | if (switch (inst.tag) { | 160 | if (switch (inst.tag) { |
| 161 | .assembly => try genAsm(&ctx, inst.castTag(.assembly).?), | 161 | .assembly => try genAsm(&ctx, inst.castTag(.assembly).?), |
| 162 | .call => try genCall(&ctx, inst.castTag(.call).?), | 162 | .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).?, "-"), | ||
| 163 | .ret => try genRet(&ctx, inst.castTag(.ret).?), | 165 | .ret => try genRet(&ctx, inst.castTag(.ret).?), |
| 164 | .retvoid => try genRetVoid(&ctx), | 166 | .retvoid => try genRetVoid(&ctx), |
| 165 | .arg => try genArg(&ctx), | 167 | .arg => try genArg(&ctx), |
| ... | @@ -207,6 +209,19 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { | ... | @@ -207,6 +209,19 @@ fn genIntCast(ctx: *Context, inst: *Inst.UnOp) !?[]u8 { |
| 207 | return name; | 209 | return name; |
| 208 | } | 210 | } |
| 209 | 211 | ||
| 212 | fn 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 | |||
| 210 | fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { | 225 | fn genCall(ctx: *Context, inst: *Inst.Call) !?[]u8 { |
| 211 | const writer = ctx.file.main.writer(); | 226 | const writer = ctx.file.main.writer(); |
| 212 | const header = ctx.file.header.writer(); | 227 | const header = ctx.file.header.writer(); |
test/stage2/cbe.zig+48| ... | @@ -147,4 +147,52 @@ pub fn addCases(ctx: *TestContext) !void { | ... | @@ -147,4 +147,52 @@ pub fn addCases(ctx: *TestContext) !void { |
| 147 | \\} | 147 | \\} |
| 148 | \\ | 148 | \\ |
| 149 | ); | 149 | ); |
| 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 | ); | ||
| 150 | } | 198 | } |