authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2019-12-27 09:37:32-05:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2019-12-29 19:50:45+02:00
logfcc82a219ab20eb4b5e26a6e46219e564ee87810
tree1d987e5708f01343647996d5d60194aa7d77143b
parent55348c9b934ee87950e828e253b86e8b4b8622c2
signature Commit is signed but in an unrecognized format.

Add macro ops


4 files changed, 178 insertions(+), 5 deletions(-)

src-self-hosted/c_tokenizer.zig+81-5
......@@ -17,6 +17,7 @@ pub const CToken = struct {
1717 NumLitInt,
1818 NumLitFloat,
1919 Identifier,
20 Plus,
2021 Minus,
2122 Slash,
2223 LParen,
......@@ -24,10 +25,17 @@ pub const CToken = struct {
2425 Eof,
2526 Dot,
2627 Asterisk,
28 Ampersand,
29 And,
30 Or,
2731 Bang,
2832 Tilde,
2933 Shl,
34 Shr,
3035 Lt,
36 Gt,
37 Increment,
38 Decrement,
3139 Comma,
3240 Fn,
3341 Arrow,
......@@ -226,6 +234,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
226234 var state: enum {
227235 Start,
228236 GotLt,
237 GotGt,
238 GotPlus,
239 GotMinus,
240 GotAmpersand,
241 GotPipe,
229242 CharLit,
230243 OpenComment,
231244 Comment,
......@@ -246,7 +259,6 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
246259 NumLitIntSuffixL,
247260 NumLitIntSuffixLL,
248261 NumLitIntSuffixUL,
249 Minus,
250262 Done,
251263 } = .Start;
252264
......@@ -275,13 +287,17 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
275287 return result;
276288 },
277289 .Start,
278 .Minus,
290 .GotMinus,
279291 .Done,
280292 .NumLitIntSuffixU,
281293 .NumLitIntSuffixL,
282294 .NumLitIntSuffixUL,
283295 .NumLitIntSuffixLL,
284296 .GotLt,
297 .GotGt,
298 .GotPlus,
299 .GotAmpersand,
300 .GotPipe,
285301 => {
286302 return result;
287303 },
......@@ -345,6 +361,10 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
345361 result.id = .Lt;
346362 state = .GotLt;
347363 },
364 '>' => {
365 result.id = .Gt;
366 state = .GotGt;
367 },
348368 '(' => {
349369 result.id = .LParen;
350370 state = .Done;
......@@ -357,9 +377,13 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
357377 result.id = .Asterisk;
358378 state = .Done;
359379 },
380 '+' => {
381 result.id = .Plus;
382 state = .GotPlus;
383 },
360384 '-' => {
361 state = .Minus;
362385 result.id = .Minus;
386 state = .GotMinus;
363387 },
364388 '!' => {
365389 result.id = .Bang;
......@@ -383,7 +407,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
383407 },
384408 '|' => {
385409 result.id = .Pipe;
386 state = .Done;
410 state = .GotPipe;
411 },
412 '&' => {
413 result.id = .Ampersand;
414 state = .GotAmpersand;
387415 },
388416 '?' => {
389417 result.id = .QuestionMark;
......@@ -400,12 +428,27 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
400428 }
401429 },
402430 .Done => return result,
403 .Minus => {
431 .GotMinus => {
404432 switch (c) {
405433 '>' => {
406434 result.id = .Arrow;
407435 state = .Done;
408436 },
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 },
409452 else => {
410453 return result;
411454 },
......@@ -422,6 +465,39 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
422465 },
423466 }
424467 },
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 },
425501 .Float => {
426502 switch (c) {
427503 '.', '0'...'9' => {},
src-self-hosted/clang.zig+4
......@@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType();
4343pub const struct_ZigClangIncompleteArrayType = @OpaqueType();
4444pub const struct_ZigClangIntegerLiteral = @OpaqueType();
4545pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType();
46pub const struct_ZigClangMacroExpansion = @OpaqueType();
4647pub const struct_ZigClangMacroQualifiedType = @OpaqueType();
4748pub const struct_ZigClangMemberExpr = @OpaqueType();
4849pub const struct_ZigClangNamedDecl = @OpaqueType();
......@@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr;
889890pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType;
890891pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral;
891892pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord;
893pub const ZigClangMacroExpansion = struct_ZigClangMacroExpansion;
892894pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType;
893895pub const ZigClangMemberExpr = struct_ZigClangMemberExpr;
894896pub const ZigClangNamedDecl = struct_ZigClangNamedDecl;
......@@ -1058,6 +1060,8 @@ pub extern fn ZigClangMacroDefinitionRecord_getName_getNameStart(*const ZigClang
10581060pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getBegin(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;
10591061pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getEnd(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;
10601062
1063pub extern fn ZigClangMacroExpansion_getDefinition(*const ZigClangMacroExpansion) *const ZigClangMacroDefinitionRecord;
1064
10611065pub extern fn ZigClangIfStmt_getThen(*const ZigClangIfStmt) *const ZigClangStmt;
10621066pub extern fn ZigClangIfStmt_getElse(*const ZigClangIfStmt) ?*const ZigClangStmt;
10631067pub 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
45424542 };
45434543 node = &bitshift_node.base;
45444544 },
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 },
45454557 .Pipe => {
45464558 const op_token = try appendToken(c, .Pipe, "|");
45474559 const rhs = try parseCExpr(c, it, source_loc, scope);
......@@ -4554,6 +4566,66 @@ fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: Zig
45544566 };
45554567 node = &or_node.base;
45564568 },
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 },
45574629 .LBrace => {
45584630 const arr_node = try transCreateNodeArrayAccess(c, node);
45594631 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 {
195195 \\pub const REDISMODULE_READ = 1 << 0;
196196 });
197197
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
198207 cases.add("double define struct",
199208 \\typedef struct Bar Bar;
200209 \\typedef struct Foo Foo;
......@@ -1068,6 +1077,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
10681077 \\pub const FOO_CHAR = '\xff';
10691078 });
10701079
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
10711092 cases.add("variable aliasing",
10721093 \\static long a = 2;
10731094 \\static long b = 2;