StatProfilerHTML.jl report
Generated on Thu, 21 Dec 2023 13:06:16
File source code
Line Exclusive Inclusive Code
1 # This file is a part of Julia. License is MIT: https://julialang.org/license
2
3 # commented-out definitions are implemented in C
4
5 #abstract type Any <: Any end
6 #abstract type Type{T} end
7
8 #abstract type Vararg{T} end
9
10 #mutable struct Symbol
11 ## opaque
12 #end
13
14 #mutable struct TypeName
15 # name::Symbol
16 #end
17
18 #mutable struct DataType <: Type
19 # name::TypeName
20 # super::Type
21 # parameters::Tuple
22 # names::Tuple
23 # types::Tuple
24 # ctor
25 # instance
26 # size::Int32
27 # abstract::Bool
28 # mutable::Bool
29 # pointerfree::Bool
30 #end
31
32 #struct Union <: Type
33 # a
34 # b
35 #end
36
37 #mutable struct TypeVar
38 # name::Symbol
39 # lb::Type
40 # ub::Type
41 #end
42
43 #struct UnionAll
44 # var::TypeVar
45 # body
46 #end
47
48 #struct Nothing
49 #end
50 #const nothing = Nothing()
51
52 #abstract type AbstractArray{T,N} end
53 #abstract type DenseArray{T,N} <: AbstractArray{T,N} end
54
55 #mutable struct Array{T,N} <: DenseArray{T,N}
56 ## opaque
57 #end
58
59 #mutable struct Module
60 ## opaque
61 #end
62
63 #mutable struct SimpleVector
64 ## opaque
65 #end
66
67 #mutable struct String
68 ## opaque
69 #end
70
71 #mutable struct Method
72 #...
73 #end
74
75 #mutable struct MethodInstance
76 #...
77 #end
78
79 #mutable struct CodeInstance
80 #...
81 #end
82
83 #mutable struct CodeInfo
84 #...
85 #end
86
87 #mutable struct TypeMapLevel
88 #...
89 #end
90
91 #mutable struct TypeMapEntry
92 #...
93 #end
94
95 #abstract type Ref{T} end
96 #primitive type Ptr{T} <: Ref{T} {32|64} end
97
98 # types for the front end
99
100 #mutable struct Expr
101 # head::Symbol
102 # args::Array{Any,1}
103 #end
104
105 #struct LineNumberNode
106 # line::Int
107 # file::Union{Symbol,Nothing}
108 #end
109
110 #struct LineInfoNode
111 # module::Module
112 # method::Any (Union{Symbol, Method, MethodInstance})
113 # file::Symbol
114 # line::Int32
115 # inlined_at::Int32
116 #end
117
118 #struct GotoNode
119 # label::Int
120 #end
121
122 #struct GotoIfNot
123 # cond::Any
124 # dest::Int
125 #end
126
127 #struct ReturnNode
128 # val::Any
129 #end
130
131 #struct PiNode
132 # val
133 # typ
134 #end
135
136 #struct PhiNode
137 # edges::Vector{Int32}
138 # values::Vector{Any}
139 #end
140
141 #struct PhiCNode
142 # values::Vector{Any}
143 #end
144
145 #struct UpsilonNode
146 # val
147 #end
148
149 #struct QuoteNode
150 # value
151 #end
152
153 #struct GlobalRef
154 # mod::Module
155 # name::Symbol
156 #end
157
158 #mutable struct Task
159 # parent::Task
160 # storage::Any
161 # state::Symbol
162 # donenotify::Any
163 # result::Any
164 # exception::Any
165 # backtrace::Any
166 # logstate::Any
167 # code::Any
168 #end
169
170 export
171 # key types
172 Any, DataType, Vararg, NTuple,
173 Tuple, Type, UnionAll, TypeVar, Union, Nothing, Cvoid,
174 AbstractArray, DenseArray, NamedTuple, Pair,
175 # special objects
176 Function, Method,
177 Module, Symbol, Task, Array, UndefInitializer, undef, WeakRef, VecElement,
178 # numeric types
179 Number, Real, Integer, Bool, Ref, Ptr,
180 AbstractFloat, Float16, Float32, Float64,
181 Signed, Int, Int8, Int16, Int32, Int64, Int128,
182 Unsigned, UInt, UInt8, UInt16, UInt32, UInt64, UInt128,
183 # string types
184 AbstractChar, Char, AbstractString, String, IO,
185 # errors
186 ErrorException, BoundsError, DivideError, DomainError, Exception,
187 InterruptException, InexactError, OutOfMemoryError, ReadOnlyMemoryError,
188 OverflowError, StackOverflowError, SegmentationFault, UndefRefError, UndefVarError,
189 TypeError, ArgumentError, MethodError, AssertionError, LoadError, InitError,
190 UndefKeywordError, ConcurrencyViolationError,
191 # AST representation
192 Expr, QuoteNode, LineNumberNode, GlobalRef,
193 # object model functions
194 fieldtype, getfield, setfield!, swapfield!, modifyfield!, replacefield!,
195 nfields, throw, tuple, ===, isdefined, eval,
196 # access to globals
197 getglobal, setglobal!,
198 # ifelse, sizeof # not exported, to avoid conflicting with Base
199 # type reflection
200 <:, typeof, isa, typeassert,
201 # method reflection
202 applicable, invoke,
203 # constants
204 nothing, Main
205
206 const getproperty = getfield # TODO: use `getglobal` for modules instead
207 const setproperty! = setfield!
208
209 abstract type Number end
210 abstract type Real <: Number end
211 abstract type AbstractFloat <: Real end
212 abstract type Integer <: Real end
213 abstract type Signed <: Integer end
214 abstract type Unsigned <: Integer end
215
216 primitive type Float16 <: AbstractFloat 16 end
217 primitive type Float32 <: AbstractFloat 32 end
218 primitive type Float64 <: AbstractFloat 64 end
219
220 #primitive type Bool <: Integer 8 end
221 abstract type AbstractChar end
222 primitive type Char <: AbstractChar 32 end
223
224 primitive type Int8 <: Signed 8 end
225 #primitive type UInt8 <: Unsigned 8 end
226 primitive type Int16 <: Signed 16 end
227 #primitive type UInt16 <: Unsigned 16 end
228 #primitive type Int32 <: Signed 32 end
229 #primitive type UInt32 <: Unsigned 32 end
230 #primitive type Int64 <: Signed 64 end
231 #primitive type UInt64 <: Unsigned 64 end
232 primitive type Int128 <: Signed 128 end
233 primitive type UInt128 <: Unsigned 128 end
234
235 if Int === Int64
236 const UInt = UInt64
237 else
238 const UInt = UInt32
239 end
240
241 function iterate end
242 function Typeof end
243 ccall(:jl_toplevel_eval_in, Any, (Any, Any),
244 Core, quote
245 (f::typeof(Typeof))(x) = ($(_expr(:meta,:nospecialize,:x)); isa(x,Type) ? Type{x} : typeof(x))
246 end)
247
248 macro nospecialize(x)
249 _expr(:meta, :nospecialize, x)
250 end
251
252 TypeVar(n::Symbol) = _typevar(n, Union{}, Any)
253 TypeVar(n::Symbol, @nospecialize(ub)) = _typevar(n, Union{}, ub)
254 TypeVar(n::Symbol, @nospecialize(lb), @nospecialize(ub)) = _typevar(n, lb, ub)
255
256 UnionAll(v::TypeVar, @nospecialize(t)) = ccall(:jl_type_unionall, Any, (Any, Any), v, t)
257
258 # simple convert for use by constructors of types in Core
259 # note that there is no actual conversion defined here,
260 # so the methods and ccall's in Core aren't permitted to use convert
261 convert(::Type{Any}, @nospecialize(x)) = x
262 convert(::Type{T}, x::T) where {T} = x
263 cconvert(::Type{T}, x) where {T} = convert(T, x)
264 unsafe_convert(::Type{T}, x::T) where {T} = x
265
266 # dispatch token indicating a kwarg (keyword sorter) call
267 function kwcall end
268 # deprecated internal functions:
269 kwfunc(@nospecialize(f)) = kwcall
270 kwftype(@nospecialize(t)) = typeof(kwcall)
271
272 # Let the compiler assume that calling Union{} as a constructor does not need
273 # to be considered ever (which comes up often as Type{<:T} inference, and
274 # occasionally in user code from eltype).
275 Union{}(a...) = throw(ArgumentError("cannot construct a value of type Union{} for return result"))
276 kwcall(kwargs, ::Type{Union{}}, a...) = Union{}(a...)
277
278 Expr(@nospecialize args...) = _expr(args...)
279
280 abstract type Exception end
281 struct ErrorException <: Exception
282 msg::AbstractString
283 end
284
285 macro inline() Expr(:meta, :inline) end
286 macro noinline() Expr(:meta, :noinline) end
287
288 struct BoundsError <: Exception
289 a::Any
290 i::Any
291 BoundsError() = new()
292 BoundsError(@nospecialize(a)) = (@noinline; new(a))
293 BoundsError(@nospecialize(a), i) = (@noinline; new(a,i))
294 end
295 struct DivideError <: Exception end
296 struct OutOfMemoryError <: Exception end
297 struct ReadOnlyMemoryError <: Exception end
298 struct SegmentationFault <: Exception end
299 struct StackOverflowError <: Exception end
300 struct UndefRefError <: Exception end
301 struct UndefVarError <: Exception
302 var::Symbol
303 end
304 struct ConcurrencyViolationError <: Exception
305 msg::AbstractString
306 end
307 struct InterruptException <: Exception end
308 struct DomainError <: Exception
309 val
310 msg::AbstractString
311 DomainError(@nospecialize(val)) = (@noinline; new(val, ""))
312 DomainError(@nospecialize(val), @nospecialize(msg)) = (@noinline; new(val, msg))
313 end
314 struct TypeError <: Exception
315 # `func` is the name of the builtin function that encountered a type error,
316 # the name of the type that hit an error in its definition or application, or
317 # some other brief description of where the error happened.
318 # `context` optionally adds extra detail, e.g. the name of the type parameter
319 # that got a bad value.
320 func::Symbol
321 context::Union{AbstractString,Symbol}
322 expected::Type
323 got
324 TypeError(func, context, @nospecialize(expected::Type), @nospecialize(got)) =
325 new(func, context, expected, got)
326 end
327 TypeError(where, @nospecialize(expected::Type), @nospecialize(got)) =
328 TypeError(Symbol(where), "", expected, got)
329 struct InexactError <: Exception
330 func::Symbol
331 T # Type
332 val
333 InexactError(f::Symbol, @nospecialize(T), @nospecialize(val)) = (@noinline; new(f, T, val))
334 end
335 struct OverflowError <: Exception
336 msg::AbstractString
337 end
338
339 struct ArgumentError <: Exception
340 msg::AbstractString
341 end
342 struct UndefKeywordError <: Exception
343 var::Symbol
344 end
345
346 struct MethodError <: Exception
347 f
348 args
349 world::UInt
350 MethodError(@nospecialize(f), @nospecialize(args), world::UInt) = new(f, args, world)
351 end
352 const typemax_UInt = ccall(:jl_typemax_uint, Any, (Any,), UInt)
353 MethodError(@nospecialize(f), @nospecialize(args)) = MethodError(f, args, typemax_UInt)
354
355 struct AssertionError <: Exception
356 msg::AbstractString
357 end
358 AssertionError() = AssertionError("")
359
360 abstract type WrappedException <: Exception end
361
362 struct LoadError <: WrappedException
363 file::AbstractString
364 line::Int
365 error
366 end
367
368 struct InitError <: WrappedException
369 mod::Symbol
370 error
371 end
372
373 struct PrecompilableError <: Exception end
374
375 String(s::String) = s # no constructor yet
376
377 const Cvoid = Nothing
378 Nothing() = nothing
379
380 # This should always be inlined
381 getptls() = ccall(:jl_get_ptls_states, Ptr{Cvoid}, ())
382
383 include(m::Module, fname::String) = ccall(:jl_load_, Any, (Any, Any), m, fname)
384
385 272 (100 %)
272 (100 %) samples spent in eval
272 (100 %) (incl.) when called from include_string line 2070
272 (100 %) samples spent calling solve
eval(m::Module, @nospecialize(e)) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
386
387 mutable struct Box
388 contents::Any
389 Box(@nospecialize(x)) = new(x)
390 Box() = new()
391 end
392
393 # constructors for built-in types
394
395 mutable struct WeakRef
396 value
397 WeakRef() = WeakRef(nothing)
398 WeakRef(@nospecialize(v)) = ccall(:jl_gc_new_weakref_th, Ref{WeakRef},
399 (Ptr{Cvoid}, Any), getptls(), v)
400 end
401
402 Tuple{}() = ()
403
404 struct VecElement{T}
405 value::T
406 VecElement{T}(value::T) where {T} = new(value) # disable converting constructor in Core
407 end
408 VecElement(arg::T) where {T} = VecElement{T}(arg)
409
410 eval(Core, quote
411 GotoNode(label::Int) = $(Expr(:new, :GotoNode, :label))
412 NewvarNode(slot::SlotNumber) = $(Expr(:new, :NewvarNode, :slot))
413 QuoteNode(@nospecialize value) = $(Expr(:new, :QuoteNode, :value))
414 SSAValue(id::Int) = $(Expr(:new, :SSAValue, :id))
415 Argument(n::Int) = $(Expr(:new, :Argument, :n))
416 ReturnNode(@nospecialize val) = $(Expr(:new, :ReturnNode, :val))
417 ReturnNode() = $(Expr(:new, :ReturnNode)) # unassigned val indicates unreachable
418 GotoIfNot(@nospecialize(cond), dest::Int) = $(Expr(:new, :GotoIfNot, :cond, :dest))
419 LineNumberNode(l::Int) = $(Expr(:new, :LineNumberNode, :l, nothing))
420 function LineNumberNode(l::Int, @nospecialize(f))
421 isa(f, String) && (f = Symbol(f))
422 return $(Expr(:new, :LineNumberNode, :l, :f))
423 end
424 LineInfoNode(mod::Module, @nospecialize(method), file::Symbol, line::Int32, inlined_at::Int32) =
425 $(Expr(:new, :LineInfoNode, :mod, :method, :file, :line, :inlined_at))
426 SlotNumber(n::Int) = $(Expr(:new, :SlotNumber, :n))
427 PhiNode(edges::Array{Int32, 1}, values::Array{Any, 1}) = $(Expr(:new, :PhiNode, :edges, :values))
428 PiNode(@nospecialize(val), @nospecialize(typ)) = $(Expr(:new, :PiNode, :val, :typ))
429 PhiCNode(values::Array{Any, 1}) = $(Expr(:new, :PhiCNode, :values))
430 UpsilonNode(@nospecialize(val)) = $(Expr(:new, :UpsilonNode, :val))
431 UpsilonNode() = $(Expr(:new, :UpsilonNode))
432 Const(@nospecialize(v)) = $(Expr(:new, :Const, :v))
433 # NOTE the main constructor is defined within `Core.Compiler`
434 _PartialStruct(@nospecialize(typ), fields::Array{Any, 1}) = $(Expr(:new, :PartialStruct, :typ, :fields))
435 PartialOpaque(@nospecialize(typ), @nospecialize(env), parent::MethodInstance, source) = $(Expr(:new, :PartialOpaque, :typ, :env, :parent, :source))
436 InterConditional(slot::Int, @nospecialize(thentype), @nospecialize(elsetype)) = $(Expr(:new, :InterConditional, :slot, :thentype, :elsetype))
437 MethodMatch(@nospecialize(spec_types), sparams::SimpleVector, method::Method, fully_covers::Bool) = $(Expr(:new, :MethodMatch, :spec_types, :sparams, :method, :fully_covers))
438 end)
439
440 function CodeInstance(
441 mi::MethodInstance, @nospecialize(rettype), @nospecialize(inferred_const),
442 @nospecialize(inferred), const_flags::Int32, min_world::UInt, max_world::UInt,
443 ipo_effects::UInt32, effects::UInt32, @nospecialize(argescapes#=::Union{Nothing,Vector{ArgEscapeInfo}}=#),
444 relocatability::UInt8)
445 return ccall(:jl_new_codeinst, Ref{CodeInstance},
446 (Any, Any, Any, Any, Int32, UInt, UInt, UInt32, UInt32, Any, UInt8),
447 mi, rettype, inferred_const, inferred, const_flags, min_world, max_world,
448 ipo_effects, effects, argescapes,
449 relocatability)
450 end
451 GlobalRef(m::Module, s::Symbol) = ccall(:jl_module_globalref, Ref{GlobalRef}, (Any, Any), m, s)
452 Module(name::Symbol=:anonymous, std_imports::Bool=true, default_names::Bool=true) = ccall(:jl_f_new_module, Ref{Module}, (Any, Bool, Bool), name, std_imports, default_names)
453
454 function _Task(@nospecialize(f), reserved_stack::Int, completion_future)
455 return ccall(:jl_new_task, Ref{Task}, (Any, Any, Int), f, completion_future, reserved_stack)
456 end
457
458 _is_internal(__module__) = __module__ === Core
459 # can be used in place of `@assume_effects :foldable` (supposed to be used for bootstrapping)
460 macro _foldable_meta()
461 return _is_internal(__module__) && Expr(:meta, Expr(:purity,
462 #=:consistent=#true,
463 #=:effect_free=#true,
464 #=:nothrow=#false,
465 #=:terminates_globally=#true,
466 #=:terminates_locally=#false,
467 #=:notaskstate=#false,
468 #=:inaccessiblememonly=#false))
469 end
470
471 const NTuple{N,T} = Tuple{Vararg{T,N}}
472
473 ## primitive Array constructors
474 struct UndefInitializer end
475 const undef = UndefInitializer()
476 # type and dimensionality specified, accepting dims as series of Ints
477 Array{T,1}(::UndefInitializer, m::Int) where {T} =
478 ccall(:jl_alloc_array_1d, Array{T,1}, (Any, Int), Array{T,1}, m)
479 7 (3 %) 7 (3 %)
7 (3 %) samples spent in Array
7 (100 %) (ex.), 7 (100 %) (incl.) when called from Array line 487
Array{T,2}(::UndefInitializer, m::Int, n::Int) where {T} =
480 ccall(:jl_alloc_array_2d, Array{T,2}, (Any, Int, Int), Array{T,2}, m, n)
481 Array{T,3}(::UndefInitializer, m::Int, n::Int, o::Int) where {T} =
482 ccall(:jl_alloc_array_3d, Array{T,3}, (Any, Int, Int, Int), Array{T,3}, m, n, o)
483 Array{T,N}(::UndefInitializer, d::Vararg{Int,N}) where {T,N} =
484 ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d)
485 # type and dimensionality specified, accepting dims as tuples of Ints
486 Array{T,1}(::UndefInitializer, d::NTuple{1,Int}) where {T} = Array{T,1}(undef, getfield(d,1))
487 7 (3 %)
7 (3 %) samples spent in Array
7 (100 %) (incl.) when called from similar line 420
7 (100 %) samples spent calling Array
Array{T,2}(::UndefInitializer, d::NTuple{2,Int}) where {T} = Array{T,2}(undef, getfield(d,1), getfield(d,2))
488 Array{T,3}(::UndefInitializer, d::NTuple{3,Int}) where {T} = Array{T,3}(undef, getfield(d,1), getfield(d,2), getfield(d,3))
489 Array{T,N}(::UndefInitializer, d::NTuple{N,Int}) where {T,N} = ccall(:jl_new_array, Array{T,N}, (Any, Any), Array{T,N}, d)
490 # type but not dimensionality specified
491 Array{T}(::UndefInitializer, m::Int) where {T} = Array{T,1}(undef, m)
492 Array{T}(::UndefInitializer, m::Int, n::Int) where {T} = Array{T,2}(undef, m, n)
493 Array{T}(::UndefInitializer, m::Int, n::Int, o::Int) where {T} = Array{T,3}(undef, m, n, o)
494 Array{T}(::UndefInitializer, d::NTuple{N,Int}) where {T,N} = Array{T,N}(undef, d)
495 # empty vector constructor
496 Array{T,1}() where {T} = Array{T,1}(undef, 0)
497
498 (Array{T,N} where T)(x::AbstractArray{S,N}) where {S,N} = Array{S,N}(x)
499
500 Array(A::AbstractArray{T,N}) where {T,N} = Array{T,N}(A)
501 Array{T}(A::AbstractArray{S,N}) where {T,N,S} = Array{T,N}(A)
502
503 AbstractArray{T}(A::AbstractArray{S,N}) where {T,S,N} = AbstractArray{T,N}(A)
504
505 # primitive Symbol constructors
506
507 ## Helper for proper GC rooting without unsafe_convert
508 eval(Core, quote
509 _Symbol(ptr::Ptr{UInt8}, sz::Int, root::Any) = $(Expr(:foreigncall, QuoteNode(:jl_symbol_n),
510 Ref{Symbol}, svec(Ptr{UInt8}, Int), 0, QuoteNode(:ccall), :ptr, :sz, :root))
511 end)
512
513 function Symbol(s::String)
514 @_foldable_meta
515 @noinline
516 return _Symbol(ccall(:jl_string_ptr, Ptr{UInt8}, (Any,), s), sizeof(s), s)
517 end
518 function Symbol(a::Array{UInt8,1})
519 @noinline
520 return _Symbol(ccall(:jl_array_ptr, Ptr{UInt8}, (Any,), a), Intrinsics.arraylen(a), a)
521 end
522 Symbol(s::Symbol) = s
523
524 # module providing the IR object model
525 module IR
526
527 export CodeInfo, MethodInstance, CodeInstance, GotoNode, GotoIfNot, ReturnNode,
528 NewvarNode, SSAValue, SlotNumber, Argument,
529 PiNode, PhiNode, PhiCNode, UpsilonNode, LineInfoNode,
530 Const, PartialStruct, InterConditional
531
532 import Core: CodeInfo, MethodInstance, CodeInstance, GotoNode, GotoIfNot, ReturnNode,
533 NewvarNode, SSAValue, SlotNumber, Argument,
534 PiNode, PhiNode, PhiCNode, UpsilonNode, LineInfoNode,
535 Const, PartialStruct, InterConditional
536
537 end # module IR
538
539 # docsystem basics
540 macro doc(x...)
541 docex = atdoc(__source__, __module__, x...)
542 isa(docex, Expr) && docex.head === :escape && return docex
543 return Expr(:escape, Expr(:var"hygienic-scope", docex, typeof(atdoc).name.module, __source__))
544 end
545 macro __doc__(x)
546 return Expr(:escape, Expr(:block, Expr(:meta, :doc), x))
547 end
548 atdoc = (source, mod, str, expr) -> Expr(:escape, expr)
549 atdoc!(λ) = global atdoc = λ
550
551 # macros for big integer syntax
552 macro int128_str end
553 macro uint128_str end
554 macro big_str end
555
556 # macro for command syntax
557 macro cmd end
558
559
560 # simple stand-alone print definitions for debugging
561 abstract type IO end
562 struct CoreSTDOUT <: IO end
563 struct CoreSTDERR <: IO end
564 const stdout = CoreSTDOUT()
565 const stderr = CoreSTDERR()
566 io_pointer(::CoreSTDOUT) = Intrinsics.pointerref(Intrinsics.cglobal(:jl_uv_stdout, Ptr{Cvoid}), 1, 1)
567 io_pointer(::CoreSTDERR) = Intrinsics.pointerref(Intrinsics.cglobal(:jl_uv_stderr, Ptr{Cvoid}), 1, 1)
568
569 unsafe_write(io::IO, x::Ptr{UInt8}, nb::UInt) =
570 (ccall(:jl_uv_puts, Cvoid, (Ptr{Cvoid}, Ptr{UInt8}, UInt), io_pointer(io), x, nb); nb)
571 unsafe_write(io::IO, x::Ptr{UInt8}, nb::Int) =
572 (ccall(:jl_uv_puts, Cvoid, (Ptr{Cvoid}, Ptr{UInt8}, Int), io_pointer(io), x, nb); nb)
573 write(io::IO, x::UInt8) =
574 (ccall(:jl_uv_putb, Cvoid, (Ptr{Cvoid}, UInt8), io_pointer(io), x); 1)
575 function write(io::IO, x::String)
576 nb = sizeof(x)
577 unsafe_write(io, ccall(:jl_string_ptr, Ptr{UInt8}, (Any,), x), nb)
578 return nb
579 end
580
581 show(io::IO, @nospecialize x) = ccall(:jl_static_show, Cvoid, (Ptr{Cvoid}, Any), io_pointer(io), x)
582 print(io::IO, x::AbstractChar) = ccall(:jl_uv_putc, Cvoid, (Ptr{Cvoid}, Char), io_pointer(io), x)
583 print(io::IO, x::String) = (write(io, x); nothing)
584 print(io::IO, @nospecialize x) = show(io, x)
585 print(io::IO, @nospecialize(x), @nospecialize a...) = (print(io, x); print(io, a...))
586 println(io::IO) = (write(io, 0x0a); nothing) # 0x0a = '\n'
587 println(io::IO, @nospecialize x...) = (print(io, x...); println(io))
588
589 show(@nospecialize a) = show(stdout, a)
590 print(@nospecialize a...) = print(stdout, a...)
591 println(@nospecialize a...) = println(stdout, a...)
592
593 struct GeneratedFunctionStub
594 gen
595 argnames::SimpleVector
596 spnames::SimpleVector
597 end
598
599 # invoke and wrap the results of @generated expression
600 function (g::GeneratedFunctionStub)(world::UInt, source::LineNumberNode, @nospecialize args...)
601 # args is (spvals..., argtypes...)
602 body = g.gen(args...)
603 file = source.file
604 file isa Symbol || (file = :none)
605 lam = Expr(:lambda, Expr(:argnames, g.argnames...).args,
606 Expr(:var"scope-block",
607 Expr(:block,
608 source,
609 Expr(:meta, :push_loc, file, :var"@generated body"),
610 Expr(:return, body),
611 Expr(:meta, :pop_loc))))
612 spnames = g.spnames
613 if spnames === svec()
614 return lam
615 else
616 return Expr(Symbol("with-static-parameters"), lam, spnames...)
617 end
618 end
619
620 NamedTuple() = NamedTuple{(),Tuple{}}(())
621
622 eval(Core, :(NamedTuple{names}(args::Tuple) where {names} =
623 $(Expr(:splatnew, :(NamedTuple{names,typeof(args)}), :args))))
624
625 using .Intrinsics: sle_int, add_int
626
627 eval(Core, :(NamedTuple{names,T}(args::T) where {names, T <: Tuple} =
628 $(Expr(:splatnew, :(NamedTuple{names,T}), :args))))
629
630 # constructors for built-in types
631
632 import .Intrinsics: eq_int, trunc_int, lshr_int, sub_int, shl_int, bitcast, sext_int, zext_int, and_int
633
634 throw_inexacterror(f::Symbol, ::Type{T}, val) where {T} = (@noinline; throw(InexactError(f, T, val)))
635
636 function is_top_bit_set(x)
637 @inline
638 eq_int(trunc_int(UInt8, lshr_int(x, sub_int(shl_int(sizeof(x), 3), 1))), trunc_int(UInt8, 1))
639 end
640
641 function is_top_bit_set(x::Union{Int8,UInt8})
642 @inline
643 eq_int(lshr_int(x, 7), trunc_int(typeof(x), 1))
644 end
645
646 function check_top_bit(::Type{To}, x) where {To}
647 @inline
648 is_top_bit_set(x) && throw_inexacterror(:check_top_bit, To, x)
649 x
650 end
651
652 function checked_trunc_sint(::Type{To}, x::From) where {To,From}
653 @inline
654 y = trunc_int(To, x)
655 back = sext_int(From, y)
656 eq_int(x, back) || throw_inexacterror(:trunc, To, x)
657 y
658 end
659
660 function checked_trunc_uint(::Type{To}, x::From) where {To,From}
661 @inline
662 y = trunc_int(To, x)
663 back = zext_int(From, y)
664 eq_int(x, back) || throw_inexacterror(:trunc, To, x)
665 y
666 end
667
668 toInt8(x::Int8) = x
669 toInt8(x::Int16) = checked_trunc_sint(Int8, x)
670 toInt8(x::Int32) = checked_trunc_sint(Int8, x)
671 toInt8(x::Int64) = checked_trunc_sint(Int8, x)
672 toInt8(x::Int128) = checked_trunc_sint(Int8, x)
673 toInt8(x::UInt8) = bitcast(Int8, check_top_bit(Int8, x))
674 toInt8(x::UInt16) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
675 toInt8(x::UInt32) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
676 toInt8(x::UInt64) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
677 toInt8(x::UInt128) = checked_trunc_sint(Int8, check_top_bit(Int8, x))
678 toInt8(x::Bool) = and_int(bitcast(Int8, x), Int8(1))
679 toInt16(x::Int8) = sext_int(Int16, x)
680 toInt16(x::Int16) = x
681 toInt16(x::Int32) = checked_trunc_sint(Int16, x)
682 toInt16(x::Int64) = checked_trunc_sint(Int16, x)
683 toInt16(x::Int128) = checked_trunc_sint(Int16, x)
684 toInt16(x::UInt8) = zext_int(Int16, x)
685 toInt16(x::UInt16) = bitcast(Int16, check_top_bit(Int16, x))
686 toInt16(x::UInt32) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
687 toInt16(x::UInt64) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
688 toInt16(x::UInt128) = checked_trunc_sint(Int16, check_top_bit(Int16, x))
689 toInt16(x::Bool) = and_int(zext_int(Int16, x), Int16(1))
690 toInt32(x::Int8) = sext_int(Int32, x)
691 toInt32(x::Int16) = sext_int(Int32, x)
692 toInt32(x::Int32) = x
693 toInt32(x::Int64) = checked_trunc_sint(Int32, x)
694 toInt32(x::Int128) = checked_trunc_sint(Int32, x)
695 toInt32(x::UInt8) = zext_int(Int32, x)
696 toInt32(x::UInt16) = zext_int(Int32, x)
697 toInt32(x::UInt32) = bitcast(Int32, check_top_bit(Int32, x))
698 toInt32(x::UInt64) = checked_trunc_sint(Int32, check_top_bit(Int32, x))
699 toInt32(x::UInt128) = checked_trunc_sint(Int32, check_top_bit(Int32, x))
700 toInt32(x::Bool) = and_int(zext_int(Int32, x), Int32(1))
701 toInt64(x::Int8) = sext_int(Int64, x)
702 toInt64(x::Int16) = sext_int(Int64, x)
703 toInt64(x::Int32) = sext_int(Int64, x)
704 toInt64(x::Int64) = x
705 toInt64(x::Int128) = checked_trunc_sint(Int64, x)
706 toInt64(x::UInt8) = zext_int(Int64, x)
707 toInt64(x::UInt16) = zext_int(Int64, x)
708 toInt64(x::UInt32) = zext_int(Int64, x)
709 toInt64(x::UInt64) = bitcast(Int64, check_top_bit(Int64, x))
710 toInt64(x::UInt128) = checked_trunc_sint(Int64, check_top_bit(Int64, x))
711 toInt64(x::Bool) = and_int(zext_int(Int64, x), Int64(1))
712 toInt128(x::Int8) = sext_int(Int128, x)
713 toInt128(x::Int16) = sext_int(Int128, x)
714 toInt128(x::Int32) = sext_int(Int128, x)
715 toInt128(x::Int64) = sext_int(Int128, x)
716 toInt128(x::Int128) = x
717 toInt128(x::UInt8) = zext_int(Int128, x)
718 toInt128(x::UInt16) = zext_int(Int128, x)
719 toInt128(x::UInt32) = zext_int(Int128, x)
720 toInt128(x::UInt64) = zext_int(Int128, x)
721 toInt128(x::UInt128) = bitcast(Int128, check_top_bit(Int128, x))
722 toInt128(x::Bool) = and_int(zext_int(Int128, x), Int128(1))
723 toUInt8(x::Int8) = bitcast(UInt8, check_top_bit(UInt8, x))
724 toUInt8(x::Int16) = checked_trunc_uint(UInt8, x)
725 toUInt8(x::Int32) = checked_trunc_uint(UInt8, x)
726 toUInt8(x::Int64) = checked_trunc_uint(UInt8, x)
727 toUInt8(x::Int128) = checked_trunc_uint(UInt8, x)
728 toUInt8(x::UInt8) = x
729 toUInt8(x::UInt16) = checked_trunc_uint(UInt8, x)
730 toUInt8(x::UInt32) = checked_trunc_uint(UInt8, x)
731 toUInt8(x::UInt64) = checked_trunc_uint(UInt8, x)
732 toUInt8(x::UInt128) = checked_trunc_uint(UInt8, x)
733 toUInt8(x::Bool) = and_int(bitcast(UInt8, x), UInt8(1))
734 toUInt16(x::Int8) = sext_int(UInt16, check_top_bit(UInt16, x))
735 toUInt16(x::Int16) = bitcast(UInt16, check_top_bit(UInt16, x))
736 toUInt16(x::Int32) = checked_trunc_uint(UInt16, x)
737 toUInt16(x::Int64) = checked_trunc_uint(UInt16, x)
738 toUInt16(x::Int128) = checked_trunc_uint(UInt16, x)
739 toUInt16(x::UInt8) = zext_int(UInt16, x)
740 toUInt16(x::UInt16) = x
741 toUInt16(x::UInt32) = checked_trunc_uint(UInt16, x)
742 toUInt16(x::UInt64) = checked_trunc_uint(UInt16, x)
743 toUInt16(x::UInt128) = checked_trunc_uint(UInt16, x)
744 toUInt16(x::Bool) = and_int(zext_int(UInt16, x), UInt16(1))
745 toUInt32(x::Int8) = sext_int(UInt32, check_top_bit(UInt32, x))
746 toUInt32(x::Int16) = sext_int(UInt32, check_top_bit(UInt32, x))
747 toUInt32(x::Int32) = bitcast(UInt32, check_top_bit(UInt32, x))
748 toUInt32(x::Int64) = checked_trunc_uint(UInt32, x)
749 toUInt32(x::Int128) = checked_trunc_uint(UInt32, x)
750 toUInt32(x::UInt8) = zext_int(UInt32, x)
751 toUInt32(x::UInt16) = zext_int(UInt32, x)
752 toUInt32(x::UInt32) = x
753 toUInt32(x::UInt64) = checked_trunc_uint(UInt32, x)
754 toUInt32(x::UInt128) = checked_trunc_uint(UInt32, x)
755 toUInt32(x::Bool) = and_int(zext_int(UInt32, x), UInt32(1))
756 toUInt64(x::Int8) = sext_int(UInt64, check_top_bit(UInt64, x))
757 toUInt64(x::Int16) = sext_int(UInt64, check_top_bit(UInt64, x))
758 toUInt64(x::Int32) = sext_int(UInt64, check_top_bit(UInt64, x))
759 toUInt64(x::Int64) = bitcast(UInt64, check_top_bit(UInt64, x))
760 toUInt64(x::Int128) = checked_trunc_uint(UInt64, x)
761 toUInt64(x::UInt8) = zext_int(UInt64, x)
762 toUInt64(x::UInt16) = zext_int(UInt64, x)
763 toUInt64(x::UInt32) = zext_int(UInt64, x)
764 toUInt64(x::UInt64) = x
765 toUInt64(x::UInt128) = checked_trunc_uint(UInt64, x)
766 toUInt64(x::Bool) = and_int(zext_int(UInt64, x), UInt64(1))
767 toUInt128(x::Int8) = sext_int(UInt128, check_top_bit(UInt128, x))
768 toUInt128(x::Int16) = sext_int(UInt128, check_top_bit(UInt128, x))
769 toUInt128(x::Int32) = sext_int(UInt128, check_top_bit(UInt128, x))
770 toUInt128(x::Int64) = sext_int(UInt128, check_top_bit(UInt128, x))
771 toUInt128(x::Int128) = bitcast(UInt128, check_top_bit(UInt128, x))
772 toUInt128(x::UInt8) = zext_int(UInt128, x)
773 toUInt128(x::UInt16) = zext_int(UInt128, x)
774 toUInt128(x::UInt32) = zext_int(UInt128, x)
775 toUInt128(x::UInt64) = zext_int(UInt128, x)
776 toUInt128(x::UInt128) = x
777 toUInt128(x::Bool) = and_int(zext_int(UInt128, x), UInt128(1))
778
779 # TODO: this is here to work around the 4 method limit in inference (#23210).
780 const BuiltinInts = Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8, Bool}
781 Int8(x::BuiltinInts) = toInt8(x)::Int8
782 Int16(x::BuiltinInts) = toInt16(x)::Int16
783 Int32(x::BuiltinInts) = toInt32(x)::Int32
784 Int64(x::BuiltinInts) = toInt64(x)::Int64
785 Int128(x::BuiltinInts) = toInt128(x)::Int128
786 UInt8(x::BuiltinInts) = toUInt8(x)::UInt8
787 UInt16(x::BuiltinInts) = toUInt16(x)::UInt16
788 UInt32(x::BuiltinInts) = toUInt32(x)::UInt32
789 UInt64(x::BuiltinInts) = toUInt64(x)::UInt64
790 UInt128(x::BuiltinInts) = toUInt128(x)::UInt128
791
792 (::Type{T})(x::T) where {T<:Number} = x
793
794 Int(x::Ptr) = bitcast(Int, x)
795 UInt(x::Ptr) = bitcast(UInt, x)
796 if Int === Int32
797 Int64(x::Ptr) = Int64(UInt32(x))
798 UInt64(x::Ptr) = UInt64(UInt32(x))
799 end
800 Ptr{T}(x::Union{Int,UInt,Ptr}) where {T} = bitcast(Ptr{T}, x)
801 Ptr{T}() where {T} = Ptr{T}(0)
802
803 Signed(x::UInt8) = Int8(x)
804 Unsigned(x::Int8) = UInt8(x)
805 Signed(x::UInt16) = Int16(x)
806 Unsigned(x::Int16) = UInt16(x)
807 Signed(x::UInt32) = Int32(x)
808 Unsigned(x::Int32) = UInt32(x)
809 Signed(x::UInt64) = Int64(x)
810 Unsigned(x::Int64) = UInt64(x)
811 Signed(x::UInt128) = Int128(x)
812 Unsigned(x::Int128) = UInt128(x)
813
814 Signed(x::Union{Float16, Float32, Float64, Bool}) = Int(x)
815 Unsigned(x::Union{Float16, Float32, Float64, Bool}) = UInt(x)
816
817 Integer(x::Integer) = x
818 Integer(x::Union{Float16, Float32, Float64}) = Int(x)
819
820 # Binding for the julia parser, called as
821 #
822 # Core._parse(text, filename, lineno, offset, options)
823 #
824 # Parse Julia code from the buffer `text`, starting at `offset` and attributing
825 # it to `filename`. `text` may be a `String` or `svec(ptr::Ptr{UInt8},
826 # len::Int)` for a raw unmanaged buffer. `options` should be one of `:atom`,
827 # `:statement` or `:all`, indicating how much the parser will consume.
828 #
829 # `_parse` must return an `svec` containing an `Expr` and the new offset as an
830 # `Int`.
831 #
832 # The internal jl_parse will call into Core._parse if not `nothing`.
833 _parse = nothing
834
835 _setparser!(parser) = setglobal!(Core, :_parse, parser)
836
837 # support for deprecated uses of internal _apply function
838 _apply(x...) = Core._apply_iterate(Main.Base.iterate, x...)
839
840 struct Pair{A, B}
841 first::A
842 second::B
843 # if we didn't inline this, it's probably because the callsite was actually dynamic
844 # to avoid potentially compiling many copies of this, we mark the arguments with `@nospecialize`
845 # but also mark the whole function with `@inline` to ensure we will inline it whenever possible
846 # (even if `convert(::Type{A}, a::A)` for some reason was expensive)
847 Pair(a, b) = new{typeof(a), typeof(b)}(a, b)
848 function Pair{A, B}(@nospecialize(a), @nospecialize(b)) where {A, B}
849 @inline
850 return new(a::A, b::B)
851 end
852 end
853
854 function _hasmethod(@nospecialize(tt)) # this function has a special tfunc
855 world = ccall(:jl_get_tls_world_age, UInt, ())
856 return Intrinsics.not_int(ccall(:jl_gf_invoke_lookup, Any, (Any, Any, UInt), tt, nothing, world) === nothing)
857 end
858
859 ccall(:jl_set_istopmod, Cvoid, (Any, Bool), Core, true)