Skip to content

Commit dbf2c4b

Browse files
committed
Move Core.Compiler into Base
This is the first step in what I am hoping will eventually result in making the compiler itself and upgradable stdlib. Over time, we've gained several non-Base consumers of `Core.Compiler`, and we've reached a bit of a breaking point where maintaining those downstream dependencies is getting more difficult than the close coupling of Core.Compiler to the runtime is worth. In this first step, I am moving Core.Compiler into Base, ending the duplication of common data structure and generic functions between Core.Compiler and Base. This split goes back quite far (although not all the way) to the early days of Julia and predates the world-age mechanism. The extant Base and Core.Compiler environments have some differences (other than the duplication). I think the primary ones are (but I will add more here if somebody points one out). - `Core.Compiler` does not use `getproperty` - `Core.Compiler` does not have extensible `==` equality In this, I decided to retain the former by setting `getproperty = getfield` for Core.Compiler itself (though of course not for the datatstructures shared with Base). I don't think it's strictly necessary, but might as well. For equality, I decided the easiest thing to do would be to try to merge the equalities and see what happens. In general, Core.Compiler is relatively restricted in the kinds of equality comparisons it can make, so I think it'll work out fine, but we can revisit this. This seems to be fully working and most of this is just moving code around. I think most of that refactoring is independently useful, so I'll pull some of it out into separate PRs to make this PR more manageable.
1 parent 37f0220 commit dbf2c4b

Some content is hidden

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

42 files changed

+445
-501
lines changed

base/Base.jl

Lines changed: 10 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -1,186 +1,13 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
baremodule Base
4-
5-
using Core.Intrinsics, Core.IR
6-
7-
# to start, we're going to use a very simple definition of `include`
8-
# that doesn't require any function (except what we can get from the `Core` top-module)
9-
# start this big so that we don't have to resize before we have defined how to grow an array
10-
const _included_files = Array{Tuple{Module,String},1}(Core.undef, 400)
11-
setfield!(_included_files, :size, (1,))
12-
function include(mod::Module, path::String)
13-
len = getfield(_included_files.size, 1)
14-
memlen = _included_files.ref.mem.length
15-
lenp1 = Core.add_int(len, 1)
16-
if len === memlen # by the time this is true we hopefully will have defined _growend!
17-
_growend!(_included_files, UInt(1))
18-
else
19-
setfield!(_included_files, :size, (lenp1,))
20-
end
21-
Core.memoryrefset!(Core.memoryref(_included_files.ref, lenp1), (mod, ccall(:jl_prepend_cwd, Any, (Any,), path)), :not_atomic, true)
22-
Core.println(path)
23-
ccall(:jl_uv_flush, Nothing, (Ptr{Nothing},), Core.io_pointer(Core.stdout))
24-
Core.include(mod, path)
25-
end
26-
include(path::String) = include(Base, path)
27-
28-
struct IncludeInto <: Function
29-
m::Module
30-
end
31-
(this::IncludeInto)(fname::AbstractString) = include(this.m, fname)
32-
33-
# from now on, this is now a top-module for resolving syntax
34-
const is_primary_base_module = ccall(:jl_module_parent, Ref{Module}, (Any,), Base) === Core.Main
35-
ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Base, is_primary_base_module)
36-
37-
# The @inline/@noinline macros that can be applied to a function declaration are not available
38-
# until after array.jl, and so we will mark them within a function body instead.
39-
macro inline() Expr(:meta, :inline) end
40-
macro noinline() Expr(:meta, :noinline) end
41-
42-
macro _boundscheck() Expr(:boundscheck) end
43-
44-
# Try to help prevent users from shooting them-selves in the foot
45-
# with ambiguities by defining a few common and critical operations
46-
# (and these don't need the extra convert code)
47-
getproperty(x::Module, f::Symbol) = (@inline; getglobal(x, f))
48-
getproperty(x::Type, f::Symbol) = (@inline; getfield(x, f))
49-
setproperty!(x::Type, f::Symbol, v) = error("setfield! fields of Types should not be changed")
50-
setproperty!(x::Array, f::Symbol, v) = error("setfield! fields of Array should not be changed")
51-
getproperty(x::Tuple, f::Int) = (@inline; getfield(x, f))
52-
setproperty!(x::Tuple, f::Int, v) = setfield!(x, f, v) # to get a decent error
53-
54-
getproperty(x, f::Symbol) = (@inline; getfield(x, f))
55-
function setproperty!(x, f::Symbol, v)
56-
ty = fieldtype(typeof(x), f)
57-
val = v isa ty ? v : convert(ty, v)
58-
return setfield!(x, f, val)
59-
end
60-
61-
typeof(function getproperty end).name.constprop_heuristic = Core.FORCE_CONST_PROP
62-
typeof(function setproperty! end).name.constprop_heuristic = Core.FORCE_CONST_PROP
63-
64-
dotgetproperty(x, f) = getproperty(x, f)
65-
66-
getproperty(x::Module, f::Symbol, order::Symbol) = (@inline; getglobal(x, f, order))
67-
function setproperty!(x::Module, f::Symbol, v, order::Symbol=:monotonic)
68-
@inline
69-
ty = Core.get_binding_type(x, f)
70-
val = v isa ty ? v : convert(ty, v)
71-
return setglobal!(x, f, val, order)
72-
end
73-
getproperty(x::Type, f::Symbol, order::Symbol) = (@inline; getfield(x, f, order))
74-
setproperty!(x::Type, f::Symbol, v, order::Symbol) = error("setfield! fields of Types should not be changed")
75-
getproperty(x::Tuple, f::Int, order::Symbol) = (@inline; getfield(x, f, order))
76-
setproperty!(x::Tuple, f::Int, v, order::Symbol) = setfield!(x, f, v, order) # to get a decent error
77-
78-
getproperty(x, f::Symbol, order::Symbol) = (@inline; getfield(x, f, order))
79-
function setproperty!(x, f::Symbol, v, order::Symbol)
80-
@inline
81-
ty = fieldtype(typeof(x), f)
82-
val = v isa ty ? v : convert(ty, v)
83-
return setfield!(x, f, val, order)
84-
end
85-
86-
function swapproperty!(x, f::Symbol, v, order::Symbol=:not_atomic)
87-
@inline
88-
ty = fieldtype(typeof(x), f)
89-
val = v isa ty ? v : convert(ty, v)
90-
return Core.swapfield!(x, f, val, order)
91-
end
92-
function modifyproperty!(x, f::Symbol, op, v, order::Symbol=:not_atomic)
93-
@inline
94-
return Core.modifyfield!(x, f, op, v, order)
95-
end
96-
function replaceproperty!(x, f::Symbol, expected, desired, success_order::Symbol=:not_atomic, fail_order::Symbol=success_order)
97-
@inline
98-
ty = fieldtype(typeof(x), f)
99-
val = desired isa ty ? desired : convert(ty, desired)
100-
return Core.replacefield!(x, f, expected, val, success_order, fail_order)
101-
end
102-
function setpropertyonce!(x, f::Symbol, desired, success_order::Symbol=:not_atomic, fail_order::Symbol=success_order)
103-
@inline
104-
ty = fieldtype(typeof(x), f)
105-
val = desired isa ty ? desired : convert(ty, desired)
106-
return Core.setfieldonce!(x, f, val, success_order, fail_order)
107-
end
108-
109-
function swapproperty!(x::Module, f::Symbol, v, order::Symbol=:not_atomic)
110-
@inline
111-
ty = Core.get_binding_type(x, f)
112-
val = v isa ty ? v : convert(ty, v)
113-
return Core.swapglobal!(x, f, val, order)
114-
end
115-
function modifyproperty!(x::Module, f::Symbol, op, v, order::Symbol=:not_atomic)
116-
@inline
117-
return Core.modifyglobal!(x, f, op, v, order)
118-
end
119-
function replaceproperty!(x::Module, f::Symbol, expected, desired, success_order::Symbol=:not_atomic, fail_order::Symbol=success_order)
120-
@inline
121-
ty = Core.get_binding_type(x, f)
122-
val = desired isa ty ? desired : convert(ty, desired)
123-
return Core.replaceglobal!(x, f, expected, val, success_order, fail_order)
3+
had_compiler = isdefined(Base, :Compiler)
4+
if had_compiler; else
5+
include("Base_compiler.jl")
1246
end
125-
function setpropertyonce!(x::Module, f::Symbol, desired, success_order::Symbol=:not_atomic, fail_order::Symbol=success_order)
126-
@inline
127-
ty = Core.get_binding_type(x, f)
128-
val = desired isa ty ? desired : convert(ty, desired)
129-
return Core.setglobalonce!(x, f, val, success_order, fail_order)
130-
end
131-
1327

133-
convert(::Type{Any}, Core.@nospecialize x) = x
134-
convert(::Type{T}, x::T) where {T} = x
135-
include("coreio.jl")
136-
137-
eval(x) = Core.eval(Base, x)
138-
eval(m::Module, x) = Core.eval(m, x)
139-
140-
# init core docsystem
141-
import Core: @doc, @__doc__, WrappedException, @int128_str, @uint128_str, @big_str, @cmd
142-
if isdefined(Core, :Compiler)
143-
import Core.Compiler.CoreDocs
144-
Core.atdoc!(CoreDocs.docm)
145-
end
8+
const start_base_include = time_ns()
1469

147-
include("exports.jl")
148-
include("public.jl")
149-
150-
if false
151-
# simple print definitions for debugging. enable these if something
152-
# goes wrong during bootstrap before printing code is available.
153-
# otherwise, they just just eventually get (noisily) overwritten later
154-
global show, print, println
155-
show(io::IO, x) = Core.show(io, x)
156-
print(io::IO, a...) = Core.print(io, a...)
157-
println(io::IO, x...) = Core.println(io, x...)
158-
end
159-
160-
"""
161-
time_ns() -> UInt64
162-
163-
Get the time in nanoseconds relative to some arbitrary time in the past. The primary use is for measuring the elapsed time
164-
between two moments in time.
165-
"""
166-
time_ns() = ccall(:jl_hrtime, UInt64, ())
167-
168-
start_base_include = time_ns()
169-
170-
# A warning to be interpolated in the docstring of every dangerous mutating function in Base, see PR #50824
171-
const _DOCS_ALIASING_WARNING = """
172-
!!! warning
173-
Behavior can be unexpected when any mutated argument shares memory with any other argument.
174-
"""
175-
176-
## Load essential files and libraries
177-
include("essentials.jl")
178-
include("ctypes.jl")
179-
include("gcutils.jl")
180-
include("generator.jl")
181-
include("runtime_internals.jl")
18210
include("reflection.jl")
183-
include("options.jl")
18411

18512
# define invoke(f, T, args...; kwargs...), without kwargs wrapping
18613
# to forward to invoke
@@ -234,33 +61,6 @@ end
23461
# The REPL stdlib hooks into Base using this Ref
23562
const REPL_MODULE_REF = Ref{Module}(Base)
23663

237-
include("checked.jl")
238-
using .Checked
239-
function cld end
240-
function fld end
241-
242-
# Lazy strings
243-
include("strings/lazy.jl")
244-
245-
# array structures
246-
include("indices.jl")
247-
include("genericmemory.jl")
248-
include("array.jl")
249-
include("abstractarray.jl")
250-
include("subarray.jl")
251-
include("views.jl")
252-
include("baseext.jl")
253-
254-
include("c.jl")
255-
include("ntuple.jl")
256-
include("abstractdict.jl")
257-
include("iddict.jl")
258-
include("idset.jl")
259-
include("iterators.jl")
260-
using .Iterators: zip, enumerate, only
261-
using .Iterators: Flatten, Filter, product # for generators
262-
using .Iterators: Stateful # compat (was formerly used in reinterpretarray.jl)
263-
include("namedtuple.jl")
26464

26565
# For OS specific stuff
26666
# We need to strcat things here, before strings are really defined
@@ -332,13 +132,6 @@ include("reduce.jl")
332132
## core structures
333133
include("reshapedarray.jl")
334134
include("reinterpretarray.jl")
335-
include("bitarray.jl")
336-
include("bitset.jl")
337-
338-
if !isdefined(Core, :Compiler)
339-
include("docs/core.jl")
340-
Core.atdoc!(CoreDocs.docm)
341-
end
342135

343136
include("multimedia.jl")
344137
using .Multimedia
@@ -347,7 +140,6 @@ using .Multimedia
347140
include("some.jl")
348141

349142
include("dict.jl")
350-
include("abstractset.jl")
351143
include("set.jl")
352144

353145
# Strings
@@ -485,10 +277,6 @@ include("accumulate.jl")
485277
include("permuteddimsarray.jl")
486278
using .PermutedDimsArrays
487279

488-
# basic data structures
489-
include("ordering.jl")
490-
using .Order
491-
492280
# Combinatorics
493281
include("sort.jl")
494282
using .Sort
@@ -566,9 +354,8 @@ include("docs/basedocs.jl")
566354
# Documentation -- should always be included last in sysimg.
567355
include("docs/Docs.jl")
568356
using .Docs
569-
if isdefined(Core, :Compiler) && is_primary_base_module
570-
Docs.loaddocs(Core.Compiler.CoreDocs.DOCS)
571-
end
357+
Docs.loaddocs(CoreDocs.DOCS)
358+
@eval CoreDocs DOCS = DocLinkedList()
572359

573360
include("precompilation.jl")
574361

@@ -591,6 +378,9 @@ a_method_to_overwrite_in_test() = inferencebarrier(1)
591378
(this::IncludeInto)(fname::AbstractString) = include(identity, this.m, fname)
592379
(this::IncludeInto)(mapexpr::Function, fname::AbstractString) = include(mapexpr, this.m, fname)
593380

381+
# Compatibility with when Compiler was in Core
382+
@eval Core const Compiler = Main.Base.Compiler
383+
594384
# External libraries vendored into Base
595385
Core.println("JuliaSyntax/src/JuliaSyntax.jl")
596386
include(@__MODULE__, string(BUILDROOT, "JuliaSyntax/src/JuliaSyntax.jl")) # include($BUILDROOT/base/JuliaSyntax/JuliaSyntax.jl)
@@ -687,4 +477,4 @@ end
687477
@assert !isassigned(_included_files, 1)
688478
_included_files[1] = (parentmodule(Base), abspath(@__FILE__))
689479

690-
end # baremodule Base
480+
had_compiler && ccall(:jl_init_restored_module, Cvoid, (Any,), Base)

0 commit comments

Comments
 (0)