Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: macro for __attribute__((section))

This is kind of a weird and un-Swift-thonic question, so bear with me.
I want to do in Swift something like the same thing I'm currently doing in Objective-C/C++, so I'll start by describing that.

I have some existing C++ code that defines a macro that, when used in an expression anywhere in the code, will insert an entry into a table in the binary at compile time. In other words, the user writes something like this:

#include "magic.h"

void foo(bool b) {
    if (b) {
        printf("%d\n", MAGIC(xyzzy));
    }
}

and thanks to the definition

#define MAGIC(Name) \
  []{ static int __attribute__((used, section("DATA,magical"))) Name; return Name; }()

what actually happens at compile time is that a static variable named xyzzy (modulo name-mangling) is created and allocated into the special magical section of my Mach-O binary, so that running nm -m foo.o to dump the symbols shows something a lot like this:

0000000000000098 (__TEXT,__eh_frame) non-external EH_frame0
0000000000000050 (__TEXT,__cstring) non-external L_.str
0000000000000000 (__TEXT,__text) external __Z3foob
00000000000000b0 (__TEXT,__eh_frame) external __Z3foob.eh
0000000000000040 (__TEXT,__text) non-external __ZZ3foobENK3$_0clEv
00000000000000d8 (__TEXT,__eh_frame) non-external __ZZ3foobENK3$_0clEv.eh
0000000000000054 (__DATA,magical) non-external [no dead strip] __ZZZ3foobENK3$_0clEvE5xyzzy
                 (undefined) external _printf

Through the magic of getsectbynamefromheader(), I can then load the symbol table for the magical section, scan through it, and find out (by demangling every symbol I find) that at some point in the user's code, he calls MAGIC(xyzzy). Eureka!


I can replicate the whole second half of that workflow just fine in Swift — starting with the getsectbynamefromheader() part. However, the first part has me stumped.

  • Swift has no preprocessor, so spelling the magic as elegantly as MAGIC(someidentifier) is impossible. I don't want it to be too ugly, though.

  • As far as I know, Swift has no way to insert symbols into a given section — no equivalent of __attribute__((section)). This is okay, though, since nothing in my plan requires a dedicated section; that part just makes the second half easier.

  • As far as I know, the only way to get a symbol into the symbol table in Swift is via a local struct definition. Something like this:

    func foo(b: Bool) -> Void {
        struct Local { static var xyzzy = 0; };
        println(Local.xyzzy);
    }
    

That works, but it's a bit of extra typing, and can't be done inline in an expression (not that that'll matter if we can't make a MAGIC macro in Swift anyway), and I'm worried that the Swift compiler might optimize it away.


So, there are three questions here, all about how to make Swift do things that Swift doesn't want to do: Macros, attributes, and creating symbols that are resistant to compiler optimization.

I'm aware of @asmname but I don't think it helps me since I can already deal with demangling on my own.

I'm aware that Swift has "generics", but they seem to be closer to Java generics than to C++ templates; I don't think they can be used as a substitute for macros in this particular case.

I'm aware that the code for the Swift compiler is now open-source; I've skimmed bits of it in vain; but I can't read through all of it looking for tricks that might not even be there.

like image 842
Quuxplusone Avatar asked Dec 26 '15 04:12

Quuxplusone


1 Answers

Here is the answer to your question about preprocessor (and macros).

Swift has no preprocessor, so spelling the magic as elegantly as MAGIC(someidentifier) is impossible. I don't want it to be too ugly, though.

Swift project has a preprocessor (but, AFAIK, it is not distributed with Swift's binary).

From swift-users mailing list:

What are .swift.gyb files?

It’s a preprocessor the Swift team wrote so that when they needed to build, say, ten nearly-identical variants of Int, they wouldn’t have to literally copy and paste the same code ten times. If you open one of those files, you’ll see that they’re mainly Swift code, but with some lines of code intermixed that are written in Python.

It is not as beautiful as C macros, but, IMHO, is more powerful. You can see the available commands with ./swift/utils/gyb --help command after cloning the Swift's git repo.

$ swift/utils/gyb --help

usage, etc (TL;DR)...

Example template:

      - Hello -
    %{
         x = 42
         def succ(a):
             return a+1
    }%

    I can assure you that ${x} < ${succ(x)}

    % if int(y) > 7:
    %    for i in range(3):
    y is greater than seven!
    %    end
    % else:
    y is less than or equal to seven
    % end

      - The End. -

When run with "gyb -Dy=9", the output is

      - Hello -

    I can assure you that 42 < 43

    y is greater than seven!
    y is greater than seven!
    y is greater than seven!

      - The End. -

My example of GYB usage is available on GitHub.Gist.

For more complex examples look for *.swift.gyb files in @apple/swift/stdlib/public/core.

like image 160
tie Avatar answered Nov 16 '22 10:11

tie