xref: /llvm-project/llvm/utils/emacs/llvm-mode.el (revision 8ab88f11a12aaecb46f7b0eb5c13e7802258c1e1)
1;;; llvm-mode.el --- Major mode for the LLVM assembler language.
2
3;; Maintainer:  The LLVM team, http://llvm.org/
4;; Version: 1.0
5
6;;; Commentary:
7
8;; Major mode for editing LLVM IR files.
9
10;;; Code:
11
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)
18    table)
19  "Syntax table used while in LLVM mode.")
20
21(defconst llvm-mode-primitive-type-regexp
22  (concat
23   "\\(i[0-9]+\\|"
24   (regexp-opt
25    '("void" "half" "bfloat" "float" "double" "fp128" "x86_fp80" "ppc_fp128"
26      "x86_mmx" "x86_amx" "ptr" "type" "label" "opaque" "token") t)
27   "\\)"))
28
29(defvar llvm-font-lock-keywords
30  (list
31   ;; Attributes
32   `(,(regexp-opt
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" "noext" "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)
39   ;; Variables
40   '("%[-a-zA-Z$._][-a-zA-Z$._0-9]*" . font-lock-variable-name-face)
41   ;; Labels
42   '("[-a-zA-Z$._0-9]+:" . font-lock-variable-name-face)
43   ;; Unnamed variable slots
44   '("%[-]?[0-9]+" . font-lock-variable-name-face)
45   ;; Function names
46   '("@[-a-zA-Z$._][-a-zA-Z$._0-9]*" . font-lock-function-name-face)
47   ;; Fixed vector types
48   `(,(concat "<[ \t]*\\([0-9]+\\)[ \t]*x[ \t]+"
49	      llvm-mode-primitive-type-regexp
50	      "[ \t]*>")
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
56	      "[ \t]*>")
57     (1 'font-lock-keyword-face)
58     (2 'font-lock-type-face)
59     (3 'font-lock-type-face))
60   ;; Primitive types
61   `(,(concat "\\<" llvm-mode-primitive-type-regexp "\\>") . font-lock-type-face)
62   ;; Integer literals
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)
68   ;; Hex constants
69   '("\\b[us]?0x[0-9A-Fa-f]+\\b" . font-lock-preprocessor-face)
70   ;; Keywords
71   `(,(regexp-opt
72       '(;; Toplevel entities
73         "declare" "define" "module" "target" "source_filename" "global" "constant" "const" "alias" "ifunc" "comdat"
74         "attributes" "uselistorder" "uselistorder_bb"
75         ;; Linkage types
76         "private" "internal" "weak" "weak_odr" "linkonce" "linkonce_odr" "available_externally" "appending" "common" "extern_weak" "external"
77         "uninitialized" "implementation" "..."
78         ;; Values
79         "zeroinitializer" "c" "asm" "blockaddress"
80
81         ;; Calling conventions
82         "ccc" "fastcc" "coldcc" "anyregcc" "preserve_mostcc" "preserve_allcc"
83         "cxx_fast_tlscc" "swiftcc" "tailcc" "swifttailcc" "cfguard_checkcc"
84         ;; Visibility styles
85         "default" "hidden" "protected"
86         ;; DLL storages
87         "dllimport" "dllexport"
88         ;; Thread local
89         "thread_local" "localdynamic" "initialexec" "localexec"
90         ;; Runtime preemption specifiers
91         "dso_preemptable" "dso_local" "dso_local_equivalent"
92
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)
103   ;; Memory operators
104   `(,(regexp-opt '("malloc" "alloca" "free" "load" "store" "getelementptr" "fence" "cmpxchg" "atomicrmw") 'symbols) . font-lock-keyword-face)
105   ;; Casts
106   `(,(regexp-opt '("bitcast" "inttoptr" "ptrtoint" "trunc" "zext" "sext" "fptrunc" "fpext" "fptoui" "fptosi" "uitofp" "sitofp" "addrspacecast") 'symbols) . font-lock-keyword-face)
107   ;; Vector ops
108   `(,(regexp-opt '("extractelement" "insertelement" "shufflevector") 'symbols) . font-lock-keyword-face)
109   ;; Aggregate ops
110   `(,(regexp-opt '("extractvalue" "insertvalue") 'symbols) . font-lock-keyword-face)
111   ;; Metadata types
112   `(,(regexp-opt '("distinct") 'symbols) . font-lock-keyword-face)
113   ;; Debug records
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)
117   ;; Fast-math flags
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.")
122
123(defun llvm-current-defun-name ()
124  "The `add-log-current-defun' function in LLVM mode."
125  (save-excursion
126    (end-of-line)
127    (if (re-search-backward "^[ \t]*define[ \t]+.+[ \t]+@\\(.+\\)(.*)" nil t)
128	(match-string-no-properties 1))))
129
130;;;###autoload
131(define-derived-mode llvm-mode prog-mode "LLVM"
132  "Major mode for editing LLVM source files.
133\\{llvm-mode-map}
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 ";"))
139
140;; Associate .ll files with llvm-mode
141;;;###autoload
142(add-to-list 'auto-mode-alist (cons "\\.ll\\'" 'llvm-mode))
143
144(provide 'llvm-mode)
145
146;;; llvm-mode.el ends here
147