1 ;;; llvm-mode.el --- Major mode for the LLVM assembler language.
3 ;; Maintainer: The LLVM team, http://llvm.org/
8 ;; Major mode for editing LLVM IR files.
12 (defvar llvm-mode-syntax-table
13 (let ((table (make-syntax-table)))
14 (modify-syntax-entry ?%
"_" table
)
15 (modify-syntax-entry ?.
"_" table
)
16 (modify-syntax-entry ?\
; "< " table)
17 (modify-syntax-entry ?
\n "> " table
)
19 "Syntax table used while in LLVM mode.")
21 (defconst llvm-mode-primitive-type-regexp
25 '("void" "half" "bfloat" "float" "double" "fp128" "x86_fp80" "ppc_fp128"
26 "x86_mmx" "x86_amx" "ptr" "type" "label" "opaque" "token") t
)
29 (defvar llvm-font-lock-keywords
33 '("alwaysinline" "argmemonly" "allocsize" "builtin" "cold" "convergent" "dereferenceable" "dereferenceable_or_null" "hot" "immarg" "inaccessiblememonly"
34 "inaccessiblemem_or_argmemonly" "inalloca" "inlinehint" "jumptable" "minsize" "mustprogress" "naked" "nobuiltin" "nonnull" "nocapture"
35 "nocallback" "nocf_check" "noduplicate" "nofree" "noimplicitfloat" "noinline" "nomerge" "nonlazybind" "noprofile" "noredzone" "noreturn"
36 "norecurse" "nosync" "noundef" "nounwind" "nosanitize_bounds" "nosanitize_coverage" "null_pointer_is_valid" "optdebug" "optforfuzzing" "optnone" "optsize" "preallocated" "readnone" "readonly" "returned" "returns_twice"
37 "shadowcallstack" "signext" "speculatable" "speculative_load_hardening" "ssp" "sspreq" "sspstrong" "safestack" "sanitize_address" "sanitize_hwaddress" "sanitize_memtag"
38 "sanitize_thread" "sanitize_memory" "strictfp" "swifterror" "uwtable" "vscale_range" "willreturn" "writeonly" "zeroext") 'symbols
) . font-lock-constant-face
)
40 '("%[-a-zA-Z$._][-a-zA-Z$._0-9]*" . font-lock-variable-name-face
)
42 '("[-a-zA-Z$._0-9]+:" . font-lock-variable-name-face
)
43 ;; Unnamed variable slots
44 '("%[-]?[0-9]+" . font-lock-variable-name-face
)
46 '("@[-a-zA-Z$._][-a-zA-Z$._0-9]*" . font-lock-function-name-face
)
48 `(,(concat "<[ \t]*\\([0-9]+\\)[ \t]*x[ \t]+"
49 llvm-mode-primitive-type-regexp
51 (1 'font-lock-type-face
)
52 (2 'font-lock-type-face
))
53 ;; Scalable vector types
54 `(,(concat "<[ \t]*\\(vscale[ \t]\\)+x[ \t]+\\([0-9]+\\)[ \t]*x[ \t]+"
55 llvm-mode-primitive-type-regexp
57 (1 'font-lock-keyword-face
)
58 (2 'font-lock-type-face
)
59 (3 'font-lock-type-face
))
61 `(,(concat "\\<" llvm-mode-primitive-type-regexp
"\\>") . font-lock-type-face
)
63 '("\\b[-]?[0-9]+\\b" . font-lock-preprocessor-face
)
64 ;; Values that can appear in a vec
65 '("\\b\\(true\\|false\\|null\\|undef\\|poison\\|none\\)\\b" . font-lock-keyword-face
)
66 ;; Floating point constants
67 '("\\b[-+]?[0-9]+.[0-9]*\\([eE][-+]?[0-9]+\\)?\\b" . font-lock-preprocessor-face
)
69 '("\\b[us]?0x[0-9A-Fa-f]+\\b" . font-lock-preprocessor-face
)
72 '(;; Toplevel entities
73 "declare" "define" "module" "target" "source_filename" "global" "constant" "const" "alias" "ifunc" "comdat"
74 "attributes" "uselistorder" "uselistorder_bb"
76 "private" "internal" "weak" "weak_odr" "linkonce" "linkonce_odr" "available_externally" "appending" "common" "extern_weak" "external"
77 "uninitialized" "implementation" "..."
79 "zeroinitializer" "c" "asm" "blockaddress"
81 ;; Calling conventions
82 "ccc" "fastcc" "coldcc" "anyregcc" "preserve_mostcc" "preserve_allcc"
83 "cxx_fast_tlscc" "swiftcc" "tailcc" "swifttailcc" "cfguard_checkcc"
85 "default" "hidden" "protected"
87 "dllimport" "dllexport"
89 "thread_local" "localdynamic" "initialexec" "localexec"
90 ;; Runtime preemption specifiers
91 "dso_preemptable" "dso_local" "dso_local_equivalent"
93 "gc" "atomic" "no_cfi" "volatile" "personality" "prologue" "section") 'symbols
) . font-lock-keyword-face
)
94 ;; Arithmetic and Logical Operators
95 `(,(regexp-opt '("add" "sub" "mul" "sdiv" "udiv" "urem" "srem" "and" "or" "xor"
96 "setne" "seteq" "setlt" "setgt" "setle" "setge") 'symbols
) . font-lock-keyword-face
)
97 ;; Floating-point operators
98 `(,(regexp-opt '("fadd" "fsub" "fneg" "fmul" "fdiv" "frem") 'symbols
) . font-lock-keyword-face
)
99 ;; Special instructions
100 `(,(regexp-opt '("phi" "tail" "call" "select" "to" "shl" "lshr" "ashr" "fcmp" "icmp" "va_arg" "landingpad" "freeze") 'symbols
) . font-lock-keyword-face
)
101 ;; Control instructions
102 `(,(regexp-opt '("ret" "br" "switch" "invoke" "resume" "unwind" "unreachable" "indirectbr" "callbr") 'symbols
) . font-lock-keyword-face
)
104 `(,(regexp-opt '("malloc" "alloca" "free" "load" "store" "getelementptr" "fence" "cmpxchg" "atomicrmw") 'symbols
) . font-lock-keyword-face
)
106 `(,(regexp-opt '("bitcast" "inttoptr" "ptrtoint" "trunc" "zext" "sext" "fptrunc" "fpext" "fptoui" "fptosi" "uitofp" "sitofp" "addrspacecast") 'symbols
) . font-lock-keyword-face
)
108 `(,(regexp-opt '("extractelement" "insertelement" "shufflevector") 'symbols
) . font-lock-keyword-face
)
110 `(,(regexp-opt '("extractvalue" "insertvalue") 'symbols
) . font-lock-keyword-face
)
112 `(,(regexp-opt '("distinct") 'symbols
) . font-lock-keyword-face
)
114 `(,(concat "#" (regexp-opt '("dbg_assign" "dbg_declare" "dbg_label" "dbg_value") 'symbols
)) . font-lock-keyword-face
)
115 ;; Atomic memory ordering constraints
116 `(,(regexp-opt '("unordered" "monotonic" "acquire" "release" "acq_rel" "seq_cst") 'symbols
) . font-lock-keyword-face
)
118 `(,(regexp-opt '("nnan" "ninf" "nsz" "arcp" "contract" "afn" "reassoc" "fast") 'symbols
) . font-lock-keyword-face
)
119 ;; Use-list order directives
120 `(,(regexp-opt '("uselistorder" "uselistorder_bb") 'symbols
) . font-lock-keyword-face
))
121 "Syntax highlighting for LLVM.")
123 (defun llvm-current-defun-name ()
124 "The `add-log-current-defun' function in LLVM mode."
127 (if (re-search-backward "^[ \t]*define[ \t]+.+[ \t]+@\\(.+\\)(.*)" nil t
)
128 (match-string-no-properties 1))))
131 (define-derived-mode llvm-mode prog-mode
"LLVM"
132 "Major mode for editing LLVM source files.
134 Runs `llvm-mode-hook' on startup."
135 (setq font-lock-defaults
`(llvm-font-lock-keywords))
136 (setq-local defun-prompt-regexp
"^[ \t]*define[ \t]+.+[ \t]+@.+(.*).+")
137 (setq-local add-log-current-defun-function
#'llvm-current-defun-name
)
138 (setq-local comment-start
";"))
140 ;; Associate .ll files with llvm-mode
142 (add-to-list 'auto-mode-alist
(cons "\\.ll\\'" 'llvm-mode
))
146 ;;; llvm-mode.el ends here