Try to let it compile on other platforms
[emacs.git] / lisp / progmodes / verilog-mode.el
blobc072754e66d35275c1fd84a6c206d426eee94794
1 ;; verilog-mode.el --- major mode for editing verilog source in Emacs
3 ;; Copyright (C) 1996-2012 Free Software Foundation, Inc.
5 ;; Author: Michael McNamara (mac@verilog.com),
6 ;; Wilson Snyder (wsnyder@wsnyder.org)
7 ;; Please see our web sites:
8 ;; http://www.verilog.com
9 ;; http://www.veripool.org
11 ;; Keywords: languages
13 ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
14 ;; file on 19/3/2008, and the maintainer agreed that when a bug is
15 ;; filed in the Emacs bug reporting system against this file, a copy
16 ;; of the bug report be sent to the maintainer's email address.
18 ;; This code supports Emacs 21.1 and later
19 ;; And XEmacs 21.1 and later
20 ;; Please do not make changes that break Emacs 21. Thanks!
24 ;; This file is part of GNU Emacs.
26 ;; GNU Emacs is free software: you can redistribute it and/or modify
27 ;; it under the terms of the GNU General Public License as published by
28 ;; the Free Software Foundation, either version 3 of the License, or
29 ;; (at your option) any later version.
31 ;; GNU Emacs is distributed in the hope that it will be useful,
32 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
33 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 ;; GNU General Public License for more details.
36 ;; You should have received a copy of the GNU General Public License
37 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
39 ;;; Commentary:
41 ;; This mode borrows heavily from the Pascal-mode and the cc-mode of Emacs
43 ;; USAGE
44 ;; =====
46 ;; A major mode for editing Verilog HDL source code. When you have
47 ;; entered Verilog mode, you may get more info by pressing C-h m. You
48 ;; may also get online help describing various functions by: C-h f
49 ;; <Name of function you want described>
51 ;; KNOWN BUGS / BUG REPORTS
52 ;; =======================
54 ;; Verilog is a rapidly evolving language, and hence this mode is
55 ;; under continuous development. Hence this is beta code, and likely
56 ;; has bugs. Please report any issues to the issue tracker at
57 ;; http://www.veripool.org/verilog-mode
58 ;; Please use verilog-submit-bug-report to submit a report; type C-c
59 ;; C-b to invoke this and as a result I will have a much easier time
60 ;; of reproducing the bug you find, and hence fixing it.
62 ;; INSTALLING THE MODE
63 ;; ===================
65 ;; An older version of this mode may be already installed as a part of
66 ;; your environment, and one method of updating would be to update
67 ;; your Emacs environment. Sometimes this is difficult for local
68 ;; political/control reasons, and hence you can always install a
69 ;; private copy (or even a shared copy) which overrides the system
70 ;; default.
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
75 ;; The short list of installation instructions are: To set up
76 ;; automatic Verilog mode, put this file in your load path, and put
77 ;; the following in code (please un comment it first!) in your
78 ;; .emacs, or in your site's site-load.el
80 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
83 ;; Be sure to examine at the help for verilog-auto, and the other
84 ;; verilog-auto-* functions for some major coding time savers.
86 ;; If you want to customize Verilog mode to fit your needs better,
87 ;; you may add the below lines (the values of the variables presented
88 ;; here are the defaults). Note also that if you use an Emacs that
89 ;; supports custom, it's probably better to use the custom menu to
90 ;; edit these. If working as a member of a large team these settings
91 ;; should be common across all users (in a site-start file), or set
92 ;; in Local Variables in every file. Otherwise, different people's
93 ;; AUTO expansion may result different whitespace changes.
95 ; ;; Enable syntax highlighting of **all** languages
96 ; (global-font-lock-mode t)
98 ; ;; User customization for Verilog mode
99 ; (setq verilog-indent-level 3
100 ; verilog-indent-level-module 3
101 ; verilog-indent-level-declaration 3
102 ; verilog-indent-level-behavioral 3
103 ; verilog-indent-level-directive 1
104 ; verilog-case-indent 2
105 ; verilog-auto-newline t
106 ; verilog-auto-indent-on-newline t
107 ; verilog-tab-always-indent t
108 ; verilog-auto-endcomments t
109 ; verilog-minimum-comment-distance 40
110 ; verilog-indent-begin-after-if t
111 ; verilog-auto-lineup 'declarations
112 ; verilog-highlight-p1800-keywords nil
113 ; verilog-linter "my_lint_shell_command"
116 ;; \f
118 ;;; History:
120 ;; See commit history at http://www.veripool.org/verilog-mode.html
121 ;; (This section is required to appease checkdoc.)
123 ;;; Code:
125 ;; This variable will always hold the version number of the mode
126 (defconst verilog-mode-version "800"
127 "Version of this Verilog mode.")
128 (defconst verilog-mode-release-date "2012-04-23-GNU"
129 "Release date of this Verilog mode.")
130 (defconst verilog-mode-release-emacs t
131 "If non-nil, this version of Verilog mode was released with Emacs itself.")
133 (defun verilog-version ()
134 "Inform caller of the version of this file."
135 (interactive)
136 (message "Using verilog-mode version %s" verilog-mode-version))
138 ;; Insure we have certain packages, and deal with it if we don't
139 ;; Be sure to note which Emacs flavor and version added each feature.
140 (eval-when-compile
141 ;; Provide stuff if we are XEmacs
142 (when (featurep 'xemacs)
143 (condition-case nil
144 (require 'easymenu)
145 (error nil))
146 (condition-case nil
147 (require 'regexp-opt)
148 (error nil))
149 ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
150 (condition-case nil
151 (load "skeleton")
152 (error nil))
153 (condition-case nil
154 (if (fboundp 'when)
155 nil ;; fab
156 (defmacro when (cond &rest body)
157 (list 'if cond (cons 'progn body))))
158 (error nil))
159 (condition-case nil
160 (if (fboundp 'unless)
161 nil ;; fab
162 (defmacro unless (cond &rest body)
163 (cons 'if (cons cond (cons nil body)))))
164 (error nil))
165 (condition-case nil
166 (if (fboundp 'store-match-data)
167 nil ;; fab
168 (defmacro store-match-data (&rest args) nil))
169 (error nil))
170 (condition-case nil
171 (if (fboundp 'char-before)
172 nil ;; great
173 (defmacro char-before (&rest body)
174 (char-after (1- (point)))))
175 (error nil))
176 (condition-case nil
177 (if (fboundp 'when)
178 nil ;; fab
179 (defsubst point-at-bol (&optional N)
180 (save-excursion (beginning-of-line N) (point))))
181 (error nil))
182 (condition-case nil
183 (if (fboundp 'when)
184 nil ;; fab
185 (defsubst point-at-eol (&optional N)
186 (save-excursion (end-of-line N) (point))))
187 (error nil))
188 (condition-case nil
189 (require 'custom)
190 (error nil))
191 (condition-case nil
192 (if (fboundp 'match-string-no-properties)
193 nil ;; great
194 (defsubst match-string-no-properties (num &optional string)
195 "Return string of text matched by last search, without text properties.
196 NUM specifies which parenthesized expression in the last regexp.
197 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
198 Zero means the entire text matched by the whole regexp or whole string.
199 STRING should be given if the last search was by `string-match' on STRING."
200 (if (match-beginning num)
201 (if string
202 (let ((result
203 (substring string
204 (match-beginning num) (match-end num))))
205 (set-text-properties 0 (length result) nil result)
206 result)
207 (buffer-substring-no-properties (match-beginning num)
208 (match-end num)
209 (current-buffer)))))
211 (error nil))
212 (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
213 nil ;; We've got what we needed
214 ;; We have the old custom-library, hack around it!
215 (defmacro defgroup (&rest args) nil)
216 (defmacro customize (&rest args)
217 (message
218 "Sorry, Customize is not available with this version of Emacs"))
219 (defmacro defcustom (var value doc &rest args)
220 `(defvar ,var ,value ,doc))
222 (if (fboundp 'defface)
223 nil ; great!
224 (defmacro defface (var values doc &rest args)
225 `(make-face ,var))
228 (if (and (featurep 'custom) (fboundp 'customize-group))
229 nil ;; We've got what we needed
230 ;; We have an intermediate custom-library, hack around it!
231 (defmacro customize-group (var &rest args)
232 `(customize ,var))
235 (unless (boundp 'inhibit-point-motion-hooks)
236 (defvar inhibit-point-motion-hooks nil))
237 (unless (boundp 'deactivate-mark)
238 (defvar deactivate-mark nil))
241 ;; OK, do this stuff if we are NOT XEmacs:
242 (unless (featurep 'xemacs)
243 (unless (fboundp 'region-active-p)
244 (defmacro region-active-p ()
245 `(and transient-mark-mode mark-active))))
248 ;; Provide a regular expression optimization routine, using regexp-opt
249 ;; if provided by the user's elisp libraries
250 (eval-and-compile
251 ;; The below were disabled when GNU Emacs 22 was released;
252 ;; perhaps some still need to be there to support Emacs 21.
253 (if (featurep 'xemacs)
254 (if (fboundp 'regexp-opt)
255 ;; regexp-opt is defined, does it take 3 or 2 arguments?
256 (if (fboundp 'function-max-args)
257 (let ((args (function-max-args `regexp-opt)))
258 (cond
259 ((eq args 3) ;; It takes 3
260 (condition-case nil ; Hide this defun from emacses
261 ;with just a two input regexp
262 (defun verilog-regexp-opt (a b)
263 "Deal with differing number of required arguments for `regexp-opt'.
264 Call 'regexp-opt' on A and B."
265 (regexp-opt a b 't))
266 (error nil))
268 ((eq args 2) ;; It takes 2
269 (defun verilog-regexp-opt (a b)
270 "Call 'regexp-opt' on A and B."
271 (regexp-opt a b))
273 (t nil)))
274 ;; We can't tell; assume it takes 2
275 (defun verilog-regexp-opt (a b)
276 "Call 'regexp-opt' on A and B."
277 (regexp-opt a b))
279 ;; There is no regexp-opt, provide our own
280 (defun verilog-regexp-opt (strings &optional paren shy)
281 (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
282 (concat open (mapconcat 'regexp-quote strings "\\|") close)))
284 ;; Emacs.
285 (defalias 'verilog-regexp-opt 'regexp-opt)))
287 (eval-and-compile
288 ;; Both xemacs and emacs
289 (condition-case nil
290 (require 'diff) ;; diff-command and diff-switches
291 (error nil))
292 (condition-case nil
293 (require 'compile) ;; compilation-error-regexp-alist-alist
294 (error nil))
295 (condition-case nil
296 (unless (fboundp 'buffer-chars-modified-tick) ;; Emacs 22 added
297 (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
298 (error nil))
299 ;; Added in Emacs 24.1
300 (condition-case nil
301 (unless (fboundp 'prog-mode)
302 (define-derived-mode prog-mode fundamental-mode "Prog"))
303 (error nil)))
305 (eval-when-compile
306 (defun verilog-regexp-words (a)
307 "Call 'regexp-opt' with word delimiters for the words A."
308 (concat "\\<" (verilog-regexp-opt a t) "\\>")))
309 (defun verilog-regexp-words (a)
310 "Call 'regexp-opt' with word delimiters for the words A."
311 ;; The FAQ references this function, so user LISP sometimes calls it
312 (concat "\\<" (verilog-regexp-opt a t) "\\>"))
314 (defun verilog-easy-menu-filter (menu)
315 "Filter `easy-menu-define' MENU to support new features."
316 (cond ((not (featurep 'xemacs))
317 menu) ;; GNU Emacs - passthru
318 ;; XEmacs doesn't support :help. Strip it.
319 ;; Recursively filter the a submenu
320 ((listp menu)
321 (mapcar 'verilog-easy-menu-filter menu))
322 ;; Look for [:help "blah"] and remove
323 ((vectorp menu)
324 (let ((i 0) (out []))
325 (while (< i (length menu))
326 (if (equal `:help (aref menu i))
327 (setq i (+ 2 i))
328 (setq out (vconcat out (vector (aref menu i)))
329 i (1+ i))))
330 out))
331 (t menu))) ;; Default - ok
332 ;;(verilog-easy-menu-filter
333 ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
334 ;; "----" ["MB" nil :help "Help MB"]))
336 (defun verilog-define-abbrev (table name expansion &optional hook)
337 "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
338 Provides SYSTEM-FLAG in newer Emacs."
339 (condition-case nil
340 (define-abbrev table name expansion hook 0 t)
341 (error
342 (define-abbrev table name expansion hook))))
344 (defun verilog-customize ()
345 "Customize variables and other settings used by Verilog-Mode."
346 (interactive)
347 (customize-group 'verilog-mode))
349 (defun verilog-font-customize ()
350 "Customize fonts used by Verilog-Mode."
351 (interactive)
352 (if (fboundp 'customize-apropos)
353 (customize-apropos "font-lock-*" 'faces)))
355 (defun verilog-booleanp (value)
356 "Return t if VALUE is boolean.
357 This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
358 This function may be removed when Emacs 21 is no longer supported."
359 (or (equal value t) (equal value nil)))
361 (defun verilog-insert-last-command-event ()
362 "Insert the `last-command-event'."
363 (insert (if (featurep 'xemacs)
364 ;; XEmacs 21.5 doesn't like last-command-event
365 last-command-char
366 ;; And GNU Emacs 22 has obsoleted last-command-char
367 last-command-event)))
369 (defvar verilog-no-change-functions nil
370 "True if `after-change-functions' is disabled.
371 Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
373 (defvar verilog-in-hooks nil
374 "True when within a `verilog-run-hooks' block.")
376 (defmacro verilog-run-hooks (&rest hooks)
377 "Run each hook in HOOKS using `run-hooks'.
378 Set `verilog-in-hooks' during this time, to assist AUTO caches."
379 `(let ((verilog-in-hooks t))
380 (run-hooks ,@hooks)))
382 (defun verilog-syntax-ppss (&optional pos)
383 (when verilog-no-change-functions
384 (if verilog-in-hooks
385 (verilog-scan-cache-flush)
386 ;; else don't let the AUTO code itself get away with flushing the cache,
387 ;; as that'll make things very slow
388 (backtrace)
389 (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
390 (verilog-point-text))))
391 (if (fboundp 'syntax-ppss)
392 (syntax-ppss pos)
393 (parse-partial-sexp (point-min) (or pos (point)))))
395 (defgroup verilog-mode nil
396 "Major mode for Verilog source code."
397 :version "22.2"
398 :group 'languages)
400 ; (defgroup verilog-mode-fonts nil
401 ; "Facilitates easy customization fonts used in Verilog source text"
402 ; :link '(customize-apropos "font-lock-*" 'faces)
403 ; :group 'verilog-mode)
405 (defgroup verilog-mode-indent nil
406 "Customize indentation and highlighting of Verilog source text."
407 :group 'verilog-mode)
409 (defgroup verilog-mode-actions nil
410 "Customize actions on Verilog source text."
411 :group 'verilog-mode)
413 (defgroup verilog-mode-auto nil
414 "Customize AUTO actions when expanding Verilog source text."
415 :group 'verilog-mode)
417 (defvar verilog-debug nil
418 "Non-nil means enable debug messages for `verilog-mode' internals.")
420 (defvar verilog-warn-fatal nil
421 "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
423 (defcustom verilog-linter
424 "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
425 "Unix program and arguments to call to run a lint checker on Verilog source.
426 Depending on the `verilog-set-compile-command', this may be invoked when
427 you type \\[compile]. When the compile completes, \\[next-error] will take
428 you to the next lint error."
429 :type 'string
430 :group 'verilog-mode-actions)
431 ;; We don't mark it safe, as it's used as a shell command
433 (defcustom verilog-coverage
434 "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
435 "Program and arguments to use to annotate for coverage Verilog source.
436 Depending on the `verilog-set-compile-command', this may be invoked when
437 you type \\[compile]. When the compile completes, \\[next-error] will take
438 you to the next lint error."
439 :type 'string
440 :group 'verilog-mode-actions)
441 ;; We don't mark it safe, as it's used as a shell command
443 (defcustom verilog-simulator
444 "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
445 "Program and arguments to use to interpret Verilog source.
446 Depending on the `verilog-set-compile-command', this may be invoked when
447 you type \\[compile]. When the compile completes, \\[next-error] will take
448 you to the next lint error."
449 :type 'string
450 :group 'verilog-mode-actions)
451 ;; We don't mark it safe, as it's used as a shell command
453 (defcustom verilog-compiler
454 "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
455 "Program and arguments to use to compile Verilog source.
456 Depending on the `verilog-set-compile-command', this may be invoked when
457 you type \\[compile]. When the compile completes, \\[next-error] will take
458 you to the next lint error."
459 :type 'string
460 :group 'verilog-mode-actions)
461 ;; We don't mark it safe, as it's used as a shell command
463 (defcustom verilog-preprocessor
464 ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
465 "vppreproc __FLAGS__ __FILE__"
466 "Program and arguments to use to preprocess Verilog source.
467 This is invoked with `verilog-preprocess', and depending on the
468 `verilog-set-compile-command', may also be invoked when you type
469 \\[compile]. When the compile completes, \\[next-error] will
470 take 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
475 (defvar verilog-preprocess-history nil
476 "History for `verilog-preprocess'.")
478 (defvar verilog-tool 'verilog-linter
479 "Which tool to use for building compiler-command.
480 Either nil, `verilog-linter, `verilog-compiler,
481 `verilog-coverage, `verilog-preprocessor, or `verilog-simulator.
482 Alternatively use the \"Choose Compilation Action\" menu. See
483 `verilog-set-compile-command' for more information.")
485 (defcustom verilog-highlight-translate-off nil
486 "Non-nil means background-highlight code excluded from translation.
487 That is, all code between \"// synopsys translate_off\" and
488 \"// synopsys translate_on\" is highlighted using a different background color
489 \(face `verilog-font-lock-translate-off-face').
491 Note: This will slow down on-the-fly fontification (and thus editing).
493 Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
494 entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
495 :type 'boolean
496 :group 'verilog-mode-indent)
497 ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
498 (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
500 (defcustom verilog-auto-lineup 'declarations
501 "Type of statements to lineup across multiple lines.
502 If 'all' is selected, then all line ups described below are done.
504 If 'declaration', then just declarations are lined up with any
505 preceding declarations, taking into account widths and the like,
506 so or example the code:
507 reg [31:0] a;
508 reg b;
509 would become
510 reg [31:0] a;
511 reg b;
513 If 'assignment', then assignments are lined up with any preceding
514 assignments, so for example the code
515 a_long_variable <= b + c;
516 d = e + f;
517 would become
518 a_long_variable <= b + c;
519 d = e + f;
521 In order to speed up editing, large blocks of statements are lined up
522 only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
523 are lineup only when \\[verilog-pretty-declarations] is typed."
525 :type '(radio (const :tag "Line up Assignments and Declarations" all)
526 (const :tag "Line up Assignment statements" assignments )
527 (const :tag "Line up Declarations" declarations)
528 (function :tag "Other"))
529 :group 'verilog-mode-indent )
530 (put 'verilog-auto-lineup 'safe-local-variable
531 '(lambda (x) (memq x '(nil all assignments declarations))))
533 (defcustom verilog-indent-level 3
534 "Indentation of Verilog statements with respect to containing block."
535 :group 'verilog-mode-indent
536 :type 'integer)
537 (put 'verilog-indent-level 'safe-local-variable 'integerp)
539 (defcustom verilog-indent-level-module 3
540 "Indentation of Module level Verilog statements (eg always, initial).
541 Set to 0 to get initial and always statements lined up on the left side of
542 your screen."
543 :group 'verilog-mode-indent
544 :type 'integer)
545 (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
547 (defcustom verilog-indent-level-declaration 3
548 "Indentation of declarations with respect to containing block.
549 Set to 0 to get them list right under containing block."
550 :group 'verilog-mode-indent
551 :type 'integer)
552 (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
554 (defcustom verilog-indent-declaration-macros nil
555 "How to treat macro expansions in a declaration.
556 If nil, indent as:
557 input [31:0] a;
558 input `CP;
559 output c;
560 If non nil, treat as:
561 input [31:0] a;
562 input `CP ;
563 output c;"
564 :group 'verilog-mode-indent
565 :type 'boolean)
566 (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
568 (defcustom verilog-indent-lists t
569 "How to treat indenting items in a list.
570 If t (the default), indent as:
571 always @( posedge a or
572 reset ) begin
574 If nil, treat as:
575 always @( posedge a or
576 reset ) begin"
577 :group 'verilog-mode-indent
578 :type 'boolean)
579 (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
581 (defcustom verilog-indent-level-behavioral 3
582 "Absolute indentation of first begin in a task or function block.
583 Set to 0 to get such code to start at the left side of the screen."
584 :group 'verilog-mode-indent
585 :type 'integer)
586 (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
588 (defcustom verilog-indent-level-directive 1
589 "Indentation to add to each level of `ifdef declarations.
590 Set to 0 to have all directives start at the left side of the screen."
591 :group 'verilog-mode-indent
592 :type 'integer)
593 (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
595 (defcustom verilog-cexp-indent 2
596 "Indentation of Verilog statements split across lines."
597 :group 'verilog-mode-indent
598 :type 'integer)
599 (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
601 (defcustom verilog-case-indent 2
602 "Indentation for case statements."
603 :group 'verilog-mode-indent
604 :type 'integer)
605 (put 'verilog-case-indent 'safe-local-variable 'integerp)
607 (defcustom verilog-auto-newline t
608 "Non-nil means automatically newline after semicolons."
609 :group 'verilog-mode-indent
610 :type 'boolean)
611 (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
613 (defcustom verilog-auto-indent-on-newline t
614 "Non-nil means automatically indent line after newline."
615 :group 'verilog-mode-indent
616 :type 'boolean)
617 (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
619 (defcustom verilog-tab-always-indent t
620 "Non-nil means TAB should always re-indent the current line.
621 A nil value means TAB will only reindent when at the beginning of the line."
622 :group 'verilog-mode-indent
623 :type 'boolean)
624 (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
626 (defcustom verilog-tab-to-comment nil
627 "Non-nil means TAB moves to the right hand column in preparation for a comment."
628 :group 'verilog-mode-actions
629 :type 'boolean)
630 (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
632 (defcustom verilog-indent-begin-after-if t
633 "Non-nil means indent begin statements following if, else, while, etc.
634 Otherwise, line them up."
635 :group 'verilog-mode-indent
636 :type 'boolean)
637 (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
639 (defcustom verilog-align-ifelse nil
640 "Non-nil means align `else' under matching `if'.
641 Otherwise else is lined up with first character on line holding matching if."
642 :group 'verilog-mode-indent
643 :type 'boolean)
644 (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
646 (defcustom verilog-minimum-comment-distance 10
647 "Minimum distance (in lines) between begin and end required before a comment.
648 Setting this variable to zero results in every end acquiring a comment; the
649 default avoids too many redundant comments in tight quarters."
650 :group 'verilog-mode-indent
651 :type 'integer)
652 (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
654 (defcustom verilog-highlight-p1800-keywords nil
655 "Non-nil means highlight words newly reserved by IEEE-1800.
656 These will appear in `verilog-font-lock-p1800-face' in order to gently
657 suggest changing where these words are used as variables to something else.
658 A nil value means highlight these words as appropriate for the SystemVerilog
659 IEEE-1800 standard. Note that changing this will require restarting Emacs
660 to see the effect as font color choices are cached by Emacs."
661 :group 'verilog-mode-indent
662 :type 'boolean)
663 (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
665 (defcustom verilog-highlight-grouping-keywords nil
666 "Non-nil means highlight grouping keywords 'begin' and 'end' more dramatically.
667 If false, these words are in the `font-lock-type-face'; if True then they are in
668 `verilog-font-lock-ams-face'. Some find that special highlighting on these
669 grouping constructs allow the structure of the code to be understood at a glance."
670 :group 'verilog-mode-indent
671 :type 'boolean)
672 (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
674 (defcustom verilog-highlight-modules nil
675 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
676 When true, mousing over module names will allow jumping to the
677 module definition. If false, this is not supported. Setting
678 this is experimental, and may lead to bad performance."
679 :group 'verilog-mode-indent
680 :type 'boolean)
681 (put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
683 (defcustom verilog-highlight-includes t
684 "Non-nil means highlight module statements for `verilog-load-file-at-point'.
685 When true, mousing over include file names will allow jumping to the
686 file referenced. If false, this is not supported."
687 :group 'verilog-mode-indent
688 :type 'boolean)
689 (put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
691 (defcustom verilog-auto-declare-nettype nil
692 "Non-nil specifies the data type to use with `verilog-auto-input' etc.
693 Set this to \"wire\" if the Verilog code uses \"`default_nettype
694 none\". Note using `default_nettype none isn't recommended practice; this
695 mode is experimental."
696 :version "24.1" ;; rev670
697 :group 'verilog-mode-actions
698 :type 'boolean)
699 (put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
701 (defcustom verilog-auto-wire-type nil
702 "Non-nil specifies the data type to use with `verilog-auto-wire' etc.
703 Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
704 :version "24.1" ;; rev673
705 :group 'verilog-mode-actions
706 :type 'boolean)
707 (put 'verilog-auto-wire-type 'safe-local-variable `stringp)
709 (defcustom verilog-auto-endcomments t
710 "Non-nil means insert a comment /* ... */ after 'end's.
711 The name of the function or case will be set between the braces."
712 :group 'verilog-mode-actions
713 :type 'boolean)
714 (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
716 (defcustom verilog-auto-delete-trailing-whitespace nil
717 "Non-nil means to `delete-trailing-whitespace' in `verilog-auto'."
718 :version "24.1" ;; rev703
719 :group 'verilog-mode-actions
720 :type 'boolean)
721 (put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
723 (defcustom verilog-auto-ignore-concat nil
724 "Non-nil means ignore signals in {...} concatenations for AUTOWIRE etc.
725 This will exclude signals referenced as pin connections in {...}
726 from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
727 for backward compatibility only and not set in new designs; it
728 may be removed in future versions."
729 :group 'verilog-mode-actions
730 :type 'boolean)
731 (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
733 (defcustom verilog-auto-read-includes nil
734 "Non-nil means to automatically read includes before AUTOs.
735 This will do a `verilog-read-defines' and `verilog-read-includes' before
736 each AUTO expansion. This makes it easier to embed defines and includes,
737 but can result in very slow reading times if there are many or large
738 include files."
739 :group 'verilog-mode-actions
740 :type 'boolean)
741 (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
743 (defcustom verilog-auto-save-policy nil
744 "Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
745 A value of `force' will always do a \\[verilog-auto] automatically if
746 needed on every save. A value of `detect' will do \\[verilog-auto]
747 automatically when it thinks necessary. A value of `ask' will query the
748 user when it thinks updating is needed.
750 You should not rely on the 'ask or 'detect policies, they are safeguards
751 only. They do not detect when AUTOINSTs need to be updated because a
752 sub-module's port list has changed."
753 :group 'verilog-mode-actions
754 :type '(choice (const nil) (const ask) (const detect) (const force)))
756 (defcustom verilog-auto-star-expand t
757 "Non-nil means to expand SystemVerilog .* instance ports.
758 They will be expanded in the same way as if there was an AUTOINST in the
759 instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
760 :group 'verilog-mode-actions
761 :type 'boolean)
762 (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
764 (defcustom verilog-auto-star-save nil
765 "Non-nil means save to disk SystemVerilog .* instance expansions.
766 A nil value indicates direct connections will be removed before saving.
767 Only meaningful to those created due to `verilog-auto-star-expand' being set.
769 Instead of setting this, you may want to use /*AUTOINST*/, which will
770 always be saved."
771 :group 'verilog-mode-actions
772 :type 'boolean)
773 (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
775 (defvar verilog-auto-update-tick nil
776 "Modification tick at which autos were last performed.")
778 (defvar verilog-auto-last-file-locals nil
779 "Text from file-local-variables during last evaluation.")
781 (defvar verilog-diff-function 'verilog-diff-report
782 "Function to run when `verilog-diff-auto' detects differences.
783 Function takes three arguments, the original buffer, the
784 difference buffer, and the point in original buffer with the
785 first difference.")
787 ;;; Compile support
788 (require 'compile)
789 (defvar verilog-error-regexp-added nil)
791 (defvar verilog-error-regexp-emacs-alist
793 (verilog-xl-1
794 "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
795 (verilog-xl-2
796 "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
797 (verilog-IES
798 ".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
799 (verilog-surefire-1
800 "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
801 (verilog-surefire-2
802 "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
803 (verilog-verbose
805 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
806 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
807 (verilog-xsim
808 "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
809 (verilog-vcs-1
810 "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
811 (verilog-vcs-2
812 "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
813 (verilog-vcs-3
814 "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
815 (verilog-vcs-4
816 "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
817 (verilog-verilator
818 "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
819 (verilog-leda
820 "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
822 "List of regexps for Verilog compilers.
823 See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
825 (defvar verilog-error-regexp-xemacs-alist
826 ;; Emacs form is '((v-tool "re" 1 2) ...)
827 ;; XEmacs form is '(verilog ("re" 1 2) ...)
828 ;; So we can just map from Emacs to XEmacs
829 (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
830 "List of regexps for Verilog compilers.
831 See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
833 (defvar verilog-error-font-lock-keywords
835 ;; verilog-xl-1
836 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
837 ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
838 ;; verilog-xl-2
839 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
840 ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
841 ;; verilog-IES (nc-verilog)
842 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
843 (".*\\*[WE],[0-9A-Z]+\\(\[[0-9A-Z_,]+\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
844 ;; verilog-surefire-1
845 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
846 ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
847 ;; verilog-surefire-2
848 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
849 ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
850 ;; verilog-verbose
852 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
853 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
855 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
856 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
857 ;; verilog-vcs-1
858 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
859 ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
860 ;; verilog-vcs-2
861 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
862 ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
863 ;; verilog-vcs-3
864 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
865 ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
866 ;; verilog-vcs-4
867 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
868 ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
869 ;; verilog-verilator
870 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
871 (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
872 ;; verilog-leda
873 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
874 ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
876 "Keywords to also highlight in Verilog *compilation* buffers.
877 Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
879 (defcustom verilog-library-flags '("")
880 "List of standard Verilog arguments to use for /*AUTOINST*/.
881 These arguments are used to find files for `verilog-auto', and match
882 the flags accepted by a standard Verilog-XL simulator.
884 -f filename Reads more `verilog-library-flags' from the filename.
885 +incdir+dir Adds the directory to `verilog-library-directories'.
886 -Idir Adds the directory to `verilog-library-directories'.
887 -y dir Adds the directory to `verilog-library-directories'.
888 +libext+.v Adds the extensions to `verilog-library-extensions'.
889 -v filename Adds the filename to `verilog-library-files'.
891 filename Adds the filename to `verilog-library-files'.
892 This is not recommended, -v is a better choice.
894 You might want these defined in each file; put at the *END* of your file
895 something like:
897 // Local Variables:
898 // verilog-library-flags:(\"-y dir -y otherdir\")
899 // End:
901 Verilog-mode attempts to detect changes to this local variable, but they
902 are only insured to be correct when the file is first visited. Thus if you
903 have problems, use \\[find-alternate-file] RET to have these take effect.
905 See also the variables mentioned above."
906 :group 'verilog-mode-auto
907 :type '(repeat string))
908 (put 'verilog-library-flags 'safe-local-variable 'listp)
910 (defcustom verilog-library-directories '(".")
911 "List of directories when looking for files for /*AUTOINST*/.
912 The directory may be relative to the current file, or absolute.
913 Environment variables are also expanded in the directory names.
914 Having at least the current directory is a good idea.
916 You might want these defined in each file; put at the *END* of your file
917 something like:
919 // Local Variables:
920 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
921 // End:
923 Verilog-mode attempts to detect changes to this local variable, but they
924 are only insured to be correct when the file is first visited. Thus if you
925 have problems, use \\[find-alternate-file] RET to have these take effect.
927 See also `verilog-library-flags', `verilog-library-files'
928 and `verilog-library-extensions'."
929 :group 'verilog-mode-auto
930 :type '(repeat file))
931 (put 'verilog-library-directories 'safe-local-variable 'listp)
933 (defcustom verilog-library-files '()
934 "List of files to search for modules.
935 AUTOINST will use this when it needs to resolve a module name.
936 This is a complete path, usually to a technology file with many standard
937 cells defined in it.
939 You might want these defined in each file; put at the *END* of your file
940 something like:
942 // Local Variables:
943 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
944 // End:
946 Verilog-mode attempts to detect changes to this local variable, but they
947 are only insured to be correct when the file is first visited. Thus if you
948 have problems, use \\[find-alternate-file] RET to have these take effect.
950 See also `verilog-library-flags', `verilog-library-directories'."
951 :group 'verilog-mode-auto
952 :type '(repeat directory))
953 (put 'verilog-library-files 'safe-local-variable 'listp)
955 (defcustom verilog-library-extensions '(".v" ".sv")
956 "List of extensions to use when looking for files for /*AUTOINST*/.
957 See also `verilog-library-flags', `verilog-library-directories'."
958 :type '(repeat string)
959 :group 'verilog-mode-auto)
960 (put 'verilog-library-extensions 'safe-local-variable 'listp)
962 (defcustom verilog-active-low-regexp nil
963 "If set, treat signals matching this regexp as active low.
964 This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
965 you will probably also need `verilog-auto-reset-widths' set."
966 :group 'verilog-mode-auto
967 :type 'string)
968 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
970 (defcustom verilog-auto-sense-include-inputs nil
971 "Non-nil means AUTOSENSE should include all inputs.
972 If nil, only inputs that are NOT output signals in the same block are
973 included."
974 :group 'verilog-mode-auto
975 :type 'boolean)
976 (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
978 (defcustom verilog-auto-sense-defines-constant nil
979 "Non-nil means AUTOSENSE should assume all defines represent constants.
980 When true, the defines will not be included in sensitivity lists. To
981 maintain compatibility with other sites, this should be set at the bottom
982 of each Verilog file that requires it, rather than being set globally."
983 :group 'verilog-mode-auto
984 :type 'boolean)
985 (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
987 (defcustom verilog-auto-reset-blocking-in-non t
988 "Non-nil means AUTORESET will reset blocking statements.
989 When true, AUTORESET will reset in blocking statements those
990 signals which were assigned with blocking assignments (=) even in
991 a block with non-blocking assignments (<=).
993 If nil, all blocking assigned signals are ignored when any
994 non-blocking assignment is in the AUTORESET block. This allows
995 blocking assignments to be used for temporary values and not have
996 those temporaries reset. See example in `verilog-auto-reset'."
997 :version "24.1" ;; rev718
998 :type 'boolean
999 :group 'verilog-mode-auto)
1000 (put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
1002 (defcustom verilog-auto-reset-widths t
1003 "True means AUTORESET should determine the width of signals.
1004 This is then used to set the width of the zero (32'h0 for example). This
1005 is required by some lint tools that aren't smart enough to ignore widths of
1006 the constant zero. This may result in ugly code when parameters determine
1007 the MSB or LSB of a signal inside an AUTORESET.
1009 If nil, AUTORESET uses \"0\" as the constant.
1011 If 'unbased', AUTORESET used the unbased unsized literal \"'0\"
1012 as the constant. This setting is strongly recommended for
1013 SystemVerilog designs."
1014 :type 'boolean
1015 :group 'verilog-mode-auto)
1016 (put 'verilog-auto-reset-widths 'safe-local-variable
1017 '(lambda (x) (memq x '(nil t unbased))))
1019 (defcustom verilog-assignment-delay ""
1020 "Text used for delays in delayed assignments. Add a trailing space if set."
1021 :group 'verilog-mode-auto
1022 :type 'string)
1023 (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
1025 (defcustom verilog-auto-arg-sort nil
1026 "Non-nil means AUTOARG signal names will be sorted, not in declaration order.
1027 Declaration order is advantageous with order based instantiations
1028 and is the default for backward compatibility. Sorted order
1029 reduces changes when declarations are moved around in a file, and
1030 it's bad practice to rely on order based instantiations anyhow.
1032 See also `verilog-auto-inst-sort'."
1033 :group 'verilog-mode-auto
1034 :type 'boolean)
1035 (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
1037 (defcustom verilog-auto-inst-dot-name nil
1038 "Non-nil means when creating ports with AUTOINST, use .name syntax.
1039 This will use \".port\" instead of \".port(port)\" when possible.
1040 This is only legal in SystemVerilog files, and will confuse older
1041 simulators. Setting `verilog-auto-inst-vector' to nil may also
1042 be desirable to increase how often .name will be used."
1043 :group 'verilog-mode-auto
1044 :type 'boolean)
1045 (put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
1047 (defcustom verilog-auto-inst-param-value nil
1048 "Non-nil means AUTOINST will replace parameters with the parameter value.
1049 If nil, leave parameters as symbolic names.
1051 Parameters must be in Verilog 2001 format #(...), and if a parameter is not
1052 listed as such there (as when the default value is acceptable), it will not
1053 be replaced, and will remain symbolic.
1055 For example, imagine a submodule uses parameters to declare the size of its
1056 inputs. This is then used by an upper module:
1058 module InstModule (o,i);
1059 parameter WIDTH;
1060 input [WIDTH-1:0] i;
1061 endmodule
1063 module ExampInst;
1064 InstModule
1065 #(PARAM(10))
1066 instName
1067 (/*AUTOINST*/
1068 .i (i[PARAM-1:0]));
1070 Note even though PARAM=10, the AUTOINST has left the parameter as a
1071 symbolic name. If `verilog-auto-inst-param-value' is set, this will
1072 instead expand to:
1074 module ExampInst;
1075 InstModule
1076 #(PARAM(10))
1077 instName
1078 (/*AUTOINST*/
1079 .i (i[9:0]));"
1080 :group 'verilog-mode-auto
1081 :type 'boolean)
1082 (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
1084 (defcustom verilog-auto-inst-sort nil
1085 "Non-nil means AUTOINST signals will be sorted, not in declaration order.
1086 Also affects AUTOINSTPARAM. Declaration order is the default for
1087 backward compatibility, and as some teams prefer signals that are
1088 declared together to remain together. Sorted order reduces
1089 changes when declarations are moved around in a file.
1091 See also `verilog-auto-arg-sort'."
1092 :version "24.1" ;; rev688
1093 :group 'verilog-mode-auto
1094 :type 'boolean)
1095 (put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
1097 (defcustom verilog-auto-inst-vector t
1098 "Non-nil means when creating default ports with AUTOINST, use bus subscripts.
1099 If nil, skip the subscript when it matches the entire bus as declared in
1100 the module (AUTOWIRE signals always are subscripted, you must manually
1101 declare the wire to have the subscripts removed.) Setting this to nil may
1102 speed up some simulators, but is less general and harder to read, so avoid."
1103 :group 'verilog-mode-auto
1104 :type 'boolean)
1105 (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
1107 (defcustom verilog-auto-inst-template-numbers nil
1108 "If true, when creating templated ports with AUTOINST, add a comment.
1110 If t, the comment will add the line number of the template that
1111 was used for that port declaration. This setting is suggested
1112 only for debugging use, as regular use may cause a large numbers
1113 of merge conflicts.
1115 If 'lhs', the comment will show the left hand side of the
1116 AUTO_TEMPLATE rule that is matched. This is less precise than
1117 numbering (t) when multiple rules have the same pin name, but
1118 won't merge conflict."
1119 :group 'verilog-mode-auto
1120 :type '(choice (const nil) (const t) (const lhs)))
1121 (put 'verilog-auto-inst-template-numbers 'safe-local-variable
1122 '(lambda (x) (memq x '(nil t lhs))))
1124 (defcustom verilog-auto-inst-column 40
1125 "Indent-to column number for net name part of AUTOINST created pin."
1126 :group 'verilog-mode-indent
1127 :type 'integer)
1128 (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
1130 (defcustom verilog-auto-inst-interfaced-ports t
1131 "Non-nil means include interfaced ports in AUTOINST expansions."
1132 :group 'verilog-mode-auto
1133 :type 'boolean)
1134 (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
1136 (defcustom verilog-auto-input-ignore-regexp nil
1137 "If set, when creating AUTOINPUT list, ignore signals matching this regexp.
1138 See the \\[verilog-faq] for examples on using this."
1139 :group 'verilog-mode-auto
1140 :type 'string)
1141 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
1143 (defcustom verilog-auto-inout-ignore-regexp nil
1144 "If set, when creating AUTOINOUT list, ignore signals matching this regexp.
1145 See the \\[verilog-faq] for examples on using this."
1146 :group 'verilog-mode-auto
1147 :type 'string)
1148 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
1150 (defcustom verilog-auto-output-ignore-regexp nil
1151 "If set, when creating AUTOOUTPUT list, ignore signals matching this regexp.
1152 See the \\[verilog-faq] for examples on using this."
1153 :group 'verilog-mode-auto
1154 :type 'string)
1155 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
1157 (defcustom verilog-auto-template-warn-unused nil
1158 "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
1159 This feature is not supported before Emacs 21.1 or XEmacs 21.4."
1160 :group 'verilog-mode-auto
1161 :type 'boolean)
1162 (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
1164 (defcustom verilog-auto-tieoff-declaration "wire"
1165 "Data type used for the declaration for AUTOTIEOFF.
1166 If \"wire\" then create a wire, if \"assign\" create an
1167 assignment, else the data type for variable creation."
1168 :version "24.1" ;; rev713
1169 :group 'verilog-mode-auto
1170 :type 'string)
1171 (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1173 (defcustom verilog-auto-tieoff-ignore-regexp nil
1174 "If set, when creating AUTOTIEOFF list, ignore signals matching this regexp.
1175 See the \\[verilog-faq] for examples on using this."
1176 :group 'verilog-mode-auto
1177 :type 'string)
1178 (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1180 (defcustom verilog-auto-unused-ignore-regexp nil
1181 "If set, when creating AUTOUNUSED list, ignore signals matching this regexp.
1182 See the \\[verilog-faq] for examples on using this."
1183 :group 'verilog-mode-auto
1184 :type 'string)
1185 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
1187 (defcustom verilog-typedef-regexp nil
1188 "If non-nil, regular expression that matches Verilog-2001 typedef names.
1189 For example, \"_t$\" matches typedefs named with _t, as in the C language."
1190 :group 'verilog-mode-auto
1191 :type 'string)
1192 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
1194 (defcustom verilog-mode-hook 'verilog-set-compile-command
1195 "Hook run after Verilog mode is loaded."
1196 :type 'hook
1197 :group 'verilog-mode)
1199 (defcustom verilog-auto-hook nil
1200 "Hook run after `verilog-mode' updates AUTOs."
1201 :group 'verilog-mode-auto
1202 :type 'hook)
1204 (defcustom verilog-before-auto-hook nil
1205 "Hook run before `verilog-mode' updates AUTOs."
1206 :group 'verilog-mode-auto
1207 :type 'hook)
1209 (defcustom verilog-delete-auto-hook nil
1210 "Hook run after `verilog-mode' deletes AUTOs."
1211 :group 'verilog-mode-auto
1212 :type 'hook)
1214 (defcustom verilog-before-delete-auto-hook nil
1215 "Hook run before `verilog-mode' deletes AUTOs."
1216 :group 'verilog-mode-auto
1217 :type 'hook)
1219 (defcustom verilog-getopt-flags-hook nil
1220 "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1221 :group 'verilog-mode-auto
1222 :type 'hook)
1224 (defcustom verilog-before-getopt-flags-hook nil
1225 "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1226 :group 'verilog-mode-auto
1227 :type 'hook)
1229 (defcustom verilog-before-save-font-hook nil
1230 "Hook run before `verilog-save-font-mods' removes highlighting."
1231 :group 'verilog-mode-auto
1232 :type 'hook)
1234 (defcustom verilog-after-save-font-hook nil
1235 "Hook run after `verilog-save-font-mods' restores highlighting."
1236 :group 'verilog-mode-auto
1237 :type 'hook)
1239 (defvar verilog-imenu-generic-expression
1240 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1241 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
1242 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1245 ;; provide a verilog-header function.
1246 ;; Customization variables:
1248 (defvar verilog-date-scientific-format nil
1249 "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1250 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1251 format (e.g. 09/17/1997) is not supported.")
1253 (defvar verilog-company nil
1254 "Default name of Company for Verilog header.
1255 If set will become buffer local.")
1256 (make-variable-buffer-local 'verilog-company)
1258 (defvar verilog-project nil
1259 "Default name of Project for Verilog header.
1260 If set will become buffer local.")
1261 (make-variable-buffer-local 'verilog-project)
1263 (defvar verilog-mode-map
1264 (let ((map (make-sparse-keymap)))
1265 (define-key map ";" 'electric-verilog-semi)
1266 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1267 (define-key map ":" 'electric-verilog-colon)
1268 ;;(define-key map "=" 'electric-verilog-equal)
1269 (define-key map "\`" 'electric-verilog-tick)
1270 (define-key map "\t" 'electric-verilog-tab)
1271 (define-key map "\r" 'electric-verilog-terminate-line)
1272 ;; backspace/delete key bindings
1273 (define-key map [backspace] 'backward-delete-char-untabify)
1274 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1275 (define-key map [delete] 'delete-char)
1276 (define-key map [(meta delete)] 'kill-word))
1277 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1278 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1279 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1280 (define-key map "\M-\t" 'verilog-complete-word)
1281 (define-key map "\M-?" 'verilog-show-completions)
1282 ;; Note \C-c and letter are reserved for users
1283 (define-key map "\C-c\`" 'verilog-lint-off)
1284 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
1285 (define-key map "\C-c\?" 'verilog-diff-auto)
1286 (define-key map "\C-c\C-r" 'verilog-label-be)
1287 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1288 (define-key map "\C-c=" 'verilog-pretty-expr)
1289 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1290 (define-key map "\M-*" 'verilog-star-comment)
1291 (define-key map "\C-c\C-c" 'verilog-comment-region)
1292 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1293 (when (featurep 'xemacs)
1294 (define-key map [(meta control h)] 'verilog-mark-defun)
1295 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1296 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1297 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1298 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1299 (define-key map "\C-c\C-a" 'verilog-auto)
1300 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1301 (define-key map "\C-c\C-p" 'verilog-preprocess)
1302 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1303 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1304 (define-key map "\C-c\C-h" 'verilog-header)
1305 map)
1306 "Keymap used in Verilog mode.")
1308 ;; menus
1309 (easy-menu-define
1310 verilog-menu verilog-mode-map "Menu for Verilog mode"
1311 (verilog-easy-menu-filter
1312 '("Verilog"
1313 ("Choose Compilation Action"
1314 ["None"
1315 (progn
1316 (setq verilog-tool nil)
1317 (verilog-set-compile-command))
1318 :style radio
1319 :selected (equal verilog-tool nil)
1320 :help "When invoking compilation, use compile-command"]
1321 ["Lint"
1322 (progn
1323 (setq verilog-tool 'verilog-linter)
1324 (verilog-set-compile-command))
1325 :style radio
1326 :selected (equal verilog-tool `verilog-linter)
1327 :help "When invoking compilation, use lint checker"]
1328 ["Coverage"
1329 (progn
1330 (setq verilog-tool 'verilog-coverage)
1331 (verilog-set-compile-command))
1332 :style radio
1333 :selected (equal verilog-tool `verilog-coverage)
1334 :help "When invoking compilation, annotate for coverage"]
1335 ["Simulator"
1336 (progn
1337 (setq verilog-tool 'verilog-simulator)
1338 (verilog-set-compile-command))
1339 :style radio
1340 :selected (equal verilog-tool `verilog-simulator)
1341 :help "When invoking compilation, interpret Verilog source"]
1342 ["Compiler"
1343 (progn
1344 (setq verilog-tool 'verilog-compiler)
1345 (verilog-set-compile-command))
1346 :style radio
1347 :selected (equal verilog-tool `verilog-compiler)
1348 :help "When invoking compilation, compile Verilog source"]
1349 ["Preprocessor"
1350 (progn
1351 (setq verilog-tool 'verilog-preprocessor)
1352 (verilog-set-compile-command))
1353 :style radio
1354 :selected (equal verilog-tool `verilog-preprocessor)
1355 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
1357 ("Move"
1358 ["Beginning of function" verilog-beg-of-defun
1359 :keys "C-M-a"
1360 :help "Move backward to the beginning of the current function or procedure"]
1361 ["End of function" verilog-end-of-defun
1362 :keys "C-M-e"
1363 :help "Move forward to the end of the current function or procedure"]
1364 ["Mark function" verilog-mark-defun
1365 :keys "C-M-h"
1366 :help "Mark the current Verilog function or procedure"]
1367 ["Goto function/module" verilog-goto-defun
1368 :help "Move to specified Verilog module/task/function"]
1369 ["Move to beginning of block" electric-verilog-backward-sexp
1370 :help "Move backward over one balanced expression"]
1371 ["Move to end of block" electric-verilog-forward-sexp
1372 :help "Move forward over one balanced expression"]
1374 ("Comments"
1375 ["Comment Region" verilog-comment-region
1376 :help "Put marked area into a comment"]
1377 ["UnComment Region" verilog-uncomment-region
1378 :help "Uncomment an area commented with Comment Region"]
1379 ["Multi-line comment insert" verilog-star-comment
1380 :help "Insert Verilog /* */ comment at point"]
1381 ["Lint error to comment" verilog-lint-off
1382 :help "Convert a Verilog linter warning line into a disable statement"]
1384 "----"
1385 ["Compile" compile
1386 :help "Perform compilation-action (above) on the current buffer"]
1387 ["AUTO, Save, Compile" verilog-auto-save-compile
1388 :help "Recompute AUTOs, save buffer, and compile"]
1389 ["Next Compile Error" next-error
1390 :help "Visit next compilation error message and corresponding source code"]
1391 ["Ignore Lint Warning at point" verilog-lint-off
1392 :help "Convert a Verilog linter warning line into a disable statement"]
1393 "----"
1394 ["Line up declarations around point" verilog-pretty-declarations
1395 :help "Line up declarations around point"]
1396 ["Line up equations around point" verilog-pretty-expr
1397 :help "Line up expressions around point"]
1398 ["Redo/insert comments on every end" verilog-label-be
1399 :help "Label matching begin ... end statements"]
1400 ["Expand [x:y] vector line" verilog-expand-vector
1401 :help "Take a signal vector on the current line and expand it to multiple lines"]
1402 ["Insert begin-end block" verilog-insert-block
1403 :help "Insert begin ... end"]
1404 ["Complete word" verilog-complete-word
1405 :help "Complete word at point"]
1406 "----"
1407 ["Recompute AUTOs" verilog-auto
1408 :help "Expand AUTO meta-comment statements"]
1409 ["Kill AUTOs" verilog-delete-auto
1410 :help "Remove AUTO expansions"]
1411 ["Diff AUTOs" verilog-diff-auto
1412 :help "Show differences in AUTO expansions"]
1413 ["Inject AUTOs" verilog-inject-auto
1414 :help "Inject AUTOs into legacy non-AUTO buffer"]
1415 ("AUTO Help..."
1416 ["AUTO General" (describe-function 'verilog-auto)
1417 :help "Help introduction on AUTOs"]
1418 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1419 :help "Help on verilog-library-flags"]
1420 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1421 :help "Help on verilog-library-directories"]
1422 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1423 :help "Help on verilog-library-files"]
1424 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1425 :help "Help on verilog-library-extensions"]
1426 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1427 :help "Help on reading `defines"]
1428 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1429 :help "Help on parsing `includes"]
1430 ["AUTOARG" (describe-function 'verilog-auto-arg)
1431 :help "Help on AUTOARG - declaring module port list"]
1432 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1433 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1434 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1435 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1436 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1437 :help "Help on AUTOINOUTCOMP - copying i/o from another file as all inputs"]
1438 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1439 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1440 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1441 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1442 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1443 :help "Help on AUTOINOUT - adding inouts from cells"]
1444 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1445 :help "Help on AUTOINPUT - adding inputs from cells"]
1446 ["AUTOINST" (describe-function 'verilog-auto-inst)
1447 :help "Help on AUTOINST - adding pins for cells"]
1448 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1449 :help "Help on expanding Verilog-2001 .* pins"]
1450 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1451 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1452 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1453 :help "Help on AUTOLOGIC - declaring logic signals"]
1454 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1455 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1456 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1457 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1458 ["AUTOREG" (describe-function 'verilog-auto-reg)
1459 :help "Help on AUTOREG - declaring registers for non-wires"]
1460 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1461 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1462 ["AUTORESET" (describe-function 'verilog-auto-reset)
1463 :help "Help on AUTORESET - resetting always blocks"]
1464 ["AUTOSENSE" (describe-function 'verilog-auto-sense)
1465 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1466 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1467 :help "Help on AUTOTIEOFF - tying off unused outputs"]
1468 ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
1469 :help "Help on AUTOUNDEF - undefine all local defines"]
1470 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1471 :help "Help on AUTOUNUSED - terminating unused inputs"]
1472 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1473 :help "Help on AUTOWIRE - declaring wires for cells"]
1475 "----"
1476 ["Submit bug report" verilog-submit-bug-report
1477 :help "Submit via mail a bug report on verilog-mode.el"]
1478 ["Version and FAQ" verilog-faq
1479 :help "Show the current version, and where to get the FAQ etc"]
1480 ["Customize Verilog Mode..." verilog-customize
1481 :help "Customize variables and other settings used by Verilog-Mode"]
1482 ["Customize Verilog Fonts & Colors" verilog-font-customize
1483 :help "Customize fonts used by Verilog-Mode."])))
1485 (easy-menu-define
1486 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1487 (verilog-easy-menu-filter
1488 '("Statements"
1489 ["Header" verilog-sk-header
1490 :help "Insert a header block at the top of file"]
1491 ["Comment" verilog-sk-comment
1492 :help "Insert a comment block"]
1493 "----"
1494 ["Module" verilog-sk-module
1495 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1496 ["OVM Class" verilog-sk-ovm-class
1497 :help "Insert an OVM class block"]
1498 ["UVM Class" verilog-sk-uvm-class
1499 :help "Insert an UVM class block"]
1500 ["Primitive" verilog-sk-primitive
1501 :help "Insert a primitive .. (.. );.. endprimitive block"]
1502 "----"
1503 ["Input" verilog-sk-input
1504 :help "Insert an input declaration"]
1505 ["Output" verilog-sk-output
1506 :help "Insert an output declaration"]
1507 ["Inout" verilog-sk-inout
1508 :help "Insert an inout declaration"]
1509 ["Wire" verilog-sk-wire
1510 :help "Insert a wire declaration"]
1511 ["Reg" verilog-sk-reg
1512 :help "Insert a register declaration"]
1513 ["Define thing under point as a register" verilog-sk-define-signal
1514 :help "Define signal under point as a register at the top of the module"]
1515 "----"
1516 ["Initial" verilog-sk-initial
1517 :help "Insert an initial begin .. end block"]
1518 ["Always" verilog-sk-always
1519 :help "Insert an always @(AS) begin .. end block"]
1520 ["Function" verilog-sk-function
1521 :help "Insert a function .. begin .. end endfunction block"]
1522 ["Task" verilog-sk-task
1523 :help "Insert a task .. begin .. end endtask block"]
1524 ["Specify" verilog-sk-specify
1525 :help "Insert a specify .. endspecify block"]
1526 ["Generate" verilog-sk-generate
1527 :help "Insert a generate .. endgenerate block"]
1528 "----"
1529 ["Begin" verilog-sk-begin
1530 :help "Insert a begin .. end block"]
1531 ["If" verilog-sk-if
1532 :help "Insert an if (..) begin .. end block"]
1533 ["(if) else" verilog-sk-else-if
1534 :help "Insert an else if (..) begin .. end block"]
1535 ["For" verilog-sk-for
1536 :help "Insert a for (...) begin .. end block"]
1537 ["While" verilog-sk-while
1538 :help "Insert a while (...) begin .. end block"]
1539 ["Fork" verilog-sk-fork
1540 :help "Insert a fork begin .. end .. join block"]
1541 ["Repeat" verilog-sk-repeat
1542 :help "Insert a repeat (..) begin .. end block"]
1543 ["Case" verilog-sk-case
1544 :help "Insert a case block, prompting for details"]
1545 ["Casex" verilog-sk-casex
1546 :help "Insert a casex (...) item: begin.. end endcase block"]
1547 ["Casez" verilog-sk-casez
1548 :help "Insert a casez (...) item: begin.. end endcase block"])))
1550 (defvar verilog-mode-abbrev-table nil
1551 "Abbrev table in use in Verilog-mode buffers.")
1553 (define-abbrev-table 'verilog-mode-abbrev-table ())
1554 (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1555 (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1556 (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1557 (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1558 (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1559 (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1560 (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1561 (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1562 (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1563 (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1564 (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1565 (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1566 (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1567 (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1568 (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1569 (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1570 (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1571 (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1572 (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1573 (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1574 (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1575 (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1576 (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1577 (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1578 (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
1581 ;; Macros
1584 (defsubst verilog-within-string ()
1585 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
1587 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1588 "Replace occurrences of FROM-STRING with TO-STRING.
1589 FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1590 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1591 will break, as the o's continuously replace. xa -> x works ok though."
1592 ;; Hopefully soon to an Emacs built-in
1593 ;; Also note \ in the replacement prevent multiple replacements; IE
1594 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1595 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
1596 (let ((start 0))
1597 (while (string-match from-string string start)
1598 (setq string (replace-match to-string fixedcase literal string)
1599 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1600 string))
1602 (defsubst verilog-string-remove-spaces (string)
1603 "Remove spaces surrounding STRING."
1604 (save-match-data
1605 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1606 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1607 string))
1609 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1610 ; checkdoc-params: (REGEXP BOUND NOERROR)
1611 "Like `re-search-forward', but skips over match in comments or strings."
1612 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1613 (while (and
1614 (re-search-forward REGEXP BOUND NOERROR)
1615 (setq mdata (match-data))
1616 (and (verilog-skip-forward-comment-or-string)
1617 (progn
1618 (setq mdata '(nil nil))
1619 (if BOUND
1620 (< (point) BOUND)
1621 t)))))
1622 (store-match-data mdata)
1623 (match-end 0)))
1625 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1626 ; checkdoc-params: (REGEXP BOUND NOERROR)
1627 "Like `re-search-backward', but skips over match in comments or strings."
1628 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1629 (while (and
1630 (re-search-backward REGEXP BOUND NOERROR)
1631 (setq mdata (match-data))
1632 (and (verilog-skip-backward-comment-or-string)
1633 (progn
1634 (setq mdata '(nil nil))
1635 (if BOUND
1636 (> (point) BOUND)
1637 t)))))
1638 (store-match-data mdata)
1639 (match-end 0)))
1641 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1642 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1643 but trashes match data and is faster for REGEXP that doesn't match often.
1644 This uses `verilog-scan' and text properties to ignore comments,
1645 so there may be a large up front penalty for the first search."
1646 (let (pt)
1647 (while (and (not pt)
1648 (re-search-forward regexp bound noerror))
1649 (if (verilog-inside-comment-or-string-p)
1650 (re-search-forward "[/\"\n]" nil t) ;; Only way a comment or quote can end
1651 (setq pt (match-end 0))))
1652 pt))
1654 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1655 ; checkdoc-params: (REGEXP BOUND NOERROR)
1656 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1657 but trashes match data and is faster for REGEXP that doesn't match often.
1658 This uses `verilog-scan' and text properties to ignore comments,
1659 so there may be a large up front penalty for the first search."
1660 (let (pt)
1661 (while (and (not pt)
1662 (re-search-backward regexp bound noerror))
1663 (if (verilog-inside-comment-or-string-p)
1664 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
1665 (setq pt (match-beginning 0))))
1666 pt))
1668 (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1669 "Like `re-search-forward', but first search for SUBSTR constant.
1670 Then searched for the normal REGEXP (which contains SUBSTR), with given
1671 BOUND and NOERROR. The REGEXP must fit within a single line.
1672 This speeds up complicated regexp matches."
1673 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1674 ;; thus require matches to be on one line, and use beginning-of-line.
1675 (let (done)
1676 (while (and (not done)
1677 (search-forward substr bound noerror))
1678 (save-excursion
1679 (beginning-of-line)
1680 (setq done (re-search-forward regexp (point-at-eol) noerror)))
1681 (unless (and (<= (match-beginning 0) (point))
1682 (>= (match-end 0) (point)))
1683 (setq done nil)))
1684 (when done (goto-char done))
1685 done))
1686 ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1688 (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1689 "Like `re-search-backward', but first search for SUBSTR constant.
1690 Then searched for the normal REGEXP (which contains SUBSTR), with given
1691 BOUND and NOERROR. The REGEXP must fit within a single line.
1692 This speeds up complicated regexp matches."
1693 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1694 ;; thus require matches to be on one line, and use beginning-of-line.
1695 (let (done)
1696 (while (and (not done)
1697 (search-backward substr bound noerror))
1698 (save-excursion
1699 (end-of-line)
1700 (setq done (re-search-backward regexp (point-at-bol) noerror)))
1701 (unless (and (<= (match-beginning 0) (point))
1702 (>= (match-end 0) (point)))
1703 (setq done nil)))
1704 (when done (goto-char done))
1705 done))
1706 ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1708 (defun verilog-delete-trailing-whitespace ()
1709 "Delete trailing spaces or tabs, but not newlines nor linefeeds."
1710 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1711 (save-excursion
1712 (goto-char (point-min))
1713 (while (re-search-forward "[ \t]+$" nil t) ;; Not syntactic WS as no formfeed
1714 (replace-match "" nil nil))))
1716 (defvar compile-command)
1718 ;; compilation program
1719 (defun verilog-set-compile-command ()
1720 "Function to compute shell command to compile Verilog.
1722 This reads `verilog-tool' and sets `compile-command'. This specifies the
1723 program that executes when you type \\[compile] or
1724 \\[verilog-auto-save-compile].
1726 By default `verilog-tool' uses a Makefile if one exists in the
1727 current directory. If not, it is set to the `verilog-linter',
1728 `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1729 or `verilog-simulator' variables, as selected with the Verilog ->
1730 \"Choose Compilation Action\" menu.
1732 You should set `verilog-tool' or the other variables to the path and
1733 arguments for your Verilog simulator. For example:
1734 \"vcs -p123 -O\"
1735 or a string like:
1736 \"(cd /tmp; surecov %s)\".
1738 In the former case, the path to the current buffer is concat'ed to the
1739 value of `verilog-tool'; in the later, the path to the current buffer is
1740 substituted for the %s.
1742 Where __FLAGS__ appears in the string `verilog-current-flags'
1743 will be substituted.
1745 Where __FILE__ appears in the string, the variable
1746 `buffer-file-name' of the current buffer, without the directory
1747 portion, will be substituted."
1748 (interactive)
1749 (cond
1750 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1751 (file-exists-p "Makefile"))
1752 (set (make-local-variable 'compile-command) "make "))
1754 (set (make-local-variable 'compile-command)
1755 (if verilog-tool
1756 (if (string-match "%s" (eval verilog-tool))
1757 (format (eval verilog-tool) (or buffer-file-name ""))
1758 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1759 ""))))
1760 (verilog-modify-compile-command))
1762 (defun verilog-expand-command (command)
1763 "Replace meta-information in COMMAND and return it.
1764 Where __FLAGS__ appears in the string `verilog-current-flags'
1765 will be substituted. Where __FILE__ appears in the string, the
1766 current buffer's file-name, without the directory portion, will
1767 be substituted."
1768 (setq command (verilog-string-replace-matches
1769 ;; Note \\b only works if under verilog syntax table
1770 "\\b__FLAGS__\\b" (verilog-current-flags)
1771 t t command))
1772 (setq command (verilog-string-replace-matches
1773 "\\b__FILE__\\b" (file-name-nondirectory
1774 (or (buffer-file-name) ""))
1775 t t command))
1776 command)
1778 (defun verilog-modify-compile-command ()
1779 "Update `compile-command' using `verilog-expand-command'."
1780 (when (and
1781 (stringp compile-command)
1782 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
1783 (set (make-local-variable 'compile-command)
1784 (verilog-expand-command compile-command))))
1786 (if (featurep 'xemacs)
1787 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1788 (defun verilog-error-regexp-add-xemacs ()
1789 "Teach XEmacs about verilog errors.
1790 Called by `compilation-mode-hook'. This allows \\[next-error] to
1791 find the errors."
1792 (interactive)
1793 (if (boundp 'compilation-error-regexp-systems-alist)
1794 (if (and
1795 (not (equal compilation-error-regexp-systems-list 'all))
1796 (not (member compilation-error-regexp-systems-list 'verilog)))
1797 (push 'verilog compilation-error-regexp-systems-list)))
1798 (if (boundp 'compilation-error-regexp-alist-alist)
1799 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1800 (setcdr compilation-error-regexp-alist-alist
1801 (cons verilog-error-regexp-xemacs-alist
1802 (cdr compilation-error-regexp-alist-alist)))))
1803 (if (boundp 'compilation-font-lock-keywords)
1804 (progn
1805 (set (make-local-variable 'compilation-font-lock-keywords)
1806 verilog-error-font-lock-keywords)
1807 (font-lock-set-defaults)))
1808 ;; Need to re-run compilation-error-regexp builder
1809 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1810 (compilation-build-compilation-error-regexp-alist))
1813 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1814 (defun verilog-error-regexp-add-emacs ()
1815 "Tell Emacs compile that we are Verilog.
1816 Called by `compilation-mode-hook'. This allows \\[next-error] to
1817 find the errors."
1818 (interactive)
1819 (if (boundp 'compilation-error-regexp-alist-alist)
1820 (progn
1821 (if (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1822 (mapcar
1823 (lambda (item)
1824 (push (car item) compilation-error-regexp-alist)
1825 (push item compilation-error-regexp-alist-alist)
1827 verilog-error-regexp-emacs-alist)))))
1829 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1830 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1832 (defconst verilog-directive-re
1833 (eval-when-compile
1834 (verilog-regexp-words
1836 "`case" "`default" "`define" "`else" "`elsif" "`endfor" "`endif"
1837 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1838 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1839 "`time_scale" "`undef" "`while" ))))
1841 (defconst verilog-directive-re-1
1842 (concat "[ \t]*" verilog-directive-re))
1844 (defconst verilog-directive-begin
1845 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1847 (defconst verilog-directive-middle
1848 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
1850 (defconst verilog-directive-end
1851 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1853 (defconst verilog-ovm-begin-re
1854 (eval-when-compile
1855 (verilog-regexp-opt
1857 "`ovm_component_utils_begin"
1858 "`ovm_component_param_utils_begin"
1859 "`ovm_field_utils_begin"
1860 "`ovm_object_utils_begin"
1861 "`ovm_object_param_utils_begin"
1862 "`ovm_sequence_utils_begin"
1863 "`ovm_sequencer_utils_begin"
1864 ) nil )))
1866 (defconst verilog-ovm-end-re
1867 (eval-when-compile
1868 (verilog-regexp-opt
1870 "`ovm_component_utils_end"
1871 "`ovm_field_utils_end"
1872 "`ovm_object_utils_end"
1873 "`ovm_sequence_utils_end"
1874 "`ovm_sequencer_utils_end"
1875 ) nil )))
1877 (defconst verilog-uvm-begin-re
1878 (eval-when-compile
1879 (verilog-regexp-opt
1881 "`uvm_component_utils_begin"
1882 "`uvm_component_param_utils_begin"
1883 "`uvm_field_utils_begin"
1884 "`uvm_object_utils_begin"
1885 "`uvm_object_param_utils_begin"
1886 "`uvm_sequence_utils_begin"
1887 "`uvm_sequencer_utils_begin"
1888 ) nil )))
1890 (defconst verilog-uvm-end-re
1891 (eval-when-compile
1892 (verilog-regexp-opt
1894 "`uvm_component_utils_end"
1895 "`uvm_field_utils_end"
1896 "`uvm_object_utils_end"
1897 "`uvm_sequence_utils_end"
1898 "`uvm_sequencer_utils_end"
1899 ) nil )))
1901 (defconst verilog-vmm-begin-re
1902 (eval-when-compile
1903 (verilog-regexp-opt
1905 "`vmm_data_member_begin"
1906 "`vmm_env_member_begin"
1907 "`vmm_scenario_member_begin"
1908 "`vmm_subenv_member_begin"
1909 "`vmm_xactor_member_begin"
1910 ) nil ) ) )
1912 (defconst verilog-vmm-end-re
1913 (eval-when-compile
1914 (verilog-regexp-opt
1916 "`vmm_data_member_end"
1917 "`vmm_env_member_end"
1918 "`vmm_scenario_member_end"
1919 "`vmm_subenv_member_end"
1920 "`vmm_xactor_member_end"
1921 ) nil ) ) )
1923 (defconst verilog-vmm-statement-re
1924 (eval-when-compile
1925 (verilog-regexp-opt
1927 ;; "`vmm_xactor_member_enum_array"
1928 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
1929 ;; "`vmm_xactor_member_scalar_array"
1930 ;; "`vmm_xactor_member_scalar"
1931 ) nil )))
1933 (defconst verilog-ovm-statement-re
1934 (eval-when-compile
1935 (verilog-regexp-opt
1937 ;; Statements
1938 "`DUT_ERROR"
1939 "`MESSAGE"
1940 "`dut_error"
1941 "`message"
1942 "`ovm_analysis_imp_decl"
1943 "`ovm_blocking_get_imp_decl"
1944 "`ovm_blocking_get_peek_imp_decl"
1945 "`ovm_blocking_master_imp_decl"
1946 "`ovm_blocking_peek_imp_decl"
1947 "`ovm_blocking_put_imp_decl"
1948 "`ovm_blocking_slave_imp_decl"
1949 "`ovm_blocking_transport_imp_decl"
1950 "`ovm_component_registry"
1951 "`ovm_component_registry_param"
1952 "`ovm_component_utils"
1953 "`ovm_create"
1954 "`ovm_create_seq"
1955 "`ovm_declare_sequence_lib"
1956 "`ovm_do"
1957 "`ovm_do_seq"
1958 "`ovm_do_seq_with"
1959 "`ovm_do_with"
1960 "`ovm_error"
1961 "`ovm_fatal"
1962 "`ovm_field_aa_int_byte"
1963 "`ovm_field_aa_int_byte_unsigned"
1964 "`ovm_field_aa_int_int"
1965 "`ovm_field_aa_int_int_unsigned"
1966 "`ovm_field_aa_int_integer"
1967 "`ovm_field_aa_int_integer_unsigned"
1968 "`ovm_field_aa_int_key"
1969 "`ovm_field_aa_int_longint"
1970 "`ovm_field_aa_int_longint_unsigned"
1971 "`ovm_field_aa_int_shortint"
1972 "`ovm_field_aa_int_shortint_unsigned"
1973 "`ovm_field_aa_int_string"
1974 "`ovm_field_aa_object_int"
1975 "`ovm_field_aa_object_string"
1976 "`ovm_field_aa_string_int"
1977 "`ovm_field_aa_string_string"
1978 "`ovm_field_array_int"
1979 "`ovm_field_array_object"
1980 "`ovm_field_array_string"
1981 "`ovm_field_enum"
1982 "`ovm_field_event"
1983 "`ovm_field_int"
1984 "`ovm_field_object"
1985 "`ovm_field_queue_int"
1986 "`ovm_field_queue_object"
1987 "`ovm_field_queue_string"
1988 "`ovm_field_sarray_int"
1989 "`ovm_field_string"
1990 "`ovm_field_utils"
1991 "`ovm_file"
1992 "`ovm_get_imp_decl"
1993 "`ovm_get_peek_imp_decl"
1994 "`ovm_info"
1995 "`ovm_info1"
1996 "`ovm_info2"
1997 "`ovm_info3"
1998 "`ovm_info4"
1999 "`ovm_line"
2000 "`ovm_master_imp_decl"
2001 "`ovm_msg_detail"
2002 "`ovm_non_blocking_transport_imp_decl"
2003 "`ovm_nonblocking_get_imp_decl"
2004 "`ovm_nonblocking_get_peek_imp_decl"
2005 "`ovm_nonblocking_master_imp_decl"
2006 "`ovm_nonblocking_peek_imp_decl"
2007 "`ovm_nonblocking_put_imp_decl"
2008 "`ovm_nonblocking_slave_imp_decl"
2009 "`ovm_object_registry"
2010 "`ovm_object_registry_param"
2011 "`ovm_object_utils"
2012 "`ovm_peek_imp_decl"
2013 "`ovm_phase_func_decl"
2014 "`ovm_phase_task_decl"
2015 "`ovm_print_aa_int_object"
2016 "`ovm_print_aa_string_int"
2017 "`ovm_print_aa_string_object"
2018 "`ovm_print_aa_string_string"
2019 "`ovm_print_array_int"
2020 "`ovm_print_array_object"
2021 "`ovm_print_array_string"
2022 "`ovm_print_object_queue"
2023 "`ovm_print_queue_int"
2024 "`ovm_print_string_queue"
2025 "`ovm_put_imp_decl"
2026 "`ovm_rand_send"
2027 "`ovm_rand_send_with"
2028 "`ovm_send"
2029 "`ovm_sequence_utils"
2030 "`ovm_slave_imp_decl"
2031 "`ovm_transport_imp_decl"
2032 "`ovm_update_sequence_lib"
2033 "`ovm_update_sequence_lib_and_item"
2034 "`ovm_warning"
2035 "`static_dut_error"
2036 "`static_message") nil )))
2038 (defconst verilog-uvm-statement-re
2039 (eval-when-compile
2040 (verilog-regexp-opt
2042 ;; Statements
2043 "`uvm_analysis_imp_decl"
2044 "`uvm_blocking_get_imp_decl"
2045 "`uvm_blocking_get_peek_imp_decl"
2046 "`uvm_blocking_master_imp_decl"
2047 "`uvm_blocking_peek_imp_decl"
2048 "`uvm_blocking_put_imp_decl"
2049 "`uvm_blocking_slave_imp_decl"
2050 "`uvm_blocking_transport_imp_decl"
2051 "`uvm_component_param_utils"
2052 "`uvm_component_registry"
2053 "`uvm_component_registry_param"
2054 "`uvm_component_utils"
2055 "`uvm_create"
2056 "`uvm_create_on"
2057 "`uvm_create_seq" ;; Undocumented in 1.1
2058 "`uvm_declare_p_sequencer"
2059 "`uvm_declare_sequence_lib" ;; Deprecated in 1.1
2060 "`uvm_do"
2061 "`uvm_do_callbacks"
2062 "`uvm_do_callbacks_exit_on"
2063 "`uvm_do_obj_callbacks"
2064 "`uvm_do_obj_callbacks_exit_on"
2065 "`uvm_do_on"
2066 "`uvm_do_on_pri"
2067 "`uvm_do_on_pri_with"
2068 "`uvm_do_on_with"
2069 "`uvm_do_pri"
2070 "`uvm_do_pri_with"
2071 "`uvm_do_seq" ;; Undocumented in 1.1
2072 "`uvm_do_seq_with" ;; Undocumented in 1.1
2073 "`uvm_do_with"
2074 "`uvm_error"
2075 "`uvm_error_context"
2076 "`uvm_fatal"
2077 "`uvm_fatal_context"
2078 "`uvm_field_aa_int_byte"
2079 "`uvm_field_aa_int_byte_unsigned"
2080 "`uvm_field_aa_int_enum"
2081 "`uvm_field_aa_int_int"
2082 "`uvm_field_aa_int_int_unsigned"
2083 "`uvm_field_aa_int_integer"
2084 "`uvm_field_aa_int_integer_unsigned"
2085 "`uvm_field_aa_int_key"
2086 "`uvm_field_aa_int_longint"
2087 "`uvm_field_aa_int_longint_unsigned"
2088 "`uvm_field_aa_int_shortint"
2089 "`uvm_field_aa_int_shortint_unsigned"
2090 "`uvm_field_aa_int_string"
2091 "`uvm_field_aa_object_int"
2092 "`uvm_field_aa_object_string"
2093 "`uvm_field_aa_string_int"
2094 "`uvm_field_aa_string_string"
2095 "`uvm_field_array_enum"
2096 "`uvm_field_array_int"
2097 "`uvm_field_array_object"
2098 "`uvm_field_array_string"
2099 "`uvm_field_enum"
2100 "`uvm_field_event"
2101 "`uvm_field_int"
2102 "`uvm_field_object"
2103 "`uvm_field_queue_enum"
2104 "`uvm_field_queue_int"
2105 "`uvm_field_queue_object"
2106 "`uvm_field_queue_string"
2107 "`uvm_field_real"
2108 "`uvm_field_sarray_enum"
2109 "`uvm_field_sarray_int"
2110 "`uvm_field_sarray_object"
2111 "`uvm_field_sarray_string"
2112 "`uvm_field_string"
2113 "`uvm_field_utils"
2114 "`uvm_file" ;; Undocumented in 1.1, use `__FILE__
2115 "`uvm_get_imp_decl"
2116 "`uvm_get_peek_imp_decl"
2117 "`uvm_info"
2118 "`uvm_info_context"
2119 "`uvm_line" ;; Undocumented in 1.1, use `__LINE__
2120 "`uvm_master_imp_decl"
2121 "`uvm_non_blocking_transport_imp_decl" ;; Deprecated in 1.1
2122 "`uvm_nonblocking_get_imp_decl"
2123 "`uvm_nonblocking_get_peek_imp_decl"
2124 "`uvm_nonblocking_master_imp_decl"
2125 "`uvm_nonblocking_peek_imp_decl"
2126 "`uvm_nonblocking_put_imp_decl"
2127 "`uvm_nonblocking_slave_imp_decl"
2128 "`uvm_nonblocking_transport_imp_decl"
2129 "`uvm_object_param_utils"
2130 "`uvm_object_registry"
2131 "`uvm_object_registry_param" ;; Undocumented in 1.1
2132 "`uvm_object_utils"
2133 "`uvm_pack_array"
2134 "`uvm_pack_arrayN"
2135 "`uvm_pack_enum"
2136 "`uvm_pack_enumN"
2137 "`uvm_pack_int"
2138 "`uvm_pack_intN"
2139 "`uvm_pack_queue"
2140 "`uvm_pack_queueN"
2141 "`uvm_pack_real"
2142 "`uvm_pack_sarray"
2143 "`uvm_pack_sarrayN"
2144 "`uvm_pack_string"
2145 "`uvm_peek_imp_decl"
2146 "`uvm_put_imp_decl"
2147 "`uvm_rand_send"
2148 "`uvm_rand_send_pri"
2149 "`uvm_rand_send_pri_with"
2150 "`uvm_rand_send_with"
2151 "`uvm_record_attribute"
2152 "`uvm_record_field"
2153 "`uvm_register_cb"
2154 "`uvm_send"
2155 "`uvm_send_pri"
2156 "`uvm_sequence_utils" ;; Deprecated in 1.1
2157 "`uvm_set_super_type"
2158 "`uvm_slave_imp_decl"
2159 "`uvm_transport_imp_decl"
2160 "`uvm_unpack_array"
2161 "`uvm_unpack_arrayN"
2162 "`uvm_unpack_enum"
2163 "`uvm_unpack_enumN"
2164 "`uvm_unpack_int"
2165 "`uvm_unpack_intN"
2166 "`uvm_unpack_queue"
2167 "`uvm_unpack_queueN"
2168 "`uvm_unpack_real"
2169 "`uvm_unpack_sarray"
2170 "`uvm_unpack_sarrayN"
2171 "`uvm_unpack_string"
2172 "`uvm_update_sequence_lib" ;; Deprecated in 1.1
2173 "`uvm_update_sequence_lib_and_item" ;; Deprecated in 1.1
2174 "`uvm_warning"
2175 "`uvm_warning_context") nil )))
2179 ;; Regular expressions used to calculate indent, etc.
2181 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
2182 ;; Want to match
2183 ;; aa :
2184 ;; aa,bb :
2185 ;; a[34:32] :
2186 ;; a,
2187 ;; b :
2188 (defconst verilog-assignment-operator-re
2189 (eval-when-compile
2190 (verilog-regexp-opt
2192 ;; blocking assignment_operator
2193 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2194 ;; non blocking assignment operator
2195 "<="
2196 ;; comparison
2197 "==" "!=" "===" "!===" "<=" ">=" "==\?" "!=\?"
2198 ;; event_trigger
2199 "->" "->>"
2200 ;; property_expr
2201 "|->" "|=>"
2202 ;; Is this a legal verilog operator?
2203 ":="
2204 ) 't
2206 (defconst verilog-assignment-operation-re
2207 (concat
2208 ; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2209 ; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2210 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
2212 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
2213 (defconst verilog-property-re
2214 (concat "\\(" verilog-label-re "\\)?"
2215 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2216 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2218 (defconst verilog-no-indent-begin-re
2219 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
2221 (defconst verilog-ends-re
2222 ;; Parenthesis indicate type of keyword found
2223 (concat
2224 "\\(\\<else\\>\\)\\|" ; 1
2225 "\\(\\<if\\>\\)\\|" ; 2
2226 "\\(\\<assert\\>\\)\\|" ; 3
2227 "\\(\\<end\\>\\)\\|" ; 3.1
2228 "\\(\\<endcase\\>\\)\\|" ; 4
2229 "\\(\\<endfunction\\>\\)\\|" ; 5
2230 "\\(\\<endtask\\>\\)\\|" ; 6
2231 "\\(\\<endspecify\\>\\)\\|" ; 7
2232 "\\(\\<endtable\\>\\)\\|" ; 8
2233 "\\(\\<endgenerate\\>\\)\\|" ; 9
2234 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2235 "\\(\\<endclass\\>\\)\\|" ; 11
2236 "\\(\\<endgroup\\>\\)\\|" ; 12
2237 ;; VMM
2238 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2239 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2240 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2241 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2242 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
2243 ;; OVM
2244 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2245 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2246 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2247 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2248 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
2249 ;; UVM
2250 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2251 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2252 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2253 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2254 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
2257 (defconst verilog-auto-end-comment-lines-re
2258 ;; Matches to names in this list cause auto-end-commenting
2259 (concat "\\("
2260 verilog-directive-re "\\)\\|\\("
2261 (eval-when-compile
2262 (verilog-regexp-words
2263 `( "begin"
2264 "else"
2265 "end"
2266 "endcase"
2267 "endclass"
2268 "endclocking"
2269 "endgroup"
2270 "endfunction"
2271 "endmodule"
2272 "endprogram"
2273 "endprimitive"
2274 "endinterface"
2275 "endpackage"
2276 "endsequence"
2277 "endspecify"
2278 "endtable"
2279 "endtask"
2280 "join"
2281 "join_any"
2282 "join_none"
2283 "module"
2284 "macromodule"
2285 "primitive"
2286 "interface"
2287 "package")))
2288 "\\)"))
2290 ;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2291 ;;; verilog-end-block-ordered-re matches exactly the same strings.
2292 (defconst verilog-end-block-ordered-re
2293 ;; Parenthesis indicate type of keyword found
2294 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2295 "\\(\\<end\\>\\)\\|" ; 2
2296 "\\(\\<end" ; 3, but not used
2297 "\\(" ; 4, but not used
2298 "\\(function\\)\\|" ; 5
2299 "\\(task\\)\\|" ; 6
2300 "\\(module\\)\\|" ; 7
2301 "\\(primitive\\)\\|" ; 8
2302 "\\(interface\\)\\|" ; 9
2303 "\\(package\\)\\|" ; 10
2304 "\\(class\\)\\|" ; 11
2305 "\\(group\\)\\|" ; 12
2306 "\\(program\\)\\|" ; 13
2307 "\\(sequence\\)\\|" ; 14
2308 "\\(clocking\\)\\|" ; 15
2309 "\\)\\>\\)"))
2310 (defconst verilog-end-block-re
2311 (eval-when-compile
2312 (verilog-regexp-words
2314 `("end" ;; closes begin
2315 "endcase" ;; closes any of case, casex casez or randcase
2316 "join" "join_any" "join_none" ;; closes fork
2317 "endclass"
2318 "endtable"
2319 "endspecify"
2320 "endfunction"
2321 "endgenerate"
2322 "endtask"
2323 "endgroup"
2324 "endproperty"
2325 "endinterface"
2326 "endpackage"
2327 "endprogram"
2328 "endsequence"
2329 "endclocking"
2330 ;; OVM
2331 "`ovm_component_utils_end"
2332 "`ovm_field_utils_end"
2333 "`ovm_object_utils_end"
2334 "`ovm_sequence_utils_end"
2335 "`ovm_sequencer_utils_end"
2336 ;; UVM
2337 "`uvm_component_utils_end"
2338 "`uvm_field_utils_end"
2339 "`uvm_object_utils_end"
2340 "`uvm_sequence_utils_end"
2341 "`uvm_sequencer_utils_end"
2342 ;; VMM
2343 "`vmm_data_member_end"
2344 "`vmm_env_member_end"
2345 "`vmm_scenario_member_end"
2346 "`vmm_subenv_member_end"
2347 "`vmm_xactor_member_end"
2348 ))))
2351 (defconst verilog-endcomment-reason-re
2352 ;; Parenthesis indicate type of keyword found
2353 (concat
2354 "\\(\\<begin\\>\\)\\|" ; 1
2355 "\\(\\<else\\>\\)\\|" ; 2
2356 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2357 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|" ; 4
2358 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|" ; 5
2359 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|" ; 6
2360 "\\(\\<fork\\>\\)\\|" ; 7
2361 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
2362 "\\(\\<if\\>\\)\\|"
2363 verilog-property-re "\\|"
2364 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
2365 "\\(\\<clocking\\>\\)\\|"
2366 "\\(\\<task\\>\\)\\|"
2367 "\\(\\<function\\>\\)\\|"
2368 "\\(\\<initial\\>\\)\\|"
2369 "\\(\\<interface\\>\\)\\|"
2370 "\\(\\<package\\>\\)\\|"
2371 "\\(\\<final\\>\\)\\|"
2372 "\\(@\\)\\|"
2373 "\\(\\<while\\>\\)\\|"
2374 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2375 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2376 "#"))
2378 (defconst verilog-named-block-re "begin[ \t]*:")
2380 ;; These words begin a block which can occur inside a module which should be indented,
2381 ;; and closed with the respective word from the end-block list
2383 (defconst verilog-beg-block-re
2384 (eval-when-compile
2385 (verilog-regexp-words
2386 `("begin"
2387 "case" "casex" "casez" "randcase"
2388 "clocking"
2389 "generate"
2390 "fork"
2391 "function"
2392 "property"
2393 "specify"
2394 "table"
2395 "task"
2396 ;; OVM
2397 "`ovm_component_utils_begin"
2398 "`ovm_component_param_utils_begin"
2399 "`ovm_field_utils_begin"
2400 "`ovm_object_utils_begin"
2401 "`ovm_object_param_utils_begin"
2402 "`ovm_sequence_utils_begin"
2403 "`ovm_sequencer_utils_begin"
2404 ;; UVM
2405 "`uvm_component_utils_begin"
2406 "`uvm_component_param_utils_begin"
2407 "`uvm_field_utils_begin"
2408 "`uvm_object_utils_begin"
2409 "`uvm_object_param_utils_begin"
2410 "`uvm_sequence_utils_begin"
2411 "`uvm_sequencer_utils_begin"
2412 ;; VMM
2413 "`vmm_data_member_begin"
2414 "`vmm_env_member_begin"
2415 "`vmm_scenario_member_begin"
2416 "`vmm_subenv_member_begin"
2417 "`vmm_xactor_member_begin"
2418 ))))
2419 ;; These are the same words, in a specific order in the regular
2420 ;; expression so that matching will work nicely for
2421 ;; verilog-forward-sexp and verilog-calc-indent
2422 (defconst verilog-beg-block-re-ordered
2423 ( concat "\\(\\<begin\\>\\)" ;1
2424 "\\|\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
2425 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
2426 "\\|\\(\\<class\\>\\)" ;6
2427 "\\|\\(\\<table\\>\\)" ;7
2428 "\\|\\(\\<specify\\>\\)" ;8
2429 "\\|\\(\\<function\\>\\)" ;9
2430 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2431 "\\|\\(\\<task\\>\\)" ;14
2432 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2433 "\\|\\(\\<generate\\>\\)" ;18
2434 "\\|\\(\\<covergroup\\>\\)" ;16 20
2435 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2436 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2437 "\\|\\(\\<clocking\\>\\)" ;22 27
2438 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
2439 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
2443 (defconst verilog-end-block-ordered-rry
2444 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2445 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2446 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2447 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2448 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2449 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2450 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2451 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2452 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2453 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2454 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2455 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2456 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2459 (defconst verilog-nameable-item-re
2460 (eval-when-compile
2461 (verilog-regexp-words
2462 `("begin"
2463 "fork"
2464 "join" "join_any" "join_none"
2465 "end"
2466 "endcase"
2467 "endconfig"
2468 "endclass"
2469 "endclocking"
2470 "endfunction"
2471 "endgenerate"
2472 "endmodule"
2473 "endprimitive"
2474 "endinterface"
2475 "endpackage"
2476 "endspecify"
2477 "endtable"
2478 "endtask" )
2481 (defconst verilog-declaration-opener
2482 (eval-when-compile
2483 (verilog-regexp-words
2484 `("module" "begin" "task" "function"))))
2486 (defconst verilog-declaration-prefix-re
2487 (eval-when-compile
2488 (verilog-regexp-words
2490 ;; port direction
2491 "inout" "input" "output" "ref"
2492 ;; changeableness
2493 "const" "static" "protected" "local"
2494 ;; parameters
2495 "localparam" "parameter" "var"
2496 ;; type creation
2497 "typedef"
2498 ))))
2499 (defconst verilog-declaration-core-re
2500 (eval-when-compile
2501 (verilog-regexp-words
2503 ;; port direction (by themselves)
2504 "inout" "input" "output"
2505 ;; integer_atom_type
2506 "byte" "shortint" "int" "longint" "integer" "time"
2507 ;; integer_vector_type
2508 "bit" "logic" "reg"
2509 ;; non_integer_type
2510 "shortreal" "real" "realtime"
2511 ;; net_type
2512 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2513 ;; misc
2514 "string" "event" "chandle" "virtual" "enum" "genvar"
2515 "struct" "union"
2516 ;; builtin classes
2517 "mailbox" "semaphore"
2518 ))))
2519 (defconst verilog-declaration-re
2520 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2521 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2522 (defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
2523 (defconst verilog-optional-signed-range-re
2524 (concat
2525 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2526 (defconst verilog-macroexp-re "`\\sw+")
2528 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2529 (defconst verilog-declaration-re-2-no-macro
2530 (concat "\\s-*" verilog-declaration-re
2531 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2532 "\\)?"))
2533 (defconst verilog-declaration-re-2-macro
2534 (concat "\\s-*" verilog-declaration-re
2535 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2536 "\\|\\(" verilog-macroexp-re "\\)"
2537 "\\)?"))
2538 (defconst verilog-declaration-re-1-macro
2539 (concat "^" verilog-declaration-re-2-macro))
2541 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2543 (defconst verilog-defun-re
2544 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2545 (defconst verilog-end-defun-re
2546 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2547 (defconst verilog-zero-indent-re
2548 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2549 (defconst verilog-inst-comment-re
2550 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
2552 (defconst verilog-behavioral-block-beg-re
2553 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2554 "function" "task"))))
2555 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2556 (defconst verilog-indent-re
2557 (eval-when-compile
2558 (verilog-regexp-words
2561 "always" "always_latch" "always_ff" "always_comb"
2562 "begin" "end"
2563 ; "unique" "priority"
2564 "case" "casex" "casez" "randcase" "endcase"
2565 "class" "endclass"
2566 "clocking" "endclocking"
2567 "config" "endconfig"
2568 "covergroup" "endgroup"
2569 "fork" "join" "join_any" "join_none"
2570 "function" "endfunction"
2571 "final"
2572 "generate" "endgenerate"
2573 "initial"
2574 "interface" "endinterface"
2575 "module" "macromodule" "endmodule"
2576 "package" "endpackage"
2577 "primitive" "endprimitive"
2578 "program" "endprogram"
2579 "property" "endproperty"
2580 "sequence" "randsequence" "endsequence"
2581 "specify" "endspecify"
2582 "table" "endtable"
2583 "task" "endtask"
2584 "virtual"
2585 "`case"
2586 "`default"
2587 "`define" "`undef"
2588 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
2589 "`while" "`endwhile"
2590 "`for" "`endfor"
2591 "`format"
2592 "`include"
2593 "`let"
2594 "`protect" "`endprotect"
2595 "`switch" "`endswitch"
2596 "`timescale"
2597 "`time_scale"
2598 ;; OVM Begin tokens
2599 "`ovm_component_utils_begin"
2600 "`ovm_component_param_utils_begin"
2601 "`ovm_field_utils_begin"
2602 "`ovm_object_utils_begin"
2603 "`ovm_object_param_utils_begin"
2604 "`ovm_sequence_utils_begin"
2605 "`ovm_sequencer_utils_begin"
2606 ;; OVM End tokens
2607 "`ovm_component_utils_end"
2608 "`ovm_field_utils_end"
2609 "`ovm_object_utils_end"
2610 "`ovm_sequence_utils_end"
2611 "`ovm_sequencer_utils_end"
2612 ;; UVM Begin tokens
2613 "`uvm_component_utils_begin"
2614 "`uvm_component_param_utils_begin"
2615 "`uvm_field_utils_begin"
2616 "`uvm_object_utils_begin"
2617 "`uvm_object_param_utils_begin"
2618 "`uvm_sequence_utils_begin"
2619 "`uvm_sequencer_utils_begin"
2620 ;; UVM End tokens
2621 "`uvm_component_utils_end" ;; Typo in spec, it's not uvm_component_end
2622 "`uvm_field_utils_end"
2623 "`uvm_object_utils_end"
2624 "`uvm_sequence_utils_end"
2625 "`uvm_sequencer_utils_end"
2626 ;; VMM Begin tokens
2627 "`vmm_data_member_begin"
2628 "`vmm_env_member_begin"
2629 "`vmm_scenario_member_begin"
2630 "`vmm_subenv_member_begin"
2631 "`vmm_xactor_member_begin"
2632 ;; VMM End tokens
2633 "`vmm_data_member_end"
2634 "`vmm_env_member_end"
2635 "`vmm_scenario_member_end"
2636 "`vmm_subenv_member_end"
2637 "`vmm_xactor_member_end"
2638 ))))
2640 (defconst verilog-defun-level-not-generate-re
2641 (eval-when-compile
2642 (verilog-regexp-words
2643 `( "module" "macromodule" "primitive" "class" "program"
2644 "interface" "package" "config"))))
2646 (defconst verilog-defun-level-re
2647 (eval-when-compile
2648 (verilog-regexp-words
2649 (append
2650 `( "module" "macromodule" "primitive" "class" "program"
2651 "interface" "package" "config")
2652 `( "initial" "final" "always" "always_comb" "always_ff"
2653 "always_latch" "endtask" "endfunction" )))))
2655 (defconst verilog-defun-level-generate-only-re
2656 (eval-when-compile
2657 (verilog-regexp-words
2658 `( "initial" "final" "always" "always_comb" "always_ff"
2659 "always_latch" "endtask" "endfunction" ))))
2661 (defconst verilog-cpp-level-re
2662 (eval-when-compile
2663 (verilog-regexp-words
2665 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2666 ))))
2667 (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
2668 (defconst verilog-extended-case-re "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
2669 (defconst verilog-extended-complete-re
2670 (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<pure\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
2671 "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
2672 "\\|\\(\\(\\<import\\>\\s-+\\)?\\(\"DPI-C\"\\s-+\\)?\\(\\<pure\\>\\s-+\\)?\\(function\\>\\|task\\>\\)\\)"
2673 "\\|" verilog-extended-case-re ))
2674 (defconst verilog-basic-complete-re
2675 (eval-when-compile
2676 (verilog-regexp-words
2678 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2679 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2680 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2681 ))))
2682 (defconst verilog-complete-reg
2683 (concat
2684 verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
2686 (defconst verilog-end-statement-re
2687 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2688 verilog-end-block-re "\\)"))
2690 (defconst verilog-endcase-re
2691 (concat verilog-extended-case-re "\\|"
2692 "\\(endcase\\)\\|"
2693 verilog-defun-re
2696 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2697 "String used to mark beginning of excluded text.")
2698 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2699 "String used to mark end of excluded text.")
2700 (defconst verilog-preprocessor-re
2701 (eval-when-compile
2702 (verilog-regexp-words
2704 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
2705 ))))
2707 (defconst verilog-keywords
2708 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
2709 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
2710 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
2711 "`time_scale" "`undef" "`while"
2713 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2714 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2715 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2716 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2717 "config" "const" "constraint" "context" "continue" "cover"
2718 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2719 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2720 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2721 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2722 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2723 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2724 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2725 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2726 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2727 "include" "initial" "inout" "input" "inside" "instance" "int"
2728 "integer" "interface" "intersect" "join" "join_any" "join_none"
2729 "large" "liblist" "library" "local" "localparam" "logic"
2730 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2731 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2732 "notif0" "notif1" "null" "or" "output" "package" "packed"
2733 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2734 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2735 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2736 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2737 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2738 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2739 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2740 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2741 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2742 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2743 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2744 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2745 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2746 "wire" "with" "within" "wor" "xnor" "xor"
2747 ;; 1800-2009
2748 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2749 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2750 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2751 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2753 "List of Verilog keywords.")
2755 (defconst verilog-comment-start-regexp "//\\|/\\*"
2756 "Dual comment value for `comment-start-regexp'.")
2758 (defvar verilog-mode-syntax-table
2759 (let ((table (make-syntax-table)))
2760 ;; Populate the syntax TABLE.
2761 (modify-syntax-entry ?\\ "\\" table)
2762 (modify-syntax-entry ?+ "." table)
2763 (modify-syntax-entry ?- "." table)
2764 (modify-syntax-entry ?= "." table)
2765 (modify-syntax-entry ?% "." table)
2766 (modify-syntax-entry ?< "." table)
2767 (modify-syntax-entry ?> "." table)
2768 (modify-syntax-entry ?& "." table)
2769 (modify-syntax-entry ?| "." table)
2770 (modify-syntax-entry ?` "w" table)
2771 (modify-syntax-entry ?_ "w" table)
2772 (modify-syntax-entry ?\' "." table)
2774 ;; Set up TABLE to handle block and line style comments.
2775 (if (featurep 'xemacs)
2776 (progn
2777 ;; XEmacs (formerly Lucid) has the best implementation
2778 (modify-syntax-entry ?/ ". 1456" table)
2779 (modify-syntax-entry ?* ". 23" table)
2780 (modify-syntax-entry ?\n "> b" table))
2781 ;; Emacs does things differently, but we can work with it
2782 (modify-syntax-entry ?/ ". 124b" table)
2783 (modify-syntax-entry ?* ". 23" table)
2784 (modify-syntax-entry ?\n "> b" table))
2785 table)
2786 "Syntax table used in Verilog mode buffers.")
2788 (defvar verilog-font-lock-keywords nil
2789 "Default highlighting for Verilog mode.")
2791 (defvar verilog-font-lock-keywords-1 nil
2792 "Subdued level highlighting for Verilog mode.")
2794 (defvar verilog-font-lock-keywords-2 nil
2795 "Medium level highlighting for Verilog mode.
2796 See also `verilog-font-lock-extra-types'.")
2798 (defvar verilog-font-lock-keywords-3 nil
2799 "Gaudy level highlighting for Verilog mode.
2800 See also `verilog-font-lock-extra-types'.")
2802 (defvar verilog-font-lock-translate-off-face
2803 'verilog-font-lock-translate-off-face
2804 "Font to use for translated off regions.")
2805 (defface verilog-font-lock-translate-off-face
2806 '((((class color)
2807 (background light))
2808 (:background "gray90" :italic t ))
2809 (((class color)
2810 (background dark))
2811 (:background "gray10" :italic t ))
2812 (((class grayscale) (background light))
2813 (:foreground "DimGray" :italic t))
2814 (((class grayscale) (background dark))
2815 (:foreground "LightGray" :italic t))
2816 (t (:italis t)))
2817 "Font lock mode face used to background highlight translate-off regions."
2818 :group 'font-lock-highlighting-faces)
2820 (defvar verilog-font-lock-p1800-face
2821 'verilog-font-lock-p1800-face
2822 "Font to use for p1800 keywords.")
2823 (defface verilog-font-lock-p1800-face
2824 '((((class color)
2825 (background light))
2826 (:foreground "DarkOrange3" :bold t ))
2827 (((class color)
2828 (background dark))
2829 (:foreground "orange1" :bold t ))
2830 (t (:italic t)))
2831 "Font lock mode face used to highlight P1800 keywords."
2832 :group 'font-lock-highlighting-faces)
2834 (defvar verilog-font-lock-ams-face
2835 'verilog-font-lock-ams-face
2836 "Font to use for Analog/Mixed Signal keywords.")
2837 (defface verilog-font-lock-ams-face
2838 '((((class color)
2839 (background light))
2840 (:foreground "Purple" :bold t ))
2841 (((class color)
2842 (background dark))
2843 (:foreground "orange1" :bold t ))
2844 (t (:italic t)))
2845 "Font lock mode face used to highlight AMS keywords."
2846 :group 'font-lock-highlighting-faces)
2848 (defvar verilog-font-grouping-keywords-face
2849 'verilog-font-lock-grouping-keywords-face
2850 "Font to use for Verilog Grouping Keywords (such as begin..end).")
2851 (defface verilog-font-lock-grouping-keywords-face
2852 '((((class color)
2853 (background light))
2854 (:foreground "red4" :bold t ))
2855 (((class color)
2856 (background dark))
2857 (:foreground "red4" :bold t ))
2858 (t (:italic t)))
2859 "Font lock mode face used to highlight verilog grouping keywords."
2860 :group 'font-lock-highlighting-faces)
2862 (let* ((verilog-type-font-keywords
2863 (eval-when-compile
2864 (verilog-regexp-opt
2866 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
2867 "event" "genvar" "inout" "input" "integer" "localparam"
2868 "logic" "mailbox" "nand" "nmos" "not" "notif0" "notif1" "or"
2869 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
2870 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
2871 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
2872 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
2873 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
2874 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
2875 ) nil )))
2877 (verilog-pragma-keywords
2878 (eval-when-compile
2879 (verilog-regexp-opt
2880 '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
2881 ) nil )))
2883 (verilog-1800-2005-keywords
2884 (eval-when-compile
2885 (verilog-regexp-opt
2886 '("alias" "assert" "assume" "automatic" "before" "bind"
2887 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
2888 "clocking" "config" "const" "constraint" "context" "continue"
2889 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
2890 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
2891 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
2892 "expect" "export" "extends" "extern" "first_match" "foreach"
2893 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
2894 "illegal_bins" "import" "incdir" "include" "inside" "instance"
2895 "int" "intersect" "large" "liblist" "library" "local" "longint"
2896 "matches" "medium" "modport" "new" "noshowcancelled" "null"
2897 "packed" "program" "property" "protected" "pull0" "pull1"
2898 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2899 "randcase" "randsequence" "ref" "release" "return" "scalared"
2900 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
2901 "specparam" "static" "string" "strong0" "strong1" "struct"
2902 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
2903 "type" "union" "unsigned" "use" "var" "virtual" "void"
2904 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
2905 ) nil )))
2907 (verilog-1800-2009-keywords
2908 (eval-when-compile
2909 (verilog-regexp-opt
2910 '("accept_on" "checker" "endchecker" "eventually" "global"
2911 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
2912 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
2913 "sync_accept_on" "sync_reject_on" "unique0" "until"
2914 "until_with" "untyped" "weak" ) nil )))
2916 (verilog-ams-keywords
2917 (eval-when-compile
2918 (verilog-regexp-opt
2919 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
2920 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
2921 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
2922 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
2923 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
2924 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
2925 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
2926 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
2927 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
2928 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
2929 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
2931 (verilog-font-keywords
2932 (eval-when-compile
2933 (verilog-regexp-opt
2935 "assign" "case" "casex" "casez" "randcase" "deassign"
2936 "default" "disable" "else" "endcase" "endfunction"
2937 "endgenerate" "endinterface" "endmodule" "endprimitive"
2938 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
2939 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
2940 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
2941 "package" "endpackage" "always" "always_comb" "always_ff"
2942 "always_latch" "posedge" "primitive" "priority" "release"
2943 "repeat" "specify" "table" "task" "unique" "wait" "while"
2944 "class" "program" "endclass" "endprogram"
2945 ) nil )))
2947 (verilog-font-grouping-keywords
2948 (eval-when-compile
2949 (verilog-regexp-opt
2950 '( "begin" "end" ) nil ))))
2952 (setq verilog-font-lock-keywords
2953 (list
2954 ;; Fontify all builtin keywords
2955 (concat "\\<\\(" verilog-font-keywords "\\|"
2956 ;; And user/system tasks and functions
2957 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
2958 "\\)\\>")
2959 ;; Fontify all types
2960 (if verilog-highlight-grouping-keywords
2961 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2962 'verilog-font-lock-ams-face)
2963 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2964 'font-lock-type-face))
2965 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
2966 'font-lock-type-face)
2967 ;; Fontify IEEE-1800-2005 keywords appropriately
2968 (if verilog-highlight-p1800-keywords
2969 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2970 'verilog-font-lock-p1800-face)
2971 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2972 'font-lock-type-face))
2973 ;; Fontify IEEE-1800-2009 keywords appropriately
2974 (if verilog-highlight-p1800-keywords
2975 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2976 'verilog-font-lock-p1800-face)
2977 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2978 'font-lock-type-face))
2979 ;; Fontify Verilog-AMS keywords
2980 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
2981 'verilog-font-lock-ams-face)))
2983 (setq verilog-font-lock-keywords-1
2984 (append verilog-font-lock-keywords
2985 (list
2986 ;; Fontify module definitions
2987 (list
2988 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
2989 '(1 font-lock-keyword-face)
2990 '(3 font-lock-function-name-face 'prepend))
2991 ;; Fontify function definitions
2992 (list
2993 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
2994 '(1 font-lock-keyword-face)
2995 '(3 font-lock-constant-face prepend))
2996 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
2997 (1 font-lock-keyword-face)
2998 (2 font-lock-constant-face append))
2999 '("\\<function\\>\\s-+\\(\\sw+\\)"
3000 1 'font-lock-constant-face append))))
3002 (setq verilog-font-lock-keywords-2
3003 (append verilog-font-lock-keywords-1
3004 (list
3005 ;; Fontify pragmas
3006 (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
3007 ;; Fontify escaped names
3008 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
3009 ;; Fontify macro definitions/ uses
3010 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
3011 'font-lock-preprocessor-face
3012 'font-lock-type-face))
3013 ;; Fontify delays/numbers
3014 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
3015 0 font-lock-type-face append)
3016 ;; Fontify instantiation names
3017 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
3020 (setq verilog-font-lock-keywords-3
3021 (append verilog-font-lock-keywords-2
3022 (when verilog-highlight-translate-off
3023 (list
3024 ;; Fontify things in translate off regions
3025 '(verilog-match-translate-off
3026 (0 'verilog-font-lock-translate-off-face prepend))
3027 )))))
3030 ;; Buffer state preservation
3032 (defmacro verilog-save-buffer-state (&rest body)
3033 "Execute BODY forms, saving state around insignificant change.
3034 Changes in text properties like `face' or `syntax-table' are
3035 considered insignificant. This macro allows text properties to
3036 be changed, even in a read-only buffer.
3038 A change is considered significant if it affects the buffer text
3039 in any way that isn't completely restored again. Any
3040 user-visible changes to the buffer must not be within a
3041 `verilog-save-buffer-state'."
3042 ;; From c-save-buffer-state
3043 `(let* ((modified (buffer-modified-p))
3044 (buffer-undo-list t)
3045 (inhibit-read-only t)
3046 (inhibit-point-motion-hooks t)
3047 (verilog-no-change-functions t)
3048 before-change-functions
3049 after-change-functions
3050 deactivate-mark
3051 buffer-file-name ; Prevent primitives checking
3052 buffer-file-truename) ; for file modification
3053 (unwind-protect
3054 (progn ,@body)
3055 (and (not modified)
3056 (buffer-modified-p)
3057 (set-buffer-modified-p nil)))))
3059 (defmacro verilog-save-no-change-functions (&rest body)
3060 "Execute BODY forms, disabling all change hooks in BODY.
3061 For insignificant changes, see instead `verilog-save-buffer-state'."
3062 `(let* ((inhibit-point-motion-hooks t)
3063 (verilog-no-change-functions t)
3064 before-change-functions
3065 after-change-functions)
3066 (progn ,@body)))
3068 (defvar verilog-save-font-mod-hooked nil
3069 "Local variable when inside a `verilog-save-font-mods' block.")
3070 (make-variable-buffer-local 'verilog-save-font-mod-hooked)
3072 (defmacro verilog-save-font-mods (&rest body)
3073 "Execute BODY forms, disabling text modifications to allow performing BODY.
3074 Includes temporary disabling of `font-lock' to restore the buffer
3075 to full text form for parsing. Additional actions may be specified with
3076 `verilog-before-save-font-hook' and `verilog-after-save-font-hook'."
3077 ;; Before version 20, match-string with font-lock returns a
3078 ;; vector that is not equal to the string. IE if on "input"
3079 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
3080 `(let* ((hooked (unless verilog-save-font-mod-hooked
3081 (verilog-run-hooks 'verilog-before-save-font-hook)
3083 (verilog-save-font-mod-hooked t)
3084 (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
3085 (font-lock-mode 0)
3086 t)))
3087 (unwind-protect
3088 (progn ,@body)
3089 ;; Unwind forms
3090 (when fontlocked (font-lock-mode t))
3091 (when hooked (verilog-run-hooks 'verilog-after-save-font-hook)))))
3094 ;; Comment detection and caching
3096 (defvar verilog-scan-cache-preserving nil
3097 "If set, the specified buffer's comment properties are static.
3098 Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
3099 and `verilog-scan'.")
3101 (defvar verilog-scan-cache-tick nil
3102 "Modification tick at which `verilog-scan' was last completed.")
3103 (make-variable-buffer-local 'verilog-scan-cache-tick)
3105 (defun verilog-scan-cache-flush ()
3106 "Flush the `verilog-scan' cache."
3107 (setq verilog-scan-cache-tick nil))
3109 (defun verilog-scan-cache-ok-p ()
3110 "Return t iff the scan cache is up to date."
3111 (or (and verilog-scan-cache-preserving
3112 (eq verilog-scan-cache-preserving (current-buffer))
3113 verilog-scan-cache-tick)
3114 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3116 (defmacro verilog-save-scan-cache (&rest body)
3117 "Execute the BODY forms, allowing scan cache preservation within BODY.
3118 This requires that insertions must use `verilog-insert'."
3119 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3120 ;; Note this must work properly if there's multiple layers of calls
3121 ;; to verilog-save-scan-cache even with differing ticks.
3122 `(progn
3123 (unless (verilog-scan-cache-ok-p) ;; Must be before let
3124 (setq verilog-scan-cache-tick nil))
3125 (let* ((verilog-scan-cache-preserving (current-buffer)))
3126 (progn ,@body))))
3128 (defun verilog-scan-region (beg end)
3129 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3130 This creates v-cmts properties where comments are in force."
3131 ;; Why properties and not overlays? Overlays have much slower non O(1)
3132 ;; lookup times.
3133 ;; This function is warm - called on every verilog-insert
3134 (save-excursion
3135 (save-match-data
3136 (verilog-save-buffer-state
3137 (let (pt)
3138 (goto-char beg)
3139 (while (< (point) end)
3140 (cond ((looking-at "//")
3141 (setq pt (point))
3142 (or (search-forward "\n" end t)
3143 (goto-char end))
3144 ;; "1+": The leading // or /* itself isn't considered as
3145 ;; being "inside" the comment, so that a (search-backward)
3146 ;; that lands at the start of the // won't mis-indicate
3147 ;; it's inside a comment. Also otherwise it would be
3148 ;; hard to find a commented out /*AS*/ vs one that isn't
3149 (put-text-property (1+ pt) (point) 'v-cmts t))
3150 ((looking-at "/\\*")
3151 (setq pt (point))
3152 (or (search-forward "*/" end t)
3153 ;; No error - let later code indicate it so we can
3154 ;; use inside functions on-the-fly
3155 ;;(error "%s: Unmatched /* */, at char %d"
3156 ;; (verilog-point-text) (point))
3157 (goto-char end))
3158 (put-text-property (1+ pt) (point) 'v-cmts t))
3159 ((looking-at "\"")
3160 (setq pt (point))
3161 (or (re-search-forward "[^\\]\"" end t) ;; don't forward-char first, since we look for a non backslash first
3162 ;; No error - let later code indicate it so we can
3163 (goto-char end))
3164 (put-text-property (1+ pt) (point) 'v-cmts t))
3166 (forward-char 1)
3167 (if (re-search-forward "[/\"]" end t)
3168 (backward-char 1)
3169 (goto-char end))))))))))
3171 (defun verilog-scan ()
3172 "Parse the buffer, marking all comments with properties.
3173 Also assumes any text inserted since `verilog-scan-cache-tick'
3174 either is ok to parse as a non-comment, or `verilog-insert' was used."
3175 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
3176 (unless (verilog-scan-cache-ok-p)
3177 (save-excursion
3178 (verilog-save-buffer-state
3179 (when verilog-debug
3180 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3181 verilog-scan-cache-preserving verilog-scan-cache-tick
3182 (buffer-chars-modified-tick)))
3183 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
3184 (verilog-scan-region (point-min) (point-max))
3185 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
3186 (when verilog-debug (message "Scanning... done"))))))
3188 (defun verilog-scan-debug ()
3189 "For debugging, show with display face results of `verilog-scan'."
3190 (font-lock-mode 0)
3191 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3192 (save-excursion
3193 (goto-char (point-min))
3194 (remove-text-properties (point-min) (point-max) '(face nil))
3195 (while (not (eobp))
3196 (cond ((get-text-property (point) 'v-cmts)
3197 (put-text-property (point) (1+ (point)) `face 'underline)
3198 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3199 (forward-char 1))
3201 (goto-char (or (next-property-change (point)) (point-max))))))))
3203 (defun verilog-scan-and-debug ()
3204 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3205 (let (verilog-scan-cache-preserving
3206 verilog-scan-cache-tick)
3207 (goto-char (point-min))
3208 (verilog-scan)
3209 (verilog-scan-debug)))
3211 (defun verilog-inside-comment-or-string-p (&optional pos)
3212 "Check if optional point POS is inside a comment.
3213 This may require a slow pre-parse of the buffer with `verilog-scan'
3214 to establish comment properties on all text."
3215 ;; This function is very hot
3216 (verilog-scan)
3217 (if pos
3218 (and (>= pos (point-min))
3219 (get-text-property pos 'v-cmts))
3220 (get-text-property (point) 'v-cmts)))
3222 (defun verilog-insert (&rest stuff)
3223 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
3224 Any insert that includes a comment must have the entire comment
3225 inserted using a single call to `verilog-insert'."
3226 (let ((pt (point)))
3227 (while stuff
3228 (insert (car stuff))
3229 (setq stuff (cdr stuff)))
3230 (verilog-scan-region pt (point))))
3232 ;; More searching
3234 (defun verilog-declaration-end ()
3235 (search-forward ";"))
3237 (defun verilog-point-text (&optional pointnum)
3238 "Return text describing where POINTNUM or current point is (for errors).
3239 Use filename, if current buffer being edited shorten to just buffer name."
3240 (concat (or (and (equal (window-buffer (selected-window)) (current-buffer))
3241 (buffer-name))
3242 buffer-file-name
3243 (buffer-name))
3244 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
3246 (defun electric-verilog-backward-sexp ()
3247 "Move backward over one balanced expression."
3248 (interactive)
3249 ;; before that see if we are in a comment
3250 (verilog-backward-sexp))
3252 (defun electric-verilog-forward-sexp ()
3253 "Move forward over one balanced expression."
3254 (interactive)
3255 ;; before that see if we are in a comment
3256 (verilog-forward-sexp))
3258 ;;;used by hs-minor-mode
3259 (defun verilog-forward-sexp-function (arg)
3260 (if (< arg 0)
3261 (verilog-backward-sexp)
3262 (verilog-forward-sexp)))
3265 (defun verilog-backward-sexp ()
3266 (let ((reg)
3267 (elsec 1)
3268 (found nil)
3269 (st (point)))
3270 (if (not (looking-at "\\<"))
3271 (forward-word -1))
3272 (cond
3273 ((verilog-skip-backward-comment-or-string))
3274 ((looking-at "\\<else\\>")
3275 (setq reg (concat
3276 verilog-end-block-re
3277 "\\|\\(\\<else\\>\\)"
3278 "\\|\\(\\<if\\>\\)"))
3279 (while (and (not found)
3280 (verilog-re-search-backward reg nil 'move))
3281 (cond
3282 ((match-end 1) ; matched verilog-end-block-re
3283 ; try to leap back to matching outward block by striding across
3284 ; indent level changing tokens then immediately
3285 ; previous line governs indentation.
3286 (verilog-leap-to-head))
3287 ((match-end 2) ; else, we're in deep
3288 (setq elsec (1+ elsec)))
3289 ((match-end 3) ; found it
3290 (setq elsec (1- elsec))
3291 (if (= 0 elsec)
3292 ;; Now previous line describes syntax
3293 (setq found 't))))))
3294 ((looking-at verilog-end-block-re)
3295 (verilog-leap-to-head))
3296 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3297 (cond
3298 ((match-end 1)
3299 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3300 ((match-end 2)
3301 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3302 ((match-end 3)
3303 (verilog-re-search-backward "\\<class\\>" nil 'move))
3304 ((match-end 4)
3305 (verilog-re-search-backward "\\<program\\>" nil 'move))
3306 ((match-end 5)
3307 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3308 ((match-end 6)
3309 (verilog-re-search-backward "\\<package\\>" nil 'move))
3311 (goto-char st)
3312 (backward-sexp 1))))
3314 (goto-char st)
3315 (backward-sexp)))))
3317 (defun verilog-forward-sexp ()
3318 (let ((reg)
3319 (md 2)
3320 (st (point))
3321 (nest 'yes))
3322 (if (not (looking-at "\\<"))
3323 (forward-word -1))
3324 (cond
3325 ((verilog-skip-forward-comment-or-string)
3326 (verilog-forward-syntactic-ws))
3327 ((looking-at verilog-beg-block-re-ordered)
3328 (cond
3329 ((match-end 1);
3330 ;; Search forward for matching end
3331 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
3332 ((match-end 2)
3333 ;; Search forward for matching endcase
3334 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3335 (setq md 3) ;; ender is third item in regexp
3337 ((match-end 4)
3338 ;; might be "disable fork" or "wait fork"
3339 (let
3340 (here)
3341 (if (or
3342 (looking-at verilog-disable-fork-re)
3343 (and (looking-at "fork")
3344 (progn
3345 (setq here (point)) ;; sometimes a fork is just a fork
3346 (forward-word -1)
3347 (looking-at verilog-disable-fork-re))))
3348 (progn ;; it is a disable fork; ignore it
3349 (goto-char (match-end 0))
3350 (forward-word 1)
3351 (setq reg nil))
3352 (progn ;; it is a nice simple fork
3353 (goto-char here) ;; return from looking for "disable fork"
3354 ;; Search forward for matching join
3355 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
3356 ((match-end 6)
3357 ;; Search forward for matching endclass
3358 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
3360 ((match-end 7)
3361 ;; Search forward for matching endtable
3362 (setq reg "\\<endtable\\>" )
3363 (setq nest 'no))
3364 ((match-end 8)
3365 ;; Search forward for matching endspecify
3366 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3367 ((match-end 9)
3368 ;; Search forward for matching endfunction
3369 (setq reg "\\<endfunction\\>" )
3370 (setq nest 'no))
3371 ((match-end 10)
3372 ;; Search forward for matching endfunction
3373 (setq reg "\\<endfunction\\>" )
3374 (setq nest 'no))
3375 ((match-end 14)
3376 ;; Search forward for matching endtask
3377 (setq reg "\\<endtask\\>" )
3378 (setq nest 'no))
3379 ((match-end 15)
3380 ;; Search forward for matching endtask
3381 (setq reg "\\<endtask\\>" )
3382 (setq nest 'no))
3383 ((match-end 19)
3384 ;; Search forward for matching endgenerate
3385 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
3386 ((match-end 20)
3387 ;; Search forward for matching endgroup
3388 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
3389 ((match-end 21)
3390 ;; Search forward for matching endproperty
3391 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
3392 ((match-end 25)
3393 ;; Search forward for matching endsequence
3394 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3395 (setq md 3)) ; 3 to get to endsequence in the reg above
3396 ((match-end 27)
3397 ;; Search forward for matching endclocking
3398 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
3399 (if (and reg
3400 (forward-word 1))
3401 (catch 'skip
3402 (if (eq nest 'yes)
3403 (let ((depth 1)
3404 here)
3405 (while (verilog-re-search-forward reg nil 'move)
3406 (cond
3407 ((match-end md) ; a closer in regular expression, so we are climbing out
3408 (setq depth (1- depth))
3409 (if (= 0 depth) ; we are out!
3410 (throw 'skip 1)))
3411 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3412 (setq here (point)) ; remember where we started
3413 (goto-char (match-beginning 1))
3414 (cond
3415 ((if (or
3416 (looking-at verilog-disable-fork-re)
3417 (and (looking-at "fork")
3418 (progn
3419 (forward-word -1)
3420 (looking-at verilog-disable-fork-re))))
3421 (progn ;; it is a disable fork; another false alarm
3422 (goto-char (match-end 0)))
3423 (progn ;; it is a simple fork (or has nothing to do with fork)
3424 (goto-char here)
3425 (setq depth (1+ depth))))))))))
3426 (if (verilog-re-search-forward reg nil 'move)
3427 (throw 'skip 1))))))
3429 ((looking-at (concat
3430 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3431 "\\(\\<primitive\\>\\)\\|"
3432 "\\(\\<class\\>\\)\\|"
3433 "\\(\\<program\\>\\)\\|"
3434 "\\(\\<interface\\>\\)\\|"
3435 "\\(\\<package\\>\\)"))
3436 (cond
3437 ((match-end 1)
3438 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3439 ((match-end 2)
3440 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3441 ((match-end 3)
3442 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3443 ((match-end 4)
3444 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3445 ((match-end 5)
3446 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3447 ((match-end 6)
3448 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3450 (goto-char st)
3451 (if (= (following-char) ?\) )
3452 (forward-char 1)
3453 (forward-sexp 1)))))
3455 (goto-char st)
3456 (if (= (following-char) ?\) )
3457 (forward-char 1)
3458 (forward-sexp 1))))))
3460 (defun verilog-declaration-beg ()
3461 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3465 ;; Mode
3467 (defvar verilog-which-tool 1)
3468 ;;;###autoload
3469 (define-derived-mode verilog-mode prog-mode "Verilog"
3470 "Major mode for editing Verilog code.
3471 \\<verilog-mode-map>
3472 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3473 AUTOs can improve coding efficiency.
3475 Use \\[verilog-faq] for a pointer to frequently asked questions.
3477 NEWLINE, TAB indents for Verilog code.
3478 Delete converts tabs to spaces as it moves back.
3480 Supports highlighting.
3482 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3483 with no args, if that value is non-nil.
3485 Variables controlling indentation/edit style:
3487 variable `verilog-indent-level' (default 3)
3488 Indentation of Verilog statements with respect to containing block.
3489 `verilog-indent-level-module' (default 3)
3490 Absolute indentation of Module level Verilog statements.
3491 Set to 0 to get initial and always statements lined up
3492 on the left side of your screen.
3493 `verilog-indent-level-declaration' (default 3)
3494 Indentation of declarations with respect to containing block.
3495 Set to 0 to get them list right under containing block.
3496 `verilog-indent-level-behavioral' (default 3)
3497 Indentation of first begin in a task or function block
3498 Set to 0 to get such code to lined up underneath the task or
3499 function keyword.
3500 `verilog-indent-level-directive' (default 1)
3501 Indentation of `ifdef/`endif blocks.
3502 `verilog-cexp-indent' (default 1)
3503 Indentation of Verilog statements broken across lines i.e.:
3504 if (a)
3505 begin
3506 `verilog-case-indent' (default 2)
3507 Indentation for case statements.
3508 `verilog-auto-newline' (default nil)
3509 Non-nil means automatically newline after semicolons and the punctuation
3510 mark after an end.
3511 `verilog-auto-indent-on-newline' (default t)
3512 Non-nil means automatically indent line after newline.
3513 `verilog-tab-always-indent' (default t)
3514 Non-nil means TAB in Verilog mode should always reindent the current line,
3515 regardless of where in the line point is when the TAB command is used.
3516 `verilog-indent-begin-after-if' (default t)
3517 Non-nil means to indent begin statements following a preceding
3518 if, else, while, for and repeat statements, if any. Otherwise,
3519 the begin is lined up with the preceding token. If t, you get:
3520 if (a)
3521 begin // amount of indent based on `verilog-cexp-indent'
3522 otherwise you get:
3523 if (a)
3524 begin
3525 `verilog-auto-endcomments' (default t)
3526 Non-nil means a comment /* ... */ is set after the ends which ends
3527 cases, tasks, functions and modules.
3528 The type and name of the object will be set between the braces.
3529 `verilog-minimum-comment-distance' (default 10)
3530 Minimum distance (in lines) between begin and end required before a comment
3531 will be inserted. Setting this variable to zero results in every
3532 end acquiring a comment; the default avoids too many redundant
3533 comments in tight quarters.
3534 `verilog-auto-lineup' (default 'declarations)
3535 List of contexts where auto lineup of code should be done.
3537 Variables controlling other actions:
3539 `verilog-linter' (default surelint)
3540 Unix program to call to run the lint checker. This is the default
3541 command for \\[compile-command] and \\[verilog-auto-save-compile].
3543 See \\[customize] for the complete list of variables.
3545 AUTO expansion functions are, in part:
3547 \\[verilog-auto] Expand AUTO statements.
3548 \\[verilog-delete-auto] Remove the AUTOs.
3549 \\[verilog-inject-auto] Insert AUTOs for the first time.
3551 Some other functions are:
3553 \\[verilog-complete-word] Complete word with appropriate possibilities.
3554 \\[verilog-mark-defun] Mark function.
3555 \\[verilog-beg-of-defun] Move to beginning of current function.
3556 \\[verilog-end-of-defun] Move to end of current function.
3557 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3559 \\[verilog-comment-region] Put marked area in a comment.
3560 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
3561 \\[verilog-insert-block] Insert begin ... end.
3562 \\[verilog-star-comment] Insert /* ... */.
3564 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
3565 \\[verilog-sk-begin] Insert a begin .. end block.
3566 \\[verilog-sk-case] Insert a case block, prompting for details.
3567 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3568 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
3569 \\[verilog-sk-header] Insert a header block at the top of file.
3570 \\[verilog-sk-initial] Insert an initial begin .. end block.
3571 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3572 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
3573 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3574 \\[verilog-sk-uvm-class] Insert an UVM Class block.
3575 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3576 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3577 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3578 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3579 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3580 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3581 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3582 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3583 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3584 \\[verilog-sk-comment] Insert a comment block.
3585 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3586 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3587 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3588 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3589 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3590 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3591 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3592 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3593 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3595 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3596 Key bindings specific to `verilog-mode-map' are:
3598 \\{verilog-mode-map}"
3599 :abbrev-table verilog-mode-abbrev-table
3600 (set (make-local-variable 'beginning-of-defun-function)
3601 'verilog-beg-of-defun)
3602 (set (make-local-variable 'end-of-defun-function)
3603 'verilog-end-of-defun)
3604 (set-syntax-table verilog-mode-syntax-table)
3605 (set (make-local-variable 'indent-line-function)
3606 #'verilog-indent-line-relative)
3607 (setq comment-indent-function 'verilog-comment-indent)
3608 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
3609 (set (make-local-variable 'comment-start) "// ")
3610 (set (make-local-variable 'comment-end) "")
3611 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3612 (set (make-local-variable 'comment-multi-line) nil)
3613 ;; Set up for compilation
3614 (setq verilog-which-tool 1)
3615 (setq verilog-tool 'verilog-linter)
3616 (verilog-set-compile-command)
3617 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
3618 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3620 ;; Setting up menus
3621 (when (featurep 'xemacs)
3622 (easy-menu-add verilog-stmt-menu)
3623 (easy-menu-add verilog-menu)
3624 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
3626 ;; Stuff for GNU Emacs
3627 (set (make-local-variable 'font-lock-defaults)
3628 `((verilog-font-lock-keywords
3629 verilog-font-lock-keywords-1
3630 verilog-font-lock-keywords-2
3631 verilog-font-lock-keywords-3)
3632 nil nil nil
3633 ,(if (functionp 'syntax-ppss)
3634 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3635 ;; font-lock-beginning-of-syntax-function, so
3636 ;; font-lock-beginning-of-syntax-function, can't use
3637 ;; verilog-beg-of-defun.
3639 'verilog-beg-of-defun)))
3640 ;;------------------------------------------------------------
3641 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
3642 ;; all buffer local:
3643 (unless noninteractive ;; Else can't see the result, and change hooks are slow
3644 (when (featurep 'xemacs)
3645 (make-local-hook 'font-lock-mode-hook)
3646 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3647 (make-local-hook 'after-change-functions))
3648 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3649 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3650 (add-hook 'after-change-functions 'verilog-highlight-region t t))
3652 ;; Tell imenu how to handle Verilog.
3653 (set (make-local-variable 'imenu-generic-expression)
3654 verilog-imenu-generic-expression)
3655 ;; Tell which-func-modes that imenu knows about verilog
3656 (when (and (boundp 'which-func-modes) (listp which-func-modes))
3657 (add-to-list 'which-func-modes 'verilog-mode))
3658 ;; hideshow support
3659 (when (boundp 'hs-special-modes-alist)
3660 (unless (assq 'verilog-mode hs-special-modes-alist)
3661 (setq hs-special-modes-alist
3662 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3663 verilog-forward-sexp-function)
3664 hs-special-modes-alist))))
3666 ;; Stuff for autos
3667 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3668 ;; verilog-mode-hook call added by define-derived-mode
3673 ;; Electric functions
3675 (defun electric-verilog-terminate-line (&optional arg)
3676 "Terminate line and indent next line.
3677 With optional ARG, remove existing end of line comments."
3678 (interactive)
3679 ;; before that see if we are in a comment
3680 (let ((state (save-excursion (verilog-syntax-ppss))))
3681 (cond
3682 ((nth 7 state) ; Inside // comment
3683 (if (eolp)
3684 (progn
3685 (delete-horizontal-space)
3686 (newline))
3687 (progn
3688 (newline)
3689 (insert "// ")
3690 (beginning-of-line)))
3691 (verilog-indent-line))
3692 ((nth 4 state) ; Inside any comment (hence /**/)
3693 (newline)
3694 (verilog-more-comment))
3695 ((eolp)
3696 ;; First, check if current line should be indented
3697 (if (save-excursion
3698 (delete-horizontal-space)
3699 (beginning-of-line)
3700 (skip-chars-forward " \t")
3701 (if (looking-at verilog-auto-end-comment-lines-re)
3702 (let ((indent-str (verilog-indent-line)))
3703 ;; Maybe we should set some endcomments
3704 (if verilog-auto-endcomments
3705 (verilog-set-auto-endcomments indent-str arg))
3706 (end-of-line)
3707 (delete-horizontal-space)
3708 (if arg
3710 (newline))
3711 nil)
3712 (progn
3713 (end-of-line)
3714 (delete-horizontal-space)
3715 't)))
3716 ;; see if we should line up assignments
3717 (progn
3718 (if (or (eq 'all verilog-auto-lineup)
3719 (eq 'assignments verilog-auto-lineup))
3720 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
3721 (newline))
3722 (forward-line 1))
3723 ;; Indent next line
3724 (if verilog-auto-indent-on-newline
3725 (verilog-indent-line)))
3727 (newline)))))
3729 (defun electric-verilog-terminate-and-indent ()
3730 "Insert a newline and indent for the next statement."
3731 (interactive)
3732 (electric-verilog-terminate-line 1))
3734 (defun electric-verilog-semi ()
3735 "Insert `;' character and reindent the line."
3736 (interactive)
3737 (verilog-insert-last-command-event)
3739 (if (or (verilog-in-comment-or-string-p)
3740 (verilog-in-escaped-name-p))
3742 (save-excursion
3743 (beginning-of-line)
3744 (verilog-forward-ws&directives)
3745 (verilog-indent-line))
3746 (if (and verilog-auto-newline
3747 (not (verilog-parenthesis-depth)))
3748 (electric-verilog-terminate-line))))
3750 (defun electric-verilog-semi-with-comment ()
3751 "Insert `;' character, reindent the line and indent for comment."
3752 (interactive)
3753 (insert "\;")
3754 (save-excursion
3755 (beginning-of-line)
3756 (verilog-indent-line))
3757 (indent-for-comment))
3759 (defun electric-verilog-colon ()
3760 "Insert `:' and do all indentations except line indent on this line."
3761 (interactive)
3762 (verilog-insert-last-command-event)
3763 ;; Do nothing if within string.
3764 (if (or
3765 (verilog-within-string)
3766 (not (verilog-in-case-region-p)))
3768 (save-excursion
3769 (let ((p (point))
3770 (lim (progn (verilog-beg-of-statement) (point))))
3771 (goto-char p)
3772 (verilog-backward-case-item lim)
3773 (verilog-indent-line)))
3774 ;; (let ((verilog-tab-always-indent nil))
3775 ;; (verilog-indent-line))
3778 ;;(defun electric-verilog-equal ()
3779 ;; "Insert `=', and do indentation if within block."
3780 ;; (interactive)
3781 ;; (verilog-insert-last-command-event)
3782 ;; Could auto line up expressions, but not yet
3783 ;; (if (eq (car (verilog-calculate-indent)) 'block)
3784 ;; (let ((verilog-tab-always-indent nil))
3785 ;; (verilog-indent-command)))
3786 ;; )
3788 (defun electric-verilog-tick ()
3789 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3790 (interactive)
3791 (verilog-insert-last-command-event)
3792 (save-excursion
3793 (if (verilog-in-directive-p)
3794 (verilog-indent-line))))
3796 (defun electric-verilog-tab ()
3797 "Function called when TAB is pressed in Verilog mode."
3798 (interactive)
3799 ;; If verilog-tab-always-indent, indent the beginning of the line.
3800 (cond
3801 ;; The region is active, indent it.
3802 ((and (region-active-p)
3803 (not (eq (region-beginning) (region-end))))
3804 (indent-region (region-beginning) (region-end) nil))
3805 ((or verilog-tab-always-indent
3806 (save-excursion
3807 (skip-chars-backward " \t")
3808 (bolp)))
3809 (let* ((oldpnt (point))
3810 (boi-point
3811 (save-excursion
3812 (beginning-of-line)
3813 (skip-chars-forward " \t")
3814 (verilog-indent-line)
3815 (back-to-indentation)
3816 (point))))
3817 (if (< (point) boi-point)
3818 (back-to-indentation)
3819 (cond ((not verilog-tab-to-comment))
3820 ((not (eolp))
3821 (end-of-line))
3823 (indent-for-comment)
3824 (when (and (eolp) (= oldpnt (point)))
3825 ; kill existing comment
3826 (beginning-of-line)
3827 (re-search-forward comment-start-skip oldpnt 'move)
3828 (goto-char (match-beginning 0))
3829 (skip-chars-backward " \t")
3830 (kill-region (point) oldpnt)))))))
3831 (t (progn (insert "\t")))))
3836 ;; Interactive functions
3839 (defun verilog-indent-buffer ()
3840 "Indent-region the entire buffer as Verilog code.
3841 To call this from the command line, see \\[verilog-batch-indent]."
3842 (interactive)
3843 (verilog-mode)
3844 (indent-region (point-min) (point-max) nil))
3846 (defun verilog-insert-block ()
3847 "Insert Verilog begin ... end; block in the code with right indentation."
3848 (interactive)
3849 (verilog-indent-line)
3850 (insert "begin")
3851 (electric-verilog-terminate-line)
3852 (save-excursion
3853 (electric-verilog-terminate-line)
3854 (insert "end")
3855 (beginning-of-line)
3856 (verilog-indent-line)))
3858 (defun verilog-star-comment ()
3859 "Insert Verilog star comment at point."
3860 (interactive)
3861 (verilog-indent-line)
3862 (insert "/*")
3863 (save-excursion
3864 (newline)
3865 (insert " */"))
3866 (newline)
3867 (insert " * "))
3869 (defun verilog-insert-1 (fmt max)
3870 "Use format string FMT to insert integers 0 to MAX - 1.
3871 Inserts one integer per line, at the current column. Stops early
3872 if it reaches the end of the buffer."
3873 (let ((col (current-column))
3874 (n 0))
3875 (save-excursion
3876 (while (< n max)
3877 (insert (format fmt n))
3878 (forward-line 1)
3879 ;; Note that this function does not bother to check for lines
3880 ;; shorter than col.
3881 (if (eobp)
3882 (setq n max)
3883 (setq n (1+ n))
3884 (move-to-column col))))))
3886 (defun verilog-insert-indices (max)
3887 "Insert a set of indices into a rectangle.
3888 The upper left corner is defined by point. Indices begin with 0
3889 and extend to the MAX - 1. If no prefix arg is given, the user
3890 is prompted for a value. The indices are surrounded by square
3891 brackets \[]. For example, the following code with the point
3892 located after the first 'a' gives:
3894 a = b a[ 0] = b
3895 a = b a[ 1] = b
3896 a = b a[ 2] = b
3897 a = b a[ 3] = b
3898 a = b ==> insert-indices ==> a[ 4] = b
3899 a = b a[ 5] = b
3900 a = b a[ 6] = b
3901 a = b a[ 7] = b
3902 a = b a[ 8] = b"
3904 (interactive "NMAX: ")
3905 (verilog-insert-1 "[%3d]" max))
3907 (defun verilog-generate-numbers (max)
3908 "Insert a set of generated numbers into a rectangle.
3909 The upper left corner is defined by point. The numbers are padded to three
3910 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
3911 is supplied, then the user is prompted for the MAX number. Consider the
3912 following code fragment:
3914 buf buf buf buf000
3915 buf buf buf buf001
3916 buf buf buf buf002
3917 buf buf buf buf003
3918 buf buf ==> generate-numbers ==> buf buf004
3919 buf buf buf buf005
3920 buf buf buf buf006
3921 buf buf buf buf007
3922 buf buf buf buf008"
3924 (interactive "NMAX: ")
3925 (verilog-insert-1 "%3.3d" max))
3927 (defun verilog-mark-defun ()
3928 "Mark the current Verilog function (or procedure).
3929 This puts the mark at the end, and point at the beginning."
3930 (interactive)
3931 (if (featurep 'xemacs)
3932 (progn
3933 (push-mark (point))
3934 (verilog-end-of-defun)
3935 (push-mark (point))
3936 (verilog-beg-of-defun)
3937 (if (fboundp 'zmacs-activate-region)
3938 (zmacs-activate-region)))
3939 (mark-defun)))
3941 (defun verilog-comment-region (start end)
3942 ; checkdoc-params: (start end)
3943 "Put the region into a Verilog comment.
3944 The comments that are in this area are \"deformed\":
3945 `*)' becomes `!(*' and `}' becomes `!{'.
3946 These deformed comments are returned to normal if you use
3947 \\[verilog-uncomment-region] to undo the commenting.
3949 The commented area starts with `verilog-exclude-str-start', and ends with
3950 `verilog-exclude-str-end'. But if you change these variables,
3951 \\[verilog-uncomment-region] won't recognize the comments."
3952 (interactive "r")
3953 (save-excursion
3954 ;; Insert start and endcomments
3955 (goto-char end)
3956 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
3957 (not (save-excursion (skip-chars-backward " \t") (bolp))))
3958 (forward-line 1)
3959 (beginning-of-line))
3960 (insert verilog-exclude-str-end)
3961 (setq end (point))
3962 (newline)
3963 (goto-char start)
3964 (beginning-of-line)
3965 (insert verilog-exclude-str-start)
3966 (newline)
3967 ;; Replace end-comments within commented area
3968 (goto-char end)
3969 (save-excursion
3970 (while (re-search-backward "\\*/" start t)
3971 (replace-match "*-/" t t)))
3972 (save-excursion
3973 (let ((s+1 (1+ start)))
3974 (while (re-search-backward "/\\*" s+1 t)
3975 (replace-match "/-*" t t))))))
3977 (defun verilog-uncomment-region ()
3978 "Uncomment a commented area; change deformed comments back to normal.
3979 This command does nothing if the pointer is not in a commented
3980 area. See also `verilog-comment-region'."
3981 (interactive)
3982 (save-excursion
3983 (let ((start (point))
3984 (end (point)))
3985 ;; Find the boundaries of the comment
3986 (save-excursion
3987 (setq start (progn (search-backward verilog-exclude-str-start nil t)
3988 (point)))
3989 (setq end (progn (search-forward verilog-exclude-str-end nil t)
3990 (point))))
3991 ;; Check if we're really inside a comment
3992 (if (or (equal start (point)) (<= end (point)))
3993 (message "Not standing within commented area.")
3994 (progn
3995 ;; Remove endcomment
3996 (goto-char end)
3997 (beginning-of-line)
3998 (let ((pos (point)))
3999 (end-of-line)
4000 (delete-region pos (1+ (point))))
4001 ;; Change comments back to normal
4002 (save-excursion
4003 (while (re-search-backward "\\*-/" start t)
4004 (replace-match "*/" t t)))
4005 (save-excursion
4006 (while (re-search-backward "/-\\*" start t)
4007 (replace-match "/*" t t)))
4008 ;; Remove start comment
4009 (goto-char start)
4010 (beginning-of-line)
4011 (let ((pos (point)))
4012 (end-of-line)
4013 (delete-region pos (1+ (point)))))))))
4015 (defun verilog-beg-of-defun ()
4016 "Move backward to the beginning of the current function or procedure."
4017 (interactive)
4018 (verilog-re-search-backward verilog-defun-re nil 'move))
4020 (defun verilog-beg-of-defun-quick ()
4021 "Move backward to the beginning of the current function or procedure.
4022 Uses `verilog-scan' cache."
4023 (interactive)
4024 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
4026 (defun verilog-end-of-defun ()
4027 "Move forward to the end of the current function or procedure."
4028 (interactive)
4029 (verilog-re-search-forward verilog-end-defun-re nil 'move))
4031 (defun verilog-get-beg-of-defun (&optional warn)
4032 (save-excursion
4033 (cond ((verilog-re-search-forward-quick verilog-defun-re nil t)
4034 (point))
4036 (error "%s: Can't find module beginning" (verilog-point-text))
4037 (point-max)))))
4038 (defun verilog-get-end-of-defun (&optional warn)
4039 (save-excursion
4040 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
4041 (point))
4043 (error "%s: Can't find endmodule" (verilog-point-text))
4044 (point-max)))))
4046 (defun verilog-label-be (&optional arg)
4047 "Label matching begin ... end, fork ... join and case ... endcase statements.
4048 With ARG, first kill any existing labels."
4049 (interactive)
4050 (let ((cnt 0)
4051 (oldpos (point))
4052 (b (progn
4053 (verilog-beg-of-defun)
4054 (point-marker)))
4055 (e (progn
4056 (verilog-end-of-defun)
4057 (point-marker))))
4058 (goto-char (marker-position b))
4059 (if (> (- e b) 200)
4060 (message "Relabeling module..."))
4061 (while (and
4062 (> (marker-position e) (point))
4063 (verilog-re-search-forward
4064 (concat
4065 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
4066 "\\|\\(`endif\\)\\|\\(`else\\)")
4067 nil 'move))
4068 (goto-char (match-beginning 0))
4069 (let ((indent-str (verilog-indent-line)))
4070 (verilog-set-auto-endcomments indent-str 't)
4071 (end-of-line)
4072 (delete-horizontal-space))
4073 (setq cnt (1+ cnt))
4074 (if (= 9 (% cnt 10))
4075 (message "%d..." cnt)))
4076 (goto-char oldpos)
4077 (if (or
4078 (> (- e b) 200)
4079 (> cnt 20))
4080 (message "%d lines auto commented" cnt))))
4082 (defun verilog-beg-of-statement ()
4083 "Move backward to beginning of statement."
4084 (interactive)
4085 ;; Move back token by token until we see the end
4086 ;; of some earlier line.
4087 (let (h)
4088 (while
4089 ;; If the current point does not begin a new
4090 ;; statement, as in the character ahead of us is a ';', or SOF
4091 ;; or the string after us unambiguously starts a statement,
4092 ;; or the token before us unambiguously ends a statement,
4093 ;; then move back a token and test again.
4094 (not (or
4095 ;; stop if beginning of buffer
4096 (bolp)
4097 ;; stop if we find a ;
4098 (= (preceding-char) ?\;)
4099 ;; stop if we see a named coverpoint
4100 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4101 ;; keep going if we are in the middle of a word
4102 (not (or (looking-at "\\<") (forward-word -1)))
4103 ;; stop if we see an assertion (perhaps labeled)
4104 (and
4105 (looking-at "\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4106 (progn
4107 (setq h (point))
4108 (save-excursion
4109 (verilog-backward-token)
4110 (if (looking-at verilog-label-re)
4111 (setq h (point))))
4112 (goto-char h)))
4113 ;; stop if we see an extended complete reg, perhaps a complete one
4114 (and
4115 (looking-at verilog-complete-reg)
4116 (let* ((p (point)))
4117 (while (and (looking-at verilog-extended-complete-re)
4118 (progn (setq p (point))
4119 (verilog-backward-token)
4120 (/= p (point)))))
4121 (goto-char p)))
4122 ;; stop if we see a complete reg (previous found extended ones)
4123 (looking-at verilog-basic-complete-re)
4124 ;; stop if previous token is an ender
4125 (save-excursion
4126 (verilog-backward-token)
4128 (looking-at verilog-end-block-re)
4129 (looking-at verilog-preprocessor-re))))) ;; end of test
4130 (verilog-backward-syntactic-ws)
4131 (verilog-backward-token))
4132 ;; Now point is where the previous line ended.
4133 (verilog-forward-syntactic-ws)))
4135 (defun verilog-beg-of-statement-1 ()
4136 "Move backward to beginning of statement."
4137 (interactive)
4138 (if (verilog-in-comment-p)
4139 (verilog-backward-syntactic-ws))
4140 (let ((pt (point)))
4141 (catch 'done
4142 (while (not (looking-at verilog-complete-reg))
4143 (setq pt (point))
4144 (verilog-backward-syntactic-ws)
4145 (if (or (bolp)
4146 (= (preceding-char) ?\;)
4147 (save-excursion
4148 (verilog-backward-token)
4149 (looking-at verilog-ends-re)))
4150 (progn
4151 (goto-char pt)
4152 (throw 'done t))
4153 (verilog-backward-token))))
4154 (verilog-forward-syntactic-ws)))
4156 ; (while (and
4157 ; (not (looking-at verilog-complete-reg))
4158 ; (not (bolp))
4159 ; (not (= (preceding-char) ?\;)))
4160 ; (verilog-backward-token)
4161 ; (verilog-backward-syntactic-ws)
4162 ; (setq pt (point)))
4163 ; (goto-char pt)
4164 ; ;(verilog-forward-syntactic-ws)
4166 (defun verilog-end-of-statement ()
4167 "Move forward to end of current statement."
4168 (interactive)
4169 (let ((nest 0) pos)
4170 (cond
4171 ((verilog-in-directive-p)
4172 (forward-line 1)
4173 (backward-char 1))
4175 ((looking-at verilog-beg-block-re)
4176 (verilog-forward-sexp))
4178 ((equal (char-after) ?\})
4179 (forward-char))
4181 ;; Skip to end of statement
4182 ((condition-case nil
4183 (setq pos
4184 (catch 'found
4185 (while t
4186 (forward-sexp 1)
4187 (verilog-skip-forward-comment-or-string)
4188 (if (eolp)
4189 (forward-line 1))
4190 (cond ((looking-at "[ \t]*;")
4191 (skip-chars-forward "^;")
4192 (forward-char 1)
4193 (throw 'found (point)))
4194 ((save-excursion
4195 (forward-sexp -1)
4196 (looking-at verilog-beg-block-re))
4197 (goto-char (match-beginning 0))
4198 (throw 'found nil))
4199 ((looking-at "[ \t]*)")
4200 (throw 'found (point)))
4201 ((eobp)
4202 (throw 'found (point)))
4206 (error nil))
4207 (if (not pos)
4208 ;; Skip a whole block
4209 (catch 'found
4210 (while t
4211 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4212 (setq nest (if (match-end 1)
4213 (1+ nest)
4214 (1- nest)))
4215 (cond ((eobp)
4216 (throw 'found (point)))
4217 ((= 0 nest)
4218 (throw 'found (verilog-end-of-statement))))))
4219 pos)))))
4221 (defun verilog-in-case-region-p ()
4222 "Return true if in a case region.
4223 More specifically, point @ in the line foo : @ begin"
4224 (interactive)
4225 (save-excursion
4226 (if (and
4227 (progn (verilog-forward-syntactic-ws)
4228 (looking-at "\\<begin\\>"))
4229 (progn (verilog-backward-syntactic-ws)
4230 (= (preceding-char) ?\:)))
4231 (catch 'found
4232 (let ((nest 1))
4233 (while t
4234 (verilog-re-search-backward
4235 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4236 "\\(\\<endcase\\>\\)\\>")
4237 nil 'move)
4238 (cond
4239 ((match-end 3)
4240 (setq nest (1+ nest)))
4241 ((match-end 2)
4242 (if (= nest 1)
4243 (throw 'found 1))
4244 (setq nest (1- nest)))
4246 (throw 'found (= nest 0)))))))
4247 nil)))
4249 (defun verilog-backward-up-list (arg)
4250 "Call `backward-up-list' ARG, ignoring comments."
4251 (let ((parse-sexp-ignore-comments t))
4252 (backward-up-list arg)))
4254 (defun verilog-forward-sexp-cmt (arg)
4255 "Call `forward-sexp' ARG, inside comments."
4256 (let ((parse-sexp-ignore-comments nil))
4257 (forward-sexp arg)))
4259 (defun verilog-forward-sexp-ign-cmt (arg)
4260 "Call `forward-sexp' ARG, ignoring comments."
4261 (let ((parse-sexp-ignore-comments t))
4262 (forward-sexp arg)))
4264 (defun verilog-in-generate-region-p ()
4265 "Return true if in a generate region.
4266 More specifically, after a generate and before an endgenerate."
4267 (interactive)
4268 (let ((nest 1))
4269 (save-excursion
4270 (catch 'done
4271 (while (and
4272 (/= nest 0)
4273 (verilog-re-search-backward
4274 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4275 (cond
4276 ((match-end 1) ; module - we have crawled out
4277 (throw 'done 1))
4278 ((match-end 2) ; generate
4279 (setq nest (1- nest)))
4280 ((match-end 3) ; endgenerate
4281 (setq nest (1+ nest))))))))
4282 (= nest 0) )) ; return nest
4284 (defun verilog-in-fork-region-p ()
4285 "Return true if between a fork and join."
4286 (interactive)
4287 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
4288 (nest 1))
4289 (save-excursion
4290 (while (and
4291 (/= nest 0)
4292 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4293 (cond
4294 ((match-end 1) ; fork
4295 (setq nest (1- nest)))
4296 ((match-end 2) ; join
4297 (setq nest (1+ nest)))))))
4298 (= nest 0) )) ; return nest
4300 (defun verilog-backward-case-item (lim)
4301 "Skip backward to nearest enclosing case item.
4302 Limit search to point LIM."
4303 (interactive)
4304 (let ((str 'nil)
4305 (lim1
4306 (progn
4307 (save-excursion
4308 (verilog-re-search-backward verilog-endcomment-reason-re
4309 lim 'move)
4310 (point)))))
4311 ;; Try to find the real :
4312 (if (save-excursion (search-backward ":" lim1 t))
4313 (let ((colon 0)
4314 b e )
4315 (while
4316 (and
4317 (< colon 1)
4318 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4319 lim1 'move))
4320 (cond
4321 ((match-end 1) ;; [
4322 (setq colon (1+ colon))
4323 (if (>= colon 0)
4324 (error "%s: unbalanced [" (verilog-point-text))))
4325 ((match-end 2) ;; ]
4326 (setq colon (1- colon)))
4328 ((match-end 3) ;; :
4329 (setq colon (1+ colon)))))
4330 ;; Skip back to beginning of case item
4331 (skip-chars-backward "\t ")
4332 (verilog-skip-backward-comment-or-string)
4333 (setq e (point))
4334 (setq b
4335 (progn
4337 (verilog-re-search-backward
4338 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4339 (progn
4340 (cond
4341 ((match-end 1)
4342 (goto-char (match-end 1))
4343 (verilog-forward-ws&directives)
4344 (if (looking-at "(")
4345 (progn
4346 (forward-sexp)
4347 (verilog-forward-ws&directives)))
4348 (point))
4350 (goto-char (match-end 0))
4351 (verilog-forward-ws&directives)
4352 (point))))
4353 (error "Malformed case item"))))
4354 (setq str (buffer-substring b e))
4356 (setq e
4357 (string-match
4358 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4359 (setq str (concat (substring str 0 e) "...")))
4360 str)
4361 'nil)))
4365 ;; Other functions
4368 (defun verilog-kill-existing-comment ()
4369 "Kill auto comment on this line."
4370 (save-excursion
4371 (let* (
4372 (e (progn
4373 (end-of-line)
4374 (point)))
4375 (b (progn
4376 (beginning-of-line)
4377 (search-forward "//" e t))))
4378 (if b
4379 (delete-region (- b 2) e)))))
4381 (defconst verilog-directive-nest-re
4382 (concat "\\(`else\\>\\)\\|"
4383 "\\(`endif\\>\\)\\|"
4384 "\\(`if\\>\\)\\|"
4385 "\\(`ifdef\\>\\)\\|"
4386 "\\(`ifndef\\>\\)\\|"
4387 "\\(`elsif\\>\\)"))
4388 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4389 "Add ending comment with given INDENT-STR.
4390 With KILL-EXISTING-COMMENT, remove what was there before.
4391 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4392 Insert `// case expr ' if this line ends a case block.
4393 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
4394 Insert `// NAME ' if this line ends a function, task, module,
4395 primitive or interface named NAME."
4396 (save-excursion
4397 (cond
4398 (; Comment close preprocessor directives
4399 (and
4400 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4401 (or kill-existing-comment
4402 (not (save-excursion
4403 (end-of-line)
4404 (search-backward "//" (point-at-bol) t)))))
4405 (let ((nest 1) b e
4407 (else (if (match-end 2) "!" " ")))
4408 (end-of-line)
4409 (if kill-existing-comment
4410 (verilog-kill-existing-comment))
4411 (delete-horizontal-space)
4412 (save-excursion
4413 (backward-sexp 1)
4414 (while (and (/= nest 0)
4415 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4416 (cond
4417 ((match-end 1) ; `else
4418 (if (= nest 1)
4419 (setq else "!")))
4420 ((match-end 2) ; `endif
4421 (setq nest (1+ nest)))
4422 ((match-end 3) ; `if
4423 (setq nest (1- nest)))
4424 ((match-end 4) ; `ifdef
4425 (setq nest (1- nest)))
4426 ((match-end 5) ; `ifndef
4427 (setq nest (1- nest)))
4428 ((match-end 6) ; `elsif
4429 (if (= nest 1)
4430 (progn
4431 (setq else "!")
4432 (setq nest 0))))))
4433 (if (match-end 0)
4434 (setq
4435 m (buffer-substring
4436 (match-beginning 0)
4437 (match-end 0))
4438 b (progn
4439 (skip-chars-forward "^ \t")
4440 (verilog-forward-syntactic-ws)
4441 (point))
4442 e (progn
4443 (skip-chars-forward "a-zA-Z0-9_")
4444 (point)))))
4445 (if b
4446 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4447 (insert (concat " // " else m " " (buffer-substring b e))))
4448 (progn
4449 (insert " // unmatched `else, `elsif or `endif")
4450 (ding 't)))))
4452 (; Comment close case/class/function/task/module and named block
4453 (and (looking-at "\\<end")
4454 (or kill-existing-comment
4455 (not (save-excursion
4456 (end-of-line)
4457 (search-backward "//" (point-at-bol) t)))))
4458 (let ((type (car indent-str)))
4459 (unless (eq type 'declaration)
4460 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
4461 (if (looking-at verilog-end-block-ordered-re)
4462 (cond
4463 (;- This is a case block; search back for the start of this case
4464 (match-end 1) ;; of verilog-end-block-ordered-re
4466 (let ((err 't)
4467 (str "UNMATCHED!!"))
4468 (save-excursion
4469 (verilog-leap-to-head)
4470 (cond
4471 ((looking-at "\\<randcase\\>")
4472 (setq str "randcase")
4473 (setq err nil))
4474 ((looking-at "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4475 (goto-char (match-end 0))
4476 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4477 (setq err nil))
4479 (end-of-line)
4480 (if kill-existing-comment
4481 (verilog-kill-existing-comment))
4482 (delete-horizontal-space)
4483 (insert (concat " // " str ))
4484 (if err (ding 't))))
4486 (;- This is a begin..end block
4487 (match-end 2) ;; of verilog-end-block-ordered-re
4488 (let ((str " // UNMATCHED !!")
4489 (err 't)
4490 (here (point))
4491 there
4492 cntx)
4493 (save-excursion
4494 (verilog-leap-to-head)
4495 (setq there (point))
4496 (if (not (match-end 0))
4497 (progn
4498 (goto-char here)
4499 (end-of-line)
4500 (if kill-existing-comment
4501 (verilog-kill-existing-comment))
4502 (delete-horizontal-space)
4503 (insert str)
4504 (ding 't))
4505 (let ((lim
4506 (save-excursion (verilog-beg-of-defun) (point)))
4507 (here (point)))
4508 (cond
4509 (;-- handle named block differently
4510 (looking-at verilog-named-block-re)
4511 (search-forward ":")
4512 (setq there (point))
4513 (setq str (verilog-get-expr))
4514 (setq err nil)
4515 (setq str (concat " // block: " str )))
4517 ((verilog-in-case-region-p) ;-- handle case item differently
4518 (goto-char here)
4519 (setq str (verilog-backward-case-item lim))
4520 (setq there (point))
4521 (setq err nil)
4522 (setq str (concat " // case: " str )))
4524 (;- try to find "reason" for this begin
4525 (cond
4527 (eq here (progn
4528 ;; (verilog-backward-token)
4529 (verilog-beg-of-statement)
4530 (point)))
4531 (setq err nil)
4532 (setq str ""))
4533 ((looking-at verilog-endcomment-reason-re)
4534 (setq there (match-end 0))
4535 (setq cntx (concat (match-string 0) " "))
4536 (cond
4537 (;- begin
4538 (match-end 1)
4539 (setq err nil)
4540 (save-excursion
4541 (if (and (verilog-continued-line)
4542 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4543 (progn
4544 (goto-char (match-end 0))
4545 (setq there (point))
4546 (setq str
4547 (concat " // " (match-string 0) " " (verilog-get-expr))))
4548 (setq str ""))))
4550 (;- else
4551 (match-end 2)
4552 (let ((nest 0)
4553 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4554 (catch 'skip
4555 (while (verilog-re-search-backward reg nil 'move)
4556 (cond
4557 ((match-end 1) ; begin
4558 (setq nest (1- nest)))
4559 ((match-end 2) ; end
4560 (setq nest (1+ nest)))
4561 ((match-end 3)
4562 (if (= 0 nest)
4563 (progn
4564 (goto-char (match-end 0))
4565 (setq there (point))
4566 (setq err nil)
4567 (setq str (verilog-get-expr))
4568 (setq str (concat " // else: !if" str ))
4569 (throw 'skip 1))))
4570 ((match-end 4)
4571 (if (= 0 nest)
4572 (progn
4573 (goto-char (match-end 0))
4574 (setq there (point))
4575 (setq err nil)
4576 (setq str (verilog-get-expr))
4577 (setq str (concat " // else: !assert " str ))
4578 (throw 'skip 1)))))))))
4579 (;- end else
4580 (match-end 3)
4581 (goto-char there)
4582 (let ((nest 0)
4583 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4584 (catch 'skip
4585 (while (verilog-re-search-backward reg nil 'move)
4586 (cond
4587 ((match-end 1) ; begin
4588 (setq nest (1- nest)))
4589 ((match-end 2) ; end
4590 (setq nest (1+ nest)))
4591 ((match-end 3)
4592 (if (= 0 nest)
4593 (progn
4594 (goto-char (match-end 0))
4595 (setq there (point))
4596 (setq err nil)
4597 (setq str (verilog-get-expr))
4598 (setq str (concat " // else: !if" str ))
4599 (throw 'skip 1))))
4600 ((match-end 4)
4601 (if (= 0 nest)
4602 (progn
4603 (goto-char (match-end 0))
4604 (setq there (point))
4605 (setq err nil)
4606 (setq str (verilog-get-expr))
4607 (setq str (concat " // else: !assert " str ))
4608 (throw 'skip 1)))))))))
4610 (; always_comb, always_ff, always_latch
4611 (or (match-end 4) (match-end 5) (match-end 6))
4612 (goto-char (match-end 0))
4613 (setq there (point))
4614 (setq err nil)
4615 (setq str (concat " // " cntx )))
4617 (;- task/function/initial et cetera
4619 (match-end 0)
4620 (goto-char (match-end 0))
4621 (setq there (point))
4622 (setq err nil)
4623 (setq str (concat " // " cntx (verilog-get-expr))))
4625 (;-- otherwise...
4626 (setq str " // auto-endcomment confused "))))
4628 ((and
4629 (verilog-in-case-region-p) ;-- handle case item differently
4630 (progn
4631 (setq there (point))
4632 (goto-char here)
4633 (setq str (verilog-backward-case-item lim))))
4634 (setq err nil)
4635 (setq str (concat " // case: " str )))
4637 ((verilog-in-fork-region-p)
4638 (setq err nil)
4639 (setq str " // fork branch" ))
4641 ((looking-at "\\<end\\>")
4642 ;; HERE
4643 (forward-word 1)
4644 (verilog-forward-syntactic-ws)
4645 (setq err nil)
4646 (setq str (verilog-get-expr))
4647 (setq str (concat " // " cntx str )))
4649 ))))
4650 (goto-char here)
4651 (end-of-line)
4652 (if kill-existing-comment
4653 (verilog-kill-existing-comment))
4654 (delete-horizontal-space)
4655 (if (or err
4656 (> (count-lines here there) verilog-minimum-comment-distance))
4657 (insert str))
4658 (if err (ding 't))
4659 ))))
4660 (;- this is endclass, which can be nested
4661 (match-end 11) ;; of verilog-end-block-ordered-re
4662 ;;(goto-char there)
4663 (let ((nest 0)
4664 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4665 string)
4666 (save-excursion
4667 (catch 'skip
4668 (while (verilog-re-search-backward reg nil 'move)
4669 (cond
4670 ((match-end 3) ; endclass
4671 (ding 't)
4672 (setq string "unmatched endclass")
4673 (throw 'skip 1))
4675 ((match-end 2) ; endclass
4676 (setq nest (1+ nest)))
4678 ((match-end 1) ; class
4679 (setq nest (1- nest))
4680 (if (< nest 0)
4681 (progn
4682 (goto-char (match-end 0))
4683 (let (b e)
4684 (setq b (progn
4685 (skip-chars-forward "^ \t")
4686 (verilog-forward-ws&directives)
4687 (point))
4688 e (progn
4689 (skip-chars-forward "a-zA-Z0-9_")
4690 (point)))
4691 (setq string (buffer-substring b e)))
4692 (throw 'skip 1))))
4693 ))))
4694 (end-of-line)
4695 (insert (concat " // " string ))))
4697 (;- this is end{function,generate,task,module,primitive,table,generate}
4698 ;- which can not be nested.
4700 (let (string reg (name-re nil))
4701 (end-of-line)
4702 (if kill-existing-comment
4703 (save-match-data
4704 (verilog-kill-existing-comment)))
4705 (delete-horizontal-space)
4706 (backward-sexp)
4707 (cond
4708 ((match-end 5) ;; of verilog-end-block-ordered-re
4709 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4710 (setq name-re "\\w+\\s-*("))
4711 ((match-end 6) ;; of verilog-end-block-ordered-re
4712 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4713 (setq name-re "\\w+\\s-*("))
4714 ((match-end 7) ;; of verilog-end-block-ordered-re
4715 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4716 ((match-end 8) ;; of verilog-end-block-ordered-re
4717 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4718 ((match-end 9) ;; of verilog-end-block-ordered-re
4719 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4720 ((match-end 10) ;; of verilog-end-block-ordered-re
4721 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4722 ((match-end 11) ;; of verilog-end-block-ordered-re
4723 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4724 ((match-end 12) ;; of verilog-end-block-ordered-re
4725 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4726 ((match-end 13) ;; of verilog-end-block-ordered-re
4727 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4728 ((match-end 14) ;; of verilog-end-block-ordered-re
4729 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4730 ((match-end 15) ;; of verilog-end-block-ordered-re
4731 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4733 (t (error "Problem in verilog-set-auto-endcomments")))
4734 (let (b e)
4735 (save-excursion
4736 (verilog-re-search-backward reg nil 'move)
4737 (cond
4738 ((match-end 1)
4739 (setq b (progn
4740 (skip-chars-forward "^ \t")
4741 (verilog-forward-ws&directives)
4742 (if (looking-at "static\\|automatic")
4743 (progn
4744 (goto-char (match-end 0))
4745 (verilog-forward-ws&directives)))
4746 (if (and name-re (verilog-re-search-forward name-re nil 'move))
4747 (progn
4748 (goto-char (match-beginning 0))
4749 (verilog-forward-ws&directives)))
4750 (point))
4751 e (progn
4752 (skip-chars-forward "a-zA-Z0-9_")
4753 (point)))
4754 (setq string (buffer-substring b e)))
4756 (ding 't)
4757 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4758 (end-of-line)
4759 (insert (concat " // " string )))
4760 ))))))))))
4762 (defun verilog-get-expr()
4763 "Grab expression at point, e.g, case ( a | b & (c ^d))."
4764 (let* ((b (progn
4765 (verilog-forward-syntactic-ws)
4766 (skip-chars-forward " \t")
4767 (point)))
4768 (e (let ((par 1))
4769 (cond
4770 ((looking-at "@")
4771 (forward-char 1)
4772 (verilog-forward-syntactic-ws)
4773 (if (looking-at "(")
4774 (progn
4775 (forward-char 1)
4776 (while (and (/= par 0)
4777 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4778 (cond
4779 ((match-end 1)
4780 (setq par (1+ par)))
4781 ((match-end 2)
4782 (setq par (1- par)))))))
4783 (point))
4784 ((looking-at "(")
4785 (forward-char 1)
4786 (while (and (/= par 0)
4787 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4788 (cond
4789 ((match-end 1)
4790 (setq par (1+ par)))
4791 ((match-end 2)
4792 (setq par (1- par)))))
4793 (point))
4794 ((looking-at "\\[")
4795 (forward-char 1)
4796 (while (and (/= par 0)
4797 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4798 (cond
4799 ((match-end 1)
4800 (setq par (1+ par)))
4801 ((match-end 2)
4802 (setq par (1- par)))))
4803 (verilog-forward-syntactic-ws)
4804 (skip-chars-forward "^ \t\n\f")
4805 (point))
4806 ((looking-at "/[/\\*]")
4809 (skip-chars-forward "^: \t\n\f")
4810 (point)))))
4811 (str (buffer-substring b e)))
4812 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4813 (setq str (concat (substring str 0 e) "...")))
4814 str))
4816 (defun verilog-expand-vector ()
4817 "Take a signal vector on the current line and expand it to multiple lines.
4818 Useful for creating tri's and other expanded fields."
4819 (interactive)
4820 (verilog-expand-vector-internal "[" "]"))
4822 (defun verilog-expand-vector-internal (bra ket)
4823 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
4824 (save-excursion
4825 (forward-line 0)
4826 (let ((signal-string (buffer-substring (point)
4827 (progn
4828 (end-of-line) (point)))))
4829 (if (string-match
4830 (concat "\\(.*\\)"
4831 (regexp-quote bra)
4832 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
4833 (regexp-quote ket)
4834 "\\(.*\\)$") signal-string)
4835 (let* ((sig-head (match-string 1 signal-string))
4836 (vec-start (string-to-number (match-string 2 signal-string)))
4837 (vec-end (if (= (match-beginning 3) (match-end 3))
4838 vec-start
4839 (string-to-number
4840 (substring signal-string (1+ (match-beginning 3))
4841 (match-end 3)))))
4842 (vec-range
4843 (if (= (match-beginning 4) (match-end 4))
4845 (string-to-number
4846 (substring signal-string (+ 2 (match-beginning 4))
4847 (match-end 4)))))
4848 (sig-tail (match-string 5 signal-string))
4849 vec)
4850 ;; Decode vectors
4851 (setq vec nil)
4852 (if (< vec-range 0)
4853 (let ((tmp vec-start))
4854 (setq vec-start vec-end
4855 vec-end tmp
4856 vec-range (- vec-range))))
4857 (if (< vec-end vec-start)
4858 (while (<= vec-end vec-start)
4859 (setq vec (append vec (list vec-start)))
4860 (setq vec-start (- vec-start vec-range)))
4861 (while (<= vec-start vec-end)
4862 (setq vec (append vec (list vec-start)))
4863 (setq vec-start (+ vec-start vec-range))))
4865 ;; Delete current line
4866 (delete-region (point) (progn (forward-line 0) (point)))
4868 ;; Expand vector
4869 (while vec
4870 (insert (concat sig-head bra
4871 (int-to-string (car vec)) ket sig-tail "\n"))
4872 (setq vec (cdr vec)))
4873 (delete-char -1)
4875 )))))
4877 (defun verilog-strip-comments ()
4878 "Strip all comments from the Verilog code."
4879 (interactive)
4880 (goto-char (point-min))
4881 (while (re-search-forward "//" nil t)
4882 (if (verilog-within-string)
4883 (re-search-forward "\"" nil t)
4884 (if (verilog-in-star-comment-p)
4885 (re-search-forward "\*/" nil t)
4886 (let ((bpt (- (point) 2)))
4887 (end-of-line)
4888 (delete-region bpt (point))))))
4890 (goto-char (point-min))
4891 (while (re-search-forward "/\\*" nil t)
4892 (if (verilog-within-string)
4893 (re-search-forward "\"" nil t)
4894 (let ((bpt (- (point) 2)))
4895 (re-search-forward "\\*/")
4896 (delete-region bpt (point))))))
4898 (defun verilog-one-line ()
4899 "Convert structural Verilog instances to occupy one line."
4900 (interactive)
4901 (goto-char (point-min))
4902 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
4903 (replace-match "\\1 " nil nil)))
4905 (defun verilog-linter-name ()
4906 "Return name of linter, either surelint or verilint."
4907 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4908 compile-command))
4909 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4910 verilog-linter)))
4911 (cond ((equal compile-word1 "surelint") `surelint)
4912 ((equal compile-word1 "verilint") `verilint)
4913 ((equal lint-word1 "surelint") `surelint)
4914 ((equal lint-word1 "verilint") `verilint)
4915 (t `surelint)))) ;; back compatibility
4917 (defun verilog-lint-off ()
4918 "Convert a Verilog linter warning line into a disable statement.
4919 For example:
4920 pci_bfm_null.v, line 46: Unused input: pci_rst_
4921 becomes a comment for the appropriate tool.
4923 The first word of the `compile-command' or `verilog-linter'
4924 variables is used to determine which product is being used.
4926 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
4927 (interactive)
4928 (let ((linter (verilog-linter-name)))
4929 (cond ((equal linter `surelint)
4930 (verilog-surelint-off))
4931 ((equal linter `verilint)
4932 (verilog-verilint-off))
4933 (t (error "Linter name not set")))))
4935 (defvar compilation-last-buffer)
4936 (defvar next-error-last-buffer)
4938 (defun verilog-surelint-off ()
4939 "Convert a SureLint warning line into a disable statement.
4940 Run from Verilog source window; assumes there is a *compile* buffer
4941 with point set appropriately.
4943 For example:
4944 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
4945 becomes:
4946 // surefire lint_line_off UDDONX"
4947 (interactive)
4948 (let ((buff (if (boundp 'next-error-last-buffer)
4949 next-error-last-buffer
4950 compilation-last-buffer)))
4951 (when (buffer-live-p buff)
4952 (save-excursion
4953 (switch-to-buffer buff)
4954 (beginning-of-line)
4955 (when
4956 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
4957 (let* ((code (match-string 2))
4958 (file (match-string 3))
4959 (line (match-string 4))
4960 (buffer (get-file-buffer file))
4961 dir filename)
4962 (unless buffer
4963 (progn
4964 (setq buffer
4965 (and (file-exists-p file)
4966 (find-file-noselect file)))
4967 (or buffer
4968 (let* ((pop-up-windows t))
4969 (let ((name (expand-file-name
4970 (read-file-name
4971 (format "Find this error in: (default %s) "
4972 file)
4973 dir file t))))
4974 (if (file-directory-p name)
4975 (setq name (expand-file-name filename name)))
4976 (setq buffer
4977 (and (file-exists-p name)
4978 (find-file-noselect name))))))))
4979 (switch-to-buffer buffer)
4980 (goto-char (point-min))
4981 (forward-line (- (string-to-number line)))
4982 (end-of-line)
4983 (catch 'already
4984 (cond
4985 ((verilog-in-slash-comment-p)
4986 (re-search-backward "//")
4987 (cond
4988 ((looking-at "// surefire lint_off_line ")
4989 (goto-char (match-end 0))
4990 (let ((lim (point-at-eol)))
4991 (if (re-search-forward code lim 'move)
4992 (throw 'already t)
4993 (insert (concat " " code)))))
4996 ((verilog-in-star-comment-p)
4997 (re-search-backward "/\*")
4998 (insert (format " // surefire lint_off_line %6s" code )))
5000 (insert (format " // surefire lint_off_line %6s" code ))
5001 )))))))))
5003 (defun verilog-verilint-off ()
5004 "Convert a Verilint warning line into a disable statement.
5006 For example:
5007 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
5008 becomes:
5009 //Verilint 240 off // WARNING: Unused input"
5010 (interactive)
5011 (save-excursion
5012 (beginning-of-line)
5013 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
5014 (replace-match (format
5015 ;; %3s makes numbers 1-999 line up nicely
5016 "\\1//Verilint %3s off // WARNING: \\3"
5017 (match-string 2)))
5018 (beginning-of-line)
5019 (verilog-indent-line))))
5021 (defun verilog-auto-save-compile ()
5022 "Update automatics with \\[verilog-auto], save the buffer, and compile."
5023 (interactive)
5024 (verilog-auto) ; Always do it for safety
5025 (save-buffer)
5026 (compile compile-command))
5028 (defun verilog-preprocess (&optional command filename)
5029 "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
5030 Takes optional COMMAND or defaults to `verilog-preprocessor', and
5031 FILENAME to find directory to run in, or defaults to `buffer-file-name`."
5032 (interactive
5033 (list
5034 (let ((default (verilog-expand-command verilog-preprocessor)))
5035 (set (make-local-variable `verilog-preprocessor)
5036 (read-from-minibuffer "Run Preprocessor (like this): "
5037 default nil nil
5038 'verilog-preprocess-history default)))))
5039 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
5040 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
5041 (dir (file-name-directory (or filename buffer-file-name)))
5042 (cmd (concat "cd " dir "; " command)))
5043 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
5044 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
5045 (insert (concat "// " cmd "\n"))
5046 (call-process shell-file-name nil t nil shell-command-switch cmd)
5047 (verilog-mode)
5048 ;; Without this force, it takes a few idle seconds
5049 ;; to get the color, which is very jarring
5050 (when fontlocked (font-lock-fontify-buffer))))))
5054 ;; Batch
5057 (defun verilog-warn (string &rest args)
5058 "Print a warning with `format' using STRING and optional ARGS."
5059 (apply 'message (concat "%%Warning: " string) args))
5061 (defun verilog-warn-error (string &rest args)
5062 "Call `error' using STRING and optional ARGS.
5063 If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
5064 (if verilog-warn-fatal
5065 (apply 'error string args)
5066 (apply 'verilog-warn string args)))
5068 (defmacro verilog-batch-error-wrapper (&rest body)
5069 "Execute BODY and add error prefix to any errors found.
5070 This lets programs calling batch mode to easily extract error messages."
5071 `(let ((verilog-warn-fatal nil))
5072 (condition-case err
5073 (progn ,@body)
5074 (error
5075 (error "%%Error: %s%s" (error-message-string err)
5076 (if (featurep 'xemacs) "\n" "")))))) ;; XEmacs forgets to add a newline
5078 (defun verilog-batch-execute-func (funref &optional no-save)
5079 "Internal processing of a batch command.
5080 Runs FUNREF on all command arguments.
5081 Save the result unless optional NO-SAVE is t."
5082 (verilog-batch-error-wrapper
5083 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5084 ;; However, this function is called only when Emacs is being used as
5085 ;; a standalone language instead of as an editor, so we'll live.
5087 ;; General globals needed
5088 (setq make-backup-files nil)
5089 (setq-default make-backup-files nil)
5090 (setq enable-local-variables t)
5091 (setq enable-local-eval t)
5092 ;; Make sure any sub-files we read get proper mode
5093 (setq-default major-mode 'verilog-mode)
5094 ;; Ditto files already read in
5095 (mapc (lambda (buf)
5096 (when (buffer-file-name buf)
5097 (with-current-buffer buf
5098 (verilog-mode))))
5099 (buffer-list))
5100 ;; Process the files
5101 (mapcar (lambda (buf)
5102 (when (buffer-file-name buf)
5103 (save-excursion
5104 (if (not (file-exists-p (buffer-file-name buf)))
5105 (error
5106 (concat "File not found: " (buffer-file-name buf))))
5107 (message (concat "Processing " (buffer-file-name buf)))
5108 (set-buffer buf)
5109 (funcall funref)
5110 (unless no-save (save-buffer)))))
5111 (buffer-list))))
5113 (defun verilog-batch-auto ()
5114 "For use with --batch, perform automatic expansions as a stand-alone tool.
5115 This sets up the appropriate Verilog mode environment, updates automatics
5116 with \\[verilog-auto] on all command-line files, and saves the buffers.
5117 For proper results, multiple filenames need to be passed on the command
5118 line in bottom-up order."
5119 (unless noninteractive
5120 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5121 (verilog-batch-execute-func `verilog-auto))
5123 (defun verilog-batch-delete-auto ()
5124 "For use with --batch, perform automatic deletion as a stand-alone tool.
5125 This sets up the appropriate Verilog mode environment, deletes automatics
5126 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5127 (unless noninteractive
5128 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5129 (verilog-batch-execute-func `verilog-delete-auto))
5131 (defun verilog-batch-diff-auto ()
5132 "For use with --batch, perform automatic differences as a stand-alone tool.
5133 This sets up the appropriate Verilog mode environment, expand automatics
5134 with \\[verilog-diff-auto] on all command-line files, and reports an error
5135 if any differences are observed. This is appropriate for adding to regressions
5136 to insure automatics are always properly maintained."
5137 (unless noninteractive
5138 (error "Use verilog-batch-diff-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5139 (verilog-batch-execute-func `verilog-diff-auto t))
5141 (defun verilog-batch-inject-auto ()
5142 "For use with --batch, perform automatic injection as a stand-alone tool.
5143 This sets up the appropriate Verilog mode environment, injects new automatics
5144 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5145 For proper results, multiple filenames need to be passed on the command
5146 line in bottom-up order."
5147 (unless noninteractive
5148 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5149 (verilog-batch-execute-func `verilog-inject-auto))
5151 (defun verilog-batch-indent ()
5152 "For use with --batch, reindent an entire file as a stand-alone tool.
5153 This sets up the appropriate Verilog mode environment, calls
5154 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5155 (unless noninteractive
5156 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
5157 (verilog-batch-execute-func `verilog-indent-buffer))
5161 ;; Indentation
5163 (defconst verilog-indent-alist
5164 '((block . (+ ind verilog-indent-level))
5165 (case . (+ ind verilog-case-indent))
5166 (cparenexp . (+ ind verilog-indent-level))
5167 (cexp . (+ ind verilog-cexp-indent))
5168 (defun . verilog-indent-level-module)
5169 (declaration . verilog-indent-level-declaration)
5170 (directive . (verilog-calculate-indent-directive))
5171 (tf . verilog-indent-level)
5172 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5173 (statement . ind)
5174 (cpp . 0)
5175 (comment . (verilog-comment-indent))
5176 (unknown . 3)
5177 (string . 0)))
5179 (defun verilog-continued-line-1 (lim)
5180 "Return true if this is a continued line.
5181 Set point to where line starts. Limit search to point LIM."
5182 (let ((continued 't))
5183 (if (eq 0 (forward-line -1))
5184 (progn
5185 (end-of-line)
5186 (verilog-backward-ws&directives lim)
5187 (if (bobp)
5188 (setq continued nil)
5189 (setq continued (verilog-backward-token))))
5190 (setq continued nil))
5191 continued))
5193 (defun verilog-calculate-indent ()
5194 "Calculate the indent of the current Verilog line.
5195 Examine previous lines. Once a line is found that is definitive as to the
5196 type of the current line, return that lines' indent level and its type.
5197 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
5198 (save-excursion
5199 (let* ((starting_position (point))
5200 (par 0)
5201 (begin (looking-at "[ \t]*begin\\>"))
5202 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5203 (type (catch 'nesting
5204 ;; Keep working backwards until we can figure out
5205 ;; what type of statement this is.
5206 ;; Basically we need to figure out
5207 ;; 1) if this is a continuation of the previous line;
5208 ;; 2) are we in a block scope (begin..end)
5210 ;; if we are in a comment, done.
5211 (if (verilog-in-star-comment-p)
5212 (throw 'nesting 'comment))
5214 ;; if we have a directive, done.
5215 (if (save-excursion (beginning-of-line)
5216 (and (looking-at verilog-directive-re-1)
5217 (not (or (looking-at "[ \t]*`[ou]vm_")
5218 (looking-at "[ \t]*`vmm_")))))
5219 (throw 'nesting 'directive))
5220 ;; indent structs as if there were module level
5221 (if (verilog-in-struct-p)
5222 (throw 'nesting 'block))
5224 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5225 ;; unless we are in the newfangled coverpoint or constraint blocks
5226 (if (and
5227 verilog-indent-lists
5228 (verilog-in-paren)
5229 (not (verilog-in-coverage-p))
5231 (progn (setq par 1)
5232 (throw 'nesting 'block)))
5234 ;; See if we are continuing a previous line
5235 (while t
5236 ;; trap out if we crawl off the top of the buffer
5237 (if (bobp) (throw 'nesting 'cpp))
5239 (if (verilog-continued-line-1 lim)
5240 (let ((sp (point)))
5241 (if (and
5242 (not (looking-at verilog-complete-reg))
5243 (verilog-continued-line-1 lim))
5244 (progn (goto-char sp)
5245 (throw 'nesting 'cexp))
5247 (goto-char sp))
5249 (if (and begin
5250 (not verilog-indent-begin-after-if)
5251 (looking-at verilog-no-indent-begin-re))
5252 (progn
5253 (beginning-of-line)
5254 (skip-chars-forward " \t")
5255 (throw 'nesting 'statement))
5256 (progn
5257 (throw 'nesting 'cexp))))
5258 ;; not a continued line
5259 (goto-char starting_position))
5261 (if (looking-at "\\<else\\>")
5262 ;; search back for governing if, striding across begin..end pairs
5263 ;; appropriately
5264 (let ((elsec 1))
5265 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5266 (cond
5267 ((match-end 1) ; else, we're in deep
5268 (setq elsec (1+ elsec)))
5269 ((match-end 2) ; if
5270 (setq elsec (1- elsec))
5271 (if (= 0 elsec)
5272 (if verilog-align-ifelse
5273 (throw 'nesting 'statement)
5274 (progn ;; back up to first word on this line
5275 (beginning-of-line)
5276 (verilog-forward-syntactic-ws)
5277 (throw 'nesting 'statement)))))
5278 ((match-end 3) ; assert block
5279 (setq elsec (1- elsec))
5280 (verilog-beg-of-statement) ;; doesn't get to beginning
5281 (if (looking-at verilog-property-re)
5282 (throw 'nesting 'statement) ; We don't need an endproperty for these
5283 (throw 'nesting 'block) ;We still need an endproperty
5285 (t ; endblock
5286 ; try to leap back to matching outward block by striding across
5287 ; indent level changing tokens then immediately
5288 ; previous line governs indentation.
5289 (let (( reg) (nest 1))
5290 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5291 (cond
5292 ((match-end 4) ; end
5293 ;; Search back for matching begin
5294 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5295 ((match-end 5) ; endcase
5296 ;; Search back for matching case
5297 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5298 ((match-end 6) ; endfunction
5299 ;; Search back for matching function
5300 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5301 ((match-end 7) ; endtask
5302 ;; Search back for matching task
5303 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5304 ((match-end 8) ; endspecify
5305 ;; Search back for matching specify
5306 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5307 ((match-end 9) ; endtable
5308 ;; Search back for matching table
5309 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5310 ((match-end 10) ; endgenerate
5311 ;; Search back for matching generate
5312 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5313 ((match-end 11) ; joins
5314 ;; Search back for matching fork
5315 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5316 ((match-end 12) ; class
5317 ;; Search back for matching class
5318 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5319 ((match-end 13) ; covergroup
5320 ;; Search back for matching covergroup
5321 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5322 (catch 'skip
5323 (while (verilog-re-search-backward reg nil 'move)
5324 (cond
5325 ((match-end 1) ; begin
5326 (setq nest (1- nest))
5327 (if (= 0 nest)
5328 (throw 'skip 1)))
5329 ((match-end 2) ; end
5330 (setq nest (1+ nest)))))
5331 )))))))
5332 (throw 'nesting (verilog-calc-1)))
5333 );; catch nesting
5334 );; type
5336 ;; Return type of block and indent level.
5337 (if (not type)
5338 (setq type 'cpp))
5339 (if (> par 0) ; Unclosed Parenthesis
5340 (list 'cparenexp par)
5341 (cond
5342 ((eq type 'case)
5343 (list type (verilog-case-indent-level)))
5344 ((eq type 'statement)
5345 (list type (current-column)))
5346 ((eq type 'defun)
5347 (list type 0))
5349 (list type (verilog-current-indent-level))))))))
5351 (defun verilog-wai ()
5352 "Show matching nesting block for debugging."
5353 (interactive)
5354 (save-excursion
5355 (let* ((type (verilog-calc-1))
5356 depth)
5357 ;; Return type of block and indent level.
5358 (if (not type)
5359 (setq type 'cpp))
5360 (if (and
5361 verilog-indent-lists
5362 (not(or (verilog-in-coverage-p)
5363 (verilog-in-struct-p)))
5364 (verilog-in-paren))
5365 (setq depth 1)
5366 (cond
5367 ((eq type 'case)
5368 (setq depth (verilog-case-indent-level)))
5369 ((eq type 'statement)
5370 (setq depth (current-column)))
5371 ((eq type 'defun)
5372 (setq depth 0))
5374 (setq depth (verilog-current-indent-level)))))
5375 (message "You are at nesting %s depth %d" type depth))))
5376 (defun verilog-calc-1 ()
5377 (catch 'nesting
5378 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)")))
5379 (while (verilog-re-search-backward re nil 'move)
5380 (catch 'continue
5381 (cond
5382 ((equal (char-after) ?\{)
5383 (if (verilog-at-constraint-p)
5384 (throw 'nesting 'block)))
5386 ((equal (char-after) ?\})
5387 (let ((there (verilog-at-close-constraint-p)))
5388 (if there ;; we are at the } that closes a constraint. Find the { that opens it
5389 (progn
5390 (forward-char 1)
5391 (backward-list 1)
5392 (verilog-beg-of-statement)))))
5394 ((looking-at verilog-beg-block-re-ordered)
5395 (cond
5396 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5397 (let ((here (point)))
5398 (verilog-beg-of-statement)
5399 (if (looking-at verilog-extended-case-re)
5400 (throw 'nesting 'case)
5401 (goto-char here)))
5402 (throw 'nesting 'case))
5404 ((match-end 4) ; *sigh* could be "disable fork"
5405 (let ((here (point)))
5406 (verilog-beg-of-statement)
5407 (if (looking-at verilog-disable-fork-re)
5408 t ; this is a normal statement
5409 (progn ; or is fork, starts a new block
5410 (goto-char here)
5411 (throw 'nesting 'block)))))
5413 ((match-end 27) ; *sigh* might be a clocking declaration
5414 (let ((here (point)))
5415 (if (verilog-in-paren)
5416 t ; this is a normal statement
5417 (progn ; or is fork, starts a new block
5418 (goto-char here)
5419 (throw 'nesting 'block)))))
5421 ;; need to consider typedef struct here...
5422 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
5423 ; *sigh* These words have an optional prefix:
5424 ; extern {virtual|protected}? function a();
5425 ; typedef class foo;
5426 ; and we don't want to confuse this with
5427 ; function a();
5428 ; property
5429 ; ...
5430 ; endfunction
5431 (verilog-beg-of-statement)
5432 (if (looking-at verilog-beg-block-re-ordered)
5433 (throw 'nesting 'block)
5434 (throw 'nesting 'defun)))
5437 ((looking-at "\\<property\\>")
5438 ; *sigh*
5439 ; {assert|assume|cover} property (); are complete
5440 ; and could also be labeled: - foo: assert property
5441 ; but
5442 ; property ID () ... needs end_property
5443 (verilog-beg-of-statement)
5444 (if (looking-at verilog-property-re)
5445 (throw 'continue 'statement) ; We don't need an endproperty for these
5446 (throw 'nesting 'block) ;We still need an endproperty
5449 (t (throw 'nesting 'block))))
5451 ((looking-at verilog-end-block-re)
5452 (verilog-leap-to-head)
5453 (if (verilog-in-case-region-p)
5454 (progn
5455 (verilog-leap-to-case-head)
5456 (if (looking-at verilog-extended-case-re)
5457 (throw 'nesting 'case)))))
5459 ((looking-at verilog-defun-level-re)
5460 (if (looking-at verilog-defun-level-generate-only-re)
5461 (if (verilog-in-generate-region-p)
5462 (throw 'continue 'foo) ; always block in a generate - keep looking
5463 (throw 'nesting 'defun))
5464 (throw 'nesting 'defun)))
5466 ((looking-at verilog-cpp-level-re)
5467 (throw 'nesting 'cpp))
5469 ((bobp)
5470 (throw 'nesting 'cpp)))))
5472 (throw 'nesting 'cpp))))
5474 (defun verilog-calculate-indent-directive ()
5475 "Return indentation level for directive.
5476 For speed, the searcher looks at the last directive, not the indent
5477 of the appropriate enclosing block."
5478 (let ((base -1) ;; Indent of the line that determines our indentation
5479 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
5480 ;; Start at current location, scan back for another directive
5482 (save-excursion
5483 (beginning-of-line)
5484 (while (and (< base 0)
5485 (verilog-re-search-backward verilog-directive-re nil t))
5486 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
5487 (setq base (current-indentation))))
5488 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
5489 (setq ind (- ind verilog-indent-level-directive)))
5490 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
5491 (setq ind (+ ind verilog-indent-level-directive)))
5492 ((looking-at verilog-directive-begin)
5493 (setq ind (+ ind verilog-indent-level-directive)))))
5494 ;; Adjust indent to starting indent of critical line
5495 (setq ind (max 0 (+ ind base))))
5497 (save-excursion
5498 (beginning-of-line)
5499 (skip-chars-forward " \t")
5500 (cond ((or (looking-at verilog-directive-middle)
5501 (looking-at verilog-directive-end))
5502 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5503 ind))
5505 (defun verilog-leap-to-case-head ()
5506 (let ((nest 1))
5507 (while (/= 0 nest)
5508 (verilog-re-search-backward
5509 (concat
5510 "\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5511 "\\|\\(\\<endcase\\>\\)" )
5512 nil 'move)
5513 (cond
5514 ((match-end 1)
5515 (let ((here (point)))
5516 (verilog-beg-of-statement)
5517 (unless (looking-at verilog-extended-case-re)
5518 (goto-char here)))
5519 (setq nest (1- nest)))
5520 ((match-end 3)
5521 (setq nest (1+ nest)))
5522 ((bobp)
5523 (ding 't)
5524 (setq nest 0))))))
5526 (defun verilog-leap-to-head ()
5527 "Move point to the head of this block.
5528 Jump from end to matching begin, from endcase to matching case, and so on."
5529 (let ((reg nil)
5530 snest
5531 (nesting 'yes)
5532 (nest 1))
5533 (cond
5534 ((looking-at "\\<end\\>")
5535 ;; 1: Search back for matching begin
5536 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5537 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
5538 ((looking-at "\\<endtask\\>")
5539 ;; 2: Search back for matching task
5540 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5541 (setq nesting 'no))
5542 ((looking-at "\\<endcase\\>")
5543 (catch 'nesting
5544 (verilog-leap-to-case-head) )
5545 (setq reg nil) ; to force skip
5548 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
5549 ;; 4: Search back for matching fork
5550 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5551 ((looking-at "\\<endclass\\>")
5552 ;; 5: Search back for matching class
5553 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5554 ((looking-at "\\<endtable\\>")
5555 ;; 6: Search back for matching table
5556 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5557 ((looking-at "\\<endspecify\\>")
5558 ;; 7: Search back for matching specify
5559 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5560 ((looking-at "\\<endfunction\\>")
5561 ;; 8: Search back for matching function
5562 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5563 (setq nesting 'no))
5564 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5565 ((looking-at "\\<endgenerate\\>")
5566 ;; 8: Search back for matching generate
5567 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5568 ((looking-at "\\<endgroup\\>")
5569 ;; 10: Search back for matching covergroup
5570 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5571 ((looking-at "\\<endproperty\\>")
5572 ;; 11: Search back for matching property
5573 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
5574 ((looking-at verilog-uvm-end-re)
5575 ;; 12: Search back for matching sequence
5576 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
5577 ((looking-at verilog-ovm-end-re)
5578 ;; 12: Search back for matching sequence
5579 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
5580 ((looking-at verilog-vmm-end-re)
5581 ;; 12: Search back for matching sequence
5582 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
5583 ((looking-at "\\<endinterface\\>")
5584 ;; 12: Search back for matching interface
5585 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5586 ((looking-at "\\<endsequence\\>")
5587 ;; 12: Search back for matching sequence
5588 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5589 ((looking-at "\\<endclocking\\>")
5590 ;; 12: Search back for matching clocking
5591 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
5592 (if reg
5593 (catch 'skip
5594 (if (eq nesting 'yes)
5595 (let (sreg)
5596 (while (verilog-re-search-backward reg nil 'move)
5597 (cond
5598 ((match-end 1) ; begin
5599 (if (looking-at "fork")
5600 (let ((here (point)))
5601 (verilog-beg-of-statement)
5602 (unless (looking-at verilog-disable-fork-re)
5603 (goto-char here)
5604 (setq nest (1- nest))))
5605 (setq nest (1- nest)))
5606 (if (= 0 nest)
5607 ;; Now previous line describes syntax
5608 (throw 'skip 1))
5609 (if (and snest
5610 (= snest nest))
5611 (setq reg sreg)))
5612 ((match-end 2) ; end
5613 (setq nest (1+ nest)))
5614 ((match-end 3)
5615 ;; endcase, jump to case
5616 (setq snest nest)
5617 (setq nest (1+ nest))
5618 (setq sreg reg)
5619 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5620 ((match-end 4)
5621 ;; join, jump to fork
5622 (setq snest nest)
5623 (setq nest (1+ nest))
5624 (setq sreg reg)
5625 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5627 ;no nesting
5628 (if (and
5629 (verilog-re-search-backward reg nil 'move)
5630 (match-end 1)) ; task -> could be virtual and/or protected
5631 (progn
5632 (verilog-beg-of-statement)
5633 (throw 'skip 1))
5634 (throw 'skip 1)))))))
5636 (defun verilog-continued-line ()
5637 "Return true if this is a continued line.
5638 Set point to where line starts."
5639 (let ((continued 't))
5640 (if (eq 0 (forward-line -1))
5641 (progn
5642 (end-of-line)
5643 (verilog-backward-ws&directives)
5644 (if (bobp)
5645 (setq continued nil)
5646 (while (and continued
5647 (save-excursion
5648 (skip-chars-backward " \t")
5649 (not (bolp))))
5650 (setq continued (verilog-backward-token)))))
5651 (setq continued nil))
5652 continued))
5654 (defun verilog-backward-token ()
5655 "Step backward token, returning true if this is a continued line."
5656 (interactive)
5657 (verilog-backward-syntactic-ws)
5658 (cond
5659 ((bolp)
5660 nil)
5661 (;-- Anything ending in a ; is complete
5662 (= (preceding-char) ?\;)
5663 nil)
5664 (; If a "}" is prefixed by a ";", then this is a complete statement
5665 ; i.e.: constraint foo { a = b; }
5666 (= (preceding-char) ?\})
5667 (progn
5668 (backward-char)
5669 (not(verilog-at-close-constraint-p))))
5670 (;-- constraint foo { a = b }
5671 ; is a complete statement. *sigh*
5672 (= (preceding-char) ?\{)
5673 (progn
5674 (backward-char)
5675 (not (verilog-at-constraint-p))))
5676 (;" string "
5677 (= (preceding-char) ?\")
5678 (backward-char)
5679 (verilog-skip-backward-comment-or-string)
5680 nil)
5682 (; [3:4]
5683 (= (preceding-char) ?\])
5684 (backward-char)
5685 (verilog-backward-open-bracket)
5688 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5689 ; also could be simply '@(foo)'
5690 ; or foo u1 #(a=8)
5691 ; (b, ... which ISN'T complete
5692 ;;;; Do we need this???
5693 (= (preceding-char) ?\))
5694 (progn
5695 (backward-char)
5696 (verilog-backward-up-list 1)
5697 (verilog-backward-syntactic-ws)
5698 (let ((back (point)))
5699 (forward-word -1)
5700 (cond
5701 ;;XX
5702 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5703 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
5704 ((looking-at verilog-uvm-statement-re)
5705 nil)
5706 ((looking-at verilog-uvm-begin-re)
5708 ((looking-at verilog-uvm-end-re)
5710 ((looking-at verilog-ovm-statement-re)
5711 nil)
5712 ((looking-at verilog-ovm-begin-re)
5714 ((looking-at verilog-ovm-end-re)
5716 ;; JBA find VMM macros
5717 ((looking-at verilog-vmm-statement-re)
5718 nil )
5719 ((looking-at verilog-vmm-begin-re)
5721 ((looking-at verilog-vmm-end-re)
5722 nil)
5723 ;; JBA trying to catch macro lines with no ; at end
5724 ((looking-at "\\<`")
5725 nil)
5727 (goto-char back)
5728 (cond
5729 ((= (preceding-char) ?\@)
5730 (backward-char)
5731 (save-excursion
5732 (verilog-backward-token)
5733 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
5734 ((= (preceding-char) ?\#)
5735 (backward-char))
5736 (t t)))))))
5738 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
5740 (forward-word -1)
5741 (while (= (preceding-char) ?\_)
5742 (forward-word -1))
5743 (cond
5744 ((looking-at "\\<else\\>")
5746 ((looking-at verilog-behavioral-block-beg-re)
5748 ((looking-at verilog-indent-re)
5749 nil)
5751 (let
5752 ((back (point)))
5753 (verilog-backward-syntactic-ws)
5754 (cond
5755 ((= (preceding-char) ?\:)
5756 (backward-char)
5757 (verilog-backward-syntactic-ws)
5758 (backward-sexp)
5759 (if (looking-at verilog-nameable-item-re )
5762 ((= (preceding-char) ?\#)
5763 (backward-char)
5765 ((= (preceding-char) ?\`)
5766 (backward-char)
5770 (goto-char back)
5771 t))))))))
5773 (defun verilog-backward-syntactic-ws ()
5774 "Move backwards putting point after first non-whitespace non-comment."
5775 (verilog-skip-backward-comments)
5776 (forward-comment (- (buffer-size))))
5778 (defun verilog-backward-syntactic-ws-quick ()
5779 "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
5780 (while (cond ((bobp)
5781 nil) ; Done
5782 ((> (skip-syntax-backward " ") 0)
5784 ((eq (preceding-char) ?\n) ;; \n's terminate // so aren't space syntax
5785 (forward-char -1)
5787 ((or (verilog-inside-comment-or-string-p (1- (point)))
5788 (verilog-inside-comment-or-string-p (point)))
5789 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
5790 t))))
5792 (defun verilog-forward-syntactic-ws ()
5793 (verilog-skip-forward-comment-p)
5794 (forward-comment (buffer-size)))
5796 (defun verilog-backward-ws&directives (&optional bound)
5797 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
5798 Optional BOUND limits search."
5799 (save-restriction
5800 (let* ((bound (or bound (point-min)))
5801 (here bound)
5802 (p nil) )
5803 (if (< bound (point))
5804 (progn
5805 (let ((state (save-excursion (verilog-syntax-ppss))))
5806 (cond
5807 ((nth 7 state) ;; in // comment
5808 (verilog-re-search-backward "//" nil 'move)
5809 (skip-chars-backward "/"))
5810 ((nth 4 state) ;; in /* */ comment
5811 (verilog-re-search-backward "/\*" nil 'move))))
5812 (narrow-to-region bound (point))
5813 (while (/= here (point))
5814 (setq here (point))
5815 (verilog-skip-backward-comments)
5816 (setq p
5817 (save-excursion
5818 (beginning-of-line)
5819 (cond
5820 ((and verilog-highlight-translate-off
5821 (verilog-within-translate-off))
5822 (verilog-back-to-start-translate-off (point-min)))
5823 ((looking-at verilog-directive-re-1)
5824 (point))
5826 nil))))
5827 (if p (goto-char p))))))))
5829 (defun verilog-forward-ws&directives (&optional bound)
5830 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
5831 Optional BOUND limits search."
5832 (save-restriction
5833 (let* ((bound (or bound (point-max)))
5834 (here bound)
5835 jump)
5836 (if (> bound (point))
5837 (progn
5838 (let ((state (save-excursion (verilog-syntax-ppss))))
5839 (cond
5840 ((nth 7 state) ;; in // comment
5841 (end-of-line)
5842 (forward-char 1)
5843 (skip-chars-forward " \t\n\f")
5845 ((nth 4 state) ;; in /* */ comment
5846 (verilog-re-search-forward "\*\/\\s-*" nil 'move))))
5847 (narrow-to-region (point) bound)
5848 (while (/= here (point))
5849 (setq here (point)
5850 jump nil)
5851 (forward-comment (buffer-size))
5852 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ;; Attribute
5853 (goto-char (match-end 0)))
5854 (save-excursion
5855 (beginning-of-line)
5856 (if (looking-at verilog-directive-re-1)
5857 (setq jump t)))
5858 (if jump
5859 (beginning-of-line 2))))))))
5861 (defun verilog-in-comment-p ()
5862 "Return true if in a star or // comment."
5863 (let ((state (save-excursion (verilog-syntax-ppss))))
5864 (or (nth 4 state) (nth 7 state))))
5866 (defun verilog-in-star-comment-p ()
5867 "Return true if in a star comment."
5868 (let ((state (save-excursion (verilog-syntax-ppss))))
5869 (and
5870 (nth 4 state) ; t if in a comment of style a // or b /**/
5871 (not
5872 (nth 7 state) ; t if in a comment of style b /**/
5873 ))))
5875 (defun verilog-in-slash-comment-p ()
5876 "Return true if in a slash comment."
5877 (let ((state (save-excursion (verilog-syntax-ppss))))
5878 (nth 7 state)))
5880 (defun verilog-in-comment-or-string-p ()
5881 "Return true if in a string or comment."
5882 (let ((state (save-excursion (verilog-syntax-ppss))))
5883 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
5885 (defun verilog-in-attribute-p ()
5886 "Return true if point is in an attribute (* [] attribute *)."
5887 (save-match-data
5888 (save-excursion
5889 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
5890 (numberp (match-beginning 1)))))
5892 (defun verilog-in-parameter-p ()
5893 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
5894 (save-match-data
5895 (save-excursion
5896 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
5897 (numberp (match-beginning 1)))))
5899 (defun verilog-in-escaped-name-p ()
5900 "Return true if in an escaped name."
5901 (save-excursion
5902 (backward-char)
5903 (skip-chars-backward "^ \t\n\f")
5904 (if (equal (char-after (point) ) ?\\ )
5906 nil)))
5907 (defun verilog-in-directive-p ()
5908 "Return true if in a directive."
5909 (save-excursion
5910 (beginning-of-line)
5911 (looking-at verilog-directive-re-1)))
5913 (defun verilog-in-parenthesis-p ()
5914 "Return true if in a ( ) expression (but not { } or [ ])."
5915 (save-match-data
5916 (save-excursion
5917 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
5918 (numberp (match-beginning 1)))))
5920 (defun verilog-in-paren ()
5921 "Return true if in a parenthetical expression.
5922 May cache result using `verilog-syntax-ppss'."
5923 (let ((state (save-excursion (verilog-syntax-ppss))))
5924 (> (nth 0 state) 0 )))
5926 (defun verilog-in-paren-quick ()
5927 "Return true if in a parenthetical expression.
5928 Always starts from `point-min', to allow inserts with hooks disabled."
5929 ;; The -quick refers to its use alongside the other -quick functions,
5930 ;; not that it's likely to be faster than verilog-in-paren.
5931 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
5932 (> (nth 0 state) 0 )))
5934 (defun verilog-in-struct-p ()
5935 "Return true if in a struct declaration."
5936 (interactive)
5937 (save-excursion
5938 (if (verilog-in-paren)
5939 (progn
5940 (verilog-backward-up-list 1)
5941 (verilog-at-struct-p)
5943 nil)))
5945 (defun verilog-in-coverage-p ()
5946 "Return true if in a constraint or coverpoint expression."
5947 (interactive)
5948 (save-excursion
5949 (if (verilog-in-paren)
5950 (progn
5951 (verilog-backward-up-list 1)
5952 (verilog-at-constraint-p)
5954 nil)))
5955 (defun verilog-at-close-constraint-p ()
5956 "If at the } that closes a constraint or covergroup, return true."
5957 (if (and
5958 (equal (char-after) ?\})
5959 (verilog-in-paren))
5961 (save-excursion
5962 (verilog-backward-ws&directives)
5963 (if (equal (char-before) ?\;)
5964 (point)
5965 nil))))
5967 (defun verilog-at-constraint-p ()
5968 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
5969 (if (save-excursion
5970 (and
5971 (equal (char-after) ?\{)
5972 (forward-list)
5973 (progn (backward-char 1)
5974 (verilog-backward-ws&directives)
5975 (equal (char-before) ?\;))))
5976 ;; maybe
5977 (verilog-re-search-backward "\\<constraint\\|coverpoint\\|cross\\>" nil 'move)
5978 ;; not
5979 nil))
5981 (defun verilog-at-struct-p ()
5982 "If at the { of a struct, return true, moving point to struct."
5983 (save-excursion
5984 (if (and (equal (char-after) ?\{)
5985 (verilog-backward-token))
5986 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
5987 nil)))
5989 (defun verilog-parenthesis-depth ()
5990 "Return non zero if in parenthetical-expression."
5991 (save-excursion (nth 1 (verilog-syntax-ppss))))
5994 (defun verilog-skip-forward-comment-or-string ()
5995 "Return true if in a string or comment."
5996 (let ((state (save-excursion (verilog-syntax-ppss))))
5997 (cond
5998 ((nth 3 state) ;Inside string
5999 (search-forward "\"")
6001 ((nth 7 state) ;Inside // comment
6002 (forward-line 1)
6004 ((nth 4 state) ;Inside any comment (hence /**/)
6005 (search-forward "*/"))
6007 nil))))
6009 (defun verilog-skip-backward-comment-or-string ()
6010 "Return true if in a string or comment."
6011 (let ((state (save-excursion (verilog-syntax-ppss))))
6012 (cond
6013 ((nth 3 state) ;Inside string
6014 (search-backward "\"")
6016 ((nth 7 state) ;Inside // comment
6017 (search-backward "//")
6018 (skip-chars-backward "/")
6020 ((nth 4 state) ;Inside /* */ comment
6021 (search-backward "/*")
6024 nil))))
6026 (defun verilog-skip-backward-comments ()
6027 "Return true if a comment was skipped."
6028 (let ((more t))
6029 (while more
6030 (setq more
6031 (let ((state (save-excursion (verilog-syntax-ppss))))
6032 (cond
6033 ((nth 7 state) ;Inside // comment
6034 (search-backward "//")
6035 (skip-chars-backward "/")
6036 (skip-chars-backward " \t\n\f")
6038 ((nth 4 state) ;Inside /* */ comment
6039 (search-backward "/*")
6040 (skip-chars-backward " \t\n\f")
6042 ((and (not (bobp))
6043 (= (char-before) ?\/)
6044 (= (char-before (1- (point))) ?\*))
6045 (goto-char (- (point) 2))
6046 t) ;; Let nth 4 state handle the rest
6047 ((and (not (bobp))
6048 (= (char-before) ?\))
6049 (= (char-before (1- (point))) ?\*))
6050 (goto-char (- (point) 2))
6051 (if (search-backward "(*" nil t)
6052 (progn
6053 (skip-chars-backward " \t\n\f")
6055 (progn
6056 (goto-char (+ (point) 2))
6057 nil)))
6059 (/= (skip-chars-backward " \t\n\f") 0))))))))
6061 (defun verilog-skip-forward-comment-p ()
6062 "If in comment, move to end and return true."
6063 (let* (h
6064 (state (save-excursion (verilog-syntax-ppss)))
6065 (skip (cond
6066 ((nth 3 state) ;Inside string
6068 ((nth 7 state) ;Inside // comment
6069 (end-of-line)
6070 (forward-char 1)
6072 ((nth 4 state) ;Inside /* comment
6073 (search-forward "*/")
6075 ((verilog-in-attribute-p) ;Inside (* attribute
6076 (search-forward "*)" nil t)
6078 (t nil))))
6079 (skip-chars-forward " \t\n\f")
6080 (while
6081 (cond
6082 ((looking-at "\\/\\*")
6083 (progn
6084 (setq h (point))
6085 (goto-char (match-end 0))
6086 (if (search-forward "*/" nil t)
6087 (progn
6088 (skip-chars-forward " \t\n\f")
6089 (setq skip 't))
6090 (progn
6091 (goto-char h)
6092 nil))))
6093 ((looking-at "(\\*")
6094 (progn
6095 (setq h (point))
6096 (goto-char (match-end 0))
6097 (if (search-forward "*)" nil t)
6098 (progn
6099 (skip-chars-forward " \t\n\f")
6100 (setq skip 't))
6101 (progn
6102 (goto-char h)
6103 nil))))
6104 (t nil)))
6105 skip))
6107 (defun verilog-indent-line-relative ()
6108 "Cheap version of indent line.
6109 Only look at a few lines to determine indent level."
6110 (interactive)
6111 (let ((indent-str)
6112 (sp (point)))
6113 (if (looking-at "^[ \t]*$")
6114 (cond ;- A blank line; No need to be too smart.
6115 ((bobp)
6116 (setq indent-str (list 'cpp 0)))
6117 ((verilog-continued-line)
6118 (let ((sp1 (point)))
6119 (if (verilog-continued-line)
6120 (progn
6121 (goto-char sp)
6122 (setq indent-str
6123 (list 'statement (verilog-current-indent-level))))
6124 (goto-char sp1)
6125 (setq indent-str (list 'block (verilog-current-indent-level)))))
6126 (goto-char sp))
6127 ((goto-char sp)
6128 (setq indent-str (verilog-calculate-indent))))
6129 (progn (skip-chars-forward " \t")
6130 (setq indent-str (verilog-calculate-indent))))
6131 (verilog-do-indent indent-str)))
6133 (defun verilog-indent-line ()
6134 "Indent for special part of code."
6135 (verilog-do-indent (verilog-calculate-indent)))
6137 (defun verilog-do-indent (indent-str)
6138 (let ((type (car indent-str))
6139 (ind (car (cdr indent-str))))
6140 (cond
6141 (; handle continued exp
6142 (eq type 'cexp)
6143 (let ((here (point)))
6144 (verilog-backward-syntactic-ws)
6145 (cond
6146 ((or
6147 (= (preceding-char) ?\,)
6148 (= (preceding-char) ?\])
6149 (save-excursion
6150 (verilog-beg-of-statement-1)
6151 (looking-at verilog-declaration-re)))
6152 (let* ( fst
6153 (val
6154 (save-excursion
6155 (backward-char 1)
6156 (verilog-beg-of-statement-1)
6157 (setq fst (point))
6158 (if (looking-at verilog-declaration-re)
6159 (progn ;; we have multiple words
6160 (goto-char (match-end 0))
6161 (skip-chars-forward " \t")
6162 (cond
6163 ((and verilog-indent-declaration-macros
6164 (= (following-char) ?\`))
6165 (progn
6166 (forward-char 1)
6167 (forward-word 1)
6168 (skip-chars-forward " \t")))
6169 ((= (following-char) ?\[)
6170 (progn
6171 (forward-char 1)
6172 (verilog-backward-up-list -1)
6173 (skip-chars-forward " \t"))))
6174 (current-column))
6175 (progn
6176 (goto-char fst)
6177 (+ (current-column) verilog-cexp-indent))))))
6178 (goto-char here)
6179 (indent-line-to val)
6180 (if (and (not verilog-indent-lists)
6181 (verilog-in-paren))
6182 (verilog-pretty-declarations-auto))
6184 ((= (preceding-char) ?\) )
6185 (goto-char here)
6186 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6187 (indent-line-to val)))
6189 (goto-char here)
6190 (let ((val))
6191 (verilog-beg-of-statement-1)
6192 (if (and (< (point) here)
6193 (verilog-re-search-forward "=[ \\t]*" here 'move))
6194 (setq val (current-column))
6195 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6196 (goto-char here)
6197 (indent-line-to val))))))
6199 (; handle inside parenthetical expressions
6200 (eq type 'cparenexp)
6201 (let* ( here
6202 (val (save-excursion
6203 (verilog-backward-up-list 1)
6204 (forward-char 1)
6205 (if verilog-indent-lists
6206 (skip-chars-forward " \t")
6207 (verilog-forward-syntactic-ws))
6208 (setq here (point))
6209 (current-column)))
6211 (decl (save-excursion
6212 (goto-char here)
6213 (verilog-forward-syntactic-ws)
6214 (setq here (point))
6215 (looking-at verilog-declaration-re))))
6216 (indent-line-to val)
6217 (if decl
6218 (verilog-pretty-declarations-auto))))
6220 (;-- Handle the ends
6222 (looking-at verilog-end-block-re )
6223 (verilog-at-close-constraint-p))
6224 (let ((val (if (eq type 'statement)
6225 (- ind verilog-indent-level)
6226 ind)))
6227 (indent-line-to val)))
6229 (;-- Case -- maybe line 'em up
6230 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6231 (progn
6232 (cond
6233 ((looking-at "\\<endcase\\>")
6234 (indent-line-to ind))
6236 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6237 (indent-line-to val))))))
6239 (;-- defun
6240 (and (eq type 'defun)
6241 (looking-at verilog-zero-indent-re))
6242 (indent-line-to 0))
6244 (;-- declaration
6245 (and (or
6246 (eq type 'defun)
6247 (eq type 'block))
6248 (looking-at verilog-declaration-re))
6249 (verilog-indent-declaration ind))
6251 (;-- Everything else
6253 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6254 (indent-line-to val))))
6256 (if (looking-at "[ \t]+$")
6257 (skip-chars-forward " \t"))
6258 indent-str ; Return indent data
6261 (defun verilog-current-indent-level ()
6262 "Return the indent-level of the current statement."
6263 (save-excursion
6264 (let (par-pos)
6265 (beginning-of-line)
6266 (setq par-pos (verilog-parenthesis-depth))
6267 (while par-pos
6268 (goto-char par-pos)
6269 (beginning-of-line)
6270 (setq par-pos (verilog-parenthesis-depth)))
6271 (skip-chars-forward " \t")
6272 (current-column))))
6274 (defun verilog-case-indent-level ()
6275 "Return the indent-level of the current statement.
6276 Do not count named blocks or case-statements."
6277 (save-excursion
6278 (skip-chars-forward " \t")
6279 (cond
6280 ((looking-at verilog-named-block-re)
6281 (current-column))
6282 ((and (not (looking-at verilog-extended-case-re))
6283 (looking-at "^[^:;]+[ \t]*:"))
6284 (verilog-re-search-forward ":" nil t)
6285 (skip-chars-forward " \t")
6286 (current-column))
6288 (current-column)))))
6290 (defun verilog-indent-comment ()
6291 "Indent current line as comment."
6292 (let* ((stcol
6293 (cond
6294 ((verilog-in-star-comment-p)
6295 (save-excursion
6296 (re-search-backward "/\\*" nil t)
6297 (1+(current-column))))
6298 (comment-column
6299 comment-column )
6301 (save-excursion
6302 (re-search-backward "//" nil t)
6303 (current-column))))))
6304 (indent-line-to stcol)
6305 stcol))
6307 (defun verilog-more-comment ()
6308 "Make more comment lines like the previous."
6309 (let* ((star 0)
6310 (stcol
6311 (cond
6312 ((verilog-in-star-comment-p)
6313 (save-excursion
6314 (setq star 1)
6315 (re-search-backward "/\\*" nil t)
6316 (1+(current-column))))
6317 (comment-column
6318 comment-column )
6320 (save-excursion
6321 (re-search-backward "//" nil t)
6322 (current-column))))))
6323 (progn
6324 (indent-to stcol)
6325 (if (and star
6326 (save-excursion
6327 (forward-line -1)
6328 (skip-chars-forward " \t")
6329 (looking-at "\*")))
6330 (insert "* ")))))
6332 (defun verilog-comment-indent (&optional arg)
6333 "Return the column number the line should be indented to.
6334 ARG is ignored, for `comment-indent-function' compatibility."
6335 (cond
6336 ((verilog-in-star-comment-p)
6337 (save-excursion
6338 (re-search-backward "/\\*" nil t)
6339 (1+(current-column))))
6340 ( comment-column
6341 comment-column )
6343 (save-excursion
6344 (re-search-backward "//" nil t)
6345 (current-column)))))
6349 (defun verilog-pretty-declarations-auto (&optional quiet)
6350 "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
6351 (when (or (eq 'all verilog-auto-lineup)
6352 (eq 'declarations verilog-auto-lineup))
6353 (verilog-pretty-declarations quiet)))
6355 (defun verilog-pretty-declarations (&optional quiet)
6356 "Line up declarations around point.
6357 Be verbose about progress unless optional QUIET set."
6358 (interactive)
6359 (let* ((m1 (make-marker))
6360 (e (point))
6363 (here (point))
6365 start
6366 startpos
6368 endpos
6369 base-ind
6371 (save-excursion
6372 (if (progn
6373 ; (verilog-beg-of-statement-1)
6374 (beginning-of-line)
6375 (verilog-forward-syntactic-ws)
6376 (and (not (verilog-in-directive-p)) ;; could have `define input foo
6377 (looking-at verilog-declaration-re)))
6378 (progn
6379 (if (verilog-parenthesis-depth)
6380 ;; in an argument list or parameter block
6381 (setq el (verilog-backward-up-list -1)
6382 start (progn
6383 (goto-char e)
6384 (verilog-backward-up-list 1)
6385 (forward-line) ;; ignore ( input foo,
6386 (verilog-re-search-forward verilog-declaration-re el 'move)
6387 (goto-char (match-beginning 0))
6388 (skip-chars-backward " \t")
6389 (point))
6390 startpos (set-marker (make-marker) start)
6391 end (progn
6392 (goto-char start)
6393 (verilog-backward-up-list -1)
6394 (forward-char -1)
6395 (verilog-backward-syntactic-ws)
6396 (point))
6397 endpos (set-marker (make-marker) end)
6398 base-ind (progn
6399 (goto-char start)
6400 (forward-char 1)
6401 (skip-chars-forward " \t")
6402 (current-column))
6404 ;; in a declaration block (not in argument list)
6405 (setq
6406 start (progn
6407 (verilog-beg-of-statement-1)
6408 (while (and (looking-at verilog-declaration-re)
6409 (not (bobp)))
6410 (skip-chars-backward " \t")
6411 (setq e (point))
6412 (beginning-of-line)
6413 (verilog-backward-syntactic-ws)
6414 (backward-char)
6415 (verilog-beg-of-statement-1))
6417 startpos (set-marker (make-marker) start)
6418 end (progn
6419 (goto-char here)
6420 (verilog-end-of-statement)
6421 (setq e (point)) ;Might be on last line
6422 (verilog-forward-syntactic-ws)
6423 (while (looking-at verilog-declaration-re)
6424 (verilog-end-of-statement)
6425 (setq e (point))
6426 (verilog-forward-syntactic-ws))
6428 endpos (set-marker (make-marker) end)
6429 base-ind (progn
6430 (goto-char start)
6431 (verilog-do-indent (verilog-calculate-indent))
6432 (verilog-forward-ws&directives)
6433 (current-column))))
6434 ;; OK, start and end are set
6435 (goto-char (marker-position startpos))
6436 (if (and (not quiet)
6437 (> (- end start) 100))
6438 (message "Lining up declarations..(please stand by)"))
6439 ;; Get the beginning of line indent first
6440 (while (progn (setq e (marker-position endpos))
6441 (< (point) e))
6442 (cond
6443 ((save-excursion (skip-chars-backward " \t")
6444 (bolp))
6445 (verilog-forward-ws&directives)
6446 (indent-line-to base-ind)
6447 (verilog-forward-ws&directives)
6448 (if (< (point) e)
6449 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6451 (just-one-space)
6452 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6453 ;;(forward-line)
6455 ;; Now find biggest prefix
6456 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6457 ;; Now indent each line.
6458 (goto-char (marker-position startpos))
6459 (while (progn (setq e (marker-position endpos))
6460 (setq r (- e (point)))
6461 (> r 0))
6462 (setq e (point))
6463 (unless quiet (message "%d" r))
6464 ;;(verilog-do-indent (verilog-calculate-indent)))
6465 (verilog-forward-ws&directives)
6466 (cond
6467 ((or (and verilog-indent-declaration-macros
6468 (looking-at verilog-declaration-re-2-macro))
6469 (looking-at verilog-declaration-re-2-no-macro))
6470 (let ((p (match-end 0)))
6471 (set-marker m1 p)
6472 (if (verilog-re-search-forward "[[#`]" p 'move)
6473 (progn
6474 (forward-char -1)
6475 (just-one-space)
6476 (goto-char (marker-position m1))
6477 (just-one-space)
6478 (indent-to ind))
6479 (progn
6480 (just-one-space)
6481 (indent-to ind)))))
6482 ((verilog-continued-line-1 (marker-position startpos))
6483 (goto-char e)
6484 (indent-line-to ind))
6485 ((verilog-in-struct-p)
6486 ;; could have a declaration of a user defined item
6487 (goto-char e)
6488 (verilog-end-of-statement))
6489 (t ; Must be comment or white space
6490 (goto-char e)
6491 (verilog-forward-ws&directives)
6492 (forward-line -1)))
6493 (forward-line 1))
6494 (unless quiet (message "")))))))
6496 (defun verilog-pretty-expr (&optional quiet myre)
6497 "Line up expressions around point, optionally QUIET with regexp MYRE ignored."
6498 (interactive)
6499 (if (not (verilog-in-comment-or-string-p))
6500 (save-excursion
6501 (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
6502 (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
6503 (beginning-of-line)
6504 (if (and (not (looking-at rexp ))
6505 (looking-at verilog-assignment-operation-re)
6506 (save-excursion
6507 (goto-char (match-end 2))
6508 (and (not (verilog-in-attribute-p))
6509 (not (verilog-in-parameter-p))
6510 (not (verilog-in-comment-or-string-p)))))
6511 (let* ((here (point))
6512 (e) (r)
6513 (start
6514 (progn
6515 (beginning-of-line)
6516 (setq e (point))
6517 (verilog-backward-syntactic-ws)
6518 (beginning-of-line)
6519 (while (and (not (looking-at rexp1))
6520 (looking-at verilog-assignment-operation-re)
6521 (not (bobp))
6523 (setq e (point))
6524 (verilog-backward-syntactic-ws)
6525 (beginning-of-line)
6526 ) ;Ack, need to grok `define
6528 (end
6529 (progn
6530 (goto-char here)
6531 (end-of-line)
6532 (setq e (point)) ;Might be on last line
6533 (verilog-forward-syntactic-ws)
6534 (beginning-of-line)
6535 (while (and
6536 (not (looking-at rexp1 ))
6537 (looking-at verilog-assignment-operation-re)
6538 (progn
6539 (end-of-line)
6540 (not (eq e (point)))))
6541 (setq e (point))
6542 (verilog-forward-syntactic-ws)
6543 (beginning-of-line)
6546 (endpos (set-marker (make-marker) end))
6547 (ind)
6549 (goto-char start)
6550 (verilog-do-indent (verilog-calculate-indent))
6551 (if (and (not quiet)
6552 (> (- end start) 100))
6553 (message "Lining up expressions..(please stand by)"))
6555 ;; Set indent to minimum throughout region
6556 (while (< (point) (marker-position endpos))
6557 (beginning-of-line)
6558 (verilog-just-one-space verilog-assignment-operation-re)
6559 (beginning-of-line)
6560 (verilog-do-indent (verilog-calculate-indent))
6561 (end-of-line)
6562 (verilog-forward-syntactic-ws)
6565 ;; Now find biggest prefix
6566 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6568 ;; Now indent each line.
6569 (goto-char start)
6570 (while (progn (setq e (marker-position endpos))
6571 (setq r (- e (point)))
6572 (> r 0))
6573 (setq e (point))
6574 (if (not quiet) (message "%d" r))
6575 (cond
6576 ((looking-at verilog-assignment-operation-re)
6577 (goto-char (match-beginning 2))
6578 (if (not (or (verilog-in-parenthesis-p) ;; leave attributes and comparisons alone
6579 (verilog-in-coverage-p)))
6580 (if (eq (char-after) ?=)
6581 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6582 (indent-to ind)
6585 ((verilog-continued-line-1 start)
6586 (goto-char e)
6587 (indent-line-to ind))
6588 (t ; Must be comment or white space
6589 (goto-char e)
6590 (verilog-forward-ws&directives)
6591 (forward-line -1))
6593 (forward-line 1))
6594 (unless quiet (message ""))
6595 ))))))
6597 (defun verilog-just-one-space (myre)
6598 "Remove extra spaces around regular expression MYRE."
6599 (interactive)
6600 (if (and (not(looking-at verilog-complete-reg))
6601 (looking-at myre))
6602 (let ((p1 (match-end 1))
6603 (p2 (match-end 2)))
6604 (progn
6605 (goto-char p2)
6606 (just-one-space)
6607 (goto-char p1)
6608 (just-one-space)))))
6610 (defun verilog-indent-declaration (baseind)
6611 "Indent current lines as declaration.
6612 Line up the variable names based on previous declaration's indentation.
6613 BASEIND is the base indent to offset everything."
6614 (interactive)
6615 (let ((pos (point-marker))
6616 (lim (save-excursion
6617 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
6618 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
6619 (point)))
6620 (ind)
6621 (val)
6622 (m1 (make-marker)))
6623 (setq val
6624 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6625 (indent-line-to val)
6627 ;; Use previous declaration (in this module) as template.
6628 (if (or (eq 'all verilog-auto-lineup)
6629 (eq 'declarations verilog-auto-lineup))
6630 (if (verilog-re-search-backward
6631 (or (and verilog-indent-declaration-macros
6632 verilog-declaration-re-1-macro)
6633 verilog-declaration-re-1-no-macro) lim t)
6634 (progn
6635 (goto-char (match-end 0))
6636 (skip-chars-forward " \t")
6637 (setq ind (current-column))
6638 (goto-char pos)
6639 (setq val
6640 (+ baseind
6641 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6642 (indent-line-to val)
6643 (if (and verilog-indent-declaration-macros
6644 (looking-at verilog-declaration-re-2-macro))
6645 (let ((p (match-end 0)))
6646 (set-marker m1 p)
6647 (if (verilog-re-search-forward "[[#`]" p 'move)
6648 (progn
6649 (forward-char -1)
6650 (just-one-space)
6651 (goto-char (marker-position m1))
6652 (just-one-space)
6653 (indent-to ind))
6654 (if (/= (current-column) ind)
6655 (progn
6656 (just-one-space)
6657 (indent-to ind)))))
6658 (if (looking-at verilog-declaration-re-2-no-macro)
6659 (let ((p (match-end 0)))
6660 (set-marker m1 p)
6661 (if (verilog-re-search-forward "[[`#]" p 'move)
6662 (progn
6663 (forward-char -1)
6664 (just-one-space)
6665 (goto-char (marker-position m1))
6666 (just-one-space)
6667 (indent-to ind))
6668 (if (/= (current-column) ind)
6669 (progn
6670 (just-one-space)
6671 (indent-to ind))))))))))
6672 (goto-char pos)))
6674 (defun verilog-get-lineup-indent (b edpos)
6675 "Return the indent level that will line up several lines within the region.
6676 Region is defined by B and EDPOS."
6677 (save-excursion
6678 (let ((ind 0) e)
6679 (goto-char b)
6680 ;; Get rightmost position
6681 (while (progn (setq e (marker-position edpos))
6682 (< (point) e))
6683 (if (verilog-re-search-forward
6684 (or (and verilog-indent-declaration-macros
6685 verilog-declaration-re-1-macro)
6686 verilog-declaration-re-1-no-macro) e 'move)
6687 (progn
6688 (goto-char (match-end 0))
6689 (verilog-backward-syntactic-ws)
6690 (if (> (current-column) ind)
6691 (setq ind (current-column)))
6692 (goto-char (match-end 0)))))
6693 (if (> ind 0)
6694 (1+ ind)
6695 ;; No lineup-string found
6696 (goto-char b)
6697 (end-of-line)
6698 (verilog-backward-syntactic-ws)
6699 ;;(skip-chars-backward " \t")
6700 (1+ (current-column))))))
6702 (defun verilog-get-lineup-indent-2 (myre b edpos)
6703 "Return the indent level that will line up several lines within the region."
6704 (save-excursion
6705 (let ((ind 0) e)
6706 (goto-char b)
6707 ;; Get rightmost position
6708 (while (progn (setq e (marker-position edpos))
6709 (< (point) e))
6710 (if (and (verilog-re-search-forward myre e 'move)
6711 (not (verilog-in-attribute-p))) ;; skip attribute exprs
6712 (progn
6713 (goto-char (match-beginning 2))
6714 (verilog-backward-syntactic-ws)
6715 (if (> (current-column) ind)
6716 (setq ind (current-column)))
6717 (goto-char (match-end 0)))
6719 (if (> ind 0)
6720 (1+ ind)
6721 ;; No lineup-string found
6722 (goto-char b)
6723 (end-of-line)
6724 (skip-chars-backward " \t")
6725 (1+ (current-column))))))
6727 (defun verilog-comment-depth (type val)
6728 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
6729 (save-excursion
6730 (let
6731 ((b (prog2
6732 (beginning-of-line)
6733 (point-marker)
6734 (end-of-line)))
6735 (e (point-marker)))
6736 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
6737 (progn
6738 (replace-match " /* -# ## */")
6739 (end-of-line))
6740 (progn
6741 (end-of-line)
6742 (insert " /* ## ## */"))))
6743 (backward-char 6)
6744 (insert
6745 (format "%s %d" type val))))
6747 ;; \f
6749 ;; Completion
6751 (defvar verilog-str nil)
6752 (defvar verilog-all nil)
6753 (defvar verilog-pred nil)
6754 (defvar verilog-buffer-to-use nil)
6755 (defvar verilog-flag nil)
6756 (defvar verilog-toggle-completions nil
6757 "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
6758 Repeated use of \\[verilog-complete-word] will show you all of them.
6759 Normally, when there is more than one possible completion,
6760 it displays a list of all possible completions.")
6763 (defvar verilog-type-keywords
6765 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
6766 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
6767 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
6768 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
6769 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
6770 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
6772 "Keywords for types used when completing a word in a declaration or parmlist.
6773 \(integer, real, reg...)")
6775 (defvar verilog-cpp-keywords
6776 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
6777 "endif")
6778 "Keywords to complete when at first word of a line in declarative scope.
6779 \(initial, always, begin, assign...)
6780 The procedures and variables defined within the Verilog program
6781 will be completed at runtime and should not be added to this list.")
6783 (defvar verilog-defun-keywords
6784 (append
6786 "always" "always_comb" "always_ff" "always_latch" "assign"
6787 "begin" "end" "generate" "endgenerate" "module" "endmodule"
6788 "specify" "endspecify" "function" "endfunction" "initial" "final"
6789 "task" "endtask" "primitive" "endprimitive"
6791 verilog-type-keywords)
6792 "Keywords to complete when at first word of a line in declarative scope.
6793 \(initial, always, begin, assign...)
6794 The procedures and variables defined within the Verilog program
6795 will be completed at runtime and should not be added to this list.")
6797 (defvar verilog-block-keywords
6799 "begin" "break" "case" "continue" "else" "end" "endfunction"
6800 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
6801 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
6802 "while")
6803 "Keywords to complete when at first word of a line in behavioral scope.
6804 \(begin, if, then, else, for, fork...)
6805 The procedures and variables defined within the Verilog program
6806 will be completed at runtime and should not be added to this list.")
6808 (defvar verilog-tf-keywords
6809 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
6810 "Keywords to complete when at first word of a line in a task or function.
6811 \(begin, if, then, else, for, fork.)
6812 The procedures and variables defined within the Verilog program
6813 will be completed at runtime and should not be added to this list.")
6815 (defvar verilog-case-keywords
6816 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
6817 "Keywords to complete when at first word of a line in case scope.
6818 \(begin, if, then, else, for, fork...)
6819 The procedures and variables defined within the Verilog program
6820 will be completed at runtime and should not be added to this list.")
6822 (defvar verilog-separator-keywords
6823 '("else" "then" "begin")
6824 "Keywords to complete when NOT standing at the first word of a statement.
6825 \(else, then, begin...)
6826 Variables and function names defined within the Verilog program
6827 will be completed at runtime and should not be added to this list.")
6829 (defvar verilog-gate-ios
6830 ;; All these have an implied {"input"...} at the end
6831 '(("and" "output")
6832 ("buf" "output")
6833 ("bufif0" "output")
6834 ("bufif1" "output")
6835 ("cmos" "output")
6836 ("nand" "output")
6837 ("nmos" "output")
6838 ("nor" "output")
6839 ("not" "output")
6840 ("notif0" "output")
6841 ("notif1" "output")
6842 ("or" "output")
6843 ("pmos" "output")
6844 ("pulldown" "output")
6845 ("pullup" "output")
6846 ("rcmos" "output")
6847 ("rnmos" "output")
6848 ("rpmos" "output")
6849 ("rtran" "inout" "inout")
6850 ("rtranif0" "inout" "inout")
6851 ("rtranif1" "inout" "inout")
6852 ("tran" "inout" "inout")
6853 ("tranif0" "inout" "inout")
6854 ("tranif1" "inout" "inout")
6855 ("xnor" "output")
6856 ("xor" "output"))
6857 "Map of direction for each positional argument to each gate primitive.")
6859 (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
6860 "Keywords for gate primitives.")
6862 (defun verilog-string-diff (str1 str2)
6863 "Return index of first letter where STR1 and STR2 differs."
6864 (catch 'done
6865 (let ((diff 0))
6866 (while t
6867 (if (or (> (1+ diff) (length str1))
6868 (> (1+ diff) (length str2)))
6869 (throw 'done diff))
6870 (or (equal (aref str1 diff) (aref str2 diff))
6871 (throw 'done diff))
6872 (setq diff (1+ diff))))))
6874 ;; Calculate all possible completions for functions if argument is `function',
6875 ;; completions for procedures if argument is `procedure' or both functions and
6876 ;; procedures otherwise.
6878 (defun verilog-func-completion (type)
6879 "Build regular expression for module/task/function names.
6880 TYPE is 'module, 'tf for task or function, or t if unknown."
6881 (if (string= verilog-str "")
6882 (setq verilog-str "[a-zA-Z_]"))
6883 (let ((verilog-str (concat (cond
6884 ((eq type 'module) "\\<\\(module\\)\\s +")
6885 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
6886 (t "\\<\\(task\\|function\\|module\\)\\s +"))
6887 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
6888 match)
6890 (if (not (looking-at verilog-defun-re))
6891 (verilog-re-search-backward verilog-defun-re nil t))
6892 (forward-char 1)
6894 ;; Search through all reachable functions
6895 (goto-char (point-min))
6896 (while (verilog-re-search-forward verilog-str (point-max) t)
6897 (progn (setq match (buffer-substring (match-beginning 2)
6898 (match-end 2)))
6899 (if (or (null verilog-pred)
6900 (funcall verilog-pred match))
6901 (setq verilog-all (cons match verilog-all)))))
6902 (if (match-beginning 0)
6903 (goto-char (match-beginning 0)))))
6905 (defun verilog-get-completion-decl (end)
6906 "Macro for searching through current declaration (var, type or const)
6907 for matches of `str' and adding the occurrence tp `all' through point END."
6908 (let ((re (or (and verilog-indent-declaration-macros
6909 verilog-declaration-re-2-macro)
6910 verilog-declaration-re-2-no-macro))
6911 decl-end match)
6912 ;; Traverse lines
6913 (while (and (< (point) end)
6914 (verilog-re-search-forward re end t))
6915 ;; Traverse current line
6916 (setq decl-end (save-excursion (verilog-declaration-end)))
6917 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
6918 (not (match-end 1)))
6919 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
6920 (if (string-match (concat "\\<" verilog-str) match)
6921 (if (or (null verilog-pred)
6922 (funcall verilog-pred match))
6923 (setq verilog-all (cons match verilog-all)))))
6924 (forward-line 1)))
6925 verilog-all)
6927 (defun verilog-type-completion ()
6928 "Calculate all possible completions for types."
6929 (let ((start (point))
6930 goon)
6931 ;; Search for all reachable type declarations
6932 (while (or (verilog-beg-of-defun)
6933 (setq goon (not goon)))
6934 (save-excursion
6935 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
6936 (point))
6937 (forward-char 1)))
6938 (verilog-re-search-forward
6939 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
6940 start t)
6941 (not (match-end 1)))
6942 ;; Check current type declaration
6943 (verilog-get-completion-decl start))))))
6945 (defun verilog-var-completion ()
6946 "Calculate all possible completions for variables (or constants)."
6947 (let ((start (point)))
6948 ;; Search for all reachable var declarations
6949 (verilog-beg-of-defun)
6950 (save-excursion
6951 ;; Check var declarations
6952 (verilog-get-completion-decl start))))
6954 (defun verilog-keyword-completion (keyword-list)
6955 "Give list of all possible completions of keywords in KEYWORD-LIST."
6956 (mapcar (lambda (s)
6957 (if (string-match (concat "\\<" verilog-str) s)
6958 (if (or (null verilog-pred)
6959 (funcall verilog-pred s))
6960 (setq verilog-all (cons s verilog-all)))))
6961 keyword-list))
6964 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
6965 "Function passed to `completing-read', `try-completion' or `all-completions'.
6966 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
6967 must be a function to be called for every match to check if this should
6968 really be a match. If VERILOG-FLAG is t, the function returns a list of
6969 all possible completions. If VERILOG-FLAG is nil it returns a string,
6970 the longest possible completion, or t if VERILOG-STR is an exact match.
6971 If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
6972 exact match, nil otherwise."
6973 (save-excursion
6974 (let ((verilog-all nil))
6975 ;; Set buffer to use for searching labels. This should be set
6976 ;; within functions which use verilog-completions
6977 (set-buffer verilog-buffer-to-use)
6979 ;; Determine what should be completed
6980 (let ((state (car (verilog-calculate-indent))))
6981 (cond ((eq state 'defun)
6982 (save-excursion (verilog-var-completion))
6983 (verilog-func-completion 'module)
6984 (verilog-keyword-completion verilog-defun-keywords))
6986 ((eq state 'behavioral)
6987 (save-excursion (verilog-var-completion))
6988 (verilog-func-completion 'module)
6989 (verilog-keyword-completion verilog-defun-keywords))
6991 ((eq state 'block)
6992 (save-excursion (verilog-var-completion))
6993 (verilog-func-completion 'tf)
6994 (verilog-keyword-completion verilog-block-keywords))
6996 ((eq state 'case)
6997 (save-excursion (verilog-var-completion))
6998 (verilog-func-completion 'tf)
6999 (verilog-keyword-completion verilog-case-keywords))
7001 ((eq state 'tf)
7002 (save-excursion (verilog-var-completion))
7003 (verilog-func-completion 'tf)
7004 (verilog-keyword-completion verilog-tf-keywords))
7006 ((eq state 'cpp)
7007 (save-excursion (verilog-var-completion))
7008 (verilog-keyword-completion verilog-cpp-keywords))
7010 ((eq state 'cparenexp)
7011 (save-excursion (verilog-var-completion)))
7013 (t;--Anywhere else
7014 (save-excursion (verilog-var-completion))
7015 (verilog-func-completion 'both)
7016 (verilog-keyword-completion verilog-separator-keywords))))
7018 ;; Now we have built a list of all matches. Give response to caller
7019 (verilog-completion-response))))
7021 (defun verilog-completion-response ()
7022 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
7023 ;; This was not called by all-completions
7024 (if (null verilog-all)
7025 ;; Return nil if there was no matching label
7027 ;; Get longest string common in the labels
7028 (let* ((elm (cdr verilog-all))
7029 (match (car verilog-all))
7030 (min (length match))
7031 tmp)
7032 (if (string= match verilog-str)
7033 ;; Return t if first match was an exact match
7034 (setq match t)
7035 (while (not (null elm))
7036 ;; Find longest common string
7037 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
7038 (progn
7039 (setq min tmp)
7040 (setq match (substring match 0 min))))
7041 ;; Terminate with match=t if this is an exact match
7042 (if (string= (car elm) verilog-str)
7043 (progn
7044 (setq match t)
7045 (setq elm nil))
7046 (setq elm (cdr elm)))))
7047 ;; If this is a test just for exact match, return nil ot t
7048 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
7050 match))))
7051 ;; If flag is t, this was called by all-completions. Return
7052 ;; list of all possible completions
7053 (verilog-flag
7054 verilog-all)))
7056 (defvar verilog-last-word-numb 0)
7057 (defvar verilog-last-word-shown nil)
7058 (defvar verilog-last-completions nil)
7060 (defun verilog-complete-word ()
7061 "Complete word at current point.
7062 \(See also `verilog-toggle-completions', `verilog-type-keywords',
7063 and `verilog-separator-keywords'.)"
7064 (interactive)
7065 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7066 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7067 (verilog-str (buffer-substring b e))
7068 ;; The following variable is used in verilog-completion
7069 (verilog-buffer-to-use (current-buffer))
7070 (allcomp (if (and verilog-toggle-completions
7071 (string= verilog-last-word-shown verilog-str))
7072 verilog-last-completions
7073 (all-completions verilog-str 'verilog-completion)))
7074 (match (if verilog-toggle-completions
7075 "" (try-completion
7076 verilog-str (mapcar (lambda (elm)
7077 (cons elm 0)) allcomp)))))
7078 ;; Delete old string
7079 (delete-region b e)
7081 ;; Toggle-completions inserts whole labels
7082 (if verilog-toggle-completions
7083 (progn
7084 ;; Update entry number in list
7085 (setq verilog-last-completions allcomp
7086 verilog-last-word-numb
7087 (if (>= verilog-last-word-numb (1- (length allcomp)))
7089 (1+ verilog-last-word-numb)))
7090 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7091 ;; Display next match or same string if no match was found
7092 (if (not (null allcomp))
7093 (insert "" verilog-last-word-shown)
7094 (insert "" verilog-str)
7095 (message "(No match)")))
7096 ;; The other form of completion does not necessarily do that.
7098 ;; Insert match if found, or the original string if no match
7099 (if (or (null match) (equal match 't))
7100 (progn (insert "" verilog-str)
7101 (message "(No match)"))
7102 (insert "" match))
7103 ;; Give message about current status of completion
7104 (cond ((equal match 't)
7105 (if (not (null (cdr allcomp)))
7106 (message "(Complete but not unique)")
7107 (message "(Sole completion)")))
7108 ;; Display buffer if the current completion didn't help
7109 ;; on completing the label.
7110 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7111 (length match)))
7112 (with-output-to-temp-buffer "*Completions*"
7113 (display-completion-list allcomp))
7114 ;; Wait for a key press. Then delete *Completion* window
7115 (momentary-string-display "" (point))
7116 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7117 )))))
7119 (defun verilog-show-completions ()
7120 "Show all possible completions at current point."
7121 (interactive)
7122 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7123 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7124 (verilog-str (buffer-substring b e))
7125 ;; The following variable is used in verilog-completion
7126 (verilog-buffer-to-use (current-buffer))
7127 (allcomp (if (and verilog-toggle-completions
7128 (string= verilog-last-word-shown verilog-str))
7129 verilog-last-completions
7130 (all-completions verilog-str 'verilog-completion))))
7131 ;; Show possible completions in a temporary buffer.
7132 (with-output-to-temp-buffer "*Completions*"
7133 (display-completion-list allcomp))
7134 ;; Wait for a key press. Then delete *Completion* window
7135 (momentary-string-display "" (point))
7136 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7139 (defun verilog-get-default-symbol ()
7140 "Return symbol around current point as a string."
7141 (save-excursion
7142 (buffer-substring (progn
7143 (skip-chars-backward " \t")
7144 (skip-chars-backward "a-zA-Z0-9_")
7145 (point))
7146 (progn
7147 (skip-chars-forward "a-zA-Z0-9_")
7148 (point)))))
7150 (defun verilog-build-defun-re (str &optional arg)
7151 "Return function/task/module starting with STR as regular expression.
7152 With optional second ARG non-nil, STR is the complete name of the instruction."
7153 (if arg
7154 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7155 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7157 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7158 "Function passed to `completing-read', `try-completion' or `all-completions'.
7159 Returns a completion on any function name based on VERILOG-STR prefix. If
7160 VERILOG-PRED is non-nil, it must be a function to be called for every match
7161 to check if this should really be a match. If VERILOG-FLAG is t, the
7162 function returns a list of all possible completions. If it is nil it
7163 returns a string, the longest possible completion, or t if VERILOG-STR is
7164 an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
7165 VERILOG-STR is an exact match, nil otherwise."
7166 (save-excursion
7167 (let ((verilog-all nil)
7168 match)
7170 ;; Set buffer to use for searching labels. This should be set
7171 ;; within functions which use verilog-completions
7172 (set-buffer verilog-buffer-to-use)
7174 (let ((verilog-str verilog-str))
7175 ;; Build regular expression for functions
7176 (if (string= verilog-str "")
7177 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7178 (setq verilog-str (verilog-build-defun-re verilog-str)))
7179 (goto-char (point-min))
7181 ;; Build a list of all possible completions
7182 (while (verilog-re-search-forward verilog-str nil t)
7183 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7184 (if (or (null verilog-pred)
7185 (funcall verilog-pred match))
7186 (setq verilog-all (cons match verilog-all)))))
7188 ;; Now we have built a list of all matches. Give response to caller
7189 (verilog-completion-response))))
7191 (defun verilog-goto-defun ()
7192 "Move to specified Verilog module/interface/task/function.
7193 The default is a name found in the buffer around point.
7194 If search fails, other files are checked based on
7195 `verilog-library-flags'."
7196 (interactive)
7197 (let* ((default (verilog-get-default-symbol))
7198 ;; The following variable is used in verilog-comp-function
7199 (verilog-buffer-to-use (current-buffer))
7200 (label (if (not (string= default ""))
7201 ;; Do completion with default
7202 (completing-read (concat "Goto-Label: (default "
7203 default ") ")
7204 'verilog-comp-defun nil nil "")
7205 ;; There is no default value. Complete without it
7206 (completing-read "Goto-Label: "
7207 'verilog-comp-defun nil nil "")))
7209 ;; Make sure library paths are correct, in case need to resolve module
7210 (verilog-auto-reeval-locals)
7211 (verilog-getopt-flags)
7212 ;; If there was no response on prompt, use default value
7213 (if (string= label "")
7214 (setq label default))
7215 ;; Goto right place in buffer if label is not an empty string
7216 (or (string= label "")
7217 (progn
7218 (save-excursion
7219 (goto-char (point-min))
7220 (setq pt
7221 (re-search-forward (verilog-build-defun-re label t) nil t)))
7222 (when pt
7223 (goto-char pt)
7224 (beginning-of-line))
7226 (verilog-goto-defun-file label))))
7228 ;; Eliminate compile warning
7229 (defvar occur-pos-list)
7231 (defun verilog-showscopes ()
7232 "List all scopes in this module."
7233 (interactive)
7234 (let ((buffer (current-buffer))
7235 (linenum 1)
7236 (nlines 0)
7237 (first 1)
7238 (prevpos (point-min))
7239 (final-context-start (make-marker))
7240 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
7241 (with-output-to-temp-buffer "*Occur*"
7242 (save-excursion
7243 (message (format "Searching for %s ..." regexp))
7244 ;; Find next match, but give up if prev match was at end of buffer.
7245 (while (and (not (= prevpos (point-max)))
7246 (verilog-re-search-forward regexp nil t))
7247 (goto-char (match-beginning 0))
7248 (beginning-of-line)
7249 (save-match-data
7250 (setq linenum (+ linenum (count-lines prevpos (point)))))
7251 (setq prevpos (point))
7252 (goto-char (match-end 0))
7253 (let* ((start (save-excursion
7254 (goto-char (match-beginning 0))
7255 (forward-line (if (< nlines 0) nlines (- nlines)))
7256 (point)))
7257 (end (save-excursion
7258 (goto-char (match-end 0))
7259 (if (> nlines 0)
7260 (forward-line (1+ nlines))
7261 (forward-line 1))
7262 (point)))
7263 (tag (format "%3d" linenum))
7264 (empty (make-string (length tag) ?\ ))
7265 tem)
7266 (save-excursion
7267 (setq tem (make-marker))
7268 (set-marker tem (point))
7269 (set-buffer standard-output)
7270 (setq occur-pos-list (cons tem occur-pos-list))
7271 (or first (zerop nlines)
7272 (insert "--------\n"))
7273 (setq first nil)
7274 (insert-buffer-substring buffer start end)
7275 (backward-char (- end start))
7276 (setq tem (if (< nlines 0) (- nlines) nlines))
7277 (while (> tem 0)
7278 (insert empty ?:)
7279 (forward-line 1)
7280 (setq tem (1- tem)))
7281 (let ((this-linenum linenum))
7282 (set-marker final-context-start
7283 (+ (point) (- (match-end 0) (match-beginning 0))))
7284 (while (< (point) final-context-start)
7285 (if (null tag)
7286 (setq tag (format "%3d" this-linenum)))
7287 (insert tag ?:)))))))
7288 (set-buffer-modified-p nil))))
7291 ;; Highlight helper functions
7292 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7293 (defun verilog-within-translate-off ()
7294 "Return point if within translate-off region, else nil."
7295 (and (save-excursion
7296 (re-search-backward
7297 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7298 nil t))
7299 (equal "off" (match-string 2))
7300 (point)))
7302 (defun verilog-start-translate-off (limit)
7303 "Return point before translate-off directive if before LIMIT, else nil."
7304 (when (re-search-forward
7305 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7306 limit t)
7307 (match-beginning 0)))
7309 (defun verilog-back-to-start-translate-off (limit)
7310 "Return point before translate-off directive if before LIMIT, else nil."
7311 (when (re-search-backward
7312 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7313 limit t)
7314 (match-beginning 0)))
7316 (defun verilog-end-translate-off (limit)
7317 "Return point after translate-on directive if before LIMIT, else nil."
7319 (re-search-forward (concat
7320 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7322 (defun verilog-match-translate-off (limit)
7323 "Match a translate-off block, setting `match-data' and returning t, else nil.
7324 Bound search by LIMIT."
7325 (when (< (point) limit)
7326 (let ((start (or (verilog-within-translate-off)
7327 (verilog-start-translate-off limit)))
7328 (case-fold-search t))
7329 (when start
7330 (let ((end (or (verilog-end-translate-off limit) limit)))
7331 (set-match-data (list start end))
7332 (goto-char end))))))
7334 (defun verilog-font-lock-match-item (limit)
7335 "Match, and move over, any declaration item after point.
7336 Bound search by LIMIT. Adapted from
7337 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
7338 (condition-case nil
7339 (save-restriction
7340 (narrow-to-region (point-min) limit)
7341 ;; match item
7342 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7343 (save-match-data
7344 (goto-char (match-end 1))
7345 ;; move to next item
7346 (if (looking-at "\\(\\s-*,\\)")
7347 (goto-char (match-end 1))
7348 (end-of-line) t))))
7349 (error nil)))
7352 ;; Added by Subbu Meiyappan for Header
7354 (defun verilog-header ()
7355 "Insert a standard Verilog file header.
7356 See also `verilog-sk-header' for an alternative format."
7357 (interactive)
7358 (let ((start (point)))
7359 (insert "\
7360 //-----------------------------------------------------------------------------
7361 // Title : <title>
7362 // Project : <project>
7363 //-----------------------------------------------------------------------------
7364 // File : <filename>
7365 // Author : <author>
7366 // Created : <credate>
7367 // Last modified : <moddate>
7368 //-----------------------------------------------------------------------------
7369 // Description :
7370 // <description>
7371 //-----------------------------------------------------------------------------
7372 // Copyright (c) <copydate> by <company> This model is the confidential and
7373 // proprietary property of <company> and the possession or use of this
7374 // file requires a written license from <company>.
7375 //------------------------------------------------------------------------------
7376 // Modification history :
7377 // <modhist>
7378 //-----------------------------------------------------------------------------
7381 (goto-char start)
7382 (search-forward "<filename>")
7383 (replace-match (buffer-name) t t)
7384 (search-forward "<author>") (replace-match "" t t)
7385 (insert (user-full-name))
7386 (insert " <" (user-login-name) "@" (system-name) ">")
7387 (search-forward "<credate>") (replace-match "" t t)
7388 (verilog-insert-date)
7389 (search-forward "<moddate>") (replace-match "" t t)
7390 (verilog-insert-date)
7391 (search-forward "<copydate>") (replace-match "" t t)
7392 (verilog-insert-year)
7393 (search-forward "<modhist>") (replace-match "" t t)
7394 (verilog-insert-date)
7395 (insert " : created")
7396 (goto-char start)
7397 (let (string)
7398 (setq string (read-string "title: "))
7399 (search-forward "<title>")
7400 (replace-match string t t)
7401 (setq string (read-string "project: " verilog-project))
7402 (setq verilog-project string)
7403 (search-forward "<project>")
7404 (replace-match string t t)
7405 (setq string (read-string "Company: " verilog-company))
7406 (setq verilog-company string)
7407 (search-forward "<company>")
7408 (replace-match string t t)
7409 (search-forward "<company>")
7410 (replace-match string t t)
7411 (search-forward "<company>")
7412 (replace-match string t t)
7413 (search-backward "<description>")
7414 (replace-match "" t t))))
7416 ;; verilog-header Uses the verilog-insert-date function
7418 (defun verilog-insert-date ()
7419 "Insert date from the system."
7420 (interactive)
7421 (if verilog-date-scientific-format
7422 (insert (format-time-string "%Y/%m/%d"))
7423 (insert (format-time-string "%d.%m.%Y"))))
7425 (defun verilog-insert-year ()
7426 "Insert year from the system."
7427 (interactive)
7428 (insert (format-time-string "%Y")))
7432 ;; Signal list parsing
7435 ;; Elements of a signal list
7436 ;; Unfortunately we use 'assoc' on this, so can't be a vector
7437 (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7438 (list name bits comment mem enum signed type multidim modport))
7439 (defsubst verilog-sig-name (sig)
7440 (car sig))
7441 (defsubst verilog-sig-bits (sig)
7442 (nth 1 sig))
7443 (defsubst verilog-sig-comment (sig)
7444 (nth 2 sig))
7445 (defsubst verilog-sig-memory (sig)
7446 (nth 3 sig))
7447 (defsubst verilog-sig-enum (sig)
7448 (nth 4 sig))
7449 (defsubst verilog-sig-signed (sig)
7450 (nth 5 sig))
7451 (defsubst verilog-sig-type (sig)
7452 (nth 6 sig))
7453 (defsubst verilog-sig-type-set (sig type)
7454 (setcar (nthcdr 6 sig) type))
7455 (defsubst verilog-sig-multidim (sig)
7456 (nth 7 sig))
7457 (defsubst verilog-sig-multidim-string (sig)
7458 (if (verilog-sig-multidim sig)
7459 (let ((str "") (args (verilog-sig-multidim sig)))
7460 (while args
7461 (setq str (concat str (car args)))
7462 (setq args (cdr args)))
7463 str)))
7464 (defsubst verilog-sig-modport (sig)
7465 (nth 8 sig))
7466 (defsubst verilog-sig-width (sig)
7467 (verilog-make-width-expression (verilog-sig-bits sig)))
7469 (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7470 (vector outputs-del outputs-imm temps inputs))
7471 (defsubst verilog-alw-get-outputs-delayed (sigs)
7472 (aref sigs 0))
7473 (defsubst verilog-alw-get-outputs-immediate (sigs)
7474 (aref sigs 1))
7475 (defsubst verilog-alw-get-temps (sigs)
7476 (aref sigs 2))
7477 (defsubst verilog-alw-get-inputs (sigs)
7478 (aref sigs 3))
7479 (defsubst verilog-alw-get-uses-delayed (sigs)
7480 (aref sigs 0))
7482 (defsubst verilog-modi-new (name fob pt type)
7483 (vector name fob pt type))
7484 (defsubst verilog-modi-name (modi)
7485 (aref modi 0))
7486 (defsubst verilog-modi-file-or-buffer (modi)
7487 (aref modi 1))
7488 (defsubst verilog-modi-get-point (modi)
7489 (aref modi 2))
7490 (defsubst verilog-modi-get-type (modi) ;; "module" or "interface"
7491 (aref modi 3))
7492 (defsubst verilog-modi-get-decls (modi)
7493 (verilog-modi-cache-results modi 'verilog-read-decls))
7494 (defsubst verilog-modi-get-sub-decls (modi)
7495 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7497 ;; Signal reading for given module
7498 ;; Note these all take modi's - as returned from verilog-modi-current
7499 (defsubst verilog-decls-new (out inout in vars unuseds assigns consts gparams interfaces)
7500 (vector out inout in vars unuseds assigns consts gparams interfaces))
7501 (defsubst verilog-decls-get-outputs (decls)
7502 (aref decls 0))
7503 (defsubst verilog-decls-get-inouts (decls)
7504 (aref decls 1))
7505 (defsubst verilog-decls-get-inputs (decls)
7506 (aref decls 2))
7507 (defsubst verilog-decls-get-vars (decls)
7508 (aref decls 3))
7509 ;;(defsubst verilog-decls-get-unused (decls)
7510 ;; (aref decls 4))
7511 (defsubst verilog-decls-get-assigns (decls)
7512 (aref decls 5))
7513 (defsubst verilog-decls-get-consts (decls)
7514 (aref decls 6))
7515 (defsubst verilog-decls-get-gparams (decls)
7516 (aref decls 7))
7517 (defsubst verilog-decls-get-interfaces (decls)
7518 (aref decls 8))
7520 (defsubst verilog-subdecls-new (out inout in intf intfd)
7521 (vector out inout in intf intfd))
7522 (defsubst verilog-subdecls-get-outputs (subdecls)
7523 (aref subdecls 0))
7524 (defsubst verilog-subdecls-get-inouts (subdecls)
7525 (aref subdecls 1))
7526 (defsubst verilog-subdecls-get-inputs (subdecls)
7527 (aref subdecls 2))
7528 (defsubst verilog-subdecls-get-interfaces (subdecls)
7529 (aref subdecls 3))
7530 (defsubst verilog-subdecls-get-interfaced (subdecls)
7531 (aref subdecls 4))
7533 (defun verilog-signals-from-signame (signame-list)
7534 "Return signals in standard form from SIGNAME-LIST, a simple list of names."
7535 (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
7536 signame-list))
7538 (defun verilog-signals-not-in (in-list not-list)
7539 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7540 Also remove any duplicates in IN-LIST.
7541 Signals must be in standard (base vector) form."
7542 ;; This function is hot, so implemented as O(1)
7543 (cond ((eval-when-compile (fboundp 'make-hash-table))
7544 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7545 out-list)
7546 (while not-list
7547 (puthash (car (car not-list)) t ht)
7548 (setq not-list (cdr not-list)))
7549 (while in-list
7550 (when (not (gethash (verilog-sig-name (car in-list)) ht))
7551 (setq out-list (cons (car in-list) out-list))
7552 (puthash (verilog-sig-name (car in-list)) t ht))
7553 (setq in-list (cdr in-list)))
7554 (nreverse out-list)))
7555 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7557 (let (out-list)
7558 (while in-list
7559 (if (not (or (assoc (verilog-sig-name (car in-list)) not-list)
7560 (assoc (verilog-sig-name (car in-list)) out-list)))
7561 (setq out-list (cons (car in-list) out-list)))
7562 (setq in-list (cdr in-list)))
7563 (nreverse out-list)))))
7564 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7566 (defun verilog-signals-memory (in-list)
7567 "Return list of signals in IN-LIST that are memorized (multidimensional)."
7568 (let (out-list)
7569 (while in-list
7570 (if (nth 3 (car in-list))
7571 (setq out-list (cons (car in-list) out-list)))
7572 (setq in-list (cdr in-list)))
7573 out-list))
7574 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
7576 (defun verilog-signals-sort-compare (a b)
7577 "Compare signal A and B for sorting."
7578 (string< (verilog-sig-name a) (verilog-sig-name b)))
7580 (defun verilog-signals-not-params (in-list)
7581 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
7582 (let (out-list)
7583 (while in-list
7584 (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
7585 (setq out-list (cons (car in-list) out-list)))
7586 (setq in-list (cdr in-list)))
7587 (nreverse out-list)))
7589 (defun verilog-signals-with (func in-list)
7590 "Return IN-LIST with only signals where FUNC passed each signal is true."
7591 (let (out-list)
7592 (while in-list
7593 (when (funcall func (car in-list))
7594 (setq out-list (cons (car in-list) out-list)))
7595 (setq in-list (cdr in-list)))
7596 (nreverse out-list)))
7598 (defun verilog-signals-combine-bus (in-list)
7599 "Return a list of signals in IN-LIST, with buses combined.
7600 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
7601 (let (combo buswarn
7602 out-list
7603 sig highbit lowbit ; Temp information about current signal
7604 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
7605 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
7606 sv-modport
7607 bus)
7608 ;; Shove signals so duplicated signals will be adjacent
7609 (setq in-list (sort in-list `verilog-signals-sort-compare))
7610 (while in-list
7611 (setq sig (car in-list))
7612 ;; No current signal; form from existing details
7613 (unless sv-name
7614 (setq sv-name (verilog-sig-name sig)
7615 sv-highbit nil
7616 sv-busstring nil
7617 sv-comment (verilog-sig-comment sig)
7618 sv-memory (verilog-sig-memory sig)
7619 sv-enum (verilog-sig-enum sig)
7620 sv-signed (verilog-sig-signed sig)
7621 sv-type (verilog-sig-type sig)
7622 sv-multidim (verilog-sig-multidim sig)
7623 sv-modport (verilog-sig-modport sig)
7624 combo ""
7625 buswarn ""))
7626 ;; Extract bus details
7627 (setq bus (verilog-sig-bits sig))
7628 (setq bus (and bus (verilog-simplify-range-expression bus)))
7629 (cond ((and bus
7630 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
7631 (setq highbit (string-to-number (match-string 1 bus))
7632 lowbit (string-to-number
7633 (match-string 2 bus))))
7634 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
7635 (setq highbit (string-to-number (match-string 1 bus))
7636 lowbit highbit))))
7637 ;; Combine bits in bus
7638 (if sv-highbit
7639 (setq sv-highbit (max highbit sv-highbit)
7640 sv-lowbit (min lowbit sv-lowbit))
7641 (setq sv-highbit highbit
7642 sv-lowbit lowbit)))
7643 (bus
7644 ;; String, probably something like `preproc:0
7645 (setq sv-busstring bus)))
7646 ;; Peek ahead to next signal
7647 (setq in-list (cdr in-list))
7648 (setq sig (car in-list))
7649 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
7650 ;; Combine with this signal
7651 (when (and sv-busstring
7652 (not (equal sv-busstring (verilog-sig-bits sig))))
7653 (when nil ;; Debugging
7654 (message (concat "Warning, can't merge into single bus "
7655 sv-name bus
7656 ", the AUTOs may be wrong")))
7657 (setq buswarn ", Couldn't Merge"))
7658 (if (verilog-sig-comment sig) (setq combo ", ..."))
7659 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
7660 sv-enum (or sv-enum (verilog-sig-enum sig))
7661 sv-signed (or sv-signed (verilog-sig-signed sig))
7662 sv-type (or sv-type (verilog-sig-type sig))
7663 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
7664 sv-modport (or sv-modport (verilog-sig-modport sig))))
7665 ;; Doesn't match next signal, add to queue, zero in prep for next
7666 ;; Note sig may also be nil for the last signal in the list
7668 (setq out-list
7669 (cons (verilog-sig-new
7670 sv-name
7671 (or sv-busstring
7672 (if sv-highbit
7673 (concat "[" (int-to-string sv-highbit) ":"
7674 (int-to-string sv-lowbit) "]")))
7675 (concat sv-comment combo buswarn)
7676 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
7677 out-list)
7678 sv-name nil))))
7680 out-list))
7682 (defun verilog-sig-tieoff (sig)
7683 "Return tieoff expression for given SIG, with appropriate width.
7684 Tieoff value uses `verilog-active-low-regexp' and
7685 `verilog-auto-reset-widths'."
7686 (concat
7687 (if (and verilog-active-low-regexp
7688 (string-match verilog-active-low-regexp (verilog-sig-name sig)))
7689 "~" "")
7690 (cond ((not verilog-auto-reset-widths)
7691 "0")
7692 ((equal verilog-auto-reset-widths 'unbased)
7693 "'0")
7694 ;; Else presume verilog-auto-reset-widths is true
7696 (let* ((width (verilog-sig-width sig)))
7697 (if (string-match "^[0-9]+$" width)
7698 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0"))
7699 (concat "{" width "{1'b0}}")))))))
7702 ;; Dumping
7705 (defun verilog-decls-princ (decls)
7706 "For debug, dump the `verilog-read-decls' structure DECLS."
7707 (verilog-signals-princ (verilog-decls-get-outputs decls)
7708 "Outputs:\n" " ")
7709 (verilog-signals-princ (verilog-decls-get-inouts decls)
7710 "Inout:\n" " ")
7711 (verilog-signals-princ (verilog-decls-get-inputs decls)
7712 "Inputs:\n" " ")
7713 (verilog-signals-princ (verilog-decls-get-vars decls)
7714 "Vars:\n" " ")
7715 (verilog-signals-princ (verilog-decls-get-assigns decls)
7716 "Assigns:\n" " ")
7717 (verilog-signals-princ (verilog-decls-get-consts decls)
7718 "Consts:\n" " ")
7719 (verilog-signals-princ (verilog-decls-get-gparams decls)
7720 "Gparams:\n" " ")
7721 (verilog-signals-princ (verilog-decls-get-interfaces decls)
7722 "Interfaces:\n" " ")
7723 (princ "\n"))
7725 (defun verilog-signals-princ (signals &optional header prefix)
7726 "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
7727 (when signals
7728 (princ header)
7729 (while signals
7730 (let ((sig (car signals)))
7731 (setq signals (cdr signals))
7732 (princ prefix)
7733 (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
7734 (princ " bits=") (princ (verilog-sig-bits sig))
7735 (princ " cmt=") (princ (verilog-sig-comment sig))
7736 (princ " mem=") (princ (verilog-sig-memory sig))
7737 (princ " enum=") (princ (verilog-sig-enum sig))
7738 (princ " sign=") (princ (verilog-sig-signed sig))
7739 (princ " type=") (princ (verilog-sig-type sig))
7740 (princ " dim=") (princ (verilog-sig-multidim sig))
7741 (princ " modp=") (princ (verilog-sig-modport sig))
7742 (princ "\n")))))
7745 ;; Port/Wire/Etc Reading
7748 (defun verilog-read-inst-backward-name ()
7749 "Internal. Move point back to beginning of inst-name."
7750 (verilog-backward-open-paren)
7751 (let (done)
7752 (while (not done)
7753 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
7754 (cond ((looking-at ")")
7755 (verilog-backward-open-paren))
7756 (t (setq done t)))))
7757 (while (looking-at "\\]")
7758 (verilog-backward-open-bracket)
7759 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
7760 (skip-chars-backward "a-zA-Z0-9`_$"))
7762 (defun verilog-read-inst-module-matcher ()
7763 "Set match data 0 with module_name when point is inside instantiation."
7764 (verilog-read-inst-backward-name)
7765 ;; Skip over instantiation name
7766 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7767 ;; Check for parameterized instantiations
7768 (when (looking-at ")")
7769 (verilog-backward-open-paren)
7770 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
7771 (skip-chars-backward "a-zA-Z0-9'_$")
7772 (looking-at "[a-zA-Z0-9`_\$]+")
7773 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7774 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
7775 ;; Caller assumes match-beginning/match-end is still set
7778 (defun verilog-read-inst-module ()
7779 "Return module_name when point is inside instantiation."
7780 (save-excursion
7781 (verilog-read-inst-module-matcher)))
7783 (defun verilog-read-inst-name ()
7784 "Return instance_name when point is inside instantiation."
7785 (save-excursion
7786 (verilog-read-inst-backward-name)
7787 (looking-at "[a-zA-Z0-9`_\$]+")
7788 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7789 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
7791 (defun verilog-read-module-name ()
7792 "Return module name when after its ( or ;."
7793 (save-excursion
7794 (re-search-backward "[(;]")
7795 ;; Due to "module x import y (" we must search for declaration begin
7796 (verilog-re-search-backward-quick verilog-defun-re nil nil)
7797 (goto-char (match-end 0))
7798 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_\$]+" nil nil)
7799 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7800 (verilog-symbol-detick
7801 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
7803 (defun verilog-read-inst-param-value ()
7804 "Return list of parameters and values when point is inside instantiation."
7805 (save-excursion
7806 (verilog-read-inst-backward-name)
7807 ;; Skip over instantiation name
7808 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7809 ;; If there are parameterized instantiations
7810 (when (looking-at ")")
7811 (let ((end-pt (point))
7812 params
7813 param-name paren-beg-pt param-value)
7814 (verilog-backward-open-paren)
7815 (while (verilog-re-search-forward-quick "\\." end-pt t)
7816 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_\$]\\)" nil nil)
7817 (skip-chars-backward "a-zA-Z0-9'_$")
7818 (looking-at "[a-zA-Z0-9`_\$]+")
7819 (setq param-name (buffer-substring-no-properties
7820 (match-beginning 0) (match-end 0)))
7821 (verilog-re-search-forward-quick "(" nil nil)
7822 (setq paren-beg-pt (point))
7823 (verilog-forward-close-paren)
7824 (setq param-value (verilog-string-remove-spaces
7825 (buffer-substring-no-properties
7826 paren-beg-pt (1- (point)))))
7827 (setq params (cons (list param-name param-value) params)))
7828 params))))
7830 (defun verilog-read-auto-params (num-param &optional max-param)
7831 "Return parameter list inside auto.
7832 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
7833 (let ((olist))
7834 (save-excursion
7835 ;; /*AUTOPUNT("parameter", "parameter")*/
7836 (backward-sexp 1)
7837 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
7838 (setq olist (cons (match-string 1) olist))
7839 (goto-char (match-end 0))))
7840 (or (eq nil num-param)
7841 (<= num-param (length olist))
7842 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
7843 (if (eq max-param nil) (setq max-param num-param))
7844 (or (eq nil max-param)
7845 (>= max-param (length olist))
7846 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
7847 (nreverse olist)))
7849 (defun verilog-read-decls ()
7850 "Compute signal declaration information for the current module at point.
7851 Return an array of [outputs inouts inputs wire reg assign const]."
7852 (let ((end-mod-point (or (verilog-get-end-of-defun t) (point-max)))
7853 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
7854 in-modport ptype ign-prop
7855 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
7856 sigs-gparam sigs-intf
7857 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
7858 modport)
7859 (save-excursion
7860 (verilog-beg-of-defun-quick)
7861 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
7862 (while (< (point) end-mod-point)
7863 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
7864 (cond
7865 ((looking-at "//")
7866 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7867 (setq enum (match-string 2)))
7868 (search-forward "\n"))
7869 ((looking-at "/\\*")
7870 (forward-char 2)
7871 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7872 (setq enum (match-string 2)))
7873 (or (search-forward "*/")
7874 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
7875 ((looking-at "(\\*")
7876 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
7877 (forward-char 1)
7878 (or (search-forward "*)")
7879 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
7880 ((eq ?\" (following-char))
7881 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
7882 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
7883 ((eq ?\; (following-char))
7884 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
7885 v2kargs-ok nil in-modport nil ign-prop nil)
7886 (forward-char 1))
7887 ((eq ?= (following-char))
7888 (setq rvalue t newsig nil)
7889 (forward-char 1))
7890 ((and (eq ?, (following-char))
7891 (eq paren sig-paren))
7892 (setq rvalue nil)
7893 (forward-char 1))
7894 ;; ,'s can occur inside {} & funcs
7895 ((looking-at "[{(]")
7896 (setq paren (1+ paren))
7897 (forward-char 1))
7898 ((looking-at "[})]")
7899 (setq paren (1- paren))
7900 (forward-char 1)
7901 (when (< paren sig-paren)
7902 (setq expect-signal nil))) ; ) that ends variables inside v2k arg list
7903 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
7904 (goto-char (match-end 0))
7905 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
7906 (setcar (cdr (cdr (cdr newsig)))
7907 (if (verilog-sig-memory newsig)
7908 (concat (verilog-sig-memory newsig) (match-string 1))
7909 (match-string 1))))
7910 (vec ;; Multidimensional
7911 (setq multidim (cons vec multidim))
7912 (setq vec (verilog-string-replace-matches
7913 "\\s-+" "" nil nil (match-string 1))))
7914 (t ;; Bit width
7915 (setq vec (verilog-string-replace-matches
7916 "\\s-+" "" nil nil (match-string 1))))))
7917 ;; Normal or escaped identifier -- note we remember the \ if escaped
7918 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
7919 (goto-char (match-end 0))
7920 (setq keywd (match-string 1))
7921 (when (string-match "^\\\\" (match-string 1))
7922 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
7923 ;; Add any :: package names to same identifier
7924 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
7925 (goto-char (match-end 0))
7926 (setq keywd (concat keywd "::" (match-string 1)))
7927 (when (string-match "^\\\\" (match-string 1))
7928 (setq keywd (concat keywd " ")))) ;; Escaped ID needs space at end
7929 (cond ((equal keywd "input")
7930 (setq vec nil enum nil rvalue nil newsig nil signed nil
7931 typedefed nil multidim nil ptype nil modport nil
7932 expect-signal 'sigs-in io t sig-paren paren))
7933 ((equal keywd "output")
7934 (setq vec nil enum nil rvalue nil newsig nil signed nil
7935 typedefed nil multidim nil ptype nil modport nil
7936 expect-signal 'sigs-out io t sig-paren paren))
7937 ((equal keywd "inout")
7938 (setq vec nil enum nil rvalue nil newsig nil signed nil
7939 typedefed nil multidim nil ptype nil modport nil
7940 expect-signal 'sigs-inout io t sig-paren paren))
7941 ((equal keywd "parameter")
7942 (setq vec nil enum nil rvalue nil signed nil
7943 typedefed nil multidim nil ptype nil modport nil
7944 expect-signal 'sigs-gparam io t sig-paren paren))
7945 ((member keywd '("wire" "reg" ; Fast
7946 ;; net_type
7947 "tri" "tri0" "tri1" "triand" "trior" "trireg"
7948 "uwire" "wand" "wor"
7949 ;; integer_atom_type
7950 "byte" "shortint" "int" "longint" "integer" "time"
7951 "supply0" "supply1"
7952 ;; integer_vector_type - "reg" above
7953 "bit" "logic"
7954 ;; non_integer_type
7955 "shortreal" "real" "realtime"
7956 ;; data_type
7957 "string" "event" "chandle"))
7958 (cond (io
7959 (setq typedefed
7960 (if typedefed (concat typedefed " " keywd) keywd)))
7961 (t (setq vec nil enum nil rvalue nil signed nil
7962 typedefed nil multidim nil sig-paren paren
7963 expect-signal 'sigs-var modport nil))))
7964 ((equal keywd "assign")
7965 (setq vec nil enum nil rvalue nil signed nil
7966 typedefed nil multidim nil ptype nil modport nil
7967 expect-signal 'sigs-assign sig-paren paren))
7968 ((member keywd '("localparam" "genvar"))
7969 (unless io
7970 (setq vec nil enum nil rvalue nil signed nil
7971 typedefed nil multidim nil ptype nil modport nil
7972 expect-signal 'sigs-const sig-paren paren)))
7973 ((member keywd '("signed" "unsigned"))
7974 (setq signed keywd))
7975 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
7976 (setq ign-prop t))
7977 ((member keywd '("class" "clocking" "covergroup" "function"
7978 "property" "randsequence" "sequence" "task"))
7979 (unless ign-prop
7980 (setq functask (1+ functask))))
7981 ((member keywd '("endclass" "endclocking" "endgroup" "endfunction"
7982 "endproperty" "endsequence" "endtask"))
7983 (setq functask (1- functask)))
7984 ((equal keywd "modport")
7985 (setq in-modport t))
7986 ((equal keywd "type")
7987 (setq ptype t))
7988 ;; Ifdef? Ignore name of define
7989 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
7990 (setq rvalue t))
7991 ;; Type?
7992 ((unless ptype
7993 (verilog-typedef-name-p keywd))
7994 (setq typedefed keywd))
7995 ;; Interface with optional modport in v2k arglist?
7996 ;; Skip over parsing modport, and take the interface name as the type
7997 ((and v2kargs-ok
7998 (eq paren 1)
7999 (not rvalue)
8000 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
8001 (when (match-end 2) (goto-char (match-end 2)))
8002 (setq vec nil enum nil rvalue nil signed nil
8003 typedefed keywd multidim nil ptype nil modport (match-string 2)
8004 newsig nil sig-paren paren
8005 expect-signal 'sigs-intf io t ))
8006 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
8007 ((looking-at "\\s-*\\.")
8008 (goto-char (match-end 0))
8009 (when (not rvalue)
8010 (setq expect-signal nil)))
8011 ;; New signal, maybe?
8012 ((and expect-signal
8013 (not rvalue)
8014 (eq functask 0)
8015 (not in-modport)
8016 (not (member keywd verilog-keywords)))
8017 ;; Add new signal to expect-signal's variable
8018 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
8019 (set expect-signal (cons newsig
8020 (symbol-value expect-signal))))))
8022 (forward-char 1)))
8023 (skip-syntax-forward " "))
8024 ;; Return arguments
8025 (verilog-decls-new (nreverse sigs-out)
8026 (nreverse sigs-inout)
8027 (nreverse sigs-in)
8028 (nreverse sigs-var)
8030 (nreverse sigs-assign)
8031 (nreverse sigs-const)
8032 (nreverse sigs-gparam)
8033 (nreverse sigs-intf)))))
8035 (defvar verilog-read-sub-decls-in-interfaced nil
8036 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
8038 (defvar verilog-read-sub-decls-gate-ios nil
8039 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
8041 (eval-when-compile
8042 ;; Prevent compile warnings; these are let's, not globals
8043 ;; Do not remove the eval-when-compile
8044 ;; - we want an error when we are debugging this code if they are refed.
8045 (defvar sigs-in)
8046 (defvar sigs-inout)
8047 (defvar sigs-out)
8048 (defvar sigs-intf)
8049 (defvar sigs-intfd))
8051 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
8052 "For `verilog-read-sub-decls-line', add a signal."
8053 ;; sig eq t to indicate .name syntax
8054 ;;(message "vrsds: %s(%S)" port sig)
8055 (let ((dotname (eq sig t))
8056 portdata)
8057 (when sig
8058 (setq port (verilog-symbol-detick-denumber port))
8059 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
8060 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
8061 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
8062 (unless (or (not sig)
8063 (equal sig "")) ;; Ignore .foo(1'b1) assignments
8064 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
8065 (equal "inout" verilog-read-sub-decls-gate-ios))
8066 (setq sigs-inout
8067 (cons (verilog-sig-new
8069 (if dotname (verilog-sig-bits portdata) vec)
8070 (concat "To/From " comment)
8071 (verilog-sig-memory portdata)
8073 (verilog-sig-signed portdata)
8074 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8075 (verilog-sig-type portdata))
8076 multidim nil)
8077 sigs-inout)))
8078 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
8079 (equal "output" verilog-read-sub-decls-gate-ios))
8080 (setq sigs-out
8081 (cons (verilog-sig-new
8083 (if dotname (verilog-sig-bits portdata) vec)
8084 (concat "From " comment)
8085 (verilog-sig-memory portdata)
8087 (verilog-sig-signed portdata)
8088 ;; Though ok in SV, in V2K code, propagating the
8089 ;; "reg" in "output reg" upwards isn't legal.
8090 ;; Also for backwards compatibility we don't propagate
8091 ;; "input wire" upwards.
8092 ;; See also `verilog-signals-edit-wire-reg'.
8093 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8094 (verilog-sig-type portdata))
8095 multidim nil)
8096 sigs-out)))
8097 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
8098 (equal "input" verilog-read-sub-decls-gate-ios))
8099 (setq sigs-in
8100 (cons (verilog-sig-new
8102 (if dotname (verilog-sig-bits portdata) vec)
8103 (concat "To " comment)
8104 (verilog-sig-memory portdata)
8106 (verilog-sig-signed portdata)
8107 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8108 (verilog-sig-type portdata))
8109 multidim nil)
8110 sigs-in)))
8111 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
8112 (setq sigs-intf
8113 (cons (verilog-sig-new
8115 (if dotname (verilog-sig-bits portdata) vec)
8116 (concat "To/From " comment)
8117 (verilog-sig-memory portdata)
8119 (verilog-sig-signed portdata)
8120 (verilog-sig-type portdata)
8121 multidim nil)
8122 sigs-intf)))
8123 ((setq portdata (and verilog-read-sub-decls-in-interfaced
8124 (assoc port (verilog-decls-get-vars submoddecls))))
8125 (setq sigs-intfd
8126 (cons (verilog-sig-new
8128 (if dotname (verilog-sig-bits portdata) vec)
8129 (concat "To/From " comment)
8130 (verilog-sig-memory portdata)
8132 (verilog-sig-signed portdata)
8133 (verilog-sig-type portdata)
8134 multidim nil)
8135 sigs-intf)))
8136 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
8137 )))))
8139 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
8140 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
8141 ;;(message "vrsde: '%s'" expr)
8142 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
8143 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
8144 ;; Remove front operators
8145 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8147 (cond
8148 ;; {..., a, b} requires us to recurse on a,b
8149 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
8150 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
8151 (unless verilog-auto-ignore-concat
8152 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
8153 mstr)
8154 (while (setq mstr (pop mlst))
8155 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
8157 (let (sig vec multidim)
8158 ;; Remove leading reduction operators, etc
8159 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8160 ;;(message "vrsde-ptop: '%s'" expr)
8161 (cond ;; Find \signal. Final space is part of escaped signal name
8162 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8163 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8164 (setq sig (match-string 1 expr)
8165 expr (substring expr (match-end 0))))
8166 ;; Find signal
8167 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
8168 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8169 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8170 expr (substring expr (match-end 0)))))
8171 ;; Find [vector] or [multi][multi][multi][vector]
8172 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8173 ;;(message "vrsde-v: '%s'" (match-string 1 expr))
8174 (when vec (setq multidim (cons vec multidim)))
8175 (setq vec (match-string 1 expr)
8176 expr (substring expr (match-end 0))))
8177 ;; If found signal, and nothing unrecognized, add the signal
8178 ;;(message "vrsde-rem: '%s'" expr)
8179 (when (and sig (string-match "^\\s-*$" expr))
8180 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
8182 (defun verilog-read-sub-decls-line (submoddecls comment)
8183 "For `verilog-read-sub-decls', read lines of port defs until none match.
8184 Inserts the list of signals found, using submodi to look up each port."
8185 (let (done port)
8186 (save-excursion
8187 (forward-line 1)
8188 (while (not done)
8189 ;; Get port name
8190 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8191 (setq port (match-string 1))
8192 (goto-char (match-end 0)))
8193 ;; .\escaped (
8194 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8195 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
8196 (goto-char (match-end 0)))
8197 ;; .name
8198 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8199 (verilog-read-sub-decls-sig
8200 submoddecls comment (match-string 1) t ; sig==t for .name
8201 nil nil) ; vec multidim
8202 (setq port nil))
8203 ;; .\escaped_name
8204 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8205 (verilog-read-sub-decls-sig
8206 submoddecls comment (concat (match-string 1) " ") t ; sig==t for .name
8207 nil nil) ; vec multidim
8208 (setq port nil))
8209 ;; random
8210 ((looking-at "\\s-*\\.[^(]*(")
8211 (setq port nil) ;; skip this line
8212 (goto-char (match-end 0)))
8214 (setq port nil done t))) ;; Unknown, ignore rest of line
8215 ;; Get signal name. Point is at the first-non-space after (
8216 ;; We intentionally ignore (non-escaped) signals with .s in them
8217 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
8218 (when port
8219 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
8220 (verilog-read-sub-decls-sig
8221 submoddecls comment port
8222 (verilog-string-remove-spaces (match-string 1)) ; sig
8223 nil nil)) ; vec multidim
8225 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
8226 (verilog-read-sub-decls-sig
8227 submoddecls comment port
8228 (verilog-string-remove-spaces (match-string 1)) ; sig
8229 (match-string 2) nil)) ; vec multidim
8230 ;; Fastpath was above looking-at's.
8231 ;; For something more complicated invoke a parser
8232 ((looking-at "[^)]+")
8233 (verilog-read-sub-decls-expr
8234 submoddecls comment port
8235 (buffer-substring
8236 (point) (1- (progn (search-backward "(") ; start at (
8237 (verilog-forward-sexp-ign-cmt 1)
8238 (point)))))))) ; expr
8240 (forward-line 1)))))
8242 (defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8243 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8244 Inserts the list of signals found."
8245 (save-excursion
8246 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8247 (while (< (point) end-inst-point)
8248 ;; Get primitive's signal name, as will never have port, and no trailing )
8249 (cond ((looking-at "//")
8250 (search-forward "\n"))
8251 ((looking-at "/\\*")
8252 (or (search-forward "*/")
8253 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8254 ((looking-at "(\\*")
8255 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8256 (forward-char 1)
8257 (or (search-forward "*)")
8258 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8259 ;; On pins, parse and advance to next pin
8260 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8261 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8262 (goto-char (match-end 0))
8263 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8264 iolist (cdr iolist))
8265 (verilog-read-sub-decls-expr
8266 submoddecls comment "primitive_port"
8267 (match-string 0)))
8269 (forward-char 1)
8270 (skip-syntax-forward " ")))))))
8272 (defun verilog-read-sub-decls ()
8273 "Internally parse signals going to modules under this module.
8274 Return an array of [ outputs inouts inputs ] signals for modules that are
8275 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8276 is an output, then SIG will be included in the list.
8278 This only works on instantiations created with /*AUTOINST*/ converted by
8279 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8280 component library to determine connectivity of the design.
8282 One work around for this problem is to manually create // Inputs and //
8283 Outputs comments above subcell signals, for example:
8285 module ModuleName (
8286 // Outputs
8287 .out (out),
8288 // Inputs
8289 .in (in));"
8290 (save-excursion
8291 (let ((end-mod-point (verilog-get-end-of-defun t))
8292 st-point end-inst-point
8293 ;; below 3 modified by verilog-read-sub-decls-line
8294 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
8295 (verilog-beg-of-defun-quick)
8296 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
8297 (save-excursion
8298 (goto-char (match-beginning 0))
8299 (unless (verilog-inside-comment-or-string-p)
8300 ;; Attempt to snarf a comment
8301 (let* ((submod (verilog-read-inst-module))
8302 (inst (verilog-read-inst-name))
8303 (subprim (member submod verilog-gate-keywords))
8304 (comment (concat inst " of " submod ".v"))
8305 submodi submoddecls)
8306 (cond
8307 (subprim
8308 (setq submodi `primitive
8309 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8310 comment (concat inst " of " submod))
8311 (verilog-backward-open-paren)
8312 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8313 (point))
8314 st-point (point))
8315 (forward-char 1)
8316 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8317 ;; Non-primitive
8319 (when (setq submodi (verilog-modi-lookup submod t))
8320 (setq submoddecls (verilog-modi-get-decls submodi)
8321 verilog-read-sub-decls-gate-ios nil)
8322 (verilog-backward-open-paren)
8323 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8324 (point))
8325 st-point (point))
8326 ;; This could have used a list created by verilog-auto-inst
8327 ;; However I want it to be runnable even on user's manually added signals
8328 (let ((verilog-read-sub-decls-in-interfaced t))
8329 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8330 (verilog-read-sub-decls-line submoddecls comment))) ;; Modifies sigs-ifd
8331 (goto-char st-point)
8332 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8333 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8334 (goto-char st-point)
8335 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8336 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8337 (goto-char st-point)
8338 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8339 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
8340 (goto-char st-point)
8341 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8342 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
8343 )))))))
8344 ;; Combine duplicate bits
8345 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
8346 (verilog-subdecls-new
8347 (verilog-signals-combine-bus (nreverse sigs-out))
8348 (verilog-signals-combine-bus (nreverse sigs-inout))
8349 (verilog-signals-combine-bus (nreverse sigs-in))
8350 (verilog-signals-combine-bus (nreverse sigs-intf))
8351 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
8353 (defun verilog-read-inst-pins ()
8354 "Return an array of [ pins ] for the current instantiation at point.
8355 For example if declare A A (.B(SIG)) then B will be included in the list."
8356 (save-excursion
8357 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
8358 pins pin)
8359 (verilog-backward-open-paren)
8360 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8361 (setq pin (match-string 1))
8362 (unless (verilog-inside-comment-or-string-p)
8363 (setq pins (cons (list pin) pins))
8364 (when (looking-at "(")
8365 (verilog-forward-sexp-ign-cmt 1))))
8366 (vector pins))))
8368 (defun verilog-read-arg-pins ()
8369 "Return an array of [ pins ] for the current argument declaration at point."
8370 (save-excursion
8371 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
8372 pins pin)
8373 (verilog-backward-open-paren)
8374 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8375 (setq pin (match-string 1))
8376 (unless (verilog-inside-comment-or-string-p)
8377 (setq pins (cons (list pin) pins))))
8378 (vector pins))))
8380 (defun verilog-read-auto-constants (beg end-mod-point)
8381 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8382 ;; Insert new
8383 (save-excursion
8384 (let (sig-list tpl-end-pt)
8385 (goto-char beg)
8386 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8387 (if (not (looking-at "\\s *("))
8388 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8389 (search-forward "(" end-mod-point)
8390 (setq tpl-end-pt (save-excursion
8391 (backward-char 1)
8392 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8393 (backward-char 1)
8394 (point)))
8395 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8396 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8397 sig-list)))
8399 (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8400 (make-variable-buffer-local 'verilog-cache-has-lisp)
8402 (defun verilog-read-auto-lisp-present ()
8403 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
8404 (save-excursion
8405 (goto-char (point-min))
8406 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8408 (defun verilog-read-auto-lisp (start end)
8409 "Look for and evaluate an AUTO_LISP between START and END.
8410 Must call `verilog-read-auto-lisp-present' before this function."
8411 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8412 (when verilog-cache-has-lisp
8413 (save-excursion
8414 (goto-char start)
8415 (while (re-search-forward "\\<AUTO_LISP(" end t)
8416 (backward-char)
8417 (let* ((beg-pt (prog1 (point)
8418 (verilog-forward-sexp-cmt 1))) ;; Closing paren
8419 (end-pt (point))
8420 (verilog-in-hooks t))
8421 (eval-region beg-pt end-pt nil))))))
8423 (eval-when-compile
8424 ;; Prevent compile warnings; these are let's, not globals
8425 ;; Do not remove the eval-when-compile
8426 ;; - we want an error when we are debugging this code if they are refed.
8427 (defvar sigs-in)
8428 (defvar sigs-out-d)
8429 (defvar sigs-out-i)
8430 (defvar sigs-out-unk)
8431 (defvar sigs-temp)
8432 (defvar vector-skip-list))
8434 (defun verilog-read-always-signals-recurse
8435 (exit-keywd rvalue temp-next)
8436 "Recursive routine for parentheses/bracket matching.
8437 EXIT-KEYWD is expression to stop at, nil if top level.
8438 RVALUE is true if at right hand side of equal.
8439 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
8440 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
8441 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
8442 ignore-next)
8443 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
8444 (while (not (or (eobp) gotend))
8445 (cond
8446 ((looking-at "//")
8447 (search-forward "\n"))
8448 ((looking-at "/\\*")
8449 (or (search-forward "*/")
8450 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8451 ((looking-at "(\\*")
8452 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8453 (forward-char 1)
8454 (or (search-forward "*)")
8455 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8456 (t (setq keywd (buffer-substring-no-properties
8457 (point)
8458 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8459 (forward-char 1))
8460 (point)))
8461 sig-last-tolk sig-tolk
8462 sig-tolk nil)
8463 ;;(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))))
8464 (cond
8465 ((equal keywd "\"")
8466 (or (re-search-forward "[^\\]\"" nil t)
8467 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8468 ;; else at top level loop, keep parsing
8469 ((and end-else-check (equal keywd "else"))
8470 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
8471 ;; no forward movement, want to see else in lower loop
8472 (setq end-else-check nil))
8473 ;; End at top level loop
8474 ((and end-else-check (looking-at "[^ \t\n\f]"))
8475 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
8476 (setq gotend t))
8477 ;; Final statement?
8478 ((and exit-keywd (equal keywd exit-keywd))
8479 (setq gotend t)
8480 (forward-char (length keywd)))
8481 ;; Standard tokens...
8482 ((equal keywd ";")
8483 (setq ignore-next nil rvalue semi-rvalue)
8484 ;; Final statement at top level loop?
8485 (when (not exit-keywd)
8486 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
8487 (setq end-else-check t))
8488 (forward-char 1))
8489 ((equal keywd "'")
8490 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
8491 (goto-char (match-end 0))
8492 (forward-char 1)))
8493 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
8494 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
8495 (setq ignore-next nil rvalue nil))
8496 ((equal "?" exit-keywd) ;; x?y:z rvalue
8497 ) ;; NOP
8498 ((equal "]" exit-keywd) ;; [x:y] rvalue
8499 ) ;; NOP
8500 (got-sig ;; label: statement
8501 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
8502 ((not rvalue) ;; begin label
8503 (setq ignore-next t rvalue nil)))
8504 (forward-char 1))
8505 ((equal keywd "=")
8506 (when got-sig
8507 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
8508 (set got-list (cons got-sig (symbol-value got-list)))
8509 (setq got-sig nil))
8510 (when (not rvalue)
8511 (if (eq (char-before) ?< )
8512 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
8513 sigs-out-unk nil)
8514 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8515 sigs-out-unk nil)))
8516 (setq ignore-next nil rvalue t)
8517 (forward-char 1))
8518 ((equal keywd "?")
8519 (forward-char 1)
8520 (verilog-read-always-signals-recurse ":" rvalue nil))
8521 ((equal keywd "[")
8522 (forward-char 1)
8523 (verilog-read-always-signals-recurse "]" t nil))
8524 ((equal keywd "(")
8525 (forward-char 1)
8526 (cond (sig-last-tolk ;; Function call; zap last signal
8527 (setq got-sig nil)))
8528 (cond ((equal last-keywd "for")
8529 ;; temp-next: Variables on LHS are lvalues, but generally we want
8530 ;; to ignore them, assuming they are loop increments
8531 (verilog-read-always-signals-recurse ";" nil t)
8532 (verilog-read-always-signals-recurse ";" t nil)
8533 (verilog-read-always-signals-recurse ")" nil nil))
8534 (t (verilog-read-always-signals-recurse ")" t nil))))
8535 ((equal keywd "begin")
8536 (skip-syntax-forward "w_")
8537 (verilog-read-always-signals-recurse "end" nil nil)
8538 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
8539 (setq ignore-next nil rvalue semi-rvalue)
8540 (if (not exit-keywd) (setq end-else-check t)))
8541 ((member keywd '("case" "casex" "casez"))
8542 (skip-syntax-forward "w_")
8543 (verilog-read-always-signals-recurse "endcase" t nil)
8544 (setq ignore-next nil rvalue semi-rvalue)
8545 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
8546 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
8547 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8548 (setq ignore-next t))
8549 ((or ignore-next
8550 (member keywd verilog-keywords)
8551 (string-match "^\\$" keywd)) ;; PLI task
8552 (setq ignore-next nil))
8554 (setq keywd (verilog-symbol-detick-denumber keywd))
8555 (when got-sig
8556 (set got-list (cons got-sig (symbol-value got-list)))
8557 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
8559 (setq got-list (cond (temp-next 'sigs-temp)
8560 (rvalue 'sigs-in)
8561 (t 'sigs-out-unk))
8562 got-sig (if (or (not keywd)
8563 (assoc keywd (symbol-value got-list)))
8564 nil (list keywd nil nil))
8565 temp-next nil
8566 sig-tolk t)))
8567 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8569 (forward-char 1)))
8570 ;; End of non-comment token
8571 (setq last-keywd keywd)))
8572 (skip-syntax-forward " "))
8573 ;; Append the final pending signal
8574 (when got-sig
8575 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
8576 (set got-list (cons got-sig (symbol-value got-list)))
8577 (setq got-sig nil))
8578 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
8581 (defun verilog-read-always-signals ()
8582 "Parse always block at point and return list of (outputs inout inputs)."
8583 (save-excursion
8584 (let* (;;(dbg "")
8585 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
8586 (search-forward ")")
8587 (verilog-read-always-signals-recurse nil nil nil)
8588 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8589 sigs-out-unk nil)
8590 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
8591 ;; Return what was found
8592 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
8594 (defun verilog-read-instants ()
8595 "Parse module at point and return list of ( ( file instance ) ... )."
8596 (verilog-beg-of-defun-quick)
8597 (let* ((end-mod-point (verilog-get-end-of-defun t))
8598 (state nil)
8599 (instants-list nil))
8600 (save-excursion
8601 (while (< (point) end-mod-point)
8602 ;; Stay at level 0, no comments
8603 (while (progn
8604 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
8605 (or (> (car state) 0) ; in parens
8606 (nth 5 state) ; comment
8608 (forward-line 1))
8609 (beginning-of-line)
8610 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
8611 ;;(if (looking-at "^\\(.+\\)$")
8612 (let ((module (match-string 1))
8613 (instant (match-string 2)))
8614 (if (not (member module verilog-keywords))
8615 (setq instants-list (cons (list module instant) instants-list)))))
8616 (forward-line 1)))
8617 instants-list))
8620 (defun verilog-read-auto-template-middle ()
8621 "With point in middle of an AUTO_TEMPLATE, parse it.
8622 Returns REGEXP and list of ( (signal_name connection_name)... )."
8623 (save-excursion
8624 ;; Find beginning
8625 (let ((tpl-regexp "\\([0-9]+\\)")
8626 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
8627 (templateno 0)
8628 tpl-sig-list tpl-wild-list tpl-end-pt rep)
8629 ;; Parse "REGEXP"
8630 ;; We reserve @"..." for future lisp expressions that evaluate
8631 ;; once-per-AUTOINST
8632 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
8633 (setq tpl-regexp (match-string 1))
8634 (goto-char (match-end 0)))
8635 (search-forward "(")
8636 ;; Parse lines in the template
8637 (when (or verilog-auto-inst-template-numbers
8638 verilog-auto-template-warn-unused)
8639 (save-excursion
8640 (let ((pre-pt (point)))
8641 (goto-char (point-min))
8642 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
8643 (setq templateno (1+ templateno)))
8644 (while (< (point) pre-pt)
8645 (forward-line 1)
8646 (setq lineno (1+ lineno))))))
8647 (setq tpl-end-pt (save-excursion
8648 (backward-char 1)
8649 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8650 (backward-char 1)
8651 (point)))
8653 (while (< (point) tpl-end-pt)
8654 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8655 (setq tpl-sig-list
8656 (cons (list
8657 (match-string-no-properties 1)
8658 (match-string-no-properties 2)
8659 templateno lineno)
8660 tpl-sig-list))
8661 (goto-char (match-end 0)))
8662 ;; Regexp form??
8663 ((looking-at
8664 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
8665 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8666 (setq rep (match-string-no-properties 3))
8667 (goto-char (match-end 0))
8668 (setq tpl-wild-list
8669 (cons (list
8670 (concat "^"
8671 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
8672 (match-string 1))
8673 "$")
8675 templateno lineno)
8676 tpl-wild-list)))
8677 ((looking-at "[ \t\f]+")
8678 (goto-char (match-end 0)))
8679 ((looking-at "\n")
8680 (setq lineno (1+ lineno))
8681 (goto-char (match-end 0)))
8682 ((looking-at "//")
8683 (search-forward "\n")
8684 (setq lineno (1+ lineno)))
8685 ((looking-at "/\\*")
8686 (forward-char 2)
8687 (or (search-forward "*/")
8688 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8690 (error "%s: AUTO_TEMPLATE parsing error: %s"
8691 (verilog-point-text)
8692 (progn (looking-at ".*$") (match-string 0))))))
8693 ;; Return
8694 (vector tpl-regexp
8695 (list tpl-sig-list tpl-wild-list)))))
8697 (defun verilog-read-auto-template (module)
8698 "Look for an auto_template for the instantiation of the given MODULE.
8699 If found returns `verilog-read-auto-template-inside' structure."
8700 (save-excursion
8701 ;; Find beginning
8702 (let ((pt (point)))
8703 ;; Note this search is expensive, as we hunt from mod-begin to point
8704 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
8705 ;; So, we look first for an exact string rather than a slow regexp.
8706 ;; Someday we may keep a cache of every template, but this would also
8707 ;; need to record the relative position of each AUTOINST, as multiple
8708 ;; templates exist for each module, and we're inserting lines.
8709 (cond ((or
8710 ;; See also regexp in `verilog-auto-template-lint'
8711 (verilog-re-search-backward-substr
8712 "AUTO_TEMPLATE"
8713 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
8714 ;; Also try forward of this AUTOINST
8715 ;; This is for historical support; this isn't speced as working
8716 (progn
8717 (goto-char pt)
8718 (verilog-re-search-forward-substr
8719 "AUTO_TEMPLATE"
8720 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
8721 (goto-char (match-end 0))
8722 (verilog-read-auto-template-middle))
8723 ;; If no template found
8724 (t (vector "" nil))))))
8725 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
8727 (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
8728 (make-variable-buffer-local 'verilog-auto-template-hits)
8730 (defun verilog-read-auto-template-hit (tpl-ass)
8731 "Record that TPL-ASS template from `verilog-read-auto-template' was used."
8732 (when (eval-when-compile (fboundp 'make-hash-table)) ;; else feature not allowed
8733 (when verilog-auto-template-warn-unused
8734 (unless verilog-auto-template-hits
8735 (setq verilog-auto-template-hits
8736 (make-hash-table :test 'equal :rehash-size 4.0)))
8737 (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
8738 verilog-auto-template-hits))))
8740 (defun verilog-set-define (defname defvalue &optional buffer enumname)
8741 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
8742 Optionally associate it with the specified enumeration ENUMNAME."
8743 (with-current-buffer (or buffer (current-buffer))
8744 (let ((mac (intern (concat "vh-" defname))))
8745 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
8746 ;; Need to define to a constant if no value given
8747 (set (make-local-variable mac)
8748 (if (equal defvalue "") "1" defvalue)))
8749 (if enumname
8750 (let ((enumvar (intern (concat "venum-" enumname))))
8751 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
8752 (unless (boundp enumvar) (set enumvar nil))
8753 (add-to-list (make-local-variable enumvar) defname)))))
8755 (defun verilog-read-defines (&optional filename recurse subcall)
8756 "Read `defines and parameters for the current file, or optional FILENAME.
8757 If the filename is provided, `verilog-library-flags' will be used to
8758 resolve it. If optional RECURSE is non-nil, recurse through `includes.
8760 Parameters must be simple assignments to constants, or have their own
8761 \"parameter\" label rather than a list of parameters. Thus:
8763 parameter X = 5, Y = 10; // Ok
8764 parameter X = {1'b1, 2'h2}; // Ok
8765 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
8767 Defines must be simple text substitutions, one on a line, starting
8768 at the beginning of the line. Any ifdefs or multiline comments around the
8769 define are ignored.
8771 Defines are stored inside Emacs variables using the name vh-{definename}.
8773 This function is useful for setting vh-* variables. The file variables
8774 feature can be used to set defines that `verilog-mode' can see; put at the
8775 *END* of your file something like:
8777 // Local Variables:
8778 // vh-macro:\"macro_definition\"
8779 // End:
8781 If macros are defined earlier in the same file and you want their values,
8782 you can read them automatically (provided `enable-local-eval' is on):
8784 // Local Variables:
8785 // eval:(verilog-read-defines)
8786 // eval:(verilog-read-defines \"group_standard_includes.v\")
8787 // End:
8789 Note these are only read when the file is first visited, you must use
8790 \\[find-alternate-file] RET to have these take effect after editing them!
8792 If you want to disable the \"Process `eval' or hook local variables\"
8793 warning message, you need to add to your .emacs file:
8795 (setq enable-local-eval t)"
8796 (let ((origbuf (current-buffer)))
8797 (save-excursion
8798 (unless subcall (verilog-getopt-flags))
8799 (when filename
8800 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
8801 (if fns
8802 (set-buffer (find-file-noselect (car fns)))
8803 (error (concat (verilog-point-text)
8804 ": Can't find verilog-read-defines file: " filename)))))
8805 (when recurse
8806 (goto-char (point-min))
8807 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
8808 (let ((inc (verilog-string-replace-matches
8809 "\"" "" nil nil (match-string-no-properties 1))))
8810 (unless (verilog-inside-comment-or-string-p)
8811 (verilog-read-defines inc recurse t)))))
8812 ;; Read `defines
8813 ;; note we don't use verilog-re... it's faster this way, and that
8814 ;; function has problems when comments are at the end of the define
8815 (goto-char (point-min))
8816 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
8817 (let ((defname (match-string-no-properties 1))
8818 (defvalue (match-string-no-properties 2)))
8819 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
8820 (verilog-set-define defname defvalue origbuf)))
8821 ;; Hack: Read parameters
8822 (goto-char (point-min))
8823 (while (re-search-forward
8824 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
8825 (let (enumname)
8826 ;; The primary way of getting defines is verilog-read-decls
8827 ;; However, that isn't called yet for included files, so we'll add another scheme
8828 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8829 (setq enumname (match-string-no-properties 2)))
8830 (forward-comment 99999)
8831 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
8832 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
8833 (verilog-set-define (match-string-no-properties 1)
8834 (match-string-no-properties 2) origbuf enumname)
8835 (goto-char (match-end 0))
8836 (forward-comment 99999)))))))
8838 (defun verilog-read-includes ()
8839 "Read `includes for the current file.
8840 This will find all of the `includes which are at the beginning of lines,
8841 ignoring any ifdefs or multiline comments around them.
8842 `verilog-read-defines' is then performed on the current and each included
8843 file.
8845 It is often useful put at the *END* of your file something like:
8847 // Local Variables:
8848 // eval:(verilog-read-defines)
8849 // eval:(verilog-read-includes)
8850 // End:
8852 Note includes are only read when the file is first visited, you must use
8853 \\[find-alternate-file] RET to have these take effect after editing them!
8855 It is good to get in the habit of including all needed files in each .v
8856 file that needs it, rather than waiting for compile time. This will aid
8857 this process, Verilint, and readability. To prevent defining the same
8858 variable over and over when many modules are compiled together, put a test
8859 around the inside each include file:
8861 foo.v (an include file):
8862 `ifdef _FOO_V // include if not already included
8863 `else
8864 `define _FOO_V
8865 ... contents of file
8866 `endif // _FOO_V"
8867 ;;slow: (verilog-read-defines nil t))
8868 (save-excursion
8869 (verilog-getopt-flags)
8870 (goto-char (point-min))
8871 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
8872 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
8873 (verilog-read-defines inc nil t)))))
8875 (defun verilog-read-signals (&optional start end)
8876 "Return a simple list of all possible signals in the file.
8877 Bounded by optional region from START to END. Overly aggressive but fast.
8878 Some macros and such are also found and included. For dinotrace.el."
8879 (let (sigs-all keywd)
8880 (progn;save-excursion
8881 (goto-char (or start (point-min)))
8882 (setq end (or end (point-max)))
8883 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
8884 (forward-char -1)
8885 (cond
8886 ((looking-at "//")
8887 (search-forward "\n"))
8888 ((looking-at "/\\*")
8889 (search-forward "*/"))
8890 ((looking-at "(\\*")
8891 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
8892 (search-forward "*)")))
8893 ((eq ?\" (following-char))
8894 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
8895 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
8896 (goto-char (match-end 0))
8897 (setq keywd (match-string-no-properties 1))
8898 (or (member keywd verilog-keywords)
8899 (member keywd sigs-all)
8900 (setq sigs-all (cons keywd sigs-all))))
8901 (t (forward-char 1))))
8902 ;; Return list
8903 sigs-all)))
8906 ;; Argument file parsing
8909 (defun verilog-getopt (arglist)
8910 "Parse -f, -v etc arguments in ARGLIST list or string."
8911 (unless (listp arglist) (setq arglist (list arglist)))
8912 (let ((space-args '())
8913 arg next-param)
8914 ;; Split on spaces, so users can pass whole command lines
8915 (while arglist
8916 (setq arg (car arglist)
8917 arglist (cdr arglist))
8918 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
8919 (setq space-args (append space-args
8920 (list (match-string-no-properties 1 arg))))
8921 (setq arg (match-string 2 arg))))
8922 ;; Parse arguments
8923 (while space-args
8924 (setq arg (car space-args)
8925 space-args (cdr space-args))
8926 (cond
8927 ;; Need another arg
8928 ((equal arg "-f")
8929 (setq next-param arg))
8930 ((equal arg "-v")
8931 (setq next-param arg))
8932 ((equal arg "-y")
8933 (setq next-param arg))
8934 ;; +libext+(ext1)+(ext2)...
8935 ((string-match "^\\+libext\\+\\(.*\\)" arg)
8936 (setq arg (match-string 1 arg))
8937 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
8938 (verilog-add-list-unique `verilog-library-extensions
8939 (match-string 1 arg))
8940 (setq arg (match-string 2 arg))))
8942 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
8943 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
8944 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
8945 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
8946 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
8948 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
8949 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
8950 (verilog-add-list-unique `verilog-library-directories
8951 (match-string 1 (substitute-in-file-name arg))))
8952 ;; Ignore
8953 ((equal "+librescan" arg))
8954 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
8955 ;; Second parameters
8956 ((equal next-param "-f")
8957 (setq next-param nil)
8958 (verilog-getopt-file (substitute-in-file-name arg)))
8959 ((equal next-param "-v")
8960 (setq next-param nil)
8961 (verilog-add-list-unique `verilog-library-files
8962 (substitute-in-file-name arg)))
8963 ((equal next-param "-y")
8964 (setq next-param nil)
8965 (verilog-add-list-unique `verilog-library-directories
8966 (substitute-in-file-name arg)))
8967 ;; Filename
8968 ((string-match "^[^-+]" arg)
8969 (verilog-add-list-unique `verilog-library-files
8970 (substitute-in-file-name arg)))
8971 ;; Default - ignore; no warning
8972 ))))
8973 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
8975 (defun verilog-getopt-file (filename)
8976 "Read Verilog options from the specified FILENAME."
8977 (save-excursion
8978 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
8979 (orig-buffer (current-buffer))
8980 line)
8981 (if fns
8982 (set-buffer (find-file-noselect (car fns)))
8983 (error (concat (verilog-point-text)
8984 ": Can't find verilog-getopt-file -f file: " filename)))
8985 (goto-char (point-min))
8986 (while (not (eobp))
8987 (setq line (buffer-substring (point) (point-at-eol)))
8988 (forward-line 1)
8989 (when (string-match "//" line)
8990 (setq line (substring line 0 (match-beginning 0))))
8991 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
8992 (verilog-getopt line))))))
8994 (defun verilog-getopt-flags ()
8995 "Convert `verilog-library-flags' into standard library variables."
8996 ;; If the flags are local, then all the outputs should be local also
8997 (when (local-variable-p `verilog-library-flags (current-buffer))
8998 (mapc 'make-local-variable '(verilog-library-extensions
8999 verilog-library-directories
9000 verilog-library-files
9001 verilog-library-flags)))
9002 ;; Allow user to customize
9003 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
9004 ;; Process arguments
9005 (verilog-getopt verilog-library-flags)
9006 ;; Allow user to customize
9007 (verilog-run-hooks 'verilog-getopt-flags-hook))
9009 (defun verilog-add-list-unique (varref object)
9010 "Append to VARREF list the given OBJECT,
9011 unless it is already a member of the variable's list."
9012 (unless (member object (symbol-value varref))
9013 (set varref (append (symbol-value varref) (list object))))
9014 varref)
9015 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
9017 (defun verilog-current-flags ()
9018 "Convert `verilog-library-flags' and similar variables to command line.
9019 Used for __FLAGS__ in `verilog-expand-command'."
9020 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
9021 (when (equal cmd "")
9022 (setq cmd (concat
9023 "+libext+" (mapconcat `concat verilog-library-extensions "+")
9024 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
9025 verilog-library-directories "")
9026 (mapconcat (lambda (i) (concat " -v " i))
9027 verilog-library-files ""))))
9028 cmd))
9029 ;;(verilog-current-flags)
9033 ;; Cached directory support
9036 (defvar verilog-dir-cache-preserving nil
9037 "If set, the directory cache is enabled, and file system changes are ignored.
9038 See `verilog-dir-exists-p' and `verilog-dir-files'.")
9040 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
9041 (defvar verilog-dir-cache-list nil
9042 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
9043 (defvar verilog-dir-cache-lib-filenames nil
9044 "Cached data for `verilog-library-filenames'.")
9046 (defmacro verilog-preserve-dir-cache (&rest body)
9047 "Execute the BODY forms, allowing directory cache preservation within BODY.
9048 This means that changes inside BODY made to the file system will not be
9049 seen by the `verilog-dir-files' and related functions."
9050 `(let ((verilog-dir-cache-preserving (current-buffer))
9051 verilog-dir-cache-list
9052 verilog-dir-cache-lib-filenames)
9053 (progn ,@body)))
9055 (defun verilog-dir-files (dirname)
9056 "Return all filenames in the DIRNAME directory.
9057 Relative paths depend on the `default-directory'.
9058 Results are cached if inside `verilog-preserve-dir-cache'."
9059 (unless verilog-dir-cache-preserving
9060 (setq verilog-dir-cache-list nil)) ;; Cache disabled
9061 ;; We don't use expand-file-name on the dirname to make key, as it's slow
9062 (let* ((cache-key (list dirname default-directory))
9063 (fass (assoc cache-key verilog-dir-cache-list))
9064 exp-dirname data)
9065 (cond (fass ;; Return data from cache hit
9066 (nth 1 fass))
9068 (setq exp-dirname (expand-file-name dirname)
9069 data (and (file-directory-p exp-dirname)
9070 (directory-files exp-dirname nil nil nil)))
9071 ;; Note we also encache nil for non-existing dirs.
9072 (setq verilog-dir-cache-list (cons (list cache-key data)
9073 verilog-dir-cache-list))
9074 data))))
9075 ;; Miss-and-hit test:
9076 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
9077 ;; (prin1 (verilog-dir-files ".")) nil)
9079 (defun verilog-dir-file-exists-p (filename)
9080 "Return true if FILENAME exists.
9081 Like `file-exists-p' but results are cached if inside
9082 `verilog-preserve-dir-cache'."
9083 (let* ((dirname (file-name-directory filename))
9084 ;; Correct for file-name-nondirectory returning same if no slash.
9085 (dirnamed (if (or (not dirname) (equal dirname filename))
9086 default-directory dirname))
9087 (flist (verilog-dir-files dirnamed)))
9088 (and flist
9089 (member (file-name-nondirectory filename) flist)
9090 t)))
9091 ;;(verilog-dir-file-exists-p "verilog-mode.el")
9092 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
9096 ;; Module name lookup
9099 (defun verilog-module-inside-filename-p (module filename)
9100 "Return modi if MODULE is specified inside FILENAME, else nil.
9101 Allows version control to check out the file if need be."
9102 (and (or (file-exists-p filename)
9103 (and (fboundp 'vc-backend)
9104 (vc-backend filename)))
9105 (let (modi type)
9106 (with-current-buffer (find-file-noselect filename)
9107 (save-excursion
9108 (goto-char (point-min))
9109 (while (and
9110 ;; It may be tempting to look for verilog-defun-re,
9111 ;; don't, it slows things down a lot!
9112 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
9113 (setq type (match-string-no-properties 0))
9114 (verilog-re-search-forward-quick "[(;]" nil t))
9115 (if (equal module (verilog-read-module-name))
9116 (setq modi (verilog-modi-new module filename (point) type))))
9117 modi)))))
9119 (defun verilog-is-number (symbol)
9120 "Return true if SYMBOL is number-like."
9121 (or (string-match "^[0-9 \t:]+$" symbol)
9122 (string-match "^[---]*[0-9]+$" symbol)
9123 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
9125 (defun verilog-symbol-detick (symbol wing-it)
9126 "Return an expanded SYMBOL name without any defines.
9127 If the variable vh-{symbol} is defined, return that value.
9128 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
9129 (while (and symbol (string-match "^`" symbol))
9130 (setq symbol (substring symbol 1))
9131 (setq symbol
9132 (if (boundp (intern (concat "vh-" symbol)))
9133 ;; Emacs has a bug where boundp on a buffer-local
9134 ;; variable in only one buffer returns t in another.
9135 ;; This can confuse, so check for nil.
9136 (let ((val (eval (intern (concat "vh-" symbol)))))
9137 (if (eq val nil)
9138 (if wing-it symbol nil)
9139 val))
9140 (if wing-it symbol nil))))
9141 symbol)
9142 ;;(verilog-symbol-detick "`mod" nil)
9144 (defun verilog-symbol-detick-denumber (symbol)
9145 "Return SYMBOL with defines converted and any numbers dropped to nil."
9146 (when (string-match "^`" symbol)
9147 ;; This only will work if the define is a simple signal, not
9148 ;; something like a[b]. Sorry, it should be substituted into the parser
9149 (setq symbol
9150 (verilog-string-replace-matches
9151 "\[[^0-9: \t]+\]" "" nil nil
9152 (or (verilog-symbol-detick symbol nil)
9153 (if verilog-auto-sense-defines-constant
9155 symbol)))))
9156 (if (verilog-is-number symbol)
9158 symbol))
9160 (defun verilog-symbol-detick-text (text)
9161 "Return TEXT without any known defines.
9162 If the variable vh-{symbol} is defined, substitute that value."
9163 (let ((ok t) symbol val)
9164 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
9165 (setq symbol (match-string 1 text))
9166 ;;(message symbol)
9167 (cond ((and
9168 (boundp (intern (concat "vh-" symbol)))
9169 ;; Emacs has a bug where boundp on a buffer-local
9170 ;; variable in only one buffer returns t in another.
9171 ;; This can confuse, so check for nil.
9172 (setq val (eval (intern (concat "vh-" symbol)))))
9173 (setq text (replace-match val nil nil text)))
9174 (t (setq ok nil)))))
9175 text)
9176 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
9178 (defun verilog-expand-dirnames (&optional dirnames)
9179 "Return a list of existing directories given a list of wildcarded DIRNAMES.
9180 Or, just the existing dirnames themselves if there are no wildcards."
9181 ;; Note this function is performance critical.
9182 ;; Do not call anything that requires disk access that cannot be cached.
9183 (interactive)
9184 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
9185 (setq dirnames (reverse dirnames)) ; not nreverse
9186 (let ((dirlist nil)
9187 pattern dirfile dirfiles dirname root filename rest basefile)
9188 (while dirnames
9189 (setq dirname (substitute-in-file-name (car dirnames))
9190 dirnames (cdr dirnames))
9191 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
9192 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
9193 "\\(.*\\)") ;; rest
9194 dirname)
9195 (setq root (match-string 1 dirname)
9196 filename (match-string 2 dirname)
9197 rest (match-string 3 dirname)
9198 pattern filename)
9199 ;; now replace those * and ? with .+ and .
9200 ;; use ^ and /> to get only whole file names
9201 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9202 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
9203 pattern (concat "^" pattern "$")
9204 dirfiles (verilog-dir-files root))
9205 (while dirfiles
9206 (setq basefile (car dirfiles)
9207 dirfile (expand-file-name (concat root basefile rest))
9208 dirfiles (cdr dirfiles))
9209 (if (and (string-match pattern basefile)
9210 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9211 (not (equal basefile "."))
9212 (not (equal basefile ".."))
9213 (file-directory-p dirfile))
9214 (setq dirlist (cons dirfile dirlist)))))
9215 ;; Defaults
9217 (if (file-directory-p dirname)
9218 (setq dirlist (cons dirname dirlist))))))
9219 dirlist))
9220 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9222 (defun verilog-library-filenames (filename &optional current check-ext)
9223 "Return a search path to find the given FILENAME or module name.
9224 Uses the optional CURRENT filename or variable `buffer-file-name', plus
9225 `verilog-library-directories' and `verilog-library-extensions'
9226 variables to build the path. With optional CHECK-EXT also check
9227 `verilog-library-extensions'."
9228 (unless current (setq current (buffer-file-name)))
9229 (unless verilog-dir-cache-preserving
9230 (setq verilog-dir-cache-lib-filenames nil))
9231 (let* ((cache-key (list filename current check-ext))
9232 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9233 chkdirs chkdir chkexts fn outlist)
9234 (cond (fass ;; Return data from cache hit
9235 (nth 1 fass))
9237 ;; Note this expand can't be easily cached, as we need to
9238 ;; pick up buffer-local variables for newly read sub-module files
9239 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9240 (while chkdirs
9241 (setq chkdir (expand-file-name (car chkdirs)
9242 (file-name-directory current))
9243 chkexts (if check-ext verilog-library-extensions `("")))
9244 (while chkexts
9245 (setq fn (expand-file-name (concat filename (car chkexts))
9246 chkdir))
9247 ;;(message "Check for %s" fn)
9248 (if (verilog-dir-file-exists-p fn)
9249 (setq outlist (cons (expand-file-name
9250 fn (file-name-directory current))
9251 outlist)))
9252 (setq chkexts (cdr chkexts)))
9253 (setq chkdirs (cdr chkdirs)))
9254 (setq outlist (nreverse outlist))
9255 (setq verilog-dir-cache-lib-filenames
9256 (cons (list cache-key outlist)
9257 verilog-dir-cache-lib-filenames))
9258 outlist))))
9260 (defun verilog-module-filenames (module current)
9261 "Return a search path to find the given MODULE name.
9262 Uses the CURRENT filename, `verilog-library-extensions',
9263 `verilog-library-directories' and `verilog-library-files'
9264 variables to build the path."
9265 ;; Return search locations for it
9266 (append (list current) ; first, current buffer
9267 (verilog-library-filenames module current t)
9268 verilog-library-files)) ; finally, any libraries
9271 ;; Module Information
9273 ;; Many of these functions work on "modi" a module information structure
9274 ;; A modi is: [module-name-string file-name begin-point]
9276 (defvar verilog-cache-enabled t
9277 "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
9279 (defvar verilog-modi-cache-list nil
9280 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9281 For speeding up verilog-modi-get-* commands.
9282 Buffer-local.")
9283 (make-variable-buffer-local 'verilog-modi-cache-list)
9285 (defvar verilog-modi-cache-preserve-tick nil
9286 "Modification tick after which the cache is still considered valid.
9287 Use `verilog-preserve-modi-cache' to set it.")
9288 (defvar verilog-modi-cache-preserve-buffer nil
9289 "Modification tick after which the cache is still considered valid.
9290 Use `verilog-preserve-modi-cache' to set it.")
9291 (defvar verilog-modi-cache-current-enable nil
9292 "Non-nil means allow caching `verilog-modi-current', set by let().")
9293 (defvar verilog-modi-cache-current nil
9294 "Currently active `verilog-modi-current', if any, set by let().")
9295 (defvar verilog-modi-cache-current-max nil
9296 "Current endmodule point for `verilog-modi-cache-current', if any.")
9298 (defun verilog-modi-current ()
9299 "Return the modi structure for the module currently at point, possibly cached."
9300 (cond ((and verilog-modi-cache-current
9301 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9302 (<= (point) verilog-modi-cache-current-max))
9303 ;; Slow assertion, for debugging the cache:
9304 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9305 verilog-modi-cache-current)
9306 (verilog-modi-cache-current-enable
9307 (setq verilog-modi-cache-current (verilog-modi-current-get)
9308 verilog-modi-cache-current-max
9309 ;; The cache expires when we pass "endmodule" as then the
9310 ;; current modi may change to the next module
9311 ;; This relies on the AUTOs generally inserting, not deleting text
9312 (save-excursion
9313 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9314 verilog-modi-cache-current)
9316 (verilog-modi-current-get))))
9318 (defun verilog-modi-current-get ()
9319 "Return the modi structure for the module currently at point."
9320 (let* (name type pt)
9321 ;; read current module's name
9322 (save-excursion
9323 (verilog-re-search-backward-quick verilog-defun-re nil nil)
9324 (setq type (match-string-no-properties 0))
9325 (verilog-re-search-forward-quick "(" nil nil)
9326 (setq name (verilog-read-module-name))
9327 (setq pt (point)))
9328 ;; return modi - note this vector built two places
9329 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
9331 (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9332 (make-variable-buffer-local 'verilog-modi-lookup-cache)
9333 (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9334 (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
9336 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9337 "Find the file and point at which MODULE is defined.
9338 If ALLOW-CACHE is set, check and remember cache of previous lookups.
9339 Return modi if successful, else print message unless IGNORE-ERROR is true."
9340 (let* ((current (or (buffer-file-name) (current-buffer)))
9341 modi)
9342 ;; Check cache
9343 ;;(message "verilog-modi-lookup: %s" module)
9344 (cond ((and verilog-modi-lookup-cache
9345 verilog-cache-enabled
9346 allow-cache
9347 (setq modi (gethash module verilog-modi-lookup-cache))
9348 (equal verilog-modi-lookup-last-current current)
9349 ;; Iff hit is in current buffer, then tick must match
9350 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9351 (not (equal current (verilog-modi-file-or-buffer modi)))))
9352 ;;(message "verilog-modi-lookup: HIT %S" modi)
9353 modi)
9354 ;; Miss
9355 (t (let* ((realmod (verilog-symbol-detick module t))
9356 (orig-filenames (verilog-module-filenames realmod current))
9357 (filenames orig-filenames)
9358 mif)
9359 (while (and filenames (not mif))
9360 (if (not (setq mif (verilog-module-inside-filename-p realmod (car filenames))))
9361 (setq filenames (cdr filenames))))
9362 ;; mif has correct form to become later elements of modi
9363 (cond (mif (setq modi mif))
9364 (t (setq modi nil)
9365 (or ignore-error
9366 (error (concat (verilog-point-text)
9367 ": Can't locate " module " module definition"
9368 (if (not (equal module realmod))
9369 (concat " (Expanded macro to " realmod ")")
9371 "\n Check the verilog-library-directories variable."
9372 "\n I looked in (if not listed, doesn't exist):\n\t"
9373 (mapconcat 'concat orig-filenames "\n\t"))))))
9374 (when (eval-when-compile (fboundp 'make-hash-table))
9375 (unless verilog-modi-lookup-cache
9376 (setq verilog-modi-lookup-cache
9377 (make-hash-table :test 'equal :rehash-size 4.0)))
9378 (puthash module modi verilog-modi-lookup-cache))
9379 (setq verilog-modi-lookup-last-current current
9380 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9381 modi))
9383 (defun verilog-modi-filename (modi)
9384 "Filename of MODI, or name of buffer if it's never been saved."
9385 (if (bufferp (verilog-modi-file-or-buffer modi))
9386 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9387 (buffer-name (verilog-modi-file-or-buffer modi)))
9388 (verilog-modi-file-or-buffer modi)))
9390 (defun verilog-modi-goto (modi)
9391 "Move point/buffer to specified MODI."
9392 (or modi (error "Passed unfound modi to goto, check earlier"))
9393 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9394 (verilog-modi-file-or-buffer modi)
9395 (find-file-noselect (verilog-modi-file-or-buffer modi))))
9396 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
9397 (verilog-mode))
9398 (goto-char (verilog-modi-get-point modi)))
9400 (defun verilog-goto-defun-file (module)
9401 "Move point to the file at which a given MODULE is defined."
9402 (interactive "sGoto File for Module: ")
9403 (let* ((modi (verilog-modi-lookup module nil)))
9404 (when modi
9405 (verilog-modi-goto modi)
9406 (switch-to-buffer (current-buffer)))))
9408 (defun verilog-modi-cache-results (modi function)
9409 "Run on MODI the given FUNCTION. Locate the module in a file.
9410 Cache the output of function so next call may have faster access."
9411 (let (fass)
9412 (save-excursion ;; Cache is buffer-local so can't avoid this.
9413 (verilog-modi-goto modi)
9414 (if (and (setq fass (assoc (list modi function)
9415 verilog-modi-cache-list))
9416 ;; Destroy caching when incorrect; Modified or file changed
9417 (not (and verilog-cache-enabled
9418 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
9419 (and verilog-modi-cache-preserve-tick
9420 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
9421 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
9422 (equal (visited-file-modtime) (nth 2 fass)))))
9423 (setq verilog-modi-cache-list nil
9424 fass nil))
9425 (cond (fass
9426 ;; Return data from cache hit
9427 (nth 3 fass))
9429 ;; Read from file
9430 ;; Clear then restore any highlighting to make emacs19 happy
9431 (let (func-returns)
9432 (verilog-save-font-mods
9433 (setq func-returns (funcall function)))
9434 ;; Cache for next time
9435 (setq verilog-modi-cache-list
9436 (cons (list (list modi function)
9437 (buffer-chars-modified-tick)
9438 (visited-file-modtime)
9439 func-returns)
9440 verilog-modi-cache-list))
9441 func-returns))))))
9443 (defun verilog-modi-cache-add (modi function element sig-list)
9444 "Add function return results to the module cache.
9445 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
9446 function now contains the additional SIG-LIST parameters."
9447 (let (fass)
9448 (save-excursion
9449 (verilog-modi-goto modi)
9450 (if (setq fass (assoc (list modi function)
9451 verilog-modi-cache-list))
9452 (let ((func-returns (nth 3 fass)))
9453 (aset func-returns element
9454 (append sig-list (aref func-returns element))))))))
9456 (defmacro verilog-preserve-modi-cache (&rest body)
9457 "Execute the BODY forms, allowing cache preservation within BODY.
9458 This means that changes to the buffer will not result in the cache being
9459 flushed. If the changes affect the modsig state, they must call the
9460 modsig-cache-add-* function, else the results of later calls may be
9461 incorrect. Without this, changes are assumed to be adding/removing signals
9462 and invalidating the cache."
9463 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
9464 (verilog-modi-cache-preserve-buffer (current-buffer)))
9465 (progn ,@body)))
9468 (defun verilog-signals-matching-enum (in-list enum)
9469 "Return all signals in IN-LIST matching the given ENUM."
9470 (let (out-list)
9471 (while in-list
9472 (if (equal (verilog-sig-enum (car in-list)) enum)
9473 (setq out-list (cons (car in-list) out-list)))
9474 (setq in-list (cdr in-list)))
9475 ;; New scheme
9476 (let* ((enumvar (intern (concat "venum-" enum)))
9477 (enumlist (and (boundp enumvar) (eval enumvar))))
9478 (while enumlist
9479 (add-to-list 'out-list (list (car enumlist)))
9480 (setq enumlist (cdr enumlist))))
9481 (nreverse out-list)))
9483 (defun verilog-signals-matching-regexp (in-list regexp)
9484 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
9485 (if (or (not regexp) (equal regexp ""))
9486 in-list
9487 (let (out-list)
9488 (while in-list
9489 (if (string-match regexp (verilog-sig-name (car in-list)))
9490 (setq out-list (cons (car in-list) out-list)))
9491 (setq in-list (cdr in-list)))
9492 (nreverse out-list))))
9494 (defun verilog-signals-not-matching-regexp (in-list regexp)
9495 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
9496 (if (or (not regexp) (equal regexp ""))
9497 in-list
9498 (let (out-list)
9499 (while in-list
9500 (if (not (string-match regexp (verilog-sig-name (car in-list))))
9501 (setq out-list (cons (car in-list) out-list)))
9502 (setq in-list (cdr in-list)))
9503 (nreverse out-list))))
9505 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
9506 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
9507 if non-nil."
9508 (if (or (not regexp) (equal regexp ""))
9509 in-list
9510 (let (out-list to-match)
9511 (while in-list
9512 ;; Note verilog-insert-one-definition matches on this order
9513 (setq to-match (concat
9514 decl-type
9515 " " (verilog-sig-signed (car in-list))
9516 " " (verilog-sig-multidim (car in-list))
9517 (verilog-sig-bits (car in-list))))
9518 (if (string-match regexp to-match)
9519 (setq out-list (cons (car in-list) out-list)))
9520 (setq in-list (cdr in-list)))
9521 (nreverse out-list))))
9523 (defun verilog-signals-edit-wire-reg (in-list)
9524 "Return all signals in IN-LIST with wire/reg data types made blank."
9525 (mapcar (lambda (sig)
9526 (when (member (verilog-sig-type sig) '("wire" "reg"))
9527 (verilog-sig-type-set sig nil))
9528 sig) in-list))
9530 ;; Combined
9531 (defun verilog-decls-get-signals (decls)
9532 "Return all declared signals in DECLS, excluding 'assign' statements."
9533 (append
9534 (verilog-decls-get-outputs decls)
9535 (verilog-decls-get-inouts decls)
9536 (verilog-decls-get-inputs decls)
9537 (verilog-decls-get-vars decls)
9538 (verilog-decls-get-consts decls)
9539 (verilog-decls-get-gparams decls)))
9541 (defun verilog-decls-get-ports (decls)
9542 (append
9543 (verilog-decls-get-outputs decls)
9544 (verilog-decls-get-inouts decls)
9545 (verilog-decls-get-inputs decls)))
9547 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
9548 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
9549 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
9550 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
9551 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
9552 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
9553 (defsubst verilog-modi-cache-add-vars (modi sig-list)
9554 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
9558 ;; Auto creation utilities
9561 (defun verilog-auto-re-search-do (search-for func)
9562 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
9563 (goto-char (point-min))
9564 (while (verilog-re-search-forward-quick search-for nil t)
9565 (funcall func)))
9567 (defun verilog-insert-one-definition (sig type indent-pt)
9568 "Print out a definition for SIG of the given TYPE,
9569 with appropriate INDENT-PT indentation."
9570 (indent-to indent-pt)
9571 ;; Note verilog-signals-matching-dir-re matches on this order
9572 (insert type)
9573 (when (verilog-sig-modport sig)
9574 (insert "." (verilog-sig-modport sig)))
9575 (when (verilog-sig-signed sig)
9576 (insert " " (verilog-sig-signed sig)))
9577 (when (verilog-sig-multidim sig)
9578 (insert " " (verilog-sig-multidim-string sig)))
9579 (when (verilog-sig-bits sig)
9580 (insert " " (verilog-sig-bits sig)))
9581 (indent-to (max 24 (+ indent-pt 16)))
9582 (unless (= (char-syntax (preceding-char)) ?\ )
9583 (insert " ")) ; Need space between "]name" if indent-to did nothing
9584 (insert (verilog-sig-name sig))
9585 (when (verilog-sig-memory sig)
9586 (insert " " (verilog-sig-memory sig))))
9588 (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
9589 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
9590 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
9591 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
9592 When MODI is non-null, also add to modi-cache, for tracking."
9593 (when modi
9594 (cond ((equal direction "wire")
9595 (verilog-modi-cache-add-vars modi sigs))
9596 ((equal direction "reg")
9597 (verilog-modi-cache-add-vars modi sigs))
9598 ((equal direction "output")
9599 (verilog-modi-cache-add-outputs modi sigs)
9600 (when verilog-auto-declare-nettype
9601 (verilog-modi-cache-add-vars modi sigs)))
9602 ((equal direction "input")
9603 (verilog-modi-cache-add-inputs modi sigs)
9604 (when verilog-auto-declare-nettype
9605 (verilog-modi-cache-add-vars modi sigs)))
9606 ((equal direction "inout")
9607 (verilog-modi-cache-add-inouts modi sigs)
9608 (when verilog-auto-declare-nettype
9609 (verilog-modi-cache-add-vars modi sigs)))
9610 ((equal direction "interface"))
9612 (error "Unsupported verilog-insert-definition direction: %s" direction))))
9613 (or dont-sort
9614 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
9615 (while sigs
9616 (let ((sig (car sigs)))
9617 (verilog-insert-one-definition
9619 ;; Want "type x" or "output type x", not "wire type x"
9620 (cond ((or (verilog-sig-type sig)
9621 verilog-auto-wire-type)
9622 (concat
9623 (when (member direction '("input" "output" "inout"))
9624 (concat direction " "))
9625 (or (verilog-sig-type sig)
9626 verilog-auto-wire-type)))
9627 ((and verilog-auto-declare-nettype
9628 (member direction '("input" "output" "inout")))
9629 (concat direction " " verilog-auto-declare-nettype))
9631 direction))
9632 indent-pt)
9633 (insert (if v2k "," ";"))
9634 (if (or (not (verilog-sig-comment sig))
9635 (equal "" (verilog-sig-comment sig)))
9636 (insert "\n")
9637 (indent-to (max 48 (+ indent-pt 40)))
9638 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
9639 (setq sigs (cdr sigs)))))
9641 (eval-when-compile
9642 (if (not (boundp 'indent-pt))
9643 (defvar indent-pt nil "Local used by insert-indent")))
9645 (defun verilog-insert-indent (&rest stuff)
9646 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
9647 Presumes that any newlines end a list element."
9648 (let ((need-indent t))
9649 (while stuff
9650 (if need-indent (indent-to indent-pt))
9651 (setq need-indent nil)
9652 (verilog-insert (car stuff))
9653 (setq need-indent (string-match "\n$" (car stuff))
9654 stuff (cdr stuff)))))
9655 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
9657 (defun verilog-repair-open-comma ()
9658 "Insert comma if previous argument is other than an open parenthesis or endif."
9659 ;; We can't just search backward for ) as it might be inside another expression.
9660 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
9661 (save-excursion
9662 (verilog-backward-syntactic-ws-quick)
9663 (when (and (not (save-excursion ;; Not beginning (, or existing ,
9664 (backward-char 1)
9665 (looking-at "[(,]")))
9666 (not (save-excursion ;; Not `endif, or user define
9667 (backward-char 1)
9668 (skip-chars-backward "[a-zA-Z0-9_`]")
9669 (looking-at "`"))))
9670 (insert ","))))
9672 (defun verilog-repair-close-comma ()
9673 "If point is at a comma followed by a close parenthesis, fix it.
9674 This repairs those mis-inserted by an AUTOARG."
9675 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
9676 (save-excursion
9677 (verilog-forward-close-paren)
9678 (backward-char 1)
9679 (verilog-backward-syntactic-ws-quick)
9680 (backward-char 1)
9681 (when (looking-at ",")
9682 (delete-char 1))))
9684 (defun verilog-get-list (start end)
9685 "Return the elements of a comma separated list between START and END."
9686 (interactive)
9687 (let ((my-list (list))
9688 my-string)
9689 (save-excursion
9690 (while (< (point) end)
9691 (when (re-search-forward "\\([^,{]+\\)" end t)
9692 (setq my-string (verilog-string-remove-spaces (match-string 1)))
9693 (setq my-list (nconc my-list (list my-string) ))
9694 (goto-char (match-end 0))))
9695 my-list)))
9697 (defun verilog-make-width-expression (range-exp)
9698 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
9699 ;; strip off the []
9700 (cond ((not range-exp)
9701 "1")
9703 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
9704 (setq range-exp (match-string 1 range-exp)))
9705 (cond ((not range-exp)
9706 "1")
9707 ;; [#:#] We can compute a numeric result
9708 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
9709 range-exp)
9710 (int-to-string
9711 (1+ (abs (- (string-to-number (match-string 1 range-exp))
9712 (string-to-number (match-string 2 range-exp)))))))
9713 ;; [PARAM-1:0] can just return PARAM
9714 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
9715 (match-string 1 range-exp))
9716 ;; [arbitrary] need math
9717 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
9718 (concat "(1+(" (match-string 1 range-exp) ")"
9719 (if (equal "0" (match-string 2 range-exp))
9720 "" ;; Don't bother with -(0)
9721 (concat "-(" (match-string 2 range-exp) ")"))
9722 ")"))
9723 (t nil)))))
9724 ;;(verilog-make-width-expression "`A:`B")
9726 (defun verilog-simplify-range-expression (expr)
9727 "Return a simplified range expression with constants eliminated from EXPR."
9728 ;; Note this is always called with brackets; ie [z] or [z:z]
9729 (if (not (string-match "[---+*()]" expr))
9730 expr ;; short-circuit
9731 (let ((out expr)
9732 (last-pass ""))
9733 (while (not (equal last-pass out))
9734 (setq last-pass out)
9735 ;; Prefix regexp needs beginning of match, or some symbol of
9736 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
9737 ;; Ditto the end.
9738 (while (string-match
9739 (concat "\\([[({:*+-]\\)" ; - must be last
9740 "(\\<\\([0-9A-Za-z_]+\\))"
9741 "\\([])}:*+-]\\)")
9742 out)
9743 (setq out (replace-match "\\1\\2\\3" nil nil out)))
9744 ;; For precedence do * before +/-
9745 (while (string-match
9746 (concat "\\([[({:*+-]\\)"
9747 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
9748 "\\([])}:*+-]\\)")
9749 out)
9750 (setq out (replace-match
9751 (concat (match-string 1 out)
9752 (int-to-string (* (string-to-number (match-string 2 out))
9753 (string-to-number (match-string 4 out))))
9754 (match-string 5 out))
9755 nil nil out)))
9756 (while (string-match
9757 (concat "\\([[({:+-]\\)" ; No * here as higher prec
9758 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
9759 "\\([])}:+-]\\)")
9760 out)
9761 (let ((pre (match-string 1 out))
9762 (lhs (string-to-number (match-string 2 out)))
9763 (rhs (string-to-number (match-string 4 out)))
9764 (post (match-string 5 out))
9765 val)
9766 (when (equal pre "-")
9767 (setq lhs (- lhs)))
9768 (setq val (if (equal (match-string 3 out) "-")
9769 (- lhs rhs)
9770 (+ lhs rhs))
9771 out (replace-match
9772 (concat (if (and (equal pre "-")
9773 (< val 0))
9774 "" ;; Not "--20" but just "-20"
9775 pre)
9776 (int-to-string val)
9777 post)
9778 nil nil out)) )))
9779 out)))
9780 ;;(verilog-simplify-range-expression "[1:3]") ;; 1
9781 ;;(verilog-simplify-range-expression "[(1):3]") ;; 1
9782 ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ;;20
9783 ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ;; 48
9784 ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ;; FOO*4-2
9785 ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ;; FOO*4+0
9786 ;;(verilog-simplify-range-expression "[(func(BAR))]") ;; func(BAR)
9787 ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ;; FOO-0
9789 (defun verilog-typedef-name-p (variable-name)
9790 "Return true if the VARIABLE-NAME is a type definition."
9791 (when verilog-typedef-regexp
9792 (string-match verilog-typedef-regexp variable-name)))
9795 ;; Auto deletion
9798 (defun verilog-delete-autos-lined ()
9799 "Delete autos that occupy multiple lines, between begin and end comments."
9800 ;; The newline must not have a comment property, so we must
9801 ;; delete the end auto's newline, not the first newline
9802 (forward-line 1)
9803 (let ((pt (point)))
9804 (when (and
9805 (looking-at "\\s-*// Beginning")
9806 (search-forward "// End of automatic" nil t))
9807 ;; End exists
9808 (end-of-line)
9809 (forward-line 1)
9810 (delete-region pt (point)))))
9812 (defun verilog-delete-empty-auto-pair ()
9813 "Delete begin/end auto pair at point, if empty."
9814 (forward-line 0)
9815 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
9816 "\\s-*// End of automatics\n"))
9817 (delete-region (point) (save-excursion (forward-line 2) (point)))))
9819 (defun verilog-forward-close-paren ()
9820 "Find the close parenthesis that match the current point.
9821 Ignore other close parenthesis with matching open parens."
9822 (let ((parens 1))
9823 (while (> parens 0)
9824 (unless (verilog-re-search-forward-quick "[()]" nil t)
9825 (error "%s: Mismatching ()" (verilog-point-text)))
9826 (cond ((= (preceding-char) ?\( )
9827 (setq parens (1+ parens)))
9828 ((= (preceding-char) ?\) )
9829 (setq parens (1- parens)))))))
9831 (defun verilog-backward-open-paren ()
9832 "Find the open parenthesis that match the current point.
9833 Ignore other open parenthesis with matching close parens."
9834 (let ((parens 1))
9835 (while (> parens 0)
9836 (unless (verilog-re-search-backward-quick "[()]" nil t)
9837 (error "%s: Mismatching ()" (verilog-point-text)))
9838 (cond ((= (following-char) ?\) )
9839 (setq parens (1+ parens)))
9840 ((= (following-char) ?\( )
9841 (setq parens (1- parens)))))))
9843 (defun verilog-backward-open-bracket ()
9844 "Find the open bracket that match the current point.
9845 Ignore other open bracket with matching close bracket."
9846 (let ((parens 1))
9847 (while (> parens 0)
9848 (unless (verilog-re-search-backward-quick "[][]" nil t)
9849 (error "%s: Mismatching []" (verilog-point-text)))
9850 (cond ((= (following-char) ?\] )
9851 (setq parens (1+ parens)))
9852 ((= (following-char) ?\[ )
9853 (setq parens (1- parens)))))))
9855 (defun verilog-delete-to-paren ()
9856 "Delete the automatic inst/sense/arg created by autos.
9857 Deletion stops at the matching end parenthesis, outside comments."
9858 (delete-region (point)
9859 (save-excursion
9860 (verilog-backward-open-paren)
9861 (verilog-forward-sexp-ign-cmt 1) ;; Moves to paren that closes argdecl's
9862 (backward-char 1)
9863 (point))))
9865 (defun verilog-auto-star-safe ()
9866 "Return if a .* AUTOINST is safe to delete or expand.
9867 It was created by the AUTOS themselves, or by the user."
9868 (and verilog-auto-star-expand
9869 (looking-at
9870 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
9872 (defun verilog-delete-auto-star-all ()
9873 "Delete a .* AUTOINST, if it is safe."
9874 (when (verilog-auto-star-safe)
9875 (verilog-delete-to-paren)))
9877 (defun verilog-delete-auto-star-implicit ()
9878 "Delete all .* implicit connections created by `verilog-auto-star'.
9879 This function will be called automatically at save unless
9880 `verilog-auto-star-save' is set, any non-templated expanded pins will be
9881 removed."
9882 (interactive)
9883 (let (paren-pt indent have-close-paren)
9884 (save-excursion
9885 (goto-char (point-min))
9886 ;; We need to match these even outside of comments.
9887 ;; For reasonable performance, we don't check if inside comments, sorry.
9888 (while (re-search-forward "// Implicit \\.\\*" nil t)
9889 (setq paren-pt (point))
9890 (beginning-of-line)
9891 (setq have-close-paren
9892 (save-excursion
9893 (when (search-forward ");" paren-pt t)
9894 (setq indent (current-indentation))
9895 t)))
9896 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
9897 (when have-close-paren
9898 ;; Delete extra commentary
9899 (save-excursion
9900 (while (progn
9901 (forward-line -1)
9902 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
9903 (delete-region (match-beginning 0) (match-end 0))))
9904 ;; If it is simple, we can put the ); on the same line as the last text
9905 (let ((rtn-pt (point)))
9906 (save-excursion
9907 (while (progn (backward-char 1)
9908 (looking-at "[ \t\n\f]")))
9909 (when (looking-at ",")
9910 (delete-region (+ 1 (point)) rtn-pt))))
9911 (when (bolp)
9912 (indent-to indent))
9913 (insert ");\n")
9914 ;; Still need to kill final comma - always is one as we put one after the .*
9915 (re-search-backward ",")
9916 (delete-char 1))))))
9918 (defun verilog-delete-auto ()
9919 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
9920 Use \\[verilog-auto] to re-insert the updated AUTOs.
9922 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
9923 called before and after this function, respectively."
9924 (interactive)
9925 (save-excursion
9926 (if (buffer-file-name)
9927 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
9928 (verilog-save-no-change-functions
9929 (verilog-save-scan-cache
9930 ;; Allow user to customize
9931 (verilog-run-hooks 'verilog-before-delete-auto-hook)
9933 ;; Remove those that have multi-line insertions, possibly with parameters
9934 ;; We allow anything beginning with AUTO, so that users can add their own
9935 ;; patterns
9936 (verilog-auto-re-search-do
9937 (concat "/\\*AUTO[A-Za-z0-9_]+"
9938 ;; Optional parens or quoted parameter or .* for (((...)))
9939 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
9940 "\\*/")
9941 'verilog-delete-autos-lined)
9942 ;; Remove those that are in parenthesis
9943 (verilog-auto-re-search-do
9944 (concat "/\\*"
9945 (eval-when-compile
9946 (verilog-regexp-words
9947 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
9948 "AUTOSENSE")))
9949 "\\*/")
9950 'verilog-delete-to-paren)
9951 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
9952 (verilog-auto-re-search-do "\\.\\*"
9953 'verilog-delete-auto-star-all)
9954 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
9955 (goto-char (point-min))
9956 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
9957 (replace-match ""))
9959 ;; Final customize
9960 (verilog-run-hooks 'verilog-delete-auto-hook)))))
9963 ;; Auto inject
9966 (defun verilog-inject-auto ()
9967 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
9969 Any always @ blocks with sensitivity lists that match computed lists will
9970 be replaced with /*AS*/ comments.
9972 Any cells will get /*AUTOINST*/ added to the end of the pin list.
9973 Pins with have identical names will be deleted.
9975 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
9976 support adding new ports. You may wish to delete older ports yourself.
9978 For example:
9980 module ExampInject (i, o);
9981 input i;
9982 input j;
9983 output o;
9984 always @ (i or j)
9985 o = i | j;
9986 InstModule instName
9987 (.foobar(baz),
9988 j(j));
9989 endmodule
9991 Typing \\[verilog-inject-auto] will make this into:
9993 module ExampInject (i, o/*AUTOARG*/
9994 // Inputs
9996 input i;
9997 output o;
9998 always @ (/*AS*/i or j)
9999 o = i | j;
10000 InstModule instName
10001 (.foobar(baz),
10002 /*AUTOINST*/
10003 // Outputs
10004 j(j));
10005 endmodule"
10006 (interactive)
10007 (verilog-auto t))
10009 (defun verilog-inject-arg ()
10010 "Inject AUTOARG into new code. See `verilog-inject-auto'."
10011 ;; Presume one module per file.
10012 (save-excursion
10013 (goto-char (point-min))
10014 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
10015 (let ((endmodp (save-excursion
10016 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
10017 (point))))
10018 ;; See if there's already a comment .. inside a comment so not verilog-re-search
10019 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
10020 (verilog-re-search-forward-quick ";" nil t)
10021 (backward-char 1)
10022 (verilog-backward-syntactic-ws-quick)
10023 (backward-char 1) ; Moves to paren that closes argdecl's
10024 (when (looking-at ")")
10025 (verilog-insert "/*AUTOARG*/")))))))
10027 (defun verilog-inject-sense ()
10028 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
10029 (save-excursion
10030 (goto-char (point-min))
10031 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
10032 (let* ((start-pt (point))
10033 (modi (verilog-modi-current))
10034 (moddecls (verilog-modi-get-decls modi))
10035 pre-sigs
10036 got-sigs)
10037 (backward-char 1)
10038 (verilog-forward-sexp-ign-cmt 1)
10039 (backward-char 1) ;; End )
10040 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
10041 (setq pre-sigs (verilog-signals-from-signame
10042 (verilog-read-signals start-pt (point)))
10043 got-sigs (verilog-auto-sense-sigs moddecls nil))
10044 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
10045 (verilog-signals-not-in got-sigs pre-sigs)))
10046 (delete-region start-pt (point))
10047 (verilog-insert "/*AS*/")))))))
10049 (defun verilog-inject-inst ()
10050 "Inject AUTOINST into new code. See `verilog-inject-auto'."
10051 (save-excursion
10052 (goto-char (point-min))
10053 ;; It's hard to distinguish modules; we'll instead search for pins.
10054 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
10055 (verilog-backward-open-paren) ;; Inst start
10056 (cond
10057 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
10058 (forward-char 1)
10059 (verilog-forward-close-paren)) ;; Parameters done
10061 (forward-char 1)
10062 (let ((indent-pt (+ (current-column)))
10063 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
10064 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
10065 (goto-char end-pt)) ;; Already there, continue search with next instance
10067 ;; Delete identical interconnect
10068 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
10069 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
10070 (delete-region (match-beginning 0) (match-end 0))
10071 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
10072 (while (or (looking-at "[ \t\n\f,]+")
10073 (looking-at "//[^\n]*"))
10074 (delete-region (match-beginning 0) (match-end 0))
10075 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
10076 (verilog-forward-close-paren)
10077 (backward-char 1)
10078 ;; Not verilog-re-search, as we don't want to strip comments
10079 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
10080 (delete-region (match-beginning 0) (match-end 0)))
10081 (verilog-insert "\n")
10082 (verilog-insert-indent "/*AUTOINST*/")))))))))
10085 ;; Auto diff
10088 (defun verilog-diff-buffers-p (b1 b2 &optional whitespace)
10089 "Return nil if buffers B1 and B2 have same contents.
10090 Else, return point in B1 that first mismatches.
10091 If optional WHITESPACE true, ignore whitespace."
10092 (save-excursion
10093 (let* ((case-fold-search nil) ;; compare-buffer-substrings cares
10094 (p1 (with-current-buffer b1 (goto-char (point-min))))
10095 (p2 (with-current-buffer b2 (goto-char (point-min))))
10096 (maxp1 (with-current-buffer b1 (point-max)))
10097 (maxp2 (with-current-buffer b2 (point-max)))
10098 (op1 -1) (op2 -1)
10099 progress size)
10100 (while (not (and (eq p1 op1) (eq p2 op2)))
10101 ;; If both windows have whitespace optionally skip over it.
10102 (when whitespace
10103 ;; skip-syntax-* doesn't count \n
10104 (with-current-buffer b1
10105 (goto-char p1)
10106 (skip-chars-forward " \t\n\r\f\v")
10107 (setq p1 (point)))
10108 (with-current-buffer b2
10109 (goto-char p2)
10110 (skip-chars-forward " \t\n\r\f\v")
10111 (setq p2 (point))))
10112 (setq size (min (- maxp1 p1) (- maxp2 p2)))
10113 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
10114 b1 p1 (+ size p1)))
10115 (setq progress (if (zerop progress) size (1- (abs progress))))
10116 (setq op1 p1 op2 p2
10117 p1 (+ p1 progress)
10118 p2 (+ p2 progress)))
10119 ;; Return value
10120 (if (and (eq p1 maxp1) (eq p2 maxp2))
10121 nil p1))))
10123 (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
10124 "View the differences between file F1 and buffer B2.
10125 This requires the external program `diff-command' to be in your `exec-path',
10126 and uses `diff-switches' in which you may want to have \"-u\" flag.
10127 Ignores WHITESPACE if t, and writes output to stdout if SHOW."
10128 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
10129 ;; call `diff' as `diff' has different calling semantics on different
10130 ;; versions of Emacs.
10131 (if (not (file-exists-p f1))
10132 (message "Buffer %s has no associated file on disc" (buffer-name b2))
10133 (with-temp-buffer "*Verilog-Diff*"
10134 (let ((outbuf (current-buffer))
10135 (f2 (make-temp-file "vm-diff-auto-")))
10136 (unwind-protect
10137 (progn
10138 (with-current-buffer b2
10139 (save-restriction
10140 (widen)
10141 (write-region (point-min) (point-max) f2 nil 'nomessage)))
10142 (call-process diff-command nil outbuf t
10143 diff-switches ;; User may want -u in diff-switches
10144 (if whitespace "-b" "")
10145 f1 f2)
10146 ;; Print out results. Alternatively we could have call-processed
10147 ;; ourself, but this way we can reuse diff switches
10148 (when show
10149 (with-current-buffer outbuf (message "%s" (buffer-string))))))
10150 (sit-for 0)
10151 (when (file-exists-p f2)
10152 (delete-file f2))))))
10154 (defun verilog-diff-report (b1 b2 diffpt)
10155 "Report differences detected with `verilog-diff-auto'.
10156 Differences are between buffers B1 and B2, starting at point
10157 DIFFPT. This function is called via `verilog-diff-function'."
10158 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
10159 (verilog-warn "%s:%d: Difference in AUTO expansion found"
10160 name1 (with-current-buffer b1
10161 (1+ (count-lines (point-min) (point)))))
10162 (cond (noninteractive
10163 (verilog-diff-file-with-buffer name1 b2 t t))
10165 (ediff-buffers b1 b2)))))
10167 (defun verilog-diff-auto ()
10168 "Expand AUTOs in a temporary buffer and indicate any change.
10169 Whitespace differences are ignored to determine identicalness, but
10170 once a difference is detected, whitespace differences may be shown.
10172 To call this from the command line, see \\[verilog-batch-diff-auto].
10174 The action on differences is selected with
10175 `verilog-diff-function'. The default is `verilog-diff-report'
10176 which will report an error and run `ediff' in interactive mode,
10177 or `diff' in batch mode."
10178 (interactive)
10179 (let ((b1 (current-buffer)) b2 diffpt
10180 (name1 (buffer-file-name))
10181 (newname "*Verilog-Diff*"))
10182 (save-excursion
10183 (when (get-buffer newname)
10184 (kill-buffer newname))
10185 (setq b2 (let (buffer-file-name) ;; Else clone is upset
10186 (clone-buffer newname)))
10187 (with-current-buffer b2
10188 ;; auto requires the filename, but can't have same filename in two
10189 ;; buffers; so override both b1 and b2's names
10190 (let ((buffer-file-name name1))
10191 (unwind-protect
10192 (progn
10193 (with-current-buffer b1 (setq buffer-file-name nil))
10194 (verilog-auto)
10195 (when (not verilog-auto-star-save)
10196 (verilog-delete-auto-star-implicit)))
10197 ;; Restore name if unwind
10198 (with-current-buffer b1 (setq buffer-file-name name1)))))
10200 (setq diffpt (verilog-diff-buffers-p b1 b2 t))
10201 (cond ((not diffpt)
10202 (unless noninteractive (message "AUTO expansion identical"))
10203 (kill-buffer newname)) ;; Nice to cleanup after oneself
10205 (funcall verilog-diff-function b1 b2 diffpt)))
10206 ;; Return result of compare
10207 diffpt)))
10211 ;; Auto save
10214 (defun verilog-auto-save-check ()
10215 "On saving see if we need auto update."
10216 (cond ((not verilog-auto-save-policy)) ; disabled
10217 ((not (save-excursion
10218 (save-match-data
10219 (let ((case-fold-search nil))
10220 (goto-char (point-min))
10221 (re-search-forward "AUTO" nil t))))))
10222 ((eq verilog-auto-save-policy 'force)
10223 (verilog-auto))
10224 ((not (buffer-modified-p)))
10225 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
10226 ((eq verilog-auto-save-policy 'detect)
10227 (verilog-auto))
10229 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10230 (verilog-auto))
10231 ;; Don't ask again if didn't update
10232 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
10233 (when (not verilog-auto-star-save)
10234 (verilog-delete-auto-star-implicit))
10235 nil) ;; Always return nil -- we don't write the file ourselves
10237 (defun verilog-auto-read-locals ()
10238 "Return file local variable segment at bottom of file."
10239 (save-excursion
10240 (goto-char (point-max))
10241 (if (re-search-backward "Local Variables:" nil t)
10242 (buffer-substring-no-properties (point) (point-max))
10243 "")))
10245 (defun verilog-auto-reeval-locals (&optional force)
10246 "Read file local variable segment at bottom of file if it has changed.
10247 If FORCE, always reread it."
10248 (let ((curlocal (verilog-auto-read-locals)))
10249 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
10250 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
10251 ;; Note this may cause this function to be recursively invoked,
10252 ;; because hack-local-variables may call (verilog-mode)
10253 ;; The above when statement will prevent it from recursing forever.
10254 (hack-local-variables)
10255 t)))
10258 ;; Auto creation
10261 (defun verilog-auto-arg-ports (sigs message indent-pt)
10262 "Print a list of ports for an AUTOINST.
10263 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10264 (when sigs
10265 (when verilog-auto-arg-sort
10266 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10267 (insert "\n")
10268 (indent-to indent-pt)
10269 (insert message)
10270 (insert "\n")
10271 (let ((space ""))
10272 (indent-to indent-pt)
10273 (while sigs
10274 (cond ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10275 (insert "\n")
10276 (indent-to indent-pt))
10277 (t (insert space)))
10278 (insert (verilog-sig-name (car sigs)) ",")
10279 (setq sigs (cdr sigs)
10280 space " ")))))
10282 (defun verilog-auto-arg ()
10283 "Expand AUTOARG statements.
10284 Replace the argument declarations at the beginning of the
10285 module with ones automatically derived from input and output
10286 statements. This can be dangerous if the module is instantiated
10287 using position-based connections, so use only name-based when
10288 instantiating the resulting module. Long lines are split based
10289 on the `fill-column', see \\[set-fill-column].
10291 Limitations:
10292 Concatenation and outputting partial buses is not supported.
10294 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10296 For example:
10298 module ExampArg (/*AUTOARG*/);
10299 input i;
10300 output o;
10301 endmodule
10303 Typing \\[verilog-auto] will make this into:
10305 module ExampArg (/*AUTOARG*/
10306 // Outputs
10308 // Inputs
10311 input i;
10312 output o;
10313 endmodule
10315 The argument declarations may be printed in declaration order to best suit
10316 order based instantiations, or alphabetically, based on the
10317 `verilog-auto-arg-sort' variable.
10319 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10320 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
10321 conservative guess on adding a comma for the first signal, if you have
10322 any ifdefs or complicated expressions before the AUTOARG you will need
10323 to choose the comma yourself.
10325 Avoid declaring ports manually, as it makes code harder to maintain."
10326 (save-excursion
10327 (let* ((modi (verilog-modi-current))
10328 (moddecls (verilog-modi-get-decls modi))
10329 (skip-pins (aref (verilog-read-arg-pins) 0)))
10330 (verilog-repair-open-comma)
10331 (verilog-auto-arg-ports (verilog-signals-not-in
10332 (verilog-decls-get-outputs moddecls)
10333 skip-pins)
10334 "// Outputs"
10335 verilog-indent-level-declaration)
10336 (verilog-auto-arg-ports (verilog-signals-not-in
10337 (verilog-decls-get-inouts moddecls)
10338 skip-pins)
10339 "// Inouts"
10340 verilog-indent-level-declaration)
10341 (verilog-auto-arg-ports (verilog-signals-not-in
10342 (verilog-decls-get-inputs moddecls)
10343 skip-pins)
10344 "// Inputs"
10345 verilog-indent-level-declaration)
10346 (verilog-repair-close-comma)
10347 (unless (eq (char-before) ?/ )
10348 (insert "\n"))
10349 (indent-to verilog-indent-level-declaration))))
10351 (defun verilog-auto-inst-port-map (port-st)
10352 nil)
10354 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
10355 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10356 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
10357 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10358 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
10359 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
10360 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10361 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10363 (defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star par-values)
10364 "Print out an instantiation connection for this PORT-ST.
10365 Insert to INDENT-PT, use template TPL-LIST.
10366 @ are instantiation numbers, replaced with TPL-NUM.
10367 @\"(expression @)\" are evaluated, with @ as a variable.
10368 If FOR-STAR add comment it is a .* expansion.
10369 If PAR-VALUES replace final strings with these parameter values."
10370 (let* ((port (verilog-sig-name port-st))
10371 (tpl-ass (or (assoc port (car tpl-list))
10372 (verilog-auto-inst-port-map port-st)))
10373 ;; vl-* are documented for user use
10374 (vl-name (verilog-sig-name port-st))
10375 (vl-width (verilog-sig-width port-st))
10376 (vl-modport (verilog-sig-modport port-st))
10377 (vl-mbits (if (verilog-sig-multidim port-st)
10378 (verilog-sig-multidim-string port-st) ""))
10379 (vl-bits (if (or verilog-auto-inst-vector
10380 (not (assoc port vector-skip-list))
10381 (not (equal (verilog-sig-bits port-st)
10382 (verilog-sig-bits (assoc port vector-skip-list)))))
10383 (or (verilog-sig-bits port-st) "")
10384 ""))
10385 (case-fold-search nil)
10386 (check-values par-values)
10387 tpl-net)
10388 ;; Replace parameters in bit-width
10389 (when (and check-values
10390 (not (equal vl-bits "")))
10391 (while check-values
10392 (setq vl-bits (verilog-string-replace-matches
10393 (concat "\\<" (nth 0 (car check-values)) "\\>")
10394 (concat "(" (nth 1 (car check-values)) ")")
10395 t t vl-bits)
10396 vl-mbits (verilog-string-replace-matches
10397 (concat "\\<" (nth 0 (car check-values)) "\\>")
10398 (concat "(" (nth 1 (car check-values)) ")")
10399 t t vl-mbits)
10400 check-values (cdr check-values)))
10401 (setq vl-bits (verilog-simplify-range-expression vl-bits)
10402 vl-mbits (verilog-simplify-range-expression vl-mbits)
10403 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
10404 ;; Default net value if not found
10405 (setq tpl-net (concat port
10406 (if vl-modport (concat "." vl-modport) "")
10407 (if (verilog-sig-multidim port-st)
10408 (concat "/*" vl-mbits vl-bits "*/")
10409 (concat vl-bits))))
10410 ;; Find template
10411 (cond (tpl-ass ; Template of exact port name
10412 (setq tpl-net (nth 1 tpl-ass)))
10413 ((nth 1 tpl-list) ; Wildcards in template, search them
10414 (let ((wildcards (nth 1 tpl-list)))
10415 (while wildcards
10416 (when (string-match (nth 0 (car wildcards)) port)
10417 (setq tpl-ass (car wildcards) ; so allow @ parsing
10418 tpl-net (replace-match (nth 1 (car wildcards))
10419 t nil port)))
10420 (setq wildcards (cdr wildcards))))))
10421 ;; Parse Templated variable
10422 (when tpl-ass
10423 ;; Evaluate @"(lispcode)"
10424 (when (string-match "@\".*[^\\]\"" tpl-net)
10425 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
10426 (setq tpl-net
10427 (concat
10428 (substring tpl-net 0 (match-beginning 0))
10429 (save-match-data
10430 (let* ((expr (match-string 1 tpl-net))
10431 (value
10432 (progn
10433 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
10434 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
10435 (prin1 (eval (car (read-from-string expr)))
10436 (lambda (ch) ())))))
10437 (if (numberp value) (setq value (number-to-string value)))
10438 value))
10439 (substring tpl-net (match-end 0))))))
10440 ;; Replace @ and [] magic variables in final output
10441 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
10442 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
10443 ;; Insert it
10444 (indent-to indent-pt)
10445 (insert "." port)
10446 (unless (and verilog-auto-inst-dot-name
10447 (equal port tpl-net))
10448 (indent-to verilog-auto-inst-column)
10449 (insert "(" tpl-net ")"))
10450 (insert ",")
10451 (cond (tpl-ass
10452 (verilog-read-auto-template-hit tpl-ass)
10453 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10454 verilog-auto-inst-column))
10455 ;; verilog-insert requires the complete comment in one call - including the newline
10456 (cond ((equal verilog-auto-inst-template-numbers `lhs)
10457 (verilog-insert " // Templated"
10458 " LHS: " (nth 0 tpl-ass)
10459 "\n"))
10460 (verilog-auto-inst-template-numbers
10461 (verilog-insert " // Templated"
10462 " T" (int-to-string (nth 2 tpl-ass))
10463 " L" (int-to-string (nth 3 tpl-ass))
10464 "\n"))
10466 (verilog-insert " // Templated\n"))))
10467 (for-star
10468 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10469 verilog-auto-inst-column))
10470 (verilog-insert " // Implicit .\*\n")) ;For some reason the . or * must be escaped...
10472 (insert "\n")))))
10473 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
10474 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
10475 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
10477 (defun verilog-auto-inst-port-list (sig-list indent-pt tpl-list tpl-num for-star par-values)
10478 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
10479 (when verilog-auto-inst-sort
10480 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
10481 (mapc (lambda (port)
10482 (verilog-auto-inst-port port indent-pt
10483 tpl-list tpl-num for-star par-values))
10484 sig-list))
10486 (defun verilog-auto-inst-first ()
10487 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
10488 ;; Do we need a trailing comma?
10489 ;; There maybe an ifdef or something similar before us. What a mess. Thus
10490 ;; to avoid trouble we only insert on preceding ) or *.
10491 ;; Insert first port on new line
10492 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
10493 (save-excursion
10494 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
10495 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
10496 (forward-char 1)
10497 (insert ","))))
10499 (defun verilog-auto-star ()
10500 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
10502 If `verilog-auto-star-expand' is set, .* pins are treated if they were
10503 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
10504 will also ignore any .* that are not last in your pin list (this prevents
10505 it from deleting pins following the .* when it expands the AUTOINST.)
10507 On writing your file, unless `verilog-auto-star-save' is set, any
10508 non-templated expanded pins will be removed. You may do this at any time
10509 with \\[verilog-delete-auto-star-implicit].
10511 If you are converting a module to use .* for the first time, you may wish
10512 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
10514 See `verilog-auto-inst' for examples, templates, and more information."
10515 (when (verilog-auto-star-safe)
10516 (verilog-auto-inst)))
10518 (defun verilog-auto-inst ()
10519 "Expand AUTOINST statements, as part of \\[verilog-auto].
10520 Replace the pin connections to an instantiation or interface
10521 declaration with ones automatically derived from the module or
10522 interface header of the instantiated item.
10524 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
10525 and delete them before saving unless `verilog-auto-star-save' is set.
10526 See `verilog-auto-star' for more information.
10528 The pins are printed in declaration order or alphabetically,
10529 based on the `verilog-auto-inst-sort' variable.
10531 Limitations:
10532 Module names must be resolvable to filenames by adding a
10533 `verilog-library-extensions', and being found in the same directory, or
10534 by changing the variable `verilog-library-flags' or
10535 `verilog-library-directories'. Macros `modname are translated through the
10536 vh-{name} Emacs variable, if that is not found, it just ignores the `.
10538 In templates you must have one signal per line, ending in a ), or ));,
10539 and have proper () nesting, including a final ); to end the template.
10541 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10543 SystemVerilog multidimensional input/output has only experimental support.
10545 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
10547 Parameters referenced by the instantiation will remain symbolic, unless
10548 `verilog-auto-inst-param-value' is set.
10550 Gate primitives (and/or) may have AUTOINST for the purpose of
10551 AUTOWIRE declarations, etc. Gates are the only case when
10552 position based connections are passed.
10554 For example, first take the submodule InstModule.v:
10556 module InstModule (o,i);
10557 output [31:0] o;
10558 input i;
10559 wire [31:0] o = {32{i}};
10560 endmodule
10562 This is then used in an upper level module:
10564 module ExampInst (o,i);
10565 output o;
10566 input i;
10567 InstModule instName
10568 (/*AUTOINST*/);
10569 endmodule
10571 Typing \\[verilog-auto] will make this into:
10573 module ExampInst (o,i);
10574 output o;
10575 input i;
10576 InstModule instName
10577 (/*AUTOINST*/
10578 // Outputs
10579 .ov (ov[31:0]),
10580 // Inputs
10581 .i (i));
10582 endmodule
10584 Where the list of inputs and outputs came from the inst module.
10586 Exceptions:
10588 Unless you are instantiating a module multiple times, or the module is
10589 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
10590 It just makes for unmaintainable code. To sanitize signal names, try
10591 vrename from URL `http://www.veripool.org'.
10593 When you need to violate this suggestion there are two ways to list
10594 exceptions, placing them before the AUTOINST, or using templates.
10596 Any ports defined before the /*AUTOINST*/ are not included in the list of
10597 automatics. This is similar to making a template as described below, but
10598 is restricted to simple connections just like you normally make. Also note
10599 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
10600 you have the appropriate // Input or // Output comment, and exactly the
10601 same line formatting as AUTOINST itself uses.
10603 InstModule instName
10604 (// Inputs
10605 .i (my_i_dont_mess_with_it),
10606 /*AUTOINST*/
10607 // Outputs
10608 .ov (ov[31:0]));
10611 Templates:
10613 For multiple instantiations based upon a single template, create a
10614 commented out template:
10616 /* InstModule AUTO_TEMPLATE (
10617 .sig3 (sigz[]),
10621 Templates go ABOVE the instantiation(s). When an instantiation is
10622 expanded `verilog-mode' simply searches up for the closest template.
10623 Thus you can have multiple templates for the same module, just alternate
10624 between the template for an instantiation and the instantiation itself.
10625 (For backward compatibility if no template is found above, it
10626 will also look below, but do not use this behavior in new designs.)
10628 The module name must be the same as the name of the module in the
10629 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
10630 words and capitalized. Only signals that must be different for each
10631 instantiation need to be listed.
10633 Inside a template, a [] in a connection name (with nothing else inside
10634 the brackets) will be replaced by the same bus subscript as it is being
10635 connected to, or the [] will be removed if it is a single bit signal.
10636 Generally it is a good idea to do this for all connections in a template,
10637 as then they will work for any width signal, and with AUTOWIRE. See
10638 PTL_BUS becoming PTL_BUSNEW below.
10640 If you have a complicated template, set `verilog-auto-inst-template-numbers'
10641 to see which regexps are matching. Don't leave that mode set after
10642 debugging is completed though, it will result in lots of extra differences
10643 and merge conflicts.
10645 Setting `verilog-auto-template-warn-unused' will report errors
10646 if any template lines are unused.
10648 For example:
10650 /* InstModule AUTO_TEMPLATE (
10651 .ptl_bus (ptl_busnew[]),
10654 InstModule ms2m (/*AUTOINST*/);
10656 Typing \\[verilog-auto] will make this into:
10658 InstModule ms2m (/*AUTOINST*/
10659 // Outputs
10660 .NotInTemplate (NotInTemplate),
10661 .ptl_bus (ptl_busnew[3:0]), // Templated
10662 ....
10665 Multiple Module Templates:
10667 The same template lines can be applied to multiple modules with
10668 the syntax as follows:
10670 /* InstModuleA AUTO_TEMPLATE
10671 InstModuleB AUTO_TEMPLATE
10672 InstModuleC AUTO_TEMPLATE
10673 InstModuleD AUTO_TEMPLATE (
10674 .ptl_bus (ptl_busnew[]),
10678 Note there is only one AUTO_TEMPLATE opening parenthesis.
10680 @ Templates:
10682 It is common to instantiate a cell multiple times, so templates make it
10683 trivial to substitute part of the cell name into the connection name.
10685 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
10686 .sig1 (sigx[@]),
10687 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
10691 If no regular expression is provided immediately after the AUTO_TEMPLATE
10692 keyword, then the @ character in any connection names will be replaced
10693 with the instantiation number; the first digits found in the cell's
10694 instantiation name.
10696 If a regular expression is provided, the @ character will be replaced
10697 with the first \(\) grouping that matches against the cell name. Using a
10698 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
10699 regexp is provided. If you use multiple layers of parenthesis,
10700 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
10701 characters after test and before _, whereas
10702 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
10703 match.
10705 For example:
10707 /* InstModule AUTO_TEMPLATE (
10708 .ptl_mapvalidx (ptl_mapvalid[@]),
10709 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
10712 InstModule ms2m (/*AUTOINST*/);
10714 Typing \\[verilog-auto] will make this into:
10716 InstModule ms2m (/*AUTOINST*/
10717 // Outputs
10718 .ptl_mapvalidx (ptl_mapvalid[2]),
10719 .ptl_mapvalidp1x (ptl_mapvalid[3]));
10721 Note the @ character was replaced with the 2 from \"ms2m\".
10723 Alternatively, using a regular expression for @:
10725 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
10726 .ptl_mapvalidx (@_ptl_mapvalid),
10727 .ptl_mapvalidp1x (ptl_mapvalid_@),
10730 InstModule ms2_FOO (/*AUTOINST*/);
10731 InstModule ms2_BAR (/*AUTOINST*/);
10733 Typing \\[verilog-auto] will make this into:
10735 InstModule ms2_FOO (/*AUTOINST*/
10736 // Outputs
10737 .ptl_mapvalidx (FOO_ptl_mapvalid),
10738 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
10739 InstModule ms2_BAR (/*AUTOINST*/
10740 // Outputs
10741 .ptl_mapvalidx (BAR_ptl_mapvalid),
10742 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
10745 Regexp Templates:
10747 A template entry of the form
10749 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
10751 will apply an Emacs style regular expression search for any port beginning
10752 in pci_req followed by numbers and ending in _l and connecting that to
10753 the pci_req_jtag_[] net, with the bus subscript coming from what matches
10754 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
10756 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
10757 does the same thing. (Note a @ in the connection/replacement text is
10758 completely different -- still use \\1 there!) Thus this is the same as
10759 the above template:
10761 .pci_req@_l (pci_req_jtag_[\\1]),
10763 Here's another example to remove the _l, useful when naming conventions
10764 specify _ alone to mean active low. Note the use of [] to keep the bus
10765 subscript:
10767 .\\(.*\\)_l (\\1_[]),
10769 Lisp Templates:
10771 First any regular expression template is expanded.
10773 If the syntax @\"( ... )\" is found in a connection, the expression in
10774 quotes will be evaluated as a Lisp expression, with @ replaced by the
10775 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
10776 4 into the brackets. Quote all double-quotes inside the expression with
10777 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
10778 regexp template backslash the backslash quote (\\\\\"...\\\\\").
10780 There are special variables defined that are useful in these
10781 Lisp functions:
10783 vl-name Name portion of the input/output port.
10784 vl-bits Bus bits portion of the input/output port ('[2:0]').
10785 vl-mbits Multidimensional array bits for port ('[2:0][3:0]').
10786 vl-width Width of the input/output port ('3' for [2:0]).
10787 May be a (...) expression if bits isn't a constant.
10788 vl-dir Direction of the pin input/output/inout/interface.
10789 vl-modport The modport, if an interface with a modport.
10790 vl-cell-type Module name/type of the cell ('InstModule').
10791 vl-cell-name Instance name of the cell ('instName').
10793 Normal Lisp variables may be used in expressions. See
10794 `verilog-read-defines' which can set vh-{definename} variables for use
10795 here. Also, any comments of the form:
10797 /*AUTO_LISP(setq foo 1)*/
10799 will evaluate any Lisp expression inside the parenthesis between the
10800 beginning of the buffer and the point of the AUTOINST. This allows
10801 functions to be defined or variables to be changed between instantiations.
10802 (See also `verilog-auto-insert-lisp' if you want the output from your
10803 lisp function to be inserted.)
10805 Note that when using lisp expressions errors may occur when @ is not a
10806 number; you may need to use the standard Emacs Lisp functions
10807 `number-to-string' and `string-to-number'.
10809 After the evaluation is completed, @ substitution and [] substitution
10810 occur.
10812 For more information see the \\[verilog-faq] and forums at URL
10813 `http://www.veripool.org'."
10814 (save-excursion
10815 ;; Find beginning
10816 (let* ((pt (point))
10817 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
10818 (indent-pt (save-excursion (verilog-backward-open-paren)
10819 (1+ (current-column))))
10820 (verilog-auto-inst-column (max verilog-auto-inst-column
10821 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
10822 (modi (verilog-modi-current))
10823 (moddecls (verilog-modi-get-decls modi))
10824 (vector-skip-list (unless verilog-auto-inst-vector
10825 (verilog-decls-get-signals moddecls)))
10826 submod submodi submoddecls
10827 inst skip-pins tpl-list tpl-num did-first par-values)
10829 ;; Find module name that is instantiated
10830 (setq submod (verilog-read-inst-module)
10831 inst (verilog-read-inst-name)
10832 vl-cell-type submod
10833 vl-cell-name inst
10834 skip-pins (aref (verilog-read-inst-pins) 0))
10836 ;; Parse any AUTO_LISP() before here
10837 (verilog-read-auto-lisp (point-min) pt)
10839 ;; Read parameters (after AUTO_LISP)
10840 (setq par-values (and verilog-auto-inst-param-value
10841 (verilog-read-inst-param-value)))
10843 ;; Lookup position, etc of submodule
10844 ;; Note this may raise an error
10845 (when (and (not (member submod verilog-gate-keywords))
10846 (setq submodi (verilog-modi-lookup submod t)))
10847 (setq submoddecls (verilog-modi-get-decls submodi))
10848 ;; If there's a number in the instantiation, it may be an argument to the
10849 ;; automatic variable instantiation program.
10850 (let* ((tpl-info (verilog-read-auto-template submod))
10851 (tpl-regexp (aref tpl-info 0)))
10852 (setq tpl-num (if (string-match tpl-regexp inst)
10853 (match-string 1 inst)
10855 tpl-list (aref tpl-info 1)))
10856 ;; Find submodule's signals and dump
10857 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
10858 (verilog-signals-not-in
10859 (verilog-decls-get-vars submoddecls)
10860 skip-pins)))
10861 (vl-dir "interfaced"))
10862 (when (and sig-list
10863 verilog-auto-inst-interfaced-ports)
10864 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10865 ;; Note these are searched for in verilog-read-sub-decls.
10866 (verilog-insert-indent "// Interfaced\n")
10867 (verilog-auto-inst-port-list sig-list indent-pt
10868 tpl-list tpl-num for-star par-values)))
10869 (let ((sig-list (verilog-signals-not-in
10870 (verilog-decls-get-interfaces submoddecls)
10871 skip-pins))
10872 (vl-dir "interface"))
10873 (when sig-list
10874 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10875 ;; Note these are searched for in verilog-read-sub-decls.
10876 (verilog-insert-indent "// Interfaces\n")
10877 (verilog-auto-inst-port-list sig-list indent-pt
10878 tpl-list tpl-num for-star par-values)))
10879 (let ((sig-list (verilog-signals-not-in
10880 (verilog-decls-get-outputs submoddecls)
10881 skip-pins))
10882 (vl-dir "output"))
10883 (when sig-list
10884 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10885 (verilog-insert-indent "// Outputs\n")
10886 (verilog-auto-inst-port-list sig-list indent-pt
10887 tpl-list tpl-num for-star par-values)))
10888 (let ((sig-list (verilog-signals-not-in
10889 (verilog-decls-get-inouts submoddecls)
10890 skip-pins))
10891 (vl-dir "inout"))
10892 (when sig-list
10893 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10894 (verilog-insert-indent "// Inouts\n")
10895 (verilog-auto-inst-port-list sig-list indent-pt
10896 tpl-list tpl-num for-star par-values)))
10897 (let ((sig-list (verilog-signals-not-in
10898 (verilog-decls-get-inputs submoddecls)
10899 skip-pins))
10900 (vl-dir "input"))
10901 (when sig-list
10902 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
10903 (verilog-insert-indent "// Inputs\n")
10904 (verilog-auto-inst-port-list sig-list indent-pt
10905 tpl-list tpl-num for-star par-values)))
10906 ;; Kill extra semi
10907 (save-excursion
10908 (cond (did-first
10909 (re-search-backward "," pt t)
10910 (delete-char 1)
10911 (insert ");")
10912 (search-forward "\n") ;; Added by inst-port
10913 (delete-char -1)
10914 (if (search-forward ")" nil t) ;; From user, moved up a line
10915 (delete-char -1))
10916 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
10917 (delete-char -1)))))))))
10919 (defun verilog-auto-inst-param ()
10920 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
10921 Replace the parameter connections to an instantiation with ones
10922 automatically derived from the module header of the instantiated netlist.
10924 See \\[verilog-auto-inst] for limitations, and templates to customize the
10925 output.
10927 For example, first take the submodule InstModule.v:
10929 module InstModule (o,i);
10930 parameter PAR;
10931 endmodule
10933 This is then used in an upper level module:
10935 module ExampInst (o,i);
10936 parameter PAR;
10937 InstModule #(/*AUTOINSTPARAM*/)
10938 instName (/*AUTOINST*/);
10939 endmodule
10941 Typing \\[verilog-auto] will make this into:
10943 module ExampInst (o,i);
10944 output o;
10945 input i;
10946 InstModule #(/*AUTOINSTPARAM*/
10947 // Parameters
10948 .PAR (PAR));
10949 instName (/*AUTOINST*/);
10950 endmodule
10952 Where the list of parameter connections come from the inst module.
10954 Templates:
10956 You can customize the parameter connections using AUTO_TEMPLATEs,
10957 just as you would with \\[verilog-auto-inst]."
10958 (save-excursion
10959 ;; Find beginning
10960 (let* ((pt (point))
10961 (indent-pt (save-excursion (verilog-backward-open-paren)
10962 (1+ (current-column))))
10963 (verilog-auto-inst-column (max verilog-auto-inst-column
10964 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
10965 (modi (verilog-modi-current))
10966 (moddecls (verilog-modi-get-decls modi))
10967 (vector-skip-list (unless verilog-auto-inst-vector
10968 (verilog-decls-get-signals moddecls)))
10969 submod submodi submoddecls
10970 inst skip-pins tpl-list tpl-num did-first)
10971 ;; Find module name that is instantiated
10972 (setq submod (save-excursion
10973 ;; Get to the point where AUTOINST normally is to read the module
10974 (verilog-re-search-forward-quick "[(;]" nil nil)
10975 (verilog-read-inst-module))
10976 inst (save-excursion
10977 ;; Get to the point where AUTOINST normally is to read the module
10978 (verilog-re-search-forward-quick "[(;]" nil nil)
10979 (verilog-read-inst-name))
10980 vl-cell-type submod
10981 vl-cell-name inst
10982 skip-pins (aref (verilog-read-inst-pins) 0))
10984 ;; Parse any AUTO_LISP() before here
10985 (verilog-read-auto-lisp (point-min) pt)
10987 ;; Lookup position, etc of submodule
10988 ;; Note this may raise an error
10989 (when (setq submodi (verilog-modi-lookup submod t))
10990 (setq submoddecls (verilog-modi-get-decls submodi))
10991 ;; If there's a number in the instantiation, it may be an argument to the
10992 ;; automatic variable instantiation program.
10993 (let* ((tpl-info (verilog-read-auto-template submod))
10994 (tpl-regexp (aref tpl-info 0)))
10995 (setq tpl-num (if (string-match tpl-regexp inst)
10996 (match-string 1 inst)
10998 tpl-list (aref tpl-info 1)))
10999 ;; Find submodule's signals and dump
11000 (let ((sig-list (verilog-signals-not-in
11001 (verilog-decls-get-gparams submoddecls)
11002 skip-pins))
11003 (vl-dir "parameter"))
11004 (when sig-list
11005 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11006 ;; Note these are searched for in verilog-read-sub-decls.
11007 (verilog-insert-indent "// Parameters\n")
11008 (verilog-auto-inst-port-list sig-list indent-pt
11009 tpl-list tpl-num nil nil)))
11010 ;; Kill extra semi
11011 (save-excursion
11012 (cond (did-first
11013 (re-search-backward "," pt t)
11014 (delete-char 1)
11015 (insert ")")
11016 (search-forward "\n") ;; Added by inst-port
11017 (delete-char -1)
11018 (if (search-forward ")" nil t) ;; From user, moved up a line
11019 (delete-char -1)))))))))
11021 (defun verilog-auto-reg ()
11022 "Expand AUTOREG statements, as part of \\[verilog-auto].
11023 Make reg statements for any output that isn't already declared,
11024 and isn't a wire output from a block. `verilog-auto-wire-type'
11025 may be used to change the datatype of the declarations.
11027 Limitations:
11028 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11030 This does NOT work on memories, declare those yourself.
11032 An example:
11034 module ExampReg (o,i);
11035 output o;
11036 input i;
11037 /*AUTOREG*/
11038 always o = i;
11039 endmodule
11041 Typing \\[verilog-auto] will make this into:
11043 module ExampReg (o,i);
11044 output o;
11045 input i;
11046 /*AUTOREG*/
11047 // Beginning of automatic regs (for this module's undeclared outputs)
11048 reg o;
11049 // End of automatics
11050 always o = i;
11051 endmodule"
11052 (save-excursion
11053 ;; Point must be at insertion point.
11054 (let* ((indent-pt (current-indentation))
11055 (modi (verilog-modi-current))
11056 (moddecls (verilog-modi-get-decls modi))
11057 (modsubdecls (verilog-modi-get-sub-decls modi))
11058 (sig-list (verilog-signals-not-in
11059 (verilog-decls-get-outputs moddecls)
11060 (append (verilog-signals-with ;; ignore typed signals
11061 'verilog-sig-type
11062 (verilog-decls-get-outputs moddecls))
11063 (verilog-decls-get-vars moddecls)
11064 (verilog-decls-get-assigns moddecls)
11065 (verilog-decls-get-consts moddecls)
11066 (verilog-decls-get-gparams moddecls)
11067 (verilog-subdecls-get-interfaced modsubdecls)
11068 (verilog-subdecls-get-outputs modsubdecls)
11069 (verilog-subdecls-get-inouts modsubdecls)))))
11070 (forward-line 1)
11071 (when sig-list
11072 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
11073 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11074 (verilog-insert-indent "// End of automatics\n")))))
11076 (defun verilog-auto-reg-input ()
11077 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
11078 Make reg statements instantiation inputs that aren't already declared.
11079 This is useful for making a top level shell for testing the module that is
11080 to be instantiated.
11082 Limitations:
11083 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
11085 This does NOT work on memories, declare those yourself.
11087 An example (see `verilog-auto-inst' for what else is going on here):
11089 module ExampRegInput (o,i);
11090 output o;
11091 input i;
11092 /*AUTOREGINPUT*/
11093 InstModule instName
11094 (/*AUTOINST*/);
11095 endmodule
11097 Typing \\[verilog-auto] will make this into:
11099 module ExampRegInput (o,i);
11100 output o;
11101 input i;
11102 /*AUTOREGINPUT*/
11103 // Beginning of automatic reg inputs (for undeclared ...
11104 reg [31:0] iv; // From inst of inst.v
11105 // End of automatics
11106 InstModule instName
11107 (/*AUTOINST*/
11108 // Outputs
11109 .o (o[31:0]),
11110 // Inputs
11111 .iv (iv));
11112 endmodule"
11113 (save-excursion
11114 ;; Point must be at insertion point.
11115 (let* ((indent-pt (current-indentation))
11116 (modi (verilog-modi-current))
11117 (moddecls (verilog-modi-get-decls modi))
11118 (modsubdecls (verilog-modi-get-sub-decls modi))
11119 (sig-list (verilog-signals-combine-bus
11120 (verilog-signals-not-in
11121 (append (verilog-subdecls-get-inputs modsubdecls)
11122 (verilog-subdecls-get-inouts modsubdecls))
11123 (append (verilog-decls-get-signals moddecls)
11124 (verilog-decls-get-assigns moddecls))))))
11125 (forward-line 1)
11126 (when sig-list
11127 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
11128 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11129 (verilog-insert-indent "// End of automatics\n")))))
11131 (defun verilog-auto-logic-setup ()
11132 "Prepare variables due to AUTOLOGIC."
11133 (unless verilog-auto-wire-type
11134 (set (make-local-variable 'verilog-auto-wire-type)
11135 "logic")))
11137 (defun verilog-auto-logic ()
11138 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
11139 Make wire statements using the SystemVerilog logic keyword.
11140 This is currently equivalent to:
11142 /*AUTOWIRE*/
11144 with the below at the bottom of the file
11146 // Local Variables:
11147 // verilog-auto-logic-type:\"logic\"
11148 // End:
11150 In the future AUTOLOGIC may declare additional identifiers,
11151 while AUTOWIRE will not."
11152 (save-excursion
11153 (verilog-auto-logic-setup)
11154 (verilog-auto-wire)))
11156 (defun verilog-auto-wire ()
11157 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
11158 Make wire statements for instantiations outputs that aren't
11159 already declared. `verilog-auto-wire-type' may be used to change
11160 the datatype of the declarations.
11162 Limitations:
11163 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
11164 and all buses must have widths, such as those from AUTOINST, or using []
11165 in AUTO_TEMPLATEs.
11167 This does NOT work on memories or SystemVerilog .name connections,
11168 declare those yourself.
11170 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
11171 determine how to bus together. This occurs when you have ports with
11172 non-numeric or non-sequential bus subscripts. If Verilog mode
11173 mis-guessed, you'll have to declare them yourself.
11175 An example (see `verilog-auto-inst' for what else is going on here):
11177 module ExampWire (o,i);
11178 output o;
11179 input i;
11180 /*AUTOWIRE*/
11181 InstModule instName
11182 (/*AUTOINST*/);
11183 endmodule
11185 Typing \\[verilog-auto] will make this into:
11187 module ExampWire (o,i);
11188 output o;
11189 input i;
11190 /*AUTOWIRE*/
11191 // Beginning of automatic wires
11192 wire [31:0] ov; // From inst of inst.v
11193 // End of automatics
11194 InstModule instName
11195 (/*AUTOINST*/
11196 // Outputs
11197 .ov (ov[31:0]),
11198 // Inputs
11199 .i (i));
11200 wire o = | ov;
11201 endmodule"
11202 (save-excursion
11203 ;; Point must be at insertion point.
11204 (let* ((indent-pt (current-indentation))
11205 (modi (verilog-modi-current))
11206 (moddecls (verilog-modi-get-decls modi))
11207 (modsubdecls (verilog-modi-get-sub-decls modi))
11208 (sig-list (verilog-signals-combine-bus
11209 (verilog-signals-not-in
11210 (append (verilog-subdecls-get-outputs modsubdecls)
11211 (verilog-subdecls-get-inouts modsubdecls))
11212 (verilog-decls-get-signals moddecls)))))
11213 (forward-line 1)
11214 (when sig-list
11215 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
11216 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
11217 (verilog-insert-indent "// End of automatics\n")
11218 ;; We used to optionally call verilog-pretty-declarations and
11219 ;; verilog-pretty-expr here, but it's too slow on huge modules,
11220 ;; plus makes everyone's module change. Finally those call
11221 ;; syntax-ppss which is broken when change hooks are disabled.
11222 ))))
11224 (defun verilog-auto-output (&optional with-params)
11225 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
11226 Make output statements for any output signal from an /*AUTOINST*/ that
11227 isn't an input to another AUTOINST. This is useful for modules which
11228 only instantiate other modules.
11230 Limitations:
11231 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11233 If placed inside the parenthesis of a module declaration, it creates
11234 Verilog 2001 style, else uses Verilog 1995 style.
11236 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11237 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11239 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11241 Signals matching `verilog-auto-output-ignore-regexp' are not included.
11243 An example (see `verilog-auto-inst' for what else is going on here):
11245 module ExampOutput (ov,i);
11246 input i;
11247 /*AUTOOUTPUT*/
11248 InstModule instName
11249 (/*AUTOINST*/);
11250 endmodule
11252 Typing \\[verilog-auto] will make this into:
11254 module ExampOutput (ov,i);
11255 input i;
11256 /*AUTOOUTPUT*/
11257 // Beginning of automatic outputs (from unused autoinst outputs)
11258 output [31:0] ov; // From inst of inst.v
11259 // End of automatics
11260 InstModule instName
11261 (/*AUTOINST*/
11262 // Outputs
11263 .ov (ov[31:0]),
11264 // Inputs
11265 .i (i));
11266 endmodule
11268 You may also provide an optional regular expression, in which case only
11269 signals matching the regular expression will be included. For example the
11270 same expansion will result from only extracting outputs starting with ov:
11272 /*AUTOOUTPUT(\"^ov\")*/"
11273 (save-excursion
11274 ;; Point must be at insertion point.
11275 (let* ((indent-pt (current-indentation))
11276 (regexp (and with-params
11277 (nth 0 (verilog-read-auto-params 1))))
11278 (v2k (verilog-in-paren-quick))
11279 (modi (verilog-modi-current))
11280 (moddecls (verilog-modi-get-decls modi))
11281 (modsubdecls (verilog-modi-get-sub-decls modi))
11282 (sig-list (verilog-signals-not-in
11283 (verilog-subdecls-get-outputs modsubdecls)
11284 (append (verilog-decls-get-outputs moddecls)
11285 (verilog-decls-get-inouts moddecls)
11286 (verilog-subdecls-get-inputs modsubdecls)
11287 (verilog-subdecls-get-inouts modsubdecls)))))
11288 (when regexp
11289 (setq sig-list (verilog-signals-matching-regexp
11290 sig-list regexp)))
11291 (setq sig-list (verilog-signals-not-matching-regexp
11292 sig-list verilog-auto-output-ignore-regexp))
11293 (forward-line 1)
11294 (when v2k (verilog-repair-open-comma))
11295 (when sig-list
11296 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
11297 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11298 (verilog-insert-indent "// End of automatics\n"))
11299 (when v2k (verilog-repair-close-comma)))))
11301 (defun verilog-auto-output-every ()
11302 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
11303 Make output statements for any signals that aren't primary inputs or
11304 outputs already. This makes every signal in the design an output. This is
11305 useful to get Synopsys to preserve every signal in the design, since it
11306 won't optimize away the outputs.
11308 An example:
11310 module ExampOutputEvery (o,i,tempa,tempb);
11311 output o;
11312 input i;
11313 /*AUTOOUTPUTEVERY*/
11314 wire tempa = i;
11315 wire tempb = tempa;
11316 wire o = tempb;
11317 endmodule
11319 Typing \\[verilog-auto] will make this into:
11321 module ExampOutputEvery (o,i,tempa,tempb);
11322 output o;
11323 input i;
11324 /*AUTOOUTPUTEVERY*/
11325 // Beginning of automatic outputs (every signal)
11326 output tempb;
11327 output tempa;
11328 // End of automatics
11329 wire tempa = i;
11330 wire tempb = tempa;
11331 wire o = tempb;
11332 endmodule"
11333 (save-excursion
11334 ;;Point must be at insertion point
11335 (let* ((indent-pt (current-indentation))
11336 (v2k (verilog-in-paren-quick))
11337 (modi (verilog-modi-current))
11338 (moddecls (verilog-modi-get-decls modi))
11339 (sig-list (verilog-signals-combine-bus
11340 (verilog-signals-not-in
11341 (verilog-decls-get-signals moddecls)
11342 (verilog-decls-get-ports moddecls)))))
11343 (forward-line 1)
11344 (when v2k (verilog-repair-open-comma))
11345 (when sig-list
11346 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
11347 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11348 (verilog-insert-indent "// End of automatics\n"))
11349 (when v2k (verilog-repair-close-comma)))))
11351 (defun verilog-auto-input (&optional with-params)
11352 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
11353 Make input statements for any input signal into an /*AUTOINST*/ that
11354 isn't declared elsewhere inside the module. This is useful for modules which
11355 only instantiate other modules.
11357 Limitations:
11358 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11360 If placed inside the parenthesis of a module declaration, it creates
11361 Verilog 2001 style, else uses Verilog 1995 style.
11363 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11364 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11366 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11368 Signals matching `verilog-auto-input-ignore-regexp' are not included.
11370 An example (see `verilog-auto-inst' for what else is going on here):
11372 module ExampInput (ov,i);
11373 output [31:0] ov;
11374 /*AUTOINPUT*/
11375 InstModule instName
11376 (/*AUTOINST*/);
11377 endmodule
11379 Typing \\[verilog-auto] will make this into:
11381 module ExampInput (ov,i);
11382 output [31:0] ov;
11383 /*AUTOINPUT*/
11384 // Beginning of automatic inputs (from unused autoinst inputs)
11385 input i; // From inst of inst.v
11386 // End of automatics
11387 InstModule instName
11388 (/*AUTOINST*/
11389 // Outputs
11390 .ov (ov[31:0]),
11391 // Inputs
11392 .i (i));
11393 endmodule
11395 You may also provide an optional regular expression, in which case only
11396 signals matching the regular expression will be included. For example the
11397 same expansion will result from only extracting inputs starting with i:
11399 /*AUTOINPUT(\"^i\")*/"
11400 (save-excursion
11401 (let* ((indent-pt (current-indentation))
11402 (regexp (and with-params
11403 (nth 0 (verilog-read-auto-params 1))))
11404 (v2k (verilog-in-paren-quick))
11405 (modi (verilog-modi-current))
11406 (moddecls (verilog-modi-get-decls modi))
11407 (modsubdecls (verilog-modi-get-sub-decls modi))
11408 (sig-list (verilog-signals-not-in
11409 (verilog-subdecls-get-inputs modsubdecls)
11410 (append (verilog-decls-get-inputs moddecls)
11411 (verilog-decls-get-inouts moddecls)
11412 (verilog-decls-get-vars moddecls)
11413 (verilog-decls-get-consts moddecls)
11414 (verilog-decls-get-gparams moddecls)
11415 (verilog-subdecls-get-interfaced modsubdecls)
11416 (verilog-subdecls-get-outputs modsubdecls)
11417 (verilog-subdecls-get-inouts modsubdecls)))))
11418 (when regexp
11419 (setq sig-list (verilog-signals-matching-regexp
11420 sig-list regexp)))
11421 (setq sig-list (verilog-signals-not-matching-regexp
11422 sig-list verilog-auto-input-ignore-regexp))
11423 (forward-line 1)
11424 (when v2k (verilog-repair-open-comma))
11425 (when sig-list
11426 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
11427 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
11428 (verilog-insert-indent "// End of automatics\n"))
11429 (when v2k (verilog-repair-close-comma)))))
11431 (defun verilog-auto-inout (&optional with-params)
11432 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
11433 Make inout statements for any inout signal in an /*AUTOINST*/ that
11434 isn't declared elsewhere inside the module.
11436 Limitations:
11437 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11439 If placed inside the parenthesis of a module declaration, it creates
11440 Verilog 2001 style, else uses Verilog 1995 style.
11442 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11443 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11445 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11447 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
11449 An example (see `verilog-auto-inst' for what else is going on here):
11451 module ExampInout (ov,i);
11452 input i;
11453 /*AUTOINOUT*/
11454 InstModule instName
11455 (/*AUTOINST*/);
11456 endmodule
11458 Typing \\[verilog-auto] will make this into:
11460 module ExampInout (ov,i);
11461 input i;
11462 /*AUTOINOUT*/
11463 // Beginning of automatic inouts (from unused autoinst inouts)
11464 inout [31:0] ov; // From inst of inst.v
11465 // End of automatics
11466 InstModule instName
11467 (/*AUTOINST*/
11468 // Inouts
11469 .ov (ov[31:0]),
11470 // Inputs
11471 .i (i));
11472 endmodule
11474 You may also provide an optional regular expression, in which case only
11475 signals matching the regular expression will be included. For example the
11476 same expansion will result from only extracting inouts starting with i:
11478 /*AUTOINOUT(\"^i\")*/"
11479 (save-excursion
11480 ;; Point must be at insertion point.
11481 (let* ((indent-pt (current-indentation))
11482 (regexp (and with-params
11483 (nth 0 (verilog-read-auto-params 1))))
11484 (v2k (verilog-in-paren-quick))
11485 (modi (verilog-modi-current))
11486 (moddecls (verilog-modi-get-decls modi))
11487 (modsubdecls (verilog-modi-get-sub-decls modi))
11488 (sig-list (verilog-signals-not-in
11489 (verilog-subdecls-get-inouts modsubdecls)
11490 (append (verilog-decls-get-outputs moddecls)
11491 (verilog-decls-get-inouts moddecls)
11492 (verilog-decls-get-inputs moddecls)
11493 (verilog-subdecls-get-inputs modsubdecls)
11494 (verilog-subdecls-get-outputs modsubdecls)))))
11495 (when regexp
11496 (setq sig-list (verilog-signals-matching-regexp
11497 sig-list regexp)))
11498 (setq sig-list (verilog-signals-not-matching-regexp
11499 sig-list verilog-auto-inout-ignore-regexp))
11500 (forward-line 1)
11501 (when v2k (verilog-repair-open-comma))
11502 (when sig-list
11503 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
11504 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
11505 (verilog-insert-indent "// End of automatics\n"))
11506 (when v2k (verilog-repair-close-comma)))))
11508 (defun verilog-auto-inout-module (&optional complement all-in)
11509 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
11510 Take input/output/inout statements from the specified module and insert
11511 into the current module. This is useful for making null templates and
11512 shell modules which need to have identical I/O with another module.
11513 Any I/O which are already defined in this module will not be redefined.
11514 For the complement of this function, see `verilog-auto-inout-comp',
11515 and to make monitors with all inputs, see `verilog-auto-inout-in'.
11517 Limitations:
11518 If placed inside the parenthesis of a module declaration, it creates
11519 Verilog 2001 style, else uses Verilog 1995 style.
11521 Concatenation and outputting partial buses is not supported.
11523 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11525 Signals are not inserted in the same order as in the original module,
11526 though they will appear to be in the same order to an AUTOINST
11527 instantiating either module.
11529 Signals declared as \"output reg\" or \"output wire\" etc will
11530 lose the wire/reg declaration so that shell modules may
11531 generate those outputs differently. However, \"output logic\"
11532 is propagated.
11534 An example:
11536 module ExampShell (/*AUTOARG*/);
11537 /*AUTOINOUTMODULE(\"ExampMain\")*/
11538 endmodule
11540 module ExampMain (i,o,io);
11541 input i;
11542 output o;
11543 inout io;
11544 endmodule
11546 Typing \\[verilog-auto] will make this into:
11548 module ExampShell (/*AUTOARG*/i,o,io);
11549 /*AUTOINOUTMODULE(\"ExampMain\")*/
11550 // Beginning of automatic in/out/inouts (from specific module)
11551 output o;
11552 inout io;
11553 input i;
11554 // End of automatics
11555 endmodule
11557 You may also provide an optional regular expression, in which case only
11558 signals matching the regular expression will be included. For example the
11559 same expansion will result from only extracting signals starting with i:
11561 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
11563 You may also provide an optional second regular expression, in
11564 which case only signals which have that pin direction and data
11565 type will be included. This matches against everything before
11566 the signal name in the declaration, for example against
11567 \"input\" (single bit), \"output logic\" (direction and type) or
11568 \"output [1:0]\" (direction and implicit type). You also
11569 probably want to skip spaces in your regexp.
11571 For example, the below will result in matching the output \"o\"
11572 against the previous example's module:
11574 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/"
11575 (save-excursion
11576 (let* ((params (verilog-read-auto-params 1 3))
11577 (submod (nth 0 params))
11578 (regexp (nth 1 params))
11579 (direction-re (nth 2 params))
11580 submodi)
11581 ;; Lookup position, etc of co-module
11582 ;; Note this may raise an error
11583 (when (setq submodi (verilog-modi-lookup submod t))
11584 (let* ((indent-pt (current-indentation))
11585 (v2k (verilog-in-paren-quick))
11586 (modi (verilog-modi-current))
11587 (moddecls (verilog-modi-get-decls modi))
11588 (submoddecls (verilog-modi-get-decls submodi))
11589 (sig-list-i (verilog-signals-not-in
11590 (cond (all-in
11591 (append
11592 (verilog-decls-get-inputs submoddecls)
11593 (verilog-decls-get-inouts submoddecls)
11594 (verilog-decls-get-outputs submoddecls)))
11595 (complement
11596 (verilog-decls-get-outputs submoddecls))
11597 (t (verilog-decls-get-inputs submoddecls)))
11598 (append (verilog-decls-get-inputs moddecls))))
11599 (sig-list-o (verilog-signals-not-in
11600 (cond (all-in nil)
11601 (complement
11602 (verilog-decls-get-inputs submoddecls))
11603 (t (verilog-decls-get-outputs submoddecls)))
11604 (append (verilog-decls-get-outputs moddecls))))
11605 (sig-list-io (verilog-signals-not-in
11606 (cond (all-in nil)
11607 (t (verilog-decls-get-inouts submoddecls)))
11608 (append (verilog-decls-get-inouts moddecls))))
11609 (sig-list-if (verilog-signals-not-in
11610 (verilog-decls-get-interfaces submoddecls)
11611 (append (verilog-decls-get-interfaces moddecls)))))
11612 (forward-line 1)
11613 (setq sig-list-i (verilog-signals-edit-wire-reg
11614 (verilog-signals-matching-dir-re
11615 (verilog-signals-matching-regexp sig-list-i regexp)
11616 "input" direction-re))
11617 sig-list-o (verilog-signals-edit-wire-reg
11618 (verilog-signals-matching-dir-re
11619 (verilog-signals-matching-regexp sig-list-o regexp)
11620 "output" direction-re))
11621 sig-list-io (verilog-signals-edit-wire-reg
11622 (verilog-signals-matching-dir-re
11623 (verilog-signals-matching-regexp sig-list-io regexp)
11624 "inout" direction-re))
11625 sig-list-if (verilog-signals-matching-dir-re
11626 (verilog-signals-matching-regexp sig-list-if regexp)
11627 "interface" direction-re))
11628 (when v2k (verilog-repair-open-comma))
11629 (when (or sig-list-i sig-list-o sig-list-io)
11630 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
11631 ;; Don't sort them so an upper AUTOINST will match the main module
11632 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
11633 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
11634 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
11635 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
11636 (verilog-insert-indent "// End of automatics\n"))
11637 (when v2k (verilog-repair-close-comma)))))))
11639 (defun verilog-auto-inout-comp ()
11640 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
11641 Take input/output/inout statements from the specified module and
11642 insert the inverse into the current module (inputs become outputs
11643 and vice-versa.) This is useful for making test and stimulus
11644 modules which need to have complementing I/O with another module.
11645 Any I/O which are already defined in this module will not be
11646 redefined. For the complement of this function, see
11647 `verilog-auto-inout-module'.
11649 Limitations:
11650 If placed inside the parenthesis of a module declaration, it creates
11651 Verilog 2001 style, else uses Verilog 1995 style.
11653 Concatenation and outputting partial buses is not supported.
11655 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11657 Signals are not inserted in the same order as in the original module,
11658 though they will appear to be in the same order to an AUTOINST
11659 instantiating either module.
11661 An example:
11663 module ExampShell (/*AUTOARG*/);
11664 /*AUTOINOUTCOMP(\"ExampMain\")*/
11665 endmodule
11667 module ExampMain (i,o,io);
11668 input i;
11669 output o;
11670 inout io;
11671 endmodule
11673 Typing \\[verilog-auto] will make this into:
11675 module ExampShell (/*AUTOARG*/i,o,io);
11676 /*AUTOINOUTCOMP(\"ExampMain\")*/
11677 // Beginning of automatic in/out/inouts (from specific module)
11678 output i;
11679 inout io;
11680 input o;
11681 // End of automatics
11682 endmodule
11684 You may also provide an optional regular expression, in which case only
11685 signals matching the regular expression will be included. For example the
11686 same expansion will result from only extracting signals starting with i:
11688 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
11689 (verilog-auto-inout-module t nil))
11691 (defun verilog-auto-inout-in ()
11692 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
11693 Take input/output/inout statements from the specified module and
11694 insert them as all inputs into the current module. This is
11695 useful for making monitor modules which need to see all signals
11696 as inputs based on another module. Any I/O which are already
11697 defined in this module will not be redefined. See also
11698 `verilog-auto-inout-module'.
11700 Limitations:
11701 If placed inside the parenthesis of a module declaration, it creates
11702 Verilog 2001 style, else uses Verilog 1995 style.
11704 Concatenation and outputting partial buses is not supported.
11706 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11708 Signals are not inserted in the same order as in the original module,
11709 though they will appear to be in the same order to an AUTOINST
11710 instantiating either module.
11712 An example:
11714 module ExampShell (/*AUTOARG*/);
11715 /*AUTOINOUTIN(\"ExampMain\")*/
11716 endmodule
11718 module ExampMain (i,o,io);
11719 input i;
11720 output o;
11721 inout io;
11722 endmodule
11724 Typing \\[verilog-auto] will make this into:
11726 module ExampShell (/*AUTOARG*/i,o,io);
11727 /*AUTOINOUTIN(\"ExampMain\")*/
11728 // Beginning of automatic in/out/inouts (from specific module)
11729 input i;
11730 input io;
11731 input o;
11732 // End of automatics
11733 endmodule
11735 You may also provide an optional regular expression, in which case only
11736 signals matching the regular expression will be included. For example the
11737 same expansion will result from only extracting signals starting with i:
11739 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
11740 (verilog-auto-inout-module nil t))
11742 (defun verilog-auto-insert-lisp ()
11743 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
11744 The Lisp code provided is called, and the Lisp code calls
11745 `insert` to insert text into the current file beginning on the
11746 line after the AUTOINSERTLISP.
11748 See also AUTO_LISP, which takes a Lisp expression and evaluates
11749 it during `verilog-auto-inst' but does not insert any text.
11751 An example:
11753 module ExampInsertLisp;
11754 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
11755 endmodule
11757 // For this example we declare the function in the
11758 // module's file itself. Often you'd define it instead
11759 // in a site-start.el or .emacs file.
11761 Local Variables:
11762 eval:
11763 (defun my-verilog-insert-hello (who)
11764 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
11765 End:
11768 Typing \\[verilog-auto] will call my-verilog-insert-hello and
11769 expand the above into:
11771 // Beginning of automatic insert lisp
11772 initial $write(\"hello world\");
11773 // End of automatics
11775 You can also call an external program and insert the returned
11776 text:
11778 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
11779 // Beginning of automatic insert lisp
11780 //hello
11781 // End of automatics"
11782 (save-excursion
11783 ;; Point is at end of /*AUTO...*/
11784 (let* ((indent-pt (current-indentation))
11785 (cmd-end-pt (save-excursion (search-backward ")")
11786 (forward-char)
11787 (point))) ;; Closing paren
11788 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
11789 (backward-sexp 1) ;; Inside comment
11790 (point))) ;; Beginning paren
11791 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
11792 (forward-line 1)
11793 ;; Some commands don't move point (like insert-file) so we always
11794 ;; add the begin/end comments, then delete it if not needed
11795 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
11796 (verilog-insert-indent "// End of automatics\n")
11797 (forward-line -1)
11798 (eval (read cmd))
11799 (forward-line -1)
11800 (setq verilog-scan-cache-tick nil) ;; Clear cache; inserted unknown text
11801 (verilog-delete-empty-auto-pair))))
11803 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
11804 "Return list of signals for current AUTOSENSE block."
11805 (let* ((sigss (verilog-read-always-signals))
11806 (sig-list (verilog-signals-not-params
11807 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
11808 (append (and (not verilog-auto-sense-include-inputs)
11809 (verilog-alw-get-outputs-delayed sigss))
11810 (and (not verilog-auto-sense-include-inputs)
11811 (verilog-alw-get-outputs-immediate sigss))
11812 (verilog-alw-get-temps sigss)
11813 (verilog-decls-get-consts moddecls)
11814 (verilog-decls-get-gparams moddecls)
11815 presense-sigs)))))
11816 sig-list))
11818 (defun verilog-auto-sense ()
11819 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
11820 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
11821 with one automatically derived from all inputs declared in the always
11822 statement. Signals that are generated within the same always block are NOT
11823 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
11824 Long lines are split based on the `fill-column', see \\[set-fill-column].
11826 Limitations:
11827 Verilog does not allow memories (multidimensional arrays) in sensitivity
11828 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
11830 Constant signals:
11831 AUTOSENSE cannot always determine if a `define is a constant or a signal
11832 (it could be in an include file for example). If a `define or other signal
11833 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
11834 declaration anywhere in the module (parenthesis are required):
11836 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
11838 Better yet, use a parameter, which will be understood to be constant
11839 automatically.
11841 OOps!
11842 If AUTOSENSE makes a mistake, please report it. (First try putting
11843 a begin/end after your always!) As a workaround, if a signal that
11844 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
11845 If a signal should be in the sensitivity list wasn't, placing it before
11846 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
11847 autos are updated (or added if it occurs there already).
11849 An example:
11851 always @ (/*AS*/) begin
11852 /* AUTO_CONSTANT (`constant) */
11853 outin = ina | inb | `constant;
11854 out = outin;
11857 Typing \\[verilog-auto] will make this into:
11859 always @ (/*AS*/ina or inb) begin
11860 /* AUTO_CONSTANT (`constant) */
11861 outin = ina | inb | `constant;
11862 out = outin;
11865 Note in Verilog 2001, you can often get the same result from the new @*
11866 operator. (This was added to the language in part due to AUTOSENSE!)
11868 always @* begin
11869 outin = ina | inb | `constant;
11870 out = outin;
11871 end"
11872 (save-excursion
11873 ;; Find beginning
11874 (let* ((start-pt (save-excursion
11875 (verilog-re-search-backward-quick "(" nil t)
11876 (point)))
11877 (indent-pt (save-excursion
11878 (or (and (goto-char start-pt) (1+ (current-column)))
11879 (current-indentation))))
11880 (modi (verilog-modi-current))
11881 (moddecls (verilog-modi-get-decls modi))
11882 (sig-memories (verilog-signals-memory
11883 (verilog-decls-get-vars moddecls)))
11884 sig-list not-first presense-sigs)
11885 ;; Read signals in always, eliminate outputs from sense list
11886 (setq presense-sigs (verilog-signals-from-signame
11887 (save-excursion
11888 (verilog-read-signals start-pt (point)))))
11889 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
11890 (when sig-memories
11891 (let ((tlen (length sig-list)))
11892 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
11893 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
11894 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
11895 (save-excursion (goto-char (point))
11896 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
11897 (verilog-re-search-backward-quick "\\s-" start-pt t)
11898 (while (looking-at "\\s-`endif")
11899 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
11900 (verilog-re-search-backward-quick "\\s-" start-pt t))
11901 (not (looking-at "\\s-or\\b"))))
11902 (setq not-first t))
11903 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
11904 (while sig-list
11905 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
11906 (insert "\n")
11907 (indent-to indent-pt)
11908 (if not-first (insert "or ")))
11909 (not-first (insert " or ")))
11910 (insert (verilog-sig-name (car sig-list)))
11911 (setq sig-list (cdr sig-list)
11912 not-first t)))))
11914 (defun verilog-auto-reset ()
11915 "Expand AUTORESET statements, as part of \\[verilog-auto].
11916 Replace the /*AUTORESET*/ comment with code to initialize all
11917 registers set elsewhere in the always block.
11919 Limitations:
11920 AUTORESET will not clear memories.
11922 AUTORESET uses <= if the signal has a <= assignment in the block,
11923 else it uses =.
11925 If <= is used, all = assigned variables are ignored if
11926 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
11927 to be temporaries.
11929 /*AUTORESET*/ presumes that any signals mentioned between the previous
11930 begin/case/if statement and the AUTORESET comment are being reset manually
11931 and should not be automatically reset. This includes omitting any signals
11932 used on the right hand side of assignments.
11934 By default, AUTORESET will include the width of the signal in the
11935 autos, SystemVerilog designs may want to change this. To control
11936 this behavior, see `verilog-auto-reset-widths'.
11938 AUTORESET ties signals to deasserted, which is presumed to be zero.
11939 Signals that match `verilog-active-low-regexp' will be deasserted by tying
11940 them to a one.
11942 An example:
11944 always @(posedge clk or negedge reset_l) begin
11945 if (!reset_l) begin
11946 c <= 1;
11947 /*AUTORESET*/
11949 else begin
11950 a <= in_a;
11951 b <= in_b;
11952 c <= in_c;
11956 Typing \\[verilog-auto] will make this into:
11958 always @(posedge core_clk or negedge reset_l) begin
11959 if (!reset_l) begin
11960 c <= 1;
11961 /*AUTORESET*/
11962 // Beginning of autoreset for uninitialized flops
11963 a <= 0;
11964 b = 0; // if `verilog-auto-reset-blocking-in-non' true
11965 // End of automatics
11967 else begin
11968 a <= in_a;
11969 b = in_b;
11970 c <= in_c;
11972 end"
11974 (interactive)
11975 (save-excursion
11976 ;; Find beginning
11977 (let* ((indent-pt (current-indentation))
11978 (modi (verilog-modi-current))
11979 (moddecls (verilog-modi-get-decls modi))
11980 (all-list (verilog-decls-get-signals moddecls))
11981 sigss sig-list dly-list prereset-sigs)
11982 ;; Read signals in always, eliminate outputs from reset list
11983 (setq prereset-sigs (verilog-signals-from-signame
11984 (save-excursion
11985 (verilog-read-signals
11986 (save-excursion
11987 (verilog-re-search-backward-quick "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
11988 (point))
11989 (point)))))
11990 (save-excursion
11991 (verilog-re-search-backward-quick "@" nil t)
11992 (setq sigss (verilog-read-always-signals)))
11993 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
11994 (setq sig-list (verilog-signals-not-in (append
11995 (verilog-alw-get-outputs-delayed sigss)
11996 (when (or (not (verilog-alw-get-uses-delayed sigss))
11997 verilog-auto-reset-blocking-in-non)
11998 (verilog-alw-get-outputs-immediate sigss)))
11999 (append
12000 (verilog-alw-get-temps sigss)
12001 prereset-sigs)))
12002 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12003 (when sig-list
12004 (insert "\n");
12005 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
12006 (while sig-list
12007 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
12008 (car sig-list))))
12009 (indent-to indent-pt)
12010 (insert (verilog-sig-name sig)
12011 (if (assoc (verilog-sig-name sig) dly-list)
12012 (concat " <= " verilog-assignment-delay)
12013 " = ")
12014 (verilog-sig-tieoff sig)
12015 ";\n")
12016 (setq sig-list (cdr sig-list))))
12017 (verilog-insert-indent "// End of automatics")))))
12019 (defun verilog-auto-tieoff ()
12020 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
12021 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
12022 signals to deasserted.
12024 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
12025 input/output list as another module, but no internals. Specifically, it
12026 finds all outputs in the module, and if that input is not otherwise declared
12027 as a register or wire, creates a tieoff.
12029 AUTORESET ties signals to deasserted, which is presumed to be zero.
12030 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12031 them to a one.
12033 You can add signals you do not want included in AUTOTIEOFF with
12034 `verilog-auto-tieoff-ignore-regexp'.
12036 `verilog-auto-wire-type' may be used to change the datatype of
12037 the declarations.
12039 `verilog-auto-reset-widths' may be used to change how the tieoff
12040 value's width is generated.
12042 An example of making a stub for another module:
12044 module ExampStub (/*AUTOINST*/);
12045 /*AUTOINOUTMODULE(\"Foo\")*/
12046 /*AUTOTIEOFF*/
12047 // verilator lint_off UNUSED
12048 wire _unused_ok = &{1'b0,
12049 /*AUTOUNUSED*/
12050 1'b0};
12051 // verilator lint_on UNUSED
12052 endmodule
12054 Typing \\[verilog-auto] will make this into:
12056 module ExampStub (/*AUTOINST*/...);
12057 /*AUTOINOUTMODULE(\"Foo\")*/
12058 // Beginning of autotieoff
12059 output [2:0] foo;
12060 // End of automatics
12062 /*AUTOTIEOFF*/
12063 // Beginning of autotieoff
12064 wire [2:0] foo = 3'b0;
12065 // End of automatics
12067 endmodule"
12068 (interactive)
12069 (save-excursion
12070 ;; Find beginning
12071 (let* ((indent-pt (current-indentation))
12072 (modi (verilog-modi-current))
12073 (moddecls (verilog-modi-get-decls modi))
12074 (modsubdecls (verilog-modi-get-sub-decls modi))
12075 (sig-list (verilog-signals-not-in
12076 (verilog-decls-get-outputs moddecls)
12077 (append (verilog-decls-get-vars moddecls)
12078 (verilog-decls-get-assigns moddecls)
12079 (verilog-decls-get-consts moddecls)
12080 (verilog-decls-get-gparams moddecls)
12081 (verilog-subdecls-get-interfaced modsubdecls)
12082 (verilog-subdecls-get-outputs modsubdecls)
12083 (verilog-subdecls-get-inouts modsubdecls)))))
12084 (setq sig-list (verilog-signals-not-matching-regexp
12085 sig-list verilog-auto-tieoff-ignore-regexp))
12086 (when sig-list
12087 (forward-line 1)
12088 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
12089 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12090 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
12091 (while sig-list
12092 (let ((sig (car sig-list)))
12093 (cond ((equal verilog-auto-tieoff-declaration "assign")
12094 (indent-to indent-pt)
12095 (insert "assign " (verilog-sig-name sig)))
12097 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
12098 (indent-to (max 48 (+ indent-pt 40)))
12099 (insert "= " (verilog-sig-tieoff sig)
12100 ";\n")
12101 (setq sig-list (cdr sig-list))))
12102 (verilog-insert-indent "// End of automatics\n")))))
12104 (defun verilog-auto-undef ()
12105 "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
12106 Take any `defines since the last AUTOUNDEF in the current file
12107 and create `undefs for them. This is used to insure that
12108 file-local defines do not pollute the global `define name space.
12110 Limitations:
12111 AUTOUNDEF presumes any identifier following `define is the
12112 name of a define. Any `ifdefs are ignored.
12114 AUTOUNDEF suppresses creating an `undef for any define that was
12115 `undefed before the AUTOUNDEF. This may be used to work around
12116 the ignoring of `ifdefs as shown below.
12118 An example:
12120 `define XX_FOO
12121 `define M_BAR(x)
12122 `define M_BAZ
12124 `ifdef NEVER
12125 `undef M_BAZ // Emacs will see this and not `undef M_BAZ
12126 `endif
12128 /*AUTOUNDEF*/
12130 Typing \\[verilog-auto] will make this into:
12133 /*AUTOUNDEF*/
12134 // Beginning of automatic undefs
12135 `undef XX_FOO
12136 `undef M_BAR
12137 // End of automatics
12139 You may also provide an optional regular expression, in which case only
12140 defines the regular expression will be undefed."
12141 (save-excursion
12142 (let* ((params (verilog-read-auto-params 0 1))
12143 (regexp (nth 0 params))
12144 (indent-pt (current-indentation))
12145 (end-pt (point))
12146 defs def)
12147 (save-excursion
12148 ;; Scan from start of file, or last AUTOUNDEF
12149 (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
12150 (goto-char (point-min)))
12151 (while (verilog-re-search-forward-quick
12152 "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
12153 (cond ((equal (match-string-no-properties 1) "define")
12154 (setq def (match-string-no-properties 2))
12155 (when (and (or (not regexp)
12156 (string-match regexp def))
12157 (not (member def defs))) ;; delete-dups not in 21.1
12158 (setq defs (cons def defs))))
12160 (setq defs (delete (match-string-no-properties 2) defs))))))
12161 ;; Insert
12162 (setq defs (sort defs 'string<))
12163 (when defs
12164 (forward-line 1)
12165 (verilog-insert-indent "// Beginning of automatic undefs\n")
12166 (while defs
12167 (verilog-insert-indent "`undef " (car defs) "\n")
12168 (setq defs (cdr defs)))
12169 (verilog-insert-indent "// End of automatics\n")))))
12171 (defun verilog-auto-unused ()
12172 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
12173 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
12174 input and inout signals.
12176 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
12177 input/output list as another module, but no internals. Specifically, it
12178 finds all inputs and inouts in the module, and if that input is not otherwise
12179 used, adds it to a comma separated list.
12181 The comma separated list is intended to be used to create a _unused_ok
12182 signal. Using the exact name \"_unused_ok\" for name of the temporary
12183 signal is recommended as it will insure maximum forward compatibility, it
12184 also makes lint warnings easy to understand; ignore any unused warnings
12185 with \"unused\" in the signal name.
12187 To reduce simulation time, the _unused_ok signal should be forced to a
12188 constant to prevent wiggling. The easiest thing to do is use a
12189 reduction-and with 1'b0 as shown.
12191 This way all unused signals are in one place, making it convenient to add
12192 your tool's specific pragmas around the assignment to disable any unused
12193 warnings.
12195 You can add signals you do not want included in AUTOUNUSED with
12196 `verilog-auto-unused-ignore-regexp'.
12198 An example of making a stub for another module:
12200 module ExampStub (/*AUTOINST*/);
12201 /*AUTOINOUTMODULE(\"Examp\")*/
12202 /*AUTOTIEOFF*/
12203 // verilator lint_off UNUSED
12204 wire _unused_ok = &{1'b0,
12205 /*AUTOUNUSED*/
12206 1'b0};
12207 // verilator lint_on UNUSED
12208 endmodule
12210 Typing \\[verilog-auto] will make this into:
12213 // verilator lint_off UNUSED
12214 wire _unused_ok = &{1'b0,
12215 /*AUTOUNUSED*/
12216 // Beginning of automatics
12217 unused_input_a,
12218 unused_input_b,
12219 unused_input_c,
12220 // End of automatics
12221 1'b0};
12222 // verilator lint_on UNUSED
12223 endmodule"
12224 (interactive)
12225 (save-excursion
12226 ;; Find beginning
12227 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
12228 (modi (verilog-modi-current))
12229 (moddecls (verilog-modi-get-decls modi))
12230 (modsubdecls (verilog-modi-get-sub-decls modi))
12231 (sig-list (verilog-signals-not-in
12232 (append (verilog-decls-get-inputs moddecls)
12233 (verilog-decls-get-inouts moddecls))
12234 (append (verilog-subdecls-get-inputs modsubdecls)
12235 (verilog-subdecls-get-inouts modsubdecls)))))
12236 (setq sig-list (verilog-signals-not-matching-regexp
12237 sig-list verilog-auto-unused-ignore-regexp))
12238 (when sig-list
12239 (forward-line 1)
12240 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
12241 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12242 (while sig-list
12243 (let ((sig (car sig-list)))
12244 (indent-to indent-pt)
12245 (insert (verilog-sig-name sig) ",\n")
12246 (setq sig-list (cdr sig-list))))
12247 (verilog-insert-indent "// End of automatics\n")))))
12249 (defun verilog-enum-ascii (signm elim-regexp)
12250 "Convert an enum name SIGNM to an ascii string for insertion.
12251 Remove user provided prefix ELIM-REGEXP."
12252 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
12253 (let ((case-fold-search t))
12254 ;; All upper becomes all lower for readability
12255 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
12257 (defun verilog-auto-ascii-enum ()
12258 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
12259 Create a register to contain the ASCII decode of an enumerated signal type.
12260 This will allow trace viewers to show the ASCII name of states.
12262 First, parameters are built into an enumeration using the synopsys enum
12263 comment. The comment must be between the keyword and the symbol.
12264 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
12266 Next, registers which that enum applies to are also tagged with the same
12267 enum.
12269 Finally, an AUTOASCIIENUM command is used.
12271 The first parameter is the name of the signal to be decoded.
12273 The second parameter is the name to store the ASCII code into. For the
12274 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
12275 a signal that is just for simulation, and the magic characters _ascii
12276 tell viewers like Dinotrace to display in ASCII format.
12278 The third optional parameter is a string which will be removed
12279 from the state names. It defaults to \"\" which removes nothing.
12281 The fourth optional parameter is \"onehot\" to force one-hot
12282 decoding. If unspecified, if and only if the first parameter
12283 width is 2^(number of states in enum) and does NOT match the
12284 width of the enum, the signal is assumed to be a one-hot
12285 decode. Otherwise, it's a normal encoded state vector.
12287 `verilog-auto-wire-type' may be used to change the datatype of
12288 the declarations.
12290 \"auto enum\" may be used in place of \"synopsys enum\".
12292 An example:
12294 //== State enumeration
12295 parameter [2:0] // synopsys enum state_info
12296 SM_IDLE = 3'b000,
12297 SM_SEND = 3'b001,
12298 SM_WAIT1 = 3'b010;
12299 //== State variables
12300 reg [2:0] /* synopsys enum state_info */
12301 state_r; /* synopsys state_vector state_r */
12302 reg [2:0] /* synopsys enum state_info */
12303 state_e1;
12305 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12307 Typing \\[verilog-auto] will make this into:
12309 ... same front matter ...
12311 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12312 // Beginning of automatic ASCII enum decoding
12313 reg [39:0] state_ascii_r; // Decode of state_r
12314 always @(state_r) begin
12315 case ({state_r})
12316 SM_IDLE: state_ascii_r = \"idle \";
12317 SM_SEND: state_ascii_r = \"send \";
12318 SM_WAIT1: state_ascii_r = \"wait1\";
12319 default: state_ascii_r = \"%Erro\";
12320 endcase
12322 // End of automatics"
12323 (save-excursion
12324 (let* ((params (verilog-read-auto-params 2 4))
12325 (undecode-name (nth 0 params))
12326 (ascii-name (nth 1 params))
12327 (elim-regexp (and (nth 2 params)
12328 (not (equal (nth 2 params) ""))
12329 (nth 2 params)))
12330 (one-hot-flag (nth 3 params))
12332 (indent-pt (current-indentation))
12333 (modi (verilog-modi-current))
12334 (moddecls (verilog-modi-get-decls modi))
12336 (sig-list-consts (append (verilog-decls-get-consts moddecls)
12337 (verilog-decls-get-gparams moddecls)))
12338 (sig-list-all (append (verilog-decls-get-vars moddecls)
12339 (verilog-decls-get-outputs moddecls)
12340 (verilog-decls-get-inouts moddecls)
12341 (verilog-decls-get-inputs moddecls)))
12343 (undecode-sig (or (assoc undecode-name sig-list-all)
12344 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
12345 (undecode-enum (or (verilog-sig-enum undecode-sig)
12346 (error "%s: Signal %s does not have an enum tag" (verilog-point-text) undecode-name)))
12348 (enum-sigs (verilog-signals-not-in
12349 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
12350 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
12351 nil))
12353 (one-hot (or
12354 (string-match "onehot" (or one-hot-flag ""))
12355 (and ;; width(enum) != width(sig)
12356 (or (not (verilog-sig-bits (car enum-sigs)))
12357 (not (equal (verilog-sig-width (car enum-sigs))
12358 (verilog-sig-width undecode-sig))))
12359 ;; count(enums) == width(sig)
12360 (equal (number-to-string (length enum-sigs))
12361 (verilog-sig-width undecode-sig)))))
12362 (enum-chars 0)
12363 (ascii-chars 0))
12365 ;; Find number of ascii chars needed
12366 (let ((tmp-sigs enum-sigs))
12367 (while tmp-sigs
12368 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
12369 ascii-chars (max ascii-chars (length (verilog-enum-ascii
12370 (verilog-sig-name (car tmp-sigs))
12371 elim-regexp)))
12372 tmp-sigs (cdr tmp-sigs))))
12374 (forward-line 1)
12375 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
12376 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
12377 (concat "Decode of " undecode-name) nil nil))))
12378 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
12380 (verilog-insert-indent "always @(" undecode-name ") begin\n")
12381 (setq indent-pt (+ indent-pt verilog-indent-level))
12382 (verilog-insert-indent "case ({" undecode-name "})\n")
12383 (setq indent-pt (+ indent-pt verilog-case-indent))
12385 (let ((tmp-sigs enum-sigs)
12386 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
12387 (+ (if one-hot 9 1) (max 8 enum-chars))
12388 ascii-name ascii-chars))
12389 (errname (substring "%Error" 0 (min 6 ascii-chars))))
12390 (while tmp-sigs
12391 (verilog-insert-indent
12392 (concat
12393 (format chrfmt
12394 (concat (if one-hot "(")
12395 ;; Use enum-sigs length as that's numeric
12396 ;; verilog-sig-width undecode-sig might not be.
12397 (if one-hot (number-to-string (length enum-sigs)))
12398 ;; We use a shift instead of var[index]
12399 ;; so that a non-one hot value will show as error.
12400 (if one-hot "'b1<<")
12401 (verilog-sig-name (car tmp-sigs))
12402 (if one-hot ")") ":")
12403 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
12404 elim-regexp))))
12405 (setq tmp-sigs (cdr tmp-sigs)))
12406 (verilog-insert-indent (format chrfmt "default:" errname)))
12408 (setq indent-pt (- indent-pt verilog-case-indent))
12409 (verilog-insert-indent "endcase\n")
12410 (setq indent-pt (- indent-pt verilog-indent-level))
12411 (verilog-insert-indent "end\n"
12412 "// End of automatics\n"))))
12414 (defun verilog-auto-templated-rel ()
12415 "Replace Templated relative line numbers with absolute line numbers.
12416 Internal use only. This hacks around the line numbers in AUTOINST Templates
12417 being different from the final output's line numbering."
12418 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
12419 ;; Find line number each template is on
12420 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
12421 (goto-char (point-min))
12422 (while (not (eobp))
12423 (when (looking-at ".*AUTO_TEMPLATE")
12424 (setq templateno (1+ templateno))
12425 (setq template-line (cons buf-line template-line)))
12426 (setq buf-line (1+ buf-line))
12427 (forward-line 1))
12428 (setq template-line (nreverse template-line))
12429 ;; Replace T# L# with absolute line number
12430 (goto-char (point-min))
12431 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
12432 (replace-match
12433 (concat " Templated "
12434 (int-to-string (+ (nth (string-to-number (match-string 1))
12435 template-line)
12436 (string-to-number (match-string 2)))))
12437 t t))))
12439 (defun verilog-auto-template-lint ()
12440 "Check AUTO_TEMPLATEs for unused lines.
12441 Enable with `verilog-auto-template-warn-unused'."
12442 (let ((name1 (or (buffer-file-name) (buffer-name))))
12443 (save-excursion
12444 (goto-char (point-min))
12445 (while (re-search-forward
12446 "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
12447 (let* ((tpl-info (verilog-read-auto-template-middle))
12448 (tpl-list (aref tpl-info 1))
12449 (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
12450 tpl-ass)
12451 (while tlines
12452 (setq tpl-ass (car tlines)
12453 tlines (cdr tlines))
12455 (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ;; Not supported, no warning
12456 (not verilog-auto-template-hits)
12457 (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
12458 verilog-auto-template-hits))
12459 (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
12460 name1
12461 (+ (elt tpl-ass 3) ;; Template line number
12462 (count-lines (point-min) (point)))
12463 (elt tpl-ass 0) (elt tpl-ass 1))
12464 )))))))
12468 ;; Auto top level
12471 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
12472 "Expand AUTO statements.
12473 Look for any /*AUTO...*/ commands in the code, as used in
12474 instantiations or argument headers. Update the list of signals
12475 following the /*AUTO...*/ command.
12477 Use \\[verilog-delete-auto] to remove the AUTOs.
12479 Use \\[verilog-diff-auto] to see differences in AUTO expansion.
12481 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
12483 Use \\[verilog-faq] for a pointer to frequently asked questions.
12485 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
12486 called before and after this function, respectively.
12488 For example:
12489 module ModuleName (/*AUTOARG*/);
12490 /*AUTOINPUT*/
12491 /*AUTOOUTPUT*/
12492 /*AUTOWIRE*/
12493 /*AUTOREG*/
12494 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
12496 You can also update the AUTOs from the shell using:
12497 emacs --batch <filenames.v> -f verilog-batch-auto
12498 Or fix indentation with:
12499 emacs --batch <filenames.v> -f verilog-batch-indent
12500 Likewise, you can delete or inject AUTOs with:
12501 emacs --batch <filenames.v> -f verilog-batch-delete-auto
12502 emacs --batch <filenames.v> -f verilog-batch-inject-auto
12503 Or check if AUTOs have the same expansion
12504 emacs --batch <filenames.v> -f verilog-batch-diff-auto
12506 Using \\[describe-function], see also:
12507 `verilog-auto-arg' for AUTOARG module instantiations
12508 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
12509 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
12510 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
12511 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
12512 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
12513 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
12514 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
12515 `verilog-auto-inst' for AUTOINST instantiation pins
12516 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
12517 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
12518 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
12519 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
12520 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
12521 `verilog-auto-reg' for AUTOREG registers
12522 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
12523 `verilog-auto-reset' for AUTORESET flop resets
12524 `verilog-auto-sense' for AUTOSENSE always sensitivity lists
12525 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
12526 `verilog-auto-undef' for AUTOUNDEF `undef of local `defines
12527 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
12528 `verilog-auto-wire' for AUTOWIRE instantiation wires
12530 `verilog-read-defines' for reading `define values
12531 `verilog-read-includes' for reading `includes
12533 If you have bugs with these autos, please file an issue at
12534 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
12535 Wilson Snyder (wsnyder@wsnyder.org)."
12536 (interactive)
12537 (unless noninteractive (message "Updating AUTOs..."))
12538 (if (fboundp 'dinotrace-unannotate-all)
12539 (dinotrace-unannotate-all))
12540 (verilog-save-font-mods
12541 (let ((oldbuf (if (not (buffer-modified-p))
12542 (buffer-string)))
12543 ;; Cache directories; we don't write new files, so can't change
12544 (verilog-dir-cache-preserving t)
12545 ;; Cache current module
12546 (verilog-modi-cache-current-enable t)
12547 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
12548 verilog-modi-cache-current)
12549 (unwind-protect
12550 ;; Disable change hooks for speed
12551 ;; This let can't be part of above let; must restore
12552 ;; after-change-functions before font-lock resumes
12553 (verilog-save-no-change-functions
12554 (verilog-save-scan-cache
12555 (save-excursion
12556 ;; Wipe cache; otherwise if we AUTOed a block above this one,
12557 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
12558 (setq verilog-modi-cache-list nil)
12559 ;; Local state
12560 (setq verilog-auto-template-hits nil)
12561 ;; If we're not in verilog-mode, change syntax table so parsing works right
12562 (unless (eq major-mode `verilog-mode) (verilog-mode))
12563 ;; Allow user to customize
12564 (verilog-run-hooks 'verilog-before-auto-hook)
12565 ;; Try to save the user from needing to revert-file to reread file local-variables
12566 (verilog-auto-reeval-locals)
12567 (verilog-read-auto-lisp-present)
12568 (verilog-read-auto-lisp (point-min) (point-max))
12569 (verilog-getopt-flags)
12570 ;; From here on out, we can cache anything we read from disk
12571 (verilog-preserve-dir-cache
12572 ;; These two may seem obvious to do always, but on large includes it can be way too slow
12573 (when verilog-auto-read-includes
12574 (verilog-read-includes)
12575 (verilog-read-defines nil nil t))
12576 ;; Setup variables due to SystemVerilog expansion
12577 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
12578 ;; This particular ordering is important
12579 ;; INST: Lower modules correct, no internal dependencies, FIRST
12580 (verilog-preserve-modi-cache
12581 ;; Clear existing autos else we'll be screwed by existing ones
12582 (verilog-delete-auto)
12583 ;; Injection if appropriate
12584 (when inject
12585 (verilog-inject-inst)
12586 (verilog-inject-sense)
12587 (verilog-inject-arg))
12589 ;; Do user inserts first, so their code can insert AUTOs
12590 ;; We may provide an AUTOINSERTLISPLAST if another cleanup pass is needed
12591 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
12592 'verilog-auto-insert-lisp)
12593 ;; Expand instances before need the signals the instances input/output
12594 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
12595 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
12596 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
12597 ;; Doesn't matter when done, but combine it with a common changer
12598 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
12599 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
12600 ;; Must be done before autoin/out as creates a reg
12601 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM([^)]*)\\*/" 'verilog-auto-ascii-enum)
12603 ;; first in/outs from other files
12604 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE([^)]*)\\*/" 'verilog-auto-inout-module)
12605 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP([^)]*)\\*/" 'verilog-auto-inout-comp)
12606 (verilog-auto-re-search-do "/\\*AUTOINOUTIN([^)]*)\\*/" 'verilog-auto-inout-in)
12607 ;; next in/outs which need previous sucked inputs first
12608 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((\"[^\"]*\")\\)\\*/"
12609 (lambda () (verilog-auto-output t)))
12610 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\*/" 'verilog-auto-output)
12611 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((\"[^\"]*\")\\)\\*/"
12612 (lambda () (verilog-auto-input t)))
12613 (verilog-auto-re-search-do "/\\*AUTOINPUT\\*/" 'verilog-auto-input)
12614 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((\"[^\"]*\")\\)\\*/"
12615 (lambda () (verilog-auto-inout t)))
12616 (verilog-auto-re-search-do "/\\*AUTOINOUT\\*/" 'verilog-auto-inout)
12617 ;; Then tie off those in/outs
12618 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
12619 ;; These can be anywhere after AUTOINSERTLISP
12620 (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((\"[^\"]*\")\\)?\\*/" 'verilog-auto-undef)
12621 ;; Wires/regs must be after inputs/outputs
12622 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
12623 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
12624 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
12625 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
12626 ;; outputevery needs AUTOOUTPUTs done first
12627 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
12628 ;; After we've created all new variables
12629 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
12630 ;; Must be after all inputs outputs are generated
12631 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
12632 ;; Fix line numbers (comments only)
12633 (when verilog-auto-inst-template-numbers
12634 (verilog-auto-templated-rel))
12635 (when verilog-auto-template-warn-unused
12636 (verilog-auto-template-lint))))
12638 (verilog-run-hooks 'verilog-auto-hook)
12640 (when verilog-auto-delete-trailing-whitespace
12641 (verilog-delete-trailing-whitespace))
12643 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
12645 ;; If end result is same as when started, clear modified flag
12646 (cond ((and oldbuf (equal oldbuf (buffer-string)))
12647 (set-buffer-modified-p nil)
12648 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
12649 (t (unless noninteractive (message "Updating AUTOs...done"))))
12650 ;; End of after-change protection
12652 ;; Unwind forms
12653 ;; Currently handled in verilog-save-font-mods
12654 ))))
12658 ;; Skeleton based code insertion
12660 (defvar verilog-template-map
12661 (let ((map (make-sparse-keymap)))
12662 (define-key map "a" 'verilog-sk-always)
12663 (define-key map "b" 'verilog-sk-begin)
12664 (define-key map "c" 'verilog-sk-case)
12665 (define-key map "f" 'verilog-sk-for)
12666 (define-key map "g" 'verilog-sk-generate)
12667 (define-key map "h" 'verilog-sk-header)
12668 (define-key map "i" 'verilog-sk-initial)
12669 (define-key map "j" 'verilog-sk-fork)
12670 (define-key map "m" 'verilog-sk-module)
12671 (define-key map "o" 'verilog-sk-ovm-class)
12672 (define-key map "p" 'verilog-sk-primitive)
12673 (define-key map "r" 'verilog-sk-repeat)
12674 (define-key map "s" 'verilog-sk-specify)
12675 (define-key map "t" 'verilog-sk-task)
12676 (define-key map "u" 'verilog-sk-uvm-class)
12677 (define-key map "w" 'verilog-sk-while)
12678 (define-key map "x" 'verilog-sk-casex)
12679 (define-key map "z" 'verilog-sk-casez)
12680 (define-key map "?" 'verilog-sk-if)
12681 (define-key map ":" 'verilog-sk-else-if)
12682 (define-key map "/" 'verilog-sk-comment)
12683 (define-key map "A" 'verilog-sk-assign)
12684 (define-key map "F" 'verilog-sk-function)
12685 (define-key map "I" 'verilog-sk-input)
12686 (define-key map "O" 'verilog-sk-output)
12687 (define-key map "S" 'verilog-sk-state-machine)
12688 (define-key map "=" 'verilog-sk-inout)
12689 (define-key map "W" 'verilog-sk-wire)
12690 (define-key map "R" 'verilog-sk-reg)
12691 (define-key map "D" 'verilog-sk-define-signal)
12692 map)
12693 "Keymap used in Verilog mode for smart template operations.")
12697 ;; Place the templates into Verilog Mode. They may be inserted under any key.
12698 ;; C-c C-t will be the default. If you use templates a lot, you
12699 ;; may want to consider moving the binding to another key in your .emacs
12700 ;; file.
12702 ;; Note \C-c and letter are reserved for users
12703 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
12705 ;;; ---- statement skeletons ------------------------------------------
12707 (define-skeleton verilog-sk-prompt-condition
12708 "Prompt for the loop condition."
12709 "[condition]: " str )
12711 (define-skeleton verilog-sk-prompt-init
12712 "Prompt for the loop init statement."
12713 "[initial statement]: " str )
12715 (define-skeleton verilog-sk-prompt-inc
12716 "Prompt for the loop increment statement."
12717 "[increment statement]: " str )
12719 (define-skeleton verilog-sk-prompt-name
12720 "Prompt for the name of something."
12721 "[name]: " str)
12723 (define-skeleton verilog-sk-prompt-clock
12724 "Prompt for the name of something."
12725 "name and edge of clock(s): " str)
12727 (defvar verilog-sk-reset nil)
12728 (defun verilog-sk-prompt-reset ()
12729 "Prompt for the name of a state machine reset."
12730 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
12733 (define-skeleton verilog-sk-prompt-state-selector
12734 "Prompt for the name of a state machine selector."
12735 "name of selector (eg {a,b,c,d}): " str )
12737 (define-skeleton verilog-sk-prompt-output
12738 "Prompt for the name of something."
12739 "output: " str)
12741 (define-skeleton verilog-sk-prompt-msb
12742 "Prompt for most significant bit specification."
12743 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
12745 (define-skeleton verilog-sk-prompt-lsb
12746 "Prompt for least significant bit specification."
12747 "lsb:" str )
12749 (defvar verilog-sk-p nil)
12750 (define-skeleton verilog-sk-prompt-width
12751 "Prompt for a width specification."
12753 (progn
12754 (setq verilog-sk-p (point))
12755 (verilog-sk-prompt-msb)
12756 (if (> (point) verilog-sk-p) "] " " ")))
12758 (defun verilog-sk-header ()
12759 "Insert a descriptive header at the top of the file.
12760 See also `verilog-header' for an alternative format."
12761 (interactive "*")
12762 (save-excursion
12763 (goto-char (point-min))
12764 (verilog-sk-header-tmpl)))
12766 (define-skeleton verilog-sk-header-tmpl
12767 "Insert a comment block containing the module title, author, etc."
12768 "[Description]: "
12769 "// -*- Mode: Verilog -*-"
12770 "\n// Filename : " (buffer-name)
12771 "\n// Description : " str
12772 "\n// Author : " (user-full-name)
12773 "\n// Created On : " (current-time-string)
12774 "\n// Last Modified By: " (user-full-name)
12775 "\n// Last Modified On: " (current-time-string)
12776 "\n// Update Count : 0"
12777 "\n// Status : Unknown, Use with caution!"
12778 "\n")
12780 (define-skeleton verilog-sk-module
12781 "Insert a module definition."
12783 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
12784 > _ \n
12785 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
12787 ;;; ------------------------------------------------------------------------
12788 ;;; Define a default OVM class, with macros and new()
12789 ;;; ------------------------------------------------------------------------
12791 (define-skeleton verilog-sk-ovm-class
12792 "Insert a class definition"
12794 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
12795 > _ \n
12796 > "`ovm_object_utils_begin(" name ")" \n
12797 > (- verilog-indent-level) " `ovm_object_utils_end" \n
12798 > _ \n
12799 > "function new(name=\"" name "\");" \n
12800 > "super.new(name);" \n
12801 > (- verilog-indent-level) "endfunction" \n
12802 > _ \n
12803 > "endclass" (progn (electric-verilog-terminate-line) nil))
12805 (define-skeleton verilog-sk-uvm-class
12806 "Insert a class definition"
12808 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
12809 > _ \n
12810 > "`uvm_object_utils_begin(" name ")" \n
12811 > (- verilog-indent-level) " `uvm_object_utils_end" \n
12812 > _ \n
12813 > "function new(name=\"" name "\");" \n
12814 > "super.new(name);" \n
12815 > (- verilog-indent-level) "endfunction" \n
12816 > _ \n
12817 > "endclass" (progn (electric-verilog-terminate-line) nil))
12819 (define-skeleton verilog-sk-primitive
12820 "Insert a task definition."
12822 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
12823 > _ \n
12824 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
12826 (define-skeleton verilog-sk-task
12827 "Insert a task definition."
12829 > "task " '(verilog-sk-prompt-name) & ?; \n
12830 > _ \n
12831 > "begin" \n
12832 > \n
12833 > (- verilog-indent-level-behavioral) "end" \n
12834 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
12836 (define-skeleton verilog-sk-function
12837 "Insert a function definition."
12839 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
12840 > _ \n
12841 > "begin" \n
12842 > \n
12843 > (- verilog-indent-level-behavioral) "end" \n
12844 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
12846 (define-skeleton verilog-sk-always
12847 "Insert always block. Uses the minibuffer to prompt
12848 for sensitivity list."
12850 > "always @ ( /*AUTOSENSE*/ ) begin\n"
12851 > _ \n
12852 > (- verilog-indent-level-behavioral) "end" \n >
12855 (define-skeleton verilog-sk-initial
12856 "Insert an initial block."
12858 > "initial begin\n"
12859 > _ \n
12860 > (- verilog-indent-level-behavioral) "end" \n > )
12862 (define-skeleton verilog-sk-specify
12863 "Insert specify block. "
12865 > "specify\n"
12866 > _ \n
12867 > (- verilog-indent-level-behavioral) "endspecify" \n > )
12869 (define-skeleton verilog-sk-generate
12870 "Insert generate block. "
12872 > "generate\n"
12873 > _ \n
12874 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
12876 (define-skeleton verilog-sk-begin
12877 "Insert begin end block. Uses the minibuffer to prompt for name."
12879 > "begin" '(verilog-sk-prompt-name) \n
12880 > _ \n
12881 > (- verilog-indent-level-behavioral) "end"
12884 (define-skeleton verilog-sk-fork
12885 "Insert a fork join block."
12887 > "fork\n"
12888 > "begin" \n
12889 > _ \n
12890 > (- verilog-indent-level-behavioral) "end" \n
12891 > "begin" \n
12892 > \n
12893 > (- verilog-indent-level-behavioral) "end" \n
12894 > (- verilog-indent-level-behavioral) "join" \n
12898 (define-skeleton verilog-sk-case
12899 "Build skeleton case statement, prompting for the selector expression,
12900 and the case items."
12901 "[selector expression]: "
12902 > "case (" str ") " \n
12903 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
12904 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12906 (define-skeleton verilog-sk-casex
12907 "Build skeleton casex statement, prompting for the selector expression,
12908 and the case items."
12909 "[selector expression]: "
12910 > "casex (" str ") " \n
12911 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
12912 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12914 (define-skeleton verilog-sk-casez
12915 "Build skeleton casez statement, prompting for the selector expression,
12916 and the case items."
12917 "[selector expression]: "
12918 > "casez (" str ") " \n
12919 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
12920 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
12922 (define-skeleton verilog-sk-if
12923 "Insert a skeleton if statement."
12924 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
12925 > _ \n
12926 > (- verilog-indent-level-behavioral) "end " \n )
12928 (define-skeleton verilog-sk-else-if
12929 "Insert a skeleton else if statement."
12930 > (verilog-indent-line) "else if ("
12931 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
12932 > _ \n
12933 > "end" (progn (electric-verilog-terminate-line) nil))
12935 (define-skeleton verilog-sk-datadef
12936 "Common routine to get data definition."
12938 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
12940 (define-skeleton verilog-sk-input
12941 "Insert an input definition."
12943 > "input [" '(verilog-sk-datadef))
12945 (define-skeleton verilog-sk-output
12946 "Insert an output definition."
12948 > "output [" '(verilog-sk-datadef))
12950 (define-skeleton verilog-sk-inout
12951 "Insert an inout definition."
12953 > "inout [" '(verilog-sk-datadef))
12955 (defvar verilog-sk-signal nil)
12956 (define-skeleton verilog-sk-def-reg
12957 "Insert a reg definition."
12959 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
12961 (defun verilog-sk-define-signal ()
12962 "Insert a definition of signal under point at top of module."
12963 (interactive "*")
12964 (let* ((sig-re "[a-zA-Z0-9_]*")
12965 (v1 (buffer-substring
12966 (save-excursion
12967 (skip-chars-backward sig-re)
12968 (point))
12969 (save-excursion
12970 (skip-chars-forward sig-re)
12971 (point)))))
12972 (if (not (member v1 verilog-keywords))
12973 (save-excursion
12974 (setq verilog-sk-signal v1)
12975 (verilog-beg-of-defun)
12976 (verilog-end-of-statement)
12977 (verilog-forward-syntactic-ws)
12978 (verilog-sk-def-reg)
12979 (message "signal at point is %s" v1))
12980 (message "object at point (%s) is a keyword" v1))))
12982 (define-skeleton verilog-sk-wire
12983 "Insert a wire definition."
12985 > "wire [" '(verilog-sk-datadef))
12987 (define-skeleton verilog-sk-reg
12988 "Insert a reg definition."
12990 > "reg [" '(verilog-sk-datadef))
12992 (define-skeleton verilog-sk-assign
12993 "Insert a skeleton assign statement."
12995 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
12997 (define-skeleton verilog-sk-while
12998 "Insert a skeleton while loop statement."
13000 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
13001 > _ \n
13002 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13004 (define-skeleton verilog-sk-repeat
13005 "Insert a skeleton repeat loop statement."
13007 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
13008 > _ \n
13009 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13011 (define-skeleton verilog-sk-for
13012 "Insert a skeleton while loop statement."
13014 > "for ("
13015 '(verilog-sk-prompt-init) "; "
13016 '(verilog-sk-prompt-condition) "; "
13017 '(verilog-sk-prompt-inc)
13018 ") begin" \n
13019 > _ \n
13020 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13022 (define-skeleton verilog-sk-comment
13023 "Inserts three comment lines, making a display comment."
13025 > "/*\n"
13026 > "* " _ \n
13027 > "*/")
13029 (define-skeleton verilog-sk-state-machine
13030 "Insert a state machine definition."
13031 "Name of state variable: "
13032 '(setq input "state")
13033 > "// State registers for " str | -23 \n
13034 '(setq verilog-sk-state str)
13035 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
13036 '(setq input nil)
13037 > \n
13038 > "// State FF for " verilog-sk-state \n
13039 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
13040 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
13041 > verilog-sk-state " = next_" verilog-sk-state ?; \n
13042 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
13043 > \n
13044 > "// Next State Logic for " verilog-sk-state \n
13045 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13046 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
13047 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
13048 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
13049 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
13053 ;; Include file loading with mouse/return event
13055 ;; idea & first impl.: M. Rouat (eldo-mode.el)
13056 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
13058 (if (featurep 'xemacs)
13059 (require 'overlay))
13061 (defconst verilog-include-file-regexp
13062 "^`include\\s-+\"\\([^\n\"]*\\)\""
13063 "Regexp that matches the include file.")
13065 (defvar verilog-mode-mouse-map
13066 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
13067 (set-keymap-parent map verilog-mode-map)
13068 ;; mouse button bindings
13069 (define-key map "\r" 'verilog-load-file-at-point)
13070 (if (featurep 'xemacs)
13071 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
13072 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
13073 (if (featurep 'xemacs)
13074 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
13075 (define-key map [S-mouse-2] 'mouse-yank-at-click))
13076 map)
13077 "Map containing mouse bindings for `verilog-mode'.")
13080 (defun verilog-highlight-region (beg end old-len)
13081 "Colorize included files and modules in the (changed?) region.
13082 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
13083 (when (or verilog-highlight-includes
13084 verilog-highlight-modules)
13085 (save-excursion
13086 (save-match-data ;; A query-replace may call this function - do not disturb
13087 (verilog-save-buffer-state
13088 (verilog-save-scan-cache
13089 (let (end-point)
13090 (goto-char end)
13091 (setq end-point (point-at-eol))
13092 (goto-char beg)
13093 (beginning-of-line) ; scan entire line
13094 ;; delete overlays existing on this line
13095 (let ((overlays (overlays-in (point) end-point)))
13096 (while overlays
13097 (if (and
13098 (overlay-get (car overlays) 'detachable)
13099 (or (overlay-get (car overlays) 'verilog-include-file)
13100 (overlay-get (car overlays) 'verilog-inst-module)))
13101 (delete-overlay (car overlays)))
13102 (setq overlays (cdr overlays))))
13104 ;; make new include overlays
13105 (when verilog-highlight-includes
13106 (while (search-forward-regexp verilog-include-file-regexp end-point t)
13107 (goto-char (match-beginning 1))
13108 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
13109 (overlay-put ov 'start-closed 't)
13110 (overlay-put ov 'end-closed 't)
13111 (overlay-put ov 'evaporate 't)
13112 (overlay-put ov 'verilog-include-file 't)
13113 (overlay-put ov 'mouse-face 'highlight)
13114 (overlay-put ov 'local-map verilog-mode-mouse-map))))
13116 ;; make new module overlays
13117 (goto-char beg)
13118 ;; This scanner is syntax-fragile, so don't get bent
13119 (when verilog-highlight-modules
13120 (condition-case nil
13121 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
13122 (save-excursion
13123 (goto-char (match-beginning 0))
13124 (unless (verilog-inside-comment-or-string-p)
13125 (verilog-read-inst-module-matcher) ;; sets match 0
13126 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
13127 (overlay-put ov 'start-closed 't)
13128 (overlay-put ov 'end-closed 't)
13129 (overlay-put ov 'evaporate 't)
13130 (overlay-put ov 'verilog-inst-module 't)
13131 (overlay-put ov 'mouse-face 'highlight)
13132 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
13133 (error nil)))
13135 ;; Future highlights:
13136 ;; variables - make an Occur buffer of where referenced
13137 ;; pins - make an Occur buffer of the sig in the declaration module
13138 )))))))
13140 (defun verilog-highlight-buffer ()
13141 "Colorize included files and modules across the whole buffer."
13142 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
13143 (interactive)
13144 ;; delete and remake overlays
13145 (verilog-highlight-region (point-min) (point-max) nil))
13147 ;; Deprecated, but was interactive, so we'll keep it around
13148 (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
13150 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
13151 ;; so define this function to do more or less the same as ffap-at-mouse
13152 ;; but first resolve filename...
13153 (defun verilog-load-file-at-mouse (event)
13154 "Load file under button 2 click's EVENT.
13155 Files are checked based on `verilog-library-flags'."
13156 (interactive "@e")
13157 (save-excursion ;; implement a Verilog specific ffap-at-mouse
13158 (mouse-set-point event)
13159 (verilog-load-file-at-point t)))
13161 ;; ffap isn't usable for Verilog mode. It uses library paths.
13162 ;; so define this function to do more or less the same as ffap
13163 ;; but first resolve filename...
13164 (defun verilog-load-file-at-point (&optional warn)
13165 "Load file under point.
13166 If WARN, throw warning if not found.
13167 Files are checked based on `verilog-library-flags'."
13168 (interactive)
13169 (save-excursion ;; implement a Verilog specific ffap
13170 (let ((overlays (overlays-in (point) (point)))
13171 hit)
13172 (while (and overlays (not hit))
13173 (when (overlay-get (car overlays) 'verilog-inst-module)
13174 (verilog-goto-defun-file (buffer-substring
13175 (overlay-start (car overlays))
13176 (overlay-end (car overlays))))
13177 (setq hit t))
13178 (setq overlays (cdr overlays)))
13179 ;; Include?
13180 (beginning-of-line)
13181 (when (and (not hit)
13182 (looking-at verilog-include-file-regexp))
13183 (if (and (car (verilog-library-filenames
13184 (match-string 1) (buffer-file-name)))
13185 (file-readable-p (car (verilog-library-filenames
13186 (match-string 1) (buffer-file-name)))))
13187 (find-file (car (verilog-library-filenames
13188 (match-string 1) (buffer-file-name))))
13189 (when warn
13190 (message
13191 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
13192 (match-string 1))))))))
13195 ;; Bug reporting
13198 (defun verilog-faq ()
13199 "Tell the user their current version, and where to get the FAQ etc."
13200 (interactive)
13201 (with-output-to-temp-buffer "*verilog-mode help*"
13202 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
13203 (princ "\n")
13204 (princ "For new releases, see http://www.verilog.com\n")
13205 (princ "\n")
13206 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
13207 (princ "\n")
13208 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
13209 (princ "\n")))
13211 (autoload 'reporter-submit-bug-report "reporter")
13212 (defvar reporter-prompt-for-summary-p)
13214 (defun verilog-submit-bug-report ()
13215 "Submit via mail a bug report on verilog-mode.el."
13216 (interactive)
13217 (let ((reporter-prompt-for-summary-p t))
13218 (reporter-submit-bug-report
13219 "mac@verilog.com, wsnyder@wsnyder.org"
13220 (concat "verilog-mode v" verilog-mode-version)
13222 verilog-active-low-regexp
13223 verilog-after-save-font-hook
13224 verilog-align-ifelse
13225 verilog-assignment-delay
13226 verilog-auto-arg-sort
13227 verilog-auto-declare-nettype
13228 verilog-auto-delete-trailing-whitespace
13229 verilog-auto-endcomments
13230 verilog-auto-hook
13231 verilog-auto-ignore-concat
13232 verilog-auto-indent-on-newline
13233 verilog-auto-inout-ignore-regexp
13234 verilog-auto-input-ignore-regexp
13235 verilog-auto-inst-column
13236 verilog-auto-inst-dot-name
13237 verilog-auto-inst-interfaced-ports
13238 verilog-auto-inst-param-value
13239 verilog-auto-inst-sort
13240 verilog-auto-inst-template-numbers
13241 verilog-auto-inst-vector
13242 verilog-auto-lineup
13243 verilog-auto-newline
13244 verilog-auto-output-ignore-regexp
13245 verilog-auto-read-includes
13246 verilog-auto-reset-blocking-in-non
13247 verilog-auto-reset-widths
13248 verilog-auto-save-policy
13249 verilog-auto-sense-defines-constant
13250 verilog-auto-sense-include-inputs
13251 verilog-auto-star-expand
13252 verilog-auto-star-save
13253 verilog-auto-template-warn-unused
13254 verilog-auto-tieoff-declaration
13255 verilog-auto-tieoff-ignore-regexp
13256 verilog-auto-unused-ignore-regexp
13257 verilog-auto-wire-type
13258 verilog-before-auto-hook
13259 verilog-before-delete-auto-hook
13260 verilog-before-getopt-flags-hook
13261 verilog-before-save-font-hook
13262 verilog-cache-enabled
13263 verilog-case-indent
13264 verilog-cexp-indent
13265 verilog-compiler
13266 verilog-coverage
13267 verilog-delete-auto-hook
13268 verilog-getopt-flags-hook
13269 verilog-highlight-grouping-keywords
13270 verilog-highlight-includes
13271 verilog-highlight-modules
13272 verilog-highlight-p1800-keywords
13273 verilog-highlight-translate-off
13274 verilog-indent-begin-after-if
13275 verilog-indent-declaration-macros
13276 verilog-indent-level
13277 verilog-indent-level-behavioral
13278 verilog-indent-level-declaration
13279 verilog-indent-level-directive
13280 verilog-indent-level-module
13281 verilog-indent-lists
13282 verilog-library-directories
13283 verilog-library-extensions
13284 verilog-library-files
13285 verilog-library-flags
13286 verilog-linter
13287 verilog-minimum-comment-distance
13288 verilog-mode-hook
13289 verilog-mode-release-date
13290 verilog-mode-release-emacs
13291 verilog-mode-version
13292 verilog-preprocessor
13293 verilog-simulator
13294 verilog-tab-always-indent
13295 verilog-tab-to-comment
13296 verilog-typedef-regexp
13297 verilog-warn-fatal
13299 nil nil
13300 (concat "Hi Mac,
13302 I want to report a bug.
13304 Before I go further, I want to say that Verilog mode has changed my life.
13305 I save so much time, my files are colored nicely, my co workers respect
13306 my coding ability... until now. I'd really appreciate anything you
13307 could do to help me out with this minor deficiency in the product.
13309 I've taken a look at the Verilog-Mode FAQ at
13310 http://www.veripool.org/verilog-mode-faq.html.
13312 And, I've considered filing the bug on the issue tracker at
13313 http://www.veripool.org/verilog-mode-bugs
13314 since I realize that public bugs are easier for you to track,
13315 and for others to search, but would prefer to email.
13317 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
13318 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
13319 the code included below.
13321 Given those lines, I expected [[Fill in here]] to happen;
13322 but instead, [[Fill in here]] happens!.
13324 == The code: =="))))
13326 (provide 'verilog-mode)
13328 ;; Local Variables:
13329 ;; checkdoc-permit-comma-termination-flag:t
13330 ;; checkdoc-force-docstrings-flag:nil
13331 ;; End:
13333 ;;; verilog-mode.el ends here