Skip to content

Commit 9817258

Browse files
committed
C-generator: use c2 types in C code generated
* no longer use `int32_t` etc. in generated C code to avoid naming conflicts. Use `i8`, u8`, `i16`, `u16`, `int`, `u32`, `i64`, `u64`, `ssize_t`, `size_t`, `float` and `double` instead. * use explicit `signed int` for bitfields to avoid implementation defined behavior on `int` bitfields
1 parent f9fddf6 commit 9817258

File tree

103 files changed

+359
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+359
-360
lines changed

generator/c/c_generator.c2

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -239,22 +239,22 @@ fn void Generator.emitEnumType(Generator* gen, string_buffer.Buf* out, Decl* d)
239239

240240
const char*[BuiltinKind] builtinType_cnames = {
241241
"char",
242-
"int8_t",
243-
"int16_t",
244-
"int32_t",
245-
"int64_t",
246-
"uint8_t",
247-
"uint16_t",
248-
"uint32_t",
249-
"uint64_t",
242+
"i8",
243+
"i16",
244+
"int",
245+
"i64",
246+
"u8",
247+
"u16",
248+
"u32",
249+
"u64",
250250
"float",
251251
"double",
252252
"ssize_t",
253253
"size_t",
254254
"bool",
255255
}
256256

257-
fn void Generator.emitTypePre(Generator* gen, string_buffer.Buf* out, QualType qt) {
257+
fn void Generator.emitTypePre(Generator* gen, string_buffer.Buf* out, QualType qt, bool bitfield = false) {
258258
Decl* decl = nil;
259259

260260
if (qt.isConst()) out.add("const ");
@@ -263,6 +263,8 @@ fn void Generator.emitTypePre(Generator* gen, string_buffer.Buf* out, QualType q
263263
switch (qt.getKind()) {
264264
case Builtin:
265265
BuiltinType* bt = (BuiltinType*)qt.getType();
266+
// special case to ensure signed behavior
267+
if (bitfield && bt.getKind() == Int32) out.add("signed ");
266268
out.add(builtinType_cnames[bt.getKind()]);
267269
return;
268270
case Pointer:
@@ -385,13 +387,14 @@ fn void Generator.emitStructMember(Generator* gen, string_buffer.Buf* out, Decl*
385387
}
386388
gen.genTypeIfNeeded(qt, true);
387389

388-
gen.emitTypePre(out, qt);
390+
VarDecl* vd = (VarDecl*)d;
391+
Expr* bitfield = vd.getBitfield();
392+
393+
gen.emitTypePre(out, qt, bitfield != nil);
389394
out.space();
390395
if (d.getNameIdx()) out.add(d.getName());
391396
gen.emitTypePost(out, qt);
392397

393-
VarDecl* vd = (VarDecl*)d;
394-
Expr* bitfield = vd.getBitfield();
395398
if (bitfield) {
396399
out.add(" : ");
397400
// C does not allow using constants here, so generate CTV Value
@@ -861,7 +864,7 @@ fn void Generator.gen_func_proto(Generator* gen, FunctionDecl* fd, string_buffer
861864
}
862865

863866
if (d == gen.mainFunc) {
864-
out.add("int32_t main");
867+
out.add("int main");
865868
} else {
866869
// 2 options:
867870
// - external .c2i files
@@ -1435,12 +1438,11 @@ const char[] C_types =
14351438
#define true 1
14361439
#define false 0
14371440
#endif
1438-
typedef signed char int8_t;
1439-
typedef unsigned char uint8_t;
1440-
typedef signed short int16_t;
1441-
typedef unsigned short uint16_t;
1442-
typedef signed int int32_t;
1443-
typedef unsigned int uint32_t;
1441+
typedef signed char i8;
1442+
typedef unsigned char u8;
1443+
typedef signed short i16;
1444+
typedef unsigned short u16;
1445+
typedef unsigned int u32;
14441446
```;
14451447
const char[] C_defines =
14461448
```c
@@ -1493,24 +1495,24 @@ fn void Generator.emit_external_header(Generator* gen, bool enable_asserts, cons
14931495
if (ast.getWordSize() == 4) {
14941496
// ILP32 (32-bit int, long and pointers)
14951497
out.add(```c
1496-
typedef signed long long int64_t;
1497-
typedef unsigned long long uint64_t;
1498+
typedef signed long long i64;
1499+
typedef unsigned long long u64;
14981500
typedef signed long ssize_t;
14991501
typedef unsigned long size_t;
15001502
```);
15011503
} else {
15021504
// LP64 (64-bit long and pointers)
15031505
out.add(```c
1504-
typedef signed long int64_t;
1505-
typedef unsigned long uint64_t;
1506+
typedef signed long i64;
1507+
typedef unsigned long u64;
15061508
typedef signed long ssize_t;
15071509
typedef unsigned long size_t;
15081510
```);
15091511
#if 0
15101512
// TODO support LLP64 (64-bit long long and pointers, but 32-bit long)
15111513
out.add(```c
1512-
typedef signed long long int64_t;
1513-
typedef unsigned long long uint64_t;
1514+
typedef signed long long i64;
1515+
typedef unsigned long long u64;
15141516
typedef signed long long ssize_t;
15151517
typedef unsigned long long size_t;
15161518
```);

generator/c/c_generator_special.c2

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,14 @@ const char[] C2_types_header =
290290
#define to_container(type, member, ptr) ((type *)((char *)(ptr)-offsetof(type, member)))
291291

292292
// NOTE: 64-bit only for now
293-
typedef signed char int8_t;
294-
typedef unsigned char uint8_t;
295-
typedef signed short int int16_t;
296-
typedef unsigned short int uint16_t;
297-
typedef signed int int32_t;
298-
typedef unsigned int uint32_t;
299-
typedef signed long int64_t;
300-
typedef unsigned long uint64_t;
293+
typedef signed char i8;
294+
typedef unsigned char u8;
295+
typedef signed short int i16;
296+
typedef unsigned short int u16;
297+
typedef signed int i32;
298+
typedef unsigned int u32;
299+
typedef signed long i64;
300+
typedef unsigned long u64;
301301

302302
#ifdef __cplusplus
303303
}

generator/c/c_generator_stmt.c2

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ fn void Generator.emitCase(Generator* gen, SwitchCase* c, u32 indent, u32 *lab)
471471
}
472472

473473
const u32 num_stmts = c.getNumStmts();
474-
if (num_stmts == 0) {
475-
//out.indent(indent+1);
476-
//out.add("fallthrough;\n");
477-
} else {
474+
if (num_stmts > 0) {
478475
Stmt** stmts = c.getStmts();
479476
for (u32 i=0; i<num_stmts; i++) {
480477
gen.emitStmt(stmts[i], indent+1, true);

generator/c/c_generator_trace.c2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,10 @@ fn void Generator.writeCalls(Generator* gen, string_buffer.Buf* out) {
193193
}
194194
out.add("};\n\n");
195195

196-
out.add("uint32_t c2_trace_length = sizeof(c2_trace_data) / sizeof(c2_trace_data[0]);\n"
197-
"uint32_t c2_trace_counts[sizeof(c2_trace_data) / sizeof(c2_trace_data[0])];\n\n");
196+
out.add("u32 c2_trace_length = sizeof(c2_trace_data) / sizeof(c2_trace_data[0]);\n"
197+
"u32 c2_trace_counts[sizeof(c2_trace_data) / sizeof(c2_trace_data[0])];\n\n");
198198
}
199199

200200
fn void Generator.writeCallExterns(Generator* gen, string_buffer.Buf* out) {
201-
out.add("extern uint32_t c2_trace_counts[];\n");
201+
out.add("extern u32 c2_trace_counts[];\n");
202202
}

libs/io_uring/io_uring.c2i

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ static inline bool io_uring_cq_eventfd_enabled(const struct io_uring *ring)
694694
static inline int io_uring_cq_eventfd_toggle(struct io_uring *ring,
695695
bool enabled)
696696
{
697-
uint32_t flags;
697+
u32 flags;
698698

699699
if (!!enabled == io_uring_cq_eventfd_enabled(ring))
700700
return 0;

test/auto_args/auto_arg_callback_ok.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public fn i32 main() {
2727

2828
// @expect{atleast, cgen/build.c}
2929

30-
typedef void (*test_Func)(const char* file, uint32_t line);
30+
typedef void (*test_Func)(const char* file, u32 line);
3131

32-
static void test_impl(const char* file, uint32_t line);
32+
static void test_impl(const char* file, u32 line);
3333
static void test_test1(test_Func f);
3434
static void test_test2(void);
3535

36-
static void test_impl(const char* file, uint32_t line)
36+
static void test_impl(const char* file, u32 line)
3737
{
3838
}
3939

test/auto_args/auto_file_ok.c2t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public fn i32 main() {
1818

1919
// @expect{atleast, cgen/build.c}
2020

21-
static void test_test2(const char* file, uint32_t line);
21+
static void test_test2(const char* file, u32 line);
2222

23-
static void test_test2(const char* file, uint32_t line)
23+
static void test_test2(const char* file, u32 line)
2424
{
2525
}
2626

27-
int32_t main(void)
27+
int main(void)
2828
{
2929
test_test2("file1.c2", 7);
3030
test_test2("file1.c2", 8);

test/auto_args/multi_file.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public fn i32 main() {
2525

2626
// @expect{atleast, cgen/test.c}
2727

28-
int32_t main(void)
28+
int main(void)
2929
{
3030
foo_Foo* f = NULL;
3131
foo_Foo_test(f, "file2.c2", 7, NULL);

test/auto_args/static_lib.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ fn void Foo.test(Foo* f, const char* file @(auto_file), u32 line @(auto_line), v
1919

2020
// @expect{atleast, foo.h}
2121

22-
void foo_Foo_test(foo_Foo* f, const char* file, uint32_t line, void* p);
22+
void foo_Foo_test(foo_Foo* f, const char* file, u32 line, void* p);
2323

2424

test/auto_args/struct_function_before_self.c2t

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ public fn i32 main() {
1919
}
2020

2121
// @expect{atleast, cgen/build.c}
22-
static void test_Foo_test(const char* file, uint32_t line, test_Foo* f, void* p);
23-
int32_t main(void);
24-
static void test_Foo_test(const char* file, uint32_t line, test_Foo* f, void* p)
22+
static void test_Foo_test(const char* file, u32 line, test_Foo* f, void* p);
23+
int main(void);
24+
static void test_Foo_test(const char* file, u32 line, test_Foo* f, void* p)
2525
{
2626
}
2727

28-
int32_t main(void)
28+
int main(void)
2929
{
3030
test_Foo f;
3131
test_Foo_test("file1.c2", 11, &f, NULL);

0 commit comments

Comments
 (0)