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 {...@@ -17,6 +17,7 @@ pub const CToken = struct {
17 NumLitInt,17 NumLitInt,
18 NumLitFloat,18 NumLitFloat,
19 Identifier,19 Identifier,
20 Plus,
20 Minus,21 Minus,
21 Slash,22 Slash,
22 LParen,23 LParen,
...@@ -24,10 +25,17 @@ pub const CToken = struct {...@@ -24,10 +25,17 @@ pub const CToken = struct {
24 Eof,25 Eof,
25 Dot,26 Dot,
26 Asterisk,27 Asterisk,
28 Ampersand,
29 And,
30 Or,
27 Bang,31 Bang,
28 Tilde,32 Tilde,
29 Shl,33 Shl,
34 Shr,
30 Lt,35 Lt,
36 Gt,
37 Increment,
38 Decrement,
31 Comma,39 Comma,
32 Fn,40 Fn,
33 Arrow,41 Arrow,
...@@ -226,6 +234,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -226,6 +234,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
226 var state: enum {234 var state: enum {
227 Start,235 Start,
228 GotLt,236 GotLt,
237 GotGt,
238 GotPlus,
239 GotMinus,
240 GotAmpersand,
241 GotPipe,
229 CharLit,242 CharLit,
230 OpenComment,243 OpenComment,
231 Comment,244 Comment,
...@@ -246,7 +259,6 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -246,7 +259,6 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
246 NumLitIntSuffixL,259 NumLitIntSuffixL,
247 NumLitIntSuffixLL,260 NumLitIntSuffixLL,
248 NumLitIntSuffixUL,261 NumLitIntSuffixUL,
249 Minus,
250 Done,262 Done,
251 } = .Start;263 } = .Start;
252264
...@@ -275,13 +287,17 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -275,13 +287,17 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
275 return result;287 return result;
276 },288 },
277 .Start,289 .Start,
278 .Minus,290 .GotMinus,
279 .Done,291 .Done,
280 .NumLitIntSuffixU,292 .NumLitIntSuffixU,
281 .NumLitIntSuffixL,293 .NumLitIntSuffixL,
282 .NumLitIntSuffixUL,294 .NumLitIntSuffixUL,
283 .NumLitIntSuffixLL,295 .NumLitIntSuffixLL,
284 .GotLt,296 .GotLt,
297 .GotGt,
298 .GotPlus,
299 .GotAmpersand,
300 .GotPipe,
285 => {301 => {
286 return result;302 return result;
287 },303 },
...@@ -345,6 +361,10 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -345,6 +361,10 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
345 result.id = .Lt;361 result.id = .Lt;
346 state = .GotLt;362 state = .GotLt;
347 },363 },
364 '>' => {
365 result.id = .Gt;
366 state = .GotGt;
367 },
348 '(' => {368 '(' => {
349 result.id = .LParen;369 result.id = .LParen;
350 state = .Done;370 state = .Done;
...@@ -357,9 +377,13 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -357,9 +377,13 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
357 result.id = .Asterisk;377 result.id = .Asterisk;
358 state = .Done;378 state = .Done;
359 },379 },
380 '+' => {
381 result.id = .Plus;
382 state = .GotPlus;
383 },
360 '-' => {384 '-' => {
361 state = .Minus;
362 result.id = .Minus;385 result.id = .Minus;
386 state = .GotMinus;
363 },387 },
364 '!' => {388 '!' => {
365 result.id = .Bang;389 result.id = .Bang;
...@@ -383,7 +407,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -383,7 +407,11 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
383 },407 },
384 '|' => {408 '|' => {
385 result.id = .Pipe;409 result.id = .Pipe;
386 state = .Done;410 state = .GotPipe;
411 },
412 '&' => {
413 result.id = .Ampersand;
414 state = .GotAmpersand;
387 },415 },
388 '?' => {416 '?' => {
389 result.id = .QuestionMark;417 result.id = .QuestionMark;
...@@ -400,12 +428,27 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -400,12 +428,27 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:
400 }428 }
401 },429 },
402 .Done => return result,430 .Done => return result,
403 .Minus => {431 .GotMinus => {
404 switch (c) {432 switch (c) {
405 '>' => {433 '>' => {
406 result.id = .Arrow;434 result.id = .Arrow;
407 state = .Done;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 else => {452 else => {
410 return result;453 return result;
411 },454 },
...@@ -422,6 +465,39 @@ fn next(ctx: *Context, loc: ZigClangSourceLocation, name: []const u8, chars: [*:...@@ -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 .Float => {501 .Float => {
426 switch (c) {502 switch (c) {
427 '.', '0'...'9' => {},503 '.', '0'...'9' => {},
src-self-hosted/clang.zig+4
...@@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType();...@@ -43,6 +43,7 @@ pub const struct_ZigClangImplicitCastExpr = @OpaqueType();
43pub const struct_ZigClangIncompleteArrayType = @OpaqueType();43pub const struct_ZigClangIncompleteArrayType = @OpaqueType();
44pub const struct_ZigClangIntegerLiteral = @OpaqueType();44pub const struct_ZigClangIntegerLiteral = @OpaqueType();
45pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType();45pub const struct_ZigClangMacroDefinitionRecord = @OpaqueType();
46pub const struct_ZigClangMacroExpansion = @OpaqueType();
46pub const struct_ZigClangMacroQualifiedType = @OpaqueType();47pub const struct_ZigClangMacroQualifiedType = @OpaqueType();
47pub const struct_ZigClangMemberExpr = @OpaqueType();48pub const struct_ZigClangMemberExpr = @OpaqueType();
48pub const struct_ZigClangNamedDecl = @OpaqueType();49pub const struct_ZigClangNamedDecl = @OpaqueType();
...@@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr;...@@ -889,6 +890,7 @@ pub const ZigClangImplicitCastExpr = struct_ZigClangImplicitCastExpr;
889pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType;890pub const ZigClangIncompleteArrayType = struct_ZigClangIncompleteArrayType;
890pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral;891pub const ZigClangIntegerLiteral = struct_ZigClangIntegerLiteral;
891pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord;892pub const ZigClangMacroDefinitionRecord = struct_ZigClangMacroDefinitionRecord;
893pub const ZigClangMacroExpansion = struct_ZigClangMacroExpansion;
892pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType;894pub const ZigClangMacroQualifiedType = struct_ZigClangMacroQualifiedType;
893pub const ZigClangMemberExpr = struct_ZigClangMemberExpr;895pub const ZigClangMemberExpr = struct_ZigClangMemberExpr;
894pub const ZigClangNamedDecl = struct_ZigClangNamedDecl;896pub const ZigClangNamedDecl = struct_ZigClangNamedDecl;
...@@ -1058,6 +1060,8 @@ pub extern fn ZigClangMacroDefinitionRecord_getName_getNameStart(*const ZigClang...@@ -1058,6 +1060,8 @@ pub extern fn ZigClangMacroDefinitionRecord_getName_getNameStart(*const ZigClang
1058pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getBegin(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;1060pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getBegin(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;
1059pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getEnd(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;1061pub extern fn ZigClangMacroDefinitionRecord_getSourceRange_getEnd(*const ZigClangMacroDefinitionRecord) ZigClangSourceLocation;
10601062
1063pub extern fn ZigClangMacroExpansion_getDefinition(*const ZigClangMacroExpansion) *const ZigClangMacroDefinitionRecord;
1064
1061pub extern fn ZigClangIfStmt_getThen(*const ZigClangIfStmt) *const ZigClangStmt;1065pub extern fn ZigClangIfStmt_getThen(*const ZigClangIfStmt) *const ZigClangStmt;
1062pub extern fn ZigClangIfStmt_getElse(*const ZigClangIfStmt) ?*const ZigClangStmt;1066pub extern fn ZigClangIfStmt_getElse(*const ZigClangIfStmt) ?*const ZigClangStmt;
1063pub extern fn ZigClangIfStmt_getCond(*const ZigClangIfStmt) *const ZigClangStmt;1067pub 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,6 +4542,18 @@ fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: Zig
4542 };4542 };
4543 node = &bitshift_node.base;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 .Pipe => {4557 .Pipe => {
4546 const op_token = try appendToken(c, .Pipe, "|");4558 const op_token = try appendToken(c, .Pipe, "|");
4547 const rhs = try parseCExpr(c, it, source_loc, scope);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,6 +4566,66 @@ fn parseCSuffixOpExpr(c: *Context, it: *ctok.TokenList.Iterator, source_loc: Zig
4554 };4566 };
4555 node = &or_node.base;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 .LBrace => {4629 .LBrace => {
4558 const arr_node = try transCreateNodeArrayAccess(c, node);4630 const arr_node = try transCreateNodeArrayAccess(c, node);
4559 arr_node.op.ArrayAccess = try parseCExpr(c, it, source_loc, scope);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,6 +195,15 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
195 \\pub const REDISMODULE_READ = 1 << 0;195 \\pub const REDISMODULE_READ = 1 << 0;
196 });196 });
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
198 cases.add("double define struct",207 cases.add("double define struct",
199 \\typedef struct Bar Bar;208 \\typedef struct Bar Bar;
200 \\typedef struct Foo Foo;209 \\typedef struct Foo Foo;
...@@ -1068,6 +1077,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1068,6 +1077,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1068 \\pub const FOO_CHAR = '\xff';1077 \\pub const FOO_CHAR = '\xff';
1069 });1078 });
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
1071 cases.add("variable aliasing",1092 cases.add("variable aliasing",
1072 \\static long a = 2;1093 \\static long a = 2;
1073 \\static long b = 2;1094 \\static long b = 2;