authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-17 11:32:11+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-03-18 22:46:00+02:00
logac7217e1f5ff15a6fc8248b9c1c651b318f472ad
treecfff232bb0186eef06ebed715635d9a215445034
parent7c6eb4161912f1c66acbdd341339e6cca8c410d8

translate-c: preserve zero fractional part in float literals


3 files changed, 21 insertions(+), 10 deletions(-)

src/translate_c.zig+4-4
...@@ -3248,11 +3248,11 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, stmt: *const clang.FloatingL...@@ -3248,11 +3248,11 @@ fn transFloatingLiteral(c: *Context, scope: *Scope, stmt: *const clang.FloatingL
3248 var dbl = stmt.getValueAsApproximateDouble();3248 var dbl = stmt.getValueAsApproximateDouble();
3249 const is_negative = dbl < 0;3249 const is_negative = dbl < 0;
3250 if (is_negative) dbl = -dbl;3250 if (is_negative) dbl = -dbl;
3251 const str = try std.fmt.allocPrint(c.arena, "{d}", .{dbl});3251 const str = if (dbl == std.math.floor(dbl))
3252 var node = if (dbl == std.math.floor(dbl))3252 try std.fmt.allocPrint(c.arena, "{d}.0", .{dbl})
3253 try Tag.integer_literal.create(c.arena, str)
3254 else3253 else
3255 try Tag.float_literal.create(c.arena, str);3254 try std.fmt.allocPrint(c.arena, "{d}", .{dbl});
3255 var node = try Tag.float_literal.create(c.arena, str);
3256 if (is_negative) node = try Tag.negate.create(c.arena, node);3256 if (is_negative) node = try Tag.negate.create(c.arena, node);
3257 return maybeSuppressResult(c, scope, used, node);3257 return maybeSuppressResult(c, scope, used, node);
3258}3258}
test/run_translated_c.zig+11
...@@ -3,6 +3,17 @@ const tests = @import("tests.zig");...@@ -3,6 +3,17 @@ const tests = @import("tests.zig");
3const nl = std.cstr.line_sep;3const nl = std.cstr.line_sep;
44
5pub fn addCases(cases: *tests.RunTranslatedCContext) void {5pub fn addCases(cases: *tests.RunTranslatedCContext) void {
6 cases.add("division of floating literals",
7 \\#define _NO_CRT_STDIO_INLINE 1
8 \\#include <stdio.h>
9 \\#define PI 3.14159265358979323846f
10 \\#define DEG2RAD (PI/180.0f)
11 \\int main(void) {
12 \\ printf("DEG2RAD is: %f\n", DEG2RAD);
13 \\ return 0;
14 \\}
15 , "DEG2RAD is: 0.017453" ++ nl);
16
6 cases.add("use global scope for record/enum/typedef type transalation if needed",17 cases.add("use global scope for record/enum/typedef type transalation if needed",
7 \\void bar(void);18 \\void bar(void);
8 \\void baz(void);19 \\void baz(void);
test/translate_c.zig+6-6
...@@ -2428,7 +2428,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2428,7 +2428,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2428 \\ b: c_int,2428 \\ b: c_int,
2429 \\};2429 \\};
2430 \\pub extern var a: struct_Foo;2430 \\pub extern var a: struct_Foo;
2431 \\pub export var b: f32 = 2;2431 \\pub export var b: f32 = 2.0;
2432 \\pub export fn foo() void {2432 \\pub export fn foo() void {
2433 \\ var c: [*c]struct_Foo = undefined;2433 \\ var c: [*c]struct_Foo = undefined;
2434 \\ _ = a.b;2434 \\ _ = a.b;
...@@ -2992,17 +2992,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2992,17 +2992,17 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2992 \\pub extern fn fn_bool(x: bool) void;2992 \\pub extern fn fn_bool(x: bool) void;
2993 \\pub extern fn fn_ptr(x: ?*c_void) void;2993 \\pub extern fn fn_ptr(x: ?*c_void) void;
2994 \\pub export fn call() void {2994 \\pub export fn call() void {
2995 \\ fn_int(@floatToInt(c_int, 3));2995 \\ fn_int(@floatToInt(c_int, 3.0));
2996 \\ fn_int(@floatToInt(c_int, 3));2996 \\ fn_int(@floatToInt(c_int, 3.0));
2997 \\ fn_int(@floatToInt(c_int, 3));2997 \\ fn_int(@floatToInt(c_int, 3.0));
2998 \\ fn_int(@as(c_int, 1094861636));2998 \\ fn_int(@as(c_int, 1094861636));
2999 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));2999 \\ fn_f32(@intToFloat(f32, @as(c_int, 3)));
3000 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));3000 \\ fn_f64(@intToFloat(f64, @as(c_int, 3)));
3001 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));3001 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '3'))));
3002 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));3002 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, '\x01'))));
3003 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));3003 \\ fn_char(@bitCast(u8, @truncate(i8, @as(c_int, 0))));
3004 \\ fn_f32(3);3004 \\ fn_f32(3.0);
3005 \\ fn_f64(3);3005 \\ fn_f64(3.0);
3006 \\ fn_bool(@as(c_int, 123) != 0);3006 \\ fn_bool(@as(c_int, 123) != 0);
3007 \\ fn_bool(@as(c_int, 0) != 0);3007 \\ fn_bool(@as(c_int, 0) != 0);
3008 \\ fn_bool(@ptrToInt(fn_int) != 0);3008 \\ fn_bool(@ptrToInt(fn_int) != 0);