Skip to content

Commit b2f4366

Browse files
committed
Make Compiler a stdlib
This is a further extension to #56128 to make the compiler into a proper stdlib, useable outside of `Base` as `using Compiler` in the same way that `JuliaSyntax` works already. There's a few remaining questions around how loading works, but mechanically, this PR is complete. For those remaining questions, I'll probably put up a separate PR that would migrate JuliaSyntax to it and then simply adopt that here once we've figured out the correct semantics.
1 parent de3a0f6 commit b2f4366

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+225
-181
lines changed

base/Base.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,7 @@ a_method_to_overwrite_in_test() = inferencebarrier(1)
380380

381381
# Compatibility with when Compiler was in Core
382382
@eval Core const Compiler = Main.Base.Compiler
383+
@eval Compiler const fl_parse = Core.Main.Base.fl_parse
383384

384385
# External libraries vendored into Base
385386
Core.println("JuliaSyntax/src/JuliaSyntax.jl")

base/Base_compiler.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -252,15 +252,14 @@ include("namedtuple.jl")
252252
include("ordering.jl")
253253
using .Order
254254

255-
include("compiler/compiler.jl")
255+
include("coreir.jl")
256+
257+
include("../stdlib/Compiler/src/Compiler.jl")
256258

257259
const _return_type = Compiler.return_type
258260

259261
# Enable compiler
260-
Core.eval(Compiler, quote
261-
include("compiler/bootstrap.jl")
262-
ccall(:jl_set_typeinf_func, Cvoid, (Any,), typeinf_ext_toplevel)
262+
Compiler.bootstrap!()
263263

264-
include("compiler/parsing.jl")
264+
include("flparse.jl")
265265
Core._setparser!(fl_parse)
266-
end)

base/boot.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ eval(Core, quote
512512
UpsilonNode(@nospecialize(val)) = $(Expr(:new, :UpsilonNode, :val))
513513
UpsilonNode() = $(Expr(:new, :UpsilonNode))
514514
Const(@nospecialize(v)) = $(Expr(:new, :Const, :v))
515-
# NOTE the main constructor is defined within `Core.Compiler`
516515
_PartialStruct(@nospecialize(typ), fields::Array{Any, 1}) = $(Expr(:new, :PartialStruct, :typ, :fields))
517516
PartialOpaque(@nospecialize(typ), @nospecialize(env), parent::MethodInstance, source) = $(Expr(:new, :PartialOpaque, :typ, :env, :parent, :source))
518517
InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) = $(Expr(:new, :InterConditional, :slot, :thentype, :elsetype))

base/compiler/bootstrap.jl

Lines changed: 0 additions & 52 deletions
This file was deleted.

base/coreir.jl

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# This file is a part of Julia. License is MIT: https://julialang.org/license
2+
3+
Core.PhiNode() = Core.PhiNode(Int32[], Any[])
4+
5+
"""
6+
struct Const
7+
val
8+
end
9+
10+
The type representing a constant value.
11+
"""
12+
Core.Const
13+
14+
"""
15+
struct PartialStruct
16+
typ
17+
fields::Vector{Any} # elements are other type lattice members
18+
end
19+
20+
This extended lattice element is introduced when we have information about an object's
21+
fields beyond what can be obtained from the object type. E.g. it represents a tuple where
22+
some elements are known to be constants or a struct whose `Any`-typed field is initialized
23+
with `Int` values.
24+
25+
- `typ` indicates the type of the object
26+
- `fields` holds the lattice elements corresponding to each field of the object
27+
28+
If `typ` is a struct, `fields` represents the fields of the struct that are guaranteed to be
29+
initialized. For instance, if the length of `fields` of `PartialStruct` representing a
30+
struct with 4 fields is 3, the 4th field may not be initialized. If the length is 4, all
31+
fields are guaranteed to be initialized.
32+
33+
If `typ` is a tuple, the last element of `fields` may be `Vararg`. In this case, it is
34+
guaranteed that the number of elements in the tuple is at least `length(fields)-1`, but the
35+
exact number of elements is unknown.
36+
"""
37+
Core.PartialStruct
38+
39+
"""
40+
struct InterConditional
41+
slot::Int
42+
thentype
43+
elsetype
44+
end
45+
46+
Similar to `Conditional`, but conveys inter-procedural constraints imposed on call arguments.
47+
This is separate from `Conditional` to catch logic errors: the lattice element name is `InterConditional`
48+
while processing a call, then `Conditional` everywhere else. Thus `InterConditional` does not appear in
49+
`CompilerTypes`—these type's usages are disjoint—though we define the lattice for `InterConditional`.
50+
"""
51+
Core.InterConditional
52+
53+
InterConditional(var::SlotNumber, @nospecialize(thentype), @nospecialize(elsetype)) =
54+
InterConditional(slot_id(var), thentype, elsetype)
File renamed without changes.

base/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2856,7 +2856,7 @@ module IRShow
28562856
Effects, ALWAYS_TRUE, ALWAYS_FALSE, DebugInfoStream, getdebugidx,
28572857
VarState, InvalidIRError, argextype, widenconst, singleton_type,
28582858
sptypes_from_meth_instance, EMPTY_SPTYPES
2859-
include("compiler/ssair/show.jl")
2859+
include("../stdlib/Compiler/src/ssair/show.jl")
28602860

28612861
const __debuginfo = Dict{Symbol, Any}(
28622862
# :full => src -> statementidx_lineinfo_printer(src), # and add variable slot information

src/toplevel.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,21 @@ JL_DLLEXPORT jl_value_t *jl_prepend_cwd(jl_value_t *str)
12891289
return jl_cstr_to_string(path);
12901290
}
12911291

1292+
JL_DLLEXPORT jl_value_t *jl_prepend_string(jl_value_t *prefix, jl_value_t *str)
1293+
{
1294+
char path[1024];
1295+
const char *pstr = (const char*)jl_string_data(prefix);
1296+
size_t sz = strlen(pstr);
1297+
const char *fstr = (const char*)jl_string_data(str);
1298+
if (strlen(fstr) + sz >= sizeof(path)) {
1299+
jl_errorf("use a bigger buffer for jl_fullpath");
1300+
}
1301+
strcpy(path, pstr);
1302+
strcpy(path + sz, fstr);
1303+
return jl_cstr_to_string(path);
1304+
}
1305+
1306+
12921307
#ifdef __cplusplus
12931308
}
12941309
#endif

stdlib/Compiler/Project.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name = "Compiler"
2+
uuid = "807dbc54-b67e-4c79-8afb-eafe4df6f2e1"
3+
version = "0.0.1"

base/compiler/compiler.jl renamed to stdlib/Compiler/src/Compiler.jl

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import Core: print, println, show, write, unsafe_write,
1212
memoryref_isassigned, memoryrefnew, memoryrefoffset, memoryrefget,
1313
memoryrefset!, typename
1414

15-
using ..Base
16-
using ..Base: Ordering, vect, EffectsOverride, BitVector, @_gc_preserve_begin, @_gc_preserve_end, RefValue,
15+
using Base
16+
using Base: Ordering, vect, EffectsOverride, BitVector, @_gc_preserve_begin, @_gc_preserve_end, RefValue,
1717
@nospecializeinfer, @_foldable_meta, fieldindex, is_function_def, indexed_iterate, isexpr, methods,
1818
get_world_counter, JLOptions, _methods_by_ftype, unwrap_unionall, cconvert, unsafe_convert,
1919
issingletontype, isType, rewrap_unionall, has_free_typevars, isvarargtype, hasgenerator,
@@ -29,8 +29,8 @@ using ..Base: Ordering, vect, EffectsOverride, BitVector, @_gc_preserve_begin, @
2929
specialize_method, hasintersect, is_nospecializeinfer, is_nospecialized,
3030
get_nospecializeinfer_sig, tls_world_age, uniontype_layout, kwerr,
3131
moduleroot, is_file_tracked, decode_effects_override
32-
using ..Base.Order
33-
import ..Base: getindex, setindex!, length, iterate, push!, isempty, first, convert, ==,
32+
using Base.Order
33+
import Base: getindex, setindex!, length, iterate, push!, isempty, first, convert, ==,
3434
copy, popfirst!, in, haskey, resize!, copy!, append!, last, get!, size,
3535
get, iterate, findall
3636

@@ -46,8 +46,21 @@ ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Compiler, false)
4646
eval(x) = Core.eval(Compiler, x)
4747
eval(m, x) = Core.eval(m, x)
4848

49-
include(x) = Base.include(Compiler, x)
50-
include(mod, x) = Base.include(mod, x)
49+
function include(x::String)
50+
if !isdefined(Base, :end_base_include)
51+
# During bootstrap, all includes are relative to `base/`
52+
x = ccall(:jl_prepend_string, Ref{String}, (Any, Any), "../stdlib/Compiler/src/", x)
53+
end
54+
Base.include(Compiler, x)
55+
end
56+
57+
function include(mod::Module, x::String)
58+
if !isdefined(Base, :end_base_include)
59+
x = ccall(:jl_prepend_string, Ref{String}, (Any, Any), "../stdlib/Compiler/src/", x)
60+
end
61+
Base.include(mod, x)
62+
end
63+
5164

5265
macro _boundscheck() Expr(:boundscheck) end
5366

@@ -59,7 +72,7 @@ abstract type AbstractInterpreter end
5972
function return_type end # promotion.jl expects this to exist
6073
is_return_type(Core.@nospecialize(f)) = f === return_type
6174

62-
include("compiler/sort.jl")
75+
include("sort.jl")
6376

6477
# We don't include some.jl, but this definition is still useful.
6578
something(x::Nothing, y...) = something(y...)
@@ -94,30 +107,32 @@ else
94107
end
95108
end
96109

97-
include("compiler/cicache.jl")
98-
include("compiler/methodtable.jl")
99-
include("compiler/effects.jl")
100-
include("compiler/types.jl")
101-
include("compiler/utilities.jl")
102-
include("compiler/validation.jl")
103-
104-
include("compiler/ssair/basicblock.jl")
105-
include("compiler/ssair/domtree.jl")
106-
include("compiler/ssair/ir.jl")
107-
include("compiler/ssair/tarjan.jl")
108-
109-
include("compiler/abstractlattice.jl")
110-
include("compiler/stmtinfo.jl")
111-
include("compiler/inferenceresult.jl")
112-
include("compiler/inferencestate.jl")
113-
114-
include("compiler/typeutils.jl")
115-
include("compiler/typelimits.jl")
116-
include("compiler/typelattice.jl")
117-
include("compiler/tfuncs.jl")
118-
119-
include("compiler/abstractinterpretation.jl")
120-
include("compiler/typeinfer.jl")
121-
include("compiler/optimize.jl")
110+
include("cicache.jl")
111+
include("methodtable.jl")
112+
include("effects.jl")
113+
include("types.jl")
114+
include("utilities.jl")
115+
include("validation.jl")
116+
117+
include("ssair/basicblock.jl")
118+
include("ssair/domtree.jl")
119+
include("ssair/ir.jl")
120+
include("ssair/tarjan.jl")
121+
122+
include("abstractlattice.jl")
123+
include("stmtinfo.jl")
124+
include("inferenceresult.jl")
125+
include("inferencestate.jl")
126+
127+
include("typeutils.jl")
128+
include("typelimits.jl")
129+
include("typelattice.jl")
130+
include("tfuncs.jl")
131+
132+
include("abstractinterpretation.jl")
133+
include("typeinfer.jl")
134+
include("optimize.jl")
135+
136+
include("bootstrap.jl")
122137

123138
end

0 commit comments

Comments
 (0)