macro sayhello(name)
return :( println("Hello, ", $name) )
end
@sayhello (macro with 1 method)
@sayhello "Andy"
Hello, Andy
Ref. official docs
@generated function foo(x)
Core.println(x)
return :(x * x)
end
foo (generic function with 1 method)
foo(5)
Int64
25
foo("5")
String
"55"
Ref. official docs
Julia functions are generic functions.
A generic function is a single function and consists of many methods.
The methods of a function is stored in a method table.
All objetcs in Julia are callable.
@generated function foo(::Array{T,N}, ...) where {T,N}
...
x = Matrix{T}()
for i = 1:N
...
end
end
foo(x, y) = x + y # Source code
foo (generic function with 2 methods)
expr = :(foo(1, 2))
:(foo(1, 2))
dump(expr) # AST
Expr head: Symbol call args: Array{Any}((3,)) 1: Symbol foo 2: Int64 1 3: Int64 2
@code_lowered foo(1, 2) # Julia IR
CodeInfo( 1 ─ %1 = x + y └── return %1 )
@code_typed foo(1, 2) # Typed Julia IR
CodeInfo( 1 ─ %1 = (Base.add_int)(x, y)::Int64 └── return %1 ) => Int64
@code_llvm foo(1, 2) # LLVM IR
; @ In[6]:1 within `foo' define i64 @julia_foo_12964(i64, i64) { top: ; ┌ @ int.jl:53 within `+' %2 = add i64 %1, %0 ; └ ret i64 %2 }
@code_native foo(1, 2) # Assembly
.text ; ┌ @ In[6]:1 within `foo' ; │┌ @ In[6]:1 within `+' leaq (%rdi,%rsi), %rax ; │└ retq nopw %cs:(%rax,%rax) ; └
f(x, y)
typeof(f).name.mt
Tuple{typeof(f), typeof(x), typeof(y)}
Ref. Function calls
typeof(sum)
typeof(sum)
typeof(sum).name
typeof(sum)
typeof(typeof(sum).name)
Core.TypeName
typeof(typeof(sum).name.mt)
Core.MethodTable
typeof(sum).name.mt
parse
: text -> Expr (femotolisp)macroexpand
@code_lowered
methods
@code_typed
@code_llvm
@code_native