]> code.delx.au - gnu-emacs/blob - lisp/progmodes/verilog-mode.el
eae2f6cb8a68fd35975f2b69d99c1a966d75a7f0
[gnu-emacs] / lisp / progmodes / verilog-mode.el
1 ;;; verilog-mode.el --- major mode for editing verilog source in Emacs
2
3 ;; Copyright (C) 1996-2015 Free Software Foundation, Inc.
4
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
11
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.
16
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!
20 ;;
21 ;;
22
23 ;; This file is part of GNU Emacs.
24
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.
29
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.
34
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/>.
37
38 ;;; Commentary:
39
40 ;; USAGE
41 ;; =====
42
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>
48
49 ;; KNOWN BUGS / BUG REPORTS
50 ;; =======================
51
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
55 ;;
56 ;; http://www.veripool.org/verilog-mode
57 ;;
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.
61
62 ;; INSTALLING THE MODE
63 ;; ===================
64
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.
71
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
74
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
79
80 ;; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ;; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
82
83 ;; Be sure to examine at the help for verilog-auto, and the other
84 ;; verilog-auto-* functions for some major coding time savers.
85 ;;
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.
94 ;;
95 ;; ;; Enable syntax highlighting of **all** languages
96 ;; (global-font-lock-mode t)
97 ;;
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 ;; )
115
116 \f
117 ;;; History:
118 ;;
119 ;; See commit history at http://www.veripool.org/verilog-mode.html
120 ;; (This section is required to appease checkdoc.)
121
122 ;;; Code:
123 ;;
124
125 ;; This variable will always hold the version number of the mode
126 (defconst verilog-mode-version "2015-09-18-314cf1d-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.")
130
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))
135
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)))))
208 )
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))
219 )
220 (if (fboundp 'defface)
221 nil ; great!
222 (defmacro defface (var values doc &rest _args)
223 `(make-face ,var))
224 )
225
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))
231 )
232
233 (unless (boundp 'inhibit-point-motion-hooks)
234 (defvar inhibit-point-motion-hooks nil))
235 (unless (boundp 'deactivate-mark)
236 (defvar deactivate-mark nil))
237 )
238 ;;
239 ;; OK, do this stuff if we are NOT XEmacs:
240 (unless (featurep 'xemacs)
241 (unless (fboundp 'region-active-p)
242 (defmacro region-active-p ()
243 `(and transient-mark-mode mark-active))))
244 )
245
246 ;; Provide a regular expression optimization routine, using regexp-opt
247 ;; if provided by the user's elisp libraries
248 (eval-and-compile
249 ;; The below were disabled when GNU Emacs 22 was released;
250 ;; perhaps some still need to be there to support Emacs 21.
251 (if (featurep 'xemacs)
252 (if (fboundp 'regexp-opt)
253 ;; regexp-opt is defined, does it take 3 or 2 arguments?
254 (if (fboundp 'function-max-args)
255 (let ((args (function-max-args `regexp-opt)))
256 (cond
257 ((eq args 3) ; It takes 3
258 (condition-case nil ; Hide this defun from emacses
259 ; with just a two input regexp
260 (defun verilog-regexp-opt (a b)
261 "Deal with differing number of required arguments for `regexp-opt'.
262 Call `regexp-opt' on A and B."
263 (regexp-opt a b t))
264 (error nil))
265 )
266 ((eq args 2) ; It takes 2
267 (defun verilog-regexp-opt (a b)
268 "Call `regexp-opt' on A and B."
269 (regexp-opt a b))
270 )
271 (t nil)))
272 ;; We can't tell; assume it takes 2
273 (defun verilog-regexp-opt (a b)
274 "Call `regexp-opt' on A and B."
275 (regexp-opt a b))
276 )
277 ;; There is no regexp-opt, provide our own
278 (defun verilog-regexp-opt (strings &optional paren _shy)
279 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
280 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
281 )
282 ;; Emacs.
283 (defalias 'verilog-regexp-opt 'regexp-opt)))
284
285 ;; emacs >=22 has looking-back, but older emacs and xemacs don't.
286 ;; This function is lifted directly from emacs's subr.el
287 ;; so that it can be used by xemacs.
288 ;; The idea for this was borrowed from org-mode via this link:
289 ;; https://lists.gnu.org/archive/html/emacs-orgmode/2009-12/msg00032.html
290 (eval-and-compile
291 (cond
292 ((fboundp 'looking-back)
293 (defalias 'verilog-looking-back 'looking-back))
294 (t
295 (defun verilog-looking-back (regexp limit &optional greedy)
296 "Return non-nil if text before point matches regular expression REGEXP.
297 Like `looking-at' except matches before point, and is slower.
298 LIMIT if non-nil speeds up the search by specifying a minimum
299 starting position, to avoid checking matches that would start
300 before LIMIT.
301
302 If GREEDY is non-nil, extend the match backwards as far as
303 possible, stopping when a single additional previous character
304 cannot be part of a match for REGEXP. When the match is
305 extended, its starting position is allowed to occur before
306 LIMIT.
307
308 As a general recommendation, try to avoid using `looking-back'
309 wherever possible, since it is slow."
310 (let ((start (point))
311 (pos
312 (save-excursion
313 (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
314 (point)))))
315 (if (and greedy pos)
316 (save-restriction
317 (narrow-to-region (point-min) start)
318 (while (and (> pos (point-min))
319 (save-excursion
320 (goto-char pos)
321 (backward-char 1)
322 (looking-at (concat "\\(?:" regexp "\\)\\'"))))
323 (setq pos (1- pos)))
324 (save-excursion
325 (goto-char pos)
326 (looking-at (concat "\\(?:" regexp "\\)\\'")))))
327 (not (null pos)))))))
328
329 (eval-and-compile
330 ;; Both xemacs and emacs
331 (condition-case nil
332 (require 'diff) ; diff-command and diff-switches
333 (error nil))
334 (condition-case nil
335 (require 'compile) ; compilation-error-regexp-alist-alist
336 (error nil))
337 (condition-case nil
338 (unless (fboundp 'buffer-chars-modified-tick) ; Emacs 22 added
339 (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
340 (error nil))
341 ;; Added in Emacs 24.1
342 (condition-case nil
343 (unless (fboundp 'prog-mode)
344 (define-derived-mode prog-mode fundamental-mode "Prog"))
345 (error nil)))
346
347 (eval-when-compile
348 (defun verilog-regexp-words (a)
349 "Call `regexp-opt' with word delimiters for the words A."
350 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
351 (defun verilog-regexp-words (a)
352 "Call `regexp-opt' with word delimiters for the words A."
353 ;; The FAQ references this function, so user LISP sometimes calls it
354 (concat "\\<" (verilog-regexp-opt a t) "\\>"))
355
356 (defun verilog-easy-menu-filter (menu)
357 "Filter `easy-menu-define' MENU to support new features."
358 (cond ((not (featurep 'xemacs))
359 menu) ; GNU Emacs - passthru
360 ;; XEmacs doesn't support :help. Strip it.
361 ;; Recursively filter the a submenu
362 ((listp menu)
363 (mapcar 'verilog-easy-menu-filter menu))
364 ;; Look for [:help "blah"] and remove
365 ((vectorp menu)
366 (let ((i 0) (out []))
367 (while (< i (length menu))
368 (if (equal `:help (aref menu i))
369 (setq i (+ 2 i))
370 (setq out (vconcat out (vector (aref menu i)))
371 i (1+ i))))
372 out))
373 (t menu))) ; Default - ok
374 ;;(verilog-easy-menu-filter
375 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
376 ;; "----" ["MB" nil :help "Help MB"]))
377
378 (defun verilog-define-abbrev (table name expansion &optional hook)
379 "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
380 Provides SYSTEM-FLAG in newer Emacs."
381 (condition-case nil
382 (define-abbrev table name expansion hook 0 t)
383 (error
384 (define-abbrev table name expansion hook))))
385
386 (defun verilog-customize ()
387 "Customize variables and other settings used by Verilog-Mode."
388 (interactive)
389 (customize-group 'verilog-mode))
390
391 (defun verilog-font-customize ()
392 "Customize fonts used by Verilog-Mode."
393 (interactive)
394 (if (fboundp 'customize-apropos)
395 (customize-apropos "font-lock-*" 'faces)))
396
397 (defun verilog-booleanp (value)
398 "Return t if VALUE is boolean.
399 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
400 This function may be removed when Emacs 21 is no longer supported."
401 (or (equal value t) (equal value nil)))
402
403 (defun verilog-insert-last-command-event ()
404 "Insert the `last-command-event'."
405 (insert (if (featurep 'xemacs)
406 ;; XEmacs 21.5 doesn't like last-command-event
407 last-command-char
408 ;; And GNU Emacs 22 has obsoleted last-command-char
409 last-command-event)))
410
411 (defvar verilog-no-change-functions nil
412 "True if `after-change-functions' is disabled.
413 Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
414
415 (defvar verilog-in-hooks nil
416 "True when within a `verilog-run-hooks' block.")
417
418 (defmacro verilog-run-hooks (&rest hooks)
419 "Run each hook in HOOKS using `run-hooks'.
420 Set `verilog-in-hooks' during this time, to assist AUTO caches."
421 `(let ((verilog-in-hooks t))
422 (run-hooks ,@hooks)))
423
424 (defun verilog-syntax-ppss (&optional pos)
425 (when verilog-no-change-functions
426 (if verilog-in-hooks
427 (verilog-scan-cache-flush)
428 ;; else don't let the AUTO code itself get away with flushing the cache,
429 ;; as that'll make things very slow
430 (backtrace)
431 (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
432 (verilog-point-text))))
433 (if (fboundp 'syntax-ppss)
434 (syntax-ppss pos)
435 (parse-partial-sexp (point-min) (or pos (point)))))
436
437 (defgroup verilog-mode nil
438 "Major mode for Verilog source code."
439 :version "22.2"
440 :group 'languages)
441
442 ;; (defgroup verilog-mode-fonts nil
443 ;; "Facilitates easy customization fonts used in Verilog source text"
444 ;; :link '(customize-apropos "font-lock-*" 'faces)
445 ;; :group 'verilog-mode)
446
447 (defgroup verilog-mode-indent nil
448 "Customize indentation and highlighting of Verilog source text."
449 :group 'verilog-mode)
450
451 (defgroup verilog-mode-actions nil
452 "Customize actions on Verilog source text."
453 :group 'verilog-mode)
454
455 (defgroup verilog-mode-auto nil
456 "Customize AUTO actions when expanding Verilog source text."
457 :group 'verilog-mode)
458
459 (defvar verilog-debug nil
460 "Non-nil means enable debug messages for `verilog-mode' internals.")
461
462 (defvar verilog-warn-fatal nil
463 "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
464
465 (defcustom verilog-linter
466 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
467 "Unix program and arguments to call to run a lint checker on Verilog source.
468 Depending on the `verilog-set-compile-command', this may be invoked when
469 you type \\[compile]. When the compile completes, \\[next-error] will take
470 you to the next lint error."
471 :type 'string
472 :group 'verilog-mode-actions)
473 ;; We don't mark it safe, as it's used as a shell command
474
475 (defcustom verilog-coverage
476 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
477 "Program and arguments to use to annotate for coverage Verilog source.
478 Depending on the `verilog-set-compile-command', this may be invoked when
479 you type \\[compile]. When the compile completes, \\[next-error] will take
480 you to the next lint error."
481 :type 'string
482 :group 'verilog-mode-actions)
483 ;; We don't mark it safe, as it's used as a shell command
484
485 (defcustom verilog-simulator
486 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
487 "Program and arguments to use to interpret Verilog source.
488 Depending on the `verilog-set-compile-command', this may be invoked when
489 you type \\[compile]. When the compile completes, \\[next-error] will take
490 you to the next lint error."
491 :type 'string
492 :group 'verilog-mode-actions)
493 ;; We don't mark it safe, as it's used as a shell command
494
495 (defcustom verilog-compiler
496 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
497 "Program and arguments to use to compile Verilog source.
498 Depending on the `verilog-set-compile-command', this may be invoked when
499 you type \\[compile]. When the compile completes, \\[next-error] will take
500 you to the next lint error."
501 :type 'string
502 :group 'verilog-mode-actions)
503 ;; We don't mark it safe, as it's used as a shell command
504
505 (defcustom verilog-preprocessor
506 ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
507 "vppreproc __FLAGS__ __FILE__"
508 "Program and arguments to use to preprocess Verilog source.
509 This is invoked with `verilog-preprocess', and depending on the
510 `verilog-set-compile-command', may also be invoked when you type
511 \\[compile]. When the compile completes, \\[next-error] will
512 take 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
516
517 (defvar verilog-preprocess-history nil
518 "History for `verilog-preprocess'.")
519
520 (defvar verilog-tool 'verilog-linter
521 "Which tool to use for building compiler-command.
522 Either nil, `verilog-linter', `verilog-compiler',
523 `verilog-coverage', `verilog-preprocessor', or `verilog-simulator'.
524 Alternatively use the \"Choose Compilation Action\" menu. See
525 `verilog-set-compile-command' for more information.")
526
527 (defcustom verilog-highlight-translate-off nil
528 "Non-nil means background-highlight code excluded from translation.
529 That is, all code between \"// synopsys translate_off\" and
530 \"// synopsys translate_on\" is highlighted using a different background color
531 \(face `verilog-font-lock-translate-off-face').
532
533 Note: This will slow down on-the-fly fontification (and thus editing).
534
535 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
536 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
537 :type 'boolean
538 :group 'verilog-mode-indent)
539 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
540 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
541
542 (defcustom verilog-auto-lineup 'declarations
543 "Type of statements to lineup across multiple lines.
544 If `all' is selected, then all line ups described below are done.
545
546 If `declarations', then just declarations are lined up with any
547 preceding declarations, taking into account widths and the like,
548 so or example the code:
549 reg [31:0] a;
550 reg b;
551 would become
552 reg [31:0] a;
553 reg b;
554
555 If `assignment', then assignments are lined up with any preceding
556 assignments, so for example the code
557 a_long_variable <= b + c;
558 d = e + f;
559 would become
560 a_long_variable <= b + c;
561 d = e + f;
562
563 In order to speed up editing, large blocks of statements are lined up
564 only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
565 are lineup only when \\[verilog-pretty-declarations] is typed."
566
567 :type '(radio (const :tag "Line up Assignments and Declarations" all)
568 (const :tag "Line up Assignment statements" assignments )
569 (const :tag "Line up Declarations" declarations)
570 (function :tag "Other"))
571 :group 'verilog-mode-indent )
572 (put 'verilog-auto-lineup 'safe-local-variable
573 '(lambda (x) (memq x '(nil all assignments declarations))))
574
575 (defcustom verilog-indent-level 3
576 "Indentation of Verilog statements with respect to containing block."
577 :group 'verilog-mode-indent
578 :type 'integer)
579 (put 'verilog-indent-level 'safe-local-variable 'integerp)
580
581 (defcustom verilog-indent-level-module 3
582 "Indentation of Module level Verilog statements (eg always, initial).
583 Set to 0 to get initial and always statements lined up on the left side of
584 your screen."
585 :group 'verilog-mode-indent
586 :type 'integer)
587 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
588
589 (defcustom verilog-indent-level-declaration 3
590 "Indentation of declarations with respect to containing block.
591 Set to 0 to get them list right under containing block."
592 :group 'verilog-mode-indent
593 :type 'integer)
594 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
595
596 (defcustom verilog-indent-declaration-macros nil
597 "How to treat macro expansions in a declaration.
598 If nil, indent as:
599 input [31:0] a;
600 input \\=`CP;
601 output c;
602 If non nil, treat as:
603 input [31:0] a;
604 input \\=`CP ;
605 output c;"
606 :group 'verilog-mode-indent
607 :type 'boolean)
608 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
609
610 (defcustom verilog-indent-lists t
611 "How to treat indenting items in a list.
612 If t (the default), indent as:
613 always @( posedge a or
614 reset ) begin
615
616 If nil, treat as:
617 always @( posedge a or
618 reset ) begin"
619 :group 'verilog-mode-indent
620 :type 'boolean)
621 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
622
623 (defcustom verilog-indent-level-behavioral 3
624 "Absolute indentation of first begin in a task or function block.
625 Set to 0 to get such code to start at the left side of the screen."
626 :group 'verilog-mode-indent
627 :type 'integer)
628 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
629
630 (defcustom verilog-indent-level-directive 1
631 "Indentation to add to each level of \\=`ifdef declarations.
632 Set to 0 to have all directives start at the left side of the screen."
633 :group 'verilog-mode-indent
634 :type 'integer)
635 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
636
637 (defcustom verilog-cexp-indent 2
638 "Indentation of Verilog statements split across lines."
639 :group 'verilog-mode-indent
640 :type 'integer)
641 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
642
643 (defcustom verilog-case-indent 2
644 "Indentation for case statements."
645 :group 'verilog-mode-indent
646 :type 'integer)
647 (put 'verilog-case-indent 'safe-local-variable 'integerp)
648
649 (defcustom verilog-auto-newline t
650 "Non-nil means automatically newline after semicolons."
651 :group 'verilog-mode-indent
652 :type 'boolean)
653 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
654
655 (defcustom verilog-auto-indent-on-newline t
656 "Non-nil means automatically indent line after newline."
657 :group 'verilog-mode-indent
658 :type 'boolean)
659 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
660
661 (defcustom verilog-tab-always-indent t
662 "Non-nil means TAB should always re-indent the current line.
663 A nil value means TAB will only reindent when at the beginning of the line."
664 :group 'verilog-mode-indent
665 :type 'boolean)
666 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
667
668 (defcustom verilog-tab-to-comment nil
669 "Non-nil means TAB moves to the right hand column in preparation for a comment."
670 :group 'verilog-mode-actions
671 :type 'boolean)
672 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
673
674 (defcustom verilog-indent-begin-after-if t
675 "Non-nil means indent begin statements following if, else, while, etc.
676 Otherwise, line them up."
677 :group 'verilog-mode-indent
678 :type 'boolean)
679 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
680
681 (defcustom verilog-align-ifelse nil
682 "Non-nil means align `else' under matching `if'.
683 Otherwise else is lined up with first character on line holding matching if."
684 :group 'verilog-mode-indent
685 :type 'boolean)
686 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
687
688 (defcustom verilog-minimum-comment-distance 10
689 "Minimum distance (in lines) between begin and end required before a comment.
690 Setting this variable to zero results in every end acquiring a comment; the
691 default avoids too many redundant comments in tight quarters."
692 :group 'verilog-mode-indent
693 :type 'integer)
694 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
695
696 (defcustom verilog-highlight-p1800-keywords nil
697 "Non-nil means highlight words newly reserved by IEEE-1800.
698 These will appear in `verilog-font-lock-p1800-face' in order to gently
699 suggest changing where these words are used as variables to something else.
700 A nil value means highlight these words as appropriate for the SystemVerilog
701 IEEE-1800 standard. Note that changing this will require restarting Emacs
702 to see the effect as font color choices are cached by Emacs."
703 :group 'verilog-mode-indent
704 :type 'boolean)
705 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
706
707 (defcustom verilog-highlight-grouping-keywords nil
708 "Non-nil means highlight grouping keywords more dramatically.
709 If false, these words are in the `font-lock-type-face'; if True
710 then they are in `verilog-font-lock-grouping-keywords-face'.
711 Some find that special highlighting on these grouping constructs
712 allow the structure of the code to be understood at a glance."
713 :group 'verilog-mode-indent
714 :type 'boolean)
715 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
716
717 (defcustom verilog-highlight-modules nil
718 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
719 When true, mousing over module names will allow jumping to the
720 module definition. If false, this is not supported. Setting
721 this is experimental, and may lead to bad performance."
722 :group 'verilog-mode-indent
723 :type 'boolean)
724 (put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
725
726 (defcustom verilog-highlight-includes t
727 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
728 When true, mousing over include file names will allow jumping to the
729 file referenced. If false, this is not supported."
730 :group 'verilog-mode-indent
731 :type 'boolean)
732 (put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
733
734 (defcustom verilog-auto-declare-nettype nil
735 "Non-nil specifies the data type to use with `verilog-auto-input' etc.
736 Set this to \"wire\" if the Verilog code uses \"\\=`default_nettype
737 none\". Note using \\=`default_nettype none isn't recommended practice; this
738 mode is experimental."
739 :version "24.1" ; rev670
740 :group 'verilog-mode-actions
741 :type 'boolean)
742 (put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
743
744 (defcustom verilog-auto-wire-type nil
745 "Non-nil specifies the data type to use with `verilog-auto-wire' etc.
746 Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
747 :version "24.1" ; rev673
748 :group 'verilog-mode-actions
749 :type 'boolean)
750 (put 'verilog-auto-wire-type 'safe-local-variable `stringp)
751
752 (defcustom verilog-auto-endcomments t
753 "Non-nil means insert a comment /* ... */ after `end's.
754 The name of the function or case will be set between the braces."
755 :group 'verilog-mode-actions
756 :type 'boolean)
757 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
758
759 (defcustom verilog-auto-delete-trailing-whitespace nil
760 "Non-nil means to `delete-trailing-whitespace' in `verilog-auto'."
761 :version "24.1" ; rev703
762 :group 'verilog-mode-actions
763 :type 'boolean)
764 (put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
765
766 (defcustom verilog-auto-ignore-concat nil
767 "Non-nil means ignore signals in {...} concatenations for AUTOWIRE etc.
768 This will exclude signals referenced as pin connections in {...}
769 from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
770 for backward compatibility only and not set in new designs; it
771 may be removed in future versions."
772 :group 'verilog-mode-actions
773 :type 'boolean)
774 (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
775
776 (defcustom verilog-auto-read-includes nil
777 "Non-nil means to automatically read includes before AUTOs.
778 This will do a `verilog-read-defines' and `verilog-read-includes' before
779 each AUTO expansion. This makes it easier to embed defines and includes,
780 but can result in very slow reading times if there are many or large
781 include files."
782 :group 'verilog-mode-actions
783 :type 'boolean)
784 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
785
786 (defcustom verilog-auto-save-policy nil
787 "Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
788 A value of `force' will always do a \\[verilog-auto] automatically if
789 needed on every save. A value of `detect' will do \\[verilog-auto]
790 automatically when it thinks necessary. A value of `ask' will query the
791 user when it thinks updating is needed.
792
793 You should not rely on the `ask' or `detect' policies, they are safeguards
794 only. They do not detect when AUTOINSTs need to be updated because a
795 sub-module's port list has changed."
796 :group 'verilog-mode-actions
797 :type '(choice (const nil) (const ask) (const detect) (const force)))
798
799 (defcustom verilog-auto-star-expand t
800 "Non-nil means to expand SystemVerilog .* instance ports.
801 They will be expanded in the same way as if there was an AUTOINST in the
802 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
803 :group 'verilog-mode-actions
804 :type 'boolean)
805 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
806
807 (defcustom verilog-auto-star-save nil
808 "Non-nil means save to disk SystemVerilog .* instance expansions.
809 A nil value indicates direct connections will be removed before saving.
810 Only meaningful to those created due to `verilog-auto-star-expand' being set.
811
812 Instead of setting this, you may want to use /*AUTOINST*/, which will
813 always be saved."
814 :group 'verilog-mode-actions
815 :type 'boolean)
816 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
817
818 (defvar verilog-auto-update-tick nil
819 "Modification tick at which autos were last performed.")
820
821 (defvar verilog-auto-last-file-locals nil
822 "Text from file-local-variables during last evaluation.")
823
824 (defvar verilog-diff-function 'verilog-diff-report
825 "Function to run when `verilog-diff-auto' detects differences.
826 Function takes three arguments, the original buffer, the
827 difference buffer, and the point in original buffer with the
828 first difference.")
829
830 ;;; Compile support:
831 ;;
832
833 (require 'compile)
834
835 (defvar verilog-error-regexp-added nil)
836
837 (defvar verilog-error-regexp-emacs-alist
838 '(
839 (verilog-xl-1
840 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
841 (verilog-xl-2
842 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
843 (verilog-IES
844 ".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
845 (verilog-surefire-1
846 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
847 (verilog-surefire-2
848 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
849 (verilog-verbose
850 "\
851 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
852 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
853 (verilog-xsim
854 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
855 (verilog-vcs-1
856 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
857 (verilog-vcs-2
858 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
859 (verilog-vcs-3
860 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
861 (verilog-vcs-4
862 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
863 (verilog-verilator
864 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
865 (verilog-leda
866 "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
867 )
868 "List of regexps for Verilog compilers.
869 See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
870
871 (defvar verilog-error-regexp-xemacs-alist
872 ;; Emacs form is '((v-tool "re" 1 2) ...)
873 ;; XEmacs form is '(verilog ("re" 1 2) ...)
874 ;; So we can just map from Emacs to XEmacs
875 (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
876 "List of regexps for Verilog compilers.
877 See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
878
879 (defvar verilog-error-font-lock-keywords
880 '(
881 ;; verilog-xl-1
882 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
883 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
884 ;; verilog-xl-2
885 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
886 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
887 ;; verilog-IES (nc-verilog)
888 (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
889 (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
890 ;; verilog-surefire-1
891 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
892 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
893 ;; verilog-surefire-2
894 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
895 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
896 ;; verilog-verbose
897 ("\
898 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
899 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
900 ("\
901 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
902 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
903 ;; verilog-vcs-1
904 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
905 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
906 ;; verilog-vcs-2
907 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
908 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
909 ;; verilog-vcs-3
910 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
911 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
912 ;; verilog-vcs-4
913 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
914 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
915 ;; verilog-verilator
916 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
917 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
918 ;; verilog-leda
919 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
920 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
921 )
922 "Keywords to also highlight in Verilog *compilation* buffers.
923 Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
924
925 (defcustom verilog-library-flags '("")
926 "List of standard Verilog arguments to use for /*AUTOINST*/.
927 These arguments are used to find files for `verilog-auto', and match
928 the flags accepted by a standard Verilog-XL simulator.
929
930 -f filename Reads more `verilog-library-flags' from the filename.
931 +incdir+dir Adds the directory to `verilog-library-directories'.
932 -Idir Adds the directory to `verilog-library-directories'.
933 -y dir Adds the directory to `verilog-library-directories'.
934 +libext+.v Adds the extensions to `verilog-library-extensions'.
935 -v filename Adds the filename to `verilog-library-files'.
936
937 filename Adds the filename to `verilog-library-files'.
938 This is not recommended, -v is a better choice.
939
940 You might want these defined in each file; put at the *END* of your file
941 something like:
942
943 // Local Variables:
944 // verilog-library-flags:(\"-y dir -y otherdir\")
945 // End:
946
947 Verilog-mode attempts to detect changes to this local variable, but they
948 are only insured to be correct when the file is first visited. Thus if you
949 have problems, use \\[find-alternate-file] RET to have these take effect.
950
951 See also the variables mentioned above."
952 :group 'verilog-mode-auto
953 :type '(repeat string))
954 (put 'verilog-library-flags 'safe-local-variable 'listp)
955
956 (defcustom verilog-library-directories '(".")
957 "List of directories when looking for files for /*AUTOINST*/.
958 The directory may be relative to the current file, or absolute.
959 Environment variables are also expanded in the directory names.
960 Having at least the current directory is a good idea.
961
962 You might want these defined in each file; put at the *END* of your file
963 something like:
964
965 // Local Variables:
966 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
967 // End:
968
969 Verilog-mode attempts to detect changes to this local variable, but they
970 are only insured to be correct when the file is first visited. Thus if you
971 have problems, use \\[find-alternate-file] RET to have these take effect.
972
973 See also `verilog-library-flags', `verilog-library-files'
974 and `verilog-library-extensions'."
975 :group 'verilog-mode-auto
976 :type '(repeat file))
977 (put 'verilog-library-directories 'safe-local-variable 'listp)
978
979 (defcustom verilog-library-files '()
980 "List of files to search for modules.
981 AUTOINST will use this when it needs to resolve a module name.
982 This is a complete path, usually to a technology file with many standard
983 cells defined in it.
984
985 You might want these defined in each file; put at the *END* of your file
986 something like:
987
988 // Local Variables:
989 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
990 // End:
991
992 Verilog-mode attempts to detect changes to this local variable, but they
993 are only insured to be correct when the file is first visited. Thus if you
994 have problems, use \\[find-alternate-file] RET to have these take effect.
995
996 See also `verilog-library-flags', `verilog-library-directories'."
997 :group 'verilog-mode-auto
998 :type '(repeat directory))
999 (put 'verilog-library-files 'safe-local-variable 'listp)
1000
1001 (defcustom verilog-library-extensions '(".v" ".sv")
1002 "List of extensions to use when looking for files for /*AUTOINST*/.
1003 See also `verilog-library-flags', `verilog-library-directories'."
1004 :type '(repeat string)
1005 :group 'verilog-mode-auto)
1006 (put 'verilog-library-extensions 'safe-local-variable 'listp)
1007
1008 (defcustom verilog-active-low-regexp nil
1009 "If true, treat signals matching this regexp as active low.
1010 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
1011 you will probably also need `verilog-auto-reset-widths' set."
1012 :group 'verilog-mode-auto
1013 :type '(choice (const nil) regexp))
1014 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
1015
1016 (defcustom verilog-auto-sense-include-inputs nil
1017 "Non-nil means AUTOSENSE should include all inputs.
1018 If nil, only inputs that are NOT output signals in the same block are
1019 included."
1020 :group 'verilog-mode-auto
1021 :type 'boolean)
1022 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
1023
1024 (defcustom verilog-auto-sense-defines-constant nil
1025 "Non-nil means AUTOSENSE should assume all defines represent constants.
1026 When true, the defines will not be included in sensitivity lists. To
1027 maintain compatibility with other sites, this should be set at the bottom
1028 of each Verilog file that requires it, rather than being set globally."
1029 :group 'verilog-mode-auto
1030 :type 'boolean)
1031 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
1032
1033 (defcustom verilog-auto-reset-blocking-in-non t
1034 "Non-nil means AUTORESET will reset blocking statements.
1035 When true, AUTORESET will reset in blocking statements those
1036 signals which were assigned with blocking assignments (=) even in
1037 a block with non-blocking assignments (<=).
1038
1039 If nil, all blocking assigned signals are ignored when any
1040 non-blocking assignment is in the AUTORESET block. This allows
1041 blocking assignments to be used for temporary values and not have
1042 those temporaries reset. See example in `verilog-auto-reset'."
1043 :version "24.1" ; rev718
1044 :type 'boolean
1045 :group 'verilog-mode-auto)
1046 (put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
1047
1048 (defcustom verilog-auto-reset-widths t
1049 "True means AUTORESET should determine the width of signals.
1050 This is then used to set the width of the zero (32'h0 for example). This
1051 is required by some lint tools that aren't smart enough to ignore widths of
1052 the constant zero. This may result in ugly code when parameters determine
1053 the MSB or LSB of a signal inside an AUTORESET.
1054
1055 If nil, AUTORESET uses \"0\" as the constant.
1056
1057 If `unbased', AUTORESET used the unbased unsized literal \"\\='0\"
1058 as the constant. This setting is strongly recommended for
1059 SystemVerilog designs."
1060 :type 'boolean
1061 :group 'verilog-mode-auto)
1062 (put 'verilog-auto-reset-widths 'safe-local-variable
1063 '(lambda (x) (memq x '(nil t unbased))))
1064
1065 (defcustom verilog-assignment-delay ""
1066 "Text used for delays in delayed assignments. Add a trailing space if set."
1067 :group 'verilog-mode-auto
1068 :type 'string)
1069 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
1070
1071 (defcustom verilog-auto-arg-format 'packed
1072 "Formatting to use for AUTOARG signal names.
1073 If `packed', then as many inputs and outputs that fit within
1074 `fill-column' will be put onto one line.
1075
1076 If `single', then a single input or output will be put onto each
1077 line."
1078 :version "25.1"
1079 :type '(radio (const :tag "Line up Assignments and Declarations" packed)
1080 (const :tag "Line up Assignment statements" single))
1081 :group 'verilog-mode-auto)
1082 (put 'verilog-auto-arg-format 'safe-local-variable
1083 '(lambda (x) (memq x '(packed single))))
1084
1085 (defcustom verilog-auto-arg-sort nil
1086 "Non-nil means AUTOARG signal names will be sorted, not in declaration order.
1087 Declaration order is advantageous with order based instantiations
1088 and is the default for backward compatibility. Sorted order
1089 reduces changes when declarations are moved around in a file, and
1090 it's bad practice to rely on order based instantiations anyhow.
1091
1092 See also `verilog-auto-inst-sort'."
1093 :group 'verilog-mode-auto
1094 :type 'boolean)
1095 (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
1096
1097 (defcustom verilog-auto-inst-dot-name nil
1098 "Non-nil means when creating ports with AUTOINST, use .name syntax.
1099 This will use \".port\" instead of \".port(port)\" when possible.
1100 This is only legal in SystemVerilog files, and will confuse older
1101 simulators. Setting `verilog-auto-inst-vector' to nil may also
1102 be desirable to increase how often .name will be used."
1103 :group 'verilog-mode-auto
1104 :type 'boolean)
1105 (put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
1106
1107 (defcustom verilog-auto-inst-param-value nil
1108 "Non-nil means AUTOINST will replace parameters with the parameter value.
1109 If nil, leave parameters as symbolic names.
1110
1111 Parameters must be in Verilog 2001 format #(...), and if a parameter is not
1112 listed as such there (as when the default value is acceptable), it will not
1113 be replaced, and will remain symbolic.
1114
1115 For example, imagine a submodule uses parameters to declare the size of its
1116 inputs. This is then used by an upper module:
1117
1118 module InstModule (o,i);
1119 parameter WIDTH;
1120 input [WIDTH-1:0] i;
1121 endmodule
1122
1123 module ExampInst;
1124 InstModule
1125 #(.PARAM(10))
1126 instName
1127 (/*AUTOINST*/
1128 .i (i[PARAM-1:0]));
1129
1130 Note even though PARAM=10, the AUTOINST has left the parameter as a
1131 symbolic name. If `verilog-auto-inst-param-value' is set, this will
1132 instead expand to:
1133
1134 module ExampInst;
1135 InstModule
1136 #(.PARAM(10))
1137 instName
1138 (/*AUTOINST*/
1139 .i (i[9:0]));"
1140 :group 'verilog-mode-auto
1141 :type 'boolean)
1142 (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
1143
1144 (defcustom verilog-auto-inst-sort nil
1145 "Non-nil means AUTOINST signals will be sorted, not in declaration order.
1146 Also affects AUTOINSTPARAM. Declaration order is the default for
1147 backward compatibility, and as some teams prefer signals that are
1148 declared together to remain together. Sorted order reduces
1149 changes when declarations are moved around in a file.
1150
1151 See also `verilog-auto-arg-sort'."
1152 :version "24.1" ; rev688
1153 :group 'verilog-mode-auto
1154 :type 'boolean)
1155 (put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
1156
1157 (defcustom verilog-auto-inst-vector t
1158 "Non-nil means when creating default ports with AUTOINST, use bus subscripts.
1159 If nil, skip the subscript when it matches the entire bus as declared in
1160 the module (AUTOWIRE signals always are subscripted, you must manually
1161 declare the wire to have the subscripts removed.) Setting this to nil may
1162 speed up some simulators, but is less general and harder to read, so avoid."
1163 :group 'verilog-mode-auto
1164 :type 'boolean)
1165 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
1166
1167 (defcustom verilog-auto-inst-template-numbers nil
1168 "If true, when creating templated ports with AUTOINST, add a comment.
1169
1170 If t, the comment will add the line number of the template that
1171 was used for that port declaration. This setting is suggested
1172 only for debugging use, as regular use may cause a large numbers
1173 of merge conflicts.
1174
1175 If `lhs', the comment will show the left hand side of the
1176 AUTO_TEMPLATE rule that is matched. This is less precise than
1177 numbering (t) when multiple rules have the same pin name, but
1178 won't merge conflict."
1179 :group 'verilog-mode-auto
1180 :type '(choice (const nil) (const t) (const lhs)))
1181 (put 'verilog-auto-inst-template-numbers 'safe-local-variable
1182 '(lambda (x) (memq x '(nil t lhs))))
1183
1184 (defcustom verilog-auto-inst-column 40
1185 "Indent-to column number for net name part of AUTOINST created pin."
1186 :group 'verilog-mode-indent
1187 :type 'integer)
1188 (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
1189
1190 (defcustom verilog-auto-inst-interfaced-ports nil
1191 "Non-nil means include interfaced ports in AUTOINST expansions."
1192 :version "24.3" ; rev773, default change rev815
1193 :group 'verilog-mode-auto
1194 :type 'boolean)
1195 (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
1196
1197 (defcustom verilog-auto-input-ignore-regexp nil
1198 "If non-nil, when creating AUTOINPUT, ignore signals matching this regexp.
1199 See the \\[verilog-faq] for examples on using this."
1200 :group 'verilog-mode-auto
1201 :type '(choice (const nil) regexp))
1202 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
1203
1204 (defcustom verilog-auto-inout-ignore-regexp nil
1205 "If non-nil, when creating AUTOINOUT, ignore signals matching this regexp.
1206 See the \\[verilog-faq] for examples on using this."
1207 :group 'verilog-mode-auto
1208 :type '(choice (const nil) regexp))
1209 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
1210
1211 (defcustom verilog-auto-output-ignore-regexp nil
1212 "If non-nil, when creating AUTOOUTPUT, ignore signals matching this regexp.
1213 See the \\[verilog-faq] for examples on using this."
1214 :group 'verilog-mode-auto
1215 :type '(choice (const nil) regexp))
1216 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
1217
1218 (defcustom verilog-auto-template-warn-unused nil
1219 "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
1220 This feature is not supported before Emacs 21.1 or XEmacs 21.4."
1221 :version "24.3" ; rev787
1222 :group 'verilog-mode-auto
1223 :type 'boolean)
1224 (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
1225
1226 (defcustom verilog-auto-tieoff-declaration "wire"
1227 "Data type used for the declaration for AUTOTIEOFF.
1228 If \"wire\" then create a wire, if \"assign\" create an
1229 assignment, else the data type for variable creation."
1230 :version "24.1" ; rev713
1231 :group 'verilog-mode-auto
1232 :type 'string)
1233 (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1234
1235 (defcustom verilog-auto-tieoff-ignore-regexp nil
1236 "If non-nil, when creating AUTOTIEOFF, ignore signals matching this regexp.
1237 See the \\[verilog-faq] for examples on using this."
1238 :group 'verilog-mode-auto
1239 :type '(choice (const nil) regexp))
1240 (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1241
1242 (defcustom verilog-auto-unused-ignore-regexp nil
1243 "If non-nil, when creating AUTOUNUSED, ignore signals matching this regexp.
1244 See the \\[verilog-faq] for examples on using this."
1245 :group 'verilog-mode-auto
1246 :type '(choice (const nil) regexp))
1247 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
1248
1249 (defcustom verilog-case-fold t
1250 "Non-nil means `verilog-mode' regexps should ignore case.
1251 This variable is t for backward compatibility; nil is suggested."
1252 :version "24.4"
1253 :group 'verilog-mode
1254 :type 'boolean)
1255 (put 'verilog-case-fold 'safe-local-variable 'verilog-booleanp)
1256
1257 (defcustom verilog-typedef-regexp nil
1258 "If non-nil, regular expression that matches Verilog-2001 typedef names.
1259 For example, \"_t$\" matches typedefs named with _t, as in the C language.
1260 See also `verilog-case-fold'."
1261 :group 'verilog-mode-auto
1262 :type '(choice (const nil) regexp))
1263 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
1264
1265 (defcustom verilog-mode-hook 'verilog-set-compile-command
1266 "Hook run after Verilog mode is loaded."
1267 :type 'hook
1268 :group 'verilog-mode)
1269
1270 (defcustom verilog-auto-hook nil
1271 "Hook run after `verilog-mode' updates AUTOs."
1272 :group 'verilog-mode-auto
1273 :type 'hook)
1274
1275 (defcustom verilog-before-auto-hook nil
1276 "Hook run before `verilog-mode' updates AUTOs."
1277 :group 'verilog-mode-auto
1278 :type 'hook)
1279
1280 (defcustom verilog-delete-auto-hook nil
1281 "Hook run after `verilog-mode' deletes AUTOs."
1282 :group 'verilog-mode-auto
1283 :type 'hook)
1284
1285 (defcustom verilog-before-delete-auto-hook nil
1286 "Hook run before `verilog-mode' deletes AUTOs."
1287 :group 'verilog-mode-auto
1288 :type 'hook)
1289
1290 (defcustom verilog-getopt-flags-hook nil
1291 "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1292 :group 'verilog-mode-auto
1293 :type 'hook)
1294
1295 (defcustom verilog-before-getopt-flags-hook nil
1296 "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1297 :group 'verilog-mode-auto
1298 :type 'hook)
1299
1300 (defcustom verilog-before-save-font-hook nil
1301 "Hook run before `verilog-save-font-mods' removes highlighting."
1302 :version "24.3" ; rev735
1303 :group 'verilog-mode-auto
1304 :type 'hook)
1305
1306 (defcustom verilog-after-save-font-hook nil
1307 "Hook run after `verilog-save-font-mods' restores highlighting."
1308 :version "24.3" ; rev735
1309 :group 'verilog-mode-auto
1310 :type 'hook)
1311
1312 (defvar verilog-imenu-generic-expression
1313 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1314 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
1315 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1316
1317 ;;
1318 ;; provide a verilog-header function.
1319 ;; Customization variables:
1320 ;;
1321 (defvar verilog-date-scientific-format nil
1322 "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1323 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1324 format (e.g. 09/17/1997) is not supported.")
1325
1326 (defvar verilog-company nil
1327 "Default name of Company for Verilog header.
1328 If set will become buffer local.")
1329 (make-variable-buffer-local 'verilog-company)
1330
1331 (defvar verilog-project nil
1332 "Default name of Project for Verilog header.
1333 If set will become buffer local.")
1334 (make-variable-buffer-local 'verilog-project)
1335
1336 ;;; Keymap and Menu:
1337 ;;
1338
1339 (defvar verilog-mode-map
1340 (let ((map (make-sparse-keymap)))
1341 (define-key map ";" 'electric-verilog-semi)
1342 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1343 (define-key map ":" 'electric-verilog-colon)
1344 ;;(define-key map "=" 'electric-verilog-equal)
1345 (define-key map "`" 'electric-verilog-tick)
1346 (define-key map "\t" 'electric-verilog-tab)
1347 (define-key map "\r" 'electric-verilog-terminate-line)
1348 ;; backspace/delete key bindings
1349 (define-key map [backspace] 'backward-delete-char-untabify)
1350 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1351 (define-key map [delete] 'delete-char)
1352 (define-key map [(meta delete)] 'kill-word))
1353 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1354 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1355 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1356 (define-key map "\M-\t" 'verilog-complete-word)
1357 (define-key map "\M-?" 'verilog-show-completions)
1358 ;; Note \C-c and letter are reserved for users
1359 (define-key map "\C-c`" 'verilog-lint-off)
1360 (define-key map "\C-c*" 'verilog-delete-auto-star-implicit)
1361 (define-key map "\C-c?" 'verilog-diff-auto)
1362 (define-key map "\C-c\C-r" 'verilog-label-be)
1363 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1364 (define-key map "\C-c=" 'verilog-pretty-expr)
1365 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1366 (define-key map "\M-*" 'verilog-star-comment)
1367 (define-key map "\C-c\C-c" 'verilog-comment-region)
1368 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1369 (when (featurep 'xemacs)
1370 (define-key map [(meta control h)] 'verilog-mark-defun)
1371 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1372 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1373 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1374 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1375 (define-key map "\C-c\C-a" 'verilog-auto)
1376 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1377 (define-key map "\C-c\C-p" 'verilog-preprocess)
1378 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1379 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1380 (define-key map "\C-c\C-h" 'verilog-header)
1381 map)
1382 "Keymap used in Verilog mode.")
1383
1384 ;; menus
1385 (easy-menu-define
1386 verilog-menu verilog-mode-map "Menu for Verilog mode"
1387 (verilog-easy-menu-filter
1388 '("Verilog"
1389 ("Choose Compilation Action"
1390 ["None"
1391 (progn
1392 (setq verilog-tool nil)
1393 (verilog-set-compile-command))
1394 :style radio
1395 :selected (equal verilog-tool nil)
1396 :help "When invoking compilation, use compile-command"]
1397 ["Lint"
1398 (progn
1399 (setq verilog-tool 'verilog-linter)
1400 (verilog-set-compile-command))
1401 :style radio
1402 :selected (equal verilog-tool `verilog-linter)
1403 :help "When invoking compilation, use lint checker"]
1404 ["Coverage"
1405 (progn
1406 (setq verilog-tool 'verilog-coverage)
1407 (verilog-set-compile-command))
1408 :style radio
1409 :selected (equal verilog-tool `verilog-coverage)
1410 :help "When invoking compilation, annotate for coverage"]
1411 ["Simulator"
1412 (progn
1413 (setq verilog-tool 'verilog-simulator)
1414 (verilog-set-compile-command))
1415 :style radio
1416 :selected (equal verilog-tool `verilog-simulator)
1417 :help "When invoking compilation, interpret Verilog source"]
1418 ["Compiler"
1419 (progn
1420 (setq verilog-tool 'verilog-compiler)
1421 (verilog-set-compile-command))
1422 :style radio
1423 :selected (equal verilog-tool `verilog-compiler)
1424 :help "When invoking compilation, compile Verilog source"]
1425 ["Preprocessor"
1426 (progn
1427 (setq verilog-tool 'verilog-preprocessor)
1428 (verilog-set-compile-command))
1429 :style radio
1430 :selected (equal verilog-tool `verilog-preprocessor)
1431 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
1432 )
1433 ("Move"
1434 ["Beginning of function" verilog-beg-of-defun
1435 :keys "C-M-a"
1436 :help "Move backward to the beginning of the current function or procedure"]
1437 ["End of function" verilog-end-of-defun
1438 :keys "C-M-e"
1439 :help "Move forward to the end of the current function or procedure"]
1440 ["Mark function" verilog-mark-defun
1441 :keys "C-M-h"
1442 :help "Mark the current Verilog function or procedure"]
1443 ["Goto function/module" verilog-goto-defun
1444 :help "Move to specified Verilog module/task/function"]
1445 ["Move to beginning of block" electric-verilog-backward-sexp
1446 :help "Move backward over one balanced expression"]
1447 ["Move to end of block" electric-verilog-forward-sexp
1448 :help "Move forward over one balanced expression"]
1449 )
1450 ("Comments"
1451 ["Comment Region" verilog-comment-region
1452 :help "Put marked area into a comment"]
1453 ["UnComment Region" verilog-uncomment-region
1454 :help "Uncomment an area commented with Comment Region"]
1455 ["Multi-line comment insert" verilog-star-comment
1456 :help "Insert Verilog /* */ comment at point"]
1457 ["Lint error to comment" verilog-lint-off
1458 :help "Convert a Verilog linter warning line into a disable statement"]
1459 )
1460 "----"
1461 ["Compile" compile
1462 :help "Perform compilation-action (above) on the current buffer"]
1463 ["AUTO, Save, Compile" verilog-auto-save-compile
1464 :help "Recompute AUTOs, save buffer, and compile"]
1465 ["Next Compile Error" next-error
1466 :help "Visit next compilation error message and corresponding source code"]
1467 ["Ignore Lint Warning at point" verilog-lint-off
1468 :help "Convert a Verilog linter warning line into a disable statement"]
1469 "----"
1470 ["Line up declarations around point" verilog-pretty-declarations
1471 :help "Line up declarations around point"]
1472 ["Line up equations around point" verilog-pretty-expr
1473 :help "Line up expressions around point"]
1474 ["Redo/insert comments on every end" verilog-label-be
1475 :help "Label matching begin ... end statements"]
1476 ["Expand [x:y] vector line" verilog-expand-vector
1477 :help "Take a signal vector on the current line and expand it to multiple lines"]
1478 ["Insert begin-end block" verilog-insert-block
1479 :help "Insert begin ... end"]
1480 ["Complete word" verilog-complete-word
1481 :help "Complete word at point"]
1482 "----"
1483 ["Recompute AUTOs" verilog-auto
1484 :help "Expand AUTO meta-comment statements"]
1485 ["Kill AUTOs" verilog-delete-auto
1486 :help "Remove AUTO expansions"]
1487 ["Diff AUTOs" verilog-diff-auto
1488 :help "Show differences in AUTO expansions"]
1489 ["Inject AUTOs" verilog-inject-auto
1490 :help "Inject AUTOs into legacy non-AUTO buffer"]
1491 ("AUTO Help..."
1492 ["AUTO General" (describe-function 'verilog-auto)
1493 :help "Help introduction on AUTOs"]
1494 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1495 :help "Help on verilog-library-flags"]
1496 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1497 :help "Help on verilog-library-directories"]
1498 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1499 :help "Help on verilog-library-files"]
1500 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1501 :help "Help on verilog-library-extensions"]
1502 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1503 :help "Help on reading `defines"]
1504 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1505 :help "Help on parsing `includes"]
1506 ["AUTOARG" (describe-function 'verilog-auto-arg)
1507 :help "Help on AUTOARG - declaring module port list"]
1508 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1509 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1510 ["AUTOASSIGNMODPORT" (describe-function 'verilog-auto-assign-modport)
1511 :help "Help on AUTOASSIGNMODPORT - creating assignments to/from modports"]
1512 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1513 :help "Help on AUTOINOUT - adding inouts from cells"]
1514 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1515 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1516 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1517 :help "Help on AUTOINOUTIN - copying i/o from another file as all inputs"]
1518 ["AUTOINOUTMODPORT" (describe-function 'verilog-auto-inout-modport)
1519 :help "Help on AUTOINOUTMODPORT - copying i/o from an interface modport"]
1520 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1521 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1522 ["AUTOINOUTPARAM" (describe-function 'verilog-auto-inout-param)
1523 :help "Help on AUTOINOUTPARAM - copying parameters from another file"]
1524 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1525 :help "Help on AUTOINPUT - adding inputs from cells"]
1526 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1527 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1528 ["AUTOINSERTLAST" (describe-function 'verilog-auto-insert-last)
1529 :help "Help on AUTOINSERTLISPLAST - insert text from a lisp function"]
1530 ["AUTOINST" (describe-function 'verilog-auto-inst)
1531 :help "Help on AUTOINST - adding pins for cells"]
1532 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1533 :help "Help on expanding Verilog-2001 .* pins"]
1534 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1535 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1536 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1537 :help "Help on AUTOLOGIC - declaring logic signals"]
1538 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1539 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1540 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1541 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1542 ["AUTOREG" (describe-function 'verilog-auto-reg)
1543 :help "Help on AUTOREG - declaring registers for non-wires"]
1544 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1545 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1546 ["AUTORESET" (describe-function 'verilog-auto-reset)
1547 :help "Help on AUTORESET - resetting always blocks"]
1548 ["AUTOSENSE or AS" (describe-function 'verilog-auto-sense)
1549 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1550 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1551 :help "Help on AUTOTIEOFF - tying off unused outputs"]
1552 ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
1553 :help "Help on AUTOUNDEF - undefine all local defines"]
1554 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1555 :help "Help on AUTOUNUSED - terminating unused inputs"]
1556 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1557 :help "Help on AUTOWIRE - declaring wires for cells"]
1558 )
1559 "----"
1560 ["Submit bug report" verilog-submit-bug-report
1561 :help "Submit via mail a bug report on verilog-mode.el"]
1562 ["Version and FAQ" verilog-faq
1563 :help "Show the current version, and where to get the FAQ etc"]
1564 ["Customize Verilog Mode..." verilog-customize
1565 :help "Customize variables and other settings used by Verilog-Mode"]
1566 ["Customize Verilog Fonts & Colors" verilog-font-customize
1567 :help "Customize fonts used by Verilog-Mode."])))
1568
1569 (easy-menu-define
1570 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1571 (verilog-easy-menu-filter
1572 '("Statements"
1573 ["Header" verilog-sk-header
1574 :help "Insert a header block at the top of file"]
1575 ["Comment" verilog-sk-comment
1576 :help "Insert a comment block"]
1577 "----"
1578 ["Module" verilog-sk-module
1579 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1580 ["OVM Class" verilog-sk-ovm-class
1581 :help "Insert an OVM class block"]
1582 ["UVM Object" verilog-sk-uvm-object
1583 :help "Insert an UVM object block"]
1584 ["UVM Component" verilog-sk-uvm-component
1585 :help "Insert an UVM component block"]
1586 ["Primitive" verilog-sk-primitive
1587 :help "Insert a primitive .. (.. );.. endprimitive block"]
1588 "----"
1589 ["Input" verilog-sk-input
1590 :help "Insert an input declaration"]
1591 ["Output" verilog-sk-output
1592 :help "Insert an output declaration"]
1593 ["Inout" verilog-sk-inout
1594 :help "Insert an inout declaration"]
1595 ["Wire" verilog-sk-wire
1596 :help "Insert a wire declaration"]
1597 ["Reg" verilog-sk-reg
1598 :help "Insert a register declaration"]
1599 ["Define thing under point as a register" verilog-sk-define-signal
1600 :help "Define signal under point as a register at the top of the module"]
1601 "----"
1602 ["Initial" verilog-sk-initial
1603 :help "Insert an initial begin .. end block"]
1604 ["Always" verilog-sk-always
1605 :help "Insert an always @(AS) begin .. end block"]
1606 ["Function" verilog-sk-function
1607 :help "Insert a function .. begin .. end endfunction block"]
1608 ["Task" verilog-sk-task
1609 :help "Insert a task .. begin .. end endtask block"]
1610 ["Specify" verilog-sk-specify
1611 :help "Insert a specify .. endspecify block"]
1612 ["Generate" verilog-sk-generate
1613 :help "Insert a generate .. endgenerate block"]
1614 "----"
1615 ["Begin" verilog-sk-begin
1616 :help "Insert a begin .. end block"]
1617 ["If" verilog-sk-if
1618 :help "Insert an if (..) begin .. end block"]
1619 ["(if) else" verilog-sk-else-if
1620 :help "Insert an else if (..) begin .. end block"]
1621 ["For" verilog-sk-for
1622 :help "Insert a for (...) begin .. end block"]
1623 ["While" verilog-sk-while
1624 :help "Insert a while (...) begin .. end block"]
1625 ["Fork" verilog-sk-fork
1626 :help "Insert a fork begin .. end .. join block"]
1627 ["Repeat" verilog-sk-repeat
1628 :help "Insert a repeat (..) begin .. end block"]
1629 ["Case" verilog-sk-case
1630 :help "Insert a case block, prompting for details"]
1631 ["Casex" verilog-sk-casex
1632 :help "Insert a casex (...) item: begin.. end endcase block"]
1633 ["Casez" verilog-sk-casez
1634 :help "Insert a casez (...) item: begin.. end endcase block"])))
1635
1636 (defvar verilog-mode-abbrev-table nil
1637 "Abbrev table in use in Verilog-mode buffers.")
1638
1639 (define-abbrev-table 'verilog-mode-abbrev-table ())
1640 (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1641 (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1642 (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1643 (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1644 (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1645 (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1646 (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1647 (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1648 (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1649 (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1650 (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1651 (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1652 (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1653 (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1654 (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1655 (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1656 (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1657 (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1658 (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1659 (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1660 (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1661 (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1662 (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1663 (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1664 (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
1665
1666 ;;
1667 ;; Macros
1668 ;;
1669
1670 (defsubst verilog-within-string ()
1671 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
1672
1673 (defsubst verilog-string-match-fold (regexp string &optional start)
1674 "Like `string-match', but use `verilog-case-fold'.
1675 Return index of start of first match for REGEXP in STRING, or nil.
1676 Matching ignores case if `verilog-case-fold' is non-nil.
1677 If third arg START is non-nil, start search at that index in STRING."
1678 (let ((case-fold-search verilog-case-fold))
1679 (string-match regexp string start)))
1680
1681 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1682 "Replace occurrences of FROM-STRING with TO-STRING.
1683 FIXEDCASE and LITERAL as in `replace-match'. STRING is what to replace.
1684 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1685 will break, as the o's continuously replace. xa -> x works ok though."
1686 ;; Hopefully soon to an Emacs built-in
1687 ;; Also note \ in the replacement prevent multiple replacements; IE
1688 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1689 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
1690 (let ((start 0))
1691 (while (string-match from-string string start)
1692 (setq string (replace-match to-string fixedcase literal string)
1693 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1694 string))
1695
1696 (defsubst verilog-string-remove-spaces (string)
1697 "Remove spaces surrounding STRING."
1698 (save-match-data
1699 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1700 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1701 string))
1702
1703 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1704 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1705 "Like `re-search-forward', but skips over match in comments or strings."
1706 (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
1707 (while (and
1708 (re-search-forward REGEXP BOUND NOERROR)
1709 (setq mdata (match-data))
1710 (and (verilog-skip-forward-comment-or-string)
1711 (progn
1712 (setq mdata '(nil nil))
1713 (if BOUND
1714 (< (point) BOUND)
1715 t)))))
1716 (store-match-data mdata)
1717 (match-end 0)))
1718
1719 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1720 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1721 "Like `re-search-backward', but skips over match in comments or strings."
1722 (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
1723 (while (and
1724 (re-search-backward REGEXP BOUND NOERROR)
1725 (setq mdata (match-data))
1726 (and (verilog-skip-backward-comment-or-string)
1727 (progn
1728 (setq mdata '(nil nil))
1729 (if BOUND
1730 (> (point) BOUND)
1731 t)))))
1732 (store-match-data mdata)
1733 (match-end 0)))
1734
1735 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1736 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1737 but trashes match data and is faster for REGEXP that doesn't match often.
1738 This uses `verilog-scan' and text properties to ignore comments,
1739 so there may be a large up front penalty for the first search."
1740 (let (pt)
1741 (while (and (not pt)
1742 (re-search-forward regexp bound noerror))
1743 (if (verilog-inside-comment-or-string-p)
1744 (re-search-forward "[/\"\n]" nil t) ; Only way a comment or quote can end
1745 (setq pt (match-end 0))))
1746 pt))
1747
1748 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1749 ;; checkdoc-params: (REGEXP BOUND NOERROR)
1750 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1751 but trashes match data and is faster for REGEXP that doesn't match often.
1752 This uses `verilog-scan' and text properties to ignore comments,
1753 so there may be a large up front penalty for the first search."
1754 (let (pt)
1755 (while (and (not pt)
1756 (re-search-backward regexp bound noerror))
1757 (if (verilog-inside-comment-or-string-p)
1758 (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
1759 (setq pt (match-beginning 0))))
1760 pt))
1761
1762 (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1763 "Like `re-search-forward', but first search for SUBSTR constant.
1764 Then searched for the normal REGEXP (which contains SUBSTR), with given
1765 BOUND and NOERROR. The REGEXP must fit within a single line.
1766 This speeds up complicated regexp matches."
1767 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1768 ;; thus require matches to be on one line, and use beginning-of-line.
1769 (let (done)
1770 (while (and (not done)
1771 (search-forward substr bound noerror))
1772 (save-excursion
1773 (beginning-of-line)
1774 (setq done (re-search-forward regexp (point-at-eol) noerror)))
1775 (unless (and (<= (match-beginning 0) (point))
1776 (>= (match-end 0) (point)))
1777 (setq done nil)))
1778 (when done (goto-char done))
1779 done))
1780 ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ; -end (test bait)
1781
1782 (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1783 "Like `re-search-backward', but first search for SUBSTR constant.
1784 Then searched for the normal REGEXP (which contains SUBSTR), with given
1785 BOUND and NOERROR. The REGEXP must fit within a single line.
1786 This speeds up complicated regexp matches."
1787 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1788 ;; thus require matches to be on one line, and use beginning-of-line.
1789 (let (done)
1790 (while (and (not done)
1791 (search-backward substr bound noerror))
1792 (save-excursion
1793 (end-of-line)
1794 (setq done (re-search-backward regexp (point-at-bol) noerror)))
1795 (unless (and (<= (match-beginning 0) (point))
1796 (>= (match-end 0) (point)))
1797 (setq done nil)))
1798 (when done (goto-char done))
1799 done))
1800 ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ; -end (test bait)
1801
1802 (defun verilog-delete-trailing-whitespace ()
1803 "Delete trailing spaces or tabs, but not newlines nor linefeeds.
1804 Also add missing final newline.
1805
1806 To call this from the command line, see \\[verilog-batch-diff-auto].
1807
1808 To call on \\[verilog-auto], set `verilog-auto-delete-trailing-whitespace'."
1809 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1810 (save-excursion
1811 (goto-char (point-min))
1812 (while (re-search-forward "[ \t]+$" nil t) ; Not syntactic WS as no formfeed
1813 (replace-match "" nil nil))
1814 (goto-char (point-max))
1815 (unless (bolp) (insert "\n"))))
1816
1817 (defvar compile-command)
1818 (defvar create-lockfiles) ; Emacs 24
1819
1820 ;; compilation program
1821 (defun verilog-set-compile-command ()
1822 "Function to compute shell command to compile Verilog.
1823
1824 This reads `verilog-tool' and sets `compile-command'. This specifies the
1825 program that executes when you type \\[compile] or
1826 \\[verilog-auto-save-compile].
1827
1828 By default `verilog-tool' uses a Makefile if one exists in the
1829 current directory. If not, it is set to the `verilog-linter',
1830 `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1831 or `verilog-simulator' variables, as selected with the Verilog ->
1832 \"Choose Compilation Action\" menu.
1833
1834 You should set `verilog-tool' or the other variables to the path and
1835 arguments for your Verilog simulator. For example:
1836 \"vcs -p123 -O\"
1837 or a string like:
1838 \"(cd /tmp; surecov %s)\".
1839
1840 In the former case, the path to the current buffer is concat'ed to the
1841 value of `verilog-tool'; in the later, the path to the current buffer is
1842 substituted for the %s.
1843
1844 Where __FLAGS__ appears in the string `verilog-current-flags'
1845 will be substituted.
1846
1847 Where __FILE__ appears in the string, the variable
1848 `buffer-file-name' of the current buffer, without the directory
1849 portion, will be substituted."
1850 (interactive)
1851 (cond
1852 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1853 (file-exists-p "Makefile"))
1854 (set (make-local-variable 'compile-command) "make "))
1855 (t
1856 (set (make-local-variable 'compile-command)
1857 (if verilog-tool
1858 (if (string-match "%s" (eval verilog-tool))
1859 (format (eval verilog-tool) (or buffer-file-name ""))
1860 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1861 ""))))
1862 (verilog-modify-compile-command))
1863
1864 (defun verilog-expand-command (command)
1865 "Replace meta-information in COMMAND and return it.
1866 Where __FLAGS__ appears in the string `verilog-current-flags'
1867 will be substituted. Where __FILE__ appears in the string, the
1868 current buffer's file-name, without the directory portion, will
1869 be substituted."
1870 (setq command (verilog-string-replace-matches
1871 ;; Note \\b only works if under verilog syntax table
1872 "\\b__FLAGS__\\b" (verilog-current-flags)
1873 t t command))
1874 (setq command (verilog-string-replace-matches
1875 "\\b__FILE__\\b" (file-name-nondirectory
1876 (or (buffer-file-name) ""))
1877 t t command))
1878 command)
1879
1880 (defun verilog-modify-compile-command ()
1881 "Update `compile-command' using `verilog-expand-command'."
1882 (when (and
1883 (stringp compile-command)
1884 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
1885 (set (make-local-variable 'compile-command)
1886 (verilog-expand-command compile-command))))
1887
1888 (if (featurep 'xemacs)
1889 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1890 (defun verilog-error-regexp-add-xemacs ()
1891 "Teach XEmacs about verilog errors.
1892 Called by `compilation-mode-hook'. This allows \\[next-error] to
1893 find the errors."
1894 (interactive)
1895 (if (boundp 'compilation-error-regexp-systems-alist)
1896 (if (and
1897 (not (equal compilation-error-regexp-systems-list 'all))
1898 (not (member compilation-error-regexp-systems-list 'verilog)))
1899 (push 'verilog compilation-error-regexp-systems-list)))
1900 (if (boundp 'compilation-error-regexp-alist-alist)
1901 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1902 (setcdr compilation-error-regexp-alist-alist
1903 (cons verilog-error-regexp-xemacs-alist
1904 (cdr compilation-error-regexp-alist-alist)))))
1905 (if (boundp 'compilation-font-lock-keywords)
1906 (progn
1907 (set (make-local-variable 'compilation-font-lock-keywords)
1908 verilog-error-font-lock-keywords)
1909 (font-lock-set-defaults)))
1910 ;; Need to re-run compilation-error-regexp builder
1911 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1912 (compilation-build-compilation-error-regexp-alist))
1913 ))
1914
1915 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1916 (defun verilog-error-regexp-add-emacs ()
1917 "Tell Emacs compile that we are Verilog.
1918 Called by `compilation-mode-hook'. This allows \\[next-error] to
1919 find the errors."
1920 (interactive)
1921 (when (boundp 'compilation-error-regexp-alist-alist)
1922 (when (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1923 (mapcar
1924 (lambda (item)
1925 (push (car item) compilation-error-regexp-alist)
1926 (push item compilation-error-regexp-alist-alist))
1927 verilog-error-regexp-emacs-alist))))
1928
1929 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1930 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1931
1932 (defconst verilog-compiler-directives
1933 (eval-when-compile
1934 '(
1935 ;; compiler directives, from IEEE 1800-2012 section 22.1
1936 "`__FILE__" "`__LINE" "`begin_keywords" "`celldefine" "`default_nettype"
1937 "`define" "`else" "`elsif" "`end_keywords" "`endcelldefine" "`endif"
1938 "`ifdef" "`ifndef" "`include" "`line" "`nounconnected_drive" "`pragma"
1939 "`resetall" "`timescale" "`unconnected_drive" "`undef" "`undefineall"
1940 ;; compiler directives not covered by IEEE 1800
1941 "`case" "`default" "`endfor" "`endprotect" "`endswitch" "`endwhile" "`for"
1942 "`format" "`if" "`let" "`protect" "`switch" "`timescale" "`time_scale"
1943 "`while"
1944 ))
1945 "List of Verilog compiler directives.")
1946
1947 (defconst verilog-directive-re
1948 (verilog-regexp-words verilog-compiler-directives))
1949
1950 (defconst verilog-directive-re-1
1951 (concat "[ \t]*" verilog-directive-re))
1952
1953 (defconst verilog-directive-begin
1954 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1955
1956 (defconst verilog-directive-middle
1957 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
1958
1959 (defconst verilog-directive-end
1960 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1961
1962 (defconst verilog-ovm-begin-re
1963 (eval-when-compile
1964 (verilog-regexp-opt
1965 '(
1966 "`ovm_component_utils_begin"
1967 "`ovm_component_param_utils_begin"
1968 "`ovm_field_utils_begin"
1969 "`ovm_object_utils_begin"
1970 "`ovm_object_param_utils_begin"
1971 "`ovm_sequence_utils_begin"
1972 "`ovm_sequencer_utils_begin"
1973 ) nil )))
1974
1975 (defconst verilog-ovm-end-re
1976 (eval-when-compile
1977 (verilog-regexp-opt
1978 '(
1979 "`ovm_component_utils_end"
1980 "`ovm_field_utils_end"
1981 "`ovm_object_utils_end"
1982 "`ovm_sequence_utils_end"
1983 "`ovm_sequencer_utils_end"
1984 ) nil )))
1985
1986 (defconst verilog-uvm-begin-re
1987 (eval-when-compile
1988 (verilog-regexp-opt
1989 '(
1990 "`uvm_component_utils_begin"
1991 "`uvm_component_param_utils_begin"
1992 "`uvm_field_utils_begin"
1993 "`uvm_object_utils_begin"
1994 "`uvm_object_param_utils_begin"
1995 "`uvm_sequence_utils_begin"
1996 "`uvm_sequencer_utils_begin"
1997 ) nil )))
1998
1999 (defconst verilog-uvm-end-re
2000 (eval-when-compile
2001 (verilog-regexp-opt
2002 '(
2003 "`uvm_component_utils_end"
2004 "`uvm_field_utils_end"
2005 "`uvm_object_utils_end"
2006 "`uvm_sequence_utils_end"
2007 "`uvm_sequencer_utils_end"
2008 ) nil )))
2009
2010 (defconst verilog-vmm-begin-re
2011 (eval-when-compile
2012 (verilog-regexp-opt
2013 '(
2014 "`vmm_data_member_begin"
2015 "`vmm_env_member_begin"
2016 "`vmm_scenario_member_begin"
2017 "`vmm_subenv_member_begin"
2018 "`vmm_xactor_member_begin"
2019 ) nil ) ) )
2020
2021 (defconst verilog-vmm-end-re
2022 (eval-when-compile
2023 (verilog-regexp-opt
2024 '(
2025 "`vmm_data_member_end"
2026 "`vmm_env_member_end"
2027 "`vmm_scenario_member_end"
2028 "`vmm_subenv_member_end"
2029 "`vmm_xactor_member_end"
2030 ) nil ) ) )
2031
2032 (defconst verilog-vmm-statement-re
2033 (eval-when-compile
2034 (verilog-regexp-opt
2035 '(
2036 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
2037 ;; "`vmm_xactor_member_enum_array"
2038 ;; "`vmm_xactor_member_scalar_array"
2039 ;; "`vmm_xactor_member_scalar"
2040 ) nil )))
2041
2042 (defconst verilog-ovm-statement-re
2043 (eval-when-compile
2044 (verilog-regexp-opt
2045 '(
2046 ;; Statements
2047 "`DUT_ERROR"
2048 "`MESSAGE"
2049 "`dut_error"
2050 "`message"
2051 "`ovm_analysis_imp_decl"
2052 "`ovm_blocking_get_imp_decl"
2053 "`ovm_blocking_get_peek_imp_decl"
2054 "`ovm_blocking_master_imp_decl"
2055 "`ovm_blocking_peek_imp_decl"
2056 "`ovm_blocking_put_imp_decl"
2057 "`ovm_blocking_slave_imp_decl"
2058 "`ovm_blocking_transport_imp_decl"
2059 "`ovm_component_registry"
2060 "`ovm_component_registry_param"
2061 "`ovm_component_utils"
2062 "`ovm_create"
2063 "`ovm_create_seq"
2064 "`ovm_declare_sequence_lib"
2065 "`ovm_do"
2066 "`ovm_do_seq"
2067 "`ovm_do_seq_with"
2068 "`ovm_do_with"
2069 "`ovm_error"
2070 "`ovm_fatal"
2071 "`ovm_field_aa_int_byte"
2072 "`ovm_field_aa_int_byte_unsigned"
2073 "`ovm_field_aa_int_int"
2074 "`ovm_field_aa_int_int_unsigned"
2075 "`ovm_field_aa_int_integer"
2076 "`ovm_field_aa_int_integer_unsigned"
2077 "`ovm_field_aa_int_key"
2078 "`ovm_field_aa_int_longint"
2079 "`ovm_field_aa_int_longint_unsigned"
2080 "`ovm_field_aa_int_shortint"
2081 "`ovm_field_aa_int_shortint_unsigned"
2082 "`ovm_field_aa_int_string"
2083 "`ovm_field_aa_object_int"
2084 "`ovm_field_aa_object_string"
2085 "`ovm_field_aa_string_int"
2086 "`ovm_field_aa_string_string"
2087 "`ovm_field_array_int"
2088 "`ovm_field_array_object"
2089 "`ovm_field_array_string"
2090 "`ovm_field_enum"
2091 "`ovm_field_event"
2092 "`ovm_field_int"
2093 "`ovm_field_object"
2094 "`ovm_field_queue_int"
2095 "`ovm_field_queue_object"
2096 "`ovm_field_queue_string"
2097 "`ovm_field_sarray_int"
2098 "`ovm_field_string"
2099 "`ovm_field_utils"
2100 "`ovm_file"
2101 "`ovm_get_imp_decl"
2102 "`ovm_get_peek_imp_decl"
2103 "`ovm_info"
2104 "`ovm_info1"
2105 "`ovm_info2"
2106 "`ovm_info3"
2107 "`ovm_info4"
2108 "`ovm_line"
2109 "`ovm_master_imp_decl"
2110 "`ovm_msg_detail"
2111 "`ovm_non_blocking_transport_imp_decl"
2112 "`ovm_nonblocking_get_imp_decl"
2113 "`ovm_nonblocking_get_peek_imp_decl"
2114 "`ovm_nonblocking_master_imp_decl"
2115 "`ovm_nonblocking_peek_imp_decl"
2116 "`ovm_nonblocking_put_imp_decl"
2117 "`ovm_nonblocking_slave_imp_decl"
2118 "`ovm_object_registry"
2119 "`ovm_object_registry_param"
2120 "`ovm_object_utils"
2121 "`ovm_peek_imp_decl"
2122 "`ovm_phase_func_decl"
2123 "`ovm_phase_task_decl"
2124 "`ovm_print_aa_int_object"
2125 "`ovm_print_aa_string_int"
2126 "`ovm_print_aa_string_object"
2127 "`ovm_print_aa_string_string"
2128 "`ovm_print_array_int"
2129 "`ovm_print_array_object"
2130 "`ovm_print_array_string"
2131 "`ovm_print_object_queue"
2132 "`ovm_print_queue_int"
2133 "`ovm_print_string_queue"
2134 "`ovm_put_imp_decl"
2135 "`ovm_rand_send"
2136 "`ovm_rand_send_with"
2137 "`ovm_send"
2138 "`ovm_sequence_utils"
2139 "`ovm_slave_imp_decl"
2140 "`ovm_transport_imp_decl"
2141 "`ovm_update_sequence_lib"
2142 "`ovm_update_sequence_lib_and_item"
2143 "`ovm_warning"
2144 "`static_dut_error"
2145 "`static_message") nil )))
2146
2147 (defconst verilog-uvm-statement-re
2148 (eval-when-compile
2149 (verilog-regexp-opt
2150 '(
2151 ;; Statements
2152 "`uvm_analysis_imp_decl"
2153 "`uvm_blocking_get_imp_decl"
2154 "`uvm_blocking_get_peek_imp_decl"
2155 "`uvm_blocking_master_imp_decl"
2156 "`uvm_blocking_peek_imp_decl"
2157 "`uvm_blocking_put_imp_decl"
2158 "`uvm_blocking_slave_imp_decl"
2159 "`uvm_blocking_transport_imp_decl"
2160 "`uvm_component_param_utils"
2161 "`uvm_component_registry"
2162 "`uvm_component_registry_param"
2163 "`uvm_component_utils"
2164 "`uvm_create"
2165 "`uvm_create_on"
2166 "`uvm_create_seq" ; Undocumented in 1.1
2167 "`uvm_declare_p_sequencer"
2168 "`uvm_declare_sequence_lib" ; Deprecated in 1.1
2169 "`uvm_do"
2170 "`uvm_do_callbacks"
2171 "`uvm_do_callbacks_exit_on"
2172 "`uvm_do_obj_callbacks"
2173 "`uvm_do_obj_callbacks_exit_on"
2174 "`uvm_do_on"
2175 "`uvm_do_on_pri"
2176 "`uvm_do_on_pri_with"
2177 "`uvm_do_on_with"
2178 "`uvm_do_pri"
2179 "`uvm_do_pri_with"
2180 "`uvm_do_seq" ; Undocumented in 1.1
2181 "`uvm_do_seq_with" ; Undocumented in 1.1
2182 "`uvm_do_with"
2183 "`uvm_error"
2184 "`uvm_error_context"
2185 "`uvm_fatal"
2186 "`uvm_fatal_context"
2187 "`uvm_field_aa_int_byte"
2188 "`uvm_field_aa_int_byte_unsigned"
2189 "`uvm_field_aa_int_enum"
2190 "`uvm_field_aa_int_int"
2191 "`uvm_field_aa_int_int_unsigned"
2192 "`uvm_field_aa_int_integer"
2193 "`uvm_field_aa_int_integer_unsigned"
2194 "`uvm_field_aa_int_key"
2195 "`uvm_field_aa_int_longint"
2196 "`uvm_field_aa_int_longint_unsigned"
2197 "`uvm_field_aa_int_shortint"
2198 "`uvm_field_aa_int_shortint_unsigned"
2199 "`uvm_field_aa_int_string"
2200 "`uvm_field_aa_object_int"
2201 "`uvm_field_aa_object_string"
2202 "`uvm_field_aa_string_int"
2203 "`uvm_field_aa_string_string"
2204 "`uvm_field_array_enum"
2205 "`uvm_field_array_int"
2206 "`uvm_field_array_object"
2207 "`uvm_field_array_string"
2208 "`uvm_field_enum"
2209 "`uvm_field_event"
2210 "`uvm_field_int"
2211 "`uvm_field_object"
2212 "`uvm_field_queue_enum"
2213 "`uvm_field_queue_int"
2214 "`uvm_field_queue_object"
2215 "`uvm_field_queue_string"
2216 "`uvm_field_real"
2217 "`uvm_field_sarray_enum"
2218 "`uvm_field_sarray_int"
2219 "`uvm_field_sarray_object"
2220 "`uvm_field_sarray_string"
2221 "`uvm_field_string"
2222 "`uvm_field_utils"
2223 "`uvm_file" ; Undocumented in 1.1, use `__FILE__
2224 "`uvm_get_imp_decl"
2225 "`uvm_get_peek_imp_decl"
2226 "`uvm_info"
2227 "`uvm_info_context"
2228 "`uvm_line" ; Undocumented in 1.1, use `__LINE__
2229 "`uvm_master_imp_decl"
2230 "`uvm_non_blocking_transport_imp_decl" ; Deprecated in 1.1
2231 "`uvm_nonblocking_get_imp_decl"
2232 "`uvm_nonblocking_get_peek_imp_decl"
2233 "`uvm_nonblocking_master_imp_decl"
2234 "`uvm_nonblocking_peek_imp_decl"
2235 "`uvm_nonblocking_put_imp_decl"
2236 "`uvm_nonblocking_slave_imp_decl"
2237 "`uvm_nonblocking_transport_imp_decl"
2238 "`uvm_object_param_utils"
2239 "`uvm_object_registry"
2240 "`uvm_object_registry_param" ; Undocumented in 1.1
2241 "`uvm_object_utils"
2242 "`uvm_pack_array"
2243 "`uvm_pack_arrayN"
2244 "`uvm_pack_enum"
2245 "`uvm_pack_enumN"
2246 "`uvm_pack_int"
2247 "`uvm_pack_intN"
2248 "`uvm_pack_queue"
2249 "`uvm_pack_queueN"
2250 "`uvm_pack_real"
2251 "`uvm_pack_sarray"
2252 "`uvm_pack_sarrayN"
2253 "`uvm_pack_string"
2254 "`uvm_peek_imp_decl"
2255 "`uvm_put_imp_decl"
2256 "`uvm_rand_send"
2257 "`uvm_rand_send_pri"
2258 "`uvm_rand_send_pri_with"
2259 "`uvm_rand_send_with"
2260 "`uvm_record_attribute"
2261 "`uvm_record_field"
2262 "`uvm_register_cb"
2263 "`uvm_send"
2264 "`uvm_send_pri"
2265 "`uvm_sequence_utils" ; Deprecated in 1.1
2266 "`uvm_set_super_type"
2267 "`uvm_slave_imp_decl"
2268 "`uvm_transport_imp_decl"
2269 "`uvm_unpack_array"
2270 "`uvm_unpack_arrayN"
2271 "`uvm_unpack_enum"
2272 "`uvm_unpack_enumN"
2273 "`uvm_unpack_int"
2274 "`uvm_unpack_intN"
2275 "`uvm_unpack_queue"
2276 "`uvm_unpack_queueN"
2277 "`uvm_unpack_real"
2278 "`uvm_unpack_sarray"
2279 "`uvm_unpack_sarrayN"
2280 "`uvm_unpack_string"
2281 "`uvm_update_sequence_lib" ; Deprecated in 1.1
2282 "`uvm_update_sequence_lib_and_item" ; Deprecated in 1.1
2283 "`uvm_warning"
2284 "`uvm_warning_context") nil )))
2285
2286
2287 ;;
2288 ;; Regular expressions used to calculate indent, etc.
2289 ;;
2290 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
2291 ;; Want to match
2292 ;; aa :
2293 ;; aa,bb :
2294 ;; a[34:32] :
2295 ;; a,
2296 ;; b :
2297 (defconst verilog-assignment-operator-re
2298 (eval-when-compile
2299 (verilog-regexp-opt
2300 `(
2301 ;; blocking assignment_operator
2302 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2303 ;; non blocking assignment operator
2304 "<="
2305 ;; comparison
2306 "==" "!=" "===" "!==" "<=" ">=" "==?" "!=?" "<->"
2307 ;; event_trigger
2308 "->" "->>"
2309 ;; property_expr
2310 "|->" "|=>" "#-#" "#=#"
2311 ;; distribution weighting
2312 ":=" ":/"
2313 ) 't
2314 )))
2315 (defconst verilog-assignment-operation-re
2316 (concat
2317 ;; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2318 ;; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2319 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
2320
2321 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
2322 (defconst verilog-property-re
2323 (concat "\\(" verilog-label-re "\\)?"
2324 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2325 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2326
2327 (defconst verilog-no-indent-begin-re
2328 (eval-when-compile
2329 (verilog-regexp-words
2330 '("always" "always_comb" "always_ff" "always_latch" "initial" "final" ; procedural blocks
2331 "if" "else" ; conditional statements
2332 "while" "for" "foreach" "repeat" "do" "forever" )))) ; loop statements
2333
2334 (defconst verilog-ends-re
2335 ;; Parenthesis indicate type of keyword found
2336 (concat
2337 "\\(\\<else\\>\\)\\|" ; 1
2338 "\\(\\<if\\>\\)\\|" ; 2
2339 "\\(\\<assert\\>\\)\\|" ; 3
2340 "\\(\\<end\\>\\)\\|" ; 3.1
2341 "\\(\\<endcase\\>\\)\\|" ; 4
2342 "\\(\\<endfunction\\>\\)\\|" ; 5
2343 "\\(\\<endtask\\>\\)\\|" ; 6
2344 "\\(\\<endspecify\\>\\)\\|" ; 7
2345 "\\(\\<endtable\\>\\)\\|" ; 8
2346 "\\(\\<endgenerate\\>\\)\\|" ; 9
2347 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2348 "\\(\\<endclass\\>\\)\\|" ; 11
2349 "\\(\\<endgroup\\>\\)\\|" ; 12
2350 ;; VMM
2351 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2352 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2353 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2354 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2355 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
2356 ;; OVM
2357 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2358 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2359 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2360 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2361 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
2362 ;; UVM
2363 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2364 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2365 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2366 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2367 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
2368 ))
2369
2370 (defconst verilog-auto-end-comment-lines-re
2371 ;; Matches to names in this list cause auto-end-commenting
2372 (concat "\\("
2373 verilog-directive-re "\\)\\|\\("
2374 (eval-when-compile
2375 (verilog-regexp-words
2376 `( "begin"
2377 "else"
2378 "end"
2379 "endcase"
2380 "endclass"
2381 "endclocking"
2382 "endgroup"
2383 "endfunction"
2384 "endmodule"
2385 "endprogram"
2386 "endprimitive"
2387 "endinterface"
2388 "endpackage"
2389 "endsequence"
2390 "endproperty"
2391 "endspecify"
2392 "endtable"
2393 "endtask"
2394 "join"
2395 "join_any"
2396 "join_none"
2397 "module"
2398 "macromodule"
2399 "primitive"
2400 "interface"
2401 "package")))
2402 "\\)"))
2403
2404 ;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2405 ;; verilog-end-block-ordered-re matches exactly the same strings.
2406 (defconst verilog-end-block-ordered-re
2407 ;; Parenthesis indicate type of keyword found
2408 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2409 "\\(\\<end\\>\\)\\|" ; 2
2410 "\\(\\<end" ; 3, but not used
2411 "\\(" ; 4, but not used
2412 "\\(function\\)\\|" ; 5
2413 "\\(task\\)\\|" ; 6
2414 "\\(module\\)\\|" ; 7
2415 "\\(primitive\\)\\|" ; 8
2416 "\\(interface\\)\\|" ; 9
2417 "\\(package\\)\\|" ; 10
2418 "\\(class\\)\\|" ; 11
2419 "\\(group\\)\\|" ; 12
2420 "\\(program\\)\\|" ; 13
2421 "\\(sequence\\)\\|" ; 14
2422 "\\(clocking\\)\\|" ; 15
2423 "\\(property\\)\\|" ; 16
2424 "\\)\\>\\)"))
2425 (defconst verilog-end-block-re
2426 (eval-when-compile
2427 (verilog-regexp-words
2428
2429 `("end" ; closes begin
2430 "endcase" ; closes any of case, casex casez or randcase
2431 "join" "join_any" "join_none" ; closes fork
2432 "endclass"
2433 "endtable"
2434 "endspecify"
2435 "endfunction"
2436 "endgenerate"
2437 "endtask"
2438 "endgroup"
2439 "endproperty"
2440 "endinterface"
2441 "endpackage"
2442 "endprogram"
2443 "endsequence"
2444 "endclocking"
2445 ;; OVM
2446 "`ovm_component_utils_end"
2447 "`ovm_field_utils_end"
2448 "`ovm_object_utils_end"
2449 "`ovm_sequence_utils_end"
2450 "`ovm_sequencer_utils_end"
2451 ;; UVM
2452 "`uvm_component_utils_end"
2453 "`uvm_field_utils_end"
2454 "`uvm_object_utils_end"
2455 "`uvm_sequence_utils_end"
2456 "`uvm_sequencer_utils_end"
2457 ;; VMM
2458 "`vmm_data_member_end"
2459 "`vmm_env_member_end"
2460 "`vmm_scenario_member_end"
2461 "`vmm_subenv_member_end"
2462 "`vmm_xactor_member_end"
2463 ))))
2464
2465
2466 (defconst verilog-endcomment-reason-re
2467 ;; Parenthesis indicate type of keyword found
2468 (concat
2469 "\\(\\<begin\\>\\)\\|" ; 1
2470 "\\(\\<else\\>\\)\\|" ; 2
2471 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2472 "\\(\\<always\\(?:_ff\\)?\\>\\(?:[ \t]*@\\)\\)\\|" ; 4 (matches always or always_ff w/ @...)
2473 "\\(\\<always\\(?:_comb\\|_latch\\)?\\>\\)\\|" ; 5 (matches always, always_comb, always_latch w/o @...)
2474 "\\(\\<fork\\>\\)\\|" ; 7
2475 "\\(\\<if\\>\\)\\|"
2476 verilog-property-re "\\|"
2477 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
2478 "\\(\\<clocking\\>\\)\\|"
2479 "\\(\\<task\\>\\)\\|"
2480 "\\(\\<function\\>\\)\\|"
2481 "\\(\\<initial\\>\\)\\|"
2482 "\\(\\<interface\\>\\)\\|"
2483 "\\(\\<package\\>\\)\\|"
2484 "\\(\\<final\\>\\)\\|"
2485 "\\(@\\)\\|"
2486 "\\(\\<while\\>\\)\\|\\(\\<do\\>\\)\\|"
2487 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2488 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2489 "#"))
2490
2491 (defconst verilog-named-block-re "begin[ \t]*:")
2492
2493 ;; These words begin a block which can occur inside a module which should be indented,
2494 ;; and closed with the respective word from the end-block list
2495
2496 (defconst verilog-beg-block-re
2497 (eval-when-compile
2498 (verilog-regexp-words
2499 `("begin"
2500 "case" "casex" "casez" "randcase"
2501 "clocking"
2502 "generate"
2503 "fork"
2504 "function"
2505 "property"
2506 "specify"
2507 "table"
2508 "task"
2509 ;; OVM
2510 "`ovm_component_utils_begin"
2511 "`ovm_component_param_utils_begin"
2512 "`ovm_field_utils_begin"
2513 "`ovm_object_utils_begin"
2514 "`ovm_object_param_utils_begin"
2515 "`ovm_sequence_utils_begin"
2516 "`ovm_sequencer_utils_begin"
2517 ;; UVM
2518 "`uvm_component_utils_begin"
2519 "`uvm_component_param_utils_begin"
2520 "`uvm_field_utils_begin"
2521 "`uvm_object_utils_begin"
2522 "`uvm_object_param_utils_begin"
2523 "`uvm_sequence_utils_begin"
2524 "`uvm_sequencer_utils_begin"
2525 ;; VMM
2526 "`vmm_data_member_begin"
2527 "`vmm_env_member_begin"
2528 "`vmm_scenario_member_begin"
2529 "`vmm_subenv_member_begin"
2530 "`vmm_xactor_member_begin"
2531 ))))
2532 ;; These are the same words, in a specific order in the regular
2533 ;; expression so that matching will work nicely for
2534 ;; verilog-forward-sexp and verilog-calc-indent
2535 (defconst verilog-beg-block-re-ordered
2536 ( concat "\\(\\<begin\\>\\)" ;1
2537 "\\|\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
2538 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
2539 "\\|\\(\\<class\\>\\)" ;6
2540 "\\|\\(\\<table\\>\\)" ;7
2541 "\\|\\(\\<specify\\>\\)" ;8
2542 "\\|\\(\\<function\\>\\)" ;9
2543 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2544 "\\|\\(\\<task\\>\\)" ;14
2545 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2546 "\\|\\(\\<generate\\>\\)" ;18
2547 "\\|\\(\\<covergroup\\>\\)" ;16 20
2548 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2549 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2550 "\\|\\(\\<clocking\\>\\)" ;22 27
2551 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
2552 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
2553 ;;
2554 ))
2555
2556 (defconst verilog-end-block-ordered-rry
2557 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2558 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2559 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2560 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2561 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2562 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2563 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2564 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2565 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2566 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2567 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2568 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2569 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2570 ] )
2571
2572 (defconst verilog-nameable-item-re
2573 (eval-when-compile
2574 (verilog-regexp-words
2575 `("begin"
2576 "fork"
2577 "join" "join_any" "join_none"
2578 "end"
2579 "endcase"
2580 "endchecker"
2581 "endclass"
2582 "endclocking"
2583 "endconfig"
2584 "endfunction"
2585 "endgenerate"
2586 "endgroup"
2587 "endmodule"
2588 "endprimitive"
2589 "endinterface"
2590 "endpackage"
2591 "endprogram"
2592 "endproperty"
2593 "endsequence"
2594 "endspecify"
2595 "endtable"
2596 "endtask" )
2597 )))
2598
2599 (defconst verilog-declaration-opener
2600 (eval-when-compile
2601 (verilog-regexp-words
2602 `("module" "begin" "task" "function"))))
2603
2604 (defconst verilog-declaration-prefix-re
2605 (eval-when-compile
2606 (verilog-regexp-words
2607 `(
2608 ;; port direction
2609 "inout" "input" "output" "ref"
2610 ;; changeableness
2611 "const" "static" "protected" "local"
2612 ;; parameters
2613 "localparam" "parameter" "var"
2614 ;; type creation
2615 "typedef"
2616 ))))
2617 (defconst verilog-declaration-core-re
2618 (eval-when-compile
2619 (verilog-regexp-words
2620 `(
2621 ;; port direction (by themselves)
2622 "inout" "input" "output"
2623 ;; integer_atom_type
2624 "byte" "shortint" "int" "longint" "integer" "time"
2625 ;; integer_vector_type
2626 "bit" "logic" "reg"
2627 ;; non_integer_type
2628 "shortreal" "real" "realtime"
2629 ;; net_type
2630 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2631 ;; misc
2632 "string" "event" "chandle" "virtual" "enum" "genvar"
2633 "struct" "union"
2634 ;; builtin classes
2635 "mailbox" "semaphore"
2636 ))))
2637 (defconst verilog-declaration-re
2638 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2639 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2640 (defconst verilog-optional-signed-re "\\s-*\\(\\(un\\)?signed\\)?")
2641 (defconst verilog-optional-signed-range-re
2642 (concat
2643 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<\\(un\\)?signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2644 (defconst verilog-macroexp-re "`\\sw+")
2645
2646 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2647 (defconst verilog-declaration-re-2-no-macro
2648 (concat "\\s-*" verilog-declaration-re
2649 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2650 "\\)?"))
2651 (defconst verilog-declaration-re-2-macro
2652 (concat "\\s-*" verilog-declaration-re
2653 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2654 "\\|\\(" verilog-macroexp-re "\\)"
2655 "\\)?"))
2656 (defconst verilog-declaration-re-1-macro
2657 (concat "^" verilog-declaration-re-2-macro))
2658
2659 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2660
2661 (defconst verilog-defun-re
2662 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2663 (defconst verilog-end-defun-re
2664 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2665 (defconst verilog-zero-indent-re
2666 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2667 (defconst verilog-inst-comment-re
2668 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
2669
2670 (defconst verilog-behavioral-block-beg-re
2671 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2672 "function" "task"))))
2673 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2674 (defconst verilog-in-constraint-re ; keywords legal in constraint blocks starting a statement/block
2675 (eval-when-compile (verilog-regexp-words `("if" "else" "solve" "foreach"))))
2676
2677 (defconst verilog-indent-re
2678 (eval-when-compile
2679 (verilog-regexp-words
2680 `(
2681 "{"
2682 "always" "always_latch" "always_ff" "always_comb"
2683 "begin" "end"
2684 ;; "unique" "priority"
2685 "case" "casex" "casez" "randcase" "endcase"
2686 "class" "endclass"
2687 "clocking" "endclocking"
2688 "config" "endconfig"
2689 "covergroup" "endgroup"
2690 "fork" "join" "join_any" "join_none"
2691 "function" "endfunction"
2692 "final"
2693 "generate" "endgenerate"
2694 "initial"
2695 "interface" "endinterface"
2696 "module" "macromodule" "endmodule"
2697 "package" "endpackage"
2698 "primitive" "endprimitive"
2699 "program" "endprogram"
2700 "property" "endproperty"
2701 "sequence" "randsequence" "endsequence"
2702 "specify" "endspecify"
2703 "table" "endtable"
2704 "task" "endtask"
2705 "virtual"
2706 "`case"
2707 "`default"
2708 "`define" "`undef"
2709 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
2710 "`while" "`endwhile"
2711 "`for" "`endfor"
2712 "`format"
2713 "`include"
2714 "`let"
2715 "`protect" "`endprotect"
2716 "`switch" "`endswitch"
2717 "`timescale"
2718 "`time_scale"
2719 ;; OVM Begin tokens
2720 "`ovm_component_utils_begin"
2721 "`ovm_component_param_utils_begin"
2722 "`ovm_field_utils_begin"
2723 "`ovm_object_utils_begin"
2724 "`ovm_object_param_utils_begin"
2725 "`ovm_sequence_utils_begin"
2726 "`ovm_sequencer_utils_begin"
2727 ;; OVM End tokens
2728 "`ovm_component_utils_end"
2729 "`ovm_field_utils_end"
2730 "`ovm_object_utils_end"
2731 "`ovm_sequence_utils_end"
2732 "`ovm_sequencer_utils_end"
2733 ;; UVM Begin tokens
2734 "`uvm_component_utils_begin"
2735 "`uvm_component_param_utils_begin"
2736 "`uvm_field_utils_begin"
2737 "`uvm_object_utils_begin"
2738 "`uvm_object_param_utils_begin"
2739 "`uvm_sequence_utils_begin"
2740 "`uvm_sequencer_utils_begin"
2741 ;; UVM End tokens
2742 "`uvm_component_utils_end" ; Typo in spec, it's not uvm_component_end
2743 "`uvm_field_utils_end"
2744 "`uvm_object_utils_end"
2745 "`uvm_sequence_utils_end"
2746 "`uvm_sequencer_utils_end"
2747 ;; VMM Begin tokens
2748 "`vmm_data_member_begin"
2749 "`vmm_env_member_begin"
2750 "`vmm_scenario_member_begin"
2751 "`vmm_subenv_member_begin"
2752 "`vmm_xactor_member_begin"
2753 ;; VMM End tokens
2754 "`vmm_data_member_end"
2755 "`vmm_env_member_end"
2756 "`vmm_scenario_member_end"
2757 "`vmm_subenv_member_end"
2758 "`vmm_xactor_member_end"
2759 ))))
2760
2761 (defconst verilog-defun-level-not-generate-re
2762 (eval-when-compile
2763 (verilog-regexp-words
2764 `( "module" "macromodule" "primitive" "class" "program"
2765 "interface" "package" "config"))))
2766
2767 (defconst verilog-defun-level-re
2768 (eval-when-compile
2769 (verilog-regexp-words
2770 (append
2771 `( "module" "macromodule" "primitive" "class" "program"
2772 "interface" "package" "config")
2773 `( "initial" "final" "always" "always_comb" "always_ff"
2774 "always_latch" "endtask" "endfunction" )))))
2775
2776 (defconst verilog-defun-level-generate-only-re
2777 (eval-when-compile
2778 (verilog-regexp-words
2779 `( "initial" "final" "always" "always_comb" "always_ff"
2780 "always_latch" "endtask" "endfunction" ))))
2781
2782 (defconst verilog-cpp-level-re
2783 (eval-when-compile
2784 (verilog-regexp-words
2785 `(
2786 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2787 ))))
2788
2789 (defconst verilog-dpi-import-export-re
2790 (eval-when-compile
2791 "\\(\\<\\(import\\|export\\)\\>\\s-+\"DPI\\(-C\\)?\"\\s-+\\(\\<\\(context\\|pure\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\<\\(function\\|task\\)\\>\\)"
2792 ))
2793
2794 (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
2795 (defconst verilog-extended-case-re "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
2796 (defconst verilog-extended-complete-re
2797 (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
2798 "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
2799 "\\|\\(\\(\\<\\(import\\|export\\)\\>\\s-+\\)?\\(\"DPI\\(-C\\)?\"\\s-+\\)?\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\(function\\>\\|task\\>\\)\\)"
2800 "\\|" verilog-extended-case-re ))
2801 (defconst verilog-basic-complete-re
2802 (eval-when-compile
2803 (verilog-regexp-words
2804 `(
2805 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2806 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2807 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2808 ))))
2809 (defconst verilog-complete-reg
2810 (concat
2811 verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
2812
2813 (defconst verilog-end-statement-re
2814 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2815 verilog-end-block-re "\\)"))
2816
2817 (defconst verilog-endcase-re
2818 (concat verilog-extended-case-re "\\|"
2819 "\\(endcase\\)\\|"
2820 verilog-defun-re
2821 ))
2822
2823 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2824 "String used to mark beginning of excluded text.")
2825 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2826 "String used to mark end of excluded text.")
2827 (defconst verilog-preprocessor-re
2828 (eval-when-compile
2829 (concat
2830 ;; single words
2831 "\\(?:"
2832 (verilog-regexp-words
2833 `("`__FILE__"
2834 "`__LINE__"
2835 "`celldefine"
2836 "`else"
2837 "`end_keywords"
2838 "`endcelldefine"
2839 "`endif"
2840 "`nounconnected_drive"
2841 "`resetall"
2842 "`unconnected_drive"
2843 "`undefineall"))
2844 "\\)\\|\\(?:"
2845 ;; two words: i.e. `ifdef DEFINE
2846 "\\<\\(`elsif\\|`ifn?def\\|`undef\\|`default_nettype\\|`begin_keywords\\)\\>\\s-"
2847 "\\)\\|\\(?:"
2848 ;; `line number "filename" level
2849 "\\<\\(`line\\)\\>\\s-+[0-9]+\\s-+\"[^\"]+\"\\s-+[012]"
2850 "\\)\\|\\(?:"
2851 ;;`include "file" or `include <file>
2852 "\\<\\(`include\\)\\>\\s-+\\(?:\"[^\"]+\"\\|<[^>]+>\\)"
2853 "\\)\\|\\(?:"
2854 ;; `pragma <stuff> (no mention in IEEE 1800-2012 that pragma can span multiple lines
2855 "\\<\\(`pragma\\)\\>\\s-+.+$"
2856 "\\)\\|\\(?:"
2857 ;; `timescale time_unit / time_precision
2858 "\\<\\(`timescale\\)\\>\\s-+10\\{0,2\\}\\s-*[munpf]?s\\s-*\\/\\s-*10\\{0,2\\}\\s-*[munpf]?s"
2859 "\\)\\|\\(?:"
2860 ;; `define and `if can span multiple lines if line ends in '\'. NOTE: `if is not IEEE 1800-2012
2861 ;; from http://www.emacswiki.org/emacs/MultilineRegexp
2862 (concat "\\<\\(`define\\|`if\\)\\>" ; directive
2863 "\\s-+" ; separator
2864 "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
2865 "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
2866 "\\)\\|\\(?:"
2867 ;; `<macro>() : i.e. `uvm_info(a,b,c) or any other pre-defined macro
2868 ;; Since parameters inside the macro can have parentheses, and
2869 ;; the macro can span multiple lines, just look for the opening
2870 ;; parentheses and then continue to the end of the first
2871 ;; non-escaped EOL
2872 (concat "\\<`\\w+\\>\\s-*("
2873 "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
2874 "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
2875 "\\)"
2876 )))
2877
2878 (defconst verilog-keywords
2879 (append verilog-compiler-directives
2880 '(
2881 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2882 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2883 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2884 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2885 "config" "const" "constraint" "context" "continue" "cover"
2886 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2887 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2888 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2889 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2890 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2891 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2892 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2893 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2894 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2895 "include" "initial" "inout" "input" "inside" "instance" "int"
2896 "integer" "interface" "intersect" "join" "join_any" "join_none"
2897 "large" "liblist" "library" "local" "localparam" "logic"
2898 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2899 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2900 "notif0" "notif1" "null" "or" "output" "package" "packed"
2901 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2902 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2903 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2904 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2905 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2906 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2907 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2908 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2909 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2910 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2911 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2912 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2913 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2914 "wire" "with" "within" "wor" "xnor" "xor"
2915 ;; 1800-2009
2916 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2917 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2918 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2919 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2920 ;; 1800-2012
2921 "implements" "interconnect" "nettype" "soft"
2922 ))
2923 "List of Verilog keywords.")
2924
2925 (defconst verilog-comment-start-regexp "//\\|/\\*"
2926 "Dual comment value for `comment-start-regexp'.")
2927
2928 (defvar verilog-mode-syntax-table
2929 (let ((table (make-syntax-table)))
2930 ;; Populate the syntax TABLE.
2931 (modify-syntax-entry ?\\ "\\" table)
2932 (modify-syntax-entry ?+ "." table)
2933 (modify-syntax-entry ?- "." table)
2934 (modify-syntax-entry ?= "." table)
2935 (modify-syntax-entry ?% "." table)
2936 (modify-syntax-entry ?< "." table)
2937 (modify-syntax-entry ?> "." table)
2938 (modify-syntax-entry ?& "." table)
2939 (modify-syntax-entry ?| "." table)
2940 ;; FIXME: This goes against Emacs conventions. Use "_" syntax instead and
2941 ;; then use regexps with things like "\\_<...\\_>".
2942 (modify-syntax-entry ?` "w" table) ; ` is part of definition symbols in Verilog
2943 (modify-syntax-entry ?_ "w" table)
2944 (modify-syntax-entry ?\' "." table)
2945
2946 ;; Set up TABLE to handle block and line style comments.
2947 (if (featurep 'xemacs)
2948 (progn
2949 ;; XEmacs (formerly Lucid) has the best implementation
2950 (modify-syntax-entry ?/ ". 1456" table)
2951 (modify-syntax-entry ?* ". 23" table)
2952 (modify-syntax-entry ?\n "> b" table))
2953 ;; Emacs does things differently, but we can work with it
2954 (modify-syntax-entry ?/ ". 124b" table)
2955 (modify-syntax-entry ?* ". 23" table)
2956 (modify-syntax-entry ?\n "> b" table))
2957 table)
2958 "Syntax table used in Verilog mode buffers.")
2959
2960 (defvar verilog-font-lock-keywords nil
2961 "Default highlighting for Verilog mode.")
2962
2963 (defvar verilog-font-lock-keywords-1 nil
2964 "Subdued level highlighting for Verilog mode.")
2965
2966 (defvar verilog-font-lock-keywords-2 nil
2967 "Medium level highlighting for Verilog mode.
2968 See also `verilog-font-lock-extra-types'.")
2969
2970 (defvar verilog-font-lock-keywords-3 nil
2971 "Gaudy level highlighting for Verilog mode.
2972 See also `verilog-font-lock-extra-types'.")
2973
2974 (defvar verilog-font-lock-translate-off-face
2975 'verilog-font-lock-translate-off-face
2976 "Font to use for translated off regions.")
2977 (defface verilog-font-lock-translate-off-face
2978 '((((class color)
2979 (background light))
2980 (:background "gray90" :italic t ))
2981 (((class color)
2982 (background dark))
2983 (:background "gray10" :italic t ))
2984 (((class grayscale) (background light))
2985 (:foreground "DimGray" :italic t))
2986 (((class grayscale) (background dark))
2987 (:foreground "LightGray" :italic t))
2988 (t (:italis t)))
2989 "Font lock mode face used to background highlight translate-off regions."
2990 :group 'font-lock-highlighting-faces)
2991
2992 (defvar verilog-font-lock-p1800-face
2993 'verilog-font-lock-p1800-face
2994 "Font to use for p1800 keywords.")
2995 (defface verilog-font-lock-p1800-face
2996 '((((class color)
2997 (background light))
2998 (:foreground "DarkOrange3" :bold t ))
2999 (((class color)
3000 (background dark))
3001 (:foreground "orange1" :bold t ))
3002 (t (:italic t)))
3003 "Font lock mode face used to highlight P1800 keywords."
3004 :group 'font-lock-highlighting-faces)
3005
3006 (defvar verilog-font-lock-ams-face
3007 'verilog-font-lock-ams-face
3008 "Font to use for Analog/Mixed Signal keywords.")
3009 (defface verilog-font-lock-ams-face
3010 '((((class color)
3011 (background light))
3012 (:foreground "Purple" :bold t ))
3013 (((class color)
3014 (background dark))
3015 (:foreground "orange1" :bold t ))
3016 (t (:italic t)))
3017 "Font lock mode face used to highlight AMS keywords."
3018 :group 'font-lock-highlighting-faces)
3019
3020 (defvar verilog-font-grouping-keywords-face
3021 'verilog-font-lock-grouping-keywords-face
3022 "Font to use for Verilog Grouping Keywords (such as begin..end).")
3023 (defface verilog-font-lock-grouping-keywords-face
3024 '((((class color)
3025 (background light))
3026 (:foreground "Purple" :bold t ))
3027 (((class color)
3028 (background dark))
3029 (:foreground "orange1" :bold t ))
3030 (t (:italic t)))
3031 "Font lock mode face used to highlight verilog grouping keywords."
3032 :group 'font-lock-highlighting-faces)
3033
3034 (let* ((verilog-type-font-keywords
3035 (eval-when-compile
3036 (verilog-regexp-opt
3037 '(
3038 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
3039 "event" "genvar" "inout" "input" "integer" "localparam"
3040 "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0" "notif1" "or"
3041 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
3042 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
3043 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
3044 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
3045 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
3046 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
3047 ) nil )))
3048
3049 (verilog-pragma-keywords
3050 (eval-when-compile
3051 (verilog-regexp-opt
3052 '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
3053 ) nil )))
3054
3055 (verilog-1800-2005-keywords
3056 (eval-when-compile
3057 (verilog-regexp-opt
3058 '("alias" "assert" "assume" "automatic" "before" "bind"
3059 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
3060 "clocking" "config" "const" "constraint" "context" "continue"
3061 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
3062 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
3063 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
3064 "expect" "export" "extends" "extern" "first_match" "foreach"
3065 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
3066 "illegal_bins" "import" "incdir" "include" "inside" "instance"
3067 "int" "intersect" "large" "liblist" "library" "local" "longint"
3068 "matches" "medium" "modport" "new" "noshowcancelled" "null"
3069 "packed" "program" "property" "protected" "pull0" "pull1"
3070 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
3071 "randcase" "randsequence" "ref" "release" "return" "scalared"
3072 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
3073 "specparam" "static" "string" "strong0" "strong1" "struct"
3074 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
3075 "type" "union" "unsigned" "use" "var" "virtual" "void"
3076 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
3077 ) nil )))
3078
3079 (verilog-1800-2009-keywords
3080 (eval-when-compile
3081 (verilog-regexp-opt
3082 '("accept_on" "checker" "endchecker" "eventually" "global"
3083 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
3084 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
3085 "sync_accept_on" "sync_reject_on" "unique0" "until"
3086 "until_with" "untyped" "weak" ) nil )))
3087
3088 (verilog-1800-2012-keywords
3089 (eval-when-compile
3090 (verilog-regexp-opt
3091 '("implements" "interconnect" "nettype" "soft" ) nil )))
3092
3093 (verilog-ams-keywords
3094 (eval-when-compile
3095 (verilog-regexp-opt
3096 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
3097 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
3098 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
3099 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
3100 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
3101 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
3102 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
3103 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
3104 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
3105 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
3106 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
3107
3108 (verilog-font-keywords
3109 (eval-when-compile
3110 (verilog-regexp-opt
3111 '(
3112 "assign" "case" "casex" "casez" "randcase" "deassign"
3113 "default" "disable" "else" "endcase" "endfunction"
3114 "endgenerate" "endinterface" "endmodule" "endprimitive"
3115 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
3116 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
3117 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
3118 "package" "endpackage" "always" "always_comb" "always_ff"
3119 "always_latch" "posedge" "primitive" "priority" "release"
3120 "repeat" "specify" "table" "task" "unique" "wait" "while"
3121 "class" "program" "endclass" "endprogram"
3122 ) nil )))
3123
3124 (verilog-font-grouping-keywords
3125 (eval-when-compile
3126 (verilog-regexp-opt
3127 '( "begin" "end" ) nil ))))
3128
3129 (setq verilog-font-lock-keywords
3130 (list
3131 ;; Fontify all builtin keywords
3132 (concat "\\<\\(" verilog-font-keywords "\\|"
3133 ;; And user/system tasks and functions
3134 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
3135 "\\)\\>")
3136 ;; Fontify all types
3137 (if verilog-highlight-grouping-keywords
3138 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3139 'verilog-font-lock-grouping-keywords-face)
3140 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
3141 'font-lock-type-face))
3142 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
3143 'font-lock-type-face)
3144 ;; Fontify IEEE-1800-2005 keywords appropriately
3145 (if verilog-highlight-p1800-keywords
3146 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3147 'verilog-font-lock-p1800-face)
3148 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
3149 'font-lock-type-face))
3150 ;; Fontify IEEE-1800-2009 keywords appropriately
3151 (if verilog-highlight-p1800-keywords
3152 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3153 'verilog-font-lock-p1800-face)
3154 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
3155 'font-lock-type-face))
3156 ;; Fontify IEEE-1800-2012 keywords appropriately
3157 (if verilog-highlight-p1800-keywords
3158 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3159 'verilog-font-lock-p1800-face)
3160 (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
3161 'font-lock-type-face))
3162 ;; Fontify Verilog-AMS keywords
3163 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
3164 'verilog-font-lock-ams-face)))
3165
3166 (setq verilog-font-lock-keywords-1
3167 (append verilog-font-lock-keywords
3168 (list
3169 ;; Fontify module definitions
3170 (list
3171 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
3172 '(1 font-lock-keyword-face)
3173 '(3 font-lock-function-name-face 'prepend))
3174 ;; Fontify function definitions
3175 (list
3176 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
3177 '(1 font-lock-keyword-face)
3178 '(3 font-lock-constant-face prepend))
3179 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
3180 (1 font-lock-keyword-face)
3181 (2 font-lock-constant-face append))
3182 '("\\<function\\>\\s-+\\(\\sw+\\)"
3183 1 'font-lock-constant-face append))))
3184
3185 (setq verilog-font-lock-keywords-2
3186 (append verilog-font-lock-keywords-1
3187 (list
3188 ;; Fontify pragmas
3189 (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
3190 ;; Fontify escaped names
3191 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
3192 ;; Fontify macro definitions/ uses
3193 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
3194 'font-lock-preprocessor-face
3195 'font-lock-type-face))
3196 ;; Fontify delays/numbers
3197 '("\\(@\\)\\|\\([ \t\n\f\r]#\\s-*\\(\\([0-9_.]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
3198 0 font-lock-type-face append)
3199 ;; Fontify property/sequence cycle delays - these start with '##'
3200 '("\\(##\\(\\sw+\\|\\[[^]]+\\]\\)\\)"
3201 0 font-lock-type-face append)
3202 ;; Fontify instantiation names
3203 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
3204 )))
3205
3206 (setq verilog-font-lock-keywords-3
3207 (append verilog-font-lock-keywords-2
3208 (when verilog-highlight-translate-off
3209 (list
3210 ;; Fontify things in translate off regions
3211 '(verilog-match-translate-off
3212 (0 'verilog-font-lock-translate-off-face prepend))
3213 )))))
3214
3215 ;;
3216 ;; Buffer state preservation
3217
3218 (defmacro verilog-save-buffer-state (&rest body)
3219 "Execute BODY forms, saving state around insignificant change.
3220 Changes in text properties like `face' or `syntax-table' are
3221 considered insignificant. This macro allows text properties to
3222 be changed, even in a read-only buffer.
3223
3224 A change is considered significant if it affects the buffer text
3225 in any way that isn't completely restored again. Any
3226 user-visible changes to the buffer must not be within a
3227 `verilog-save-buffer-state'."
3228 ;; From c-save-buffer-state
3229 `(let* ((modified (buffer-modified-p))
3230 (buffer-undo-list t)
3231 (inhibit-read-only t)
3232 (inhibit-point-motion-hooks t)
3233 (verilog-no-change-functions t)
3234 before-change-functions
3235 after-change-functions
3236 deactivate-mark
3237 buffer-file-name ; Prevent primitives checking
3238 buffer-file-truename) ; for file modification
3239 (unwind-protect
3240 (progn ,@body)
3241 (and (not modified)
3242 (buffer-modified-p)
3243 (set-buffer-modified-p nil)))))
3244
3245 (defmacro verilog-save-no-change-functions (&rest body)
3246 "Execute BODY forms, disabling all change hooks in BODY.
3247 For insignificant changes, see instead `verilog-save-buffer-state'."
3248 `(let* ((inhibit-point-motion-hooks t)
3249 (verilog-no-change-functions t)
3250 before-change-functions
3251 after-change-functions)
3252 (progn ,@body)))
3253
3254 (defvar verilog-save-font-mod-hooked nil
3255 "Local variable when inside a `verilog-save-font-mods' block.")
3256 (make-variable-buffer-local 'verilog-save-font-mod-hooked)
3257
3258 (defmacro verilog-save-font-mods (&rest body)
3259 "Execute BODY forms, disabling text modifications to allow performing BODY.
3260 Includes temporary disabling of `font-lock' to restore the buffer
3261 to full text form for parsing. Additional actions may be specified with
3262 `verilog-before-save-font-hook' and `verilog-after-save-font-hook'."
3263 ;; Before version 20, match-string with font-lock returns a
3264 ;; vector that is not equal to the string. IE if on "input"
3265 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
3266 `(let* ((hooked (unless verilog-save-font-mod-hooked
3267 (verilog-run-hooks 'verilog-before-save-font-hook)
3268 t))
3269 (verilog-save-font-mod-hooked t)
3270 (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
3271 (font-lock-mode 0)
3272 t)))
3273 (unwind-protect
3274 (progn ,@body)
3275 ;; Unwind forms
3276 (when fontlocked (font-lock-mode t))
3277 (when hooked (verilog-run-hooks 'verilog-after-save-font-hook)))))
3278
3279 ;;
3280 ;; Comment detection and caching
3281
3282 (defvar verilog-scan-cache-preserving nil
3283 "If true, the specified buffer's comment properties are static.
3284 Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
3285 and `verilog-scan'.")
3286
3287 (defvar verilog-scan-cache-tick nil
3288 "Modification tick at which `verilog-scan' was last completed.")
3289 (make-variable-buffer-local 'verilog-scan-cache-tick)
3290
3291 (defun verilog-scan-cache-flush ()
3292 "Flush the `verilog-scan' cache."
3293 (setq verilog-scan-cache-tick nil))
3294
3295 (defun verilog-scan-cache-ok-p ()
3296 "Return t if the scan cache is up to date."
3297 (or (and verilog-scan-cache-preserving
3298 (eq verilog-scan-cache-preserving (current-buffer))
3299 verilog-scan-cache-tick)
3300 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3301
3302 (defmacro verilog-save-scan-cache (&rest body)
3303 "Execute the BODY forms, allowing scan cache preservation within BODY.
3304 This requires that insertions must use `verilog-insert'."
3305 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3306 ;; Note this must work properly if there's multiple layers of calls
3307 ;; to verilog-save-scan-cache even with differing ticks.
3308 `(progn
3309 (unless (verilog-scan-cache-ok-p) ; Must be before let
3310 (setq verilog-scan-cache-tick nil))
3311 (let* ((verilog-scan-cache-preserving (current-buffer)))
3312 (progn ,@body))))
3313
3314 (defun verilog-scan-region (beg end)
3315 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3316 This creates v-cmts properties where comments are in force."
3317 ;; Why properties and not overlays? Overlays have much slower non O(1)
3318 ;; lookup times.
3319 ;; This function is warm - called on every verilog-insert
3320 (save-excursion
3321 (save-match-data
3322 (verilog-save-buffer-state
3323 (let (pt)
3324 (goto-char beg)
3325 (while (< (point) end)
3326 (cond ((looking-at "//")
3327 (setq pt (point))
3328 (or (search-forward "\n" end t)
3329 (goto-char end))
3330 ;; "1+": The leading // or /* itself isn't considered as
3331 ;; being "inside" the comment, so that a (search-backward)
3332 ;; that lands at the start of the // won't mis-indicate
3333 ;; it's inside a comment. Also otherwise it would be
3334 ;; hard to find a commented out /*AS*/ vs one that isn't
3335 (put-text-property (1+ pt) (point) 'v-cmts t))
3336 ((looking-at "/\\*")
3337 (setq pt (point))
3338 (or (search-forward "*/" end t)
3339 ;; No error - let later code indicate it so we can
3340 ;; use inside functions on-the-fly
3341 ;;(error "%s: Unmatched /* */, at char %d"
3342 ;; (verilog-point-text) (point))
3343 (goto-char end))
3344 (put-text-property (1+ pt) (point) 'v-cmts t))
3345 ((looking-at "\"")
3346 (setq pt (point))
3347 (or (re-search-forward "[^\\]\"" end t) ; don't forward-char first, since we look for a non backslash first
3348 ;; No error - let later code indicate it so we can
3349 (goto-char end))
3350 (put-text-property (1+ pt) (point) 'v-cmts t))
3351 (t
3352 (forward-char 1)
3353 (if (re-search-forward "[/\"]" end t)
3354 (backward-char 1)
3355 (goto-char end))))))))))
3356
3357 (defun verilog-scan ()
3358 "Parse the buffer, marking all comments with properties.
3359 Also assumes any text inserted since `verilog-scan-cache-tick'
3360 either is ok to parse as a non-comment, or `verilog-insert' was used."
3361 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
3362 (unless (verilog-scan-cache-ok-p)
3363 (save-excursion
3364 (verilog-save-buffer-state
3365 (when verilog-debug
3366 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3367 verilog-scan-cache-preserving verilog-scan-cache-tick
3368 (buffer-chars-modified-tick)))
3369 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
3370 (verilog-scan-region (point-min) (point-max))
3371 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
3372 (when verilog-debug (message "Scanning... done"))))))
3373
3374 (defun verilog-scan-debug ()
3375 "For debugging, show with display face results of `verilog-scan'."
3376 (font-lock-mode 0)
3377 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3378 (save-excursion
3379 (goto-char (point-min))
3380 (remove-text-properties (point-min) (point-max) '(face nil))
3381 (while (not (eobp))
3382 (cond ((get-text-property (point) 'v-cmts)
3383 (put-text-property (point) (1+ (point)) `face 'underline)
3384 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3385 (forward-char 1))
3386 (t
3387 (goto-char (or (next-property-change (point)) (point-max))))))))
3388
3389 (defun verilog-scan-and-debug ()
3390 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3391 (let (verilog-scan-cache-preserving
3392 verilog-scan-cache-tick)
3393 (goto-char (point-min))
3394 (verilog-scan)
3395 (verilog-scan-debug)))
3396
3397 (defun verilog-inside-comment-or-string-p (&optional pos)
3398 "Check if optional point POS is inside a comment.
3399 This may require a slow pre-parse of the buffer with `verilog-scan'
3400 to establish comment properties on all text."
3401 ;; This function is very hot
3402 (verilog-scan)
3403 (if pos
3404 (and (>= pos (point-min))
3405 (get-text-property pos 'v-cmts))
3406 (get-text-property (point) 'v-cmts)))
3407
3408 (defun verilog-insert (&rest stuff)
3409 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
3410 Any insert that includes a comment must have the entire comment
3411 inserted using a single call to `verilog-insert'."
3412 (let ((pt (point)))
3413 (while stuff
3414 (insert (car stuff))
3415 (setq stuff (cdr stuff)))
3416 (verilog-scan-region pt (point))))
3417
3418 ;; More searching
3419
3420 (defun verilog-declaration-end ()
3421 (search-forward ";"))
3422
3423 (defun verilog-point-text (&optional pointnum)
3424 "Return text describing where POINTNUM or current point is (for errors).
3425 Use filename, if current buffer being edited shorten to just buffer name."
3426 (concat (or (and (equal (window-buffer) (current-buffer))
3427 (buffer-name))
3428 buffer-file-name
3429 (buffer-name))
3430 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
3431
3432 (defun electric-verilog-backward-sexp ()
3433 "Move backward over one balanced expression."
3434 (interactive)
3435 ;; before that see if we are in a comment
3436 (verilog-backward-sexp))
3437
3438 (defun electric-verilog-forward-sexp ()
3439 "Move forward over one balanced expression."
3440 (interactive)
3441 ;; before that see if we are in a comment
3442 (verilog-forward-sexp))
3443
3444 (defun verilog-forward-sexp-function (arg)
3445 "Move forward ARG sexps."
3446 ;; Used by hs-minor-mode
3447 (if (< arg 0)
3448 (verilog-backward-sexp)
3449 (verilog-forward-sexp)))
3450
3451 (defun verilog-backward-sexp ()
3452 (let ((reg)
3453 (elsec 1)
3454 (found nil)
3455 (st (point)))
3456 (if (not (looking-at "\\<"))
3457 (forward-word -1))
3458 (cond
3459 ((verilog-skip-backward-comment-or-string))
3460 ((looking-at "\\<else\\>")
3461 (setq reg (concat
3462 verilog-end-block-re
3463 "\\|\\(\\<else\\>\\)"
3464 "\\|\\(\\<if\\>\\)"))
3465 (while (and (not found)
3466 (verilog-re-search-backward reg nil 'move))
3467 (cond
3468 ((match-end 1) ; matched verilog-end-block-re
3469 ;; try to leap back to matching outward block by striding across
3470 ;; indent level changing tokens then immediately
3471 ;; previous line governs indentation.
3472 (verilog-leap-to-head))
3473 ((match-end 2) ; else, we're in deep
3474 (setq elsec (1+ elsec)))
3475 ((match-end 3) ; found it
3476 (setq elsec (1- elsec))
3477 (if (= 0 elsec)
3478 ;; Now previous line describes syntax
3479 (setq found 't))))))
3480 ((looking-at verilog-end-block-re)
3481 (verilog-leap-to-head))
3482 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3483 (cond
3484 ((match-end 1)
3485 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3486 ((match-end 2)
3487 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3488 ((match-end 3)
3489 (verilog-re-search-backward "\\<class\\>" nil 'move))
3490 ((match-end 4)
3491 (verilog-re-search-backward "\\<program\\>" nil 'move))
3492 ((match-end 5)
3493 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3494 ((match-end 6)
3495 (verilog-re-search-backward "\\<package\\>" nil 'move))
3496 (t
3497 (goto-char st)
3498 (backward-sexp 1))))
3499 (t
3500 (goto-char st)
3501 (backward-sexp)))))
3502
3503 (defun verilog-forward-sexp ()
3504 (let ((reg)
3505 (md 2)
3506 (st (point))
3507 (nest 'yes))
3508 (if (not (looking-at "\\<"))
3509 (forward-word -1))
3510 (cond
3511 ((verilog-skip-forward-comment-or-string)
3512 (verilog-forward-syntactic-ws))
3513 ((looking-at verilog-beg-block-re-ordered)
3514 (cond
3515 ((match-end 1);
3516 ;; Search forward for matching end
3517 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
3518 ((match-end 2)
3519 ;; Search forward for matching endcase
3520 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique0?\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3521 (setq md 3) ; ender is third item in regexp
3522 )
3523 ((match-end 4)
3524 ;; might be "disable fork" or "wait fork"
3525 (let
3526 (here)
3527 (if (or
3528 (looking-at verilog-disable-fork-re)
3529 (and (looking-at "fork")
3530 (progn
3531 (setq here (point)) ; sometimes a fork is just a fork
3532 (forward-word -1)
3533 (looking-at verilog-disable-fork-re))))
3534 (progn ; it is a disable fork; ignore it
3535 (goto-char (match-end 0))
3536 (forward-word 1)
3537 (setq reg nil))
3538 (progn ; it is a nice simple fork
3539 (goto-char here) ; return from looking for "disable fork"
3540 ;; Search forward for matching join
3541 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
3542 ((match-end 6)
3543 ;; Search forward for matching endclass
3544 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
3545
3546 ((match-end 7)
3547 ;; Search forward for matching endtable
3548 (setq reg "\\<endtable\\>" )
3549 (setq nest 'no))
3550 ((match-end 8)
3551 ;; Search forward for matching endspecify
3552 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3553 ((match-end 9)
3554 ;; Search forward for matching endfunction
3555 (setq reg "\\<endfunction\\>" )
3556 (setq nest 'no))
3557 ((match-end 10)
3558 ;; Search forward for matching endfunction
3559 (setq reg "\\<endfunction\\>" )
3560 (setq nest 'no))
3561 ((match-end 14)
3562 ;; Search forward for matching endtask
3563 (setq reg "\\<endtask\\>" )
3564 (setq nest 'no))
3565 ((match-end 15)
3566 ;; Search forward for matching endtask
3567 (setq reg "\\<endtask\\>" )
3568 (setq nest 'no))
3569 ((match-end 19)
3570 ;; Search forward for matching endgenerate
3571 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
3572 ((match-end 20)
3573 ;; Search forward for matching endgroup
3574 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
3575 ((match-end 21)
3576 ;; Search forward for matching endproperty
3577 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
3578 ((match-end 25)
3579 ;; Search forward for matching endsequence
3580 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3581 (setq md 3)) ; 3 to get to endsequence in the reg above
3582 ((match-end 27)
3583 ;; Search forward for matching endclocking
3584 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
3585 (if (and reg
3586 (forward-word 1))
3587 (catch 'skip
3588 (if (eq nest 'yes)
3589 (let ((depth 1)
3590 here)
3591 (while (verilog-re-search-forward reg nil 'move)
3592 (cond
3593 ((match-end md) ; a closer in regular expression, so we are climbing out
3594 (setq depth (1- depth))
3595 (if (= 0 depth) ; we are out!
3596 (throw 'skip 1)))
3597 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3598 (setq here (point)) ; remember where we started
3599 (goto-char (match-beginning 1))
3600 (cond
3601 ((if (or
3602 (looking-at verilog-disable-fork-re)
3603 (and (looking-at "fork")
3604 (progn
3605 (forward-word -1)
3606 (looking-at verilog-disable-fork-re))))
3607 (progn ; it is a disable fork; another false alarm
3608 (goto-char (match-end 0)))
3609 (progn ; it is a simple fork (or has nothing to do with fork)
3610 (goto-char here)
3611 (setq depth (1+ depth))))))))))
3612 (if (verilog-re-search-forward reg nil 'move)
3613 (throw 'skip 1))))))
3614
3615 ((looking-at (concat
3616 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3617 "\\(\\<primitive\\>\\)\\|"
3618 "\\(\\<class\\>\\)\\|"
3619 "\\(\\<program\\>\\)\\|"
3620 "\\(\\<interface\\>\\)\\|"
3621 "\\(\\<package\\>\\)"))
3622 (cond
3623 ((match-end 1)
3624 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3625 ((match-end 2)
3626 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3627 ((match-end 3)
3628 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3629 ((match-end 4)
3630 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3631 ((match-end 5)
3632 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3633 ((match-end 6)
3634 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3635 (t
3636 (goto-char st)
3637 (if (= (following-char) ?\) )
3638 (forward-char 1)
3639 (forward-sexp 1)))))
3640 (t
3641 (goto-char st)
3642 (if (= (following-char) ?\) )
3643 (forward-char 1)
3644 (forward-sexp 1))))))
3645
3646 (defun verilog-declaration-beg ()
3647 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3648
3649 ;;
3650 ;;
3651 ;; Mode
3652 ;;
3653 (defvar verilog-which-tool 1)
3654 ;;;###autoload
3655 (define-derived-mode verilog-mode prog-mode "Verilog"
3656 "Major mode for editing Verilog code.
3657 \\<verilog-mode-map>
3658 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3659 AUTOs can improve coding efficiency.
3660
3661 Use \\[verilog-faq] for a pointer to frequently asked questions.
3662
3663 NEWLINE, TAB indents for Verilog code.
3664 Delete converts tabs to spaces as it moves back.
3665
3666 Supports highlighting.
3667
3668 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3669 with no args, if that value is non-nil.
3670
3671 Variables controlling indentation/edit style:
3672
3673 variable `verilog-indent-level' (default 3)
3674 Indentation of Verilog statements with respect to containing block.
3675 `verilog-indent-level-module' (default 3)
3676 Absolute indentation of Module level Verilog statements.
3677 Set to 0 to get initial and always statements lined up
3678 on the left side of your screen.
3679 `verilog-indent-level-declaration' (default 3)
3680 Indentation of declarations with respect to containing block.
3681 Set to 0 to get them list right under containing block.
3682 `verilog-indent-level-behavioral' (default 3)
3683 Indentation of first begin in a task or function block
3684 Set to 0 to get such code to lined up underneath the task or
3685 function keyword.
3686 `verilog-indent-level-directive' (default 1)
3687 Indentation of \\=`ifdef/\\=`endif blocks.
3688 `verilog-cexp-indent' (default 1)
3689 Indentation of Verilog statements broken across lines i.e.:
3690 if (a)
3691 begin
3692 `verilog-case-indent' (default 2)
3693 Indentation for case statements.
3694 `verilog-auto-newline' (default nil)
3695 Non-nil means automatically newline after semicolons and the punctuation
3696 mark after an end.
3697 `verilog-auto-indent-on-newline' (default t)
3698 Non-nil means automatically indent line after newline.
3699 `verilog-tab-always-indent' (default t)
3700 Non-nil means TAB in Verilog mode should always reindent the current line,
3701 regardless of where in the line point is when the TAB command is used.
3702 `verilog-indent-begin-after-if' (default t)
3703 Non-nil means to indent begin statements following a preceding
3704 if, else, while, for and repeat statements, if any. Otherwise,
3705 the begin is lined up with the preceding token. If t, you get:
3706 if (a)
3707 begin // amount of indent based on `verilog-cexp-indent'
3708 otherwise you get:
3709 if (a)
3710 begin
3711 `verilog-auto-endcomments' (default t)
3712 Non-nil means a comment /* ... */ is set after the ends which ends
3713 cases, tasks, functions and modules.
3714 The type and name of the object will be set between the braces.
3715 `verilog-minimum-comment-distance' (default 10)
3716 Minimum distance (in lines) between begin and end required before a comment
3717 will be inserted. Setting this variable to zero results in every
3718 end acquiring a comment; the default avoids too many redundant
3719 comments in tight quarters.
3720 `verilog-auto-lineup' (default `declarations')
3721 List of contexts where auto lineup of code should be done.
3722
3723 Variables controlling other actions:
3724
3725 `verilog-linter' (default `surelint')
3726 Unix program to call to run the lint checker. This is the default
3727 command for \\[compile-command] and \\[verilog-auto-save-compile].
3728
3729 See \\[customize] for the complete list of variables.
3730
3731 AUTO expansion functions are, in part:
3732
3733 \\[verilog-auto] Expand AUTO statements.
3734 \\[verilog-delete-auto] Remove the AUTOs.
3735 \\[verilog-inject-auto] Insert AUTOs for the first time.
3736
3737 Some other functions are:
3738
3739 \\[verilog-complete-word] Complete word with appropriate possibilities.
3740 \\[verilog-mark-defun] Mark function.
3741 \\[verilog-beg-of-defun] Move to beginning of current function.
3742 \\[verilog-end-of-defun] Move to end of current function.
3743 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3744
3745 \\[verilog-comment-region] Put marked area in a comment.
3746 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
3747 \\[verilog-insert-block] Insert begin ... end.
3748 \\[verilog-star-comment] Insert /* ... */.
3749
3750 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
3751 \\[verilog-sk-begin] Insert a begin .. end block.
3752 \\[verilog-sk-case] Insert a case block, prompting for details.
3753 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3754 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
3755 \\[verilog-sk-header] Insert a header block at the top of file.
3756 \\[verilog-sk-initial] Insert an initial begin .. end block.
3757 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3758 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
3759 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3760 \\[verilog-sk-uvm-object] Insert an UVM Object block.
3761 \\[verilog-sk-uvm-component] Insert an UVM Component block.
3762 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3763 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3764 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3765 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3766 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3767 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3768 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3769 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3770 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3771 \\[verilog-sk-comment] Insert a comment block.
3772 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3773 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3774 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3775 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3776 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3777 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3778 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3779 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3780 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3781
3782 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3783 Key bindings specific to `verilog-mode-map' are:
3784
3785 \\{verilog-mode-map}"
3786 :abbrev-table verilog-mode-abbrev-table
3787 (set (make-local-variable 'beginning-of-defun-function)
3788 'verilog-beg-of-defun)
3789 (set (make-local-variable 'end-of-defun-function)
3790 'verilog-end-of-defun)
3791 (set-syntax-table verilog-mode-syntax-table)
3792 (set (make-local-variable 'indent-line-function)
3793 #'verilog-indent-line-relative)
3794 (set (make-local-variable 'comment-indent-function) 'verilog-comment-indent)
3795 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
3796 (set (make-local-variable 'comment-start) "// ")
3797 (set (make-local-variable 'comment-end) "")
3798 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3799 (set (make-local-variable 'comment-multi-line) nil)
3800 ;; Set up for compilation
3801 (setq verilog-which-tool 1)
3802 (setq verilog-tool 'verilog-linter)
3803 (verilog-set-compile-command)
3804 (when (boundp 'hack-local-variables-hook) ; Also modify any file-local-variables
3805 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3806
3807 ;; Setting up menus
3808 (when (featurep 'xemacs)
3809 (easy-menu-add verilog-stmt-menu)
3810 (easy-menu-add verilog-menu)
3811 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
3812
3813 ;; Stuff for GNU Emacs
3814 (set (make-local-variable 'font-lock-defaults)
3815 `((verilog-font-lock-keywords
3816 verilog-font-lock-keywords-1
3817 verilog-font-lock-keywords-2
3818 verilog-font-lock-keywords-3)
3819 nil nil nil
3820 ,(if (functionp 'syntax-ppss)
3821 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3822 ;; font-lock-beginning-of-syntax-function, so
3823 ;; font-lock-beginning-of-syntax-function, can't use
3824 ;; verilog-beg-of-defun.
3825 nil
3826 'verilog-beg-of-defun)))
3827 ;;------------------------------------------------------------
3828 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
3829 ;; all buffer local:
3830 (unless noninteractive ; Else can't see the result, and change hooks are slow
3831 (when (featurep 'xemacs)
3832 (make-local-hook 'font-lock-mode-hook)
3833 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3834 (make-local-hook 'after-change-functions))
3835 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3836 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3837 (add-hook 'after-change-functions 'verilog-highlight-region t t))
3838
3839 ;; Tell imenu how to handle Verilog.
3840 (set (make-local-variable 'imenu-generic-expression)
3841 verilog-imenu-generic-expression)
3842 ;; Tell which-func-modes that imenu knows about verilog
3843 (when (and (boundp 'which-func-modes) (listp which-func-modes))
3844 (add-to-list 'which-func-modes 'verilog-mode))
3845 ;; hideshow support
3846 (when (boundp 'hs-special-modes-alist)
3847 (unless (assq 'verilog-mode hs-special-modes-alist)
3848 (setq hs-special-modes-alist
3849 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3850 verilog-forward-sexp-function)
3851 hs-special-modes-alist))))
3852
3853 ;; Stuff for autos
3854 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3855 ;; verilog-mode-hook call added by define-derived-mode
3856 )
3857 \f
3858 ;;; Electric functions:
3859 ;;
3860
3861 (defun electric-verilog-terminate-line (&optional arg)
3862 "Terminate line and indent next line.
3863 With optional ARG, remove existing end of line comments."
3864 (interactive)
3865 ;; before that see if we are in a comment
3866 (let ((state (save-excursion (verilog-syntax-ppss))))
3867 (cond
3868 ((nth 7 state) ; Inside // comment
3869 (if (eolp)
3870 (progn
3871 (delete-horizontal-space)
3872 (newline))
3873 (progn
3874 (newline)
3875 (insert "// ")
3876 (beginning-of-line)))
3877 (verilog-indent-line))
3878 ((nth 4 state) ; Inside any comment (hence /**/)
3879 (newline)
3880 (verilog-more-comment))
3881 ((eolp)
3882 ;; First, check if current line should be indented
3883 (if (save-excursion
3884 (delete-horizontal-space)
3885 (beginning-of-line)
3886 (skip-chars-forward " \t")
3887 (if (looking-at verilog-auto-end-comment-lines-re)
3888 (let ((indent-str (verilog-indent-line)))
3889 ;; Maybe we should set some endcomments
3890 (if verilog-auto-endcomments
3891 (verilog-set-auto-endcomments indent-str arg))
3892 (end-of-line)
3893 (delete-horizontal-space)
3894 (if arg
3895 ()
3896 (newline))
3897 nil)
3898 (progn
3899 (end-of-line)
3900 (delete-horizontal-space)
3901 't)))
3902 ;; see if we should line up assignments
3903 (progn
3904 (if (or (eq 'all verilog-auto-lineup)
3905 (eq 'assignments verilog-auto-lineup))
3906 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
3907 (newline))
3908 (forward-line 1))
3909 ;; Indent next line
3910 (if verilog-auto-indent-on-newline
3911 (verilog-indent-line)))
3912 (t
3913 (newline)))))
3914
3915 (defun electric-verilog-terminate-and-indent ()
3916 "Insert a newline and indent for the next statement."
3917 (interactive)
3918 (electric-verilog-terminate-line 1))
3919
3920 (defun electric-verilog-semi ()
3921 "Insert `;' character and reindent the line."
3922 (interactive)
3923 (verilog-insert-last-command-event)
3924
3925 (if (or (verilog-in-comment-or-string-p)
3926 (verilog-in-escaped-name-p))
3927 ()
3928 (save-excursion
3929 (beginning-of-line)
3930 (verilog-forward-ws&directives)
3931 (verilog-indent-line))
3932 (if (and verilog-auto-newline
3933 (not (verilog-parenthesis-depth)))
3934 (electric-verilog-terminate-line))))
3935
3936 (defun electric-verilog-semi-with-comment ()
3937 "Insert `;' character, reindent the line and indent for comment."
3938 (interactive)
3939 (insert ";")
3940 (save-excursion
3941 (beginning-of-line)
3942 (verilog-indent-line))
3943 (indent-for-comment))
3944
3945 (defun electric-verilog-colon ()
3946 "Insert `:' and do all indentations except line indent on this line."
3947 (interactive)
3948 (verilog-insert-last-command-event)
3949 ;; Do nothing if within string.
3950 (if (or
3951 (verilog-within-string)
3952 (not (verilog-in-case-region-p)))
3953 ()
3954 (save-excursion
3955 (let ((p (point))
3956 (lim (progn (verilog-beg-of-statement) (point))))
3957 (goto-char p)
3958 (verilog-backward-case-item lim)
3959 (verilog-indent-line)))
3960 ;; (let ((verilog-tab-always-indent nil))
3961 ;; (verilog-indent-line))
3962 ))
3963
3964 ;;(defun electric-verilog-equal ()
3965 ;; "Insert `=', and do indentation if within block."
3966 ;; (interactive)
3967 ;; (verilog-insert-last-command-event)
3968 ;; Could auto line up expressions, but not yet
3969 ;; (if (eq (car (verilog-calculate-indent)) 'block)
3970 ;; (let ((verilog-tab-always-indent nil))
3971 ;; (verilog-indent-command)))
3972 ;; )
3973
3974 (defun electric-verilog-tick ()
3975 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3976 (interactive)
3977 (verilog-insert-last-command-event)
3978 (save-excursion
3979 (if (verilog-in-directive-p)
3980 (verilog-indent-line))))
3981
3982 (defun electric-verilog-tab ()
3983 "Function called when TAB is pressed in Verilog mode."
3984 (interactive)
3985 ;; If verilog-tab-always-indent, indent the beginning of the line.
3986 (cond
3987 ;; The region is active, indent it.
3988 ((and (region-active-p)
3989 (not (eq (region-beginning) (region-end))))
3990 (indent-region (region-beginning) (region-end) nil))
3991 ((or verilog-tab-always-indent
3992 (save-excursion
3993 (skip-chars-backward " \t")
3994 (bolp)))
3995 (let* ((oldpnt (point))
3996 (boi-point
3997 (save-excursion
3998 (beginning-of-line)
3999 (skip-chars-forward " \t")
4000 (verilog-indent-line)
4001 (back-to-indentation)
4002 (point))))
4003 (if (< (point) boi-point)
4004 (back-to-indentation)
4005 (cond ((not verilog-tab-to-comment))
4006 ((not (eolp))
4007 (end-of-line))
4008 (t
4009 (indent-for-comment)
4010 (when (and (eolp) (= oldpnt (point)))
4011 ;; kill existing comment
4012 (beginning-of-line)
4013 (re-search-forward comment-start-skip oldpnt 'move)
4014 (goto-char (match-beginning 0))
4015 (skip-chars-backward " \t")
4016 (kill-region (point) oldpnt)))))))
4017 (t (progn (insert "\t")))))
4018
4019 \f
4020 ;;; Interactive functions:
4021 ;;
4022
4023 (defun verilog-indent-buffer ()
4024 "Indent-region the entire buffer as Verilog code.
4025 To call this from the command line, see \\[verilog-batch-indent]."
4026 (interactive)
4027 (verilog-mode)
4028 (indent-region (point-min) (point-max) nil))
4029
4030 (defun verilog-insert-block ()
4031 "Insert Verilog begin ... end; block in the code with right indentation."
4032 (interactive)
4033 (verilog-indent-line)
4034 (insert "begin")
4035 (electric-verilog-terminate-line)
4036 (save-excursion
4037 (electric-verilog-terminate-line)
4038 (insert "end")
4039 (beginning-of-line)
4040 (verilog-indent-line)))
4041
4042 (defun verilog-star-comment ()
4043 "Insert Verilog star comment at point."
4044 (interactive)
4045 (verilog-indent-line)
4046 (insert "/*")
4047 (save-excursion
4048 (newline)
4049 (insert " */"))
4050 (newline)
4051 (insert " * "))
4052
4053 (defun verilog-insert-1 (fmt max)
4054 "Use format string FMT to insert integers 0 to MAX - 1.
4055 Inserts one integer per line, at the current column. Stops early
4056 if it reaches the end of the buffer."
4057 (let ((col (current-column))
4058 (n 0))
4059 (save-excursion
4060 (while (< n max)
4061 (insert (format fmt n))
4062 (forward-line 1)
4063 ;; Note that this function does not bother to check for lines
4064 ;; shorter than col.
4065 (if (eobp)
4066 (setq n max)
4067 (setq n (1+ n))
4068 (move-to-column col))))))
4069
4070 (defun verilog-insert-indices (max)
4071 "Insert a set of indices into a rectangle.
4072 The upper left corner is defined by point. Indices begin with 0
4073 and extend to the MAX - 1. If no prefix arg is given, the user
4074 is prompted for a value. The indices are surrounded by square
4075 brackets []. For example, the following code with the point
4076 located after the first `a' gives:
4077
4078 a = b a[ 0] = b
4079 a = b a[ 1] = b
4080 a = b a[ 2] = b
4081 a = b a[ 3] = b
4082 a = b ==> insert-indices ==> a[ 4] = b
4083 a = b a[ 5] = b
4084 a = b a[ 6] = b
4085 a = b a[ 7] = b
4086 a = b a[ 8] = b"
4087
4088 (interactive "NMAX: ")
4089 (verilog-insert-1 "[%3d]" max))
4090
4091 (defun verilog-generate-numbers (max)
4092 "Insert a set of generated numbers into a rectangle.
4093 The upper left corner is defined by point. The numbers are padded to three
4094 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
4095 is supplied, then the user is prompted for the MAX number. Consider the
4096 following code fragment:
4097
4098 buf buf buf buf000
4099 buf buf buf buf001
4100 buf buf buf buf002
4101 buf buf buf buf003
4102 buf buf ==> generate-numbers ==> buf buf004
4103 buf buf buf buf005
4104 buf buf buf buf006
4105 buf buf buf buf007
4106 buf buf buf buf008"
4107
4108 (interactive "NMAX: ")
4109 (verilog-insert-1 "%3.3d" max))
4110
4111 (defun verilog-mark-defun ()
4112 "Mark the current Verilog function (or procedure).
4113 This puts the mark at the end, and point at the beginning."
4114 (interactive)
4115 (if (featurep 'xemacs)
4116 (progn
4117 (push-mark (point))
4118 (verilog-end-of-defun)
4119 (push-mark (point))
4120 (verilog-beg-of-defun)
4121 (if (fboundp 'zmacs-activate-region)
4122 (zmacs-activate-region)))
4123 (mark-defun)))
4124
4125 (defun verilog-comment-region (start end)
4126 ;; checkdoc-params: (start end)
4127 "Put the region into a Verilog comment.
4128 The comments that are in this area are \"deformed\":
4129 `*)' becomes `!(*' and `}' becomes `!{'.
4130 These deformed comments are returned to normal if you use
4131 \\[verilog-uncomment-region] to undo the commenting.
4132
4133 The commented area starts with `verilog-exclude-str-start', and ends with
4134 `verilog-exclude-str-end'. But if you change these variables,
4135 \\[verilog-uncomment-region] won't recognize the comments."
4136 (interactive "r")
4137 (save-excursion
4138 ;; Insert start and endcomments
4139 (goto-char end)
4140 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
4141 (not (save-excursion (skip-chars-backward " \t") (bolp))))
4142 (forward-line 1)
4143 (beginning-of-line))
4144 (insert verilog-exclude-str-end)
4145 (setq end (point))
4146 (newline)
4147 (goto-char start)
4148 (beginning-of-line)
4149 (insert verilog-exclude-str-start)
4150 (newline)
4151 ;; Replace end-comments within commented area
4152 (goto-char end)
4153 (save-excursion
4154 (while (re-search-backward "\\*/" start t)
4155 (replace-match "*-/" t t)))
4156 (save-excursion
4157 (let ((s+1 (1+ start)))
4158 (while (re-search-backward "/\\*" s+1 t)
4159 (replace-match "/-*" t t))))))
4160
4161 (defun verilog-uncomment-region ()
4162 "Uncomment a commented area; change deformed comments back to normal.
4163 This command does nothing if the pointer is not in a commented
4164 area. See also `verilog-comment-region'."
4165 (interactive)
4166 (save-excursion
4167 (let ((start (point))
4168 (end (point)))
4169 ;; Find the boundaries of the comment
4170 (save-excursion
4171 (setq start (progn (search-backward verilog-exclude-str-start nil t)
4172 (point)))
4173 (setq end (progn (search-forward verilog-exclude-str-end nil t)
4174 (point))))
4175 ;; Check if we're really inside a comment
4176 (if (or (equal start (point)) (<= end (point)))
4177 (message "Not standing within commented area.")
4178 (progn
4179 ;; Remove endcomment
4180 (goto-char end)
4181 (beginning-of-line)
4182 (let ((pos (point)))
4183 (end-of-line)
4184 (delete-region pos (1+ (point))))
4185 ;; Change comments back to normal
4186 (save-excursion
4187 (while (re-search-backward "\\*-/" start t)
4188 (replace-match "*/" t t)))
4189 (save-excursion
4190 (while (re-search-backward "/-\\*" start t)
4191 (replace-match "/*" t t)))
4192 ;; Remove start comment
4193 (goto-char start)
4194 (beginning-of-line)
4195 (let ((pos (point)))
4196 (end-of-line)
4197 (delete-region pos (1+ (point)))))))))
4198
4199 (defun verilog-beg-of-defun ()
4200 "Move backward to the beginning of the current function or procedure."
4201 (interactive)
4202 (verilog-re-search-backward verilog-defun-re nil 'move))
4203
4204 (defun verilog-beg-of-defun-quick ()
4205 "Move backward to the beginning of the current function or procedure.
4206 Uses `verilog-scan' cache."
4207 (interactive)
4208 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
4209
4210 (defun verilog-end-of-defun ()
4211 "Move forward to the end of the current function or procedure."
4212 (interactive)
4213 (verilog-re-search-forward verilog-end-defun-re nil 'move))
4214
4215 (defun verilog-get-end-of-defun ()
4216 (save-excursion
4217 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
4218 (point))
4219 (t
4220 (error "%s: Can't find endmodule" (verilog-point-text))
4221 (point-max)))))
4222
4223 (defun verilog-label-be ()
4224 "Label matching begin ... end, fork ... join and case ... endcase statements."
4225 (interactive)
4226 (let ((cnt 0)
4227 (case-fold-search nil)
4228 (oldpos (point))
4229 (b (progn
4230 (verilog-beg-of-defun)
4231 (point-marker)))
4232 (e (progn
4233 (verilog-end-of-defun)
4234 (point-marker))))
4235 (goto-char (marker-position b))
4236 (if (> (- e b) 200)
4237 (message "Relabeling module..."))
4238 (while (and
4239 (> (marker-position e) (point))
4240 (verilog-re-search-forward
4241 verilog-auto-end-comment-lines-re
4242 nil 'move))
4243 (goto-char (match-beginning 0))
4244 (let ((indent-str (verilog-indent-line)))
4245 (verilog-set-auto-endcomments indent-str 't)
4246 (end-of-line)
4247 (delete-horizontal-space))
4248 (setq cnt (1+ cnt))
4249 (if (= 9 (% cnt 10))
4250 (message "%d..." cnt)))
4251 (goto-char oldpos)
4252 (if (or
4253 (> (- e b) 200)
4254 (> cnt 20))
4255 (message "%d lines auto commented" cnt))))
4256
4257 (defun verilog-beg-of-statement ()
4258 "Move backward to beginning of statement."
4259 (interactive)
4260 ;; Move back token by token until we see the end
4261 ;; of some earlier line.
4262 (let (h)
4263 (while
4264 ;; If the current point does not begin a new
4265 ;; statement, as in the character ahead of us is a ';', or SOF
4266 ;; or the string after us unambiguously starts a statement,
4267 ;; or the token before us unambiguously ends a statement,
4268 ;; then move back a token and test again.
4269 (not (or
4270 ;; stop if beginning of buffer
4271 (bobp)
4272 ;; stop if looking at a pre-processor directive
4273 (looking-at "`\\w+")
4274 ;; stop if we find a ;
4275 (= (preceding-char) ?\;)
4276 ;; stop if we see a named coverpoint
4277 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4278 ;; keep going if we are in the middle of a word
4279 (not (or (looking-at "\\<") (forward-word -1)))
4280 ;; stop if we see an assertion (perhaps labeled)
4281 (and
4282 (looking-at "\\(\\w+\\W*:\\W*\\)?\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4283 (progn
4284 (setq h (point))
4285 (save-excursion
4286 (verilog-backward-token)
4287 (if (and (looking-at verilog-label-re)
4288 (not (looking-at verilog-end-block-re)))
4289 (setq h (point))))
4290 (goto-char h)))
4291 ;; stop if we see an extended complete reg, perhaps a complete one
4292 (and
4293 (looking-at verilog-complete-reg)
4294 (let* ((p (point)))
4295 (while (and (looking-at verilog-extended-complete-re)
4296 (progn (setq p (point))
4297 (verilog-backward-token)
4298 (/= p (point)))))
4299 (goto-char p)))
4300 ;; stop if we see a complete reg (previous found extended ones)
4301 (looking-at verilog-basic-complete-re)
4302 ;; stop if previous token is an ender
4303 (save-excursion
4304 (verilog-backward-token)
4305 (looking-at verilog-end-block-re))))
4306 (verilog-backward-syntactic-ws)
4307 (verilog-backward-token))
4308 ;; Now point is where the previous line ended.
4309 (verilog-forward-syntactic-ws)
4310 ;; Skip forward over any preprocessor directives, as they have wacky indentation
4311 (if (looking-at verilog-preprocessor-re)
4312 (progn (goto-char (match-end 0))
4313 (verilog-forward-syntactic-ws)))))
4314
4315 (defun verilog-beg-of-statement-1 ()
4316 "Move backward to beginning of statement."
4317 (interactive)
4318 (if (verilog-in-comment-p)
4319 (verilog-backward-syntactic-ws))
4320 (let ((pt (point)))
4321 (catch 'done
4322 (while (not (looking-at verilog-complete-reg))
4323 (setq pt (point))
4324 (verilog-backward-syntactic-ws)
4325 (if (or (bolp)
4326 (= (preceding-char) ?\;)
4327 (progn
4328 (verilog-backward-token)
4329 (looking-at verilog-ends-re)))
4330 (progn
4331 (goto-char pt)
4332 (throw 'done t)))))
4333 (verilog-forward-syntactic-ws)))
4334 ;;
4335 ;; (while (and
4336 ;; (not (looking-at verilog-complete-reg))
4337 ;; (not (bolp))
4338 ;; (not (= (preceding-char) ?\;)))
4339 ;; (verilog-backward-token)
4340 ;; (verilog-backward-syntactic-ws)
4341 ;; (setq pt (point)))
4342 ;; (goto-char pt)
4343 ;; ;(verilog-forward-syntactic-ws)
4344
4345 (defun verilog-end-of-statement ()
4346 "Move forward to end of current statement."
4347 (interactive)
4348 (let ((nest 0) pos)
4349 (cond
4350 ((verilog-in-directive-p)
4351 (forward-line 1)
4352 (backward-char 1))
4353
4354 ((looking-at verilog-beg-block-re)
4355 (verilog-forward-sexp))
4356
4357 ((equal (char-after) ?\})
4358 (forward-char))
4359
4360 ;; Skip to end of statement
4361 ((condition-case nil
4362 (setq pos
4363 (catch 'found
4364 (while t
4365 (forward-sexp 1)
4366 (verilog-skip-forward-comment-or-string)
4367 (if (eolp)
4368 (forward-line 1))
4369 (cond ((looking-at "[ \t]*;")
4370 (skip-chars-forward "^;")
4371 (forward-char 1)
4372 (throw 'found (point)))
4373 ((save-excursion
4374 (forward-sexp -1)
4375 (looking-at verilog-beg-block-re))
4376 (goto-char (match-beginning 0))
4377 (throw 'found nil))
4378 ((looking-at "[ \t]*)")
4379 (throw 'found (point)))
4380 ((eobp)
4381 (throw 'found (point)))
4382 )))
4383
4384 )
4385 (error nil))
4386 (if (not pos)
4387 ;; Skip a whole block
4388 (catch 'found
4389 (while t
4390 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4391 (setq nest (if (match-end 1)
4392 (1+ nest)
4393 (1- nest)))
4394 (cond ((eobp)
4395 (throw 'found (point)))
4396 ((= 0 nest)
4397 (throw 'found (verilog-end-of-statement))))))
4398 pos)))))
4399
4400 (defun verilog-in-case-region-p ()
4401 "Return true if in a case region.
4402 More specifically, point @ in the line foo : @ begin"
4403 (interactive)
4404 (save-excursion
4405 (if (and
4406 (progn (verilog-forward-syntactic-ws)
4407 (looking-at "\\<begin\\>"))
4408 (progn (verilog-backward-syntactic-ws)
4409 (= (preceding-char) ?\:)))
4410 (catch 'found
4411 (let ((nest 1))
4412 (while t
4413 (verilog-re-search-backward
4414 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4415 "\\(\\<endcase\\>\\)\\>")
4416 nil 'move)
4417 (cond
4418 ((match-end 3)
4419 (setq nest (1+ nest)))
4420 ((match-end 2)
4421 (if (= nest 1)
4422 (throw 'found 1))
4423 (setq nest (1- nest)))
4424 (t
4425 (throw 'found (= nest 0)))))))
4426 nil)))
4427
4428 (defun verilog-backward-up-list (arg)
4429 "Call `backward-up-list' ARG, ignoring comments."
4430 (let ((parse-sexp-ignore-comments t))
4431 (backward-up-list arg)))
4432
4433 (defun verilog-forward-sexp-cmt (arg)
4434 "Call `forward-sexp' ARG, inside comments."
4435 (let ((parse-sexp-ignore-comments nil))
4436 (forward-sexp arg)))
4437
4438 (defun verilog-forward-sexp-ign-cmt (arg)
4439 "Call `forward-sexp' ARG, ignoring comments."
4440 (let ((parse-sexp-ignore-comments t))
4441 (forward-sexp arg)))
4442
4443 (defun verilog-in-generate-region-p ()
4444 "Return true if in a generate region.
4445 More specifically, after a generate and before an endgenerate."
4446 (interactive)
4447 (let ((nest 1))
4448 (save-excursion
4449 (catch 'done
4450 (while (and
4451 (/= nest 0)
4452 (verilog-re-search-backward
4453 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4454 (cond
4455 ((match-end 1) ; module - we have crawled out
4456 (throw 'done 1))
4457 ((match-end 2) ; generate
4458 (setq nest (1- nest)))
4459 ((match-end 3) ; endgenerate
4460 (setq nest (1+ nest))))))))
4461 (= nest 0) )) ; return nest
4462
4463 (defun verilog-in-fork-region-p ()
4464 "Return true if between a fork and join."
4465 (interactive)
4466 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
4467 (nest 1))
4468 (save-excursion
4469 (while (and
4470 (/= nest 0)
4471 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4472 (cond
4473 ((match-end 1) ; fork
4474 (setq nest (1- nest)))
4475 ((match-end 2) ; join
4476 (setq nest (1+ nest)))))))
4477 (= nest 0) )) ; return nest
4478
4479 (defun verilog-in-deferred-immediate-final-p ()
4480 "Return true if inside an `assert/assume/cover final' statement."
4481 (interactive)
4482 (and (looking-at "final")
4483 (verilog-looking-back "\\<\\(?:assert\\|assume\\|cover\\)\\>\\s-+" nil))
4484 )
4485
4486 (defun verilog-backward-case-item (lim)
4487 "Skip backward to nearest enclosing case item.
4488 Limit search to point LIM."
4489 (interactive)
4490 (let ((str 'nil)
4491 (lim1
4492 (progn
4493 (save-excursion
4494 (verilog-re-search-backward verilog-endcomment-reason-re
4495 lim 'move)
4496 (point)))))
4497 ;; Try to find the real :
4498 (if (save-excursion (search-backward ":" lim1 t))
4499 (let ((colon 0)
4500 b e )
4501 (while
4502 (and
4503 (< colon 1)
4504 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4505 lim1 'move))
4506 (cond
4507 ((match-end 1) ; [
4508 (setq colon (1+ colon))
4509 (if (>= colon 0)
4510 (error "%s: unbalanced [" (verilog-point-text))))
4511 ((match-end 2) ; ]
4512 (setq colon (1- colon)))
4513
4514 ((match-end 3) ; :
4515 (setq colon (1+ colon)))))
4516 ;; Skip back to beginning of case item
4517 (skip-chars-backward "\t ")
4518 (verilog-skip-backward-comment-or-string)
4519 (setq e (point))
4520 (setq b
4521 (progn
4522 (if
4523 (verilog-re-search-backward
4524 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4525 (progn
4526 (cond
4527 ((match-end 1)
4528 (goto-char (match-end 1))
4529 (verilog-forward-ws&directives)
4530 (if (looking-at "(")
4531 (progn
4532 (forward-sexp)
4533 (verilog-forward-ws&directives)))
4534 (point))
4535 (t
4536 (goto-char (match-end 0))
4537 (verilog-forward-ws&directives)
4538 (point))))
4539 (error "Malformed case item"))))
4540 (setq str (buffer-substring b e))
4541 (if
4542 (setq e
4543 (string-match
4544 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4545 (setq str (concat (substring str 0 e) "...")))
4546 str)
4547 'nil)))
4548 \f
4549 ;;; Other functions:
4550 ;;
4551
4552 (defun verilog-kill-existing-comment ()
4553 "Kill auto comment on this line."
4554 (save-excursion
4555 (let* (
4556 (e (progn
4557 (end-of-line)
4558 (point)))
4559 (b (progn
4560 (beginning-of-line)
4561 (search-forward "//" e t))))
4562 (if b
4563 (delete-region (- b 2) e)))))
4564
4565 (defconst verilog-directive-nest-re
4566 (concat "\\(`else\\>\\)\\|"
4567 "\\(`endif\\>\\)\\|"
4568 "\\(`if\\>\\)\\|"
4569 "\\(`ifdef\\>\\)\\|"
4570 "\\(`ifndef\\>\\)\\|"
4571 "\\(`elsif\\>\\)"))
4572
4573 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4574 "Add ending comment with given INDENT-STR.
4575 With KILL-EXISTING-COMMENT, remove what was there before.
4576 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4577 Insert `// case expr ' if this line ends a case block.
4578 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
4579 Insert `// NAME ' if this line ends a function, task, module,
4580 primitive or interface named NAME."
4581 (save-excursion
4582 (cond
4583 (; Comment close preprocessor directives
4584 (and
4585 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4586 (or kill-existing-comment
4587 (not (save-excursion
4588 (end-of-line)
4589 (search-backward "//" (point-at-bol) t)))))
4590 (let ((nest 1) b e
4591 m
4592 (else (if (match-end 2) "!" " ")))
4593 (end-of-line)
4594 (if kill-existing-comment
4595 (verilog-kill-existing-comment))
4596 (delete-horizontal-space)
4597 (save-excursion
4598 (backward-sexp 1)
4599 (while (and (/= nest 0)
4600 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4601 (cond
4602 ((match-end 1) ; `else
4603 (if (= nest 1)
4604 (setq else "!")))
4605 ((match-end 2) ; `endif
4606 (setq nest (1+ nest)))
4607 ((match-end 3) ; `if
4608 (setq nest (1- nest)))
4609 ((match-end 4) ; `ifdef
4610 (setq nest (1- nest)))
4611 ((match-end 5) ; `ifndef
4612 (setq nest (1- nest)))
4613 ((match-end 6) ; `elsif
4614 (if (= nest 1)
4615 (progn
4616 (setq else "!")
4617 (setq nest 0))))))
4618 (if (match-end 0)
4619 (setq
4620 m (buffer-substring
4621 (match-beginning 0)
4622 (match-end 0))
4623 b (progn
4624 (skip-chars-forward "^ \t")
4625 (verilog-forward-syntactic-ws)
4626 (point))
4627 e (progn
4628 (skip-chars-forward "a-zA-Z0-9_")
4629 (point)))))
4630 (if b
4631 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4632 (insert (concat " // " else m " " (buffer-substring b e))))
4633 (progn
4634 (insert " // unmatched `else, `elsif or `endif")
4635 (ding 't)))))
4636
4637 (; Comment close case/class/function/task/module and named block
4638 (and (looking-at "\\<end")
4639 (or kill-existing-comment
4640 (not (save-excursion
4641 (end-of-line)
4642 (search-backward "//" (point-at-bol) t)))))
4643 (let ((type (car indent-str)))
4644 (unless (eq type 'declaration)
4645 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ; ignore named ends
4646 (if (looking-at verilog-end-block-ordered-re)
4647 (cond
4648 (;- This is a case block; search back for the start of this case
4649 (match-end 1) ; of verilog-end-block-ordered-re
4650
4651 (let ((err 't)
4652 (str "UNMATCHED!!"))
4653 (save-excursion
4654 (verilog-leap-to-head)
4655 (cond
4656 ((looking-at "\\<randcase\\>")
4657 (setq str "randcase")
4658 (setq err nil))
4659 ((looking-at "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4660 (goto-char (match-end 0))
4661 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4662 (setq err nil))
4663 ))
4664 (end-of-line)
4665 (if kill-existing-comment
4666 (verilog-kill-existing-comment))
4667 (delete-horizontal-space)
4668 (insert (concat " // " str ))
4669 (if err (ding 't))))
4670
4671 (;- This is a begin..end block
4672 (match-end 2) ; of verilog-end-block-ordered-re
4673 (let ((str " // UNMATCHED !!")
4674 (err 't)
4675 (here (point))
4676 there
4677 cntx)
4678 (save-excursion
4679 (verilog-leap-to-head)
4680 (setq there (point))
4681 (if (not (match-end 0))
4682 (progn
4683 (goto-char here)
4684 (end-of-line)
4685 (if kill-existing-comment
4686 (verilog-kill-existing-comment))
4687 (delete-horizontal-space)
4688 (insert str)
4689 (ding 't))
4690 (let ((lim
4691 (save-excursion (verilog-beg-of-defun) (point)))
4692 (here (point)))
4693 (cond
4694 (;-- handle named block differently
4695 (looking-at verilog-named-block-re)
4696 (search-forward ":")
4697 (setq there (point))
4698 (setq str (verilog-get-expr))
4699 (setq err nil)
4700 (setq str (concat " // block: " str )))
4701
4702 ((verilog-in-case-region-p) ;-- handle case item differently
4703 (goto-char here)
4704 (setq str (verilog-backward-case-item lim))
4705 (setq there (point))
4706 (setq err nil)
4707 (setq str (concat " // case: " str )))
4708
4709 (;- try to find "reason" for this begin
4710 (cond
4711 (;
4712 (eq here (progn
4713 ;; (verilog-backward-token)
4714 (verilog-beg-of-statement)
4715 (point)))
4716 (setq err nil)
4717 (setq str ""))
4718 ((looking-at verilog-endcomment-reason-re)
4719 (setq there (match-end 0))
4720 (setq cntx (concat (match-string 0) " "))
4721 (cond
4722 (;- begin
4723 (match-end 1)
4724 (setq err nil)
4725 (save-excursion
4726 (if (and (verilog-continued-line)
4727 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4728 (progn
4729 (goto-char (match-end 0))
4730 (setq there (point))
4731 (setq str
4732 (concat " // " (match-string 0) " " (verilog-get-expr))))
4733 (setq str ""))))
4734
4735 (;- else
4736 (match-end 2)
4737 (let ((nest 0)
4738 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4739 (catch 'skip
4740 (while (verilog-re-search-backward reg nil 'move)
4741 (cond
4742 ((match-end 1) ; begin
4743 (setq nest (1- nest)))
4744 ((match-end 2) ; end
4745 (setq nest (1+ nest)))
4746 ((match-end 3)
4747 (if (= 0 nest)
4748 (progn
4749 (goto-char (match-end 0))
4750 (setq there (point))
4751 (setq err nil)
4752 (setq str (verilog-get-expr))
4753 (setq str (concat " // else: !if" str ))
4754 (throw 'skip 1))))
4755 ((match-end 4)
4756 (if (= 0 nest)
4757 (progn
4758 (goto-char (match-end 0))
4759 (setq there (point))
4760 (setq err nil)
4761 (setq str (verilog-get-expr))
4762 (setq str (concat " // else: !assert " str ))
4763 (throw 'skip 1)))))))))
4764 (;- end else
4765 (match-end 3)
4766 (goto-char there)
4767 (let ((nest 0)
4768 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4769 (catch 'skip
4770 (while (verilog-re-search-backward reg nil 'move)
4771 (cond
4772 ((match-end 1) ; begin
4773 (setq nest (1- nest)))
4774 ((match-end 2) ; end
4775 (setq nest (1+ nest)))
4776 ((match-end 3)
4777 (if (= 0 nest)
4778 (progn
4779 (goto-char (match-end 0))
4780 (setq there (point))
4781 (setq err nil)
4782 (setq str (verilog-get-expr))
4783 (setq str (concat " // else: !if" str ))
4784 (throw 'skip 1))))
4785 ((match-end 4)
4786 (if (= 0 nest)
4787 (progn
4788 (goto-char (match-end 0))
4789 (setq there (point))
4790 (setq err nil)
4791 (setq str (verilog-get-expr))
4792 (setq str (concat " // else: !assert " str ))
4793 (throw 'skip 1)))))))))
4794
4795 (; always, always_comb, always_latch w/o @...
4796 (match-end 5)
4797 (goto-char (match-end 0))
4798 (setq there (point))
4799 (setq err nil)
4800 (setq str (concat " // " cntx )))
4801
4802 (;- task/function/initial et cetera
4803 t
4804 (match-end 0)
4805 (goto-char (match-end 0))
4806 (setq there (point))
4807 (setq err nil)
4808 (setq str (concat " // " cntx (verilog-get-expr))))
4809
4810 (;-- otherwise...
4811 (setq str " // auto-endcomment confused "))))
4812
4813 ((and
4814 (verilog-in-case-region-p) ;-- handle case item differently
4815 (progn
4816 (setq there (point))
4817 (goto-char here)
4818 (setq str (verilog-backward-case-item lim))))
4819 (setq err nil)
4820 (setq str (concat " // case: " str )))
4821
4822 ((verilog-in-fork-region-p)
4823 (setq err nil)
4824 (setq str " // fork branch" ))
4825
4826 ((looking-at "\\<end\\>")
4827 ;; HERE
4828 (forward-word 1)
4829 (verilog-forward-syntactic-ws)
4830 (setq err nil)
4831 (setq str (verilog-get-expr))
4832 (setq str (concat " // " cntx str )))
4833
4834 ))))
4835 (goto-char here)
4836 (end-of-line)
4837 (if kill-existing-comment
4838 (verilog-kill-existing-comment))
4839 (delete-horizontal-space)
4840 (if (or err
4841 (> (count-lines here there) verilog-minimum-comment-distance))
4842 (insert str))
4843 (if err (ding 't))
4844 ))))
4845 (;- this is endclass, which can be nested
4846 (match-end 11) ; of verilog-end-block-ordered-re
4847 ;;(goto-char there)
4848 (let ((nest 0)
4849 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4850 string)
4851 (save-excursion
4852 (catch 'skip
4853 (while (verilog-re-search-backward reg nil 'move)
4854 (cond
4855 ((match-end 3) ; endclass
4856 (ding 't)
4857 (setq string "unmatched endclass")
4858 (throw 'skip 1))
4859
4860 ((match-end 2) ; endclass
4861 (setq nest (1+ nest)))
4862
4863 ((match-end 1) ; class
4864 (setq nest (1- nest))
4865 (if (< nest 0)
4866 (progn
4867 (goto-char (match-end 0))
4868 (let (b e)
4869 (setq b (progn
4870 (skip-chars-forward "^ \t")
4871 (verilog-forward-ws&directives)
4872 (point))
4873 e (progn
4874 (skip-chars-forward "a-zA-Z0-9_")
4875 (point)))
4876 (setq string (buffer-substring b e)))
4877 (throw 'skip 1))))
4878 ))))
4879 (end-of-line)
4880 (if kill-existing-comment
4881 (verilog-kill-existing-comment))
4882 (delete-horizontal-space)
4883 (insert (concat " // " string ))))
4884
4885 (; - this is end{function,generate,task,module,primitive,table,generate}
4886 ;; - which can not be nested.
4887 t
4888 (let (string reg (name-re nil))
4889 (end-of-line)
4890 (if kill-existing-comment
4891 (save-match-data
4892 (verilog-kill-existing-comment)))
4893 (delete-horizontal-space)
4894 (backward-sexp)
4895 (cond
4896 ((match-end 5) ; of verilog-end-block-ordered-re
4897 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4898 (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
4899 ((match-end 6) ; of verilog-end-block-ordered-re
4900 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4901 (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
4902 ((match-end 7) ; of verilog-end-block-ordered-re
4903 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4904 ((match-end 8) ; of verilog-end-block-ordered-re
4905 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4906 ((match-end 9) ; of verilog-end-block-ordered-re
4907 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4908 ((match-end 10) ; of verilog-end-block-ordered-re
4909 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4910 ((match-end 11) ; of verilog-end-block-ordered-re
4911 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4912 ((match-end 12) ; of verilog-end-block-ordered-re
4913 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4914 ((match-end 13) ; of verilog-end-block-ordered-re
4915 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4916 ((match-end 14) ; of verilog-end-block-ordered-re
4917 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4918 ((match-end 15) ; of verilog-end-block-ordered-re
4919 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4920 ((match-end 16) ; of verilog-end-block-ordered-re
4921 (setq reg "\\(\\<property\\>\\)\\|\\<endproperty\\>"))
4922
4923 (t (error "Problem in verilog-set-auto-endcomments")))
4924 (let (b e)
4925 (save-excursion
4926 (verilog-re-search-backward reg nil 'move)
4927 (cond
4928 ((match-end 1)
4929 (setq b (progn
4930 (skip-chars-forward "^ \t")
4931 (verilog-forward-ws&directives)
4932 (if (looking-at "static\\|automatic")
4933 (progn
4934 (goto-char (match-end 0))
4935 (verilog-forward-ws&directives)))
4936 (if (and name-re (verilog-re-search-forward name-re nil 'move))
4937 (progn
4938 (goto-char (match-beginning 0))
4939 (verilog-forward-ws&directives)))
4940 (point))
4941 e (progn
4942 (skip-chars-forward "a-zA-Z0-9_")
4943 (point)))
4944 (setq string (buffer-substring b e)))
4945 (t
4946 (ding 't)
4947 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4948 (end-of-line)
4949 (insert (concat " // " string )))
4950 ))))))))))
4951
4952 (defun verilog-get-expr()
4953 "Grab expression at point, e.g., case ( a | b & (c ^d))."
4954 (let* ((b (progn
4955 (verilog-forward-syntactic-ws)
4956 (skip-chars-forward " \t")
4957 (point)))
4958 (e (let ((par 1))
4959 (cond
4960 ((looking-at "@")
4961 (forward-char 1)
4962 (verilog-forward-syntactic-ws)
4963 (if (looking-at "(")
4964 (progn
4965 (forward-char 1)
4966 (while (and (/= par 0)
4967 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4968 (cond
4969 ((match-end 1)
4970 (setq par (1+ par)))
4971 ((match-end 2)
4972 (setq par (1- par)))))))
4973 (point))
4974 ((looking-at "(")
4975 (forward-char 1)
4976 (while (and (/= par 0)
4977 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4978 (cond
4979 ((match-end 1)
4980 (setq par (1+ par)))
4981 ((match-end 2)
4982 (setq par (1- par)))))
4983 (point))
4984 ((looking-at "\\[")
4985 (forward-char 1)
4986 (while (and (/= par 0)
4987 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4988 (cond
4989 ((match-end 1)
4990 (setq par (1+ par)))
4991 ((match-end 2)
4992 (setq par (1- par)))))
4993 (verilog-forward-syntactic-ws)
4994 (skip-chars-forward "^ \t\n\f")
4995 (point))
4996 ((looking-at "/[/\\*]")
4997 b)
4998 ('t
4999 (skip-chars-forward "^: \t\n\f")
5000 (point)))))
5001 (str (buffer-substring b e)))
5002 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
5003 (setq str (concat (substring str 0 e) "...")))
5004 str))
5005
5006 (defun verilog-expand-vector ()
5007 "Take a signal vector on the current line and expand it to multiple lines.
5008 Useful for creating tri's and other expanded fields."
5009 (interactive)
5010 (verilog-expand-vector-internal "[" "]"))
5011
5012 (defun verilog-expand-vector-internal (bra ket)
5013 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
5014 (save-excursion
5015 (forward-line 0)
5016 (let ((signal-string (buffer-substring (point)
5017 (progn
5018 (end-of-line) (point)))))
5019 (if (string-match
5020 (concat "\\(.*\\)"
5021 (regexp-quote bra)
5022 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
5023 (regexp-quote ket)
5024 "\\(.*\\)$") signal-string)
5025 (let* ((sig-head (match-string 1 signal-string))
5026 (vec-start (string-to-number (match-string 2 signal-string)))
5027 (vec-end (if (= (match-beginning 3) (match-end 3))
5028 vec-start
5029 (string-to-number
5030 (substring signal-string (1+ (match-beginning 3))
5031 (match-end 3)))))
5032 (vec-range
5033 (if (= (match-beginning 4) (match-end 4))
5034 1
5035 (string-to-number
5036 (substring signal-string (+ 2 (match-beginning 4))
5037 (match-end 4)))))
5038 (sig-tail (match-string 5 signal-string))
5039 vec)
5040 ;; Decode vectors
5041 (setq vec nil)
5042 (if (< vec-range 0)
5043 (let ((tmp vec-start))
5044 (setq vec-start vec-end
5045 vec-end tmp
5046 vec-range (- vec-range))))
5047 (if (< vec-end vec-start)
5048 (while (<= vec-end vec-start)
5049 (setq vec (append vec (list vec-start)))
5050 (setq vec-start (- vec-start vec-range)))
5051 (while (<= vec-start vec-end)
5052 (setq vec (append vec (list vec-start)))
5053 (setq vec-start (+ vec-start vec-range))))
5054 ;;
5055 ;; Delete current line
5056 (delete-region (point) (progn (forward-line 0) (point)))
5057 ;;
5058 ;; Expand vector
5059 (while vec
5060 (insert (concat sig-head bra
5061 (int-to-string (car vec)) ket sig-tail "\n"))
5062 (setq vec (cdr vec)))
5063 (delete-char -1)
5064 ;;
5065 )))))
5066
5067 (defun verilog-strip-comments ()
5068 "Strip all comments from the Verilog code."
5069 (interactive)
5070 (goto-char (point-min))
5071 (while (re-search-forward "//" nil t)
5072 (if (verilog-within-string)
5073 (re-search-forward "\"" nil t)
5074 (if (verilog-in-star-comment-p)
5075 (re-search-forward "\\*/" nil t)
5076 (let ((bpt (- (point) 2)))
5077 (end-of-line)
5078 (delete-region bpt (point))))))
5079 ;;
5080 (goto-char (point-min))
5081 (while (re-search-forward "/\\*" nil t)
5082 (if (verilog-within-string)
5083 (re-search-forward "\"" nil t)
5084 (let ((bpt (- (point) 2)))
5085 (re-search-forward "\\*/")
5086 (delete-region bpt (point))))))
5087
5088 (defun verilog-one-line ()
5089 "Convert structural Verilog instances to occupy one line."
5090 (interactive)
5091 (goto-char (point-min))
5092 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
5093 (replace-match "\\1 " nil nil)))
5094
5095 (defun verilog-linter-name ()
5096 "Return name of linter, either surelint or verilint."
5097 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
5098 compile-command))
5099 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
5100 verilog-linter)))
5101 (cond ((equal compile-word1 "surelint") `surelint)
5102 ((equal compile-word1 "verilint") `verilint)
5103 ((equal lint-word1 "surelint") `surelint)
5104 ((equal lint-word1 "verilint") `verilint)
5105 (t `surelint)))) ; back compatibility
5106
5107 (defun verilog-lint-off ()
5108 "Convert a Verilog linter warning line into a disable statement.
5109 For example:
5110 pci_bfm_null.v, line 46: Unused input: pci_rst_
5111 becomes a comment for the appropriate tool.
5112
5113 The first word of the `compile-command' or `verilog-linter'
5114 variables is used to determine which product is being used.
5115
5116 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
5117 (interactive)
5118 (let ((linter (verilog-linter-name)))
5119 (cond ((equal linter `surelint)
5120 (verilog-surelint-off))
5121 ((equal linter `verilint)
5122 (verilog-verilint-off))
5123 (t (error "Linter name not set")))))
5124
5125 (defvar compilation-last-buffer)
5126 (defvar next-error-last-buffer)
5127
5128 (defun verilog-surelint-off ()
5129 "Convert a SureLint warning line into a disable statement.
5130 Run from Verilog source window; assumes there is a *compile* buffer
5131 with point set appropriately.
5132
5133 For example:
5134 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
5135 becomes:
5136 // surefire lint_line_off UDDONX"
5137 (interactive)
5138 (let ((buff (if (boundp 'next-error-last-buffer)
5139 next-error-last-buffer
5140 compilation-last-buffer)))
5141 (when (buffer-live-p buff)
5142 (save-excursion
5143 (switch-to-buffer buff)
5144 (beginning-of-line)
5145 (when
5146 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
5147 (let* ((code (match-string 2))
5148 (file (match-string 3))
5149 (line (match-string 4))
5150 (buffer (get-file-buffer file))
5151 dir filename)
5152 (unless buffer
5153 (progn
5154 (setq buffer
5155 (and (file-exists-p file)
5156 (find-file-noselect file)))
5157 (or buffer
5158 (let* ((pop-up-windows t))
5159 (let ((name (expand-file-name
5160 (read-file-name
5161 (format "Find this error in: (default %s) "
5162 file)
5163 dir file t))))
5164 (if (file-directory-p name)
5165 (setq name (expand-file-name filename name)))
5166 (setq buffer
5167 (and (file-exists-p name)
5168 (find-file-noselect name))))))))
5169 (switch-to-buffer buffer)
5170 (goto-char (point-min))
5171 (forward-line (- (string-to-number line)))
5172 (end-of-line)
5173 (catch 'already
5174 (cond
5175 ((verilog-in-slash-comment-p)
5176 (re-search-backward "//")
5177 (cond
5178 ((looking-at "// surefire lint_off_line ")
5179 (goto-char (match-end 0))
5180 (let ((lim (point-at-eol)))
5181 (if (re-search-forward code lim 'move)
5182 (throw 'already t)
5183 (insert (concat " " code)))))
5184 (t
5185 )))
5186 ((verilog-in-star-comment-p)
5187 (re-search-backward "/\\*")
5188 (insert (format " // surefire lint_off_line %6s" code )))
5189 (t
5190 (insert (format " // surefire lint_off_line %6s" code ))
5191 )))))))))
5192
5193 (defun verilog-verilint-off ()
5194 "Convert a Verilint warning line into a disable statement.
5195
5196 For example:
5197 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
5198 becomes:
5199 //Verilint 240 off // WARNING: Unused input"
5200 (interactive)
5201 (save-excursion
5202 (beginning-of-line)
5203 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
5204 (replace-match (format
5205 ;; %3s makes numbers 1-999 line up nicely
5206 "\\1//Verilint %3s off // WARNING: \\3"
5207 (match-string 2)))
5208 (beginning-of-line)
5209 (verilog-indent-line))))
5210
5211 (defun verilog-auto-save-compile ()
5212 "Update automatics with \\[verilog-auto], save the buffer, and compile."
5213 (interactive)
5214 (verilog-auto) ; Always do it for safety
5215 (save-buffer)
5216 (compile compile-command))
5217
5218 (defun verilog-preprocess (&optional command filename)
5219 "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
5220 Takes optional COMMAND or defaults to `verilog-preprocessor', and
5221 FILENAME to find directory to run in, or defaults to `buffer-file-name'."
5222 (interactive
5223 (list
5224 (let ((default (verilog-expand-command verilog-preprocessor)))
5225 (set (make-local-variable `verilog-preprocessor)
5226 (read-from-minibuffer "Run Preprocessor (like this): "
5227 default nil nil
5228 'verilog-preprocess-history default)))))
5229 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
5230 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
5231 (dir (file-name-directory (or filename buffer-file-name)))
5232 (cmd (concat "cd " dir "; " command)))
5233 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
5234 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
5235 (insert (concat "// " cmd "\n"))
5236 (call-process shell-file-name nil t nil shell-command-switch cmd)
5237 (verilog-mode)
5238 ;; Without this force, it takes a few idle seconds
5239 ;; to get the color, which is very jarring
5240 (unless (fboundp 'font-lock-ensure)
5241 ;; We should use font-lock-ensure in preference to
5242 ;; font-lock-fontify-buffer, but IIUC the problem this is supposed to
5243 ;; solve only appears in Emacsen older than font-lock-ensure anyway.
5244 ;; So avoid bytecomp's interactive-only by going through intern.
5245 (when fontlocked (funcall (intern "font-lock-fontify-buffer"))))))))
5246 \f
5247 ;;; Batch:
5248 ;;
5249
5250 (defun verilog-warn (string &rest args)
5251 "Print a warning with `format' using STRING and optional ARGS."
5252 (apply 'message (concat "%%Warning: " string) args))
5253
5254 (defun verilog-warn-error (string &rest args)
5255 "Call `error' using STRING and optional ARGS.
5256 If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
5257 (if verilog-warn-fatal
5258 (apply 'error string args)
5259 (apply 'verilog-warn string args)))
5260
5261 (defmacro verilog-batch-error-wrapper (&rest body)
5262 "Execute BODY and add error prefix to any errors found.
5263 This lets programs calling batch mode to easily extract error messages."
5264 `(let ((verilog-warn-fatal nil))
5265 (condition-case err
5266 (progn ,@body)
5267 (error
5268 (error "%%Error: %s%s" (error-message-string err)
5269 (if (featurep 'xemacs) "\n" "")))))) ; XEmacs forgets to add a newline
5270
5271 (defun verilog-batch-execute-func (funref &optional no-save)
5272 "Internal processing of a batch command.
5273 Runs FUNREF on all command arguments.
5274 Save the result unless optional NO-SAVE is t."
5275 (verilog-batch-error-wrapper
5276 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5277 ;; However, this function is called only when Emacs is being used as
5278 ;; a standalone language instead of as an editor, so we'll live.
5279 ;;
5280 ;; General globals needed
5281 (setq make-backup-files nil)
5282 (setq-default make-backup-files nil)
5283 (setq enable-local-variables t)
5284 (setq enable-local-eval t)
5285 (setq create-lockfiles nil)
5286 ;; Make sure any sub-files we read get proper mode
5287 (setq-default major-mode 'verilog-mode)
5288 ;; Ditto files already read in
5289 ;; Remember buffer list, so don't later pickup any verilog-getopt files
5290 (let ((orig-buffer-list (buffer-list)))
5291 (mapc (lambda (buf)
5292 (when (buffer-file-name buf)
5293 (with-current-buffer buf
5294 (verilog-mode)
5295 (verilog-auto-reeval-locals)
5296 (verilog-getopt-flags))))
5297 orig-buffer-list)
5298 ;; Process the files
5299 (mapcar (lambda (buf)
5300 (when (buffer-file-name buf)
5301 (save-excursion
5302 (if (not (file-exists-p (buffer-file-name buf)))
5303 (error
5304 "File not found: %s" (buffer-file-name buf)))
5305 (message "Processing %s" (buffer-file-name buf))
5306 (set-buffer buf)
5307 (funcall funref)
5308 (when (and (not no-save)
5309 (buffer-modified-p)) ; Avoid "no changes to be saved"
5310 (save-buffer)))))
5311 orig-buffer-list))))
5312
5313 (defun verilog-batch-auto ()
5314 "For use with --batch, perform automatic expansions as a stand-alone tool.
5315 This sets up the appropriate Verilog mode environment, updates automatics
5316 with \\[verilog-auto] on all command-line files, and saves the buffers.
5317 For proper results, multiple filenames need to be passed on the command
5318 line in bottom-up order."
5319 (unless noninteractive
5320 (error "Use verilog-batch-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5321 (verilog-batch-execute-func `verilog-auto))
5322
5323 (defun verilog-batch-delete-auto ()
5324 "For use with --batch, perform automatic deletion as a stand-alone tool.
5325 This sets up the appropriate Verilog mode environment, deletes automatics
5326 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5327 (unless noninteractive
5328 (error "Use verilog-batch-delete-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5329 (verilog-batch-execute-func `verilog-delete-auto))
5330
5331 (defun verilog-batch-delete-trailing-whitespace ()
5332 "For use with --batch, perform whitespace deletion as a stand-alone tool.
5333 This sets up the appropriate Verilog mode environment, removes
5334 whitespace with \\[verilog-delete-trailing-whitespace] on all
5335 command-line files, and saves the buffers."
5336 (unless noninteractive
5337 (error "Use verilog-batch-delete-trailing-whitespace only with --batch")) ; Otherwise we'd mess up buffer modes
5338 (verilog-batch-execute-func `verilog-delete-trailing-whitespace))
5339
5340 (defun verilog-batch-diff-auto ()
5341 "For use with --batch, perform automatic differences as a stand-alone tool.
5342 This sets up the appropriate Verilog mode environment, expand automatics
5343 with \\[verilog-diff-auto] on all command-line files, and reports an error
5344 if any differences are observed. This is appropriate for adding to regressions
5345 to insure automatics are always properly maintained."
5346 (unless noninteractive
5347 (error "Use verilog-batch-diff-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5348 (verilog-batch-execute-func `verilog-diff-auto t))
5349
5350 (defun verilog-batch-inject-auto ()
5351 "For use with --batch, perform automatic injection as a stand-alone tool.
5352 This sets up the appropriate Verilog mode environment, injects new automatics
5353 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5354 For proper results, multiple filenames need to be passed on the command
5355 line in bottom-up order."
5356 (unless noninteractive
5357 (error "Use verilog-batch-inject-auto only with --batch")) ; Otherwise we'd mess up buffer modes
5358 (verilog-batch-execute-func `verilog-inject-auto))
5359
5360 (defun verilog-batch-indent ()
5361 "For use with --batch, reindent an entire file as a stand-alone tool.
5362 This sets up the appropriate Verilog mode environment, calls
5363 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5364 (unless noninteractive
5365 (error "Use verilog-batch-indent only with --batch")) ; Otherwise we'd mess up buffer modes
5366 (verilog-batch-execute-func `verilog-indent-buffer))
5367 \f
5368 ;;; Indentation:
5369 ;;
5370 (defconst verilog-indent-alist
5371 '((block . (+ ind verilog-indent-level))
5372 (case . (+ ind verilog-case-indent))
5373 (cparenexp . (+ ind verilog-indent-level))
5374 (cexp . (+ ind verilog-cexp-indent))
5375 (defun . verilog-indent-level-module)
5376 (declaration . verilog-indent-level-declaration)
5377 (directive . (verilog-calculate-indent-directive))
5378 (tf . verilog-indent-level)
5379 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5380 (statement . ind)
5381 (cpp . 0)
5382 (comment . (verilog-comment-indent))
5383 (unknown . 3)
5384 (string . 0)))
5385
5386 (defun verilog-continued-line-1 (lim)
5387 "Return true if this is a continued line.
5388 Set point to where line starts. Limit search to point LIM."
5389 (let ((continued 't))
5390 (if (eq 0 (forward-line -1))
5391 (progn
5392 (end-of-line)
5393 (verilog-backward-ws&directives lim)
5394 (if (bobp)
5395 (setq continued nil)
5396 (setq continued (verilog-backward-token))))
5397 (setq continued nil))
5398 continued))
5399
5400 (defun verilog-calculate-indent ()
5401 "Calculate the indent of the current Verilog line.
5402 Examine previous lines. Once a line is found that is definitive as to the
5403 type of the current line, return that lines' indent level and its type.
5404 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
5405 (save-excursion
5406 (let* ((starting_position (point))
5407 (case-fold-search nil)
5408 (par 0)
5409 (begin (looking-at "[ \t]*begin\\>"))
5410 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5411 (structres nil)
5412 (type (catch 'nesting
5413 ;; Keep working backwards until we can figure out
5414 ;; what type of statement this is.
5415 ;; Basically we need to figure out
5416 ;; 1) if this is a continuation of the previous line;
5417 ;; 2) are we in a block scope (begin..end)
5418
5419 ;; if we are in a comment, done.
5420 (if (verilog-in-star-comment-p)
5421 (throw 'nesting 'comment))
5422
5423 ;; if we have a directive, done.
5424 (if (save-excursion (beginning-of-line)
5425 (and (looking-at verilog-directive-re-1)
5426 (not (or (looking-at "[ \t]*`[ou]vm_")
5427 (looking-at "[ \t]*`vmm_")))))
5428 (throw 'nesting 'directive))
5429 ;; indent structs as if there were module level
5430 (setq structres (verilog-in-struct-nested-p))
5431 (cond ((not structres) nil)
5432 ;;((and structres (equal (char-after) ?\})) (throw 'nesting 'struct-close))
5433 ((> structres 0) (throw 'nesting 'nested-struct))
5434 ((= structres 0) (throw 'nesting 'block))
5435 (t nil))
5436
5437 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5438 ;; unless we are in the newfangled coverpoint or constraint blocks
5439 (if (and
5440 verilog-indent-lists
5441 (verilog-in-paren)
5442 (not (verilog-in-coverage-p))
5443 )
5444 (progn (setq par 1)
5445 (throw 'nesting 'block)))
5446
5447 ;; See if we are continuing a previous line
5448 (while t
5449 ;; trap out if we crawl off the top of the buffer
5450 (if (bobp) (throw 'nesting 'cpp))
5451
5452 (if (and (verilog-continued-line-1 lim)
5453 (or (not (verilog-in-coverage-p))
5454 (looking-at verilog-in-constraint-re) )) ; may still get hosed if concat in constraint
5455 (let ((sp (point)))
5456 (if (and
5457 (not (looking-at verilog-complete-reg))
5458 (verilog-continued-line-1 lim))
5459 (progn (goto-char sp)
5460 (throw 'nesting 'cexp))
5461
5462 (goto-char sp))
5463 (if (and (verilog-in-coverage-p)
5464 (looking-at verilog-in-constraint-re))
5465 (progn
5466 (beginning-of-line)
5467 (skip-chars-forward " \t")
5468 (throw 'nesting 'constraint)))
5469 (if (and begin
5470 (not verilog-indent-begin-after-if)
5471 (looking-at verilog-no-indent-begin-re))
5472 (progn
5473 (beginning-of-line)
5474 (skip-chars-forward " \t")
5475 (throw 'nesting 'statement))
5476 (progn
5477 (throw 'nesting 'cexp))))
5478 ;; not a continued line
5479 (goto-char starting_position))
5480
5481 (if (looking-at "\\<else\\>")
5482 ;; search back for governing if, striding across begin..end pairs
5483 ;; appropriately
5484 (let ((elsec 1))
5485 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5486 (cond
5487 ((match-end 1) ; else, we're in deep
5488 (setq elsec (1+ elsec)))
5489 ((match-end 2) ; if
5490 (setq elsec (1- elsec))
5491 (if (= 0 elsec)
5492 (if verilog-align-ifelse
5493 (throw 'nesting 'statement)
5494 (progn ; back up to first word on this line
5495 (beginning-of-line)
5496 (verilog-forward-syntactic-ws)
5497 (throw 'nesting 'statement)))))
5498 ((match-end 3) ; assert block
5499 (setq elsec (1- elsec))
5500 (verilog-beg-of-statement) ; doesn't get to beginning
5501 (if (looking-at verilog-property-re)
5502 (throw 'nesting 'statement) ; We don't need an endproperty for these
5503 (throw 'nesting 'block) ; We still need an endproperty
5504 ))
5505 (t ; endblock
5506 ;; try to leap back to matching outward block by striding across
5507 ;; indent level changing tokens then immediately
5508 ;; previous line governs indentation.
5509 (let (( reg) (nest 1))
5510 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5511 (cond
5512 ((match-end 4) ; end
5513 ;; Search back for matching begin
5514 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5515 ((match-end 5) ; endcase
5516 ;; Search back for matching case
5517 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5518 ((match-end 6) ; endfunction
5519 ;; Search back for matching function
5520 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5521 ((match-end 7) ; endtask
5522 ;; Search back for matching task
5523 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5524 ((match-end 8) ; endspecify
5525 ;; Search back for matching specify
5526 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5527 ((match-end 9) ; endtable
5528 ;; Search back for matching table
5529 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5530 ((match-end 10) ; endgenerate
5531 ;; Search back for matching generate
5532 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5533 ((match-end 11) ; joins
5534 ;; Search back for matching fork
5535 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5536 ((match-end 12) ; class
5537 ;; Search back for matching class
5538 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5539 ((match-end 13) ; covergroup
5540 ;; Search back for matching covergroup
5541 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5542 (catch 'skip
5543 (while (verilog-re-search-backward reg nil 'move)
5544 (cond
5545 ((match-end 1) ; begin
5546 (setq nest (1- nest))
5547 (if (= 0 nest)
5548 (throw 'skip 1)))
5549 ((match-end 2) ; end
5550 (setq nest (1+ nest)))))
5551 )))))))
5552 (throw 'nesting (verilog-calc-1)))
5553 ) ; catch nesting
5554 ) ; type
5555 )
5556 ;; Return type of block and indent level.
5557 (if (not type)
5558 (setq type 'cpp))
5559 (if (> par 0) ; Unclosed Parenthesis
5560 (list 'cparenexp par)
5561 (cond
5562 ((eq type 'case)
5563 (list type (verilog-case-indent-level)))
5564 ((eq type 'statement)
5565 (list type (current-column)))
5566 ((eq type 'defun)
5567 (list type 0))
5568 ((eq type 'constraint)
5569 (list 'block (current-column)))
5570 ((eq type 'nested-struct)
5571 (list 'block structres))
5572 (t
5573 (list type (verilog-current-indent-level))))))))
5574
5575 (defun verilog-wai ()
5576 "Show matching nesting block for debugging."
5577 (interactive)
5578 (save-excursion
5579 (let* ((type (verilog-calc-1))
5580 depth)
5581 ;; Return type of block and indent level.
5582 (if (not type)
5583 (setq type 'cpp))
5584 (if (and
5585 verilog-indent-lists
5586 (not(or (verilog-in-coverage-p)
5587 (verilog-in-struct-p)))
5588 (verilog-in-paren))
5589 (setq depth 1)
5590 (cond
5591 ((eq type 'case)
5592 (setq depth (verilog-case-indent-level)))
5593 ((eq type 'statement)
5594 (setq depth (current-column)))
5595 ((eq type 'defun)
5596 (setq depth 0))
5597 (t
5598 (setq depth (verilog-current-indent-level)))))
5599 (message "You are at nesting %s depth %d" type depth))))
5600
5601 (defun verilog-calc-1 ()
5602 (catch 'nesting
5603 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)"))
5604 (inconstraint (verilog-in-coverage-p)))
5605 (while (verilog-re-search-backward re nil 'move)
5606 (catch 'continue
5607 (cond
5608 ((equal (char-after) ?\{)
5609 ;; block type returned based on outer constraint { or inner
5610 (if (verilog-at-constraint-p)
5611 (cond (inconstraint
5612 (beginning-of-line nil)
5613 (skip-chars-forward " \t")
5614 (throw 'nesting 'constraint))
5615 (t
5616 (throw 'nesting 'statement)))))
5617 ((equal (char-after) ?\})
5618 (let (par-pos
5619 (there (verilog-at-close-constraint-p)))
5620 (if there ; we are at the } that closes a constraint. Find the { that opens it
5621 (progn
5622 (if (> (verilog-in-paren-count) 0)
5623 (forward-char 1))
5624 (setq par-pos (verilog-parenthesis-depth))
5625 (cond (par-pos
5626 (goto-char par-pos)
5627 (forward-char 1))
5628 (t
5629 (backward-char 1)))))))
5630
5631 ((looking-at verilog-beg-block-re-ordered)
5632 (cond
5633 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5634 (let ((here (point)))
5635 (verilog-beg-of-statement)
5636 (if (looking-at verilog-extended-case-re)
5637 (throw 'nesting 'case)
5638 (goto-char here)))
5639 (throw 'nesting 'case))
5640
5641 ((match-end 4) ; *sigh* could be "disable fork"
5642 (let ((here (point)))
5643 (verilog-beg-of-statement)
5644 (if (looking-at verilog-disable-fork-re)
5645 t ; this is a normal statement
5646 (progn ; or is fork, starts a new block
5647 (goto-char here)
5648 (throw 'nesting 'block)))))
5649
5650 ((match-end 27) ; *sigh* might be a clocking declaration
5651 (let ((here (point)))
5652 (if (verilog-in-paren)
5653 t ; this is a normal statement
5654 (progn ; or is fork, starts a new block
5655 (goto-char here)
5656 (throw 'nesting 'block)))))
5657
5658 ;; need to consider typedef struct here...
5659 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
5660 ;; *sigh* These words have an optional prefix:
5661 ;; extern {virtual|protected}? function a();
5662 ;; typedef class foo;
5663 ;; and we don't want to confuse this with
5664 ;; function a();
5665 ;; property
5666 ;; ...
5667 ;; endfunction
5668 (verilog-beg-of-statement)
5669 (cond
5670 ((looking-at verilog-dpi-import-export-re)
5671 (throw 'continue 'foo))
5672 ((looking-at "\\<pure\\>\\s-+\\<virtual\\>\\s-+\\(?:\\<\\(local\\|protected\\|static\\)\\>\\s-+\\)?\\<\\(function\\|task\\)\\>\\s-+")
5673 (throw 'nesting 'statement))
5674 ((looking-at verilog-beg-block-re-ordered)
5675 (throw 'nesting 'block))
5676 (t
5677 (throw 'nesting 'defun))))
5678
5679 ;;
5680 ((looking-at "\\<property\\>")
5681 ;; *sigh*
5682 ;; {assert|assume|cover} property (); are complete
5683 ;; and could also be labeled: - foo: assert property
5684 ;; but
5685 ;; property ID () ... needs end_property
5686 (verilog-beg-of-statement)
5687 (if (looking-at verilog-property-re)
5688 (throw 'continue 'statement) ; We don't need an endproperty for these
5689 (throw 'nesting 'block) ;We still need an endproperty
5690 ))
5691
5692 (t (throw 'nesting 'block))))
5693
5694 ((looking-at verilog-end-block-re)
5695 (verilog-leap-to-head)
5696 (if (verilog-in-case-region-p)
5697 (progn
5698 (verilog-leap-to-case-head)
5699 (if (looking-at verilog-extended-case-re)
5700 (throw 'nesting 'case)))))
5701
5702 ((looking-at verilog-defun-level-re)
5703 (if (looking-at verilog-defun-level-generate-only-re)
5704 (if (or (verilog-in-generate-region-p)
5705 (verilog-in-deferred-immediate-final-p))
5706 (throw 'continue 'foo) ; always block in a generate - keep looking
5707 (throw 'nesting 'defun))
5708 (throw 'nesting 'defun)))
5709
5710 ((looking-at verilog-cpp-level-re)
5711 (throw 'nesting 'cpp))
5712
5713 ((bobp)
5714 (throw 'nesting 'cpp)))))
5715
5716 (throw 'nesting 'cpp))))
5717
5718 (defun verilog-calculate-indent-directive ()
5719 "Return indentation level for directive.
5720 For speed, the searcher looks at the last directive, not the indent
5721 of the appropriate enclosing block."
5722 (let ((base -1) ; Indent of the line that determines our indentation
5723 (ind 0)) ; Relative offset caused by other directives (like `endif on same line as `else)
5724 ;; Start at current location, scan back for another directive
5725
5726 (save-excursion
5727 (beginning-of-line)
5728 (while (and (< base 0)
5729 (verilog-re-search-backward verilog-directive-re nil t))
5730 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
5731 (setq base (current-indentation))))
5732 (cond ((and (looking-at verilog-directive-end) (< base 0)) ; Only matters when not at BOL
5733 (setq ind (- ind verilog-indent-level-directive)))
5734 ((and (looking-at verilog-directive-middle) (>= base 0)) ; Only matters when at BOL
5735 (setq ind (+ ind verilog-indent-level-directive)))
5736 ((looking-at verilog-directive-begin)
5737 (setq ind (+ ind verilog-indent-level-directive)))))
5738 ;; Adjust indent to starting indent of critical line
5739 (setq ind (max 0 (+ ind base))))
5740
5741 (save-excursion
5742 (beginning-of-line)
5743 (skip-chars-forward " \t")
5744 (cond ((or (looking-at verilog-directive-middle)
5745 (looking-at verilog-directive-end))
5746 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5747 ind))
5748
5749 (defun verilog-leap-to-case-head ()
5750 (let ((nest 1))
5751 (while (/= 0 nest)
5752 (verilog-re-search-backward
5753 (concat
5754 "\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5755 "\\|\\(\\<endcase\\>\\)" )
5756 nil 'move)
5757 (cond
5758 ((match-end 1)
5759 (let ((here (point)))
5760 (verilog-beg-of-statement)
5761 (unless (looking-at verilog-extended-case-re)
5762 (goto-char here)))
5763 (setq nest (1- nest)))
5764 ((match-end 3)
5765 (setq nest (1+ nest)))
5766 ((bobp)
5767 (ding 't)
5768 (setq nest 0))))))
5769
5770 (defun verilog-leap-to-head ()
5771 "Move point to the head of this block.
5772 Jump from end to matching begin, from endcase to matching case, and so on."
5773 (let ((reg nil)
5774 snest
5775 (nesting 'yes)
5776 (nest 1))
5777 (cond
5778 ((looking-at "\\<end\\>")
5779 ;; 1: Search back for matching begin
5780 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5781 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
5782 ((looking-at "\\<endtask\\>")
5783 ;; 2: Search back for matching task
5784 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5785 (setq nesting 'no))
5786 ((looking-at "\\<endcase\\>")
5787 (catch 'nesting
5788 (verilog-leap-to-case-head) )
5789 (setq reg nil) ; to force skip
5790 )
5791
5792 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
5793 ;; 4: Search back for matching fork
5794 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5795 ((looking-at "\\<endclass\\>")
5796 ;; 5: Search back for matching class
5797 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5798 ((looking-at "\\<endtable\\>")
5799 ;; 6: Search back for matching table
5800 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5801 ((looking-at "\\<endspecify\\>")
5802 ;; 7: Search back for matching specify
5803 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5804 ((looking-at "\\<endfunction\\>")
5805 ;; 8: Search back for matching function
5806 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5807 (setq nesting 'no))
5808 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5809 ((looking-at "\\<endgenerate\\>")
5810 ;; 8: Search back for matching generate
5811 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5812 ((looking-at "\\<endgroup\\>")
5813 ;; 10: Search back for matching covergroup
5814 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5815 ((looking-at "\\<endproperty\\>")
5816 ;; 11: Search back for matching property
5817 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
5818 ((looking-at verilog-uvm-end-re)
5819 ;; 12: Search back for matching sequence
5820 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
5821 ((looking-at verilog-ovm-end-re)
5822 ;; 12: Search back for matching sequence
5823 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
5824 ((looking-at verilog-vmm-end-re)
5825 ;; 12: Search back for matching sequence
5826 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
5827 ((looking-at "\\<endinterface\\>")
5828 ;; 12: Search back for matching interface
5829 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5830 ((looking-at "\\<endsequence\\>")
5831 ;; 12: Search back for matching sequence
5832 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5833 ((looking-at "\\<endclocking\\>")
5834 ;; 12: Search back for matching clocking
5835 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
5836 (if reg
5837 (catch 'skip
5838 (if (eq nesting 'yes)
5839 (let (sreg)
5840 (while (verilog-re-search-backward reg nil 'move)
5841 (cond
5842 ((match-end 1) ; begin
5843 (if (looking-at "fork")
5844 (let ((here (point)))
5845 (verilog-beg-of-statement)
5846 (unless (looking-at verilog-disable-fork-re)
5847 (goto-char here)
5848 (setq nest (1- nest))))
5849 (setq nest (1- nest)))
5850 (if (= 0 nest)
5851 ;; Now previous line describes syntax
5852 (throw 'skip 1))
5853 (if (and snest
5854 (= snest nest))
5855 (setq reg sreg)))
5856 ((match-end 2) ; end
5857 (setq nest (1+ nest)))
5858 ((match-end 3)
5859 ;; endcase, jump to case
5860 (setq snest nest)
5861 (setq nest (1+ nest))
5862 (setq sreg reg)
5863 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5864 ((match-end 4)
5865 ;; join, jump to fork
5866 (setq snest nest)
5867 (setq nest (1+ nest))
5868 (setq sreg reg)
5869 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5870 )))
5871 ;; no nesting
5872 (if (and
5873 (verilog-re-search-backward reg nil 'move)
5874 (match-end 1)) ; task -> could be virtual and/or protected
5875 (progn
5876 (verilog-beg-of-statement)
5877 (throw 'skip 1))
5878 (throw 'skip 1)))))))
5879
5880 (defun verilog-continued-line ()
5881 "Return true if this is a continued line.
5882 Set point to where line starts."
5883 (let ((continued 't))
5884 (if (eq 0 (forward-line -1))
5885 (progn
5886 (end-of-line)
5887 (verilog-backward-ws&directives)
5888 (if (bobp)
5889 (setq continued nil)
5890 (while (and continued
5891 (save-excursion
5892 (skip-chars-backward " \t")
5893 (not (bolp))))
5894 (setq continued (verilog-backward-token)))))
5895 (setq continued nil))
5896 continued))
5897
5898 (defun verilog-backward-token ()
5899 "Step backward token, returning true if this is a continued line."
5900 (interactive)
5901 (verilog-backward-syntactic-ws)
5902 (cond
5903 ((bolp)
5904 nil)
5905 (;-- Anything ending in a ; is complete
5906 (= (preceding-char) ?\;)
5907 nil)
5908 (; If a "}" is prefixed by a ";", then this is a complete statement
5909 ;; i.e.: constraint foo { a = b; }
5910 (= (preceding-char) ?\})
5911 (progn
5912 (backward-char)
5913 (not(verilog-at-close-constraint-p))))
5914 (;-- constraint foo { a = b }
5915 ;; is a complete statement. *sigh*
5916 (= (preceding-char) ?\{)
5917 (progn
5918 (backward-char)
5919 (not (verilog-at-constraint-p))))
5920 (;" string "
5921 (= (preceding-char) ?\")
5922 (backward-char)
5923 (verilog-skip-backward-comment-or-string)
5924 nil)
5925
5926 (; [3:4]
5927 (= (preceding-char) ?\])
5928 (backward-char)
5929 (verilog-backward-open-bracket)
5930 t)
5931
5932 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5933 ;; also could be simply '@(foo)'
5934 ;; or foo u1 #(a=8)
5935 ;; (b, ... which ISN'T complete
5936 ;; Do we need this???
5937 (= (preceding-char) ?\))
5938 (progn
5939 (backward-char)
5940 (verilog-backward-up-list 1)
5941 (verilog-backward-syntactic-ws)
5942 (let ((back (point)))
5943 (forward-word -1)
5944 (cond
5945 ;;XX
5946 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5947 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
5948 ((looking-at verilog-uvm-statement-re)
5949 nil)
5950 ((looking-at verilog-uvm-begin-re)
5951 t)
5952 ((looking-at verilog-uvm-end-re)
5953 t)
5954 ((looking-at verilog-ovm-statement-re)
5955 nil)
5956 ((looking-at verilog-ovm-begin-re)
5957 t)
5958 ((looking-at verilog-ovm-end-re)
5959 t)
5960 ;; JBA find VMM macros
5961 ((looking-at verilog-vmm-statement-re)
5962 nil )
5963 ((looking-at verilog-vmm-begin-re)
5964 t)
5965 ((looking-at verilog-vmm-end-re)
5966 nil)
5967 ;; JBA trying to catch macro lines with no ; at end
5968 ((looking-at "\\<`")
5969 nil)
5970 (t
5971 (goto-char back)
5972 (cond
5973 ((= (preceding-char) ?\@)
5974 (backward-char)
5975 (save-excursion
5976 (verilog-backward-token)
5977 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
5978 ((= (preceding-char) ?\#)
5979 (backward-char))
5980 (t t)))))))
5981
5982 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
5983 t
5984 (forward-word -1)
5985 (while (or (= (preceding-char) ?\_)
5986 (= (preceding-char) ?\@)
5987 (= (preceding-char) ?\.))
5988 (forward-word -1))
5989 (cond
5990 ((looking-at "\\<else\\>")
5991 t)
5992 ((looking-at verilog-behavioral-block-beg-re)
5993 t)
5994 ((looking-at verilog-indent-re)
5995 nil)
5996 (t
5997 (let
5998 ((back (point)))
5999 (verilog-backward-syntactic-ws)
6000 (cond
6001 ((= (preceding-char) ?\:)
6002 (backward-char)
6003 (verilog-backward-syntactic-ws)
6004 (backward-sexp)
6005 (if (looking-at verilog-nameable-item-re )
6006 nil
6007 t))
6008 ((= (preceding-char) ?\#)
6009 (backward-char)
6010 t)
6011 ((= (preceding-char) ?\`)
6012 (backward-char)
6013 t)
6014
6015 (t
6016 (goto-char back)
6017 t))))))))
6018
6019 (defun verilog-backward-syntactic-ws ()
6020 "Move backwards putting point after first non-whitespace non-comment."
6021 (verilog-skip-backward-comments)
6022 (forward-comment (- (buffer-size))))
6023
6024 (defun verilog-backward-syntactic-ws-quick ()
6025 "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
6026 (while (cond ((bobp)
6027 nil) ; Done
6028 ((< (skip-syntax-backward " ") 0)
6029 t)
6030 ((eq (preceding-char) ?\n) ; \n's terminate // so aren't space syntax
6031 (forward-char -1)
6032 t)
6033 ((or (verilog-inside-comment-or-string-p (1- (point)))
6034 (verilog-inside-comment-or-string-p (point)))
6035 (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
6036 t))))
6037
6038 (defun verilog-forward-syntactic-ws ()
6039 (verilog-skip-forward-comment-p)
6040 (forward-comment (buffer-size)))
6041
6042 (defun verilog-backward-ws&directives (&optional bound)
6043 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
6044 Optional BOUND limits search."
6045 (save-restriction
6046 (let* ((bound (or bound (point-min)))
6047 (here bound)
6048 (p nil) )
6049 (if (< bound (point))
6050 (progn
6051 (let ((state (save-excursion (verilog-syntax-ppss))))
6052 (cond
6053 ((nth 7 state) ; in // comment
6054 (verilog-re-search-backward "//" nil 'move)
6055 (skip-chars-backward "/"))
6056 ((nth 4 state) ; in /* */ comment
6057 (verilog-re-search-backward "/\\*" nil 'move))))
6058 (narrow-to-region bound (point))
6059 (while (/= here (point))
6060 (setq here (point))
6061 (verilog-skip-backward-comments)
6062 (setq p
6063 (save-excursion
6064 (beginning-of-line)
6065 ;; for as long as we're right after a continued line, keep moving up
6066 (while (and (verilog-looking-back "\\\\[\n\r\f]" nil)
6067 (forward-line -1)))
6068 (cond
6069 ((and verilog-highlight-translate-off
6070 (verilog-within-translate-off))
6071 (verilog-back-to-start-translate-off (point-min)))
6072 ((looking-at verilog-directive-re-1)
6073 (point))
6074 (t
6075 nil))))
6076 (if p (goto-char p))))))))
6077
6078 (defun verilog-forward-ws&directives (&optional bound)
6079 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
6080 Optional BOUND limits search."
6081 (save-restriction
6082 (let* ((bound (or bound (point-max)))
6083 (here bound)
6084 jump)
6085 (if (> bound (point))
6086 (progn
6087 (let ((state (save-excursion (verilog-syntax-ppss))))
6088 (cond
6089 ((nth 7 state) ; in // comment
6090 (end-of-line)
6091 (forward-char 1)
6092 (skip-chars-forward " \t\n\f")
6093 )
6094 ((nth 4 state) ; in /* */ comment
6095 (verilog-re-search-forward "\\*/\\s-*" nil 'move))))
6096 (narrow-to-region (point) bound)
6097 (while (/= here (point))
6098 (setq here (point)
6099 jump nil)
6100 (forward-comment (buffer-size))
6101 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ; Attribute
6102 (goto-char (match-end 0)))
6103 (save-excursion
6104 (beginning-of-line)
6105 (if (looking-at verilog-directive-re-1)
6106 (setq jump t)))
6107 (if jump
6108 (beginning-of-line 2))))))))
6109
6110 (defun verilog-in-comment-p ()
6111 "Return true if in a star or // comment."
6112 (let ((state (save-excursion (verilog-syntax-ppss))))
6113 (or (nth 4 state) (nth 7 state))))
6114
6115 (defun verilog-in-star-comment-p ()
6116 "Return true if in a star comment."
6117 (let ((state (save-excursion (verilog-syntax-ppss))))
6118 (and
6119 (nth 4 state) ; t if in a comment of style a // or b /**/
6120 (not
6121 (nth 7 state) ; t if in a comment of style b /**/
6122 ))))
6123
6124 (defun verilog-in-slash-comment-p ()
6125 "Return true if in a slash comment."
6126 (let ((state (save-excursion (verilog-syntax-ppss))))
6127 (nth 7 state)))
6128
6129 (defun verilog-in-comment-or-string-p ()
6130 "Return true if in a string or comment."
6131 (let ((state (save-excursion (verilog-syntax-ppss))))
6132 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
6133
6134 (defun verilog-in-attribute-p ()
6135 "Return true if point is in an attribute (* [] attribute *)."
6136 (save-match-data
6137 (save-excursion
6138 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
6139 (cond
6140 ((match-end 1)
6141 (progn (goto-char (match-end 1))
6142 (not (looking-at "\\s-*)")))
6143 nil)
6144 ((match-end 2)
6145 (progn (goto-char (match-beginning 2))
6146 (not (looking-at "(\\s-*")))
6147 nil)
6148 (t nil)))))
6149
6150 (defun verilog-in-parameter-p ()
6151 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
6152 (save-match-data
6153 (save-excursion
6154 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
6155 (numberp (match-beginning 1)))))
6156
6157 (defun verilog-in-escaped-name-p ()
6158 "Return true if in an escaped name."
6159 (save-excursion
6160 (backward-char)
6161 (skip-chars-backward "^ \t\n\f")
6162 (if (equal (char-after (point) ) ?\\ )
6163 t
6164 nil)))
6165
6166 (defun verilog-in-directive-p ()
6167 "Return true if in a directive."
6168 (save-excursion
6169 (beginning-of-line)
6170 (looking-at verilog-directive-re-1)))
6171
6172 (defun verilog-in-parenthesis-p ()
6173 "Return true if in a ( ) expression (but not { } or [ ])."
6174 (save-match-data
6175 (save-excursion
6176 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
6177 (numberp (match-beginning 1)))))
6178
6179 (defun verilog-in-paren ()
6180 "Return true if in a parenthetical expression.
6181 May cache result using `verilog-syntax-ppss'."
6182 (let ((state (save-excursion (verilog-syntax-ppss))))
6183 (> (nth 0 state) 0 )))
6184
6185 (defun verilog-in-paren-count ()
6186 "Return paren depth, floor to 0.
6187 May cache result using `verilog-syntax-ppss'."
6188 (let ((state (save-excursion (verilog-syntax-ppss))))
6189 (if (> (nth 0 state) 0)
6190 (nth 0 state)
6191 0 )))
6192
6193 (defun verilog-in-paren-quick ()
6194 "Return true if in a parenthetical expression.
6195 Always starts from `point-min', to allow inserts with hooks disabled."
6196 ;; The -quick refers to its use alongside the other -quick functions,
6197 ;; not that it's likely to be faster than verilog-in-paren.
6198 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
6199 (> (nth 0 state) 0 )))
6200
6201 (defun verilog-in-struct-p ()
6202 "Return true if in a struct declaration."
6203 (interactive)
6204 (save-excursion
6205 (if (verilog-in-paren)
6206 (progn
6207 (verilog-backward-up-list 1)
6208 (verilog-at-struct-p)
6209 )
6210 nil)))
6211
6212 (defun verilog-in-struct-nested-p ()
6213 "Return nil for not in struct.
6214 Return 0 for in non-nested struct.
6215 Return >0 for nested struct."
6216 (interactive)
6217 (let (col)
6218 (save-excursion
6219 (if (verilog-in-paren)
6220 (progn
6221 (verilog-backward-up-list 1)
6222 (setq col (verilog-at-struct-mv-p))
6223 (if col
6224 (if (verilog-in-struct-p) (current-column) 0)))
6225 nil))))
6226
6227 (defun verilog-in-coverage-p ()
6228 "Return true if in a constraint or coverpoint expression."
6229 (interactive)
6230 (save-excursion
6231 (if (verilog-in-paren)
6232 (progn
6233 (verilog-backward-up-list 1)
6234 (verilog-at-constraint-p)
6235 )
6236 nil)))
6237
6238 (defun verilog-at-close-constraint-p ()
6239 "If at the } that closes a constraint or covergroup, return true."
6240 (if (and
6241 (equal (char-after) ?\})
6242 (verilog-in-coverage-p))
6243
6244 (save-excursion
6245 (verilog-backward-ws&directives)
6246 (if (or (equal (char-before) ?\;)
6247 (equal (char-before) ?\}) ; can end with inner constraint { } block or ;
6248 (equal (char-before) ?\{)) ; empty constraint block
6249 (point)
6250 nil))))
6251
6252 (defun verilog-at-constraint-p ()
6253 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
6254 (if (save-excursion
6255 (let ((p (point)))
6256 (and
6257 (equal (char-after) ?\{)
6258 (forward-list)
6259 (progn (backward-char 1)
6260 (verilog-backward-ws&directives)
6261 (and
6262 (or (equal (char-before) ?\{) ; empty case
6263 (equal (char-before) ?\;)
6264 (equal (char-before) ?\}))
6265 ;; skip what looks like bus repetition operator {#{
6266 (not (string-match "^{\\s-*[0-9]+\\s-*{" (buffer-substring p (point)))))))))
6267 (progn
6268 (let ( (pt (point)) (pass 0))
6269 (verilog-backward-ws&directives)
6270 (verilog-backward-token)
6271 (if (looking-at (concat "\\<constraint\\|coverpoint\\|cross\\|with\\>\\|" verilog-in-constraint-re))
6272 (progn (setq pass 1)
6273 (if (looking-at "\\<with\\>")
6274 (progn (verilog-backward-ws&directives)
6275 (beginning-of-line) ; 1
6276 (verilog-forward-ws&directives)
6277 1 )
6278 (verilog-beg-of-statement)
6279 ))
6280 ;; if first word token not keyword, it maybe the instance name
6281 ;; check next word token
6282 (if (looking-at "\\<\\w+\\>\\|\\s-*(\\s-*\\S-+")
6283 (progn (verilog-beg-of-statement)
6284 (if (looking-at (concat "\\<\\(constraint\\|"
6285 "\\(?:\\w+\\s-*:\\s-*\\)?\\(coverpoint\\|cross\\)"
6286 "\\|with\\)\\>\\|" verilog-in-constraint-re))
6287 (setq pass 1)))))
6288 (if (eq pass 0)
6289 (progn (goto-char pt) nil) 1)))
6290 ;; not
6291 nil))
6292
6293 (defun verilog-at-struct-p ()
6294 "If at the { of a struct, return true, not moving point."
6295 (save-excursion
6296 (if (and (equal (char-after) ?\{)
6297 (verilog-backward-token))
6298 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6299 nil)))
6300
6301 (defun verilog-at-struct-mv-p ()
6302 "If at the { of a struct, return true, moving point to struct."
6303 (let ((pt (point)))
6304 (if (and (equal (char-after) ?\{)
6305 (verilog-backward-token))
6306 (if (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6307 (progn (verilog-beg-of-statement) (point))
6308 (progn (goto-char pt) nil))
6309 (progn (goto-char pt) nil))))
6310
6311 (defun verilog-at-close-struct-p ()
6312 "If at the } that closes a struct, return true."
6313 (if (and
6314 (equal (char-after) ?\})
6315 (verilog-in-struct-p))
6316 ;; true
6317 (save-excursion
6318 (if (looking-at "}\\(?:\\s-*\\w+\\s-*\\)?;") 1))
6319 ;; false
6320 nil))
6321
6322 (defun verilog-parenthesis-depth ()
6323 "Return non zero if in parenthetical-expression."
6324 (save-excursion (nth 1 (verilog-syntax-ppss))))
6325
6326
6327 (defun verilog-skip-forward-comment-or-string ()
6328 "Return true if in a string or comment."
6329 (let ((state (save-excursion (verilog-syntax-ppss))))
6330 (cond
6331 ((nth 3 state) ;Inside string
6332 (search-forward "\"")
6333 t)
6334 ((nth 7 state) ;Inside // comment
6335 (forward-line 1)
6336 t)
6337 ((nth 4 state) ;Inside any comment (hence /**/)
6338 (search-forward "*/"))
6339 (t
6340 nil))))
6341
6342 (defun verilog-skip-backward-comment-or-string ()
6343 "Return true if in a string or comment."
6344 (let ((state (save-excursion (verilog-syntax-ppss))))
6345 (cond
6346 ((nth 3 state) ;Inside string
6347 (search-backward "\"")
6348 t)
6349 ((nth 7 state) ;Inside // comment
6350 (search-backward "//")
6351 (skip-chars-backward "/")
6352 t)
6353 ((nth 4 state) ;Inside /* */ comment
6354 (search-backward "/*")
6355 t)
6356 (t
6357 nil))))
6358
6359 (defun verilog-skip-backward-comments ()
6360 "Return true if a comment was skipped."
6361 (let ((more t))
6362 (while more
6363 (setq more
6364 (let ((state (save-excursion (verilog-syntax-ppss))))
6365 (cond
6366 ((nth 7 state) ;Inside // comment
6367 (search-backward "//")
6368 (skip-chars-backward "/")
6369 (skip-chars-backward " \t\n\f")
6370 t)
6371 ((nth 4 state) ;Inside /* */ comment
6372 (search-backward "/*")
6373 (skip-chars-backward " \t\n\f")
6374 t)
6375 ((and (not (bobp))
6376 (= (char-before) ?\/)
6377 (= (char-before (1- (point))) ?\*))
6378 (goto-char (- (point) 2))
6379 t) ; Let nth 4 state handle the rest
6380 ((and (not (bobp))
6381 ;;(verilog-looking-back "\\*)" nil) ;; super slow, use two char-before instead
6382 (= (char-before) ?\))
6383 (= (char-before (1- (point))) ?\*)
6384 (not (verilog-looking-back "(\\s-*\\*)" nil))) ;; slow but unlikely to be called
6385 (goto-char (- (point) 2))
6386 (if (search-backward "(*" nil t)
6387 (progn
6388 (skip-chars-backward " \t\n\f")
6389 t)
6390 (progn
6391 (goto-char (+ (point) 2))
6392 nil)))
6393 (t
6394 (/= (skip-chars-backward " \t\n\f") 0))))))))
6395
6396 (defun verilog-skip-forward-comment-p ()
6397 "If in comment, move to end and return true."
6398 (let* (h
6399 (state (save-excursion (verilog-syntax-ppss)))
6400 (skip (cond
6401 ((nth 3 state) ;Inside string
6402 t)
6403 ((nth 7 state) ;Inside // comment
6404 (end-of-line)
6405 (forward-char 1)
6406 t)
6407 ((nth 4 state) ;Inside /* comment
6408 (search-forward "*/")
6409 t)
6410 ((verilog-in-attribute-p) ;Inside (* attribute
6411 (search-forward "*)" nil t)
6412 t)
6413 (t nil))))
6414 (skip-chars-forward " \t\n\f")
6415 (while
6416 (cond
6417 ((looking-at "\\/\\*")
6418 (progn
6419 (setq h (point))
6420 (goto-char (match-end 0))
6421 (if (search-forward "*/" nil t)
6422 (progn
6423 (skip-chars-forward " \t\n\f")
6424 (setq skip 't))
6425 (progn
6426 (goto-char h)
6427 nil))))
6428 ((and (looking-at "(\\*") ; attribute start, but not an event (*) or (* )
6429 (not (looking-at "(\\*\\s-*)")))
6430 (progn
6431 (setq h (point))
6432 (goto-char (match-end 0))
6433 (if (search-forward "*)" nil t)
6434 (progn
6435 (skip-chars-forward " \t\n\f")
6436 (setq skip 't))
6437 (progn
6438 (goto-char h)
6439 nil))))
6440 (t nil)))
6441 skip))
6442
6443 (defun verilog-indent-line-relative ()
6444 "Cheap version of indent line.
6445 Only look at a few lines to determine indent level."
6446 (interactive)
6447 (let ((indent-str)
6448 (sp (point)))
6449 (if (looking-at "^[ \t]*$")
6450 (cond ;- A blank line; No need to be too smart.
6451 ((bobp)
6452 (setq indent-str (list 'cpp 0)))
6453 ((verilog-continued-line)
6454 (let ((sp1 (point)))
6455 (if (verilog-continued-line)
6456 (progn
6457 (goto-char sp)
6458 (setq indent-str
6459 (list 'statement (verilog-current-indent-level))))
6460 (goto-char sp1)
6461 (setq indent-str (list 'block (verilog-current-indent-level)))))
6462 (goto-char sp))
6463 ((goto-char sp)
6464 (setq indent-str (verilog-calculate-indent))))
6465 (progn (skip-chars-forward " \t")
6466 (setq indent-str (verilog-calculate-indent))))
6467 (verilog-do-indent indent-str)))
6468
6469 (defun verilog-indent-line ()
6470 "Indent for special part of code."
6471 (verilog-do-indent (verilog-calculate-indent)))
6472
6473 (defun verilog-do-indent (indent-str)
6474 (let ((type (car indent-str))
6475 (ind (car (cdr indent-str))))
6476 (cond
6477 (; handle continued exp
6478 (eq type 'cexp)
6479 (let ((here (point)))
6480 (verilog-backward-syntactic-ws)
6481 (cond
6482 ((or
6483 (= (preceding-char) ?\,)
6484 (save-excursion
6485 (verilog-beg-of-statement-1)
6486 (looking-at verilog-declaration-re)))
6487 (let* ( fst
6488 (val
6489 (save-excursion
6490 (backward-char 1)
6491 (verilog-beg-of-statement-1)
6492 (setq fst (point))
6493 (if (looking-at verilog-declaration-re)
6494 (progn ; we have multiple words
6495 (goto-char (match-end 0))
6496 (skip-chars-forward " \t")
6497 (cond
6498 ((and verilog-indent-declaration-macros
6499 (= (following-char) ?\`))
6500 (progn
6501 (forward-char 1)
6502 (forward-word 1)
6503 (skip-chars-forward " \t")))
6504 ((= (following-char) ?\[)
6505 (progn
6506 (forward-char 1)
6507 (verilog-backward-up-list -1)
6508 (skip-chars-forward " \t"))))
6509 (current-column))
6510 (progn
6511 (goto-char fst)
6512 (+ (current-column) verilog-cexp-indent))))))
6513 (goto-char here)
6514 (indent-line-to val)
6515 (if (and (not verilog-indent-lists)
6516 (verilog-in-paren))
6517 (verilog-pretty-declarations-auto))
6518 ))
6519 ((= (preceding-char) ?\) )
6520 (goto-char here)
6521 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6522 (indent-line-to val)))
6523 (t
6524 (goto-char here)
6525 (let ((val))
6526 (verilog-beg-of-statement-1)
6527 (if (and (< (point) here)
6528 (verilog-re-search-forward "=[ \\t]*" here 'move)
6529 ;; not at a |=>, #=#, or [=n] operator
6530 (not (string-match "\\[=.\\|#=#\\||=>"
6531 (or (buffer-substring (- (point) 2) (1+ (point)))
6532 "")))) ; don't let buffer over/under-run spoil the party
6533 (setq val (current-column))
6534 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6535 (goto-char here)
6536 (indent-line-to val))))))
6537
6538 (; handle inside parenthetical expressions
6539 (eq type 'cparenexp)
6540 (let* ( here
6541 (val (save-excursion
6542 (verilog-backward-up-list 1)
6543 (forward-char 1)
6544 (if verilog-indent-lists
6545 (skip-chars-forward " \t")
6546 (verilog-forward-syntactic-ws))
6547 (setq here (point))
6548 (current-column)))
6549
6550 (decl (save-excursion
6551 (goto-char here)
6552 (verilog-forward-syntactic-ws)
6553 (setq here (point))
6554 (looking-at verilog-declaration-re))))
6555 (indent-line-to val)
6556 (if decl
6557 (verilog-pretty-declarations-auto))))
6558
6559 (;-- Handle the ends
6560 (or
6561 (looking-at verilog-end-block-re)
6562 (verilog-at-close-constraint-p)
6563 (verilog-at-close-struct-p))
6564 (let ((val (if (eq type 'statement)
6565 (- ind verilog-indent-level)
6566 ind)))
6567 (indent-line-to val)))
6568
6569 (;-- Case -- maybe line 'em up
6570 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6571 (progn
6572 (cond
6573 ((looking-at "\\<endcase\\>")
6574 (indent-line-to ind))
6575 (t
6576 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6577 (indent-line-to val))))))
6578
6579 (;-- defun
6580 (and (eq type 'defun)
6581 (looking-at verilog-zero-indent-re))
6582 (indent-line-to 0))
6583
6584 (;-- declaration
6585 (and (or
6586 (eq type 'defun)
6587 (eq type 'block))
6588 (looking-at verilog-declaration-re)
6589 ;; Do not consider "virtual function", "virtual task", "virtual class"
6590 ;; as declarations
6591 (not (looking-at (concat verilog-declaration-re
6592 "\\s-+\\(function\\|task\\|class\\)\\b"))))
6593 (verilog-indent-declaration ind))
6594
6595 (;-- form feeds - ignored as bug in indent-line-to in < 24.5
6596 (looking-at "\f"))
6597
6598 (;-- Everything else
6599 t
6600 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6601 (indent-line-to val))))
6602
6603 (if (looking-at "[ \t]+$")
6604 (skip-chars-forward " \t"))
6605 indent-str ; Return indent data
6606 ))
6607
6608 (defun verilog-current-indent-level ()
6609 "Return the indent-level of the current statement."
6610 (save-excursion
6611 (let (par-pos)
6612 (beginning-of-line)
6613 (setq par-pos (verilog-parenthesis-depth))
6614 (while par-pos
6615 (goto-char par-pos)
6616 (beginning-of-line)
6617 (setq par-pos (verilog-parenthesis-depth)))
6618 (skip-chars-forward " \t")
6619 (current-column))))
6620
6621 (defun verilog-case-indent-level ()
6622 "Return the indent-level of the current statement.
6623 Do not count named blocks or case-statements."
6624 (save-excursion
6625 (skip-chars-forward " \t")
6626 (cond
6627 ((looking-at verilog-named-block-re)
6628 (current-column))
6629 ((and (not (looking-at verilog-extended-case-re))
6630 (looking-at "^[^:;]+[ \t]*:"))
6631 (verilog-re-search-forward ":" nil t)
6632 (skip-chars-forward " \t")
6633 (current-column))
6634 (t
6635 (current-column)))))
6636
6637 (defun verilog-indent-comment ()
6638 "Indent current line as comment."
6639 (let* ((stcol
6640 (cond
6641 ((verilog-in-star-comment-p)
6642 (save-excursion
6643 (re-search-backward "/\\*" nil t)
6644 (1+(current-column))))
6645 (comment-column
6646 comment-column )
6647 (t
6648 (save-excursion
6649 (re-search-backward "//" nil t)
6650 (current-column))))))
6651 (indent-line-to stcol)
6652 stcol))
6653
6654 (defun verilog-more-comment ()
6655 "Make more comment lines like the previous."
6656 (let* ((star 0)
6657 (stcol
6658 (cond
6659 ((verilog-in-star-comment-p)
6660 (save-excursion
6661 (setq star 1)
6662 (re-search-backward "/\\*" nil t)
6663 (1+(current-column))))
6664 (comment-column
6665 comment-column )
6666 (t
6667 (save-excursion
6668 (re-search-backward "//" nil t)
6669 (current-column))))))
6670 (progn
6671 (indent-to stcol)
6672 (if (and star
6673 (save-excursion
6674 (forward-line -1)
6675 (skip-chars-forward " \t")
6676 (looking-at "\\*")))
6677 (insert "* ")))))
6678
6679 (defun verilog-comment-indent (&optional _arg)
6680 "Return the column number the line should be indented to.
6681 _ARG is ignored, for `comment-indent-function' compatibility."
6682 (cond
6683 ((verilog-in-star-comment-p)
6684 (save-excursion
6685 (re-search-backward "/\\*" nil t)
6686 (1+(current-column))))
6687 ( comment-column
6688 comment-column )
6689 (t
6690 (save-excursion
6691 (re-search-backward "//" nil t)
6692 (current-column)))))
6693
6694 ;;
6695
6696 (defun verilog-pretty-declarations-auto (&optional quiet)
6697 "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
6698 (when (or (eq 'all verilog-auto-lineup)
6699 (eq 'declarations verilog-auto-lineup))
6700 (verilog-pretty-declarations quiet)))
6701
6702 (defun verilog-pretty-declarations (&optional quiet)
6703 "Line up declarations around point.
6704 Be verbose about progress unless optional QUIET set."
6705 (interactive)
6706 (let* ((m1 (make-marker))
6707 (e (point))
6708 el
6709 r
6710 (here (point))
6711 ind
6712 start
6713 startpos
6714 end
6715 endpos
6716 base-ind
6717 )
6718 (save-excursion
6719 (if (progn
6720 ;; (verilog-beg-of-statement-1)
6721 (beginning-of-line)
6722 (verilog-forward-syntactic-ws)
6723 (and (not (verilog-in-directive-p)) ; could have `define input foo
6724 (looking-at verilog-declaration-re)))
6725 (progn
6726 (if (verilog-parenthesis-depth)
6727 ;; in an argument list or parameter block
6728 (setq el (verilog-backward-up-list -1)
6729 start (progn
6730 (goto-char e)
6731 (verilog-backward-up-list 1)
6732 (forward-line) ; ignore ( input foo,
6733 (verilog-re-search-forward verilog-declaration-re el 'move)
6734 (goto-char (match-beginning 0))
6735 (skip-chars-backward " \t")
6736 (point))
6737 startpos (set-marker (make-marker) start)
6738 end (progn
6739 (goto-char start)
6740 (verilog-backward-up-list -1)
6741 (forward-char -1)
6742 (verilog-backward-syntactic-ws)
6743 (point))
6744 endpos (set-marker (make-marker) end)
6745 base-ind (progn
6746 (goto-char start)
6747 (forward-char 1)
6748 (skip-chars-forward " \t")
6749 (current-column)))
6750 ;; in a declaration block (not in argument list)
6751 (setq
6752 start (progn
6753 (verilog-beg-of-statement-1)
6754 (while (and (looking-at verilog-declaration-re)
6755 (not (bobp)))
6756 (skip-chars-backward " \t")
6757 (setq e (point))
6758 (beginning-of-line)
6759 (verilog-backward-syntactic-ws)
6760 (backward-char)
6761 (verilog-beg-of-statement-1))
6762 e)
6763 startpos (set-marker (make-marker) start)
6764 end (progn
6765 (goto-char here)
6766 (verilog-end-of-statement)
6767 (setq e (point)) ;Might be on last line
6768 (verilog-forward-syntactic-ws)
6769 (while (looking-at verilog-declaration-re)
6770 (verilog-end-of-statement)
6771 (setq e (point))
6772 (verilog-forward-syntactic-ws))
6773 e)
6774 endpos (set-marker (make-marker) end)
6775 base-ind (progn
6776 (goto-char start)
6777 (verilog-do-indent (verilog-calculate-indent))
6778 (verilog-forward-ws&directives)
6779 (current-column))))
6780 ;; OK, start and end are set
6781 (goto-char (marker-position startpos))
6782 (if (and (not quiet)
6783 (> (- end start) 100))
6784 (message "Lining up declarations..(please stand by)"))
6785 ;; Get the beginning of line indent first
6786 (while (progn (setq e (marker-position endpos))
6787 (< (point) e))
6788 (cond
6789 ((save-excursion (skip-chars-backward " \t")
6790 (bolp))
6791 (verilog-forward-ws&directives)
6792 (indent-line-to base-ind)
6793 (verilog-forward-ws&directives)
6794 (if (< (point) e)
6795 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6796 (t
6797 (just-one-space)
6798 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6799 ;;(forward-line)
6800 )
6801 ;; Now find biggest prefix
6802 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6803 ;; Now indent each line.
6804 (goto-char (marker-position startpos))
6805 (while (progn (setq e (marker-position endpos))
6806 (setq r (- e (point)))
6807 (> r 0))
6808 (setq e (point))
6809 (unless quiet (message "%d" r))
6810 ;; (verilog-do-indent (verilog-calculate-indent)))
6811 (verilog-forward-ws&directives)
6812 (cond
6813 ((or (and verilog-indent-declaration-macros
6814 (looking-at verilog-declaration-re-2-macro))
6815 (looking-at verilog-declaration-re-2-no-macro))
6816 (let ((p (match-end 0)))
6817 (set-marker m1 p)
6818 (if (verilog-re-search-forward "[[#`]" p 'move)
6819 (progn
6820 (forward-char -1)
6821 (just-one-space)
6822 (goto-char (marker-position m1))
6823 (just-one-space)
6824 (indent-to ind))
6825 (progn
6826 (just-one-space)
6827 (indent-to ind)))))
6828 ((verilog-continued-line-1 (marker-position startpos))
6829 (goto-char e)
6830 (indent-line-to ind))
6831 ((verilog-in-struct-p)
6832 ;; could have a declaration of a user defined item
6833 (goto-char e)
6834 (verilog-end-of-statement))
6835 (t ; Must be comment or white space
6836 (goto-char e)
6837 (verilog-forward-ws&directives)
6838 (forward-line -1)))
6839 (forward-line 1))
6840 (unless quiet (message "")))))))
6841
6842 (defun verilog-pretty-expr (&optional quiet _myre)
6843 "Line up expressions around point, optionally QUIET with regexp _MYRE ignored."
6844 (interactive)
6845 (if (not (verilog-in-comment-or-string-p))
6846 (save-excursion
6847 (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
6848 (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
6849 (beginning-of-line)
6850 (if (and (not (looking-at rexp ))
6851 (looking-at verilog-assignment-operation-re)
6852 (save-excursion
6853 (goto-char (match-end 2))
6854 (and (not (verilog-in-attribute-p))
6855 (not (verilog-in-parameter-p))
6856 (not (verilog-in-comment-or-string-p)))))
6857 (let* ((here (point))
6858 (e) (r)
6859 (start
6860 (progn
6861 (beginning-of-line)
6862 (setq e (point))
6863 (verilog-backward-syntactic-ws)
6864 (beginning-of-line)
6865 (while (and (not (looking-at rexp1))
6866 (looking-at verilog-assignment-operation-re)
6867 (not (bobp))
6868 )
6869 (setq e (point))
6870 (verilog-backward-syntactic-ws)
6871 (beginning-of-line)
6872 ) ;Ack, need to grok `define
6873 e))
6874 (end
6875 (progn
6876 (goto-char here)
6877 (end-of-line)
6878 (setq e (point)) ;Might be on last line
6879 (verilog-forward-syntactic-ws)
6880 (beginning-of-line)
6881 (while (and
6882 (not (looking-at rexp1 ))
6883 (looking-at verilog-assignment-operation-re)
6884 (progn
6885 (end-of-line)
6886 (not (eq e (point)))))
6887 (setq e (point))
6888 (verilog-forward-syntactic-ws)
6889 (beginning-of-line)
6890 )
6891 e))
6892 (endpos (set-marker (make-marker) end))
6893 (ind)
6894 )
6895 (goto-char start)
6896 (verilog-do-indent (verilog-calculate-indent))
6897 (if (and (not quiet)
6898 (> (- end start) 100))
6899 (message "Lining up expressions..(please stand by)"))
6900
6901 ;; Set indent to minimum throughout region
6902 (while (< (point) (marker-position endpos))
6903 (beginning-of-line)
6904 (verilog-just-one-space verilog-assignment-operation-re)
6905 (beginning-of-line)
6906 (verilog-do-indent (verilog-calculate-indent))
6907 (end-of-line)
6908 (verilog-forward-syntactic-ws)
6909 )
6910
6911 ;; Now find biggest prefix
6912 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6913
6914 ;; Now indent each line.
6915 (goto-char start)
6916 (while (progn (setq e (marker-position endpos))
6917 (setq r (- e (point)))
6918 (> r 0))
6919 (setq e (point))
6920 (if (not quiet) (message "%d" r))
6921 (cond
6922 ((looking-at verilog-assignment-operation-re)
6923 (goto-char (match-beginning 2))
6924 (if (not (or (verilog-in-parenthesis-p) ; leave attributes and comparisons alone
6925 (verilog-in-coverage-p)))
6926 (if (eq (char-after) ?=)
6927 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6928 (indent-to ind)
6929 ))
6930 )
6931 ((verilog-continued-line-1 start)
6932 (goto-char e)
6933 (indent-line-to ind))
6934 (t ; Must be comment or white space
6935 (goto-char e)
6936 (verilog-forward-ws&directives)
6937 (forward-line -1))
6938 )
6939 (forward-line 1))
6940 (unless quiet (message ""))
6941 ))))))
6942
6943 (defun verilog-just-one-space (myre)
6944 "Remove extra spaces around regular expression MYRE."
6945 (interactive)
6946 (if (and (not(looking-at verilog-complete-reg))
6947 (looking-at myre))
6948 (let ((p1 (match-end 1))
6949 (p2 (match-end 2)))
6950 (progn
6951 (goto-char p2)
6952 (just-one-space)
6953 (goto-char p1)
6954 (just-one-space)))))
6955
6956 (defun verilog-indent-declaration (baseind)
6957 "Indent current lines as declaration.
6958 Line up the variable names based on previous declaration's indentation.
6959 BASEIND is the base indent to offset everything."
6960 (interactive)
6961 (let ((pos (point-marker))
6962 (lim (save-excursion
6963 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
6964 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
6965 (point)))
6966 (ind)
6967 (val)
6968 (m1 (make-marker)))
6969 (setq val
6970 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6971 (indent-line-to val)
6972
6973 ;; Use previous declaration (in this module) as template.
6974 (if (or (eq 'all verilog-auto-lineup)
6975 (eq 'declarations verilog-auto-lineup))
6976 (if (verilog-re-search-backward
6977 (or (and verilog-indent-declaration-macros
6978 verilog-declaration-re-1-macro)
6979 verilog-declaration-re-1-no-macro) lim t)
6980 (progn
6981 (goto-char (match-end 0))
6982 (skip-chars-forward " \t")
6983 (setq ind (current-column))
6984 (goto-char pos)
6985 (setq val
6986 (+ baseind
6987 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6988 (indent-line-to val)
6989 (if (and verilog-indent-declaration-macros
6990 (looking-at verilog-declaration-re-2-macro))
6991 (let ((p (match-end 0)))
6992 (set-marker m1 p)
6993 (if (verilog-re-search-forward "[[#`]" p 'move)
6994 (progn
6995 (forward-char -1)
6996 (just-one-space)
6997 (goto-char (marker-position m1))
6998 (just-one-space)
6999 (indent-to ind))
7000 (if (/= (current-column) ind)
7001 (progn
7002 (just-one-space)
7003 (indent-to ind)))))
7004 (if (looking-at verilog-declaration-re-2-no-macro)
7005 (let ((p (match-end 0)))
7006 (set-marker m1 p)
7007 (if (verilog-re-search-forward "[[`#]" p 'move)
7008 (progn
7009 (forward-char -1)
7010 (just-one-space)
7011 (goto-char (marker-position m1))
7012 (just-one-space)
7013 (indent-to ind))
7014 (if (/= (current-column) ind)
7015 (progn
7016 (just-one-space)
7017 (indent-to ind))))))))))
7018 (goto-char pos)))
7019
7020 (defun verilog-get-lineup-indent (b edpos)
7021 "Return the indent level that will line up several lines within the region.
7022 Region is defined by B and EDPOS."
7023 (save-excursion
7024 (let ((ind 0) e)
7025 (goto-char b)
7026 ;; Get rightmost position
7027 (while (progn (setq e (marker-position edpos))
7028 (< (point) e))
7029 (if (verilog-re-search-forward
7030 (or (and verilog-indent-declaration-macros
7031 verilog-declaration-re-1-macro)
7032 verilog-declaration-re-1-no-macro) e 'move)
7033 (progn
7034 (goto-char (match-end 0))
7035 (verilog-backward-syntactic-ws)
7036 (if (> (current-column) ind)
7037 (setq ind (current-column)))
7038 (goto-char (match-end 0)))))
7039 (if (> ind 0)
7040 (1+ ind)
7041 ;; No lineup-string found
7042 (goto-char b)
7043 (end-of-line)
7044 (verilog-backward-syntactic-ws)
7045 ;;(skip-chars-backward " \t")
7046 (1+ (current-column))))))
7047
7048 (defun verilog-get-lineup-indent-2 (myre b edpos)
7049 "Return the indent level that will line up several lines within the region."
7050 (save-excursion
7051 (let ((ind 0) e)
7052 (goto-char b)
7053 ;; Get rightmost position
7054 (while (progn (setq e (marker-position edpos))
7055 (< (point) e))
7056 (if (and (verilog-re-search-forward myre e 'move)
7057 (not (verilog-in-attribute-p))) ; skip attribute exprs
7058 (progn
7059 (goto-char (match-beginning 2))
7060 (verilog-backward-syntactic-ws)
7061 (if (> (current-column) ind)
7062 (setq ind (current-column)))
7063 (goto-char (match-end 0)))
7064 ))
7065 (if (> ind 0)
7066 (1+ ind)
7067 ;; No lineup-string found
7068 (goto-char b)
7069 (end-of-line)
7070 (skip-chars-backward " \t")
7071 (1+ (current-column))))))
7072
7073 (defun verilog-comment-depth (type val)
7074 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
7075 (save-excursion
7076 (let
7077 ((b (prog2
7078 (beginning-of-line)
7079 (point-marker)
7080 (end-of-line))))
7081 (if (re-search-backward " /\\* [#-]# [a-zA-Z]+ [0-9]+ ## \\*/" b t)
7082 (progn
7083 (replace-match " /* -# ## */")
7084 (end-of-line))
7085 (progn
7086 (end-of-line)
7087 (insert " /* ## ## */"))))
7088 (backward-char 6)
7089 (insert
7090 (format "%s %d" type val))))
7091
7092 \f
7093 ;;; Completion:
7094 ;;
7095 (defvar verilog-str nil)
7096 (defvar verilog-all nil)
7097 (defvar verilog-pred nil)
7098 (defvar verilog-buffer-to-use nil)
7099 (defvar verilog-flag nil)
7100 (defvar verilog-toggle-completions nil
7101 "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
7102 Repeated use of \\[verilog-complete-word] will show you all of them.
7103 Normally, when there is more than one possible completion,
7104 it displays a list of all possible completions.")
7105
7106
7107 (defvar verilog-type-keywords
7108 '(
7109 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
7110 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
7111 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
7112 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
7113 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
7114 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
7115 )
7116 "Keywords for types used when completing a word in a declaration or parmlist.
7117 \(integer, real, reg...)")
7118
7119 (defvar verilog-cpp-keywords
7120 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
7121 "endif")
7122 "Keywords to complete when at first word of a line in declarative scope.
7123 \(initial, always, begin, assign...)
7124 The procedures and variables defined within the Verilog program
7125 will be completed at runtime and should not be added to this list.")
7126
7127 (defvar verilog-defun-keywords
7128 (append
7129 '(
7130 "always" "always_comb" "always_ff" "always_latch" "assign"
7131 "begin" "end" "generate" "endgenerate" "module" "endmodule"
7132 "specify" "endspecify" "function" "endfunction" "initial" "final"
7133 "task" "endtask" "primitive" "endprimitive"
7134 )
7135 verilog-type-keywords)
7136 "Keywords to complete when at first word of a line in declarative scope.
7137 \(initial, always, begin, assign...)
7138 The procedures and variables defined within the Verilog program
7139 will be completed at runtime and should not be added to this list.")
7140
7141 (defvar verilog-block-keywords
7142 '(
7143 "begin" "break" "case" "continue" "else" "end" "endfunction"
7144 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
7145 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
7146 "while")
7147 "Keywords to complete when at first word of a line in behavioral scope.
7148 \(begin, if, then, else, for, fork...)
7149 The procedures and variables defined within the Verilog program
7150 will be completed at runtime and should not be added to this list.")
7151
7152 (defvar verilog-tf-keywords
7153 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
7154 "Keywords to complete when at first word of a line in a task or function.
7155 \(begin, if, then, else, for, fork.)
7156 The procedures and variables defined within the Verilog program
7157 will be completed at runtime and should not be added to this list.")
7158
7159 (defvar verilog-case-keywords
7160 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
7161 "Keywords to complete when at first word of a line in case scope.
7162 \(begin, if, then, else, for, fork...)
7163 The procedures and variables defined within the Verilog program
7164 will be completed at runtime and should not be added to this list.")
7165
7166 (defvar verilog-separator-keywords
7167 '("else" "then" "begin")
7168 "Keywords to complete when NOT standing at the first word of a statement.
7169 \(else, then, begin...)
7170 Variables and function names defined within the Verilog program
7171 will be completed at runtime and should not be added to this list.")
7172
7173 (defvar verilog-gate-ios
7174 ;; All these have an implied {"input"...} at the end
7175 '(("and" "output")
7176 ("buf" "output")
7177 ("bufif0" "output")
7178 ("bufif1" "output")
7179 ("cmos" "output")
7180 ("nand" "output")
7181 ("nmos" "output")
7182 ("nor" "output")
7183 ("not" "output")
7184 ("notif0" "output")
7185 ("notif1" "output")
7186 ("or" "output")
7187 ("pmos" "output")
7188 ("pulldown" "output")
7189 ("pullup" "output")
7190 ("rcmos" "output")
7191 ("rnmos" "output")
7192 ("rpmos" "output")
7193 ("rtran" "inout" "inout")
7194 ("rtranif0" "inout" "inout")
7195 ("rtranif1" "inout" "inout")
7196 ("tran" "inout" "inout")
7197 ("tranif0" "inout" "inout")
7198 ("tranif1" "inout" "inout")
7199 ("xnor" "output")
7200 ("xor" "output"))
7201 "Map of direction for each positional argument to each gate primitive.")
7202
7203 (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
7204 "Keywords for gate primitives.")
7205
7206 (defun verilog-string-diff (str1 str2)
7207 "Return index of first letter where STR1 and STR2 differs."
7208 (catch 'done
7209 (let ((diff 0))
7210 (while t
7211 (if (or (> (1+ diff) (length str1))
7212 (> (1+ diff) (length str2)))
7213 (throw 'done diff))
7214 (or (equal (aref str1 diff) (aref str2 diff))
7215 (throw 'done diff))
7216 (setq diff (1+ diff))))))
7217
7218 ;; Calculate all possible completions for functions if argument is `function',
7219 ;; completions for procedures if argument is `procedure' or both functions and
7220 ;; procedures otherwise.
7221
7222 (defun verilog-func-completion (type)
7223 "Build regular expression for module/task/function names.
7224 TYPE is `module', `tf' for task or function, or t if unknown."
7225 (if (string= verilog-str "")
7226 (setq verilog-str "[a-zA-Z_]"))
7227 (let ((verilog-str (concat (cond
7228 ((eq type 'module) "\\<\\(module\\)\\s +")
7229 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
7230 (t "\\<\\(task\\|function\\|module\\)\\s +"))
7231 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
7232 match)
7233
7234 (if (not (looking-at verilog-defun-re))
7235 (verilog-re-search-backward verilog-defun-re nil t))
7236 (forward-char 1)
7237
7238 ;; Search through all reachable functions
7239 (goto-char (point-min))
7240 (while (verilog-re-search-forward verilog-str (point-max) t)
7241 (progn (setq match (buffer-substring (match-beginning 2)
7242 (match-end 2)))
7243 (if (or (null verilog-pred)
7244 (funcall verilog-pred match))
7245 (setq verilog-all (cons match verilog-all)))))
7246 (if (match-beginning 0)
7247 (goto-char (match-beginning 0)))))
7248
7249 (defun verilog-get-completion-decl (end)
7250 "Macro for searching through current declaration (var, type or const)
7251 for matches of `str' and adding the occurrence tp `all' through point END."
7252 (let ((re (or (and verilog-indent-declaration-macros
7253 verilog-declaration-re-2-macro)
7254 verilog-declaration-re-2-no-macro))
7255 decl-end match)
7256 ;; Traverse lines
7257 (while (and (< (point) end)
7258 (verilog-re-search-forward re end t))
7259 ;; Traverse current line
7260 (setq decl-end (save-excursion (verilog-declaration-end)))
7261 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
7262 (not (match-end 1)))
7263 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
7264 (if (string-match (concat "\\<" verilog-str) match)
7265 (if (or (null verilog-pred)
7266 (funcall verilog-pred match))
7267 (setq verilog-all (cons match verilog-all)))))
7268 (forward-line 1)))
7269 verilog-all)
7270
7271 (defun verilog-var-completion ()
7272 "Calculate all possible completions for variables (or constants)."
7273 (let ((start (point)))
7274 ;; Search for all reachable var declarations
7275 (verilog-beg-of-defun)
7276 (save-excursion
7277 ;; Check var declarations
7278 (verilog-get-completion-decl start))))
7279
7280 (defun verilog-keyword-completion (keyword-list)
7281 "Give list of all possible completions of keywords in KEYWORD-LIST."
7282 (mapcar (lambda (s)
7283 (if (string-match (concat "\\<" verilog-str) s)
7284 (if (or (null verilog-pred)
7285 (funcall verilog-pred s))
7286 (setq verilog-all (cons s verilog-all)))))
7287 keyword-list))
7288
7289
7290 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
7291 "Function passed to `completing-read', `try-completion' or `all-completions'.
7292 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
7293 must be a function to be called for every match to check if this should
7294 really be a match. If VERILOG-FLAG is t, the function returns a list of
7295 all possible completions. If VERILOG-FLAG is nil it returns a string,
7296 the longest possible completion, or t if VERILOG-STR is an exact match.
7297 If VERILOG-FLAG is `lambda', the function returns t if VERILOG-STR is an
7298 exact match, nil otherwise."
7299 (save-excursion
7300 (let ((verilog-all nil))
7301 ;; Set buffer to use for searching labels. This should be set
7302 ;; within functions which use verilog-completions
7303 (set-buffer verilog-buffer-to-use)
7304
7305 ;; Determine what should be completed
7306 (let ((state (car (verilog-calculate-indent))))
7307 (cond ((eq state 'defun)
7308 (save-excursion (verilog-var-completion))
7309 (verilog-func-completion 'module)
7310 (verilog-keyword-completion verilog-defun-keywords))
7311
7312 ((eq state 'behavioral)
7313 (save-excursion (verilog-var-completion))
7314 (verilog-func-completion 'module)
7315 (verilog-keyword-completion verilog-defun-keywords))
7316
7317 ((eq state 'block)
7318 (save-excursion (verilog-var-completion))
7319 (verilog-func-completion 'tf)
7320 (verilog-keyword-completion verilog-block-keywords))
7321
7322 ((eq state 'case)
7323 (save-excursion (verilog-var-completion))
7324 (verilog-func-completion 'tf)
7325 (verilog-keyword-completion verilog-case-keywords))
7326
7327 ((eq state 'tf)
7328 (save-excursion (verilog-var-completion))
7329 (verilog-func-completion 'tf)
7330 (verilog-keyword-completion verilog-tf-keywords))
7331
7332 ((eq state 'cpp)
7333 (save-excursion (verilog-var-completion))
7334 (verilog-keyword-completion verilog-cpp-keywords))
7335
7336 ((eq state 'cparenexp)
7337 (save-excursion (verilog-var-completion)))
7338
7339 (t;--Anywhere else
7340 (save-excursion (verilog-var-completion))
7341 (verilog-func-completion 'both)
7342 (verilog-keyword-completion verilog-separator-keywords))))
7343
7344 ;; Now we have built a list of all matches. Give response to caller
7345 (verilog-completion-response))))
7346
7347 (defun verilog-completion-response ()
7348 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
7349 ;; This was not called by all-completions
7350 (if (null verilog-all)
7351 ;; Return nil if there was no matching label
7352 nil
7353 ;; Get longest string common in the labels
7354 ;; FIXME: Why not use `try-completion'?
7355 (let* ((elm (cdr verilog-all))
7356 (match (car verilog-all))
7357 (min (length match))
7358 tmp)
7359 (if (string= match verilog-str)
7360 ;; Return t if first match was an exact match
7361 (setq match t)
7362 (while (not (null elm))
7363 ;; Find longest common string
7364 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
7365 (progn
7366 (setq min tmp)
7367 (setq match (substring match 0 min))))
7368 ;; Terminate with match=t if this is an exact match
7369 (if (string= (car elm) verilog-str)
7370 (progn
7371 (setq match t)
7372 (setq elm nil))
7373 (setq elm (cdr elm)))))
7374 ;; If this is a test just for exact match, return nil ot t
7375 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
7376 nil
7377 match))))
7378 ;; If flag is t, this was called by all-completions. Return
7379 ;; list of all possible completions
7380 (verilog-flag
7381 verilog-all)))
7382
7383 (defvar verilog-last-word-numb 0)
7384 (defvar verilog-last-word-shown nil)
7385 (defvar verilog-last-completions nil)
7386
7387 (defun verilog-complete-word ()
7388 "Complete word at current point.
7389 \(See also `verilog-toggle-completions', `verilog-type-keywords',
7390 and `verilog-separator-keywords'.)"
7391 ;; FIXME: Provide completion-at-point-function.
7392 (interactive)
7393 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7394 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7395 (verilog-str (buffer-substring b e))
7396 ;; The following variable is used in verilog-completion
7397 (verilog-buffer-to-use (current-buffer))
7398 (allcomp (if (and verilog-toggle-completions
7399 (string= verilog-last-word-shown verilog-str))
7400 verilog-last-completions
7401 (all-completions verilog-str 'verilog-completion)))
7402 (match (if verilog-toggle-completions
7403 "" (try-completion
7404 verilog-str (mapcar (lambda (elm)
7405 (cons elm 0)) allcomp)))))
7406 ;; Delete old string
7407 (delete-region b e)
7408
7409 ;; Toggle-completions inserts whole labels
7410 (if verilog-toggle-completions
7411 (progn
7412 ;; Update entry number in list
7413 (setq verilog-last-completions allcomp
7414 verilog-last-word-numb
7415 (if (>= verilog-last-word-numb (1- (length allcomp)))
7416 0
7417 (1+ verilog-last-word-numb)))
7418 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7419 ;; Display next match or same string if no match was found
7420 (if (not (null allcomp))
7421 (insert "" verilog-last-word-shown)
7422 (insert "" verilog-str)
7423 (message "(No match)")))
7424 ;; The other form of completion does not necessarily do that.
7425
7426 ;; Insert match if found, or the original string if no match
7427 (if (or (null match) (equal match 't))
7428 (progn (insert "" verilog-str)
7429 (message "(No match)"))
7430 (insert "" match))
7431 ;; Give message about current status of completion
7432 (cond ((equal match 't)
7433 (if (not (null (cdr allcomp)))
7434 (message "(Complete but not unique)")
7435 (message "(Sole completion)")))
7436 ;; Display buffer if the current completion didn't help
7437 ;; on completing the label.
7438 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7439 (length match)))
7440 (with-output-to-temp-buffer "*Completions*"
7441 (display-completion-list allcomp))
7442 ;; Wait for a key press. Then delete *Completion* window
7443 (momentary-string-display "" (point))
7444 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7445 )))))
7446
7447 (defun verilog-show-completions ()
7448 "Show all possible completions at current point."
7449 (interactive)
7450 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7451 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7452 (verilog-str (buffer-substring b e))
7453 ;; The following variable is used in verilog-completion
7454 (verilog-buffer-to-use (current-buffer))
7455 (allcomp (if (and verilog-toggle-completions
7456 (string= verilog-last-word-shown verilog-str))
7457 verilog-last-completions
7458 (all-completions verilog-str 'verilog-completion))))
7459 ;; Show possible completions in a temporary buffer.
7460 (with-output-to-temp-buffer "*Completions*"
7461 (display-completion-list allcomp))
7462 ;; Wait for a key press. Then delete *Completion* window
7463 (momentary-string-display "" (point))
7464 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7465
7466
7467 (defun verilog-get-default-symbol ()
7468 "Return symbol around current point as a string."
7469 (save-excursion
7470 (buffer-substring (progn
7471 (skip-chars-backward " \t")
7472 (skip-chars-backward "a-zA-Z0-9_")
7473 (point))
7474 (progn
7475 (skip-chars-forward "a-zA-Z0-9_")
7476 (point)))))
7477
7478 (defun verilog-build-defun-re (str &optional arg)
7479 "Return function/task/module starting with STR as regular expression.
7480 With optional second ARG non-nil, STR is the complete name of the instruction."
7481 (if arg
7482 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7483 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7484
7485 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7486 "Function passed to `completing-read', `try-completion' or `all-completions'.
7487 Returns a completion on any function name based on VERILOG-STR prefix. If
7488 VERILOG-PRED is non-nil, it must be a function to be called for every match
7489 to check if this should really be a match. If VERILOG-FLAG is t, the
7490 function returns a list of all possible completions. If it is nil it
7491 returns a string, the longest possible completion, or t if VERILOG-STR is
7492 an exact match. If VERILOG-FLAG is `lambda', the function returns t if
7493 VERILOG-STR is an exact match, nil otherwise."
7494 (save-excursion
7495 (let ((verilog-all nil)
7496 match)
7497
7498 ;; Set buffer to use for searching labels. This should be set
7499 ;; within functions which use verilog-completions
7500 (set-buffer verilog-buffer-to-use)
7501
7502 (let ((verilog-str verilog-str))
7503 ;; Build regular expression for functions
7504 (if (string= verilog-str "")
7505 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7506 (setq verilog-str (verilog-build-defun-re verilog-str)))
7507 (goto-char (point-min))
7508
7509 ;; Build a list of all possible completions
7510 (while (verilog-re-search-forward verilog-str nil t)
7511 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7512 (if (or (null verilog-pred)
7513 (funcall verilog-pred match))
7514 (setq verilog-all (cons match verilog-all)))))
7515
7516 ;; Now we have built a list of all matches. Give response to caller
7517 (verilog-completion-response))))
7518
7519 (defun verilog-goto-defun ()
7520 "Move to specified Verilog module/interface/task/function.
7521 The default is a name found in the buffer around point.
7522 If search fails, other files are checked based on
7523 `verilog-library-flags'."
7524 (interactive)
7525 (let* ((default (verilog-get-default-symbol))
7526 ;; The following variable is used in verilog-comp-function
7527 (verilog-buffer-to-use (current-buffer))
7528 (label (if (not (string= default ""))
7529 ;; Do completion with default
7530 (completing-read (concat "Goto-Label: (default "
7531 default ") ")
7532 'verilog-comp-defun nil nil "")
7533 ;; There is no default value. Complete without it
7534 (completing-read "Goto-Label: "
7535 'verilog-comp-defun nil nil "")))
7536 pt)
7537 ;; Make sure library paths are correct, in case need to resolve module
7538 (verilog-auto-reeval-locals)
7539 (verilog-getopt-flags)
7540 ;; If there was no response on prompt, use default value
7541 (if (string= label "")
7542 (setq label default))
7543 ;; Goto right place in buffer if label is not an empty string
7544 (or (string= label "")
7545 (progn
7546 (save-excursion
7547 (goto-char (point-min))
7548 (setq pt
7549 (re-search-forward (verilog-build-defun-re label t) nil t)))
7550 (when pt
7551 (goto-char pt)
7552 (beginning-of-line))
7553 pt)
7554 (verilog-goto-defun-file label))))
7555
7556 ;; Eliminate compile warning
7557 (defvar occur-pos-list)
7558
7559 (defun verilog-showscopes ()
7560 "List all scopes in this module."
7561 (interactive)
7562 (let ((buffer (current-buffer))
7563 (linenum 1)
7564 (nlines 0)
7565 (first 1)
7566 (prevpos (point-min))
7567 (final-context-start (make-marker))
7568 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
7569 (with-output-to-temp-buffer "*Occur*"
7570 (save-excursion
7571 (message "Searching for %s ..." regexp)
7572 ;; Find next match, but give up if prev match was at end of buffer.
7573 (while (and (not (= prevpos (point-max)))
7574 (verilog-re-search-forward regexp nil t))
7575 (goto-char (match-beginning 0))
7576 (beginning-of-line)
7577 (save-match-data
7578 (setq linenum (+ linenum (count-lines prevpos (point)))))
7579 (setq prevpos (point))
7580 (goto-char (match-end 0))
7581 (let* ((start (save-excursion
7582 (goto-char (match-beginning 0))
7583 (forward-line (if (< nlines 0) nlines (- nlines)))
7584 (point)))
7585 (end (save-excursion
7586 (goto-char (match-end 0))
7587 (if (> nlines 0)
7588 (forward-line (1+ nlines))
7589 (forward-line 1))
7590 (point)))
7591 (tag (format "%3d" linenum))
7592 (empty (make-string (length tag) ?\ ))
7593 tem)
7594 (save-excursion
7595 (setq tem (make-marker))
7596 (set-marker tem (point))
7597 (set-buffer standard-output)
7598 (setq occur-pos-list (cons tem occur-pos-list))
7599 (or first (zerop nlines)
7600 (insert "--------\n"))
7601 (setq first nil)
7602 (insert-buffer-substring buffer start end)
7603 (backward-char (- end start))
7604 (setq tem (if (< nlines 0) (- nlines) nlines))
7605 (while (> tem 0)
7606 (insert empty ?:)
7607 (forward-line 1)
7608 (setq tem (1- tem)))
7609 (let ((this-linenum linenum))
7610 (set-marker final-context-start
7611 (+ (point) (- (match-end 0) (match-beginning 0))))
7612 (while (< (point) final-context-start)
7613 (if (null tag)
7614 (setq tag (format "%3d" this-linenum)))
7615 (insert tag ?:)))))))
7616 (set-buffer-modified-p nil))))
7617
7618
7619 ;; Highlight helper functions
7620 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7621
7622 (defun verilog-within-translate-off ()
7623 "Return point if within translate-off region, else nil."
7624 (and (save-excursion
7625 (re-search-backward
7626 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7627 nil t))
7628 (equal "off" (match-string 2))
7629 (point)))
7630
7631 (defun verilog-start-translate-off (limit)
7632 "Return point before translate-off directive if before LIMIT, else nil."
7633 (when (re-search-forward
7634 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7635 limit t)
7636 (match-beginning 0)))
7637
7638 (defun verilog-back-to-start-translate-off (limit)
7639 "Return point before translate-off directive if before LIMIT, else nil."
7640 (when (re-search-backward
7641 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7642 limit t)
7643 (match-beginning 0)))
7644
7645 (defun verilog-end-translate-off (limit)
7646 "Return point after translate-on directive if before LIMIT, else nil."
7647
7648 (re-search-forward (concat
7649 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7650
7651 (defun verilog-match-translate-off (limit)
7652 "Match a translate-off block, setting `match-data' and returning t, else nil.
7653 Bound search by LIMIT."
7654 (when (< (point) limit)
7655 (let ((start (or (verilog-within-translate-off)
7656 (verilog-start-translate-off limit)))
7657 (case-fold-search t))
7658 (when start
7659 (let ((end (or (verilog-end-translate-off limit) limit)))
7660 (set-match-data (list start end))
7661 (goto-char end))))))
7662
7663 (defun verilog-font-lock-match-item (limit)
7664 "Match, and move over, any declaration item after point.
7665 Bound search by LIMIT. Adapted from
7666 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
7667 (condition-case nil
7668 (save-restriction
7669 (narrow-to-region (point-min) limit)
7670 ;; match item
7671 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7672 (save-match-data
7673 (goto-char (match-end 1))
7674 ;; move to next item
7675 (if (looking-at "\\(\\s-*,\\)")
7676 (goto-char (match-end 1))
7677 (end-of-line) t))))
7678 (error nil)))
7679
7680
7681 ;; Added by Subbu Meiyappan for Header
7682
7683 (defun verilog-header ()
7684 "Insert a standard Verilog file header.
7685 See also `verilog-sk-header' for an alternative format."
7686 (interactive)
7687 (let ((start (point)))
7688 (insert "\
7689 //-----------------------------------------------------------------------------
7690 // Title : <title>
7691 // Project : <project>
7692 //-----------------------------------------------------------------------------
7693 // File : <filename>
7694 // Author : <author>
7695 // Created : <credate>
7696 // Last modified : <moddate>
7697 //-----------------------------------------------------------------------------
7698 // Description :
7699 // <description>
7700 //-----------------------------------------------------------------------------
7701 // Copyright (c) <copydate> by <company> This model is the confidential and
7702 // proprietary property of <company> and the possession or use of this
7703 // file requires a written license from <company>.
7704 //------------------------------------------------------------------------------
7705 // Modification history :
7706 // <modhist>
7707 //-----------------------------------------------------------------------------
7708
7709 ")
7710 (goto-char start)
7711 (search-forward "<filename>")
7712 (replace-match (buffer-name) t t)
7713 (search-forward "<author>") (replace-match "" t t)
7714 (insert (user-full-name))
7715 (insert " <" (user-login-name) "@" (system-name) ">")
7716 (search-forward "<credate>") (replace-match "" t t)
7717 (verilog-insert-date)
7718 (search-forward "<moddate>") (replace-match "" t t)
7719 (verilog-insert-date)
7720 (search-forward "<copydate>") (replace-match "" t t)
7721 (verilog-insert-year)
7722 (search-forward "<modhist>") (replace-match "" t t)
7723 (verilog-insert-date)
7724 (insert " : created")
7725 (goto-char start)
7726 (let (string)
7727 (setq string (read-string "title: "))
7728 (search-forward "<title>")
7729 (replace-match string t t)
7730 (setq string (read-string "project: " verilog-project))
7731 (setq verilog-project string)
7732 (search-forward "<project>")
7733 (replace-match string t t)
7734 (setq string (read-string "Company: " verilog-company))
7735 (setq verilog-company string)
7736 (search-forward "<company>")
7737 (replace-match string t t)
7738 (search-forward "<company>")
7739 (replace-match string t t)
7740 (search-forward "<company>")
7741 (replace-match string t t)
7742 (search-backward "<description>")
7743 (replace-match "" t t))))
7744
7745 ;; verilog-header Uses the verilog-insert-date function
7746
7747 (defun verilog-insert-date ()
7748 "Insert date from the system."
7749 (interactive)
7750 (if verilog-date-scientific-format
7751 (insert (format-time-string "%Y/%m/%d"))
7752 (insert (format-time-string "%d.%m.%Y"))))
7753
7754 (defun verilog-insert-year ()
7755 "Insert year from the system."
7756 (interactive)
7757 (insert (format-time-string "%Y")))
7758
7759 \f
7760 ;;; Signal list parsing:
7761 ;;
7762
7763 ;; Elements of a signal list
7764 ;; Unfortunately we use 'assoc' on this, so can't be a vector
7765 (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7766 (list name bits comment mem enum signed type multidim modport))
7767 (defsubst verilog-sig-name (sig)
7768 (car sig))
7769 (defsubst verilog-sig-bits (sig) ; First element of packed array (pre signal-name)
7770 (nth 1 sig))
7771 (defsubst verilog-sig-comment (sig)
7772 (nth 2 sig))
7773 (defsubst verilog-sig-memory (sig) ; Unpacked array (post signal-name)
7774 (nth 3 sig))
7775 (defsubst verilog-sig-enum (sig)
7776 (nth 4 sig))
7777 (defsubst verilog-sig-signed (sig)
7778 (nth 5 sig))
7779 (defsubst verilog-sig-type (sig)
7780 (nth 6 sig))
7781 (defsubst verilog-sig-type-set (sig type)
7782 (setcar (nthcdr 6 sig) type))
7783 (defsubst verilog-sig-multidim (sig) ; Second and additional elements of packed array
7784 (nth 7 sig))
7785 (defsubst verilog-sig-multidim-string (sig)
7786 (if (verilog-sig-multidim sig)
7787 (let ((str "") (args (verilog-sig-multidim sig)))
7788 (while args
7789 (setq str (concat str (car args)))
7790 (setq args (cdr args)))
7791 str)))
7792 (defsubst verilog-sig-modport (sig)
7793 (nth 8 sig))
7794 (defsubst verilog-sig-width (sig)
7795 (verilog-make-width-expression (verilog-sig-bits sig)))
7796
7797 (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7798 (vector outputs-del outputs-imm temps inputs))
7799 (defsubst verilog-alw-get-outputs-delayed (sigs)
7800 (aref sigs 0))
7801 (defsubst verilog-alw-get-outputs-immediate (sigs)
7802 (aref sigs 1))
7803 (defsubst verilog-alw-get-temps (sigs)
7804 (aref sigs 2))
7805 (defsubst verilog-alw-get-inputs (sigs)
7806 (aref sigs 3))
7807 (defsubst verilog-alw-get-uses-delayed (sigs)
7808 (aref sigs 0))
7809
7810 (defsubst verilog-modport-new (name clockings decls)
7811 (list name clockings decls))
7812 (defsubst verilog-modport-name (sig)
7813 (car sig))
7814 (defsubst verilog-modport-clockings (sig)
7815 (nth 1 sig)) ; Returns list of names
7816 (defsubst verilog-modport-clockings-add (sig val)
7817 (setcar (nthcdr 1 sig) (cons val (nth 1 sig))))
7818 (defsubst verilog-modport-decls (sig)
7819 (nth 2 sig)) ; Returns verilog-decls-* structure
7820 (defsubst verilog-modport-decls-set (sig val)
7821 (setcar (nthcdr 2 sig) val))
7822
7823 (defsubst verilog-modi-new (name fob pt type)
7824 (vector name fob pt type))
7825 (defsubst verilog-modi-name (modi)
7826 (aref modi 0))
7827 (defsubst verilog-modi-file-or-buffer (modi)
7828 (aref modi 1))
7829 (defsubst verilog-modi-get-point (modi)
7830 (aref modi 2))
7831 (defsubst verilog-modi-get-type (modi) ; "module" or "interface"
7832 (aref modi 3))
7833 (defsubst verilog-modi-get-decls (modi)
7834 (verilog-modi-cache-results modi 'verilog-read-decls))
7835 (defsubst verilog-modi-get-sub-decls (modi)
7836 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7837
7838 ;; Signal reading for given module
7839 ;; Note these all take modi's - as returned from verilog-modi-current
7840 (defsubst verilog-decls-new (out inout in vars modports assigns consts gparams interfaces)
7841 (vector out inout in vars modports assigns consts gparams interfaces))
7842 (defsubst verilog-decls-append (a b)
7843 (cond ((not a) b) ((not b) a)
7844 (t (vector (append (aref a 0) (aref b 0)) (append (aref a 1) (aref b 1))
7845 (append (aref a 2) (aref b 2)) (append (aref a 3) (aref b 3))
7846 (append (aref a 4) (aref b 4)) (append (aref a 5) (aref b 5))
7847 (append (aref a 6) (aref b 6)) (append (aref a 7) (aref b 7))
7848 (append (aref a 8) (aref b 8))))))
7849 (defsubst verilog-decls-get-outputs (decls)
7850 (aref decls 0))
7851 (defsubst verilog-decls-get-inouts (decls)
7852 (aref decls 1))
7853 (defsubst verilog-decls-get-inputs (decls)
7854 (aref decls 2))
7855 (defsubst verilog-decls-get-vars (decls)
7856 (aref decls 3))
7857 (defsubst verilog-decls-get-modports (decls) ; Also for clocking blocks; contains another verilog-decls struct
7858 (aref decls 4)) ; Returns verilog-modport* structure
7859 (defsubst verilog-decls-get-assigns (decls)
7860 (aref decls 5))
7861 (defsubst verilog-decls-get-consts (decls)
7862 (aref decls 6))
7863 (defsubst verilog-decls-get-gparams (decls)
7864 (aref decls 7))
7865 (defsubst verilog-decls-get-interfaces (decls)
7866 (aref decls 8))
7867
7868
7869 (defsubst verilog-subdecls-new (out inout in intf intfd)
7870 (vector out inout in intf intfd))
7871 (defsubst verilog-subdecls-get-outputs (subdecls)
7872 (aref subdecls 0))
7873 (defsubst verilog-subdecls-get-inouts (subdecls)
7874 (aref subdecls 1))
7875 (defsubst verilog-subdecls-get-inputs (subdecls)
7876 (aref subdecls 2))
7877 (defsubst verilog-subdecls-get-interfaces (subdecls)
7878 (aref subdecls 3))
7879 (defsubst verilog-subdecls-get-interfaced (subdecls)
7880 (aref subdecls 4))
7881
7882 (defun verilog-signals-from-signame (signame-list)
7883 "Return signals in standard form from SIGNAME-LIST, a simple list of names."
7884 (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
7885 signame-list))
7886
7887 (defun verilog-signals-in (in-list not-list)
7888 "Return list of signals in IN-LIST that are also in NOT-LIST.
7889 Also remove any duplicates in IN-LIST.
7890 Signals must be in standard (base vector) form."
7891 ;; This function is hot, so implemented as O(1)
7892 (cond ((eval-when-compile (fboundp 'make-hash-table))
7893 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7894 (ht-not (make-hash-table :test 'equal :rehash-size 4.0))
7895 out-list)
7896 (while not-list
7897 (puthash (car (car not-list)) t ht-not)
7898 (setq not-list (cdr not-list)))
7899 (while in-list
7900 (when (and (gethash (verilog-sig-name (car in-list)) ht-not)
7901 (not (gethash (verilog-sig-name (car in-list)) ht)))
7902 (setq out-list (cons (car in-list) out-list))
7903 (puthash (verilog-sig-name (car in-list)) t ht))
7904 (setq in-list (cdr in-list)))
7905 (nreverse out-list)))
7906 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7907 (t
7908 (let (out-list)
7909 (while in-list
7910 (if (and (assoc (verilog-sig-name (car in-list)) not-list)
7911 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7912 (setq out-list (cons (car in-list) out-list)))
7913 (setq in-list (cdr in-list)))
7914 (nreverse out-list)))))
7915 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("C" "")))
7916
7917 (defun verilog-signals-not-in (in-list not-list)
7918 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7919 Also remove any duplicates in IN-LIST.
7920 Signals must be in standard (base vector) form."
7921 ;; This function is hot, so implemented as O(1)
7922 (cond ((eval-when-compile (fboundp 'make-hash-table))
7923 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7924 out-list)
7925 (while not-list
7926 (puthash (car (car not-list)) t ht)
7927 (setq not-list (cdr not-list)))
7928 (while in-list
7929 (when (not (gethash (verilog-sig-name (car in-list)) ht))
7930 (setq out-list (cons (car in-list) out-list))
7931 (puthash (verilog-sig-name (car in-list)) t ht))
7932 (setq in-list (cdr in-list)))
7933 (nreverse out-list)))
7934 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7935 (t
7936 (let (out-list)
7937 (while in-list
7938 (if (and (not (assoc (verilog-sig-name (car in-list)) not-list))
7939 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7940 (setq out-list (cons (car in-list) out-list)))
7941 (setq in-list (cdr in-list)))
7942 (nreverse out-list)))))
7943 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7944
7945 (defun verilog-signals-not-in-struct (in-list not-list)
7946 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7947 Also remove any duplicates in IN-LIST.
7948 Any structure in not-list will remove all members in in-list.
7949 Signals must be in standard (base vector) form."
7950 (cond ((eval-when-compile (fboundp 'make-hash-table))
7951 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7952 out-list addit nm)
7953 (while not-list
7954 (puthash (car (car not-list)) t ht)
7955 (setq not-list (cdr not-list)))
7956 (while in-list
7957 (setq nm (verilog-sig-name (car in-list)))
7958 (when (not (gethash nm ht))
7959 (setq addit t)
7960 (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
7961 (setq nm (match-string 1 nm))
7962 (setq addit (and addit
7963 (not (gethash nm ht)))))
7964 (when addit
7965 (setq out-list (cons (car in-list) out-list))
7966 (puthash (verilog-sig-name (car in-list)) t ht)))
7967 (setq in-list (cdr in-list)))
7968 (nreverse out-list)))
7969 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7970 (t
7971 (let (out-list addit nm)
7972 (while in-list
7973 (setq nm (verilog-sig-name (car in-list)))
7974 (when (and (not (assoc nm not-list))
7975 (not (assoc nm out-list)))
7976 (setq addit t)
7977 (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
7978 (setq nm (match-string 1 nm))
7979 (setq addit (and addit
7980 (not (assoc nm not-list)))))
7981 (when addit
7982 (setq out-list (cons (car in-list) out-list))))
7983 (setq in-list (cdr in-list)))
7984 (nreverse out-list)))))
7985 ;;(verilog-signals-not-in-struct '(("A" "") ("B" "") ("DEL.SUB.A" "[2:3]")) '(("DEL.SUB" "") ("EXT" "")))
7986
7987 (defun verilog-signals-memory (in-list)
7988 "Return list of signals in IN-LIST that are memorized (multidimensional)."
7989 (let (out-list)
7990 (while in-list
7991 (if (nth 3 (car in-list))
7992 (setq out-list (cons (car in-list) out-list)))
7993 (setq in-list (cdr in-list)))
7994 out-list))
7995 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
7996
7997 (defun verilog-signals-sort-compare (a b)
7998 "Compare signal A and B for sorting."
7999 (string< (verilog-sig-name a) (verilog-sig-name b)))
8000
8001 (defun verilog-signals-not-params (in-list)
8002 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
8003 (let (out-list)
8004 (while in-list
8005 ;; Namespace intentionally short for AUTOs and compatibility
8006 (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
8007 (setq out-list (cons (car in-list) out-list)))
8008 (setq in-list (cdr in-list)))
8009 (nreverse out-list)))
8010
8011 (defun verilog-signals-with (func in-list)
8012 "Return list of signals where FUNC is true executed on each signal in IN-LIST."
8013 (let (out-list)
8014 (while in-list
8015 (when (funcall func (car in-list))
8016 (setq out-list (cons (car in-list) out-list)))
8017 (setq in-list (cdr in-list)))
8018 (nreverse out-list)))
8019
8020 (defun verilog-signals-combine-bus (in-list)
8021 "Return a list of signals in IN-LIST, with buses combined.
8022 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
8023 (let (combo
8024 buswarn
8025 out-list
8026 sig highbit lowbit ; Temp information about current signal
8027 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
8028 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
8029 sv-modport
8030 bus)
8031 ;; Shove signals so duplicated signals will be adjacent
8032 (setq in-list (sort in-list `verilog-signals-sort-compare))
8033 (while in-list
8034 (setq sig (car in-list))
8035 ;; No current signal; form from existing details
8036 (unless sv-name
8037 (setq sv-name (verilog-sig-name sig)
8038 sv-highbit nil
8039 sv-busstring nil
8040 sv-comment (verilog-sig-comment sig)
8041 sv-memory (verilog-sig-memory sig)
8042 sv-enum (verilog-sig-enum sig)
8043 sv-signed (verilog-sig-signed sig)
8044 sv-type (verilog-sig-type sig)
8045 sv-multidim (verilog-sig-multidim sig)
8046 sv-modport (verilog-sig-modport sig)
8047 combo ""
8048 buswarn ""))
8049 ;; Extract bus details
8050 (setq bus (verilog-sig-bits sig))
8051 (setq bus (and bus (verilog-simplify-range-expression bus)))
8052 (cond ((and bus
8053 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
8054 (setq highbit (string-to-number (match-string 1 bus))
8055 lowbit (string-to-number
8056 (match-string 2 bus))))
8057 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
8058 (setq highbit (string-to-number (match-string 1 bus))
8059 lowbit highbit))))
8060 ;; Combine bits in bus
8061 (if sv-highbit
8062 (setq sv-highbit (max highbit sv-highbit)
8063 sv-lowbit (min lowbit sv-lowbit))
8064 (setq sv-highbit highbit
8065 sv-lowbit lowbit)))
8066 (bus
8067 ;; String, probably something like `preproc:0
8068 (setq sv-busstring bus)))
8069 ;; Peek ahead to next signal
8070 (setq in-list (cdr in-list))
8071 (setq sig (car in-list))
8072 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
8073 ;; Combine with this signal
8074 (when (and sv-busstring
8075 (not (equal sv-busstring (verilog-sig-bits sig))))
8076 (when nil ; Debugging
8077 (message (concat "Warning, can't merge into single bus %s%s"
8078 ", the AUTOs may be wrong")
8079 sv-name bus))
8080 (setq buswarn ", Couldn't Merge"))
8081 (if (verilog-sig-comment sig) (setq combo ", ..."))
8082 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
8083 sv-enum (or sv-enum (verilog-sig-enum sig))
8084 sv-signed (or sv-signed (verilog-sig-signed sig))
8085 sv-type (or sv-type (verilog-sig-type sig))
8086 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
8087 sv-modport (or sv-modport (verilog-sig-modport sig))))
8088 ;; Doesn't match next signal, add to queue, zero in prep for next
8089 ;; Note sig may also be nil for the last signal in the list
8090 (t
8091 (setq out-list
8092 (cons (verilog-sig-new
8093 sv-name
8094 (or sv-busstring
8095 (if sv-highbit
8096 (concat "[" (int-to-string sv-highbit) ":"
8097 (int-to-string sv-lowbit) "]")))
8098 (concat sv-comment combo buswarn)
8099 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
8100 out-list)
8101 sv-name nil))))
8102 ;;
8103 out-list))
8104
8105 (defun verilog-sig-tieoff (sig)
8106 "Return tieoff expression for given SIG, with appropriate width.
8107 Tieoff value uses `verilog-active-low-regexp' and
8108 `verilog-auto-reset-widths'."
8109 (concat
8110 (if (and verilog-active-low-regexp
8111 (verilog-string-match-fold verilog-active-low-regexp (verilog-sig-name sig)))
8112 "~" "")
8113 (cond ((not verilog-auto-reset-widths)
8114 "0")
8115 ((equal verilog-auto-reset-widths 'unbased)
8116 "'0")
8117 ;; Else presume verilog-auto-reset-widths is true
8118 (t
8119 (let* ((width (verilog-sig-width sig)))
8120 (cond ((not width)
8121 "`0/*NOWIDTH*/")
8122 ((string-match "^[0-9]+$" width)
8123 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
8124 (t
8125 (concat "{" width "{1'b0}}"))))))))
8126
8127 ;;
8128 ;; Dumping
8129 ;;
8130
8131 (defun verilog-decls-princ (decls &optional header prefix)
8132 "For debug, dump the `verilog-read-decls' structure DECLS.
8133 Use optional HEADER and PREFIX."
8134 (when decls
8135 (if header (princ header))
8136 (setq prefix (or prefix ""))
8137 (verilog-signals-princ (verilog-decls-get-outputs decls)
8138 (concat prefix "Outputs:\n") (concat prefix " "))
8139 (verilog-signals-princ (verilog-decls-get-inouts decls)
8140 (concat prefix "Inout:\n") (concat prefix " "))
8141 (verilog-signals-princ (verilog-decls-get-inputs decls)
8142 (concat prefix "Inputs:\n") (concat prefix " "))
8143 (verilog-signals-princ (verilog-decls-get-vars decls)
8144 (concat prefix "Vars:\n") (concat prefix " "))
8145 (verilog-signals-princ (verilog-decls-get-assigns decls)
8146 (concat prefix "Assigns:\n") (concat prefix " "))
8147 (verilog-signals-princ (verilog-decls-get-consts decls)
8148 (concat prefix "Consts:\n") (concat prefix " "))
8149 (verilog-signals-princ (verilog-decls-get-gparams decls)
8150 (concat prefix "Gparams:\n") (concat prefix " "))
8151 (verilog-signals-princ (verilog-decls-get-interfaces decls)
8152 (concat prefix "Interfaces:\n") (concat prefix " "))
8153 (verilog-modport-princ (verilog-decls-get-modports decls)
8154 (concat prefix "Modports:\n") (concat prefix " "))
8155 (princ "\n")))
8156
8157 (defun verilog-signals-princ (signals &optional header prefix)
8158 "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
8159 (when signals
8160 (if header (princ header))
8161 (while signals
8162 (let ((sig (car signals)))
8163 (setq signals (cdr signals))
8164 (princ prefix)
8165 (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
8166 (princ " bits=") (princ (verilog-sig-bits sig))
8167 (princ " cmt=") (princ (verilog-sig-comment sig))
8168 (princ " mem=") (princ (verilog-sig-memory sig))
8169 (princ " enum=") (princ (verilog-sig-enum sig))
8170 (princ " sign=") (princ (verilog-sig-signed sig))
8171 (princ " type=") (princ (verilog-sig-type sig))
8172 (princ " dim=") (princ (verilog-sig-multidim sig))
8173 (princ " modp=") (princ (verilog-sig-modport sig))
8174 (princ "\n")))))
8175
8176 (defun verilog-modport-princ (modports &optional header prefix)
8177 "For debug, dump internal MODPORTS structures, with HEADER and PREFIX."
8178 (when modports
8179 (if header (princ header))
8180 (while modports
8181 (let ((sig (car modports)))
8182 (setq modports (cdr modports))
8183 (princ prefix)
8184 (princ "\"") (princ (verilog-modport-name sig)) (princ "\"")
8185 (princ " clockings=") (princ (verilog-modport-clockings sig))
8186 (princ "\n")
8187 (verilog-decls-princ (verilog-modport-decls sig)
8188 (concat prefix " syms:\n")
8189 (concat prefix " "))))))
8190
8191 ;;
8192 ;; Port/Wire/Etc Reading
8193 ;;
8194
8195 (defun verilog-read-inst-backward-name ()
8196 "Internal. Move point back to beginning of inst-name."
8197 (verilog-backward-open-paren)
8198 (let (done)
8199 (while (not done)
8200 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil) ; ] isn't word boundary
8201 (cond ((looking-at ")")
8202 (verilog-backward-open-paren))
8203 (t (setq done t)))))
8204 (while (looking-at "\\]")
8205 (verilog-backward-open-bracket)
8206 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil))
8207 (skip-chars-backward "a-zA-Z0-9`_$"))
8208
8209 (defun verilog-read-inst-module-matcher ()
8210 "Set match data 0 with module_name when point is inside instantiation."
8211 (verilog-read-inst-backward-name)
8212 ;; Skip over instantiation name
8213 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
8214 ;; Check for parameterized instantiations
8215 (when (looking-at ")")
8216 (verilog-backward-open-paren)
8217 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil))
8218 (skip-chars-backward "a-zA-Z0-9'_$")
8219 ;; #1 is legal syntax for gate primitives
8220 (when (save-excursion
8221 (verilog-backward-syntactic-ws-quick)
8222 (eq ?# (char-before)))
8223 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil)
8224 (skip-chars-backward "a-zA-Z0-9'_$"))
8225 (looking-at "[a-zA-Z0-9`_$]+")
8226 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8227 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
8228 ;; Caller assumes match-beginning/match-end is still set
8229 )
8230
8231 (defun verilog-read-inst-module ()
8232 "Return module_name when point is inside instantiation."
8233 (save-excursion
8234 (verilog-read-inst-module-matcher)))
8235
8236 (defun verilog-read-inst-name ()
8237 "Return instance_name when point is inside instantiation."
8238 (save-excursion
8239 (verilog-read-inst-backward-name)
8240 (looking-at "[a-zA-Z0-9`_$]+")
8241 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8242 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
8243
8244 (defun verilog-read-module-name ()
8245 "Return module name when after its ( or ;."
8246 (save-excursion
8247 (re-search-backward "[(;]")
8248 ;; Due to "module x import y (" we must search for declaration begin
8249 (verilog-re-search-backward-quick verilog-defun-re nil nil)
8250 (goto-char (match-end 0))
8251 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_$]+" nil nil)
8252 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
8253 (verilog-symbol-detick
8254 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
8255
8256 (defun verilog-read-inst-param-value ()
8257 "Return list of parameters and values when point is inside instantiation."
8258 (save-excursion
8259 (verilog-read-inst-backward-name)
8260 ;; Skip over instantiation name
8261 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
8262 ;; If there are parameterized instantiations
8263 (when (looking-at ")")
8264 (let ((end-pt (point))
8265 params
8266 param-name paren-beg-pt param-value)
8267 (verilog-backward-open-paren)
8268 (while (verilog-re-search-forward-quick "\\." end-pt t)
8269 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_$]\\)" nil nil)
8270 (skip-chars-backward "a-zA-Z0-9'_$")
8271 (looking-at "[a-zA-Z0-9`_$]+")
8272 (setq param-name (buffer-substring-no-properties
8273 (match-beginning 0) (match-end 0)))
8274 (verilog-re-search-forward-quick "(" nil nil)
8275 (setq paren-beg-pt (point))
8276 (verilog-forward-close-paren)
8277 (setq param-value (verilog-string-remove-spaces
8278 (buffer-substring-no-properties
8279 paren-beg-pt (1- (point)))))
8280 (setq params (cons (list param-name param-value) params)))
8281 params))))
8282
8283 (defun verilog-read-auto-params (num-param &optional max-param)
8284 "Return parameter list inside auto.
8285 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
8286 (let ((olist))
8287 (save-excursion
8288 ;; /*AUTOPUNT("parameter", "parameter")*/
8289 (backward-sexp 1)
8290 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
8291 (setq olist (cons (match-string 1) olist))
8292 (goto-char (match-end 0))))
8293 (or (eq nil num-param)
8294 (<= num-param (length olist))
8295 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
8296 (if (eq max-param nil) (setq max-param num-param))
8297 (or (eq nil max-param)
8298 (>= max-param (length olist))
8299 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
8300 (nreverse olist)))
8301
8302 (defun verilog-read-decls ()
8303 "Compute signal declaration information for the current module at point.
8304 Return an array of [outputs inouts inputs wire reg assign const]."
8305 (let ((end-mod-point (or (verilog-get-end-of-defun) (point-max)))
8306 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
8307 in-modport in-clocking in-ign-to-semi ptype ign-prop
8308 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
8309 sigs-gparam sigs-intf sigs-modports
8310 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
8311 modport
8312 varstack tmp)
8313 ;;(if dbg (setq dbg (concat dbg (format "\n\nverilog-read-decls START PT %s END %s\n" (point) end-mod-point))))
8314 (save-excursion
8315 (verilog-beg-of-defun-quick)
8316 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
8317 (while (< (point) end-mod-point)
8318 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
8319 (cond
8320 ((looking-at "//")
8321 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8322 (setq enum (match-string 2)))
8323 (search-forward "\n"))
8324 ((looking-at "/\\*")
8325 (forward-char 2)
8326 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8327 (setq enum (match-string 2)))
8328 (or (search-forward "*/")
8329 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8330 ((looking-at "(\\*")
8331 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8332 (forward-char 1)
8333 (or (search-forward "*)")
8334 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8335 ((eq ?\" (following-char))
8336 (or (re-search-forward "[^\\]\"" nil t) ; don't forward-char first, since we look for a non backslash first
8337 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8338 ((eq ?\; (following-char))
8339 (cond (in-ign-to-semi ; Such as inside a "import ...;" in a module header
8340 (setq in-ign-to-semi nil))
8341 ((and in-modport (not (eq in-modport t))) ; end of a modport declaration
8342 (verilog-modport-decls-set
8343 in-modport
8344 (verilog-decls-new sigs-out sigs-inout sigs-in
8345 nil nil nil nil nil nil))
8346 ;; Pop from varstack to restore state to pre-clocking
8347 (setq tmp (car varstack)
8348 varstack (cdr varstack)
8349 sigs-out (aref tmp 0)
8350 sigs-inout (aref tmp 1)
8351 sigs-in (aref tmp 2))
8352 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8353 v2kargs-ok nil in-modport nil ign-prop nil))
8354 (t
8355 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8356 v2kargs-ok nil in-modport nil ign-prop nil)))
8357 (forward-char 1))
8358 ((eq ?= (following-char))
8359 (setq rvalue t newsig nil)
8360 (forward-char 1))
8361 ((and (eq ?, (following-char))
8362 (eq paren sig-paren))
8363 (setq rvalue nil)
8364 (forward-char 1))
8365 ;; ,'s can occur inside {} & funcs
8366 ((looking-at "[{(]")
8367 (setq paren (1+ paren))
8368 (forward-char 1))
8369 ((looking-at "[})]")
8370 (setq paren (1- paren))
8371 (forward-char 1)
8372 (when (< paren sig-paren)
8373 (setq expect-signal nil rvalue nil))) ; ) that ends variables inside v2k arg list
8374 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
8375 (goto-char (match-end 0))
8376 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
8377 (setcar (cdr (cdr (cdr newsig)))
8378 (if (verilog-sig-memory newsig)
8379 (concat (verilog-sig-memory newsig) (match-string 1))
8380 (match-string 1))))
8381 (vec ; Multidimensional
8382 (setq multidim (cons vec multidim))
8383 (setq vec (verilog-string-replace-matches
8384 "\\s-+" "" nil nil (match-string 1))))
8385 (t ; Bit width
8386 (setq vec (verilog-string-replace-matches
8387 "\\s-+" "" nil nil (match-string 1))))))
8388 ;; Normal or escaped identifier -- note we remember the \ if escaped
8389 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8390 (goto-char (match-end 0))
8391 (setq keywd (match-string 1))
8392 (when (string-match "^\\\\" (match-string 1))
8393 (setq keywd (concat keywd " "))) ; Escaped ID needs space at end
8394 ;; Add any :: package names to same identifier
8395 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8396 (goto-char (match-end 0))
8397 (setq keywd (concat keywd "::" (match-string 1)))
8398 (when (string-match "^\\\\" (match-string 1))
8399 (setq keywd (concat keywd " ")))) ; Escaped ID needs space at end
8400 (cond ((equal keywd "input")
8401 (setq vec nil enum nil rvalue nil newsig nil signed nil
8402 typedefed nil multidim nil ptype nil modport nil
8403 expect-signal 'sigs-in io t sig-paren paren))
8404 ((equal keywd "output")
8405 (setq vec nil enum nil rvalue nil newsig nil signed nil
8406 typedefed nil multidim nil ptype nil modport nil
8407 expect-signal 'sigs-out io t sig-paren paren))
8408 ((equal keywd "inout")
8409 (setq vec nil enum nil rvalue nil newsig nil signed nil
8410 typedefed nil multidim nil ptype nil modport nil
8411 expect-signal 'sigs-inout io t sig-paren paren))
8412 ((equal keywd "parameter")
8413 (setq vec nil enum nil rvalue nil signed nil
8414 typedefed nil multidim nil ptype nil modport nil
8415 expect-signal 'sigs-gparam io t sig-paren paren))
8416 ((member keywd '("wire" "reg" ; Fast
8417 ;; net_type
8418 "tri" "tri0" "tri1" "triand" "trior" "trireg"
8419 "uwire" "wand" "wor"
8420 ;; integer_atom_type
8421 "byte" "shortint" "int" "longint" "integer" "time"
8422 "supply0" "supply1"
8423 ;; integer_vector_type - "reg" above
8424 "bit" "logic"
8425 ;; non_integer_type
8426 "shortreal" "real" "realtime"
8427 ;; data_type
8428 "string" "event" "chandle"))
8429 (cond (io
8430 (setq typedefed
8431 (if typedefed (concat typedefed " " keywd) keywd)))
8432 (t (setq vec nil enum nil rvalue nil signed nil
8433 typedefed nil multidim nil sig-paren paren
8434 expect-signal 'sigs-var modport nil))))
8435 ((equal keywd "assign")
8436 (setq vec nil enum nil rvalue nil signed nil
8437 typedefed nil multidim nil ptype nil modport nil
8438 expect-signal 'sigs-assign sig-paren paren))
8439 ((member keywd '("localparam" "genvar"))
8440 (setq vec nil enum nil rvalue nil signed nil
8441 typedefed nil multidim nil ptype nil modport nil
8442 expect-signal 'sigs-const sig-paren paren))
8443 ((member keywd '("signed" "unsigned"))
8444 (setq signed keywd))
8445 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
8446 (setq ign-prop t))
8447 ((member keywd '("class" "covergroup" "function"
8448 "property" "randsequence" "sequence" "task"))
8449 (unless ign-prop
8450 (setq functask (1+ functask))))
8451 ((member keywd '("endclass" "endgroup" "endfunction"
8452 "endproperty" "endsequence" "endtask"))
8453 (setq functask (1- functask)))
8454 ((equal keywd "modport")
8455 (setq in-modport t))
8456 ((equal keywd "clocking")
8457 (setq in-clocking t))
8458 ((equal keywd "import")
8459 (if v2kargs-ok ; import in module header, not a modport import
8460 (setq in-ign-to-semi t rvalue t)))
8461 ((equal keywd "type")
8462 (setq ptype t))
8463 ((equal keywd "var"))
8464 ;; Ifdef? Ignore name of define
8465 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8466 (setq rvalue t))
8467 ;; Type?
8468 ((unless ptype
8469 (verilog-typedef-name-p keywd))
8470 (cond (io
8471 (setq typedefed
8472 (if typedefed (concat typedefed " " keywd) keywd)))
8473 (t (setq vec nil enum nil rvalue nil signed nil
8474 typedefed keywd ; Have a type
8475 multidim nil sig-paren paren
8476 expect-signal 'sigs-var modport nil))))
8477 ;; Interface with optional modport in v2k arglist?
8478 ;; Skip over parsing modport, and take the interface name as the type
8479 ((and v2kargs-ok
8480 (eq paren 1)
8481 (not rvalue)
8482 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
8483 (when (match-end 2) (goto-char (match-end 2)))
8484 (setq vec nil enum nil rvalue nil signed nil
8485 typedefed keywd multidim nil ptype nil modport (match-string 2)
8486 newsig nil sig-paren paren
8487 expect-signal 'sigs-intf io t ))
8488 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
8489 ((looking-at "\\s-*\\.")
8490 (goto-char (match-end 0))
8491 (when (not rvalue)
8492 (setq expect-signal nil)))
8493 ;; "modport <keywd>"
8494 ((and (eq in-modport t)
8495 (not (member keywd verilog-keywords)))
8496 (setq in-modport (verilog-modport-new keywd nil nil))
8497 (setq sigs-modports (cons in-modport sigs-modports))
8498 ;; Push old sig values to stack and point to new signal list
8499 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8500 varstack))
8501 (setq sigs-in nil sigs-inout nil sigs-out nil))
8502 ;; "modport x (clocking <keywd>)"
8503 ((and in-modport in-clocking)
8504 (verilog-modport-clockings-add in-modport keywd)
8505 (setq in-clocking nil))
8506 ;; endclocking
8507 ((and in-clocking
8508 (equal keywd "endclocking"))
8509 (unless (eq in-clocking t)
8510 (verilog-modport-decls-set
8511 in-clocking
8512 (verilog-decls-new sigs-out sigs-inout sigs-in
8513 nil nil nil nil nil nil))
8514 ;; Pop from varstack to restore state to pre-clocking
8515 (setq tmp (car varstack)
8516 varstack (cdr varstack)
8517 sigs-out (aref tmp 0)
8518 sigs-inout (aref tmp 1)
8519 sigs-in (aref tmp 2)))
8520 (setq in-clocking nil))
8521 ;; "clocking <keywd>"
8522 ((and (eq in-clocking t)
8523 (not (member keywd verilog-keywords)))
8524 (setq in-clocking (verilog-modport-new keywd nil nil))
8525 (setq sigs-modports (cons in-clocking sigs-modports))
8526 ;; Push old sig values to stack and point to new signal list
8527 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8528 varstack))
8529 (setq sigs-in nil sigs-inout nil sigs-out nil))
8530 ;; New signal, maybe?
8531 ((and expect-signal
8532 (not rvalue)
8533 (eq functask 0)
8534 (not (member keywd verilog-keywords)))
8535 ;; Add new signal to expect-signal's variable
8536 ;;(if dbg (setq dbg (concat dbg (format "Pt %s New sig %s'\n" (point) keywd))))
8537 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
8538 (set expect-signal (cons newsig
8539 (symbol-value expect-signal))))))
8540 (t
8541 (forward-char 1)))
8542 (skip-syntax-forward " "))
8543 ;; Return arguments
8544 (setq tmp (verilog-decls-new (nreverse sigs-out)
8545 (nreverse sigs-inout)
8546 (nreverse sigs-in)
8547 (nreverse sigs-var)
8548 (nreverse sigs-modports)
8549 (nreverse sigs-assign)
8550 (nreverse sigs-const)
8551 (nreverse sigs-gparam)
8552 (nreverse sigs-intf)))
8553 ;;(if dbg (verilog-decls-princ tmp))
8554 tmp)))
8555
8556 (defvar verilog-read-sub-decls-in-interfaced nil
8557 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
8558
8559 (defvar verilog-read-sub-decls-gate-ios nil
8560 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
8561
8562 (eval-when-compile
8563 ;; Prevent compile warnings; these are let's, not globals
8564 ;; Do not remove the eval-when-compile
8565 ;; - we want an error when we are debugging this code if they are refed.
8566 (defvar sigs-in)
8567 (defvar sigs-inout)
8568 (defvar sigs-intf)
8569 (defvar sigs-intfd)
8570 (defvar sigs-out)
8571 (defvar sigs-out-d)
8572 (defvar sigs-out-i)
8573 (defvar sigs-out-unk)
8574 (defvar sigs-temp)
8575 ;; These are known to be from other packages and may not be defined
8576 (defvar diff-command nil)
8577 ;; There are known to be from newer versions of Emacs
8578 (defvar create-lockfiles))
8579
8580 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
8581 "For `verilog-read-sub-decls-line', add a signal."
8582 ;; sig eq t to indicate .name syntax
8583 ;;(message "vrsds: %s(%S)" port sig)
8584 (let ((dotname (eq sig t))
8585 portdata)
8586 (when sig
8587 (setq port (verilog-symbol-detick-denumber port))
8588 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
8589 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
8590 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
8591 (unless (or (not sig)
8592 (equal sig "")) ; Ignore .foo(1'b1) assignments
8593 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
8594 (equal "inout" verilog-read-sub-decls-gate-ios))
8595 (setq sigs-inout
8596 (cons (verilog-sig-new
8597 sig
8598 (if dotname (verilog-sig-bits portdata) vec)
8599 (concat "To/From " comment)
8600 (verilog-sig-memory portdata)
8601 nil
8602 (verilog-sig-signed portdata)
8603 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8604 (verilog-sig-type portdata))
8605 multidim nil)
8606 sigs-inout)))
8607 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
8608 (equal "output" verilog-read-sub-decls-gate-ios))
8609 (setq sigs-out
8610 (cons (verilog-sig-new
8611 sig
8612 (if dotname (verilog-sig-bits portdata) vec)
8613 (concat "From " comment)
8614 (verilog-sig-memory portdata)
8615 nil
8616 (verilog-sig-signed portdata)
8617 ;; Though ok in SV, in V2K code, propagating the
8618 ;; "reg" in "output reg" upwards isn't legal.
8619 ;; Also for backwards compatibility we don't propagate
8620 ;; "input wire" upwards.
8621 ;; See also `verilog-signals-edit-wire-reg'.
8622 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8623 (verilog-sig-type portdata))
8624 multidim nil)
8625 sigs-out)))
8626 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
8627 (equal "input" verilog-read-sub-decls-gate-ios))
8628 (setq sigs-in
8629 (cons (verilog-sig-new
8630 sig
8631 (if dotname (verilog-sig-bits portdata) vec)
8632 (concat "To " comment)
8633 (verilog-sig-memory portdata)
8634 nil
8635 (verilog-sig-signed portdata)
8636 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8637 (verilog-sig-type portdata))
8638 multidim nil)
8639 sigs-in)))
8640 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
8641 (setq sigs-intf
8642 (cons (verilog-sig-new
8643 sig
8644 (if dotname (verilog-sig-bits portdata) vec)
8645 (concat "To/From " comment)
8646 (verilog-sig-memory portdata)
8647 nil
8648 (verilog-sig-signed portdata)
8649 (verilog-sig-type portdata)
8650 multidim nil)
8651 sigs-intf)))
8652 ((setq portdata (and verilog-read-sub-decls-in-interfaced
8653 (assoc port (verilog-decls-get-vars submoddecls))))
8654 (setq sigs-intfd
8655 (cons (verilog-sig-new
8656 sig
8657 (if dotname (verilog-sig-bits portdata) vec)
8658 (concat "To/From " comment)
8659 (verilog-sig-memory portdata)
8660 nil
8661 (verilog-sig-signed portdata)
8662 (verilog-sig-type portdata)
8663 multidim nil)
8664 sigs-intf)))
8665 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
8666 )))))
8667
8668 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
8669 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
8670 ;;(message "vrsde: `%s'" expr)
8671 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
8672 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
8673 ;; Remove front operators
8674 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8675 ;;
8676 (cond
8677 ;; {..., a, b} requires us to recurse on a,b
8678 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
8679 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
8680 (unless verilog-auto-ignore-concat
8681 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
8682 mstr)
8683 (while (setq mstr (pop mlst))
8684 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
8685 (t
8686 (let (sig vec multidim)
8687 ;; Remove leading reduction operators, etc
8688 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8689 ;;(message "vrsde-ptop: `%s'" expr)
8690 (cond ; Find \signal. Final space is part of escaped signal name
8691 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8692 ;;(message "vrsde-s: `%s'" (match-string 1 expr))
8693 (setq sig (match-string 1 expr)
8694 expr (substring expr (match-end 0))))
8695 ;; Find signal
8696 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
8697 ;;(message "vrsde-s: `%s'" (match-string 1 expr))
8698 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8699 expr (substring expr (match-end 0)))))
8700 ;; Find [vector] or [multi][multi][multi][vector]
8701 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8702 ;;(message "vrsde-v: `%s'" (match-string 1 expr))
8703 (when vec (setq multidim (cons vec multidim)))
8704 (setq vec (match-string 1 expr)
8705 expr (substring expr (match-end 0))))
8706 ;; If found signal, and nothing unrecognized, add the signal
8707 ;;(message "vrsde-rem: `%s'" expr)
8708 (when (and sig (string-match "^\\s-*$" expr))
8709 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
8710
8711 (defun verilog-read-sub-decls-line (submoddecls comment)
8712 "For `verilog-read-sub-decls', read lines of port defs until none match.
8713 Inserts the list of signals found, using submodi to look up each port."
8714 (let (done port)
8715 (save-excursion
8716 (forward-line 1)
8717 (while (not done)
8718 ;; Get port name
8719 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8720 (setq port (match-string 1))
8721 (goto-char (match-end 0)))
8722 ;; .\escaped (
8723 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8724 (setq port (concat (match-string 1) " ")) ; escaped id's need trailing space
8725 (goto-char (match-end 0)))
8726 ;; .name
8727 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8728 (verilog-read-sub-decls-sig
8729 submoddecls comment (match-string 1) t ; sig==t for .name
8730 nil nil) ; vec multidim
8731 (setq port nil))
8732 ;; .\escaped_name
8733 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8734 (verilog-read-sub-decls-sig
8735 submoddecls comment (concat (match-string 1) " ") t ; sig==t for .name
8736 nil nil) ; vec multidim
8737 (setq port nil))
8738 ;; random
8739 ((looking-at "\\s-*\\.[^(]*(")
8740 (setq port nil) ; skip this line
8741 (goto-char (match-end 0)))
8742 (t
8743 (setq port nil done t))) ; Unknown, ignore rest of line
8744 ;; Get signal name. Point is at the first-non-space after (
8745 ;; We intentionally ignore (non-escaped) signals with .s in them
8746 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
8747 (when port
8748 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
8749 (verilog-read-sub-decls-sig
8750 submoddecls comment port
8751 (verilog-string-remove-spaces (match-string 1)) ; sig
8752 nil nil)) ; vec multidim
8753 ;;
8754 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
8755 (verilog-read-sub-decls-sig
8756 submoddecls comment port
8757 (verilog-string-remove-spaces (match-string 1)) ; sig
8758 (match-string 2) nil)) ; vec multidim
8759 ;; Fastpath was above looking-at's.
8760 ;; For something more complicated invoke a parser
8761 ((looking-at "[^)]+")
8762 (verilog-read-sub-decls-expr
8763 submoddecls comment port
8764 (buffer-substring
8765 (point) (1- (progn (search-backward "(") ; start at (
8766 (verilog-forward-sexp-ign-cmt 1)
8767 (point)))))))) ; expr
8768 ;;
8769 (forward-line 1)))))
8770
8771 (defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8772 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8773 Inserts the list of signals found."
8774 (save-excursion
8775 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8776 (while (< (point) end-inst-point)
8777 ;; Get primitive's signal name, as will never have port, and no trailing )
8778 (cond ((looking-at "//")
8779 (search-forward "\n"))
8780 ((looking-at "/\\*")
8781 (or (search-forward "*/")
8782 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8783 ((looking-at "(\\*")
8784 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8785 (forward-char 1)
8786 (or (search-forward "*)")
8787 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8788 ;; On pins, parse and advance to next pin
8789 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8790 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8791 (goto-char (match-end 0))
8792 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8793 iolist (cdr iolist))
8794 (verilog-read-sub-decls-expr
8795 submoddecls comment "primitive_port"
8796 (match-string 0)))
8797 (t
8798 (forward-char 1)
8799 (skip-syntax-forward " ")))))))
8800
8801 (defun verilog-read-sub-decls ()
8802 "Internally parse signals going to modules under this module.
8803 Return an array of [ outputs inouts inputs ] signals for modules that are
8804 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8805 is an output, then SIG will be included in the list.
8806
8807 This only works on instantiations created with /*AUTOINST*/ converted by
8808 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8809 component library to determine connectivity of the design.
8810
8811 One work around for this problem is to manually create // Inputs and //
8812 Outputs comments above subcell signals, for example:
8813
8814 module ModuleName (
8815 // Outputs
8816 .out (out),
8817 // Inputs
8818 .in (in));"
8819 (save-excursion
8820 (let ((end-mod-point (verilog-get-end-of-defun))
8821 st-point end-inst-point
8822 ;; below 3 modified by verilog-read-sub-decls-line
8823 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
8824 (verilog-beg-of-defun-quick)
8825 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
8826 (save-excursion
8827 (goto-char (match-beginning 0))
8828 (unless (verilog-inside-comment-or-string-p)
8829 ;; Attempt to snarf a comment
8830 (let* ((submod (verilog-read-inst-module))
8831 (inst (verilog-read-inst-name))
8832 (subprim (member submod verilog-gate-keywords))
8833 (comment (concat inst " of " submod ".v"))
8834 submodi submoddecls)
8835 (cond
8836 (subprim
8837 (setq submodi `primitive
8838 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8839 comment (concat inst " of " submod))
8840 (verilog-backward-open-paren)
8841 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8842 (point))
8843 st-point (point))
8844 (forward-char 1)
8845 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8846 ;; Non-primitive
8847 (t
8848 (when (setq submodi (verilog-modi-lookup submod t))
8849 (setq submoddecls (verilog-modi-get-decls submodi)
8850 verilog-read-sub-decls-gate-ios nil)
8851 (verilog-backward-open-paren)
8852 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8853 (point))
8854 st-point (point))
8855 ;; This could have used a list created by verilog-auto-inst
8856 ;; However I want it to be runnable even on user's manually added signals
8857 (let ((verilog-read-sub-decls-in-interfaced t))
8858 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8859 (verilog-read-sub-decls-line submoddecls comment))) ; Modifies sigs-ifd
8860 (goto-char st-point)
8861 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8862 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-out
8863 (goto-char st-point)
8864 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8865 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-out
8866 (goto-char st-point)
8867 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8868 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-inout
8869 (goto-char st-point)
8870 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8871 (verilog-read-sub-decls-line submoddecls comment)) ; Modifies sigs-in
8872 )))))))
8873 ;; Combine duplicate bits
8874 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
8875 (verilog-subdecls-new
8876 (verilog-signals-combine-bus (nreverse sigs-out))
8877 (verilog-signals-combine-bus (nreverse sigs-inout))
8878 (verilog-signals-combine-bus (nreverse sigs-in))
8879 (verilog-signals-combine-bus (nreverse sigs-intf))
8880 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
8881
8882 (defun verilog-read-inst-pins ()
8883 "Return an array of [ pins ] for the current instantiation at point.
8884 For example if declare A A (.B(SIG)) then B will be included in the list."
8885 (save-excursion
8886 (let ((end-mod-point (point)) ; presume at /*AUTOINST*/ point
8887 pins pin)
8888 (verilog-backward-open-paren)
8889 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8890 (setq pin (match-string 1))
8891 (unless (verilog-inside-comment-or-string-p)
8892 (setq pins (cons (list pin) pins))
8893 (when (looking-at "(")
8894 (verilog-forward-sexp-ign-cmt 1))))
8895 (vector pins))))
8896
8897 (defun verilog-read-arg-pins ()
8898 "Return an array of [ pins ] for the current argument declaration at point."
8899 (save-excursion
8900 (let ((end-mod-point (point)) ; presume at /*AUTOARG*/ point
8901 pins pin)
8902 (verilog-backward-open-paren)
8903 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8904 (setq pin (match-string 1))
8905 (unless (verilog-inside-comment-or-string-p)
8906 (setq pins (cons (list pin) pins))))
8907 (vector pins))))
8908
8909 (defun verilog-read-auto-constants (beg end-mod-point)
8910 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8911 ;; Insert new
8912 (save-excursion
8913 (let (sig-list tpl-end-pt)
8914 (goto-char beg)
8915 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8916 (if (not (looking-at "\\s *("))
8917 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8918 (search-forward "(" end-mod-point)
8919 (setq tpl-end-pt (save-excursion
8920 (backward-char 1)
8921 (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
8922 (backward-char 1)
8923 (point)))
8924 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8925 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8926 sig-list)))
8927
8928 (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8929 (make-variable-buffer-local 'verilog-cache-has-lisp)
8930
8931 (defun verilog-read-auto-lisp-present ()
8932 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
8933 (save-excursion
8934 (goto-char (point-min))
8935 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8936
8937 (defun verilog-read-auto-lisp (start end)
8938 "Look for and evaluate an AUTO_LISP between START and END.
8939 Must call `verilog-read-auto-lisp-present' before this function."
8940 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8941 (when verilog-cache-has-lisp
8942 (save-excursion
8943 (goto-char start)
8944 (while (re-search-forward "\\<AUTO_LISP(" end t)
8945 (backward-char)
8946 (let* ((beg-pt (prog1 (point)
8947 (verilog-forward-sexp-cmt 1))) ; Closing paren
8948 (end-pt (point))
8949 (verilog-in-hooks t))
8950 (eval-region beg-pt end-pt nil))))))
8951
8952 (defun verilog-read-always-signals-recurse
8953 (exit-keywd rvalue temp-next)
8954 "Recursive routine for parentheses/bracket matching.
8955 EXIT-KEYWD is expression to stop at, nil if top level.
8956 RVALUE is true if at right hand side of equal.
8957 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
8958 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ; true if after a ; we are looking for rvalue
8959 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
8960 ignore-next)
8961 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
8962 (while (not (or (eobp) gotend))
8963 (cond
8964 ((looking-at "//")
8965 (search-forward "\n"))
8966 ((looking-at "/\\*")
8967 (or (search-forward "*/")
8968 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8969 ((looking-at "(\\*")
8970 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8971 (forward-char 1)
8972 (or (search-forward "*)")
8973 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8974 (t (setq keywd (buffer-substring-no-properties
8975 (point)
8976 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8977 (forward-char 1))
8978 (point)))
8979 sig-last-tolk sig-tolk
8980 sig-tolk nil)
8981 ;;(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))))
8982 (cond
8983 ((equal keywd "\"")
8984 (or (re-search-forward "[^\\]\"" nil t)
8985 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8986 ;; else at top level loop, keep parsing
8987 ((and end-else-check (equal keywd "else"))
8988 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
8989 ;; no forward movement, want to see else in lower loop
8990 (setq end-else-check nil))
8991 ;; End at top level loop
8992 ((and end-else-check (looking-at "[^ \t\n\f]"))
8993 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
8994 (setq gotend t))
8995 ;; Final statement?
8996 ((and exit-keywd (equal keywd exit-keywd))
8997 (setq gotend t)
8998 (forward-char (length keywd)))
8999 ;; Standard tokens...
9000 ((equal keywd ";")
9001 (setq ignore-next nil rvalue semi-rvalue)
9002 ;; Final statement at top level loop?
9003 (when (not exit-keywd)
9004 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
9005 (setq end-else-check t))
9006 (forward-char 1))
9007 ((equal keywd "'")
9008 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
9009 (goto-char (match-end 0))
9010 (forward-char 1)))
9011 ((equal keywd ":") ; Case statement, begin/end label, x?y:z
9012 (cond ((equal "endcase" exit-keywd) ; case x: y=z; statement next
9013 (setq ignore-next nil rvalue nil))
9014 ((equal "?" exit-keywd) ; x?y:z rvalue
9015 ) ; NOP
9016 ((equal "]" exit-keywd) ; [x:y] rvalue
9017 ) ; NOP
9018 (got-sig ; label: statement
9019 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
9020 ((not rvalue) ; begin label
9021 (setq ignore-next t rvalue nil)))
9022 (forward-char 1))
9023 ((equal keywd "=")
9024 (when got-sig
9025 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
9026 (set got-list (cons got-sig (symbol-value got-list)))
9027 (setq got-sig nil))
9028 (when (not rvalue)
9029 (if (eq (char-before) ?< )
9030 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
9031 sigs-out-unk nil)
9032 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
9033 sigs-out-unk nil)))
9034 (setq ignore-next nil rvalue t)
9035 (forward-char 1))
9036 ((equal keywd "?")
9037 (forward-char 1)
9038 (verilog-read-always-signals-recurse ":" rvalue nil))
9039 ((equal keywd "[")
9040 (forward-char 1)
9041 (verilog-read-always-signals-recurse "]" t nil))
9042 ((equal keywd "(")
9043 (forward-char 1)
9044 (cond (sig-last-tolk ; Function call; zap last signal
9045 (setq got-sig nil)))
9046 (cond ((equal last-keywd "for")
9047 ;; temp-next: Variables on LHS are lvalues, but generally we want
9048 ;; to ignore them, assuming they are loop increments
9049 (verilog-read-always-signals-recurse ";" nil t)
9050 (verilog-read-always-signals-recurse ";" t nil)
9051 (verilog-read-always-signals-recurse ")" nil nil))
9052 (t (verilog-read-always-signals-recurse ")" t nil))))
9053 ((equal keywd "begin")
9054 (skip-syntax-forward "w_")
9055 (verilog-read-always-signals-recurse "end" nil nil)
9056 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
9057 (setq ignore-next nil rvalue semi-rvalue)
9058 (if (not exit-keywd) (setq end-else-check t)))
9059 ((member keywd '("case" "casex" "casez"))
9060 (skip-syntax-forward "w_")
9061 (verilog-read-always-signals-recurse "endcase" t nil)
9062 (setq ignore-next nil rvalue semi-rvalue)
9063 (if (not exit-keywd) (setq gotend t))) ; top level begin/end
9064 ((string-match "^[$`a-zA-Z_]" keywd) ; not exactly word constituent
9065 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
9066 (setq ignore-next t))
9067 ((or ignore-next
9068 (member keywd verilog-keywords)
9069 (string-match "^\\$" keywd)) ; PLI task
9070 (setq ignore-next nil))
9071 (t
9072 (setq keywd (verilog-symbol-detick-denumber keywd))
9073 (when got-sig
9074 (set got-list (cons got-sig (symbol-value got-list)))
9075 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
9076 )
9077 (setq got-list (cond (temp-next 'sigs-temp)
9078 (rvalue 'sigs-in)
9079 (t 'sigs-out-unk))
9080 got-sig (if (or (not keywd)
9081 (assoc keywd (symbol-value got-list)))
9082 nil (list keywd nil nil))
9083 temp-next nil
9084 sig-tolk t)))
9085 (skip-chars-forward "a-zA-Z0-9$_.%`"))
9086 (t
9087 (forward-char 1)))
9088 ;; End of non-comment token
9089 (setq last-keywd keywd)))
9090 (skip-syntax-forward " "))
9091 ;; Append the final pending signal
9092 (when got-sig
9093 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
9094 (set got-list (cons got-sig (symbol-value got-list)))
9095 (setq got-sig nil))
9096 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
9097 ))
9098
9099 (defun verilog-read-always-signals ()
9100 "Parse always block at point and return list of (outputs inout inputs)."
9101 (save-excursion
9102 (let* (;(dbg "")
9103 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
9104 (verilog-read-always-signals-recurse nil nil nil)
9105 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
9106 sigs-out-unk nil)
9107 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
9108 ;; Return what was found
9109 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
9110
9111 (defun verilog-read-instants ()
9112 "Parse module at point and return list of ( ( file instance ) ... )."
9113 (verilog-beg-of-defun-quick)
9114 (let* ((end-mod-point (verilog-get-end-of-defun))
9115 (state nil)
9116 (instants-list nil))
9117 (save-excursion
9118 (while (< (point) end-mod-point)
9119 ;; Stay at level 0, no comments
9120 (while (progn
9121 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
9122 (or (> (car state) 0) ; in parens
9123 (nth 5 state) ; comment
9124 ))
9125 (forward-line 1))
9126 (beginning-of-line)
9127 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
9128 ;;(if (looking-at "^\\(.+\\)$")
9129 (let ((module (match-string 1))
9130 (instant (match-string 2)))
9131 (if (not (member module verilog-keywords))
9132 (setq instants-list (cons (list module instant) instants-list)))))
9133 (forward-line 1)))
9134 instants-list))
9135
9136
9137 (defun verilog-read-auto-template-middle ()
9138 "With point in middle of an AUTO_TEMPLATE, parse it.
9139 Returns REGEXP and list of ( (signal_name connection_name)... )."
9140 (save-excursion
9141 ;; Find beginning
9142 (let ((tpl-regexp "\\([0-9]+\\)")
9143 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
9144 (templateno 0)
9145 tpl-sig-list tpl-wild-list tpl-end-pt rep)
9146 ;; Parse "REGEXP"
9147 ;; We reserve @"..." for future lisp expressions that evaluate
9148 ;; once-per-AUTOINST
9149 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
9150 (setq tpl-regexp (match-string 1))
9151 (goto-char (match-end 0)))
9152 (search-forward "(")
9153 ;; Parse lines in the template
9154 (when (or verilog-auto-inst-template-numbers
9155 verilog-auto-template-warn-unused)
9156 (save-excursion
9157 (let ((pre-pt (point)))
9158 (goto-char (point-min))
9159 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
9160 (setq templateno (1+ templateno)))
9161 (while (< (point) pre-pt)
9162 (forward-line 1)
9163 (setq lineno (1+ lineno))))))
9164 (setq tpl-end-pt (save-excursion
9165 (backward-char 1)
9166 (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
9167 (backward-char 1)
9168 (point)))
9169 ;;
9170 (while (< (point) tpl-end-pt)
9171 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
9172 (setq tpl-sig-list
9173 (cons (list
9174 (match-string-no-properties 1)
9175 (match-string-no-properties 2)
9176 templateno lineno)
9177 tpl-sig-list))
9178 (goto-char (match-end 0)))
9179 ;; Regexp form??
9180 ((looking-at
9181 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
9182 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
9183 (setq rep (match-string-no-properties 3))
9184 (goto-char (match-end 0))
9185 (setq tpl-wild-list
9186 (cons (list
9187 (concat "^"
9188 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
9189 (match-string 1))
9190 "$")
9191 rep
9192 templateno lineno)
9193 tpl-wild-list)))
9194 ((looking-at "[ \t\f]+")
9195 (goto-char (match-end 0)))
9196 ((looking-at "\n")
9197 (setq lineno (1+ lineno))
9198 (goto-char (match-end 0)))
9199 ((looking-at "//")
9200 (search-forward "\n")
9201 (setq lineno (1+ lineno)))
9202 ((looking-at "/\\*")
9203 (forward-char 2)
9204 (or (search-forward "*/")
9205 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
9206 (t
9207 (error "%s: AUTO_TEMPLATE parsing error: %s"
9208 (verilog-point-text)
9209 (progn (looking-at ".*$") (match-string 0))))))
9210 ;; Return
9211 (vector tpl-regexp
9212 (list tpl-sig-list tpl-wild-list)))))
9213
9214 (defun verilog-read-auto-template (module)
9215 "Look for an auto_template for the instantiation of the given MODULE.
9216 If found returns `verilog-read-auto-template-inside' structure."
9217 (save-excursion
9218 ;; Find beginning
9219 (let ((pt (point)))
9220 ;; Note this search is expensive, as we hunt from mod-begin to point
9221 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
9222 ;; So, we look first for an exact string rather than a slow regexp.
9223 ;; Someday we may keep a cache of every template, but this would also
9224 ;; need to record the relative position of each AUTOINST, as multiple
9225 ;; templates exist for each module, and we're inserting lines.
9226 (cond ((or
9227 ;; See also regexp in `verilog-auto-template-lint'
9228 (verilog-re-search-backward-substr
9229 "AUTO_TEMPLATE"
9230 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
9231 ;; Also try forward of this AUTOINST
9232 ;; This is for historical support; this isn't speced as working
9233 (progn
9234 (goto-char pt)
9235 (verilog-re-search-forward-substr
9236 "AUTO_TEMPLATE"
9237 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
9238 (goto-char (match-end 0))
9239 (verilog-read-auto-template-middle))
9240 ;; If no template found
9241 (t (vector "" nil))))))
9242 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
9243
9244 (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
9245 (make-variable-buffer-local 'verilog-auto-template-hits)
9246
9247 (defun verilog-read-auto-template-init ()
9248 "Initialize `verilog-read-auto-template'."
9249 (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
9250 (when verilog-auto-template-warn-unused
9251 (setq verilog-auto-template-hits
9252 (make-hash-table :test 'equal :rehash-size 4.0)))))
9253
9254 (defun verilog-read-auto-template-hit (tpl-ass)
9255 "Record that TPL-ASS template from `verilog-read-auto-template' was used."
9256 (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
9257 (when verilog-auto-template-warn-unused
9258 (unless verilog-auto-template-hits
9259 (verilog-read-auto-template-init))
9260 (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
9261 verilog-auto-template-hits))))
9262
9263 (defun verilog-set-define (defname defvalue &optional buffer enumname)
9264 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
9265 Optionally associate it with the specified enumeration ENUMNAME."
9266 (with-current-buffer (or buffer (current-buffer))
9267 ;; Namespace intentionally short for AUTOs and compatibility
9268 (let ((mac (intern (concat "vh-" defname))))
9269 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9270 ;; Need to define to a constant if no value given
9271 (set (make-local-variable mac)
9272 (if (equal defvalue "") "1" defvalue)))
9273 (if enumname
9274 ;; Namespace intentionally short for AUTOs and compatibility
9275 (let ((enumvar (intern (concat "venum-" enumname))))
9276 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
9277 (unless (boundp enumvar) (set enumvar nil))
9278 (add-to-list (make-local-variable enumvar) defname)))))
9279
9280 (defun verilog-read-defines (&optional filename recurse subcall)
9281 "Read \\=`defines and parameters for the current file, or optional FILENAME.
9282 If the filename is provided, `verilog-library-flags' will be used to
9283 resolve it. If optional RECURSE is non-nil, recurse through \\=`includes.
9284
9285 Parameters must be simple assignments to constants, or have their own
9286 \"parameter\" label rather than a list of parameters. Thus:
9287
9288 parameter X = 5, Y = 10; // Ok
9289 parameter X = {1\\='b1, 2\\='h2}; // Ok
9290 parameter X = {1\\='b1, 2\\='h2}, Y = 10; // Bad, make into 2 parameter lines
9291
9292 Defines must be simple text substitutions, one on a line, starting
9293 at the beginning of the line. Any ifdefs or multiline comments around the
9294 define are ignored.
9295
9296 Defines are stored inside Emacs variables using the name vh-{definename}.
9297
9298 This function is useful for setting vh-* variables. The file variables
9299 feature can be used to set defines that `verilog-mode' can see; put at the
9300 *END* of your file something like:
9301
9302 // Local Variables:
9303 // vh-macro:\"macro_definition\"
9304 // End:
9305
9306 If macros are defined earlier in the same file and you want their values,
9307 you can read them automatically (provided `enable-local-eval' is on):
9308
9309 // Local Variables:
9310 // eval:(verilog-read-defines)
9311 // eval:(verilog-read-defines \"group_standard_includes.v\")
9312 // End:
9313
9314 Note these are only read when the file is first visited, you must use
9315 \\[find-alternate-file] RET to have these take effect after editing them!
9316
9317 If you want to disable the \"Process `eval' or hook local variables\"
9318 warning message, you need to add to your init file:
9319
9320 (setq enable-local-eval t)"
9321 (let ((origbuf (current-buffer)))
9322 (save-excursion
9323 (unless subcall (verilog-getopt-flags))
9324 (when filename
9325 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
9326 (if fns
9327 (set-buffer (find-file-noselect (car fns)))
9328 (error "%s: Can't find verilog-read-defines file: %s"
9329 (verilog-point-text) filename))))
9330 (when recurse
9331 (goto-char (point-min))
9332 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9333 (let ((inc (verilog-string-replace-matches
9334 "\"" "" nil nil (match-string-no-properties 1))))
9335 (unless (verilog-inside-comment-or-string-p)
9336 (verilog-read-defines inc recurse t)))))
9337 ;; Read `defines
9338 ;; note we don't use verilog-re... it's faster this way, and that
9339 ;; function has problems when comments are at the end of the define
9340 (goto-char (point-min))
9341 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
9342 (let ((defname (match-string-no-properties 1))
9343 (defvalue (match-string-no-properties 2)))
9344 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9345 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
9346 (verilog-set-define defname defvalue origbuf))))
9347 ;; Hack: Read parameters
9348 (goto-char (point-min))
9349 (while (re-search-forward
9350 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
9351 (let (enumname)
9352 ;; The primary way of getting defines is verilog-read-decls
9353 ;; However, that isn't called yet for included files, so we'll add another scheme
9354 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
9355 (setq enumname (match-string-no-properties 2)))
9356 (forward-comment 99999)
9357 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
9358 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
9359 (unless (verilog-inside-comment-or-string-p (match-beginning 0))
9360 (verilog-set-define (match-string-no-properties 1)
9361 (match-string-no-properties 2) origbuf enumname))
9362 (goto-char (match-end 0))
9363 (forward-comment 99999)))))))
9364
9365 (defun verilog-read-includes ()
9366 "Read \\=`includes for the current file.
9367 This will find all of the \\=`includes which are at the beginning of lines,
9368 ignoring any ifdefs or multiline comments around them.
9369 `verilog-read-defines' is then performed on the current and each included
9370 file.
9371
9372 It is often useful put at the *END* of your file something like:
9373
9374 // Local Variables:
9375 // eval:(verilog-read-defines)
9376 // eval:(verilog-read-includes)
9377 // End:
9378
9379 Note includes are only read when the file is first visited, you must use
9380 \\[find-alternate-file] RET to have these take effect after editing them!
9381
9382 It is good to get in the habit of including all needed files in each .v
9383 file that needs it, rather than waiting for compile time. This will aid
9384 this process, Verilint, and readability. To prevent defining the same
9385 variable over and over when many modules are compiled together, put a test
9386 around the inside each include file:
9387
9388 foo.v (an include file):
9389 \\=`ifdef _FOO_V // include if not already included
9390 \\=`else
9391 \\=`define _FOO_V
9392 ... contents of file
9393 \\=`endif // _FOO_V"
9394 ;;slow: (verilog-read-defines nil t)
9395 (save-excursion
9396 (verilog-getopt-flags)
9397 (goto-char (point-min))
9398 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9399 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
9400 (verilog-read-defines inc nil t)))))
9401
9402 (defun verilog-read-signals (&optional start end)
9403 "Return a simple list of all possible signals in the file.
9404 Bounded by optional region from START to END. Overly aggressive but fast.
9405 Some macros and such are also found and included. For dinotrace.el."
9406 (let (sigs-all keywd)
9407 (progn;save-excursion
9408 (goto-char (or start (point-min)))
9409 (setq end (or end (point-max)))
9410 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
9411 (forward-char -1)
9412 (cond
9413 ((looking-at "//")
9414 (search-forward "\n"))
9415 ((looking-at "/\\*")
9416 (search-forward "*/"))
9417 ((looking-at "(\\*")
9418 (or (looking-at "(\\*\\s-*)") ; It's an "always @ (*)"
9419 (search-forward "*)")))
9420 ((eq ?\" (following-char))
9421 (re-search-forward "[^\\]\"")) ; don't forward-char first, since we look for a non backslash first
9422 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
9423 (goto-char (match-end 0))
9424 (setq keywd (match-string-no-properties 1))
9425 (or (member keywd verilog-keywords)
9426 (member keywd sigs-all)
9427 (setq sigs-all (cons keywd sigs-all))))
9428 (t (forward-char 1))))
9429 ;; Return list
9430 sigs-all)))
9431
9432 ;;
9433 ;; Argument file parsing
9434 ;;
9435
9436 (defun verilog-getopt (arglist)
9437 "Parse -f, -v etc arguments in ARGLIST list or string."
9438 (unless (listp arglist) (setq arglist (list arglist)))
9439 (let ((space-args '())
9440 arg next-param)
9441 ;; Split on spaces, so users can pass whole command lines
9442 (while arglist
9443 (setq arg (car arglist)
9444 arglist (cdr arglist))
9445 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
9446 (setq space-args (append space-args
9447 (list (match-string-no-properties 1 arg))))
9448 (setq arg (match-string 2 arg))))
9449 ;; Parse arguments
9450 (while space-args
9451 (setq arg (car space-args)
9452 space-args (cdr space-args))
9453 (cond
9454 ;; Need another arg
9455 ((equal arg "-f")
9456 (setq next-param arg))
9457 ((equal arg "-v")
9458 (setq next-param arg))
9459 ((equal arg "-y")
9460 (setq next-param arg))
9461 ;; +libext+(ext1)+(ext2)...
9462 ((string-match "^\\+libext\\+\\(.*\\)" arg)
9463 (setq arg (match-string 1 arg))
9464 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
9465 (verilog-add-list-unique `verilog-library-extensions
9466 (match-string 1 arg))
9467 (setq arg (match-string 2 arg))))
9468 ;;
9469 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ; -Ddefine=val
9470 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ; -Ddefine
9471 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ; +define+val
9472 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ; +define+define
9473 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
9474 ;;
9475 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ; +incdir+dir
9476 (string-match "^-I\\(.*\\)" arg)) ; -Idir
9477 (verilog-add-list-unique `verilog-library-directories
9478 (match-string 1 (substitute-in-file-name arg))))
9479 ;; Ignore
9480 ((equal "+librescan" arg))
9481 ((string-match "^-U\\(.*\\)" arg)) ; -Udefine
9482 ;; Second parameters
9483 ((equal next-param "-f")
9484 (setq next-param nil)
9485 (verilog-getopt-file (substitute-in-file-name arg)))
9486 ((equal next-param "-v")
9487 (setq next-param nil)
9488 (verilog-add-list-unique `verilog-library-files
9489 (substitute-in-file-name arg)))
9490 ((equal next-param "-y")
9491 (setq next-param nil)
9492 (verilog-add-list-unique `verilog-library-directories
9493 (substitute-in-file-name arg)))
9494 ;; Filename
9495 ((string-match "^[^-+]" arg)
9496 (verilog-add-list-unique `verilog-library-files
9497 (substitute-in-file-name arg)))
9498 ;; Default - ignore; no warning
9499 ))))
9500 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
9501
9502 (defun verilog-getopt-file (filename)
9503 "Read Verilog options from the specified FILENAME."
9504 (save-excursion
9505 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
9506 (orig-buffer (current-buffer))
9507 line)
9508 (if fns
9509 (set-buffer (find-file-noselect (car fns)))
9510 (error "%s: Can't find verilog-getopt-file -f file: %s"
9511 (verilog-point-text) filename))
9512 (goto-char (point-min))
9513 (while (not (eobp))
9514 (setq line (buffer-substring (point) (point-at-eol)))
9515 (forward-line 1)
9516 (when (string-match "//" line)
9517 (setq line (substring line 0 (match-beginning 0))))
9518 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
9519 (verilog-getopt line))))))
9520
9521 (defun verilog-getopt-flags ()
9522 "Convert `verilog-library-flags' into standard library variables."
9523 ;; If the flags are local, then all the outputs should be local also
9524 (when (local-variable-p `verilog-library-flags (current-buffer))
9525 (mapc 'make-local-variable '(verilog-library-extensions
9526 verilog-library-directories
9527 verilog-library-files
9528 verilog-library-flags)))
9529 ;; Allow user to customize
9530 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
9531 ;; Process arguments
9532 (verilog-getopt verilog-library-flags)
9533 ;; Allow user to customize
9534 (verilog-run-hooks 'verilog-getopt-flags-hook))
9535
9536 (defun verilog-add-list-unique (varref object)
9537 "Append to VARREF list the given OBJECT,
9538 unless it is already a member of the variable's list."
9539 (unless (member object (symbol-value varref))
9540 (set varref (append (symbol-value varref) (list object))))
9541 varref)
9542 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
9543
9544 (defun verilog-current-flags ()
9545 "Convert `verilog-library-flags' and similar variables to command line.
9546 Used for __FLAGS__ in `verilog-expand-command'."
9547 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
9548 (when (equal cmd "")
9549 (setq cmd (concat
9550 "+libext+" (mapconcat `concat verilog-library-extensions "+")
9551 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
9552 verilog-library-directories "")
9553 (mapconcat (lambda (i) (concat " -v " i))
9554 verilog-library-files ""))))
9555 cmd))
9556 ;;(verilog-current-flags)
9557
9558 \f
9559 ;;; Cached directory support:
9560 ;;
9561
9562 (defvar verilog-dir-cache-preserving nil
9563 "If true, the directory cache is enabled, and file system changes are ignored.
9564 See `verilog-dir-exists-p' and `verilog-dir-files'.")
9565
9566 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
9567 (defvar verilog-dir-cache-list nil
9568 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
9569 (defvar verilog-dir-cache-lib-filenames nil
9570 "Cached data for `verilog-library-filenames'.")
9571
9572 (defmacro verilog-preserve-dir-cache (&rest body)
9573 "Execute the BODY forms, allowing directory cache preservation within BODY.
9574 This means that changes inside BODY made to the file system will not be
9575 seen by the `verilog-dir-files' and related functions."
9576 `(let ((verilog-dir-cache-preserving (current-buffer))
9577 verilog-dir-cache-list
9578 verilog-dir-cache-lib-filenames)
9579 (progn ,@body)))
9580
9581 (defun verilog-dir-files (dirname)
9582 "Return all filenames in the DIRNAME directory.
9583 Relative paths depend on the `default-directory'.
9584 Results are cached if inside `verilog-preserve-dir-cache'."
9585 (unless verilog-dir-cache-preserving
9586 (setq verilog-dir-cache-list nil)) ; Cache disabled
9587 ;; We don't use expand-file-name on the dirname to make key, as it's slow
9588 (let* ((cache-key (list dirname default-directory))
9589 (fass (assoc cache-key verilog-dir-cache-list))
9590 exp-dirname data)
9591 (cond (fass ; Return data from cache hit
9592 (nth 1 fass))
9593 (t
9594 (setq exp-dirname (expand-file-name dirname)
9595 data (and (file-directory-p exp-dirname)
9596 (directory-files exp-dirname nil nil nil)))
9597 ;; Note we also encache nil for non-existing dirs.
9598 (setq verilog-dir-cache-list (cons (list cache-key data)
9599 verilog-dir-cache-list))
9600 data))))
9601 ;; Miss-and-hit test:
9602 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
9603 ;; (prin1 (verilog-dir-files ".")) nil)
9604
9605 (defun verilog-dir-file-exists-p (filename)
9606 "Return true if FILENAME exists.
9607 Like `file-exists-p' but results are cached if inside
9608 `verilog-preserve-dir-cache'."
9609 (let* ((dirname (file-name-directory filename))
9610 ;; Correct for file-name-nondirectory returning same if no slash.
9611 (dirnamed (if (or (not dirname) (equal dirname filename))
9612 default-directory dirname))
9613 (flist (verilog-dir-files dirnamed)))
9614 (and flist
9615 (member (file-name-nondirectory filename) flist)
9616 t)))
9617 ;;(verilog-dir-file-exists-p "verilog-mode.el")
9618 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
9619
9620 \f
9621 ;;; Module name lookup:
9622 ;;
9623
9624 (defun verilog-module-inside-filename-p (module filename)
9625 "Return modi if MODULE is specified inside FILENAME, else nil.
9626 Allows version control to check out the file if need be."
9627 (and (or (file-exists-p filename)
9628 (and (fboundp 'vc-backend)
9629 (vc-backend filename)))
9630 (let (modi type)
9631 (with-current-buffer (find-file-noselect filename)
9632 (save-excursion
9633 (goto-char (point-min))
9634 (while (and
9635 ;; It may be tempting to look for verilog-defun-re,
9636 ;; don't, it slows things down a lot!
9637 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
9638 (setq type (match-string-no-properties 0))
9639 (verilog-re-search-forward-quick "[(;]" nil t))
9640 (if (equal module (verilog-read-module-name))
9641 (setq modi (verilog-modi-new module filename (point) type))))
9642 modi)))))
9643
9644 (defun verilog-is-number (symbol)
9645 "Return true if SYMBOL is number-like."
9646 (or (string-match "^[0-9 \t:]+$" symbol)
9647 (string-match "^[---]*[0-9]+$" symbol)
9648 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
9649
9650 (defun verilog-symbol-detick (symbol wing-it)
9651 "Return an expanded SYMBOL name without any defines.
9652 If the variable vh-{symbol} is defined, return that value.
9653 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
9654 (while (and symbol (string-match "^`" symbol))
9655 (setq symbol (substring symbol 1))
9656 (setq symbol
9657 ;; Namespace intentionally short for AUTOs and compatibility
9658 (if (boundp (intern (concat "vh-" symbol)))
9659 ;; Emacs has a bug where boundp on a buffer-local
9660 ;; variable in only one buffer returns t in another.
9661 ;; This can confuse, so check for nil.
9662 ;; Namespace intentionally short for AUTOs and compatibility
9663 (let ((val (eval (intern (concat "vh-" symbol)))))
9664 (if (eq val nil)
9665 (if wing-it symbol nil)
9666 val))
9667 (if wing-it symbol nil))))
9668 symbol)
9669 ;;(verilog-symbol-detick "`mod" nil)
9670
9671 (defun verilog-symbol-detick-denumber (symbol)
9672 "Return SYMBOL with defines converted and any numbers dropped to nil."
9673 (when (string-match "^`" symbol)
9674 ;; This only will work if the define is a simple signal, not
9675 ;; something like a[b]. Sorry, it should be substituted into the parser
9676 (setq symbol
9677 (verilog-string-replace-matches
9678 "\\[[^0-9: \t]+\\]" "" nil nil
9679 (or (verilog-symbol-detick symbol nil)
9680 (if verilog-auto-sense-defines-constant
9681 "0"
9682 symbol)))))
9683 (if (verilog-is-number symbol)
9684 nil
9685 symbol))
9686
9687 (defun verilog-symbol-detick-text (text)
9688 "Return TEXT without any known defines.
9689 If the variable vh-{symbol} is defined, substitute that value."
9690 (let ((ok t) symbol val)
9691 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
9692 (setq symbol (match-string 1 text))
9693 ;;(message symbol)
9694 (cond ((and
9695 ;; Namespace intentionally short for AUTOs and compatibility
9696 (boundp (intern (concat "vh-" symbol)))
9697 ;; Emacs has a bug where boundp on a buffer-local
9698 ;; variable in only one buffer returns t in another.
9699 ;; This can confuse, so check for nil.
9700 ;; Namespace intentionally short for AUTOs and compatibility
9701 (setq val (eval (intern (concat "vh-" symbol)))))
9702 (setq text (replace-match val nil nil text)))
9703 (t (setq ok nil)))))
9704 text)
9705 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
9706
9707 (defun verilog-expand-dirnames (&optional dirnames)
9708 "Return a list of existing directories given a list of wildcarded DIRNAMES.
9709 Or, just the existing dirnames themselves if there are no wildcards."
9710 ;; Note this function is performance critical.
9711 ;; Do not call anything that requires disk access that cannot be cached.
9712 (interactive)
9713 (unless dirnames
9714 (error "`verilog-library-directories' should include at least `.'"))
9715 (setq dirnames (reverse dirnames)) ; not nreverse
9716 (let ((dirlist nil)
9717 pattern dirfile dirfiles dirname root filename rest basefile)
9718 (while dirnames
9719 (setq dirname (substitute-in-file-name (car dirnames))
9720 dirnames (cdr dirnames))
9721 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ; root
9722 "\\([^/\\]*[*?][^/\\]*\\)" ; filename with *?
9723 "\\(.*\\)") ; rest
9724 dirname)
9725 (setq root (match-string 1 dirname)
9726 filename (match-string 2 dirname)
9727 rest (match-string 3 dirname)
9728 pattern filename)
9729 ;; now replace those * and ? with .+ and .
9730 ;; use ^ and /> to get only whole file names
9731 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9732 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
9733 pattern (concat "^" pattern "$")
9734 dirfiles (verilog-dir-files root))
9735 (while dirfiles
9736 (setq basefile (car dirfiles)
9737 dirfile (expand-file-name (concat root basefile rest))
9738 dirfiles (cdr dirfiles))
9739 (if (and (string-match pattern basefile)
9740 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9741 (not (equal basefile "."))
9742 (not (equal basefile ".."))
9743 (file-directory-p dirfile))
9744 (setq dirlist (cons dirfile dirlist)))))
9745 ;; Defaults
9746 (t
9747 (if (file-directory-p dirname)
9748 (setq dirlist (cons dirname dirlist))))))
9749 dirlist))
9750 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9751
9752 (defun verilog-library-filenames (filename &optional current check-ext)
9753 "Return a search path to find the given FILENAME or module name.
9754 Uses the optional CURRENT filename or variable `buffer-file-name', plus
9755 `verilog-library-directories' and `verilog-library-extensions'
9756 variables to build the path. With optional CHECK-EXT also check
9757 `verilog-library-extensions'."
9758 (unless current (setq current (buffer-file-name)))
9759 (unless verilog-dir-cache-preserving
9760 (setq verilog-dir-cache-lib-filenames nil))
9761 (let* ((cache-key (list filename current check-ext))
9762 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9763 chkdirs chkdir chkexts fn outlist)
9764 (cond (fass ; Return data from cache hit
9765 (nth 1 fass))
9766 (t
9767 ;; Note this expand can't be easily cached, as we need to
9768 ;; pick up buffer-local variables for newly read sub-module files
9769 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9770 (while chkdirs
9771 (setq chkdir (expand-file-name (car chkdirs)
9772 (file-name-directory current))
9773 chkexts (if check-ext verilog-library-extensions `("")))
9774 (while chkexts
9775 (setq fn (expand-file-name (concat filename (car chkexts))
9776 chkdir))
9777 ;;(message "Check for %s" fn)
9778 (if (verilog-dir-file-exists-p fn)
9779 (setq outlist (cons (expand-file-name
9780 fn (file-name-directory current))
9781 outlist)))
9782 (setq chkexts (cdr chkexts)))
9783 (setq chkdirs (cdr chkdirs)))
9784 (setq outlist (nreverse outlist))
9785 (setq verilog-dir-cache-lib-filenames
9786 (cons (list cache-key outlist)
9787 verilog-dir-cache-lib-filenames))
9788 outlist))))
9789
9790 (defun verilog-module-filenames (module current)
9791 "Return a search path to find the given MODULE name.
9792 Uses the CURRENT filename, `verilog-library-extensions',
9793 `verilog-library-directories' and `verilog-library-files'
9794 variables to build the path."
9795 ;; Return search locations for it
9796 (append (list current) ; first, current buffer
9797 (verilog-library-filenames module current t)
9798 verilog-library-files)) ; finally, any libraries
9799
9800 ;;
9801 ;; Module Information
9802 ;;
9803 ;; Many of these functions work on "modi" a module information structure
9804 ;; A modi is: [module-name-string file-name begin-point]
9805
9806 (defvar verilog-cache-enabled t
9807 "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
9808
9809 (defvar verilog-modi-cache-list nil
9810 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9811 For speeding up verilog-modi-get-* commands.
9812 Buffer-local.")
9813 (make-variable-buffer-local 'verilog-modi-cache-list)
9814
9815 (defvar verilog-modi-cache-preserve-tick nil
9816 "Modification tick after which the cache is still considered valid.
9817 Use `verilog-preserve-modi-cache' to set it.")
9818 (defvar verilog-modi-cache-preserve-buffer nil
9819 "Modification tick after which the cache is still considered valid.
9820 Use `verilog-preserve-modi-cache' to set it.")
9821 (defvar verilog-modi-cache-current-enable nil
9822 "Non-nil means allow caching `verilog-modi-current', set by let().")
9823 (defvar verilog-modi-cache-current nil
9824 "Currently active `verilog-modi-current', if any, set by let().")
9825 (defvar verilog-modi-cache-current-max nil
9826 "Current endmodule point for `verilog-modi-cache-current', if any.")
9827
9828 (defun verilog-modi-current ()
9829 "Return the modi structure for the module currently at point, possibly cached."
9830 (cond ((and verilog-modi-cache-current
9831 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9832 (<= (point) verilog-modi-cache-current-max))
9833 ;; Slow assertion, for debugging the cache:
9834 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9835 verilog-modi-cache-current)
9836 (verilog-modi-cache-current-enable
9837 (setq verilog-modi-cache-current (verilog-modi-current-get)
9838 verilog-modi-cache-current-max
9839 ;; The cache expires when we pass "endmodule" as then the
9840 ;; current modi may change to the next module
9841 ;; This relies on the AUTOs generally inserting, not deleting text
9842 (save-excursion
9843 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9844 verilog-modi-cache-current)
9845 (t
9846 (verilog-modi-current-get))))
9847
9848 (defun verilog-modi-current-get ()
9849 "Return the modi structure for the module currently at point."
9850 (let* (name type pt)
9851 ;; read current module's name
9852 (save-excursion
9853 (verilog-re-search-backward-quick verilog-defun-re nil nil)
9854 (setq type (match-string-no-properties 0))
9855 (verilog-re-search-forward-quick "(" nil nil)
9856 (setq name (verilog-read-module-name))
9857 (setq pt (point)))
9858 ;; return modi - note this vector built two places
9859 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
9860
9861 (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9862 (make-variable-buffer-local 'verilog-modi-lookup-cache)
9863 (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9864 (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
9865
9866 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9867 "Find the file and point at which MODULE is defined.
9868 If ALLOW-CACHE is set, check and remember cache of previous lookups.
9869 Return modi if successful, else print message unless IGNORE-ERROR is true."
9870 (let* ((current (or (buffer-file-name) (current-buffer)))
9871 modi)
9872 ;; Check cache
9873 ;;(message "verilog-modi-lookup: %s" module)
9874 (cond ((and verilog-modi-lookup-cache
9875 verilog-cache-enabled
9876 allow-cache
9877 (setq modi (gethash module verilog-modi-lookup-cache))
9878 (equal verilog-modi-lookup-last-current current)
9879 ;; If hit is in current buffer, then tick must match
9880 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9881 (not (equal current (verilog-modi-file-or-buffer modi)))))
9882 ;;(message "verilog-modi-lookup: HIT %S" modi)
9883 modi)
9884 ;; Miss
9885 (t (let* ((realname (verilog-symbol-detick module t))
9886 (orig-filenames (verilog-module-filenames realname current))
9887 (filenames orig-filenames)
9888 mif)
9889 (while (and filenames (not mif))
9890 (if (not (setq mif (verilog-module-inside-filename-p realname (car filenames))))
9891 (setq filenames (cdr filenames))))
9892 ;; mif has correct form to become later elements of modi
9893 (setq modi mif)
9894 (or mif ignore-error
9895 (error
9896 (concat
9897 "%s: Can't locate %s module definition%s"
9898 "\n Check the verilog-library-directories variable."
9899 "\n I looked in (if not listed, doesn't exist):\n\t%s")
9900 (verilog-point-text) module
9901 (if (not (equal module realname))
9902 (concat " (Expanded macro to " realname ")")
9903 "")
9904 (mapconcat 'concat orig-filenames "\n\t")))
9905 (when (eval-when-compile (fboundp 'make-hash-table))
9906 (unless verilog-modi-lookup-cache
9907 (setq verilog-modi-lookup-cache
9908 (make-hash-table :test 'equal :rehash-size 4.0)))
9909 (puthash module modi verilog-modi-lookup-cache))
9910 (setq verilog-modi-lookup-last-current current
9911 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9912 modi))
9913
9914 (defun verilog-modi-filename (modi)
9915 "Filename of MODI, or name of buffer if it's never been saved."
9916 (if (bufferp (verilog-modi-file-or-buffer modi))
9917 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9918 (buffer-name (verilog-modi-file-or-buffer modi)))
9919 (verilog-modi-file-or-buffer modi)))
9920
9921 (defun verilog-modi-goto (modi)
9922 "Move point/buffer to specified MODI."
9923 (or modi (error "Passed unfound modi to goto, check earlier"))
9924 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9925 (verilog-modi-file-or-buffer modi)
9926 (find-file-noselect (verilog-modi-file-or-buffer modi))))
9927 (or (equal major-mode `verilog-mode) ; Put into Verilog mode to get syntax
9928 (verilog-mode))
9929 (goto-char (verilog-modi-get-point modi)))
9930
9931 (defun verilog-goto-defun-file (module)
9932 "Move point to the file at which a given MODULE is defined."
9933 (interactive "sGoto File for Module: ")
9934 (let* ((modi (verilog-modi-lookup module nil)))
9935 (when modi
9936 (verilog-modi-goto modi)
9937 (switch-to-buffer (current-buffer)))))
9938
9939 (defun verilog-modi-cache-results (modi function)
9940 "Run on MODI the given FUNCTION. Locate the module in a file.
9941 Cache the output of function so next call may have faster access."
9942 (let (fass)
9943 (save-excursion ; Cache is buffer-local so can't avoid this.
9944 (verilog-modi-goto modi)
9945 (if (and (setq fass (assoc (list modi function)
9946 verilog-modi-cache-list))
9947 ;; Destroy caching when incorrect; Modified or file changed
9948 (not (and verilog-cache-enabled
9949 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
9950 (and verilog-modi-cache-preserve-tick
9951 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
9952 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
9953 (equal (visited-file-modtime) (nth 2 fass)))))
9954 (setq verilog-modi-cache-list nil
9955 fass nil))
9956 (cond (fass
9957 ;; Return data from cache hit
9958 (nth 3 fass))
9959 (t
9960 ;; Read from file
9961 ;; Clear then restore any highlighting to make emacs19 happy
9962 (let (func-returns)
9963 (verilog-save-font-mods
9964 (setq func-returns (funcall function)))
9965 ;; Cache for next time
9966 (setq verilog-modi-cache-list
9967 (cons (list (list modi function)
9968 (buffer-chars-modified-tick)
9969 (visited-file-modtime)
9970 func-returns)
9971 verilog-modi-cache-list))
9972 func-returns))))))
9973
9974 (defun verilog-modi-cache-add (modi function element sig-list)
9975 "Add function return results to the module cache.
9976 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
9977 function now contains the additional SIG-LIST parameters."
9978 (let (fass)
9979 (save-excursion
9980 (verilog-modi-goto modi)
9981 (if (setq fass (assoc (list modi function)
9982 verilog-modi-cache-list))
9983 (let ((func-returns (nth 3 fass)))
9984 (aset func-returns element
9985 (append sig-list (aref func-returns element))))))))
9986
9987 (defmacro verilog-preserve-modi-cache (&rest body)
9988 "Execute the BODY forms, allowing cache preservation within BODY.
9989 This means that changes to the buffer will not result in the cache being
9990 flushed. If the changes affect the modsig state, they must call the
9991 modsig-cache-add-* function, else the results of later calls may be
9992 incorrect. Without this, changes are assumed to be adding/removing signals
9993 and invalidating the cache."
9994 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
9995 (verilog-modi-cache-preserve-buffer (current-buffer)))
9996 (progn ,@body)))
9997
9998
9999 (defun verilog-modi-modport-lookup-one (modi name &optional ignore-error)
10000 "Given a MODI, return the declarations related to the given modport NAME.
10001 Report errors unless optional IGNORE-ERROR."
10002 ;; Recursive routine - see below
10003 (let* ((realname (verilog-symbol-detick name t))
10004 (modport (assoc name (verilog-decls-get-modports (verilog-modi-get-decls modi)))))
10005 (or modport ignore-error
10006 (error "%s: Can't locate %s modport definition%s"
10007 (verilog-point-text) name
10008 (if (not (equal name realname))
10009 (concat " (Expanded macro to " realname ")")
10010 "")))
10011 (let* ((decls (verilog-modport-decls modport))
10012 (clks (verilog-modport-clockings modport)))
10013 ;; Now expand any clocking's
10014 (while clks
10015 (setq decls (verilog-decls-append
10016 decls
10017 (verilog-modi-modport-lookup-one modi (car clks) ignore-error)))
10018 (setq clks (cdr clks)))
10019 decls)))
10020
10021 (defun verilog-modi-modport-lookup (modi name-re &optional ignore-error)
10022 "Given a MODI, return the declarations related to the given modport NAME-RE.
10023 If the modport points to any clocking blocks, expand the signals to include
10024 those clocking block's signals."
10025 ;; Recursive routine - see below
10026 (let* ((mod-decls (verilog-modi-get-decls modi))
10027 (clks (verilog-decls-get-modports mod-decls))
10028 (name-re (concat "^" name-re "$"))
10029 (decls (verilog-decls-new nil nil nil nil nil nil nil nil nil)))
10030 ;; Pull in all modports
10031 (while clks
10032 (when (string-match name-re (verilog-modport-name (car clks)))
10033 (setq decls (verilog-decls-append
10034 decls
10035 (verilog-modi-modport-lookup-one modi (verilog-modport-name (car clks)) ignore-error))))
10036 (setq clks (cdr clks)))
10037 decls))
10038
10039 (defun verilog-signals-matching-enum (in-list enum)
10040 "Return all signals in IN-LIST matching the given ENUM."
10041 (let (out-list)
10042 (while in-list
10043 (if (equal (verilog-sig-enum (car in-list)) enum)
10044 (setq out-list (cons (car in-list) out-list)))
10045 (setq in-list (cdr in-list)))
10046 ;; New scheme
10047 ;; Namespace intentionally short for AUTOs and compatibility
10048 (let* ((enumvar (intern (concat "venum-" enum)))
10049 (enumlist (and (boundp enumvar) (eval enumvar))))
10050 (while enumlist
10051 (add-to-list 'out-list (list (car enumlist)))
10052 (setq enumlist (cdr enumlist))))
10053 (nreverse out-list)))
10054
10055 (defun verilog-signals-matching-regexp (in-list regexp)
10056 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
10057 (if (or (not regexp) (equal regexp ""))
10058 in-list
10059 (let ((case-fold-search verilog-case-fold)
10060 out-list)
10061 (while in-list
10062 (if (string-match regexp (verilog-sig-name (car in-list)))
10063 (setq out-list (cons (car in-list) out-list)))
10064 (setq in-list (cdr in-list)))
10065 (nreverse out-list))))
10066
10067 (defun verilog-signals-not-matching-regexp (in-list regexp)
10068 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
10069 (if (or (not regexp) (equal regexp ""))
10070 in-list
10071 (let ((case-fold-search verilog-case-fold)
10072 out-list)
10073 (while in-list
10074 (if (not (string-match regexp (verilog-sig-name (car in-list))))
10075 (setq out-list (cons (car in-list) out-list)))
10076 (setq in-list (cdr in-list)))
10077 (nreverse out-list))))
10078
10079 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
10080 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
10081 if non-nil."
10082 (if (or (not regexp) (equal regexp ""))
10083 in-list
10084 (let (out-list to-match)
10085 (while in-list
10086 ;; Note verilog-insert-one-definition matches on this order
10087 (setq to-match (concat
10088 decl-type
10089 " " (verilog-sig-signed (car in-list))
10090 " " (verilog-sig-multidim (car in-list))
10091 (verilog-sig-bits (car in-list))))
10092 (if (string-match regexp to-match)
10093 (setq out-list (cons (car in-list) out-list)))
10094 (setq in-list (cdr in-list)))
10095 (nreverse out-list))))
10096
10097 (defun verilog-signals-edit-wire-reg (in-list)
10098 "Return all signals in IN-LIST with wire/reg data types made blank."
10099 (mapcar (lambda (sig)
10100 (when (member (verilog-sig-type sig) '("wire" "reg"))
10101 (verilog-sig-type-set sig nil))
10102 sig) in-list))
10103
10104 ;; Combined
10105 (defun verilog-decls-get-signals (decls)
10106 "Return all declared signals in DECLS, excluding `assign' statements."
10107 (append
10108 (verilog-decls-get-outputs decls)
10109 (verilog-decls-get-inouts decls)
10110 (verilog-decls-get-inputs decls)
10111 (verilog-decls-get-vars decls)
10112 (verilog-decls-get-consts decls)
10113 (verilog-decls-get-gparams decls)))
10114
10115 (defun verilog-decls-get-ports (decls)
10116 (append
10117 (verilog-decls-get-outputs decls)
10118 (verilog-decls-get-inouts decls)
10119 (verilog-decls-get-inputs decls)))
10120
10121 (defun verilog-decls-get-iovars (decls)
10122 (append
10123 (verilog-decls-get-vars decls)
10124 (verilog-decls-get-outputs decls)
10125 (verilog-decls-get-inouts decls)
10126 (verilog-decls-get-inputs decls)))
10127
10128 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
10129 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
10130 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
10131 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
10132 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
10133 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
10134 (defsubst verilog-modi-cache-add-vars (modi sig-list)
10135 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
10136 (defsubst verilog-modi-cache-add-gparams (modi sig-list)
10137 (verilog-modi-cache-add modi 'verilog-read-decls 7 sig-list))
10138
10139 \f
10140 ;;; Auto creation utilities:
10141 ;;
10142
10143 (defun verilog-auto-re-search-do (search-for func)
10144 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
10145 (goto-char (point-min))
10146 (while (verilog-re-search-forward-quick search-for nil t)
10147 (funcall func)))
10148
10149 (defun verilog-insert-one-definition (sig type indent-pt)
10150 "Print out a definition for SIG of the given TYPE,
10151 with appropriate INDENT-PT indentation."
10152 (indent-to indent-pt)
10153 ;; Note verilog-signals-matching-dir-re matches on this order
10154 (insert type)
10155 (when (verilog-sig-modport sig)
10156 (insert "." (verilog-sig-modport sig)))
10157 (when (verilog-sig-signed sig)
10158 (insert " " (verilog-sig-signed sig)))
10159 (when (verilog-sig-multidim sig)
10160 (insert " " (verilog-sig-multidim-string sig)))
10161 (when (verilog-sig-bits sig)
10162 (insert " " (verilog-sig-bits sig)))
10163 (indent-to (max 24 (+ indent-pt 16)))
10164 (unless (= (char-syntax (preceding-char)) ?\ )
10165 (insert " ")) ; Need space between "]name" if indent-to did nothing
10166 (insert (verilog-sig-name sig))
10167 (when (verilog-sig-memory sig)
10168 (insert " " (verilog-sig-memory sig))))
10169
10170 (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
10171 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
10172 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
10173 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
10174 When MODI is non-null, also add to modi-cache, for tracking."
10175 (when modi
10176 (cond ((equal direction "wire")
10177 (verilog-modi-cache-add-vars modi sigs))
10178 ((equal direction "reg")
10179 (verilog-modi-cache-add-vars modi sigs))
10180 ((equal direction "output")
10181 (verilog-modi-cache-add-outputs modi sigs)
10182 (when verilog-auto-declare-nettype
10183 (verilog-modi-cache-add-vars modi sigs)))
10184 ((equal direction "input")
10185 (verilog-modi-cache-add-inputs modi sigs)
10186 (when verilog-auto-declare-nettype
10187 (verilog-modi-cache-add-vars modi sigs)))
10188 ((equal direction "inout")
10189 (verilog-modi-cache-add-inouts modi sigs)
10190 (when verilog-auto-declare-nettype
10191 (verilog-modi-cache-add-vars modi sigs)))
10192 ((equal direction "interface"))
10193 ((equal direction "parameter")
10194 (verilog-modi-cache-add-gparams modi sigs))
10195 (t
10196 (error "Unsupported verilog-insert-definition direction: %s" direction))))
10197 (or dont-sort
10198 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10199 (while sigs
10200 (let ((sig (car sigs)))
10201 (verilog-insert-one-definition
10202 sig
10203 ;; Want "type x" or "output type x", not "wire type x"
10204 (cond ((or (verilog-sig-type sig)
10205 verilog-auto-wire-type)
10206 (concat
10207 (when (member direction '("input" "output" "inout"))
10208 (concat direction " "))
10209 (or (verilog-sig-type sig)
10210 verilog-auto-wire-type)))
10211 ((and verilog-auto-declare-nettype
10212 (member direction '("input" "output" "inout")))
10213 (concat direction " " verilog-auto-declare-nettype))
10214 (t
10215 direction))
10216 indent-pt)
10217 (insert (if v2k "," ";"))
10218 (if (or (not (verilog-sig-comment sig))
10219 (equal "" (verilog-sig-comment sig)))
10220 (insert "\n")
10221 (indent-to (max 48 (+ indent-pt 40)))
10222 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
10223 (setq sigs (cdr sigs)))))
10224
10225 (eval-when-compile
10226 (if (not (boundp 'indent-pt))
10227 (defvar indent-pt nil "Local used by insert-indent")))
10228
10229 (defun verilog-insert-indent (&rest stuff)
10230 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
10231 Presumes that any newlines end a list element."
10232 (let ((need-indent t))
10233 (while stuff
10234 (if need-indent (indent-to indent-pt))
10235 (setq need-indent nil)
10236 (verilog-insert (car stuff))
10237 (setq need-indent (string-match "\n$" (car stuff))
10238 stuff (cdr stuff)))))
10239 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
10240
10241 (defun verilog-forward-or-insert-line ()
10242 "Move forward a line, unless at EOB, then insert a newline."
10243 (if (eobp) (insert "\n")
10244 (forward-line)))
10245
10246 (defun verilog-repair-open-comma ()
10247 "Insert comma if previous argument is other than an open parenthesis or endif."
10248 ;; We can't just search backward for ) as it might be inside another expression.
10249 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
10250 (save-excursion
10251 (verilog-backward-syntactic-ws-quick)
10252 (when (and (not (save-excursion ; Not beginning (, or existing ,
10253 (backward-char 1)
10254 (looking-at "[(,]")))
10255 (not (save-excursion ; Not `endif, or user define
10256 (backward-char 1)
10257 (skip-chars-backward "[a-zA-Z0-9_`]")
10258 (looking-at "`"))))
10259 (insert ","))))
10260
10261 (defun verilog-repair-close-comma ()
10262 "If point is at a comma followed by a close parenthesis, fix it.
10263 This repairs those mis-inserted by an AUTOARG."
10264 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
10265 (save-excursion
10266 (verilog-forward-close-paren)
10267 (backward-char 1)
10268 (verilog-backward-syntactic-ws-quick)
10269 (backward-char 1)
10270 (when (looking-at ",")
10271 (delete-char 1))))
10272
10273 (defun verilog-make-width-expression (range-exp)
10274 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
10275 ;; strip off the []
10276 (cond ((not range-exp)
10277 "1")
10278 (t
10279 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
10280 (setq range-exp (match-string 1 range-exp)))
10281 (cond ((not range-exp)
10282 "1")
10283 ;; [#:#] We can compute a numeric result
10284 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
10285 range-exp)
10286 (int-to-string
10287 (1+ (abs (- (string-to-number (match-string 1 range-exp))
10288 (string-to-number (match-string 2 range-exp)))))))
10289 ;; [PARAM-1:0] can just return PARAM
10290 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
10291 (match-string 1 range-exp))
10292 ;; [arbitrary] need math
10293 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
10294 (concat "(1+(" (match-string 1 range-exp) ")"
10295 (if (equal "0" (match-string 2 range-exp))
10296 "" ; Don't bother with -(0)
10297 (concat "-(" (match-string 2 range-exp) ")"))
10298 ")"))
10299 (t nil)))))
10300 ;;(verilog-make-width-expression "`A:`B")
10301
10302 (defun verilog-simplify-range-expression (expr)
10303 "Return a simplified range expression with constants eliminated from EXPR."
10304 ;; Note this is always called with brackets; ie [z] or [z:z]
10305 (if (not (string-match "[---+*()]" expr))
10306 expr ; short-circuit
10307 (let ((out expr)
10308 (last-pass ""))
10309 (while (not (equal last-pass out))
10310 (setq last-pass out)
10311 ;; Prefix regexp needs beginning of match, or some symbol of
10312 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
10313 ;; Ditto the end.
10314 (while (string-match
10315 (concat "\\([[({:*+-]\\)" ; - must be last
10316 "(\\<\\([0-9A-Za-z_]+\\))"
10317 "\\([])}:*+-]\\)")
10318 out)
10319 (setq out (replace-match "\\1\\2\\3" nil nil out)))
10320 (while (string-match
10321 (concat "\\([[({:*+-]\\)" ; - must be last
10322 "\\$clog2\\s *(\\<\\([0-9]+\\))"
10323 "\\([])}:*+-]\\)")
10324 out)
10325 (setq out (replace-match
10326 (concat
10327 (match-string 1 out)
10328 (int-to-string (verilog-clog2 (string-to-number (match-string 2 out))))
10329 (match-string 3 out))
10330 nil nil out)))
10331 ;; For precedence do * before +/-
10332 (while (string-match
10333 (concat "\\([[({:*+-]\\)"
10334 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
10335 "\\([])}:*+-]\\)")
10336 out)
10337 (setq out (replace-match
10338 (concat (match-string 1 out)
10339 (int-to-string (* (string-to-number (match-string 2 out))
10340 (string-to-number (match-string 4 out))))
10341 (match-string 5 out))
10342 nil nil out)))
10343 (while (string-match
10344 (concat "\\([[({:+-]\\)" ; No * here as higher prec
10345 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
10346 "\\([])}:+-]\\)")
10347 out)
10348 (let ((pre (match-string 1 out))
10349 (lhs (string-to-number (match-string 2 out)))
10350 (rhs (string-to-number (match-string 4 out)))
10351 (post (match-string 5 out))
10352 val)
10353 (when (equal pre "-")
10354 (setq lhs (- lhs)))
10355 (setq val (if (equal (match-string 3 out) "-")
10356 (- lhs rhs)
10357 (+ lhs rhs))
10358 out (replace-match
10359 (concat (if (and (equal pre "-")
10360 (< val 0))
10361 "" ; Not "--20" but just "-20"
10362 pre)
10363 (int-to-string val)
10364 post)
10365 nil nil out)) )))
10366 out)))
10367
10368 ;;(verilog-simplify-range-expression "[1:3]") ; 1
10369 ;;(verilog-simplify-range-expression "[(1):3]") ; 1
10370 ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ; 20
10371 ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ; 48
10372 ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ; FOO*4-2
10373 ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ; FOO*4+0
10374 ;;(verilog-simplify-range-expression "[(func(BAR))]") ; func(BAR)
10375 ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ; FOO-0
10376 ;;(verilog-simplify-range-expression "[$clog2(2)]") ; 1
10377 ;;(verilog-simplify-range-expression "[$clog2(7)]") ; 3
10378
10379 (defun verilog-clog2 (value)
10380 "Compute $clog2 - ceiling log2 of VALUE."
10381 (if (< value 1)
10382 0
10383 (ceiling (/ (log value) (log 2)))))
10384
10385 (defun verilog-typedef-name-p (variable-name)
10386 "Return true if the VARIABLE-NAME is a type definition."
10387 (when verilog-typedef-regexp
10388 (verilog-string-match-fold verilog-typedef-regexp variable-name)))
10389 \f
10390 ;;; Auto deletion:
10391 ;;
10392
10393 (defun verilog-delete-autos-lined ()
10394 "Delete autos that occupy multiple lines, between begin and end comments."
10395 ;; The newline must not have a comment property, so we must
10396 ;; delete the end auto's newline, not the first newline
10397 (forward-line 1)
10398 (let ((pt (point)))
10399 (when (and
10400 (looking-at "\\s-*// Beginning")
10401 (search-forward "// End of automatic" nil t))
10402 ;; End exists
10403 (end-of-line)
10404 (forward-line 1)
10405 (delete-region pt (point)))))
10406
10407 (defun verilog-delete-empty-auto-pair ()
10408 "Delete begin/end auto pair at point, if empty."
10409 (forward-line 0)
10410 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
10411 "\\s-*// End of automatics\n"))
10412 (delete-region (point) (save-excursion (forward-line 2) (point)))))
10413
10414 (defun verilog-forward-close-paren ()
10415 "Find the close parenthesis that match the current point.
10416 Ignore other close parenthesis with matching open parens."
10417 (let ((parens 1))
10418 (while (> parens 0)
10419 (unless (verilog-re-search-forward-quick "[()]" nil t)
10420 (error "%s: Mismatching ()" (verilog-point-text)))
10421 (cond ((= (preceding-char) ?\( )
10422 (setq parens (1+ parens)))
10423 ((= (preceding-char) ?\) )
10424 (setq parens (1- parens)))))))
10425
10426 (defun verilog-backward-open-paren ()
10427 "Find the open parenthesis that match the current point.
10428 Ignore other open parenthesis with matching close parens."
10429 (let ((parens 1))
10430 (while (> parens 0)
10431 (unless (verilog-re-search-backward-quick "[()]" nil t)
10432 (error "%s: Mismatching ()" (verilog-point-text)))
10433 (cond ((= (following-char) ?\) )
10434 (setq parens (1+ parens)))
10435 ((= (following-char) ?\( )
10436 (setq parens (1- parens)))))))
10437
10438 (defun verilog-backward-open-bracket ()
10439 "Find the open bracket that match the current point.
10440 Ignore other open bracket with matching close bracket."
10441 (let ((parens 1))
10442 (while (> parens 0)
10443 (unless (verilog-re-search-backward-quick "[][]" nil t)
10444 (error "%s: Mismatching []" (verilog-point-text)))
10445 (cond ((= (following-char) ?\] )
10446 (setq parens (1+ parens)))
10447 ((= (following-char) ?\[ )
10448 (setq parens (1- parens)))))))
10449
10450 (defun verilog-delete-to-paren ()
10451 "Delete the automatic inst/sense/arg created by autos.
10452 Deletion stops at the matching end parenthesis, outside comments."
10453 (delete-region (point)
10454 (save-excursion
10455 (verilog-backward-open-paren)
10456 (verilog-forward-sexp-ign-cmt 1) ; Moves to paren that closes argdecl's
10457 (backward-char 1)
10458 (point))))
10459
10460 (defun verilog-auto-star-safe ()
10461 "Return if a .* AUTOINST is safe to delete or expand.
10462 It was created by the AUTOS themselves, or by the user."
10463 (and verilog-auto-star-expand
10464 (looking-at
10465 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
10466
10467 (defun verilog-delete-auto-star-all ()
10468 "Delete a .* AUTOINST, if it is safe."
10469 (when (verilog-auto-star-safe)
10470 (verilog-delete-to-paren)))
10471
10472 (defun verilog-delete-auto-star-implicit ()
10473 "Delete all .* implicit connections created by `verilog-auto-star'.
10474 This function will be called automatically at save unless
10475 `verilog-auto-star-save' is set, any non-templated expanded pins will be
10476 removed."
10477 (interactive)
10478 (let (paren-pt indent have-close-paren)
10479 (save-excursion
10480 (goto-char (point-min))
10481 ;; We need to match these even outside of comments.
10482 ;; For reasonable performance, we don't check if inside comments, sorry.
10483 (while (re-search-forward "// Implicit \\.\\*" nil t)
10484 (setq paren-pt (point))
10485 (beginning-of-line)
10486 (setq have-close-paren
10487 (save-excursion
10488 (when (search-forward ");" paren-pt t)
10489 (setq indent (current-indentation))
10490 t)))
10491 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
10492 (when have-close-paren
10493 ;; Delete extra commentary
10494 (save-excursion
10495 (while (progn
10496 (forward-line -1)
10497 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
10498 (delete-region (match-beginning 0) (match-end 0))))
10499 ;; If it is simple, we can put the ); on the same line as the last text
10500 (let ((rtn-pt (point)))
10501 (save-excursion
10502 (while (progn (backward-char 1)
10503 (looking-at "[ \t\n\f]")))
10504 (when (looking-at ",")
10505 (delete-region (+ 1 (point)) rtn-pt))))
10506 (when (bolp)
10507 (indent-to indent))
10508 (insert ");\n")
10509 ;; Still need to kill final comma - always is one as we put one after the .*
10510 (re-search-backward ",")
10511 (delete-char 1))))))
10512
10513 (defun verilog-delete-auto ()
10514 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
10515 Use \\[verilog-auto] to re-insert the updated AUTOs.
10516
10517 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
10518 called before and after this function, respectively."
10519 (interactive)
10520 (save-excursion
10521 (if (buffer-file-name)
10522 (find-file-noselect (buffer-file-name))) ; To check we have latest version
10523 (verilog-save-no-change-functions
10524 (verilog-save-scan-cache
10525 ;; Allow user to customize
10526 (verilog-run-hooks 'verilog-before-delete-auto-hook)
10527
10528 ;; Remove those that have multi-line insertions, possibly with parameters
10529 ;; We allow anything beginning with AUTO, so that users can add their own
10530 ;; patterns
10531 (verilog-auto-re-search-do
10532 (concat "/\\*AUTO[A-Za-z0-9_]+"
10533 ;; Optional parens or quoted parameter or .* for (((...)))
10534 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
10535 "\\*/")
10536 'verilog-delete-autos-lined)
10537 ;; Remove those that are in parenthesis
10538 (verilog-auto-re-search-do
10539 (concat "/\\*"
10540 (eval-when-compile
10541 (verilog-regexp-words
10542 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
10543 "AUTOSENSE")))
10544 "\\*/")
10545 'verilog-delete-to-paren)
10546 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
10547 (verilog-auto-re-search-do "\\.\\*"
10548 'verilog-delete-auto-star-all)
10549 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
10550 (goto-char (point-min))
10551 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
10552 (replace-match ""))
10553
10554 ;; Final customize
10555 (verilog-run-hooks 'verilog-delete-auto-hook)))))
10556 \f
10557 ;;; Auto inject:
10558 ;;
10559
10560 (defun verilog-inject-auto ()
10561 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
10562
10563 Any always @ blocks with sensitivity lists that match computed lists will
10564 be replaced with /*AS*/ comments.
10565
10566 Any cells will get /*AUTOINST*/ added to the end of the pin list.
10567 Pins with have identical names will be deleted.
10568
10569 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
10570 support adding new ports. You may wish to delete older ports yourself.
10571
10572 For example:
10573
10574 module ExampInject (i, o);
10575 input i;
10576 input j;
10577 output o;
10578 always @ (i or j)
10579 o = i | j;
10580 InstModule instName
10581 (.foobar(baz),
10582 j(j));
10583 endmodule
10584
10585 Typing \\[verilog-inject-auto] will make this into:
10586
10587 module ExampInject (i, o/*AUTOARG*/
10588 // Inputs
10589 j);
10590 input i;
10591 output o;
10592 always @ (/*AS*/i or j)
10593 o = i | j;
10594 InstModule instName
10595 (.foobar(baz),
10596 /*AUTOINST*/
10597 // Outputs
10598 j(j));
10599 endmodule"
10600 (interactive)
10601 (verilog-auto t))
10602
10603 (defun verilog-inject-arg ()
10604 "Inject AUTOARG into new code. See `verilog-inject-auto'."
10605 ;; Presume one module per file.
10606 (save-excursion
10607 (goto-char (point-min))
10608 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
10609 (let ((endmodp (save-excursion
10610 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
10611 (point))))
10612 ;; See if there's already a comment .. inside a comment so not verilog-re-search
10613 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
10614 (verilog-re-search-forward-quick ";" nil t)
10615 (backward-char 1)
10616 (verilog-backward-syntactic-ws-quick)
10617 (backward-char 1) ; Moves to paren that closes argdecl's
10618 (when (looking-at ")")
10619 (verilog-insert "/*AUTOARG*/")))))))
10620
10621 (defun verilog-inject-sense ()
10622 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
10623 (save-excursion
10624 (goto-char (point-min))
10625 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
10626 (let* ((start-pt (point))
10627 (modi (verilog-modi-current))
10628 (moddecls (verilog-modi-get-decls modi))
10629 pre-sigs
10630 got-sigs)
10631 (backward-char 1)
10632 (verilog-forward-sexp-ign-cmt 1)
10633 (backward-char 1) ; End )
10634 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
10635 (setq pre-sigs (verilog-signals-from-signame
10636 (verilog-read-signals start-pt (point)))
10637 got-sigs (verilog-auto-sense-sigs moddecls nil))
10638 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
10639 (verilog-signals-not-in got-sigs pre-sigs)))
10640 (delete-region start-pt (point))
10641 (verilog-insert "/*AS*/")))))))
10642
10643 (defun verilog-inject-inst ()
10644 "Inject AUTOINST into new code. See `verilog-inject-auto'."
10645 (save-excursion
10646 (goto-char (point-min))
10647 ;; It's hard to distinguish modules; we'll instead search for pins.
10648 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_$]+\\s *(\\s *[a-zA-Z0-9`_$]+\\s *)" nil t)
10649 (verilog-backward-open-paren) ; Inst start
10650 (cond
10651 ((= (preceding-char) ?\#) ; #(...) parameter section, not pin. Skip.
10652 (forward-char 1)
10653 (verilog-forward-close-paren)) ; Parameters done
10654 (t
10655 (forward-char 1)
10656 (let ((indent-pt (+ (current-column)))
10657 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
10658 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
10659 (goto-char end-pt)) ; Already there, continue search with next instance
10660 (t
10661 ;; Delete identical interconnect
10662 (let ((case-fold-search nil)) ; So we don't convert upper-to-lower, etc
10663 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
10664 (delete-region (match-beginning 0) (match-end 0))
10665 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ; Keep it correct
10666 (while (or (looking-at "[ \t\n\f,]+")
10667 (looking-at "//[^\n]*"))
10668 (delete-region (match-beginning 0) (match-end 0))
10669 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
10670 (verilog-forward-close-paren)
10671 (backward-char 1)
10672 ;; Not verilog-re-search, as we don't want to strip comments
10673 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
10674 (delete-region (match-beginning 0) (match-end 0)))
10675 (verilog-insert "\n")
10676 (verilog-insert-indent "/*AUTOINST*/")))))))))
10677
10678 ;;
10679 ;; Auto diff:
10680 ;;
10681
10682 (defun verilog-diff-buffers-p (b1 b2 &optional whitespace)
10683 "Return nil if buffers B1 and B2 have same contents.
10684 Else, return point in B1 that first mismatches.
10685 If optional WHITESPACE true, ignore whitespace."
10686 (save-excursion
10687 (let* ((case-fold-search nil) ; compare-buffer-substrings cares
10688 (p1 (with-current-buffer b1 (goto-char (point-min))))
10689 (p2 (with-current-buffer b2 (goto-char (point-min))))
10690 (maxp1 (with-current-buffer b1 (point-max)))
10691 (maxp2 (with-current-buffer b2 (point-max)))
10692 (op1 -1) (op2 -1)
10693 progress size)
10694 (while (not (and (eq p1 op1) (eq p2 op2)))
10695 ;; If both windows have whitespace optionally skip over it.
10696 (when whitespace
10697 ;; skip-syntax-* doesn't count \n
10698 (with-current-buffer b1
10699 (goto-char p1)
10700 (skip-chars-forward " \t\n\r\f\v")
10701 (setq p1 (point)))
10702 (with-current-buffer b2
10703 (goto-char p2)
10704 (skip-chars-forward " \t\n\r\f\v")
10705 (setq p2 (point))))
10706 (setq size (min (- maxp1 p1) (- maxp2 p2)))
10707 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
10708 b1 p1 (+ size p1)))
10709 (setq progress (if (zerop progress) size (1- (abs progress))))
10710 (setq op1 p1 op2 p2
10711 p1 (+ p1 progress)
10712 p2 (+ p2 progress)))
10713 ;; Return value
10714 (if (and (eq p1 maxp1) (eq p2 maxp2))
10715 nil p1))))
10716
10717 (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
10718 "View the differences between file F1 and buffer B2.
10719 This requires the external program `diff-command' to be in your `exec-path',
10720 and uses `diff-switches' in which you may want to have \"-u\" flag.
10721 Ignores WHITESPACE if t, and writes output to stdout if SHOW."
10722 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
10723 ;; call `diff' as `diff' has different calling semantics on different
10724 ;; versions of Emacs.
10725 (if (not (file-exists-p f1))
10726 (message "Buffer %s has no associated file on disc" (buffer-name b2))
10727 (with-temp-buffer "*Verilog-Diff*"
10728 (let ((outbuf (current-buffer))
10729 (f2 (make-temp-file "vm-diff-auto-")))
10730 (unwind-protect
10731 (progn
10732 (with-current-buffer b2
10733 (save-restriction
10734 (widen)
10735 (write-region (point-min) (point-max) f2 nil 'nomessage)))
10736 (call-process diff-command nil outbuf t
10737 diff-switches ; User may want -u in diff-switches
10738 (if whitespace "-b" "")
10739 f1 f2)
10740 ;; Print out results. Alternatively we could have call-processed
10741 ;; ourself, but this way we can reuse diff switches
10742 (when show
10743 (with-current-buffer outbuf (message "%s" (buffer-string))))))
10744 (sit-for 0)
10745 (when (file-exists-p f2)
10746 (delete-file f2))))))
10747
10748 (defun verilog-diff-report (b1 b2 diffpt)
10749 "Report differences detected with `verilog-diff-auto'.
10750 Differences are between buffers B1 and B2, starting at point
10751 DIFFPT. This function is called via `verilog-diff-function'."
10752 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
10753 (verilog-warn "%s:%d: Difference in AUTO expansion found"
10754 name1 (with-current-buffer b1
10755 (count-lines (point-min) diffpt)))
10756 (cond (noninteractive
10757 (verilog-diff-file-with-buffer name1 b2 t t))
10758 (t
10759 (ediff-buffers b1 b2)))))
10760
10761 (defun verilog-diff-auto ()
10762 "Expand AUTOs in a temporary buffer and indicate any change.
10763 Whitespace is ignored when detecting differences, but once a
10764 difference is detected, whitespace differences may be shown.
10765
10766 To call this from the command line, see \\[verilog-batch-diff-auto].
10767
10768 The action on differences is selected with
10769 `verilog-diff-function'. The default is `verilog-diff-report'
10770 which will report an error and run `ediff' in interactive mode,
10771 or `diff' in batch mode."
10772 (interactive)
10773 (let ((b1 (current-buffer)) b2 diffpt
10774 (name1 (buffer-file-name))
10775 (newname "*Verilog-Diff*"))
10776 (save-excursion
10777 (when (get-buffer newname)
10778 (kill-buffer newname))
10779 (setq b2 (let (buffer-file-name) ; Else clone is upset
10780 (clone-buffer newname)))
10781 (with-current-buffer b2
10782 ;; auto requires the filename, but can't have same filename in two
10783 ;; buffers; so override both b1 and b2's names
10784 (let ((buffer-file-name name1))
10785 (unwind-protect
10786 (progn
10787 (with-current-buffer b1 (setq buffer-file-name nil))
10788 (verilog-auto)
10789 (when (not verilog-auto-star-save)
10790 (verilog-delete-auto-star-implicit)))
10791 ;; Restore name if unwind
10792 (with-current-buffer b1 (setq buffer-file-name name1)))))
10793 ;;
10794 (setq diffpt (verilog-diff-buffers-p b1 b2 t))
10795 (cond ((not diffpt)
10796 (unless noninteractive (message "AUTO expansion identical"))
10797 (kill-buffer newname)) ; Nice to cleanup after oneself
10798 (t
10799 (funcall verilog-diff-function b1 b2 diffpt)))
10800 ;; Return result of compare
10801 diffpt)))
10802
10803 ;;
10804 ;; Auto save
10805 ;;
10806
10807 (defun verilog-auto-save-check ()
10808 "On saving see if we need auto update."
10809 (cond ((not verilog-auto-save-policy)) ; disabled
10810 ((not (save-excursion
10811 (save-match-data
10812 (let ((case-fold-search nil))
10813 (goto-char (point-min))
10814 (re-search-forward "AUTO" nil t))))))
10815 ((eq verilog-auto-save-policy 'force)
10816 (verilog-auto))
10817 ((not (buffer-modified-p)))
10818 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
10819 ((eq verilog-auto-save-policy 'detect)
10820 (verilog-auto))
10821 (t
10822 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10823 (verilog-auto))
10824 ;; Don't ask again if didn't update
10825 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
10826 (when (not verilog-auto-star-save)
10827 (verilog-delete-auto-star-implicit))
10828 nil) ; Always return nil -- we don't write the file ourselves
10829
10830 (defun verilog-auto-read-locals ()
10831 "Return file local variable segment at bottom of file."
10832 (save-excursion
10833 (goto-char (point-max))
10834 (if (re-search-backward "Local Variables:" nil t)
10835 (buffer-substring-no-properties (point) (point-max))
10836 "")))
10837
10838 (defun verilog-auto-reeval-locals (&optional force)
10839 "Read file local variable segment at bottom of file if it has changed.
10840 If FORCE, always reread it."
10841 (let ((curlocal (verilog-auto-read-locals)))
10842 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
10843 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
10844 ;; Note this may cause this function to be recursively invoked,
10845 ;; because hack-local-variables may call (verilog-mode)
10846 ;; The above when statement will prevent it from recursing forever.
10847 (hack-local-variables)
10848 t)))
10849 \f
10850 ;;; Auto creation:
10851 ;;
10852
10853 (defun verilog-auto-arg-ports (sigs message indent-pt)
10854 "Print a list of ports for AUTOARG.
10855 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10856 (when sigs
10857 (when verilog-auto-arg-sort
10858 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10859 (insert "\n")
10860 (indent-to indent-pt)
10861 (insert message)
10862 (insert "\n")
10863 (let ((space ""))
10864 (indent-to indent-pt)
10865 (while sigs
10866 (cond ((equal verilog-auto-arg-format 'single)
10867 (insert space)
10868 (indent-to indent-pt)
10869 (setq space "\n"))
10870 ;; verilog-auto-arg-format 'packed
10871 ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10872 (insert "\n")
10873 (indent-to indent-pt)
10874 (setq space " "))
10875 (t
10876 (insert space)
10877 (setq space " ")))
10878 (insert (verilog-sig-name (car sigs)) ",")
10879 (setq sigs (cdr sigs))))))
10880
10881 (defun verilog-auto-arg ()
10882 "Expand AUTOARG statements.
10883 Replace the argument declarations at the beginning of the
10884 module with ones automatically derived from input and output
10885 statements. This can be dangerous if the module is instantiated
10886 using position-based connections, so use only name-based when
10887 instantiating the resulting module. Long lines are split based
10888 on the `fill-column', see \\[set-fill-column].
10889
10890 Limitations:
10891 Concatenation and outputting partial buses is not supported.
10892
10893 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10894
10895 For example:
10896
10897 module ExampArg (/*AUTOARG*/);
10898 input i;
10899 output o;
10900 endmodule
10901
10902 Typing \\[verilog-auto] will make this into:
10903
10904 module ExampArg (/*AUTOARG*/
10905 // Outputs
10906 o,
10907 // Inputs
10908 i
10909 );
10910 input i;
10911 output o;
10912 endmodule
10913
10914 The argument declarations may be printed in declaration order to
10915 best suit order based instantiations, or alphabetically, based on
10916 the `verilog-auto-arg-sort' variable.
10917
10918 Formatting is controlled with `verilog-auto-arg-format' variable.
10919
10920 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10921 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
10922 conservative guess on adding a comma for the first signal, if you have
10923 any ifdefs or complicated expressions before the AUTOARG you will need
10924 to choose the comma yourself.
10925
10926 Avoid declaring ports manually, as it makes code harder to maintain."
10927 (save-excursion
10928 (let* ((modi (verilog-modi-current))
10929 (moddecls (verilog-modi-get-decls modi))
10930 (skip-pins (aref (verilog-read-arg-pins) 0)))
10931 (verilog-repair-open-comma)
10932 (verilog-auto-arg-ports (verilog-signals-not-in
10933 (verilog-decls-get-outputs moddecls)
10934 skip-pins)
10935 "// Outputs"
10936 verilog-indent-level-declaration)
10937 (verilog-auto-arg-ports (verilog-signals-not-in
10938 (verilog-decls-get-inouts moddecls)
10939 skip-pins)
10940 "// Inouts"
10941 verilog-indent-level-declaration)
10942 (verilog-auto-arg-ports (verilog-signals-not-in
10943 (verilog-decls-get-inputs moddecls)
10944 skip-pins)
10945 "// Inputs"
10946 verilog-indent-level-declaration)
10947 (verilog-repair-close-comma)
10948 (unless (eq (char-before) ?/ )
10949 (insert "\n"))
10950 (indent-to verilog-indent-level-declaration))))
10951
10952 (defun verilog-auto-assign-modport ()
10953 "Expand AUTOASSIGNMODPORT statements, as part of \\[verilog-auto].
10954 Take input/output/inout statements from the specified interface
10955 and modport and use to build assignments into the modport, for
10956 making verification modules that connect to UVM interfaces.
10957
10958 The first parameter is the name of an interface.
10959
10960 The second parameter is a regexp of modports to read from in
10961 that interface.
10962
10963 The third parameter is the instance name to use to dot reference into.
10964
10965 The optional fourth parameter is a regular expression, and only
10966 signals matching the regular expression will be included.
10967
10968 Limitations:
10969
10970 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
10971
10972 Inouts are not supported, as assignments must be unidirectional.
10973
10974 If a signal is part of the interface header and in both a
10975 modport and the interface itself, it will not be listed. (As
10976 this would result in a syntax error when the connections are
10977 made.)
10978
10979 See the example in `verilog-auto-inout-modport'."
10980 (save-excursion
10981 (let* ((params (verilog-read-auto-params 3 4))
10982 (submod (nth 0 params))
10983 (modport-re (nth 1 params))
10984 (inst-name (nth 2 params))
10985 (regexp (nth 3 params))
10986 direction-re submodi) ; direction argument not supported until requested
10987 ;; Lookup position, etc of co-module
10988 ;; Note this may raise an error
10989 (when (setq submodi (verilog-modi-lookup submod t))
10990 (let* ((indent-pt (current-indentation))
10991 (submoddecls (verilog-modi-get-decls submodi))
10992 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
10993 (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
10994 (verilog-decls-get-vars submoddecls)
10995 (verilog-signals-not-in
10996 (verilog-decls-get-inputs submodportdecls)
10997 (verilog-decls-get-ports submoddecls))))
10998 (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
10999 (verilog-decls-get-vars submoddecls)
11000 (verilog-signals-not-in
11001 (verilog-decls-get-outputs submodportdecls)
11002 (verilog-decls-get-ports submoddecls)))))
11003 (forward-line 1)
11004 (setq sig-list-i (verilog-signals-edit-wire-reg
11005 (verilog-signals-matching-dir-re
11006 (verilog-signals-matching-regexp sig-list-i regexp)
11007 "input" direction-re))
11008 sig-list-o (verilog-signals-edit-wire-reg
11009 (verilog-signals-matching-dir-re
11010 (verilog-signals-matching-regexp sig-list-o regexp)
11011 "output" direction-re)))
11012 (setq sig-list-i (sort (copy-alist sig-list-i) `verilog-signals-sort-compare))
11013 (setq sig-list-o (sort (copy-alist sig-list-o) `verilog-signals-sort-compare))
11014 (when (or sig-list-i sig-list-o)
11015 (verilog-insert-indent "// Beginning of automatic assignments from modport\n")
11016 ;; Don't sort them so an upper AUTOINST will match the main module
11017 (let ((sigs sig-list-o))
11018 (while sigs
11019 (verilog-insert-indent "assign " (verilog-sig-name (car sigs))
11020 " = " inst-name
11021 "." (verilog-sig-name (car sigs)) ";\n")
11022 (setq sigs (cdr sigs))))
11023 (let ((sigs sig-list-i))
11024 (while sigs
11025 (verilog-insert-indent "assign " inst-name
11026 "." (verilog-sig-name (car sigs))
11027 " = " (verilog-sig-name (car sigs)) ";\n")
11028 (setq sigs (cdr sigs))))
11029 (verilog-insert-indent "// End of automatics\n")))))))
11030
11031 (defun verilog-auto-inst-port-map (_port-st)
11032 nil)
11033
11034 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
11035 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
11036 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
11037 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
11038 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
11039 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
11040 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
11041 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
11042
11043 (defun verilog-auto-inst-port (port-st indent-pt moddecls tpl-list tpl-num for-star par-values)
11044 "Print out an instantiation connection for this PORT-ST.
11045 Insert to INDENT-PT, use template TPL-LIST.
11046 @ are instantiation numbers, replaced with TPL-NUM.
11047 @\"(expression @)\" are evaluated, with @ as a variable.
11048 If FOR-STAR add comment it is a .* expansion.
11049 If PAR-VALUES replace final strings with these parameter values."
11050 (let* ((port (verilog-sig-name port-st))
11051 (tpl-ass (or (assoc port (car tpl-list))
11052 (verilog-auto-inst-port-map port-st)))
11053 ;; vl-* are documented for user use
11054 (vl-name (verilog-sig-name port-st))
11055 (vl-width (verilog-sig-width port-st))
11056 (vl-modport (verilog-sig-modport port-st))
11057 (vl-mbits (if (verilog-sig-multidim port-st)
11058 (verilog-sig-multidim-string port-st) ""))
11059 (vl-bits (if (or verilog-auto-inst-vector
11060 (not (assoc port (verilog-decls-get-signals moddecls)))
11061 (not (equal (verilog-sig-bits port-st)
11062 (verilog-sig-bits
11063 (assoc port (verilog-decls-get-signals moddecls))))))
11064 (or (verilog-sig-bits port-st) "")
11065 ""))
11066 (case-fold-search nil)
11067 (check-values par-values)
11068 tpl-net dflt-bits)
11069 ;; Replace parameters in bit-width
11070 (when (and check-values
11071 (not (equal vl-bits "")))
11072 (while check-values
11073 (setq vl-bits (verilog-string-replace-matches
11074 (concat "\\<" (nth 0 (car check-values)) "\\>")
11075 (concat "(" (nth 1 (car check-values)) ")")
11076 t t vl-bits)
11077 vl-mbits (verilog-string-replace-matches
11078 (concat "\\<" (nth 0 (car check-values)) "\\>")
11079 (concat "(" (nth 1 (car check-values)) ")")
11080 t t vl-mbits)
11081 check-values (cdr check-values)))
11082 (setq vl-bits (verilog-simplify-range-expression vl-bits)
11083 vl-mbits (verilog-simplify-range-expression vl-mbits)
11084 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
11085 ;; Default net value if not found
11086 (setq dflt-bits (if (and (verilog-sig-bits port-st)
11087 (or (verilog-sig-multidim port-st)
11088 (verilog-sig-memory port-st)))
11089 (concat "/*" vl-mbits vl-bits "*/")
11090 (concat vl-bits))
11091 tpl-net (concat port
11092 (if (and vl-modport
11093 ;; .modport cannot be added if attachment is
11094 ;; already declared as modport, VCS croaks
11095 (let ((sig (assoc port (verilog-decls-get-interfaces moddecls))))
11096 (not (and sig (verilog-sig-modport sig)))))
11097 (concat "." vl-modport) "")
11098 dflt-bits))
11099 ;; Find template
11100 (cond (tpl-ass ; Template of exact port name
11101 (setq tpl-net (nth 1 tpl-ass)))
11102 ((nth 1 tpl-list) ; Wildcards in template, search them
11103 (let ((wildcards (nth 1 tpl-list)))
11104 (while wildcards
11105 (when (string-match (nth 0 (car wildcards)) port)
11106 (setq tpl-ass (car wildcards) ; so allow @ parsing
11107 tpl-net (replace-match (nth 1 (car wildcards))
11108 t nil port)))
11109 (setq wildcards (cdr wildcards))))))
11110 ;; Parse Templated variable
11111 (when tpl-ass
11112 ;; Evaluate @"(lispcode)"
11113 (when (string-match "@\".*[^\\]\"" tpl-net)
11114 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
11115 (setq tpl-net
11116 (concat
11117 (substring tpl-net 0 (match-beginning 0))
11118 (save-match-data
11119 (let* ((expr (match-string 1 tpl-net))
11120 (value
11121 (progn
11122 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
11123 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
11124 (prin1 (eval (car (read-from-string expr)))
11125 (lambda (_ch) ())))))
11126 (if (numberp value) (setq value (number-to-string value)))
11127 value))
11128 (substring tpl-net (match-end 0))))))
11129 ;; Replace @ and [] magic variables in final output
11130 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
11131 (setq tpl-net (verilog-string-replace-matches "\\[\\]\\[\\]" dflt-bits nil nil tpl-net))
11132 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
11133 ;; Insert it
11134 (indent-to indent-pt)
11135 (insert "." port)
11136 (unless (and verilog-auto-inst-dot-name
11137 (equal port tpl-net))
11138 (indent-to verilog-auto-inst-column)
11139 (insert "(" tpl-net ")"))
11140 (insert ",")
11141 (cond (tpl-ass
11142 (verilog-read-auto-template-hit tpl-ass)
11143 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
11144 verilog-auto-inst-column))
11145 ;; verilog-insert requires the complete comment in one call - including the newline
11146 (cond ((equal verilog-auto-inst-template-numbers `lhs)
11147 (verilog-insert " // Templated"
11148 " LHS: " (nth 0 tpl-ass)
11149 "\n"))
11150 (verilog-auto-inst-template-numbers
11151 (verilog-insert " // Templated"
11152 " T" (int-to-string (nth 2 tpl-ass))
11153 " L" (int-to-string (nth 3 tpl-ass))
11154 "\n"))
11155 (t
11156 (verilog-insert " // Templated\n"))))
11157 (for-star
11158 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
11159 verilog-auto-inst-column))
11160 (verilog-insert " // Implicit .\*\n")) ;For some reason the . or * must be escaped...
11161 (t
11162 (insert "\n")))))
11163 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
11164 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
11165 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
11166
11167 (defun verilog-auto-inst-port-list (sig-list indent-pt moddecls tpl-list tpl-num for-star par-values)
11168 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
11169 (when verilog-auto-inst-sort
11170 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
11171 (mapc (lambda (port)
11172 (verilog-auto-inst-port port indent-pt moddecls
11173 tpl-list tpl-num for-star par-values))
11174 sig-list))
11175
11176 (defun verilog-auto-inst-first ()
11177 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
11178 ;; Do we need a trailing comma?
11179 ;; There maybe an ifdef or something similar before us. What a mess. Thus
11180 ;; to avoid trouble we only insert on preceding ) or *.
11181 ;; Insert first port on new line
11182 (insert "\n") ; Must insert before search, so point will move forward if insert comma
11183 (save-excursion
11184 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
11185 (when (looking-at ")\\|\\*") ; Generally don't insert, unless we are fairly sure
11186 (forward-char 1)
11187 (insert ","))))
11188
11189 (defun verilog-auto-star ()
11190 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
11191
11192 If `verilog-auto-star-expand' is set, .* pins are treated if they were
11193 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
11194 will also ignore any .* that are not last in your pin list (this prevents
11195 it from deleting pins following the .* when it expands the AUTOINST.)
11196
11197 On writing your file, unless `verilog-auto-star-save' is set, any
11198 non-templated expanded pins will be removed. You may do this at any time
11199 with \\[verilog-delete-auto-star-implicit].
11200
11201 If you are converting a module to use .* for the first time, you may wish
11202 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
11203
11204 See `verilog-auto-inst' for examples, templates, and more information."
11205 (when (verilog-auto-star-safe)
11206 (verilog-auto-inst)))
11207
11208 (defun verilog-auto-inst ()
11209 "Expand AUTOINST statements, as part of \\[verilog-auto].
11210 Replace the pin connections to an instantiation or interface
11211 declaration with ones automatically derived from the module or
11212 interface header of the instantiated item.
11213
11214 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
11215 and delete them before saving unless `verilog-auto-star-save' is set.
11216 See `verilog-auto-star' for more information.
11217
11218 The pins are printed in declaration order or alphabetically,
11219 based on the `verilog-auto-inst-sort' variable.
11220
11221 Limitations:
11222 Module names must be resolvable to filenames by adding a
11223 `verilog-library-extensions', and being found in the same directory, or
11224 by changing the variable `verilog-library-flags' or
11225 `verilog-library-directories'. Macros `modname are translated through the
11226 vh-{name} Emacs variable, if that is not found, it just ignores the \\=`.
11227
11228 In templates you must have one signal per line, ending in a ), or ));,
11229 and have proper () nesting, including a final ); to end the template.
11230
11231 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11232
11233 SystemVerilog multidimensional input/output has only experimental support.
11234
11235 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
11236
11237 Parameters referenced by the instantiation will remain symbolic, unless
11238 `verilog-auto-inst-param-value' is set.
11239
11240 Gate primitives (and/or) may have AUTOINST for the purpose of
11241 AUTOWIRE declarations, etc. Gates are the only case when
11242 position based connections are passed.
11243
11244 The array part of arrayed instances are ignored; this may
11245 result in undesirable default AUTOINST connections; use a
11246 template instead.
11247
11248 For example, first take the submodule InstModule.v:
11249
11250 module InstModule (o,i);
11251 output [31:0] o;
11252 input i;
11253 wire [31:0] o = {32{i}};
11254 endmodule
11255
11256 This is then used in an upper level module:
11257
11258 module ExampInst (o,i);
11259 output o;
11260 input i;
11261 InstModule instName
11262 (/*AUTOINST*/);
11263 endmodule
11264
11265 Typing \\[verilog-auto] will make this into:
11266
11267 module ExampInst (o,i);
11268 output o;
11269 input i;
11270 InstModule instName
11271 (/*AUTOINST*/
11272 // Outputs
11273 .ov (ov[31:0]),
11274 // Inputs
11275 .i (i));
11276 endmodule
11277
11278 Where the list of inputs and outputs came from the inst module.
11279 \f
11280 Exceptions:
11281
11282 Unless you are instantiating a module multiple times, or the module is
11283 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
11284 It just makes for unmaintainable code. To sanitize signal names, try
11285 vrename from URL `http://www.veripool.org'.
11286
11287 When you need to violate this suggestion there are two ways to list
11288 exceptions, placing them before the AUTOINST, or using templates.
11289
11290 Any ports defined before the /*AUTOINST*/ are not included in the list of
11291 automatics. This is similar to making a template as described below, but
11292 is restricted to simple connections just like you normally make. Also note
11293 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
11294 you have the appropriate // Input or // Output comment, and exactly the
11295 same line formatting as AUTOINST itself uses.
11296
11297 InstModule instName
11298 (// Inputs
11299 .i (my_i_dont_mess_with_it),
11300 /*AUTOINST*/
11301 // Outputs
11302 .ov (ov[31:0]));
11303
11304 \f
11305 Templates:
11306
11307 For multiple instantiations based upon a single template, create a
11308 commented out template:
11309
11310 /* InstModule AUTO_TEMPLATE (
11311 .sig3 (sigz[]),
11312 );
11313 */
11314
11315 Templates go ABOVE the instantiation(s). When an instantiation is
11316 expanded `verilog-mode' simply searches up for the closest template.
11317 Thus you can have multiple templates for the same module, just alternate
11318 between the template for an instantiation and the instantiation itself.
11319 (For backward compatibility if no template is found above, it
11320 will also look below, but do not use this behavior in new designs.)
11321
11322 The module name must be the same as the name of the module in the
11323 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
11324 words and capitalized. Only signals that must be different for each
11325 instantiation need to be listed.
11326
11327 Inside a template, a [] in a connection name (with nothing else
11328 inside the brackets) will be replaced by the same bus subscript
11329 as it is being connected to, or the [] will be removed if it is
11330 a single bit signal.
11331
11332 Inside a template, a [][] in a connection name will behave
11333 similarly to a [] for scalar or single-dimensional connection;
11334 for a multidimensional connection it will print a comment
11335 similar to that printed when a template is not used. Generally
11336 it is a good idea to do this for all connections in a template,
11337 as then they will work for any width signal, and with AUTOWIRE.
11338 See PTL_BUS becoming PTL_BUSNEW below.
11339
11340 Inside a template, a [] in a connection name (with nothing else inside
11341 the brackets) will be replaced by the same bus subscript as it is being
11342 connected to, or the [] will be removed if it is a single bit signal.
11343 Generally it is a good idea to do this for all connections in a template,
11344 as then they will work for any width signal, and with AUTOWIRE. See
11345 PTL_BUS becoming PTL_BUSNEW below.
11346
11347 If you have a complicated template, set `verilog-auto-inst-template-numbers'
11348 to see which regexps are matching. Don't leave that mode set after
11349 debugging is completed though, it will result in lots of extra differences
11350 and merge conflicts.
11351
11352 Setting `verilog-auto-template-warn-unused' will report errors
11353 if any template lines are unused.
11354
11355 For example:
11356
11357 /* InstModule AUTO_TEMPLATE (
11358 .ptl_bus (ptl_busnew[]),
11359 );
11360 */
11361 InstModule ms2m (/*AUTOINST*/);
11362
11363 Typing \\[verilog-auto] will make this into:
11364
11365 InstModule ms2m (/*AUTOINST*/
11366 // Outputs
11367 .NotInTemplate (NotInTemplate),
11368 .ptl_bus (ptl_busnew[3:0]), // Templated
11369 ....
11370
11371 \f
11372 Multiple Module Templates:
11373
11374 The same template lines can be applied to multiple modules with
11375 the syntax as follows:
11376
11377 /* InstModuleA AUTO_TEMPLATE
11378 InstModuleB AUTO_TEMPLATE
11379 InstModuleC AUTO_TEMPLATE
11380 InstModuleD AUTO_TEMPLATE (
11381 .ptl_bus (ptl_busnew[]),
11382 );
11383 */
11384
11385 Note there is only one AUTO_TEMPLATE opening parenthesis.
11386 \f
11387 @ Templates:
11388
11389 It is common to instantiate a cell multiple times, so templates make it
11390 trivial to substitute part of the cell name into the connection name.
11391
11392 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
11393 .sig1 (sigx[@]),
11394 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
11395 );
11396 */
11397
11398 If no regular expression is provided immediately after the AUTO_TEMPLATE
11399 keyword, then the @ character in any connection names will be replaced
11400 with the instantiation number; the first digits found in the cell's
11401 instantiation name.
11402
11403 If a regular expression is provided, the @ character will be replaced
11404 with the first () grouping that matches against the cell name. Using a
11405 regexp of `\\([0-9]+\\)' provides identical values for @ as when no
11406 regexp is provided. If you use multiple layers of parenthesis,
11407 `test\\([^0-9]+\\)_\\([0-9]+\\)' would replace @ with non-number
11408 characters after test and before _, whereas
11409 `\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)' would replace @ with the entire
11410 match.
11411
11412 For example:
11413
11414 /* InstModule AUTO_TEMPLATE (
11415 .ptl_mapvalidx (ptl_mapvalid[@]),
11416 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
11417 );
11418 */
11419 InstModule ms2m (/*AUTOINST*/);
11420
11421 Typing \\[verilog-auto] will make this into:
11422
11423 InstModule ms2m (/*AUTOINST*/
11424 // Outputs
11425 .ptl_mapvalidx (ptl_mapvalid[2]),
11426 .ptl_mapvalidp1x (ptl_mapvalid[3]));
11427
11428 Note the @ character was replaced with the 2 from \"ms2m\".
11429
11430 Alternatively, using a regular expression for @:
11431
11432 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
11433 .ptl_mapvalidx (@_ptl_mapvalid),
11434 .ptl_mapvalidp1x (ptl_mapvalid_@),
11435 );
11436 */
11437 InstModule ms2_FOO (/*AUTOINST*/);
11438 InstModule ms2_BAR (/*AUTOINST*/);
11439
11440 Typing \\[verilog-auto] will make this into:
11441
11442 InstModule ms2_FOO (/*AUTOINST*/
11443 // Outputs
11444 .ptl_mapvalidx (FOO_ptl_mapvalid),
11445 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
11446 InstModule ms2_BAR (/*AUTOINST*/
11447 // Outputs
11448 .ptl_mapvalidx (BAR_ptl_mapvalid),
11449 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
11450
11451 \f
11452 Regexp Templates:
11453
11454 A template entry of the form
11455
11456 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
11457
11458 will apply an Emacs style regular expression search for any port beginning
11459 in pci_req followed by numbers and ending in _l and connecting that to
11460 the pci_req_jtag_[] net, with the bus subscript coming from what matches
11461 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
11462
11463 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
11464 does the same thing. (Note a @ in the connection/replacement text is
11465 completely different -- still use \\1 there!) Thus this is the same as
11466 the above template:
11467
11468 .pci_req@_l (pci_req_jtag_[\\1]),
11469
11470 Here's another example to remove the _l, useful when naming conventions
11471 specify _ alone to mean active low. Note the use of [] to keep the bus
11472 subscript:
11473
11474 .\\(.*\\)_l (\\1_[]),
11475 \f
11476 Lisp Templates:
11477
11478 First any regular expression template is expanded.
11479
11480 If the syntax @\"( ... )\" is found in a connection, the expression in
11481 quotes will be evaluated as a Lisp expression, with @ replaced by the
11482 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
11483 4 into the brackets. Quote all double-quotes inside the expression with
11484 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
11485 regexp template backslash the backslash quote (\\\\\"...\\\\\").
11486
11487 There are special variables defined that are useful in these
11488 Lisp functions:
11489
11490 vl-name Name portion of the input/output port.
11491 vl-bits Bus bits portion of the input/output port (`[2:0]').
11492 vl-mbits Multidimensional array bits for port (`[2:0][3:0]').
11493 vl-width Width of the input/output port (`3' for [2:0]).
11494 May be a (...) expression if bits isn't a constant.
11495 vl-dir Direction of the pin input/output/inout/interface.
11496 vl-modport The modport, if an interface with a modport.
11497 vl-cell-type Module name/type of the cell (`InstModule').
11498 vl-cell-name Instance name of the cell (`instName').
11499
11500 Normal Lisp variables may be used in expressions. See
11501 `verilog-read-defines' which can set vh-{definename} variables for use
11502 here. Also, any comments of the form:
11503
11504 /*AUTO_LISP(setq foo 1)*/
11505
11506 will evaluate any Lisp expression inside the parenthesis between the
11507 beginning of the buffer and the point of the AUTOINST. This allows
11508 functions to be defined or variables to be changed between instantiations.
11509 (See also `verilog-auto-insert-lisp' if you want the output from your
11510 lisp function to be inserted.)
11511
11512 Note that when using lisp expressions errors may occur when @ is not a
11513 number; you may need to use the standard Emacs Lisp functions
11514 `number-to-string' and `string-to-number'.
11515
11516 After the evaluation is completed, @ substitution and [] substitution
11517 occur.
11518
11519 For more information see the \\[verilog-faq] and forums at URL
11520 `http://www.veripool.org'."
11521 (save-excursion
11522 ;; Find beginning
11523 (let* ((pt (point))
11524 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
11525 (indent-pt (save-excursion (verilog-backward-open-paren)
11526 (1+ (current-column))))
11527 (verilog-auto-inst-column (max verilog-auto-inst-column
11528 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11529 (modi (verilog-modi-current))
11530 (moddecls (verilog-modi-get-decls modi))
11531 submod submodi submoddecls
11532 inst skip-pins tpl-list tpl-num did-first par-values)
11533
11534 ;; Find module name that is instantiated
11535 (setq submod (verilog-read-inst-module)
11536 inst (verilog-read-inst-name)
11537 vl-cell-type submod
11538 vl-cell-name inst
11539 skip-pins (aref (verilog-read-inst-pins) 0))
11540
11541 ;; Parse any AUTO_LISP() before here
11542 (verilog-read-auto-lisp (point-min) pt)
11543
11544 ;; Read parameters (after AUTO_LISP)
11545 (setq par-values (and verilog-auto-inst-param-value
11546 (verilog-read-inst-param-value)))
11547
11548 ;; Lookup position, etc of submodule
11549 ;; Note this may raise an error
11550 (when (and (not (member submod verilog-gate-keywords))
11551 (setq submodi (verilog-modi-lookup submod t)))
11552 (setq submoddecls (verilog-modi-get-decls submodi))
11553 ;; If there's a number in the instantiation, it may be an argument to the
11554 ;; automatic variable instantiation program.
11555 (let* ((tpl-info (verilog-read-auto-template submod))
11556 (tpl-regexp (aref tpl-info 0)))
11557 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11558 (match-string 1 inst)
11559 "")
11560 tpl-list (aref tpl-info 1)))
11561 ;; Find submodule's signals and dump
11562 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
11563 (verilog-signals-not-in
11564 (verilog-decls-get-vars submoddecls)
11565 skip-pins)))
11566 (vl-dir "interfaced"))
11567 (when (and sig-list
11568 verilog-auto-inst-interfaced-ports)
11569 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11570 ;; Note these are searched for in verilog-read-sub-decls.
11571 (verilog-insert-indent "// Interfaced\n")
11572 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11573 tpl-list tpl-num for-star par-values)))
11574 (let ((sig-list (verilog-signals-not-in
11575 (verilog-decls-get-interfaces submoddecls)
11576 skip-pins))
11577 (vl-dir "interface"))
11578 (when sig-list
11579 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11580 ;; Note these are searched for in verilog-read-sub-decls.
11581 (verilog-insert-indent "// Interfaces\n")
11582 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11583 tpl-list tpl-num for-star par-values)))
11584 (let ((sig-list (verilog-signals-not-in
11585 (verilog-decls-get-outputs submoddecls)
11586 skip-pins))
11587 (vl-dir "output"))
11588 (when sig-list
11589 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11590 (verilog-insert-indent "// Outputs\n")
11591 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11592 tpl-list tpl-num for-star par-values)))
11593 (let ((sig-list (verilog-signals-not-in
11594 (verilog-decls-get-inouts submoddecls)
11595 skip-pins))
11596 (vl-dir "inout"))
11597 (when sig-list
11598 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11599 (verilog-insert-indent "// Inouts\n")
11600 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11601 tpl-list tpl-num for-star par-values)))
11602 (let ((sig-list (verilog-signals-not-in
11603 (verilog-decls-get-inputs submoddecls)
11604 skip-pins))
11605 (vl-dir "input"))
11606 (when sig-list
11607 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11608 (verilog-insert-indent "// Inputs\n")
11609 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11610 tpl-list tpl-num for-star par-values)))
11611 ;; Kill extra semi
11612 (save-excursion
11613 (cond (did-first
11614 (re-search-backward "," pt t)
11615 (delete-char 1)
11616 (insert ");")
11617 (search-forward "\n") ; Added by inst-port
11618 (delete-char -1)
11619 (if (search-forward ")" nil t) ; From user, moved up a line
11620 (delete-char -1))
11621 (if (search-forward ";" nil t) ; Don't error if user had syntax error and forgot it
11622 (delete-char -1)))))))))
11623
11624 (defun verilog-auto-inst-param ()
11625 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
11626 Replace the parameter connections to an instantiation with ones
11627 automatically derived from the module header of the instantiated netlist.
11628
11629 See \\[verilog-auto-inst] for limitations, and templates to customize the
11630 output.
11631
11632 For example, first take the submodule InstModule.v:
11633
11634 module InstModule (o,i);
11635 parameter PAR;
11636 endmodule
11637
11638 This is then used in an upper level module:
11639
11640 module ExampInst (o,i);
11641 parameter PAR;
11642 InstModule #(/*AUTOINSTPARAM*/)
11643 instName (/*AUTOINST*/);
11644 endmodule
11645
11646 Typing \\[verilog-auto] will make this into:
11647
11648 module ExampInst (o,i);
11649 output o;
11650 input i;
11651 InstModule #(/*AUTOINSTPARAM*/
11652 // Parameters
11653 .PAR (PAR));
11654 instName (/*AUTOINST*/);
11655 endmodule
11656
11657 Where the list of parameter connections come from the inst module.
11658 \f
11659 Templates:
11660
11661 You can customize the parameter connections using AUTO_TEMPLATEs,
11662 just as you would with \\[verilog-auto-inst]."
11663 (save-excursion
11664 ;; Find beginning
11665 (let* ((pt (point))
11666 (indent-pt (save-excursion (verilog-backward-open-paren)
11667 (1+ (current-column))))
11668 (verilog-auto-inst-column (max verilog-auto-inst-column
11669 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11670 (modi (verilog-modi-current))
11671 (moddecls (verilog-modi-get-decls modi))
11672 submod submodi submoddecls
11673 inst skip-pins tpl-list tpl-num did-first)
11674 ;; Find module name that is instantiated
11675 (setq submod (save-excursion
11676 ;; Get to the point where AUTOINST normally is to read the module
11677 (verilog-re-search-forward-quick "[(;]" nil nil)
11678 (verilog-read-inst-module))
11679 inst (save-excursion
11680 ;; Get to the point where AUTOINST normally is to read the module
11681 (verilog-re-search-forward-quick "[(;]" nil nil)
11682 (verilog-read-inst-name))
11683 vl-cell-type submod
11684 vl-cell-name inst
11685 skip-pins (aref (verilog-read-inst-pins) 0))
11686
11687 ;; Parse any AUTO_LISP() before here
11688 (verilog-read-auto-lisp (point-min) pt)
11689
11690 ;; Lookup position, etc of submodule
11691 ;; Note this may raise an error
11692 (when (setq submodi (verilog-modi-lookup submod t))
11693 (setq submoddecls (verilog-modi-get-decls submodi))
11694 ;; If there's a number in the instantiation, it may be an argument to the
11695 ;; automatic variable instantiation program.
11696 (let* ((tpl-info (verilog-read-auto-template submod))
11697 (tpl-regexp (aref tpl-info 0)))
11698 (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
11699 (match-string 1 inst)
11700 "")
11701 tpl-list (aref tpl-info 1)))
11702 ;; Find submodule's signals and dump
11703 (let ((sig-list (verilog-signals-not-in
11704 (verilog-decls-get-gparams submoddecls)
11705 skip-pins))
11706 (vl-dir "parameter"))
11707 (when sig-list
11708 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11709 ;; Note these are searched for in verilog-read-sub-decls.
11710 (verilog-insert-indent "// Parameters\n")
11711 (verilog-auto-inst-port-list sig-list indent-pt moddecls
11712 tpl-list tpl-num nil nil)))
11713 ;; Kill extra semi
11714 (save-excursion
11715 (cond (did-first
11716 (re-search-backward "," pt t)
11717 (delete-char 1)
11718 (insert ")")
11719 (search-forward "\n") ; Added by inst-port
11720 (delete-char -1)
11721 (if (search-forward ")" nil t) ; From user, moved up a line
11722 (delete-char -1)))))))))
11723
11724 (defun verilog-auto-reg ()
11725 "Expand AUTOREG statements, as part of \\[verilog-auto].
11726 Make reg statements for any output that isn't already declared,
11727 and isn't a wire output from a block. `verilog-auto-wire-type'
11728 may be used to change the datatype of the declarations.
11729
11730 Limitations:
11731 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11732
11733 This does NOT work on memories, declare those yourself.
11734
11735 An example:
11736
11737 module ExampReg (o,i);
11738 output o;
11739 input i;
11740 /*AUTOREG*/
11741 always o = i;
11742 endmodule
11743
11744 Typing \\[verilog-auto] will make this into:
11745
11746 module ExampReg (o,i);
11747 output o;
11748 input i;
11749 /*AUTOREG*/
11750 // Beginning of automatic regs (for this module's undeclared outputs)
11751 reg o;
11752 // End of automatics
11753 always o = i;
11754 endmodule"
11755 (save-excursion
11756 ;; Point must be at insertion point.
11757 (let* ((indent-pt (current-indentation))
11758 (modi (verilog-modi-current))
11759 (moddecls (verilog-modi-get-decls modi))
11760 (modsubdecls (verilog-modi-get-sub-decls modi))
11761 (sig-list (verilog-signals-not-in
11762 (verilog-decls-get-outputs moddecls)
11763 (append (verilog-signals-with ; ignore typed signals
11764 'verilog-sig-type
11765 (verilog-decls-get-outputs moddecls))
11766 (verilog-decls-get-vars moddecls)
11767 (verilog-decls-get-assigns moddecls)
11768 (verilog-decls-get-consts moddecls)
11769 (verilog-decls-get-gparams moddecls)
11770 (verilog-subdecls-get-interfaced modsubdecls)
11771 (verilog-subdecls-get-outputs modsubdecls)
11772 (verilog-subdecls-get-inouts modsubdecls)))))
11773 (when sig-list
11774 (verilog-forward-or-insert-line)
11775 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
11776 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11777 (verilog-insert-indent "// End of automatics\n")))))
11778
11779 (defun verilog-auto-reg-input ()
11780 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
11781 Make reg statements instantiation inputs that aren't already declared.
11782 This is useful for making a top level shell for testing the module that is
11783 to be instantiated.
11784
11785 Limitations:
11786 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
11787
11788 This does NOT work on memories, declare those yourself.
11789
11790 An example (see `verilog-auto-inst' for what else is going on here):
11791
11792 module ExampRegInput (o,i);
11793 output o;
11794 input i;
11795 /*AUTOREGINPUT*/
11796 InstModule instName
11797 (/*AUTOINST*/);
11798 endmodule
11799
11800 Typing \\[verilog-auto] will make this into:
11801
11802 module ExampRegInput (o,i);
11803 output o;
11804 input i;
11805 /*AUTOREGINPUT*/
11806 // Beginning of automatic reg inputs (for undeclared ...
11807 reg [31:0] iv; // From inst of inst.v
11808 // End of automatics
11809 InstModule instName
11810 (/*AUTOINST*/
11811 // Outputs
11812 .o (o[31:0]),
11813 // Inputs
11814 .iv (iv));
11815 endmodule"
11816 (save-excursion
11817 ;; Point must be at insertion point.
11818 (let* ((indent-pt (current-indentation))
11819 (modi (verilog-modi-current))
11820 (moddecls (verilog-modi-get-decls modi))
11821 (modsubdecls (verilog-modi-get-sub-decls modi))
11822 (sig-list (verilog-signals-combine-bus
11823 (verilog-signals-not-in
11824 (append (verilog-subdecls-get-inputs modsubdecls)
11825 (verilog-subdecls-get-inouts modsubdecls))
11826 (append (verilog-decls-get-signals moddecls)
11827 (verilog-decls-get-assigns moddecls))))))
11828 (when sig-list
11829 (verilog-forward-or-insert-line)
11830 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
11831 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11832 (verilog-insert-indent "// End of automatics\n")))))
11833
11834 (defun verilog-auto-logic-setup ()
11835 "Prepare variables due to AUTOLOGIC."
11836 (unless verilog-auto-wire-type
11837 (set (make-local-variable 'verilog-auto-wire-type)
11838 "logic")))
11839
11840 (defun verilog-auto-logic ()
11841 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
11842 Make wire statements using the SystemVerilog logic keyword.
11843 This is currently equivalent to:
11844
11845 /*AUTOWIRE*/
11846
11847 with the below at the bottom of the file
11848
11849 // Local Variables:
11850 // verilog-auto-logic-type:\"logic\"
11851 // End:
11852
11853 In the future AUTOLOGIC may declare additional identifiers,
11854 while AUTOWIRE will not."
11855 (save-excursion
11856 (verilog-auto-logic-setup)
11857 (verilog-auto-wire)))
11858
11859 (defun verilog-auto-wire ()
11860 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
11861 Make wire statements for instantiations outputs that aren't
11862 already declared. `verilog-auto-wire-type' may be used to change
11863 the datatype of the declarations.
11864
11865 Limitations:
11866 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
11867 and all buses must have widths, such as those from AUTOINST, or using []
11868 in AUTO_TEMPLATEs.
11869
11870 This does NOT work on memories or SystemVerilog .name connections,
11871 declare those yourself.
11872
11873 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
11874 determine how to bus together. This occurs when you have ports with
11875 non-numeric or non-sequential bus subscripts. If Verilog mode
11876 mis-guessed, you'll have to declare them yourself.
11877
11878 An example (see `verilog-auto-inst' for what else is going on here):
11879
11880 module ExampWire (o,i);
11881 output o;
11882 input i;
11883 /*AUTOWIRE*/
11884 InstModule instName
11885 (/*AUTOINST*/);
11886 endmodule
11887
11888 Typing \\[verilog-auto] will make this into:
11889
11890 module ExampWire (o,i);
11891 output o;
11892 input i;
11893 /*AUTOWIRE*/
11894 // Beginning of automatic wires
11895 wire [31:0] ov; // From inst of inst.v
11896 // End of automatics
11897 InstModule instName
11898 (/*AUTOINST*/
11899 // Outputs
11900 .ov (ov[31:0]),
11901 // Inputs
11902 .i (i));
11903 wire o = | ov;
11904 endmodule"
11905 (save-excursion
11906 ;; Point must be at insertion point.
11907 (let* ((indent-pt (current-indentation))
11908 (modi (verilog-modi-current))
11909 (moddecls (verilog-modi-get-decls modi))
11910 (modsubdecls (verilog-modi-get-sub-decls modi))
11911 (sig-list (verilog-signals-combine-bus
11912 (verilog-signals-not-in
11913 (append (verilog-subdecls-get-outputs modsubdecls)
11914 (verilog-subdecls-get-inouts modsubdecls))
11915 (verilog-decls-get-signals moddecls)))))
11916 (when sig-list
11917 (verilog-forward-or-insert-line)
11918 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
11919 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
11920 (verilog-insert-indent "// End of automatics\n")
11921 ;; We used to optionally call verilog-pretty-declarations and
11922 ;; verilog-pretty-expr here, but it's too slow on huge modules,
11923 ;; plus makes everyone's module change. Finally those call
11924 ;; syntax-ppss which is broken when change hooks are disabled.
11925 ))))
11926
11927 (defun verilog-auto-output ()
11928 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
11929 Make output statements for any output signal from an /*AUTOINST*/ that
11930 isn't an input to another AUTOINST. This is useful for modules which
11931 only instantiate other modules.
11932
11933 Limitations:
11934 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11935
11936 If placed inside the parenthesis of a module declaration, it creates
11937 Verilog 2001 style, else uses Verilog 1995 style.
11938
11939 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11940 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11941
11942 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11943
11944 Types are added to declarations if an AUTOLOGIC or
11945 `verilog-auto-wire-type' is set to logic.
11946
11947 Signals matching `verilog-auto-output-ignore-regexp' are not included.
11948
11949 An example (see `verilog-auto-inst' for what else is going on here):
11950
11951 module ExampOutput (ov,i);
11952 input i;
11953 /*AUTOOUTPUT*/
11954 InstModule instName
11955 (/*AUTOINST*/);
11956 endmodule
11957
11958 Typing \\[verilog-auto] will make this into:
11959
11960 module ExampOutput (ov,i);
11961 input i;
11962 /*AUTOOUTPUT*/
11963 // Beginning of automatic outputs (from unused autoinst outputs)
11964 output [31:0] ov; // From inst of inst.v
11965 // End of automatics
11966 InstModule instName
11967 (/*AUTOINST*/
11968 // Outputs
11969 .ov (ov[31:0]),
11970 // Inputs
11971 .i (i));
11972 endmodule
11973
11974 You may also provide an optional regular expression, in which case only
11975 signals matching the regular expression will be included. For example the
11976 same expansion will result from only extracting outputs starting with ov:
11977
11978 /*AUTOOUTPUT(\"^ov\")*/"
11979 (save-excursion
11980 ;; Point must be at insertion point.
11981 (let* ((indent-pt (current-indentation))
11982 (params (verilog-read-auto-params 0 1))
11983 (regexp (nth 0 params))
11984 (v2k (verilog-in-paren-quick))
11985 (modi (verilog-modi-current))
11986 (moddecls (verilog-modi-get-decls modi))
11987 (modsubdecls (verilog-modi-get-sub-decls modi))
11988 (sig-list (verilog-signals-not-in
11989 (verilog-subdecls-get-outputs modsubdecls)
11990 (append (verilog-decls-get-outputs moddecls)
11991 (verilog-decls-get-inouts moddecls)
11992 (verilog-decls-get-inputs moddecls)
11993 (verilog-subdecls-get-inputs modsubdecls)
11994 (verilog-subdecls-get-inouts modsubdecls)))))
11995 (when regexp
11996 (setq sig-list (verilog-signals-matching-regexp
11997 sig-list regexp)))
11998 (setq sig-list (verilog-signals-not-matching-regexp
11999 sig-list verilog-auto-output-ignore-regexp))
12000 (verilog-forward-or-insert-line)
12001 (when v2k (verilog-repair-open-comma))
12002 (when sig-list
12003 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
12004 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
12005 (verilog-insert-indent "// End of automatics\n"))
12006 (when v2k (verilog-repair-close-comma)))))
12007
12008 (defun verilog-auto-output-every ()
12009 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
12010 Make output statements for any signals that aren't primary inputs or
12011 outputs already. This makes every signal in the design an output. This is
12012 useful to get Synopsys to preserve every signal in the design, since it
12013 won't optimize away the outputs.
12014
12015 An example:
12016
12017 module ExampOutputEvery (o,i,tempa,tempb);
12018 output o;
12019 input i;
12020 /*AUTOOUTPUTEVERY*/
12021 wire tempa = i;
12022 wire tempb = tempa;
12023 wire o = tempb;
12024 endmodule
12025
12026 Typing \\[verilog-auto] will make this into:
12027
12028 module ExampOutputEvery (o,i,tempa,tempb);
12029 output o;
12030 input i;
12031 /*AUTOOUTPUTEVERY*/
12032 // Beginning of automatic outputs (every signal)
12033 output tempb;
12034 output tempa;
12035 // End of automatics
12036 wire tempa = i;
12037 wire tempb = tempa;
12038 wire o = tempb;
12039 endmodule
12040
12041 You may also provide an optional regular expression, in which case only
12042 signals matching the regular expression will be included. For example the
12043 same expansion will result from only extracting outputs starting with ov:
12044
12045 /*AUTOOUTPUTEVERY(\"^ov\")*/"
12046 (save-excursion
12047 ;;Point must be at insertion point
12048 (let* ((indent-pt (current-indentation))
12049 (params (verilog-read-auto-params 0 1))
12050 (regexp (nth 0 params))
12051 (v2k (verilog-in-paren-quick))
12052 (modi (verilog-modi-current))
12053 (moddecls (verilog-modi-get-decls modi))
12054 (sig-list (verilog-signals-combine-bus
12055 (verilog-signals-not-in
12056 (verilog-decls-get-signals moddecls)
12057 (verilog-decls-get-ports moddecls)))))
12058 (when regexp
12059 (setq sig-list (verilog-signals-matching-regexp
12060 sig-list regexp)))
12061 (setq sig-list (verilog-signals-not-matching-regexp
12062 sig-list verilog-auto-output-ignore-regexp))
12063 (verilog-forward-or-insert-line)
12064 (when v2k (verilog-repair-open-comma))
12065 (when sig-list
12066 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
12067 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
12068 (verilog-insert-indent "// End of automatics\n"))
12069 (when v2k (verilog-repair-close-comma)))))
12070
12071 (defun verilog-auto-input ()
12072 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
12073 Make input statements for any input signal into an /*AUTOINST*/ that
12074 isn't declared elsewhere inside the module. This is useful for modules which
12075 only instantiate other modules.
12076
12077 Limitations:
12078 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
12079
12080 If placed inside the parenthesis of a module declaration, it creates
12081 Verilog 2001 style, else uses Verilog 1995 style.
12082
12083 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
12084 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
12085
12086 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
12087
12088 Types are added to declarations if an AUTOLOGIC or
12089 `verilog-auto-wire-type' is set to logic.
12090
12091 Signals matching `verilog-auto-input-ignore-regexp' are not included.
12092
12093 An example (see `verilog-auto-inst' for what else is going on here):
12094
12095 module ExampInput (ov,i);
12096 output [31:0] ov;
12097 /*AUTOINPUT*/
12098 InstModule instName
12099 (/*AUTOINST*/);
12100 endmodule
12101
12102 Typing \\[verilog-auto] will make this into:
12103
12104 module ExampInput (ov,i);
12105 output [31:0] ov;
12106 /*AUTOINPUT*/
12107 // Beginning of automatic inputs (from unused autoinst inputs)
12108 input i; // From inst of inst.v
12109 // End of automatics
12110 InstModule instName
12111 (/*AUTOINST*/
12112 // Outputs
12113 .ov (ov[31:0]),
12114 // Inputs
12115 .i (i));
12116 endmodule
12117
12118 You may also provide an optional regular expression, in which case only
12119 signals matching the regular expression will be included. For example the
12120 same expansion will result from only extracting inputs starting with i:
12121
12122 /*AUTOINPUT(\"^i\")*/"
12123 (save-excursion
12124 (let* ((indent-pt (current-indentation))
12125 (params (verilog-read-auto-params 0 1))
12126 (regexp (nth 0 params))
12127 (v2k (verilog-in-paren-quick))
12128 (modi (verilog-modi-current))
12129 (moddecls (verilog-modi-get-decls modi))
12130 (modsubdecls (verilog-modi-get-sub-decls modi))
12131 (sig-list (verilog-signals-not-in
12132 (verilog-subdecls-get-inputs modsubdecls)
12133 (append (verilog-decls-get-inputs moddecls)
12134 (verilog-decls-get-inouts moddecls)
12135 (verilog-decls-get-outputs moddecls)
12136 (verilog-decls-get-vars moddecls)
12137 (verilog-decls-get-consts moddecls)
12138 (verilog-decls-get-gparams moddecls)
12139 (verilog-subdecls-get-interfaced modsubdecls)
12140 (verilog-subdecls-get-outputs modsubdecls)
12141 (verilog-subdecls-get-inouts modsubdecls)))))
12142 (when regexp
12143 (setq sig-list (verilog-signals-matching-regexp
12144 sig-list regexp)))
12145 (setq sig-list (verilog-signals-not-matching-regexp
12146 sig-list verilog-auto-input-ignore-regexp))
12147 (verilog-forward-or-insert-line)
12148 (when v2k (verilog-repair-open-comma))
12149 (when sig-list
12150 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
12151 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
12152 (verilog-insert-indent "// End of automatics\n"))
12153 (when v2k (verilog-repair-close-comma)))))
12154
12155 (defun verilog-auto-inout ()
12156 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
12157 Make inout statements for any inout signal in an /*AUTOINST*/ that
12158 isn't declared elsewhere inside the module.
12159
12160 Limitations:
12161 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
12162
12163 If placed inside the parenthesis of a module declaration, it creates
12164 Verilog 2001 style, else uses Verilog 1995 style.
12165
12166 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
12167 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
12168
12169 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
12170
12171 Types are added to declarations if an AUTOLOGIC or
12172 `verilog-auto-wire-type' is set to logic.
12173
12174 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
12175
12176 An example (see `verilog-auto-inst' for what else is going on here):
12177
12178 module ExampInout (ov,i);
12179 input i;
12180 /*AUTOINOUT*/
12181 InstModule instName
12182 (/*AUTOINST*/);
12183 endmodule
12184
12185 Typing \\[verilog-auto] will make this into:
12186
12187 module ExampInout (ov,i);
12188 input i;
12189 /*AUTOINOUT*/
12190 // Beginning of automatic inouts (from unused autoinst inouts)
12191 inout [31:0] ov; // From inst of inst.v
12192 // End of automatics
12193 InstModule instName
12194 (/*AUTOINST*/
12195 // Inouts
12196 .ov (ov[31:0]),
12197 // Inputs
12198 .i (i));
12199 endmodule
12200
12201 You may also provide an optional regular expression, in which case only
12202 signals matching the regular expression will be included. For example the
12203 same expansion will result from only extracting inouts starting with i:
12204
12205 /*AUTOINOUT(\"^i\")*/"
12206 (save-excursion
12207 ;; Point must be at insertion point.
12208 (let* ((indent-pt (current-indentation))
12209 (params (verilog-read-auto-params 0 1))
12210 (regexp (nth 0 params))
12211 (v2k (verilog-in-paren-quick))
12212 (modi (verilog-modi-current))
12213 (moddecls (verilog-modi-get-decls modi))
12214 (modsubdecls (verilog-modi-get-sub-decls modi))
12215 (sig-list (verilog-signals-not-in
12216 (verilog-subdecls-get-inouts modsubdecls)
12217 (append (verilog-decls-get-outputs moddecls)
12218 (verilog-decls-get-inouts moddecls)
12219 (verilog-decls-get-inputs moddecls)
12220 (verilog-subdecls-get-inputs modsubdecls)
12221 (verilog-subdecls-get-outputs modsubdecls)))))
12222 (when regexp
12223 (setq sig-list (verilog-signals-matching-regexp
12224 sig-list regexp)))
12225 (setq sig-list (verilog-signals-not-matching-regexp
12226 sig-list verilog-auto-inout-ignore-regexp))
12227 (verilog-forward-or-insert-line)
12228 (when v2k (verilog-repair-open-comma))
12229 (when sig-list
12230 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
12231 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
12232 (verilog-insert-indent "// End of automatics\n"))
12233 (when v2k (verilog-repair-close-comma)))))
12234
12235 (defun verilog-auto-inout-module (&optional complement all-in)
12236 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
12237 Take input/output/inout statements from the specified module and insert
12238 into the current module. This is useful for making null templates and
12239 shell modules which need to have identical I/O with another module.
12240 Any I/O which are already defined in this module will not be redefined.
12241 For the complement of this function, see `verilog-auto-inout-comp',
12242 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12243
12244 Limitations:
12245 If placed inside the parenthesis of a module declaration, it creates
12246 Verilog 2001 style, else uses Verilog 1995 style.
12247
12248 Concatenation and outputting partial buses is not supported.
12249
12250 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12251
12252 Signals are not inserted in the same order as in the original module,
12253 though they will appear to be in the same order to an AUTOINST
12254 instantiating either module.
12255
12256 Signals declared as \"output reg\" or \"output wire\" etc will
12257 lose the wire/reg declaration so that shell modules may
12258 generate those outputs differently. However, \"output logic\"
12259 is propagated.
12260
12261 An example:
12262
12263 module ExampShell (/*AUTOARG*/);
12264 /*AUTOINOUTMODULE(\"ExampMain\")*/
12265 endmodule
12266
12267 module ExampMain (i,o,io);
12268 input i;
12269 output o;
12270 inout io;
12271 endmodule
12272
12273 Typing \\[verilog-auto] will make this into:
12274
12275 module ExampShell (/*AUTOARG*/i,o,io);
12276 /*AUTOINOUTMODULE(\"ExampMain\")*/
12277 // Beginning of automatic in/out/inouts (from specific module)
12278 output o;
12279 inout io;
12280 input i;
12281 // End of automatics
12282 endmodule
12283
12284 You may also provide an optional regular expression, in which case only
12285 signals matching the regular expression will be included. For example the
12286 same expansion will result from only extracting signals starting with i:
12287
12288 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
12289
12290 You may also provide an optional third argument regular
12291 expression, in which case only signals which have that pin
12292 direction and data type matching that regular expression will be
12293 included. This matches against everything before the signal name
12294 in the declaration, for example against \"input\" (single
12295 bit), \"output logic\" (direction and type) or
12296 \"output [1:0]\" (direction and implicit type). You also
12297 probably want to skip spaces in your regexp.
12298
12299 For example, the below will result in matching the output \"o\"
12300 against the previous example's module:
12301
12302 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/
12303
12304 You may also provide an optional fourth argument regular
12305 expression, which if not \"\" only signals which do NOT match
12306 that expression are included."
12307 ;; Beware spacing of quotes in above as can mess up Emacs indenter
12308 (save-excursion
12309 (let* ((params (verilog-read-auto-params 1 4))
12310 (submod (nth 0 params))
12311 (regexp (nth 1 params))
12312 (direction-re (nth 2 params))
12313 (not-re (nth 3 params))
12314 submodi)
12315 ;; Lookup position, etc of co-module
12316 ;; Note this may raise an error
12317 (when (setq submodi (verilog-modi-lookup submod t))
12318 (let* ((indent-pt (current-indentation))
12319 (v2k (verilog-in-paren-quick))
12320 (modi (verilog-modi-current))
12321 (moddecls (verilog-modi-get-decls modi))
12322 (submoddecls (verilog-modi-get-decls submodi))
12323 (sig-list-i (verilog-signals-not-in
12324 (cond (all-in
12325 (append
12326 (verilog-decls-get-inputs submoddecls)
12327 (verilog-decls-get-inouts submoddecls)
12328 (verilog-decls-get-outputs submoddecls)))
12329 (complement
12330 (verilog-decls-get-outputs submoddecls))
12331 (t (verilog-decls-get-inputs submoddecls)))
12332 (append (verilog-decls-get-inputs moddecls))))
12333 (sig-list-o (verilog-signals-not-in
12334 (cond (all-in nil)
12335 (complement
12336 (verilog-decls-get-inputs submoddecls))
12337 (t (verilog-decls-get-outputs submoddecls)))
12338 (append (verilog-decls-get-outputs moddecls))))
12339 (sig-list-io (verilog-signals-not-in
12340 (cond (all-in nil)
12341 (t (verilog-decls-get-inouts submoddecls)))
12342 (append (verilog-decls-get-inouts moddecls))))
12343 (sig-list-if (verilog-signals-not-in
12344 (verilog-decls-get-interfaces submoddecls)
12345 (append (verilog-decls-get-interfaces moddecls)))))
12346 (forward-line 1)
12347 (setq sig-list-i (verilog-signals-edit-wire-reg
12348 (verilog-signals-not-matching-regexp
12349 (verilog-signals-matching-dir-re
12350 (verilog-signals-matching-regexp sig-list-i regexp)
12351 "input" direction-re) not-re))
12352 sig-list-o (verilog-signals-edit-wire-reg
12353 (verilog-signals-not-matching-regexp
12354 (verilog-signals-matching-dir-re
12355 (verilog-signals-matching-regexp sig-list-o regexp)
12356 "output" direction-re) not-re))
12357 sig-list-io (verilog-signals-edit-wire-reg
12358 (verilog-signals-not-matching-regexp
12359 (verilog-signals-matching-dir-re
12360 (verilog-signals-matching-regexp sig-list-io regexp)
12361 "inout" direction-re) not-re))
12362 sig-list-if (verilog-signals-not-matching-regexp
12363 (verilog-signals-matching-dir-re
12364 (verilog-signals-matching-regexp sig-list-if regexp)
12365 "interface" direction-re) not-re))
12366 (when v2k (verilog-repair-open-comma))
12367 (when (or sig-list-i sig-list-o sig-list-io sig-list-if)
12368 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
12369 ;; Don't sort them so an upper AUTOINST will match the main module
12370 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12371 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12372 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12373 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
12374 (verilog-insert-indent "// End of automatics\n"))
12375 (when v2k (verilog-repair-close-comma)))))))
12376
12377 (defun verilog-auto-inout-comp ()
12378 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
12379 Take input/output/inout statements from the specified module and
12380 insert the inverse into the current module (inputs become outputs
12381 and vice-versa.) This is useful for making test and stimulus
12382 modules which need to have complementing I/O with another module.
12383 Any I/O which are already defined in this module will not be
12384 redefined. For the complement of this function, see
12385 `verilog-auto-inout-module'.
12386
12387 Limitations:
12388 If placed inside the parenthesis of a module declaration, it creates
12389 Verilog 2001 style, else uses Verilog 1995 style.
12390
12391 Concatenation and outputting partial buses is not supported.
12392
12393 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12394
12395 Signals are not inserted in the same order as in the original module,
12396 though they will appear to be in the same order to an AUTOINST
12397 instantiating either module.
12398
12399 An example:
12400
12401 module ExampShell (/*AUTOARG*/);
12402 /*AUTOINOUTCOMP(\"ExampMain\")*/
12403 endmodule
12404
12405 module ExampMain (i,o,io);
12406 input i;
12407 output o;
12408 inout io;
12409 endmodule
12410
12411 Typing \\[verilog-auto] will make this into:
12412
12413 module ExampShell (/*AUTOARG*/i,o,io);
12414 /*AUTOINOUTCOMP(\"ExampMain\")*/
12415 // Beginning of automatic in/out/inouts (from specific module)
12416 output i;
12417 inout io;
12418 input o;
12419 // End of automatics
12420 endmodule
12421
12422 You may also provide an optional regular expression, in which case only
12423 signals matching the regular expression will be included. For example the
12424 same expansion will result from only extracting signals starting with i:
12425
12426 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/
12427
12428 You may also provide an optional third argument regular
12429 expression, in which case only signals which have that pin
12430 direction and data type matching that regular expression will be
12431 included. This matches against everything before the signal name
12432 in the declaration, for example against \"input\" (single
12433 bit), \"output logic\" (direction and type)
12434 or \"output [1:0]\" (direction and implicit type). You also
12435 probably want to skip spaces in your regexp.
12436
12437 For example, the below will result in matching the output \"o\"
12438 against the previous example's module:
12439
12440 /*AUTOINOUTCOMP(\"ExampMain\",\"\",\"^output.*\")*/
12441
12442 You may also provide an optional fourth argument regular
12443 expression, which if not \"\" only signals which do NOT match
12444 that expression are included."
12445 ;; Beware spacing of quotes in above as can mess up Emacs indenter
12446 (verilog-auto-inout-module t nil))
12447
12448 (defun verilog-auto-inout-in ()
12449 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
12450 Take input/output/inout statements from the specified module and
12451 insert them as all inputs into the current module. This is
12452 useful for making monitor modules which need to see all signals
12453 as inputs based on another module. Any I/O which are already
12454 defined in this module will not be redefined. See also
12455 `verilog-auto-inout-module'.
12456
12457 Limitations:
12458 If placed inside the parenthesis of a module declaration, it creates
12459 Verilog 2001 style, else uses Verilog 1995 style.
12460
12461 Concatenation and outputting partial buses is not supported.
12462
12463 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12464
12465 Signals are not inserted in the same order as in the original module,
12466 though they will appear to be in the same order to an AUTOINST
12467 instantiating either module.
12468
12469 An example:
12470
12471 module ExampShell (/*AUTOARG*/);
12472 /*AUTOINOUTIN(\"ExampMain\")*/
12473 endmodule
12474
12475 module ExampMain (i,o,io);
12476 input i;
12477 output o;
12478 inout io;
12479 endmodule
12480
12481 Typing \\[verilog-auto] will make this into:
12482
12483 module ExampShell (/*AUTOARG*/i,o,io);
12484 /*AUTOINOUTIN(\"ExampMain\")*/
12485 // Beginning of automatic in/out/inouts (from specific module)
12486 input i;
12487 input io;
12488 input o;
12489 // End of automatics
12490 endmodule
12491
12492 You may also provide an optional regular expression, in which case only
12493 signals matching the regular expression will be included. For example the
12494 same expansion will result from only extracting signals starting with i:
12495
12496 /*AUTOINOUTIN(\"ExampMain\",\"^i\")*/"
12497 (verilog-auto-inout-module nil t))
12498
12499 (defun verilog-auto-inout-param ()
12500 "Expand AUTOINOUTPARAM statements, as part of \\[verilog-auto].
12501 Take input/output/inout statements from the specified module and insert
12502 into the current module. This is useful for making null templates and
12503 shell modules which need to have identical I/O with another module.
12504 Any I/O which are already defined in this module will not be redefined.
12505 For the complement of this function, see `verilog-auto-inout-comp',
12506 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12507
12508 Limitations:
12509 If placed inside the parenthesis of a module declaration, it creates
12510 Verilog 2001 style, else uses Verilog 1995 style.
12511
12512 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12513
12514 Parameters are inserted in the same order as in the original module.
12515
12516 Parameters do not have values, which is SystemVerilog 2009 syntax.
12517
12518 An example:
12519
12520 module ExampShell ();
12521 /*AUTOINOUTPARAM(\"ExampMain\")*/
12522 endmodule
12523
12524 module ExampMain ();
12525 parameter PARAM = 22;
12526 endmodule
12527
12528 Typing \\[verilog-auto] will make this into:
12529
12530 module ExampShell (/*AUTOARG*/i,o,io);
12531 /*AUTOINOUTPARAM(\"ExampMain\")*/
12532 // Beginning of automatic parameters (from specific module)
12533 parameter PARAM;
12534 // End of automatics
12535 endmodule
12536
12537 You may also provide an optional regular expression, in which case only
12538 parameters matching the regular expression will be included. For example the
12539 same expansion will result from only extracting parameters starting with i:
12540
12541 /*AUTOINOUTPARAM(\"ExampMain\",\"^i\")*/"
12542 (save-excursion
12543 (let* ((params (verilog-read-auto-params 1 2))
12544 (submod (nth 0 params))
12545 (regexp (nth 1 params))
12546 submodi)
12547 ;; Lookup position, etc of co-module
12548 ;; Note this may raise an error
12549 (when (setq submodi (verilog-modi-lookup submod t))
12550 (let* ((indent-pt (current-indentation))
12551 (v2k (verilog-in-paren-quick))
12552 (modi (verilog-modi-current))
12553 (moddecls (verilog-modi-get-decls modi))
12554 (submoddecls (verilog-modi-get-decls submodi))
12555 (sig-list-p (verilog-signals-not-in
12556 (verilog-decls-get-gparams submoddecls)
12557 (append (verilog-decls-get-gparams moddecls)))))
12558 (forward-line 1)
12559 (setq sig-list-p (verilog-signals-matching-regexp sig-list-p regexp))
12560 (when v2k (verilog-repair-open-comma))
12561 (when sig-list-p
12562 (verilog-insert-indent "// Beginning of automatic parameters (from specific module)\n")
12563 ;; Don't sort them so an upper AUTOINST will match the main module
12564 (verilog-insert-definition modi sig-list-p "parameter" indent-pt v2k t)
12565 (verilog-insert-indent "// End of automatics\n"))
12566 (when v2k (verilog-repair-close-comma)))))))
12567
12568 (defun verilog-auto-inout-modport ()
12569 "Expand AUTOINOUTMODPORT statements, as part of \\[verilog-auto].
12570 Take input/output/inout statements from the specified interface
12571 and modport and insert into the current module. This is useful
12572 for making verification modules that connect to UVM interfaces.
12573
12574 The first parameter is the name of an interface.
12575
12576 The second parameter is a regexp of modports to read from in
12577 that interface.
12578
12579 The optional third parameter is a regular expression, and only
12580 signals matching the regular expression will be included.
12581
12582 Limitations:
12583 If placed inside the parenthesis of a module declaration, it creates
12584 Verilog 2001 style, else uses Verilog 1995 style.
12585
12586 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
12587
12588 As with other autos, any inputs/outputs declared in the module
12589 will suppress the AUTO from redeclaring an inputs/outputs by
12590 the same name.
12591
12592 An example:
12593
12594 interface ExampIf
12595 ( input logic clk );
12596 logic req_val;
12597 logic [7:0] req_dat;
12598 clocking mon_clkblk @(posedge clk);
12599 input req_val;
12600 input req_dat;
12601 endclocking
12602 modport mp(clocking mon_clkblk);
12603 endinterface
12604
12605 module ExampMain
12606 ( input clk,
12607 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12608 // Beginning of automatic in/out/inouts (from modport)
12609 input [7:0] req_dat,
12610 input req_val
12611 // End of automatics
12612 );
12613 /*AUTOASSIGNMODPORT(\"ExampIf\" \"mp\")*/
12614 endmodule
12615
12616 Typing \\[verilog-auto] will make this into:
12617
12618 ...
12619 module ExampMain
12620 ( input clk,
12621 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12622 // Beginning of automatic in/out/inouts (from modport)
12623 input req_dat,
12624 input req_val
12625 // End of automatics
12626 );
12627
12628 If the modport is part of a UVM monitor/driver class, this
12629 creates a wrapper module that may be used to instantiate the
12630 driver/monitor using AUTOINST in the testbench."
12631 (save-excursion
12632 (let* ((params (verilog-read-auto-params 2 3))
12633 (submod (nth 0 params))
12634 (modport-re (nth 1 params))
12635 (regexp (nth 2 params))
12636 direction-re submodi) ; direction argument not supported until requested
12637 ;; Lookup position, etc of co-module
12638 ;; Note this may raise an error
12639 (when (setq submodi (verilog-modi-lookup submod t))
12640 (let* ((indent-pt (current-indentation))
12641 (v2k (verilog-in-paren-quick))
12642 (modi (verilog-modi-current))
12643 (moddecls (verilog-modi-get-decls modi))
12644 (submoddecls (verilog-modi-get-decls submodi))
12645 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
12646 (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
12647 (verilog-decls-get-vars submoddecls)
12648 (verilog-signals-not-in
12649 (verilog-decls-get-inputs submodportdecls)
12650 (append (verilog-decls-get-ports submoddecls)
12651 (verilog-decls-get-ports moddecls)))))
12652 (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
12653 (verilog-decls-get-vars submoddecls)
12654 (verilog-signals-not-in
12655 (verilog-decls-get-outputs submodportdecls)
12656 (append (verilog-decls-get-ports submoddecls)
12657 (verilog-decls-get-ports moddecls)))))
12658 (sig-list-io (verilog-signals-in ; Decls doesn't have data types, must resolve
12659 (verilog-decls-get-vars submoddecls)
12660 (verilog-signals-not-in
12661 (verilog-decls-get-inouts submodportdecls)
12662 (append (verilog-decls-get-ports submoddecls)
12663 (verilog-decls-get-ports moddecls))))))
12664 (forward-line 1)
12665 (setq sig-list-i (verilog-signals-edit-wire-reg
12666 (verilog-signals-matching-dir-re
12667 (verilog-signals-matching-regexp sig-list-i regexp)
12668 "input" direction-re))
12669 sig-list-o (verilog-signals-edit-wire-reg
12670 (verilog-signals-matching-dir-re
12671 (verilog-signals-matching-regexp sig-list-o regexp)
12672 "output" direction-re))
12673 sig-list-io (verilog-signals-edit-wire-reg
12674 (verilog-signals-matching-dir-re
12675 (verilog-signals-matching-regexp sig-list-io regexp)
12676 "inout" direction-re)))
12677 (when v2k (verilog-repair-open-comma))
12678 (when (or sig-list-i sig-list-o sig-list-io)
12679 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from modport)\n")
12680 ;; Don't sort them so an upper AUTOINST will match the main module
12681 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12682 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12683 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12684 (verilog-insert-indent "// End of automatics\n"))
12685 (when v2k (verilog-repair-close-comma)))))))
12686
12687 (defun verilog-auto-insert-lisp ()
12688 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
12689 The Lisp code provided is called before other AUTOS are expanded,
12690 and the Lisp code generally will call `insert' to insert text
12691 into the current file beginning on the line after the
12692 AUTOINSERTLISP.
12693
12694 See also AUTOINSERTLAST and `verilog-auto-insert-last' which
12695 executes after (as opposed to before) other AUTOs.
12696
12697 See also AUTO_LISP, which takes a Lisp expression and evaluates
12698 it during `verilog-auto-inst' but does not insert any text.
12699
12700 An example:
12701
12702 module ExampInsertLisp;
12703 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
12704 endmodule
12705
12706 // For this example we declare the function in the
12707 // module's file itself. Often you'd define it instead
12708 // in a site-start.el or init file.
12709 /*
12710 Local Variables:
12711 eval:
12712 (defun my-verilog-insert-hello (who)
12713 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
12714 End:
12715 */
12716
12717 Typing \\[verilog-auto] will call my-verilog-insert-hello and
12718 expand the above into:
12719
12720 // Beginning of automatic insert lisp
12721 initial $write(\"hello world\");
12722 // End of automatics
12723
12724 You can also call an external program and insert the returned
12725 text:
12726
12727 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
12728 // Beginning of automatic insert lisp
12729 //hello
12730 // End of automatics"
12731 (save-excursion
12732 ;; Point is at end of /*AUTO...*/
12733 (let* ((indent-pt (current-indentation))
12734 (cmd-end-pt (save-excursion (search-backward ")")
12735 (forward-char)
12736 (point))) ; Closing paren
12737 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
12738 (backward-sexp 1) ; Inside comment
12739 (point))) ; Beginning paren
12740 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
12741 (verilog-forward-or-insert-line)
12742 ;; Some commands don't move point (like insert-file) so we always
12743 ;; add the begin/end comments, then delete it if not needed
12744 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
12745 (verilog-insert-indent "// End of automatics\n")
12746 (forward-line -1)
12747 (eval (read cmd))
12748 (forward-line -1)
12749 (setq verilog-scan-cache-tick nil) ; Clear cache; inserted unknown text
12750 (verilog-delete-empty-auto-pair))))
12751
12752 (defun verilog-auto-insert-last ()
12753 "Expand AUTOINSERTLAST statements, as part of \\[verilog-auto].
12754 The Lisp code provided is called after all other AUTOS have been
12755 expanded, and the Lisp code generally will call `insert' to
12756 insert text into the current file beginning on the line after the
12757 AUTOINSERTLAST.
12758
12759 Other than when called (after AUTOs are expanded), the functionality
12760 is otherwise identical to AUTOINSERTLISP and `verilog-auto-insert-lisp' which
12761 executes before (as opposed to after) other AUTOs.
12762
12763 See `verilog-auto-insert-lisp' for examples."
12764 (verilog-auto-insert-lisp))
12765
12766 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
12767 "Return list of signals for current AUTOSENSE block."
12768 (let* ((sigss (save-excursion
12769 (search-forward ")")
12770 (verilog-read-always-signals)))
12771 (sig-list (verilog-signals-not-params
12772 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
12773 (append (and (not verilog-auto-sense-include-inputs)
12774 (verilog-alw-get-outputs-delayed sigss))
12775 (and (not verilog-auto-sense-include-inputs)
12776 (verilog-alw-get-outputs-immediate sigss))
12777 (verilog-alw-get-temps sigss)
12778 (verilog-decls-get-consts moddecls)
12779 (verilog-decls-get-gparams moddecls)
12780 presense-sigs)))))
12781 sig-list))
12782
12783 (defun verilog-auto-sense ()
12784 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
12785 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
12786 with one automatically derived from all inputs declared in the always
12787 statement. Signals that are generated within the same always block are NOT
12788 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
12789 Long lines are split based on the `fill-column', see \\[set-fill-column].
12790
12791 Limitations:
12792 Verilog does not allow memories (multidimensional arrays) in sensitivity
12793 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
12794
12795 Constant signals:
12796 AUTOSENSE cannot always determine if a \\=`define is a constant or a signal
12797 (it could be in an include file for example). If a \\=`define or other signal
12798 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
12799 declaration anywhere in the module (parenthesis are required):
12800
12801 /* AUTO_CONSTANT ( \\=`this_is_really_constant_dont_autosense_it ) */
12802
12803 Better yet, use a parameter, which will be understood to be constant
12804 automatically.
12805
12806 OOps!
12807 If AUTOSENSE makes a mistake, please report it. (First try putting
12808 a begin/end after your always!) As a workaround, if a signal that
12809 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
12810 If a signal should be in the sensitivity list wasn't, placing it before
12811 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
12812 autos are updated (or added if it occurs there already).
12813
12814 An example:
12815
12816 always @ (/*AS*/) begin
12817 /* AUTO_CONSTANT (\\=`constant) */
12818 outin = ina | inb | \\=`constant;
12819 out = outin;
12820 end
12821
12822 Typing \\[verilog-auto] will make this into:
12823
12824 always @ (/*AS*/ina or inb) begin
12825 /* AUTO_CONSTANT (\\=`constant) */
12826 outin = ina | inb | \\=`constant;
12827 out = outin;
12828 end
12829
12830 Note in Verilog 2001, you can often get the same result from the new @*
12831 operator. (This was added to the language in part due to AUTOSENSE!)
12832
12833 always @* begin
12834 outin = ina | inb | \\=`constant;
12835 out = outin;
12836 end"
12837 (save-excursion
12838 ;; Find beginning
12839 (let* ((start-pt (save-excursion
12840 (verilog-re-search-backward-quick "(" nil t)
12841 (point)))
12842 (indent-pt (save-excursion
12843 (or (and (goto-char start-pt) (1+ (current-column)))
12844 (current-indentation))))
12845 (modi (verilog-modi-current))
12846 (moddecls (verilog-modi-get-decls modi))
12847 (sig-memories (verilog-signals-memory
12848 (verilog-decls-get-vars moddecls)))
12849 sig-list not-first presense-sigs)
12850 ;; Read signals in always, eliminate outputs from sense list
12851 (setq presense-sigs (verilog-signals-from-signame
12852 (save-excursion
12853 (verilog-read-signals start-pt (point)))))
12854 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
12855 (when sig-memories
12856 (let ((tlen (length sig-list)))
12857 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
12858 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
12859 (if (and presense-sigs ; Add a "or" if not "(.... or /*AUTOSENSE*/"
12860 (save-excursion (goto-char (point))
12861 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12862 (verilog-re-search-backward-quick "\\s-" start-pt t)
12863 (while (looking-at "\\s-`endif")
12864 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12865 (verilog-re-search-backward-quick "\\s-" start-pt t))
12866 (not (looking-at "\\s-or\\b"))))
12867 (setq not-first t))
12868 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12869 (while sig-list
12870 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
12871 (insert "\n")
12872 (indent-to indent-pt)
12873 (if not-first (insert "or ")))
12874 (not-first (insert " or ")))
12875 (insert (verilog-sig-name (car sig-list)))
12876 (setq sig-list (cdr sig-list)
12877 not-first t)))))
12878
12879 (defun verilog-auto-reset ()
12880 "Expand AUTORESET statements, as part of \\[verilog-auto].
12881 Replace the /*AUTORESET*/ comment with code to initialize all
12882 registers set elsewhere in the always block.
12883
12884 Limitations:
12885 AUTORESET will not clear memories.
12886
12887 AUTORESET uses <= if the signal has a <= assignment in the block,
12888 else it uses =.
12889
12890 If <= is used, all = assigned variables are ignored if
12891 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
12892 to be temporaries.
12893
12894 /*AUTORESET*/ presumes that any signals mentioned between the previous
12895 begin/case/if statement and the AUTORESET comment are being reset manually
12896 and should not be automatically reset. This includes omitting any signals
12897 used on the right hand side of assignments.
12898
12899 By default, AUTORESET will include the width of the signal in the
12900 autos, SystemVerilog designs may want to change this. To control
12901 this behavior, see `verilog-auto-reset-widths'. In some cases
12902 AUTORESET must use a \\='0 assignment and it will print NOWIDTH; use
12903 `verilog-auto-reset-widths' unbased to prevent this.
12904
12905 AUTORESET ties signals to deasserted, which is presumed to be zero.
12906 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12907 them to a one.
12908
12909 AUTORESET may try to reset arrays or structures that cannot be
12910 reset by a simple assignment, resulting in compile errors. This
12911 is a feature to be taken as a hint that you need to reset these
12912 signals manually (or put them into a \"\\=`ifdef NEVER signal<=\\=`0;
12913 \\=`endif\" so Verilog-Mode ignores them.)
12914
12915 An example:
12916
12917 always @(posedge clk or negedge reset_l) begin
12918 if (!reset_l) begin
12919 c <= 1;
12920 /*AUTORESET*/
12921 end
12922 else begin
12923 a <= in_a;
12924 b <= in_b;
12925 c <= in_c;
12926 end
12927 end
12928
12929 Typing \\[verilog-auto] will make this into:
12930
12931 always @(posedge core_clk or negedge reset_l) begin
12932 if (!reset_l) begin
12933 c <= 1;
12934 /*AUTORESET*/
12935 // Beginning of autoreset for uninitialized flops
12936 a <= 0;
12937 b = 0; // if `verilog-auto-reset-blocking-in-non' true
12938 // End of automatics
12939 end
12940 else begin
12941 a <= in_a;
12942 b = in_b;
12943 c <= in_c;
12944 end
12945 end"
12946
12947 (interactive)
12948 (save-excursion
12949 ;; Find beginning
12950 (let* ((indent-pt (current-indentation))
12951 (modi (verilog-modi-current))
12952 (moddecls (verilog-modi-get-decls modi))
12953 (all-list (verilog-decls-get-signals moddecls))
12954 sigss sig-list dly-list prereset-sigs)
12955 ;; Read signals in always, eliminate outputs from reset list
12956 (setq prereset-sigs (verilog-signals-from-signame
12957 (save-excursion
12958 (verilog-read-signals
12959 (save-excursion
12960 (verilog-re-search-backward-quick
12961 "\\(@\\|\\<\\(begin\\|if\\|case\\|always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
12962 (point))
12963 (point)))))
12964 (save-excursion
12965 (verilog-re-search-backward-quick "\\(@\\|\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
12966 (setq sigss (verilog-read-always-signals)))
12967 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
12968 (setq sig-list (verilog-signals-not-in-struct
12969 (append
12970 (verilog-alw-get-outputs-delayed sigss)
12971 (when (or (not (verilog-alw-get-uses-delayed sigss))
12972 verilog-auto-reset-blocking-in-non)
12973 (verilog-alw-get-outputs-immediate sigss)))
12974 (append
12975 (verilog-alw-get-temps sigss)
12976 prereset-sigs)))
12977 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12978 (when sig-list
12979 (insert "\n");
12980 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
12981 (while sig-list
12982 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ; As sig-list has no widths
12983 (car sig-list))))
12984 (indent-to indent-pt)
12985 (insert (verilog-sig-name sig)
12986 (if (assoc (verilog-sig-name sig) dly-list)
12987 (concat " <= " verilog-assignment-delay)
12988 " = ")
12989 (verilog-sig-tieoff sig)
12990 ";\n")
12991 (setq sig-list (cdr sig-list))))
12992 (verilog-insert-indent "// End of automatics")))))
12993
12994 (defun verilog-auto-tieoff ()
12995 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
12996 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
12997 signals to deasserted.
12998
12999 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
13000 input/output list as another module, but no internals. Specifically, it
13001 finds all outputs in the module, and if that input is not otherwise declared
13002 as a register or wire, creates a tieoff.
13003
13004 AUTORESET ties signals to deasserted, which is presumed to be zero.
13005 Signals that match `verilog-active-low-regexp' will be deasserted by tying
13006 them to a one.
13007
13008 You can add signals you do not want included in AUTOTIEOFF with
13009 `verilog-auto-tieoff-ignore-regexp'.
13010
13011 `verilog-auto-wire-type' may be used to change the datatype of
13012 the declarations.
13013
13014 `verilog-auto-reset-widths' may be used to change how the tieoff
13015 value's width is generated.
13016
13017 An example of making a stub for another module:
13018
13019 module ExampStub (/*AUTOINST*/);
13020 /*AUTOINOUTPARAM(\"Foo\")*/
13021 /*AUTOINOUTMODULE(\"Foo\")*/
13022 /*AUTOTIEOFF*/
13023 // verilator lint_off UNUSED
13024 wire _unused_ok = &{1\\='b0,
13025 /*AUTOUNUSED*/
13026 1\\='b0};
13027 // verilator lint_on UNUSED
13028 endmodule
13029
13030 Typing \\[verilog-auto] will make this into:
13031
13032 module ExampStub (/*AUTOINST*/...);
13033 /*AUTOINOUTPARAM(\"Foo\")*/
13034 /*AUTOINOUTMODULE(\"Foo\")*/
13035 // Beginning of autotieoff
13036 output [2:0] foo;
13037 // End of automatics
13038
13039 /*AUTOTIEOFF*/
13040 // Beginning of autotieoff
13041 wire [2:0] foo = 3\\='b0;
13042 // End of automatics
13043 ...
13044 endmodule"
13045 (interactive)
13046 (save-excursion
13047 ;; Find beginning
13048 (let* ((indent-pt (current-indentation))
13049 (modi (verilog-modi-current))
13050 (moddecls (verilog-modi-get-decls modi))
13051 (modsubdecls (verilog-modi-get-sub-decls modi))
13052 (sig-list (verilog-signals-not-in
13053 (verilog-decls-get-outputs moddecls)
13054 (append (verilog-decls-get-vars moddecls)
13055 (verilog-decls-get-assigns moddecls)
13056 (verilog-decls-get-consts moddecls)
13057 (verilog-decls-get-gparams moddecls)
13058 (verilog-subdecls-get-interfaced modsubdecls)
13059 (verilog-subdecls-get-outputs modsubdecls)
13060 (verilog-subdecls-get-inouts modsubdecls)))))
13061 (setq sig-list (verilog-signals-not-matching-regexp
13062 sig-list verilog-auto-tieoff-ignore-regexp))
13063 (when sig-list
13064 (verilog-forward-or-insert-line)
13065 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
13066 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
13067 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
13068 (while sig-list
13069 (let ((sig (car sig-list)))
13070 (cond ((equal verilog-auto-tieoff-declaration "assign")
13071 (indent-to indent-pt)
13072 (insert "assign " (verilog-sig-name sig)))
13073 (t
13074 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
13075 (indent-to (max 48 (+ indent-pt 40)))
13076 (insert "= " (verilog-sig-tieoff sig)
13077 ";\n")
13078 (setq sig-list (cdr sig-list))))
13079 (verilog-insert-indent "// End of automatics\n")))))
13080
13081 (defun verilog-auto-undef ()
13082 "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
13083 Take any \\=`defines since the last AUTOUNDEF in the current file
13084 and create \\=`undefs for them. This is used to insure that
13085 file-local defines do not pollute the global \\=`define name space.
13086
13087 Limitations:
13088 AUTOUNDEF presumes any identifier following \\=`define is the
13089 name of a define. Any \\=`ifdefs are ignored.
13090
13091 AUTOUNDEF suppresses creating an \\=`undef for any define that was
13092 \\=`undefed before the AUTOUNDEF. This may be used to work around
13093 the ignoring of \\=`ifdefs as shown below.
13094
13095 An example:
13096
13097 \\=`define XX_FOO
13098 \\=`define M_BAR(x)
13099 \\=`define M_BAZ
13100 ...
13101 \\=`ifdef NEVER
13102 \\=`undef M_BAZ // Emacs will see this and not \\=`undef M_BAZ
13103 \\=`endif
13104 ...
13105 /*AUTOUNDEF*/
13106
13107 Typing \\[verilog-auto] will make this into:
13108
13109 ...
13110 /*AUTOUNDEF*/
13111 // Beginning of automatic undefs
13112 \\=`undef XX_FOO
13113 \\=`undef M_BAR
13114 // End of automatics
13115
13116 You may also provide an optional regular expression, in which case only
13117 defines the regular expression will be undefed."
13118 (save-excursion
13119 (let* ((params (verilog-read-auto-params 0 1))
13120 (regexp (nth 0 params))
13121 (indent-pt (current-indentation))
13122 (end-pt (point))
13123 defs def)
13124 (save-excursion
13125 ;; Scan from start of file, or last AUTOUNDEF
13126 (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
13127 (goto-char (point-min)))
13128 (while (verilog-re-search-forward-quick
13129 "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
13130 (cond ((equal (match-string-no-properties 1) "define")
13131 (setq def (match-string-no-properties 2))
13132 (when (and (or (not regexp)
13133 (string-match regexp def))
13134 (not (member def defs))) ; delete-dups not in 21.1
13135 (setq defs (cons def defs))))
13136 (t
13137 (setq defs (delete (match-string-no-properties 2) defs))))))
13138 ;; Insert
13139 (setq defs (sort defs 'string<))
13140 (when defs
13141 (verilog-forward-or-insert-line)
13142 (verilog-insert-indent "// Beginning of automatic undefs\n")
13143 (while defs
13144 (verilog-insert-indent "`undef " (car defs) "\n")
13145 (setq defs (cdr defs)))
13146 (verilog-insert-indent "// End of automatics\n")))))
13147
13148 (defun verilog-auto-unused ()
13149 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
13150 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
13151 input and inout signals.
13152
13153 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
13154 input/output list as another module, but no internals. Specifically, it
13155 finds all inputs and inouts in the module, and if that input is not otherwise
13156 used, adds it to a comma separated list.
13157
13158 The comma separated list is intended to be used to create a _unused_ok
13159 signal. Using the exact name \"_unused_ok\" for name of the temporary
13160 signal is recommended as it will insure maximum forward compatibility, it
13161 also makes lint warnings easy to understand; ignore any unused warnings
13162 with \"unused\" in the signal name.
13163
13164 To reduce simulation time, the _unused_ok signal should be forced to a
13165 constant to prevent wiggling. The easiest thing to do is use a
13166 reduction-and with 1\\='b0 as shown.
13167
13168 This way all unused signals are in one place, making it convenient to add
13169 your tool's specific pragmas around the assignment to disable any unused
13170 warnings.
13171
13172 You can add signals you do not want included in AUTOUNUSED with
13173 `verilog-auto-unused-ignore-regexp'.
13174
13175 An example of making a stub for another module:
13176
13177 module ExampStub (/*AUTOINST*/);
13178 /*AUTOINOUTPARAM(\"Examp\")*/
13179 /*AUTOINOUTMODULE(\"Examp\")*/
13180 /*AUTOTIEOFF*/
13181 // verilator lint_off UNUSED
13182 wire _unused_ok = &{1\\='b0,
13183 /*AUTOUNUSED*/
13184 1\\='b0};
13185 // verilator lint_on UNUSED
13186 endmodule
13187
13188 Typing \\[verilog-auto] will make this into:
13189
13190 ...
13191 // verilator lint_off UNUSED
13192 wire _unused_ok = &{1\\='b0,
13193 /*AUTOUNUSED*/
13194 // Beginning of automatics
13195 unused_input_a,
13196 unused_input_b,
13197 unused_input_c,
13198 // End of automatics
13199 1\\='b0};
13200 // verilator lint_on UNUSED
13201 endmodule"
13202 (interactive)
13203 (save-excursion
13204 ;; Find beginning
13205 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
13206 (modi (verilog-modi-current))
13207 (moddecls (verilog-modi-get-decls modi))
13208 (modsubdecls (verilog-modi-get-sub-decls modi))
13209 (sig-list (verilog-signals-not-in
13210 (append (verilog-decls-get-inputs moddecls)
13211 (verilog-decls-get-inouts moddecls))
13212 (append (verilog-subdecls-get-inputs modsubdecls)
13213 (verilog-subdecls-get-inouts modsubdecls)))))
13214 (setq sig-list (verilog-signals-not-matching-regexp
13215 sig-list verilog-auto-unused-ignore-regexp))
13216 (when sig-list
13217 (verilog-forward-or-insert-line)
13218 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
13219 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
13220 (while sig-list
13221 (let ((sig (car sig-list)))
13222 (indent-to indent-pt)
13223 (insert (verilog-sig-name sig) ",\n")
13224 (setq sig-list (cdr sig-list))))
13225 (verilog-insert-indent "// End of automatics\n")))))
13226
13227 (defun verilog-enum-ascii (signm elim-regexp)
13228 "Convert an enum name SIGNM to an ascii string for insertion.
13229 Remove user provided prefix ELIM-REGEXP."
13230 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
13231 (let ((case-fold-search t))
13232 ;; All upper becomes all lower for readability
13233 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
13234
13235 (defun verilog-auto-ascii-enum ()
13236 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
13237 Create a register to contain the ASCII decode of an enumerated signal type.
13238 This will allow trace viewers to show the ASCII name of states.
13239
13240 First, parameters are built into an enumeration using the synopsys enum
13241 comment. The comment must be between the keyword and the symbol.
13242 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
13243
13244 Next, registers which that enum applies to are also tagged with the same
13245 enum.
13246
13247 Finally, an AUTOASCIIENUM command is used.
13248
13249 The first parameter is the name of the signal to be decoded.
13250
13251 The second parameter is the name to store the ASCII code into. For the
13252 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
13253 a signal that is just for simulation, and the magic characters _ascii
13254 tell viewers like Dinotrace to display in ASCII format.
13255
13256 The third optional parameter is a string which will be removed
13257 from the state names. It defaults to \"\" which removes nothing.
13258
13259 The fourth optional parameter is \"onehot\" to force one-hot
13260 decoding. If unspecified, if and only if the first parameter
13261 width is 2^(number of states in enum) and does NOT match the
13262 width of the enum, the signal is assumed to be a one-hot
13263 decode. Otherwise, it's a normal encoded state vector.
13264
13265 `verilog-auto-wire-type' may be used to change the datatype of
13266 the declarations.
13267
13268 \"auto enum\" may be used in place of \"synopsys enum\".
13269
13270 An example:
13271
13272 //== State enumeration
13273 parameter [2:0] // synopsys enum state_info
13274 SM_IDLE = 3\\='b000,
13275 SM_SEND = 3\\='b001,
13276 SM_WAIT1 = 3\\='b010;
13277 //== State variables
13278 reg [2:0] /* synopsys enum state_info */
13279 state_r; /* synopsys state_vector state_r */
13280 reg [2:0] /* synopsys enum state_info */
13281 state_e1;
13282
13283 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
13284
13285 Typing \\[verilog-auto] will make this into:
13286
13287 ... same front matter ...
13288
13289 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
13290 // Beginning of automatic ASCII enum decoding
13291 reg [39:0] state_ascii_r; // Decode of state_r
13292 always @(state_r) begin
13293 case ({state_r})
13294 SM_IDLE: state_ascii_r = \"idle \";
13295 SM_SEND: state_ascii_r = \"send \";
13296 SM_WAIT1: state_ascii_r = \"wait1\";
13297 default: state_ascii_r = \"%Erro\";
13298 endcase
13299 end
13300 // End of automatics"
13301 (save-excursion
13302 (let* ((params (verilog-read-auto-params 2 4))
13303 (undecode-name (nth 0 params))
13304 (ascii-name (nth 1 params))
13305 (elim-regexp (and (nth 2 params)
13306 (not (equal (nth 2 params) ""))
13307 (nth 2 params)))
13308 (one-hot-flag (nth 3 params))
13309 ;;
13310 (indent-pt (current-indentation))
13311 (modi (verilog-modi-current))
13312 (moddecls (verilog-modi-get-decls modi))
13313 ;;
13314 (sig-list-consts (append (verilog-decls-get-consts moddecls)
13315 (verilog-decls-get-gparams moddecls)))
13316 (sig-list-all (verilog-decls-get-iovars moddecls))
13317 ;;
13318 (undecode-sig (or (assoc undecode-name sig-list-all)
13319 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
13320 (undecode-enum (or (verilog-sig-enum undecode-sig)
13321 (error "%s: Signal %s does not have an enum tag" (verilog-point-text) undecode-name)))
13322 ;;
13323 (enum-sigs (verilog-signals-not-in
13324 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
13325 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
13326 nil))
13327 ;;
13328 (one-hot (or
13329 (string-match "onehot" (or one-hot-flag ""))
13330 (and ; width(enum) != width(sig)
13331 (or (not (verilog-sig-bits (car enum-sigs)))
13332 (not (equal (verilog-sig-width (car enum-sigs))
13333 (verilog-sig-width undecode-sig))))
13334 ;; count(enums) == width(sig)
13335 (equal (number-to-string (length enum-sigs))
13336 (verilog-sig-width undecode-sig)))))
13337 (enum-chars 0)
13338 (ascii-chars 0))
13339 ;;
13340 ;; Find number of ascii chars needed
13341 (let ((tmp-sigs enum-sigs))
13342 (while tmp-sigs
13343 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
13344 ascii-chars (max ascii-chars (length (verilog-enum-ascii
13345 (verilog-sig-name (car tmp-sigs))
13346 elim-regexp)))
13347 tmp-sigs (cdr tmp-sigs))))
13348 ;;
13349 (verilog-forward-or-insert-line)
13350 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
13351 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
13352 (concat "Decode of " undecode-name) nil nil))))
13353 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
13354 ;;
13355 (verilog-insert-indent "always @(" undecode-name ") begin\n")
13356 (setq indent-pt (+ indent-pt verilog-indent-level))
13357 (verilog-insert-indent "case ({" undecode-name "})\n")
13358 (setq indent-pt (+ indent-pt verilog-case-indent))
13359 ;;
13360 (let ((tmp-sigs enum-sigs)
13361 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
13362 (+ (if one-hot 9 1) (max 8 enum-chars))
13363 ascii-name ascii-chars))
13364 (errname (substring "%Error" 0 (min 6 ascii-chars))))
13365 (while tmp-sigs
13366 (verilog-insert-indent
13367 (concat
13368 (format chrfmt
13369 (concat (if one-hot "(")
13370 ;; Use enum-sigs length as that's numeric
13371 ;; verilog-sig-width undecode-sig might not be.
13372 (if one-hot (number-to-string (length enum-sigs)))
13373 ;; We use a shift instead of var[index]
13374 ;; so that a non-one hot value will show as error.
13375 (if one-hot "'b1<<")
13376 (verilog-sig-name (car tmp-sigs))
13377 (if one-hot ")") ":")
13378 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
13379 elim-regexp))))
13380 (setq tmp-sigs (cdr tmp-sigs)))
13381 (verilog-insert-indent (format chrfmt "default:" errname)))
13382 ;;
13383 (setq indent-pt (- indent-pt verilog-case-indent))
13384 (verilog-insert-indent "endcase\n")
13385 (setq indent-pt (- indent-pt verilog-indent-level))
13386 (verilog-insert-indent "end\n"
13387 "// End of automatics\n"))))
13388
13389 (defun verilog-auto-templated-rel ()
13390 "Replace Templated relative line numbers with absolute line numbers.
13391 Internal use only. This hacks around the line numbers in AUTOINST Templates
13392 being different from the final output's line numbering."
13393 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
13394 ;; Find line number each template is on
13395 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
13396 (goto-char (point-min))
13397 (while (not (eobp))
13398 (when (looking-at ".*AUTO_TEMPLATE")
13399 (setq templateno (1+ templateno))
13400 (setq template-line (cons buf-line template-line)))
13401 (setq buf-line (1+ buf-line))
13402 (forward-line 1))
13403 (setq template-line (nreverse template-line))
13404 ;; Replace T# L# with absolute line number
13405 (goto-char (point-min))
13406 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
13407 (replace-match
13408 (concat " Templated "
13409 (int-to-string (+ (nth (string-to-number (match-string 1))
13410 template-line)
13411 (string-to-number (match-string 2)))))
13412 t t))))
13413
13414 (defun verilog-auto-template-lint ()
13415 "Check AUTO_TEMPLATEs for unused lines.
13416 Enable with `verilog-auto-template-warn-unused'."
13417 (let ((name1 (or (buffer-file-name) (buffer-name))))
13418 (save-excursion
13419 (goto-char (point-min))
13420 (while (re-search-forward
13421 "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
13422 (let* ((tpl-info (verilog-read-auto-template-middle))
13423 (tpl-list (aref tpl-info 1))
13424 (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
13425 tpl-ass)
13426 (while tlines
13427 (setq tpl-ass (car tlines)
13428 tlines (cdr tlines))
13429 ;;
13430 (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ; Not supported, no warning
13431 (not verilog-auto-template-hits)
13432 (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
13433 verilog-auto-template-hits))
13434 (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
13435 name1
13436 (+ (elt tpl-ass 3) ; Template line number
13437 (count-lines (point-min) (point)))
13438 (elt tpl-ass 0) (elt tpl-ass 1))
13439 )))))))
13440
13441 \f
13442 ;;; Auto top level:
13443 ;;
13444
13445 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
13446 "Expand AUTO statements.
13447 Look for any /*AUTO...*/ commands in the code, as used in
13448 instantiations or argument headers. Update the list of signals
13449 following the /*AUTO...*/ command.
13450
13451 Use \\[verilog-delete-auto] to remove the AUTOs.
13452
13453 Use \\[verilog-diff-auto] to see differences in AUTO expansion.
13454
13455 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
13456
13457 Use \\[verilog-faq] for a pointer to frequently asked questions.
13458
13459 For new users, we recommend setting `verilog-case-fold' to nil
13460 and `verilog-auto-arg-sort' to t.
13461
13462 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
13463 called before and after this function, respectively.
13464
13465 For example:
13466 module ModuleName (/*AUTOARG*/);
13467 /*AUTOINPUT*/
13468 /*AUTOOUTPUT*/
13469 /*AUTOWIRE*/
13470 /*AUTOREG*/
13471 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
13472
13473 You can also update the AUTOs from the shell using:
13474 emacs --batch <filenames.v> -f verilog-batch-auto
13475 Or fix indentation with:
13476 emacs --batch <filenames.v> -f verilog-batch-indent
13477 Likewise, you can delete or inject AUTOs with:
13478 emacs --batch <filenames.v> -f verilog-batch-delete-auto
13479 emacs --batch <filenames.v> -f verilog-batch-inject-auto
13480 Or check if AUTOs have the same expansion
13481 emacs --batch <filenames.v> -f verilog-batch-diff-auto
13482
13483 Using \\[describe-function], see also:
13484 `verilog-auto-arg' for AUTOARG module instantiations
13485 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
13486 `verilog-auto-assign-modport' for AUTOASSIGNMODPORT assignment to/from modport
13487 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
13488 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
13489 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
13490 `verilog-auto-inout-modport' for AUTOINOUTMODPORT i/o from an interface modport
13491 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
13492 `verilog-auto-inout-param' for AUTOINOUTPARAM copying params from elsewhere
13493 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
13494 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
13495 `verilog-auto-insert-last' for AUTOINSERTLAST insert code from lisp function
13496 `verilog-auto-inst' for AUTOINST instantiation pins
13497 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
13498 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
13499 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
13500 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
13501 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
13502 `verilog-auto-reg' for AUTOREG registers
13503 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
13504 `verilog-auto-reset' for AUTORESET flop resets
13505 `verilog-auto-sense' for AUTOSENSE or AS always sensitivity lists
13506 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
13507 `verilog-auto-undef' for AUTOUNDEF \\=`undef of local \\=`defines
13508 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
13509 `verilog-auto-wire' for AUTOWIRE instantiation wires
13510
13511 `verilog-read-defines' for reading \\=`define values
13512 `verilog-read-includes' for reading \\=`includes
13513
13514 If you have bugs with these autos, please file an issue at
13515 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
13516 Wilson Snyder (wsnyder@wsnyder.org)."
13517 (interactive)
13518 (unless noninteractive (message "Updating AUTOs..."))
13519 (if (fboundp 'dinotrace-unannotate-all)
13520 (dinotrace-unannotate-all))
13521 (verilog-save-font-mods
13522 (let ((oldbuf (if (not (buffer-modified-p))
13523 (buffer-string)))
13524 (case-fold-search verilog-case-fold)
13525 ;; Cache directories; we don't write new files, so can't change
13526 (verilog-dir-cache-preserving t)
13527 ;; Cache current module
13528 (verilog-modi-cache-current-enable t)
13529 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
13530 verilog-modi-cache-current)
13531 (unwind-protect
13532 ;; Disable change hooks for speed
13533 ;; This let can't be part of above let; must restore
13534 ;; after-change-functions before font-lock resumes
13535 (verilog-save-no-change-functions
13536 (verilog-save-scan-cache
13537 (save-excursion
13538 ;; Wipe cache; otherwise if we AUTOed a block above this one,
13539 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
13540 (setq verilog-modi-cache-list nil)
13541 ;; Local state
13542 (verilog-read-auto-template-init)
13543 ;; If we're not in verilog-mode, change syntax table so parsing works right
13544 (unless (eq major-mode `verilog-mode) (verilog-mode))
13545 ;; Allow user to customize
13546 (verilog-run-hooks 'verilog-before-auto-hook)
13547 ;; Try to save the user from needing to revert-file to reread file local-variables
13548 (verilog-auto-reeval-locals)
13549 (verilog-read-auto-lisp-present)
13550 (verilog-read-auto-lisp (point-min) (point-max))
13551 (verilog-getopt-flags)
13552 ;; From here on out, we can cache anything we read from disk
13553 (verilog-preserve-dir-cache
13554 ;; These two may seem obvious to do always, but on large includes it can be way too slow
13555 (when verilog-auto-read-includes
13556 (verilog-read-includes)
13557 (verilog-read-defines nil nil t))
13558 ;; Setup variables due to SystemVerilog expansion
13559 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
13560 ;; This particular ordering is important
13561 ;; INST: Lower modules correct, no internal dependencies, FIRST
13562 (verilog-preserve-modi-cache
13563 ;; Clear existing autos else we'll be screwed by existing ones
13564 (verilog-delete-auto)
13565 ;; Injection if appropriate
13566 (when inject
13567 (verilog-inject-inst)
13568 (verilog-inject-sense)
13569 (verilog-inject-arg))
13570 ;;
13571 ;; Do user inserts first, so their code can insert AUTOs
13572 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
13573 'verilog-auto-insert-lisp)
13574 ;; Expand instances before need the signals the instances input/output
13575 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
13576 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
13577 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
13578 ;; Doesn't matter when done, but combine it with a common changer
13579 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
13580 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
13581 ;; Must be done before autoin/out as creates a reg
13582 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM(.*?)\\*/" 'verilog-auto-ascii-enum)
13583 ;;
13584 ;; first in/outs from other files
13585 (verilog-auto-re-search-do "/\\*AUTOINOUTMODPORT(.*?)\\*/" 'verilog-auto-inout-modport)
13586 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE(.*?)\\*/" 'verilog-auto-inout-module)
13587 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP(.*?)\\*/" 'verilog-auto-inout-comp)
13588 (verilog-auto-re-search-do "/\\*AUTOINOUTIN(.*?)\\*/" 'verilog-auto-inout-in)
13589 (verilog-auto-re-search-do "/\\*AUTOINOUTPARAM(.*?)\\*/" 'verilog-auto-inout-param)
13590 ;; next in/outs which need previous sucked inputs first
13591 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((.*?)\\)?\\*/" 'verilog-auto-output)
13592 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((.*?)\\)?\\*/" 'verilog-auto-input)
13593 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((.*?)\\)?\\*/" 'verilog-auto-inout)
13594 ;; Then tie off those in/outs
13595 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
13596 ;; These can be anywhere after AUTOINSERTLISP
13597 (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((.*?)\\)?\\*/" 'verilog-auto-undef)
13598 ;; Wires/regs must be after inputs/outputs
13599 (verilog-auto-re-search-do "/\\*AUTOASSIGNMODPORT(.*?)\\*/" 'verilog-auto-assign-modport)
13600 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
13601 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
13602 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
13603 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
13604 ;; outputevery needs AUTOOUTPUTs done first
13605 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\((.*?)\\)?\\*/" 'verilog-auto-output-every)
13606 ;; After we've created all new variables
13607 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
13608 ;; Must be after all inputs outputs are generated
13609 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
13610 ;; User inserts
13611 (verilog-auto-re-search-do "/\\*AUTOINSERTLAST(.*?)\\*/" 'verilog-auto-insert-last)
13612 ;; Fix line numbers (comments only)
13613 (when verilog-auto-inst-template-numbers
13614 (verilog-auto-templated-rel))
13615 (when verilog-auto-template-warn-unused
13616 (verilog-auto-template-lint))))
13617 ;;
13618 (verilog-run-hooks 'verilog-auto-hook)
13619 ;;
13620 (when verilog-auto-delete-trailing-whitespace
13621 (verilog-delete-trailing-whitespace))
13622 ;;
13623 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
13624 ;;
13625 ;; If end result is same as when started, clear modified flag
13626 (cond ((and oldbuf (equal oldbuf (buffer-string)))
13627 (set-buffer-modified-p nil)
13628 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
13629 (t (unless noninteractive (message "Updating AUTOs...done"))))
13630 ;; End of after-change protection
13631 )))
13632 ;; Unwind forms
13633 ;; Currently handled in verilog-save-font-mods
13634 ))))
13635 \f
13636 ;;; Skeletons:
13637 ;;
13638
13639 (defvar verilog-template-map
13640 (let ((map (make-sparse-keymap)))
13641 (define-key map "a" 'verilog-sk-always)
13642 (define-key map "b" 'verilog-sk-begin)
13643 (define-key map "c" 'verilog-sk-case)
13644 (define-key map "f" 'verilog-sk-for)
13645 (define-key map "g" 'verilog-sk-generate)
13646 (define-key map "h" 'verilog-sk-header)
13647 (define-key map "i" 'verilog-sk-initial)
13648 (define-key map "j" 'verilog-sk-fork)
13649 (define-key map "m" 'verilog-sk-module)
13650 (define-key map "o" 'verilog-sk-ovm-class)
13651 (define-key map "p" 'verilog-sk-primitive)
13652 (define-key map "r" 'verilog-sk-repeat)
13653 (define-key map "s" 'verilog-sk-specify)
13654 (define-key map "t" 'verilog-sk-task)
13655 (define-key map "u" 'verilog-sk-uvm-object)
13656 (define-key map "w" 'verilog-sk-while)
13657 (define-key map "x" 'verilog-sk-casex)
13658 (define-key map "z" 'verilog-sk-casez)
13659 (define-key map "?" 'verilog-sk-if)
13660 (define-key map ":" 'verilog-sk-else-if)
13661 (define-key map "/" 'verilog-sk-comment)
13662 (define-key map "A" 'verilog-sk-assign)
13663 (define-key map "F" 'verilog-sk-function)
13664 (define-key map "I" 'verilog-sk-input)
13665 (define-key map "O" 'verilog-sk-output)
13666 (define-key map "S" 'verilog-sk-state-machine)
13667 (define-key map "=" 'verilog-sk-inout)
13668 (define-key map "U" 'verilog-sk-uvm-component)
13669 (define-key map "W" 'verilog-sk-wire)
13670 (define-key map "R" 'verilog-sk-reg)
13671 (define-key map "D" 'verilog-sk-define-signal)
13672 map)
13673 "Keymap used in Verilog mode for smart template operations.")
13674
13675
13676 ;;
13677 ;; Place the templates into Verilog Mode. They may be inserted under any key.
13678 ;; C-c C-t will be the default. If you use templates a lot, you
13679 ;; may want to consider moving the binding to another key in your init
13680 ;; file.
13681 ;;
13682 ;; Note \C-c and letter are reserved for users
13683 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
13684
13685 ;; ---- statement skeletons ------------------------------------------
13686
13687 (define-skeleton verilog-sk-prompt-condition
13688 "Prompt for the loop condition."
13689 "[condition]: " str )
13690
13691 (define-skeleton verilog-sk-prompt-init
13692 "Prompt for the loop init statement."
13693 "[initial statement]: " str )
13694
13695 (define-skeleton verilog-sk-prompt-inc
13696 "Prompt for the loop increment statement."
13697 "[increment statement]: " str )
13698
13699 (define-skeleton verilog-sk-prompt-name
13700 "Prompt for the name of something."
13701 "[name]: " str)
13702
13703 (define-skeleton verilog-sk-prompt-clock
13704 "Prompt for the name of something."
13705 "name and edge of clock(s): " str)
13706
13707 (defvar verilog-sk-reset nil)
13708 (defun verilog-sk-prompt-reset ()
13709 "Prompt for the name of a state machine reset."
13710 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
13711
13712
13713 (define-skeleton verilog-sk-prompt-state-selector
13714 "Prompt for the name of a state machine selector."
13715 "name of selector (eg {a,b,c,d}): " str )
13716
13717 (define-skeleton verilog-sk-prompt-output
13718 "Prompt for the name of something."
13719 "output: " str)
13720
13721 (define-skeleton verilog-sk-prompt-msb
13722 "Prompt for most significant bit specification."
13723 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
13724
13725 (define-skeleton verilog-sk-prompt-lsb
13726 "Prompt for least significant bit specification."
13727 "lsb:" str )
13728
13729 (defvar verilog-sk-p nil)
13730 (define-skeleton verilog-sk-prompt-width
13731 "Prompt for a width specification."
13732 ()
13733 (progn
13734 (setq verilog-sk-p (point))
13735 (verilog-sk-prompt-msb)
13736 (if (> (point) verilog-sk-p) "] " " ")))
13737
13738 (defun verilog-sk-header ()
13739 "Insert a descriptive header at the top of the file.
13740 See also `verilog-header' for an alternative format."
13741 (interactive "*")
13742 (save-excursion
13743 (goto-char (point-min))
13744 (verilog-sk-header-tmpl)))
13745
13746 (define-skeleton verilog-sk-header-tmpl
13747 "Insert a comment block containing the module title, author, etc."
13748 "[Description]: "
13749 "// -*- Mode: Verilog -*-"
13750 "\n// Filename : " (buffer-name)
13751 "\n// Description : " str
13752 "\n// Author : " (user-full-name)
13753 "\n// Created On : " (current-time-string)
13754 "\n// Last Modified By: " (user-full-name)
13755 "\n// Last Modified On: " (current-time-string)
13756 "\n// Update Count : 0"
13757 "\n// Status : Unknown, Use with caution!"
13758 "\n")
13759
13760 (define-skeleton verilog-sk-module
13761 "Insert a module definition."
13762 ()
13763 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
13764 > _ \n
13765 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
13766
13767 ;; ------------------------------------------------------------------------
13768 ;; Define a default OVM class, with macros and new()
13769 ;; ------------------------------------------------------------------------
13770
13771 (define-skeleton verilog-sk-ovm-class
13772 "Insert a class definition"
13773 ()
13774 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13775 > _ \n
13776 > "`ovm_object_utils_begin(" name ")" \n
13777 > (- verilog-indent-level) " `ovm_object_utils_end" \n
13778 > _ \n
13779 > "function new(string name=\"" name "\");" \n
13780 > "super.new(name);" \n
13781 > (- verilog-indent-level) "endfunction" \n
13782 > _ \n
13783 > "endclass" (progn (electric-verilog-terminate-line) nil))
13784
13785 (define-skeleton verilog-sk-uvm-object
13786 "Insert a class definition"
13787 ()
13788 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13789 > _ \n
13790 > "`uvm_object_utils_begin(" name ")" \n
13791 > (- verilog-indent-level) "`uvm_object_utils_end" \n
13792 > _ \n
13793 > "function new(string name=\"" name "\");" \n
13794 > "super.new(name);" \n
13795 > (- verilog-indent-level) "endfunction" \n
13796 > _ \n
13797 > "endclass" (progn (electric-verilog-terminate-line) nil))
13798
13799 (define-skeleton verilog-sk-uvm-component
13800 "Insert a class definition"
13801 ()
13802 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13803 > _ \n
13804 > "`uvm_component_utils_begin(" name ")" \n
13805 > (- verilog-indent-level) "`uvm_component_utils_end" \n
13806 > _ \n
13807 > "function new(string name=\"\", uvm_component parent);" \n
13808 > "super.new(name, parent);" \n
13809 > (- verilog-indent-level) "endfunction" \n
13810 > _ \n
13811 > "endclass" (progn (electric-verilog-terminate-line) nil))
13812
13813 (define-skeleton verilog-sk-primitive
13814 "Insert a task definition."
13815 ()
13816 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
13817 > _ \n
13818 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
13819
13820 (define-skeleton verilog-sk-task
13821 "Insert a task definition."
13822 ()
13823 > "task " '(verilog-sk-prompt-name) & ?; \n
13824 > _ \n
13825 > "begin" \n
13826 > \n
13827 > (- verilog-indent-level-behavioral) "end" \n
13828 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
13829
13830 (define-skeleton verilog-sk-function
13831 "Insert a function definition."
13832 ()
13833 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
13834 > _ \n
13835 > "begin" \n
13836 > \n
13837 > (- verilog-indent-level-behavioral) "end" \n
13838 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
13839
13840 (define-skeleton verilog-sk-always
13841 "Insert always block. Uses the minibuffer to prompt
13842 for sensitivity list."
13843 ()
13844 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13845 > _ \n
13846 > (- verilog-indent-level-behavioral) "end" \n >
13847 )
13848
13849 (define-skeleton verilog-sk-initial
13850 "Insert an initial block."
13851 ()
13852 > "initial begin\n"
13853 > _ \n
13854 > (- verilog-indent-level-behavioral) "end" \n > )
13855
13856 (define-skeleton verilog-sk-specify
13857 "Insert specify block. "
13858 ()
13859 > "specify\n"
13860 > _ \n
13861 > (- verilog-indent-level-behavioral) "endspecify" \n > )
13862
13863 (define-skeleton verilog-sk-generate
13864 "Insert generate block. "
13865 ()
13866 > "generate\n"
13867 > _ \n
13868 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
13869
13870 (define-skeleton verilog-sk-begin
13871 "Insert begin end block. Uses the minibuffer to prompt for name."
13872 ()
13873 > "begin" '(verilog-sk-prompt-name) \n
13874 > _ \n
13875 > (- verilog-indent-level-behavioral) "end" )
13876
13877 (define-skeleton verilog-sk-fork
13878 "Insert a fork join block."
13879 ()
13880 > "fork\n"
13881 > "begin" \n
13882 > _ \n
13883 > (- verilog-indent-level-behavioral) "end" \n
13884 > "begin" \n
13885 > \n
13886 > (- verilog-indent-level-behavioral) "end" \n
13887 > (- verilog-indent-level-behavioral) "join" \n
13888 > )
13889
13890
13891 (define-skeleton verilog-sk-case
13892 "Build skeleton case statement, prompting for the selector expression,
13893 and the case items."
13894 "[selector expression]: "
13895 > "case (" str ") " \n
13896 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13897 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13898
13899 (define-skeleton verilog-sk-casex
13900 "Build skeleton casex statement, prompting for the selector expression,
13901 and the case items."
13902 "[selector expression]: "
13903 > "casex (" str ") " \n
13904 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13905 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13906
13907 (define-skeleton verilog-sk-casez
13908 "Build skeleton casez statement, prompting for the selector expression,
13909 and the case items."
13910 "[selector expression]: "
13911 > "casez (" str ") " \n
13912 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13913 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13914
13915 (define-skeleton verilog-sk-if
13916 "Insert a skeleton if statement."
13917 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
13918 > _ \n
13919 > (- verilog-indent-level-behavioral) "end " \n )
13920
13921 (define-skeleton verilog-sk-else-if
13922 "Insert a skeleton else if statement."
13923 > (verilog-indent-line) "else if ("
13924 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
13925 > _ \n
13926 > "end" (progn (electric-verilog-terminate-line) nil))
13927
13928 (define-skeleton verilog-sk-datadef
13929 "Common routine to get data definition."
13930 ()
13931 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
13932
13933 (define-skeleton verilog-sk-input
13934 "Insert an input definition."
13935 ()
13936 > "input [" '(verilog-sk-datadef))
13937
13938 (define-skeleton verilog-sk-output
13939 "Insert an output definition."
13940 ()
13941 > "output [" '(verilog-sk-datadef))
13942
13943 (define-skeleton verilog-sk-inout
13944 "Insert an inout definition."
13945 ()
13946 > "inout [" '(verilog-sk-datadef))
13947
13948 (defvar verilog-sk-signal nil)
13949 (define-skeleton verilog-sk-def-reg
13950 "Insert a reg definition."
13951 ()
13952 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
13953
13954 (defun verilog-sk-define-signal ()
13955 "Insert a definition of signal under point at top of module."
13956 (interactive "*")
13957 (let* ((sig-re "[a-zA-Z0-9_]*")
13958 (v1 (buffer-substring
13959 (save-excursion
13960 (skip-chars-backward sig-re)
13961 (point))
13962 (save-excursion
13963 (skip-chars-forward sig-re)
13964 (point)))))
13965 (if (not (member v1 verilog-keywords))
13966 (save-excursion
13967 (setq verilog-sk-signal v1)
13968 (verilog-beg-of-defun)
13969 (verilog-end-of-statement)
13970 (verilog-forward-syntactic-ws)
13971 (verilog-sk-def-reg)
13972 (message "signal at point is %s" v1))
13973 (message "object at point (%s) is a keyword" v1))))
13974
13975 (define-skeleton verilog-sk-wire
13976 "Insert a wire definition."
13977 ()
13978 > "wire [" '(verilog-sk-datadef))
13979
13980 (define-skeleton verilog-sk-reg
13981 "Insert a reg definition."
13982 ()
13983 > "reg [" '(verilog-sk-datadef))
13984
13985 (define-skeleton verilog-sk-assign
13986 "Insert a skeleton assign statement."
13987 ()
13988 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
13989
13990 (define-skeleton verilog-sk-while
13991 "Insert a skeleton while loop statement."
13992 ()
13993 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
13994 > _ \n
13995 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13996
13997 (define-skeleton verilog-sk-repeat
13998 "Insert a skeleton repeat loop statement."
13999 ()
14000 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
14001 > _ \n
14002 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
14003
14004 (define-skeleton verilog-sk-for
14005 "Insert a skeleton while loop statement."
14006 ()
14007 > "for ("
14008 '(verilog-sk-prompt-init) "; "
14009 '(verilog-sk-prompt-condition) "; "
14010 '(verilog-sk-prompt-inc)
14011 ") begin" \n
14012 > _ \n
14013 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
14014
14015 (define-skeleton verilog-sk-comment
14016 "Inserts three comment lines, making a display comment."
14017 ()
14018 > "/*\n"
14019 > "* " _ \n
14020 > "*/")
14021
14022 (define-skeleton verilog-sk-state-machine
14023 "Insert a state machine definition."
14024 "Name of state variable: "
14025 '(setq input "state")
14026 > "// State registers for " str | -23 \n
14027 '(setq verilog-sk-state str)
14028 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
14029 '(setq input nil)
14030 > \n
14031 > "// State FF for " verilog-sk-state \n
14032 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
14033 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
14034 > verilog-sk-state " = next_" verilog-sk-state ?; \n
14035 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
14036 > \n
14037 > "// Next State Logic for " verilog-sk-state \n
14038 > "always @ ( /*AUTOSENSE*/ ) begin\n"
14039 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
14040 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
14041 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
14042 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
14043 \f
14044 ;;; Mouse Events:
14045 ;;
14046 ;; Include file loading with mouse/return event
14047 ;;
14048 ;; idea & first impl.: M. Rouat (eldo-mode.el)
14049 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
14050
14051 (if (featurep 'xemacs)
14052 (require 'overlay))
14053
14054 (defconst verilog-include-file-regexp
14055 "^`include\\s-+\"\\([^\n\"]*\\)\""
14056 "Regexp that matches the include file.")
14057
14058 (defvar verilog-mode-mouse-map
14059 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
14060 (set-keymap-parent map verilog-mode-map)
14061 ;; mouse button bindings
14062 (define-key map "\r" 'verilog-load-file-at-point)
14063 (if (featurep 'xemacs)
14064 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
14065 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
14066 (if (featurep 'xemacs)
14067 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
14068 (define-key map [S-mouse-2] 'mouse-yank-at-click))
14069 map)
14070 "Map containing mouse bindings for `verilog-mode'.")
14071
14072
14073 (defun verilog-highlight-region (beg end _old-len)
14074 "Colorize included files and modules in the (changed?) region.
14075 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
14076 (when (or verilog-highlight-includes
14077 verilog-highlight-modules)
14078 (save-excursion
14079 (save-match-data ; A query-replace may call this function - do not disturb
14080 (verilog-save-buffer-state
14081 (verilog-save-scan-cache
14082 (let (end-point)
14083 (goto-char end)
14084 (setq end-point (point-at-eol))
14085 (goto-char beg)
14086 (beginning-of-line) ; scan entire line
14087 ;; delete overlays existing on this line
14088 (let ((overlays (overlays-in (point) end-point)))
14089 (while overlays
14090 (if (and
14091 (overlay-get (car overlays) 'detachable)
14092 (or (overlay-get (car overlays) 'verilog-include-file)
14093 (overlay-get (car overlays) 'verilog-inst-module)))
14094 (delete-overlay (car overlays)))
14095 (setq overlays (cdr overlays))))
14096 ;;
14097 ;; make new include overlays
14098 (when verilog-highlight-includes
14099 (while (search-forward-regexp verilog-include-file-regexp end-point t)
14100 (goto-char (match-beginning 1))
14101 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
14102 (overlay-put ov 'start-closed 't)
14103 (overlay-put ov 'end-closed 't)
14104 (overlay-put ov 'evaporate 't)
14105 (overlay-put ov 'verilog-include-file 't)
14106 (overlay-put ov 'mouse-face 'highlight)
14107 (overlay-put ov 'local-map verilog-mode-mouse-map))))
14108 ;;
14109 ;; make new module overlays
14110 (goto-char beg)
14111 ;; This scanner is syntax-fragile, so don't get bent
14112 (when verilog-highlight-modules
14113 (condition-case nil
14114 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
14115 (save-excursion
14116 (goto-char (match-beginning 0))
14117 (unless (verilog-inside-comment-or-string-p)
14118 (verilog-read-inst-module-matcher) ; sets match 0
14119 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
14120 (overlay-put ov 'start-closed 't)
14121 (overlay-put ov 'end-closed 't)
14122 (overlay-put ov 'evaporate 't)
14123 (overlay-put ov 'verilog-inst-module 't)
14124 (overlay-put ov 'mouse-face 'highlight)
14125 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
14126 (error nil)))
14127 ;;
14128 ;; Future highlights:
14129 ;; variables - make an Occur buffer of where referenced
14130 ;; pins - make an Occur buffer of the sig in the declaration module
14131 )))))))
14132
14133 (defun verilog-highlight-buffer ()
14134 "Colorize included files and modules across the whole buffer."
14135 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
14136 (interactive)
14137 ;; delete and remake overlays
14138 (verilog-highlight-region (point-min) (point-max) nil))
14139
14140 ;; Deprecated, but was interactive, so we'll keep it around
14141 (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
14142
14143 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
14144 ;; so define this function to do more or less the same as ffap-at-mouse
14145 ;; but first resolve filename...
14146 (defun verilog-load-file-at-mouse (event)
14147 "Load file under button 2 click's EVENT.
14148 Files are checked based on `verilog-library-flags'."
14149 (interactive "@e")
14150 (save-excursion ; implement a Verilog specific ffap-at-mouse
14151 (mouse-set-point event)
14152 (verilog-load-file-at-point t)))
14153
14154 ;; ffap isn't usable for Verilog mode. It uses library paths.
14155 ;; so define this function to do more or less the same as ffap
14156 ;; but first resolve filename...
14157 (defun verilog-load-file-at-point (&optional warn)
14158 "Load file under point.
14159 If WARN, throw warning if not found.
14160 Files are checked based on `verilog-library-flags'."
14161 (interactive)
14162 (save-excursion ; implement a Verilog specific ffap
14163 (let ((overlays (overlays-in (point) (point)))
14164 hit)
14165 (while (and overlays (not hit))
14166 (when (overlay-get (car overlays) 'verilog-inst-module)
14167 (verilog-goto-defun-file (buffer-substring
14168 (overlay-start (car overlays))
14169 (overlay-end (car overlays))))
14170 (setq hit t))
14171 (setq overlays (cdr overlays)))
14172 ;; Include?
14173 (beginning-of-line)
14174 (when (and (not hit)
14175 (looking-at verilog-include-file-regexp))
14176 (if (and (car (verilog-library-filenames
14177 (match-string 1) (buffer-file-name)))
14178 (file-readable-p (car (verilog-library-filenames
14179 (match-string 1) (buffer-file-name)))))
14180 (find-file (car (verilog-library-filenames
14181 (match-string 1) (buffer-file-name))))
14182 (when warn
14183 (message
14184 "File `%s' isn't readable, use shift-mouse2 to paste in this field"
14185 (match-string 1))))))))
14186
14187 \f
14188 ;;; Bug reporting:
14189 ;;
14190
14191 (defun verilog-faq ()
14192 "Tell the user their current version, and where to get the FAQ etc."
14193 (interactive)
14194 (with-output-to-temp-buffer "*verilog-mode help*"
14195 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
14196 (princ "\n")
14197 (princ "For new releases, see http://www.verilog.com\n")
14198 (princ "\n")
14199 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
14200 (princ "\n")
14201 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
14202 (princ "\n")))
14203
14204 (autoload 'reporter-submit-bug-report "reporter")
14205 (defvar reporter-prompt-for-summary-p)
14206
14207 (defun verilog-submit-bug-report ()
14208 "Submit via mail a bug report on verilog-mode.el."
14209 (interactive)
14210 (let ((reporter-prompt-for-summary-p t))
14211 (reporter-submit-bug-report
14212 "mac@verilog.com, wsnyder@wsnyder.org"
14213 (concat "verilog-mode v" verilog-mode-version)
14214 '(
14215 verilog-active-low-regexp
14216 verilog-after-save-font-hook
14217 verilog-align-ifelse
14218 verilog-assignment-delay
14219 verilog-auto-arg-sort
14220 verilog-auto-declare-nettype
14221 verilog-auto-delete-trailing-whitespace
14222 verilog-auto-endcomments
14223 verilog-auto-hook
14224 verilog-auto-ignore-concat
14225 verilog-auto-indent-on-newline
14226 verilog-auto-inout-ignore-regexp
14227 verilog-auto-input-ignore-regexp
14228 verilog-auto-inst-column
14229 verilog-auto-inst-dot-name
14230 verilog-auto-inst-interfaced-ports
14231 verilog-auto-inst-param-value
14232 verilog-auto-inst-sort
14233 verilog-auto-inst-template-numbers
14234 verilog-auto-inst-vector
14235 verilog-auto-lineup
14236 verilog-auto-newline
14237 verilog-auto-output-ignore-regexp
14238 verilog-auto-read-includes
14239 verilog-auto-reset-blocking-in-non
14240 verilog-auto-reset-widths
14241 verilog-auto-save-policy
14242 verilog-auto-sense-defines-constant
14243 verilog-auto-sense-include-inputs
14244 verilog-auto-star-expand
14245 verilog-auto-star-save
14246 verilog-auto-template-warn-unused
14247 verilog-auto-tieoff-declaration
14248 verilog-auto-tieoff-ignore-regexp
14249 verilog-auto-unused-ignore-regexp
14250 verilog-auto-wire-type
14251 verilog-before-auto-hook
14252 verilog-before-delete-auto-hook
14253 verilog-before-getopt-flags-hook
14254 verilog-before-save-font-hook
14255 verilog-cache-enabled
14256 verilog-case-fold
14257 verilog-case-indent
14258 verilog-cexp-indent
14259 verilog-compiler
14260 verilog-coverage
14261 verilog-delete-auto-hook
14262 verilog-getopt-flags-hook
14263 verilog-highlight-grouping-keywords
14264 verilog-highlight-includes
14265 verilog-highlight-modules
14266 verilog-highlight-p1800-keywords
14267 verilog-highlight-translate-off
14268 verilog-indent-begin-after-if
14269 verilog-indent-declaration-macros
14270 verilog-indent-level
14271 verilog-indent-level-behavioral
14272 verilog-indent-level-declaration
14273 verilog-indent-level-directive
14274 verilog-indent-level-module
14275 verilog-indent-lists
14276 verilog-library-directories
14277 verilog-library-extensions
14278 verilog-library-files
14279 verilog-library-flags
14280 verilog-linter
14281 verilog-minimum-comment-distance
14282 verilog-mode-hook
14283 verilog-mode-release-emacs
14284 verilog-mode-version
14285 verilog-preprocessor
14286 verilog-simulator
14287 verilog-tab-always-indent
14288 verilog-tab-to-comment
14289 verilog-typedef-regexp
14290 verilog-warn-fatal
14291 )
14292 nil nil
14293 (concat "Hi Mac,
14294
14295 I want to report a bug.
14296
14297 Before I go further, I want to say that Verilog mode has changed my life.
14298 I save so much time, my files are colored nicely, my co workers respect
14299 my coding ability... until now. I'd really appreciate anything you
14300 could do to help me out with this minor deficiency in the product.
14301
14302 I've taken a look at the Verilog-Mode FAQ at
14303 http://www.veripool.org/verilog-mode-faq.html.
14304
14305 And, I've considered filing the bug on the issue tracker at
14306 http://www.veripool.org/verilog-mode-bugs
14307 since I realize that public bugs are easier for you to track,
14308 and for others to search, but would prefer to email.
14309
14310 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
14311 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
14312 the code included below.
14313
14314 Given those lines, I expected [[Fill in here]] to happen;
14315 but instead, [[Fill in here]] happens!.
14316
14317 == The code: =="))))
14318
14319 (provide 'verilog-mode)
14320
14321 ;; Local Variables:
14322 ;; checkdoc-permit-comma-termination-flag:t
14323 ;; checkdoc-force-docstrings-flag:nil
14324 ;; indent-tabs-mode:nil
14325 ;; End:
14326
14327 ;;; verilog-mode.el ends here