• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37606

31 Aug 2023 03:12AM UTC coverage: 86.167% (-0.03%) from 86.2%
#37606

push

local

web-flow
Refine effects based on optimizer-derived information (#50805)

The optimizer may be able to derive information that is not available to
inference. For example, it may SROA a mutable value to derive additional
constant information. Additionally, some effects, like :consistent are
path-dependent and should ideally be scanned once all optimizations are
done. Now, there is a bit of a complication that we have generally so
far taken the position that the optimizer may do non-IPO-safe
optimizations, although in practice we never actually implemented any.
This was a sensible choice, because we weren't really doing anything
with the post-optimized IR other than feeding it into codegen anyway.
However, with irinterp and this change, there's now two consumers of
IPO-safely optimized IR. I do still think we may at some point want to
run passes that allow IPO-unsafe optimizations, but we can always add
them at the end of the pipeline.

With these changes, the effect analysis is a lot more precise. For
example, we can now derive :consistent for these functions:
```
function f1(b)
    if Base.inferencebarrier(b)
        error()
    end
    return b
end

function f3(x)
    @fastmath sqrt(x)
    return x
end
```
and we can derive `:nothrow` for this function:
```
function f2()
    if Ref(false)[]
        error()
    end
    return true
end
```

414 of 414 new or added lines in 13 files covered. (100.0%)

73383 of 85164 relevant lines covered (86.17%)

12690106.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.67
/stdlib/InteractiveUtils/src/codeview.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
# highlighting settings
4
const highlighting = Dict{Symbol, Bool}(
5
    :warntype => true,
6
    :llvm => true,
7
    :native => true,
8
)
9

10
const llstyle = Dict{Symbol, Tuple{Bool, Union{Symbol, Int}}}(
11
    :default     => (false, :normal), # e.g. comma, equal sign, unknown token
12
    :comment     => (false, :light_black),
13
    :label       => (false, :light_red),
14
    :instruction => ( true, :light_cyan),
15
    :type        => (false, :cyan),
16
    :number      => (false, :yellow),
17
    :bracket     => (false, :yellow),
18
    :variable    => (false, :normal), # e.g. variable, register
19
    :keyword     => (false, :light_magenta),
20
    :funcname    => (false, :light_yellow),
21
)
22

23
function printstyled_ll(io::IO, x, s::Symbol, trailing_spaces="")
1,563✔
24
    printstyled(io, x, bold=llstyle[s][1], color=llstyle[s][2])
1,563✔
25
    print(io, trailing_spaces)
1,042✔
26
end
27

28
# displaying type warnings
29

30
function warntype_type_printer(io::IO; @nospecialize(type), used::Bool, show_type::Bool=true, _...)
580✔
31
    (show_type && used) || return nothing
383✔
32
    str = "::$type"
197✔
33
    if !highlighting[:warntype]
197✔
34
        print(io, str)
4✔
35
    elseif type isa Union && is_expected_union(type)
193✔
36
        Base.emphasize(io, str, Base.warn_color()) # more mild user notification
6✔
37
    elseif type isa Type && (!Base.isdispatchelem(type) || type == Core.Box)
325✔
38
        Base.emphasize(io, str)
7✔
39
    else
40
        Base.printstyled(io, str, color=:cyan) # show the "good" type
180✔
41
    end
42
    return nothing
197✔
43
end
44

45
# True if one can be pretty certain that the compiler handles this union well,
46
# i.e. must be small with concrete types.
47
function is_expected_union(u::Union)
14✔
48
    Base.unionlen(u) < 4 || return false
14✔
49
    for x in Base.uniontypes(u)
14✔
50
        if !Base.isdispatchelem(x) || x == Core.Box
57✔
51
            return false
3✔
52
        end
53
    end
38✔
54
    return true
11✔
55
end
56

57
"""
58
    code_warntype([io::IO], f, types; debuginfo=:default)
59

60
Prints lowered and type-inferred ASTs for the methods matching the given generic function
61
and type signature to `io` which defaults to `stdout`. The ASTs are annotated in such a way
62
as to cause "non-leaf" types which may be problematic for performance to be emphasized
63
(if color is available, displayed in red). This serves as a warning of potential type instability.
64

65
Not all non-leaf types are particularly problematic for performance, and the performance
66
characteristics of a particular type is an implementation detail of the compiler.
67
`code_warntype` will err on the side of coloring types red if they might be a performance
68
concern, so some types may be colored red even if they do not impact performance.
69
Small unions of concrete types are usually not a concern, so these are highlighted in yellow.
70

71
Keyword argument `debuginfo` may be one of `:source` or `:none` (default), to specify the verbosity of code comments.
72

73
See [`@code_warntype`](@ref man-code-warntype) for more information.
74
"""
75
function code_warntype(io::IO, @nospecialize(f), @nospecialize(t=Base.default_tt(f));
54✔
76
                       debuginfo::Symbol=:default, optimize::Bool=false, kwargs...)
77
    debuginfo = Base.IRShow.debuginfo(debuginfo)
26✔
78
    lineprinter = Base.IRShow.__debuginfo[debuginfo]
26✔
79
    for (src, rettype) in code_typed(f, t; optimize, kwargs...)
26✔
80
        if !(src isa Core.CodeInfo)
26✔
81
            println(io, src)
×
82
            println(io, "  failed to infer")
×
83
            continue
×
84
        end
85
        lambda_io::IOContext = io
26✔
86
        p = src.parent
26✔
87
        nargs::Int = 0
26✔
88
        if p isa Core.MethodInstance
26✔
89
            println(io, p)
26✔
90
            print(io, "  from ")
26✔
91
            println(io, p.def)
26✔
92
            p.def isa Method && (nargs = p.def.nargs)
52✔
93
            if !isempty(p.sparam_vals)
26✔
94
                println(io, "Static Parameters")
4✔
95
                sig = p.def.sig
4✔
96
                warn_color = Base.warn_color() # more mild user notification
4✔
97
                for i = 1:length(p.sparam_vals)
8✔
98
                    sig = sig::UnionAll
8✔
99
                    name = sig.var.name
8✔
100
                    val = p.sparam_vals[i]
8✔
101
                    print_highlighted(io::IO, v::String, color::Symbol) =
17✔
102
                        if highlighting[:warntype]
103
                            Base.printstyled(io, v; color)
9✔
104
                        else
105
                            Base.print(io, v)
×
106
                        end
107
                    if val isa TypeVar
8✔
108
                        if val.lb === Union{}
3✔
109
                            print(io, "  ", name, " <: ")
1✔
110
                            print_highlighted(io, "$(val.ub)", warn_color)
1✔
111
                        elseif val.ub === Any
2✔
112
                            print(io, "  ", sig.var.name, " >: ")
1✔
113
                            print_highlighted(io, "$(val.lb)", warn_color)
1✔
114
                        else
115
                            print(io, "  ")
1✔
116
                            print_highlighted(io, "$(val.lb)", warn_color)
1✔
117
                            print(io, " <: ", sig.var.name, " <: ")
1✔
118
                            print_highlighted(io, "$(val.ub)", warn_color)
4✔
119
                        end
120
                    elseif val isa typeof(Vararg)
5✔
121
                        print(io, "  ", name, "::")
1✔
122
                        print_highlighted(io, "Int", warn_color)
1✔
123
                    else
124
                        print(io, "  ", sig.var.name, " = ")
4✔
125
                        print_highlighted(io, "$(val)", :cyan) # show the "good" type
4✔
126
                    end
127
                    println(io)
8✔
128
                    sig = sig.body
8✔
129
                end
12✔
130
            end
131
        end
132
        if src.slotnames !== nothing
26✔
133
            slotnames = Base.sourceinfo_slotnames(src)
26✔
134
            lambda_io = IOContext(lambda_io, :SOURCE_SLOTNAMES => slotnames)
26✔
135
            slottypes = src.slottypes
26✔
136
            nargs > 0 && println(io, "Arguments")
26✔
137
            for i = 1:length(slotnames)
52✔
138
                if i == nargs + 1
75✔
139
                    println(io, "Locals")
6✔
140
                end
141
                print(io, "  ", slotnames[i])
75✔
142
                if isa(slottypes, Vector{Any})
75✔
143
                    warntype_type_printer(io; type=slottypes[i], used=true)
75✔
144
                end
145
                println(io)
75✔
146
            end
124✔
147
        end
148
        print(io, "Body")
26✔
149
        warntype_type_printer(io; type=rettype, used=true)
26✔
150
        println(io)
26✔
151
        irshow_config = Base.IRShow.IRShowConfig(lineprinter(src), warntype_type_printer)
26✔
152
        Base.IRShow.show_ir(lambda_io, src, irshow_config)
26✔
153
        println(io)
26✔
154
    end
52✔
155
    nothing
26✔
156
end
157
code_warntype(args...; kwargs...) = (@nospecialize; code_warntype(stdout, args...; kwargs...))
×
158

159
using Base: CodegenParams
160

161
const GENERIC_SIG_WARNING = "; WARNING: This code may not match what actually runs.\n"
162
const OC_MISMATCH_WARNING =
163
"""
164
; WARNING: The pre-inferred opaque closure is not callable with the given arguments
165
;          and will error on dispatch with this signature.
166
"""
167

168
# Printing code representations in IR and assembly
169

170
function _dump_function(@nospecialize(f), @nospecialize(t), native::Bool, wrapper::Bool,
125✔
171
                        raw::Bool, dump_module::Bool, syntax::Symbol,
172
                        optimize::Bool, debuginfo::Symbol, binary::Bool,
173
                        params::CodegenParams=CodegenParams(debug_info_kind=Cint(0), safepoint_on_entry=raw, gcstack_arg=raw))
174
    ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions")
125✔
175
    if isa(f, Core.Builtin)
120✔
176
        throw(ArgumentError("argument is not a generic function"))
2✔
177
    end
178
    warning = ""
118✔
179
    # get the MethodInstance for the method match
180
    if !isa(f, Core.OpaqueClosure)
118✔
181
        world = Base.get_world_counter()
116✔
182
        match = Base._which(signature_type(f, t); world)
116✔
183
        mi = Core.Compiler.specialize_method(match)
113✔
184
        # TODO: use jl_is_cacheable_sig instead of isdispatchtuple
185
        isdispatchtuple(mi.specTypes) || (warning = GENERIC_SIG_WARNING)
113✔
186
    else
187
        world = UInt64(f.world)
2✔
188
        if Core.Compiler.is_source_inferred(f.source.source)
2✔
189
            # OC was constructed from inferred source. There's only one
190
            # specialization and we can't infer anything more precise either.
191
            world = f.source.primary_world
2✔
192
            mi = f.source.specializations::Core.MethodInstance
2✔
193
            Core.Compiler.hasintersect(typeof(f).parameters[1], t) || (warning = OC_MISMATCH_WARNING)
2✔
194
        else
195
            mi = Core.Compiler.specialize_method(f.source, Tuple{typeof(f.captures), t.parameters...}, Core.svec())
×
196
            actual = isdispatchtuple(mi.specTypes)
×
197
            isdispatchtuple(mi.specTypes) || (warning = GENERIC_SIG_WARNING)
×
198
        end
199
    end
200
    # get the code for it
201
    if debuginfo === :default
115✔
202
        debuginfo = :source
74✔
203
    elseif debuginfo !== :source && debuginfo !== :none
41✔
204
        throw(ArgumentError("'debuginfo' must be either :source or :none"))
×
205
    end
206
    if native
115✔
207
        if syntax !== :att && syntax !== :intel
28✔
208
            throw(ArgumentError("'syntax' must be either :intel or :att"))
×
209
        end
210
        if dump_module
28✔
211
            # we want module metadata, so use LLVM to generate assembly output
212
            str = _dump_function_native_assembly(mi, world, wrapper, syntax, debuginfo, binary, raw, params)
25✔
213
        else
214
            # if we don't want the module metadata, just disassemble what our JIT has
215
            str = _dump_function_native_disassembly(mi, world, wrapper, syntax, debuginfo, binary)
31✔
216
        end
217
    else
218
        str = _dump_function_llvm(mi, world, wrapper, !raw, dump_module, optimize, debuginfo, params)
87✔
219
    end
220
    str = warning * str
115✔
221
    return str
115✔
222
end
223

224
function _dump_function_native_disassembly(mi::Core.MethodInstance, world::UInt,
3✔
225
                                           wrapper::Bool, syntax::Symbol,
226
                                           debuginfo::Symbol, binary::Bool)
227
    str = @ccall jl_dump_method_asm(mi::Any, world::UInt, false::Bool, wrapper::Bool,
3✔
228
                                    syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
229
                                    binary::Bool)::Ref{String}
230
    return str
3✔
231
end
232

233
struct LLVMFDump
234
    tsm::Ptr{Cvoid} # opaque
235
    f::Ptr{Cvoid} # opaque
236
end
237

238
function _dump_function_native_assembly(mi::Core.MethodInstance, world::UInt,
25✔
239
                                        wrapper::Bool, syntax::Symbol, debuginfo::Symbol,
240
                                        binary::Bool, raw::Bool, params::CodegenParams)
241
    llvmf_dump = Ref{LLVMFDump}()
25✔
242
    @ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump},mi::Any, world::UInt, wrapper::Bool,
25✔
243
                             true::Bool, params::CodegenParams)::Cvoid
244
    llvmf_dump[].f == C_NULL && error("could not compile the specified method")
25✔
245
    str = @ccall jl_dump_function_asm(llvmf_dump::Ptr{LLVMFDump}, false::Bool,
25✔
246
                                      syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
247
                                      binary::Bool, raw::Bool)::Ref{String}
248
    return str
25✔
249
end
250

251
function _dump_function_llvm(
87✔
252
        mi::Core.MethodInstance, world::UInt, wrapper::Bool,
253
        strip_ir_metadata::Bool, dump_module::Bool,
254
        optimize::Bool, debuginfo::Symbol,
255
        params::CodegenParams)
256
    llvmf_dump = Ref{LLVMFDump}()
87✔
257
    @ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump}, mi::Any, world::UInt,
87✔
258
                             wrapper::Bool, optimize::Bool, params::CodegenParams)::Cvoid
259
    llvmf_dump[].f == C_NULL && error("could not compile the specified method")
87✔
260
    str = @ccall jl_dump_function_ir(llvmf_dump::Ptr{LLVMFDump}, strip_ir_metadata::Bool,
87✔
261
                                     dump_module::Bool, debuginfo::Ptr{UInt8})::Ref{String}
262
    return str
87✔
263
end
264

265
"""
266
    code_llvm([io=stdout,], f, types; raw=false, dump_module=false, optimize=true, debuginfo=:default)
267

268
Prints the LLVM bitcodes generated for running the method matching the given generic
269
function and type signature to `io`.
270

271
If the `optimize` keyword is unset, the code will be shown before LLVM optimizations.
272
All metadata and dbg.* calls are removed from the printed bitcode. For the full IR, set the `raw` keyword to true.
273
To dump the entire module that encapsulates the function (with declarations), set the `dump_module` keyword to true.
274
Keyword argument `debuginfo` may be one of source (default) or none, to specify the verbosity of code comments.
275
"""
276
function code_llvm(io::IO, @nospecialize(f), @nospecialize(types=Base.default_tt(f));
108✔
277
                   raw::Bool=false, dump_module::Bool=false, optimize::Bool=true, debuginfo::Symbol=:default,
278
                   params::CodegenParams=CodegenParams(debug_info_kind=Cint(0), safepoint_on_entry=raw, gcstack_arg=raw))
279
    d = _dump_function(f, types, false, false, raw, dump_module, :intel, optimize, debuginfo, false, params)
53✔
280
    if highlighting[:llvm] && get(io, :color, false)::Bool
49✔
281
        print_llvm(io, d)
1✔
282
    else
283
        print(io, d)
48✔
284
    end
285
end
286
code_llvm(args...; kwargs...) = (@nospecialize; code_llvm(stdout, args...; kwargs...))
12✔
287

288
"""
289
    code_native([io=stdout,], f, types; syntax=:intel, debuginfo=:default, binary=false, dump_module=true)
290

291
Prints the native assembly instructions generated for running the method matching the given
292
generic function and type signature to `io`.
293

294
* Set assembly syntax by setting `syntax` to `:intel` (default) for intel syntax or `:att` for AT&T syntax.
295
* Specify verbosity of code comments by setting `debuginfo` to `:source` (default) or `:none`.
296
* If `binary` is `true`, also print the binary machine code for each instruction precedented by an abbreviated address.
297
* If `dump_module` is `false`, do not print metadata such as rodata or directives.
298
* If `raw` is `false`, uninteresting instructions (like the safepoint function prologue) are elided.
299

300
See also: [`@code_native`](@ref), [`code_llvm`](@ref), [`code_typed`](@ref) and [`code_lowered`](@ref)
301
"""
302
function code_native(io::IO, @nospecialize(f), @nospecialize(types=Base.default_tt(f));
68✔
303
                     dump_module::Bool=true, syntax::Symbol=:intel, raw::Bool=false,
304
                     debuginfo::Symbol=:default, binary::Bool=false,
305
                     params::CodegenParams=CodegenParams(debug_info_kind=Cint(0), safepoint_on_entry=raw, gcstack_arg=raw))
306
    d = _dump_function(f, types, true, false, raw, dump_module, syntax, true, debuginfo, binary, params)
33✔
307
    if highlighting[:native] && get(io, :color, false)::Bool
28✔
308
        print_native(io, d)
1✔
309
    else
310
        print(io, d)
27✔
311
    end
312
end
313
code_native(args...; kwargs...) = (@nospecialize; code_native(stdout, args...; kwargs...))
14✔
314

315
## colorized IR and assembly printing
316

317
const num_regex = r"^(?:\$?-?\d+|0x[0-9A-Fa-f]+|-?(?:\d+\.?\d*|\.\d+)(?:[eE][+-]?\d+)?)$"
318

319
function print_llvm(io::IO, code::String)
34✔
320
    buf = IOBuffer(code)
34✔
321
    for line in eachline(buf)
68✔
322
        m = match(r"^(\s*)((?:[^;]|;\")*)(.*)$", line)
43✔
323
        m === nothing && continue
43✔
324
        indent, tokens, comment = m.captures
43✔
325
        print(io, indent)
86✔
326
        print_llvm_tokens(io, tokens)
86✔
327
        printstyled_ll(io, comment, :comment)
86✔
328
        println(io)
43✔
329
    end
43✔
330
end
331

332
const llvm_types =
333
    r"^(?:void|half|float|double|x86_\w+|ppc_\w+|label|metadata|type|opaque|token|i\d+)$"
334
const llvm_cond = r"^(?:[ou]?eq|[ou]?ne|[uso][gl][te]|ord|uno)$" # true|false
335

336
function print_llvm_tokens(io, tokens)
43✔
337
    m = match(r"^((?:[^\s:]+:)?)(\s*)(.*)", tokens)
43✔
338
    if m !== nothing
43✔
339
        label, spaces, tokens = m.captures
43✔
340
        printstyled_ll(io, label, :label, spaces)
86✔
341
    end
342
    m = match(r"^(%[^\s=]+)(\s*)=(\s*)(.*)", tokens)
43✔
343
    if m !== nothing
43✔
344
        result, spaces, spaces2, tokens = m.captures
21✔
345
        printstyled_ll(io, result, :variable, spaces)
42✔
346
        printstyled_ll(io, '=', :default, spaces2)
42✔
347
    end
348
    m = match(r"^([a-z]\w*)(\s*)(.*)", tokens)
43✔
349
    if m !== nothing
43✔
350
        inst, spaces, tokens = m.captures
34✔
351
        iskeyword = occursin(r"^(?:define|declare|type)$", inst) || occursin("=", tokens)
64✔
352
        printstyled_ll(io, inst, iskeyword ? :keyword : :instruction, spaces)
68✔
353
    end
354

355
    print_llvm_operands(io, tokens)
43✔
356
end
357

358
function print_llvm_operands(io, tokens)
67✔
359
    while !isempty(tokens)
240✔
360
        tokens = print_llvm_operand(io, tokens)
212✔
361
    end
106✔
362
    return tokens
67✔
363
end
364

365
function print_llvm_operand(io, tokens)
106✔
366
    islabel = false
106✔
367
    while !isempty(tokens)
640✔
368
        m = match(r"^,(\s*)(.*)", tokens)
269✔
369
        if m !== nothing
269✔
370
            spaces, tokens = m.captures
31✔
371
            printstyled_ll(io, ',', :default, spaces)
62✔
372
            break
31✔
373
        end
374
        m = match(r"^(\*+|=)(\s*)(.*)", tokens)
238✔
375
        if m !== nothing
238✔
376
            sym, spaces, tokens = m.captures
20✔
377
            printstyled_ll(io, sym, :default, spaces)
40✔
378
            continue
20✔
379
        end
380
        m = match(r"^(\"[^\"]*\")(\s*)(.*)", tokens)
218✔
381
        if m !== nothing
218✔
382
            str, spaces, tokens = m.captures
3✔
383
            printstyled_ll(io, str, :variable, spaces)
6✔
384
            continue
3✔
385
        end
386
        m = match(r"^([({\[<])(\s*)(.*)", tokens)
215✔
387
        if m !== nothing
215✔
388
            bracket, spaces, tokens = m.captures
24✔
389
            printstyled_ll(io, bracket, :bracket, spaces)
48✔
390
            tokens = print_llvm_operands(io, tokens) # enter
48✔
391
            continue
24✔
392
        end
393
        m = match(r"^([)}\]>])(\s*)(.*)", tokens)
191✔
394
        if m !== nothing
191✔
395
            bracket, spaces, tokens = m.captures
24✔
396
            printstyled_ll(io, bracket, :bracket, spaces)
48✔
397
            break # leave
24✔
398
        end
399

400
        m = match(r"^([^\s,*=(){}\[\]<>]+)(\s*)(.*)", tokens)
167✔
401
        m === nothing && break
167✔
402
        token, spaces, tokens = m.captures
167✔
403
        if occursin(llvm_types, token)
167✔
404
            printstyled_ll(io, token, :type)
55✔
405
            islabel = token == "label"
106✔
406
        elseif occursin(llvm_cond, token) # condition code is instruction-level
112✔
407
            printstyled_ll(io, token, :instruction)
1✔
408
        elseif occursin(num_regex, token)
111✔
409
            printstyled_ll(io, token, :number)
29✔
410
        elseif occursin(r"^@.+$", token)
82✔
411
            printstyled_ll(io, token, :funcname)
5✔
412
        elseif occursin(r"^%.+$", token)
77✔
413
            islabel |= occursin(r"^%[^\d].*$", token) & occursin(r"^\]", tokens)
38✔
414
            printstyled_ll(io, token, islabel ? :label : :variable)
38✔
415
            islabel = false
38✔
416
        elseif occursin(r"^[a-z]\w+$", token)
39✔
417
            printstyled_ll(io, token, :keyword)
34✔
418
        else
419
            printstyled_ll(io, token, :default)
5✔
420
        end
421
        print(io, spaces)
167✔
422
    end
214✔
423
    return tokens
106✔
424
end
425

426
function print_native(io::IO, code::String, arch::Symbol=sys_arch_category())
59✔
427
    archv = Val(arch)
59✔
428
    buf = IOBuffer(code)
58✔
429
    for line in eachline(buf)
116✔
430
        m = match(r"^(\s*)((?:[^;#/]|#\S|;\"|/[^/])*)(.*)$", line)
96✔
431
        m === nothing && continue
96✔
432
        indent, tokens, comment = m.captures
96✔
433
        print(io, indent)
192✔
434
        print_native_tokens(io, tokens, archv)
96✔
435
        printstyled_ll(io, comment, :comment)
192✔
436
        println(io)
96✔
437
    end
96✔
438
end
439

440
function sys_arch_category()
1✔
441
    if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
1✔
442
        :x86
1✔
443
    elseif Sys.ARCH === :aarch64 || startswith(string(Sys.ARCH), "arm")
×
444
        :arm
×
445
    else
446
        :unsupported
×
447
    end
448
end
449

450
print_native_tokens(io, line, ::Val) = print(io, line)
1✔
451

452
const x86_ptr = r"^(?:(?:[xyz]mm|[dq])?word|byte|ptr|offset)$"
453
const avx512flags = r"^(?:z|r[nduz]-sae|sae|1to1?\d)$"
454
const arm_cond = r"^(?:eq|ne|cs|ho|cc|lo|mi|pl|vs|vc|hi|ls|[lg][te]|al|nv)$"
455
const arm_keywords = r"^(?:lsl|lsr|asr|ror|rrx|!|/[zm])$"
456

457
function print_native_tokens(io, tokens, arch::Union{Val{:x86}, Val{:arm}})
95✔
458
    x86 = arch isa Val{:x86}
95✔
459
    m = match(r"^((?:[^\s:]+:|\"[^\"]+\":)?)(\s*)(.*)", tokens)
95✔
460
    if m !== nothing
95✔
461
        label, spaces, tokens = m.captures
95✔
462
        printstyled_ll(io, label, :label, spaces)
190✔
463
    end
464
    haslabel = false
95✔
465
    m = match(r"^([a-z][\w.]*)(\s*)(.*)", tokens)
95✔
466
    if m !== nothing
95✔
467
        instruction, spaces, tokens = m.captures
57✔
468
        printstyled_ll(io, instruction, :instruction, spaces)
114✔
469
        haslabel = occursin(r"^(?:bl?|bl?\.\w{2,5}|[ct]bn?z)?$", instruction)
57✔
470
    end
471

472
    isfuncname = false
95✔
473
    while !isempty(tokens)
896✔
474
        m = match(r"^([,:*])(\s*)(.*)", tokens)
353✔
475
        if m !== nothing
353✔
476
            sym, spaces, tokens = m.captures
86✔
477
            printstyled_ll(io, sym, :default, spaces)
172✔
478
            isfuncname = false
86✔
479
            continue
86✔
480
        end
481
        m = match(r"^([(){}\[\]])(\s*)(.*)", tokens)
267✔
482
        if m !== nothing
267✔
483
            bracket, spaces, tokens = m.captures
56✔
484
            printstyled_ll(io, bracket, :bracket, spaces)
112✔
485
            continue
56✔
486
        end
487
        m = match(r"^#([0-9a-fx.-]+)(\s*)(.*)", tokens)
211✔
488
        if !x86 && m !== nothing && occursin(num_regex, m.captures[1])
211✔
489
            num, spaces, tokens = m.captures
6✔
490
            printstyled_ll(io, "#" * num, :number, spaces)
12✔
491
            continue
6✔
492
        end
493

494
        m = match(r"^([^\s,:*(){}\[\]][^\s,:*/(){}\[\]]*)(\s*)(.*)", tokens)
205✔
495
        m === nothing && break
205✔
496
        token, spaces, tokens = m.captures
205✔
497
        if occursin(num_regex, token)
205✔
498
            printstyled_ll(io, token, :number)
20✔
499
        elseif x86 && occursin(x86_ptr, token) || occursin(avx512flags, token)
310✔
500
            printstyled_ll(io, token, :keyword)
22✔
501
            isfuncname = token == "offset"
22✔
502
        elseif !x86 && (occursin(arm_keywords, token) || occursin(arm_cond, token))
202✔
503
            printstyled_ll(io, token, :keyword)
7✔
504
        elseif occursin(r"^L.+$", token)
156✔
505
            printstyled_ll(io, token, :label)
5✔
506
        elseif occursin(r"^\$.+$", token)
151✔
507
            printstyled_ll(io, token, :funcname)
2✔
508
        elseif occursin(r"^%?(?:[a-z][\w.]+|\"[^\"]+\")$", token)
149✔
509
            islabel = haslabel & !occursin(',', tokens)
188✔
510
            printstyled_ll(io, token, islabel ? :label : isfuncname ? :funcname : :variable)
228✔
511
            isfuncname = false
114✔
512
        else
513
            printstyled_ll(io, token, :default)
35✔
514
        end
515
        print(io, spaces)
205✔
516
    end
448✔
517
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc