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

JuliaLang / julia / #37551

pending completion
#37551

push

local

web-flow
Improve error when indexing is interpreted as a typed comprehension (#49939)

* Improve errors for typed_hcat by adding a special error for indexing that gets resolved as a typed comprehension.

* Add a test for issue #49676

1 of 1 new or added line in 1 file covered. (100.0%)

71890 of 83662 relevant lines covered (85.93%)

34589844.48 hits per line

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

95.41
/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,467✔
24
    printstyled(io, x, bold=llstyle[s][1], color=llstyle[s][2])
1,467✔
25
    print(io, trailing_spaces)
983✔
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(@nospecialize(f), @nospecialize(t=Base.default_tt(f)); kwargs...) =
×
158
    code_warntype(stdout, f, t; kwargs...)
159

160
import Base.CodegenParams
161

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

169
# Printing code representations in IR and assembly
170

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

232
function _dump_function_native_disassembly(mi::Core.MethodInstance, world::UInt,
3✔
233
                                           wrapper::Bool, syntax::Symbol,
234
                                           debuginfo::Symbol, binary::Bool)
235
    str = @ccall jl_dump_method_asm(mi::Any, world::UInt, false::Bool, wrapper::Bool,
3✔
236
                                    syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
237
                                    binary::Bool)::Ref{String}
238
    return str
3✔
239
end
240

241
struct LLVMFDump
242
    tsm::Ptr{Cvoid} # opaque
243
    f::Ptr{Cvoid} # opaque
244
end
245

246
function _dump_function_native_assembly(mi::Core.MethodInstance, world::UInt,
25✔
247
                                        wrapper::Bool, syntax::Symbol, debuginfo::Symbol,
248
                                        binary::Bool, raw::Bool, params::CodegenParams)
249
    llvmf_dump = Ref{LLVMFDump}()
25✔
250
    @ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump},mi::Any, world::UInt, wrapper::Bool,
25✔
251
                             true::Bool, params::CodegenParams)::Cvoid
252
    llvmf_dump[].f == C_NULL && error("could not compile the specified method")
25✔
253
    str = @ccall jl_dump_function_asm(llvmf_dump::Ptr{LLVMFDump}, false::Bool,
25✔
254
                                      syntax::Ptr{UInt8}, debuginfo::Ptr{UInt8},
255
                                      binary::Bool, raw::Bool)::Ref{String}
256
    return str
25✔
257
end
258

259
function _dump_function_llvm(
87✔
260
        mi::Core.MethodInstance, world::UInt, wrapper::Bool,
261
        strip_ir_metadata::Bool, dump_module::Bool,
262
        optimize::Bool, debuginfo::Symbol,
263
        params::CodegenParams)
264
    llvmf_dump = Ref{LLVMFDump}()
87✔
265
    @ccall jl_get_llvmf_defn(llvmf_dump::Ptr{LLVMFDump}, mi::Any, world::UInt,
87✔
266
                             wrapper::Bool, optimize::Bool, params::CodegenParams)::Cvoid
267
    llvmf_dump[].f == C_NULL && error("could not compile the specified method")
87✔
268
    str = @ccall jl_dump_function_ir(llvmf_dump::Ptr{LLVMFDump}, strip_ir_metadata::Bool,
87✔
269
                                     dump_module::Bool, debuginfo::Ptr{UInt8})::Ref{String}
270
    return str
87✔
271
end
272

273
"""
274
    code_llvm([io=stdout,], f, types; raw=false, dump_module=false, optimize=true, debuginfo=:default)
275

276
Prints the LLVM bitcodes generated for running the method matching the given generic
277
function and type signature to `io`.
278

279
If the `optimize` keyword is unset, the code will be shown before LLVM optimizations.
280
All metadata and dbg.* calls are removed from the printed bitcode. For the full IR, set the `raw` keyword to true.
281
To dump the entire module that encapsulates the function (with declarations), set the `dump_module` keyword to true.
282
Keyword argument `debuginfo` may be one of source (default) or none, to specify the verbosity of code comments.
283
"""
284
function code_llvm(io::IO, @nospecialize(f), @nospecialize(types), raw::Bool,
53✔
285
                   dump_module::Bool=false, optimize::Bool=true, debuginfo::Symbol=:default)
286
    d = _dump_function(f, types, false, false, raw, dump_module, :intel, optimize, debuginfo, false)
53✔
287
    if highlighting[:llvm] && get(io, :color, false)::Bool
49✔
288
        print_llvm(io, d)
1✔
289
    else
290
        print(io, d)
48✔
291
    end
292
end
293
code_llvm(io::IO, @nospecialize(f), @nospecialize(types=Base.default_tt(f)); raw::Bool=false, dump_module::Bool=false, optimize::Bool=true, debuginfo::Symbol=:default) =
108✔
294
    code_llvm(io, f, types, raw, dump_module, optimize, debuginfo)
295
code_llvm(@nospecialize(f), @nospecialize(types=Base.default_tt(f)); raw=false, dump_module=false, optimize=true, debuginfo::Symbol=:default) =
12✔
296
    code_llvm(stdout, f, types; raw, dump_module, optimize, debuginfo)
297

298
"""
299
    code_native([io=stdout,], f, types; syntax=:intel, debuginfo=:default, binary=false, dump_module=true)
300

301
Prints the native assembly instructions generated for running the method matching the given
302
generic function and type signature to `io`.
303

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

310
See also: [`@code_native`](@ref), [`code_llvm`](@ref), [`code_typed`](@ref) and [`code_lowered`](@ref)
311
"""
312
function code_native(io::IO, @nospecialize(f), @nospecialize(types=Base.default_tt(f));
68✔
313
                     dump_module::Bool=true, syntax::Symbol=:intel, raw::Bool=false,
314
                     debuginfo::Symbol=:default, binary::Bool=false)
315
    d = _dump_function(f, types, true, false, raw, dump_module, syntax, true, debuginfo, binary)
33✔
316
    if highlighting[:native] && get(io, :color, false)::Bool
28✔
317
        print_native(io, d)
1✔
318
    else
319
        print(io, d)
27✔
320
    end
321
end
322
code_native(@nospecialize(f), @nospecialize(types=Base.default_tt(f)); dump_module::Bool=true, syntax::Symbol=:intel, raw::Bool=false, debuginfo::Symbol=:default, binary::Bool=false) =
14✔
323
    code_native(stdout, f, types; dump_module, syntax, raw, debuginfo, binary)
324
code_native(::IO, ::Any, ::Symbol) = error("invalid code_native call") # resolve ambiguous call
×
325

326
## colorized IR and assembly printing
327

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

330
function print_llvm(io::IO, code::String)
34✔
331
    buf = IOBuffer(code)
34✔
332
    for line in eachline(buf)
68✔
333
        m = match(r"^(\s*)((?:[^;]|;\")*)(.*)$", line)
42✔
334
        m === nothing && continue
42✔
335
        indent, tokens, comment = m.captures
42✔
336
        print(io, indent)
84✔
337
        print_llvm_tokens(io, tokens)
84✔
338
        printstyled_ll(io, comment, :comment)
84✔
339
        println(io)
42✔
340
    end
42✔
341
end
342

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

347
function print_llvm_tokens(io, tokens)
42✔
348
    m = match(r"^((?:[^\s:]+:)?)(\s*)(.*)", tokens)
42✔
349
    if m !== nothing
42✔
350
        label, spaces, tokens = m.captures
42✔
351
        printstyled_ll(io, label, :label, spaces)
84✔
352
    end
353
    m = match(r"^(%[^\s=]+)(\s*)=(\s*)(.*)", tokens)
42✔
354
    if m !== nothing
42✔
355
        result, spaces, spaces2, tokens = m.captures
21✔
356
        printstyled_ll(io, result, :variable, spaces)
42✔
357
        printstyled_ll(io, '=', :default, spaces2)
42✔
358
    end
359
    m = match(r"^([a-z]\w*)(\s*)(.*)", tokens)
42✔
360
    if m !== nothing
42✔
361
        inst, spaces, tokens = m.captures
34✔
362
        iskeyword = occursin(r"^(?:define|declare|type)$", inst) || occursin("=", tokens)
64✔
363
        printstyled_ll(io, inst, iskeyword ? :keyword : :instruction, spaces)
68✔
364
    end
365

366
    print_llvm_operands(io, tokens)
42✔
367
end
368

369
function print_llvm_operands(io, tokens)
66✔
370
    while !isempty(tokens)
238✔
371
        tokens = print_llvm_operand(io, tokens)
212✔
372
    end
106✔
373
    return tokens
66✔
374
end
375

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

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

437
function print_native(io::IO, code::String, arch::Symbol=sys_arch_category())
59✔
438
    archv = Val(arch)
59✔
439
    buf = IOBuffer(code)
58✔
440
    for line in eachline(buf)
116✔
441
        m = match(r"^(\s*)((?:[^;#/]|#\S|;\"|/[^/])*)(.*)$", line)
83✔
442
        m === nothing && continue
83✔
443
        indent, tokens, comment = m.captures
83✔
444
        print(io, indent)
166✔
445
        print_native_tokens(io, tokens, archv)
83✔
446
        printstyled_ll(io, comment, :comment)
166✔
447
        println(io)
83✔
448
    end
83✔
449
end
450

451
function sys_arch_category()
1✔
452
    if Sys.ARCH === :x86_64 || Sys.ARCH === :i686
1✔
453
        :x86
1✔
454
    elseif Sys.ARCH === :aarch64 || startswith(string(Sys.ARCH), "arm")
×
455
        :arm
×
456
    else
457
        :unsupported
×
458
    end
459
end
460

461
print_native_tokens(io, line, ::Val) = print(io, line)
1✔
462

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

468
function print_native_tokens(io, tokens, arch::Union{Val{:x86}, Val{:arm}})
82✔
469
    x86 = arch isa Val{:x86}
82✔
470
    m = match(r"^((?:[^\s:]+:|\"[^\"]+\":)?)(\s*)(.*)", tokens)
82✔
471
    if m !== nothing
82✔
472
        label, spaces, tokens = m.captures
82✔
473
        printstyled_ll(io, label, :label, spaces)
164✔
474
    end
475
    haslabel = false
82✔
476
    m = match(r"^([a-z][\w.]*)(\s*)(.*)", tokens)
82✔
477
    if m !== nothing
82✔
478
        instruction, spaces, tokens = m.captures
56✔
479
        printstyled_ll(io, instruction, :instruction, spaces)
112✔
480
        haslabel = occursin(r"^(?:bl?|bl?\.\w{2,5}|[ct]bn?z)?$", instruction)
56✔
481
    end
482

483
    isfuncname = false
82✔
484
    while !isempty(tokens)
810✔
485
        m = match(r"^([,:*])(\s*)(.*)", tokens)
323✔
486
        if m !== nothing
323✔
487
            sym, spaces, tokens = m.captures
79✔
488
            printstyled_ll(io, sym, :default, spaces)
158✔
489
            isfuncname = false
79✔
490
            continue
79✔
491
        end
492
        m = match(r"^([(){}\[\]])(\s*)(.*)", tokens)
244✔
493
        if m !== nothing
244✔
494
            bracket, spaces, tokens = m.captures
56✔
495
            printstyled_ll(io, bracket, :bracket, spaces)
112✔
496
            continue
56✔
497
        end
498
        m = match(r"^#([0-9a-fx.-]+)(\s*)(.*)", tokens)
188✔
499
        if !x86 && m !== nothing && occursin(num_regex, m.captures[1])
188✔
500
            num, spaces, tokens = m.captures
6✔
501
            printstyled_ll(io, "#" * num, :number, spaces)
12✔
502
            continue
6✔
503
        end
504

505
        m = match(r"^([^\s,:*(){}\[\]][^\s,:*/(){}\[\]]*)(\s*)(.*)", tokens)
182✔
506
        m === nothing && break
182✔
507
        token, spaces, tokens = m.captures
182✔
508
        if occursin(num_regex, token)
182✔
509
            printstyled_ll(io, token, :number)
16✔
510
        elseif x86 && occursin(x86_ptr, token) || occursin(avx512flags, token)
272✔
511
            printstyled_ll(io, token, :keyword)
22✔
512
            isfuncname = token == "offset"
22✔
513
        elseif !x86 && (occursin(arm_keywords, token) || occursin(arm_cond, token))
183✔
514
            printstyled_ll(io, token, :keyword)
7✔
515
        elseif occursin(r"^L.+$", token)
137✔
516
            printstyled_ll(io, token, :label)
5✔
517
        elseif occursin(r"^\$.+$", token)
132✔
518
            printstyled_ll(io, token, :funcname)
1✔
519
        elseif occursin(r"^%?(?:[a-z][\w.]+|\"[^\"]+\")$", token)
131✔
520
            islabel = haslabel & !occursin(',', tokens)
177✔
521
            printstyled_ll(io, token, islabel ? :label : isfuncname ? :funcname : :variable)
216✔
522
            isfuncname = false
108✔
523
        else
524
            printstyled_ll(io, token, :default)
23✔
525
        end
526
        print(io, spaces)
182✔
527
    end
405✔
528
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

© 2026 Coveralls, Inc