Demo of my Swift Interpreter that uses SwiftSyntax

I've open sourced SwiftFileBuilder!

The next step is to open source the SwiftLite repo itself. However, I will not release the entire source code at once. I'll probably start with some of the code generation, then the VM, then the disassembler and assembler, then the linker, etc. Each of these incremental releases will be added to the main SwiftLite repo.

3 Likes

Congrats!

In your release schedule, when will we be able to see the first vertical slice of the Swift Interpreter? Will that require all of the components to be released, or can you slice the components in such a way that for instance we can run print("Hello, World!") as a first slice?

There's a LOT that goes into hello world! So it may take awhile. I'm focusing on just getting the VM with assembler and disassembler out first.

Just a short list of what print("hello world") needs:

  • bytecode spec
  • vm
  • native functions
  • defining/loading stdlib
  • linking
  • bytecode emission from IR
  • type analysis/resolution on IR
  • AST to IR

In the earliest versions of the interpreter, it utilized a lot of hacks. For example, I had an opcode called OP_PRINT. But now printing is just a normal call to a Swift function exposed to the interpreter that the SwiftLite stdlib exposes.

1 Like

Who would have thought that a print("Hello, World") would require so many moving parts.

Sounds like a nice vertical slice though.

More updates!

Memory safely

I've migrated off AllocatorKit which I used to help catch memory leaks at runtime. I've now migrated the majority of the VM to use ~Copyable types to prove memory safety. A few places use ~Escapable types like for Ref or MutableRef.

Embedded Mode support

I have Swift Lite building in embedded mode now! This brought the wasm binary from ~100mb to ~1.5mb (~612kb gziped). This unblocks web demos as a primary way to show off new features.

The technical way I pulled this off was building SwiftSyntax and SwiftParser in Embedded mode! I have a script which modifies the source code to remove features which break in Embedded mode.

I was also able to avoid linking libswiftUnicodeDataTables which would have grown the WASM binary beyond what's responsible for a web app.

Simplified representation of runtime values

I've migrated off SwiftTaggedPointer as well. I actually really liked the results I had using this library to optimize the "SLValue" type at runtime, but it was a premature optimization. All runtime values in the SwiftLite VM are heap allocated now which makes things easier to work on.

Binding system + 0 cost startup

TLDR; Loading binding modules is now a near 0 cost on startup time.

The binding system is what allows Swift Lite to interop with real Swift APIs. The system's flaw was that each exposed native Swift API required SwiftLite to parse the source code String and compile it as if it were SwiftLite code. This negatively affects startup times.

Now the system utilizes generated c files which allow the pre-compiled SwiftLite IR to be encoded as a data structure stored in .rodata (readonly data segment) of the binary. The SwiftLite compiler just references a pointer and that's it!

There's a little more history behind this feature too. At my day job at Meta, I was able to use this technique to deliver a major optimization to a Swift analysis tool.

No more linking

I mentioned in my last comment how there was a linking step. That no longer exists. The interpreter's steps are now:

  1. Parse source code into SwiftSyntax.
  2. Parse SwiftSyntax to SwiftLite IR nodes.
  3. Run the compiler on the IR to turn it into resolved IR (same nodes, just with populated properties)
  4. If generating a binding, emit c files for the binding module
  5. If running, emit bytecode chunks.
  6. To run, initialize globals and execute main chunk.
2 Likes

Is there a hosted web demo or source code to review?

Not yet for the latest version, sorry.

I do have a web demo for the old version back in March though http://sl-web-editor.vercel.app/

So what is the point or goal of this project? Will it be closed source and proprietary once you complete development? Will it be open source?

I'll open source it later this year. But I'll also have a proprietary tool for iOS development associated with it.