Commit 813d3308cc

Andrew Kelley <andrew@ziglang.org>
2020-12-29 04:27:58
stage2: update C backend test cases for new output
1 parent bbe6657
Changed files (2)
src
codegen
test
stage2
src/codegen/c.zig
@@ -198,6 +198,13 @@ fn renderFunctionSignature(
     try writer.writeByte(')');
 }
 
+fn indent(file: *C) !void {
+    const indent_size = 4;
+    const indent_level = 1;
+    const indent_amt = indent_size * indent_level;
+    try file.main.writer().writeByteNTimes(' ', indent_amt);
+}
+
 pub fn generate(file: *C, decl: *Decl) !void {
     const tv = decl.typed_value.most_recent.typed_value;
 
@@ -228,11 +235,6 @@ pub fn generate(file: *C, decl: *Decl) !void {
         if (instructions.len > 0) {
             try writer.writeAll("\n");
             for (instructions) |inst| {
-                if (inst.tag != .dbg_stmt) {
-                    const indent_size = 4;
-                    const indent_level = 1;
-                    try writer.writeByteNTimes(' ', indent_size * indent_level);
-                }
                 if (switch (inst.tag) {
                     .assembly => try genAsm(&ctx, file, inst.castTag(.assembly).?),
                     .call => try genCall(&ctx, file, inst.castTag(.call).?),
@@ -361,11 +363,13 @@ fn genArg(ctx: *Context) !?[]u8 {
 }
 
 fn genRetVoid(file: *C) !?[]u8 {
+    try indent(file);
     try file.main.writer().print("return;\n", .{});
     return null;
 }
 
 fn genRet(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
+    try indent(file);
     const writer = file.main.writer();
     try writer.writeAll("return ");
     try genValue(ctx, writer, inst.operand);
@@ -384,6 +388,7 @@ fn genValue(ctx: *Context, writer: Writer, inst: *Inst) !void {
 fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
     if (inst.base.isUnused())
         return null;
+    try indent(file);
     const op = inst.operand;
     const writer = file.main.writer();
     const name = try ctx.name();
@@ -399,6 +404,7 @@ fn genIntCast(ctx: *Context, file: *C, inst: *Inst.UnOp) !?[]u8 {
 fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []const u8) !?[]u8 {
     if (inst.base.isUnused())
         return null;
+    try indent(file);
     const lhs = ctx.resolveInst(inst.lhs);
     const rhs = ctx.resolveInst(inst.rhs);
     const writer = file.main.writer();
@@ -410,6 +416,7 @@ fn genBinOp(ctx: *Context, file: *C, inst: *Inst.BinOp, comptime operator: []con
 }
 
 fn genCall(ctx: *Context, file: *C, inst: *Inst.Call) !?[]u8 {
+    try indent(file);
     const writer = file.main.writer();
     const header = file.header.buf.writer();
     if (inst.func.castTag(.constant)) |func_inst| {
@@ -458,16 +465,19 @@ fn genDbgStmt(ctx: *Context, inst: *Inst.NoOp) !?[]u8 {
 }
 
 fn genBreakpoint(file: *C, inst: *Inst.NoOp) !?[]u8 {
+    try indent(file);
     try file.main.writer().writeAll("zig_breakpoint();\n");
     return null;
 }
 
 fn genUnreach(file: *C, inst: *Inst.NoOp) !?[]u8 {
+    try indent(file);
     try file.main.writer().writeAll("zig_unreachable();\n");
     return null;
 }
 
 fn genAsm(ctx: *Context, file: *C, as: *Inst.Assembly) !?[]u8 {
+    try indent(file);
     const writer = file.main.writer();
     for (as.inputs) |i, index| {
         if (i[0] == '{' and i[i.len - 1] == '}') {
test/stage2/cbe.zig
@@ -23,6 +23,7 @@ pub fn addCases(ctx: *TestContext) !void {
 
         // Now change the message only
         // TODO fix C backend not supporting updates
+        // https://github.com/ziglang/zig/issues/7589
         //case.addCompareOutput(
         //    \\extern fn puts(s: [*:0]const u8) c_int;
         //    \\export fn main() c_int {
@@ -88,9 +89,9 @@ pub fn addCases(ctx: *TestContext) !void {
     ,
         \\zig_noreturn void exitGood(void);
         \\
-        \\const char *const exitGood__anon_0 = "{rax}";
-        \\const char *const exitGood__anon_1 = "{rdi}";
-        \\const char *const exitGood__anon_2 = "syscall";
+        \\static uint8_t exitGood__anon_0[6] = "{rax}";
+        \\static uint8_t exitGood__anon_1[6] = "{rdi}";
+        \\static uint8_t exitGood__anon_2[8] = "syscall";
         \\
         \\zig_noreturn void _start(void) {
         \\    exitGood();
@@ -122,9 +123,9 @@ pub fn addCases(ctx: *TestContext) !void {
     ,
         \\zig_noreturn void exit(uintptr_t arg0);
         \\
-        \\const char *const exit__anon_0 = "{rax}";
-        \\const char *const exit__anon_1 = "{rdi}";
-        \\const char *const exit__anon_2 = "syscall";
+        \\static uint8_t exit__anon_0[6] = "{rax}";
+        \\static uint8_t exit__anon_1[6] = "{rdi}";
+        \\static uint8_t exit__anon_2[8] = "syscall";
         \\
         \\zig_noreturn void _start(void) {
         \\    exit(0);
@@ -154,13 +155,11 @@ pub fn addCases(ctx: *TestContext) !void {
         \\}
         \\
     ,
-        \\#include <stdint.h>
-        \\
         \\zig_noreturn void exit(uint8_t arg0);
         \\
-        \\const char *const exit__anon_0 = "{rax}";
-        \\const char *const exit__anon_1 = "{rdi}";
-        \\const char *const exit__anon_2 = "syscall";
+        \\static uint8_t exit__anon_0[6] = "{rax}";
+        \\static uint8_t exit__anon_1[6] = "{rdi}";
+        \\static uint8_t exit__anon_2[8] = "syscall";
         \\
         \\zig_noreturn void _start(void) {
         \\    exit(0);
@@ -195,14 +194,12 @@ pub fn addCases(ctx: *TestContext) !void {
         \\}
         \\
     ,
-        \\#include <stdint.h>
-        \\
         \\zig_noreturn void exitMath(uint8_t arg0);
         \\zig_noreturn void exit(uint8_t arg0);
         \\
-        \\const char *const exit__anon_0 = "{rax}";
-        \\const char *const exit__anon_1 = "{rdi}";
-        \\const char *const exit__anon_2 = "syscall";
+        \\static uint8_t exit__anon_0[6] = "{rax}";
+        \\static uint8_t exit__anon_1[6] = "{rdi}";
+        \\static uint8_t exit__anon_2[8] = "syscall";
         \\
         \\zig_noreturn void _start(void) {
         \\    exitMath(1);
@@ -243,14 +240,12 @@ pub fn addCases(ctx: *TestContext) !void {
         \\}
         \\
     ,
-        \\#include <stdint.h>
-        \\
         \\zig_noreturn void exitMath(uint8_t arg0);
         \\zig_noreturn void exit(uint8_t arg0);
         \\
-        \\const char *const exit__anon_0 = "{rax}";
-        \\const char *const exit__anon_1 = "{rdi}";
-        \\const char *const exit__anon_2 = "syscall";
+        \\static uint8_t exit__anon_0[6] = "{rax}";
+        \\static uint8_t exit__anon_1[6] = "{rdi}";
+        \\static uint8_t exit__anon_2[8] = "syscall";
         \\
         \\zig_noreturn void _start(void) {
         \\    exitMath(1);
@@ -275,24 +270,18 @@ pub fn addCases(ctx: *TestContext) !void {
     ctx.h("header with single param function", linux_x64,
         \\export fn start(a: u8) void{}
     ,
-        \\#include <stdint.h>
-        \\
         \\void start(uint8_t arg0);
         \\
     );
     ctx.h("header with multiple param function", linux_x64,
         \\export fn start(a: u8, b: u8, c: u8) void{}
     ,
-        \\#include <stdint.h>
-        \\
         \\void start(uint8_t arg0, uint8_t arg1, uint8_t arg2);
         \\
     );
     ctx.h("header with u32 param function", linux_x64,
         \\export fn start(a: u32) void{}
     ,
-        \\#include <stdint.h>
-        \\
         \\void start(uint32_t arg0);
         \\
     );
@@ -329,8 +318,6 @@ pub fn addCases(ctx: *TestContext) !void {
     ctx.h("header with multiple includes", linux_x64,
         \\export fn start(a: u32, b: usize) void{}
     ,
-        \\#include <stdint.h>
-        \\
         \\void start(uint32_t arg0, uintptr_t arg1);
         \\
     );