authorgravatar for evan@lagerdata.comEvan Haas <evan@lagerdata.com> 2021-05-12 23:06:28-07:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2021-05-15 11:53:01+03:00
log36da8d02b56e06c92279b4df21fbfaa0273e6c7b
tree41253e368aee0245f1e7667fa682d6ba8dd9f6dc
parent607abb5b1484a17abd0ce98de5260c877fd08e49

translate-c: translate global (file scope) assembly


6 files changed, 62 insertions(+), 0 deletions(-)

src/clang.zig+5
...@@ -905,6 +905,11 @@ pub const TypedefNameDecl = opaque {...@@ -905,6 +905,11 @@ pub const TypedefNameDecl = opaque {
905 extern fn ZigClangTypedefNameDecl_getLocation(*const TypedefNameDecl) SourceLocation;905 extern fn ZigClangTypedefNameDecl_getLocation(*const TypedefNameDecl) SourceLocation;
906};906};
907907
908pub const FileScopeAsmDecl = opaque {
909 pub const getAsmString = ZigClangFileScopeAsmDecl_getAsmString;
910 extern fn ZigClangFileScopeAsmDecl_getAsmString(*const FileScopeAsmDecl) *const StringLiteral;
911};
912
908pub const TypedefType = opaque {913pub const TypedefType = opaque {
909 pub const getDecl = ZigClangTypedefType_getDecl;914 pub const getDecl = ZigClangTypedefType_getDecl;
910 extern fn ZigClangTypedefType_getDecl(*const TypedefType) *const TypedefNameDecl;915 extern fn ZigClangTypedefType_getDecl(*const TypedefType) *const TypedefNameDecl;
src/translate_c.zig+18
...@@ -480,6 +480,9 @@ fn declVisitor(c: *Context, decl: *const clang.Decl) Error!void {...@@ -480,6 +480,9 @@ fn declVisitor(c: *Context, decl: *const clang.Decl) Error!void {
480 .Empty => {480 .Empty => {
481 // Do nothing481 // Do nothing
482 },482 },
483 .FileScopeAsm => {
484 try transFileScopeAsm(c, &c.global_scope.base, @ptrCast(*const clang.FileScopeAsmDecl, decl));
485 },
483 else => {486 else => {
484 const decl_name = try c.str(decl.getDeclKindName());487 const decl_name = try c.str(decl.getDeclKindName());
485 try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name});488 try warn(c, &c.global_scope.base, decl.getLocation(), "ignoring {s} declaration", .{decl_name});
...@@ -487,6 +490,21 @@ fn declVisitor(c: *Context, decl: *const clang.Decl) Error!void {...@@ -487,6 +490,21 @@ fn declVisitor(c: *Context, decl: *const clang.Decl) Error!void {
487 }490 }
488}491}
489492
493fn transFileScopeAsm(c: *Context, scope: *Scope, file_scope_asm: *const clang.FileScopeAsmDecl) Error!void {
494 const asm_string = file_scope_asm.getAsmString();
495 var len: usize = undefined;
496 const bytes_ptr = asm_string.getString_bytes_begin_size(&len);
497
498 const str = try std.fmt.allocPrint(c.arena, "\"{}\"", .{std.zig.fmtEscapes(bytes_ptr[0..len])});
499 const str_node = try Tag.string_literal.create(c.arena, str);
500
501 const asm_node = try Tag.asm_simple.create(c.arena, str_node);
502 const block = try Tag.block_single.create(c.arena, asm_node);
503 const comptime_node = try Tag.@"comptime".create(c.arena, block);
504
505 try scope.appendNode(comptime_node);
506}
507
490fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {508fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
491 const fn_name = try c.str(@ptrCast(*const clang.NamedDecl, fn_decl).getName_bytes_begin());509 const fn_name = try c.str(@ptrCast(*const clang.NamedDecl, fn_decl).getName_bytes_begin());
492 if (c.global_scope.sym_table.contains(fn_name))510 if (c.global_scope.sym_table.contains(fn_name))
src/translate_c/ast.zig+17
...@@ -161,6 +161,8 @@ pub const Node = extern union {...@@ -161,6 +161,8 @@ pub const Node = extern union {
161 /// @shuffle(type, a, b, mask)161 /// @shuffle(type, a, b, mask)
162 shuffle,162 shuffle,
163163
164 asm_simple,
165
164 negate,166 negate,
165 negate_wrap,167 negate_wrap,
166 bit_not,168 bit_not,
...@@ -245,6 +247,7 @@ pub const Node = extern union {...@@ -245,6 +247,7 @@ pub const Node = extern union {
245 .std_mem_zeroes,247 .std_mem_zeroes,
246 .@"return",248 .@"return",
247 .@"comptime",249 .@"comptime",
250 .asm_simple,
248 .discard,251 .discard,
249 .std_math_Log2Int,252 .std_math_Log2Int,
250 .negate,253 .negate,
...@@ -1017,6 +1020,19 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {...@@ -1017,6 +1020,19 @@ fn renderNode(c: *Context, node: Node) Allocator.Error!NodeIndex {
1017 },1020 },
1018 });1021 });
1019 },1022 },
1023 .asm_simple => {
1024 const payload = node.castTag(.asm_simple).?.data;
1025 const asm_token = try c.addToken(.keyword_asm, "asm");
1026 _ = try c.addToken(.l_paren, "(");
1027 return c.addNode(.{
1028 .tag = .asm_simple,
1029 .main_token = asm_token,
1030 .data = .{
1031 .lhs = try renderNode(c, payload),
1032 .rhs = try c.addToken(.r_paren, ")"),
1033 },
1034 });
1035 },
1020 .type => {1036 .type => {
1021 const payload = node.castTag(.type).?.data;1037 const payload = node.castTag(.type).?.data;
1022 return c.addNode(.{1038 return c.addNode(.{
...@@ -2257,6 +2273,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {...@@ -2257,6 +2273,7 @@ fn renderNodeGrouped(c: *Context, node: Node) !NodeIndex {
2257 .@"continue",2273 .@"continue",
2258 .@"return",2274 .@"return",
2259 .@"comptime",2275 .@"comptime",
2276 .asm_simple,
2260 .usingnamespace_builtins,2277 .usingnamespace_builtins,
2261 .while_true,2278 .while_true,
2262 .if_not_break,2279 .if_not_break,
src/zig_clang.cpp+5
...@@ -1820,6 +1820,11 @@ const ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const ZigClangEnumDecl *z...@@ -1820,6 +1820,11 @@ const ZigClangEnumDecl *ZigClangEnumDecl_getDefinition(const ZigClangEnumDecl *z
1820 return reinterpret_cast<const ZigClangEnumDecl *>(definition);1820 return reinterpret_cast<const ZigClangEnumDecl *>(definition);
1821}1821}
18221822
1823const ZigClangStringLiteral *ZigClangFileScopeAsmDecl_getAsmString(const ZigClangFileScopeAsmDecl *self) {
1824 const clang::StringLiteral *result = reinterpret_cast<const clang::FileScopeAsmDecl*>(self)->getAsmString();
1825 return reinterpret_cast<const ZigClangStringLiteral *>(result);
1826}
1827
1823bool ZigClangRecordDecl_isUnion(const ZigClangRecordDecl *record_decl) {1828bool ZigClangRecordDecl_isUnion(const ZigClangRecordDecl *record_decl) {
1824 return reinterpret_cast<const clang::RecordDecl*>(record_decl)->isUnion();1829 return reinterpret_cast<const clang::RecordDecl*>(record_decl)->isUnion();
1825}1830}
src/zig_clang.h+3
...@@ -124,6 +124,7 @@ struct ZigClangEnumType;...@@ -124,6 +124,7 @@ struct ZigClangEnumType;
124struct ZigClangExpr;124struct ZigClangExpr;
125struct ZigClangFieldDecl;125struct ZigClangFieldDecl;
126struct ZigClangFileID;126struct ZigClangFileID;
127struct ZigClangFileScopeAsmDecl;
127struct ZigClangFloatingLiteral;128struct ZigClangFloatingLiteral;
128struct ZigClangForStmt;129struct ZigClangForStmt;
129struct ZigClangFullSourceLoc;130struct ZigClangFullSourceLoc;
...@@ -1000,6 +1001,8 @@ ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangV...@@ -1000,6 +1001,8 @@ ZIG_EXTERN_C unsigned ZigClangVarDecl_getAlignedAttribute(const struct ZigClangV
1000ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);1001ZIG_EXTERN_C unsigned ZigClangFunctionDecl_getAlignedAttribute(const struct ZigClangFunctionDecl *self, const ZigClangASTContext* ctx);
1001ZIG_EXTERN_C unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx);1002ZIG_EXTERN_C unsigned ZigClangFieldDecl_getAlignedAttribute(const struct ZigClangFieldDecl *self, const ZigClangASTContext* ctx);
10021003
1004ZIG_EXTERN_C const struct ZigClangStringLiteral *ZigClangFileScopeAsmDecl_getAsmString(const struct ZigClangFileScopeAsmDecl *self);
1005
1003ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);1006ZIG_EXTERN_C struct ZigClangQualType ZigClangParmVarDecl_getOriginalType(const struct ZigClangParmVarDecl *self);
10041007
1005ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *);1008ZIG_EXTERN_C bool ZigClangRecordDecl_getPackedAttribute(const struct ZigClangRecordDecl *);
test/translate_c.zig+14
...@@ -3499,4 +3499,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -3499,4 +3499,18 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
3499 \\ '\u{1f4af}',3499 \\ '\u{1f4af}',
3500 \\};3500 \\};
3501 });3501 });
3502
3503 cases.add("global assembly",
3504 \\__asm__(".globl func\n\t"
3505 \\ ".type func, @function\n\t"
3506 \\ "func:\n\t"
3507 \\ ".cfi_startproc\n\t"
3508 \\ "movl $42, %eax\n\t"
3509 \\ "ret\n\t"
3510 \\ ".cfi_endproc");
3511 , &[_][]const u8{
3512 \\comptime {
3513 \\ asm (".globl func\n\t.type func, @function\n\tfunc:\n\t.cfi_startproc\n\tmovl $42, %eax\n\tret\n\t.cfi_endproc");
3514 \\}
3515 });
3502}3516}