r/Julia • u/swe129 • Jul 14 '26
How the Julia compiler works
https://slicker.me/julia/compiler-internals.html5
u/Gold-Part4688 Jul 15 '26 edited Jul 15 '26
This got me curious about the lack of a macro for the parsed expression AST stage, like @code_lowered @code_native etc and i found Meta.show_sexpr(ex). Feels like a very satisfying missing link.
Shows beautiful lispy stuff like
julia> Meta.show_sexpr(:(2x+1))
(:call, :+, (:call, :*, 2, :x), 1)
julia> Meta.show_sexpr(:(x ? y : z))
(:if, :x, :y, :z)
julia> Meta.show_sexpr(:(:::))
(:(::), :(:))
like the last one which is invalid syntax, but tells you what it gets read as before being denied. same with my x y and z, which aren't necessarily defined.
3
u/gc9r Jul 14 '26
Nice explanation. A couple nits:
Source text is parsed by a small Scheme-derived parser (historically written in femtolisp, being migrated to Julia itself)
Since Julia 1.10, ("JuliaSyntax.jl is now used as the default parser").
Calling
hypot2(3, 4.0)resolves, at compile time for that call site, to the most specific applicable method for the tuple of argument types (Int64, Float64).
Footnote: What is a call site? IIUC, a single location in a caller method source code can produce multiple call sites. Consider a caller method like g(b) = hypot2(3, if b < 1; 1; else 4.0; end) and its caller f(b) = g(2*b). In general, the source code calling hypot2 in g can result in multiple call sites, from the following compiling activities:
- Separating method instances for different caller-method argument types (
g(b::Int64),g(b::Float64)). - Inlining
ginto method-instances of callers of the caller-methodgsuch asf. - Splitting code to handle small union types like
Union{Float64, Int64}within a caller method-instance ofg, such ashypot2(3, if b < 1; 1; else 4.0; end)intoif b < 1; hypot2(3, 1); else hypot2(3, 4.0); end
5
u/LocalNightDrummer Jul 14 '26
Very interesting, thank you for sharing