├── .gitignore └── cuda-mode.el /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | *.elc -------------------------------------------------------------------------------- /cuda-mode.el: -------------------------------------------------------------------------------- 1 | ;;; cuda-mode.el --- NVIDIA CUDA Major Mode derived from C++-mode. -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2014 Jack Morrison 4 | 5 | ;; Author: Jack Morrison 6 | ;; URL: https://github.com/chachi/cuda-mode 7 | ;; Keywords: c, languages, cuda 8 | ;; Version: 0.1 9 | 10 | ;; Package-Requires: ((compat "29")) 11 | 12 | ;; This program is free software; you can redistribute it and/or modify 13 | ;; it under the terms of the GNU General Public License as published by 14 | ;; the Free Software Foundation, either version 3 of the License, or 15 | ;; (at your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | ;; GNU General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with this program. If not, see . 24 | 25 | ;;; Commentary: 26 | 27 | ;; Originally found on EmacsWiki @ http://www.emacswiki.org/emacs/CudaMode 28 | 29 | ;;; Code: 30 | 31 | (require 'compat) 32 | 33 | (require 'cc-mode) 34 | 35 | ;; These are only required at compile time to get the sources for the 36 | ;; language constants. (The cc-fonts require and the font-lock 37 | ;; related constants could additionally be put inside an 38 | ;; (eval-after-load "font-lock" ...) but then some trickery is 39 | ;; necessary to get them compiled.) 40 | (eval-when-compile 41 | (require 'cc-langs) 42 | (require 'cc-fonts)) 43 | 44 | 45 | (eval-and-compile 46 | ;; Make our mode known to the language constant system. Use C 47 | ;; mode as the fallback for the constants we don't change here. 48 | ;; This needs to be done also at compile time since the language 49 | ;; constants are evaluated then. 50 | (c-add-language 'cuda-mode 'c++-mode)) 51 | 52 | ;; cuda has no boolean but a string and a vector type. 53 | (c-lang-defconst c-primitive-type-kwds 54 | "Primitive type keywords. As opposed to the other keyword lists, the 55 | keywords listed here are fontified with the type face instead of the 56 | keyword face. 57 | 58 | If any of these also are on `c-type-list-kwds', `c-ref-list-kwds', 59 | `c-colon-type-list-kwds', `c-paren-nontype-kwds', `c-paren-type-kwds', 60 | `c-<>-type-kwds', or `c-<>-arglist-kwds' then the associated clauses 61 | will be handled. 62 | 63 | Do not try to modify this list for end user customizations; the 64 | `*-font-lock-extra-types' variable, where `*' is the mode prefix, is 65 | the appropriate place for that." 66 | cuda 67 | (append 68 | '("dim3" 69 | "char1" "uchar1" "char2" "uchar2" "char3" "uchar3" "char4" "uchar4" 70 | "short1" "ushort1" "short2" "ushort2" "short3" "ushort3" "short4" "ushort4" 71 | "int1" "uint1" "int2" "uint2" "int3" "uint3" "int4" "uint4" 72 | "long1" "ulong1" "long2" "ulong2" "long3" "ulong3" "long4" "ulong4" 73 | "float1" "float2" "float3" "float4" 74 | "double1" "double2" ) 75 | (c-lang-const c-primitive-type-kwds c++))) 76 | 77 | (c-lang-defconst c-modifier-kwds 78 | cuda (append 79 | '("__host__" "__device__" "__global__") 80 | (c-lang-const c-modifier-kwds c++))) 81 | 82 | (c-lang-defconst c-type-modifier-prefix-kwds 83 | cuda (append 84 | '("__device__" "__constant__" "__shared__" "__grid_constant__" "__managed__" "__restrict__") 85 | (c-lang-const c-type-modifier-prefix-kwds c++))) 86 | 87 | (c-lang-defconst c-other-op-syntax-tokens 88 | "List of the tokens made up of characters in the punctuation or 89 | parenthesis syntax classes that have uses other than as expression 90 | operators." 91 | cuda (append 92 | '("<<<" ">>>") 93 | (c-lang-const c-other-op-syntax-tokens c++))) 94 | 95 | (c-lang-defconst c-primary-expr-kwds 96 | "Keywords besides constants and operators that start primary expressions." 97 | cuda '("gridDim" "blockIdx" "blockDim" "threadIdx" "warpSize")) 98 | 99 | (eval-and-compile ;; required by cc-mode 100 | (defvar cuda-builtins 101 | '(;; atom 102 | "atomicAdd" 103 | "atomicAnd" 104 | "atomicCAS" 105 | "atomicDec" 106 | "atomicExch" 107 | "atomicInc" 108 | "atomicMax" 109 | "atomicMin" 110 | "atomicOr" 111 | "atomicSub" 112 | "atomicXor" 113 | ;; Address space predicate functions 114 | "__isGlobal" "__isShared" "__isConstant" "__isGridConstant" "__isLocal" 115 | ;; dev 116 | "tex1D" 117 | "tex1Dfetch" 118 | "tex2D" 119 | "__float_as_int" 120 | "__int_as_float" 121 | "__float2int_rn" 122 | "__float2int_rz" 123 | "__float2int_ru" 124 | "__float2int_rd" 125 | "__float2uint_rn" 126 | "__float2uint_rz" 127 | "__float2uint_ru" 128 | "__float2uint_rd" 129 | "__int2float_rn" 130 | "__int2float_rz" 131 | "__int2float_ru" 132 | "__int2float_rd" 133 | "__uint2float_rn" 134 | "__uint2float_rz" 135 | "__uint2float_ru" 136 | "__uint2float_rd" 137 | "__fadd_rz" 138 | "__fmul_rz" 139 | "__fdividef" 140 | "__mul24" 141 | "__umul24" 142 | "__mulhi" 143 | "__umulhi" 144 | "__mul64hi" 145 | "__umul64hi" 146 | "min" 147 | "umin" 148 | "fminf" 149 | "fmin" 150 | "max" 151 | "umax" 152 | "fmaxf" 153 | "fmax" 154 | "abs" 155 | "fabsf" 156 | "fabs" 157 | "sqrtf" 158 | "sqrt" 159 | "sinf" 160 | "__sinf" 161 | "sin" 162 | "cosf" 163 | "__cosf" 164 | "cos" 165 | "sincosf" 166 | "__sincosf" 167 | "expf" 168 | "__expf" 169 | "exp" 170 | "logf" 171 | "__logf" 172 | "log" 173 | ;; runtime 174 | "cudaBindTexture" 175 | "cudaBindTextureToArray" 176 | "cudaChooseDevice" 177 | "cudaConfigureCall" 178 | "cudaCreateChannelDesc" 179 | "cudaD3D10GetDevice" 180 | "cudaD3D10MapResources" 181 | "cudaD3D10RegisterResource" 182 | "cudaD3D10ResourceGetMappedArray" 183 | "cudaD3D10ResourceGetMappedPitch" 184 | "cudaD3D10ResourceGetMappedPointer" 185 | "cudaD3D10ResourceGetMappedSize" 186 | "cudaD3D10ResourceGetSurfaceDimensions" 187 | "cudaD3D10ResourceSetMapFlags" 188 | "cudaD3D10SetDirect3DDevice" 189 | "cudaD3D10UnmapResources" 190 | "cudaD3D10UnregisterResource" 191 | "cudaD3D9GetDevice" 192 | "cudaD3D9GetDirect3DDevice" 193 | "cudaD3D9MapResources" 194 | "cudaD3D9RegisterResource" 195 | "cudaD3D9ResourceGetMappedArray" 196 | "cudaD3D9ResourceGetMappedPitch" 197 | "cudaD3D9ResourceGetMappedPointer" 198 | "cudaD3D9ResourceGetMappedSize" 199 | "cudaD3D9ResourceGetSurfaceDimensions" 200 | "cudaD3D9ResourceSetMapFlags" 201 | "cudaD3D9SetDirect3DDevice" 202 | "cudaD3D9UnmapResources" 203 | "cudaD3D9UnregisterResource" 204 | "cudaEventCreate" 205 | "cudaEventDestroy" 206 | "cudaEventElapsedTime" 207 | "cudaEventQuery" 208 | "cudaEventRecord" 209 | "cudaEventSynchronize" 210 | "cudaFree" 211 | "cudaFreeArray" 212 | "cudaFreeHost " 213 | "cudaGetChannelDesc" 214 | "cudaGetDevice" 215 | "cudaGetDeviceCount" 216 | "cudaGetDeviceProperties" 217 | "cudaGetErrorString" 218 | "cudaGetLastError" 219 | "cudaGetSymbolAddress" 220 | "cudaGetSymbolSize" 221 | "cudaGetTextureAlignmentOffset" 222 | "cudaGetTextureReference" 223 | "cudaGLMapBufferObject" 224 | "cudaGLRegisterBufferObject" 225 | "cudaGLSetGLDevice" 226 | "cudaGLUnmapBufferObject" 227 | "cudaGLUnregisterBufferObject" 228 | "cudaLaunch" 229 | "cudaMalloc" 230 | "cudaMalloc3D" 231 | "cudaMalloc3DArray" 232 | "cudaMallocArray" 233 | "cudaMallocHost" 234 | "cudaMallocPitch" 235 | "cudaMemcpy" 236 | "cudaMemcpy2D" 237 | "cudaMemcpy2DArrayToArray" 238 | "cudaMemcpy2DFromArray" 239 | "cudaMemcpy2DToArray" 240 | "cudaMemcpy3D" 241 | "cudaMemcpyArrayToArray" 242 | "cudaMemcpyFromArray" 243 | "cudaMemcpyFromSymbol" 244 | "cudaMemcpyToArray" 245 | "cudaMemcpyToSymbol" 246 | "cudaMemset" 247 | "cudaMemset2D" 248 | "cudaMemset3D" 249 | "cudaSetDevice" 250 | "cudaSetupArgument" 251 | "cudaStreamCreate" 252 | "cudaStreamDestroy" 253 | "cudaStreamQuery" 254 | "cudaStreamSynchronize" 255 | "cudaThreadExit" 256 | "cudaThreadSynchronize" 257 | "cudaUnbindTexture" 258 | ;; other 259 | "__syncthreads" 260 | ;; warp functions (CUDA 9) 261 | "__shfl_sync" "__shfl_up_sync" "__shfl_down_sync" "__shfl_xor_sync" 262 | "__all_sync" "__any_sync" "__uni_sync" "__ballot_sync" 263 | "__match_any_sync" "__match_all_sync" "__activemask" "__syncwarp" 264 | ;; Some numeric common ops 265 | "__ffs" "__fns" "__popc" "__clz" "__brev" 266 | ) 267 | "Names of built-in cuda functions.")) 268 | 269 | 270 | (c-lang-defconst c-other-kwds 271 | cuda `,(append 272 | (c-lang-const c-other-kwds c++) 273 | cuda-builtins)) 274 | 275 | (defconst cuda-font-lock-keywords-1 276 | (c-lang-const c-matchers-1 cuda) 277 | "Minimal highlighting for CUDA mode.") 278 | 279 | (defconst cuda-font-lock-keywords-2 280 | (c-lang-const c-matchers-2 cuda) 281 | "Fast normal highlighting for CUDA mode.") 282 | 283 | (defconst cuda-font-lock-keywords-3 284 | (c-lang-const c-matchers-3 cuda) 285 | "Accurate normal highlighting for CUDA mode.") 286 | 287 | (defvar cuda-font-lock-keywords cuda-font-lock-keywords-3 288 | "Default expressions to highlight in CUDA mode.") 289 | 290 | (defvar cuda-mode-syntax-table nil 291 | "Syntax table used in cuda-mode buffers.") 292 | (or cuda-mode-syntax-table 293 | (setq cuda-mode-syntax-table 294 | (funcall (c-lang-const c-make-mode-syntax-table cuda)))) 295 | 296 | (defvar cuda-mode-abbrev-table nil 297 | "Abbreviation table used in cuda-mode buffers.") 298 | 299 | (c-define-abbrev-table 'cuda-mode-abbrev-table 300 | ;; Keywords that if they occur first on a line might alter the 301 | ;; syntactic context, and which therefore should trig reindentation 302 | ;; when they are completed. 303 | '(("else" "else" c-electric-continued-statement 0) 304 | ("while" "while" c-electric-continued-statement 0))) 305 | 306 | (defvar-keymap cuda-mode-map 307 | :parent c++-mode-map 308 | :doc "Cuda keymap inherited from C++") 309 | 310 | ;;;###autoload 311 | (add-to-list 'auto-mode-alist '("\\.cu[h]?\\'" . cuda-mode)) 312 | 313 | (defun cuda-completion-function () 314 | "Generate completion list for primitive capf support." 315 | (when-let ((is-cuda (eq major-mode 'cuda-mode)) ;; only work in cuda-mode 316 | (bounds (bounds-of-thing-at-point 'symbol))) 317 | (list (car bounds) 318 | (cdr bounds) 319 | cuda-builtins 320 | :exclusive 'no))) 321 | 322 | ;;;###autoload 323 | (define-derived-mode cuda-mode c++-mode "Cuda" 324 | "Major mode for editing Cuda code. 325 | This mode derives from C++ mode. 326 | Key bindings: 327 | \\{ccuda-mode-map}" 328 | :after-hook (progn (c-make-noise-macro-regexps) 329 | (c-make-macro-with-semi-re) 330 | (c-update-modeline)) 331 | (c-initialize-cc-mode t) 332 | (setq abbrev-mode t) 333 | (c-init-language-vars cuda-mode) 334 | (c-common-init 'cuda-mode) 335 | (cc-imenu-init cc-imenu-c++-generic-expression) 336 | (add-hook 'flymake-diagnostic-functions 'flymake-cc nil t) 337 | (add-hook 'completion-at-point-functions #'cuda-completion-function) 338 | (c-run-mode-hooks 'c-mode-common-hook)) 339 | 340 | 341 | (provide 'cuda-mode) 342 | ;;; cuda-mode.el ends here 343 | --------------------------------------------------------------------------------