[swift-dev] swift module format

John McCall rjmccall at apple.com
Fri Aug 25 00:56:07 CDT 2017

> On Aug 25, 2017, at 1:41 AM, Coder via swift-dev <swift-dev at swift.org> wrote:
> I’m working on a swift target for haxe and want it to use the interfaces in swift modules. The haxe compiler is written in ocml and so far I’ve created an llvm bitcode parser in ocaml which can read all the blocks and records, then read through the DECLS_AND_TYPES_BLOCK and map DECLS to their parents.
> My current guess is I’m not understanding how DECLs (like Int) that come from outside the module file work and maybe something to do with XREFs.
> I do understand the module format will change and am fine with making changes later. 

You say this, but... I really hate for you to spend a lot of time on something that is so low-level and that is so likely to be unstable.  I think it would make a lot more sense to write a C API to the AST and talk to it over the OCaml FFI.  Or if you really want to avoid the FFI, you could take our current ASTDumper output, which is currently pseudo-machine-readable, and make it actually machine-readable.  The elegant approach there would be to take the entire dumper and remove its pervasive dependence on an output stream; instead, it would be rewritten to make calls on a SAX-like streaming interface, and there would just be a standard implementation that formatted things as XML / JSON / our weird s-expression format.


> I’ll definitely take a look at those tools.
> Thanks for the help,
> Aaron
>> On Aug 24, 2017, at 5:56 PM, Slava Pestov <spestov at apple.com> wrote:
>> Yeah, it’s important to keep in mind we don’t have a stable module format right now, so anything developed Swift 4 will likely need to be revised again with the next version.
>> The C++ API is not stable either, but approach could be to dump the ‘generated interface’ using swift-ide-tool or similar, and parse the text. Also, it’s worth taking a look at swift-api-digester too.
>> Slava
>>> On Aug 24, 2017, at 4:35 PM, Argyrios Kyrtzidis via swift-dev <swift-dev at swift.org> wrote:
>>> Hi,
>>> What are you trying to do exactly ? Have you considered reading the decls with a tool using the C++ APIs (load a module and iterate over the decls) ?
>>> I think that would be easier and more future-proof.
>>>> On Aug 22, 2017, at 11:57 AM, Coder via swift-dev <swift-dev at swift.org> wrote:
>>>> Hello,
>>>> I’m working on extracting information from .swiftmodule files. Currently I can read all blocks and records but I’m having trouble identifying all DECLs in the DECLS_AND_TYPES_BLOCK. Below is a bcanalyzer dump of a swifmodule file created from this one line of code:
>>>> class Example {} 
>>>> A couple questions:
>>>> • The DECL_OFFSETS record contains 6 indexes but I only count 5 DECLs (class, destructor, constructor, 2 param). Where is the 6th?
>>>> • According to documentation, the DECL_OFFSETS point to records in the DECLS_AND_TYPES_BLOCK blob but offset indexes show below (the “op” attributes) are larger than the blob length. Where do the indexes start from?
>>>> Sorry if this is the wrong place to ask and thanks for any help,
>>>> Aaron
>>>> --------- bcanalyzer dump ---------
>>>> <MODULE_BLOCK NumWords=514 BlockCodeSize=2>
>>>> <CONTROL_BLOCK NumWords=35 BlockCodeSize=3>
>>>> <MODULE_NAME abbrevid=4/> blob data = 'tool'
>>>> <METADATA abbrevid=5 op0=0 op1=310 op2=3/> blob data = '3.1/Apple Swift version 3.1 (swiftlang-802.0.53 clang-802.0.42)'
>>>> <TARGET abbrevid=6/> blob data = 'x86_64-apple-macosx10.9'
>>>> <OPTIONS_BLOCK NumWords=2 BlockCodeSize=3>
>>>>   <IS_SIB abbrevid=4 op0=0/>
>>>> <INPUT_BLOCK NumWords=17 BlockCodeSize=4>
>>>> <IMPORTED_MODULE abbrevid=4 op0=0 op1=0/> blob data = 'Swift'
>>>> <IMPORTED_MODULE abbrevid=4 op0=0 op1=0/> blob data = 'SwiftOnoneSupport'
>>>> <SIL_BLOCK NumWords=72 BlockCodeSize=6>
>>>> </SIL_BLOCK>
>>>> <SIL_INDEX_BLOCK NumWords=3 BlockCodeSize=4>
>>>> <DECLS_AND_TYPES_BLOCK NumWords=309 BlockCodeSize=8>
>>>> <CLASS_DECL abbrevid=49 op0=3 op1=0 op2=0 op3=0 op4=0 op5=0 op6=0 op7=2 op8=1/> record string = ''
>>>> <MEMBERS abbrevid=90 op0=2 op1=3/>
>>>> <NORMAL_PROTOCOL_CONFORMANCE_ID abbrevid=85 op0=1/>
>>>> <NOMINAL_TYPE abbrevid=7 op0=1 op1=0/>
>>>> <ObjC_DECL_ATTR abbrevid=95 op0=1 op1=0 op2=0/> record string = ''
>>>> <DESTRUCTOR_DECL abbrevid=54 op0=1 op1=1 op2=1 op3=0 op4=2/>
>>>> <PARAMETERLIST abbrevid=55 op0=1/>
>>>> <PARAMETERLIST_ELT abbrevid=56 op0=4 op1=0 op2=0/>
>>>> <CONSTRUCTOR_DECL abbrevid=38 op0=1 op1=0 op2=1 op3=0 op4=0 op5=0 op6=0 op7=0 op8=3 op9=0 op10=2/> record string = ''
>>>> <PARAMETERLIST abbrevid=55 op0=1/>
>>>> <PARAMETERLIST_ELT abbrevid=56 op0=5 op1=0 op2=0/>
>>>> <PARAMETERLIST abbrevid=55 op0=0/>
>>>> <FUNCTION_TYPE abbrevid=11 op0=1 op1=4 op2=0 op3=0 op4=0 op5=0/>
>>>> <LOCAL_DISCRIMINATOR abbrevid=88 op0=0/>
>>>> <PARAM_DECL abbrevid=40 op0=0 op1=4 op2=2 op3=1 op4=1/>
>>>> <FUNCTION_TYPE abbrevid=11 op0=5 op1=6 op2=0 op3=0 op4=0 op5=0/>
>>>> <LOCAL_DISCRIMINATOR abbrevid=88 op0=0/>
>>>> <PARAM_DECL abbrevid=40 op0=0 op1=4 op2=3 op3=1 op4=1/>
>>>> <FUNCTION_TYPE abbrevid=11 op0=7 op1=7 op2=0 op3=0 op4=0 op5=0/>
>>>> <METATYPE_TYPE abbrevid=12 op0=1 op1=0/>
>>>> <FUNCTION_TYPE abbrevid=11 op0=7 op1=1 op2=0 op3=0 op4=0 op5=0/>
>>>> <TUPLE_TYPE abbrevid=9/>
>>>> <DECL_CONTEXT abbrevid=70 op0=1 op1=1/>
>>>> <DECL_CONTEXT abbrevid=70 op0=2 op1=1/>
>>>> <DECL_CONTEXT abbrevid=70 op0=3 op1=1/>
>>>> <NORMAL_PROTOCOL_CONFORMANCE abbrevid=82 op0=6 op1=1 op2=0 op3=0 op4=0/> record string = ''
>>>> <XREF abbrevid=91 op0=5 op1=1/>
>>>> <XREF_TYPE_PATH_PIECE abbrevid=75 op0=6 op1=0/>
>>>> <IDENTIFIER_DATA_BLOCK NumWords=10 BlockCodeSize=3>
>>>> <IDENTIFIER_DATA abbrevid=4/> blob data = unprintable, 30 bytes.
>>>> <INDEX_BLOCK NumWords=53 BlockCodeSize=4>
>>>> <DECL_OFFSETS abbrevid=4 op0=73085 op1=73418 op2=73598 op3=73894 op4=74114 op5=74671/>
>>>> <TYPE_OFFSETS abbrevid=4 op0=73348 op1=73817 op2=74037 op3=74257 op4=74334 op5=74375 op6=74452/>
>>>> <IDENTIFIER_OFFSETS abbrevid=4 op0=1 op1=9 op2=14 op3=20/>
>>>> <DECL_CONTEXT_OFFSETS abbrevid=4 op0=74460 op1=74500 op2=74540/>
>>>> <LOCAL_DECL_CONTEXT_OFFSETS abbrevid=4/> record string = ''
>>>> <GENERIC_ENVIRONMENT_OFFSETS abbrevid=4/> record string = ''
>>>> <NORMAL_CONFORMANCE_OFFSETS abbrevid=4 op0=74580/> record string = 'T'
>>>> <SIL_LAYOUT_OFFSETS abbrevid=4/> record string = ''
>>>> <TOP_LEVEL_DECLS abbrevid=5 op0=28/> blob data = unprintable, 40 bytes.
>>>> <OBJC_METHODS abbrevid=6 op0=32/> blob data = unprintable, 44 bytes.
>>>> _______________________________________________
>>>> swift-dev mailing list
>>>> swift-dev at swift.org
>>>> https://lists.swift.org/mailman/listinfo/swift-dev
>>> _______________________________________________
>>> swift-dev mailing list
>>> swift-dev at swift.org
>>> https://lists.swift.org/mailman/listinfo/swift-dev
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

More information about the swift-dev mailing list