authorgravatar for kris.tate+github@gmail.comkristopher tate <kris.tate+github@gmail.com> 2018-08-19 00:22:46+09:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-20 22:46:11-04:00
logb023db2e822baa5b54e15e0dbc1c5e35c8931cb6
tree7e6b8713a6e3db7b2e979244df114f600c9ccc37
parent7e7e59d8811b9fd0ba7dde345f61597a49a3bd13

src/translate_c.cpp: correctly bridge llvm::APSInt with Zig BigInt;

ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData; Internally to Zig we store an integer's sign outside of getRawData! (~aps_int) calls .flip() internally on the raw data to match Zig. test/translate_c.zig: enum: add wider range of values (u64) to try;

2 files changed, 55 insertions(+), 29 deletions(-)

src/translate_c.cpp+8-10
......@@ -458,16 +458,14 @@ static const char *decl_name(const Decl *decl) {
458458static AstNode *trans_create_node_apint(Context *c, const llvm::APSInt &aps_int) {
459459 AstNode *node = trans_create_node(c, NodeTypeIntLiteral);
460460 node->data.int_literal.bigint = allocate<BigInt>(1);
461
462 llvm::APSInt copy = aps_int;
463 llvm::APSInt positive = (~copy)++;
464
465 if (!aps_int.isNegative()) {
466 bigint_init_data(node->data.int_literal.bigint, aps_int.getRawData(), aps_int.getNumWords(), aps_int.isNegative());
467 } else {
468 bigint_init_data(node->data.int_literal.bigint, positive.getRawData(), positive.getNumWords(), aps_int.isNegative());
469 }
470
461 bool is_negative = aps_int.isNegative();
462 // ACHTUNG: llvm::APSInt stores an int's sign inside of its getRawData;
463 // Internally to Zig we store an integer's sign outside of getRawData!
464 // ++(~aps_int) calls .flip() internally on the raw data to match Zig.
465 bigint_init_data( node->data.int_literal.bigint
466 , (is_negative ? (++(~aps_int)) : aps_int).getRawData()
467 , aps_int.getNumWords()
468 , is_negative );
471469 return node;
472470}
473471
test/translate_c.zig+47-19
......@@ -1361,27 +1361,55 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13611361
13621362 cases.add("correctly translate enum init values",
13631363 \\enum EnumWithInits {
1364 \\ VAL1 = 0,
1365 \\ VAL2 = 1,
1366 \\ VAL3 = 2,
1367 \\ VAL4 = 3,
1368 \\ VAL5 = -1,
1369 \\ VAL6 = -2,
1370 \\ VAL7 = -3,
1371 \\ VAL8 = -4,
1372 \\ VAL9 = VAL2 + VAL8,
1364 \\ VAL01 = 0,
1365 \\ VAL02 = 1,
1366 \\ VAL03 = 2,
1367 \\ VAL04 = 3,
1368 \\ VAL05 = -1,
1369 \\ VAL06 = -2,
1370 \\ VAL07 = -3,
1371 \\ VAL08 = -4,
1372 \\ VAL09 = VAL02 + VAL08,
1373 \\ VAL10 = -1000012000,
1374 \\ VAL11 = -1000161000,
1375 \\ VAL12 = -1000174001,
1376 \\ VAL13 = VAL09,
1377 \\ VAL14 = VAL10,
1378 \\ VAL15 = VAL11,
1379 \\ VAL16 = VAL13,
1380 \\ VAL17 = (VAL16 - VAL10 + 1),
1381 \\ VAL18 = 0x1000000000000000L,
1382 \\ VAL19 = VAL18 + VAL18 + VAL18 - 1,
1383 \\ VAL20 = VAL19 + VAL19,
1384 \\ VAL21 = VAL20 + 0xFFFFFFFFFFFFFFFF,
1385 \\ VAL22 = 0xFFFFFFFFFFFFFFFF + 1,
1386 \\ VAL23 = 0xFFFFFFFFFFFFFFFF,
13731387 \\};
13741388 ,
1375 \\pub const enum_EnumWithInits = extern enum {
1376 \\ VAL1 = 0,
1377 \\ VAL2 = 1,
1378 \\ VAL3 = 2,
1379 \\ VAL4 = 3,
1380 \\ VAL5 = -1,
1381 \\ VAL6 = -2,
1382 \\ VAL7 = -3,
1383 \\ VAL8 = -4,
1384 \\ VAL9 = -3,
1389 \\pub const enum_EnumWithInits = extern enum(c_longlong) {
1390 \\ VAL01 = 0,
1391 \\ VAL02 = 1,
1392 \\ VAL03 = 2,
1393 \\ VAL04 = 3,
1394 \\ VAL05 = -1,
1395 \\ VAL06 = -2,
1396 \\ VAL07 = -3,
1397 \\ VAL08 = -4,
1398 \\ VAL09 = -3,
1399 \\ VAL10 = -1000012000,
1400 \\ VAL11 = -1000161000,
1401 \\ VAL12 = -1000174001,
1402 \\ VAL13 = -3,
1403 \\ VAL14 = -1000012000,
1404 \\ VAL15 = -1000161000,
1405 \\ VAL16 = -3,
1406 \\ VAL17 = 1000011998,
1407 \\ VAL18 = 1152921504606846976,
1408 \\ VAL19 = 3458764513820540927,
1409 \\ VAL20 = 6917529027641081854,
1410 \\ VAL21 = 6917529027641081853,
1411 \\ VAL22 = 0,
1412 \\ VAL23 = -1,
13851413 \\};
13861414 );
13871415}