| author | |
| committer | |
| log | fcc82a219ab20eb4b5e26a6e46219e564ee87810 |
| tree | 1d987e5708f01343647996d5d60194aa7d77143b |
| parent | 55348c9b934ee87950e828e253b86e8b4b8622c2 |
| signature | Commit is signed but in an unrecognized format. |
4 files changed, 178 insertions(+), 5 deletions(-)
src-self-hosted/c_tokenizer.zig+81-5| ... | ... | @@ -17,6 +17,7 @@ pub const CToken = struct { |
| 17 | 17 | NumLitInt, |
| 18 | 18 | NumLitFloat, |
| 19 | 19 | Identifier, |
| 20 | Plus, | |
| 20 | 21 | Minus, |
| 21 | 22 | Slash, |
| 22 | 23 | LParen, |
| ... | ... | @@ -24,10 +25,17 @@ pub const CToken = struct { |
| 24 | 25 | Eof, |
| 25 | 26 | Dot, |
| 26 | 27 | Asterisk, |
| 28 | Ampersand, | |
| 29 | And, | |
| 30 | Or, | |
| 27 | 31 | Bang, |
| 28 | 32 | Tilde, |
| 29 | 33 | Shl, |
| 34 | Shr, | |
| 30 | 35 | Lt, |
| 36 | Gt, | |
| 37 | Increment, | |
| 38 | Decrement, | |
| 31 | 39 | Comma, |
| 32 | 40 | Fn, |
| 33 | 41 | Arrow, |
| ... | ... | @@ -226,6 +234,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 226 | 234 | var state: enum { |
| 227 | 235 | Start, |
| 228 | 236 | GotLt, |
| 237 | GotGt, | |
| 238 | GotPlus, | |
| 239 | GotMinus, | |
| 240 | GotAmpersand, | |
| 241 | GotPipe, | |
| 229 | 242 | CharLit, |
| 230 | 243 | OpenComment, |
| 231 | 244 | Comment, |
| ... | ... | @@ -246,7 +259,6 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 246 | 259 | NumLitIntSuffixL, |
| 247 | 260 | NumLitIntSuffixLL, |
| 248 | 261 | NumLitIntSuffixUL, |
| 249 | Minus, | |
| 250 | 262 | Done, |
| 251 | 263 | } = .Start; |
| 252 | 264 | |
| ... | ... | @@ -275,13 +287,17 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 275 | 287 | return result; |
| 276 | 288 | }, |
| 277 | 289 | .Start, |
| 278 | .Minus, | |
| 290 | .GotMinus, | |
| 279 | 291 | .Done, |
| 280 | 292 | .NumLitIntSuffixU, |
| 281 | 293 | .NumLitIntSuffixL, |
| 282 | 294 | .NumLitIntSuffixUL, |
| 283 | 295 | .NumLitIntSuffixLL, |
| 284 | 296 | .GotLt, |
| 297 | .GotGt, | |
| 298 | .GotPlus, | |
| 299 | .GotAmpersand, | |
| 300 | .GotPipe, | |
| 285 | 301 | => { |
| 286 | 302 | return result; |
| 287 | 303 | }, |
| ... | ... | @@ -345,6 +361,10 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 345 | 361 | result.id = .Lt; |
| 346 | 362 | state = .GotLt; |
| 347 | 363 | }, |
| 364 | '>' => { | |
| 365 | result.id = .Gt; | |
| 366 | state = .GotGt; | |
| 367 | }, | |
| 348 | 368 | '(' => { |
| 349 | 369 | result.id = .LParen; |
| 350 | 370 | state = .Done; |
| ... | ... | @@ -357,9 +377,13 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 357 | 377 | result.id = .Asterisk; |
| 358 | 378 | state = .Done; |
| 359 | 379 | }, |
| 380 | '+' => { | |
| 381 | result.id = .Plus; | |
| 382 | state = .GotPlus; | |
| 383 | }, | |
| 360 | 384 | '-' => { |
| 361 | state = .Minus; | |
| 362 | 385 | result.id = .Minus; |
| 386 | state = .GotMinus; | |
| 363 | 387 | }, |
| 364 | 388 | '!' => { |
| 365 | 389 | result.id = .Bang; |
| ... | ... | @@ -383,7 +407,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 383 | 407 | }, |
| 384 | 408 | '|' => { |
| 385 | 409 | result.id = .Pipe; |
| 386 | state = .Done; | |
| 410 | state = .GotPipe; | |
| 411 | }, | |
| 412 | '&' => { | |
| 413 | result.id = .Ampersand; | |
| 414 | state = .GotAmpersand; | |
| 387 | 415 | }, |
| 388 | 416 | '?' => { |
| 389 | 417 | result.id = .QuestionMark; |
| ... | ... | @@ -400,12 +428,27 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 400 | 428 | } |
| 401 | 429 | }, |
| 402 | 430 | .Done => return result, |
| 403 | .Minus => { | |
| 431 | .GotMinus => { | |
| 404 | 432 | switch (c) { |
| 405 | 433 | '>' => { |
| 406 | 434 | result.id = .Arrow; |
| 407 | 435 | state = .Done; |
| 408 | 436 | }, |
| 437 | '-' => { | |
| 438 | result.id = .Decrement; | |
| 439 | state = .Done; | |
| 440 | }, | |
| 441 | else => { | |
| 442 | return result; | |
| 443 | }, | |
| 444 | } | |
| 445 | }, | |
| 446 | .GotPlus => { | |
| 447 | switch (c) { | |
| 448 | '+' => { | |
| 449 | result.id = .Increment; | |
| 450 | state = .Done; | |
| 451 | }, | |
| 409 | 452 | else => { |
| 410 | 453 | return result; |
| 411 | 454 | }, |
| ... | ... | @@ -422,6 +465,39 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*: |
| 422 | 465 | }, |
| 423 | 466 | } |
| 424 | 467 | }, |
| 468 | .GotGt => { | |
| 469 | switch (c) { | |
| 470 | '>' => { | |
| 471 | result.id = .Shr; | |
| 472 | state = .Done; | |
| 473 | }, | |
| 474 | else => { | |
| 475 | return result; | |
| 476 | }, | |
| 477 | } | |
| 478 | }, | |
| 479 | .GotPipe => { | |
| 480 | switch (c) { | |
| 481 | '|' => { | |
| 482 | result.id = .Or; | |
| 483 | state = .Done; | |
| 484 | }, | |
| 485 | else => { | |
| 486 | return result; | |
| 487 | }, | |
| 488 | } | |
| 489 | }, | |
| 490 | .GotAmpersand => { | |
| 491 | switch (c) { | |
| 492 | '&' => { | |
| 493 | result.id = .And; | |
| 494 | state = .Done; | |
| 495 | }, | |
| 496 | else => { | |
| 497 | return result; | |
| 498 | }, | |
| 499 | } | |
| 500 | }, | |
| 425 | 501 | .Float => { |
| 426 | 502 | switch (c) { |
| 427 | 503 | '.', '0'...'9' => {}, |
src-self-hosted/clang.zig+4| ... | ... | @@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType(); |
| 43 | 43 | pub const struct_ZigClangIncompleteArrayType = @OpaqueType(); |
| 44 | 44 | pub const struct_ZigClangIntegerLiteral = @OpaqueType(); |
| 45 | 45 | pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType(); |
| 46 | pub const struct_ZigClangMacroExpansion = @OpaqueType(); | |
| 46 | 47 | pub const struct_ZigClangMacroQualifiedType = @OpaqueType(); |
| 47 | 48 | pub const struct_ZigClangMemberExpr = @OpaqueType(); |
| 48 | 49 | pub const struct_ZigClangNamedDecl = @OpaqueType(); |
| ... | ... | @@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr; |
| 889 | 890 | pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType; |
| 890 | 891 | pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral; |
| 891 | 892 | pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord; |
| 893 | pub const ZigClangMacroExpansion = struct_ZigClangMacroExpansion; | |
| 892 | 894 | pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType; |
| 893 | 895 | pub const ZigClangMemberExpr = struct_ZigClangMemberExpr; |
| 894 | 896 | pub const ZigClangNamedDecl = struct_ZigClangNamedDecl; |
| ... | ... | @@ -1058,6 +1060,8 @@ pub extern fn ZigClangMacroDefinitionRecord_getName_getNameStart(*const ZigClang |
| 1058 | 1060 | pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getBegin(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation; |
| 1059 | 1061 | pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getEnd(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation; |
| 1060 | 1062 | |
| 1063 | pub extern fn ZigClangMacroExpansion_getDefinition(*const ZigClangMacroExpansion) *const ZigClangMacroDefinitionRecord; | |
| 1064 | ||
| 1061 | 1065 | pub extern fn ZigClangIfStmt_getThen(*const ZigClangIfStmt) *const ZigClangStmt; |
| 1062 | 1066 | pub extern fn ZigClangIfStmt_getElse(*const ZigClangIfStmt) ?*const ZigClangStmt; |
| 1063 | 1067 | pub extern fn ZigClangIfStmt_getCond(*const ZigClangIfStmt) *const ZigClangStmt; |
src-self-hosted/translate_c.zig+72| ... | ... | @@ -4542,6 +4542,18 @@ fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: Zig |
| 4542 | 4542 | }; |
| 4543 | 4543 | node = &bitshift_node.base; |
| 4544 | 4544 | }, |
| 4545 | .Shr => { | |
| 4546 | const op_token = try appendToken(rp.c, .AngleBracketAngleBracketRight, ">>"); | |
| 4547 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4548 | const bitshift_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4549 | bitshift_node.* = .{ | |
| 4550 | .op_token = op_token, | |
| 4551 | .lhs = node, | |
| 4552 | .op = .BitShiftRight, | |
| 4553 | .rhs = rhs, | |
| 4554 | }; | |
| 4555 | node = &bitshift_node.base; | |
| 4556 | }, | |
| 4545 | 4557 | .Pipe => { |
| 4546 | 4558 | const op_token = try appendToken(c, .Pipe, "|"); |
| 4547 | 4559 | const rhs = try parseCExpr(c, it, source_loc, scope); |
| ... | ... | @@ -4554,6 +4566,66 @@ fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: Zig |
| 4554 | 4566 | }; |
| 4555 | 4567 | node = &or_node.base; |
| 4556 | 4568 | }, |
| 4569 | .Ampersand => { | |
| 4570 | const op_token = try appendToken(rp.c, .Ampersand, "&"); | |
| 4571 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4572 | const bitand_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4573 | bitand_node.* = .{ | |
| 4574 | .op_token = op_token, | |
| 4575 | .lhs = node, | |
| 4576 | .op = .BitAnd, | |
| 4577 | .rhs = rhs, | |
| 4578 | }; | |
| 4579 | node = &bitand_node.base; | |
| 4580 | }, | |
| 4581 | .Plus => { | |
| 4582 | const op_token = try appendToken(rp.c, .Plus, "+"); | |
| 4583 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4584 | const add_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4585 | add_node.* = .{ | |
| 4586 | .op_token = op_token, | |
| 4587 | .lhs = node, | |
| 4588 | .op = .Add, | |
| 4589 | .rhs = rhs, | |
| 4590 | }; | |
| 4591 | node = &add_node.base; | |
| 4592 | }, | |
| 4593 | .Minus => { | |
| 4594 | const op_token = try appendToken(rp.c, .Minus, "-"); | |
| 4595 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4596 | const sub_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4597 | sub_node.* = .{ | |
| 4598 | .op_token = op_token, | |
| 4599 | .lhs = node, | |
| 4600 | .op = .Sub, | |
| 4601 | .rhs = rhs, | |
| 4602 | }; | |
| 4603 | node = &sub_node.base; | |
| 4604 | }, | |
| 4605 | .And => { | |
| 4606 | const op_token = try appendToken(rp.c, .Keyword_and, "and"); | |
| 4607 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4608 | const and_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4609 | and_node.* = .{ | |
| 4610 | .op_token = op_token, | |
| 4611 | .lhs = node, | |
| 4612 | .op = .BoolAnd, | |
| 4613 | .rhs = rhs, | |
| 4614 | }; | |
| 4615 | node = &and_node.base; | |
| 4616 | }, | |
| 4617 | .Or => { | |
| 4618 | const op_token = try appendToken(rp.c, .Keyword_or, "or"); | |
| 4619 | const rhs = try parseCExpr(rp, it, source_loc, scope); | |
| 4620 | const or_node = try rp.c.a().create(ast.Node.InfixOp); | |
| 4621 | or_node.* = .{ | |
| 4622 | .op_token = op_token, | |
| 4623 | .lhs = node, | |
| 4624 | .op = .BoolOr, | |
| 4625 | .rhs = rhs, | |
| 4626 | }; | |
| 4627 | node = &or_node.base; | |
| 4628 | }, | |
| 4557 | 4629 | .LBrace => { |
| 4558 | 4630 | const arr_node = try transCreateNodeArrayAccess(c, node); |
| 4559 | 4631 | arr_node.op.ArrayAccess = try parseCExpr(c, it, source_loc, scope); |
test/translate_c.zig+21| ... | ... | @@ -195,6 +195,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 195 | 195 | \\pub const REDISMODULE_READ = 1 << 0; |
| 196 | 196 | }); |
| 197 | 197 | |
| 198 | cases.add("macro with right shift", | |
| 199 | \\#define FLASH_SIZE 0x200000UL /* 2 MB */ | |
| 200 | \\#define FLASH_BANK_SIZE (FLASH_SIZE >> 1) /* 1 MB */ | |
| 201 | , &[_][]const u8{ | |
| 202 | \\pub const FLASH_SIZE = @as(c_ulong, 0x200000); | |
| 203 | , | |
| 204 | \\pub const FLASH_BANK_SIZE = FLASH_SIZE >> 1; | |
| 205 | }); | |
| 206 | ||
| 198 | 207 | cases.add("double define struct", |
| 199 | 208 | \\typedef struct Bar Bar; |
| 200 | 209 | \\typedef struct Foo Foo; |
| ... | ... | @@ -1068,6 +1077,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void { |
| 1068 | 1077 | \\pub const FOO_CHAR = '\xff'; |
| 1069 | 1078 | }); |
| 1070 | 1079 | |
| 1080 | cases.add("macro add", | |
| 1081 | \\#define PERIPH_BASE (0x40000000UL) /*!< Base address of : AHB/APB Peripherals */ | |
| 1082 | \\#define D3_APB1PERIPH_BASE (PERIPH_BASE + 0x18000000UL) | |
| 1083 | \\#define RCC_BASE (D3_AHB1PERIPH_BASE + 0x4400UL) | |
| 1084 | , &[_][]const u8{ | |
| 1085 | \\pub const PERIPH_BASE = @as(c_ulong, 0x40000000); | |
| 1086 | , | |
| 1087 | \\pub const D3_APB1PERIPH_BASE = PERIPH_BASE + @as(c_ulong, 0x18000000); | |
| 1088 | , | |
| 1089 | \\pub const RCC_BASE = D3_AHB1PERIPH_BASE + @as(c_ulong, 0x4400); | |
| 1090 | }); | |
| 1091 | ||
| 1071 | 1092 | cases.add("variable aliasing", |
| 1072 | 1093 | \\static long a = 2; |
| 1073 | 1094 | \\static long b = 2; |