file-attribute-collect: New defun
[emacs.git] / lisp / progmodes / verilog-mode.el
blobfd2e96af48b572643010af9c23d7a8bdcf0ee26e
1 ;;; verilog-mode.el --- major mode for editing verilog source in Emacs
3 ;; Copyright (C) 1996-2016 Free Software Foundation, Inc.
5 ;; Author: Michael McNamara <mac@verilog.com>
6 ;; Wilson Snyder <wsnyder@wsnyder.org>
7 ;; http://www.verilog.com
8 ;; http://www.veripool.org
9 ;; Created: 3 Jan 1996
10 ;; Keywords: languages
12 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
13 ;; file on 19/3/2008, and the maintainer agreed that when a bug is
14 ;; filed in the Emacs bug reporting system against this file, a copy
15 ;; of the bug report be sent to the maintainer's email address.
17 ;; This code supports Emacs 21.1 and later
18 ;; And XEmacs 21.1 and later
19 ;; Please do not make changes that break Emacs 21. Thanks!
23 ;; This file is part of GNU Emacs.
25 ;; GNU Emacs is free software: you can redistribute it and/or modify
26 ;; it under the terms of the GNU General Public License as published by
27 ;; the Free Software Foundation, either version 3 of the License, or
28 ;; (at your option) any later version.
30 ;; GNU Emacs is distributed in the hope that it will be useful,
31 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 ;; GNU General Public License for more details.
35 ;; You should have received a copy of the GNU General Public License
36 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
38 ;;; Commentary:
40 ;; USAGE
41 ;; =====
43 ;; A major mode for editing Verilog and SystemVerilog HDL source code (IEEE
44 ;; 1364-2005 and IEEE 1800-2012 standards). When you have entered Verilog
45 ;; mode, you may get more info by pressing C-h m. You may also get online
46 ;; help describing various functions by: C-h f <Name of function you want
47 ;; described>
49 ;; KNOWN BUGS / BUG REPORTS
50 ;; =======================
52 ;; SystemVerilog is a rapidly evolving language, and hence this mode is
53 ;; under continuous development. Please report any issues to the issue
54 ;; tracker at
56 ;; http://www.veripool.org/verilog-mode
58 ;; Please use verilog-submit-bug-report to submit a report; type C-c
59 ;; C-b to invoke this and as a result we will have a much easier time
60 ;; of reproducing the bug you find, and hence fixing it.
62 ;; INSTALLING THE MODE
63 ;; ===================
65 ;; An older version of this mode may be already installed as a part of
66 ;; your environment, and one method of updating would be to update
67 ;; your Emacs environment. Sometimes this is difficult for local
68 ;; political/control reasons, and hence you can always install a
69 ;; private copy (or even a shared copy) which overrides the system
70 ;; default.
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
75 ;; The short list of installation instructions are: To set up
76 ;; automatic Verilog mode, put this file in your load path, and put
77 ;; the following in code (please un comment it first!) in your
78 ;; .emacs, or in your site's site-load.el
80 ;; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ;; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
83 ;; Be sure to examine at the help for verilog-auto, and the other
84 ;; verilog-auto-* functions for some major coding time savers.
86 ;; If you want to customize Verilog mode to fit your needs better,
87 ;; you may add the below lines (the values of the variables presented
88 ;; here are the defaults). Note also that if you use an Emacs that
89 ;; supports custom, it's probably better to use the custom menu to
90 ;; edit these. If working as a member of a large team these settings
91 ;; should be common across all users (in a site-start file), or set
92 ;; in Local Variables in every file. Otherwise, different people's
93 ;; AUTO expansion may result different whitespace changes.
95 ;; ;; Enable syntax highlighting of **all** languages
96 ;; (global-font-lock-mode t)
98 ;; ;; User customization for Verilog mode
99 ;; (setq verilog-indent-level 3
100 ;; verilog-indent-level-module 3
101 ;; verilog-indent-level-declaration 3
102 ;; verilog-indent-level-behavioral 3
103 ;; verilog-indent-level-directive 1
104 ;; verilog-case-indent 2
105 ;; verilog-auto-newline t
106 ;; verilog-auto-indent-on-newline t
107 ;; verilog-tab-always-indent t
108 ;; verilog-auto-endcomments t
109 ;; verilog-minimum-comment-distance 40
110 ;; verilog-indent-begin-after-if t
111 ;; verilog-auto-lineup 'declarations
112 ;; verilog-highlight-p1800-keywords nil
113 ;; verilog-linter "my_lint_shell_command"
114 ;; )
117 ;;; History:
119 ;; See commit history at http://www.veripool.org/verilog-mode.html
120 ;; (This section is required to appease checkdoc.)
122 ;;; Code:
125 ;; This variable will always hold the version number of the mode
126 (defconst verilog-mode-version "2016-03-22-7547e76-vpo-GNU"
127 "Version of this Verilog mode.")
128 (defconst verilog-mode-release-emacs t
129 "If non-nil, this version of Verilog mode was released with Emacs itself.")
131 (defun verilog-version ()
132 "Inform caller of the version of this file."
133 (interactive)
134 (message "Using verilog-mode version %s" verilog-mode-version))
136 ;; Insure we have certain packages, and deal with it if we don't
137 ;; Be sure to note which Emacs flavor and version added each feature.
138 (eval-when-compile
139 ;; Provide stuff if we are XEmacs
140 (when (featurep 'xemacs)
141 (condition-case nil
142 (require 'easymenu)
143 (error nil))
144 (condition-case nil
145 (require 'regexp-opt)
146 (error nil))
147 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
148 (condition-case nil
149 (load "skeleton")
150 (error nil))
151 (condition-case nil
152 (if (fboundp 'when)
153 nil ; fab
154 (defmacro when (cond &rest body)
155 (list 'if cond (cons 'progn body))))
156 (error nil))
157 (condition-case nil
158 (if (fboundp 'unless)
159 nil ; fab
160 (defmacro unless (cond &rest body)
161 (cons 'if (cons cond (cons nil body)))))
162 (error nil))
163 (condition-case nil
164 (if (fboundp 'store-match-data)
165 nil ; fab
166 (defmacro store-match-data (&rest _args) nil))
167 (error nil))
168 (condition-case nil
169 (if (fboundp 'char-before)
170 nil ; great
171 (defmacro char-before (&rest _body)
172 (char-after (1- (point)))))
173 (error nil))
174 (condition-case nil
175 (if (fboundp 'when)
176 nil ; fab
177 (defsubst point-at-bol (&optional N)
178 (save-excursion (beginning-of-line N) (point))))
179 (error nil))
180 (condition-case nil
181 (if (fboundp 'when)
182 nil ; fab
183 (defsubst point-at-eol (&optional N)
184 (save-excursion (end-of-line N) (point))))
185 (error nil))
186 (condition-case nil
187 (require 'custom)
188 (error nil))
189 (condition-case nil
190 (if (fboundp 'match-string-no-properties)
191 nil ; great
192 (defsubst match-string-no-properties (num &optional string)
193 "Return string of text matched by last search, without text properties.
194 NUM specifies which parenthesized expression in the last regexp.
195 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
196 Zero means the entire text matched by the whole regexp or whole string.
197 STRING should be given if the last search was by `string-match' on STRING."
198 (if (match-beginning num)
199 (if string
200 (let ((result
201 (substring string
202 (match-beginning num) (match-end num))))
203 (set-text-properties 0 (length result) nil result)
204 result)
205 (buffer-substring-no-properties (match-beginning num)
206 (match-end num)
207 (current-buffer)))))
209 (error nil))
210 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
211 nil ; We've got what we needed
212 ;; We have the old custom-library, hack around it!
213 (defmacro defgroup (&rest _args) nil)
214 (defmacro customize (&rest _args)
215 (message
216 "Sorry, Customize is not available with this version of Emacs"))
217 (defmacro defcustom (var value doc &rest _args)
218 `(defvar ,var ,value ,doc))
220 (if (fboundp 'defface)
221 nil ; great!
222 (defmacro defface (var values doc &rest _args)
223 `(make-face ,var))
226 (if (and (featurep 'custom) (fboundp 'customize-group))
227 nil ; We've got what we needed
228 ;; We have an intermediate custom-library, hack around it!
229 (defmacro customize-group (var &rest _args)
230 `(customize ,var))
233 (defvar inhibit-modification-hooks)
234 (defvar inhibit-point-motion-hooks)
235 (defvar deactivate-mark)
238 ;; OK, do this stuff if we are NOT XEmacs:
239 (unless (featurep 'xemacs)
240 (unless (fboundp 'region-active-p)
241 (defmacro region-active-p ()
242 `(and transient-mark-mode mark-active))))
245 ;; Provide a regular expression optimization routine, using regexp-opt
246 ;; if provided by the user's elisp libraries
247 (eval-and-compile
248 ;; The below were disabled when GNU Emacs 22 was released;
249 ;; perhaps some still need to be there to support Emacs 21.
250 (if (featurep 'xemacs)
251 (if (fboundp 'regexp-opt)
252 ;; regexp-opt is defined, does it take 3 or 2 arguments?
253 (if (fboundp 'function-max-args)
254 (let ((args (function-max-args `regexp-opt)))
255 (cond
256 ((eq args 3) ; It takes 3
257 (condition-case nil ; Hide this defun from emacses
258 ; with just a two input regexp
259 (defun verilog-regexp-opt (a b)
260 "Deal with differing number of required arguments for `regexp-opt'.
261 Call `regexp-opt' on A and B."
262 (regexp-opt a b t))
263 (error nil))
265 ((eq args 2) ; It takes 2
266 (defun verilog-regexp-opt (a b)
267 "Call `regexp-opt' on A and B."
268 (regexp-opt a b))
270 (t nil)))
271 ;; We can't tell; assume it takes 2
272 (defun verilog-regexp-opt (a b)
273 "Call `regexp-opt' on A and B."
274 (regexp-opt a b))
276 ;; There is no regexp-opt, provide our own
277 (defun verilog-regexp-opt (strings &optional paren _shy)
278 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
279 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
281 ;; Emacs.
282 (defalias 'verilog-regexp-opt 'regexp-opt)))
284 ;; emacs >=22 has looking-back, but older emacs and xemacs don't.
285 ;; This function is lifted directly from emacs's subr.el
286 ;; so that it can be used by xemacs.
287 ;; The idea for this was borrowed from org-mode via this link:
288 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2009-12/msg00032.html
289 (eval-and-compile
290 (cond
291 ((fboundp 'looking-back)
292 (defalias 'verilog-looking-back 'looking-back))
294 (defun verilog-looking-back (regexp limit &optional greedy)
295 "Return non-nil if text before point matches regular expression REGEXP.
296 Like `looking-at' except matches before point, and is slower.
297 LIMIT if non-nil speeds up the search by specifying a minimum
298 starting position, to avoid checking matches that would start
299 before LIMIT.
301 If GREEDY is non-nil, extend the match backwards as far as
302 possible, stopping when a single additional previous character
303 cannot be part of a match for REGEXP. When the match is
304 extended, its starting position is allowed to occur before
305 LIMIT.
307 As a general recommendation, try to avoid using `looking-back'
308 wherever possible, since it is slow."
309 (let ((start (point))
310 (pos
311 (save-excursion
312 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
313 (point)))))
314 (if (and greedy pos)
315 (save-restriction
316 (narrow-to-region (point-min) start)
317 (while (and (> pos (point-min))
318 (save-excursion
319 (goto-char pos)
320 (backward-char 1)
321 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
322 (setq pos (1- pos)))
323 (save-excursion
324 (goto-char pos)
325 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
326 (not (null pos)))))))
328 (eval-and-compile
329 (cond
330 ((fboundp 'restore-buffer-modified-p)
331 ;; Faster, as does not update mode line when nothing changes
332 (defalias 'verilog-restore-buffer-modified-p 'restore-buffer-modified-p))
334 (defalias 'verilog-restore-buffer-modified-p 'set-buffer-modified-p))))
336 (eval-and-compile
337 ;; Both xemacs and emacs
338 (condition-case nil
339 (require 'diff) ; diff-command and diff-switches
340 (error nil))
341 (condition-case nil
342 (require 'compile) ; compilation-error-regexp-alist-alist
343 (error nil))
344 (condition-case nil
345 (unless (fboundp 'buffer-chars-modified-tick) ; Emacs 22 added
346 (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
347 (error nil))
348 ;; Added in Emacs 24.1
349 (condition-case nil
350 (unless (fboundp 'prog-mode)
351 (define-derived-mode prog-mode fundamental-mode "Prog"))
352 (error nil))
353 ;; Added in Emacs 25.1
354 (condition-case nil
355 (unless (fboundp 'forward-word-strictly)
356 (defalias 'forward-word-strictly 'forward-word))
357 (error nil)))
359 (eval-when-compile
360 (defun verilog-regexp-words (a)
361 "Call `regexp-opt' with word delimiters for the words A."
362 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
363 (defun verilog-regexp-words (a)
364 "Call `regexp-opt' with word delimiters for the words A."
365 ;; The FAQ references this function, so user LISP sometimes calls it
366 (concat "\\<" (verilog-regexp-opt a t) "\\>"))
368 (defun verilog-easy-menu-filter (menu)
369 "Filter `easy-menu-define' MENU to support new features."
370 (cond ((not (featurep 'xemacs))
371 menu) ; GNU Emacs - passthru
372 ;; XEmacs doesn't support :help. Strip it.
373 ;; Recursively filter the a submenu
374 ((listp menu)
375 (mapcar 'verilog-easy-menu-filter menu))
376 ;; Look for [:help "blah"] and remove
377 ((vectorp menu)
378 (let ((i 0) (out []))
379 (while (< i (length menu))
380 (if (equal `:help (aref menu i))
381 (setq i (+ 2 i))
382 (setq out (vconcat out (vector (aref menu i)))
383 i (1+ i))))
384 out))
385 (t menu))) ; Default - ok
386 ;;(verilog-easy-menu-filter
387 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
388 ;; "----" ["MB" nil :help "Help MB"]))
390 (defun verilog-define-abbrev (table name expansion &optional hook)
391 "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
392 Provides SYSTEM-FLAG in newer Emacs."
393 (condition-case nil
394 (define-abbrev table name expansion hook 0 t)
395 (error
396 (define-abbrev table name expansion hook))))
398 (defun verilog-customize ()
399 "Customize variables and other settings used by Verilog-Mode."
400 (interactive)
401 (customize-group 'verilog-mode))
403 (defun verilog-font-customize ()
404 "Customize fonts used by Verilog-Mode."
405 (interactive)
406 (if (fboundp 'customize-apropos)
407 (customize-apropos "font-lock-*" 'faces)))
409 (defun verilog-booleanp (value)
410 "Return t if VALUE is boolean.
411 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
412 This function may be removed when Emacs 21 is no longer supported."
413 (or (equal value t) (equal value nil)))
415 (defun verilog-insert-last-command-event ()
416 "Insert the `last-command-event'."
417 (insert (if (featurep 'xemacs)
418 ;; XEmacs 21.5 doesn't like last-command-event
419 last-command-char
420 ;; And GNU Emacs 22 has obsoleted last-command-char
421 last-command-event)))
423 (defvar verilog-no-change-functions nil
424 "True if `after-change-functions' is disabled.
425 Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
427 (defvar verilog-in-hooks nil
428 "True when within a `verilog-run-hooks' block.")
430 (defmacro verilog-run-hooks (&rest hooks)
431 "Run each hook in HOOKS using `run-hooks'.
432 Set `verilog-in-hooks' during this time, to assist AUTO caches."
433 `(let ((verilog-in-hooks t))
434 (run-hooks ,@hooks)))
436 (defun verilog-syntax-ppss (&optional pos)
437 (when verilog-no-change-functions
438 (if verilog-in-hooks
439 (verilog-scan-cache-flush)
440 ;; else don't let the AUTO code itself get away with flushing the cache,
441 ;; as that'll make things very slow
442 (backtrace)
443 (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
444 (verilog-point-text))))
445 (if (fboundp 'syntax-ppss)
446 (syntax-ppss pos)
447 (parse-partial-sexp (point-min) (or pos (point)))))
449 (defgroup verilog-mode nil
450 "Major mode for Verilog source code."
451 :version "22.2"
452 :group 'languages)
454 ;; (defgroup verilog-mode-fonts nil
455 ;; "Facilitates easy customization fonts used in Verilog source text"
456 ;; :link '(customize-apropos "font-lock-*" 'faces)
457 ;; :group 'verilog-mode)
459 (defgroup verilog-mode-indent nil
460 "Customize indentation and highlighting of Verilog source text."
461 :group 'verilog-mode)
463 (defgroup verilog-mode-actions nil
464 "Customize actions on Verilog source text."
465 :group 'verilog-mode)
467 (defgroup verilog-mode-auto nil
468 "Customize AUTO actions when expanding Verilog source text."
469 :group 'verilog-mode)
471 (defvar verilog-debug nil
472 "Non-nil means enable debug messages for `verilog-mode' internals.")
474 (defvar verilog-warn-fatal nil
475 "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
477 (defcustom verilog-linter
478 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
479 "Unix program and arguments to call to run a lint checker on Verilog source.
480 Depending on the `verilog-set-compile-command', this may be invoked when
481 you type \\[compile]. When the compile completes, \\[next-error] will take
482 you to the next lint error."
483 :type 'string
484 :group 'verilog-mode-actions)
485 ;; We don't mark it safe, as it's used as a shell command
487 (defcustom verilog-coverage
488 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
489 "Program and arguments to use to annotate for coverage Verilog source.
490 Depending on the `verilog-set-compile-command', this may be invoked when
491 you type \\[compile]. When the compile completes, \\[next-error] will take
492 you to the next lint error."
493 :type 'string
494 :group 'verilog-mode-actions)
495 ;; We don't mark it safe, as it's used as a shell command
497 (defcustom verilog-simulator
498 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
499 "Program and arguments to use to interpret Verilog source.
500 Depending on the `verilog-set-compile-command', this may be invoked when
501 you type \\[compile]. When the compile completes, \\[next-error] will take
502 you to the next lint error."
503 :type 'string
504 :group 'verilog-mode-actions)
505 ;; We don't mark it safe, as it's used as a shell command
507 (defcustom verilog-compiler
508 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
509 "Program and arguments to use to compile Verilog source.
510 Depending on the `verilog-set-compile-command', this may be invoked when
511 you type \\[compile]. When the compile completes, \\[next-error] will take
512 you to the next lint error."
513 :type 'string
514 :group 'verilog-mode-actions)
515 ;; We don't mark it safe, as it's used as a shell command
517 (defcustom verilog-preprocessor
518 ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
519 "vppreproc __FLAGS__ __FILE__"
520 "Program and arguments to use to preprocess Verilog source.
521 This is invoked with `verilog-preprocess', and depending on the
522 `verilog-set-compile-command', may also be invoked when you type
523 \\[compile]. When the compile completes, \\[next-error] will
524 take you to the next lint error."
525 :type 'string
526 :group 'verilog-mode-actions)
527 ;; We don't mark it safe, as it's used as a shell command
529 (defvar verilog-preprocess-history nil
530 "History for `verilog-preprocess'.")
532 (defvar verilog-tool 'verilog-linter
533 "Which tool to use for building compiler-command.
534 Either nil, `verilog-linter', `verilog-compiler',
535 `verilog-coverage', `verilog-preprocessor', or `verilog-simulator'.
536 Alternatively use the \"Choose Compilation Action\" menu. See
537 `verilog-set-compile-command' for more information.")
539 (defcustom verilog-highlight-translate-off nil
540 "Non-nil means background-highlight code excluded from translation.
541 That is, all code between \"// synopsys translate_off\" and
542 \"// synopsys translate_on\" is highlighted using a different background color
543 \(face `verilog-font-lock-translate-off-face').
545 Note: This will slow down on-the-fly fontification (and thus editing).
547 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
548 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
549 :type 'boolean
550 :group 'verilog-mode-indent)
551 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
552 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
554 (defcustom verilog-auto-lineup 'declarations
555 "Type of statements to lineup across multiple lines.
556 If `all' is selected, then all line ups described below are done.
558 If `declarations', then just declarations are lined up with any
559 preceding declarations, taking into account widths and the like,
560 so or example the code:
561 reg [31:0] a;
562 reg b;
563 would become
564 reg [31:0] a;
565 reg b;
567 If `assignment', then assignments are lined up with any preceding
568 assignments, so for example the code
569 a_long_variable <= b + c;
570 d = e + f;
571 would become
572 a_long_variable <= b + c;
573 d = e + f;
575 In order to speed up editing, large blocks of statements are lined up
576 only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
577 are lineup only when \\[verilog-pretty-declarations] is typed."
579 :type '(radio (const :tag "Line up Assignments and Declarations" all)
580 (const :tag "Line up Assignment statements" assignments )
581 (const :tag "Line up Declarations" declarations)
582 (function :tag "Other"))
583 :group 'verilog-mode-indent )
584 (put 'verilog-auto-lineup 'safe-local-variable
585 '(lambda (x) (memq x '(nil all assignments declarations))))
587 (defcustom verilog-indent-level 3
588 "Indentation of Verilog statements with respect to containing block."
589 :group 'verilog-mode-indent
590 :type 'integer)
591 (put 'verilog-indent-level 'safe-local-variable 'integerp)
593 (defcustom verilog-indent-level-module 3
594 "Indentation of Module level Verilog statements (eg always, initial).
595 Set to 0 to get initial and always statements lined up on the left side of
596 your screen."
597 :group 'verilog-mode-indent
598 :type 'integer)
599 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
601 (defcustom verilog-indent-level-declaration 3
602 "Indentation of declarations with respect to containing block.
603 Set to 0 to get them list right under containing block."
604 :group 'verilog-mode-indent
605 :type 'integer)
606 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
608 (defcustom verilog-indent-declaration-macros nil
609 "How to treat macro expansions in a declaration.
610 If nil, indent as:
611 input [31:0] a;
612 input \\=`CP;
613 output c;
614 If non nil, treat as:
615 input [31:0] a;
616 input \\=`CP ;
617 output c;"
618 :group 'verilog-mode-indent
619 :type 'boolean)
620 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
622 (defcustom verilog-indent-lists t
623 "How to treat indenting items in a list.
624 If t (the default), indent as:
625 always @( posedge a or
626 reset ) begin
628 If nil, treat as:
629 always @( posedge a or
630 reset ) begin"
631 :group 'verilog-mode-indent
632 :type 'boolean)
633 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
635 (defcustom verilog-indent-level-behavioral 3
636 "Absolute indentation of first begin in a task or function block.
637 Set to 0 to get such code to start at the left side of the screen."
638 :group 'verilog-mode-indent
639 :type 'integer)
640 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
642 (defcustom verilog-indent-level-directive 1
643 "Indentation to add to each level of \\=`ifdef declarations.
644 Set to 0 to have all directives start at the left side of the screen."
645 :group 'verilog-mode-indent
646 :type 'integer)
647 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
649 (defcustom verilog-cexp-indent 2
650 "Indentation of Verilog statements split across lines."
651 :group 'verilog-mode-indent
652 :type 'integer)
653 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
655 (defcustom verilog-case-indent 2
656 "Indentation for case statements."
657 :group 'verilog-mode-indent
658 :type 'integer)
659 (put 'verilog-case-indent 'safe-local-variable 'integerp)
661 (defcustom verilog-auto-newline t
662 "Non-nil means automatically newline after semicolons."
663 :group 'verilog-mode-indent
664 :type 'boolean)
665 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
667 (defcustom verilog-auto-indent-on-newline t
668 "Non-nil means automatically indent line after newline."
669 :group 'verilog-mode-indent
670 :type 'boolean)
671 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
673 (defcustom verilog-tab-always-indent t
674 "Non-nil means TAB should always re-indent the current line.
675 A nil value means TAB will only reindent when at the beginning of the line."
676 :group 'verilog-mode-indent
677 :type 'boolean)
678 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
680 (defcustom verilog-tab-to-comment nil
681 "Non-nil means TAB moves to the right hand column in preparation for a comment."
682 :group 'verilog-mode-actions
683 :type 'boolean)
684 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
686 (defcustom verilog-indent-begin-after-if t
687 "Non-nil means indent begin statements following if, else, while, etc.
688 Otherwise, line them up."
689 :group 'verilog-mode-indent
690 :type 'boolean)
691 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
693 (defcustom verilog-align-ifelse nil
694 "Non-nil means align `else' under matching `if'.
695 Otherwise else is lined up with first character on line holding matching if."
696 :group 'verilog-mode-indent
697 :type 'boolean)
698 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
700 (defcustom verilog-minimum-comment-distance 10
701 "Minimum distance (in lines) between begin and end required before a comment.
702 Setting this variable to zero results in every end acquiring a comment; the
703 default avoids too many redundant comments in tight quarters."
704 :group 'verilog-mode-indent
705 :type 'integer)
706 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
708 (defcustom verilog-highlight-p1800-keywords nil
709 "Non-nil means highlight words newly reserved by IEEE-1800.
710 These will appear in `verilog-font-lock-p1800-face' in order to gently
711 suggest changing where these words are used as variables to something else.
712 A nil value means highlight these words as appropriate for the SystemVerilog
713 IEEE-1800 standard. Note that changing this will require restarting Emacs
714 to see the effect as font color choices are cached by Emacs."
715 :group 'verilog-mode-indent
716 :type 'boolean)
717 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
719 (defcustom verilog-highlight-grouping-keywords nil
720 "Non-nil means highlight grouping keywords more dramatically.
721 If false, these words are in the `font-lock-type-face'; if True
722 then they are in `verilog-font-lock-grouping-keywords-face'.
723 Some find that special highlighting on these grouping constructs
724 allow the structure of the code to be understood at a glance."
725 :group 'verilog-mode-indent
726 :type 'boolean)
727 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
729 (defcustom verilog-highlight-modules nil
730 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
731 When true, mousing over module names will allow jumping to the
732 module definition. If false, this is not supported. Setting
733 this is experimental, and may lead to bad performance."
734 :group 'verilog-mode-indent
735 :type 'boolean)
736 (put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
738 (defcustom verilog-highlight-includes t
739 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
740 When true, mousing over include file names will allow jumping to the
741 file referenced. If false, this is not supported."
742 :group 'verilog-mode-indent
743 :type 'boolean)
744 (put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
746 (defcustom verilog-auto-declare-nettype nil
747 "Non-nil specifies the data type to use with `verilog-auto-input' etc.
748 Set this to \"wire\" if the Verilog code uses \"\\=`default_nettype
749 none\". Note using \\=`default_nettype none isn't recommended practice; this
750 mode is experimental."
751 :version "24.1" ; rev670
752 :group 'verilog-mode-actions
753 :type 'boolean)
754 (put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
756 (defcustom verilog-auto-wire-type nil
757 "Non-nil specifies the data type to use with `verilog-auto-wire' etc.
758 Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
759 :version "24.1" ; rev673
760 :group 'verilog-mode-actions
761 :type 'boolean)
762 (put 'verilog-auto-wire-type 'safe-local-variable `stringp)
764 (defcustom verilog-auto-endcomments t
765 "Non-nil means insert a comment /* ... */ after `end's.
766 The name of the function or case will be set between the braces."
767 :group 'verilog-mode-actions
768 :type 'boolean)
769 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
771 (defcustom verilog-auto-delete-trailing-whitespace nil
772 "Non-nil means to `delete-trailing-whitespace' in `verilog-auto'."
773 :version "24.1" ; rev703
774 :group 'verilog-mode-actions
775 :type 'boolean)
776 (put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
778 (defcustom verilog-auto-ignore-concat nil
779 "Non-nil means ignore signals in {...} concatenations for AUTOWIRE etc.
780 This will exclude signals referenced as pin connections in {...}
781 from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
782 for backward compatibility only and not set in new designs; it
783 may be removed in future versions."
784 :group 'verilog-mode-actions
785 :type 'boolean)
786 (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
788 (defcustom verilog-auto-read-includes nil
789 "Non-nil means to automatically read includes before AUTOs.
790 This will do a `verilog-read-defines' and `verilog-read-includes' before
791 each AUTO expansion. This makes it easier to embed defines and includes,
792 but can result in very slow reading times if there are many or large
793 include files."
794 :group 'verilog-mode-actions
795 :type 'boolean)
796 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
798 (defcustom verilog-auto-save-policy nil
799 "Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
800 A value of `force' will always do a \\[verilog-auto] automatically if
801 needed on every save. A value of `detect' will do \\[verilog-auto]
802 automatically when it thinks necessary. A value of `ask' will query the
803 user when it thinks updating is needed.
805 You should not rely on the `ask' or `detect' policies, they are safeguards
806 only. They do not detect when AUTOINSTs need to be updated because a
807 sub-module's port list has changed."
808 :group 'verilog-mode-actions
809 :type '(choice (const nil) (const ask) (const detect) (const force)))
811 (defcustom verilog-auto-star-expand t
812 "Non-nil means to expand SystemVerilog .* instance ports.
813 They will be expanded in the same way as if there was an AUTOINST in the
814 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
815 :group 'verilog-mode-actions
816 :type 'boolean)
817 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
819 (defcustom verilog-auto-star-save nil
820 "Non-nil means save to disk SystemVerilog .* instance expansions.
821 A nil value indicates direct connections will be removed before saving.
822 Only meaningful to those created due to `verilog-auto-star-expand' being set.
824 Instead of setting this, you may want to use /*AUTOINST*/, which will
825 always be saved."
826 :group 'verilog-mode-actions
827 :type 'boolean)
828 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
830 (defvar verilog-auto-update-tick nil
831 "Modification tick at which autos were last performed.")
833 (defvar verilog-auto-last-file-locals nil
834 "Text from file-local-variables during last evaluation.")
836 (defvar verilog-diff-function 'verilog-diff-report
837 "Function to run when `verilog-diff-auto' detects differences.
838 Function takes three arguments, the original buffer, the
839 difference buffer, and the point in original buffer with the
840 first difference.")
842 (defvar verilog-diff-ignore-regexp nil
843 "Non-nil specifies regexp which `verilog-diff-auto' will ignore.
844 This is typically nil.")
846 ;;; Compile support:
849 (require 'compile)
851 (defvar verilog-error-regexp-added nil)
853 (defvar verilog-error-regexp-emacs-alist
855 (verilog-xl-1
856 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
857 (verilog-xl-2
858 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
859 (verilog-IES
860 ".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
861 (verilog-surefire-1
862 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
863 (verilog-surefire-2
864 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
865 (verilog-verbose
867 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
868 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
869 (verilog-xsim
870 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
871 (verilog-vcs-1
872 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
873 (verilog-vcs-2
874 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
875 (verilog-vcs-3
876 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
877 (verilog-vcs-4
878 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
879 (verilog-verilator
880 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
881 (verilog-leda
882 "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
884 "List of regexps for Verilog compilers.
885 See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
887 (defvar verilog-error-regexp-xemacs-alist
888 ;; Emacs form is '((v-tool "re" 1 2) ...)
889 ;; XEmacs form is '(verilog ("re" 1 2) ...)
890 ;; So we can just map from Emacs to XEmacs
891 (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
892 "List of regexps for Verilog compilers.
893 See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
895 (defvar verilog-error-font-lock-keywords
897 ;; verilog-xl-1
898 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
899 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
900 ;; verilog-xl-2
901 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
902 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
903 ;; verilog-IES (nc-verilog)
904 (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
905 (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
906 ;; verilog-surefire-1
907 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
908 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
909 ;; verilog-surefire-2
910 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
911 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
912 ;; verilog-verbose
914 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
915 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
917 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
918 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
919 ;; verilog-vcs-1
920 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
921 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
922 ;; verilog-vcs-2
923 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
924 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
925 ;; verilog-vcs-3
926 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
927 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
928 ;; verilog-vcs-4
929 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
930 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
931 ;; verilog-verilator
932 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
933 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
934 ;; verilog-leda
935 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
936 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
938 "Keywords to also highlight in Verilog *compilation* buffers.
939 Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
941 (defcustom verilog-library-flags '("")
942 "List of standard Verilog arguments to use for /*AUTOINST*/.
943 These arguments are used to find files for `verilog-auto', and match
944 the flags accepted by a standard Verilog-XL simulator.
946 -f filename Reads more `verilog-library-flags' from the filename.
947 +incdir+dir Adds the directory to `verilog-library-directories'.
948 -Idir Adds the directory to `verilog-library-directories'.
949 -y dir Adds the directory to `verilog-library-directories'.
950 +libext+.v Adds the extensions to `verilog-library-extensions'.
951 -v filename Adds the filename to `verilog-library-files'.
953 filename Adds the filename to `verilog-library-files'.
954 This is not recommended, -v is a better choice.
956 You might want these defined in each file; put at the *END* of your file
957 something like:
959 // Local Variables:
960 // verilog-library-flags:(\"-y dir -y otherdir\")
961 // End:
963 Verilog-mode attempts to detect changes to this local variable, but they
964 are only insured to be correct when the file is first visited. Thus if you
965 have problems, use \\[find-alternate-file] RET to have these take effect.
967 See also the variables mentioned above."
968 :group 'verilog-mode-auto
969 :type '(repeat string))
970 (put 'verilog-library-flags 'safe-local-variable 'listp)
972 (defcustom verilog-library-directories '(".")
973 "List of directories when looking for files for /*AUTOINST*/.
974 The directory may be relative to the current file, or absolute.
975 Environment variables are also expanded in the directory names.
976 Having at least the current directory is a good idea.
978 You might want these defined in each file; put at the *END* of your file
979 something like:
981 // Local Variables:
982 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
983 // End:
985 Verilog-mode attempts to detect changes to this local variable, but they
986 are only insured to be correct when the file is first visited. Thus if you
987 have problems, use \\[find-alternate-file] RET to have these take effect.
989 See also `verilog-library-flags', `verilog-library-files'
990 and `verilog-library-extensions'."
991 :group 'verilog-mode-auto
992 :type '(repeat file))
993 (put 'verilog-library-directories 'safe-local-variable 'listp)
995 (defcustom verilog-library-files '()
996 "List of files to search for modules.
997 AUTOINST will use this when it needs to resolve a module name.
998 This is a complete path, usually to a technology file with many standard
999 cells defined in it.
1001 You might want these defined in each file; put at the *END* of your file
1002 something like:
1004 // Local Variables:
1005 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
1006 // End:
1008 Verilog-mode attempts to detect changes to this local variable, but they
1009 are only insured to be correct when the file is first visited. Thus if you
1010 have problems, use \\[find-alternate-file] RET to have these take effect.
1012 See also `verilog-library-flags', `verilog-library-directories'."
1013 :group 'verilog-mode-auto
1014 :type '(repeat directory))
1015 (put 'verilog-library-files 'safe-local-variable 'listp)
1017 (defcustom verilog-library-extensions '(".v" ".sv")
1018 "List of extensions to use when looking for files for /*AUTOINST*/.
1019 See also `verilog-library-flags', `verilog-library-directories'."
1020 :type '(repeat string)
1021 :group 'verilog-mode-auto)
1022 (put 'verilog-library-extensions 'safe-local-variable 'listp)
1024 (defcustom verilog-active-low-regexp nil
1025 "If true, treat signals matching this regexp as active low.
1026 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
1027 you will probably also need `verilog-auto-reset-widths' set."
1028 :group 'verilog-mode-auto
1029 :type '(choice (const nil) regexp))
1030 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
1032 (defcustom verilog-auto-sense-include-inputs nil
1033 "Non-nil means AUTOSENSE should include all inputs.
1034 If nil, only inputs that are NOT output signals in the same block are
1035 included."
1036 :group 'verilog-mode-auto
1037 :type 'boolean)
1038 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
1040 (defcustom verilog-auto-sense-defines-constant nil
1041 "Non-nil means AUTOSENSE should assume all defines represent constants.
1042 When true, the defines will not be included in sensitivity lists. To
1043 maintain compatibility with other sites, this should be set at the bottom
1044 of each Verilog file that requires it, rather than being set globally."
1045 :group 'verilog-mode-auto
1046 :type 'boolean)
1047 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
1049 (defcustom verilog-auto-reset-blocking-in-non t
1050 "Non-nil means AUTORESET will reset blocking statements.
1051 When true, AUTORESET will reset in blocking statements those
1052 signals which were assigned with blocking assignments (=) even in
1053 a block with non-blocking assignments (<=).
1055 If nil, all blocking assigned signals are ignored when any
1056 non-blocking assignment is in the AUTORESET block. This allows
1057 blocking assignments to be used for temporary values and not have
1058 those temporaries reset. See example in `verilog-auto-reset'."
1059 :version "24.1" ; rev718
1060 :type 'boolean
1061 :group 'verilog-mode-auto)
1062 (put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
1064 (defcustom verilog-auto-reset-widths t
1065 "True means AUTORESET should determine the width of signals.
1066 This is then used to set the width of the zero (32'h0 for example). This
1067 is required by some lint tools that aren't smart enough to ignore widths of
1068 the constant zero. This may result in ugly code when parameters determine
1069 the MSB or LSB of a signal inside an AUTORESET.
1071 If nil, AUTORESET uses \"0\" as the constant.
1073 If `unbased', AUTORESET used the unbased unsized literal \"\\='0\"
1074 as the constant. This setting is strongly recommended for
1075 SystemVerilog designs."
1076 :type 'boolean
1077 :group 'verilog-mode-auto)
1078 (put 'verilog-auto-reset-widths 'safe-local-variable
1079 '(lambda (x) (memq x '(nil t unbased))))
1081 (defcustom verilog-assignment-delay ""
1082 "Text used for delays in delayed assignments. Add a trailing space if set."
1083 :group 'verilog-mode-auto
1084 :type 'string)
1085 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
1087 (defcustom verilog-auto-arg-format 'packed
1088 "Formatting to use for AUTOARG signal names.
1089 If `packed', then as many inputs and outputs that fit within
1090 `fill-column' will be put onto one line.
1092 If `single', then a single input or output will be put onto each
1093 line."
1094 :version "25.1"
1095 :type '(radio (const :tag "Line up Assignments and Declarations" packed)
1096 (const :tag "Line up Assignment statements" single))
1097 :group 'verilog-mode-auto)
1098 (put 'verilog-auto-arg-format 'safe-local-variable
1099 '(lambda (x) (memq x '(packed single))))
1101 (defcustom verilog-auto-arg-sort nil
1102 "Non-nil means AUTOARG signal names will be sorted, not in declaration order.
1103 Declaration order is advantageous with order based instantiations
1104 and is the default for backward compatibility. Sorted order
1105 reduces changes when declarations are moved around in a file, and
1106 it's bad practice to rely on order based instantiations anyhow.
1108 See also `verilog-auto-inst-sort'."
1109 :group 'verilog-mode-auto
1110 :type 'boolean)
1111 (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
1113 (defcustom verilog-auto-inst-dot-name nil
1114 "Non-nil means when creating ports with AUTOINST, use .name syntax.
1115 This will use \".port\" instead of \".port(port)\" when possible.
1116 This is only legal in SystemVerilog files, and will confuse older
1117 simulators. Setting `verilog-auto-inst-vector' to nil may also
1118 be desirable to increase how often .name will be used."
1119 :group 'verilog-mode-auto
1120 :type 'boolean)
1121 (put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
1123 (defcustom verilog-auto-inst-param-value nil
1124 "Non-nil means AUTOINST will replace parameters with the parameter value.
1125 If nil, leave parameters as symbolic names.
1127 Parameters must be in Verilog 2001 format #(...), and if a parameter is not
1128 listed as such there (as when the default value is acceptable), it will not
1129 be replaced, and will remain symbolic.
1131 For example, imagine a submodule uses parameters to declare the size of its
1132 inputs. This is then used by an upper module:
1134 module InstModule (o,i);
1135 parameter WIDTH;
1136 input [WIDTH-1:0] i;
1137 endmodule
1139 module ExampInst;
1140 InstModule
1141 #(.PARAM(10))
1142 instName
1143 (/*AUTOINST*/
1144 .i (i[PARAM-1:0]));
1146 Note even though PARAM=10, the AUTOINST has left the parameter as a
1147 symbolic name. If `verilog-auto-inst-param-value' is set, this will
1148 instead expand to:
1150 module ExampInst;
1151 InstModule
1152 #(.PARAM(10))
1153 instName
1154 (/*AUTOINST*/
1155 .i (i[9:0]));"
1156 :group 'verilog-mode-auto
1157 :type 'boolean)
1158 (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
1160 (defcustom verilog-auto-inst-sort nil
1161 "Non-nil means AUTOINST signals will be sorted, not in declaration order.
1162 Also affects AUTOINSTPARAM. Declaration order is the default for
1163 backward compatibility, and as some teams prefer signals that are
1164 declared together to remain together. Sorted order reduces
1165 changes when declarations are moved around in a file.
1167 See also `verilog-auto-arg-sort'."
1168 :version "24.1" ; rev688
1169 :group 'verilog-mode-auto
1170 :type 'boolean)
1171 (put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
1173 (defcustom verilog-auto-inst-vector t
1174 "Non-nil means when creating default ports with AUTOINST, use bus subscripts.
1175 If nil, skip the subscript when it matches the entire bus as declared in
1176 the module (AUTOWIRE signals always are subscripted, you must manually
1177 declare the wire to have the subscripts removed.) Setting this to nil may
1178 speed up some simulators, but is less general and harder to read, so avoid."
1179 :group 'verilog-mode-auto
1180 :type 'boolean)
1181 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
1183 (defcustom verilog-auto-inst-template-numbers nil
1184 "If true, when creating templated ports with AUTOINST, add a comment.
1186 If t, the comment will add the line number of the template that
1187 was used for that port declaration. This setting is suggested
1188 only for debugging use, as regular use may cause a large numbers
1189 of merge conflicts.
1191 If `lhs', the comment will show the left hand side of the
1192 AUTO_TEMPLATE rule that is matched. This is less precise than
1193 numbering (t) when multiple rules have the same pin name, but
1194 won't merge conflict."
1195 :group 'verilog-mode-auto
1196 :type '(choice (const nil) (const t) (const lhs)))
1197 (put 'verilog-auto-inst-template-numbers 'safe-local-variable
1198 '(lambda (x) (memq x '(nil t lhs))))
1200 (defcustom verilog-auto-inst-column 40
1201 "Indent-to column number for net name part of AUTOINST created pin."
1202 :group 'verilog-mode-indent
1203 :type 'integer)
1204 (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
1206 (defcustom verilog-auto-inst-interfaced-ports nil
1207 "Non-nil means include interfaced ports in AUTOINST expansions."
1208 :version "24.3" ; rev773, default change rev815
1209 :group 'verilog-mode-auto
1210 :type 'boolean)
1211 (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
1213 (defcustom verilog-auto-input-ignore-regexp nil
1214 "If non-nil, when creating AUTOINPUT, ignore signals matching this regexp.
1215 See the \\[verilog-faq] for examples on using this."
1216 :group 'verilog-mode-auto
1217 :type '(choice (const nil) regexp))
1218 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
1220 (defcustom verilog-auto-inout-ignore-regexp nil
1221 "If non-nil, when creating AUTOINOUT, ignore signals matching this regexp.
1222 See the \\[verilog-faq] for examples on using this."
1223 :group 'verilog-mode-auto
1224 :type '(choice (const nil) regexp))
1225 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
1227 (defcustom verilog-auto-output-ignore-regexp nil
1228 "If non-nil, when creating AUTOOUTPUT, ignore signals matching this regexp.
1229 See the \\[verilog-faq] for examples on using this."
1230 :group 'verilog-mode-auto
1231 :type '(choice (const nil) regexp))
1232 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
1234 (defcustom verilog-auto-template-warn-unused nil
1235 "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
1236 This feature is not supported before Emacs 21.1 or XEmacs 21.4."
1237 :version "24.3" ; rev787
1238 :group 'verilog-mode-auto
1239 :type 'boolean)
1240 (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
1242 (defcustom verilog-auto-tieoff-declaration "wire"
1243 "Data type used for the declaration for AUTOTIEOFF.
1244 If \"wire\" then create a wire, if \"assign\" create an
1245 assignment, else the data type for variable creation."
1246 :version "24.1" ; rev713
1247 :group 'verilog-mode-auto
1248 :type 'string)
1249 (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1251 (defcustom verilog-auto-tieoff-ignore-regexp nil
1252 "If non-nil, when creating AUTOTIEOFF, ignore signals matching this regexp.
1253 See the \\[verilog-faq] for examples on using this."
1254 :group 'verilog-mode-auto
1255 :type '(choice (const nil) regexp))
1256 (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1258 (defcustom verilog-auto-unused-ignore-regexp nil
1259 "If non-nil, when creating AUTOUNUSED, ignore signals matching this regexp.
1260 See the \\[verilog-faq] for examples on using this."
1261 :group 'verilog-mode-auto
1262 :type '(choice (const nil) regexp))
1263 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
1265 (defcustom verilog-case-fold t
1266 "Non-nil means `verilog-mode' regexps should ignore case.
1267 This variable is t for backward compatibility; nil is suggested."
1268 :version "24.4"
1269 :group 'verilog-mode
1270 :type 'boolean)
1271 (put 'verilog-case-fold 'safe-local-variable 'verilog-booleanp)
1273 (defcustom verilog-typedef-regexp nil
1274 "If non-nil, regular expression that matches Verilog-2001 typedef names.
1275 For example, \"_t$\" matches typedefs named with _t, as in the C language.
1276 See also `verilog-case-fold'."
1277 :group 'verilog-mode-auto
1278 :type '(choice (const nil) regexp))
1279 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
1281 (defcustom verilog-mode-hook 'verilog-set-compile-command
1282 "Hook run after Verilog mode is loaded."
1283 :type 'hook
1284 :group 'verilog-mode)
1286 (defcustom verilog-auto-hook nil
1287 "Hook run after `verilog-mode' updates AUTOs."
1288 :group 'verilog-mode-auto
1289 :type 'hook)
1291 (defcustom verilog-before-auto-hook nil
1292 "Hook run before `verilog-mode' updates AUTOs."
1293 :group 'verilog-mode-auto
1294 :type 'hook)
1296 (defcustom verilog-delete-auto-hook nil
1297 "Hook run after `verilog-mode' deletes AUTOs."
1298 :group 'verilog-mode-auto
1299 :type 'hook)
1301 (defcustom verilog-before-delete-auto-hook nil
1302 "Hook run before `verilog-mode' deletes AUTOs."
1303 :group 'verilog-mode-auto
1304 :type 'hook)
1306 (defcustom verilog-getopt-flags-hook nil
1307 "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1308 :group 'verilog-mode-auto
1309 :type 'hook)
1311 (defcustom verilog-before-getopt-flags-hook nil
1312 "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1313 :group 'verilog-mode-auto
1314 :type 'hook)
1316 (defcustom verilog-before-save-font-hook nil
1317 "Hook run before `verilog-save-font-mods' removes highlighting."
1318 :version "24.3" ; rev735
1319 :group 'verilog-mode-auto
1320 :type 'hook)
1322 (defcustom verilog-after-save-font-hook nil
1323 "Hook run after `verilog-save-font-mods' restores highlighting."
1324 :version "24.3" ; rev735
1325 :group 'verilog-mode-auto
1326 :type 'hook)
1328 (defvar verilog-imenu-generic-expression
1329 '((nil "^\\s-*\\(?:m\\(?:odule\\|acromodule\\)\\|p\\(?:rimitive\\|rogram\\|ackage\\)\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 1)
1330 ("*Variables*" "^\\s-*\\(reg\\|wire\\|logic\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3)
1331 ("*Classes*" "^\\s-*\\(?:\\(?:virtual\\|interface\\)\\s-+\\)?class\\s-+\\([A-Za-z_][A-Za-z0-9_]+\\)" 1)
1332 ("*Tasks*" "^\\s-*\\(?:\\(?:static\\|pure\\|virtual\\|local\\|protected\\)\\s-+\\)*task\\s-+\\(?:\\(?:static\\|automatic\\)\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_:]+\\)" 1)
1333 ("*Functions*" "^\\s-*\\(?:\\(?:static\\|pure\\|virtual\\|local\\|protected\\)\\s-+\\)*function\\s-+\\(?:\\(?:static\\|automatic\\)\\s-+\\)?\\(?:\\w+\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_:]+\\)" 1)
1334 ("*Interfaces*" "^\\s-*interface\\s-+\\([a-zA-Z_0-9]+\\)" 1)
1335 ("*Types*" "^\\s-*typedef\\s-+.*\\s-+\\([a-zA-Z_0-9]+\\)\\s-*;" 1))
1336 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1339 ;; provide a verilog-header function.
1340 ;; Customization variables:
1342 (defvar verilog-date-scientific-format nil
1343 "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1344 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1345 format (e.g. 09/17/1997) is not supported.")
1347 (defvar verilog-company nil
1348 "Default name of Company for Verilog header.
1349 If set will become buffer local.")
1350 (make-variable-buffer-local 'verilog-company)
1352 (defvar verilog-project nil
1353 "Default name of Project for Verilog header.
1354 If set will become buffer local.")
1355 (make-variable-buffer-local 'verilog-project)
1357 ;;; Keymap and Menu:
1360 (defvar verilog-mode-map
1361 (let ((map (make-sparse-keymap)))
1362 (define-key map ";" 'electric-verilog-semi)
1363 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1364 (define-key map ":" 'electric-verilog-colon)
1365 ;;(define-key map "=" 'electric-verilog-equal)
1366 (define-key map "`" 'electric-verilog-tick)
1367 (define-key map "\t" 'electric-verilog-tab)
1368 (define-key map "\r" 'electric-verilog-terminate-line)
1369 ;; backspace/delete key bindings
1370 (define-key map [backspace] 'backward-delete-char-untabify)
1371 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1372 (define-key map [delete] 'delete-char)
1373 (define-key map [(meta delete)] 'kill-word))
1374 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1375 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1376 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1377 (define-key map "\M-\t" 'verilog-complete-word)
1378 (define-key map "\M-?" 'verilog-show-completions)
1379 ;; Note \C-c and letter are reserved for users
1380 (define-key map "\C-c`" 'verilog-lint-off)
1381 (define-key map "\C-c*" 'verilog-delete-auto-star-implicit)
1382 (define-key map "\C-c?" 'verilog-diff-auto)
1383 (define-key map "\C-c\C-r" 'verilog-label-be)
1384 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1385 (define-key map "\C-c=" 'verilog-pretty-expr)
1386 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1387 (define-key map "\C-c/" 'verilog-star-comment)
1388 (define-key map "\C-c\C-c" 'verilog-comment-region)
1389 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1390 (when (featurep 'xemacs)
1391 (define-key map [(meta control h)] 'verilog-mark-defun)
1392 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1393 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1394 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1395 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1396 (define-key map "\C-c\C-a" 'verilog-auto)
1397 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1398 (define-key map "\C-c\C-p" 'verilog-preprocess)
1399 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1400 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1401 (define-key map "\C-c\C-h" 'verilog-header)
1402 map)
1403 "Keymap used in Verilog mode.")
1405 ;; menus
1406 (easy-menu-define
1407 verilog-menu verilog-mode-map "Menu for Verilog mode"
1408 (verilog-easy-menu-filter
1409 '("Verilog"
1410 ("Choose Compilation Action"
1411 ["None"
1412 (progn
1413 (setq verilog-tool nil)
1414 (verilog-set-compile-command))
1415 :style radio
1416 :selected (equal verilog-tool nil)
1417 :help "When invoking compilation, use compile-command"]
1418 ["Lint"
1419 (progn
1420 (setq verilog-tool 'verilog-linter)
1421 (verilog-set-compile-command))
1422 :style radio
1423 :selected (equal verilog-tool `verilog-linter)
1424 :help "When invoking compilation, use lint checker"]
1425 ["Coverage"
1426 (progn
1427 (setq verilog-tool 'verilog-coverage)
1428 (verilog-set-compile-command))
1429 :style radio
1430 :selected (equal verilog-tool `verilog-coverage)
1431 :help "When invoking compilation, annotate for coverage"]
1432 ["Simulator"
1433 (progn
1434 (setq verilog-tool 'verilog-simulator)
1435 (verilog-set-compile-command))
1436 :style radio
1437 :selected (equal verilog-tool `verilog-simulator)
1438 :help "When invoking compilation, interpret Verilog source"]
1439 ["Compiler"
1440 (progn
1441 (setq verilog-tool 'verilog-compiler)
1442 (verilog-set-compile-command))
1443 :style radio
1444 :selected (equal verilog-tool `verilog-compiler)
1445 :help "When invoking compilation, compile Verilog source"]
1446 ["Preprocessor"
1447 (progn
1448 (setq verilog-tool 'verilog-preprocessor)
1449 (verilog-set-compile-command))
1450 :style radio
1451 :selected (equal verilog-tool `verilog-preprocessor)
1452 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
1454 ("Move"
1455 ["Beginning of function" verilog-beg-of-defun
1456 :keys "C-M-a"
1457 :help "Move backward to the beginning of the current function or procedure"]
1458 ["End of function" verilog-end-of-defun
1459 :keys "C-M-e"
1460 :help "Move forward to the end of the current function or procedure"]
1461 ["Mark function" verilog-mark-defun
1462 :keys "C-M-h"
1463 :help "Mark the current Verilog function or procedure"]
1464 ["Goto function/module" verilog-goto-defun
1465 :help "Move to specified Verilog module/task/function"]
1466 ["Move to beginning of block" electric-verilog-backward-sexp
1467 :help "Move backward over one balanced expression"]
1468 ["Move to end of block" electric-verilog-forward-sexp
1469 :help "Move forward over one balanced expression"]
1471 ("Comments"
1472 ["Comment Region" verilog-comment-region
1473 :help "Put marked area into a comment"]
1474 ["UnComment Region" verilog-uncomment-region
1475 :help "Uncomment an area commented with Comment Region"]
1476 ["Multi-line comment insert" verilog-star-comment
1477 :help "Insert Verilog /* */ comment at point"]
1478 ["Lint error to comment" verilog-lint-off
1479 :help "Convert a Verilog linter warning line into a disable statement"]
1481 "----"
1482 ["Compile" compile
1483 :help "Perform compilation-action (above) on the current buffer"]
1484 ["AUTO, Save, Compile" verilog-auto-save-compile
1485 :help "Recompute AUTOs, save buffer, and compile"]
1486 ["Next Compile Error" next-error
1487 :help "Visit next compilation error message and corresponding source code"]
1488 ["Ignore Lint Warning at point" verilog-lint-off
1489 :help "Convert a Verilog linter warning line into a disable statement"]
1490 "----"
1491 ["Line up declarations around point" verilog-pretty-declarations
1492 :help "Line up declarations around point"]
1493 ["Line up equations around point" verilog-pretty-expr
1494 :help "Line up expressions around point"]
1495 ["Redo/insert comments on every end" verilog-label-be
1496 :help "Label matching begin ... end statements"]
1497 ["Expand [x:y] vector line" verilog-expand-vector
1498 :help "Take a signal vector on the current line and expand it to multiple lines"]
1499 ["Insert begin-end block" verilog-insert-block
1500 :help "Insert begin ... end"]
1501 ["Complete word" verilog-complete-word
1502 :help "Complete word at point"]
1503 "----"
1504 ["Recompute AUTOs" verilog-auto
1505 :help "Expand AUTO meta-comment statements"]
1506 ["Kill AUTOs" verilog-delete-auto
1507 :help "Remove AUTO expansions"]
1508 ["Diff AUTOs" verilog-diff-auto
1509 :help "Show differences in AUTO expansions"]
1510 ["Inject AUTOs" verilog-inject-auto
1511 :help "Inject AUTOs into legacy non-AUTO buffer"]
1512 ("AUTO Help..."
1513 ["AUTO General" (describe-function 'verilog-auto)
1514 :help "Help introduction on AUTOs"]
1515 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1516 :help "Help on verilog-library-flags"]
1517 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1518 :help "Help on verilog-library-directories"]
1519 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1520 :help "Help on verilog-library-files"]
1521 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1522 :help "Help on verilog-library-extensions"]
1523 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1524 :help "Help on reading `defines"]
1525 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1526 :help "Help on parsing `includes"]
1527 ["AUTOARG" (describe-function 'verilog-auto-arg)
1528 :help "Help on AUTOARG - declaring module port list"]
1529 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1530 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1531 ["AUTOASSIGNMODPORT" (describe-function 'verilog-auto-assign-modport)
1532 :help "Help on AUTOASSIGNMODPORT - creating assignments to/from modports"]
1533 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1534 :help "Help on AUTOINOUT - adding inouts from cells"]
1535 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1536 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1537 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1538 :help "Help on AUTOINOUTIN - copying i/o from another file as all inputs"]
1539 ["AUTOINOUTMODPORT" (describe-function 'verilog-auto-inout-modport)
1540 :help "Help on AUTOINOUTMODPORT - copying i/o from an interface modport"]
1541 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1542 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1543 ["AUTOINOUTPARAM" (describe-function 'verilog-auto-inout-param)
1544 :help "Help on AUTOINOUTPARAM - copying parameters from another file"]
1545 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1546 :help "Help on AUTOINPUT - adding inputs from cells"]
1547 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1548 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1549 ["AUTOINSERTLAST" (describe-function 'verilog-auto-insert-last)
1550 :help "Help on AUTOINSERTLISPLAST - insert text from a lisp function"]
1551 ["AUTOINST" (describe-function 'verilog-auto-inst)
1552 :help "Help on AUTOINST - adding pins for cells"]
1553 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1554 :help "Help on expanding Verilog-2001 .* pins"]
1555 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1556 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1557 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1558 :help "Help on AUTOLOGIC - declaring logic signals"]
1559 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1560 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1561 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1562 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1563 ["AUTOREG" (describe-function 'verilog-auto-reg)
1564 :help "Help on AUTOREG - declaring registers for non-wires"]
1565 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1566 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1567 ["AUTORESET" (describe-function 'verilog-auto-reset)
1568 :help "Help on AUTORESET - resetting always blocks"]
1569 ["AUTOSENSE or AS" (describe-function 'verilog-auto-sense)
1570 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1571 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1572 :help "Help on AUTOTIEOFF - tying off unused outputs"]
1573 ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
1574 :help "Help on AUTOUNDEF - undefine all local defines"]
1575 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1576 :help "Help on AUTOUNUSED - terminating unused inputs"]
1577 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1578 :help "Help on AUTOWIRE - declaring wires for cells"]
1580 "----"
1581 ["Submit bug report" verilog-submit-bug-report
1582 :help "Submit via mail a bug report on verilog-mode.el"]
1583 ["Version and FAQ" verilog-faq
1584 :help "Show the current version, and where to get the FAQ etc"]
1585 ["Customize Verilog Mode..." verilog-customize
1586 :help "Customize variables and other settings used by Verilog-Mode"]
1587 ["Customize Verilog Fonts & Colors" verilog-font-customize
1588 :help "Customize fonts used by Verilog-Mode."])))
1590 (easy-menu-define
1591 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1592 (verilog-easy-menu-filter
1593 '("Statements"
1594 ["Header" verilog-sk-header
1595 :help "Insert a header block at the top of file"]
1596 ["Comment" verilog-sk-comment
1597 :help "Insert a comment block"]
1598 "----"
1599 ["Module" verilog-sk-module
1600 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1601 ["OVM Class" verilog-sk-ovm-class
1602 :help "Insert an OVM class block"]
1603 ["UVM Object" verilog-sk-uvm-object
1604 :help "Insert an UVM object block"]
1605 ["UVM Component" verilog-sk-uvm-component
1606 :help "Insert an UVM component block"]
1607 ["Primitive" verilog-sk-primitive
1608 :help "Insert a primitive .. (.. );.. endprimitive block"]
1609 "----"
1610 ["Input" verilog-sk-input
1611 :help "Insert an input declaration"]
1612 ["Output" verilog-sk-output
1613 :help "Insert an output declaration"]
1614 ["Inout" verilog-sk-inout
1615 :help "Insert an inout declaration"]
1616 ["Wire" verilog-sk-wire
1617 :help "Insert a wire declaration"]
1618 ["Reg" verilog-sk-reg
1619 :help "Insert a register declaration"]
1620 ["Define thing under point as a register" verilog-sk-define-signal
1621 :help "Define signal under point as a register at the top of the module"]
1622 "----"
1623 ["Initial" verilog-sk-initial
1624 :help "Insert an initial begin .. end block"]
1625 ["Always" verilog-sk-always
1626 :help "Insert an always @(AS) begin .. end block"]
1627 ["Function" verilog-sk-function
1628 :help "Insert a function .. begin .. end endfunction block"]
1629 ["Task" verilog-sk-task
1630 :help "Insert a task .. begin .. end endtask block"]
1631 ["Specify" verilog-sk-specify
1632 :help "Insert a specify .. endspecify block"]
1633 ["Generate" verilog-sk-generate
1634 :help "Insert a generate .. endgenerate block"]
1635 "----"
1636 ["Begin" verilog-sk-begin
1637 :help "Insert a begin .. end block"]
1638 ["If" verilog-sk-if
1639 :help "Insert an if (..) begin .. end block"]
1640 ["(if) else" verilog-sk-else-if
1641 :help "Insert an else if (..) begin .. end block"]
1642 ["For" verilog-sk-for
1643 :help "Insert a for (...) begin .. end block"]
1644 ["While" verilog-sk-while
1645 :help "Insert a while (...) begin .. end block"]
1646 ["Fork" verilog-sk-fork
1647 :help "Insert a fork begin .. end .. join block"]
1648 ["Repeat" verilog-sk-repeat
1649 :help "Insert a repeat (..) begin .. end block"]
1650 ["Case" verilog-sk-case
1651 :help "Insert a case block, prompting for details"]
1652 ["Casex" verilog-sk-casex
1653 :help "Insert a casex (...) item: begin.. end endcase block"]
1654 ["Casez" verilog-sk-casez
1655 :help "Insert a casez (...) item: begin.. end endcase block"])))
1657 (defvar verilog-mode-abbrev-table nil
1658 "Abbrev table in use in Verilog-mode buffers.")
1660 (define-abbrev-table 'verilog-mode-abbrev-table ())
1661 (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1662 (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1663 (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1664 (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1665 (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1666 (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1667 (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1668 (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1669 (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1670 (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1671 (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1672 (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1673 (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1674 (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1675 (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1676 (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1677 (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1678 (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1679 (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1680 (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1681 (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1682 (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1683 (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1684 (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1685 (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
1688 ;; Macros
1691 (defsubst verilog-within-string ()
1692 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
1694 (defsubst verilog-string-match-fold (regexp string &optional start)
1695 "Like `string-match', but use `verilog-case-fold'.
1696 Return index of start of first match for REGEXP in STRING, or nil.
1697 Matching ignores case if `verilog-case-fold' is non-nil.
1698 If third arg START is non-nil, start search at that index in STRING."
1699 (let ((case-fold-search verilog-case-fold))
1700 (string-match regexp string start)))
1702 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1703 "Replace occurrences of FROM-STRING with TO-STRING.
1704 FIXEDCASE and LITERAL as in `replace-match'. STRING is what to replace.
1705 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1706 will break, as the o's continuously replace. xa -> x works ok though."
1707 ;; Hopefully soon to an Emacs built-in
1708 ;; Also note \ in the replacement prevent multiple replacements; IE
1709 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1710 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
1711 (let ((start 0))
1712 (while (string-match from-string string start)
1713 (setq string (replace-match to-string fixedcase literal string)
1714 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1715 string))
1717 (defsubst verilog-string-remove-spaces (string)
1718 "Remove spaces surrounding STRING."
1719 (save-match-data
1720 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1721 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1722 string))
1724 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1725 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1726 "Like `re-search-forward', but skips over match in comments or strings."
1727 (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
1728 (while (and
1729 (re-search-forward REGEXP BOUND NOERROR)
1730 (setq mdata (match-data))
1731 (and (verilog-skip-forward-comment-or-string)
1732 (progn
1733 (setq mdata '(nil nil))
1734 (if BOUND
1735 (< (point) BOUND)
1736 t)))))
1737 (store-match-data mdata)
1738 (match-end 0)))
1740 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1741 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1742 "Like `re-search-backward', but skips over match in comments or strings."
1743 (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
1744 (while (and
1745 (re-search-backward REGEXP BOUND NOERROR)
1746 (setq mdata (match-data))
1747 (and (verilog-skip-backward-comment-or-string)
1748 (progn
1749 (setq mdata '(nil nil))
1750 (if BOUND
1751 (> (point) BOUND)
1752 t)))))
1753 (store-match-data mdata)
1754 (match-end 0)))
1756 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1757 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1758 but trashes match data and is faster for REGEXP that doesn't match often.
1759 This uses `verilog-scan' and text properties to ignore comments,
1760 so there may be a large up front penalty for the first search."
1761 (let (pt)
1762 (while (and (not pt)
1763 (re-search-forward regexp bound noerror))
1764 (if (verilog-inside-comment-or-string-p)
1765 (re-search-forward "[/\"\n]" nil t) ; Only way a comment or quote can end
1766 (setq pt (match-end 0))))
1767 pt))
1769 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1770 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1771 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1772 but trashes match data and is faster for REGEXP that doesn't match often.
1773 This uses `verilog-scan' and text properties to ignore comments,
1774 so there may be a large up front penalty for the first search."
1775 (let (pt)
1776 (while (and (not pt)
1777 (re-search-backward regexp bound noerror))
1778 (if (verilog-inside-comment-or-string-p)
1779 (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
1780 (setq pt (match-beginning 0))))
1781 pt))
1783 (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1784 "Like `re-search-forward', but first search for SUBSTR constant.
1785 Then searched for the normal REGEXP (which contains SUBSTR), with given
1786 BOUND and NOERROR. The REGEXP must fit within a single line.
1787 This speeds up complicated regexp matches."
1788 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1789 ;; thus require matches to be on one line, and use beginning-of-line.
1790 (let (done)
1791 (while (and (not done)
1792 (search-forward substr bound noerror))
1793 (save-excursion
1794 (beginning-of-line)
1795 (setq done (re-search-forward regexp (point-at-eol) noerror)))
1796 (unless (and (<= (match-beginning 0) (point))
1797 (>= (match-end 0) (point)))
1798 (setq done nil)))
1799 (when done (goto-char done))
1800 done))
1801 ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ; -end (test bait)
1803 (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1804 "Like `re-search-backward', but first search for SUBSTR constant.
1805 Then searched for the normal REGEXP (which contains SUBSTR), with given
1806 BOUND and NOERROR. The REGEXP must fit within a single line.
1807 This speeds up complicated regexp matches."
1808 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1809 ;; thus require matches to be on one line, and use beginning-of-line.
1810 (let (done)
1811 (while (and (not done)
1812 (search-backward substr bound noerror))
1813 (save-excursion
1814 (end-of-line)
1815 (setq done (re-search-backward regexp (point-at-bol) noerror)))
1816 (unless (and (<= (match-beginning 0) (point))
1817 (>= (match-end 0) (point)))
1818 (setq done nil)))
1819 (when done (goto-char done))
1820 done))
1821 ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ; -end (test bait)
1823 (defun verilog-delete-trailing-whitespace ()
1824 "Delete trailing spaces or tabs, but not newlines nor linefeeds.
1825 Also add missing final newline.
1827 To call this from the command line, see \\[verilog-batch-diff-auto].
1829 To call on \\[verilog-auto], set `verilog-auto-delete-trailing-whitespace'."
1830 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1831 (save-excursion
1832 (goto-char (point-min))
1833 (while (re-search-forward "[ \t]+$" nil t) ; Not syntactic WS as no formfeed
1834 (replace-match "" nil nil))
1835 (goto-char (point-max))
1836 (unless (bolp) (insert "\n"))))
1838 (defvar compile-command)
1839 (defvar create-lockfiles) ; Emacs 24
1841 ;; compilation program
1842 (defun verilog-set-compile-command ()
1843 "Function to compute shell command to compile Verilog.
1845 This reads `verilog-tool' and sets `compile-command'. This specifies the
1846 program that executes when you type \\[compile] or
1847 \\[verilog-auto-save-compile].
1849 By default `verilog-tool' uses a Makefile if one exists in the
1850 current directory. If not, it is set to the `verilog-linter',
1851 `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1852 or `verilog-simulator' variables, as selected with the Verilog ->
1853 \"Choose Compilation Action\" menu.
1855 You should set `verilog-tool' or the other variables to the path and
1856 arguments for your Verilog simulator. For example:
1857 \"vcs -p123 -O\"
1858 or a string like:
1859 \"(cd /tmp; surecov %s)\".
1861 In the former case, the path to the current buffer is concat'ed to the
1862 value of `verilog-tool'; in the later, the path to the current buffer is
1863 substituted for the %s.
1865 Where __FLAGS__ appears in the string `verilog-current-flags'
1866 will be substituted.
1868 Where __FILE__ appears in the string, the variable
1869 `buffer-file-name' of the current buffer, without the directory
1870 portion, will be substituted."
1871 (interactive)
1872 (cond
1873 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1874 (file-exists-p "Makefile"))
1875 (set (make-local-variable 'compile-command) "make "))
1877 (set (make-local-variable 'compile-command)
1878 (if verilog-tool
1879 (if (string-match "%s" (eval verilog-tool))
1880 (format (eval verilog-tool) (or buffer-file-name ""))
1881 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1882 ""))))
1883 (verilog-modify-compile-command))
1885 (defun verilog-expand-command (command)
1886 "Replace meta-information in COMMAND and return it.
1887 Where __FLAGS__ appears in the string `verilog-current-flags'
1888 will be substituted. Where __FILE__ appears in the string, the
1889 current buffer's file-name, without the directory portion, will
1890 be substituted."
1891 (setq command (verilog-string-replace-matches
1892 ;; Note \\b only works if under verilog syntax table
1893 "\\b__FLAGS__\\b" (verilog-current-flags)
1894 t t command))
1895 (setq command (verilog-string-replace-matches
1896 "\\b__FILE__\\b" (file-name-nondirectory
1897 (or (buffer-file-name) ""))
1898 t t command))
1899 command)
1901 (defun verilog-modify-compile-command ()
1902 "Update `compile-command' using `verilog-expand-command'."
1903 (when (and
1904 (stringp compile-command)
1905 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
1906 (set (make-local-variable 'compile-command)
1907 (verilog-expand-command compile-command))))
1909 (if (featurep 'xemacs)
1910 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1911 (defun verilog-error-regexp-add-xemacs ()
1912 "Teach XEmacs about verilog errors.
1913 Called by `compilation-mode-hook'. This allows \\[next-error] to
1914 find the errors."
1915 (interactive)
1916 (if (boundp 'compilation-error-regexp-systems-alist)
1917 (if (and
1918 (not (equal compilation-error-regexp-systems-list 'all))
1919 (not (member compilation-error-regexp-systems-list 'verilog)))
1920 (push 'verilog compilation-error-regexp-systems-list)))
1921 (if (boundp 'compilation-error-regexp-alist-alist)
1922 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1923 (setcdr compilation-error-regexp-alist-alist
1924 (cons verilog-error-regexp-xemacs-alist
1925 (cdr compilation-error-regexp-alist-alist)))))
1926 (if (boundp 'compilation-font-lock-keywords)
1927 (progn
1928 (set (make-local-variable 'compilation-font-lock-keywords)
1929 verilog-error-font-lock-keywords)
1930 (font-lock-set-defaults)))
1931 ;; Need to re-run compilation-error-regexp builder
1932 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1933 (compilation-build-compilation-error-regexp-alist))
1936 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1937 (defun verilog-error-regexp-add-emacs ()
1938 "Tell Emacs compile that we are Verilog.
1939 Called by `compilation-mode-hook'. This allows \\[next-error] to
1940 find the errors."
1941 (interactive)
1942 (when (boundp 'compilation-error-regexp-alist-alist)
1943 (when (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1944 (mapcar
1945 (lambda (item)
1946 (push (car item) compilation-error-regexp-alist)
1947 (push item compilation-error-regexp-alist-alist))
1948 verilog-error-regexp-emacs-alist))))
1950 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1951 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1953 (defconst verilog-compiler-directives
1954 (eval-when-compile
1956 ;; compiler directives, from IEEE 1800-2012 section 22.1
1957 "`__FILE__" "`__LINE" "`begin_keywords" "`celldefine" "`default_nettype"
1958 "`define" "`else" "`elsif" "`end_keywords" "`endcelldefine" "`endif"
1959 "`ifdef" "`ifndef" "`include" "`line" "`nounconnected_drive" "`pragma"
1960 "`resetall" "`timescale" "`unconnected_drive" "`undef" "`undefineall"
1961 ;; compiler directives not covered by IEEE 1800
1962 "`case" "`default" "`endfor" "`endprotect" "`endswitch" "`endwhile" "`for"
1963 "`format" "`if" "`let" "`protect" "`switch" "`timescale" "`time_scale"
1964 "`while"
1966 "List of Verilog compiler directives.")
1968 (defconst verilog-directive-re
1969 (verilog-regexp-words verilog-compiler-directives))
1971 (defconst verilog-directive-re-1
1972 (concat "[ \t]*" verilog-directive-re))
1974 (defconst verilog-directive-begin
1975 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1977 (defconst verilog-directive-middle
1978 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
1980 (defconst verilog-directive-end
1981 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1983 (defconst verilog-ovm-begin-re
1984 (eval-when-compile
1985 (verilog-regexp-opt
1987 "`ovm_component_utils_begin"
1988 "`ovm_component_param_utils_begin"
1989 "`ovm_field_utils_begin"
1990 "`ovm_object_utils_begin"
1991 "`ovm_object_param_utils_begin"
1992 "`ovm_sequence_utils_begin"
1993 "`ovm_sequencer_utils_begin"
1994 ) nil )))
1996 (defconst verilog-ovm-end-re
1997 (eval-when-compile
1998 (verilog-regexp-opt
2000 "`ovm_component_utils_end"
2001 "`ovm_field_utils_end"
2002 "`ovm_object_utils_end"
2003 "`ovm_sequence_utils_end"
2004 "`ovm_sequencer_utils_end"
2005 ) nil )))
2007 (defconst verilog-uvm-begin-re
2008 (eval-when-compile
2009 (verilog-regexp-opt
2011 "`uvm_component_utils_begin"
2012 "`uvm_component_param_utils_begin"
2013 "`uvm_field_utils_begin"
2014 "`uvm_object_utils_begin"
2015 "`uvm_object_param_utils_begin"
2016 "`uvm_sequence_utils_begin"
2017 "`uvm_sequencer_utils_begin"
2018 ) nil )))
2020 (defconst verilog-uvm-end-re
2021 (eval-when-compile
2022 (verilog-regexp-opt
2024 "`uvm_component_utils_end"
2025 "`uvm_field_utils_end"
2026 "`uvm_object_utils_end"
2027 "`uvm_sequence_utils_end"
2028 "`uvm_sequencer_utils_end"
2029 ) nil )))
2031 (defconst verilog-vmm-begin-re
2032 (eval-when-compile
2033 (verilog-regexp-opt
2035 "`vmm_data_member_begin"
2036 "`vmm_env_member_begin"
2037 "`vmm_scenario_member_begin"
2038 "`vmm_subenv_member_begin"
2039 "`vmm_xactor_member_begin"
2040 ) nil ) ) )
2042 (defconst verilog-vmm-end-re
2043 (eval-when-compile
2044 (verilog-regexp-opt
2046 "`vmm_data_member_end"
2047 "`vmm_env_member_end"
2048 "`vmm_scenario_member_end"
2049 "`vmm_subenv_member_end"
2050 "`vmm_xactor_member_end"
2051 ) nil ) ) )
2053 (defconst verilog-vmm-statement-re
2054 (eval-when-compile
2055 (verilog-regexp-opt
2057 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
2058 ;; "`vmm_xactor_member_enum_array"
2059 ;; "`vmm_xactor_member_scalar_array"
2060 ;; "`vmm_xactor_member_scalar"
2061 ) nil )))
2063 (defconst verilog-ovm-statement-re
2064 (eval-when-compile
2065 (verilog-regexp-opt
2067 ;; Statements
2068 "`DUT_ERROR"
2069 "`MESSAGE"
2070 "`dut_error"
2071 "`message"
2072 "`ovm_analysis_imp_decl"
2073 "`ovm_blocking_get_imp_decl"
2074 "`ovm_blocking_get_peek_imp_decl"
2075 "`ovm_blocking_master_imp_decl"
2076 "`ovm_blocking_peek_imp_decl"
2077 "`ovm_blocking_put_imp_decl"
2078 "`ovm_blocking_slave_imp_decl"
2079 "`ovm_blocking_transport_imp_decl"
2080 "`ovm_component_registry"
2081 "`ovm_component_registry_param"
2082 "`ovm_component_utils"
2083 "`ovm_create"
2084 "`ovm_create_seq"
2085 "`ovm_declare_sequence_lib"
2086 "`ovm_do"
2087 "`ovm_do_seq"
2088 "`ovm_do_seq_with"
2089 "`ovm_do_with"
2090 "`ovm_error"
2091 "`ovm_fatal"
2092 "`ovm_field_aa_int_byte"
2093 "`ovm_field_aa_int_byte_unsigned"
2094 "`ovm_field_aa_int_int"
2095 "`ovm_field_aa_int_int_unsigned"
2096 "`ovm_field_aa_int_integer"
2097 "`ovm_field_aa_int_integer_unsigned"
2098 "`ovm_field_aa_int_key"
2099 "`ovm_field_aa_int_longint"
2100 "`ovm_field_aa_int_longint_unsigned"
2101 "`ovm_field_aa_int_shortint"
2102 "`ovm_field_aa_int_shortint_unsigned"
2103 "`ovm_field_aa_int_string"
2104 "`ovm_field_aa_object_int"
2105 "`ovm_field_aa_object_string"
2106 "`ovm_field_aa_string_int"
2107 "`ovm_field_aa_string_string"
2108 "`ovm_field_array_int"
2109 "`ovm_field_array_object"
2110 "`ovm_field_array_string"
2111 "`ovm_field_enum"
2112 "`ovm_field_event"
2113 "`ovm_field_int"
2114 "`ovm_field_object"
2115 "`ovm_field_queue_int"
2116 "`ovm_field_queue_object"
2117 "`ovm_field_queue_string"
2118 "`ovm_field_sarray_int"
2119 "`ovm_field_string"
2120 "`ovm_field_utils"
2121 "`ovm_file"
2122 "`ovm_get_imp_decl"
2123 "`ovm_get_peek_imp_decl"
2124 "`ovm_info"
2125 "`ovm_info1"
2126 "`ovm_info2"
2127 "`ovm_info3"
2128 "`ovm_info4"
2129 "`ovm_line"
2130 "`ovm_master_imp_decl"
2131 "`ovm_msg_detail"
2132 "`ovm_non_blocking_transport_imp_decl"
2133 "`ovm_nonblocking_get_imp_decl"
2134 "`ovm_nonblocking_get_peek_imp_decl"
2135 "`ovm_nonblocking_master_imp_decl"
2136 "`ovm_nonblocking_peek_imp_decl"
2137 "`ovm_nonblocking_put_imp_decl"
2138 "`ovm_nonblocking_slave_imp_decl"
2139 "`ovm_object_registry"
2140 "`ovm_object_registry_param"
2141 "`ovm_object_utils"
2142 "`ovm_peek_imp_decl"
2143 "`ovm_phase_func_decl"
2144 "`ovm_phase_task_decl"
2145 "`ovm_print_aa_int_object"
2146 "`ovm_print_aa_string_int"
2147 "`ovm_print_aa_string_object"
2148 "`ovm_print_aa_string_string"
2149 "`ovm_print_array_int"
2150 "`ovm_print_array_object"
2151 "`ovm_print_array_string"
2152 "`ovm_print_object_queue"
2153 "`ovm_print_queue_int"
2154 "`ovm_print_string_queue"
2155 "`ovm_put_imp_decl"
2156 "`ovm_rand_send"
2157 "`ovm_rand_send_with"
2158 "`ovm_send"
2159 "`ovm_sequence_utils"
2160 "`ovm_slave_imp_decl"
2161 "`ovm_transport_imp_decl"
2162 "`ovm_update_sequence_lib"
2163 "`ovm_update_sequence_lib_and_item"
2164 "`ovm_warning"
2165 "`static_dut_error"
2166 "`static_message") nil )))
2168 (defconst verilog-uvm-statement-re
2169 (eval-when-compile
2170 (verilog-regexp-opt
2172 ;; Statements
2173 "`uvm_analysis_imp_decl"
2174 "`uvm_blocking_get_imp_decl"
2175 "`uvm_blocking_get_peek_imp_decl"
2176 "`uvm_blocking_master_imp_decl"
2177 "`uvm_blocking_peek_imp_decl"
2178 "`uvm_blocking_put_imp_decl"
2179 "`uvm_blocking_slave_imp_decl"
2180 "`uvm_blocking_transport_imp_decl"
2181 "`uvm_component_param_utils"
2182 "`uvm_component_registry"
2183 "`uvm_component_registry_param"
2184 "`uvm_component_utils"
2185 "`uvm_create"
2186 "`uvm_create_on"
2187 "`uvm_create_seq" ; Undocumented in 1.1
2188 "`uvm_declare_p_sequencer"
2189 "`uvm_declare_sequence_lib" ; Deprecated in 1.1
2190 "`uvm_do"
2191 "`uvm_do_callbacks"
2192 "`uvm_do_callbacks_exit_on"
2193 "`uvm_do_obj_callbacks"
2194 "`uvm_do_obj_callbacks_exit_on"
2195 "`uvm_do_on"
2196 "`uvm_do_on_pri"
2197 "`uvm_do_on_pri_with"
2198 "`uvm_do_on_with"
2199 "`uvm_do_pri"
2200 "`uvm_do_pri_with"
2201 "`uvm_do_seq" ; Undocumented in 1.1
2202 "`uvm_do_seq_with" ; Undocumented in 1.1
2203 "`uvm_do_with"
2204 "`uvm_error"
2205 "`uvm_error_context"
2206 "`uvm_fatal"
2207 "`uvm_fatal_context"
2208 "`uvm_field_aa_int_byte"
2209 "`uvm_field_aa_int_byte_unsigned"
2210 "`uvm_field_aa_int_enum"
2211 "`uvm_field_aa_int_int"
2212 "`uvm_field_aa_int_int_unsigned"
2213 "`uvm_field_aa_int_integer"
2214 "`uvm_field_aa_int_integer_unsigned"
2215 "`uvm_field_aa_int_key"
2216 "`uvm_field_aa_int_longint"
2217 "`uvm_field_aa_int_longint_unsigned"
2218 "`uvm_field_aa_int_shortint"
2219 "`uvm_field_aa_int_shortint_unsigned"
2220 "`uvm_field_aa_int_string"
2221 "`uvm_field_aa_object_int"
2222 "`uvm_field_aa_object_string"
2223 "`uvm_field_aa_string_int"
2224 "`uvm_field_aa_string_string"
2225 "`uvm_field_array_enum"
2226 "`uvm_field_array_int"
2227 "`uvm_field_array_object"
2228 "`uvm_field_array_string"
2229 "`uvm_field_enum"
2230 "`uvm_field_event"
2231 "`uvm_field_int"
2232 "`uvm_field_object"
2233 "`uvm_field_queue_enum"
2234 "`uvm_field_queue_int"
2235 "`uvm_field_queue_object"
2236 "`uvm_field_queue_string"
2237 "`uvm_field_real"
2238 "`uvm_field_sarray_enum"
2239 "`uvm_field_sarray_int"
2240 "`uvm_field_sarray_object"
2241 "`uvm_field_sarray_string"
2242 "`uvm_field_string"
2243 "`uvm_field_utils"
2244 "`uvm_file" ; Undocumented in 1.1, use `__FILE__
2245 "`uvm_get_imp_decl"
2246 "`uvm_get_peek_imp_decl"
2247 "`uvm_info"
2248 "`uvm_info_context"
2249 "`uvm_line" ; Undocumented in 1.1, use `__LINE__
2250 "`uvm_master_imp_decl"
2251 "`uvm_non_blocking_transport_imp_decl" ; Deprecated in 1.1
2252 "`uvm_nonblocking_get_imp_decl"
2253 "`uvm_nonblocking_get_peek_imp_decl"
2254 "`uvm_nonblocking_master_imp_decl"
2255 "`uvm_nonblocking_peek_imp_decl"
2256 "`uvm_nonblocking_put_imp_decl"
2257 "`uvm_nonblocking_slave_imp_decl"
2258 "`uvm_nonblocking_transport_imp_decl"
2259 "`uvm_object_param_utils"
2260 "`uvm_object_registry"
2261 "`uvm_object_registry_param" ; Undocumented in 1.1
2262 "`uvm_object_utils"
2263 "`uvm_pack_array"
2264 "`uvm_pack_arrayN"
2265 "`uvm_pack_enum"
2266 "`uvm_pack_enumN"
2267 "`uvm_pack_int"
2268 "`uvm_pack_intN"
2269 "`uvm_pack_queue"
2270 "`uvm_pack_queueN"
2271 "`uvm_pack_real"
2272 "`uvm_pack_sarray"
2273 "`uvm_pack_sarrayN"
2274 "`uvm_pack_string"
2275 "`uvm_peek_imp_decl"
2276 "`uvm_put_imp_decl"
2277 "`uvm_rand_send"
2278 "`uvm_rand_send_pri"
2279 "`uvm_rand_send_pri_with"
2280 "`uvm_rand_send_with"
2281 "`uvm_record_attribute"
2282 "`uvm_record_field"
2283 "`uvm_register_cb"
2284 "`uvm_send"
2285 "`uvm_send_pri"
2286 "`uvm_sequence_utils" ; Deprecated in 1.1
2287 "`uvm_set_super_type"
2288 "`uvm_slave_imp_decl"
2289 "`uvm_transport_imp_decl"
2290 "`uvm_unpack_array"
2291 "`uvm_unpack_arrayN"
2292 "`uvm_unpack_enum"
2293 "`uvm_unpack_enumN"
2294 "`uvm_unpack_int"
2295 "`uvm_unpack_intN"
2296 "`uvm_unpack_queue"
2297 "`uvm_unpack_queueN"
2298 "`uvm_unpack_real"
2299 "`uvm_unpack_sarray"
2300 "`uvm_unpack_sarrayN"
2301 "`uvm_unpack_string"
2302 "`uvm_update_sequence_lib" ; Deprecated in 1.1
2303 "`uvm_update_sequence_lib_and_item" ; Deprecated in 1.1
2304 "`uvm_warning"
2305 "`uvm_warning_context") nil )))
2309 ;; Regular expressions used to calculate indent, etc.
2311 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
2312 ;; Want to match
2313 ;; aa :
2314 ;; aa,bb :
2315 ;; a[34:32] :
2316 ;; a,
2317 ;; b :
2318 (defconst verilog-assignment-operator-re
2319 (eval-when-compile
2320 (verilog-regexp-opt
2322 ;; blocking assignment_operator
2323 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2324 ;; non blocking assignment operator
2325 "<="
2326 ;; comparison
2327 "==" "!=" "===" "!==" "<=" ">=" "==?" "!=?" "<->"
2328 ;; event_trigger
2329 "->" "->>"
2330 ;; property_expr
2331 "|->" "|=>" "#-#" "#=#"
2332 ;; distribution weighting
2333 ":=" ":/"
2334 ) 't
2336 (defconst verilog-assignment-operation-re
2337 (concat
2338 ;; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2339 ;; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2340 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
2342 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
2343 (defconst verilog-property-re
2344 (concat "\\(" verilog-label-re "\\)?"
2345 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2346 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2348 (defconst verilog-no-indent-begin-re
2349 (eval-when-compile
2350 (verilog-regexp-words
2351 '("always" "always_comb" "always_ff" "always_latch" "initial" "final" ; procedural blocks
2352 "if" "else" ; conditional statements
2353 "while" "for" "foreach" "repeat" "do" "forever" )))) ; loop statements
2355 (defconst verilog-ends-re
2356 ;; Parenthesis indicate type of keyword found
2357 (concat
2358 "\\(\\<else\\>\\)\\|" ; 1
2359 "\\(\\<if\\>\\)\\|" ; 2
2360 "\\(\\<assert\\>\\)\\|" ; 3
2361 "\\(\\<end\\>\\)\\|" ; 3.1
2362 "\\(\\<endcase\\>\\)\\|" ; 4
2363 "\\(\\<endfunction\\>\\)\\|" ; 5
2364 "\\(\\<endtask\\>\\)\\|" ; 6
2365 "\\(\\<endspecify\\>\\)\\|" ; 7
2366 "\\(\\<endtable\\>\\)\\|" ; 8
2367 "\\(\\<endgenerate\\>\\)\\|" ; 9
2368 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2369 "\\(\\<endclass\\>\\)\\|" ; 11
2370 "\\(\\<endgroup\\>\\)\\|" ; 12
2371 ;; VMM
2372 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2373 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2374 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2375 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2376 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
2377 ;; OVM
2378 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2379 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2380 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2381 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2382 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
2383 ;; UVM
2384 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2385 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2386 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2387 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2388 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
2391 (defconst verilog-auto-end-comment-lines-re
2392 ;; Matches to names in this list cause auto-end-commenting
2393 (concat "\\("
2394 verilog-directive-re "\\)\\|\\("
2395 (eval-when-compile
2396 (verilog-regexp-words
2397 `( "begin"
2398 "else"
2399 "end"
2400 "endcase"
2401 "endclass"
2402 "endclocking"
2403 "endgroup"
2404 "endfunction"
2405 "endmodule"
2406 "endprogram"
2407 "endprimitive"
2408 "endinterface"
2409 "endpackage"
2410 "endsequence"
2411 "endproperty"
2412 "endspecify"
2413 "endtable"
2414 "endtask"
2415 "join"
2416 "join_any"
2417 "join_none"
2418 "module"
2419 "macromodule"
2420 "primitive"
2421 "interface"
2422 "package")))
2423 "\\)"))
2425 ;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2426 ;; verilog-end-block-ordered-re matches exactly the same strings.
2427 (defconst verilog-end-block-ordered-re
2428 ;; Parenthesis indicate type of keyword found
2429 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2430 "\\(\\<end\\>\\)\\|" ; 2
2431 "\\(\\<end" ; 3, but not used
2432 "\\(" ; 4, but not used
2433 "\\(function\\)\\|" ; 5
2434 "\\(task\\)\\|" ; 6
2435 "\\(module\\)\\|" ; 7
2436 "\\(primitive\\)\\|" ; 8
2437 "\\(interface\\)\\|" ; 9
2438 "\\(package\\)\\|" ; 10
2439 "\\(class\\)\\|" ; 11
2440 "\\(group\\)\\|" ; 12
2441 "\\(program\\)\\|" ; 13
2442 "\\(sequence\\)\\|" ; 14
2443 "\\(clocking\\)\\|" ; 15
2444 "\\(property\\)\\|" ; 16
2445 "\\)\\>\\)"))
2446 (defconst verilog-end-block-re
2447 (eval-when-compile
2448 (verilog-regexp-words
2450 `("end" ; closes begin
2451 "endcase" ; closes any of case, casex casez or randcase
2452 "join" "join_any" "join_none" ; closes fork
2453 "endclass"
2454 "endtable"
2455 "endspecify"
2456 "endfunction"
2457 "endgenerate"
2458 "endtask"
2459 "endgroup"
2460 "endproperty"
2461 "endinterface"
2462 "endpackage"
2463 "endprogram"
2464 "endsequence"
2465 "endclocking"
2466 ;; OVM
2467 "`ovm_component_utils_end"
2468 "`ovm_field_utils_end"
2469 "`ovm_object_utils_end"
2470 "`ovm_sequence_utils_end"
2471 "`ovm_sequencer_utils_end"
2472 ;; UVM
2473 "`uvm_component_utils_end"
2474 "`uvm_field_utils_end"
2475 "`uvm_object_utils_end"
2476 "`uvm_sequence_utils_end"
2477 "`uvm_sequencer_utils_end"
2478 ;; VMM
2479 "`vmm_data_member_end"
2480 "`vmm_env_member_end"
2481 "`vmm_scenario_member_end"
2482 "`vmm_subenv_member_end"
2483 "`vmm_xactor_member_end"
2484 ))))
2487 (defconst verilog-endcomment-reason-re
2488 ;; Parenthesis indicate type of keyword found
2489 (concat
2490 "\\(\\<begin\\>\\)\\|" ; 1
2491 "\\(\\<else\\>\\)\\|" ; 2
2492 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2493 "\\(\\<always\\(?:_ff\\)?\\>\\(?:[ \t]*@\\)\\)\\|" ; 4 (matches always or always_ff w/ @...)
2494 "\\(\\<always\\(?:_comb\\|_latch\\)?\\>\\)\\|" ; 5 (matches always, always_comb, always_latch w/o @...)
2495 "\\(\\<fork\\>\\)\\|" ; 7
2496 "\\(\\<if\\>\\)\\|"
2497 verilog-property-re "\\|"
2498 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
2499 "\\(\\<clocking\\>\\)\\|"
2500 "\\(\\<task\\>\\)\\|"
2501 "\\(\\<function\\>\\)\\|"
2502 "\\(\\<initial\\>\\)\\|"
2503 "\\(\\<interface\\>\\)\\|"
2504 "\\(\\<package\\>\\)\\|"
2505 "\\(\\<final\\>\\)\\|"
2506 "\\(@\\)\\|"
2507 "\\(\\<while\\>\\)\\|\\(\\<do\\>\\)\\|"
2508 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2509 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2510 "#"))
2512 (defconst verilog-named-block-re "begin[ \t]*:")
2514 ;; These words begin a block which can occur inside a module which should be indented,
2515 ;; and closed with the respective word from the end-block list
2517 (defconst verilog-beg-block-re
2518 (eval-when-compile
2519 (verilog-regexp-words
2520 `("begin"
2521 "case" "casex" "casez" "randcase"
2522 "clocking"
2523 "generate"
2524 "fork"
2525 "function"
2526 "property"
2527 "specify"
2528 "table"
2529 "task"
2530 ;; OVM
2531 "`ovm_component_utils_begin"
2532 "`ovm_component_param_utils_begin"
2533 "`ovm_field_utils_begin"
2534 "`ovm_object_utils_begin"
2535 "`ovm_object_param_utils_begin"
2536 "`ovm_sequence_utils_begin"
2537 "`ovm_sequencer_utils_begin"
2538 ;; UVM
2539 "`uvm_component_utils_begin"
2540 "`uvm_component_param_utils_begin"
2541 "`uvm_field_utils_begin"
2542 "`uvm_object_utils_begin"
2543 "`uvm_object_param_utils_begin"
2544 "`uvm_sequence_utils_begin"
2545 "`uvm_sequencer_utils_begin"
2546 ;; VMM
2547 "`vmm_data_member_begin"
2548 "`vmm_env_member_begin"
2549 "`vmm_scenario_member_begin"
2550 "`vmm_subenv_member_begin"
2551 "`vmm_xactor_member_begin"
2552 ))))
2553 ;; These are the same words, in a specific order in the regular
2554 ;; expression so that matching will work nicely for
2555 ;; verilog-forward-sexp and verilog-calc-indent
2556 (defconst verilog-beg-block-re-ordered
2557 ( concat "\\(\\<begin\\>\\)" ;1
2558 "\\|\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
2559 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
2560 "\\|\\(\\<class\\>\\)" ;6
2561 "\\|\\(\\<table\\>\\)" ;7
2562 "\\|\\(\\<specify\\>\\)" ;8
2563 "\\|\\(\\<function\\>\\)" ;9
2564 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2565 "\\|\\(\\<task\\>\\)" ;14
2566 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2567 "\\|\\(\\<generate\\>\\)" ;18
2568 "\\|\\(\\<covergroup\\>\\)" ;16 20
2569 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2570 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2571 "\\|\\(\\<clocking\\>\\)" ;22 27
2572 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
2573 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
2577 (defconst verilog-end-block-ordered-rry
2578 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2579 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2580 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2581 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2582 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2583 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2584 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2585 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2586 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2587 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2588 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2589 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2590 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2593 (defconst verilog-nameable-item-re
2594 (eval-when-compile
2595 (verilog-regexp-words
2596 `("begin"
2597 "fork"
2598 "join" "join_any" "join_none"
2599 "end"
2600 "endcase"
2601 "endchecker"
2602 "endclass"
2603 "endclocking"
2604 "endconfig"
2605 "endfunction"
2606 "endgenerate"
2607 "endgroup"
2608 "endmodule"
2609 "endprimitive"
2610 "endinterface"
2611 "endpackage"
2612 "endprogram"
2613 "endproperty"
2614 "endsequence"
2615 "endspecify"
2616 "endtable"
2617 "endtask" )
2620 (defconst verilog-declaration-opener
2621 (eval-when-compile
2622 (verilog-regexp-words
2623 `("module" "begin" "task" "function"))))
2625 (defconst verilog-declaration-prefix-re
2626 (eval-when-compile
2627 (verilog-regexp-words
2629 ;; port direction
2630 "inout" "input" "output" "ref"
2631 ;; changeableness
2632 "const" "static" "protected" "local"
2633 ;; parameters
2634 "localparam" "parameter" "var"
2635 ;; type creation
2636 "typedef"
2637 ))))
2638 (defconst verilog-declaration-core-re
2639 (eval-when-compile
2640 (verilog-regexp-words
2642 ;; port direction (by themselves)
2643 "inout" "input" "output"
2644 ;; integer_atom_type
2645 "byte" "shortint" "int" "longint" "integer" "time"
2646 ;; integer_vector_type
2647 "bit" "logic" "reg"
2648 ;; non_integer_type
2649 "shortreal" "real" "realtime"
2650 ;; net_type
2651 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2652 ;; misc
2653 "string" "event" "chandle" "virtual" "enum" "genvar"
2654 "struct" "union"
2655 ;; builtin classes
2656 "mailbox" "semaphore"
2657 ))))
2658 (defconst verilog-declaration-re
2659 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2660 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2661 (defconst verilog-optional-signed-re "\\s-*\\(\\(un\\)?signed\\)?")
2662 (defconst verilog-optional-signed-range-re
2663 (concat
2664 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<\\(un\\)?signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2665 (defconst verilog-macroexp-re "`\\sw+")
2667 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2668 (defconst verilog-declaration-re-2-no-macro
2669 (concat "\\s-*" verilog-declaration-re
2670 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2671 "\\)?"))
2672 (defconst verilog-declaration-re-2-macro
2673 (concat "\\s-*" verilog-declaration-re
2674 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2675 "\\|\\(" verilog-macroexp-re "\\)"
2676 "\\)?"))
2677 (defconst verilog-declaration-re-1-macro
2678 (concat "^" verilog-declaration-re-2-macro))
2680 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2682 (defconst verilog-defun-re
2683 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2684 (defconst verilog-end-defun-re
2685 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2686 (defconst verilog-zero-indent-re
2687 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2688 (defconst verilog-inst-comment-re
2689 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
2691 (defconst verilog-behavioral-block-beg-re
2692 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2693 "function" "task"))))
2694 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2695 (defconst verilog-in-constraint-re ; keywords legal in constraint blocks starting a statement/block
2696 (eval-when-compile (verilog-regexp-words `("if" "else" "solve" "foreach"))))
2698 (defconst verilog-indent-re
2699 (eval-when-compile
2700 (verilog-regexp-words
2703 "always" "always_latch" "always_ff" "always_comb"
2704 "begin" "end"
2705 ;; "unique" "priority"
2706 "case" "casex" "casez" "randcase" "endcase"
2707 "class" "endclass"
2708 "clocking" "endclocking"
2709 "config" "endconfig"
2710 "covergroup" "endgroup"
2711 "fork" "join" "join_any" "join_none"
2712 "function" "endfunction"
2713 "final"
2714 "generate" "endgenerate"
2715 "initial"
2716 "interface" "endinterface"
2717 "module" "macromodule" "endmodule"
2718 "package" "endpackage"
2719 "primitive" "endprimitive"
2720 "program" "endprogram"
2721 "property" "endproperty"
2722 "sequence" "randsequence" "endsequence"
2723 "specify" "endspecify"
2724 "table" "endtable"
2725 "task" "endtask"
2726 "virtual"
2727 "`case"
2728 "`default"
2729 "`define" "`undef"
2730 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
2731 "`while" "`endwhile"
2732 "`for" "`endfor"
2733 "`format"
2734 "`include"
2735 "`let"
2736 "`protect" "`endprotect"
2737 "`switch" "`endswitch"
2738 "`timescale"
2739 "`time_scale"
2740 ;; OVM Begin tokens
2741 "`ovm_component_utils_begin"
2742 "`ovm_component_param_utils_begin"
2743 "`ovm_field_utils_begin"
2744 "`ovm_object_utils_begin"
2745 "`ovm_object_param_utils_begin"
2746 "`ovm_sequence_utils_begin"
2747 "`ovm_sequencer_utils_begin"
2748 ;; OVM End tokens
2749 "`ovm_component_utils_end"
2750 "`ovm_field_utils_end"
2751 "`ovm_object_utils_end"
2752 "`ovm_sequence_utils_end"
2753 "`ovm_sequencer_utils_end"
2754 ;; UVM Begin tokens
2755 "`uvm_component_utils_begin"
2756 "`uvm_component_param_utils_begin"
2757 "`uvm_field_utils_begin"
2758 "`uvm_object_utils_begin"
2759 "`uvm_object_param_utils_begin"
2760 "`uvm_sequence_utils_begin"
2761 "`uvm_sequencer_utils_begin"
2762 ;; UVM End tokens
2763 "`uvm_component_utils_end" ; Typo in spec, it's not uvm_component_end
2764 "`uvm_field_utils_end"
2765 "`uvm_object_utils_end"
2766 "`uvm_sequence_utils_end"
2767 "`uvm_sequencer_utils_end"
2768 ;; VMM Begin tokens
2769 "`vmm_data_member_begin"
2770 "`vmm_env_member_begin"
2771 "`vmm_scenario_member_begin"
2772 "`vmm_subenv_member_begin"
2773 "`vmm_xactor_member_begin"
2774 ;; VMM End tokens
2775 "`vmm_data_member_end"
2776 "`vmm_env_member_end"
2777 "`vmm_scenario_member_end"
2778 "`vmm_subenv_member_end"
2779 "`vmm_xactor_member_end"
2780 ))))
2782 (defconst verilog-defun-level-not-generate-re
2783 (eval-when-compile
2784 (verilog-regexp-words
2785 `( "module" "macromodule" "primitive" "class" "program"
2786 "interface" "package" "config"))))
2788 (defconst verilog-defun-level-re
2789 (eval-when-compile
2790 (verilog-regexp-words
2791 (append
2792 `( "module" "macromodule" "primitive" "class" "program"
2793 "interface" "package" "config")
2794 `( "initial" "final" "always" "always_comb" "always_ff"
2795 "always_latch" "endtask" "endfunction" )))))
2797 (defconst verilog-defun-level-generate-only-re
2798 (eval-when-compile
2799 (verilog-regexp-words
2800 `( "initial" "final" "always" "always_comb" "always_ff"
2801 "always_latch" "endtask" "endfunction" ))))
2803 (defconst verilog-cpp-level-re
2804 (eval-when-compile
2805 (verilog-regexp-words
2807 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2808 ))))
2810 (defconst verilog-dpi-import-export-re
2811 (eval-when-compile
2812 "\\(\\<\\(import\\|export\\)\\>\\s-+\"DPI\\(-C\\)?\"\\s-+\\(\\<\\(context\\|pure\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\<\\(function\\|task\\)\\>\\)"
2815 (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
2816 (defconst verilog-extended-case-re "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
2817 (defconst verilog-extended-complete-re
2818 (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
2819 "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
2820 "\\|\\(\\(\\<\\(import\\|export\\)\\>\\s-+\\)?\\(\"DPI\\(-C\\)?\"\\s-+\\)?\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\(function\\>\\|task\\>\\)\\)"
2821 "\\|" verilog-extended-case-re ))
2822 (defconst verilog-basic-complete-re
2823 (eval-when-compile
2824 (verilog-regexp-words
2826 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2827 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2828 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2829 ))))
2830 (defconst verilog-complete-reg
2831 (concat
2832 verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
2834 (defconst verilog-end-statement-re
2835 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2836 verilog-end-block-re "\\)"))
2838 (defconst verilog-endcase-re
2839 (concat verilog-extended-case-re "\\|"
2840 "\\(endcase\\)\\|"
2841 verilog-defun-re
2844 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2845 "String used to mark beginning of excluded text.")
2846 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2847 "String used to mark end of excluded text.")
2848 (defconst verilog-preprocessor-re
2849 (eval-when-compile
2850 (concat
2851 ;; single words
2852 "\\(?:"
2853 (verilog-regexp-words
2854 `("`__FILE__"
2855 "`__LINE__"
2856 "`celldefine"
2857 "`else"
2858 "`end_keywords"
2859 "`endcelldefine"
2860 "`endif"
2861 "`nounconnected_drive"
2862 "`resetall"
2863 "`unconnected_drive"
2864 "`undefineall"))
2865 "\\)\\|\\(?:"
2866 ;; two words: i.e. `ifdef DEFINE
2867 "\\<\\(`elsif\\|`ifn?def\\|`undef\\|`default_nettype\\|`begin_keywords\\)\\>\\s-"
2868 "\\)\\|\\(?:"
2869 ;; `line number "filename" level
2870 "\\<\\(`line\\)\\>\\s-+[0-9]+\\s-+\"[^\"]+\"\\s-+[012]"
2871 "\\)\\|\\(?:"
2872 ;;`include "file" or `include <file>
2873 "\\<\\(`include\\)\\>\\s-+\\(?:\"[^\"]+\"\\|<[^>]+>\\)"
2874 "\\)\\|\\(?:"
2875 ;; `pragma <stuff> (no mention in IEEE 1800-2012 that pragma can span multiple lines
2876 "\\<\\(`pragma\\)\\>\\s-+.+$"
2877 "\\)\\|\\(?:"
2878 ;; `timescale time_unit / time_precision
2879 "\\<\\(`timescale\\)\\>\\s-+10\\{0,2\\}\\s-*[munpf]?s\\s-*\\/\\s-*10\\{0,2\\}\\s-*[munpf]?s"
2880 "\\)\\|\\(?:"
2881 ;; `define and `if can span multiple lines if line ends in '\'. NOTE: `if is not IEEE 1800-2012
2882 ;; from http://www.emacswiki.org/emacs/MultilineRegexp
2883 (concat "\\<\\(`define\\|`if\\)\\>" ; directive
2884 "\\s-+" ; separator
2885 "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
2886 "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
2887 "\\)\\|\\(?:"
2888 ;; `<macro>() : i.e. `uvm_info(a,b,c) or any other pre-defined macro
2889 ;; Since parameters inside the macro can have parentheses, and
2890 ;; the macro can span multiple lines, just look for the opening
2891 ;; parentheses and then continue to the end of the first
2892 ;; non-escaped EOL
2893 (concat "\\<`\\w+\\>\\s-*("
2894 "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
2895 "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
2896 "\\)"
2899 (defconst verilog-keywords
2900 (append verilog-compiler-directives
2902 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2903 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2904 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2905 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2906 "config" "const" "constraint" "context" "continue" "cover"
2907 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2908 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2909 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2910 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2911 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2912 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2913 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2914 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2915 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2916 "include" "initial" "inout" "input" "inside" "instance" "int"
2917 "integer" "interface" "intersect" "join" "join_any" "join_none"
2918 "large" "liblist" "library" "local" "localparam" "logic"
2919 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2920 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2921 "notif0" "notif1" "null" "or" "output" "package" "packed"
2922 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2923 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2924 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2925 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2926 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2927 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2928 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2929 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2930 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2931 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2932 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2933 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2934 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2935 "wire" "with" "within" "wor" "xnor" "xor"
2936 ;; 1800-2009
2937 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2938 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2939 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2940 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2941 ;; 1800-2012
2942 "implements" "interconnect" "nettype" "soft"
2944 "List of Verilog keywords.")
2946 (defconst verilog-comment-start-regexp "//\\|/\\*"
2947 "Dual comment value for `comment-start-regexp'.")
2949 (defvar verilog-mode-syntax-table
2950 (let ((table (make-syntax-table)))
2951 ;; Populate the syntax TABLE.
2952 (modify-syntax-entry ?\\ "\\" table)
2953 (modify-syntax-entry ?+ "." table)
2954 (modify-syntax-entry ?- "." table)
2955 (modify-syntax-entry ?= "." table)
2956 (modify-syntax-entry ?% "." table)
2957 (modify-syntax-entry ?< "." table)
2958 (modify-syntax-entry ?> "." table)
2959 (modify-syntax-entry ?& "." table)
2960 (modify-syntax-entry ?| "." table)
2961 (modify-syntax-entry ?` "w" table) ; ` is part of definition symbols in Verilog
2962 (modify-syntax-entry ?_ "w" table)
2963 (modify-syntax-entry ?\' "." table)
2965 ;; Set up TABLE to handle block and line style comments.
2966 (if (featurep 'xemacs)
2967 (progn
2968 ;; XEmacs (formerly Lucid) has the best implementation
2969 (modify-syntax-entry ?/ ". 1456" table)
2970 (modify-syntax-entry ?* ". 23" table)
2971 (modify-syntax-entry ?\n "> b" table))
2972 ;; Emacs does things differently, but we can work with it
2973 (modify-syntax-entry ?/ ". 124b" table)
2974 (modify-syntax-entry ?* ". 23" table)
2975 (modify-syntax-entry ?\n "> b" table))
2976 table)
2977 "Syntax table used in Verilog mode buffers.")
2979 (defvar verilog-font-lock-keywords nil
2980 "Default highlighting for Verilog mode.")
2982 (defvar verilog-font-lock-keywords-1 nil
2983 "Subdued level highlighting for Verilog mode.")
2985 (defvar verilog-font-lock-keywords-2 nil
2986 "Medium level highlighting for Verilog mode.
2987 See also `verilog-font-lock-extra-types'.")
2989 (defvar verilog-font-lock-keywords-3 nil
2990 "Gaudy level highlighting for Verilog mode.
2991 See also `verilog-font-lock-extra-types'.")
2993 (defvar verilog-font-lock-translate-off-face
2994 'verilog-font-lock-translate-off-face
2995 "Font to use for translated off regions.")
2996 (defface verilog-font-lock-translate-off-face
2997 '((((class color)
2998 (background light))
2999 (:background "gray90" :italic t ))
3000 (((class color)
3001 (background dark))
3002 (:background "gray10" :italic t ))
3003 (((class grayscale) (background light))
3004 (:foreground "DimGray" :italic t))
3005 (((class grayscale) (background dark))
3006 (:foreground "LightGray" :italic t))
3007 (t (:italis t)))
3008 "Font lock mode face used to background highlight translate-off regions."
3009 :group 'font-lock-highlighting-faces)
3011 (defvar verilog-font-lock-p1800-face
3012 'verilog-font-lock-p1800-face
3013 "Font to use for p1800 keywords.")
3014 (defface verilog-font-lock-p1800-face
3015 '((((class color)
3016 (background light))
3017 (:foreground "DarkOrange3" :bold t ))
3018 (((class color)
3019 (background dark))
3020 (:foreground "orange1" :bold t ))
3021 (t (:italic t)))
3022 "Font lock mode face used to highlight P1800 keywords."
3023 :group 'font-lock-highlighting-faces)
3025 (defvar verilog-font-lock-ams-face
3026 'verilog-font-lock-ams-face
3027 "Font to use for Analog/Mixed Signal keywords.")
3028 (defface verilog-font-lock-ams-face
3029 '((((class color)
3030 (background light))
3031 (:foreground "Purple" :bold t ))
3032 (((class color)
3033 (background dark))
3034 (:foreground "orange1" :bold t ))
3035 (t (:italic t)))
3036 "Font lock mode face used to highlight AMS keywords."
3037 :group 'font-lock-highlighting-faces)
3039 (defvar verilog-font-lock-grouping-keywords-face
3040 'verilog-font-lock-grouping-keywords-face
3041 "Font to use for Verilog Grouping Keywords (such as begin..end).")
3042 (defface verilog-font-lock-grouping-keywords-face
3043 '((((class color)
3044 (background light))
3045 (:foreground "Purple" :bold t ))
3046 (((class color)
3047 (background dark))
3048 (:foreground "orange1" :bold t ))
3049 (t (:italic t)))
3050 "Font lock mode face used to highlight verilog grouping keywords."
3051 :group 'font-lock-highlighting-faces)
3053 (let* ((verilog-type-font-keywords
3054 (eval-when-compile
3055 (verilog-regexp-opt
3057 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
3058 "event" "genvar" "inout" "input" "integer" "localparam"
3059 "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0" "notif1" "or"
3060 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
3061 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
3062 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
3063 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
3064 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
3065 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
3066 ) nil )))
3068 (verilog-pragma-keywords
3069 (eval-when-compile
3070 (verilog-regexp-opt
3071 '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
3072 ) nil )))
3074 (verilog-1800-2005-keywords
3075 (eval-when-compile
3076 (verilog-regexp-opt
3077 '("alias" "assert" "assume" "automatic" "before" "bind"
3078 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
3079 "clocking" "config" "const" "constraint" "context" "continue"
3080 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
3081 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
3082 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
3083 "expect" "export" "extends" "extern" "first_match" "foreach"
3084 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
3085 "illegal_bins" "import" "incdir" "include" "inside" "instance"
3086 "int" "intersect" "large" "liblist" "library" "local" "longint"
3087 "matches" "medium" "modport" "new" "noshowcancelled" "null"
3088 "packed" "program" "property" "protected" "pull0" "pull1"
3089 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
3090 "randcase" "randsequence" "ref" "release" "return" "scalared"
3091 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
3092 "specparam" "static" "string" "strong0" "strong1" "struct"
3093 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
3094 "type" "union" "unsigned" "use" "var" "virtual" "void"
3095 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
3096 ) nil )))
3098 (verilog-1800-2009-keywords
3099 (eval-when-compile
3100 (verilog-regexp-opt
3101 '("accept_on" "checker" "endchecker" "eventually" "global"
3102 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
3103 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
3104 "sync_accept_on" "sync_reject_on" "unique0" "until"
3105 "until_with" "untyped" "weak" ) nil )))
3107 (verilog-1800-2012-keywords
3108 (eval-when-compile
3109 (verilog-regexp-opt
3110 '("implements" "interconnect" "nettype" "soft" ) nil )))
3112 (verilog-ams-keywords
3113 (eval-when-compile
3114 (verilog-regexp-opt
3115 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
3116 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
3117 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
3118 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
3119 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
3120 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
3121 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
3122 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
3123 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
3124 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
3125 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
3127 (verilog-font-keywords
3128 (eval-when-compile
3129 (verilog-regexp-opt
3131 "assign" "case" "casex" "casez" "randcase" "deassign"
3132 "default" "disable" "else" "endcase" "endfunction"
3133 "endgenerate" "endinterface" "endmodule" "endprimitive"
3134 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
3135 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
3136 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
3137 "package" "endpackage" "always" "always_comb" "always_ff"
3138 "always_latch" "posedge" "primitive" "priority" "release"
3139 "repeat" "specify" "table" "task" "unique" "wait" "while"
3140 "class" "program" "endclass" "endprogram"
3141 ) nil )))
3143 (verilog-font-grouping-keywords
3144 (eval-when-compile
3145 (verilog-regexp-opt
3146 '( "begin" "end" ) nil ))))
3148 (setq verilog-font-lock-keywords
3149 (list
3150 ;; Fontify all builtin keywords
3151 (concat "\\<\\(" verilog-font-keywords "\\|"
3152 ;; And user/system tasks and functions
3153 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
3154 "\\)\\>")
3155 ;; Fontify all types
3156 (if verilog-highlight-grouping-keywords
3157 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3158 'verilog-font-lock-grouping-keywords-face)
3159 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3160 'font-lock-type-face))
3161 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
3162 'font-lock-type-face)
3163 ;; Fontify IEEE-1800-2005 keywords appropriately
3164 (if verilog-highlight-p1800-keywords
3165 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3166 'verilog-font-lock-p1800-face)
3167 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3168 'font-lock-type-face))
3169 ;; Fontify IEEE-1800-2009 keywords appropriately
3170 (if verilog-highlight-p1800-keywords
3171 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3172 'verilog-font-lock-p1800-face)
3173 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3174 'font-lock-type-face))
3175 ;; Fontify IEEE-1800-2012 keywords appropriately
3176 (if verilog-highlight-p1800-keywords
3177 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3178 'verilog-font-lock-p1800-face)
3179 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3180 'font-lock-type-face))
3181 ;; Fontify Verilog-AMS keywords
3182 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
3183 'verilog-font-lock-ams-face)))
3185 (setq verilog-font-lock-keywords-1
3186 (append verilog-font-lock-keywords
3187 (list
3188 ;; Fontify module definitions
3189 (list
3190 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
3191 '(1 font-lock-keyword-face)
3192 '(3 font-lock-function-name-face 'prepend))
3193 ;; Fontify function definitions
3194 (list
3195 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
3196 '(1 font-lock-keyword-face)
3197 '(3 font-lock-constant-face prepend))
3198 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
3199 (1 font-lock-keyword-face)
3200 (2 font-lock-constant-face append))
3201 '("\\<function\\>\\s-+\\(\\sw+\\)"
3202 1 'font-lock-constant-face append))))
3204 (setq verilog-font-lock-keywords-2
3205 (append verilog-font-lock-keywords-1
3206 (list
3207 ;; Fontify pragmas
3208 (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
3209 ;; Fontify escaped names
3210 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
3211 ;; Fontify macro definitions/ uses
3212 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
3213 'font-lock-preprocessor-face
3214 'font-lock-type-face))
3215 ;; Fontify delays/numbers
3216 '("\\(@\\)\\|\\([ \t\n\f\r]#\\s-*\\(\\([0-9_.]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
3217 0 font-lock-type-face append)
3218 ;; Fontify property/sequence cycle delays - these start with '##'
3219 '("\\(##\\(\\sw+\\|\\[[^]]+\\]\\)\\)"
3220 0 font-lock-type-face append)
3221 ;; Fontify instantiation names
3222 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
3225 (setq verilog-font-lock-keywords-3
3226 (append verilog-font-lock-keywords-2
3227 (when verilog-highlight-translate-off
3228 (list
3229 ;; Fontify things in translate off regions
3230 '(verilog-match-translate-off
3231 (0 'verilog-font-lock-translate-off-face prepend))
3232 )))))
3235 ;; Buffer state preservation
3237 (defmacro verilog-save-buffer-state (&rest body)
3238 "Execute BODY forms, saving state around insignificant change.
3239 Changes in text properties like `face' or `syntax-table' are
3240 considered insignificant. This macro allows text properties to
3241 be changed, even in a read-only buffer.
3243 A change is considered significant if it affects the buffer text
3244 in any way that isn't completely restored again. Any
3245 user-visible changes to the buffer must not be within a
3246 `verilog-save-buffer-state'."
3247 `(let ((inhibit-point-motion-hooks t)
3248 (verilog-no-change-functions t))
3249 ,(if (fboundp 'with-silent-modifications)
3250 `(with-silent-modifications ,@body)
3251 ;; Backward compatible version of with-silent-modifications
3252 `(let* ((modified (buffer-modified-p))
3253 (buffer-undo-list t)
3254 (inhibit-read-only t)
3255 (inhibit-modification-hooks t)
3256 ;; XEmacs ignores inhibit-modification-hooks.
3257 before-change-functions after-change-functions
3258 deactivate-mark
3259 buffer-file-name ; Prevent primitives checking
3260 buffer-file-truename) ; for file modification
3261 (unwind-protect
3262 (progn ,@body)
3263 (and (not modified)
3264 (buffer-modified-p)
3265 (verilog-restore-buffer-modified-p nil)))))))
3268 (defvar verilog-save-font-mod-hooked nil
3269 "Local variable when inside a `verilog-save-font-no-change-functions' block.")
3270 (make-variable-buffer-local 'verilog-save-font-mod-hooked)
3272 (defmacro verilog-save-font-no-change-functions (&rest body)
3273 "Execute BODY forms, disabling all change hooks in BODY.
3274 Includes temporary disabling of `font-lock' to restore the buffer
3275 to full text form for parsing. Additional actions may be specified with
3276 `verilog-before-save-font-hook' and `verilog-after-save-font-hook'.
3277 For insignificant changes, see instead `verilog-save-buffer-state'."
3278 `(if verilog-save-font-mod-hooked ; Short-circuit a recursive call
3279 (progn ,@body)
3280 ;; Before version 20, match-string with font-lock returns a
3281 ;; vector that is not equal to the string. IE if on "input"
3282 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
3283 ;; Therefore we must remove and restore font-lock mode
3284 (verilog-run-hooks 'verilog-before-save-font-hook)
3285 (let* ((verilog-save-font-mod-hooked (- (point-max) (point-min)))
3286 ;; Significant speed savings with no font-lock properties
3287 (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
3288 (font-lock-mode 0)
3289 t)))
3290 (run-hook-with-args 'before-change-functions (point-min) (point-max))
3291 (unwind-protect
3292 ;; Must inhibit and restore hooks before restoring font-lock
3293 (let* ((inhibit-point-motion-hooks t)
3294 (inhibit-modification-hooks t)
3295 (verilog-no-change-functions t)
3296 ;; XEmacs and pre-Emacs 21 ignore inhibit-modification-hooks.
3297 before-change-functions after-change-functions)
3298 (progn ,@body))
3299 ;; Unwind forms
3300 (run-hook-with-args 'after-change-functions (point-min) (point-max)
3301 verilog-save-font-mod-hooked) ; old length
3302 (when fontlocked (font-lock-mode t))
3303 (verilog-run-hooks 'verilog-after-save-font-hook)))))
3306 ;; Comment detection and caching
3308 (defvar verilog-scan-cache-preserving nil
3309 "If true, the specified buffer's comment properties are static.
3310 Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
3311 and `verilog-scan'.")
3313 (defvar verilog-scan-cache-tick nil
3314 "Modification tick at which `verilog-scan' was last completed.")
3315 (make-variable-buffer-local 'verilog-scan-cache-tick)
3317 (defun verilog-scan-cache-flush ()
3318 "Flush the `verilog-scan' cache."
3319 (setq verilog-scan-cache-tick nil))
3321 (defun verilog-scan-cache-ok-p ()
3322 "Return t if the scan cache is up to date."
3323 (or (and verilog-scan-cache-preserving
3324 (eq verilog-scan-cache-preserving (current-buffer))
3325 verilog-scan-cache-tick)
3326 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3328 (defmacro verilog-save-scan-cache (&rest body)
3329 "Execute the BODY forms, allowing scan cache preservation within BODY.
3330 This requires that insertions must use `verilog-insert'."
3331 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3332 ;; Note this must work properly if there's multiple layers of calls
3333 ;; to verilog-save-scan-cache even with differing ticks.
3334 `(progn
3335 (unless (verilog-scan-cache-ok-p) ; Must be before let
3336 (setq verilog-scan-cache-tick nil))
3337 (let* ((verilog-scan-cache-preserving (current-buffer)))
3338 (progn ,@body))))
3340 (defun verilog-scan-region (beg end)
3341 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3342 This creates v-cmts properties where comments are in force."
3343 ;; Why properties and not overlays? Overlays have much slower non O(1)
3344 ;; lookup times.
3345 ;; This function is warm - called on every verilog-insert
3346 (save-excursion
3347 (save-match-data
3348 (verilog-save-buffer-state
3349 (let (pt)
3350 (goto-char beg)
3351 (while (< (point) end)
3352 (cond ((looking-at "//")
3353 (setq pt (point))
3354 (or (search-forward "\n" end t)
3355 (goto-char end))
3356 ;; "1+": The leading // or /* itself isn't considered as
3357 ;; being "inside" the comment, so that a (search-backward)
3358 ;; that lands at the start of the // won't mis-indicate
3359 ;; it's inside a comment. Also otherwise it would be
3360 ;; hard to find a commented out /*AS*/ vs one that isn't
3361 (put-text-property (1+ pt) (point) 'v-cmts t))
3362 ((looking-at "/\\*")
3363 (setq pt (point))
3364 (or (search-forward "*/" end t)
3365 ;; No error - let later code indicate it so we can
3366 ;; use inside functions on-the-fly
3367 ;;(error "%s: Unmatched /* */, at char %d"
3368 ;; (verilog-point-text) (point))
3369 (goto-char end))
3370 (put-text-property (1+ pt) (point) 'v-cmts t))
3371 ((looking-at "\"")
3372 (setq pt (point))
3373 (or (re-search-forward "[^\\]\"" end t) ; don't forward-char first, since we look for a non backslash first
3374 ;; No error - let later code indicate it so we can
3375 (goto-char end))
3376 (put-text-property (1+ pt) (point) 'v-cmts t))
3378 (forward-char 1)
3379 (if (re-search-forward "[/\"]" end t)
3380 (backward-char 1)
3381 (goto-char end))))))))))
3383 (defun verilog-scan ()
3384 "Parse the buffer, marking all comments with properties.
3385 Also assumes any text inserted since `verilog-scan-cache-tick'
3386 either is ok to parse as a non-comment, or `verilog-insert' was used."
3387 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
3388 (unless (verilog-scan-cache-ok-p)
3389 (save-excursion
3390 (verilog-save-buffer-state
3391 (when verilog-debug
3392 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3393 verilog-scan-cache-preserving verilog-scan-cache-tick
3394 (buffer-chars-modified-tick)))
3395 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
3396 (verilog-scan-region (point-min) (point-max))
3397 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
3398 (when verilog-debug (message "Scanning... done"))))))
3400 (defun verilog-scan-debug ()
3401 "For debugging, show with display face results of `verilog-scan'."
3402 (font-lock-mode 0)
3403 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3404 (save-excursion
3405 (goto-char (point-min))
3406 (remove-text-properties (point-min) (point-max) '(face nil))
3407 (while (not (eobp))
3408 (cond ((get-text-property (point) 'v-cmts)
3409 (put-text-property (point) (1+ (point)) `face 'underline)
3410 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3411 (forward-char 1))
3413 (goto-char (or (next-property-change (point)) (point-max))))))))
3415 (defun verilog-scan-and-debug ()
3416 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3417 (let (verilog-scan-cache-preserving
3418 verilog-scan-cache-tick)
3419 (goto-char (point-min))
3420 (verilog-scan)
3421 (verilog-scan-debug)))
3423 (defun verilog-inside-comment-or-string-p (&optional pos)
3424 "Check if optional point POS is inside a comment.
3425 This may require a slow pre-parse of the buffer with `verilog-scan'
3426 to establish comment properties on all text."
3427 ;; This function is very hot
3428 (verilog-scan)
3429 (if pos
3430 (and (>= pos (point-min))
3431 (get-text-property pos 'v-cmts))
3432 (get-text-property (point) 'v-cmts)))
3434 (defun verilog-insert (&rest stuff)
3435 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
3436 Any insert that includes a comment must have the entire comment
3437 inserted using a single call to `verilog-insert'."
3438 (let ((pt (point)))
3439 (while stuff
3440 (insert (car stuff))
3441 (setq stuff (cdr stuff)))
3442 (verilog-scan-region pt (point))))
3444 ;; More searching
3446 (defun verilog-declaration-end ()
3447 (search-forward ";"))
3449 (defun verilog-point-text (&optional pointnum)
3450 "Return text describing where POINTNUM or current point is (for errors).
3451 Use filename, if current buffer being edited shorten to just buffer name."
3452 (concat (or (and (equal (window-buffer) (current-buffer))
3453 (buffer-name))
3454 buffer-file-name
3455 (buffer-name))
3456 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
3458 (defun electric-verilog-backward-sexp ()
3459 "Move backward over one balanced expression."
3460 (interactive)
3461 ;; before that see if we are in a comment
3462 (verilog-backward-sexp))
3464 (defun electric-verilog-forward-sexp ()
3465 "Move forward over one balanced expression."
3466 (interactive)
3467 ;; before that see if we are in a comment
3468 (verilog-forward-sexp))
3470 (defun verilog-forward-sexp-function (arg)
3471 "Move forward ARG sexps."
3472 ;; Used by hs-minor-mode
3473 (if (< arg 0)
3474 (verilog-backward-sexp)
3475 (verilog-forward-sexp)))
3477 (defun verilog-backward-sexp ()
3478 (let ((reg)
3479 (elsec 1)
3480 (found nil)
3481 (st (point)))
3482 (if (not (looking-at "\\<"))
3483 (forward-word-strictly -1))
3484 (cond
3485 ((verilog-skip-backward-comment-or-string))
3486 ((looking-at "\\<else\\>")
3487 (setq reg (concat
3488 verilog-end-block-re
3489 "\\|\\(\\<else\\>\\)"
3490 "\\|\\(\\<if\\>\\)"))
3491 (while (and (not found)
3492 (verilog-re-search-backward reg nil 'move))
3493 (cond
3494 ((match-end 1) ; matched verilog-end-block-re
3495 ;; try to leap back to matching outward block by striding across
3496 ;; indent level changing tokens then immediately
3497 ;; previous line governs indentation.
3498 (verilog-leap-to-head))
3499 ((match-end 2) ; else, we're in deep
3500 (setq elsec (1+ elsec)))
3501 ((match-end 3) ; found it
3502 (setq elsec (1- elsec))
3503 (if (= 0 elsec)
3504 ;; Now previous line describes syntax
3505 (setq found 't))))))
3506 ((looking-at verilog-end-block-re)
3507 (verilog-leap-to-head))
3508 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3509 (cond
3510 ((match-end 1)
3511 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3512 ((match-end 2)
3513 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3514 ((match-end 3)
3515 (verilog-re-search-backward "\\<class\\>" nil 'move))
3516 ((match-end 4)
3517 (verilog-re-search-backward "\\<program\\>" nil 'move))
3518 ((match-end 5)
3519 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3520 ((match-end 6)
3521 (verilog-re-search-backward "\\<package\\>" nil 'move))
3523 (goto-char st)
3524 (backward-sexp 1))))
3526 (goto-char st)
3527 (backward-sexp)))))
3529 (defun verilog-forward-sexp ()
3530 (let ((reg)
3531 (md 2)
3532 (st (point))
3533 (nest 'yes))
3534 (if (not (looking-at "\\<"))
3535 (forward-word-strictly -1))
3536 (cond
3537 ((verilog-skip-forward-comment-or-string)
3538 (verilog-forward-syntactic-ws))
3539 ((looking-at verilog-beg-block-re-ordered)
3540 (cond
3541 ((match-end 1);
3542 ;; Search forward for matching end
3543 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
3544 ((match-end 2)
3545 ;; Search forward for matching endcase
3546 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique0?\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3547 (setq md 3) ; ender is third item in regexp
3549 ((match-end 4)
3550 ;; might be "disable fork" or "wait fork"
3551 (let
3552 (here)
3553 (if (or
3554 (looking-at verilog-disable-fork-re)
3555 (and (looking-at "fork")
3556 (progn
3557 (setq here (point)) ; sometimes a fork is just a fork
3558 (forward-word-strictly -1)
3559 (looking-at verilog-disable-fork-re))))
3560 (progn ; it is a disable fork; ignore it
3561 (goto-char (match-end 0))
3562 (forward-word-strictly 1)
3563 (setq reg nil))
3564 (progn ; it is a nice simple fork
3565 (goto-char here) ; return from looking for "disable fork"
3566 ;; Search forward for matching join
3567 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
3568 ((match-end 6)
3569 ;; Search forward for matching endclass
3570 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
3572 ((match-end 7)
3573 ;; Search forward for matching endtable
3574 (setq reg "\\<endtable\\>" )
3575 (setq nest 'no))
3576 ((match-end 8)
3577 ;; Search forward for matching endspecify
3578 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3579 ((match-end 9)
3580 ;; Search forward for matching endfunction
3581 (setq reg "\\<endfunction\\>" )
3582 (setq nest 'no))
3583 ((match-end 10)
3584 ;; Search forward for matching endfunction
3585 (setq reg "\\<endfunction\\>" )
3586 (setq nest 'no))
3587 ((match-end 14)
3588 ;; Search forward for matching endtask
3589 (setq reg "\\<endtask\\>" )
3590 (setq nest 'no))
3591 ((match-end 15)
3592 ;; Search forward for matching endtask
3593 (setq reg "\\<endtask\\>" )
3594 (setq nest 'no))
3595 ((match-end 19)
3596 ;; Search forward for matching endgenerate
3597 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
3598 ((match-end 20)
3599 ;; Search forward for matching endgroup
3600 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
3601 ((match-end 21)
3602 ;; Search forward for matching endproperty
3603 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
3604 ((match-end 25)
3605 ;; Search forward for matching endsequence
3606 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3607 (setq md 3)) ; 3 to get to endsequence in the reg above
3608 ((match-end 27)
3609 ;; Search forward for matching endclocking
3610 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
3611 (if (and reg
3612 (forward-word-strictly 1))
3613 (catch 'skip
3614 (if (eq nest 'yes)
3615 (let ((depth 1)
3616 here)
3617 (while (verilog-re-search-forward reg nil 'move)
3618 (cond
3619 ((match-end md) ; a closer in regular expression, so we are climbing out
3620 (setq depth (1- depth))
3621 (if (= 0 depth) ; we are out!
3622 (throw 'skip 1)))
3623 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3624 (setq here (point)) ; remember where we started
3625 (goto-char (match-beginning 1))
3626 (cond
3627 ((if (or
3628 (looking-at verilog-disable-fork-re)
3629 (and (looking-at "fork")
3630 (progn
3631 (forward-word-strictly -1)
3632 (looking-at verilog-disable-fork-re))))
3633 (progn ; it is a disable fork; another false alarm
3634 (goto-char (match-end 0)))
3635 (progn ; it is a simple fork (or has nothing to do with fork)
3636 (goto-char here)
3637 (setq depth (1+ depth))))))))))
3638 (if (verilog-re-search-forward reg nil 'move)
3639 (throw 'skip 1))))))
3641 ((looking-at (concat
3642 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3643 "\\(\\<primitive\\>\\)\\|"
3644 "\\(\\<class\\>\\)\\|"
3645 "\\(\\<program\\>\\)\\|"
3646 "\\(\\<interface\\>\\)\\|"
3647 "\\(\\<package\\>\\)"))
3648 (cond
3649 ((match-end 1)
3650 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3651 ((match-end 2)
3652 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3653 ((match-end 3)
3654 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3655 ((match-end 4)
3656 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3657 ((match-end 5)
3658 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3659 ((match-end 6)
3660 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3662 (goto-char st)
3663 (if (= (following-char) ?\) )
3664 (forward-char 1)
3665 (forward-sexp 1)))))
3667 (goto-char st)
3668 (if (= (following-char) ?\) )
3669 (forward-char 1)
3670 (forward-sexp 1))))))
3672 (defun verilog-declaration-beg ()
3673 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3677 ;; Mode
3679 (defvar verilog-which-tool 1)
3680 ;;;###autoload
3681 (define-derived-mode verilog-mode prog-mode "Verilog"
3682 "Major mode for editing Verilog code.
3683 \\<verilog-mode-map>
3684 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3685 AUTOs can improve coding efficiency.
3687 Use \\[verilog-faq] for a pointer to frequently asked questions.
3689 NEWLINE, TAB indents for Verilog code.
3690 Delete converts tabs to spaces as it moves back.
3692 Supports highlighting.
3694 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3695 with no args, if that value is non-nil.
3697 Variables controlling indentation/edit style:
3699 variable `verilog-indent-level' (default 3)
3700 Indentation of Verilog statements with respect to containing block.
3701 `verilog-indent-level-module' (default 3)
3702 Absolute indentation of Module level Verilog statements.
3703 Set to 0 to get initial and always statements lined up
3704 on the left side of your screen.
3705 `verilog-indent-level-declaration' (default 3)
3706 Indentation of declarations with respect to containing block.
3707 Set to 0 to get them list right under containing block.
3708 `verilog-indent-level-behavioral' (default 3)
3709 Indentation of first begin in a task or function block
3710 Set to 0 to get such code to lined up underneath the task or
3711 function keyword.
3712 `verilog-indent-level-directive' (default 1)
3713 Indentation of \\=`ifdef/\\=`endif blocks.
3714 `verilog-cexp-indent' (default 1)
3715 Indentation of Verilog statements broken across lines i.e.:
3716 if (a)
3717 begin
3718 `verilog-case-indent' (default 2)
3719 Indentation for case statements.
3720 `verilog-auto-newline' (default nil)
3721 Non-nil means automatically newline after semicolons and the punctuation
3722 mark after an end.
3723 `verilog-auto-indent-on-newline' (default t)
3724 Non-nil means automatically indent line after newline.
3725 `verilog-tab-always-indent' (default t)
3726 Non-nil means TAB in Verilog mode should always reindent the current line,
3727 regardless of where in the line point is when the TAB command is used.
3728 `verilog-indent-begin-after-if' (default t)
3729 Non-nil means to indent begin statements following a preceding
3730 if, else, while, for and repeat statements, if any. Otherwise,
3731 the begin is lined up with the preceding token. If t, you get:
3732 if (a)
3733 begin // amount of indent based on `verilog-cexp-indent'
3734 otherwise you get:
3735 if (a)
3736 begin
3737 `verilog-auto-endcomments' (default t)
3738 Non-nil means a comment /* ... */ is set after the ends which ends
3739 cases, tasks, functions and modules.
3740 The type and name of the object will be set between the braces.
3741 `verilog-minimum-comment-distance' (default 10)
3742 Minimum distance (in lines) between begin and end required before a comment
3743 will be inserted. Setting this variable to zero results in every
3744 end acquiring a comment; the default avoids too many redundant
3745 comments in tight quarters.
3746 `verilog-auto-lineup' (default `declarations')
3747 List of contexts where auto lineup of code should be done.
3749 Variables controlling other actions:
3751 `verilog-linter' (default `surelint')
3752 Unix program to call to run the lint checker. This is the default
3753 command for \\[compile-command] and \\[verilog-auto-save-compile].
3755 See \\[customize] for the complete list of variables.
3757 AUTO expansion functions are, in part:
3759 \\[verilog-auto] Expand AUTO statements.
3760 \\[verilog-delete-auto] Remove the AUTOs.
3761 \\[verilog-inject-auto] Insert AUTOs for the first time.
3763 Some other functions are:
3765 \\[verilog-complete-word] Complete word with appropriate possibilities.
3766 \\[verilog-mark-defun] Mark function.
3767 \\[verilog-beg-of-defun] Move to beginning of current function.
3768 \\[verilog-end-of-defun] Move to end of current function.
3769 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3771 \\[verilog-comment-region] Put marked area in a comment.
3772 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
3773 \\[verilog-insert-block] Insert begin ... end.
3774 \\[verilog-star-comment] Insert /* ... */.
3776 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
3777 \\[verilog-sk-begin] Insert a begin .. end block.
3778 \\[verilog-sk-case] Insert a case block, prompting for details.
3779 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3780 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
3781 \\[verilog-sk-header] Insert a header block at the top of file.
3782 \\[verilog-sk-initial] Insert an initial begin .. end block.
3783 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3784 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
3785 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3786 \\[verilog-sk-uvm-object] Insert an UVM Object block.
3787 \\[verilog-sk-uvm-component] Insert an UVM Component block.
3788 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3789 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3790 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3791 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3792 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3793 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3794 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3795 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3796 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3797 \\[verilog-sk-comment] Insert a comment block.
3798 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3799 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3800 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3801 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3802 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3803 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3804 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3805 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3806 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3808 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3809 Key bindings specific to `verilog-mode-map' are:
3811 \\{verilog-mode-map}"
3812 :abbrev-table verilog-mode-abbrev-table
3813 (set (make-local-variable 'beginning-of-defun-function)
3814 'verilog-beg-of-defun)
3815 (set (make-local-variable 'end-of-defun-function)
3816 'verilog-end-of-defun)
3817 (set-syntax-table verilog-mode-syntax-table)
3818 (set (make-local-variable 'indent-line-function)
3819 #'verilog-indent-line-relative)
3820 (set (make-local-variable 'comment-indent-function) 'verilog-comment-indent)
3821 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
3822 (set (make-local-variable 'comment-start) "// ")
3823 (set (make-local-variable 'comment-end) "")
3824 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3825 (set (make-local-variable 'comment-multi-line) nil)
3826 ;; Set up for compilation
3827 (setq verilog-which-tool 1)
3828 (setq verilog-tool 'verilog-linter)
3829 (verilog-set-compile-command)
3830 (when (boundp 'hack-local-variables-hook) ; Also modify any file-local-variables
3831 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3833 ;; Setting up menus
3834 (when (featurep 'xemacs)
3835 (easy-menu-add verilog-stmt-menu)
3836 (easy-menu-add verilog-menu)
3837 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
3839 ;; Stuff for GNU Emacs
3840 (set (make-local-variable 'font-lock-defaults)
3841 `((verilog-font-lock-keywords
3842 verilog-font-lock-keywords-1
3843 verilog-font-lock-keywords-2
3844 verilog-font-lock-keywords-3)
3845 nil nil nil
3846 ,(if (functionp 'syntax-ppss)
3847 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3848 ;; font-lock-beginning-of-syntax-function, so
3849 ;; font-lock-beginning-of-syntax-function, can't use
3850 ;; verilog-beg-of-defun.
3852 'verilog-beg-of-defun)))
3853 ;;------------------------------------------------------------
3854 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
3855 ;; all buffer local:
3856 (unless noninteractive ; Else can't see the result, and change hooks are slow
3857 (when (featurep 'xemacs)
3858 (make-local-hook 'font-lock-mode-hook)
3859 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3860 (make-local-hook 'after-change-functions))
3861 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3862 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3863 (add-hook 'after-change-functions 'verilog-highlight-region t t))
3865 ;; Tell imenu how to handle Verilog.
3866 (set (make-local-variable 'imenu-generic-expression)
3867 verilog-imenu-generic-expression)
3868 ;; Tell which-func-modes that imenu knows about verilog
3869 (when (and (boundp 'which-func-modes) (listp which-func-modes))
3870 (add-to-list 'which-func-modes 'verilog-mode))
3871 ;; hideshow support
3872 (when (boundp 'hs-special-modes-alist)
3873 (unless (assq 'verilog-mode hs-special-modes-alist)
3874 (setq hs-special-modes-alist
3875 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3876 verilog-forward-sexp-function)
3877 hs-special-modes-alist))))
3879 ;; Stuff for autos
3880 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3881 ;; verilog-mode-hook call added by define-derived-mode
3884 ;;; Integration with the speedbar
3887 (declare-function speedbar-add-supported-extension "speedbar" (extension))
3889 (defun verilog-speedbar-initialize ()
3890 "Initialize speedbar to understand `verilog-mode'."
3891 ;; Set Verilog file extensions (extracted from `auto-mode-alist')
3892 (let ((mode-alist auto-mode-alist))
3893 (while mode-alist
3894 (when (eq (cdar mode-alist) 'verilog-mode)
3895 (speedbar-add-supported-extension (caar mode-alist)))
3896 (setq mode-alist (cdr mode-alist)))))
3898 ;; If the speedbar is loaded, execute initialization instructions right away,
3899 ;; otherwise add the initialization instructions to the speedbar loader.
3900 (eval-after-load "speedbar" '(verilog-speedbar-initialize))
3903 ;;; Electric functions:
3906 (defun electric-verilog-terminate-line (&optional arg)
3907 "Terminate line and indent next line.
3908 With optional ARG, remove existing end of line comments."
3909 (interactive)
3910 ;; before that see if we are in a comment
3911 (let ((state (save-excursion (verilog-syntax-ppss))))
3912 (cond
3913 ((nth 7 state) ; Inside // comment
3914 (if (eolp)
3915 (progn
3916 (delete-horizontal-space)
3917 (newline))
3918 (progn
3919 (newline)
3920 (insert "// ")
3921 (beginning-of-line)))
3922 (verilog-indent-line))
3923 ((nth 4 state) ; Inside any comment (hence /**/)
3924 (newline)
3925 (verilog-more-comment))
3926 ((eolp)
3927 ;; First, check if current line should be indented
3928 (if (save-excursion
3929 (delete-horizontal-space)
3930 (beginning-of-line)
3931 (skip-chars-forward " \t")
3932 (if (looking-at verilog-auto-end-comment-lines-re)
3933 (let ((indent-str (verilog-indent-line)))
3934 ;; Maybe we should set some endcomments
3935 (if verilog-auto-endcomments
3936 (verilog-set-auto-endcomments indent-str arg))
3937 (end-of-line)
3938 (delete-horizontal-space)
3939 (if arg
3941 (newline))
3942 nil)
3943 (progn
3944 (end-of-line)
3945 (delete-horizontal-space)
3946 't)))
3947 ;; see if we should line up assignments
3948 (progn
3949 (if (or (eq 'all verilog-auto-lineup)
3950 (eq 'assignments verilog-auto-lineup))
3951 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
3952 (newline))
3953 (forward-line 1))
3954 ;; Indent next line
3955 (if verilog-auto-indent-on-newline
3956 (verilog-indent-line)))
3958 (newline)))))
3960 (defun electric-verilog-terminate-and-indent ()
3961 "Insert a newline and indent for the next statement."
3962 (interactive)
3963 (electric-verilog-terminate-line 1))
3965 (defun electric-verilog-semi ()
3966 "Insert `;' character and reindent the line."
3967 (interactive)
3968 (verilog-insert-last-command-event)
3970 (if (or (verilog-in-comment-or-string-p)
3971 (verilog-in-escaped-name-p))
3973 (save-excursion
3974 (beginning-of-line)
3975 (verilog-forward-ws&directives)
3976 (verilog-indent-line))
3977 (if (and verilog-auto-newline
3978 (not (verilog-parenthesis-depth)))
3979 (electric-verilog-terminate-line))))
3981 (defun electric-verilog-semi-with-comment ()
3982 "Insert `;' character, reindent the line and indent for comment."
3983 (interactive)
3984 (insert ";")
3985 (save-excursion
3986 (beginning-of-line)
3987 (verilog-indent-line))
3988 (indent-for-comment))
3990 (defun electric-verilog-colon ()
3991 "Insert `:' and do all indentations except line indent on this line."
3992 (interactive)
3993 (verilog-insert-last-command-event)
3994 ;; Do nothing if within string.
3995 (if (or
3996 (verilog-within-string)
3997 (not (verilog-in-case-region-p)))
3999 (save-excursion
4000 (let ((p (point))
4001 (lim (progn (verilog-beg-of-statement) (point))))
4002 (goto-char p)
4003 (verilog-backward-case-item lim)
4004 (verilog-indent-line)))
4005 ;; (let ((verilog-tab-always-indent nil))
4006 ;; (verilog-indent-line))
4009 ;;(defun electric-verilog-equal ()
4010 ;; "Insert `=', and do indentation if within block."
4011 ;; (interactive)
4012 ;; (verilog-insert-last-command-event)
4013 ;; Could auto line up expressions, but not yet
4014 ;; (if (eq (car (verilog-calculate-indent)) 'block)
4015 ;; (let ((verilog-tab-always-indent nil))
4016 ;; (verilog-indent-command)))
4017 ;; )
4019 (defun electric-verilog-tick ()
4020 "Insert back-tick, and indent to column 0 if this is a CPP directive."
4021 (interactive)
4022 (verilog-insert-last-command-event)
4023 (save-excursion
4024 (if (verilog-in-directive-p)
4025 (verilog-indent-line))))
4027 (defun electric-verilog-tab ()
4028 "Function called when TAB is pressed in Verilog mode."
4029 (interactive)
4030 ;; If verilog-tab-always-indent, indent the beginning of the line.
4031 (cond
4032 ;; The region is active, indent it.
4033 ((and (region-active-p)
4034 (not (eq (region-beginning) (region-end))))
4035 (indent-region (region-beginning) (region-end) nil))
4036 ((or verilog-tab-always-indent
4037 (save-excursion
4038 (skip-chars-backward " \t")
4039 (bolp)))
4040 (let* ((oldpnt (point))
4041 (boi-point
4042 (save-excursion
4043 (beginning-of-line)
4044 (skip-chars-forward " \t")
4045 (verilog-indent-line)
4046 (back-to-indentation)
4047 (point))))
4048 (if (< (point) boi-point)
4049 (back-to-indentation)
4050 (cond ((not verilog-tab-to-comment))
4051 ((not (eolp))
4052 (end-of-line))
4054 (indent-for-comment)
4055 (when (and (eolp) (= oldpnt (point)))
4056 ;; kill existing comment
4057 (beginning-of-line)
4058 (re-search-forward comment-start-skip oldpnt 'move)
4059 (goto-char (match-beginning 0))
4060 (skip-chars-backward " \t")
4061 (kill-region (point) oldpnt)))))))
4062 (t (progn (insert "\t")))))
4065 ;;; Interactive functions:
4068 (defun verilog-indent-buffer ()
4069 "Indent-region the entire buffer as Verilog code.
4070 To call this from the command line, see \\[verilog-batch-indent]."
4071 (interactive)
4072 (verilog-mode)
4073 (indent-region (point-min) (point-max) nil))
4075 (defun verilog-insert-block ()
4076 "Insert Verilog begin ... end; block in the code with right indentation."
4077 (interactive)
4078 (verilog-indent-line)
4079 (insert "begin")
4080 (electric-verilog-terminate-line)
4081 (save-excursion
4082 (electric-verilog-terminate-line)
4083 (insert "end")
4084 (beginning-of-line)
4085 (verilog-indent-line)))
4087 (defun verilog-star-comment ()
4088 "Insert Verilog star comment at point."
4089 (interactive)
4090 (verilog-indent-line)
4091 (insert "/*")
4092 (save-excursion
4093 (newline)
4094 (insert " */"))
4095 (newline)
4096 (insert " * "))
4098 (defun verilog-insert-1 (fmt max)
4099 "Use format string FMT to insert integers 0 to MAX - 1.
4100 Inserts one integer per line, at the current column. Stops early
4101 if it reaches the end of the buffer."
4102 (let ((col (current-column))
4103 (n 0))
4104 (save-excursion
4105 (while (< n max)
4106 (insert (format fmt n))
4107 (forward-line 1)
4108 ;; Note that this function does not bother to check for lines
4109 ;; shorter than col.
4110 (if (eobp)
4111 (setq n max)
4112 (setq n (1+ n))
4113 (move-to-column col))))))
4115 (defun verilog-insert-indices (max)
4116 "Insert a set of indices into a rectangle.
4117 The upper left corner is defined by point. Indices begin with 0
4118 and extend to the MAX - 1. If no prefix arg is given, the user
4119 is prompted for a value. The indices are surrounded by square
4120 brackets []. For example, the following code with the point
4121 located after the first `a' gives:
4123 a = b a[ 0] = b
4124 a = b a[ 1] = b
4125 a = b a[ 2] = b
4126 a = b a[ 3] = b
4127 a = b ==> insert-indices ==> a[ 4] = b
4128 a = b a[ 5] = b
4129 a = b a[ 6] = b
4130 a = b a[ 7] = b
4131 a = b a[ 8] = b"
4133 (interactive "NMAX: ")
4134 (verilog-insert-1 "[%3d]" max))
4136 (defun verilog-generate-numbers (max)
4137 "Insert a set of generated numbers into a rectangle.
4138 The upper left corner is defined by point. The numbers are padded to three
4139 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
4140 is supplied, then the user is prompted for the MAX number. Consider the
4141 following code fragment:
4143 buf buf buf buf000
4144 buf buf buf buf001
4145 buf buf buf buf002
4146 buf buf buf buf003
4147 buf buf ==> generate-numbers ==> buf buf004
4148 buf buf buf buf005
4149 buf buf buf buf006
4150 buf buf buf buf007
4151 buf buf buf buf008"
4153 (interactive "NMAX: ")
4154 (verilog-insert-1 "%3.3d" max))
4156 (defun verilog-mark-defun ()
4157 "Mark the current Verilog function (or procedure).
4158 This puts the mark at the end, and point at the beginning."
4159 (interactive)
4160 (if (featurep 'xemacs)
4161 (progn
4162 (push-mark (point))
4163 (verilog-end-of-defun)
4164 (push-mark (point))
4165 (verilog-beg-of-defun)
4166 (if (fboundp 'zmacs-activate-region)
4167 (zmacs-activate-region)))
4168 (mark-defun)))
4170 (defun verilog-comment-region (start end)
4171 ;; checkdoc-params: (start end)
4172 "Put the region into a Verilog comment.
4173 The comments that are in this area are \"deformed\":
4174 `*)' becomes `!(*' and `}' becomes `!{'.
4175 These deformed comments are returned to normal if you use
4176 \\[verilog-uncomment-region] to undo the commenting.
4178 The commented area starts with `verilog-exclude-str-start', and ends with
4179 `verilog-exclude-str-end'. But if you change these variables,
4180 \\[verilog-uncomment-region] won't recognize the comments."
4181 (interactive "r")
4182 (save-excursion
4183 ;; Insert start and endcomments
4184 (goto-char end)
4185 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
4186 (not (save-excursion (skip-chars-backward " \t") (bolp))))
4187 (forward-line 1)
4188 (beginning-of-line))
4189 (insert verilog-exclude-str-end)
4190 (setq end (point))
4191 (newline)
4192 (goto-char start)
4193 (beginning-of-line)
4194 (insert verilog-exclude-str-start)
4195 (newline)
4196 ;; Replace end-comments within commented area
4197 (goto-char end)
4198 (save-excursion
4199 (while (re-search-backward "\\*/" start t)
4200 (replace-match "*-/" t t)))
4201 (save-excursion
4202 (let ((s+1 (1+ start)))
4203 (while (re-search-backward "/\\*" s+1 t)
4204 (replace-match "/-*" t t))))))
4206 (defun verilog-uncomment-region ()
4207 "Uncomment a commented area; change deformed comments back to normal.
4208 This command does nothing if the pointer is not in a commented
4209 area. See also `verilog-comment-region'."
4210 (interactive)
4211 (save-excursion
4212 (let ((start (point))
4213 (end (point)))
4214 ;; Find the boundaries of the comment
4215 (save-excursion
4216 (setq start (progn (search-backward verilog-exclude-str-start nil t)
4217 (point)))
4218 (setq end (progn (search-forward verilog-exclude-str-end nil t)
4219 (point))))
4220 ;; Check if we're really inside a comment
4221 (if (or (equal start (point)) (<= end (point)))
4222 (message "Not standing within commented area.")
4223 (progn
4224 ;; Remove endcomment
4225 (goto-char end)
4226 (beginning-of-line)
4227 (let ((pos (point)))
4228 (end-of-line)
4229 (delete-region pos (1+ (point))))
4230 ;; Change comments back to normal
4231 (save-excursion
4232 (while (re-search-backward "\\*-/" start t)
4233 (replace-match "*/" t t)))
4234 (save-excursion
4235 (while (re-search-backward "/-\\*" start t)
4236 (replace-match "/*" t t)))
4237 ;; Remove start comment
4238 (goto-char start)
4239 (beginning-of-line)
4240 (let ((pos (point)))
4241 (end-of-line)
4242 (delete-region pos (1+ (point)))))))))
4244 (defun verilog-beg-of-defun ()
4245 "Move backward to the beginning of the current function or procedure."
4246 (interactive)
4247 (verilog-re-search-backward verilog-defun-re nil 'move))
4249 (defun verilog-beg-of-defun-quick ()
4250 "Move backward to the beginning of the current function or procedure.
4251 Uses `verilog-scan' cache."
4252 (interactive)
4253 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
4255 (defun verilog-end-of-defun ()
4256 "Move forward to the end of the current function or procedure."
4257 (interactive)
4258 (verilog-re-search-forward verilog-end-defun-re nil 'move))
4260 (defun verilog-get-end-of-defun ()
4261 (save-excursion
4262 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
4263 (point))
4265 (error "%s: Can't find endmodule" (verilog-point-text))
4266 (point-max)))))
4268 (defun verilog-label-be ()
4269 "Label matching begin ... end, fork ... join and case ... endcase statements."
4270 (interactive)
4271 (let ((cnt 0)
4272 (case-fold-search nil)
4273 (oldpos (point))
4274 (b (progn
4275 (verilog-beg-of-defun)
4276 (point-marker)))
4277 (e (progn
4278 (verilog-end-of-defun)
4279 (point-marker))))
4280 (goto-char (marker-position b))
4281 (if (> (- e b) 200)
4282 (message "Relabeling module..."))
4283 (while (and
4284 (> (marker-position e) (point))
4285 (verilog-re-search-forward
4286 verilog-auto-end-comment-lines-re
4287 nil 'move))
4288 (goto-char (match-beginning 0))
4289 (let ((indent-str (verilog-indent-line)))
4290 (verilog-set-auto-endcomments indent-str 't)
4291 (end-of-line)
4292 (delete-horizontal-space))
4293 (setq cnt (1+ cnt))
4294 (if (= 9 (% cnt 10))
4295 (message "%d..." cnt)))
4296 (goto-char oldpos)
4297 (if (or
4298 (> (- e b) 200)
4299 (> cnt 20))
4300 (message "%d lines auto commented" cnt))))
4302 (defun verilog-beg-of-statement ()
4303 "Move backward to beginning of statement."
4304 (interactive)
4305 ;; Move back token by token until we see the end
4306 ;; of some earlier line.
4307 (let (h)
4308 (while
4309 ;; If the current point does not begin a new
4310 ;; statement, as in the character ahead of us is a ';', or SOF
4311 ;; or the string after us unambiguously starts a statement,
4312 ;; or the token before us unambiguously ends a statement,
4313 ;; then move back a token and test again.
4314 (not (or
4315 ;; stop if beginning of buffer
4316 (bobp)
4317 ;; stop if looking at a pre-processor directive
4318 (looking-at "`\\w+")
4319 ;; stop if we find a ;
4320 (= (preceding-char) ?\;)
4321 ;; stop if we see a named coverpoint
4322 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4323 ;; keep going if we are in the middle of a word
4324 (not (or (looking-at "\\<") (forward-word-strictly -1)))
4325 ;; stop if we see an assertion (perhaps labeled)
4326 (and
4327 (looking-at "\\(\\w+\\W*:\\W*\\)?\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4328 (progn
4329 (setq h (point))
4330 (save-excursion
4331 (verilog-backward-token)
4332 (if (and (looking-at verilog-label-re)
4333 (not (looking-at verilog-end-block-re)))
4334 (setq h (point))))
4335 (goto-char h)))
4336 ;; stop if we see an extended complete reg, perhaps a complete one
4337 (and
4338 (looking-at verilog-complete-reg)
4339 (let* ((p (point)))
4340 (while (and (looking-at verilog-extended-complete-re)
4341 (progn (setq p (point))
4342 (verilog-backward-token)
4343 (/= p (point)))))
4344 (goto-char p)))
4345 ;; stop if we see a complete reg (previous found extended ones)
4346 (looking-at verilog-basic-complete-re)
4347 ;; stop if previous token is an ender
4348 (save-excursion
4349 (verilog-backward-token)
4350 (looking-at verilog-end-block-re))))
4351 (verilog-backward-syntactic-ws)
4352 (verilog-backward-token))
4353 ;; Now point is where the previous line ended.
4354 (verilog-forward-syntactic-ws)
4355 ;; Skip forward over any preprocessor directives, as they have wacky indentation
4356 (if (looking-at verilog-preprocessor-re)
4357 (progn (goto-char (match-end 0))
4358 (verilog-forward-syntactic-ws)))))
4360 (defun verilog-beg-of-statement-1 ()
4361 "Move backward to beginning of statement."
4362 (interactive)
4363 (if (verilog-in-comment-p)
4364 (verilog-backward-syntactic-ws))
4365 (let ((pt (point)))
4366 (catch 'done
4367 (while (not (looking-at verilog-complete-reg))
4368 (setq pt (point))
4369 (verilog-backward-syntactic-ws)
4370 (if (or (bolp)
4371 (= (preceding-char) ?\;)
4372 (progn
4373 (verilog-backward-token)
4374 (looking-at verilog-ends-re)))
4375 (progn
4376 (goto-char pt)
4377 (throw 'done t)))))
4378 (verilog-forward-syntactic-ws)))
4380 ;; (while (and
4381 ;; (not (looking-at verilog-complete-reg))
4382 ;; (not (bolp))
4383 ;; (not (= (preceding-char) ?\;)))
4384 ;; (verilog-backward-token)
4385 ;; (verilog-backward-syntactic-ws)
4386 ;; (setq pt (point)))
4387 ;; (goto-char pt)
4388 ;; ;(verilog-forward-syntactic-ws)
4390 (defun verilog-end-of-statement ()
4391 "Move forward to end of current statement."
4392 (interactive)
4393 (let ((nest 0) pos)
4394 (cond
4395 ((verilog-in-directive-p)
4396 (forward-line 1)
4397 (backward-char 1))
4399 ((looking-at verilog-beg-block-re)
4400 (verilog-forward-sexp))
4402 ((equal (char-after) ?\})
4403 (forward-char))
4405 ;; Skip to end of statement
4406 ((condition-case nil
4407 (setq pos
4408 (catch 'found
4409 (while t
4410 (forward-sexp 1)
4411 (verilog-skip-forward-comment-or-string)
4412 (if (eolp)
4413 (forward-line 1))
4414 (cond ((looking-at "[ \t]*;")
4415 (skip-chars-forward "^;")
4416 (forward-char 1)
4417 (throw 'found (point)))
4418 ((save-excursion
4419 (forward-sexp -1)
4420 (looking-at verilog-beg-block-re))
4421 (goto-char (match-beginning 0))
4422 (throw 'found nil))
4423 ((looking-at "[ \t]*)")
4424 (throw 'found (point)))
4425 ((eobp)
4426 (throw 'found (point)))
4430 (error nil))
4431 (if (not pos)
4432 ;; Skip a whole block
4433 (catch 'found
4434 (while t
4435 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4436 (setq nest (if (match-end 1)
4437 (1+ nest)
4438 (1- nest)))
4439 (cond ((eobp)
4440 (throw 'found (point)))
4441 ((= 0 nest)
4442 (throw 'found (verilog-end-of-statement))))))
4443 pos)))))
4445 (defun verilog-in-case-region-p ()
4446 "Return true if in a case region.
4447 More specifically, point @ in the line foo : @ begin"
4448 (interactive)
4449 (save-excursion
4450 (if (and
4451 (progn (verilog-forward-syntactic-ws)
4452 (looking-at "\\<begin\\>"))
4453 (progn (verilog-backward-syntactic-ws)
4454 (= (preceding-char) ?\:)))
4455 (catch 'found
4456 (let ((nest 1))
4457 (while t
4458 (verilog-re-search-backward
4459 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4460 "\\(\\<endcase\\>\\)\\>")
4461 nil 'move)
4462 (cond
4463 ((match-end 3)
4464 (setq nest (1+ nest)))
4465 ((match-end 2)
4466 (if (= nest 1)
4467 (throw 'found 1))
4468 (setq nest (1- nest)))
4470 (throw 'found (= nest 0)))))))
4471 nil)))
4473 (defun verilog-backward-up-list (arg)
4474 "Call `backward-up-list' ARG, ignoring comments."
4475 (let ((parse-sexp-ignore-comments t))
4476 (backward-up-list arg)))
4478 (defun verilog-forward-sexp-cmt (arg)
4479 "Call `forward-sexp' ARG, inside comments."
4480 (let ((parse-sexp-ignore-comments nil))
4481 (forward-sexp arg)))
4483 (defun verilog-forward-sexp-ign-cmt (arg)
4484 "Call `forward-sexp' ARG, ignoring comments."
4485 (let ((parse-sexp-ignore-comments t))
4486 (forward-sexp arg)))
4488 (defun verilog-in-generate-region-p ()
4489 "Return true if in a generate region.
4490 More specifically, after a generate and before an endgenerate."
4491 (interactive)
4492 (let ((nest 1))
4493 (save-excursion
4494 (catch 'done
4495 (while (and
4496 (/= nest 0)
4497 (verilog-re-search-backward
4498 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4499 (cond
4500 ((match-end 1) ; module - we have crawled out
4501 (throw 'done 1))
4502 ((match-end 2) ; generate
4503 (setq nest (1- nest)))
4504 ((match-end 3) ; endgenerate
4505 (setq nest (1+ nest))))))))
4506 (= nest 0) )) ; return nest
4508 (defun verilog-in-fork-region-p ()
4509 "Return true if between a fork and join."
4510 (interactive)
4511 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
4512 (nest 1))
4513 (save-excursion
4514 (while (and
4515 (/= nest 0)
4516 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4517 (cond
4518 ((match-end 1) ; fork
4519 (setq nest (1- nest)))
4520 ((match-end 2) ; join
4521 (setq nest (1+ nest)))))))
4522 (= nest 0) )) ; return nest
4524 (defun verilog-in-deferred-immediate-final-p ()
4525 "Return true if inside an `assert/assume/cover final' statement."
4526 (interactive)
4527 (and (looking-at "final")
4528 (verilog-looking-back "\\<\\(?:assert\\|assume\\|cover\\)\\>\\s-+" nil))
4531 (defun verilog-backward-case-item (lim)
4532 "Skip backward to nearest enclosing case item.
4533 Limit search to point LIM."
4534 (interactive)
4535 (let ((str 'nil)
4536 (lim1
4537 (progn
4538 (save-excursion
4539 (verilog-re-search-backward verilog-endcomment-reason-re
4540 lim 'move)
4541 (point)))))
4542 ;; Try to find the real :
4543 (if (save-excursion (search-backward ":" lim1 t))
4544 (let ((colon 0)
4545 b e )
4546 (while
4547 (and
4548 (< colon 1)
4549 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4550 lim1 'move))
4551 (cond
4552 ((match-end 1) ; [
4553 (setq colon (1+ colon))
4554 (if (>= colon 0)
4555 (error "%s: unbalanced [" (verilog-point-text))))
4556 ((match-end 2) ; ]
4557 (setq colon (1- colon)))
4559 ((match-end 3) ; :
4560 (setq colon (1+ colon)))))
4561 ;; Skip back to beginning of case item
4562 (skip-chars-backward "\t ")
4563 (verilog-skip-backward-comment-or-string)
4564 (setq e (point))
4565 (setq b
4566 (progn
4568 (verilog-re-search-backward
4569 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4570 (progn
4571 (cond
4572 ((match-end 1)
4573 (goto-char (match-end 1))
4574 (verilog-forward-ws&directives)
4575 (if (looking-at "(")
4576 (progn
4577 (forward-sexp)
4578 (verilog-forward-ws&directives)))
4579 (point))
4581 (goto-char (match-end 0))
4582 (verilog-forward-ws&directives)
4583 (point))))
4584 (error "Malformed case item"))))
4585 (setq str (buffer-substring b e))
4587 (setq e
4588 (string-match
4589 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4590 (setq str (concat (substring str 0 e) "...")))
4591 str)
4592 'nil)))
4594 ;;; Other functions:
4597 (defun verilog-kill-existing-comment ()
4598 "Kill auto comment on this line."
4599 (save-excursion
4600 (let* (
4601 (e (progn
4602 (end-of-line)
4603 (point)))
4604 (b (progn
4605 (beginning-of-line)
4606 (search-forward "//" e t))))
4607 (if b
4608 (delete-region (- b 2) e)))))
4610 (defconst verilog-directive-nest-re
4611 (concat "\\(`else\\>\\)\\|"
4612 "\\(`endif\\>\\)\\|"
4613 "\\(`if\\>\\)\\|"
4614 "\\(`ifdef\\>\\)\\|"
4615 "\\(`ifndef\\>\\)\\|"
4616 "\\(`elsif\\>\\)"))
4618 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4619 "Add ending comment with given INDENT-STR.
4620 With KILL-EXISTING-COMMENT, remove what was there before.
4621 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4622 Insert `// case expr ' if this line ends a case block.
4623 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
4624 Insert `// NAME ' if this line ends a function, task, module,
4625 primitive or interface named NAME."
4626 (save-excursion
4627 (cond
4628 (; Comment close preprocessor directives
4629 (and
4630 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4631 (or kill-existing-comment
4632 (not (save-excursion
4633 (end-of-line)
4634 (search-backward "//" (point-at-bol) t)))))
4635 (let ((nest 1) b e
4637 (else (if (match-end 2) "!" " ")))
4638 (end-of-line)
4639 (if kill-existing-comment
4640 (verilog-kill-existing-comment))
4641 (delete-horizontal-space)
4642 (save-excursion
4643 (backward-sexp 1)
4644 (while (and (/= nest 0)
4645 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4646 (cond
4647 ((match-end 1) ; `else
4648 (if (= nest 1)
4649 (setq else "!")))
4650 ((match-end 2) ; `endif
4651 (setq nest (1+ nest)))
4652 ((match-end 3) ; `if
4653 (setq nest (1- nest)))
4654 ((match-end 4) ; `ifdef
4655 (setq nest (1- nest)))
4656 ((match-end 5) ; `ifndef
4657 (setq nest (1- nest)))
4658 ((match-end 6) ; `elsif
4659 (if (= nest 1)
4660 (progn
4661 (setq else "!")
4662 (setq nest 0))))))
4663 (if (match-end 0)
4664 (setq
4665 m (buffer-substring
4666 (match-beginning 0)
4667 (match-end 0))
4668 b (progn
4669 (skip-chars-forward "^ \t")
4670 (verilog-forward-syntactic-ws)
4671 (point))
4672 e (progn
4673 (skip-chars-forward "a-zA-Z0-9_")
4674 (point)))))
4675 (if b
4676 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4677 (insert (concat " // " else m " " (buffer-substring b e))))
4678 (progn
4679 (insert " // unmatched `else, `elsif or `endif")
4680 (ding 't)))))
4682 (; Comment close case/class/function/task/module and named block
4683 (and (looking-at "\\<end")
4684 (or kill-existing-comment
4685 (not (save-excursion
4686 (end-of-line)
4687 (search-backward "//" (point-at-bol) t)))))
4688 (let ((type (car indent-str)))
4689 (unless (eq type 'declaration)
4690 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ; ignore named ends
4691 (if (looking-at verilog-end-block-ordered-re)
4692 (cond
4693 (;- This is a case block; search back for the start of this case
4694 (match-end 1) ; of verilog-end-block-ordered-re
4696 (let ((err 't)
4697 (str "UNMATCHED!!"))
4698 (save-excursion
4699 (verilog-leap-to-head)
4700 (cond
4701 ((looking-at "\\<randcase\\>")
4702 (setq str "randcase")
4703 (setq err nil))
4704 ((looking-at "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4705 (goto-char (match-end 0))
4706 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4707 (setq err nil))
4709 (end-of-line)
4710 (if kill-existing-comment
4711 (verilog-kill-existing-comment))
4712 (delete-horizontal-space)
4713 (insert (concat " // " str ))
4714 (if err (ding 't))))
4716 (;- This is a begin..end block
4717 (match-end 2) ; of verilog-end-block-ordered-re
4718 (let ((str " // UNMATCHED !!")
4719 (err 't)
4720 (here (point))
4721 there
4722 cntx)
4723 (save-excursion
4724 (verilog-leap-to-head)
4725 (setq there (point))
4726 (if (not (match-end 0))
4727 (progn
4728 (goto-char here)
4729 (end-of-line)
4730 (if kill-existing-comment
4731 (verilog-kill-existing-comment))
4732 (delete-horizontal-space)
4733 (insert str)
4734 (ding 't))
4735 (let ((lim
4736 (save-excursion (verilog-beg-of-defun) (point)))
4737 (here (point)))
4738 (cond
4739 (;-- handle named block differently
4740 (looking-at verilog-named-block-re)
4741 (search-forward ":")
4742 (setq there (point))
4743 (setq str (verilog-get-expr))
4744 (setq err nil)
4745 (setq str (concat " // block: " str )))
4747 ((verilog-in-case-region-p) ;-- handle case item differently
4748 (goto-char here)
4749 (setq str (verilog-backward-case-item lim))
4750 (setq there (point))
4751 (setq err nil)
4752 (setq str (concat " // case: " str )))
4754 (;- try to find "reason" for this begin
4755 (cond
4757 (eq here (progn
4758 ;; (verilog-backward-token)
4759 (verilog-beg-of-statement)
4760 (point)))
4761 (setq err nil)
4762 (setq str ""))
4763 ((looking-at verilog-endcomment-reason-re)
4764 (setq there (match-end 0))
4765 (setq cntx (concat (match-string 0) " "))
4766 (cond
4767 (;- begin
4768 (match-end 1)
4769 (setq err nil)
4770 (save-excursion
4771 (if (and (verilog-continued-line)
4772 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4773 (progn
4774 (goto-char (match-end 0))
4775 (setq there (point))
4776 (setq str
4777 (concat " // " (match-string 0) " " (verilog-get-expr))))
4778 (setq str ""))))
4780 (;- else
4781 (match-end 2)
4782 (let ((nest 0)
4783 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4784 (catch 'skip
4785 (while (verilog-re-search-backward reg nil 'move)
4786 (cond
4787 ((match-end 1) ; begin
4788 (setq nest (1- nest)))
4789 ((match-end 2) ; end
4790 (setq nest (1+ nest)))
4791 ((match-end 3)
4792 (if (= 0 nest)
4793 (progn
4794 (goto-char (match-end 0))
4795 (setq there (point))
4796 (setq err nil)
4797 (setq str (verilog-get-expr))
4798 (setq str (concat " // else: !if" str ))
4799 (throw 'skip 1))))
4800 ((match-end 4)
4801 (if (= 0 nest)
4802 (progn
4803 (goto-char (match-end 0))
4804 (setq there (point))
4805 (setq err nil)
4806 (setq str (verilog-get-expr))
4807 (setq str (concat " // else: !assert " str ))
4808 (throw 'skip 1)))))))))
4809 (;- end else
4810 (match-end 3)
4811 (goto-char there)
4812 (let ((nest 0)
4813 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4814 (catch 'skip
4815 (while (verilog-re-search-backward reg nil 'move)
4816 (cond
4817 ((match-end 1) ; begin
4818 (setq nest (1- nest)))
4819 ((match-end 2) ; end
4820 (setq nest (1+ nest)))
4821 ((match-end 3)
4822 (if (= 0 nest)
4823 (progn
4824 (goto-char (match-end 0))
4825 (setq there (point))
4826 (setq err nil)
4827 (setq str (verilog-get-expr))
4828 (setq str (concat " // else: !if" str ))
4829 (throw 'skip 1))))
4830 ((match-end 4)
4831 (if (= 0 nest)
4832 (progn
4833 (goto-char (match-end 0))
4834 (setq there (point))
4835 (setq err nil)
4836 (setq str (verilog-get-expr))
4837 (setq str (concat " // else: !assert " str ))
4838 (throw 'skip 1)))))))))
4840 (; always, always_comb, always_latch w/o @...
4841 (match-end 5)
4842 (goto-char (match-end 0))
4843 (setq there (point))
4844 (setq err nil)
4845 (setq str (concat " // " cntx )))
4847 (;- task/function/initial et cetera
4849 (match-end 0)
4850 (goto-char (match-end 0))
4851 (setq there (point))
4852 (setq err nil)
4853 (setq str (concat " // " cntx (verilog-get-expr))))
4855 (;-- otherwise...
4856 (setq str " // auto-endcomment confused "))))
4858 ((and
4859 (verilog-in-case-region-p) ;-- handle case item differently
4860 (progn
4861 (setq there (point))
4862 (goto-char here)
4863 (setq str (verilog-backward-case-item lim))))
4864 (setq err nil)
4865 (setq str (concat " // case: " str )))
4867 ((verilog-in-fork-region-p)
4868 (setq err nil)
4869 (setq str " // fork branch" ))
4871 ((looking-at "\\<end\\>")
4872 ;; HERE
4873 (forward-word-strictly 1)
4874 (verilog-forward-syntactic-ws)
4875 (setq err nil)
4876 (setq str (verilog-get-expr))
4877 (setq str (concat " // " cntx str )))
4879 ))))
4880 (goto-char here)
4881 (end-of-line)
4882 (if kill-existing-comment
4883 (verilog-kill-existing-comment))
4884 (delete-horizontal-space)
4885 (if (or err
4886 (> (count-lines here there) verilog-minimum-comment-distance))
4887 (insert str))
4888 (if err (ding 't))
4889 ))))
4890 (;- this is endclass, which can be nested
4891 (match-end 11) ; of verilog-end-block-ordered-re
4892 ;;(goto-char there)
4893 (let ((nest 0)
4894 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4895 string)
4896 (save-excursion
4897 (catch 'skip
4898 (while (verilog-re-search-backward reg nil 'move)
4899 (cond
4900 ((match-end 3) ; endclass
4901 (ding 't)
4902 (setq string "unmatched endclass")
4903 (throw 'skip 1))
4905 ((match-end 2) ; endclass
4906 (setq nest (1+ nest)))
4908 ((match-end 1) ; class
4909 (setq nest (1- nest))
4910 (if (< nest 0)
4911 (progn
4912 (goto-char (match-end 0))
4913 (let (b e)
4914 (setq b (progn
4915 (skip-chars-forward "^ \t")
4916 (verilog-forward-ws&directives)
4917 (point))
4918 e (progn
4919 (skip-chars-forward "a-zA-Z0-9_")
4920 (point)))
4921 (setq string (buffer-substring b e)))
4922 (throw 'skip 1))))
4923 ))))
4924 (end-of-line)
4925 (if kill-existing-comment
4926 (verilog-kill-existing-comment))
4927 (delete-horizontal-space)
4928 (insert (concat " // " string ))))
4930 (; - this is end{function,generate,task,module,primitive,table,generate}
4931 ;; - which can not be nested.
4933 (let (string reg (name-re nil))
4934 (end-of-line)
4935 (if kill-existing-comment
4936 (save-match-data
4937 (verilog-kill-existing-comment)))
4938 (delete-horizontal-space)
4939 (backward-sexp)
4940 (cond
4941 ((match-end 5) ; of verilog-end-block-ordered-re
4942 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4943 (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
4944 ((match-end 6) ; of verilog-end-block-ordered-re
4945 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4946 (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
4947 ((match-end 7) ; of verilog-end-block-ordered-re
4948 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4949 ((match-end 8) ; of verilog-end-block-ordered-re
4950 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4951 ((match-end 9) ; of verilog-end-block-ordered-re
4952 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4953 ((match-end 10) ; of verilog-end-block-ordered-re
4954 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4955 ((match-end 11) ; of verilog-end-block-ordered-re
4956 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4957 ((match-end 12) ; of verilog-end-block-ordered-re
4958 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4959 ((match-end 13) ; of verilog-end-block-ordered-re
4960 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4961 ((match-end 14) ; of verilog-end-block-ordered-re
4962 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4963 ((match-end 15) ; of verilog-end-block-ordered-re
4964 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4965 ((match-end 16) ; of verilog-end-block-ordered-re
4966 (setq reg "\\(\\<property\\>\\)\\|\\<endproperty\\>"))
4968 (t (error "Problem in verilog-set-auto-endcomments")))
4969 (let (b e)
4970 (save-excursion
4971 (verilog-re-search-backward reg nil 'move)
4972 (cond
4973 ((match-end 1)
4974 (setq b (progn
4975 (skip-chars-forward "^ \t")
4976 (verilog-forward-ws&directives)
4977 (if (looking-at "static\\|automatic")
4978 (progn
4979 (goto-char (match-end 0))
4980 (verilog-forward-ws&directives)))
4981 (if (and name-re (verilog-re-search-forward name-re nil 'move))
4982 (progn
4983 (goto-char (match-beginning 0))
4984 (verilog-forward-ws&directives)))
4985 (point))
4986 e (progn
4987 (skip-chars-forward "a-zA-Z0-9_")
4988 (point)))
4989 (setq string (buffer-substring b e)))
4991 (ding 't)
4992 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4993 (end-of-line)
4994 (insert (concat " // " string )))
4995 ))))))))))
4997 (defun verilog-get-expr()
4998 "Grab expression at point, e.g., case ( a | b & (c ^d))."
4999 (let* ((b (progn
5000 (verilog-forward-syntactic-ws)
5001 (skip-chars-forward " \t")
5002 (point)))
5003 (e (let ((par 1))
5004 (cond
5005 ((looking-at "@")
5006 (forward-char 1)
5007 (verilog-forward-syntactic-ws)
5008 (if (looking-at "(")
5009 (progn
5010 (forward-char 1)
5011 (while (and (/= par 0)
5012 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
5013 (cond
5014 ((match-end 1)
5015 (setq par (1+ par)))
5016 ((match-end 2)
5017 (setq par (1- par)))))))
5018 (point))
5019 ((looking-at "(")
5020 (forward-char 1)
5021 (while (and (/= par 0)
5022 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
5023 (cond
5024 ((match-end 1)
5025 (setq par (1+ par)))
5026 ((match-end 2)
5027 (setq par (1- par)))))
5028 (point))
5029 ((looking-at "\\[")
5030 (forward-char 1)
5031 (while (and (/= par 0)
5032 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
5033 (cond
5034 ((match-end 1)
5035 (setq par (1+ par)))
5036 ((match-end 2)
5037 (setq par (1- par)))))
5038 (verilog-forward-syntactic-ws)
5039 (skip-chars-forward "^ \t\n\f")
5040 (point))
5041 ((looking-at "/[/\\*]")
5044 (skip-chars-forward "^: \t\n\f")
5045 (point)))))
5046 (str (buffer-substring b e)))
5047 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
5048 (setq str (concat (substring str 0 e) "...")))
5049 str))
5051 (defun verilog-expand-vector ()
5052 "Take a signal vector on the current line and expand it to multiple lines.
5053 Useful for creating tri's and other expanded fields."
5054 (interactive)
5055 (verilog-expand-vector-internal "[" "]"))
5057 (defun verilog-expand-vector-internal (bra ket)
5058 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
5059 (save-excursion
5060 (forward-line 0)
5061 (let ((signal-string (buffer-substring (point)
5062 (progn
5063 (end-of-line) (point)))))
5064 (if (string-match
5065 (concat "\\(.*\\)"
5066 (regexp-quote bra)
5067 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
5068 (regexp-quote ket)
5069 "\\(.*\\)$") signal-string)
5070 (let* ((sig-head (match-string 1 signal-string))
5071 (vec-start (string-to-number (match-string 2 signal-string)))
5072 (vec-end (if (= (match-beginning 3) (match-end 3))
5073 vec-start
5074 (string-to-number
5075 (substring signal-string (1+ (match-beginning 3))
5076 (match-end 3)))))
5077 (vec-range
5078 (if (= (match-beginning 4) (match-end 4))
5080 (string-to-number
5081 (substring signal-string (+ 2 (match-beginning 4))
5082 (match-end 4)))))
5083 (sig-tail (match-string 5 signal-string))
5084 vec)
5085 ;; Decode vectors
5086 (setq vec nil)
5087 (if (< vec-range 0)
5088 (let ((tmp vec-start))
5089 (setq vec-start vec-end
5090 vec-end tmp
5091 vec-range (- vec-range))))
5092 (if (< vec-end vec-start)
5093 (while (<= vec-end vec-start)
5094 (setq vec (append vec (list vec-start)))
5095 (setq vec-start (- vec-start vec-range)))
5096 (while (<= vec-start vec-end)
5097 (setq vec (append vec (list vec-start)))
5098 (setq vec-start (+ vec-start vec-range))))
5100 ;; Delete current line
5101 (delete-region (point) (progn (forward-line 0) (point)))
5103 ;; Expand vector
5104 (while vec
5105 (insert (concat sig-head bra
5106 (int-to-string (car vec)) ket sig-tail "\n"))
5107 (setq vec (cdr vec)))
5108 (delete-char -1)
5110 )))))
5112 (defun verilog-strip-comments ()
5113 "Strip all comments from the Verilog code."
5114 (interactive)
5115 (goto-char (point-min))
5116 (while (re-search-forward "//" nil t)
5117 (if (verilog-within-string)
5118 (re-search-forward "\"" nil t)
5119 (if (verilog-in-star-comment-p)
5120 (re-search-forward "\\*/" nil t)
5121 (let ((bpt (- (point) 2)))
5122 (end-of-line)
5123 (delete-region bpt (point))))))
5125 (goto-char (point-min))
5126 (while (re-search-forward "/\\*" nil t)
5127 (if (verilog-within-string)
5128 (re-search-forward "\"" nil t)
5129 (let ((bpt (- (point) 2)))
5130 (re-search-forward "\\*/")
5131 (delete-region bpt (point))))))
5133 (defun verilog-one-line ()
5134 "Convert structural Verilog instances to occupy one line."
5135 (interactive)
5136 (goto-char (point-min))
5137 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
5138 (replace-match "\\1 " nil nil)))
5140 (defun verilog-linter-name ()
5141 "Return name of linter, either surelint or verilint."
5142 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
5143 compile-command))
5144 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
5145 verilog-linter)))
5146 (cond ((equal compile-word1 "surelint") `surelint)
5147 ((equal compile-word1 "verilint") `verilint)
5148 ((equal lint-word1 "surelint") `surelint)
5149 ((equal lint-word1 "verilint") `verilint)
5150 (t `surelint)))) ; back compatibility
5152 (defun verilog-lint-off ()
5153 "Convert a Verilog linter warning line into a disable statement.
5154 For example:
5155 pci_bfm_null.v, line 46: Unused input: pci_rst_
5156 becomes a comment for the appropriate tool.
5158 The first word of the `compile-command' or `verilog-linter'
5159 variables is used to determine which product is being used.
5161 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
5162 (interactive)
5163 (let ((linter (verilog-linter-name)))
5164 (cond ((equal linter `surelint)
5165 (verilog-surelint-off))
5166 ((equal linter `verilint)
5167 (verilog-verilint-off))
5168 (t (error "Linter name not set")))))
5170 (defvar compilation-last-buffer)
5171 (defvar next-error-last-buffer)
5173 (defun verilog-surelint-off ()
5174 "Convert a SureLint warning line into a disable statement.
5175 Run from Verilog source window; assumes there is a *compile* buffer
5176 with point set appropriately.
5178 For example:
5179 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
5180 becomes:
5181 // surefire lint_line_off UDDONX"
5182 (interactive)
5183 (let ((buff (if (boundp 'next-error-last-buffer)
5184 next-error-last-buffer
5185 compilation-last-buffer)))
5186 (when (buffer-live-p buff)
5187 (save-excursion
5188 (switch-to-buffer buff)
5189 (beginning-of-line)
5190 (when
5191 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
5192 (let* ((code (match-string 2))
5193 (file (match-string 3))
5194 (line (match-string 4))
5195 (buffer (get-file-buffer file))
5196 dir filename)
5197 (unless buffer
5198 (progn
5199 (setq buffer
5200 (and (file-exists-p file)
5201 (find-file-noselect file)))
5202 (or buffer
5203 (let* ((pop-up-windows t))
5204 (let ((name (expand-file-name
5205 (read-file-name
5206 (format "Find this error in: (default %s) "
5207 file)
5208 dir file t))))
5209 (if (file-directory-p name)
5210 (setq name (expand-file-name filename name)))
5211 (setq buffer
5212 (and (file-exists-p name)
5213 (find-file-noselect name))))))))
5214 (switch-to-buffer buffer)
5215 (goto-char (point-min))
5216 (forward-line (- (string-to-number line)))
5217 (end-of-line)
5218 (catch 'already
5219 (cond
5220 ((verilog-in-slash-comment-p)
5221 (re-search-backward "//")
5222 (cond
5223 ((looking-at "// surefire lint_off_line ")
5224 (goto-char (match-end 0))
5225 (let ((lim (point-at-eol)))
5226 (if (re-search-forward code lim 'move)
5227 (throw 'already t)
5228 (insert (concat " " code)))))
5231 ((verilog-in-star-comment-p)
5232 (re-search-backward "/\\*")
5233 (insert (format " // surefire lint_off_line %6s" code )))
5235 (insert (format " // surefire lint_off_line %6s" code ))
5236 )))))))))
5238 (defun verilog-verilint-off ()
5239 "Convert a Verilint warning line into a disable statement.
5241 For example:
5242 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
5243 becomes:
5244 //Verilint 240 off // WARNING: Unused input"
5245 (interactive)
5246 (save-excursion
5247 (beginning-of-line)
5248 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
5249 (replace-match (format
5250 ;; %3s makes numbers 1-999 line up nicely
5251 "\\1//Verilint %3s off // WARNING: \\3"
5252 (match-string 2)))
5253 (beginning-of-line)
5254 (verilog-indent-line))))
5256 (defun verilog-auto-save-compile ()
5257 "Update automatics with \\[verilog-auto], save the buffer, and compile."
5258 (interactive)
5259 (verilog-auto) ; Always do it for safety
5260 (save-buffer)
5261 (compile compile-command))
5263 (defun verilog-preprocess (&optional command filename)
5264 "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
5265 Takes optional COMMAND or defaults to `verilog-preprocessor', and
5266 FILENAME to find directory to run in, or defaults to `buffer-file-name'."
5267 (interactive
5268 (list
5269 (let ((default (verilog-expand-command verilog-preprocessor)))
5270 (set (make-local-variable `verilog-preprocessor)
5271 (read-from-minibuffer "Run Preprocessor (like this): "
5272 default nil nil
5273 'verilog-preprocess-history default)))))
5274 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
5275 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
5276 (dir (file-name-directory (or filename buffer-file-name)))
5277 (cmd (concat "cd " dir "; " command)))
5278 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
5279 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
5280 (insert (concat "// " cmd "\n"))
5281 (call-process shell-file-name nil t nil shell-command-switch cmd)
5282 (verilog-mode)
5283 ;; Without this force, it takes a few idle seconds
5284 ;; to get the color, which is very jarring
5285 (unless (fboundp 'font-lock-ensure)
5286 ;; We should use font-lock-ensure in preference to
5287 ;; font-lock-fontify-buffer, but IIUC the problem this is supposed to
5288 ;; solve only appears in Emacsen older than font-lock-ensure anyway.
5289 ;; So avoid bytecomp's interactive-only by going through intern.
5290 (when fontlocked (funcall (intern "font-lock-fontify-buffer"))))))))
5292 ;;; Batch:
5295 (defun verilog-warn (string &rest args)
5296 "Print a warning with `format' using STRING and optional ARGS."
5297 (apply 'message (concat "%%Warning: " string) args))
5299 (defun verilog-warn-error (string &rest args)
5300 "Call `error' using STRING and optional ARGS.
5301 If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
5302 (if verilog-warn-fatal
5303 (apply 'error string args)
5304 (apply 'verilog-warn string args)))
5306 (defmacro verilog-batch-error-wrapper (&rest body)
5307 "Execute BODY and add error prefix to any errors found.
5308 This lets programs calling batch mode to easily extract error messages."
5309 `(let ((verilog-warn-fatal nil))
5310 (condition-case err
5311 (progn ,@body)
5312 (error
5313 (error "%%Error: %s%s" (error-message-string err)
5314 (if (featurep 'xemacs) "\n" "")))))) ; XEmacs forgets to add a newline
5316 (defun verilog-batch-execute-func (funref &optional no-save)
5317 "Internal processing of a batch command.
5318 Runs FUNREF on all command arguments.
5319 Save the result unless optional NO-SAVE is t."
5320 (verilog-batch-error-wrapper
5321 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5322 ;; However, this function is called only when Emacs is being used as
5323 ;; a standalone language instead of as an editor, so we'll live.
5325 ;; General globals needed
5326 (setq make-backup-files nil)
5327 (setq-default make-backup-files nil)
5328 (setq enable-local-variables t)
5329 (setq enable-local-eval t)
5330 (setq create-lockfiles nil)
5331 ;; Make sure any sub-files we read get proper mode
5332 (setq-default major-mode 'verilog-mode)
5333 ;; Ditto files already read in
5334 ;; Remember buffer list, so don't later pickup any verilog-getopt files
5335 (let ((orig-buffer-list (buffer-list)))
5336 (mapc (lambda (buf)
5337 (when (buffer-file-name buf)
5338 (with-current-buffer buf
5339 (verilog-mode)
5340 (verilog-auto-reeval-locals)
5341 (verilog-getopt-flags))))
5342 orig-buffer-list)
5343 ;; Process the files
5344 (mapcar (lambda (buf)
5345 (when (buffer-file-name buf)
5346 (save-excursion
5347 (if (not (file-exists-p (buffer-file-name buf)))
5348 (error
5349 "File not found: %s" (buffer-file-name buf)))
5350 (message "Processing %s" (buffer-file-name buf))
5351 (set-buffer buf)
5352 (funcall funref)
5353 (when (and (not no-save)
5354 (buffer-modified-p)) ; Avoid "no changes to be saved"
5355 (save-buffer)))))
5356 orig-buffer-list))))
5358 (defun verilog-batch-auto ()
5359 "For use with --batch, perform automatic expansions as a stand-alone tool.
5360 This sets up the appropriate Verilog mode environment, updates automatics
5361 with \\[verilog-auto] on all command-line files, and saves the buffers.
5362 For proper results, multiple filenames need to be passed on the command
5363 line in bottom-up order."
5364 (unless noninteractive
5365 (error "Use verilog-batch-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5366 (verilog-batch-execute-func `verilog-auto))
5368 (defun verilog-batch-delete-auto ()
5369 "For use with --batch, perform automatic deletion as a stand-alone tool.
5370 This sets up the appropriate Verilog mode environment, deletes automatics
5371 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5372 (unless noninteractive
5373 (error "Use verilog-batch-delete-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5374 (verilog-batch-execute-func `verilog-delete-auto))
5376 (defun verilog-batch-delete-trailing-whitespace ()
5377 "For use with --batch, perform whitespace deletion as a stand-alone tool.
5378 This sets up the appropriate Verilog mode environment, removes
5379 whitespace with \\[verilog-delete-trailing-whitespace] on all
5380 command-line files, and saves the buffers."
5381 (unless noninteractive
5382 (error "Use verilog-batch-delete-trailing-whitespace only with --batch")) ; Otherwise we'd mess up buffer modes
5383 (verilog-batch-execute-func `verilog-delete-trailing-whitespace))
5385 (defun verilog-batch-diff-auto ()
5386 "For use with --batch, perform automatic differences as a stand-alone tool.
5387 This sets up the appropriate Verilog mode environment, expand automatics
5388 with \\[verilog-diff-auto] on all command-line files, and reports an error
5389 if any differences are observed. This is appropriate for adding to regressions
5390 to insure automatics are always properly maintained."
5391 (unless noninteractive
5392 (error "Use verilog-batch-diff-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5393 (verilog-batch-execute-func `verilog-diff-auto t))
5395 (defun verilog-batch-inject-auto ()
5396 "For use with --batch, perform automatic injection as a stand-alone tool.
5397 This sets up the appropriate Verilog mode environment, injects new automatics
5398 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5399 For proper results, multiple filenames need to be passed on the command
5400 line in bottom-up order."
5401 (unless noninteractive
5402 (error "Use verilog-batch-inject-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5403 (verilog-batch-execute-func `verilog-inject-auto))
5405 (defun verilog-batch-indent ()
5406 "For use with --batch, reindent an entire file as a stand-alone tool.
5407 This sets up the appropriate Verilog mode environment, calls
5408 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5409 (unless noninteractive
5410 (error "Use verilog-batch-indent only with --batch")) ; Otherwise we'd mess up buffer modes
5411 (verilog-batch-execute-func `verilog-indent-buffer))
5413 ;;; Indentation:
5415 (defconst verilog-indent-alist
5416 '((block . (+ ind verilog-indent-level))
5417 (case . (+ ind verilog-case-indent))
5418 (cparenexp . (+ ind verilog-indent-level))
5419 (cexp . (+ ind verilog-cexp-indent))
5420 (defun . verilog-indent-level-module)
5421 (declaration . verilog-indent-level-declaration)
5422 (directive . (verilog-calculate-indent-directive))
5423 (tf . verilog-indent-level)
5424 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5425 (statement . ind)
5426 (cpp . 0)
5427 (comment . (verilog-comment-indent))
5428 (unknown . 3)
5429 (string . 0)))
5431 (defun verilog-continued-line-1 (lim)
5432 "Return true if this is a continued line.
5433 Set point to where line starts. Limit search to point LIM."
5434 (let ((continued 't))
5435 (if (eq 0 (forward-line -1))
5436 (progn
5437 (end-of-line)
5438 (verilog-backward-ws&directives lim)
5439 (if (bobp)
5440 (setq continued nil)
5441 (setq continued (verilog-backward-token))))
5442 (setq continued nil))
5443 continued))
5445 (defun verilog-calculate-indent ()
5446 "Calculate the indent of the current Verilog line.
5447 Examine previous lines. Once a line is found that is definitive as to the
5448 type of the current line, return that lines' indent level and its type.
5449 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
5450 (save-excursion
5451 (let* ((starting_position (point))
5452 (case-fold-search nil)
5453 (par 0)
5454 (begin (looking-at "[ \t]*begin\\>"))
5455 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5456 (structres nil)
5457 (type (catch 'nesting
5458 ;; Keep working backwards until we can figure out
5459 ;; what type of statement this is.
5460 ;; Basically we need to figure out
5461 ;; 1) if this is a continuation of the previous line;
5462 ;; 2) are we in a block scope (begin..end)
5464 ;; if we are in a comment, done.
5465 (if (verilog-in-star-comment-p)
5466 (throw 'nesting 'comment))
5468 ;; if we have a directive, done.
5469 (if (save-excursion (beginning-of-line)
5470 (and (looking-at verilog-directive-re-1)
5471 (not (or (looking-at "[ \t]*`[ou]vm_")
5472 (looking-at "[ \t]*`vmm_")))))
5473 (throw 'nesting 'directive))
5474 ;; indent structs as if there were module level
5475 (setq structres (verilog-in-struct-nested-p))
5476 (cond ((not structres) nil)
5477 ;;((and structres (equal (char-after) ?\})) (throw 'nesting 'struct-close))
5478 ((> structres 0) (throw 'nesting 'nested-struct))
5479 ((= structres 0) (throw 'nesting 'block))
5480 (t nil))
5482 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5483 ;; unless we are in the newfangled coverpoint or constraint blocks
5484 (if (and
5485 verilog-indent-lists
5486 (verilog-in-paren)
5487 (not (verilog-in-coverage-p))
5489 (progn (setq par 1)
5490 (throw 'nesting 'block)))
5492 ;; See if we are continuing a previous line
5493 (while t
5494 ;; trap out if we crawl off the top of the buffer
5495 (if (bobp) (throw 'nesting 'cpp))
5497 (if (and (verilog-continued-line-1 lim)
5498 (or (not (verilog-in-coverage-p))
5499 (looking-at verilog-in-constraint-re) )) ; may still get hosed if concat in constraint
5500 (let ((sp (point)))
5501 (if (and
5502 (not (looking-at verilog-complete-reg))
5503 (verilog-continued-line-1 lim))
5504 (progn (goto-char sp)
5505 (throw 'nesting 'cexp))
5507 (goto-char sp))
5508 (if (and (verilog-in-coverage-p)
5509 (looking-at verilog-in-constraint-re))
5510 (progn
5511 (beginning-of-line)
5512 (skip-chars-forward " \t")
5513 (throw 'nesting 'constraint)))
5514 (if (and begin
5515 (not verilog-indent-begin-after-if)
5516 (looking-at verilog-no-indent-begin-re))
5517 (progn
5518 (beginning-of-line)
5519 (skip-chars-forward " \t")
5520 (throw 'nesting 'statement))
5521 (progn
5522 (throw 'nesting 'cexp))))
5523 ;; not a continued line
5524 (goto-char starting_position))
5526 (if (looking-at "\\<else\\>")
5527 ;; search back for governing if, striding across begin..end pairs
5528 ;; appropriately
5529 (let ((elsec 1))
5530 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5531 (cond
5532 ((match-end 1) ; else, we're in deep
5533 (setq elsec (1+ elsec)))
5534 ((match-end 2) ; if
5535 (setq elsec (1- elsec))
5536 (if (= 0 elsec)
5537 (if verilog-align-ifelse
5538 (throw 'nesting 'statement)
5539 (progn ; back up to first word on this line
5540 (beginning-of-line)
5541 (verilog-forward-syntactic-ws)
5542 (throw 'nesting 'statement)))))
5543 ((match-end 3) ; assert block
5544 (setq elsec (1- elsec))
5545 (verilog-beg-of-statement) ; doesn't get to beginning
5546 (if (looking-at verilog-property-re)
5547 (throw 'nesting 'statement) ; We don't need an endproperty for these
5548 (throw 'nesting 'block) ; We still need an endproperty
5550 (t ; endblock
5551 ;; try to leap back to matching outward block by striding across
5552 ;; indent level changing tokens then immediately
5553 ;; previous line governs indentation.
5554 (let (( reg) (nest 1))
5555 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5556 (cond
5557 ((match-end 4) ; end
5558 ;; Search back for matching begin
5559 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5560 ((match-end 5) ; endcase
5561 ;; Search back for matching case
5562 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5563 ((match-end 6) ; endfunction
5564 ;; Search back for matching function
5565 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5566 ((match-end 7) ; endtask
5567 ;; Search back for matching task
5568 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5569 ((match-end 8) ; endspecify
5570 ;; Search back for matching specify
5571 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5572 ((match-end 9) ; endtable
5573 ;; Search back for matching table
5574 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5575 ((match-end 10) ; endgenerate
5576 ;; Search back for matching generate
5577 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5578 ((match-end 11) ; joins
5579 ;; Search back for matching fork
5580 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5581 ((match-end 12) ; class
5582 ;; Search back for matching class
5583 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5584 ((match-end 13) ; covergroup
5585 ;; Search back for matching covergroup
5586 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5587 (catch 'skip
5588 (while (verilog-re-search-backward reg nil 'move)
5589 (cond
5590 ((match-end 1) ; begin
5591 (setq nest (1- nest))
5592 (if (= 0 nest)
5593 (throw 'skip 1)))
5594 ((match-end 2) ; end
5595 (setq nest (1+ nest)))))
5596 )))))))
5597 (throw 'nesting (verilog-calc-1)))
5598 ) ; catch nesting
5599 ) ; type
5601 ;; Return type of block and indent level.
5602 (if (not type)
5603 (setq type 'cpp))
5604 (if (> par 0) ; Unclosed Parenthesis
5605 (list 'cparenexp par)
5606 (cond
5607 ((eq type 'case)
5608 (list type (verilog-case-indent-level)))
5609 ((eq type 'statement)
5610 (list type (current-column)))
5611 ((eq type 'defun)
5612 (list type 0))
5613 ((eq type 'constraint)
5614 (list 'block (current-column)))
5615 ((eq type 'nested-struct)
5616 (list 'block structres))
5618 (list type (verilog-current-indent-level))))))))
5620 (defun verilog-wai ()
5621 "Show matching nesting block for debugging."
5622 (interactive)
5623 (save-excursion
5624 (let* ((type (verilog-calc-1))
5625 depth)
5626 ;; Return type of block and indent level.
5627 (if (not type)
5628 (setq type 'cpp))
5629 (if (and
5630 verilog-indent-lists
5631 (not(or (verilog-in-coverage-p)
5632 (verilog-in-struct-p)))
5633 (verilog-in-paren))
5634 (setq depth 1)
5635 (cond
5636 ((eq type 'case)
5637 (setq depth (verilog-case-indent-level)))
5638 ((eq type 'statement)
5639 (setq depth (current-column)))
5640 ((eq type 'defun)
5641 (setq depth 0))
5643 (setq depth (verilog-current-indent-level)))))
5644 (message "You are at nesting %s depth %d" type depth))))
5646 (defun verilog-calc-1 ()
5647 (catch 'nesting
5648 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)"))
5649 (inconstraint (verilog-in-coverage-p)))
5650 (while (verilog-re-search-backward re nil 'move)
5651 (catch 'continue
5652 (cond
5653 ((equal (char-after) ?\{)
5654 ;; block type returned based on outer constraint { or inner
5655 (if (verilog-at-constraint-p)
5656 (cond (inconstraint
5657 (beginning-of-line nil)
5658 (skip-chars-forward " \t")
5659 (throw 'nesting 'constraint))
5661 (throw 'nesting 'statement)))))
5662 ((equal (char-after) ?\})
5663 (let (par-pos
5664 (there (verilog-at-close-constraint-p)))
5665 (if there ; we are at the } that closes a constraint. Find the { that opens it
5666 (progn
5667 (if (> (verilog-in-paren-count) 0)
5668 (forward-char 1))
5669 (setq par-pos (verilog-parenthesis-depth))
5670 (cond (par-pos
5671 (goto-char par-pos)
5672 (forward-char 1))
5674 (backward-char 1)))))))
5676 ((looking-at verilog-beg-block-re-ordered)
5677 (cond
5678 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5679 (let ((here (point)))
5680 (verilog-beg-of-statement)
5681 (if (looking-at verilog-extended-case-re)
5682 (throw 'nesting 'case)
5683 (goto-char here)))
5684 (throw 'nesting 'case))
5686 ((match-end 4) ; *sigh* could be "disable fork"
5687 (let ((here (point)))
5688 (verilog-beg-of-statement)
5689 (if (looking-at verilog-disable-fork-re)
5690 t ; this is a normal statement
5691 (progn ; or is fork, starts a new block
5692 (goto-char here)
5693 (throw 'nesting 'block)))))
5695 ((match-end 27) ; *sigh* might be a clocking declaration
5696 (let ((here (point)))
5697 (if (verilog-in-paren)
5698 t ; this is a normal statement
5699 (progn ; or is fork, starts a new block
5700 (goto-char here)
5701 (throw 'nesting 'block)))))
5703 ;; need to consider typedef struct here...
5704 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
5705 ;; *sigh* These words have an optional prefix:
5706 ;; extern {virtual|protected}? function a();
5707 ;; typedef class foo;
5708 ;; and we don't want to confuse this with
5709 ;; function a();
5710 ;; property
5711 ;; ...
5712 ;; endfunction
5713 (verilog-beg-of-statement)
5714 (cond
5715 ((looking-at verilog-dpi-import-export-re)
5716 (throw 'continue 'foo))
5717 ((looking-at "\\<pure\\>\\s-+\\<virtual\\>\\s-+\\(?:\\<\\(local\\|protected\\|static\\)\\>\\s-+\\)?\\<\\(function\\|task\\)\\>\\s-+")
5718 (throw 'nesting 'statement))
5719 ((looking-at verilog-beg-block-re-ordered)
5720 (throw 'nesting 'block))
5722 (throw 'nesting 'defun))))
5725 ((looking-at "\\<property\\>")
5726 ;; *sigh*
5727 ;; {assert|assume|cover} property (); are complete
5728 ;; and could also be labeled: - foo: assert property
5729 ;; but
5730 ;; property ID () ... needs end_property
5731 (verilog-beg-of-statement)
5732 (if (looking-at verilog-property-re)
5733 (throw 'continue 'statement) ; We don't need an endproperty for these
5734 (throw 'nesting 'block) ;We still need an endproperty
5737 (t (throw 'nesting 'block))))
5739 ((looking-at verilog-end-block-re)
5740 (verilog-leap-to-head)
5741 (if (verilog-in-case-region-p)
5742 (progn
5743 (verilog-leap-to-case-head)
5744 (if (looking-at verilog-extended-case-re)
5745 (throw 'nesting 'case)))))
5747 ((looking-at verilog-defun-level-re)
5748 (if (looking-at verilog-defun-level-generate-only-re)
5749 (if (or (verilog-in-generate-region-p)
5750 (verilog-in-deferred-immediate-final-p))
5751 (throw 'continue 'foo) ; always block in a generate - keep looking
5752 (throw 'nesting 'defun))
5753 (throw 'nesting 'defun)))
5755 ((looking-at verilog-cpp-level-re)
5756 (throw 'nesting 'cpp))
5758 ((bobp)
5759 (throw 'nesting 'cpp)))))
5761 (throw 'nesting 'cpp))))
5763 (defun verilog-calculate-indent-directive ()
5764 "Return indentation level for directive.
5765 For speed, the searcher looks at the last directive, not the indent
5766 of the appropriate enclosing block."
5767 (let ((base -1) ; Indent of the line that determines our indentation
5768 (ind 0)) ; Relative offset caused by other directives (like `endif on same line as `else)
5769 ;; Start at current location, scan back for another directive
5771 (save-excursion
5772 (beginning-of-line)
5773 (while (and (< base 0)
5774 (verilog-re-search-backward verilog-directive-re nil t))
5775 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
5776 (setq base (current-indentation))))
5777 (cond ((and (looking-at verilog-directive-end) (< base 0)) ; Only matters when not at BOL
5778 (setq ind (- ind verilog-indent-level-directive)))
5779 ((and (looking-at verilog-directive-middle) (>= base 0)) ; Only matters when at BOL
5780 (setq ind (+ ind verilog-indent-level-directive)))
5781 ((looking-at verilog-directive-begin)
5782 (setq ind (+ ind verilog-indent-level-directive)))))
5783 ;; Adjust indent to starting indent of critical line
5784 (setq ind (max 0 (+ ind base))))
5786 (save-excursion
5787 (beginning-of-line)
5788 (skip-chars-forward " \t")
5789 (cond ((or (looking-at verilog-directive-middle)
5790 (looking-at verilog-directive-end))
5791 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5792 ind))
5794 (defun verilog-leap-to-case-head ()
5795 (let ((nest 1))
5796 (while (/= 0 nest)
5797 (verilog-re-search-backward
5798 (concat
5799 "\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5800 "\\|\\(\\<endcase\\>\\)" )
5801 nil 'move)
5802 (cond
5803 ((match-end 1)
5804 (let ((here (point)))
5805 (verilog-beg-of-statement)
5806 (unless (looking-at verilog-extended-case-re)
5807 (goto-char here)))
5808 (setq nest (1- nest)))
5809 ((match-end 3)
5810 (setq nest (1+ nest)))
5811 ((bobp)
5812 (ding 't)
5813 (setq nest 0))))))
5815 (defun verilog-leap-to-head ()
5816 "Move point to the head of this block.
5817 Jump from end to matching begin, from endcase to matching case, and so on."
5818 (let ((reg nil)
5819 snest
5820 (nesting 'yes)
5821 (nest 1))
5822 (cond
5823 ((looking-at "\\<end\\>")
5824 ;; 1: Search back for matching begin
5825 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5826 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
5827 ((looking-at "\\<endtask\\>")
5828 ;; 2: Search back for matching task
5829 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5830 (setq nesting 'no))
5831 ((looking-at "\\<endcase\\>")
5832 (catch 'nesting
5833 (verilog-leap-to-case-head) )
5834 (setq reg nil) ; to force skip
5837 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
5838 ;; 4: Search back for matching fork
5839 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5840 ((looking-at "\\<endclass\\>")
5841 ;; 5: Search back for matching class
5842 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5843 ((looking-at "\\<endtable\\>")
5844 ;; 6: Search back for matching table
5845 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5846 ((looking-at "\\<endspecify\\>")
5847 ;; 7: Search back for matching specify
5848 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5849 ((looking-at "\\<endfunction\\>")
5850 ;; 8: Search back for matching function
5851 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5852 (setq nesting 'no))
5853 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5854 ((looking-at "\\<endgenerate\\>")
5855 ;; 8: Search back for matching generate
5856 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5857 ((looking-at "\\<endgroup\\>")
5858 ;; 10: Search back for matching covergroup
5859 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5860 ((looking-at "\\<endproperty\\>")
5861 ;; 11: Search back for matching property
5862 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
5863 ((looking-at verilog-uvm-end-re)
5864 ;; 12: Search back for matching sequence
5865 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
5866 ((looking-at verilog-ovm-end-re)
5867 ;; 12: Search back for matching sequence
5868 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
5869 ((looking-at verilog-vmm-end-re)
5870 ;; 12: Search back for matching sequence
5871 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
5872 ((looking-at "\\<endinterface\\>")
5873 ;; 12: Search back for matching interface
5874 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5875 ((looking-at "\\<endsequence\\>")
5876 ;; 12: Search back for matching sequence
5877 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5878 ((looking-at "\\<endclocking\\>")
5879 ;; 12: Search back for matching clocking
5880 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
5881 (if reg
5882 (catch 'skip
5883 (if (eq nesting 'yes)
5884 (let (sreg)
5885 (while (verilog-re-search-backward reg nil 'move)
5886 (cond
5887 ((match-end 1) ; begin
5888 (if (looking-at "fork")
5889 (let ((here (point)))
5890 (verilog-beg-of-statement)
5891 (unless (looking-at verilog-disable-fork-re)
5892 (goto-char here)
5893 (setq nest (1- nest))))
5894 (setq nest (1- nest)))
5895 (if (= 0 nest)
5896 ;; Now previous line describes syntax
5897 (throw 'skip 1))
5898 (if (and snest
5899 (= snest nest))
5900 (setq reg sreg)))
5901 ((match-end 2) ; end
5902 (setq nest (1+ nest)))
5903 ((match-end 3)
5904 ;; endcase, jump to case
5905 (setq snest nest)
5906 (setq nest (1+ nest))
5907 (setq sreg reg)
5908 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5909 ((match-end 4)
5910 ;; join, jump to fork
5911 (setq snest nest)
5912 (setq nest (1+ nest))
5913 (setq sreg reg)
5914 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5916 ;; no nesting
5917 (if (and
5918 (verilog-re-search-backward reg nil 'move)
5919 (match-end 1)) ; task -> could be virtual and/or protected
5920 (progn
5921 (verilog-beg-of-statement)
5922 (throw 'skip 1))
5923 (throw 'skip 1)))))))
5925 (defun verilog-continued-line ()
5926 "Return true if this is a continued line.
5927 Set point to where line starts."
5928 (let ((continued 't))
5929 (if (eq 0 (forward-line -1))
5930 (progn
5931 (end-of-line)
5932 (verilog-backward-ws&directives)
5933 (if (bobp)
5934 (setq continued nil)
5935 (while (and continued
5936 (save-excursion
5937 (skip-chars-backward " \t")
5938 (not (bolp))))
5939 (setq continued (verilog-backward-token)))))
5940 (setq continued nil))
5941 continued))
5943 (defun verilog-backward-token ()
5944 "Step backward token, returning true if this is a continued line."
5945 (interactive)
5946 (verilog-backward-syntactic-ws)
5947 (cond
5948 ((bolp)
5949 nil)
5950 (;-- Anything ending in a ; is complete
5951 (= (preceding-char) ?\;)
5952 nil)
5953 (; If a "}" is prefixed by a ";", then this is a complete statement
5954 ;; i.e.: constraint foo { a = b; }
5955 (= (preceding-char) ?\})
5956 (progn
5957 (backward-char)
5958 (not(verilog-at-close-constraint-p))))
5959 (;-- constraint foo { a = b }
5960 ;; is a complete statement. *sigh*
5961 (= (preceding-char) ?\{)
5962 (progn
5963 (backward-char)
5964 (not (verilog-at-constraint-p))))
5965 (;" string "
5966 (= (preceding-char) ?\")
5967 (backward-char)
5968 (verilog-skip-backward-comment-or-string)
5969 nil)
5971 (; [3:4]
5972 (= (preceding-char) ?\])
5973 (backward-char)
5974 (verilog-backward-open-bracket)
5977 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5978 ;; also could be simply '@(foo)'
5979 ;; or foo u1 #(a=8)
5980 ;; (b, ... which ISN'T complete
5981 ;; Do we need this???
5982 (= (preceding-char) ?\))
5983 (progn
5984 (backward-char)
5985 (verilog-backward-up-list 1)
5986 (verilog-backward-syntactic-ws)
5987 (let ((back (point)))
5988 (forward-word-strictly -1)
5989 (cond
5990 ;;XX
5991 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5992 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
5993 ((looking-at verilog-uvm-statement-re)
5994 nil)
5995 ((looking-at verilog-uvm-begin-re)
5997 ((looking-at verilog-uvm-end-re)
5999 ((looking-at verilog-ovm-statement-re)
6000 nil)
6001 ((looking-at verilog-ovm-begin-re)
6003 ((looking-at verilog-ovm-end-re)
6005 ;; JBA find VMM macros
6006 ((looking-at verilog-vmm-statement-re)
6007 nil )
6008 ((looking-at verilog-vmm-begin-re)
6010 ((looking-at verilog-vmm-end-re)
6011 nil)
6012 ;; JBA trying to catch macro lines with no ; at end
6013 ((looking-at "\\<`")
6014 nil)
6016 (goto-char back)
6017 (cond
6018 ((= (preceding-char) ?\@)
6019 (backward-char)
6020 (save-excursion
6021 (verilog-backward-token)
6022 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
6023 ((= (preceding-char) ?\#)
6024 (backward-char))
6025 (t t)))))))
6027 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
6029 (forward-word-strictly -1)
6030 (while (or (= (preceding-char) ?\_)
6031 (= (preceding-char) ?\@)
6032 (= (preceding-char) ?\.))
6033 (forward-word-strictly -1))
6034 (cond
6035 ((looking-at "\\<else\\>")
6037 ((looking-at verilog-behavioral-block-beg-re)
6039 ((looking-at verilog-indent-re)
6040 nil)
6042 (let
6043 ((back (point)))
6044 (verilog-backward-syntactic-ws)
6045 (cond
6046 ((= (preceding-char) ?\:)
6047 (backward-char)
6048 (verilog-backward-syntactic-ws)
6049 (backward-sexp)
6050 (if (looking-at verilog-nameable-item-re )
6053 ((= (preceding-char) ?\#)
6054 (backward-char)
6056 ((= (preceding-char) ?\`)
6057 (backward-char)
6061 (goto-char back)
6062 t))))))))
6064 (defun verilog-backward-syntactic-ws ()
6065 "Move backwards putting point after first non-whitespace non-comment."
6066 (verilog-skip-backward-comments)
6067 (forward-comment (- (buffer-size))))
6069 (defun verilog-backward-syntactic-ws-quick ()
6070 "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
6071 (while (cond ((bobp)
6072 nil) ; Done
6073 ((< (skip-syntax-backward " ") 0)
6075 ((eq (preceding-char) ?\n) ; \n's terminate // so aren't space syntax
6076 (forward-char -1)
6078 ((or (verilog-inside-comment-or-string-p (1- (point)))
6079 (verilog-inside-comment-or-string-p (point)))
6080 (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
6081 t))))
6083 (defun verilog-forward-syntactic-ws ()
6084 (verilog-skip-forward-comment-p)
6085 (forward-comment (buffer-size)))
6087 (defun verilog-backward-ws&directives (&optional bound)
6088 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
6089 Optional BOUND limits search."
6090 (save-restriction
6091 (let* ((bound (or bound (point-min)))
6092 (here bound)
6093 (p nil) )
6094 (if (< bound (point))
6095 (progn
6096 (let ((state (save-excursion (verilog-syntax-ppss))))
6097 (cond
6098 ((nth 7 state) ; in // comment
6099 (verilog-re-search-backward "//" nil 'move)
6100 (skip-chars-backward "/"))
6101 ((nth 4 state) ; in /* */ comment
6102 (verilog-re-search-backward "/\\*" nil 'move))))
6103 (narrow-to-region bound (point))
6104 (while (/= here (point))
6105 (setq here (point))
6106 (verilog-skip-backward-comments)
6107 (setq p
6108 (save-excursion
6109 (beginning-of-line)
6110 ;; for as long as we're right after a continued line, keep moving up
6111 (while (and (verilog-looking-back "\\\\[\n\r\f]" nil)
6112 (forward-line -1)))
6113 (cond
6114 ((and verilog-highlight-translate-off
6115 (verilog-within-translate-off))
6116 (verilog-back-to-start-translate-off (point-min)))
6117 ((looking-at verilog-directive-re-1)
6118 (point))
6120 nil))))
6121 (if p (goto-char p))))))))
6123 (defun verilog-forward-ws&directives (&optional bound)
6124 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
6125 Optional BOUND limits search."
6126 (save-restriction
6127 (let* ((bound (or bound (point-max)))
6128 (here bound)
6129 jump)
6130 (if (> bound (point))
6131 (progn
6132 (let ((state (save-excursion (verilog-syntax-ppss))))
6133 (cond
6134 ((nth 7 state) ; in // comment
6135 (end-of-line)
6136 (forward-char 1)
6137 (skip-chars-forward " \t\n\f")
6139 ((nth 4 state) ; in /* */ comment
6140 (verilog-re-search-forward "\\*/\\s-*" nil 'move))))
6141 (narrow-to-region (point) bound)
6142 (while (/= here (point))
6143 (setq here (point)
6144 jump nil)
6145 (forward-comment (buffer-size))
6146 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ; Attribute
6147 (goto-char (match-end 0)))
6148 (save-excursion
6149 (beginning-of-line)
6150 (if (looking-at verilog-directive-re-1)
6151 (setq jump t)))
6152 (if jump
6153 (beginning-of-line 2))))))))
6155 (defun verilog-in-comment-p ()
6156 "Return true if in a star or // comment."
6157 (let ((state (save-excursion (verilog-syntax-ppss))))
6158 (or (nth 4 state) (nth 7 state))))
6160 (defun verilog-in-star-comment-p ()
6161 "Return true if in a star comment."
6162 (let ((state (save-excursion (verilog-syntax-ppss))))
6163 (and
6164 (nth 4 state) ; t if in a comment of style a // or b /**/
6165 (not
6166 (nth 7 state) ; t if in a comment of style b /**/
6167 ))))
6169 (defun verilog-in-slash-comment-p ()
6170 "Return true if in a slash comment."
6171 (let ((state (save-excursion (verilog-syntax-ppss))))
6172 (nth 7 state)))
6174 (defun verilog-in-comment-or-string-p ()
6175 "Return true if in a string or comment."
6176 (let ((state (save-excursion (verilog-syntax-ppss))))
6177 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
6179 (defun verilog-in-attribute-p ()
6180 "Return true if point is in an attribute (* [] attribute *)."
6181 (save-match-data
6182 (save-excursion
6183 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
6184 (cond
6185 ((match-end 1)
6186 (progn (goto-char (match-end 1))
6187 (not (looking-at "\\s-*)")))
6188 nil)
6189 ((match-end 2)
6190 (progn (goto-char (match-beginning 2))
6191 (not (looking-at "(\\s-*")))
6192 nil)
6193 (t nil)))))
6195 (defun verilog-in-parameter-p ()
6196 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
6197 (save-match-data
6198 (save-excursion
6199 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
6200 (numberp (match-beginning 1)))))
6202 (defun verilog-in-escaped-name-p ()
6203 "Return true if in an escaped name."
6204 (save-excursion
6205 (backward-char)
6206 (skip-chars-backward "^ \t\n\f")
6207 (if (equal (char-after (point) ) ?\\ )
6209 nil)))
6211 (defun verilog-in-directive-p ()
6212 "Return true if in a directive."
6213 (save-excursion
6214 (beginning-of-line)
6215 (looking-at verilog-directive-re-1)))
6217 (defun verilog-in-parenthesis-p ()
6218 "Return true if in a ( ) expression (but not { } or [ ])."
6219 (save-match-data
6220 (save-excursion
6221 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
6222 (numberp (match-beginning 1)))))
6224 (defun verilog-in-paren ()
6225 "Return true if in a parenthetical expression.
6226 May cache result using `verilog-syntax-ppss'."
6227 (let ((state (save-excursion (verilog-syntax-ppss))))
6228 (> (nth 0 state) 0 )))
6230 (defun verilog-in-paren-count ()
6231 "Return paren depth, floor to 0.
6232 May cache result using `verilog-syntax-ppss'."
6233 (let ((state (save-excursion (verilog-syntax-ppss))))
6234 (if (> (nth 0 state) 0)
6235 (nth 0 state)
6236 0 )))
6238 (defun verilog-in-paren-quick ()
6239 "Return true if in a parenthetical expression.
6240 Always starts from `point-min', to allow inserts with hooks disabled."
6241 ;; The -quick refers to its use alongside the other -quick functions,
6242 ;; not that it's likely to be faster than verilog-in-paren.
6243 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
6244 (> (nth 0 state) 0 )))
6246 (defun verilog-in-struct-p ()
6247 "Return true if in a struct declaration."
6248 (interactive)
6249 (save-excursion
6250 (if (verilog-in-paren)
6251 (progn
6252 (verilog-backward-up-list 1)
6253 (verilog-at-struct-p)
6255 nil)))
6257 (defun verilog-in-struct-nested-p ()
6258 "Return nil for not in struct.
6259 Return 0 for in non-nested struct.
6260 Return >0 for nested struct."
6261 (interactive)
6262 (let (col)
6263 (save-excursion
6264 (if (verilog-in-paren)
6265 (progn
6266 (verilog-backward-up-list 1)
6267 (setq col (verilog-at-struct-mv-p))
6268 (if col
6269 (if (verilog-in-struct-p) (current-column) 0)))
6270 nil))))
6272 (defun verilog-in-coverage-p ()
6273 "Return true if in a constraint or coverpoint expression."
6274 (interactive)
6275 (save-excursion
6276 (if (verilog-in-paren)
6277 (progn
6278 (verilog-backward-up-list 1)
6279 (verilog-at-constraint-p)
6281 nil)))
6283 (defun verilog-at-close-constraint-p ()
6284 "If at the } that closes a constraint or covergroup, return true."
6285 (if (and
6286 (equal (char-after) ?\})
6287 (verilog-in-coverage-p))
6289 (save-excursion
6290 (verilog-backward-ws&directives)
6291 (if (or (equal (char-before) ?\;)
6292 (equal (char-before) ?\}) ; can end with inner constraint { } block or ;
6293 (equal (char-before) ?\{)) ; empty constraint block
6294 (point)
6295 nil))))
6297 (defun verilog-at-constraint-p ()
6298 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
6299 (if (save-excursion
6300 (let ((p (point)))
6301 (and
6302 (equal (char-after) ?\{)
6303 (forward-list)
6304 (progn (backward-char 1)
6305 (verilog-backward-ws&directives)
6306 (and
6307 (or (equal (char-before) ?\{) ; empty case
6308 (equal (char-before) ?\;)
6309 (equal (char-before) ?\}))
6310 ;; skip what looks like bus repetition operator {#{
6311 (not (string-match "^{\\s-*[0-9]+\\s-*{" (buffer-substring p (point)))))))))
6312 (progn
6313 (let ( (pt (point)) (pass 0))
6314 (verilog-backward-ws&directives)
6315 (verilog-backward-token)
6316 (if (looking-at (concat "\\<constraint\\|coverpoint\\|cross\\|with\\>\\|" verilog-in-constraint-re))
6317 (progn (setq pass 1)
6318 (if (looking-at "\\<with\\>")
6319 (progn (verilog-backward-ws&directives)
6320 (beginning-of-line) ; 1
6321 (verilog-forward-ws&directives)
6323 (verilog-beg-of-statement)
6325 ;; if first word token not keyword, it maybe the instance name
6326 ;; check next word token
6327 (if (looking-at "\\<\\w+\\>\\|\\s-*(\\s-*\\S-+")
6328 (progn (verilog-beg-of-statement)
6329 (if (looking-at (concat "\\<\\(constraint\\|"
6330 "\\(?:\\w+\\s-*:\\s-*\\)?\\(coverpoint\\|cross\\)"
6331 "\\|with\\)\\>\\|" verilog-in-constraint-re))
6332 (setq pass 1)))))
6333 (if (eq pass 0)
6334 (progn (goto-char pt) nil) 1)))
6335 ;; not
6336 nil))
6338 (defun verilog-at-struct-p ()
6339 "If at the { of a struct, return true, not moving point."
6340 (save-excursion
6341 (if (and (equal (char-after) ?\{)
6342 (verilog-backward-token))
6343 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6344 nil)))
6346 (defun verilog-at-struct-mv-p ()
6347 "If at the { of a struct, return true, moving point to struct."
6348 (let ((pt (point)))
6349 (if (and (equal (char-after) ?\{)
6350 (verilog-backward-token))
6351 (if (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6352 (progn (verilog-beg-of-statement) (point))
6353 (progn (goto-char pt) nil))
6354 (progn (goto-char pt) nil))))
6356 (defun verilog-at-close-struct-p ()
6357 "If at the } that closes a struct, return true."
6358 (if (and
6359 (equal (char-after) ?\})
6360 (verilog-in-struct-p))
6361 ;; true
6362 (save-excursion
6363 (if (looking-at "}\\(?:\\s-*\\w+\\s-*\\)?;") 1))
6364 ;; false
6365 nil))
6367 (defun verilog-parenthesis-depth ()
6368 "Return non zero if in parenthetical-expression."
6369 (save-excursion (nth 1 (verilog-syntax-ppss))))
6372 (defun verilog-skip-forward-comment-or-string ()
6373 "Return true if in a string or comment."
6374 (let ((state (save-excursion (verilog-syntax-ppss))))
6375 (cond
6376 ((nth 3 state) ;Inside string
6377 (search-forward "\"")
6379 ((nth 7 state) ;Inside // comment
6380 (forward-line 1)
6382 ((nth 4 state) ;Inside any comment (hence /**/)
6383 (search-forward "*/"))
6385 nil))))
6387 (defun verilog-skip-backward-comment-or-string ()
6388 "Return true if in a string or comment."
6389 (let ((state (save-excursion (verilog-syntax-ppss))))
6390 (cond
6391 ((nth 3 state) ;Inside string
6392 (search-backward "\"")
6394 ((nth 7 state) ;Inside // comment
6395 (search-backward "//")
6396 (skip-chars-backward "/")
6398 ((nth 4 state) ;Inside /* */ comment
6399 (search-backward "/*")
6402 nil))))
6404 (defun verilog-skip-backward-comments ()
6405 "Return true if a comment was skipped."
6406 (let ((more t))
6407 (while more
6408 (setq more
6409 (let ((state (save-excursion (verilog-syntax-ppss))))
6410 (cond
6411 ((nth 7 state) ;Inside // comment
6412 (search-backward "//")
6413 (skip-chars-backward "/")
6414 (skip-chars-backward " \t\n\f")
6416 ((nth 4 state) ;Inside /* */ comment
6417 (search-backward "/*")
6418 (skip-chars-backward " \t\n\f")
6420 ((and (not (bobp))
6421 (= (char-before) ?\/)
6422 (= (char-before (1- (point))) ?\*))
6423 (goto-char (- (point) 2))
6424 t) ; Let nth 4 state handle the rest
6425 ((and (not (bobp))
6426 ;;(verilog-looking-back "\\*)" nil) ;; super slow, use two char-before instead
6427 (= (char-before) ?\))
6428 (= (char-before (1- (point))) ?\*)
6429 (not (verilog-looking-back "(\\s-*\\*)" nil))) ;; slow but unlikely to be called
6430 (goto-char (- (point) 2))
6431 (if (search-backward "(*" nil t)
6432 (progn
6433 (skip-chars-backward " \t\n\f")
6435 (progn
6436 (goto-char (+ (point) 2))
6437 nil)))
6439 (/= (skip-chars-backward " \t\n\f") 0))))))))
6441 (defun verilog-skip-forward-comment-p ()
6442 "If in comment, move to end and return true."
6443 (let* (h
6444 (state (save-excursion (verilog-syntax-ppss)))
6445 (skip (cond
6446 ((nth 3 state) ;Inside string
6448 ((nth 7 state) ;Inside // comment
6449 (end-of-line)
6450 (forward-char 1)
6452 ((nth 4 state) ;Inside /* comment
6453 (search-forward "*/")
6455 ((verilog-in-attribute-p) ;Inside (* attribute
6456 (search-forward "*)" nil t)
6458 (t nil))))
6459 (skip-chars-forward " \t\n\f")
6460 (while
6461 (cond
6462 ((looking-at "\\/\\*")
6463 (progn
6464 (setq h (point))
6465 (goto-char (match-end 0))
6466 (if (search-forward "*/" nil t)
6467 (progn
6468 (skip-chars-forward " \t\n\f")
6469 (setq skip 't))
6470 (progn
6471 (goto-char h)
6472 nil))))
6473 ((and (looking-at "(\\*") ; attribute start, but not an event (*) or (* )
6474 (not (looking-at "(\\*\\s-*)")))
6475 (progn
6476 (setq h (point))
6477 (goto-char (match-end 0))
6478 (if (search-forward "*)" nil t)
6479 (progn
6480 (skip-chars-forward " \t\n\f")
6481 (setq skip 't))
6482 (progn
6483 (goto-char h)
6484 nil))))
6485 (t nil)))
6486 skip))
6488 (defun verilog-indent-line-relative ()
6489 "Cheap version of indent line.
6490 Only look at a few lines to determine indent level."
6491 (interactive)
6492 (let ((indent-str)
6493 (sp (point)))
6494 (if (looking-at "^[ \t]*$")
6495 (cond ;- A blank line; No need to be too smart.
6496 ((bobp)
6497 (setq indent-str (list 'cpp 0)))
6498 ((verilog-continued-line)
6499 (let ((sp1 (point)))
6500 (if (verilog-continued-line)
6501 (progn
6502 (goto-char sp)
6503 (setq indent-str
6504 (list 'statement (verilog-current-indent-level))))
6505 (goto-char sp1)
6506 (setq indent-str (list 'block (verilog-current-indent-level)))))
6507 (goto-char sp))
6508 ((goto-char sp)
6509 (setq indent-str (verilog-calculate-indent))))
6510 (progn (skip-chars-forward " \t")
6511 (setq indent-str (verilog-calculate-indent))))
6512 (verilog-do-indent indent-str)))
6514 (defun verilog-indent-line ()
6515 "Indent for special part of code."
6516 (verilog-do-indent (verilog-calculate-indent)))
6518 (defun verilog-do-indent (indent-str)
6519 (let ((type (car indent-str))
6520 (ind (car (cdr indent-str))))
6521 (cond
6522 (; handle continued exp
6523 (eq type 'cexp)
6524 (let ((here (point)))
6525 (verilog-backward-syntactic-ws)
6526 (cond
6527 ((or
6528 (= (preceding-char) ?\,)
6529 (save-excursion
6530 (verilog-beg-of-statement-1)
6531 (looking-at verilog-declaration-re)))
6532 (let* ( fst
6533 (val
6534 (save-excursion
6535 (backward-char 1)
6536 (verilog-beg-of-statement-1)
6537 (setq fst (point))
6538 (if (looking-at verilog-declaration-re)
6539 (progn ; we have multiple words
6540 (goto-char (match-end 0))
6541 (skip-chars-forward " \t")
6542 (cond
6543 ((and verilog-indent-declaration-macros
6544 (= (following-char) ?\`))
6545 (progn
6546 (forward-char 1)
6547 (forward-word-strictly 1)
6548 (skip-chars-forward " \t")))
6549 ((= (following-char) ?\[)
6550 (progn
6551 (forward-char 1)
6552 (verilog-backward-up-list -1)
6553 (skip-chars-forward " \t"))))
6554 (current-column))
6555 (progn
6556 (goto-char fst)
6557 (+ (current-column) verilog-cexp-indent))))))
6558 (goto-char here)
6559 (indent-line-to val)
6560 (if (and (not verilog-indent-lists)
6561 (verilog-in-paren))
6562 (verilog-pretty-declarations-auto))
6564 ((= (preceding-char) ?\) )
6565 (goto-char here)
6566 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6567 (indent-line-to val)))
6569 (goto-char here)
6570 (let ((val))
6571 (verilog-beg-of-statement-1)
6572 (if (and (< (point) here)
6573 (verilog-re-search-forward "=[ \\t]*" here 'move)
6574 ;; not at a |=>, #=#, or [=n] operator
6575 (not (string-match "\\[=.\\|#=#\\||=>"
6576 (or (buffer-substring (- (point) 2) (1+ (point)))
6577 "")))) ; don't let buffer over/under-run spoil the party
6578 (setq val (current-column))
6579 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6580 (goto-char here)
6581 (indent-line-to val))))))
6583 (; handle inside parenthetical expressions
6584 (eq type 'cparenexp)
6585 (let* ( here
6586 (val (save-excursion
6587 (verilog-backward-up-list 1)
6588 (forward-char 1)
6589 (if verilog-indent-lists
6590 (skip-chars-forward " \t")
6591 (verilog-forward-syntactic-ws))
6592 (setq here (point))
6593 (current-column)))
6595 (decl (save-excursion
6596 (goto-char here)
6597 (verilog-forward-syntactic-ws)
6598 (setq here (point))
6599 (looking-at verilog-declaration-re))))
6600 (indent-line-to val)
6601 (if decl
6602 (verilog-pretty-declarations-auto))))
6604 (;-- Handle the ends
6606 (looking-at verilog-end-block-re)
6607 (verilog-at-close-constraint-p)
6608 (verilog-at-close-struct-p))
6609 (let ((val (if (eq type 'statement)
6610 (- ind verilog-indent-level)
6611 ind)))
6612 (indent-line-to val)))
6614 (;-- Case -- maybe line 'em up
6615 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6616 (progn
6617 (cond
6618 ((looking-at "\\<endcase\\>")
6619 (indent-line-to ind))
6621 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6622 (indent-line-to val))))))
6624 (;-- defun
6625 (and (eq type 'defun)
6626 (looking-at verilog-zero-indent-re))
6627 (indent-line-to 0))
6629 (;-- declaration
6630 (and (or
6631 (eq type 'defun)
6632 (eq type 'block))
6633 (looking-at verilog-declaration-re)
6634 ;; Do not consider "virtual function", "virtual task", "virtual class"
6635 ;; as declarations
6636 (not (looking-at (concat verilog-declaration-re
6637 "\\s-+\\(function\\|task\\|class\\)\\b"))))
6638 (verilog-indent-declaration ind))
6640 (;-- form feeds - ignored as bug in indent-line-to in < 24.5
6641 (looking-at "\f"))
6643 (;-- Everything else
6645 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6646 (indent-line-to val))))
6648 (if (looking-at "[ \t]+$")
6649 (skip-chars-forward " \t"))
6650 indent-str ; Return indent data
6653 (defun verilog-current-indent-level ()
6654 "Return the indent-level of the current statement."
6655 (save-excursion
6656 (let (par-pos)
6657 (beginning-of-line)
6658 (setq par-pos (verilog-parenthesis-depth))
6659 (while par-pos
6660 (goto-char par-pos)
6661 (beginning-of-line)
6662 (setq par-pos (verilog-parenthesis-depth)))
6663 (skip-chars-forward " \t")
6664 (current-column))))
6666 (defun verilog-case-indent-level ()
6667 "Return the indent-level of the current statement.
6668 Do not count named blocks or case-statements."
6669 (save-excursion
6670 (skip-chars-forward " \t")
6671 (cond
6672 ((looking-at verilog-named-block-re)
6673 (current-column))
6674 ((and (not (looking-at verilog-extended-case-re))
6675 (looking-at "^[^:;]+[ \t]*:"))
6676 (verilog-re-search-forward ":" nil t)
6677 (skip-chars-forward " \t")
6678 (current-column))
6680 (current-column)))))
6682 (defun verilog-indent-comment ()
6683 "Indent current line as comment."
6684 (let* ((stcol
6685 (cond
6686 ((verilog-in-star-comment-p)
6687 (save-excursion
6688 (re-search-backward "/\\*" nil t)
6689 (1+(current-column))))
6690 (comment-column
6691 comment-column )
6693 (save-excursion
6694 (re-search-backward "//" nil t)
6695 (current-column))))))
6696 (indent-line-to stcol)
6697 stcol))
6699 (defun verilog-more-comment ()
6700 "Make more comment lines like the previous."
6701 (let* ((star 0)
6702 (stcol
6703 (cond
6704 ((verilog-in-star-comment-p)
6705 (save-excursion
6706 (setq star 1)
6707 (re-search-backward "/\\*" nil t)
6708 (1+(current-column))))
6709 (comment-column
6710 comment-column )
6712 (save-excursion
6713 (re-search-backward "//" nil t)
6714 (current-column))))))
6715 (progn
6716 (indent-to stcol)
6717 (if (and star
6718 (save-excursion
6719 (forward-line -1)
6720 (skip-chars-forward " \t")
6721 (looking-at "\\*")))
6722 (insert "* ")))))
6724 (defun verilog-comment-indent (&optional _arg)
6725 "Return the column number the line should be indented to.
6726 _ARG is ignored, for `comment-indent-function' compatibility."
6727 (cond
6728 ((verilog-in-star-comment-p)
6729 (save-excursion
6730 (re-search-backward "/\\*" nil t)
6731 (1+(current-column))))
6732 ( comment-column
6733 comment-column )
6735 (save-excursion
6736 (re-search-backward "//" nil t)
6737 (current-column)))))
6741 (defun verilog-pretty-declarations-auto (&optional quiet)
6742 "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
6743 (when (or (eq 'all verilog-auto-lineup)
6744 (eq 'declarations verilog-auto-lineup))
6745 (verilog-pretty-declarations quiet)))
6747 (defun verilog-pretty-declarations (&optional quiet)
6748 "Line up declarations around point.
6749 Be verbose about progress unless optional QUIET set."
6750 (interactive)
6751 (let* ((m1 (make-marker))
6752 (e (point))
6755 (here (point))
6757 start
6758 startpos
6760 endpos
6761 base-ind
6763 (save-excursion
6764 (if (progn
6765 ;; (verilog-beg-of-statement-1)
6766 (beginning-of-line)
6767 (verilog-forward-syntactic-ws)
6768 (and (not (verilog-in-directive-p)) ; could have `define input foo
6769 (looking-at verilog-declaration-re)))
6770 (progn
6771 (if (verilog-parenthesis-depth)
6772 ;; in an argument list or parameter block
6773 (setq el (verilog-backward-up-list -1)
6774 start (progn
6775 (goto-char e)
6776 (verilog-backward-up-list 1)
6777 (forward-line) ; ignore ( input foo,
6778 (verilog-re-search-forward verilog-declaration-re el 'move)
6779 (goto-char (match-beginning 0))
6780 (skip-chars-backward " \t")
6781 (point))
6782 startpos (set-marker (make-marker) start)
6783 end (progn
6784 (goto-char start)
6785 (verilog-backward-up-list -1)
6786 (forward-char -1)
6787 (verilog-backward-syntactic-ws)
6788 (point))
6789 endpos (set-marker (make-marker) end)
6790 base-ind (progn
6791 (goto-char start)
6792 (forward-char 1)
6793 (skip-chars-forward " \t")
6794 (current-column)))
6795 ;; in a declaration block (not in argument list)
6796 (setq
6797 start (progn
6798 (verilog-beg-of-statement-1)
6799 (while (and (looking-at verilog-declaration-re)
6800 (not (bobp)))
6801 (skip-chars-backward " \t")
6802 (setq e (point))
6803 (beginning-of-line)
6804 (verilog-backward-syntactic-ws)
6805 (backward-char)
6806 (verilog-beg-of-statement-1))
6808 startpos (set-marker (make-marker) start)
6809 end (progn
6810 (goto-char here)
6811 (verilog-end-of-statement)
6812 (setq e (point)) ;Might be on last line
6813 (verilog-forward-syntactic-ws)
6814 (while (looking-at verilog-declaration-re)
6815 (verilog-end-of-statement)
6816 (setq e (point))
6817 (verilog-forward-syntactic-ws))
6819 endpos (set-marker (make-marker) end)
6820 base-ind (progn
6821 (goto-char start)
6822 (verilog-do-indent (verilog-calculate-indent))
6823 (verilog-forward-ws&directives)
6824 (current-column))))
6825 ;; OK, start and end are set
6826 (goto-char (marker-position startpos))
6827 (if (and (not quiet)
6828 (> (- end start) 100))
6829 (message "Lining up declarations..(please stand by)"))
6830 ;; Get the beginning of line indent first
6831 (while (progn (setq e (marker-position endpos))
6832 (< (point) e))
6833 (cond
6834 ((save-excursion (skip-chars-backward " \t")
6835 (bolp))
6836 (verilog-forward-ws&directives)
6837 (indent-line-to base-ind)
6838 (verilog-forward-ws&directives)
6839 (if (< (point) e)
6840 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6842 (just-one-space)
6843 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6844 ;;(forward-line)
6846 ;; Now find biggest prefix
6847 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6848 ;; Now indent each line.
6849 (goto-char (marker-position startpos))
6850 (while (progn (setq e (marker-position endpos))
6851 (setq r (- e (point)))
6852 (> r 0))
6853 (setq e (point))
6854 (unless quiet (message "%d" r))
6855 ;; (verilog-do-indent (verilog-calculate-indent)))
6856 (verilog-forward-ws&directives)
6857 (cond
6858 ((or (and verilog-indent-declaration-macros
6859 (looking-at verilog-declaration-re-2-macro))
6860 (looking-at verilog-declaration-re-2-no-macro))
6861 (let ((p (match-end 0)))
6862 (set-marker m1 p)
6863 (if (verilog-re-search-forward "[[#`]" p 'move)
6864 (progn
6865 (forward-char -1)
6866 (just-one-space)
6867 (goto-char (marker-position m1))
6868 (just-one-space)
6869 (indent-to ind))
6870 (progn
6871 (just-one-space)
6872 (indent-to ind)))))
6873 ((verilog-continued-line-1 (marker-position startpos))
6874 (goto-char e)
6875 (indent-line-to ind))
6876 ((verilog-in-struct-p)
6877 ;; could have a declaration of a user defined item
6878 (goto-char e)
6879 (verilog-end-of-statement))
6880 (t ; Must be comment or white space
6881 (goto-char e)
6882 (verilog-forward-ws&directives)
6883 (forward-line -1)))
6884 (forward-line 1))
6885 (unless quiet (message "")))))))
6887 (defun verilog-pretty-expr (&optional quiet _myre)
6888 "Line up expressions around point, optionally QUIET with regexp _MYRE ignored."
6889 (interactive)
6890 (if (not (verilog-in-comment-or-string-p))
6891 (save-excursion
6892 (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
6893 (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
6894 (beginning-of-line)
6895 (if (and (not (looking-at rexp ))
6896 (looking-at verilog-assignment-operation-re)
6897 (save-excursion
6898 (goto-char (match-end 2))
6899 (and (not (verilog-in-attribute-p))
6900 (not (verilog-in-parameter-p))
6901 (not (verilog-in-comment-or-string-p)))))
6902 (let* ((here (point))
6903 (e) (r)
6904 (start
6905 (progn
6906 (beginning-of-line)
6907 (setq e (point))
6908 (verilog-backward-syntactic-ws)
6909 (beginning-of-line)
6910 (while (and (not (looking-at rexp1))
6911 (looking-at verilog-assignment-operation-re)
6912 (not (bobp))
6914 (setq e (point))
6915 (verilog-backward-syntactic-ws)
6916 (beginning-of-line)
6917 ) ;Ack, need to grok `define
6919 (end
6920 (progn
6921 (goto-char here)
6922 (end-of-line)
6923 (setq e (point)) ;Might be on last line
6924 (verilog-forward-syntactic-ws)
6925 (beginning-of-line)
6926 (while (and
6927 (not (looking-at rexp1 ))
6928 (looking-at verilog-assignment-operation-re)
6929 (progn
6930 (end-of-line)
6931 (not (eq e (point)))))
6932 (setq e (point))
6933 (verilog-forward-syntactic-ws)
6934 (beginning-of-line)
6937 (endpos (set-marker (make-marker) end))
6938 (ind)
6940 (goto-char start)
6941 (verilog-do-indent (verilog-calculate-indent))
6942 (if (and (not quiet)
6943 (> (- end start) 100))
6944 (message "Lining up expressions..(please stand by)"))
6946 ;; Set indent to minimum throughout region
6947 (while (< (point) (marker-position endpos))
6948 (beginning-of-line)
6949 (verilog-just-one-space verilog-assignment-operation-re)
6950 (beginning-of-line)
6951 (verilog-do-indent (verilog-calculate-indent))
6952 (end-of-line)
6953 (verilog-forward-syntactic-ws)
6956 ;; Now find biggest prefix
6957 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6959 ;; Now indent each line.
6960 (goto-char start)
6961 (while (progn (setq e (marker-position endpos))
6962 (setq r (- e (point)))
6963 (> r 0))
6964 (setq e (point))
6965 (if (not quiet) (message "%d" r))
6966 (cond
6967 ((looking-at verilog-assignment-operation-re)
6968 (goto-char (match-beginning 2))
6969 (if (not (or (verilog-in-parenthesis-p) ; leave attributes and comparisons alone
6970 (verilog-in-coverage-p)))
6971 (if (eq (char-after) ?=)
6972 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6973 (indent-to ind)
6976 ((verilog-continued-line-1 start)
6977 (goto-char e)
6978 (indent-line-to ind))
6979 (t ; Must be comment or white space
6980 (goto-char e)
6981 (verilog-forward-ws&directives)
6982 (forward-line -1))
6984 (forward-line 1))
6985 (unless quiet (message ""))
6986 ))))))
6988 (defun verilog-just-one-space (myre)
6989 "Remove extra spaces around regular expression MYRE."
6990 (interactive)
6991 (if (and (not(looking-at verilog-complete-reg))
6992 (looking-at myre))
6993 (let ((p1 (match-end 1))
6994 (p2 (match-end 2)))
6995 (progn
6996 (goto-char p2)
6997 (just-one-space)
6998 (goto-char p1)
6999 (just-one-space)))))
7001 (defun verilog-indent-declaration (baseind)
7002 "Indent current lines as declaration.
7003 Line up the variable names based on previous declaration's indentation.
7004 BASEIND is the base indent to offset everything."
7005 (interactive)
7006 (let ((pos (point-marker))
7007 (lim (save-excursion
7008 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
7009 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
7010 (point)))
7011 (ind)
7012 (val)
7013 (m1 (make-marker)))
7014 (setq val
7015 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
7016 (indent-line-to val)
7018 ;; Use previous declaration (in this module) as template.
7019 (if (or (eq 'all verilog-auto-lineup)
7020 (eq 'declarations verilog-auto-lineup))
7021 (if (verilog-re-search-backward
7022 (or (and verilog-indent-declaration-macros
7023 verilog-declaration-re-1-macro)
7024 verilog-declaration-re-1-no-macro) lim t)
7025 (progn
7026 (goto-char (match-end 0))
7027 (skip-chars-forward " \t")
7028 (setq ind (current-column))
7029 (goto-char pos)
7030 (setq val
7031 (+ baseind
7032 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
7033 (indent-line-to val)
7034 (if (and verilog-indent-declaration-macros
7035 (looking-at verilog-declaration-re-2-macro))
7036 (let ((p (match-end 0)))
7037 (set-marker m1 p)
7038 (if (verilog-re-search-forward "[[#`]" p 'move)
7039 (progn
7040 (forward-char -1)
7041 (just-one-space)
7042 (goto-char (marker-position m1))
7043 (just-one-space)
7044 (indent-to ind))
7045 (if (/= (current-column) ind)
7046 (progn
7047 (just-one-space)
7048 (indent-to ind)))))
7049 (if (looking-at verilog-declaration-re-2-no-macro)
7050 (let ((p (match-end 0)))
7051 (set-marker m1 p)
7052 (if (verilog-re-search-forward "[[`#]" p 'move)
7053 (progn
7054 (forward-char -1)
7055 (just-one-space)
7056 (goto-char (marker-position m1))
7057 (just-one-space)
7058 (indent-to ind))
7059 (if (/= (current-column) ind)
7060 (progn
7061 (just-one-space)
7062 (indent-to ind))))))))))
7063 (goto-char pos)))
7065 (defun verilog-get-lineup-indent (b edpos)
7066 "Return the indent level that will line up several lines within the region.
7067 Region is defined by B and EDPOS."
7068 (save-excursion
7069 (let ((ind 0) e)
7070 (goto-char b)
7071 ;; Get rightmost position
7072 (while (progn (setq e (marker-position edpos))
7073 (< (point) e))
7074 (if (verilog-re-search-forward
7075 (or (and verilog-indent-declaration-macros
7076 verilog-declaration-re-1-macro)
7077 verilog-declaration-re-1-no-macro) e 'move)
7078 (progn
7079 (goto-char (match-end 0))
7080 (verilog-backward-syntactic-ws)
7081 (if (> (current-column) ind)
7082 (setq ind (current-column)))
7083 (goto-char (match-end 0)))))
7084 (if (> ind 0)
7085 (1+ ind)
7086 ;; No lineup-string found
7087 (goto-char b)
7088 (end-of-line)
7089 (verilog-backward-syntactic-ws)
7090 ;;(skip-chars-backward " \t")
7091 (1+ (current-column))))))
7093 (defun verilog-get-lineup-indent-2 (myre b edpos)
7094 "Return the indent level that will line up several lines within the region."
7095 (save-excursion
7096 (let ((ind 0) e)
7097 (goto-char b)
7098 ;; Get rightmost position
7099 (while (progn (setq e (marker-position edpos))
7100 (< (point) e))
7101 (if (and (verilog-re-search-forward myre e 'move)
7102 (not (verilog-in-attribute-p))) ; skip attribute exprs
7103 (progn
7104 (goto-char (match-beginning 2))
7105 (verilog-backward-syntactic-ws)
7106 (if (> (current-column) ind)
7107 (setq ind (current-column)))
7108 (goto-char (match-end 0)))
7110 (if (> ind 0)
7111 (1+ ind)
7112 ;; No lineup-string found
7113 (goto-char b)
7114 (end-of-line)
7115 (skip-chars-backward " \t")
7116 (1+ (current-column))))))
7118 (defun verilog-comment-depth (type val)
7119 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
7120 (save-excursion
7121 (let
7122 ((b (prog2
7123 (beginning-of-line)
7124 (point-marker)
7125 (end-of-line))))
7126 (if (re-search-backward " /\\* [#-]# [a-zA-Z]+ [0-9]+ ## \\*/" b t)
7127 (progn
7128 (replace-match " /* -# ## */")
7129 (end-of-line))
7130 (progn
7131 (end-of-line)
7132 (insert " /* ## ## */"))))
7133 (backward-char 6)
7134 (insert
7135 (format "%s %d" type val))))
7138 ;;; Completion:
7140 (defvar verilog-str nil)
7141 (defvar verilog-all nil)
7142 (defvar verilog-pred nil)
7143 (defvar verilog-buffer-to-use nil)
7144 (defvar verilog-flag nil)
7145 (defvar verilog-toggle-completions nil
7146 "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
7147 Repeated use of \\[verilog-complete-word] will show you all of them.
7148 Normally, when there is more than one possible completion,
7149 it displays a list of all possible completions.")
7152 (defvar verilog-type-keywords
7154 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
7155 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
7156 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
7157 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
7158 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
7159 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
7161 "Keywords for types used when completing a word in a declaration or parmlist.
7162 \(integer, real, reg...)")
7164 (defvar verilog-cpp-keywords
7165 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
7166 "endif")
7167 "Keywords to complete when at first word of a line in declarative scope.
7168 \(initial, always, begin, assign...)
7169 The procedures and variables defined within the Verilog program
7170 will be completed at runtime and should not be added to this list.")
7172 (defvar verilog-defun-keywords
7173 (append
7175 "always" "always_comb" "always_ff" "always_latch" "assign"
7176 "begin" "end" "generate" "endgenerate" "module" "endmodule"
7177 "specify" "endspecify" "function" "endfunction" "initial" "final"
7178 "task" "endtask" "primitive" "endprimitive"
7180 verilog-type-keywords)
7181 "Keywords to complete when at first word of a line in declarative scope.
7182 \(initial, always, begin, assign...)
7183 The procedures and variables defined within the Verilog program
7184 will be completed at runtime and should not be added to this list.")
7186 (defvar verilog-block-keywords
7188 "begin" "break" "case" "continue" "else" "end" "endfunction"
7189 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
7190 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
7191 "while")
7192 "Keywords to complete when at first word of a line in behavioral scope.
7193 \(begin, if, then, else, for, fork...)
7194 The procedures and variables defined within the Verilog program
7195 will be completed at runtime and should not be added to this list.")
7197 (defvar verilog-tf-keywords
7198 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
7199 "Keywords to complete when at first word of a line in a task or function.
7200 \(begin, if, then, else, for, fork.)
7201 The procedures and variables defined within the Verilog program
7202 will be completed at runtime and should not be added to this list.")
7204 (defvar verilog-case-keywords
7205 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
7206 "Keywords to complete when at first word of a line in case scope.
7207 \(begin, if, then, else, for, fork...)
7208 The procedures and variables defined within the Verilog program
7209 will be completed at runtime and should not be added to this list.")
7211 (defvar verilog-separator-keywords
7212 '("else" "then" "begin")
7213 "Keywords to complete when NOT standing at the first word of a statement.
7214 \(else, then, begin...)
7215 Variables and function names defined within the Verilog program
7216 will be completed at runtime and should not be added to this list.")
7218 (defvar verilog-gate-ios
7219 ;; All these have an implied {"input"...} at the end
7220 '(("and" "output")
7221 ("buf" "output")
7222 ("bufif0" "output")
7223 ("bufif1" "output")
7224 ("cmos" "output")
7225 ("nand" "output")
7226 ("nmos" "output")
7227 ("nor" "output")
7228 ("not" "output")
7229 ("notif0" "output")
7230 ("notif1" "output")
7231 ("or" "output")
7232 ("pmos" "output")
7233 ("pulldown" "output")
7234 ("pullup" "output")
7235 ("rcmos" "output")
7236 ("rnmos" "output")
7237 ("rpmos" "output")
7238 ("rtran" "inout" "inout")
7239 ("rtranif0" "inout" "inout")
7240 ("rtranif1" "inout" "inout")
7241 ("tran" "inout" "inout")
7242 ("tranif0" "inout" "inout")
7243 ("tranif1" "inout" "inout")
7244 ("xnor" "output")
7245 ("xor" "output"))
7246 "Map of direction for each positional argument to each gate primitive.")
7248 (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
7249 "Keywords for gate primitives.")
7251 (defun verilog-string-diff (str1 str2)
7252 "Return index of first letter where STR1 and STR2 differs."
7253 (catch 'done
7254 (let ((diff 0))
7255 (while t
7256 (if (or (> (1+ diff) (length str1))
7257 (> (1+ diff) (length str2)))
7258 (throw 'done diff))
7259 (or (equal (aref str1 diff) (aref str2 diff))
7260 (throw 'done diff))
7261 (setq diff (1+ diff))))))
7263 ;; Calculate all possible completions for functions if argument is `function',
7264 ;; completions for procedures if argument is `procedure' or both functions and
7265 ;; procedures otherwise.
7267 (defun verilog-func-completion (type)
7268 "Build regular expression for module/task/function names.
7269 TYPE is `module', `tf' for task or function, or t if unknown."
7270 (if (string= verilog-str "")
7271 (setq verilog-str "[a-zA-Z_]"))
7272 (let ((verilog-str (concat (cond
7273 ((eq type 'module) "\\<\\(module\\)\\s +")
7274 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
7275 (t "\\<\\(task\\|function\\|module\\)\\s +"))
7276 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
7277 match)
7279 (if (not (looking-at verilog-defun-re))
7280 (verilog-re-search-backward verilog-defun-re nil t))
7281 (forward-char 1)
7283 ;; Search through all reachable functions
7284 (goto-char (point-min))
7285 (while (verilog-re-search-forward verilog-str (point-max) t)
7286 (progn (setq match (buffer-substring (match-beginning 2)
7287 (match-end 2)))
7288 (if (or (null verilog-pred)
7289 (funcall verilog-pred match))
7290 (setq verilog-all (cons match verilog-all)))))
7291 (if (match-beginning 0)
7292 (goto-char (match-beginning 0)))))
7294 (defun verilog-get-completion-decl (end)
7295 "Macro for searching through current declaration (var, type or const)
7296 for matches of `str' and adding the occurrence tp `all' through point END."
7297 (let ((re (or (and verilog-indent-declaration-macros
7298 verilog-declaration-re-2-macro)
7299 verilog-declaration-re-2-no-macro))
7300 decl-end match)
7301 ;; Traverse lines
7302 (while (and (< (point) end)
7303 (verilog-re-search-forward re end t))
7304 ;; Traverse current line
7305 (setq decl-end (save-excursion (verilog-declaration-end)))
7306 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
7307 (not (match-end 1)))
7308 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
7309 (if (string-match (concat "\\<" verilog-str) match)
7310 (if (or (null verilog-pred)
7311 (funcall verilog-pred match))
7312 (setq verilog-all (cons match verilog-all)))))
7313 (forward-line 1)))
7314 verilog-all)
7316 (defun verilog-var-completion ()
7317 "Calculate all possible completions for variables (or constants)."
7318 (let ((start (point)))
7319 ;; Search for all reachable var declarations
7320 (verilog-beg-of-defun)
7321 (save-excursion
7322 ;; Check var declarations
7323 (verilog-get-completion-decl start))))
7325 (defun verilog-keyword-completion (keyword-list)
7326 "Give list of all possible completions of keywords in KEYWORD-LIST."
7327 (mapcar (lambda (s)
7328 (if (string-match (concat "\\<" verilog-str) s)
7329 (if (or (null verilog-pred)
7330 (funcall verilog-pred s))
7331 (setq verilog-all (cons s verilog-all)))))
7332 keyword-list))
7335 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
7336 "Function passed to `completing-read', `try-completion' or `all-completions'.
7337 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
7338 must be a function to be called for every match to check if this should
7339 really be a match. If VERILOG-FLAG is t, the function returns a list of
7340 all possible completions. If VERILOG-FLAG is nil it returns a string,
7341 the longest possible completion, or t if VERILOG-STR is an exact match.
7342 If VERILOG-FLAG is `lambda', the function returns t if VERILOG-STR is an
7343 exact match, nil otherwise."
7344 (save-excursion
7345 (let ((verilog-all nil))
7346 ;; Set buffer to use for searching labels. This should be set
7347 ;; within functions which use verilog-completions
7348 (set-buffer verilog-buffer-to-use)
7350 ;; Determine what should be completed
7351 (let ((state (car (verilog-calculate-indent))))
7352 (cond ((eq state 'defun)
7353 (save-excursion (verilog-var-completion))
7354 (verilog-func-completion 'module)
7355 (verilog-keyword-completion verilog-defun-keywords))
7357 ((eq state 'behavioral)
7358 (save-excursion (verilog-var-completion))
7359 (verilog-func-completion 'module)
7360 (verilog-keyword-completion verilog-defun-keywords))
7362 ((eq state 'block)
7363 (save-excursion (verilog-var-completion))
7364 (verilog-func-completion 'tf)
7365 (verilog-keyword-completion verilog-block-keywords))
7367 ((eq state 'case)
7368 (save-excursion (verilog-var-completion))
7369 (verilog-func-completion 'tf)
7370 (verilog-keyword-completion verilog-case-keywords))
7372 ((eq state 'tf)
7373 (save-excursion (verilog-var-completion))
7374 (verilog-func-completion 'tf)
7375 (verilog-keyword-completion verilog-tf-keywords))
7377 ((eq state 'cpp)
7378 (save-excursion (verilog-var-completion))
7379 (verilog-keyword-completion verilog-cpp-keywords))
7381 ((eq state 'cparenexp)
7382 (save-excursion (verilog-var-completion)))
7384 (t;--Anywhere else
7385 (save-excursion (verilog-var-completion))
7386 (verilog-func-completion 'both)
7387 (verilog-keyword-completion verilog-separator-keywords))))
7389 ;; Now we have built a list of all matches. Give response to caller
7390 (verilog-completion-response))))
7392 (defun verilog-completion-response ()
7393 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
7394 ;; This was not called by all-completions
7395 (if (null verilog-all)
7396 ;; Return nil if there was no matching label
7398 ;; Get longest string common in the labels
7399 ;; FIXME: Why not use `try-completion'?
7400 (let* ((elm (cdr verilog-all))
7401 (match (car verilog-all))
7402 (min (length match))
7403 tmp)
7404 (if (string= match verilog-str)
7405 ;; Return t if first match was an exact match
7406 (setq match t)
7407 (while (not (null elm))
7408 ;; Find longest common string
7409 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
7410 (progn
7411 (setq min tmp)
7412 (setq match (substring match 0 min))))
7413 ;; Terminate with match=t if this is an exact match
7414 (if (string= (car elm) verilog-str)
7415 (progn
7416 (setq match t)
7417 (setq elm nil))
7418 (setq elm (cdr elm)))))
7419 ;; If this is a test just for exact match, return nil ot t
7420 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
7422 match))))
7423 ;; If flag is t, this was called by all-completions. Return
7424 ;; list of all possible completions
7425 (verilog-flag
7426 verilog-all)))
7428 (defvar verilog-last-word-numb 0)
7429 (defvar verilog-last-word-shown nil)
7430 (defvar verilog-last-completions nil)
7432 (defun verilog-complete-word ()
7433 "Complete word at current point.
7434 \(See also `verilog-toggle-completions', `verilog-type-keywords',
7435 and `verilog-separator-keywords'.)"
7436 ;; FIXME: Provide completion-at-point-function.
7437 (interactive)
7438 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7439 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7440 (verilog-str (buffer-substring b e))
7441 ;; The following variable is used in verilog-completion
7442 (verilog-buffer-to-use (current-buffer))
7443 (allcomp (if (and verilog-toggle-completions
7444 (string= verilog-last-word-shown verilog-str))
7445 verilog-last-completions
7446 (all-completions verilog-str 'verilog-completion)))
7447 (match (if verilog-toggle-completions
7448 "" (try-completion
7449 verilog-str (mapcar (lambda (elm)
7450 (cons elm 0)) allcomp)))))
7451 ;; Delete old string
7452 (delete-region b e)
7454 ;; Toggle-completions inserts whole labels
7455 (if verilog-toggle-completions
7456 (progn
7457 ;; Update entry number in list
7458 (setq verilog-last-completions allcomp
7459 verilog-last-word-numb
7460 (if (>= verilog-last-word-numb (1- (length allcomp)))
7462 (1+ verilog-last-word-numb)))
7463 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7464 ;; Display next match or same string if no match was found
7465 (if (not (null allcomp))
7466 (insert "" verilog-last-word-shown)
7467 (insert "" verilog-str)
7468 (message "(No match)")))
7469 ;; The other form of completion does not necessarily do that.
7471 ;; Insert match if found, or the original string if no match
7472 (if (or (null match) (equal match 't))
7473 (progn (insert "" verilog-str)
7474 (message "(No match)"))
7475 (insert "" match))
7476 ;; Give message about current status of completion
7477 (cond ((equal match 't)
7478 (if (not (null (cdr allcomp)))
7479 (message "(Complete but not unique)")
7480 (message "(Sole completion)")))
7481 ;; Display buffer if the current completion didn't help
7482 ;; on completing the label.
7483 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7484 (length match)))
7485 (with-output-to-temp-buffer "*Completions*"
7486 (display-completion-list allcomp))
7487 ;; Wait for a key press. Then delete *Completion* window
7488 (momentary-string-display "" (point))
7489 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7490 )))))
7492 (defun verilog-show-completions ()
7493 "Show all possible completions at current point."
7494 (interactive)
7495 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7496 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7497 (verilog-str (buffer-substring b e))
7498 ;; The following variable is used in verilog-completion
7499 (verilog-buffer-to-use (current-buffer))
7500 (allcomp (if (and verilog-toggle-completions
7501 (string= verilog-last-word-shown verilog-str))
7502 verilog-last-completions
7503 (all-completions verilog-str 'verilog-completion))))
7504 ;; Show possible completions in a temporary buffer.
7505 (with-output-to-temp-buffer "*Completions*"
7506 (display-completion-list allcomp))
7507 ;; Wait for a key press. Then delete *Completion* window
7508 (momentary-string-display "" (point))
7509 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7512 (defun verilog-get-default-symbol ()
7513 "Return symbol around current point as a string."
7514 (save-excursion
7515 (buffer-substring (progn
7516 (skip-chars-backward " \t")
7517 (skip-chars-backward "a-zA-Z0-9_")
7518 (point))
7519 (progn
7520 (skip-chars-forward "a-zA-Z0-9_")
7521 (point)))))
7523 (defun verilog-build-defun-re (str &optional arg)
7524 "Return function/task/module starting with STR as regular expression.
7525 With optional second ARG non-nil, STR is the complete name of the instruction."
7526 (if arg
7527 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7528 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7530 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7531 "Function passed to `completing-read', `try-completion' or `all-completions'.
7532 Returns a completion on any function name based on VERILOG-STR prefix. If
7533 VERILOG-PRED is non-nil, it must be a function to be called for every match
7534 to check if this should really be a match. If VERILOG-FLAG is t, the
7535 function returns a list of all possible completions. If it is nil it
7536 returns a string, the longest possible completion, or t if VERILOG-STR is
7537 an exact match. If VERILOG-FLAG is `lambda', the function returns t if
7538 VERILOG-STR is an exact match, nil otherwise."
7539 (save-excursion
7540 (let ((verilog-all nil)
7541 match)
7543 ;; Set buffer to use for searching labels. This should be set
7544 ;; within functions which use verilog-completions
7545 (set-buffer verilog-buffer-to-use)
7547 (let ((verilog-str verilog-str))
7548 ;; Build regular expression for functions
7549 (if (string= verilog-str "")
7550 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7551 (setq verilog-str (verilog-build-defun-re verilog-str)))
7552 (goto-char (point-min))
7554 ;; Build a list of all possible completions
7555 (while (verilog-re-search-forward verilog-str nil t)
7556 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7557 (if (or (null verilog-pred)
7558 (funcall verilog-pred match))
7559 (setq verilog-all (cons match verilog-all)))))
7561 ;; Now we have built a list of all matches. Give response to caller
7562 (verilog-completion-response))))
7564 (defun verilog-goto-defun ()
7565 "Move to specified Verilog module/interface/task/function.
7566 The default is a name found in the buffer around point.
7567 If search fails, other files are checked based on
7568 `verilog-library-flags'."
7569 (interactive)
7570 (let* ((default (verilog-get-default-symbol))
7571 ;; The following variable is used in verilog-comp-function
7572 (verilog-buffer-to-use (current-buffer))
7573 (label (if (not (string= default ""))
7574 ;; Do completion with default
7575 (completing-read (concat "Goto-Label: (default "
7576 default ") ")
7577 'verilog-comp-defun nil nil "")
7578 ;; There is no default value. Complete without it
7579 (completing-read "Goto-Label: "
7580 'verilog-comp-defun nil nil "")))
7582 ;; Make sure library paths are correct, in case need to resolve module
7583 (verilog-auto-reeval-locals)
7584 (verilog-getopt-flags)
7585 ;; If there was no response on prompt, use default value
7586 (if (string= label "")
7587 (setq label default))
7588 ;; Goto right place in buffer if label is not an empty string
7589 (or (string= label "")
7590 (progn
7591 (save-excursion
7592 (goto-char (point-min))
7593 (setq pt
7594 (re-search-forward (verilog-build-defun-re label t) nil t)))
7595 (when pt
7596 (goto-char pt)
7597 (beginning-of-line))
7599 (verilog-goto-defun-file label))))
7601 ;; Eliminate compile warning
7602 (defvar occur-pos-list)
7604 (defun verilog-showscopes ()
7605 "List all scopes in this module."
7606 (interactive)
7607 (let ((buffer (current-buffer))
7608 (linenum 1)
7609 (nlines 0)
7610 (first 1)
7611 (prevpos (point-min))
7612 (final-context-start (make-marker))
7613 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
7614 (with-output-to-temp-buffer "*Occur*"
7615 (save-excursion
7616 (message "Searching for %s ..." regexp)
7617 ;; Find next match, but give up if prev match was at end of buffer.
7618 (while (and (not (= prevpos (point-max)))
7619 (verilog-re-search-forward regexp nil t))
7620 (goto-char (match-beginning 0))
7621 (beginning-of-line)
7622 (save-match-data
7623 (setq linenum (+ linenum (count-lines prevpos (point)))))
7624 (setq prevpos (point))
7625 (goto-char (match-end 0))
7626 (let* ((start (save-excursion
7627 (goto-char (match-beginning 0))
7628 (forward-line (if (< nlines 0) nlines (- nlines)))
7629 (point)))
7630 (end (save-excursion
7631 (goto-char (match-end 0))
7632 (if (> nlines 0)
7633 (forward-line (1+ nlines))
7634 (forward-line 1))
7635 (point)))
7636 (tag (format "%3d" linenum))
7637 (empty (make-string (length tag) ?\ ))
7638 tem)
7639 (save-excursion
7640 (setq tem (make-marker))
7641 (set-marker tem (point))
7642 (set-buffer standard-output)
7643 (setq occur-pos-list (cons tem occur-pos-list))
7644 (or first (zerop nlines)
7645 (insert "--------\n"))
7646 (setq first nil)
7647 (insert-buffer-substring buffer start end)
7648 (backward-char (- end start))
7649 (setq tem (if (< nlines 0) (- nlines) nlines))
7650 (while (> tem 0)
7651 (insert empty ?:)
7652 (forward-line 1)
7653 (setq tem (1- tem)))
7654 (let ((this-linenum linenum))
7655 (set-marker final-context-start
7656 (+ (point) (- (match-end 0) (match-beginning 0))))
7657 (while (< (point) final-context-start)
7658 (if (null tag)
7659 (setq tag (format "%3d" this-linenum)))
7660 (insert tag ?:)))))))
7661 (set-buffer-modified-p nil))))
7664 ;; Highlight helper functions
7665 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7667 (defun verilog-within-translate-off ()
7668 "Return point if within translate-off region, else nil."
7669 (and (save-excursion
7670 (re-search-backward
7671 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7672 nil t))
7673 (equal "off" (match-string 2))
7674 (point)))
7676 (defun verilog-start-translate-off (limit)
7677 "Return point before translate-off directive if before LIMIT, else nil."
7678 (when (re-search-forward
7679 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7680 limit t)
7681 (match-beginning 0)))
7683 (defun verilog-back-to-start-translate-off (limit)
7684 "Return point before translate-off directive if before LIMIT, else nil."
7685 (when (re-search-backward
7686 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7687 limit t)
7688 (match-beginning 0)))
7690 (defun verilog-end-translate-off (limit)
7691 "Return point after translate-on directive if before LIMIT, else nil."
7693 (re-search-forward (concat
7694 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7696 (defun verilog-match-translate-off (limit)
7697 "Match a translate-off block, setting `match-data' and returning t, else nil.
7698 Bound search by LIMIT."
7699 (when (< (point) limit)
7700 (let ((start (or (verilog-within-translate-off)
7701 (verilog-start-translate-off limit)))
7702 (case-fold-search t))
7703 (when start
7704 (let ((end (or (verilog-end-translate-off limit) limit)))
7705 (set-match-data (list start end))
7706 (goto-char end))))))
7708 (defun verilog-font-lock-match-item (limit)
7709 "Match, and move over, any declaration item after point.
7710 Bound search by LIMIT. Adapted from
7711 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
7712 (condition-case nil
7713 (save-restriction
7714 (narrow-to-region (point-min) limit)
7715 ;; match item
7716 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7717 (save-match-data
7718 (goto-char (match-end 1))
7719 ;; move to next item
7720 (if (looking-at "\\(\\s-*,\\)")
7721 (goto-char (match-end 1))
7722 (end-of-line) t))))
7723 (error nil)))
7726 ;; Added by Subbu Meiyappan for Header
7728 (defun verilog-header ()
7729 "Insert a standard Verilog file header.
7730 See also `verilog-sk-header' for an alternative format."
7731 (interactive)
7732 (let ((start (point)))
7733 (insert "\
7734 //-----------------------------------------------------------------------------
7735 // Title : <title>
7736 // Project : <project>
7737 //-----------------------------------------------------------------------------
7738 // File : <filename>
7739 // Author : <author>
7740 // Created : <credate>
7741 // Last modified : <moddate>
7742 //-----------------------------------------------------------------------------
7743 // Description :
7744 // <description>
7745 //-----------------------------------------------------------------------------
7746 // Copyright (c) <copydate> by <company> This model is the confidential and
7747 // proprietary property of <company> and the possession or use of this
7748 // file requires a written license from <company>.
7749 //------------------------------------------------------------------------------
7750 // Modification history :
7751 // <modhist>
7752 //-----------------------------------------------------------------------------
7755 (goto-char start)
7756 (search-forward "<filename>")
7757 (replace-match (buffer-name) t t)
7758 (search-forward "<author>") (replace-match "" t t)
7759 (insert (user-full-name))
7760 (insert " <" (user-login-name) "@" (system-name) ">")
7761 (search-forward "<credate>") (replace-match "" t t)
7762 (verilog-insert-date)
7763 (search-forward "<moddate>") (replace-match "" t t)
7764 (verilog-insert-date)
7765 (search-forward "<copydate>") (replace-match "" t t)
7766 (verilog-insert-year)
7767 (search-forward "<modhist>") (replace-match "" t t)
7768 (verilog-insert-date)
7769 (insert " : created")
7770 (goto-char start)
7771 (let (string)
7772 (setq string (read-string "title: "))
7773 (search-forward "<title>")
7774 (replace-match string t t)
7775 (setq string (read-string "project: " verilog-project))
7776 (setq verilog-project string)
7777 (search-forward "<project>")
7778 (replace-match string t t)
7779 (setq string (read-string "Company: " verilog-company))
7780 (setq verilog-company string)
7781 (search-forward "<company>")
7782 (replace-match string t t)
7783 (search-forward "<company>")
7784 (replace-match string t t)
7785 (search-forward "<company>")
7786 (replace-match string t t)
7787 (search-backward "<description>")
7788 (replace-match "" t t))))
7790 ;; verilog-header Uses the verilog-insert-date function
7792 (defun verilog-insert-date ()
7793 "Insert date from the system."
7794 (interactive)
7795 (if verilog-date-scientific-format
7796 (insert (format-time-string "%Y/%m/%d"))
7797 (insert (format-time-string "%d.%m.%Y"))))
7799 (defun verilog-insert-year ()
7800 "Insert year from the system."
7801 (interactive)
7802 (insert (format-time-string "%Y")))
7805 ;;; Signal list parsing:
7808 ;; Elements of a signal list
7809 ;; Unfortunately we use 'assoc' on this, so can't be a vector
7810 (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7811 (list name bits comment mem enum signed type multidim modport))
7812 (defsubst verilog-sig-name (sig)
7813 (car sig))
7814 (defsubst verilog-sig-bits (sig) ; First element of packed array (pre signal-name)
7815 (nth 1 sig))
7816 (defsubst verilog-sig-comment (sig)
7817 (nth 2 sig))
7818 (defsubst verilog-sig-memory (sig) ; Unpacked array (post signal-name)
7819 (nth 3 sig))
7820 (defsubst verilog-sig-enum (sig)
7821 (nth 4 sig))
7822 (defsubst verilog-sig-signed (sig)
7823 (nth 5 sig))
7824 (defsubst verilog-sig-type (sig)
7825 (nth 6 sig))
7826 (defsubst verilog-sig-type-set (sig type)
7827 (setcar (nthcdr 6 sig) type))
7828 (defsubst verilog-sig-multidim (sig) ; Second and additional elements of packed array
7829 (nth 7 sig))
7830 (defsubst verilog-sig-multidim-string (sig)
7831 (if (verilog-sig-multidim sig)
7832 (let ((str "") (args (verilog-sig-multidim sig)))
7833 (while args
7834 (setq str (concat str (car args)))
7835 (setq args (cdr args)))
7836 str)))
7837 (defsubst verilog-sig-modport (sig)
7838 (nth 8 sig))
7839 (defsubst verilog-sig-width (sig)
7840 (verilog-make-width-expression (verilog-sig-bits sig)))
7842 (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7843 (vector outputs-del outputs-imm temps inputs))
7844 (defsubst verilog-alw-get-outputs-delayed (sigs)
7845 (aref sigs 0))
7846 (defsubst verilog-alw-get-outputs-immediate (sigs)
7847 (aref sigs 1))
7848 (defsubst verilog-alw-get-temps (sigs)
7849 (aref sigs 2))
7850 (defsubst verilog-alw-get-inputs (sigs)
7851 (aref sigs 3))
7852 (defsubst verilog-alw-get-uses-delayed (sigs)
7853 (aref sigs 0))
7855 (defsubst verilog-modport-new (name clockings decls)
7856 (list name clockings decls))
7857 (defsubst verilog-modport-name (sig)
7858 (car sig))
7859 (defsubst verilog-modport-clockings (sig)
7860 (nth 1 sig)) ; Returns list of names
7861 (defsubst verilog-modport-clockings-add (sig val)
7862 (setcar (nthcdr 1 sig) (cons val (nth 1 sig))))
7863 (defsubst verilog-modport-decls (sig)
7864 (nth 2 sig)) ; Returns verilog-decls-* structure
7865 (defsubst verilog-modport-decls-set (sig val)
7866 (setcar (nthcdr 2 sig) val))
7868 (defsubst verilog-modi-new (name fob pt type)
7869 (vector name fob pt type))
7870 (defsubst verilog-modi-name (modi)
7871 (aref modi 0))
7872 (defsubst verilog-modi-file-or-buffer (modi)
7873 (aref modi 1))
7874 (defsubst verilog-modi-get-point (modi)
7875 (aref modi 2))
7876 (defsubst verilog-modi-get-type (modi) ; "module" or "interface"
7877 (aref modi 3))
7878 (defsubst verilog-modi-get-decls (modi)
7879 (verilog-modi-cache-results modi 'verilog-read-decls))
7880 (defsubst verilog-modi-get-sub-decls (modi)
7881 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7883 ;; Signal reading for given module
7884 ;; Note these all take modi's - as returned from verilog-modi-current
7885 (defsubst verilog-decls-new (out inout in vars modports assigns consts gparams interfaces)
7886 (vector out inout in vars modports assigns consts gparams interfaces))
7887 (defsubst verilog-decls-append (a b)
7888 (cond ((not a) b) ((not b) a)
7889 (t (vector (append (aref a 0) (aref b 0)) (append (aref a 1) (aref b 1))
7890 (append (aref a 2) (aref b 2)) (append (aref a 3) (aref b 3))
7891 (append (aref a 4) (aref b 4)) (append (aref a 5) (aref b 5))
7892 (append (aref a 6) (aref b 6)) (append (aref a 7) (aref b 7))
7893 (append (aref a 8) (aref b 8))))))
7894 (defsubst verilog-decls-get-outputs (decls)
7895 (aref decls 0))
7896 (defsubst verilog-decls-get-inouts (decls)
7897 (aref decls 1))
7898 (defsubst verilog-decls-get-inputs (decls)
7899 (aref decls 2))
7900 (defsubst verilog-decls-get-vars (decls)
7901 (aref decls 3))
7902 (defsubst verilog-decls-get-modports (decls) ; Also for clocking blocks; contains another verilog-decls struct
7903 (aref decls 4)) ; Returns verilog-modport* structure
7904 (defsubst verilog-decls-get-assigns (decls)
7905 (aref decls 5))
7906 (defsubst verilog-decls-get-consts (decls)
7907 (aref decls 6))
7908 (defsubst verilog-decls-get-gparams (decls)
7909 (aref decls 7))
7910 (defsubst verilog-decls-get-interfaces (decls)
7911 (aref decls 8))
7914 (defsubst verilog-subdecls-new (out inout in intf intfd)
7915 (vector out inout in intf intfd))
7916 (defsubst verilog-subdecls-get-outputs (subdecls)
7917 (aref subdecls 0))
7918 (defsubst verilog-subdecls-get-inouts (subdecls)
7919 (aref subdecls 1))
7920 (defsubst verilog-subdecls-get-inputs (subdecls)
7921 (aref subdecls 2))
7922 (defsubst verilog-subdecls-get-interfaces (subdecls)
7923 (aref subdecls 3))
7924 (defsubst verilog-subdecls-get-interfaced (subdecls)
7925 (aref subdecls 4))
7927 (defun verilog-signals-from-signame (signame-list)
7928 "Return signals in standard form from SIGNAME-LIST, a simple list of names."
7929 (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
7930 signame-list))
7932 (defun verilog-signals-in (in-list not-list)
7933 "Return list of signals in IN-LIST that are also in NOT-LIST.
7934 Also remove any duplicates in IN-LIST.
7935 Signals must be in standard (base vector) form."
7936 ;; This function is hot, so implemented as O(1)
7937 (cond ((eval-when-compile (fboundp 'make-hash-table))
7938 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7939 (ht-not (make-hash-table :test 'equal :rehash-size 4.0))
7940 out-list)
7941 (while not-list
7942 (puthash (car (car not-list)) t ht-not)
7943 (setq not-list (cdr not-list)))
7944 (while in-list
7945 (when (and (gethash (verilog-sig-name (car in-list)) ht-not)
7946 (not (gethash (verilog-sig-name (car in-list)) ht)))
7947 (setq out-list (cons (car in-list) out-list))
7948 (puthash (verilog-sig-name (car in-list)) t ht))
7949 (setq in-list (cdr in-list)))
7950 (nreverse out-list)))
7951 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7953 (let (out-list)
7954 (while in-list
7955 (if (and (assoc (verilog-sig-name (car in-list)) not-list)
7956 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7957 (setq out-list (cons (car in-list) out-list)))
7958 (setq in-list (cdr in-list)))
7959 (nreverse out-list)))))
7960 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("C" "")))
7962 (defun verilog-signals-not-in (in-list not-list)
7963 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7964 Also remove any duplicates in IN-LIST.
7965 Signals must be in standard (base vector) form."
7966 ;; This function is hot, so implemented as O(1)
7967 (cond ((eval-when-compile (fboundp 'make-hash-table))
7968 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7969 out-list)
7970 (while not-list
7971 (puthash (car (car not-list)) t ht)
7972 (setq not-list (cdr not-list)))
7973 (while in-list
7974 (when (not (gethash (verilog-sig-name (car in-list)) ht))
7975 (setq out-list (cons (car in-list) out-list))
7976 (puthash (verilog-sig-name (car in-list)) t ht))
7977 (setq in-list (cdr in-list)))
7978 (nreverse out-list)))
7979 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7981 (let (out-list)
7982 (while in-list
7983 (if (and (not (assoc (verilog-sig-name (car in-list)) not-list))
7984 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7985 (setq out-list (cons (car in-list) out-list)))
7986 (setq in-list (cdr in-list)))
7987 (nreverse out-list)))))
7988 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7990 (defun verilog-signals-not-in-struct (in-list not-list)
7991 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7992 Also remove any duplicates in IN-LIST.
7993 Any structure in not-list will remove all members in in-list.
7994 Signals must be in standard (base vector) form."
7995 (cond ((eval-when-compile (fboundp 'make-hash-table))
7996 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7997 out-list addit nm)
7998 (while not-list
7999 (puthash (car (car not-list)) t ht)
8000 (setq not-list (cdr not-list)))
8001 (while in-list
8002 (setq nm (verilog-sig-name (car in-list)))
8003 (when (not (gethash nm ht))
8004 (setq addit t)
8005 (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
8006 (setq nm (match-string 1 nm))
8007 (setq addit (and addit
8008 (not (gethash nm ht)))))
8009 (when addit
8010 (setq out-list (cons (car in-list) out-list))
8011 (puthash (verilog-sig-name (car in-list)) t ht)))
8012 (setq in-list (cdr in-list)))
8013 (nreverse out-list)))
8014 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
8016 (let (out-list addit nm)
8017 (while in-list
8018 (setq nm (verilog-sig-name (car in-list)))
8019 (when (and (not (assoc nm not-list))
8020 (not (assoc nm out-list)))
8021 (setq addit t)
8022 (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
8023 (setq nm (match-string 1 nm))
8024 (setq addit (and addit
8025 (not (assoc nm not-list)))))
8026 (when addit
8027 (setq out-list (cons (car in-list) out-list))))
8028 (setq in-list (cdr in-list)))
8029 (nreverse out-list)))))
8030 ;;(verilog-signals-not-in-struct '(("A" "") ("B" "") ("DEL.SUB.A" "[2:3]")) '(("DEL.SUB" "") ("EXT" "")))
8032 (defun verilog-signals-memory (in-list)
8033 "Return list of signals in IN-LIST that are memorized (multidimensional)."
8034 (let (out-list)
8035 (while in-list
8036 (if (nth 3 (car in-list))
8037 (setq out-list (cons (car in-list) out-list)))
8038 (setq in-list (cdr in-list)))
8039 out-list))
8040 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
8042 (defun verilog-signals-sort-compare (a b)
8043 "Compare signal A and B for sorting."
8044 (string< (verilog-sig-name a) (verilog-sig-name b)))
8046 (defun verilog-signals-not-params (in-list)
8047 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
8048 (let (out-list)
8049 (while in-list
8050 ;; Namespace intentionally short for AUTOs and compatibility
8051 (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
8052 (setq out-list (cons (car in-list) out-list)))
8053 (setq in-list (cdr in-list)))
8054 (nreverse out-list)))
8056 (defun verilog-signals-with (func in-list)
8057 "Return list of signals where FUNC is true executed on each signal in IN-LIST."
8058 (let (out-list)
8059 (while in-list
8060 (when (funcall func (car in-list))
8061 (setq out-list (cons (car in-list) out-list)))
8062 (setq in-list (cdr in-list)))
8063 (nreverse out-list)))
8065 (defun verilog-signals-combine-bus (in-list)
8066 "Return a list of signals in IN-LIST, with buses combined.
8067 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
8068 (let (combo
8069 buswarn
8070 out-list
8071 sig highbit lowbit ; Temp information about current signal
8072 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
8073 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
8074 sv-modport
8075 bus)
8076 ;; Shove signals so duplicated signals will be adjacent
8077 (setq in-list (sort in-list `verilog-signals-sort-compare))
8078 (while in-list
8079 (setq sig (car in-list))
8080 ;; No current signal; form from existing details
8081 (unless sv-name
8082 (setq sv-name (verilog-sig-name sig)
8083 sv-highbit nil
8084 sv-busstring nil
8085 sv-comment (verilog-sig-comment sig)
8086 sv-memory (verilog-sig-memory sig)
8087 sv-enum (verilog-sig-enum sig)
8088 sv-signed (verilog-sig-signed sig)
8089 sv-type (verilog-sig-type sig)
8090 sv-multidim (verilog-sig-multidim sig)
8091 sv-modport (verilog-sig-modport sig)
8092 combo ""
8093 buswarn ""))
8094 ;; Extract bus details
8095 (setq bus (verilog-sig-bits sig))
8096 (setq bus (and bus (verilog-simplify-range-expression bus)))
8097 (cond ((and bus
8098 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
8099 (setq highbit (string-to-number (match-string 1 bus))
8100 lowbit (string-to-number
8101 (match-string 2 bus))))
8102 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
8103 (setq highbit (string-to-number (match-string 1 bus))
8104 lowbit highbit))))
8105 ;; Combine bits in bus
8106 (if sv-highbit
8107 (setq sv-highbit (max highbit sv-highbit)
8108 sv-lowbit (min lowbit sv-lowbit))
8109 (setq sv-highbit highbit
8110 sv-lowbit lowbit)))
8111 (bus
8112 ;; String, probably something like `preproc:0
8113 (setq sv-busstring bus)))
8114 ;; Peek ahead to next signal
8115 (setq in-list (cdr in-list))
8116 (setq sig (car in-list))
8117 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
8118 ;; Combine with this signal
8119 (when (and sv-busstring
8120 (not (equal sv-busstring (verilog-sig-bits sig))))
8121 (when nil ; Debugging
8122 (message (concat "Warning, can't merge into single bus `%s%s'"
8123 ", the AUTOs may be wrong")
8124 sv-name bus))
8125 (setq buswarn ", Couldn't Merge"))
8126 (if (verilog-sig-comment sig) (setq combo ", ..."))
8127 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
8128 sv-enum (or sv-enum (verilog-sig-enum sig))
8129 sv-signed (or sv-signed (verilog-sig-signed sig))
8130 sv-type (or sv-type (verilog-sig-type sig))
8131 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
8132 sv-modport (or sv-modport (verilog-sig-modport sig))))
8133 ;; Doesn't match next signal, add to queue, zero in prep for next
8134 ;; Note sig may also be nil for the last signal in the list
8136 (setq out-list
8137 (cons (verilog-sig-new
8138 sv-name
8139 (or sv-busstring
8140 (if sv-highbit
8141 (concat "[" (int-to-string sv-highbit) ":"
8142 (int-to-string sv-lowbit) "]")))
8143 (concat sv-comment combo buswarn)
8144 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
8145 out-list)
8146 sv-name nil))))
8148 out-list))
8150 (defun verilog-sig-tieoff (sig)
8151 "Return tieoff expression for given SIG, with appropriate width.
8152 Tieoff value uses `verilog-active-low-regexp' and
8153 `verilog-auto-reset-widths'."
8154 (concat
8155 (if (and verilog-active-low-regexp
8156 (verilog-string-match-fold verilog-active-low-regexp (verilog-sig-name sig)))
8157 "~" "")
8158 (cond ((not verilog-auto-reset-widths)
8159 "0")
8160 ((equal verilog-auto-reset-widths 'unbased)
8161 "'0")
8162 ;; Else presume verilog-auto-reset-widths is true
8164 (let* ((width (verilog-sig-width sig)))
8165 (cond ((not width)
8166 "`0/*NOWIDTH*/")
8167 ((string-match "^[0-9]+$" width)
8168 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
8170 (concat "{" width "{1'b0}}"))))))))
8173 ;; Dumping
8176 (defun verilog-decls-princ (decls &optional header prefix)
8177 "For debug, dump the `verilog-read-decls' structure DECLS.
8178 Use optional HEADER and PREFIX."
8179 (when decls
8180 (if header (princ header))
8181 (setq prefix (or prefix ""))
8182 (verilog-signals-princ (verilog-decls-get-outputs decls)
8183 (concat prefix "Outputs:\n") (concat prefix " "))
8184 (verilog-signals-princ (verilog-decls-get-inouts decls)
8185 (concat prefix "Inout:\n") (concat prefix " "))
8186 (verilog-signals-princ (verilog-decls-get-inputs decls)
8187 (concat prefix "Inputs:\n") (concat prefix " "))
8188 (verilog-signals-princ (verilog-decls-get-vars decls)
8189 (concat prefix "Vars:\n") (concat prefix " "))
8190 (verilog-signals-princ (verilog-decls-get-assigns decls)
8191 (concat prefix "Assigns:\n") (concat prefix " "))
8192 (verilog-signals-princ (verilog-decls-get-consts decls)
8193 (concat prefix "Consts:\n") (concat prefix " "))
8194 (verilog-signals-princ (verilog-decls-get-gparams decls)
8195 (concat prefix "Gparams:\n") (concat prefix " "))
8196 (verilog-signals-princ (verilog-decls-get-interfaces decls)
8197 (concat prefix "Interfaces:\n") (concat prefix " "))
8198 (verilog-modport-princ (verilog-decls-get-modports decls)
8199 (concat prefix "Modports:\n") (concat prefix " "))
8200 (princ "\n")))
8202 (defun verilog-signals-princ (signals &optional header prefix)
8203 "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
8204 (when signals
8205 (if header (princ header))
8206 (while signals
8207 (let ((sig (car signals)))
8208 (setq signals (cdr signals))
8209 (princ prefix)
8210 (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
8211 (princ " bits=") (princ (verilog-sig-bits sig))
8212 (princ " cmt=") (princ (verilog-sig-comment sig))
8213 (princ " mem=") (princ (verilog-sig-memory sig))
8214 (princ " enum=") (princ (verilog-sig-enum sig))
8215 (princ " sign=") (princ (verilog-sig-signed sig))
8216 (princ " type=") (princ (verilog-sig-type sig))
8217 (princ " dim=") (princ (verilog-sig-multidim sig))
8218 (princ " modp=") (princ (verilog-sig-modport sig))
8219 (princ "\n")))))
8221 (defun verilog-modport-princ (modports &optional header prefix)
8222 "For debug, dump internal MODPORTS structures, with HEADER and PREFIX."
8223 (when modports
8224 (if header (princ header))
8225 (while modports
8226 (let ((sig (car modports)))
8227 (setq modports (cdr modports))
8228 (princ prefix)
8229 (princ "\"") (princ (verilog-modport-name sig)) (princ "\"")
8230 (princ " clockings=") (princ (verilog-modport-clockings sig))
8231 (princ "\n")
8232 (verilog-decls-princ (verilog-modport-decls sig)
8233 (concat prefix " syms:\n")
8234 (concat prefix " "))))))
8237 ;; Port/Wire/Etc Reading
8240 (defun verilog-read-inst-backward-name ()
8241 "Internal. Move point back to beginning of inst-name."
8242 (verilog-backward-open-paren)
8243 (let (done)
8244 (while (not done)
8245 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil) ; ] isn't word boundary
8246 (cond ((looking-at ")")
8247 (verilog-backward-open-paren))
8248 (t (setq done t)))))
8249 (while (looking-at "\\]")
8250 (verilog-backward-open-bracket)
8251 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil))
8252 (skip-chars-backward "a-zA-Z0-9`_$"))
8254 (defun verilog-read-inst-module-matcher ()
8255 "Set match data 0 with module_name when point is inside instantiation."
8256 (verilog-read-inst-backward-name)
8257 ;; Skip over instantiation name
8258 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
8259 ;; Check for parameterized instantiations
8260 (when (looking-at ")")
8261 (verilog-backward-open-paren)
8262 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil))
8263 (skip-chars-backward "a-zA-Z0-9'_$")
8264 ;; #1 is legal syntax for gate primitives
8265 (when (save-excursion
8266 (verilog-backward-syntactic-ws-quick)
8267 (eq ?# (char-before)))
8268 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil)
8269 (skip-chars-backward "a-zA-Z0-9'_$"))
8270 (looking-at "[a-zA-Z0-9`_$]+")
8271 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8272 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
8273 ;; Caller assumes match-beginning/match-end is still set
8276 (defun verilog-read-inst-module ()
8277 "Return module_name when point is inside instantiation."
8278 (save-excursion
8279 (verilog-read-inst-module-matcher)))
8281 (defun verilog-read-inst-name ()
8282 "Return instance_name when point is inside instantiation."
8283 (save-excursion
8284 (verilog-read-inst-backward-name)
8285 (looking-at "[a-zA-Z0-9`_$]+")
8286 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8287 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
8289 (defun verilog-read-module-name ()
8290 "Return module name when after its ( or ;."
8291 (save-excursion
8292 (re-search-backward "[(;]")
8293 ;; Due to "module x import y (" we must search for declaration begin
8294 (verilog-re-search-backward-quick verilog-defun-re nil nil)
8295 (goto-char (match-end 0))
8296 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_$]+" nil nil)
8297 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8298 (verilog-symbol-detick
8299 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
8301 (defun verilog-read-inst-param-value ()
8302 "Return list of parameters and values when point is inside instantiation."
8303 (save-excursion
8304 (verilog-read-inst-backward-name)
8305 ;; Skip over instantiation name
8306 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
8307 ;; If there are parameterized instantiations
8308 (when (looking-at ")")
8309 (let ((end-pt (point))
8310 params
8311 param-name paren-beg-pt param-value)
8312 (verilog-backward-open-paren)
8313 (while (verilog-re-search-forward-quick "\\." end-pt t)
8314 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_$]\\)" nil nil)
8315 (skip-chars-backward "a-zA-Z0-9'_$")
8316 (looking-at "[a-zA-Z0-9`_$]+")
8317 (setq param-name (buffer-substring-no-properties
8318 (match-beginning 0) (match-end 0)))
8319 (verilog-re-search-forward-quick "(" nil nil)
8320 (setq paren-beg-pt (point))
8321 (verilog-forward-close-paren)
8322 (setq param-value (verilog-string-remove-spaces
8323 (buffer-substring-no-properties
8324 paren-beg-pt (1- (point)))))
8325 (setq params (cons (list param-name param-value) params)))
8326 params))))
8328 (defun verilog-read-auto-params (num-param &optional max-param)
8329 "Return parameter list inside auto.
8330 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
8331 (let ((olist))
8332 (save-excursion
8333 ;; /*AUTOPUNT("parameter", "parameter")*/
8334 (backward-sexp 1)
8335 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
8336 (setq olist (cons (match-string 1) olist))
8337 (goto-char (match-end 0))))
8338 (or (eq nil num-param)
8339 (<= num-param (length olist))
8340 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
8341 (if (eq max-param nil) (setq max-param num-param))
8342 (or (eq nil max-param)
8343 (>= max-param (length olist))
8344 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
8345 (nreverse olist)))
8347 (defun verilog-read-decls ()
8348 "Compute signal declaration information for the current module at point.
8349 Return an array of [outputs inouts inputs wire reg assign const]."
8350 (let ((end-mod-point (or (verilog-get-end-of-defun) (point-max)))
8351 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
8352 in-modport in-clocking in-ign-to-semi ptype ign-prop
8353 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
8354 sigs-gparam sigs-intf sigs-modports
8355 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
8356 modport
8357 varstack tmp)
8358 ;;(if dbg (setq dbg (concat dbg (format "\n\nverilog-read-decls START PT %s END %s\n" (point) end-mod-point))))
8359 (save-excursion
8360 (verilog-beg-of-defun-quick)
8361 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
8362 (while (< (point) end-mod-point)
8363 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
8364 (cond
8365 ((looking-at "//")
8366 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8367 (setq enum (match-string 2)))
8368 (search-forward "\n"))
8369 ((looking-at "/\\*")
8370 (forward-char 2)
8371 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8372 (setq enum (match-string 2)))
8373 (or (search-forward "*/")
8374 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8375 ((looking-at "(\\*")
8376 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8377 (forward-char 1)
8378 (or (search-forward "*)")
8379 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8380 ((eq ?\" (following-char))
8381 (or (re-search-forward "[^\\]\"" nil t) ; don't forward-char first, since we look for a non backslash first
8382 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8383 ((eq ?\; (following-char))
8384 (cond (in-ign-to-semi ; Such as inside a "import ...;" in a module header
8385 (setq in-ign-to-semi nil))
8386 ((and in-modport (not (eq in-modport t))) ; end of a modport declaration
8387 (verilog-modport-decls-set
8388 in-modport
8389 (verilog-decls-new sigs-out sigs-inout sigs-in
8390 nil nil nil nil nil nil))
8391 ;; Pop from varstack to restore state to pre-clocking
8392 (setq tmp (car varstack)
8393 varstack (cdr varstack)
8394 sigs-out (aref tmp 0)
8395 sigs-inout (aref tmp 1)
8396 sigs-in (aref tmp 2))
8397 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8398 v2kargs-ok nil in-modport nil ign-prop nil))
8400 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8401 v2kargs-ok nil in-modport nil ign-prop nil)))
8402 (forward-char 1))
8403 ((eq ?= (following-char))
8404 (setq rvalue t newsig nil)
8405 (forward-char 1))
8406 ((and (eq ?, (following-char))
8407 (eq paren sig-paren))
8408 (setq rvalue nil)
8409 (forward-char 1))
8410 ;; ,'s can occur inside {} & funcs
8411 ((looking-at "[{(]")
8412 (setq paren (1+ paren))
8413 (forward-char 1))
8414 ((looking-at "[})]")
8415 (setq paren (1- paren))
8416 (forward-char 1)
8417 (when (< paren sig-paren)
8418 (setq expect-signal nil rvalue nil))) ; ) that ends variables inside v2k arg list
8419 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
8420 (goto-char (match-end 0))
8421 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
8422 (setcar (cdr (cdr (cdr newsig)))
8423 (if (verilog-sig-memory newsig)
8424 (concat (verilog-sig-memory newsig) (match-string 1))
8425 (match-string-no-properties 1))))
8426 (vec ; Multidimensional
8427 (setq multidim (cons vec multidim))
8428 (setq vec (verilog-string-replace-matches
8429 "\\s-+" "" nil nil (match-string-no-properties 1))))
8430 (t ; Bit width
8431 (setq vec (verilog-string-replace-matches
8432 "\\s-+" "" nil nil (match-string-no-properties 1))))))
8433 ;; Normal or escaped identifier -- note we remember the \ if escaped
8434 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8435 (goto-char (match-end 0))
8436 (setq keywd (match-string-no-properties 1))
8437 (when (string-match "^\\\\" (match-string 1))
8438 (setq keywd (concat keywd " "))) ; Escaped ID needs space at end
8439 ;; Add any :: package names to same identifier
8440 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8441 (goto-char (match-end 0))
8442 (setq keywd (concat keywd "::" (match-string 1)))
8443 (when (string-match "^\\\\" (match-string 1))
8444 (setq keywd (concat keywd " ")))) ; Escaped ID needs space at end
8445 (cond ((equal keywd "input")
8446 (setq vec nil enum nil rvalue nil newsig nil signed nil
8447 typedefed nil multidim nil ptype nil modport nil
8448 expect-signal 'sigs-in io t sig-paren paren))
8449 ((equal keywd "output")
8450 (setq vec nil enum nil rvalue nil newsig nil signed nil
8451 typedefed nil multidim nil ptype nil modport nil
8452 expect-signal 'sigs-out io t sig-paren paren))
8453 ((equal keywd "inout")
8454 (setq vec nil enum nil rvalue nil newsig nil signed nil
8455 typedefed nil multidim nil ptype nil modport nil
8456 expect-signal 'sigs-inout io t sig-paren paren))
8457 ((equal keywd "parameter")
8458 (setq vec nil enum nil rvalue nil signed nil
8459 typedefed nil multidim nil ptype nil modport nil
8460 expect-signal 'sigs-gparam io t sig-paren paren))
8461 ((member keywd '("wire" "reg" ; Fast
8462 ;; net_type
8463 "tri" "tri0" "tri1" "triand" "trior" "trireg"
8464 "uwire" "wand" "wor"
8465 ;; integer_atom_type
8466 "byte" "shortint" "int" "longint" "integer" "time"
8467 "supply0" "supply1"
8468 ;; integer_vector_type - "reg" above
8469 "bit" "logic"
8470 ;; non_integer_type
8471 "shortreal" "real" "realtime"
8472 ;; data_type
8473 "string" "event" "chandle"))
8474 (cond (io
8475 (setq typedefed
8476 (if typedefed (concat typedefed " " keywd) keywd)))
8477 (t (setq vec nil enum nil rvalue nil signed nil
8478 typedefed nil multidim nil sig-paren paren
8479 expect-signal 'sigs-var modport nil))))
8480 ((equal keywd "assign")
8481 (setq vec nil enum nil rvalue nil signed nil
8482 typedefed nil multidim nil ptype nil modport nil
8483 expect-signal 'sigs-assign sig-paren paren))
8484 ((member keywd '("localparam" "genvar"))
8485 (setq vec nil enum nil rvalue nil signed nil
8486 typedefed nil multidim nil ptype nil modport nil
8487 expect-signal 'sigs-const sig-paren paren))
8488 ((member keywd '("signed" "unsigned"))
8489 (setq signed keywd))
8490 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
8491 (setq ign-prop t))
8492 ((member keywd '("class" "covergroup" "function"
8493 "property" "randsequence" "sequence" "task"))
8494 (unless ign-prop
8495 (setq functask (1+ functask))))
8496 ((member keywd '("endclass" "endgroup" "endfunction"
8497 "endproperty" "endsequence" "endtask"))
8498 (setq functask (1- functask)))
8499 ((equal keywd "modport")
8500 (setq in-modport t))
8501 ((equal keywd "clocking")
8502 (setq in-clocking t))
8503 ((equal keywd "import")
8504 (if v2kargs-ok ; import in module header, not a modport import
8505 (setq in-ign-to-semi t rvalue t)))
8506 ((equal keywd "type")
8507 (setq ptype t))
8508 ((equal keywd "var"))
8509 ;; Ifdef? Ignore name of define
8510 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8511 (setq rvalue t))
8512 ;; Type?
8513 ((unless ptype
8514 (verilog-typedef-name-p keywd))
8515 (cond (io
8516 (setq typedefed
8517 (if typedefed (concat typedefed " " keywd) keywd)))
8518 (t (setq vec nil enum nil rvalue nil signed nil
8519 typedefed keywd ; Have a type
8520 multidim nil sig-paren paren
8521 expect-signal 'sigs-var modport nil))))
8522 ;; Interface with optional modport in v2k arglist?
8523 ;; Skip over parsing modport, and take the interface name as the type
8524 ((and v2kargs-ok
8525 (eq paren 1)
8526 (not rvalue)
8527 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
8528 (when (match-end 2) (goto-char (match-end 2)))
8529 (setq vec nil enum nil rvalue nil signed nil
8530 typedefed keywd multidim nil ptype nil modport (match-string 2)
8531 newsig nil sig-paren paren
8532 expect-signal 'sigs-intf io t ))
8533 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
8534 ((looking-at "\\s-*\\.")
8535 (goto-char (match-end 0))
8536 (when (not rvalue)
8537 (setq expect-signal nil)))
8538 ;; "modport <keywd>"
8539 ((and (eq in-modport t)
8540 (not (member keywd verilog-keywords)))
8541 (setq in-modport (verilog-modport-new keywd nil nil))
8542 (setq sigs-modports (cons in-modport sigs-modports))
8543 ;; Push old sig values to stack and point to new signal list
8544 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8545 varstack))
8546 (setq sigs-in nil sigs-inout nil sigs-out nil))
8547 ;; "modport x (clocking <keywd>)"
8548 ((and in-modport in-clocking)
8549 (verilog-modport-clockings-add in-modport keywd)
8550 (setq in-clocking nil))
8551 ;; endclocking
8552 ((and in-clocking
8553 (equal keywd "endclocking"))
8554 (unless (eq in-clocking t)
8555 (verilog-modport-decls-set
8556 in-clocking
8557 (verilog-decls-new sigs-out sigs-inout sigs-in
8558 nil nil nil nil nil nil))
8559 ;; Pop from varstack to restore state to pre-clocking
8560 (setq tmp (car varstack)
8561 varstack (cdr varstack)
8562 sigs-out (aref tmp 0)
8563 sigs-inout (aref tmp 1)
8564 sigs-in (aref tmp 2)))
8565 (setq in-clocking nil))
8566 ;; "clocking <keywd>"
8567 ((and (eq in-clocking t)
8568 (not (member keywd verilog-keywords)))
8569 (setq in-clocking (verilog-modport-new keywd nil nil))
8570 (setq sigs-modports (cons in-clocking sigs-modports))
8571 ;; Push old sig values to stack and point to new signal list
8572 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8573 varstack))
8574 (setq sigs-in nil sigs-inout nil sigs-out nil))
8575 ;; New signal, maybe?
8576 ((and expect-signal
8577 (not rvalue)
8578 (eq functask 0)
8579 (not (member keywd verilog-keywords)))
8580 ;; Add new signal to expect-signal's variable
8581 ;;(if dbg (setq dbg (concat dbg (format "Pt %s New sig %s'\n" (point) keywd))))
8582 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
8583 (set expect-signal (cons newsig
8584 (symbol-value expect-signal))))))
8586 (forward-char 1)))
8587 (skip-syntax-forward " "))
8588 ;; Return arguments
8589 (setq tmp (verilog-decls-new (nreverse sigs-out)
8590 (nreverse sigs-inout)
8591 (nreverse sigs-in)
8592 (nreverse sigs-var)
8593 (nreverse sigs-modports)
8594 (nreverse sigs-assign)
8595 (nreverse sigs-const)
8596 (nreverse sigs-gparam)
8597 (nreverse sigs-intf)))
8598 ;;(if dbg (verilog-decls-princ tmp))
8599 tmp)))
8601 (defvar verilog-read-sub-decls-in-interfaced nil
8602 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
8604 (defvar verilog-read-sub-decls-gate-ios nil
8605 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
8607 (eval-when-compile
8608 ;; Prevent compile warnings; these are let's, not globals
8609 ;; Do not remove the eval-when-compile
8610 ;; - we want an error when we are debugging this code if they are refed.
8611 (defvar sigs-in)
8612 (defvar sigs-inout)
8613 (defvar sigs-intf)
8614 (defvar sigs-intfd)
8615 (defvar sigs-out)
8616 (defvar sigs-out-d)
8617 (defvar sigs-out-i)
8618 (defvar sigs-out-unk)
8619 (defvar sigs-temp)
8620 ;; These are known to be from other packages and may not be defined
8621 (defvar diff-command)
8622 ;; There are known to be from newer versions of Emacs
8623 (defvar create-lockfiles)
8624 (defvar which-func-modes))
8626 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim mem)
8627 "For `verilog-read-sub-decls-line', add a signal."
8628 ;; sig eq t to indicate .name syntax
8629 ;;(message "vrsds: %s(%S)" port sig)
8630 (let ((dotname (eq sig t))
8631 portdata)
8632 (when sig
8633 (setq port (verilog-symbol-detick-denumber port))
8634 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
8635 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
8636 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
8637 (if mem (setq mem (verilog-symbol-detick-denumber mem)))
8638 (unless (or (not sig)
8639 (equal sig "")) ; Ignore .foo(1'b1) assignments
8640 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
8641 (equal "inout" verilog-read-sub-decls-gate-ios))
8642 (setq sigs-inout
8643 (cons (verilog-sig-new
8645 (if dotname (verilog-sig-bits portdata) vec)
8646 (concat "To/From " comment)
8649 (verilog-sig-signed portdata)
8650 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8651 (verilog-sig-type portdata))
8652 multidim nil)
8653 sigs-inout)))
8654 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
8655 (equal "output" verilog-read-sub-decls-gate-ios))
8656 (setq sigs-out
8657 (cons (verilog-sig-new
8659 (if dotname (verilog-sig-bits portdata) vec)
8660 (concat "From " comment)
8663 (verilog-sig-signed portdata)
8664 ;; Though ok in SV, in V2K code, propagating the
8665 ;; "reg" in "output reg" upwards isn't legal.
8666 ;; Also for backwards compatibility we don't propagate
8667 ;; "input wire" upwards.
8668 ;; See also `verilog-signals-edit-wire-reg'.
8669 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8670 (verilog-sig-type portdata))
8671 multidim nil)
8672 sigs-out)))
8673 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
8674 (equal "input" verilog-read-sub-decls-gate-ios))
8675 (setq sigs-in
8676 (cons (verilog-sig-new
8678 (if dotname (verilog-sig-bits portdata) vec)
8679 (concat "To " comment)
8682 (verilog-sig-signed portdata)
8683 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8684 (verilog-sig-type portdata))
8685 multidim nil)
8686 sigs-in)))
8687 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
8688 (setq sigs-intf
8689 (cons (verilog-sig-new
8691 (if dotname (verilog-sig-bits portdata) vec)
8692 (concat "To/From " comment)
8695 (verilog-sig-signed portdata)
8696 (verilog-sig-type portdata)
8697 multidim nil)
8698 sigs-intf)))
8699 ((setq portdata (and verilog-read-sub-decls-in-interfaced
8700 (assoc port (verilog-decls-get-vars submoddecls))))
8701 (setq sigs-intfd
8702 (cons (verilog-sig-new
8704 (if dotname (verilog-sig-bits portdata) vec)
8705 (concat "To/From " comment)
8708 (verilog-sig-signed portdata)
8709 (verilog-sig-type portdata)
8710 multidim nil)
8711 sigs-intf)))
8712 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
8713 )))))
8715 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
8716 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
8717 ;;(message "vrsde: `%s'" expr)
8718 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
8719 (setq expr (verilog-string-replace-matches "/\\*\\(\\.?\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
8720 ;; Remove front operators
8721 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8723 (cond
8724 ;; {..., a, b} requires us to recurse on a,b
8725 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
8726 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
8727 (unless verilog-auto-ignore-concat
8728 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
8729 mstr)
8730 (while (setq mstr (pop mlst))
8731 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
8733 (let (sig vec multidim mem)
8734 ;; Remove leading reduction operators, etc
8735 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8736 ;;(message "vrsde-ptop: `%s'" expr)
8737 (cond ; Find \signal. Final space is part of escaped signal name
8738 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8739 ;;(message "vrsde-s: `%s'" (match-string 1 expr))
8740 (setq sig (match-string 1 expr)
8741 expr (substring expr (match-end 0))))
8742 ;; Find signal
8743 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
8744 ;;(message "vrsde-s: `%s'" (match-string 1 expr))
8745 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8746 expr (substring expr (match-end 0)))))
8747 ;; Find [vector] or [multi][multi][multi][vector]
8748 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8749 ;;(message "vrsde-v: `%s'" (match-string 1 expr))
8750 (when vec (setq multidim (cons vec multidim)))
8751 (setq vec (match-string 1 expr)
8752 expr (substring expr (match-end 0))))
8753 ;; Find .[unpacked_memory] or .[unpacked][unpacked]...
8754 (while (string-match "^\\s-*\\.\\(\\[[^]]+\\]\\)" expr)
8755 ;;(message "vrsde-m: `%s'" (match-string 1 expr))
8756 (setq mem (match-string 1 expr)
8757 expr (substring expr (match-end 0))))
8758 ;; If found signal, and nothing unrecognized, add the signal
8759 ;;(message "vrsde-rem: `%s'" expr)
8760 (when (and sig (string-match "^\\s-*$" expr))
8761 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim mem))))))
8763 (defun verilog-read-sub-decls-line (submoddecls comment)
8764 "For `verilog-read-sub-decls', read lines of port defs until none match.
8765 Inserts the list of signals found, using submodi to look up each port."
8766 (let (done port)
8767 (save-excursion
8768 (forward-line 1)
8769 (while (not done)
8770 ;; Get port name
8771 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8772 (setq port (match-string-no-properties 1))
8773 (goto-char (match-end 0)))
8774 ;; .\escaped (
8775 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8776 (setq port (concat (match-string-no-properties 1) " ")) ; escaped id's need trailing space
8777 (goto-char (match-end 0)))
8778 ;; .name
8779 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8780 (verilog-read-sub-decls-sig
8781 submoddecls comment (match-string-no-properties 1) t ; sig==t for .name
8782 nil nil nil) ; vec multidim mem
8783 (setq port nil))
8784 ;; .\escaped_name
8785 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8786 (verilog-read-sub-decls-sig
8787 submoddecls comment (concat (match-string-no-properties 1) " ") t ; sig==t for .name
8788 nil nil nil) ; vec multidim mem
8789 (setq port nil))
8790 ;; random
8791 ((looking-at "\\s-*\\.[^(]*(")
8792 (setq port nil) ; skip this line
8793 (goto-char (match-end 0)))
8795 (setq port nil done t))) ; Unknown, ignore rest of line
8796 ;; Get signal name. Point is at the first-non-space after (
8797 ;; We intentionally ignore (non-escaped) signals with .s in them
8798 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
8799 (when port
8800 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
8801 (verilog-read-sub-decls-sig
8802 submoddecls comment port
8803 (verilog-string-remove-spaces (match-string-no-properties 1)) ; sig
8804 nil nil nil)) ; vec multidim mem
8806 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
8807 (verilog-read-sub-decls-sig
8808 submoddecls comment port
8809 (verilog-string-remove-spaces (match-string-no-properties 1)) ; sig
8810 (match-string-no-properties 2) nil nil)) ; vec multidim mem
8811 ;; Fastpath was above looking-at's.
8812 ;; For something more complicated invoke a parser
8813 ((looking-at "[^)]+")
8814 (verilog-read-sub-decls-expr
8815 submoddecls comment port
8816 (buffer-substring-no-properties
8817 (point) (1- (progn (search-backward "(") ; start at (
8818 (verilog-forward-sexp-ign-cmt 1)
8819 (point)))))))) ; expr
8821 (forward-line 1)))))
8823 (defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8824 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8825 Inserts the list of signals found."
8826 (save-excursion
8827 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8828 (while (< (point) end-inst-point)
8829 ;; Get primitive's signal name, as will never have port, and no trailing )
8830 (cond ((looking-at "//")
8831 (search-forward "\n"))
8832 ((looking-at "/\\*")
8833 (or (search-forward "*/")
8834 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8835 ((looking-at "(\\*")
8836 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8837 (forward-char 1)
8838 (or (search-forward "*)")
8839 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8840 ;; On pins, parse and advance to next pin
8841 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8842 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8843 (goto-char (match-end 0))
8844 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8845 iolist (cdr iolist))
8846 (verilog-read-sub-decls-expr
8847 submoddecls comment "primitive_port"
8848 (match-string 0)))
8850 (forward-char 1)
8851 (skip-syntax-forward " ")))))))
8853 (defun verilog-read-sub-decls ()
8854 "Internally parse signals going to modules under this module.
8855 Return an array of [ outputs inouts inputs ] signals for modules that are
8856 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8857 is an output, then SIG will be included in the list.
8859 This only works on instantiations created with /*AUTOINST*/ converted by
8860 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8861 component library to determine connectivity of the design.
8863 One work around for this problem is to manually create // Inputs and //
8864 Outputs comments above subcell signals, for example:
8866 module ModuleName (
8867 // Outputs
8868 .out (out),
8869 // Inputs
8870 .in (in));"
8871 (save-excursion
8872 (let ((end-mod-point (verilog-get-end-of-defun))
8873 st-point end-inst-point
8874 ;; below 3 modified by verilog-read-sub-decls-line
8875 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
8876 (verilog-beg-of-defun-quick)
8877 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
8878 (save-excursion
8879 (goto-char (match-beginning 0))
8880 (unless (verilog-inside-comment-or-string-p)
8881 ;; Attempt to snarf a comment
8882 (let* ((submod (verilog-read-inst-module))
8883 (inst (verilog-read-inst-name))
8884 (subprim (member submod verilog-gate-keywords))
8885 (comment (concat inst " of " submod ".v"))
8886 submodi submoddecls)
8887 (cond
8888 (subprim
8889 (setq submodi `primitive
8890 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8891 comment (concat inst " of " submod))
8892 (verilog-backward-open-paren)
8893 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8894 (point))
8895 st-point (point))
8896 (forward-char 1)
8897 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8898 ;; Non-primitive
8900 (when (setq submodi (verilog-modi-lookup submod t))
8901 (setq submoddecls (verilog-modi-get-decls submodi)
8902 verilog-read-sub-decls-gate-ios nil)
8903 (verilog-backward-open-paren)
8904 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8905 (point))
8906 st-point (point))
8907 ;; This could have used a list created by verilog-auto-inst
8908 ;; However I want it to be runnable even on user's manually added signals
8909 (let ((verilog-read-sub-decls-in-interfaced t))
8910 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8911 (verilog-read-sub-decls-line submoddecls comment))) ; Modifies sigs-ifd
8912 (goto-char st-point)
8913 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8914 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-out
8915 (goto-char st-point)
8916 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8917 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-out
8918 (goto-char st-point)
8919 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8920 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-inout
8921 (goto-char st-point)
8922 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8923 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-in
8924 )))))))
8925 ;; Combine duplicate bits
8926 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
8927 (verilog-subdecls-new
8928 (verilog-signals-combine-bus (nreverse sigs-out))
8929 (verilog-signals-combine-bus (nreverse sigs-inout))
8930 (verilog-signals-combine-bus (nreverse sigs-in))
8931 (verilog-signals-combine-bus (nreverse sigs-intf))
8932 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
8934 (defun verilog-read-inst-pins ()
8935 "Return an array of [ pins ] for the current instantiation at point.
8936 For example if declare A A (.B(SIG)) then B will be included in the list."
8937 (save-excursion
8938 (let ((end-mod-point (point)) ; presume at /*AUTOINST*/ point
8939 pins pin)
8940 (verilog-backward-open-paren)
8941 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8942 (setq pin (match-string 1))
8943 (unless (verilog-inside-comment-or-string-p)
8944 (setq pins (cons (list pin) pins))
8945 (when (looking-at "(")
8946 (verilog-forward-sexp-ign-cmt 1))))
8947 (vector pins))))
8949 (defun verilog-read-arg-pins ()
8950 "Return an array of [ pins ] for the current argument declaration at point."
8951 (save-excursion
8952 (let ((end-mod-point (point)) ; presume at /*AUTOARG*/ point
8953 pins pin)
8954 (verilog-backward-open-paren)
8955 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8956 (setq pin (match-string 1))
8957 (unless (verilog-inside-comment-or-string-p)
8958 (setq pins (cons (list pin) pins))))
8959 (vector pins))))
8961 (defun verilog-read-auto-constants (beg end-mod-point)
8962 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8963 ;; Insert new
8964 (save-excursion
8965 (let (sig-list tpl-end-pt)
8966 (goto-char beg)
8967 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8968 (if (not (looking-at "\\s *("))
8969 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8970 (search-forward "(" end-mod-point)
8971 (setq tpl-end-pt (save-excursion
8972 (backward-char 1)
8973 (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
8974 (backward-char 1)
8975 (point)))
8976 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8977 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8978 sig-list)))
8980 (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8981 (make-variable-buffer-local 'verilog-cache-has-lisp)
8983 (defun verilog-read-auto-lisp-present ()
8984 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
8985 (save-excursion
8986 (goto-char (point-min))
8987 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8989 (defun verilog-read-auto-lisp (start end)
8990 "Look for and evaluate an AUTO_LISP between START and END.
8991 Must call `verilog-read-auto-lisp-present' before this function."
8992 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8993 (when verilog-cache-has-lisp
8994 (save-excursion
8995 (goto-char start)
8996 (while (re-search-forward "\\<AUTO_LISP(" end t)
8997 (backward-char)
8998 (let* ((beg-pt (prog1 (point)
8999 (verilog-forward-sexp-cmt 1))) ; Closing paren
9000 (end-pt (point))
9001 (verilog-in-hooks t))
9002 (eval-region beg-pt end-pt nil))))))
9004 (defun verilog-read-always-signals-recurse
9005 (exit-keywd rvalue temp-next)
9006 "Recursive routine for parentheses/bracket matching.
9007 EXIT-KEYWD is expression to stop at, nil if top level.
9008 RVALUE is true if at right hand side of equal.
9009 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
9010 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ; true if after a ; we are looking for rvalue
9011 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
9012 ignore-next)
9013 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
9014 (while (not (or (eobp) gotend))
9015 (cond
9016 ((looking-at "//")
9017 (search-forward "\n"))
9018 ((looking-at "/\\*")
9019 (or (search-forward "*/")
9020 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
9021 ((looking-at "(\\*")
9022 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
9023 (forward-char 1)
9024 (or (search-forward "*)")
9025 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
9026 (t (setq keywd (buffer-substring-no-properties
9027 (point)
9028 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
9029 (forward-char 1))
9030 (point)))
9031 sig-last-tolk sig-tolk
9032 sig-tolk nil)
9033 ;;(if dbg (setq dbg (concat dbg (format "\tPt=%S %S\trv=%S in=%S ee=%S gs=%S\n" (point) keywd rvalue ignore-next end-else-check got-sig))))
9034 (cond
9035 ((equal keywd "\"")
9036 (or (re-search-forward "[^\\]\"" nil t)
9037 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
9038 ;; else at top level loop, keep parsing
9039 ((and end-else-check (equal keywd "else"))
9040 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
9041 ;; no forward movement, want to see else in lower loop
9042 (setq end-else-check nil))
9043 ;; End at top level loop
9044 ((and end-else-check (looking-at "[^ \t\n\f]"))
9045 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
9046 (setq gotend t))
9047 ;; Final statement?
9048 ((and exit-keywd (and (equal keywd exit-keywd)
9049 (not (looking-at "::"))))
9050 (setq gotend t)
9051 (forward-char (length keywd)))
9052 ;; Standard tokens...
9053 ((equal keywd ";")
9054 (setq ignore-next nil rvalue semi-rvalue)
9055 ;; Final statement at top level loop?
9056 (when (not exit-keywd)
9057 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
9058 (setq end-else-check t))
9059 (forward-char 1))
9060 ((equal keywd "'")
9061 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
9062 (goto-char (match-end 0))
9063 (forward-char 1)))
9064 ((equal keywd ":") ; Case statement, begin/end label, x?y:z
9065 (cond ((looking-at "::")
9066 (forward-char 1)) ; Another forward-char below
9067 ((equal "endcase" exit-keywd) ; case x: y=z; statement next
9068 (setq ignore-next nil rvalue nil))
9069 ((equal "?" exit-keywd) ; x?y:z rvalue
9070 ) ; NOP
9071 ((equal "]" exit-keywd) ; [x:y] rvalue
9072 ) ; NOP
9073 (got-sig ; label: statement
9074 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
9075 ((not rvalue) ; begin label
9076 (setq ignore-next t rvalue nil)))
9077 (forward-char 1))
9078 ((equal keywd "=")
9079 (when got-sig
9080 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
9081 (set got-list (cons got-sig (symbol-value got-list)))
9082 (setq got-sig nil))
9083 (when (not rvalue)
9084 (if (eq (char-before) ?< )
9085 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
9086 sigs-out-unk nil)
9087 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
9088 sigs-out-unk nil)))
9089 (setq ignore-next nil rvalue t)
9090 (forward-char 1))
9091 ((equal keywd "?")
9092 (forward-char 1)
9093 (verilog-read-always-signals-recurse ":" rvalue nil))
9094 ((equal keywd "[")
9095 (forward-char 1)
9096 (verilog-read-always-signals-recurse "]" t nil))
9097 ((equal keywd "(")
9098 (forward-char 1)
9099 (cond (sig-last-tolk ; Function call; zap last signal
9100 (setq got-sig nil)))
9101 (cond ((equal last-keywd "for")
9102 ;; temp-next: Variables on LHS are lvalues, but generally we want
9103 ;; to ignore them, assuming they are loop increments
9104 (verilog-read-always-signals-recurse ";" nil t)
9105 (verilog-read-always-signals-recurse ";" t nil)
9106 (verilog-read-always-signals-recurse ")" nil nil))
9107 (t (verilog-read-always-signals-recurse ")" t nil))))
9108 ((equal keywd "begin")
9109 (skip-syntax-forward "w_")
9110 (verilog-read-always-signals-recurse "end" nil nil)
9111 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
9112 (setq ignore-next nil rvalue semi-rvalue)
9113 (if (not exit-keywd) (setq end-else-check t)))
9114 ((member keywd '("case" "casex" "casez"))
9115 (skip-syntax-forward "w_")
9116 (verilog-read-always-signals-recurse "endcase" t nil)
9117 (setq ignore-next nil rvalue semi-rvalue)
9118 (if (not exit-keywd) (setq gotend t))) ; top level begin/end
9119 ((string-match "^[$`a-zA-Z_]" keywd) ; not exactly word constituent
9120 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
9121 (setq ignore-next t))
9122 ((or ignore-next
9123 (member keywd verilog-keywords)
9124 (string-match "^\\$" keywd)) ; PLI task
9125 (setq ignore-next nil))
9127 (setq keywd (verilog-symbol-detick-denumber keywd))
9128 (when got-sig
9129 (set got-list (cons got-sig (symbol-value got-list)))
9130 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
9132 (setq got-list (cond (temp-next 'sigs-temp)
9133 (rvalue 'sigs-in)
9134 (t 'sigs-out-unk))
9135 got-sig (if (or (not keywd)
9136 (assoc keywd (symbol-value got-list)))
9137 nil (list keywd nil nil))
9138 temp-next nil
9139 sig-tolk t)))
9140 (skip-chars-forward "a-zA-Z0-9$_.%`"))
9142 (forward-char 1)))
9143 ;; End of non-comment token
9144 (setq last-keywd keywd)))
9145 (skip-syntax-forward " "))
9146 ;; Append the final pending signal
9147 (when got-sig
9148 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
9149 (set got-list (cons got-sig (symbol-value got-list)))
9150 (setq got-sig nil))
9151 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
9154 (defun verilog-read-always-signals ()
9155 "Parse always block at point and return list of (outputs inout inputs)."
9156 (save-excursion
9157 (let* (;(dbg "")
9158 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
9159 (verilog-read-always-signals-recurse nil nil nil)
9160 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
9161 sigs-out-unk nil)
9162 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*") (delete-region (point-min) (point-max)) (insert dbg) (setq dbg "")))
9163 ;; Return what was found
9164 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
9166 (defun verilog-read-instants ()
9167 "Parse module at point and return list of ( ( file instance ) ... )."
9168 (verilog-beg-of-defun-quick)
9169 (let* ((end-mod-point (verilog-get-end-of-defun))
9170 (state nil)
9171 (instants-list nil))
9172 (save-excursion
9173 (while (< (point) end-mod-point)
9174 ;; Stay at level 0, no comments
9175 (while (progn
9176 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
9177 (or (> (car state) 0) ; in parens
9178 (nth 5 state) ; comment
9180 (forward-line 1))
9181 (beginning-of-line)
9182 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
9183 ;;(if (looking-at "^\\(.+\\)$")
9184 (let ((module (match-string 1))
9185 (instant (match-string 2)))
9186 (if (not (member module verilog-keywords))
9187 (setq instants-list (cons (list module instant) instants-list)))))
9188 (forward-line 1)))
9189 instants-list))
9192 (defun verilog-read-auto-template-middle ()
9193 "With point in middle of an AUTO_TEMPLATE, parse it.
9194 Returns REGEXP and list of ( (signal_name connection_name)... )."
9195 (save-excursion
9196 ;; Find beginning
9197 (let ((tpl-regexp "\\([0-9]+\\)")
9198 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
9199 (templateno 0)
9200 tpl-sig-list tpl-wild-list tpl-end-pt rep)
9201 ;; Parse "REGEXP"
9202 ;; We reserve @"..." for future lisp expressions that evaluate
9203 ;; once-per-AUTOINST
9204 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
9205 (setq tpl-regexp (match-string 1))
9206 (goto-char (match-end 0)))
9207 (search-forward "(")
9208 ;; Parse lines in the template
9209 (when (or verilog-auto-inst-template-numbers
9210 verilog-auto-template-warn-unused)
9211 (save-excursion
9212 (let ((pre-pt (point)))
9213 (goto-char (point-min))
9214 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
9215 (setq templateno (1+ templateno)))
9216 (while (< (point) pre-pt)
9217 (forward-line 1)
9218 (setq lineno (1+ lineno))))))
9219 (setq tpl-end-pt (save-excursion
9220 (backward-char 1)
9221 (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
9222 (backward-char 1)
9223 (point)))
9225 (while (< (point) tpl-end-pt)
9226 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
9227 (setq tpl-sig-list
9228 (cons (list
9229 (match-string-no-properties 1)
9230 (match-string-no-properties 2)
9231 templateno lineno)
9232 tpl-sig-list))
9233 (goto-char (match-end 0)))
9234 ;; Regexp form??
9235 ((looking-at
9236 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
9237 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
9238 (setq rep (match-string-no-properties 3))
9239 (goto-char (match-end 0))
9240 (setq tpl-wild-list
9241 (cons (list
9242 (concat "^"
9243 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
9244 (match-string 1))
9245 "$")
9247 templateno lineno)
9248 tpl-wild-list)))
9249 ((looking-at "[ \t\f]+")
9250 (goto-char (match-end 0)))
9251 ((looking-at "\n")
9252 (setq lineno (1+ lineno))
9253 (goto-char (match-end 0)))
9254 ((looking-at "//")
9255 (search-forward "\n")
9256 (setq lineno (1+ lineno)))
9257 ((looking-at "/\\*")
9258 (forward-char 2)
9259 (or (search-forward "*/")
9260 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
9262 (error "%s: AUTO_TEMPLATE parsing error: %s"
9263 (verilog-point-text)
9264 (progn (looking-at ".*$") (match-string 0))))))
9265 ;; Return
9266 (vector tpl-regexp
9267 (list tpl-sig-list tpl-wild-list)))))
9269 (defun verilog-read-auto-template (module)
9270 "Look for an auto_template for the instantiation of the given MODULE.
9271 If found returns `verilog-read-auto-template-inside' structure."
9272 (save-excursion
9273 ;; Find beginning
9274 (let ((pt (point)))
9275 ;; Note this search is expensive, as we hunt from mod-begin to point
9276 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
9277 ;; So, we look first for an exact string rather than a slow regexp.
9278 ;; Someday we may keep a cache of every template, but this would also
9279 ;; need to record the relative position of each AUTOINST, as multiple
9280 ;; templates exist for each module, and we're inserting lines.
9281 (cond ((or
9282 ;; See also regexp in `verilog-auto-template-lint'
9283 (verilog-re-search-backward-substr
9284 "AUTO_TEMPLATE"
9285 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
9286 ;; Also try forward of this AUTOINST
9287 ;; This is for historical support; this isn't speced as working
9288 (progn
9289 (goto-char pt)
9290 (verilog-re-search-forward-substr
9291 "AUTO_TEMPLATE"
9292 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
9293 (goto-char (match-end 0))
9294 (verilog-read-auto-template-middle))
9295 ;; If no template found
9296 (t (vector "" nil))))))
9297 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
9299 (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
9300 (make-variable-buffer-local 'verilog-auto-template-hits)
9302 (defun verilog-read-auto-template-init ()
9303 "Initialize `verilog-read-auto-template'."
9304 (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
9305 (when verilog-auto-template-warn-unused
9306 (setq verilog-auto-template-hits
9307 (make-hash-table :test 'equal :rehash-size 4.0)))))
9309 (defun verilog-read-auto-template-hit (tpl-ass)
9310 "Record that TPL-ASS template from `verilog-read-auto-template' was used."
9311 (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
9312 (when verilog-auto-template-warn-unused
9313 (unless verilog-auto-template-hits
9314 (verilog-read-auto-template-init))
9315 (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
9316 verilog-auto-template-hits))))
9318 (defun verilog-set-define (defname defvalue &optional buffer enumname)
9319 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
9320 Optionally associate it with the specified enumeration ENUMNAME."
9321 (with-current-buffer (or buffer (current-buffer))
9322 ;; Namespace intentionally short for AUTOs and compatibility
9323 (let ((mac (intern (concat "vh-" defname))))
9324 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9325 ;; Need to define to a constant if no value given
9326 (set (make-local-variable mac)
9327 (if (equal defvalue "") "1" defvalue)))
9328 (if enumname
9329 ;; Namespace intentionally short for AUTOs and compatibility
9330 (let ((enumvar (intern (concat "venum-" enumname))))
9331 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9332 (unless (boundp enumvar) (set enumvar nil))
9333 (add-to-list (make-local-variable enumvar) defname)))))
9335 (defun verilog-read-defines (&optional filename recurse subcall)
9336 "Read \\=`defines and parameters for the current file, or optional FILENAME.
9337 If the filename is provided, `verilog-library-flags' will be used to
9338 resolve it. If optional RECURSE is non-nil, recurse through \\=`includes.
9340 Parameters must be simple assignments to constants, or have their own
9341 \"parameter\" label rather than a list of parameters. Thus:
9343 parameter X = 5, Y = 10; // Ok
9344 parameter X = {1\\='b1, 2\\='h2}; // Ok
9345 parameter X = {1\\='b1, 2\\='h2}, Y = 10; // Bad, make into 2 parameter lines
9347 Defines must be simple text substitutions, one on a line, starting
9348 at the beginning of the line. Any ifdefs or multiline comments around the
9349 define are ignored.
9351 Defines are stored inside Emacs variables using the name vh-{definename}.
9353 This function is useful for setting vh-* variables. The file variables
9354 feature can be used to set defines that `verilog-mode' can see; put at the
9355 *END* of your file something like:
9357 // Local Variables:
9358 // vh-macro:\"macro_definition\"
9359 // End:
9361 If macros are defined earlier in the same file and you want their values,
9362 you can read them automatically (provided `enable-local-eval' is on):
9364 // Local Variables:
9365 // eval:(verilog-read-defines)
9366 // eval:(verilog-read-defines \"group_standard_includes.v\")
9367 // End:
9369 Note these are only read when the file is first visited, you must use
9370 \\[find-alternate-file] RET to have these take effect after editing them!
9372 If you want to disable the \"Process `eval' or hook local variables\"
9373 warning message, you need to add to your init file:
9375 (setq enable-local-eval t)"
9376 (let ((origbuf (current-buffer)))
9377 (save-excursion
9378 (unless subcall (verilog-getopt-flags))
9379 (when filename
9380 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
9381 (if fns
9382 (set-buffer (find-file-noselect (car fns)))
9383 (error "%s: Can't find verilog-read-defines file: %s"
9384 (verilog-point-text) filename))))
9385 (when recurse
9386 (goto-char (point-min))
9387 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9388 (let ((inc (verilog-string-replace-matches
9389 "\"" "" nil nil (match-string-no-properties 1))))
9390 (unless (verilog-inside-comment-or-string-p)
9391 (verilog-read-defines inc recurse t)))))
9392 ;; Read `defines
9393 ;; note we don't use verilog-re... it's faster this way, and that
9394 ;; function has problems when comments are at the end of the define
9395 (goto-char (point-min))
9396 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
9397 (let ((defname (match-string-no-properties 1))
9398 (defvalue (match-string-no-properties 2)))
9399 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9400 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
9401 (verilog-set-define defname defvalue origbuf))))
9402 ;; Hack: Read parameters
9403 (goto-char (point-min))
9404 (while (re-search-forward
9405 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
9406 (let (enumname)
9407 ;; The primary way of getting defines is verilog-read-decls
9408 ;; However, that isn't called yet for included files, so we'll add another scheme
9409 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
9410 (setq enumname (match-string-no-properties 2)))
9411 (forward-comment 99999)
9412 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
9413 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
9414 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9415 (verilog-set-define (match-string-no-properties 1)
9416 (match-string-no-properties 2) origbuf enumname))
9417 (goto-char (match-end 0))
9418 (forward-comment 99999)))))))
9420 (defun verilog-read-includes ()
9421 "Read \\=`includes for the current file.
9422 This will find all of the \\=`includes which are at the beginning of lines,
9423 ignoring any ifdefs or multiline comments around them.
9424 `verilog-read-defines' is then performed on the current and each included
9425 file.
9427 It is often useful put at the *END* of your file something like:
9429 // Local Variables:
9430 // eval:(verilog-read-defines)
9431 // eval:(verilog-read-includes)
9432 // End:
9434 Note includes are only read when the file is first visited, you must use
9435 \\[find-alternate-file] RET to have these take effect after editing them!
9437 It is good to get in the habit of including all needed files in each .v
9438 file that needs it, rather than waiting for compile time. This will aid
9439 this process, Verilint, and readability. To prevent defining the same
9440 variable over and over when many modules are compiled together, put a test
9441 around the inside each include file:
9443 foo.v (an include file):
9444 \\=`ifdef _FOO_V // include if not already included
9445 \\=`else
9446 \\=`define _FOO_V
9447 ... contents of file
9448 \\=`endif // _FOO_V"
9449 ;;slow: (verilog-read-defines nil t)
9450 (save-excursion
9451 (verilog-getopt-flags)
9452 (goto-char (point-min))
9453 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9454 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
9455 (verilog-read-defines inc nil t)))))
9457 (defun verilog-read-signals (&optional start end)
9458 "Return a simple list of all possible signals in the file.
9459 Bounded by optional region from START to END. Overly aggressive but fast.
9460 Some macros and such are also found and included. For dinotrace.el."
9461 (let (sigs-all keywd)
9462 (progn;save-excursion
9463 (goto-char (or start (point-min)))
9464 (setq end (or end (point-max)))
9465 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
9466 (forward-char -1)
9467 (cond
9468 ((looking-at "//")
9469 (search-forward "\n"))
9470 ((looking-at "/\\*")
9471 (search-forward "*/"))
9472 ((looking-at "(\\*")
9473 (or (looking-at "(\\*\\s-*)") ; It's an "always @ (*)"
9474 (search-forward "*)")))
9475 ((eq ?\" (following-char))
9476 (re-search-forward "[^\\]\"")) ; don't forward-char first, since we look for a non backslash first
9477 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
9478 (goto-char (match-end 0))
9479 (setq keywd (match-string-no-properties 1))
9480 (or (member keywd verilog-keywords)
9481 (member keywd sigs-all)
9482 (setq sigs-all (cons keywd sigs-all))))
9483 (t (forward-char 1))))
9484 ;; Return list
9485 sigs-all)))
9488 ;; Argument file parsing
9491 (defun verilog-getopt (arglist)
9492 "Parse -f, -v etc arguments in ARGLIST list or string."
9493 (unless (listp arglist) (setq arglist (list arglist)))
9494 (let ((space-args '())
9495 arg next-param)
9496 ;; Split on spaces, so users can pass whole command lines
9497 (while arglist
9498 (setq arg (car arglist)
9499 arglist (cdr arglist))
9500 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
9501 (setq space-args (append space-args
9502 (list (match-string-no-properties 1 arg))))
9503 (setq arg (match-string 2 arg))))
9504 ;; Parse arguments
9505 (while space-args
9506 (setq arg (car space-args)
9507 space-args (cdr space-args))
9508 (cond
9509 ;; Need another arg
9510 ((equal arg "-f")
9511 (setq next-param arg))
9512 ((equal arg "-v")
9513 (setq next-param arg))
9514 ((equal arg "-y")
9515 (setq next-param arg))
9516 ;; +libext+(ext1)+(ext2)...
9517 ((string-match "^\\+libext\\+\\(.*\\)" arg)
9518 (setq arg (match-string 1 arg))
9519 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
9520 (verilog-add-list-unique `verilog-library-extensions
9521 (match-string 1 arg))
9522 (setq arg (match-string 2 arg))))
9524 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ; -Ddefine=val
9525 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ; -Ddefine
9526 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ; +define+val
9527 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ; +define+define
9528 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
9530 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ; +incdir+dir
9531 (string-match "^-I\\(.*\\)" arg)) ; -Idir
9532 (verilog-add-list-unique `verilog-library-directories
9533 (match-string 1 (substitute-in-file-name arg))))
9534 ;; Ignore
9535 ((equal "+librescan" arg))
9536 ((string-match "^-U\\(.*\\)" arg)) ; -Udefine
9537 ;; Second parameters
9538 ((equal next-param "-f")
9539 (setq next-param nil)
9540 (verilog-getopt-file (substitute-in-file-name arg)))
9541 ((equal next-param "-v")
9542 (setq next-param nil)
9543 (verilog-add-list-unique `verilog-library-files
9544 (substitute-in-file-name arg)))
9545 ((equal next-param "-y")
9546 (setq next-param nil)
9547 (verilog-add-list-unique `verilog-library-directories
9548 (substitute-in-file-name arg)))
9549 ;; Filename
9550 ((string-match "^[^-+]" arg)
9551 (verilog-add-list-unique `verilog-library-files
9552 (substitute-in-file-name arg)))
9553 ;; Default - ignore; no warning
9554 ))))
9555 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
9557 (defun verilog-getopt-file (filename)
9558 "Read Verilog options from the specified FILENAME."
9559 (save-excursion
9560 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
9561 (orig-buffer (current-buffer))
9562 line)
9563 (if fns
9564 (set-buffer (find-file-noselect (car fns)))
9565 (error "%s: Can't find verilog-getopt-file -f file: %s"
9566 (verilog-point-text) filename))
9567 (goto-char (point-min))
9568 (while (not (eobp))
9569 (setq line (buffer-substring (point) (point-at-eol)))
9570 (forward-line 1)
9571 (when (string-match "//" line)
9572 (setq line (substring line 0 (match-beginning 0))))
9573 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
9574 (verilog-getopt line))))))
9576 (defun verilog-getopt-flags ()
9577 "Convert `verilog-library-flags' into standard library variables."
9578 ;; If the flags are local, then all the outputs should be local also
9579 (when (local-variable-p `verilog-library-flags (current-buffer))
9580 (mapc 'make-local-variable '(verilog-library-extensions
9581 verilog-library-directories
9582 verilog-library-files
9583 verilog-library-flags)))
9584 ;; Allow user to customize
9585 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
9586 ;; Process arguments
9587 (verilog-getopt verilog-library-flags)
9588 ;; Allow user to customize
9589 (verilog-run-hooks 'verilog-getopt-flags-hook))
9591 (defun verilog-add-list-unique (varref object)
9592 "Append to VARREF list the given OBJECT,
9593 unless it is already a member of the variable's list."
9594 (unless (member object (symbol-value varref))
9595 (set varref (append (symbol-value varref) (list object))))
9596 varref)
9597 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
9599 (defun verilog-current-flags ()
9600 "Convert `verilog-library-flags' and similar variables to command line.
9601 Used for __FLAGS__ in `verilog-expand-command'."
9602 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
9603 (when (equal cmd "")
9604 (setq cmd (concat
9605 "+libext+" (mapconcat `concat verilog-library-extensions "+")
9606 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
9607 verilog-library-directories "")
9608 (mapconcat (lambda (i) (concat " -v " i))
9609 verilog-library-files ""))))
9610 cmd))
9611 ;;(verilog-current-flags)
9614 ;;; Cached directory support:
9617 (defvar verilog-dir-cache-preserving nil
9618 "If true, the directory cache is enabled, and file system changes are ignored.
9619 See `verilog-dir-exists-p' and `verilog-dir-files'.")
9621 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
9622 (defvar verilog-dir-cache-list nil
9623 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
9624 (defvar verilog-dir-cache-lib-filenames nil
9625 "Cached data for `verilog-library-filenames'.")
9627 (defmacro verilog-preserve-dir-cache (&rest body)
9628 "Execute the BODY forms, allowing directory cache preservation within BODY.
9629 This means that changes inside BODY made to the file system will not be
9630 seen by the `verilog-dir-files' and related functions."
9631 `(let ((verilog-dir-cache-preserving (current-buffer))
9632 verilog-dir-cache-list
9633 verilog-dir-cache-lib-filenames)
9634 (progn ,@body)))
9636 (defun verilog-dir-files (dirname)
9637 "Return all filenames in the DIRNAME directory.
9638 Relative paths depend on the `default-directory'.
9639 Results are cached if inside `verilog-preserve-dir-cache'."
9640 (unless verilog-dir-cache-preserving
9641 (setq verilog-dir-cache-list nil)) ; Cache disabled
9642 ;; We don't use expand-file-name on the dirname to make key, as it's slow
9643 (let* ((cache-key (list dirname default-directory))
9644 (fass (assoc cache-key verilog-dir-cache-list))
9645 exp-dirname data)
9646 (cond (fass ; Return data from cache hit
9647 (nth 1 fass))
9649 (setq exp-dirname (expand-file-name dirname)
9650 data (and (file-directory-p exp-dirname)
9651 (directory-files exp-dirname nil nil nil)))
9652 ;; Note we also encache nil for non-existing dirs.
9653 (setq verilog-dir-cache-list (cons (list cache-key data)
9654 verilog-dir-cache-list))
9655 data))))
9656 ;; Miss-and-hit test:
9657 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
9658 ;; (prin1 (verilog-dir-files ".")) nil)
9660 (defun verilog-dir-file-exists-p (filename)
9661 "Return true if FILENAME exists.
9662 Like `file-exists-p' but results are cached if inside
9663 `verilog-preserve-dir-cache'."
9664 (let* ((dirname (file-name-directory filename))
9665 ;; Correct for file-name-nondirectory returning same if no slash.
9666 (dirnamed (if (or (not dirname) (equal dirname filename))
9667 default-directory dirname))
9668 (flist (verilog-dir-files dirnamed)))
9669 (and flist
9670 (member (file-name-nondirectory filename) flist)
9671 t)))
9672 ;;(verilog-dir-file-exists-p "verilog-mode.el")
9673 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
9676 ;;; Module name lookup:
9679 (defun verilog-module-inside-filename-p (module filename)
9680 "Return modi if MODULE is specified inside FILENAME, else nil.
9681 Allows version control to check out the file if need be."
9682 (and (or (file-exists-p filename)
9683 (and (fboundp 'vc-backend)
9684 (vc-backend filename)))
9685 (let (modi type)
9686 (with-current-buffer (find-file-noselect filename)
9687 (save-excursion
9688 (goto-char (point-min))
9689 (while (and
9690 ;; It may be tempting to look for verilog-defun-re,
9691 ;; don't, it slows things down a lot!
9692 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
9693 (setq type (match-string-no-properties 0))
9694 (verilog-re-search-forward-quick "[(;]" nil t))
9695 (if (equal module (verilog-read-module-name))
9696 (setq modi (verilog-modi-new module filename (point) type))))
9697 modi)))))
9699 (defun verilog-is-number (symbol)
9700 "Return true if SYMBOL is number-like."
9701 (or (string-match "^[0-9 \t:]+$" symbol)
9702 (string-match "^[---]*[0-9]+$" symbol)
9703 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
9705 (defun verilog-symbol-detick (symbol wing-it)
9706 "Return an expanded SYMBOL name without any defines.
9707 If the variable vh-{symbol} is defined, return that value.
9708 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
9709 (while (and symbol (string-match "^`" symbol))
9710 (setq symbol (substring symbol 1))
9711 (setq symbol
9712 ;; Namespace intentionally short for AUTOs and compatibility
9713 (if (boundp (intern (concat "vh-" symbol)))
9714 ;; Emacs has a bug where boundp on a buffer-local
9715 ;; variable in only one buffer returns t in another.
9716 ;; This can confuse, so check for nil.
9717 ;; Namespace intentionally short for AUTOs and compatibility
9718 (let ((val (eval (intern (concat "vh-" symbol)))))
9719 (if (eq val nil)
9720 (if wing-it symbol nil)
9721 val))
9722 (if wing-it symbol nil))))
9723 symbol)
9724 ;;(verilog-symbol-detick "`mod" nil)
9726 (defun verilog-symbol-detick-denumber (symbol)
9727 "Return SYMBOL with defines converted and any numbers dropped to nil."
9728 (when (string-match "^`" symbol)
9729 ;; This only will work if the define is a simple signal, not
9730 ;; something like a[b]. Sorry, it should be substituted into the parser
9731 (setq symbol
9732 (verilog-string-replace-matches
9733 "\\[[^0-9: \t]+\\]" "" nil nil
9734 (or (verilog-symbol-detick symbol nil)
9735 (if verilog-auto-sense-defines-constant
9737 symbol)))))
9738 (if (verilog-is-number symbol)
9740 symbol))
9742 (defun verilog-symbol-detick-text (text)
9743 "Return TEXT without any known defines.
9744 If the variable vh-{symbol} is defined, substitute that value."
9745 (let ((ok t) symbol val)
9746 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
9747 (setq symbol (match-string 1 text))
9748 ;;(message symbol)
9749 (cond ((and
9750 ;; Namespace intentionally short for AUTOs and compatibility
9751 (boundp (intern (concat "vh-" symbol)))
9752 ;; Emacs has a bug where boundp on a buffer-local
9753 ;; variable in only one buffer returns t in another.
9754 ;; This can confuse, so check for nil.
9755 ;; Namespace intentionally short for AUTOs and compatibility
9756 (setq val (eval (intern (concat "vh-" symbol)))))
9757 (setq text (replace-match val nil nil text)))
9758 (t (setq ok nil)))))
9759 text)
9760 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
9762 (defun verilog-expand-dirnames (&optional dirnames)
9763 "Return a list of existing directories given a list of wildcarded DIRNAMES.
9764 Or, just the existing dirnames themselves if there are no wildcards."
9765 ;; Note this function is performance critical.
9766 ;; Do not call anything that requires disk access that cannot be cached.
9767 (interactive)
9768 (unless dirnames
9769 (error "`verilog-library-directories' should include at least `.'"))
9770 (setq dirnames (reverse dirnames)) ; not nreverse
9771 (let ((dirlist nil)
9772 pattern dirfile dirfiles dirname root filename rest basefile)
9773 (while dirnames
9774 (setq dirname (substitute-in-file-name (car dirnames))
9775 dirnames (cdr dirnames))
9776 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ; root
9777 "\\([^/\\]*[*?][^/\\]*\\)" ; filename with *?
9778 "\\(.*\\)") ; rest
9779 dirname)
9780 (setq root (match-string 1 dirname)
9781 filename (match-string 2 dirname)
9782 rest (match-string 3 dirname)
9783 pattern filename)
9784 ;; now replace those * and ? with .+ and .
9785 ;; use ^ and /> to get only whole file names
9786 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9787 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
9788 pattern (concat "^" pattern "$")
9789 dirfiles (verilog-dir-files root))
9790 (while dirfiles
9791 (setq basefile (car dirfiles)
9792 dirfile (expand-file-name (concat root basefile rest))
9793 dirfiles (cdr dirfiles))
9794 (if (and (string-match pattern basefile)
9795 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9796 (not (equal basefile "."))
9797 (not (equal basefile ".."))
9798 (file-directory-p dirfile))
9799 (setq dirlist (cons dirfile dirlist)))))
9800 ;; Defaults
9802 (if (file-directory-p dirname)
9803 (setq dirlist (cons dirname dirlist))))))
9804 dirlist))
9805 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9807 (defun verilog-library-filenames (filename &optional current check-ext)
9808 "Return a search path to find the given FILENAME or module name.
9809 Uses the optional CURRENT filename or variable `buffer-file-name', plus
9810 `verilog-library-directories' and `verilog-library-extensions'
9811 variables to build the path. With optional CHECK-EXT also check
9812 `verilog-library-extensions'."
9813 (unless current (setq current (buffer-file-name)))
9814 (unless verilog-dir-cache-preserving
9815 (setq verilog-dir-cache-lib-filenames nil))
9816 (let* ((cache-key (list filename current check-ext))
9817 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9818 chkdirs chkdir chkexts fn outlist)
9819 (cond (fass ; Return data from cache hit
9820 (nth 1 fass))
9822 ;; Note this expand can't be easily cached, as we need to
9823 ;; pick up buffer-local variables for newly read sub-module files
9824 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9825 (while chkdirs
9826 (setq chkdir (expand-file-name (car chkdirs)
9827 (file-name-directory current))
9828 chkexts (if check-ext verilog-library-extensions `("")))
9829 (while chkexts
9830 (setq fn (expand-file-name (concat filename (car chkexts))
9831 chkdir))
9832 ;;(message "Check for %s" fn)
9833 (if (verilog-dir-file-exists-p fn)
9834 (setq outlist (cons (expand-file-name
9835 fn (file-name-directory current))
9836 outlist)))
9837 (setq chkexts (cdr chkexts)))
9838 (setq chkdirs (cdr chkdirs)))
9839 (setq outlist (nreverse outlist))
9840 (setq verilog-dir-cache-lib-filenames
9841 (cons (list cache-key outlist)
9842 verilog-dir-cache-lib-filenames))
9843 outlist))))
9845 (defun verilog-module-filenames (module current)
9846 "Return a search path to find the given MODULE name.
9847 Uses the CURRENT filename, `verilog-library-extensions',
9848 `verilog-library-directories' and `verilog-library-files'
9849 variables to build the path."
9850 ;; Return search locations for it
9851 (append (list current) ; first, current buffer
9852 (verilog-library-filenames module current t)
9853 verilog-library-files)) ; finally, any libraries
9856 ;; Module Information
9858 ;; Many of these functions work on "modi" a module information structure
9859 ;; A modi is: [module-name-string file-name begin-point]
9861 (defvar verilog-cache-enabled t
9862 "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
9864 (defvar verilog-modi-cache-list nil
9865 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9866 For speeding up verilog-modi-get-* commands.
9867 Buffer-local.")
9868 (make-variable-buffer-local 'verilog-modi-cache-list)
9870 (defvar verilog-modi-cache-preserve-tick nil
9871 "Modification tick after which the cache is still considered valid.
9872 Use `verilog-preserve-modi-cache' to set it.")
9873 (defvar verilog-modi-cache-preserve-buffer nil
9874 "Modification tick after which the cache is still considered valid.
9875 Use `verilog-preserve-modi-cache' to set it.")
9876 (defvar verilog-modi-cache-current-enable nil
9877 "Non-nil means allow caching `verilog-modi-current', set by let().")
9878 (defvar verilog-modi-cache-current nil
9879 "Currently active `verilog-modi-current', if any, set by let().")
9880 (defvar verilog-modi-cache-current-max nil
9881 "Current endmodule point for `verilog-modi-cache-current', if any.")
9883 (defun verilog-modi-current ()
9884 "Return the modi structure for the module currently at point, possibly cached."
9885 (cond ((and verilog-modi-cache-current
9886 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9887 (<= (point) verilog-modi-cache-current-max))
9888 ;; Slow assertion, for debugging the cache:
9889 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9890 verilog-modi-cache-current)
9891 (verilog-modi-cache-current-enable
9892 (setq verilog-modi-cache-current (verilog-modi-current-get)
9893 verilog-modi-cache-current-max
9894 ;; The cache expires when we pass "endmodule" as then the
9895 ;; current modi may change to the next module
9896 ;; This relies on the AUTOs generally inserting, not deleting text
9897 (save-excursion
9898 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9899 verilog-modi-cache-current)
9901 (verilog-modi-current-get))))
9903 (defun verilog-modi-current-get ()
9904 "Return the modi structure for the module currently at point."
9905 (let* (name type pt)
9906 ;; read current module's name
9907 (save-excursion
9908 (verilog-re-search-backward-quick verilog-defun-re nil nil)
9909 (setq type (match-string-no-properties 0))
9910 (verilog-re-search-forward-quick "(" nil nil)
9911 (setq name (verilog-read-module-name))
9912 (setq pt (point)))
9913 ;; return modi - note this vector built two places
9914 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
9916 (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9917 (make-variable-buffer-local 'verilog-modi-lookup-cache)
9918 (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9919 (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
9921 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9922 "Find the file and point at which MODULE is defined.
9923 If ALLOW-CACHE is set, check and remember cache of previous lookups.
9924 Return modi if successful, else print message unless IGNORE-ERROR is true."
9925 (let* ((current (or (buffer-file-name) (current-buffer)))
9926 modi)
9927 ;; Check cache
9928 ;;(message "verilog-modi-lookup: %s" module)
9929 (cond ((and verilog-modi-lookup-cache
9930 verilog-cache-enabled
9931 allow-cache
9932 (setq modi (gethash module verilog-modi-lookup-cache))
9933 (equal verilog-modi-lookup-last-current current)
9934 ;; If hit is in current buffer, then tick must match
9935 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9936 (not (equal current (verilog-modi-file-or-buffer modi)))))
9937 ;;(message "verilog-modi-lookup: HIT %S" modi)
9938 modi)
9939 ;; Miss
9940 (t (let* ((realname (verilog-symbol-detick module t))
9941 (orig-filenames (verilog-module-filenames realname current))
9942 (filenames orig-filenames)
9943 mif)
9944 (while (and filenames (not mif))
9945 (if (not (setq mif (verilog-module-inside-filename-p realname (car filenames))))
9946 (setq filenames (cdr filenames))))
9947 ;; mif has correct form to become later elements of modi
9948 (setq modi mif)
9949 (or mif ignore-error
9950 (error
9951 (concat
9952 "%s: Can't locate `%s' module definition%s"
9953 "\n Check the verilog-library-directories variable."
9954 "\n I looked in (if not listed, doesn't exist):\n\t%s")
9955 (verilog-point-text) module
9956 (if (not (equal module realname))
9957 (concat " (Expanded macro to " realname ")")
9959 (mapconcat 'concat orig-filenames "\n\t")))
9960 (when (eval-when-compile (fboundp 'make-hash-table))
9961 (unless verilog-modi-lookup-cache
9962 (setq verilog-modi-lookup-cache
9963 (make-hash-table :test 'equal :rehash-size 4.0)))
9964 (puthash module modi verilog-modi-lookup-cache))
9965 (setq verilog-modi-lookup-last-current current
9966 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9967 modi))
9969 (defun verilog-modi-filename (modi)
9970 "Filename of MODI, or name of buffer if it's never been saved."
9971 (if (bufferp (verilog-modi-file-or-buffer modi))
9972 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9973 (buffer-name (verilog-modi-file-or-buffer modi)))
9974 (verilog-modi-file-or-buffer modi)))
9976 (defun verilog-modi-goto (modi)
9977 "Move point/buffer to specified MODI."
9978 (or modi (error "Passed unfound modi to goto, check earlier"))
9979 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9980 (verilog-modi-file-or-buffer modi)
9981 (find-file-noselect (verilog-modi-file-or-buffer modi))))
9982 (or (equal major-mode `verilog-mode) ; Put into Verilog mode to get syntax
9983 (verilog-mode))
9984 (goto-char (verilog-modi-get-point modi)))
9986 (defun verilog-goto-defun-file (module)
9987 "Move point to the file at which a given MODULE is defined."
9988 (interactive "sGoto File for Module: ")
9989 (let* ((modi (verilog-modi-lookup module nil)))
9990 (when modi
9991 (verilog-modi-goto modi)
9992 (switch-to-buffer (current-buffer)))))
9994 (defun verilog-modi-cache-results (modi function)
9995 "Run on MODI the given FUNCTION. Locate the module in a file.
9996 Cache the output of function so next call may have faster access."
9997 (let (fass)
9998 (save-excursion ; Cache is buffer-local so can't avoid this.
9999 (verilog-modi-goto modi)
10000 (if (and (setq fass (assoc (list modi function)
10001 verilog-modi-cache-list))
10002 ;; Destroy caching when incorrect; Modified or file changed
10003 (not (and verilog-cache-enabled
10004 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
10005 (and verilog-modi-cache-preserve-tick
10006 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
10007 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
10008 (equal (visited-file-modtime) (nth 2 fass)))))
10009 (setq verilog-modi-cache-list nil
10010 fass nil))
10011 (cond (fass
10012 ;; Return data from cache hit
10013 (nth 3 fass))
10015 ;; Read from file
10016 ;; Clear then restore any highlighting to make emacs19 happy
10017 (let ((func-returns
10018 (verilog-save-font-no-change-functions
10019 (funcall function))))
10020 ;; Cache for next time
10021 (setq verilog-modi-cache-list
10022 (cons (list (list modi function)
10023 (buffer-chars-modified-tick)
10024 (visited-file-modtime)
10025 func-returns)
10026 verilog-modi-cache-list))
10027 func-returns))))))
10029 (defun verilog-modi-cache-add (modi function element sig-list)
10030 "Add function return results to the module cache.
10031 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
10032 function now contains the additional SIG-LIST parameters."
10033 (let (fass)
10034 (save-excursion
10035 (verilog-modi-goto modi)
10036 (if (setq fass (assoc (list modi function)
10037 verilog-modi-cache-list))
10038 (let ((func-returns (nth 3 fass)))
10039 (aset func-returns element
10040 (append sig-list (aref func-returns element))))))))
10042 (defmacro verilog-preserve-modi-cache (&rest body)
10043 "Execute the BODY forms, allowing cache preservation within BODY.
10044 This means that changes to the buffer will not result in the cache being
10045 flushed. If the changes affect the modsig state, they must call the
10046 modsig-cache-add-* function, else the results of later calls may be
10047 incorrect. Without this, changes are assumed to be adding/removing signals
10048 and invalidating the cache."
10049 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
10050 (verilog-modi-cache-preserve-buffer (current-buffer)))
10051 (progn ,@body)))
10054 (defun verilog-modi-modport-lookup-one (modi name &optional ignore-error)
10055 "Given a MODI, return the declarations related to the given modport NAME.
10056 Report errors unless optional IGNORE-ERROR."
10057 ;; Recursive routine - see below
10058 (let* ((realname (verilog-symbol-detick name t))
10059 (modport (assoc name (verilog-decls-get-modports (verilog-modi-get-decls modi)))))
10060 (or modport ignore-error
10061 (error "%s: Can't locate `%s' modport definition%s"
10062 (verilog-point-text) name
10063 (if (not (equal name realname))
10064 (concat " (Expanded macro to " realname ")")
10065 "")))
10066 (let* ((decls (verilog-modport-decls modport))
10067 (clks (verilog-modport-clockings modport)))
10068 ;; Now expand any clocking's
10069 (while clks
10070 (setq decls (verilog-decls-append
10071 decls
10072 (verilog-modi-modport-lookup-one modi (car clks) ignore-error)))
10073 (setq clks (cdr clks)))
10074 decls)))
10076 (defun verilog-modi-modport-lookup (modi name-re &optional ignore-error)
10077 "Given a MODI, return the declarations related to the given modport NAME-RE.
10078 If the modport points to any clocking blocks, expand the signals to include
10079 those clocking block's signals."
10080 ;; Recursive routine - see below
10081 (let* ((mod-decls (verilog-modi-get-decls modi))
10082 (clks (verilog-decls-get-modports mod-decls))
10083 (name-re (concat "^" name-re "$"))
10084 (decls (verilog-decls-new nil nil nil nil nil nil nil nil nil)))
10085 ;; Pull in all modports
10086 (while clks
10087 (when (string-match name-re (verilog-modport-name (car clks)))
10088 (setq decls (verilog-decls-append
10089 decls
10090 (verilog-modi-modport-lookup-one modi (verilog-modport-name (car clks)) ignore-error))))
10091 (setq clks (cdr clks)))
10092 decls))
10094 (defun verilog-signals-matching-enum (in-list enum)
10095 "Return all signals in IN-LIST matching the given ENUM."
10096 (let (out-list)
10097 (while in-list
10098 (if (equal (verilog-sig-enum (car in-list)) enum)
10099 (setq out-list (cons (car in-list) out-list)))
10100 (setq in-list (cdr in-list)))
10101 ;; New scheme
10102 ;; Namespace intentionally short for AUTOs and compatibility
10103 (let* ((enumvar (intern (concat "venum-" enum)))
10104 (enumlist (and (boundp enumvar) (eval enumvar))))
10105 (while enumlist
10106 (add-to-list 'out-list (list (car enumlist)))
10107 (setq enumlist (cdr enumlist))))
10108 (nreverse out-list)))
10110 (defun verilog-signals-matching-regexp (in-list regexp)
10111 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
10112 (if (or (not regexp) (equal regexp ""))
10113 in-list
10114 (let ((case-fold-search verilog-case-fold)
10115 out-list)
10116 (while in-list
10117 (if (string-match regexp (verilog-sig-name (car in-list)))
10118 (setq out-list (cons (car in-list) out-list)))
10119 (setq in-list (cdr in-list)))
10120 (nreverse out-list))))
10122 (defun verilog-signals-not-matching-regexp (in-list regexp)
10123 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
10124 (if (or (not regexp) (equal regexp ""))
10125 in-list
10126 (let ((case-fold-search verilog-case-fold)
10127 out-list)
10128 (while in-list
10129 (if (not (string-match regexp (verilog-sig-name (car in-list))))
10130 (setq out-list (cons (car in-list) out-list)))
10131 (setq in-list (cdr in-list)))
10132 (nreverse out-list))))
10134 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
10135 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
10136 if non-nil."
10137 (if (or (not regexp) (equal regexp ""))
10138 in-list
10139 (let (out-list to-match)
10140 (while in-list
10141 ;; Note verilog-insert-one-definition matches on this order
10142 (setq to-match (concat
10143 decl-type
10144 " " (verilog-sig-signed (car in-list))
10145 " " (verilog-sig-multidim (car in-list))
10146 (verilog-sig-bits (car in-list))))
10147 (if (string-match regexp to-match)
10148 (setq out-list (cons (car in-list) out-list)))
10149 (setq in-list (cdr in-list)))
10150 (nreverse out-list))))
10152 (defun verilog-signals-edit-wire-reg (in-list)
10153 "Return all signals in IN-LIST with wire/reg data types made blank."
10154 (mapcar (lambda (sig)
10155 (when (member (verilog-sig-type sig) '("wire" "reg"))
10156 (verilog-sig-type-set sig nil))
10157 sig) in-list))
10159 ;; Combined
10160 (defun verilog-decls-get-signals (decls)
10161 "Return all declared signals in DECLS, excluding `assign' statements."
10162 (append
10163 (verilog-decls-get-outputs decls)
10164 (verilog-decls-get-inouts decls)
10165 (verilog-decls-get-inputs decls)
10166 (verilog-decls-get-vars decls)
10167 (verilog-decls-get-consts decls)
10168 (verilog-decls-get-gparams decls)))
10170 (defun verilog-decls-get-ports (decls)
10171 (append
10172 (verilog-decls-get-outputs decls)
10173 (verilog-decls-get-inouts decls)
10174 (verilog-decls-get-inputs decls)))
10176 (defun verilog-decls-get-iovars (decls)
10177 (append
10178 (verilog-decls-get-vars decls)
10179 (verilog-decls-get-outputs decls)
10180 (verilog-decls-get-inouts decls)
10181 (verilog-decls-get-inputs decls)))
10183 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
10184 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
10185 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
10186 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
10187 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
10188 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
10189 (defsubst verilog-modi-cache-add-vars (modi sig-list)
10190 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
10191 (defsubst verilog-modi-cache-add-gparams (modi sig-list)
10192 (verilog-modi-cache-add modi 'verilog-read-decls 7 sig-list))
10195 ;;; Auto creation utilities:
10198 (defun verilog-auto-re-search-do (search-for func)
10199 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
10200 (goto-char (point-min))
10201 (while (verilog-re-search-forward-quick search-for nil t)
10202 (funcall func)))
10204 (defun verilog-insert-one-definition (sig type indent-pt)
10205 "Print out a definition for SIG of the given TYPE,
10206 with appropriate INDENT-PT indentation."
10207 (indent-to indent-pt)
10208 ;; Note verilog-signals-matching-dir-re matches on this order
10209 (insert type)
10210 (when (verilog-sig-modport sig)
10211 (insert "." (verilog-sig-modport sig)))
10212 (when (verilog-sig-signed sig)
10213 (insert " " (verilog-sig-signed sig)))
10214 (when (verilog-sig-multidim sig)
10215 (insert " " (verilog-sig-multidim-string sig)))
10216 (when (verilog-sig-bits sig)
10217 (insert " " (verilog-sig-bits sig)))
10218 (indent-to (max 24 (+ indent-pt 16)))
10219 (unless (= (char-syntax (preceding-char)) ?\ )
10220 (insert " ")) ; Need space between "]name" if indent-to did nothing
10221 (insert (verilog-sig-name sig))
10222 (when (verilog-sig-memory sig)
10223 (insert " " (verilog-sig-memory sig))))
10225 (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
10226 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
10227 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
10228 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
10229 When MODI is non-null, also add to modi-cache, for tracking."
10230 (when modi
10231 (cond ((equal direction "wire")
10232 (verilog-modi-cache-add-vars modi sigs))
10233 ((equal direction "reg")
10234 (verilog-modi-cache-add-vars modi sigs))
10235 ((equal direction "output")
10236 (verilog-modi-cache-add-outputs modi sigs)
10237 (when verilog-auto-declare-nettype
10238 (verilog-modi-cache-add-vars modi sigs)))
10239 ((equal direction "input")
10240 (verilog-modi-cache-add-inputs modi sigs)
10241 (when verilog-auto-declare-nettype
10242 (verilog-modi-cache-add-vars modi sigs)))
10243 ((equal direction "inout")
10244 (verilog-modi-cache-add-inouts modi sigs)
10245 (when verilog-auto-declare-nettype
10246 (verilog-modi-cache-add-vars modi sigs)))
10247 ((equal direction "interface"))
10248 ((equal direction "parameter")
10249 (verilog-modi-cache-add-gparams modi sigs))
10251 (error "Unsupported verilog-insert-definition direction: `%s'" direction))))
10252 (or dont-sort
10253 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10254 (while sigs
10255 (let ((sig (car sigs)))
10256 (verilog-insert-one-definition
10258 ;; Want "type x" or "output type x", not "wire type x"
10259 (cond ((or (verilog-sig-type sig)
10260 verilog-auto-wire-type)
10261 (concat
10262 (when (member direction '("input" "output" "inout"))
10263 (concat direction " "))
10264 (or (verilog-sig-type sig)
10265 verilog-auto-wire-type)))
10266 ((and verilog-auto-declare-nettype
10267 (member direction '("input" "output" "inout")))
10268 (concat direction " " verilog-auto-declare-nettype))
10270 direction))
10271 indent-pt)
10272 (insert (if v2k "," ";"))
10273 (if (or (not (verilog-sig-comment sig))
10274 (equal "" (verilog-sig-comment sig)))
10275 (insert "\n")
10276 (indent-to (max 48 (+ indent-pt 40)))
10277 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
10278 (setq sigs (cdr sigs)))))
10280 (eval-when-compile
10281 (if (not (boundp 'indent-pt))
10282 (defvar indent-pt nil "Local used by `verilog-insert-indent'.")))
10284 (defun verilog-insert-indent (&rest stuff)
10285 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
10286 Presumes that any newlines end a list element."
10287 (let ((need-indent t))
10288 (while stuff
10289 (if need-indent (indent-to indent-pt))
10290 (setq need-indent nil)
10291 (verilog-insert (car stuff))
10292 (setq need-indent (string-match "\n$" (car stuff))
10293 stuff (cdr stuff)))))
10294 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
10296 (defun verilog-forward-or-insert-line ()
10297 "Move forward a line, unless at EOB, then insert a newline."
10298 (if (eobp) (insert "\n")
10299 (forward-line)))
10301 (defun verilog-repair-open-comma ()
10302 "Insert comma if previous argument is other than an open parenthesis or endif."
10303 ;; We can't just search backward for ) as it might be inside another expression.
10304 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
10305 (save-excursion
10306 (verilog-backward-syntactic-ws-quick)
10307 (when (and (not (save-excursion ; Not beginning (, or existing ,
10308 (backward-char 1)
10309 (looking-at "[(,]")))
10310 (not (save-excursion ; Not `endif, or user define
10311 (backward-char 1)
10312 (skip-chars-backward "[a-zA-Z0-9_`]")
10313 (looking-at "`"))))
10314 (insert ","))))
10316 (defun verilog-repair-close-comma ()
10317 "If point is at a comma followed by a close parenthesis, fix it.
10318 This repairs those mis-inserted by an AUTOARG."
10319 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
10320 (save-excursion
10321 (verilog-forward-close-paren)
10322 (backward-char 1)
10323 (verilog-backward-syntactic-ws-quick)
10324 (backward-char 1)
10325 (when (looking-at ",")
10326 (delete-char 1))))
10328 (defun verilog-make-width-expression (range-exp)
10329 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
10330 ;; strip off the []
10331 (cond ((not range-exp)
10332 "1")
10334 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
10335 (setq range-exp (match-string 1 range-exp)))
10336 (cond ((not range-exp)
10337 "1")
10338 ;; [#:#] We can compute a numeric result
10339 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
10340 range-exp)
10341 (int-to-string
10342 (1+ (abs (- (string-to-number (match-string 1 range-exp))
10343 (string-to-number (match-string 2 range-exp)))))))
10344 ;; [PARAM-1:0] can just return PARAM
10345 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
10346 (match-string 1 range-exp))
10347 ;; [arbitrary] need math
10348 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
10349 (concat "(1+(" (match-string 1 range-exp) ")"
10350 (if (equal "0" (match-string 2 range-exp))
10351 "" ; Don't bother with -(0)
10352 (concat "-(" (match-string 2 range-exp) ")"))
10353 ")"))
10354 (t nil)))))
10355 ;;(verilog-make-width-expression "`A:`B")
10357 (defun verilog-simplify-range-expression (expr)
10358 "Return a simplified range expression with constants eliminated from EXPR."
10359 ;; Note this is always called with brackets; ie [z] or [z:z]
10360 (if (not (string-match "[---+*()]" expr))
10361 expr ; short-circuit
10362 (let ((out expr)
10363 (last-pass ""))
10364 (while (not (equal last-pass out))
10365 (setq last-pass out)
10366 ;; Prefix regexp needs beginning of match, or some symbol of
10367 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
10368 ;; Ditto the end.
10369 (while (string-match
10370 (concat "\\([[({:*+-]\\)" ; - must be last
10371 "(\\<\\([0-9A-Za-z_]+\\))"
10372 "\\([])}:*+-]\\)")
10373 out)
10374 (setq out (replace-match "\\1\\2\\3" nil nil out)))
10375 (while (string-match
10376 (concat "\\([[({:*+-]\\)" ; - must be last
10377 "\\$clog2\\s *(\\<\\([0-9]+\\))"
10378 "\\([])}:*+-]\\)")
10379 out)
10380 (setq out (replace-match
10381 (concat
10382 (match-string 1 out)
10383 (int-to-string (verilog-clog2 (string-to-number (match-string 2 out))))
10384 (match-string 3 out))
10385 nil nil out)))
10386 ;; For precedence do * before +/-
10387 (while (string-match
10388 (concat "\\([[({:*+-]\\)"
10389 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
10390 "\\([])}:*+-]\\)")
10391 out)
10392 (setq out (replace-match
10393 (concat (match-string 1 out)
10394 (int-to-string (* (string-to-number (match-string 2 out))
10395 (string-to-number (match-string 4 out))))
10396 (match-string 5 out))
10397 nil nil out)))
10398 (while (string-match
10399 (concat "\\([[({:+-]\\)" ; No * here as higher prec
10400 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
10401 "\\([])}:+-]\\)")
10402 out)
10403 (let ((pre (match-string 1 out))
10404 (lhs (string-to-number (match-string 2 out)))
10405 (rhs (string-to-number (match-string 4 out)))
10406 (post (match-string 5 out))
10407 val)
10408 (when (equal pre "-")
10409 (setq lhs (- lhs)))
10410 (setq val (if (equal (match-string 3 out) "-")
10411 (- lhs rhs)
10412 (+ lhs rhs))
10413 out (replace-match
10414 (concat (if (and (equal pre "-")
10415 (< val 0))
10416 "" ; Not "--20" but just "-20"
10417 pre)
10418 (int-to-string val)
10419 post)
10420 nil nil out)) )))
10421 out)))
10423 ;;(verilog-simplify-range-expression "[1:3]") ; 1
10424 ;;(verilog-simplify-range-expression "[(1):3]") ; 1
10425 ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ; 20
10426 ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ; 48
10427 ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ; FOO*4-2
10428 ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ; FOO*4+0
10429 ;;(verilog-simplify-range-expression "[(func(BAR))]") ; func(BAR)
10430 ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ; FOO-0
10431 ;;(verilog-simplify-range-expression "[$clog2(2)]") ; 1
10432 ;;(verilog-simplify-range-expression "[$clog2(7)]") ; 3
10434 (defun verilog-clog2 (value)
10435 "Compute $clog2 - ceiling log2 of VALUE."
10436 (if (< value 1)
10438 (ceiling (/ (log value) (log 2)))))
10440 (defun verilog-typedef-name-p (variable-name)
10441 "Return true if the VARIABLE-NAME is a type definition."
10442 (when verilog-typedef-regexp
10443 (verilog-string-match-fold verilog-typedef-regexp variable-name)))
10445 ;;; Auto deletion:
10448 (defun verilog-delete-autos-lined ()
10449 "Delete autos that occupy multiple lines, between begin and end comments."
10450 ;; The newline must not have a comment property, so we must
10451 ;; delete the end auto's newline, not the first newline
10452 (forward-line 1)
10453 (let ((pt (point)))
10454 (when (and
10455 (looking-at "\\s-*// Beginning")
10456 (search-forward "// End of automatic" nil t))
10457 ;; End exists
10458 (end-of-line)
10459 (forward-line 1)
10460 (delete-region pt (point)))))
10462 (defun verilog-delete-empty-auto-pair ()
10463 "Delete begin/end auto pair at point, if empty."
10464 (forward-line 0)
10465 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
10466 "\\s-*// End of automatics\n"))
10467 (delete-region (point) (save-excursion (forward-line 2) (point)))))
10469 (defun verilog-forward-close-paren ()
10470 "Find the close parenthesis that match the current point.
10471 Ignore other close parenthesis with matching open parens."
10472 (let ((parens 1))
10473 (while (> parens 0)
10474 (unless (verilog-re-search-forward-quick "[()]" nil t)
10475 (error "%s: Mismatching ()" (verilog-point-text)))
10476 (cond ((= (preceding-char) ?\( )
10477 (setq parens (1+ parens)))
10478 ((= (preceding-char) ?\) )
10479 (setq parens (1- parens)))))))
10481 (defun verilog-backward-open-paren ()
10482 "Find the open parenthesis that match the current point.
10483 Ignore other open parenthesis with matching close parens."
10484 (let ((parens 1))
10485 (while (> parens 0)
10486 (unless (verilog-re-search-backward-quick "[()]" nil t)
10487 (error "%s: Mismatching ()" (verilog-point-text)))
10488 (cond ((= (following-char) ?\) )
10489 (setq parens (1+ parens)))
10490 ((= (following-char) ?\( )
10491 (setq parens (1- parens)))))))
10493 (defun verilog-backward-open-bracket ()
10494 "Find the open bracket that match the current point.
10495 Ignore other open bracket with matching close bracket."
10496 (let ((parens 1))
10497 (while (> parens 0)
10498 (unless (verilog-re-search-backward-quick "[][]" nil t)
10499 (error "%s: Mismatching []" (verilog-point-text)))
10500 (cond ((= (following-char) ?\] )
10501 (setq parens (1+ parens)))
10502 ((= (following-char) ?\[ )
10503 (setq parens (1- parens)))))))
10505 (defun verilog-delete-to-paren ()
10506 "Delete the automatic inst/sense/arg created by autos.
10507 Deletion stops at the matching end parenthesis, outside comments."
10508 (delete-region (point)
10509 (save-excursion
10510 (verilog-backward-open-paren)
10511 (verilog-forward-sexp-ign-cmt 1) ; Moves to paren that closes argdecl's
10512 (backward-char 1)
10513 (point))))
10515 (defun verilog-auto-star-safe ()
10516 "Return if a .* AUTOINST is safe to delete or expand.
10517 It was created by the AUTOS themselves, or by the user."
10518 (and verilog-auto-star-expand
10519 (looking-at
10520 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
10522 (defun verilog-delete-auto-star-all ()
10523 "Delete a .* AUTOINST, if it is safe."
10524 (when (verilog-auto-star-safe)
10525 (verilog-delete-to-paren)))
10527 (defun verilog-delete-auto-star-implicit ()
10528 "Delete all .* implicit connections created by `verilog-auto-star'.
10529 This function will be called automatically at save unless
10530 `verilog-auto-star-save' is set, any non-templated expanded pins will be
10531 removed."
10532 (interactive)
10533 (let (paren-pt indent have-close-paren)
10534 (save-excursion
10535 (goto-char (point-min))
10536 ;; We need to match these even outside of comments.
10537 ;; For reasonable performance, we don't check if inside comments, sorry.
10538 (while (re-search-forward "// Implicit \\.\\*" nil t)
10539 (setq paren-pt (point))
10540 (beginning-of-line)
10541 (setq have-close-paren
10542 (save-excursion
10543 (when (search-forward ");" paren-pt t)
10544 (setq indent (current-indentation))
10545 t)))
10546 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
10547 (when have-close-paren
10548 ;; Delete extra commentary
10549 (save-excursion
10550 (while (progn
10551 (forward-line -1)
10552 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
10553 (delete-region (match-beginning 0) (match-end 0))))
10554 ;; If it is simple, we can put the ); on the same line as the last text
10555 (let ((rtn-pt (point)))
10556 (save-excursion
10557 (while (progn (backward-char 1)
10558 (looking-at "[ \t\n\f]")))
10559 (when (looking-at ",")
10560 (delete-region (+ 1 (point)) rtn-pt))))
10561 (when (bolp)
10562 (indent-to indent))
10563 (insert ");\n")
10564 ;; Still need to kill final comma - always is one as we put one after the .*
10565 (re-search-backward ",")
10566 (delete-char 1))))))
10568 (defun verilog-delete-auto-buffer ()
10569 "Perform `verilog-delete-auto' on the current buffer.
10570 Intended for internal use inside a `verilog-save-font-no-change-functions' block."
10571 ;; Allow user to customize
10572 (verilog-run-hooks 'verilog-before-delete-auto-hook)
10574 ;; Remove those that have multi-line insertions, possibly with parameters
10575 ;; We allow anything beginning with AUTO, so that users can add their own
10576 ;; patterns
10577 (verilog-auto-re-search-do
10578 (concat "/\\*AUTO[A-Za-z0-9_]+"
10579 ;; Optional parens or quoted parameter or .* for (((...)))
10580 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
10581 "\\*/")
10582 'verilog-delete-autos-lined)
10583 ;; Remove those that are in parenthesis
10584 (verilog-auto-re-search-do
10585 (concat "/\\*"
10586 (eval-when-compile
10587 (verilog-regexp-words
10588 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
10589 "AUTOSENSE")))
10590 "\\*/")
10591 'verilog-delete-to-paren)
10592 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
10593 (verilog-auto-re-search-do "\\.\\*"
10594 'verilog-delete-auto-star-all)
10595 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
10596 (goto-char (point-min))
10597 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
10598 (replace-match ""))
10600 ;; Final customize
10601 (verilog-run-hooks 'verilog-delete-auto-hook))
10603 (defun verilog-delete-auto ()
10604 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
10605 Use \\[verilog-auto] to re-insert the updated AUTOs.
10607 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
10608 called before and after this function, respectively."
10609 (interactive)
10610 (save-excursion
10611 (if (buffer-file-name)
10612 (find-file-noselect (buffer-file-name))) ; To check we have latest version
10613 (verilog-save-font-no-change-functions
10614 (verilog-save-scan-cache
10615 (verilog-delete-auto-buffer)))))
10618 ;;; Auto inject:
10621 (defun verilog-inject-auto ()
10622 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
10624 Any always @ blocks with sensitivity lists that match computed lists will
10625 be replaced with /*AS*/ comments.
10627 Any cells will get /*AUTOINST*/ added to the end of the pin list.
10628 Pins with have identical names will be deleted.
10630 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
10631 support adding new ports. You may wish to delete older ports yourself.
10633 For example:
10635 module ExampInject (i, o);
10636 input i;
10637 input j;
10638 output o;
10639 always @ (i or j)
10640 o = i | j;
10641 InstModule instName
10642 (.foobar(baz),
10643 j(j));
10644 endmodule
10646 Typing \\[verilog-inject-auto] will make this into:
10648 module ExampInject (i, o/*AUTOARG*/
10649 // Inputs
10651 input i;
10652 output o;
10653 always @ (/*AS*/i or j)
10654 o = i | j;
10655 InstModule instName
10656 (.foobar(baz),
10657 /*AUTOINST*/
10658 // Outputs
10659 j(j));
10660 endmodule"
10661 (interactive)
10662 (verilog-auto t))
10664 (defun verilog-inject-arg ()
10665 "Inject AUTOARG into new code. See `verilog-inject-auto'."
10666 ;; Presume one module per file.
10667 (save-excursion
10668 (goto-char (point-min))
10669 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
10670 (let ((endmodp (save-excursion
10671 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
10672 (point))))
10673 ;; See if there's already a comment .. inside a comment so not verilog-re-search
10674 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
10675 (verilog-re-search-forward-quick ";" nil t)
10676 (backward-char 1)
10677 (verilog-backward-syntactic-ws-quick)
10678 (backward-char 1) ; Moves to paren that closes argdecl's
10679 (when (looking-at ")")
10680 (verilog-insert "/*AUTOARG*/")))))))
10682 (defun verilog-inject-sense ()
10683 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
10684 (save-excursion
10685 (goto-char (point-min))
10686 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
10687 (let* ((start-pt (point))
10688 (modi (verilog-modi-current))
10689 (moddecls (verilog-modi-get-decls modi))
10690 pre-sigs
10691 got-sigs)
10692 (backward-char 1)
10693 (verilog-forward-sexp-ign-cmt 1)
10694 (backward-char 1) ; End )
10695 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
10696 (setq pre-sigs (verilog-signals-from-signame
10697 (verilog-read-signals start-pt (point)))
10698 got-sigs (verilog-auto-sense-sigs moddecls nil))
10699 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
10700 (verilog-signals-not-in got-sigs pre-sigs)))
10701 (delete-region start-pt (point))
10702 (verilog-insert "/*AS*/")))))))
10704 (defun verilog-inject-inst ()
10705 "Inject AUTOINST into new code. See `verilog-inject-auto'."
10706 (save-excursion
10707 (goto-char (point-min))
10708 ;; It's hard to distinguish modules; we'll instead search for pins.
10709 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_$]+\\s *(\\s *[a-zA-Z0-9`_$]+\\s *)" nil t)
10710 (verilog-backward-open-paren) ; Inst start
10711 (cond
10712 ((= (preceding-char) ?\#) ; #(...) parameter section, not pin. Skip.
10713 (forward-char 1)
10714 (verilog-forward-close-paren)) ; Parameters done
10716 (forward-char 1)
10717 (let ((indent-pt (+ (current-column)))
10718 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
10719 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
10720 (goto-char end-pt)) ; Already there, continue search with next instance
10722 ;; Delete identical interconnect
10723 (let ((case-fold-search nil)) ; So we don't convert upper-to-lower, etc
10724 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
10725 (delete-region (match-beginning 0) (match-end 0))
10726 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ; Keep it correct
10727 (while (or (looking-at "[ \t\n\f,]+")
10728 (looking-at "//[^\n]*"))
10729 (delete-region (match-beginning 0) (match-end 0))
10730 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
10731 (verilog-forward-close-paren)
10732 (backward-char 1)
10733 ;; Not verilog-re-search, as we don't want to strip comments
10734 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
10735 (delete-region (match-beginning 0) (match-end 0)))
10736 (verilog-insert "\n")
10737 (verilog-insert-indent "/*AUTOINST*/")))))))))
10740 ;; Auto diff:
10743 (defun verilog-diff-buffers-p (b1 b2 &optional whitespace regexp)
10744 "Return nil if buffers B1 and B2 have same contents.
10745 Else, return point in B1 that first mismatches.
10746 If optional WHITESPACE true, ignore whitespace.
10747 If optional REGEXP, ignore differences matching it."
10748 (save-excursion
10749 (let* ((case-fold-search nil) ; compare-buffer-substrings cares
10750 (p1 (with-current-buffer b1 (goto-char (point-min))))
10751 (p2 (with-current-buffer b2 (goto-char (point-min))))
10752 (maxp1 (with-current-buffer b1 (point-max)))
10753 (maxp2 (with-current-buffer b2 (point-max)))
10754 (op1 -1) (op2 -1)
10755 progress size)
10756 (while (not (and (eq p1 op1) (eq p2 op2)))
10757 ;; If both windows have whitespace optionally skip over it.
10758 (when whitespace
10759 ;; skip-syntax-* doesn't count \n
10760 (with-current-buffer b1
10761 (goto-char p1)
10762 (skip-chars-forward " \t\n\r\f\v")
10763 (setq p1 (point)))
10764 (with-current-buffer b2
10765 (goto-char p2)
10766 (skip-chars-forward " \t\n\r\f\v")
10767 (setq p2 (point))))
10768 (when regexp
10769 (with-current-buffer b1
10770 (goto-char p1)
10771 (when (looking-at regexp)
10772 (setq p1 (match-end 0))))
10773 (with-current-buffer b2
10774 (goto-char p2)
10775 (when (looking-at regexp)
10776 (setq p2 (match-end 0)))))
10777 (setq size (min (- maxp1 p1) (- maxp2 p2)))
10778 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
10779 b1 p1 (+ size p1)))
10780 (setq progress (if (zerop progress) size (1- (abs progress))))
10781 (setq op1 p1 op2 p2
10782 p1 (+ p1 progress)
10783 p2 (+ p2 progress)))
10784 ;; Return value
10785 (if (and (eq p1 maxp1) (eq p2 maxp2))
10786 nil p1))))
10788 (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
10789 "View the differences between file F1 and buffer B2.
10790 This requires the external program `diff-command' to be in your `exec-path',
10791 and uses `diff-switches' in which you may want to have \"-u\" flag.
10792 Ignores WHITESPACE if t, and writes output to stdout if SHOW."
10793 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
10794 ;; call `diff' as `diff' has different calling semantics on different
10795 ;; versions of Emacs.
10796 (if (not (file-exists-p f1))
10797 (message "Buffer `%s' has no associated file on disk" (buffer-name b2))
10798 (with-temp-buffer "*Verilog-Diff*"
10799 (let ((outbuf (current-buffer))
10800 (f2 (make-temp-file "vm-diff-auto-")))
10801 (unwind-protect
10802 (progn
10803 (with-current-buffer b2
10804 (save-restriction
10805 (widen)
10806 (write-region (point-min) (point-max) f2 nil 'nomessage)))
10807 (call-process diff-command nil outbuf t
10808 diff-switches ; User may want -u in diff-switches
10809 (if whitespace "-b" "")
10810 f1 f2)
10811 ;; Print out results. Alternatively we could have call-processed
10812 ;; ourself, but this way we can reuse diff switches
10813 (when show
10814 (with-current-buffer outbuf (message "%s" (buffer-string))))))
10815 (sit-for 0)
10816 (when (file-exists-p f2)
10817 (delete-file f2))))))
10819 (defun verilog-diff-report (b1 b2 diffpt)
10820 "Report differences detected with `verilog-diff-auto'.
10821 Differences are between buffers B1 and B2, starting at point
10822 DIFFPT. This function is called via `verilog-diff-function'."
10823 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
10824 (verilog-warn "%s:%d: Difference in AUTO expansion found"
10825 name1 (with-current-buffer b1
10826 (count-lines (point-min) diffpt)))
10827 (cond (noninteractive
10828 (verilog-diff-file-with-buffer name1 b2 t t))
10830 (ediff-buffers b1 b2)))))
10832 (defun verilog-diff-auto ()
10833 "Expand AUTOs in a temporary buffer and indicate any change.
10834 Whitespace is ignored when detecting differences, but once a
10835 difference is detected, whitespace differences may be shown.
10837 To call this from the command line, see \\[verilog-batch-diff-auto].
10839 The action on differences is selected with
10840 `verilog-diff-function'. The default is `verilog-diff-report'
10841 which will report an error and run `ediff' in interactive mode,
10842 or `diff' in batch mode."
10843 (interactive)
10844 (let ((b1 (current-buffer)) b2 diffpt
10845 (name1 (buffer-file-name))
10846 (newname "*Verilog-Diff*"))
10847 (save-excursion
10848 (when (get-buffer newname)
10849 (kill-buffer newname))
10850 (setq b2 (let (buffer-file-name) ; Else clone is upset
10851 (clone-buffer newname)))
10852 (with-current-buffer b2
10853 ;; auto requires the filename, but can't have same filename in two
10854 ;; buffers; so override both b1 and b2's names
10855 (let ((buffer-file-name name1))
10856 (unwind-protect
10857 (progn
10858 (with-current-buffer b1 (setq buffer-file-name nil))
10859 (verilog-auto)
10860 (when (not verilog-auto-star-save)
10861 (verilog-delete-auto-star-implicit)))
10862 ;; Restore name if unwind
10863 (with-current-buffer b1 (setq buffer-file-name name1)))))
10865 (setq diffpt (verilog-diff-buffers-p b1 b2 t verilog-diff-ignore-regexp))
10866 (cond ((not diffpt)
10867 (unless noninteractive (message "AUTO expansion identical"))
10868 (kill-buffer newname)) ; Nice to cleanup after oneself
10870 (funcall verilog-diff-function b1 b2 diffpt)))
10871 ;; Return result of compare
10872 diffpt)))
10875 ;; Auto save
10878 (defun verilog-auto-save-check ()
10879 "On saving see if we need auto update."
10880 (cond ((not verilog-auto-save-policy)) ; disabled
10881 ((not (save-excursion
10882 (save-match-data
10883 (let ((case-fold-search nil))
10884 (goto-char (point-min))
10885 (re-search-forward "AUTO" nil t))))))
10886 ((eq verilog-auto-save-policy 'force)
10887 (verilog-auto))
10888 ((not (buffer-modified-p)))
10889 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
10890 ((eq verilog-auto-save-policy 'detect)
10891 (verilog-auto))
10893 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10894 (verilog-auto))
10895 ;; Don't ask again if didn't update
10896 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
10897 (when (not verilog-auto-star-save)
10898 (verilog-delete-auto-star-implicit))
10899 nil) ; Always return nil -- we don't write the file ourselves
10901 (defun verilog-auto-read-locals ()
10902 "Return file local variable segment at bottom of file."
10903 (save-excursion
10904 (goto-char (point-max))
10905 (if (re-search-backward "Local Variables:" nil t)
10906 (buffer-substring-no-properties (point) (point-max))
10907 "")))
10909 (defun verilog-auto-reeval-locals (&optional force)
10910 "Read file local variable segment at bottom of file if it has changed.
10911 If FORCE, always reread it."
10912 (let ((curlocal (verilog-auto-read-locals)))
10913 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
10914 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
10915 ;; Note this may cause this function to be recursively invoked,
10916 ;; because hack-local-variables may call (verilog-mode)
10917 ;; The above when statement will prevent it from recursing forever.
10918 (hack-local-variables)
10919 t)))
10921 ;;; Auto creation:
10924 (defun verilog-auto-arg-ports (sigs message indent-pt)
10925 "Print a list of ports for AUTOARG.
10926 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10927 (when sigs
10928 (when verilog-auto-arg-sort
10929 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10930 (insert "\n")
10931 (indent-to indent-pt)
10932 (insert message)
10933 (insert "\n")
10934 (let ((space ""))
10935 (indent-to indent-pt)
10936 (while sigs
10937 (cond ((equal verilog-auto-arg-format 'single)
10938 (insert space)
10939 (indent-to indent-pt)
10940 (setq space "\n"))
10941 ;; verilog-auto-arg-format 'packed
10942 ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10943 (insert "\n")
10944 (indent-to indent-pt)
10945 (setq space " "))
10947 (insert space)
10948 (setq space " ")))
10949 (insert (verilog-sig-name (car sigs)) ",")
10950 (setq sigs (cdr sigs))))))
10952 (defun verilog-auto-arg ()
10953 "Expand AUTOARG statements.
10954 Replace the argument declarations at the beginning of the
10955 module with ones automatically derived from input and output
10956 statements. This can be dangerous if the module is instantiated
10957 using position-based connections, so use only name-based when
10958 instantiating the resulting module. Long lines are split based
10959 on the `fill-column', see \\[set-fill-column].
10961 Limitations:
10962 Concatenation and outputting partial buses is not supported.
10964 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10966 For example:
10968 module ExampArg (/*AUTOARG*/);
10969 input i;
10970 output o;
10971 endmodule
10973 Typing \\[verilog-auto] will make this into:
10975 module ExampArg (/*AUTOARG*/
10976 // Outputs
10978 // Inputs
10981 input i;
10982 output o;
10983 endmodule
10985 The argument declarations may be printed in declaration order to
10986 best suit order based instantiations, or alphabetically, based on
10987 the `verilog-auto-arg-sort' variable.
10989 Formatting is controlled with `verilog-auto-arg-format' variable.
10991 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10992 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
10993 conservative guess on adding a comma for the first signal, if you have
10994 any ifdefs or complicated expressions before the AUTOARG you will need
10995 to choose the comma yourself.
10997 Avoid declaring ports manually, as it makes code harder to maintain."
10998 (save-excursion
10999 (let* ((modi (verilog-modi-current))
11000 (moddecls (verilog-modi-get-decls modi))
11001 (skip-pins (aref (verilog-read-arg-pins) 0)))
11002 (verilog-repair-open-comma)
11003 (verilog-auto-arg-ports (verilog-signals-not-in
11004 (verilog-decls-get-outputs moddecls)
11005 skip-pins)
11006 "// Outputs"
11007 verilog-indent-level-declaration)
11008 (verilog-auto-arg-ports (verilog-signals-not-in
11009 (verilog-decls-get-inouts moddecls)
11010 skip-pins)
11011 "// Inouts"
11012 verilog-indent-level-declaration)
11013 (verilog-auto-arg-ports (verilog-signals-not-in
11014 (verilog-decls-get-inputs moddecls)
11015 skip-pins)
11016 "// Inputs"
11017 verilog-indent-level-declaration)
11018 (verilog-repair-close-comma)
11019 (unless (eq (char-before) ?/ )
11020 (insert "\n"))
11021 (indent-to verilog-indent-level-declaration))))
11023 (defun verilog-auto-assign-modport ()
11024 "Expand AUTOASSIGNMODPORT statements, as part of \\[verilog-auto].
11025 Take input/output/inout statements from the specified interface
11026 and modport and use to build assignments into the modport, for
11027 making verification modules that connect to UVM interfaces.
11029 The first parameter is the name of an interface.
11031 The second parameter is a regexp of modports to read from in
11032 that interface.
11034 The third parameter is the instance name to use to dot reference into.
11036 The optional fourth parameter is a regular expression, and only
11037 signals matching the regular expression will be included.
11039 Limitations:
11041 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
11043 Inouts are not supported, as assignments must be unidirectional.
11045 If a signal is part of the interface header and in both a
11046 modport and the interface itself, it will not be listed. (As
11047 this would result in a syntax error when the connections are
11048 made.)
11050 See the example in `verilog-auto-inout-modport'."
11051 (save-excursion
11052 (let* ((params (verilog-read-auto-params 3 4))
11053 (submod (nth 0 params))
11054 (modport-re (nth 1 params))
11055 (inst-name (nth 2 params))
11056 (regexp (nth 3 params))
11057 direction-re submodi) ; direction argument not supported until requested
11058 ;; Lookup position, etc of co-module
11059 ;; Note this may raise an error
11060 (when (setq submodi (verilog-modi-lookup submod t))
11061 (let* ((indent-pt (current-indentation))
11062 (submoddecls (verilog-modi-get-decls submodi))
11063 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
11064 (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
11065 (verilog-decls-get-vars submoddecls)
11066 (verilog-signals-not-in
11067 (verilog-decls-get-inputs submodportdecls)
11068 (verilog-decls-get-ports submoddecls))))
11069 (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
11070 (verilog-decls-get-vars submoddecls)
11071 (verilog-signals-not-in
11072 (verilog-decls-get-outputs submodportdecls)
11073 (verilog-decls-get-ports submoddecls)))))
11074 (forward-line 1)
11075 (setq sig-list-i (verilog-signals-edit-wire-reg
11076 (verilog-signals-matching-dir-re
11077 (verilog-signals-matching-regexp sig-list-i regexp)
11078 "input" direction-re))
11079 sig-list-o (verilog-signals-edit-wire-reg
11080 (verilog-signals-matching-dir-re
11081 (verilog-signals-matching-regexp sig-list-o regexp)
11082 "output" direction-re)))
11083 (setq sig-list-i (sort (copy-alist sig-list-i) `verilog-signals-sort-compare))
11084 (setq sig-list-o (sort (copy-alist sig-list-o) `verilog-signals-sort-compare))
11085 (when (or sig-list-i sig-list-o)
11086 (verilog-insert-indent "// Beginning of automatic assignments from modport\n")
11087 ;; Don't sort them so an upper AUTOINST will match the main module
11088 (let ((sigs sig-list-o))
11089 (while sigs
11090 (verilog-insert-indent "assign " (verilog-sig-name (car sigs))
11091 " = " inst-name
11092 "." (verilog-sig-name (car sigs)) ";\n")
11093 (setq sigs (cdr sigs))))
11094 (let ((sigs sig-list-i))
11095 (while sigs
11096 (verilog-insert-indent "assign " inst-name
11097 "." (verilog-sig-name (car sigs))
11098 " = " (verilog-sig-name (car sigs)) ";\n")
11099 (setq sigs (cdr sigs))))
11100 (verilog-insert-indent "// End of automatics\n")))))))
11102 (defun verilog-auto-inst-port-map (_port-st)
11103 nil)
11105 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
11106 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
11107 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
11108 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
11109 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
11110 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
11111 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
11112 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
11114 (defun verilog-auto-inst-port (port-st indent-pt moddecls tpl-list tpl-num for-star par-values)
11115 "Print out an instantiation connection for this PORT-ST.
11116 Insert to INDENT-PT, use template TPL-LIST.
11117 @ are instantiation numbers, replaced with TPL-NUM.
11118 @\"(expression @)\" are evaluated, with @ as a variable.
11119 If FOR-STAR add comment it is a .* expansion.
11120 If PAR-VALUES replace final strings with these parameter values."
11121 (let* ((port (verilog-sig-name port-st))
11122 (tpl-ass (or (assoc port (car tpl-list))
11123 (verilog-auto-inst-port-map port-st)))
11124 ;; vl-* are documented for user use
11125 (vl-name (verilog-sig-name port-st))
11126 (vl-width (verilog-sig-width port-st))
11127 (vl-modport (verilog-sig-modport port-st))
11128 (vl-memory (verilog-sig-memory port-st))
11129 (vl-mbits (if (verilog-sig-multidim port-st)
11130 (verilog-sig-multidim-string port-st) ""))
11131 (vl-bits (if (or verilog-auto-inst-vector
11132 (not (assoc port (verilog-decls-get-signals moddecls)))
11133 (not (equal (verilog-sig-bits port-st)
11134 (verilog-sig-bits
11135 (assoc port (verilog-decls-get-signals moddecls))))))
11136 (or (verilog-sig-bits port-st) "")
11137 ""))
11138 (case-fold-search nil)
11139 (check-values par-values)
11140 tpl-net dflt-bits)
11141 ;; Replace parameters in bit-width
11142 (when (and check-values
11143 (not (equal vl-bits "")))
11144 (while check-values
11145 (setq vl-bits (verilog-string-replace-matches
11146 (concat "\\<" (nth 0 (car check-values)) "\\>")
11147 (concat "(" (nth 1 (car check-values)) ")")
11148 t t vl-bits)
11149 vl-mbits (verilog-string-replace-matches
11150 (concat "\\<" (nth 0 (car check-values)) "\\>")
11151 (concat "(" (nth 1 (car check-values)) ")")
11152 t t vl-mbits)
11153 vl-memory (when vl-memory
11154 (verilog-string-replace-matches
11155 (concat "\\<" (nth 0 (car check-values)) "\\>")
11156 (concat "(" (nth 1 (car check-values)) ")")
11157 t t vl-memory))
11158 check-values (cdr check-values)))
11159 (setq vl-bits (verilog-simplify-range-expression vl-bits)
11160 vl-mbits (verilog-simplify-range-expression vl-mbits)
11161 vl-memory (when vl-memory (verilog-simplify-range-expression vl-memory))
11162 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
11163 ;; Default net value if not found
11164 (setq dflt-bits (if (or (and (verilog-sig-bits port-st)
11165 (verilog-sig-multidim port-st))
11166 (verilog-sig-memory port-st))
11167 (concat "/*" vl-mbits vl-bits
11168 ;; .[ used to separate packed from unpacked
11169 (if vl-memory "." "")
11170 (if vl-memory vl-memory "")
11171 "*/")
11172 (concat vl-bits))
11173 tpl-net (concat port
11174 (if (and vl-modport
11175 ;; .modport cannot be added if attachment is
11176 ;; already declared as modport, VCS croaks
11177 (let ((sig (assoc port (verilog-decls-get-interfaces moddecls))))
11178 (not (and sig (verilog-sig-modport sig)))))
11179 (concat "." vl-modport) "")
11180 dflt-bits))
11181 ;; Find template
11182 (cond (tpl-ass ; Template of exact port name
11183 (setq tpl-net (nth 1 tpl-ass)))
11184 ((nth 1 tpl-list) ; Wildcards in template, search them
11185 (let ((wildcards (nth 1 tpl-list)))
11186 (while wildcards
11187 (when (string-match (nth 0 (car wildcards)) port)
11188 (setq tpl-ass (car wildcards) ; so allow @ parsing
11189 tpl-net (replace-match (nth 1 (car wildcards))
11190 t nil port)))
11191 (setq wildcards (cdr wildcards))))))
11192 ;; Parse Templated variable
11193 (when tpl-ass
11194 ;; Evaluate @"(lispcode)"
11195 (when (string-match "@\".*[^\\]\"" tpl-net)
11196 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
11197 (setq tpl-net
11198 (concat
11199 (substring tpl-net 0 (match-beginning 0))
11200 (save-match-data
11201 (let* ((expr (match-string 1 tpl-net))
11202 (value
11203 (progn
11204 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
11205 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
11206 (prin1 (eval (car (read-from-string expr)))
11207 (lambda (_ch) ())))))
11208 (if (numberp value) (setq value (number-to-string value)))
11209 value))
11210 (substring tpl-net (match-end 0))))))
11211 ;; Replace @ and [] magic variables in final output
11212 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
11213 (setq tpl-net (verilog-string-replace-matches "\\[\\]\\[\\]" dflt-bits nil nil tpl-net))
11214 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
11215 ;; Insert it
11216 (indent-to indent-pt)
11217 (insert "." port)
11218 (unless (and verilog-auto-inst-dot-name
11219 (equal port tpl-net))
11220 (indent-to verilog-auto-inst-column)
11221 (insert "(" tpl-net ")"))
11222 (insert ",")
11223 (cond (tpl-ass
11224 (verilog-read-auto-template-hit tpl-ass)
11225 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
11226 verilog-auto-inst-column))
11227 ;; verilog-insert requires the complete comment in one call - including the newline
11228 (cond ((equal verilog-auto-inst-template-numbers `lhs)
11229 (verilog-insert " // Templated"
11230 " LHS: " (nth 0 tpl-ass)
11231 "\n"))
11232 (verilog-auto-inst-template-numbers
11233 (verilog-insert " // Templated"
11234 " T" (int-to-string (nth 2 tpl-ass))
11235 " L" (int-to-string (nth 3 tpl-ass))
11236 "\n"))
11238 (verilog-insert " // Templated\n"))))
11239 (for-star
11240 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
11241 verilog-auto-inst-column))
11242 (verilog-insert " // Implicit .*\n"))
11244 (insert "\n")))))
11245 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
11246 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
11247 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
11249 (defun verilog-auto-inst-port-list (sig-list indent-pt moddecls tpl-list tpl-num for-star par-values)
11250 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
11251 (when verilog-auto-inst-sort
11252 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
11253 (mapc (lambda (port)
11254 (verilog-auto-inst-port port indent-pt moddecls
11255 tpl-list tpl-num for-star par-values))
11256 sig-list))
11258 (defun verilog-auto-inst-first ()
11259 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
11260 ;; Do we need a trailing comma?
11261 ;; There maybe an ifdef or something similar before us. What a mess. Thus
11262 ;; to avoid trouble we only insert on preceding ) or *.
11263 ;; Insert first port on new line
11264 (insert "\n") ; Must insert before search, so point will move forward if insert comma
11265 (save-excursion
11266 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
11267 (when (looking-at ")\\|\\*") ; Generally don't insert, unless we are fairly sure
11268 (forward-char 1)
11269 (insert ","))))
11271 (defun verilog-auto-star ()
11272 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
11274 If `verilog-auto-star-expand' is set, .* pins are treated if they were
11275 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
11276 will also ignore any .* that are not last in your pin list (this prevents
11277 it from deleting pins following the .* when it expands the AUTOINST.)
11279 On writing your file, unless `verilog-auto-star-save' is set, any
11280 non-templated expanded pins will be removed. You may do this at any time
11281 with \\[verilog-delete-auto-star-implicit].
11283 If you are converting a module to use .* for the first time, you may wish
11284 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
11286 See `verilog-auto-inst' for examples, templates, and more information."
11287 (when (verilog-auto-star-safe)
11288 (verilog-auto-inst)))
11290 (defun verilog-auto-inst ()
11291 "Expand AUTOINST statements, as part of \\[verilog-auto].
11292 Replace the pin connections to an instantiation or interface
11293 declaration with ones automatically derived from the module or
11294 interface header of the instantiated item.
11296 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
11297 and delete them before saving unless `verilog-auto-star-save' is set.
11298 See `verilog-auto-star' for more information.
11300 The pins are printed in declaration order or alphabetically,
11301 based on the `verilog-auto-inst-sort' variable.
11303 Limitations:
11304 Module names must be resolvable to filenames by adding a
11305 `verilog-library-extensions', and being found in the same directory, or
11306 by changing the variable `verilog-library-flags' or
11307 `verilog-library-directories'. Macros `modname are translated through the
11308 vh-{name} Emacs variable, if that is not found, it just ignores the \\=`.
11310 In templates you must have one signal per line, ending in a ), or ));,
11311 and have proper () nesting, including a final ); to end the template.
11313 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11315 SystemVerilog multidimensional input/output has only experimental support.
11317 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
11319 Parameters referenced by the instantiation will remain symbolic, unless
11320 `verilog-auto-inst-param-value' is set.
11322 Gate primitives (and/or) may have AUTOINST for the purpose of
11323 AUTOWIRE declarations, etc. Gates are the only case when
11324 position based connections are passed.
11326 The array part of arrayed instances are ignored; this may
11327 result in undesirable default AUTOINST connections; use a
11328 template instead.
11330 For example, first take the submodule InstModule.v:
11332 module InstModule (o,i);
11333 output [31:0] o;
11334 input i;
11335 wire [31:0] o = {32{i}};
11336 endmodule
11338 This is then used in an upper level module:
11340 module ExampInst (o,i);
11341 output o;
11342 input i;
11343 InstModule instName
11344 (/*AUTOINST*/);
11345 endmodule
11347 Typing \\[verilog-auto] will make this into:
11349 module ExampInst (o,i);
11350 output o;
11351 input i;
11352 InstModule instName
11353 (/*AUTOINST*/
11354 // Outputs
11355 .ov (ov[31:0]),
11356 // Inputs
11357 .i (i));
11358 endmodule
11360 Where the list of inputs and outputs came from the inst module.
11362 Exceptions:
11364 Unless you are instantiating a module multiple times, or the module is
11365 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
11366 It just makes for unmaintainable code. To sanitize signal names, try
11367 vrename from URL `http://www.veripool.org'.
11369 When you need to violate this suggestion there are two ways to list
11370 exceptions, placing them before the AUTOINST, or using templates.
11372 Any ports defined before the /*AUTOINST*/ are not included in the list of
11373 automatics. This is similar to making a template as described below, but
11374 is restricted to simple connections just like you normally make. Also note
11375 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
11376 you have the appropriate // Input or // Output comment, and exactly the
11377 same line formatting as AUTOINST itself uses.
11379 InstModule instName
11380 (// Inputs
11381 .i (my_i_dont_mess_with_it),
11382 /*AUTOINST*/
11383 // Outputs
11384 .ov (ov[31:0]));
11387 Templates:
11389 For multiple instantiations based upon a single template, create a
11390 commented out template:
11392 /* InstModule AUTO_TEMPLATE (
11393 .sig3 (sigz[]),
11397 Templates go ABOVE the instantiation(s). When an instantiation is
11398 expanded `verilog-mode' simply searches up for the closest template.
11399 Thus you can have multiple templates for the same module, just alternate
11400 between the template for an instantiation and the instantiation itself.
11401 (For backward compatibility if no template is found above, it
11402 will also look below, but do not use this behavior in new designs.)
11404 The module name must be the same as the name of the module in the
11405 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
11406 words and capitalized. Only signals that must be different for each
11407 instantiation need to be listed.
11409 Inside a template, a [] in a connection name (with nothing else
11410 inside the brackets) will be replaced by the same bus subscript
11411 as it is being connected to, or the [] will be removed if it is
11412 a single bit signal.
11414 Inside a template, a [][] in a connection name will behave
11415 similarly to a [] for scalar or single-dimensional connection;
11416 for a multidimensional connection it will print a comment
11417 similar to that printed when a template is not used. Generally
11418 it is a good idea to do this for all connections in a template,
11419 as then they will work for any width signal, and with AUTOWIRE.
11420 See PTL_BUS becoming PTL_BUSNEW below.
11422 Inside a template, a [] in a connection name (with nothing else inside
11423 the brackets) will be replaced by the same bus subscript as it is being
11424 connected to, or the [] will be removed if it is a single bit signal.
11425 Generally it is a good idea to do this for all connections in a template,
11426 as then they will work for any width signal, and with AUTOWIRE. See
11427 PTL_BUS becoming PTL_BUSNEW below.
11429 If you have a complicated template, set `verilog-auto-inst-template-numbers'
11430 to see which regexps are matching. Don't leave that mode set after
11431 debugging is completed though, it will result in lots of extra differences
11432 and merge conflicts.
11434 Setting `verilog-auto-template-warn-unused' will report errors
11435 if any template lines are unused.
11437 For example:
11439 /* InstModule AUTO_TEMPLATE (
11440 .ptl_bus (ptl_busnew[]),
11443 InstModule ms2m (/*AUTOINST*/);
11445 Typing \\[verilog-auto] will make this into:
11447 InstModule ms2m (/*AUTOINST*/
11448 // Outputs
11449 .NotInTemplate (NotInTemplate),
11450 .ptl_bus (ptl_busnew[3:0]), // Templated
11451 ....
11454 Multiple Module Templates:
11456 The same template lines can be applied to multiple modules with
11457 the syntax as follows:
11459 /* InstModuleA AUTO_TEMPLATE
11460 InstModuleB AUTO_TEMPLATE
11461 InstModuleC AUTO_TEMPLATE
11462 InstModuleD AUTO_TEMPLATE (
11463 .ptl_bus (ptl_busnew[]),
11467 Note there is only one AUTO_TEMPLATE opening parenthesis.
11469 @ Templates:
11471 It is common to instantiate a cell multiple times, so templates make it
11472 trivial to substitute part of the cell name into the connection name.
11474 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
11475 .sig1 (sigx[@]),
11476 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
11480 If no regular expression is provided immediately after the AUTO_TEMPLATE
11481 keyword, then the @ character in any connection names will be replaced
11482 with the instantiation number; the first digits found in the cell's
11483 instantiation name.
11485 If a regular expression is provided, the @ character will be replaced
11486 with the first () grouping that matches against the cell name. Using a
11487 regexp of `\\([0-9]+\\)' provides identical values for @ as when no
11488 regexp is provided. If you use multiple layers of parenthesis,
11489 `test\\([^0-9]+\\)_\\([0-9]+\\)' would replace @ with non-number
11490 characters after test and before _, whereas
11491 `\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)' would replace @ with the entire
11492 match.
11494 For example:
11496 /* InstModule AUTO_TEMPLATE (
11497 .ptl_mapvalidx (ptl_mapvalid[@]),
11498 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
11501 InstModule ms2m (/*AUTOINST*/);
11503 Typing \\[verilog-auto] will make this into:
11505 InstModule ms2m (/*AUTOINST*/
11506 // Outputs
11507 .ptl_mapvalidx (ptl_mapvalid[2]),
11508 .ptl_mapvalidp1x (ptl_mapvalid[3]));
11510 Note the @ character was replaced with the 2 from \"ms2m\".
11512 Alternatively, using a regular expression for @:
11514 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
11515 .ptl_mapvalidx (@_ptl_mapvalid),
11516 .ptl_mapvalidp1x (ptl_mapvalid_@),
11519 InstModule ms2_FOO (/*AUTOINST*/);
11520 InstModule ms2_BAR (/*AUTOINST*/);
11522 Typing \\[verilog-auto] will make this into:
11524 InstModule ms2_FOO (/*AUTOINST*/
11525 // Outputs
11526 .ptl_mapvalidx (FOO_ptl_mapvalid),
11527 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
11528 InstModule ms2_BAR (/*AUTOINST*/
11529 // Outputs
11530 .ptl_mapvalidx (BAR_ptl_mapvalid),
11531 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
11534 Regexp Templates:
11536 A template entry of the form
11538 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
11540 will apply an Emacs style regular expression search for any port beginning
11541 in pci_req followed by numbers and ending in _l and connecting that to
11542 the pci_req_jtag_[] net, with the bus subscript coming from what matches
11543 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
11545 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
11546 does the same thing. (Note a @ in the connection/replacement text is
11547 completely different -- still use \\1 there!) Thus this is the same as
11548 the above template:
11550 .pci_req@_l (pci_req_jtag_[\\1]),
11552 Here's another example to remove the _l, useful when naming conventions
11553 specify _ alone to mean active low. Note the use of [] to keep the bus
11554 subscript:
11556 .\\(.*\\)_l (\\1_[]),
11558 Lisp Templates:
11560 First any regular expression template is expanded.
11562 If the syntax @\"( ... )\" is found in a connection, the expression in
11563 quotes will be evaluated as a Lisp expression, with @ replaced by the
11564 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
11565 4 into the brackets. Quote all double-quotes inside the expression with
11566 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
11567 regexp template backslash the backslash quote (\\\\\"...\\\\\").
11569 There are special variables defined that are useful in these
11570 Lisp functions:
11572 vl-name Name portion of the input/output port.
11573 vl-bits Bus bits portion of the input/output port (`[2:0]').
11574 vl-mbits Multidimensional array bits for port (`[2:0][3:0]').
11575 vl-width Width of the input/output port (`3' for [2:0]).
11576 May be a (...) expression if bits isn't a constant.
11577 vl-dir Direction of the pin input/output/inout/interface.
11578 vl-modport The modport, if an interface with a modport.
11579 vl-cell-type Module name/type of the cell (`InstModule').
11580 vl-cell-name Instance name of the cell (`instName').
11582 Normal Lisp variables may be used in expressions. See
11583 `verilog-read-defines' which can set vh-{definename} variables for use
11584 here. Also, any comments of the form:
11586 /*AUTO_LISP(setq foo 1)*/
11588 will evaluate any Lisp expression inside the parenthesis between the
11589 beginning of the buffer and the point of the AUTOINST. This allows
11590 functions to be defined or variables to be changed between instantiations.
11591 (See also `verilog-auto-insert-lisp' if you want the output from your
11592 lisp function to be inserted.)
11594 Note that when using lisp expressions errors may occur when @ is not a
11595 number; you may need to use the standard Emacs Lisp functions
11596 `number-to-string' and `string-to-number'.
11598 After the evaluation is completed, @ substitution and [] substitution
11599 occur.
11601 For more information see the \\[verilog-faq] and forums at URL
11602 `http://www.veripool.org'."
11603 (save-excursion
11604 ;; Find beginning
11605 (let* ((pt (point))
11606 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
11607 (indent-pt (save-excursion (verilog-backward-open-paren)
11608 (1+ (current-column))))
11609 (verilog-auto-inst-column (max verilog-auto-inst-column
11610 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11611 (modi (verilog-modi-current))
11612 (moddecls (verilog-modi-get-decls modi))
11613 submod submodi submoddecls
11614 inst skip-pins tpl-list tpl-num did-first par-values)
11616 ;; Find module name that is instantiated
11617 (setq submod (verilog-read-inst-module)
11618 inst (verilog-read-inst-name)
11619 vl-cell-type submod
11620 vl-cell-name inst
11621 skip-pins (aref (verilog-read-inst-pins) 0))
11623 ;; Parse any AUTO_LISP() before here
11624 (verilog-read-auto-lisp (point-min) pt)
11626 ;; Read parameters (after AUTO_LISP)
11627 (setq par-values (and verilog-auto-inst-param-value
11628 (verilog-read-inst-param-value)))
11630 ;; Lookup position, etc of submodule
11631 ;; Note this may raise an error
11632 (when (and (not (member submod verilog-gate-keywords))
11633 (setq submodi (verilog-modi-lookup submod t)))
11634 (setq submoddecls (verilog-modi-get-decls submodi))
11635 ;; If there's a number in the instantiation, it may be an argument to the
11636 ;; automatic variable instantiation program.
11637 (let* ((tpl-info (verilog-read-auto-template submod))
11638 (tpl-regexp (aref tpl-info 0)))
11639 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11640 (match-string 1 inst)
11642 tpl-list (aref tpl-info 1)))
11643 ;; Find submodule's signals and dump
11644 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
11645 (verilog-signals-not-in
11646 (verilog-decls-get-vars submoddecls)
11647 skip-pins)))
11648 (vl-dir "interfaced"))
11649 (when (and sig-list
11650 verilog-auto-inst-interfaced-ports)
11651 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11652 ;; Note these are searched for in verilog-read-sub-decls.
11653 (verilog-insert-indent "// Interfaced\n")
11654 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11655 tpl-list tpl-num for-star par-values)))
11656 (let ((sig-list (verilog-signals-not-in
11657 (verilog-decls-get-interfaces submoddecls)
11658 skip-pins))
11659 (vl-dir "interface"))
11660 (when sig-list
11661 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11662 ;; Note these are searched for in verilog-read-sub-decls.
11663 (verilog-insert-indent "// Interfaces\n")
11664 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11665 tpl-list tpl-num for-star par-values)))
11666 (let ((sig-list (verilog-signals-not-in
11667 (verilog-decls-get-outputs submoddecls)
11668 skip-pins))
11669 (vl-dir "output"))
11670 (when sig-list
11671 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11672 (verilog-insert-indent "// Outputs\n")
11673 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11674 tpl-list tpl-num for-star par-values)))
11675 (let ((sig-list (verilog-signals-not-in
11676 (verilog-decls-get-inouts submoddecls)
11677 skip-pins))
11678 (vl-dir "inout"))
11679 (when sig-list
11680 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11681 (verilog-insert-indent "// Inouts\n")
11682 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11683 tpl-list tpl-num for-star par-values)))
11684 (let ((sig-list (verilog-signals-not-in
11685 (verilog-decls-get-inputs submoddecls)
11686 skip-pins))
11687 (vl-dir "input"))
11688 (when sig-list
11689 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11690 (verilog-insert-indent "// Inputs\n")
11691 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11692 tpl-list tpl-num for-star par-values)))
11693 ;; Kill extra semi
11694 (save-excursion
11695 (cond (did-first
11696 (re-search-backward "," pt t)
11697 (delete-char 1)
11698 (insert ");")
11699 (search-forward "\n") ; Added by inst-port
11700 (delete-char -1)
11701 (if (search-forward ")" nil t) ; From user, moved up a line
11702 (delete-char -1))
11703 (if (search-forward ";" nil t) ; Don't error if user had syntax error and forgot it
11704 (delete-char -1)))))))))
11706 (defun verilog-auto-inst-param ()
11707 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
11708 Replace the parameter connections to an instantiation with ones
11709 automatically derived from the module header of the instantiated netlist.
11711 See \\[verilog-auto-inst] for limitations, and templates to customize the
11712 output.
11714 For example, first take the submodule InstModule.v:
11716 module InstModule (o,i);
11717 parameter PAR;
11718 endmodule
11720 This is then used in an upper level module:
11722 module ExampInst (o,i);
11723 parameter PAR;
11724 InstModule #(/*AUTOINSTPARAM*/)
11725 instName (/*AUTOINST*/);
11726 endmodule
11728 Typing \\[verilog-auto] will make this into:
11730 module ExampInst (o,i);
11731 output o;
11732 input i;
11733 InstModule #(/*AUTOINSTPARAM*/
11734 // Parameters
11735 .PAR (PAR));
11736 instName (/*AUTOINST*/);
11737 endmodule
11739 Where the list of parameter connections come from the inst module.
11741 Templates:
11743 You can customize the parameter connections using AUTO_TEMPLATEs,
11744 just as you would with \\[verilog-auto-inst]."
11745 (save-excursion
11746 ;; Find beginning
11747 (let* ((pt (point))
11748 (indent-pt (save-excursion (verilog-backward-open-paren)
11749 (1+ (current-column))))
11750 (verilog-auto-inst-column (max verilog-auto-inst-column
11751 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11752 (modi (verilog-modi-current))
11753 (moddecls (verilog-modi-get-decls modi))
11754 submod submodi submoddecls
11755 inst skip-pins tpl-list tpl-num did-first)
11756 ;; Find module name that is instantiated
11757 (setq submod (save-excursion
11758 ;; Get to the point where AUTOINST normally is to read the module
11759 (verilog-re-search-forward-quick "[(;]" nil nil)
11760 (verilog-read-inst-module))
11761 inst (save-excursion
11762 ;; Get to the point where AUTOINST normally is to read the module
11763 (verilog-re-search-forward-quick "[(;]" nil nil)
11764 (verilog-read-inst-name))
11765 vl-cell-type submod
11766 vl-cell-name inst
11767 skip-pins (aref (verilog-read-inst-pins) 0))
11769 ;; Parse any AUTO_LISP() before here
11770 (verilog-read-auto-lisp (point-min) pt)
11772 ;; Lookup position, etc of submodule
11773 ;; Note this may raise an error
11774 (when (setq submodi (verilog-modi-lookup submod t))
11775 (setq submoddecls (verilog-modi-get-decls submodi))
11776 ;; If there's a number in the instantiation, it may be an argument to the
11777 ;; automatic variable instantiation program.
11778 (let* ((tpl-info (verilog-read-auto-template submod))
11779 (tpl-regexp (aref tpl-info 0)))
11780 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11781 (match-string 1 inst)
11783 tpl-list (aref tpl-info 1)))
11784 ;; Find submodule's signals and dump
11785 (let ((sig-list (verilog-signals-not-in
11786 (verilog-decls-get-gparams submoddecls)
11787 skip-pins))
11788 (vl-dir "parameter"))
11789 (when sig-list
11790 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11791 ;; Note these are searched for in verilog-read-sub-decls.
11792 (verilog-insert-indent "// Parameters\n")
11793 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11794 tpl-list tpl-num nil nil)))
11795 ;; Kill extra semi
11796 (save-excursion
11797 (cond (did-first
11798 (re-search-backward "," pt t)
11799 (delete-char 1)
11800 (insert ")")
11801 (search-forward "\n") ; Added by inst-port
11802 (delete-char -1)
11803 (if (search-forward ")" nil t) ; From user, moved up a line
11804 (delete-char -1)))))))))
11806 (defun verilog-auto-reg ()
11807 "Expand AUTOREG statements, as part of \\[verilog-auto].
11808 Make reg statements for any output that isn't already declared,
11809 and isn't a wire output from a block. `verilog-auto-wire-type'
11810 may be used to change the datatype of the declarations.
11812 Limitations:
11813 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11815 This does NOT work on memories, declare those yourself.
11817 An example:
11819 module ExampReg (o,i);
11820 output o;
11821 input i;
11822 /*AUTOREG*/
11823 always o = i;
11824 endmodule
11826 Typing \\[verilog-auto] will make this into:
11828 module ExampReg (o,i);
11829 output o;
11830 input i;
11831 /*AUTOREG*/
11832 // Beginning of automatic regs (for this module's undeclared outputs)
11833 reg o;
11834 // End of automatics
11835 always o = i;
11836 endmodule"
11837 (save-excursion
11838 ;; Point must be at insertion point.
11839 (let* ((indent-pt (current-indentation))
11840 (modi (verilog-modi-current))
11841 (moddecls (verilog-modi-get-decls modi))
11842 (modsubdecls (verilog-modi-get-sub-decls modi))
11843 (sig-list (verilog-signals-not-in
11844 (verilog-decls-get-outputs moddecls)
11845 (append (verilog-signals-with ; ignore typed signals
11846 'verilog-sig-type
11847 (verilog-decls-get-outputs moddecls))
11848 (verilog-decls-get-vars moddecls)
11849 (verilog-decls-get-assigns moddecls)
11850 (verilog-decls-get-consts moddecls)
11851 (verilog-decls-get-gparams moddecls)
11852 (verilog-subdecls-get-interfaced modsubdecls)
11853 (verilog-subdecls-get-outputs modsubdecls)
11854 (verilog-subdecls-get-inouts modsubdecls)))))
11855 (when sig-list
11856 (verilog-forward-or-insert-line)
11857 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
11858 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11859 (verilog-insert-indent "// End of automatics\n")))))
11861 (defun verilog-auto-reg-input ()
11862 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
11863 Make reg statements instantiation inputs that aren't already declared.
11864 This is useful for making a top level shell for testing the module that is
11865 to be instantiated.
11867 Limitations:
11868 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
11870 This does NOT work on memories, declare those yourself.
11872 An example (see `verilog-auto-inst' for what else is going on here):
11874 module ExampRegInput (o,i);
11875 output o;
11876 input i;
11877 /*AUTOREGINPUT*/
11878 InstModule instName
11879 (/*AUTOINST*/);
11880 endmodule
11882 Typing \\[verilog-auto] will make this into:
11884 module ExampRegInput (o,i);
11885 output o;
11886 input i;
11887 /*AUTOREGINPUT*/
11888 // Beginning of automatic reg inputs (for undeclared ...
11889 reg [31:0] iv; // From inst of inst.v
11890 // End of automatics
11891 InstModule instName
11892 (/*AUTOINST*/
11893 // Outputs
11894 .o (o[31:0]),
11895 // Inputs
11896 .iv (iv));
11897 endmodule"
11898 (save-excursion
11899 ;; Point must be at insertion point.
11900 (let* ((indent-pt (current-indentation))
11901 (modi (verilog-modi-current))
11902 (moddecls (verilog-modi-get-decls modi))
11903 (modsubdecls (verilog-modi-get-sub-decls modi))
11904 (sig-list (verilog-signals-combine-bus
11905 (verilog-signals-not-in
11906 (append (verilog-subdecls-get-inputs modsubdecls)
11907 (verilog-subdecls-get-inouts modsubdecls))
11908 (append (verilog-decls-get-signals moddecls)
11909 (verilog-decls-get-assigns moddecls))))))
11910 (when sig-list
11911 (verilog-forward-or-insert-line)
11912 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
11913 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11914 (verilog-insert-indent "// End of automatics\n")))))
11916 (defun verilog-auto-logic-setup ()
11917 "Prepare variables due to AUTOLOGIC."
11918 (unless verilog-auto-wire-type
11919 (set (make-local-variable 'verilog-auto-wire-type)
11920 "logic")))
11922 (defun verilog-auto-logic ()
11923 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
11924 Make wire statements using the SystemVerilog logic keyword.
11925 This is currently equivalent to:
11927 /*AUTOWIRE*/
11929 with the below at the bottom of the file
11931 // Local Variables:
11932 // verilog-auto-logic-type:\"logic\"
11933 // End:
11935 In the future AUTOLOGIC may declare additional identifiers,
11936 while AUTOWIRE will not."
11937 (save-excursion
11938 (verilog-auto-logic-setup)
11939 (verilog-auto-wire)))
11941 (defun verilog-auto-wire ()
11942 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
11943 Make wire statements for instantiations outputs that aren't
11944 already declared. `verilog-auto-wire-type' may be used to change
11945 the datatype of the declarations.
11947 Limitations:
11948 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
11949 and all buses must have widths, such as those from AUTOINST, or using []
11950 in AUTO_TEMPLATEs.
11952 This does NOT work on memories or SystemVerilog .name connections,
11953 declare those yourself.
11955 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
11956 determine how to bus together. This occurs when you have ports with
11957 non-numeric or non-sequential bus subscripts. If Verilog mode
11958 mis-guessed, you'll have to declare them yourself.
11960 An example (see `verilog-auto-inst' for what else is going on here):
11962 module ExampWire (o,i);
11963 output o;
11964 input i;
11965 /*AUTOWIRE*/
11966 InstModule instName
11967 (/*AUTOINST*/);
11968 endmodule
11970 Typing \\[verilog-auto] will make this into:
11972 module ExampWire (o,i);
11973 output o;
11974 input i;
11975 /*AUTOWIRE*/
11976 // Beginning of automatic wires
11977 wire [31:0] ov; // From inst of inst.v
11978 // End of automatics
11979 InstModule instName
11980 (/*AUTOINST*/
11981 // Outputs
11982 .ov (ov[31:0]),
11983 // Inputs
11984 .i (i));
11985 wire o = | ov;
11986 endmodule"
11987 (save-excursion
11988 ;; Point must be at insertion point.
11989 (let* ((indent-pt (current-indentation))
11990 (modi (verilog-modi-current))
11991 (moddecls (verilog-modi-get-decls modi))
11992 (modsubdecls (verilog-modi-get-sub-decls modi))
11993 (sig-list (verilog-signals-combine-bus
11994 (verilog-signals-not-in
11995 (append (verilog-subdecls-get-outputs modsubdecls)
11996 (verilog-subdecls-get-inouts modsubdecls))
11997 (verilog-decls-get-signals moddecls)))))
11998 (when sig-list
11999 (verilog-forward-or-insert-line)
12000 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
12001 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
12002 (verilog-insert-indent "// End of automatics\n")
12003 ;; We used to optionally call verilog-pretty-declarations and
12004 ;; verilog-pretty-expr here, but it's too slow on huge modules,
12005 ;; plus makes everyone's module change. Finally those call
12006 ;; syntax-ppss which is broken when change hooks are disabled.
12007 ))))
12009 (defun verilog-auto-output ()
12010 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
12011 Make output statements for any output signal from an /*AUTOINST*/ that
12012 isn't an input to another AUTOINST. This is useful for modules which
12013 only instantiate other modules.
12015 Limitations:
12016 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
12018 If placed inside the parenthesis of a module declaration, it creates
12019 Verilog 2001 style, else uses Verilog 1995 style.
12021 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
12022 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
12024 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
12026 Types are added to declarations if an AUTOLOGIC or
12027 `verilog-auto-wire-type' is set to logic.
12029 Signals matching `verilog-auto-output-ignore-regexp' are not included.
12031 An example (see `verilog-auto-inst' for what else is going on here):
12033 module ExampOutput (ov,i);
12034 input i;
12035 /*AUTOOUTPUT*/
12036 InstModule instName
12037 (/*AUTOINST*/);
12038 endmodule
12040 Typing \\[verilog-auto] will make this into:
12042 module ExampOutput (ov,i);
12043 input i;
12044 /*AUTOOUTPUT*/
12045 // Beginning of automatic outputs (from unused autoinst outputs)
12046 output [31:0] ov; // From inst of inst.v
12047 // End of automatics
12048 InstModule instName
12049 (/*AUTOINST*/
12050 // Outputs
12051 .ov (ov[31:0]),
12052 // Inputs
12053 .i (i));
12054 endmodule
12056 You may also provide an optional regular expression, in which case only
12057 signals matching the regular expression will be included. For example the
12058 same expansion will result from only extracting outputs starting with ov:
12060 /*AUTOOUTPUT(\"^ov\")*/"
12061 (save-excursion
12062 ;; Point must be at insertion point.
12063 (let* ((indent-pt (current-indentation))
12064 (params (verilog-read-auto-params 0 1))
12065 (regexp (nth 0 params))
12066 (v2k (verilog-in-paren-quick))
12067 (modi (verilog-modi-current))
12068 (moddecls (verilog-modi-get-decls modi))
12069 (modsubdecls (verilog-modi-get-sub-decls modi))
12070 (sig-list (verilog-signals-not-in
12071 (verilog-subdecls-get-outputs modsubdecls)
12072 (append (verilog-decls-get-outputs moddecls)
12073 (verilog-decls-get-inouts moddecls)
12074 (verilog-decls-get-inputs moddecls)
12075 (verilog-subdecls-get-inputs modsubdecls)
12076 (verilog-subdecls-get-inouts modsubdecls)))))
12077 (when regexp
12078 (setq sig-list (verilog-signals-matching-regexp
12079 sig-list regexp)))
12080 (setq sig-list (verilog-signals-not-matching-regexp
12081 sig-list verilog-auto-output-ignore-regexp))
12082 (verilog-forward-or-insert-line)
12083 (when v2k (verilog-repair-open-comma))
12084 (when sig-list
12085 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
12086 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
12087 (verilog-insert-indent "// End of automatics\n"))
12088 (when v2k (verilog-repair-close-comma)))))
12090 (defun verilog-auto-output-every ()
12091 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
12092 Make output statements for any signals that aren't primary inputs or
12093 outputs already. This makes every signal in the design an output. This is
12094 useful to get Synopsys to preserve every signal in the design, since it
12095 won't optimize away the outputs.
12097 An example:
12099 module ExampOutputEvery (o,i,tempa,tempb);
12100 output o;
12101 input i;
12102 /*AUTOOUTPUTEVERY*/
12103 wire tempa = i;
12104 wire tempb = tempa;
12105 wire o = tempb;
12106 endmodule
12108 Typing \\[verilog-auto] will make this into:
12110 module ExampOutputEvery (o,i,tempa,tempb);
12111 output o;
12112 input i;
12113 /*AUTOOUTPUTEVERY*/
12114 // Beginning of automatic outputs (every signal)
12115 output tempb;
12116 output tempa;
12117 // End of automatics
12118 wire tempa = i;
12119 wire tempb = tempa;
12120 wire o = tempb;
12121 endmodule
12123 You may also provide an optional regular expression, in which case only
12124 signals matching the regular expression will be included. For example the
12125 same expansion will result from only extracting outputs starting with ov:
12127 /*AUTOOUTPUTEVERY(\"^ov\")*/"
12128 (save-excursion
12129 ;;Point must be at insertion point
12130 (let* ((indent-pt (current-indentation))
12131 (params (verilog-read-auto-params 0 1))
12132 (regexp (nth 0 params))
12133 (v2k (verilog-in-paren-quick))
12134 (modi (verilog-modi-current))
12135 (moddecls (verilog-modi-get-decls modi))
12136 (sig-list (verilog-signals-combine-bus
12137 (verilog-signals-not-in
12138 (verilog-decls-get-signals moddecls)
12139 (verilog-decls-get-ports moddecls)))))
12140 (when regexp
12141 (setq sig-list (verilog-signals-matching-regexp
12142 sig-list regexp)))
12143 (setq sig-list (verilog-signals-not-matching-regexp
12144 sig-list verilog-auto-output-ignore-regexp))
12145 (verilog-forward-or-insert-line)
12146 (when v2k (verilog-repair-open-comma))
12147 (when sig-list
12148 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
12149 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
12150 (verilog-insert-indent "// End of automatics\n"))
12151 (when v2k (verilog-repair-close-comma)))))
12153 (defun verilog-auto-input ()
12154 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
12155 Make input statements for any input signal into an /*AUTOINST*/ that
12156 isn't declared elsewhere inside the module. This is useful for modules which
12157 only instantiate other modules.
12159 Limitations:
12160 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
12162 If placed inside the parenthesis of a module declaration, it creates
12163 Verilog 2001 style, else uses Verilog 1995 style.
12165 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
12166 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
12168 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
12170 Types are added to declarations if an AUTOLOGIC or
12171 `verilog-auto-wire-type' is set to logic.
12173 Signals matching `verilog-auto-input-ignore-regexp' are not included.
12175 An example (see `verilog-auto-inst' for what else is going on here):
12177 module ExampInput (ov,i);
12178 output [31:0] ov;
12179 /*AUTOINPUT*/
12180 InstModule instName
12181 (/*AUTOINST*/);
12182 endmodule
12184 Typing \\[verilog-auto] will make this into:
12186 module ExampInput (ov,i);
12187 output [31:0] ov;
12188 /*AUTOINPUT*/
12189 // Beginning of automatic inputs (from unused autoinst inputs)
12190 input i; // From inst of inst.v
12191 // End of automatics
12192 InstModule instName
12193 (/*AUTOINST*/
12194 // Outputs
12195 .ov (ov[31:0]),
12196 // Inputs
12197 .i (i));
12198 endmodule
12200 You may also provide an optional regular expression, in which case only
12201 signals matching the regular expression will be included. For example the
12202 same expansion will result from only extracting inputs starting with i:
12204 /*AUTOINPUT(\"^i\")*/"
12205 (save-excursion
12206 (let* ((indent-pt (current-indentation))
12207 (params (verilog-read-auto-params 0 1))
12208 (regexp (nth 0 params))
12209 (v2k (verilog-in-paren-quick))
12210 (modi (verilog-modi-current))
12211 (moddecls (verilog-modi-get-decls modi))
12212 (modsubdecls (verilog-modi-get-sub-decls modi))
12213 (sig-list (verilog-signals-not-in
12214 (verilog-subdecls-get-inputs modsubdecls)
12215 (append (verilog-decls-get-inputs moddecls)
12216 (verilog-decls-get-inouts moddecls)
12217 (verilog-decls-get-outputs moddecls)
12218 (verilog-decls-get-vars moddecls)
12219 (verilog-decls-get-consts moddecls)
12220 (verilog-decls-get-gparams moddecls)
12221 (verilog-subdecls-get-interfaced modsubdecls)
12222 (verilog-subdecls-get-outputs modsubdecls)
12223 (verilog-subdecls-get-inouts modsubdecls)))))
12224 (when regexp
12225 (setq sig-list (verilog-signals-matching-regexp
12226 sig-list regexp)))
12227 (setq sig-list (verilog-signals-not-matching-regexp
12228 sig-list verilog-auto-input-ignore-regexp))
12229 (verilog-forward-or-insert-line)
12230 (when v2k (verilog-repair-open-comma))
12231 (when sig-list
12232 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
12233 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
12234 (verilog-insert-indent "// End of automatics\n"))
12235 (when v2k (verilog-repair-close-comma)))))
12237 (defun verilog-auto-inout ()
12238 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
12239 Make inout statements for any inout signal in an /*AUTOINST*/ that
12240 isn't declared elsewhere inside the module.
12242 Limitations:
12243 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
12245 If placed inside the parenthesis of a module declaration, it creates
12246 Verilog 2001 style, else uses Verilog 1995 style.
12248 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
12249 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
12251 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
12253 Types are added to declarations if an AUTOLOGIC or
12254 `verilog-auto-wire-type' is set to logic.
12256 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
12258 An example (see `verilog-auto-inst' for what else is going on here):
12260 module ExampInout (ov,i);
12261 input i;
12262 /*AUTOINOUT*/
12263 InstModule instName
12264 (/*AUTOINST*/);
12265 endmodule
12267 Typing \\[verilog-auto] will make this into:
12269 module ExampInout (ov,i);
12270 input i;
12271 /*AUTOINOUT*/
12272 // Beginning of automatic inouts (from unused autoinst inouts)
12273 inout [31:0] ov; // From inst of inst.v
12274 // End of automatics
12275 InstModule instName
12276 (/*AUTOINST*/
12277 // Inouts
12278 .ov (ov[31:0]),
12279 // Inputs
12280 .i (i));
12281 endmodule
12283 You may also provide an optional regular expression, in which case only
12284 signals matching the regular expression will be included. For example the
12285 same expansion will result from only extracting inouts starting with i:
12287 /*AUTOINOUT(\"^i\")*/"
12288 (save-excursion
12289 ;; Point must be at insertion point.
12290 (let* ((indent-pt (current-indentation))
12291 (params (verilog-read-auto-params 0 1))
12292 (regexp (nth 0 params))
12293 (v2k (verilog-in-paren-quick))
12294 (modi (verilog-modi-current))
12295 (moddecls (verilog-modi-get-decls modi))
12296 (modsubdecls (verilog-modi-get-sub-decls modi))
12297 (sig-list (verilog-signals-not-in
12298 (verilog-subdecls-get-inouts modsubdecls)
12299 (append (verilog-decls-get-outputs moddecls)
12300 (verilog-decls-get-inouts moddecls)
12301 (verilog-decls-get-inputs moddecls)
12302 (verilog-subdecls-get-inputs modsubdecls)
12303 (verilog-subdecls-get-outputs modsubdecls)))))
12304 (when regexp
12305 (setq sig-list (verilog-signals-matching-regexp
12306 sig-list regexp)))
12307 (setq sig-list (verilog-signals-not-matching-regexp
12308 sig-list verilog-auto-inout-ignore-regexp))
12309 (verilog-forward-or-insert-line)
12310 (when v2k (verilog-repair-open-comma))
12311 (when sig-list
12312 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
12313 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
12314 (verilog-insert-indent "// End of automatics\n"))
12315 (when v2k (verilog-repair-close-comma)))))
12317 (defun verilog-auto-inout-module (&optional complement all-in)
12318 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
12319 Take input/output/inout statements from the specified module and insert
12320 into the current module. This is useful for making null templates and
12321 shell modules which need to have identical I/O with another module.
12322 Any I/O which are already defined in this module will not be redefined.
12323 For the complement of this function, see `verilog-auto-inout-comp',
12324 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12326 Limitations:
12327 If placed inside the parenthesis of a module declaration, it creates
12328 Verilog 2001 style, else uses Verilog 1995 style.
12330 Concatenation and outputting partial buses is not supported.
12332 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12334 Signals are not inserted in the same order as in the original module,
12335 though they will appear to be in the same order to an AUTOINST
12336 instantiating either module.
12338 Signals declared as \"output reg\" or \"output wire\" etc will
12339 lose the wire/reg declaration so that shell modules may
12340 generate those outputs differently. However, \"output logic\"
12341 is propagated.
12343 An example:
12345 module ExampShell (/*AUTOARG*/);
12346 /*AUTOINOUTMODULE(\"ExampMain\")*/
12347 endmodule
12349 module ExampMain (i,o,io);
12350 input i;
12351 output o;
12352 inout io;
12353 endmodule
12355 Typing \\[verilog-auto] will make this into:
12357 module ExampShell (/*AUTOARG*/i,o,io);
12358 /*AUTOINOUTMODULE(\"ExampMain\")*/
12359 // Beginning of automatic in/out/inouts (from specific module)
12360 output o;
12361 inout io;
12362 input i;
12363 // End of automatics
12364 endmodule
12366 You may also provide an optional regular expression, in which case only
12367 signals matching the regular expression will be included. For example the
12368 same expansion will result from only extracting signals starting with i:
12370 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
12372 You may also provide an optional third argument regular
12373 expression, in which case only signals which have that pin
12374 direction and data type matching that regular expression will be
12375 included. This matches against everything before the signal name
12376 in the declaration, for example against \"input\" (single
12377 bit), \"output logic\" (direction and type) or
12378 \"output [1:0]\" (direction and implicit type). You also
12379 probably want to skip spaces in your regexp.
12381 For example, the below will result in matching the output \"o\"
12382 against the previous example's module:
12384 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/
12386 You may also provide an optional fourth argument regular
12387 expression, which if not \"\" only signals which do NOT match
12388 that expression are included."
12389 ;; Beware spacing of quotes in above as can mess up Emacs indenter
12390 (save-excursion
12391 (let* ((params (verilog-read-auto-params 1 4))
12392 (submod (nth 0 params))
12393 (regexp (nth 1 params))
12394 (direction-re (nth 2 params))
12395 (not-re (nth 3 params))
12396 submodi)
12397 ;; Lookup position, etc of co-module
12398 ;; Note this may raise an error
12399 (when (setq submodi (verilog-modi-lookup submod t))
12400 (let* ((indent-pt (current-indentation))
12401 (v2k (verilog-in-paren-quick))
12402 (modi (verilog-modi-current))
12403 (moddecls (verilog-modi-get-decls modi))
12404 (submoddecls (verilog-modi-get-decls submodi))
12405 (sig-list-i (verilog-signals-not-in
12406 (cond (all-in
12407 (append
12408 (verilog-decls-get-inputs submoddecls)
12409 (verilog-decls-get-inouts submoddecls)
12410 (verilog-decls-get-outputs submoddecls)))
12411 (complement
12412 (verilog-decls-get-outputs submoddecls))
12413 (t (verilog-decls-get-inputs submoddecls)))
12414 (append (verilog-decls-get-inputs moddecls))))
12415 (sig-list-o (verilog-signals-not-in
12416 (cond (all-in nil)
12417 (complement
12418 (verilog-decls-get-inputs submoddecls))
12419 (t (verilog-decls-get-outputs submoddecls)))
12420 (append (verilog-decls-get-outputs moddecls))))
12421 (sig-list-io (verilog-signals-not-in
12422 (cond (all-in nil)
12423 (t (verilog-decls-get-inouts submoddecls)))
12424 (append (verilog-decls-get-inouts moddecls))))
12425 (sig-list-if (verilog-signals-not-in
12426 (verilog-decls-get-interfaces submoddecls)
12427 (append (verilog-decls-get-interfaces moddecls)))))
12428 (forward-line 1)
12429 (setq sig-list-i (verilog-signals-edit-wire-reg
12430 (verilog-signals-not-matching-regexp
12431 (verilog-signals-matching-dir-re
12432 (verilog-signals-matching-regexp sig-list-i regexp)
12433 "input" direction-re) not-re))
12434 sig-list-o (verilog-signals-edit-wire-reg
12435 (verilog-signals-not-matching-regexp
12436 (verilog-signals-matching-dir-re
12437 (verilog-signals-matching-regexp sig-list-o regexp)
12438 "output" direction-re) not-re))
12439 sig-list-io (verilog-signals-edit-wire-reg
12440 (verilog-signals-not-matching-regexp
12441 (verilog-signals-matching-dir-re
12442 (verilog-signals-matching-regexp sig-list-io regexp)
12443 "inout" direction-re) not-re))
12444 sig-list-if (verilog-signals-not-matching-regexp
12445 (verilog-signals-matching-dir-re
12446 (verilog-signals-matching-regexp sig-list-if regexp)
12447 "interface" direction-re) not-re))
12448 (when v2k (verilog-repair-open-comma))
12449 (when (or sig-list-i sig-list-o sig-list-io sig-list-if)
12450 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
12451 ;; Don't sort them so an upper AUTOINST will match the main module
12452 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12453 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12454 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12455 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
12456 (verilog-insert-indent "// End of automatics\n"))
12457 (when v2k (verilog-repair-close-comma)))))))
12459 (defun verilog-auto-inout-comp ()
12460 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
12461 Take input/output/inout statements from the specified module and
12462 insert the inverse into the current module (inputs become outputs
12463 and vice-versa.) This is useful for making test and stimulus
12464 modules which need to have complementing I/O with another module.
12465 Any I/O which are already defined in this module will not be
12466 redefined. For the complement of this function, see
12467 `verilog-auto-inout-module'.
12469 Limitations:
12470 If placed inside the parenthesis of a module declaration, it creates
12471 Verilog 2001 style, else uses Verilog 1995 style.
12473 Concatenation and outputting partial buses is not supported.
12475 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12477 Signals are not inserted in the same order as in the original module,
12478 though they will appear to be in the same order to an AUTOINST
12479 instantiating either module.
12481 An example:
12483 module ExampShell (/*AUTOARG*/);
12484 /*AUTOINOUTCOMP(\"ExampMain\")*/
12485 endmodule
12487 module ExampMain (i,o,io);
12488 input i;
12489 output o;
12490 inout io;
12491 endmodule
12493 Typing \\[verilog-auto] will make this into:
12495 module ExampShell (/*AUTOARG*/i,o,io);
12496 /*AUTOINOUTCOMP(\"ExampMain\")*/
12497 // Beginning of automatic in/out/inouts (from specific module)
12498 output i;
12499 inout io;
12500 input o;
12501 // End of automatics
12502 endmodule
12504 You may also provide an optional regular expression, in which case only
12505 signals matching the regular expression will be included. For example the
12506 same expansion will result from only extracting signals starting with i:
12508 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/
12510 You may also provide an optional third argument regular
12511 expression, in which case only signals which have that pin
12512 direction and data type matching that regular expression will be
12513 included. This matches against everything before the signal name
12514 in the declaration, for example against \"input\" (single
12515 bit), \"output logic\" (direction and type)
12516 or \"output [1:0]\" (direction and implicit type). You also
12517 probably want to skip spaces in your regexp.
12519 For example, the below will result in matching the output \"o\"
12520 against the previous example's module:
12522 /*AUTOINOUTCOMP(\"ExampMain\",\"\",\"^output.*\")*/
12524 You may also provide an optional fourth argument regular
12525 expression, which if not \"\" only signals which do NOT match
12526 that expression are included."
12527 ;; Beware spacing of quotes in above as can mess up Emacs indenter
12528 (verilog-auto-inout-module t nil))
12530 (defun verilog-auto-inout-in ()
12531 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
12532 Take input/output/inout statements from the specified module and
12533 insert them as all inputs into the current module. This is
12534 useful for making monitor modules which need to see all signals
12535 as inputs based on another module. Any I/O which are already
12536 defined in this module will not be redefined. See also
12537 `verilog-auto-inout-module'.
12539 Limitations:
12540 If placed inside the parenthesis of a module declaration, it creates
12541 Verilog 2001 style, else uses Verilog 1995 style.
12543 Concatenation and outputting partial buses is not supported.
12545 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12547 Signals are not inserted in the same order as in the original module,
12548 though they will appear to be in the same order to an AUTOINST
12549 instantiating either module.
12551 An example:
12553 module ExampShell (/*AUTOARG*/);
12554 /*AUTOINOUTIN(\"ExampMain\")*/
12555 endmodule
12557 module ExampMain (i,o,io);
12558 input i;
12559 output o;
12560 inout io;
12561 endmodule
12563 Typing \\[verilog-auto] will make this into:
12565 module ExampShell (/*AUTOARG*/i,o,io);
12566 /*AUTOINOUTIN(\"ExampMain\")*/
12567 // Beginning of automatic in/out/inouts (from specific module)
12568 input i;
12569 input io;
12570 input o;
12571 // End of automatics
12572 endmodule
12574 You may also provide an optional regular expression, in which case only
12575 signals matching the regular expression will be included. For example the
12576 same expansion will result from only extracting signals starting with i:
12578 /*AUTOINOUTIN(\"ExampMain\",\"^i\")*/"
12579 (verilog-auto-inout-module nil t))
12581 (defun verilog-auto-inout-param ()
12582 "Expand AUTOINOUTPARAM statements, as part of \\[verilog-auto].
12583 Take input/output/inout statements from the specified module and insert
12584 into the current module. This is useful for making null templates and
12585 shell modules which need to have identical I/O with another module.
12586 Any I/O which are already defined in this module will not be redefined.
12587 For the complement of this function, see `verilog-auto-inout-comp',
12588 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12590 Limitations:
12591 If placed inside the parenthesis of a module declaration, it creates
12592 Verilog 2001 style, else uses Verilog 1995 style.
12594 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12596 Parameters are inserted in the same order as in the original module.
12598 Parameters do not have values, which is SystemVerilog 2009 syntax.
12600 An example:
12602 module ExampShell ();
12603 /*AUTOINOUTPARAM(\"ExampMain\")*/
12604 endmodule
12606 module ExampMain ();
12607 parameter PARAM = 22;
12608 endmodule
12610 Typing \\[verilog-auto] will make this into:
12612 module ExampShell (/*AUTOARG*/i,o,io);
12613 /*AUTOINOUTPARAM(\"ExampMain\")*/
12614 // Beginning of automatic parameters (from specific module)
12615 parameter PARAM;
12616 // End of automatics
12617 endmodule
12619 You may also provide an optional regular expression, in which case only
12620 parameters matching the regular expression will be included. For example the
12621 same expansion will result from only extracting parameters starting with i:
12623 /*AUTOINOUTPARAM(\"ExampMain\",\"^i\")*/"
12624 (save-excursion
12625 (let* ((params (verilog-read-auto-params 1 2))
12626 (submod (nth 0 params))
12627 (regexp (nth 1 params))
12628 submodi)
12629 ;; Lookup position, etc of co-module
12630 ;; Note this may raise an error
12631 (when (setq submodi (verilog-modi-lookup submod t))
12632 (let* ((indent-pt (current-indentation))
12633 (v2k (verilog-in-paren-quick))
12634 (modi (verilog-modi-current))
12635 (moddecls (verilog-modi-get-decls modi))
12636 (submoddecls (verilog-modi-get-decls submodi))
12637 (sig-list-p (verilog-signals-not-in
12638 (verilog-decls-get-gparams submoddecls)
12639 (append (verilog-decls-get-gparams moddecls)))))
12640 (forward-line 1)
12641 (setq sig-list-p (verilog-signals-matching-regexp sig-list-p regexp))
12642 (when v2k (verilog-repair-open-comma))
12643 (when sig-list-p
12644 (verilog-insert-indent "// Beginning of automatic parameters (from specific module)\n")
12645 ;; Don't sort them so an upper AUTOINST will match the main module
12646 (verilog-insert-definition modi sig-list-p "parameter" indent-pt v2k t)
12647 (verilog-insert-indent "// End of automatics\n"))
12648 (when v2k (verilog-repair-close-comma)))))))
12650 (defun verilog-auto-inout-modport ()
12651 "Expand AUTOINOUTMODPORT statements, as part of \\[verilog-auto].
12652 Take input/output/inout statements from the specified interface
12653 and modport and insert into the current module. This is useful
12654 for making verification modules that connect to UVM interfaces.
12656 The first parameter is the name of an interface.
12658 The second parameter is a regexp of modports to read from in
12659 that interface.
12661 The optional third parameter is a regular expression, and only
12662 signals matching the regular expression will be included.
12664 Limitations:
12665 If placed inside the parenthesis of a module declaration, it creates
12666 Verilog 2001 style, else uses Verilog 1995 style.
12668 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
12670 As with other autos, any inputs/outputs declared in the module
12671 will suppress the AUTO from redeclaring an inputs/outputs by
12672 the same name.
12674 An example:
12676 interface ExampIf
12677 ( input logic clk );
12678 logic req_val;
12679 logic [7:0] req_dat;
12680 clocking mon_clkblk @(posedge clk);
12681 input req_val;
12682 input req_dat;
12683 endclocking
12684 modport mp(clocking mon_clkblk);
12685 endinterface
12687 module ExampMain
12688 ( input clk,
12689 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12690 // Beginning of automatic in/out/inouts (from modport)
12691 input [7:0] req_dat,
12692 input req_val
12693 // End of automatics
12695 /*AUTOASSIGNMODPORT(\"ExampIf\" \"mp\")*/
12696 endmodule
12698 Typing \\[verilog-auto] will make this into:
12701 module ExampMain
12702 ( input clk,
12703 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12704 // Beginning of automatic in/out/inouts (from modport)
12705 input req_dat,
12706 input req_val
12707 // End of automatics
12710 If the modport is part of a UVM monitor/driver class, this
12711 creates a wrapper module that may be used to instantiate the
12712 driver/monitor using AUTOINST in the testbench."
12713 (save-excursion
12714 (let* ((params (verilog-read-auto-params 2 3))
12715 (submod (nth 0 params))
12716 (modport-re (nth 1 params))
12717 (regexp (nth 2 params))
12718 direction-re submodi) ; direction argument not supported until requested
12719 ;; Lookup position, etc of co-module
12720 ;; Note this may raise an error
12721 (when (setq submodi (verilog-modi-lookup submod t))
12722 (let* ((indent-pt (current-indentation))
12723 (v2k (verilog-in-paren-quick))
12724 (modi (verilog-modi-current))
12725 (moddecls (verilog-modi-get-decls modi))
12726 (submoddecls (verilog-modi-get-decls submodi))
12727 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
12728 (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
12729 (verilog-decls-get-vars submoddecls)
12730 (verilog-signals-not-in
12731 (verilog-decls-get-inputs submodportdecls)
12732 (append (verilog-decls-get-ports submoddecls)
12733 (verilog-decls-get-ports moddecls)))))
12734 (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
12735 (verilog-decls-get-vars submoddecls)
12736 (verilog-signals-not-in
12737 (verilog-decls-get-outputs submodportdecls)
12738 (append (verilog-decls-get-ports submoddecls)
12739 (verilog-decls-get-ports moddecls)))))
12740 (sig-list-io (verilog-signals-in ; Decls doesn't have data types, must resolve
12741 (verilog-decls-get-vars submoddecls)
12742 (verilog-signals-not-in
12743 (verilog-decls-get-inouts submodportdecls)
12744 (append (verilog-decls-get-ports submoddecls)
12745 (verilog-decls-get-ports moddecls))))))
12746 (forward-line 1)
12747 (setq sig-list-i (verilog-signals-edit-wire-reg
12748 (verilog-signals-matching-dir-re
12749 (verilog-signals-matching-regexp sig-list-i regexp)
12750 "input" direction-re))
12751 sig-list-o (verilog-signals-edit-wire-reg
12752 (verilog-signals-matching-dir-re
12753 (verilog-signals-matching-regexp sig-list-o regexp)
12754 "output" direction-re))
12755 sig-list-io (verilog-signals-edit-wire-reg
12756 (verilog-signals-matching-dir-re
12757 (verilog-signals-matching-regexp sig-list-io regexp)
12758 "inout" direction-re)))
12759 (when v2k (verilog-repair-open-comma))
12760 (when (or sig-list-i sig-list-o sig-list-io)
12761 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from modport)\n")
12762 ;; Don't sort them so an upper AUTOINST will match the main module
12763 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12764 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12765 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12766 (verilog-insert-indent "// End of automatics\n"))
12767 (when v2k (verilog-repair-close-comma)))))))
12769 (defun verilog-auto-insert-lisp ()
12770 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
12771 The Lisp code provided is called before other AUTOS are expanded,
12772 and the Lisp code generally will call `insert' to insert text
12773 into the current file beginning on the line after the
12774 AUTOINSERTLISP.
12776 See also AUTOINSERTLAST and `verilog-auto-insert-last' which
12777 executes after (as opposed to before) other AUTOs.
12779 See also AUTO_LISP, which takes a Lisp expression and evaluates
12780 it during `verilog-auto-inst' but does not insert any text.
12782 An example:
12784 module ExampInsertLisp;
12785 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
12786 endmodule
12788 // For this example we declare the function in the
12789 // module's file itself. Often you'd define it instead
12790 // in a site-start.el or init file.
12792 Local Variables:
12793 eval:
12794 (defun my-verilog-insert-hello (who)
12795 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
12796 End:
12799 Typing \\[verilog-auto] will call my-verilog-insert-hello and
12800 expand the above into:
12802 // Beginning of automatic insert lisp
12803 initial $write(\"hello world\");
12804 // End of automatics
12806 You can also call an external program and insert the returned
12807 text:
12809 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
12810 // Beginning of automatic insert lisp
12811 //hello
12812 // End of automatics"
12813 (save-excursion
12814 ;; Point is at end of /*AUTO...*/
12815 (let* ((indent-pt (current-indentation))
12816 (cmd-end-pt (save-excursion (search-backward ")")
12817 (forward-char)
12818 (point))) ; Closing paren
12819 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
12820 (backward-sexp 1) ; Inside comment
12821 (point))) ; Beginning paren
12822 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
12823 (verilog-forward-or-insert-line)
12824 ;; Some commands don't move point (like insert-file) so we always
12825 ;; add the begin/end comments, then delete it if not needed
12826 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
12827 (verilog-insert-indent "// End of automatics\n")
12828 (forward-line -1)
12829 (eval (read cmd))
12830 (forward-line -1)
12831 (setq verilog-scan-cache-tick nil) ; Clear cache; inserted unknown text
12832 (verilog-delete-empty-auto-pair))))
12834 (defun verilog-auto-insert-last ()
12835 "Expand AUTOINSERTLAST statements, as part of \\[verilog-auto].
12836 The Lisp code provided is called after all other AUTOS have been
12837 expanded, and the Lisp code generally will call `insert' to
12838 insert text into the current file beginning on the line after the
12839 AUTOINSERTLAST.
12841 Other than when called (after AUTOs are expanded), the functionality
12842 is otherwise identical to AUTOINSERTLISP and `verilog-auto-insert-lisp' which
12843 executes before (as opposed to after) other AUTOs.
12845 See `verilog-auto-insert-lisp' for examples."
12846 (verilog-auto-insert-lisp))
12848 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
12849 "Return list of signals for current AUTOSENSE block."
12850 (let* ((sigss (save-excursion
12851 (search-forward ")")
12852 (verilog-read-always-signals)))
12853 (sig-list (verilog-signals-not-params
12854 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
12855 (append (and (not verilog-auto-sense-include-inputs)
12856 (verilog-alw-get-outputs-delayed sigss))
12857 (and (not verilog-auto-sense-include-inputs)
12858 (verilog-alw-get-outputs-immediate sigss))
12859 (verilog-alw-get-temps sigss)
12860 (verilog-decls-get-consts moddecls)
12861 (verilog-decls-get-gparams moddecls)
12862 presense-sigs)))))
12863 sig-list))
12865 (defun verilog-auto-sense ()
12866 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
12867 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
12868 with one automatically derived from all inputs declared in the always
12869 statement. Signals that are generated within the same always block are NOT
12870 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
12871 Long lines are split based on the `fill-column', see \\[set-fill-column].
12873 Limitations:
12874 Verilog does not allow memories (multidimensional arrays) in sensitivity
12875 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
12877 Constant signals:
12878 AUTOSENSE cannot always determine if a \\=`define is a constant or a signal
12879 (it could be in an include file for example). If a \\=`define or other signal
12880 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
12881 declaration anywhere in the module (parenthesis are required):
12883 /* AUTO_CONSTANT ( \\=`this_is_really_constant_dont_autosense_it ) */
12885 Better yet, use a parameter, which will be understood to be constant
12886 automatically.
12888 OOps!
12889 If AUTOSENSE makes a mistake, please report it. (First try putting
12890 a begin/end after your always!) As a workaround, if a signal that
12891 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
12892 If a signal should be in the sensitivity list wasn't, placing it before
12893 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
12894 autos are updated (or added if it occurs there already).
12896 An example:
12898 always @ (/*AS*/) begin
12899 /* AUTO_CONSTANT (\\=`constant) */
12900 outin = ina | inb | \\=`constant;
12901 out = outin;
12904 Typing \\[verilog-auto] will make this into:
12906 always @ (/*AS*/ina or inb) begin
12907 /* AUTO_CONSTANT (\\=`constant) */
12908 outin = ina | inb | \\=`constant;
12909 out = outin;
12912 Note in Verilog 2001, you can often get the same result from the new @*
12913 operator. (This was added to the language in part due to AUTOSENSE!)
12915 always @* begin
12916 outin = ina | inb | \\=`constant;
12917 out = outin;
12918 end"
12919 (save-excursion
12920 ;; Find beginning
12921 (let* ((start-pt (save-excursion
12922 (verilog-re-search-backward-quick "(" nil t)
12923 (point)))
12924 (indent-pt (save-excursion
12925 (or (and (goto-char start-pt) (1+ (current-column)))
12926 (current-indentation))))
12927 (modi (verilog-modi-current))
12928 (moddecls (verilog-modi-get-decls modi))
12929 (sig-memories (verilog-signals-memory
12930 (verilog-decls-get-vars moddecls)))
12931 sig-list not-first presense-sigs)
12932 ;; Read signals in always, eliminate outputs from sense list
12933 (setq presense-sigs (verilog-signals-from-signame
12934 (save-excursion
12935 (verilog-read-signals start-pt (point)))))
12936 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
12937 (when sig-memories
12938 (let ((tlen (length sig-list)))
12939 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
12940 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
12941 (if (and presense-sigs ; Add a "or" if not "(.... or /*AUTOSENSE*/"
12942 (save-excursion (goto-char (point))
12943 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12944 (verilog-re-search-backward-quick "\\s-" start-pt t)
12945 (while (looking-at "\\s-`endif")
12946 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12947 (verilog-re-search-backward-quick "\\s-" start-pt t))
12948 (not (looking-at "\\s-or\\b"))))
12949 (setq not-first t))
12950 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12951 (while sig-list
12952 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
12953 (insert "\n")
12954 (indent-to indent-pt)
12955 (if not-first (insert "or ")))
12956 (not-first (insert " or ")))
12957 (insert (verilog-sig-name (car sig-list)))
12958 (setq sig-list (cdr sig-list)
12959 not-first t)))))
12961 (defun verilog-auto-reset ()
12962 "Expand AUTORESET statements, as part of \\[verilog-auto].
12963 Replace the /*AUTORESET*/ comment with code to initialize all
12964 registers set elsewhere in the always block.
12966 Limitations:
12967 AUTORESET will not clear memories.
12969 AUTORESET uses <= if the signal has a <= assignment in the block,
12970 else it uses =.
12972 If <= is used, all = assigned variables are ignored if
12973 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
12974 to be temporaries.
12976 /*AUTORESET*/ presumes that any signals mentioned between the previous
12977 begin/case/if statement and the AUTORESET comment are being reset manually
12978 and should not be automatically reset. This includes omitting any signals
12979 used on the right hand side of assignments.
12981 By default, AUTORESET will include the width of the signal in the
12982 autos, SystemVerilog designs may want to change this. To control
12983 this behavior, see `verilog-auto-reset-widths'. In some cases
12984 AUTORESET must use a \\='0 assignment and it will print NOWIDTH; use
12985 `verilog-auto-reset-widths' unbased to prevent this.
12987 AUTORESET ties signals to deasserted, which is presumed to be zero.
12988 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12989 them to a one.
12991 AUTORESET may try to reset arrays or structures that cannot be
12992 reset by a simple assignment, resulting in compile errors. This
12993 is a feature to be taken as a hint that you need to reset these
12994 signals manually (or put them into a \"\\=`ifdef NEVER signal<=\\=`0;
12995 \\=`endif\" so Verilog-Mode ignores them.)
12997 An example:
12999 always @(posedge clk or negedge reset_l) begin
13000 if (!reset_l) begin
13001 c <= 1;
13002 /*AUTORESET*/
13004 else begin
13005 a <= in_a;
13006 b <= in_b;
13007 c <= in_c;
13011 Typing \\[verilog-auto] will make this into:
13013 always @(posedge core_clk or negedge reset_l) begin
13014 if (!reset_l) begin
13015 c <= 1;
13016 /*AUTORESET*/
13017 // Beginning of autoreset for uninitialized flops
13018 a <= 0;
13019 b = 0; // if `verilog-auto-reset-blocking-in-non' true
13020 // End of automatics
13022 else begin
13023 a <= in_a;
13024 b = in_b;
13025 c <= in_c;
13027 end"
13029 (interactive)
13030 (save-excursion
13031 ;; Find beginning
13032 (let* ((indent-pt (current-indentation))
13033 (modi (verilog-modi-current))
13034 (moddecls (verilog-modi-get-decls modi))
13035 (all-list (verilog-decls-get-signals moddecls))
13036 sigss sig-list dly-list prereset-sigs)
13037 ;; Read signals in always, eliminate outputs from reset list
13038 (setq prereset-sigs (verilog-signals-from-signame
13039 (save-excursion
13040 (verilog-read-signals
13041 (save-excursion
13042 (verilog-re-search-backward-quick
13043 "\\(@\\|\\<\\(begin\\|if\\|case\\|always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
13044 (point))
13045 (point)))))
13046 (save-excursion
13047 (verilog-re-search-backward-quick "\\(@\\|\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
13048 (setq sigss (verilog-read-always-signals)))
13049 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
13050 (setq sig-list (verilog-signals-not-in-struct
13051 (append
13052 (verilog-alw-get-outputs-delayed sigss)
13053 (when (or (not (verilog-alw-get-uses-delayed sigss))
13054 verilog-auto-reset-blocking-in-non)
13055 (verilog-alw-get-outputs-immediate sigss)))
13056 (append
13057 (verilog-alw-get-temps sigss)
13058 prereset-sigs)))
13059 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
13060 (when sig-list
13061 (insert "\n");
13062 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
13063 (while sig-list
13064 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ; As sig-list has no widths
13065 (car sig-list))))
13066 (indent-to indent-pt)
13067 (insert (verilog-sig-name sig)
13068 (if (assoc (verilog-sig-name sig) dly-list)
13069 (concat " <= " verilog-assignment-delay)
13070 " = ")
13071 (verilog-sig-tieoff sig)
13072 ";\n")
13073 (setq sig-list (cdr sig-list))))
13074 (verilog-insert-indent "// End of automatics")))))
13076 (defun verilog-auto-tieoff ()
13077 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
13078 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
13079 signals to deasserted.
13081 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
13082 input/output list as another module, but no internals. Specifically, it
13083 finds all outputs in the module, and if that input is not otherwise declared
13084 as a register or wire, creates a tieoff.
13086 AUTORESET ties signals to deasserted, which is presumed to be zero.
13087 Signals that match `verilog-active-low-regexp' will be deasserted by tying
13088 them to a one.
13090 You can add signals you do not want included in AUTOTIEOFF with
13091 `verilog-auto-tieoff-ignore-regexp'.
13093 `verilog-auto-wire-type' may be used to change the datatype of
13094 the declarations.
13096 `verilog-auto-reset-widths' may be used to change how the tieoff
13097 value's width is generated.
13099 An example of making a stub for another module:
13101 module ExampStub (/*AUTOINST*/);
13102 /*AUTOINOUTPARAM(\"Foo\")*/
13103 /*AUTOINOUTMODULE(\"Foo\")*/
13104 /*AUTOTIEOFF*/
13105 // verilator lint_off UNUSED
13106 wire _unused_ok = &{1\\='b0,
13107 /*AUTOUNUSED*/
13108 1\\='b0};
13109 // verilator lint_on UNUSED
13110 endmodule
13112 Typing \\[verilog-auto] will make this into:
13114 module ExampStub (/*AUTOINST*/...);
13115 /*AUTOINOUTPARAM(\"Foo\")*/
13116 /*AUTOINOUTMODULE(\"Foo\")*/
13117 // Beginning of autotieoff
13118 output [2:0] foo;
13119 // End of automatics
13121 /*AUTOTIEOFF*/
13122 // Beginning of autotieoff
13123 wire [2:0] foo = 3\\='b0;
13124 // End of automatics
13126 endmodule"
13127 (interactive)
13128 (save-excursion
13129 ;; Find beginning
13130 (let* ((indent-pt (current-indentation))
13131 (modi (verilog-modi-current))
13132 (moddecls (verilog-modi-get-decls modi))
13133 (modsubdecls (verilog-modi-get-sub-decls modi))
13134 (sig-list (verilog-signals-not-in
13135 (verilog-decls-get-outputs moddecls)
13136 (append (verilog-decls-get-vars moddecls)
13137 (verilog-decls-get-assigns moddecls)
13138 (verilog-decls-get-consts moddecls)
13139 (verilog-decls-get-gparams moddecls)
13140 (verilog-subdecls-get-interfaced modsubdecls)
13141 (verilog-subdecls-get-outputs modsubdecls)
13142 (verilog-subdecls-get-inouts modsubdecls)))))
13143 (setq sig-list (verilog-signals-not-matching-regexp
13144 sig-list verilog-auto-tieoff-ignore-regexp))
13145 (when sig-list
13146 (verilog-forward-or-insert-line)
13147 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
13148 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
13149 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
13150 (while sig-list
13151 (let ((sig (car sig-list)))
13152 (cond ((equal verilog-auto-tieoff-declaration "assign")
13153 (indent-to indent-pt)
13154 (insert "assign " (verilog-sig-name sig)))
13156 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
13157 (indent-to (max 48 (+ indent-pt 40)))
13158 (insert "= " (verilog-sig-tieoff sig)
13159 ";\n")
13160 (setq sig-list (cdr sig-list))))
13161 (verilog-insert-indent "// End of automatics\n")))))
13163 (defun verilog-auto-undef ()
13164 "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
13165 Take any \\=`defines since the last AUTOUNDEF in the current file
13166 and create \\=`undefs for them. This is used to insure that
13167 file-local defines do not pollute the global \\=`define name space.
13169 Limitations:
13170 AUTOUNDEF presumes any identifier following \\=`define is the
13171 name of a define. Any \\=`ifdefs are ignored.
13173 AUTOUNDEF suppresses creating an \\=`undef for any define that was
13174 \\=`undefed before the AUTOUNDEF. This may be used to work around
13175 the ignoring of \\=`ifdefs as shown below.
13177 An example:
13179 \\=`define XX_FOO
13180 \\=`define M_BAR(x)
13181 \\=`define M_BAZ
13183 \\=`ifdef NEVER
13184 \\=`undef M_BAZ // Emacs will see this and not \\=`undef M_BAZ
13185 \\=`endif
13187 /*AUTOUNDEF*/
13189 Typing \\[verilog-auto] will make this into:
13192 /*AUTOUNDEF*/
13193 // Beginning of automatic undefs
13194 \\=`undef XX_FOO
13195 \\=`undef M_BAR
13196 // End of automatics
13198 You may also provide an optional regular expression, in which case only
13199 defines the regular expression will be undefed."
13200 (save-excursion
13201 (let* ((params (verilog-read-auto-params 0 1))
13202 (regexp (nth 0 params))
13203 (indent-pt (current-indentation))
13204 (end-pt (point))
13205 defs def)
13206 (save-excursion
13207 ;; Scan from start of file, or last AUTOUNDEF
13208 (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
13209 (goto-char (point-min)))
13210 (while (verilog-re-search-forward-quick
13211 "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
13212 (cond ((equal (match-string-no-properties 1) "define")
13213 (setq def (match-string-no-properties 2))
13214 (when (and (or (not regexp)
13215 (string-match regexp def))
13216 (not (member def defs))) ; delete-dups not in 21.1
13217 (setq defs (cons def defs))))
13219 (setq defs (delete (match-string-no-properties 2) defs))))))
13220 ;; Insert
13221 (setq defs (sort defs 'string<))
13222 (when defs
13223 (verilog-forward-or-insert-line)
13224 (verilog-insert-indent "// Beginning of automatic undefs\n")
13225 (while defs
13226 (verilog-insert-indent "`undef " (car defs) "\n")
13227 (setq defs (cdr defs)))
13228 (verilog-insert-indent "// End of automatics\n")))))
13230 (defun verilog-auto-unused ()
13231 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
13232 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
13233 input and inout signals.
13235 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
13236 input/output list as another module, but no internals. Specifically, it
13237 finds all inputs and inouts in the module, and if that input is not otherwise
13238 used, adds it to a comma separated list.
13240 The comma separated list is intended to be used to create a _unused_ok
13241 signal. Using the exact name \"_unused_ok\" for name of the temporary
13242 signal is recommended as it will insure maximum forward compatibility, it
13243 also makes lint warnings easy to understand; ignore any unused warnings
13244 with \"unused\" in the signal name.
13246 To reduce simulation time, the _unused_ok signal should be forced to a
13247 constant to prevent wiggling. The easiest thing to do is use a
13248 reduction-and with 1\\='b0 as shown.
13250 This way all unused signals are in one place, making it convenient to add
13251 your tool's specific pragmas around the assignment to disable any unused
13252 warnings.
13254 You can add signals you do not want included in AUTOUNUSED with
13255 `verilog-auto-unused-ignore-regexp'.
13257 An example of making a stub for another module:
13259 module ExampStub (/*AUTOINST*/);
13260 /*AUTOINOUTPARAM(\"Examp\")*/
13261 /*AUTOINOUTMODULE(\"Examp\")*/
13262 /*AUTOTIEOFF*/
13263 // verilator lint_off UNUSED
13264 wire _unused_ok = &{1\\='b0,
13265 /*AUTOUNUSED*/
13266 1\\='b0};
13267 // verilator lint_on UNUSED
13268 endmodule
13270 Typing \\[verilog-auto] will make this into:
13273 // verilator lint_off UNUSED
13274 wire _unused_ok = &{1\\='b0,
13275 /*AUTOUNUSED*/
13276 // Beginning of automatics
13277 unused_input_a,
13278 unused_input_b,
13279 unused_input_c,
13280 // End of automatics
13281 1\\='b0};
13282 // verilator lint_on UNUSED
13283 endmodule"
13284 (interactive)
13285 (save-excursion
13286 ;; Find beginning
13287 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
13288 (modi (verilog-modi-current))
13289 (moddecls (verilog-modi-get-decls modi))
13290 (modsubdecls (verilog-modi-get-sub-decls modi))
13291 (sig-list (verilog-signals-not-in
13292 (append (verilog-decls-get-inputs moddecls)
13293 (verilog-decls-get-inouts moddecls))
13294 (append (verilog-subdecls-get-inputs modsubdecls)
13295 (verilog-subdecls-get-inouts modsubdecls)))))
13296 (setq sig-list (verilog-signals-not-matching-regexp
13297 sig-list verilog-auto-unused-ignore-regexp))
13298 (when sig-list
13299 (verilog-forward-or-insert-line)
13300 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
13301 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
13302 (while sig-list
13303 (let ((sig (car sig-list)))
13304 (indent-to indent-pt)
13305 (insert (verilog-sig-name sig) ",\n")
13306 (setq sig-list (cdr sig-list))))
13307 (verilog-insert-indent "// End of automatics\n")))))
13309 (defun verilog-enum-ascii (signm elim-regexp)
13310 "Convert an enum name SIGNM to an ascii string for insertion.
13311 Remove user provided prefix ELIM-REGEXP."
13312 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
13313 (let ((case-fold-search t))
13314 ;; All upper becomes all lower for readability
13315 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
13317 (defun verilog-auto-ascii-enum ()
13318 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
13319 Create a register to contain the ASCII decode of an enumerated signal type.
13320 This will allow trace viewers to show the ASCII name of states.
13322 First, parameters are built into an enumeration using the synopsys enum
13323 comment. The comment must be between the keyword and the symbol.
13324 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
13326 Next, registers which that enum applies to are also tagged with the same
13327 enum.
13329 Finally, an AUTOASCIIENUM command is used.
13331 The first parameter is the name of the signal to be decoded.
13333 The second parameter is the name to store the ASCII code into. For the
13334 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
13335 a signal that is just for simulation, and the magic characters _ascii
13336 tell viewers like Dinotrace to display in ASCII format.
13338 The third optional parameter is a string which will be removed
13339 from the state names. It defaults to \"\" which removes nothing.
13341 The fourth optional parameter is \"onehot\" to force one-hot
13342 decoding. If unspecified, if and only if the first parameter
13343 width is 2^(number of states in enum) and does NOT match the
13344 width of the enum, the signal is assumed to be a one-hot
13345 decode. Otherwise, it's a normal encoded state vector.
13347 `verilog-auto-wire-type' may be used to change the datatype of
13348 the declarations.
13350 \"auto enum\" may be used in place of \"synopsys enum\".
13352 An example:
13354 //== State enumeration
13355 parameter [2:0] // synopsys enum state_info
13356 SM_IDLE = 3\\='b000,
13357 SM_SEND = 3\\='b001,
13358 SM_WAIT1 = 3\\='b010;
13359 //== State variables
13360 reg [2:0] /* synopsys enum state_info */
13361 state_r; /* synopsys state_vector state_r */
13362 reg [2:0] /* synopsys enum state_info */
13363 state_e1;
13365 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
13367 Typing \\[verilog-auto] will make this into:
13369 ... same front matter ...
13371 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
13372 // Beginning of automatic ASCII enum decoding
13373 reg [39:0] state_ascii_r; // Decode of state_r
13374 always @(state_r) begin
13375 case ({state_r})
13376 SM_IDLE: state_ascii_r = \"idle \";
13377 SM_SEND: state_ascii_r = \"send \";
13378 SM_WAIT1: state_ascii_r = \"wait1\";
13379 default: state_ascii_r = \"%Erro\";
13380 endcase
13382 // End of automatics"
13383 (save-excursion
13384 (let* ((params (verilog-read-auto-params 2 4))
13385 (undecode-name (nth 0 params))
13386 (ascii-name (nth 1 params))
13387 (elim-regexp (and (nth 2 params)
13388 (not (equal (nth 2 params) ""))
13389 (nth 2 params)))
13390 (one-hot-flag (nth 3 params))
13392 (indent-pt (current-indentation))
13393 (modi (verilog-modi-current))
13394 (moddecls (verilog-modi-get-decls modi))
13396 (sig-list-consts (append (verilog-decls-get-consts moddecls)
13397 (verilog-decls-get-gparams moddecls)))
13398 (sig-list-all (verilog-decls-get-iovars moddecls))
13400 (undecode-sig (or (assoc undecode-name sig-list-all)
13401 (error "%s: Signal `%s' not found in design"
13402 (verilog-point-text) undecode-name)))
13403 (undecode-enum (or (verilog-sig-enum undecode-sig)
13404 (error "%s: Signal `%s' does not have an enum tag"
13405 (verilog-point-text) undecode-name)))
13407 (enum-sigs (verilog-signals-not-in
13408 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
13409 (error "%s: No state definitions for `%s'"
13410 (verilog-point-text) undecode-enum))
13411 nil))
13413 (one-hot (or
13414 (string-match "onehot" (or one-hot-flag ""))
13415 (and ; width(enum) != width(sig)
13416 (or (not (verilog-sig-bits (car enum-sigs)))
13417 (not (equal (verilog-sig-width (car enum-sigs))
13418 (verilog-sig-width undecode-sig))))
13419 ;; count(enums) == width(sig)
13420 (equal (number-to-string (length enum-sigs))
13421 (verilog-sig-width undecode-sig)))))
13422 (enum-chars 0)
13423 (ascii-chars 0))
13425 ;; Find number of ascii chars needed
13426 (let ((tmp-sigs enum-sigs))
13427 (while tmp-sigs
13428 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
13429 ascii-chars (max ascii-chars (length (verilog-enum-ascii
13430 (verilog-sig-name (car tmp-sigs))
13431 elim-regexp)))
13432 tmp-sigs (cdr tmp-sigs))))
13434 (verilog-forward-or-insert-line)
13435 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
13436 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
13437 (concat "Decode of " undecode-name) nil nil))))
13438 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
13440 (verilog-insert-indent "always @(" undecode-name ") begin\n")
13441 (setq indent-pt (+ indent-pt verilog-indent-level))
13442 (verilog-insert-indent "case ({" undecode-name "})\n")
13443 (setq indent-pt (+ indent-pt verilog-case-indent))
13445 (let ((tmp-sigs enum-sigs)
13446 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
13447 (+ (if one-hot 9 1) (max 8 enum-chars))
13448 ascii-name ascii-chars))
13449 (errname (substring "%Error" 0 (min 6 ascii-chars))))
13450 (while tmp-sigs
13451 (verilog-insert-indent
13452 (concat
13453 (format chrfmt
13454 (concat (if one-hot "(")
13455 ;; Use enum-sigs length as that's numeric
13456 ;; verilog-sig-width undecode-sig might not be.
13457 (if one-hot (number-to-string (length enum-sigs)))
13458 ;; We use a shift instead of var[index]
13459 ;; so that a non-one hot value will show as error.
13460 (if one-hot "'b1<<")
13461 (verilog-sig-name (car tmp-sigs))
13462 (if one-hot ")") ":")
13463 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
13464 elim-regexp))))
13465 (setq tmp-sigs (cdr tmp-sigs)))
13466 (verilog-insert-indent (format chrfmt "default:" errname)))
13468 (setq indent-pt (- indent-pt verilog-case-indent))
13469 (verilog-insert-indent "endcase\n")
13470 (setq indent-pt (- indent-pt verilog-indent-level))
13471 (verilog-insert-indent "end\n"
13472 "// End of automatics\n"))))
13474 (defun verilog-auto-templated-rel ()
13475 "Replace Templated relative line numbers with absolute line numbers.
13476 Internal use only. This hacks around the line numbers in AUTOINST Templates
13477 being different from the final output's line numbering."
13478 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
13479 ;; Find line number each template is on
13480 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
13481 (goto-char (point-min))
13482 (while (not (eobp))
13483 (when (looking-at ".*AUTO_TEMPLATE")
13484 (setq templateno (1+ templateno))
13485 (setq template-line (cons buf-line template-line)))
13486 (setq buf-line (1+ buf-line))
13487 (forward-line 1))
13488 (setq template-line (nreverse template-line))
13489 ;; Replace T# L# with absolute line number
13490 (goto-char (point-min))
13491 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
13492 (replace-match
13493 (concat " Templated "
13494 (int-to-string (+ (nth (string-to-number (match-string 1))
13495 template-line)
13496 (string-to-number (match-string 2)))))
13497 t t))))
13499 (defun verilog-auto-template-lint ()
13500 "Check AUTO_TEMPLATEs for unused lines.
13501 Enable with `verilog-auto-template-warn-unused'."
13502 (let ((name1 (or (buffer-file-name) (buffer-name))))
13503 (save-excursion
13504 (goto-char (point-min))
13505 (while (re-search-forward
13506 "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
13507 (let* ((tpl-info (verilog-read-auto-template-middle))
13508 (tpl-list (aref tpl-info 1))
13509 (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
13510 tpl-ass)
13511 (while tlines
13512 (setq tpl-ass (car tlines)
13513 tlines (cdr tlines))
13515 (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ; Not supported, no warning
13516 (not verilog-auto-template-hits)
13517 (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
13518 verilog-auto-template-hits))
13519 (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
13520 name1
13521 (+ (elt tpl-ass 3) ; Template line number
13522 (count-lines (point-min) (point)))
13523 (elt tpl-ass 0) (elt tpl-ass 1))
13524 )))))))
13527 ;;; Auto top level:
13530 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
13531 "Expand AUTO statements.
13532 Look for any /*AUTO...*/ commands in the code, as used in
13533 instantiations or argument headers. Update the list of signals
13534 following the /*AUTO...*/ command.
13536 Use \\[verilog-delete-auto] to remove the AUTOs.
13538 Use \\[verilog-diff-auto] to see differences in AUTO expansion.
13540 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
13542 Use \\[verilog-faq] for a pointer to frequently asked questions.
13544 For new users, we recommend setting `verilog-case-fold' to nil
13545 and `verilog-auto-arg-sort' to t.
13547 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
13548 called before and after this function, respectively.
13550 For example:
13551 module ModuleName (/*AUTOARG*/);
13552 /*AUTOINPUT*/
13553 /*AUTOOUTPUT*/
13554 /*AUTOWIRE*/
13555 /*AUTOREG*/
13556 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
13558 You can also update the AUTOs from the shell using:
13559 emacs --batch <filenames.v> -f verilog-batch-auto
13560 Or fix indentation with:
13561 emacs --batch <filenames.v> -f verilog-batch-indent
13562 Likewise, you can delete or inject AUTOs with:
13563 emacs --batch <filenames.v> -f verilog-batch-delete-auto
13564 emacs --batch <filenames.v> -f verilog-batch-inject-auto
13565 Or check if AUTOs have the same expansion
13566 emacs --batch <filenames.v> -f verilog-batch-diff-auto
13568 Using \\[describe-function], see also:
13569 `verilog-auto-arg' for AUTOARG module instantiations
13570 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
13571 `verilog-auto-assign-modport' for AUTOASSIGNMODPORT assignment to/from modport
13572 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
13573 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
13574 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
13575 `verilog-auto-inout-modport' for AUTOINOUTMODPORT i/o from an interface modport
13576 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
13577 `verilog-auto-inout-param' for AUTOINOUTPARAM copying params from elsewhere
13578 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
13579 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
13580 `verilog-auto-insert-last' for AUTOINSERTLAST insert code from lisp function
13581 `verilog-auto-inst' for AUTOINST instantiation pins
13582 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
13583 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
13584 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
13585 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
13586 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
13587 `verilog-auto-reg' for AUTOREG registers
13588 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
13589 `verilog-auto-reset' for AUTORESET flop resets
13590 `verilog-auto-sense' for AUTOSENSE or AS always sensitivity lists
13591 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
13592 `verilog-auto-undef' for AUTOUNDEF \\=`undef of local \\=`defines
13593 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
13594 `verilog-auto-wire' for AUTOWIRE instantiation wires
13596 `verilog-read-defines' for reading \\=`define values
13597 `verilog-read-includes' for reading \\=`includes
13599 If you have bugs with these autos, please file an issue at
13600 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
13601 Wilson Snyder (wsnyder@wsnyder.org)."
13602 (interactive)
13603 (unless noninteractive (message "Updating AUTOs..."))
13604 (if (fboundp 'dinotrace-unannotate-all)
13605 (dinotrace-unannotate-all))
13606 ;; Disable change hooks for speed
13607 ;; This let can't be part of above let; must restore
13608 ;; after-change-functions before font-lock resumes
13609 (verilog-save-font-no-change-functions
13610 (let ((oldbuf (if (not (buffer-modified-p))
13611 (buffer-string)))
13612 (case-fold-search verilog-case-fold)
13613 ;; Cache directories; we don't write new files, so can't change
13614 (verilog-dir-cache-preserving t)
13615 ;; Cache current module
13616 (verilog-modi-cache-current-enable t)
13617 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
13618 verilog-modi-cache-current)
13619 (verilog-save-scan-cache
13620 (save-excursion
13621 ;; Wipe cache; otherwise if we AUTOed a block above this one,
13622 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
13623 (setq verilog-modi-cache-list nil)
13624 ;; Local state
13625 (verilog-read-auto-template-init)
13626 ;; If we're not in verilog-mode, change syntax table so parsing works right
13627 (unless (eq major-mode `verilog-mode) (verilog-mode))
13628 ;; Allow user to customize
13629 (verilog-run-hooks 'verilog-before-auto-hook)
13630 ;; Try to save the user from needing to revert-file to reread file local-variables
13631 (verilog-auto-reeval-locals)
13632 (verilog-read-auto-lisp-present)
13633 (verilog-read-auto-lisp (point-min) (point-max))
13634 (verilog-getopt-flags)
13635 ;; From here on out, we can cache anything we read from disk
13636 (verilog-preserve-dir-cache
13637 ;; These two may seem obvious to do always, but on large includes it can be way too slow
13638 (when verilog-auto-read-includes
13639 (verilog-read-includes)
13640 (verilog-read-defines nil nil t))
13641 ;; Setup variables due to SystemVerilog expansion
13642 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
13643 ;; This particular ordering is important
13644 ;; INST: Lower modules correct, no internal dependencies, FIRST
13645 (verilog-preserve-modi-cache
13646 ;; Clear existing autos else we'll be screwed by existing ones
13647 (verilog-delete-auto-buffer)
13648 ;; Injection if appropriate
13649 (when inject
13650 (verilog-inject-inst)
13651 (verilog-inject-sense)
13652 (verilog-inject-arg))
13654 ;; Do user inserts first, so their code can insert AUTOs
13655 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
13656 'verilog-auto-insert-lisp)
13657 ;; Expand instances before need the signals the instances input/output
13658 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
13659 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
13660 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
13661 ;; Doesn't matter when done, but combine it with a common changer
13662 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
13663 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
13664 ;; Must be done before autoin/out as creates a reg
13665 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM(.*?)\\*/" 'verilog-auto-ascii-enum)
13667 ;; first in/outs from other files
13668 (verilog-auto-re-search-do "/\\*AUTOINOUTMODPORT(.*?)\\*/" 'verilog-auto-inout-modport)
13669 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE(.*?)\\*/" 'verilog-auto-inout-module)
13670 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP(.*?)\\*/" 'verilog-auto-inout-comp)
13671 (verilog-auto-re-search-do "/\\*AUTOINOUTIN(.*?)\\*/" 'verilog-auto-inout-in)
13672 (verilog-auto-re-search-do "/\\*AUTOINOUTPARAM(.*?)\\*/" 'verilog-auto-inout-param)
13673 ;; next in/outs which need previous sucked inputs first
13674 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((.*?)\\)?\\*/" 'verilog-auto-output)
13675 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((.*?)\\)?\\*/" 'verilog-auto-input)
13676 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((.*?)\\)?\\*/" 'verilog-auto-inout)
13677 ;; Then tie off those in/outs
13678 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
13679 ;; These can be anywhere after AUTOINSERTLISP
13680 (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((.*?)\\)?\\*/" 'verilog-auto-undef)
13681 ;; Wires/regs must be after inputs/outputs
13682 (verilog-auto-re-search-do "/\\*AUTOASSIGNMODPORT(.*?)\\*/" 'verilog-auto-assign-modport)
13683 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
13684 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
13685 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
13686 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
13687 ;; outputevery needs AUTOOUTPUTs done first
13688 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\((.*?)\\)?\\*/" 'verilog-auto-output-every)
13689 ;; After we've created all new variables
13690 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
13691 ;; Must be after all inputs outputs are generated
13692 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
13693 ;; User inserts
13694 (verilog-auto-re-search-do "/\\*AUTOINSERTLAST(.*?)\\*/" 'verilog-auto-insert-last)
13695 ;; Fix line numbers (comments only)
13696 (when verilog-auto-inst-template-numbers
13697 (verilog-auto-templated-rel))
13698 (when verilog-auto-template-warn-unused
13699 (verilog-auto-template-lint))))
13701 (verilog-run-hooks 'verilog-auto-hook)
13703 (when verilog-auto-delete-trailing-whitespace
13704 (verilog-delete-trailing-whitespace))
13706 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
13708 ;; If end result is same as when started, clear modified flag
13709 (cond ((and oldbuf (equal oldbuf (buffer-string)))
13710 (verilog-restore-buffer-modified-p nil)
13711 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
13712 (t (unless noninteractive (message "Updating AUTOs...done"))))
13713 ;; End of save-cache
13714 )))))
13716 ;;; Skeletons:
13719 (defvar verilog-template-map
13720 (let ((map (make-sparse-keymap)))
13721 (define-key map "a" 'verilog-sk-always)
13722 (define-key map "b" 'verilog-sk-begin)
13723 (define-key map "c" 'verilog-sk-case)
13724 (define-key map "f" 'verilog-sk-for)
13725 (define-key map "g" 'verilog-sk-generate)
13726 (define-key map "h" 'verilog-sk-header)
13727 (define-key map "i" 'verilog-sk-initial)
13728 (define-key map "j" 'verilog-sk-fork)
13729 (define-key map "m" 'verilog-sk-module)
13730 (define-key map "o" 'verilog-sk-ovm-class)
13731 (define-key map "p" 'verilog-sk-primitive)
13732 (define-key map "r" 'verilog-sk-repeat)
13733 (define-key map "s" 'verilog-sk-specify)
13734 (define-key map "t" 'verilog-sk-task)
13735 (define-key map "u" 'verilog-sk-uvm-object)
13736 (define-key map "w" 'verilog-sk-while)
13737 (define-key map "x" 'verilog-sk-casex)
13738 (define-key map "z" 'verilog-sk-casez)
13739 (define-key map "?" 'verilog-sk-if)
13740 (define-key map ":" 'verilog-sk-else-if)
13741 (define-key map "/" 'verilog-sk-comment)
13742 (define-key map "A" 'verilog-sk-assign)
13743 (define-key map "F" 'verilog-sk-function)
13744 (define-key map "I" 'verilog-sk-input)
13745 (define-key map "O" 'verilog-sk-output)
13746 (define-key map "S" 'verilog-sk-state-machine)
13747 (define-key map "=" 'verilog-sk-inout)
13748 (define-key map "U" 'verilog-sk-uvm-component)
13749 (define-key map "W" 'verilog-sk-wire)
13750 (define-key map "R" 'verilog-sk-reg)
13751 (define-key map "D" 'verilog-sk-define-signal)
13752 map)
13753 "Keymap used in Verilog mode for smart template operations.")
13757 ;; Place the templates into Verilog Mode. They may be inserted under any key.
13758 ;; C-c C-t will be the default. If you use templates a lot, you
13759 ;; may want to consider moving the binding to another key in your init
13760 ;; file.
13762 ;; Note \C-c and letter are reserved for users
13763 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
13765 ;; ---- statement skeletons ------------------------------------------
13767 (define-skeleton verilog-sk-prompt-condition
13768 "Prompt for the loop condition."
13769 "[condition]: " str )
13771 (define-skeleton verilog-sk-prompt-init
13772 "Prompt for the loop init statement."
13773 "[initial statement]: " str )
13775 (define-skeleton verilog-sk-prompt-inc
13776 "Prompt for the loop increment statement."
13777 "[increment statement]: " str )
13779 (define-skeleton verilog-sk-prompt-name
13780 "Prompt for the name of something."
13781 "[name]: " str)
13783 (define-skeleton verilog-sk-prompt-clock
13784 "Prompt for the name of something."
13785 "name and edge of clock(s): " str)
13787 (defvar verilog-sk-reset nil)
13788 (defun verilog-sk-prompt-reset ()
13789 "Prompt for the name of a state machine reset."
13790 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
13793 (define-skeleton verilog-sk-prompt-state-selector
13794 "Prompt for the name of a state machine selector."
13795 "name of selector (eg {a,b,c,d}): " str )
13797 (define-skeleton verilog-sk-prompt-output
13798 "Prompt for the name of something."
13799 "output: " str)
13801 (define-skeleton verilog-sk-prompt-msb
13802 "Prompt for most significant bit specification."
13803 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
13805 (define-skeleton verilog-sk-prompt-lsb
13806 "Prompt for least significant bit specification."
13807 "lsb:" str )
13809 (defvar verilog-sk-p nil)
13810 (define-skeleton verilog-sk-prompt-width
13811 "Prompt for a width specification."
13813 (progn
13814 (setq verilog-sk-p (point))
13815 (verilog-sk-prompt-msb)
13816 (if (> (point) verilog-sk-p) "] " " ")))
13818 (defun verilog-sk-header ()
13819 "Insert a descriptive header at the top of the file.
13820 See also `verilog-header' for an alternative format."
13821 (interactive "*")
13822 (save-excursion
13823 (goto-char (point-min))
13824 (verilog-sk-header-tmpl)))
13826 (define-skeleton verilog-sk-header-tmpl
13827 "Insert a comment block containing the module title, author, etc."
13828 "[Description]: "
13829 "// -*- Mode: Verilog -*-"
13830 "\n// Filename : " (buffer-name)
13831 "\n// Description : " str
13832 "\n// Author : " (user-full-name)
13833 "\n// Created On : " (current-time-string)
13834 "\n// Last Modified By: " (user-full-name)
13835 "\n// Last Modified On: " (current-time-string)
13836 "\n// Update Count : 0"
13837 "\n// Status : Unknown, Use with caution!"
13838 "\n")
13840 (define-skeleton verilog-sk-module
13841 "Insert a module definition."
13843 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
13844 > _ \n
13845 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
13847 ;; ------------------------------------------------------------------------
13848 ;; Define a default OVM class, with macros and new()
13849 ;; ------------------------------------------------------------------------
13851 (define-skeleton verilog-sk-ovm-class
13852 "Insert a class definition"
13854 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13855 > _ \n
13856 > "`ovm_object_utils_begin(" name ")" \n
13857 > (- verilog-indent-level) " `ovm_object_utils_end" \n
13858 > _ \n
13859 > "function new(string name=\"" name "\");" \n
13860 > "super.new(name);" \n
13861 > (- verilog-indent-level) "endfunction" \n
13862 > _ \n
13863 > "endclass" (progn (electric-verilog-terminate-line) nil))
13865 (define-skeleton verilog-sk-uvm-object
13866 "Insert a class definition"
13868 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13869 > _ \n
13870 > "`uvm_object_utils_begin(" name ")" \n
13871 > (- verilog-indent-level) "`uvm_object_utils_end" \n
13872 > _ \n
13873 > "function new(string name=\"" name "\");" \n
13874 > "super.new(name);" \n
13875 > (- verilog-indent-level) "endfunction" \n
13876 > _ \n
13877 > "endclass" (progn (electric-verilog-terminate-line) nil))
13879 (define-skeleton verilog-sk-uvm-component
13880 "Insert a class definition"
13882 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13883 > _ \n
13884 > "`uvm_component_utils_begin(" name ")" \n
13885 > (- verilog-indent-level) "`uvm_component_utils_end" \n
13886 > _ \n
13887 > "function new(string name=\"\", uvm_component parent);" \n
13888 > "super.new(name, parent);" \n
13889 > (- verilog-indent-level) "endfunction" \n
13890 > _ \n
13891 > "endclass" (progn (electric-verilog-terminate-line) nil))
13893 (define-skeleton verilog-sk-primitive
13894 "Insert a task definition."
13896 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
13897 > _ \n
13898 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
13900 (define-skeleton verilog-sk-task
13901 "Insert a task definition."
13903 > "task " '(verilog-sk-prompt-name) & ?; \n
13904 > _ \n
13905 > "begin" \n
13906 > \n
13907 > (- verilog-indent-level-behavioral) "end" \n
13908 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
13910 (define-skeleton verilog-sk-function
13911 "Insert a function definition."
13913 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
13914 > _ \n
13915 > "begin" \n
13916 > \n
13917 > (- verilog-indent-level-behavioral) "end" \n
13918 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
13920 (define-skeleton verilog-sk-always
13921 "Insert always block. Uses the minibuffer to prompt
13922 for sensitivity list."
13924 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13925 > _ \n
13926 > (- verilog-indent-level-behavioral) "end" \n >
13929 (define-skeleton verilog-sk-initial
13930 "Insert an initial block."
13932 > "initial begin\n"
13933 > _ \n
13934 > (- verilog-indent-level-behavioral) "end" \n > )
13936 (define-skeleton verilog-sk-specify
13937 "Insert specify block. "
13939 > "specify\n"
13940 > _ \n
13941 > (- verilog-indent-level-behavioral) "endspecify" \n > )
13943 (define-skeleton verilog-sk-generate
13944 "Insert generate block. "
13946 > "generate\n"
13947 > _ \n
13948 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
13950 (define-skeleton verilog-sk-begin
13951 "Insert begin end block. Uses the minibuffer to prompt for name."
13953 > "begin" '(verilog-sk-prompt-name) \n
13954 > _ \n
13955 > (- verilog-indent-level-behavioral) "end" )
13957 (define-skeleton verilog-sk-fork
13958 "Insert a fork join block."
13960 > "fork\n"
13961 > "begin" \n
13962 > _ \n
13963 > (- verilog-indent-level-behavioral) "end" \n
13964 > "begin" \n
13965 > \n
13966 > (- verilog-indent-level-behavioral) "end" \n
13967 > (- verilog-indent-level-behavioral) "join" \n
13971 (define-skeleton verilog-sk-case
13972 "Build skeleton case statement, prompting for the selector expression,
13973 and the case items."
13974 "[selector expression]: "
13975 > "case (" str ") " \n
13976 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13977 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13979 (define-skeleton verilog-sk-casex
13980 "Build skeleton casex statement, prompting for the selector expression,
13981 and the case items."
13982 "[selector expression]: "
13983 > "casex (" str ") " \n
13984 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13985 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13987 (define-skeleton verilog-sk-casez
13988 "Build skeleton casez statement, prompting for the selector expression,
13989 and the case items."
13990 "[selector expression]: "
13991 > "casez (" str ") " \n
13992 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13993 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13995 (define-skeleton verilog-sk-if
13996 "Insert a skeleton if statement."
13997 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
13998 > _ \n
13999 > (- verilog-indent-level-behavioral) "end " \n )
14001 (define-skeleton verilog-sk-else-if
14002 "Insert a skeleton else if statement."
14003 > (verilog-indent-line) "else if ("
14004 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
14005 > _ \n
14006 > "end" (progn (electric-verilog-terminate-line) nil))
14008 (define-skeleton verilog-sk-datadef
14009 "Common routine to get data definition."
14011 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
14013 (define-skeleton verilog-sk-input
14014 "Insert an input definition."
14016 > "input [" '(verilog-sk-datadef))
14018 (define-skeleton verilog-sk-output
14019 "Insert an output definition."
14021 > "output [" '(verilog-sk-datadef))
14023 (define-skeleton verilog-sk-inout
14024 "Insert an inout definition."
14026 > "inout [" '(verilog-sk-datadef))
14028 (defvar verilog-sk-signal nil)
14029 (define-skeleton verilog-sk-def-reg
14030 "Insert a reg definition."
14032 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
14034 (defun verilog-sk-define-signal ()
14035 "Insert a definition of signal under point at top of module."
14036 (interactive "*")
14037 (let* ((sig-re "[a-zA-Z0-9_]*")
14038 (v1 (buffer-substring
14039 (save-excursion
14040 (skip-chars-backward sig-re)
14041 (point))
14042 (save-excursion
14043 (skip-chars-forward sig-re)
14044 (point)))))
14045 (if (not (member v1 verilog-keywords))
14046 (save-excursion
14047 (setq verilog-sk-signal v1)
14048 (verilog-beg-of-defun)
14049 (verilog-end-of-statement)
14050 (verilog-forward-syntactic-ws)
14051 (verilog-sk-def-reg)
14052 (message "signal at point is %s" v1))
14053 (message "object at point (%s) is a keyword" v1))))
14055 (define-skeleton verilog-sk-wire
14056 "Insert a wire definition."
14058 > "wire [" '(verilog-sk-datadef))
14060 (define-skeleton verilog-sk-reg
14061 "Insert a reg definition."
14063 > "reg [" '(verilog-sk-datadef))
14065 (define-skeleton verilog-sk-assign
14066 "Insert a skeleton assign statement."
14068 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
14070 (define-skeleton verilog-sk-while
14071 "Insert a skeleton while loop statement."
14073 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
14074 > _ \n
14075 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
14077 (define-skeleton verilog-sk-repeat
14078 "Insert a skeleton repeat loop statement."
14080 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
14081 > _ \n
14082 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
14084 (define-skeleton verilog-sk-for
14085 "Insert a skeleton while loop statement."
14087 > "for ("
14088 '(verilog-sk-prompt-init) "; "
14089 '(verilog-sk-prompt-condition) "; "
14090 '(verilog-sk-prompt-inc)
14091 ") begin" \n
14092 > _ \n
14093 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
14095 (define-skeleton verilog-sk-comment
14096 "Inserts three comment lines, making a display comment."
14098 > "/*\n"
14099 > "* " _ \n
14100 > "*/")
14102 (define-skeleton verilog-sk-state-machine
14103 "Insert a state machine definition."
14104 "Name of state variable: "
14105 '(setq input "state")
14106 > "// State registers for " str | -23 \n
14107 '(setq verilog-sk-state str)
14108 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
14109 '(setq input nil)
14110 > \n
14111 > "// State FF for " verilog-sk-state \n
14112 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
14113 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
14114 > verilog-sk-state " = next_" verilog-sk-state ?; \n
14115 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
14116 > \n
14117 > "// Next State Logic for " verilog-sk-state \n
14118 > "always @ ( /*AUTOSENSE*/ ) begin\n"
14119 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
14120 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
14121 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
14122 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
14124 ;;; Mouse Events:
14126 ;; Include file loading with mouse/return event
14128 ;; idea & first impl.: M. Rouat (eldo-mode.el)
14129 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
14131 (if (featurep 'xemacs)
14132 (require 'overlay))
14134 (defconst verilog-include-file-regexp
14135 "^`include\\s-+\"\\([^\n\"]*\\)\""
14136 "Regexp that matches the include file.")
14138 (defvar verilog-mode-mouse-map
14139 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
14140 (set-keymap-parent map verilog-mode-map)
14141 ;; mouse button bindings
14142 (define-key map "\r" 'verilog-load-file-at-point)
14143 (if (featurep 'xemacs)
14144 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
14145 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
14146 (if (featurep 'xemacs)
14147 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
14148 (define-key map [S-mouse-2] 'mouse-yank-at-click))
14149 map)
14150 "Map containing mouse bindings for `verilog-mode'.")
14153 (defun verilog-highlight-region (beg end _old-len)
14154 "Colorize included files and modules in the (changed?) region.
14155 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
14156 (when (or verilog-highlight-includes
14157 verilog-highlight-modules)
14158 (save-excursion
14159 (save-match-data ; A query-replace may call this function - do not disturb
14160 (verilog-save-buffer-state
14161 (verilog-save-scan-cache
14162 (let (end-point)
14163 (goto-char end)
14164 (setq end-point (point-at-eol))
14165 (goto-char beg)
14166 (beginning-of-line) ; scan entire line
14167 ;; delete overlays existing on this line
14168 (let ((overlays (overlays-in (point) end-point)))
14169 (while overlays
14170 (if (and
14171 (overlay-get (car overlays) 'detachable)
14172 (or (overlay-get (car overlays) 'verilog-include-file)
14173 (overlay-get (car overlays) 'verilog-inst-module)))
14174 (delete-overlay (car overlays)))
14175 (setq overlays (cdr overlays))))
14177 ;; make new include overlays
14178 (when verilog-highlight-includes
14179 (while (search-forward-regexp verilog-include-file-regexp end-point t)
14180 (goto-char (match-beginning 1))
14181 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
14182 (overlay-put ov 'start-closed 't)
14183 (overlay-put ov 'end-closed 't)
14184 (overlay-put ov 'evaporate 't)
14185 (overlay-put ov 'verilog-include-file 't)
14186 (overlay-put ov 'mouse-face 'highlight)
14187 (overlay-put ov 'local-map verilog-mode-mouse-map))))
14189 ;; make new module overlays
14190 (goto-char beg)
14191 ;; This scanner is syntax-fragile, so don't get bent
14192 (when verilog-highlight-modules
14193 (condition-case nil
14194 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
14195 (save-excursion
14196 (goto-char (match-beginning 0))
14197 (unless (verilog-inside-comment-or-string-p)
14198 (verilog-read-inst-module-matcher) ; sets match 0
14199 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
14200 (overlay-put ov 'start-closed 't)
14201 (overlay-put ov 'end-closed 't)
14202 (overlay-put ov 'evaporate 't)
14203 (overlay-put ov 'verilog-inst-module 't)
14204 (overlay-put ov 'mouse-face 'highlight)
14205 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
14206 (error nil)))
14208 ;; Future highlights:
14209 ;; variables - make an Occur buffer of where referenced
14210 ;; pins - make an Occur buffer of the sig in the declaration module
14211 )))))))
14213 (defun verilog-highlight-buffer ()
14214 "Colorize included files and modules across the whole buffer."
14215 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
14216 (interactive)
14217 ;; delete and remake overlays
14218 (verilog-highlight-region (point-min) (point-max) nil))
14220 ;; Deprecated, but was interactive, so we'll keep it around
14221 (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
14223 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
14224 ;; so define this function to do more or less the same as ffap-at-mouse
14225 ;; but first resolve filename...
14226 (defun verilog-load-file-at-mouse (event)
14227 "Load file under button 2 click's EVENT.
14228 Files are checked based on `verilog-library-flags'."
14229 (interactive "@e")
14230 (save-excursion ; implement a Verilog specific ffap-at-mouse
14231 (mouse-set-point event)
14232 (verilog-load-file-at-point t)))
14234 ;; ffap isn't usable for Verilog mode. It uses library paths.
14235 ;; so define this function to do more or less the same as ffap
14236 ;; but first resolve filename...
14237 (defun verilog-load-file-at-point (&optional warn)
14238 "Load file under point.
14239 If WARN, throw warning if not found.
14240 Files are checked based on `verilog-library-flags'."
14241 (interactive)
14242 (save-excursion ; implement a Verilog specific ffap
14243 (let ((overlays (overlays-in (point) (point)))
14244 hit)
14245 (while (and overlays (not hit))
14246 (when (overlay-get (car overlays) 'verilog-inst-module)
14247 (verilog-goto-defun-file (buffer-substring
14248 (overlay-start (car overlays))
14249 (overlay-end (car overlays))))
14250 (setq hit t))
14251 (setq overlays (cdr overlays)))
14252 ;; Include?
14253 (beginning-of-line)
14254 (when (and (not hit)
14255 (looking-at verilog-include-file-regexp))
14256 (if (and (car (verilog-library-filenames
14257 (match-string 1) (buffer-file-name)))
14258 (file-readable-p (car (verilog-library-filenames
14259 (match-string 1) (buffer-file-name)))))
14260 (find-file (car (verilog-library-filenames
14261 (match-string 1) (buffer-file-name))))
14262 (when warn
14263 (message
14264 "File `%s' isn't readable, use shift-mouse2 to paste in this field"
14265 (match-string 1))))))))
14268 ;;; Bug reporting:
14271 (defun verilog-faq ()
14272 "Tell the user their current version, and where to get the FAQ etc."
14273 (interactive)
14274 (with-output-to-temp-buffer "*verilog-mode help*"
14275 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
14276 (princ "\n")
14277 (princ "For new releases, see http://www.verilog.com\n")
14278 (princ "\n")
14279 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
14280 (princ "\n")
14281 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
14282 (princ "\n")))
14284 (autoload 'reporter-submit-bug-report "reporter")
14285 (defvar reporter-prompt-for-summary-p)
14287 (defun verilog-submit-bug-report ()
14288 "Submit via mail a bug report on verilog-mode.el."
14289 (interactive)
14290 (let ((reporter-prompt-for-summary-p t))
14291 (reporter-submit-bug-report
14292 "mac@verilog.com, wsnyder@wsnyder.org"
14293 (concat "verilog-mode v" verilog-mode-version)
14295 verilog-active-low-regexp
14296 verilog-after-save-font-hook
14297 verilog-align-ifelse
14298 verilog-assignment-delay
14299 verilog-auto-arg-sort
14300 verilog-auto-declare-nettype
14301 verilog-auto-delete-trailing-whitespace
14302 verilog-auto-endcomments
14303 verilog-auto-hook
14304 verilog-auto-ignore-concat
14305 verilog-auto-indent-on-newline
14306 verilog-auto-inout-ignore-regexp
14307 verilog-auto-input-ignore-regexp
14308 verilog-auto-inst-column
14309 verilog-auto-inst-dot-name
14310 verilog-auto-inst-interfaced-ports
14311 verilog-auto-inst-param-value
14312 verilog-auto-inst-sort
14313 verilog-auto-inst-template-numbers
14314 verilog-auto-inst-vector
14315 verilog-auto-lineup
14316 verilog-auto-newline
14317 verilog-auto-output-ignore-regexp
14318 verilog-auto-read-includes
14319 verilog-auto-reset-blocking-in-non
14320 verilog-auto-reset-widths
14321 verilog-auto-save-policy
14322 verilog-auto-sense-defines-constant
14323 verilog-auto-sense-include-inputs
14324 verilog-auto-star-expand
14325 verilog-auto-star-save
14326 verilog-auto-template-warn-unused
14327 verilog-auto-tieoff-declaration
14328 verilog-auto-tieoff-ignore-regexp
14329 verilog-auto-unused-ignore-regexp
14330 verilog-auto-wire-type
14331 verilog-before-auto-hook
14332 verilog-before-delete-auto-hook
14333 verilog-before-getopt-flags-hook
14334 verilog-before-save-font-hook
14335 verilog-cache-enabled
14336 verilog-case-fold
14337 verilog-case-indent
14338 verilog-cexp-indent
14339 verilog-compiler
14340 verilog-coverage
14341 verilog-delete-auto-hook
14342 verilog-getopt-flags-hook
14343 verilog-highlight-grouping-keywords
14344 verilog-highlight-includes
14345 verilog-highlight-modules
14346 verilog-highlight-p1800-keywords
14347 verilog-highlight-translate-off
14348 verilog-indent-begin-after-if
14349 verilog-indent-declaration-macros
14350 verilog-indent-level
14351 verilog-indent-level-behavioral
14352 verilog-indent-level-declaration
14353 verilog-indent-level-directive
14354 verilog-indent-level-module
14355 verilog-indent-lists
14356 verilog-library-directories
14357 verilog-library-extensions
14358 verilog-library-files
14359 verilog-library-flags
14360 verilog-linter
14361 verilog-minimum-comment-distance
14362 verilog-mode-hook
14363 verilog-mode-release-emacs
14364 verilog-mode-version
14365 verilog-preprocessor
14366 verilog-simulator
14367 verilog-tab-always-indent
14368 verilog-tab-to-comment
14369 verilog-typedef-regexp
14370 verilog-warn-fatal
14372 nil nil
14373 (concat "Hi Mac,
14375 I want to report a bug.
14377 Before I go further, I want to say that Verilog mode has changed my life.
14378 I save so much time, my files are colored nicely, my co workers respect
14379 my coding ability... until now. I'd really appreciate anything you
14380 could do to help me out with this minor deficiency in the product.
14382 I've taken a look at the Verilog-Mode FAQ at
14383 http://www.veripool.org/verilog-mode-faq.html.
14385 And, I've considered filing the bug on the issue tracker at
14386 http://www.veripool.org/verilog-mode-bugs
14387 since I realize that public bugs are easier for you to track,
14388 and for others to search, but would prefer to email.
14390 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
14391 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
14392 the code included below.
14394 Given those lines, I expected [[Fill in here]] to happen;
14395 but instead, [[Fill in here]] happens!.
14397 == The code: =="))))
14399 (provide 'verilog-mode)
14401 ;; Local Variables:
14402 ;; checkdoc-permit-comma-termination-flag:t
14403 ;; checkdoc-force-docstrings-flag:nil
14404 ;; indent-tabs-mode:nil
14405 ;; End:
14407 ;;; verilog-mode.el ends here