* term/xterm.el (xterm--query): Stop after first matching handler. (Bug#14615)
[bpt/emacs.git] / lisp / progmodes / verilog-mode.el
1 ;; verilog-mode.el --- major mode for editing verilog source in Emacs
2
3 ;; Copyright (C) 1996-2013 Free Software Foundation, Inc.
4
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
10 ;;
11 ;; Keywords: languages
12
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.
17
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!
21 ;;
22 ;;
23
24 ;; This file is part of GNU Emacs.
25
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.
30
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.
35
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/>.
38
39 ;;; Commentary:
40
41 ;; This mode borrows heavily from the Pascal-mode and the cc-mode of Emacs
42
43 ;; USAGE
44 ;; =====
45
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>
50
51 ;; KNOWN BUGS / BUG REPORTS
52 ;; =======================
53
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.
61
62 ;; INSTALLING THE MODE
63 ;; ===================
64
65 ;; An older version of this mode may be already installed as a part of
66 ;; your environment, and one method of updating would be to update
67 ;; your Emacs environment. Sometimes this is difficult for local
68 ;; political/control reasons, and hence you can always install a
69 ;; private copy (or even a shared copy) which overrides the system
70 ;; default.
71
72 ;; You can get step by step help in installing this file by going to
73 ;; <http://www.verilog.com/emacs_install.html>
74
75 ;; The short list of installation instructions are: To set up
76 ;; automatic Verilog mode, put this file in your load path, and put
77 ;; the following in code (please un comment it first!) in your
78 ;; .emacs, or in your site's site-load.el
79
80 ; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
81 ; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
82
83 ;; Be sure to examine at the help for verilog-auto, and the other
84 ;; verilog-auto-* functions for some major coding time savers.
85 ;;
86 ;; If you want to customize Verilog mode to fit your needs better,
87 ;; you may add the below lines (the values of the variables presented
88 ;; here are the defaults). Note also that if you use an Emacs that
89 ;; supports custom, it's probably better to use the custom menu to
90 ;; edit these. If working as a member of a large team these settings
91 ;; should be common across all users (in a site-start file), or set
92 ;; in Local Variables in every file. Otherwise, different people's
93 ;; AUTO expansion may result different whitespace changes.
94 ;;
95 ; ;; Enable syntax highlighting of **all** languages
96 ; (global-font-lock-mode t)
97 ;
98 ; ;; User customization for Verilog mode
99 ; (setq verilog-indent-level 3
100 ; verilog-indent-level-module 3
101 ; verilog-indent-level-declaration 3
102 ; verilog-indent-level-behavioral 3
103 ; verilog-indent-level-directive 1
104 ; verilog-case-indent 2
105 ; verilog-auto-newline t
106 ; verilog-auto-indent-on-newline t
107 ; verilog-tab-always-indent t
108 ; verilog-auto-endcomments t
109 ; verilog-minimum-comment-distance 40
110 ; verilog-indent-begin-after-if t
111 ; verilog-auto-lineup 'declarations
112 ; verilog-highlight-p1800-keywords nil
113 ; verilog-linter "my_lint_shell_command"
114 ; )
115
116 ;; \f
117
118 ;;; History:
119 ;;
120 ;; See commit history at http://www.veripool.org/verilog-mode.html
121 ;; (This section is required to appease checkdoc.)
122
123 ;;; Code:
124
125 ;; This variable will always hold the version number of the mode
126 (defconst verilog-mode-version (substring "$$Revision: 840 $$" 12 -3)
127 "Version of this Verilog mode.")
128 (defconst verilog-mode-release-date (substring "$$Date: 2013-01-03 05:29:05 -0800 (Thu, 03 Jan 2013) $$" 8 -3)
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.")
132
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))
137
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)))))
210 )
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))
221 )
222 (if (fboundp 'defface)
223 nil ; great!
224 (defmacro defface (var values doc &rest args)
225 `(make-face ,var))
226 )
227
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))
233 )
234
235 (unless (boundp 'inhibit-point-motion-hooks)
236 (defvar inhibit-point-motion-hooks nil))
237 (unless (boundp 'deactivate-mark)
238 (defvar deactivate-mark nil))
239 )
240 ;;
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))))
246 )
247
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))
267 )
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))
272 )
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))
278 )
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)))
283 )
284 ;; Emacs.
285 (defalias 'verilog-regexp-opt 'regexp-opt)))
286
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)))
304
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) "\\>"))
313
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"]))
335
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))))
343
344 (defun verilog-customize ()
345 "Customize variables and other settings used by Verilog-Mode."
346 (interactive)
347 (customize-group 'verilog-mode))
348
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)))
354
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)))
360
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)))
368
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.")
372
373 (defvar verilog-in-hooks nil
374 "True when within a `verilog-run-hooks' block.")
375
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)))
381
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)))))
394
395 (defgroup verilog-mode nil
396 "Major mode for Verilog source code."
397 :version "22.2"
398 :group 'languages)
399
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)
404
405 (defgroup verilog-mode-indent nil
406 "Customize indentation and highlighting of Verilog source text."
407 :group 'verilog-mode)
408
409 (defgroup verilog-mode-actions nil
410 "Customize actions on Verilog source text."
411 :group 'verilog-mode)
412
413 (defgroup verilog-mode-auto nil
414 "Customize AUTO actions when expanding Verilog source text."
415 :group 'verilog-mode)
416
417 (defvar verilog-debug nil
418 "Non-nil means enable debug messages for `verilog-mode' internals.")
419
420 (defvar verilog-warn-fatal nil
421 "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
422
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
432
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
442
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
452
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
462
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
474
475 (defvar verilog-preprocess-history nil
476 "History for `verilog-preprocess'.")
477
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.")
484
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').
490
491 Note: This will slow down on-the-fly fontification (and thus editing).
492
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)
499
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.
503
504 If 'declarations', 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;
512
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;
520
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."
524
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))))
532
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)
538
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)
546
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)
553
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)
567
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
573
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)
580
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)
587
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)
594
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)
600
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)
606
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)
612
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)
618
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)
625
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)
631
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)
638
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)
645
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)
653
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)
664
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)
673
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)
682
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)
690
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)
700
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)
708
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)
715
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)
722
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)
732
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)
742
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.
749
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)))
755
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)
763
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.
768
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)
774
775 (defvar verilog-auto-update-tick nil
776 "Modification tick at which autos were last performed.")
777
778 (defvar verilog-auto-last-file-locals nil
779 "Text from file-local-variables during last evaluation.")
780
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.")
786
787 ;;; Compile support
788 (require 'compile)
789 (defvar verilog-error-regexp-added nil)
790
791 (defvar verilog-error-regexp-emacs-alist
792 '(
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
804 "\
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)
821 )
822 "List of regexps for Verilog compilers.
823 See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
824
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.")
832
833 (defvar verilog-error-font-lock-keywords
834 '(
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
851 ("\
852 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
853 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
854 ("\
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)
875 )
876 "Keywords to also highlight in Verilog *compilation* buffers.
877 Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
878
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.
883
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'.
890
891 filename Adds the filename to `verilog-library-files'.
892 This is not recommended, -v is a better choice.
893
894 You might want these defined in each file; put at the *END* of your file
895 something like:
896
897 // Local Variables:
898 // verilog-library-flags:(\"-y dir -y otherdir\")
899 // End:
900
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.
904
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)
909
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.
915
916 You might want these defined in each file; put at the *END* of your file
917 something like:
918
919 // Local Variables:
920 // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
921 // End:
922
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.
926
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)
932
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.
938
939 You might want these defined in each file; put at the *END* of your file
940 something like:
941
942 // Local Variables:
943 // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
944 // End:
945
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.
949
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)
954
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)
961
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 '(choice (const nil) regexp))
968 (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
969
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)
977
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)
986
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 (<=).
992
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)
1001
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.
1008
1009 If nil, AUTORESET uses \"0\" as the constant.
1010
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))))
1018
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)
1024
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.
1031
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)
1036
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)
1046
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.
1050
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.
1054
1055 For example, imagine a submodule uses parameters to declare the size of its
1056 inputs. This is then used by an upper module:
1057
1058 module InstModule (o,i);
1059 parameter WIDTH;
1060 input [WIDTH-1:0] i;
1061 endmodule
1062
1063 module ExampInst;
1064 InstModule
1065 #(PARAM(10))
1066 instName
1067 (/*AUTOINST*/
1068 .i (i[PARAM-1:0]));
1069
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:
1073
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)
1083
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.
1090
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)
1096
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)
1106
1107 (defcustom verilog-auto-inst-template-numbers nil
1108 "If true, when creating templated ports with AUTOINST, add a comment.
1109
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.
1114
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))))
1123
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)
1129
1130 (defcustom verilog-auto-inst-interfaced-ports nil
1131 "Non-nil means include interfaced ports in AUTOINST expansions."
1132 :version "24.3" ;; rev773, default change rev815
1133 :group 'verilog-mode-auto
1134 :type 'boolean)
1135 (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
1136
1137 (defcustom verilog-auto-input-ignore-regexp nil
1138 "If set, when creating AUTOINPUT list, ignore signals matching this regexp.
1139 See the \\[verilog-faq] for examples on using this."
1140 :group 'verilog-mode-auto
1141 :type '(choice (const nil) regexp))
1142 (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
1143
1144 (defcustom verilog-auto-inout-ignore-regexp nil
1145 "If set, when creating AUTOINOUT list, ignore signals matching this regexp.
1146 See the \\[verilog-faq] for examples on using this."
1147 :group 'verilog-mode-auto
1148 :type '(choice (const nil) regexp))
1149 (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
1150
1151 (defcustom verilog-auto-output-ignore-regexp nil
1152 "If set, when creating AUTOOUTPUT list, ignore signals matching this regexp.
1153 See the \\[verilog-faq] for examples on using this."
1154 :group 'verilog-mode-auto
1155 :type '(choice (const nil) regexp))
1156 (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
1157
1158 (defcustom verilog-auto-template-warn-unused nil
1159 "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
1160 This feature is not supported before Emacs 21.1 or XEmacs 21.4."
1161 :version "24.3" ;;rev787
1162 :group 'verilog-mode-auto
1163 :type 'boolean)
1164 (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
1165
1166 (defcustom verilog-auto-tieoff-declaration "wire"
1167 "Data type used for the declaration for AUTOTIEOFF.
1168 If \"wire\" then create a wire, if \"assign\" create an
1169 assignment, else the data type for variable creation."
1170 :version "24.1" ;; rev713
1171 :group 'verilog-mode-auto
1172 :type 'string)
1173 (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
1174
1175 (defcustom verilog-auto-tieoff-ignore-regexp nil
1176 "If set, when creating AUTOTIEOFF list, ignore signals matching this regexp.
1177 See the \\[verilog-faq] for examples on using this."
1178 :group 'verilog-mode-auto
1179 :type '(choice (const nil) regexp))
1180 (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
1181
1182 (defcustom verilog-auto-unused-ignore-regexp nil
1183 "If set, when creating AUTOUNUSED list, ignore signals matching this regexp.
1184 See the \\[verilog-faq] for examples on using this."
1185 :group 'verilog-mode-auto
1186 :type '(choice (const nil) regexp))
1187 (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
1188
1189 (defcustom verilog-typedef-regexp nil
1190 "If non-nil, regular expression that matches Verilog-2001 typedef names.
1191 For example, \"_t$\" matches typedefs named with _t, as in the C language."
1192 :group 'verilog-mode-auto
1193 :type '(choice (const nil) regexp))
1194 (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
1195
1196 (defcustom verilog-mode-hook 'verilog-set-compile-command
1197 "Hook run after Verilog mode is loaded."
1198 :type 'hook
1199 :group 'verilog-mode)
1200
1201 (defcustom verilog-auto-hook nil
1202 "Hook run after `verilog-mode' updates AUTOs."
1203 :group 'verilog-mode-auto
1204 :type 'hook)
1205
1206 (defcustom verilog-before-auto-hook nil
1207 "Hook run before `verilog-mode' updates AUTOs."
1208 :group 'verilog-mode-auto
1209 :type 'hook)
1210
1211 (defcustom verilog-delete-auto-hook nil
1212 "Hook run after `verilog-mode' deletes AUTOs."
1213 :group 'verilog-mode-auto
1214 :type 'hook)
1215
1216 (defcustom verilog-before-delete-auto-hook nil
1217 "Hook run before `verilog-mode' deletes AUTOs."
1218 :group 'verilog-mode-auto
1219 :type 'hook)
1220
1221 (defcustom verilog-getopt-flags-hook nil
1222 "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
1223 :group 'verilog-mode-auto
1224 :type 'hook)
1225
1226 (defcustom verilog-before-getopt-flags-hook nil
1227 "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
1228 :group 'verilog-mode-auto
1229 :type 'hook)
1230
1231 (defcustom verilog-before-save-font-hook nil
1232 "Hook run before `verilog-save-font-mods' removes highlighting."
1233 :version "24.3" ;;rev735
1234 :group 'verilog-mode-auto
1235 :type 'hook)
1236
1237 (defcustom verilog-after-save-font-hook nil
1238 "Hook run after `verilog-save-font-mods' restores highlighting."
1239 :version "24.3" ;;rev735
1240 :group 'verilog-mode-auto
1241 :type 'hook)
1242
1243 (defvar verilog-imenu-generic-expression
1244 '((nil "^\\s-*\\(\\(m\\(odule\\|acromodule\\)\\)\\|primitive\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 4)
1245 ("*Vars*" "^\\s-*\\(reg\\|wire\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3))
1246 "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
1247
1248 ;;
1249 ;; provide a verilog-header function.
1250 ;; Customization variables:
1251 ;;
1252 (defvar verilog-date-scientific-format nil
1253 "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
1254 If nil, in European format (e.g. 17.09.1997). The brain-dead American
1255 format (e.g. 09/17/1997) is not supported.")
1256
1257 (defvar verilog-company nil
1258 "Default name of Company for Verilog header.
1259 If set will become buffer local.")
1260 (make-variable-buffer-local 'verilog-company)
1261
1262 (defvar verilog-project nil
1263 "Default name of Project for Verilog header.
1264 If set will become buffer local.")
1265 (make-variable-buffer-local 'verilog-project)
1266
1267 (defvar verilog-mode-map
1268 (let ((map (make-sparse-keymap)))
1269 (define-key map ";" 'electric-verilog-semi)
1270 (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
1271 (define-key map ":" 'electric-verilog-colon)
1272 ;;(define-key map "=" 'electric-verilog-equal)
1273 (define-key map "\`" 'electric-verilog-tick)
1274 (define-key map "\t" 'electric-verilog-tab)
1275 (define-key map "\r" 'electric-verilog-terminate-line)
1276 ;; backspace/delete key bindings
1277 (define-key map [backspace] 'backward-delete-char-untabify)
1278 (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
1279 (define-key map [delete] 'delete-char)
1280 (define-key map [(meta delete)] 'kill-word))
1281 (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
1282 (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
1283 (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
1284 (define-key map "\M-\t" 'verilog-complete-word)
1285 (define-key map "\M-?" 'verilog-show-completions)
1286 ;; Note \C-c and letter are reserved for users
1287 (define-key map "\C-c\`" 'verilog-lint-off)
1288 (define-key map "\C-c\*" 'verilog-delete-auto-star-implicit)
1289 (define-key map "\C-c\?" 'verilog-diff-auto)
1290 (define-key map "\C-c\C-r" 'verilog-label-be)
1291 (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
1292 (define-key map "\C-c=" 'verilog-pretty-expr)
1293 (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
1294 (define-key map "\M-*" 'verilog-star-comment)
1295 (define-key map "\C-c\C-c" 'verilog-comment-region)
1296 (define-key map "\C-c\C-u" 'verilog-uncomment-region)
1297 (when (featurep 'xemacs)
1298 (define-key map [(meta control h)] 'verilog-mark-defun)
1299 (define-key map "\M-\C-a" 'verilog-beg-of-defun)
1300 (define-key map "\M-\C-e" 'verilog-end-of-defun))
1301 (define-key map "\C-c\C-d" 'verilog-goto-defun)
1302 (define-key map "\C-c\C-k" 'verilog-delete-auto)
1303 (define-key map "\C-c\C-a" 'verilog-auto)
1304 (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
1305 (define-key map "\C-c\C-p" 'verilog-preprocess)
1306 (define-key map "\C-c\C-z" 'verilog-inject-auto)
1307 (define-key map "\C-c\C-e" 'verilog-expand-vector)
1308 (define-key map "\C-c\C-h" 'verilog-header)
1309 map)
1310 "Keymap used in Verilog mode.")
1311
1312 ;; menus
1313 (easy-menu-define
1314 verilog-menu verilog-mode-map "Menu for Verilog mode"
1315 (verilog-easy-menu-filter
1316 '("Verilog"
1317 ("Choose Compilation Action"
1318 ["None"
1319 (progn
1320 (setq verilog-tool nil)
1321 (verilog-set-compile-command))
1322 :style radio
1323 :selected (equal verilog-tool nil)
1324 :help "When invoking compilation, use compile-command"]
1325 ["Lint"
1326 (progn
1327 (setq verilog-tool 'verilog-linter)
1328 (verilog-set-compile-command))
1329 :style radio
1330 :selected (equal verilog-tool `verilog-linter)
1331 :help "When invoking compilation, use lint checker"]
1332 ["Coverage"
1333 (progn
1334 (setq verilog-tool 'verilog-coverage)
1335 (verilog-set-compile-command))
1336 :style radio
1337 :selected (equal verilog-tool `verilog-coverage)
1338 :help "When invoking compilation, annotate for coverage"]
1339 ["Simulator"
1340 (progn
1341 (setq verilog-tool 'verilog-simulator)
1342 (verilog-set-compile-command))
1343 :style radio
1344 :selected (equal verilog-tool `verilog-simulator)
1345 :help "When invoking compilation, interpret Verilog source"]
1346 ["Compiler"
1347 (progn
1348 (setq verilog-tool 'verilog-compiler)
1349 (verilog-set-compile-command))
1350 :style radio
1351 :selected (equal verilog-tool `verilog-compiler)
1352 :help "When invoking compilation, compile Verilog source"]
1353 ["Preprocessor"
1354 (progn
1355 (setq verilog-tool 'verilog-preprocessor)
1356 (verilog-set-compile-command))
1357 :style radio
1358 :selected (equal verilog-tool `verilog-preprocessor)
1359 :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
1360 )
1361 ("Move"
1362 ["Beginning of function" verilog-beg-of-defun
1363 :keys "C-M-a"
1364 :help "Move backward to the beginning of the current function or procedure"]
1365 ["End of function" verilog-end-of-defun
1366 :keys "C-M-e"
1367 :help "Move forward to the end of the current function or procedure"]
1368 ["Mark function" verilog-mark-defun
1369 :keys "C-M-h"
1370 :help "Mark the current Verilog function or procedure"]
1371 ["Goto function/module" verilog-goto-defun
1372 :help "Move to specified Verilog module/task/function"]
1373 ["Move to beginning of block" electric-verilog-backward-sexp
1374 :help "Move backward over one balanced expression"]
1375 ["Move to end of block" electric-verilog-forward-sexp
1376 :help "Move forward over one balanced expression"]
1377 )
1378 ("Comments"
1379 ["Comment Region" verilog-comment-region
1380 :help "Put marked area into a comment"]
1381 ["UnComment Region" verilog-uncomment-region
1382 :help "Uncomment an area commented with Comment Region"]
1383 ["Multi-line comment insert" verilog-star-comment
1384 :help "Insert Verilog /* */ comment at point"]
1385 ["Lint error to comment" verilog-lint-off
1386 :help "Convert a Verilog linter warning line into a disable statement"]
1387 )
1388 "----"
1389 ["Compile" compile
1390 :help "Perform compilation-action (above) on the current buffer"]
1391 ["AUTO, Save, Compile" verilog-auto-save-compile
1392 :help "Recompute AUTOs, save buffer, and compile"]
1393 ["Next Compile Error" next-error
1394 :help "Visit next compilation error message and corresponding source code"]
1395 ["Ignore Lint Warning at point" verilog-lint-off
1396 :help "Convert a Verilog linter warning line into a disable statement"]
1397 "----"
1398 ["Line up declarations around point" verilog-pretty-declarations
1399 :help "Line up declarations around point"]
1400 ["Line up equations around point" verilog-pretty-expr
1401 :help "Line up expressions around point"]
1402 ["Redo/insert comments on every end" verilog-label-be
1403 :help "Label matching begin ... end statements"]
1404 ["Expand [x:y] vector line" verilog-expand-vector
1405 :help "Take a signal vector on the current line and expand it to multiple lines"]
1406 ["Insert begin-end block" verilog-insert-block
1407 :help "Insert begin ... end"]
1408 ["Complete word" verilog-complete-word
1409 :help "Complete word at point"]
1410 "----"
1411 ["Recompute AUTOs" verilog-auto
1412 :help "Expand AUTO meta-comment statements"]
1413 ["Kill AUTOs" verilog-delete-auto
1414 :help "Remove AUTO expansions"]
1415 ["Diff AUTOs" verilog-diff-auto
1416 :help "Show differences in AUTO expansions"]
1417 ["Inject AUTOs" verilog-inject-auto
1418 :help "Inject AUTOs into legacy non-AUTO buffer"]
1419 ("AUTO Help..."
1420 ["AUTO General" (describe-function 'verilog-auto)
1421 :help "Help introduction on AUTOs"]
1422 ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
1423 :help "Help on verilog-library-flags"]
1424 ["AUTO Library Path" (describe-variable 'verilog-library-directories)
1425 :help "Help on verilog-library-directories"]
1426 ["AUTO Library Files" (describe-variable 'verilog-library-files)
1427 :help "Help on verilog-library-files"]
1428 ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
1429 :help "Help on verilog-library-extensions"]
1430 ["AUTO `define Reading" (describe-function 'verilog-read-defines)
1431 :help "Help on reading `defines"]
1432 ["AUTO `include Reading" (describe-function 'verilog-read-includes)
1433 :help "Help on parsing `includes"]
1434 ["AUTOARG" (describe-function 'verilog-auto-arg)
1435 :help "Help on AUTOARG - declaring module port list"]
1436 ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
1437 :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
1438 ["AUTOASSIGNMODPORT" (describe-function 'verilog-auto-assign-modport)
1439 :help "Help on AUTOASSIGNMODPORT - creating assignments to/from modports"]
1440 ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
1441 :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
1442 ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
1443 :help "Help on AUTOINOUTIN - copying i/o from another file as all inputs"]
1444 ["AUTOINOUTMODPORT" (describe-function 'verilog-auto-inout-modport)
1445 :help "Help on AUTOINOUTMODPORT - copying i/o from an interface modport"]
1446 ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
1447 :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
1448 ["AUTOINOUTPARAM" (describe-function 'verilog-auto-inout-param)
1449 :help "Help on AUTOINOUTPARAM - copying parameters from another file"]
1450 ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
1451 :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
1452 ["AUTOINOUT" (describe-function 'verilog-auto-inout)
1453 :help "Help on AUTOINOUT - adding inouts from cells"]
1454 ["AUTOINPUT" (describe-function 'verilog-auto-input)
1455 :help "Help on AUTOINPUT - adding inputs from cells"]
1456 ["AUTOINST" (describe-function 'verilog-auto-inst)
1457 :help "Help on AUTOINST - adding pins for cells"]
1458 ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
1459 :help "Help on expanding Verilog-2001 .* pins"]
1460 ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
1461 :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
1462 ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
1463 :help "Help on AUTOLOGIC - declaring logic signals"]
1464 ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
1465 :help "Help on AUTOOUTPUT - adding outputs from cells"]
1466 ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
1467 :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
1468 ["AUTOREG" (describe-function 'verilog-auto-reg)
1469 :help "Help on AUTOREG - declaring registers for non-wires"]
1470 ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
1471 :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
1472 ["AUTORESET" (describe-function 'verilog-auto-reset)
1473 :help "Help on AUTORESET - resetting always blocks"]
1474 ["AUTOSENSE" (describe-function 'verilog-auto-sense)
1475 :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
1476 ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
1477 :help "Help on AUTOTIEOFF - tying off unused outputs"]
1478 ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
1479 :help "Help on AUTOUNDEF - undefine all local defines"]
1480 ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
1481 :help "Help on AUTOUNUSED - terminating unused inputs"]
1482 ["AUTOWIRE" (describe-function 'verilog-auto-wire)
1483 :help "Help on AUTOWIRE - declaring wires for cells"]
1484 )
1485 "----"
1486 ["Submit bug report" verilog-submit-bug-report
1487 :help "Submit via mail a bug report on verilog-mode.el"]
1488 ["Version and FAQ" verilog-faq
1489 :help "Show the current version, and where to get the FAQ etc"]
1490 ["Customize Verilog Mode..." verilog-customize
1491 :help "Customize variables and other settings used by Verilog-Mode"]
1492 ["Customize Verilog Fonts & Colors" verilog-font-customize
1493 :help "Customize fonts used by Verilog-Mode."])))
1494
1495 (easy-menu-define
1496 verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
1497 (verilog-easy-menu-filter
1498 '("Statements"
1499 ["Header" verilog-sk-header
1500 :help "Insert a header block at the top of file"]
1501 ["Comment" verilog-sk-comment
1502 :help "Insert a comment block"]
1503 "----"
1504 ["Module" verilog-sk-module
1505 :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
1506 ["OVM Class" verilog-sk-ovm-class
1507 :help "Insert an OVM class block"]
1508 ["UVM Class" verilog-sk-uvm-class
1509 :help "Insert an UVM class block"]
1510 ["Primitive" verilog-sk-primitive
1511 :help "Insert a primitive .. (.. );.. endprimitive block"]
1512 "----"
1513 ["Input" verilog-sk-input
1514 :help "Insert an input declaration"]
1515 ["Output" verilog-sk-output
1516 :help "Insert an output declaration"]
1517 ["Inout" verilog-sk-inout
1518 :help "Insert an inout declaration"]
1519 ["Wire" verilog-sk-wire
1520 :help "Insert a wire declaration"]
1521 ["Reg" verilog-sk-reg
1522 :help "Insert a register declaration"]
1523 ["Define thing under point as a register" verilog-sk-define-signal
1524 :help "Define signal under point as a register at the top of the module"]
1525 "----"
1526 ["Initial" verilog-sk-initial
1527 :help "Insert an initial begin .. end block"]
1528 ["Always" verilog-sk-always
1529 :help "Insert an always @(AS) begin .. end block"]
1530 ["Function" verilog-sk-function
1531 :help "Insert a function .. begin .. end endfunction block"]
1532 ["Task" verilog-sk-task
1533 :help "Insert a task .. begin .. end endtask block"]
1534 ["Specify" verilog-sk-specify
1535 :help "Insert a specify .. endspecify block"]
1536 ["Generate" verilog-sk-generate
1537 :help "Insert a generate .. endgenerate block"]
1538 "----"
1539 ["Begin" verilog-sk-begin
1540 :help "Insert a begin .. end block"]
1541 ["If" verilog-sk-if
1542 :help "Insert an if (..) begin .. end block"]
1543 ["(if) else" verilog-sk-else-if
1544 :help "Insert an else if (..) begin .. end block"]
1545 ["For" verilog-sk-for
1546 :help "Insert a for (...) begin .. end block"]
1547 ["While" verilog-sk-while
1548 :help "Insert a while (...) begin .. end block"]
1549 ["Fork" verilog-sk-fork
1550 :help "Insert a fork begin .. end .. join block"]
1551 ["Repeat" verilog-sk-repeat
1552 :help "Insert a repeat (..) begin .. end block"]
1553 ["Case" verilog-sk-case
1554 :help "Insert a case block, prompting for details"]
1555 ["Casex" verilog-sk-casex
1556 :help "Insert a casex (...) item: begin.. end endcase block"]
1557 ["Casez" verilog-sk-casez
1558 :help "Insert a casez (...) item: begin.. end endcase block"])))
1559
1560 (defvar verilog-mode-abbrev-table nil
1561 "Abbrev table in use in Verilog-mode buffers.")
1562
1563 (define-abbrev-table 'verilog-mode-abbrev-table ())
1564 (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
1565 (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
1566 (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
1567 (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
1568 (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
1569 (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
1570 (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
1571 (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
1572 (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
1573 (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
1574 (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
1575 (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
1576 (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
1577 (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
1578 (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
1579 (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
1580 (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
1581 (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
1582 (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
1583 (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
1584 (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
1585 (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
1586 (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
1587 (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
1588 (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
1589
1590 ;;
1591 ;; Macros
1592 ;;
1593
1594 (defsubst verilog-within-string ()
1595 (nth 3 (parse-partial-sexp (point-at-bol) (point))))
1596
1597 (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
1598 "Replace occurrences of FROM-STRING with TO-STRING.
1599 FIXEDCASE and LITERAL as in `replace-match`. STRING is what to replace.
1600 The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
1601 will break, as the o's continuously replace. xa -> x works ok though."
1602 ;; Hopefully soon to an Emacs built-in
1603 ;; Also note \ in the replacement prevent multiple replacements; IE
1604 ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
1605 ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
1606 (let ((start 0))
1607 (while (string-match from-string string start)
1608 (setq string (replace-match to-string fixedcase literal string)
1609 start (min (length string) (+ (match-beginning 0) (length to-string)))))
1610 string))
1611
1612 (defsubst verilog-string-remove-spaces (string)
1613 "Remove spaces surrounding STRING."
1614 (save-match-data
1615 (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
1616 (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
1617 string))
1618
1619 (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
1620 ; checkdoc-params: (REGEXP BOUND NOERROR)
1621 "Like `re-search-forward', but skips over match in comments or strings."
1622 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1623 (while (and
1624 (re-search-forward REGEXP BOUND NOERROR)
1625 (setq mdata (match-data))
1626 (and (verilog-skip-forward-comment-or-string)
1627 (progn
1628 (setq mdata '(nil nil))
1629 (if BOUND
1630 (< (point) BOUND)
1631 t)))))
1632 (store-match-data mdata)
1633 (match-end 0)))
1634
1635 (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
1636 ; checkdoc-params: (REGEXP BOUND NOERROR)
1637 "Like `re-search-backward', but skips over match in comments or strings."
1638 (let ((mdata '(nil nil))) ;; So match-end will return nil if no matches found
1639 (while (and
1640 (re-search-backward REGEXP BOUND NOERROR)
1641 (setq mdata (match-data))
1642 (and (verilog-skip-backward-comment-or-string)
1643 (progn
1644 (setq mdata '(nil nil))
1645 (if BOUND
1646 (> (point) BOUND)
1647 t)))))
1648 (store-match-data mdata)
1649 (match-end 0)))
1650
1651 (defsubst verilog-re-search-forward-quick (regexp bound noerror)
1652 "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
1653 but trashes match data and is faster for REGEXP that doesn't match often.
1654 This uses `verilog-scan' and text properties to ignore comments,
1655 so there may be a large up front penalty for the first search."
1656 (let (pt)
1657 (while (and (not pt)
1658 (re-search-forward regexp bound noerror))
1659 (if (verilog-inside-comment-or-string-p)
1660 (re-search-forward "[/\"\n]" nil t) ;; Only way a comment or quote can end
1661 (setq pt (match-end 0))))
1662 pt))
1663
1664 (defsubst verilog-re-search-backward-quick (regexp bound noerror)
1665 ; checkdoc-params: (REGEXP BOUND NOERROR)
1666 "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
1667 but trashes match data and is faster for REGEXP that doesn't match often.
1668 This uses `verilog-scan' and text properties to ignore comments,
1669 so there may be a large up front penalty for the first search."
1670 (let (pt)
1671 (while (and (not pt)
1672 (re-search-backward regexp bound noerror))
1673 (if (verilog-inside-comment-or-string-p)
1674 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
1675 (setq pt (match-beginning 0))))
1676 pt))
1677
1678 (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
1679 "Like `re-search-forward', but first search for SUBSTR constant.
1680 Then searched for the normal REGEXP (which contains SUBSTR), with given
1681 BOUND and NOERROR. The REGEXP must fit within a single line.
1682 This speeds up complicated regexp matches."
1683 ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
1684 ;; thus require matches to be on one line, and use beginning-of-line.
1685 (let (done)
1686 (while (and (not done)
1687 (search-forward substr bound noerror))
1688 (save-excursion
1689 (beginning-of-line)
1690 (setq done (re-search-forward regexp (point-at-eol) noerror)))
1691 (unless (and (<= (match-beginning 0) (point))
1692 (>= (match-end 0) (point)))
1693 (setq done nil)))
1694 (when done (goto-char done))
1695 done))
1696 ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1697
1698 (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
1699 "Like `re-search-backward', but first search for SUBSTR constant.
1700 Then searched for the normal REGEXP (which contains SUBSTR), with given
1701 BOUND and NOERROR. The REGEXP must fit within a single line.
1702 This speeds up complicated regexp matches."
1703 ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
1704 ;; thus require matches to be on one line, and use beginning-of-line.
1705 (let (done)
1706 (while (and (not done)
1707 (search-backward substr bound noerror))
1708 (save-excursion
1709 (end-of-line)
1710 (setq done (re-search-backward regexp (point-at-bol) noerror)))
1711 (unless (and (<= (match-beginning 0) (point))
1712 (>= (match-end 0) (point)))
1713 (setq done nil)))
1714 (when done (goto-char done))
1715 done))
1716 ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ;;-end (test bait)
1717
1718 (defun verilog-delete-trailing-whitespace ()
1719 "Delete trailing spaces or tabs, but not newlines nor linefeeds.
1720 Also add missing final newline.
1721
1722 To call this from the command line, see \\[verilog-batch-diff-auto].
1723
1724 To call on \\[verilog-auto], set `verilog-auto-delete-trailing-whitespace'."
1725 ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
1726 (save-excursion
1727 (goto-char (point-min))
1728 (while (re-search-forward "[ \t]+$" nil t) ;; Not syntactic WS as no formfeed
1729 (replace-match "" nil nil))
1730 (goto-char (point-max))
1731 (unless (bolp) (insert "\n"))))
1732
1733 (defvar compile-command)
1734
1735 ;; compilation program
1736 (defun verilog-set-compile-command ()
1737 "Function to compute shell command to compile Verilog.
1738
1739 This reads `verilog-tool' and sets `compile-command'. This specifies the
1740 program that executes when you type \\[compile] or
1741 \\[verilog-auto-save-compile].
1742
1743 By default `verilog-tool' uses a Makefile if one exists in the
1744 current directory. If not, it is set to the `verilog-linter',
1745 `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
1746 or `verilog-simulator' variables, as selected with the Verilog ->
1747 \"Choose Compilation Action\" menu.
1748
1749 You should set `verilog-tool' or the other variables to the path and
1750 arguments for your Verilog simulator. For example:
1751 \"vcs -p123 -O\"
1752 or a string like:
1753 \"(cd /tmp; surecov %s)\".
1754
1755 In the former case, the path to the current buffer is concat'ed to the
1756 value of `verilog-tool'; in the later, the path to the current buffer is
1757 substituted for the %s.
1758
1759 Where __FLAGS__ appears in the string `verilog-current-flags'
1760 will be substituted.
1761
1762 Where __FILE__ appears in the string, the variable
1763 `buffer-file-name' of the current buffer, without the directory
1764 portion, will be substituted."
1765 (interactive)
1766 (cond
1767 ((or (file-exists-p "makefile") ;If there is a makefile, use it
1768 (file-exists-p "Makefile"))
1769 (set (make-local-variable 'compile-command) "make "))
1770 (t
1771 (set (make-local-variable 'compile-command)
1772 (if verilog-tool
1773 (if (string-match "%s" (eval verilog-tool))
1774 (format (eval verilog-tool) (or buffer-file-name ""))
1775 (concat (eval verilog-tool) " " (or buffer-file-name "")))
1776 ""))))
1777 (verilog-modify-compile-command))
1778
1779 (defun verilog-expand-command (command)
1780 "Replace meta-information in COMMAND and return it.
1781 Where __FLAGS__ appears in the string `verilog-current-flags'
1782 will be substituted. Where __FILE__ appears in the string, the
1783 current buffer's file-name, without the directory portion, will
1784 be substituted."
1785 (setq command (verilog-string-replace-matches
1786 ;; Note \\b only works if under verilog syntax table
1787 "\\b__FLAGS__\\b" (verilog-current-flags)
1788 t t command))
1789 (setq command (verilog-string-replace-matches
1790 "\\b__FILE__\\b" (file-name-nondirectory
1791 (or (buffer-file-name) ""))
1792 t t command))
1793 command)
1794
1795 (defun verilog-modify-compile-command ()
1796 "Update `compile-command' using `verilog-expand-command'."
1797 (when (and
1798 (stringp compile-command)
1799 (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
1800 (set (make-local-variable 'compile-command)
1801 (verilog-expand-command compile-command))))
1802
1803 (if (featurep 'xemacs)
1804 ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
1805 (defun verilog-error-regexp-add-xemacs ()
1806 "Teach XEmacs about verilog errors.
1807 Called by `compilation-mode-hook'. This allows \\[next-error] to
1808 find the errors."
1809 (interactive)
1810 (if (boundp 'compilation-error-regexp-systems-alist)
1811 (if (and
1812 (not (equal compilation-error-regexp-systems-list 'all))
1813 (not (member compilation-error-regexp-systems-list 'verilog)))
1814 (push 'verilog compilation-error-regexp-systems-list)))
1815 (if (boundp 'compilation-error-regexp-alist-alist)
1816 (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
1817 (setcdr compilation-error-regexp-alist-alist
1818 (cons verilog-error-regexp-xemacs-alist
1819 (cdr compilation-error-regexp-alist-alist)))))
1820 (if (boundp 'compilation-font-lock-keywords)
1821 (progn
1822 (set (make-local-variable 'compilation-font-lock-keywords)
1823 verilog-error-font-lock-keywords)
1824 (font-lock-set-defaults)))
1825 ;; Need to re-run compilation-error-regexp builder
1826 (if (fboundp 'compilation-build-compilation-error-regexp-alist)
1827 (compilation-build-compilation-error-regexp-alist))
1828 ))
1829
1830 ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
1831 (defun verilog-error-regexp-add-emacs ()
1832 "Tell Emacs compile that we are Verilog.
1833 Called by `compilation-mode-hook'. This allows \\[next-error] to
1834 find the errors."
1835 (interactive)
1836 (if (boundp 'compilation-error-regexp-alist-alist)
1837 (progn
1838 (if (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
1839 (mapcar
1840 (lambda (item)
1841 (push (car item) compilation-error-regexp-alist)
1842 (push item compilation-error-regexp-alist-alist)
1843 )
1844 verilog-error-regexp-emacs-alist)))))
1845
1846 (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
1847 (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
1848
1849 (defconst verilog-directive-re
1850 (eval-when-compile
1851 (verilog-regexp-words
1852 '(
1853 "`case" "`default" "`define" "`else" "`elsif" "`endfor" "`endif"
1854 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
1855 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
1856 "`time_scale" "`undef" "`while" ))))
1857
1858 (defconst verilog-directive-re-1
1859 (concat "[ \t]*" verilog-directive-re))
1860
1861 (defconst verilog-directive-begin
1862 "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
1863
1864 (defconst verilog-directive-middle
1865 "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
1866
1867 (defconst verilog-directive-end
1868 "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
1869
1870 (defconst verilog-ovm-begin-re
1871 (eval-when-compile
1872 (verilog-regexp-opt
1873 '(
1874 "`ovm_component_utils_begin"
1875 "`ovm_component_param_utils_begin"
1876 "`ovm_field_utils_begin"
1877 "`ovm_object_utils_begin"
1878 "`ovm_object_param_utils_begin"
1879 "`ovm_sequence_utils_begin"
1880 "`ovm_sequencer_utils_begin"
1881 ) nil )))
1882
1883 (defconst verilog-ovm-end-re
1884 (eval-when-compile
1885 (verilog-regexp-opt
1886 '(
1887 "`ovm_component_utils_end"
1888 "`ovm_field_utils_end"
1889 "`ovm_object_utils_end"
1890 "`ovm_sequence_utils_end"
1891 "`ovm_sequencer_utils_end"
1892 ) nil )))
1893
1894 (defconst verilog-uvm-begin-re
1895 (eval-when-compile
1896 (verilog-regexp-opt
1897 '(
1898 "`uvm_component_utils_begin"
1899 "`uvm_component_param_utils_begin"
1900 "`uvm_field_utils_begin"
1901 "`uvm_object_utils_begin"
1902 "`uvm_object_param_utils_begin"
1903 "`uvm_sequence_utils_begin"
1904 "`uvm_sequencer_utils_begin"
1905 ) nil )))
1906
1907 (defconst verilog-uvm-end-re
1908 (eval-when-compile
1909 (verilog-regexp-opt
1910 '(
1911 "`uvm_component_utils_end"
1912 "`uvm_field_utils_end"
1913 "`uvm_object_utils_end"
1914 "`uvm_sequence_utils_end"
1915 "`uvm_sequencer_utils_end"
1916 ) nil )))
1917
1918 (defconst verilog-vmm-begin-re
1919 (eval-when-compile
1920 (verilog-regexp-opt
1921 '(
1922 "`vmm_data_member_begin"
1923 "`vmm_env_member_begin"
1924 "`vmm_scenario_member_begin"
1925 "`vmm_subenv_member_begin"
1926 "`vmm_xactor_member_begin"
1927 ) nil ) ) )
1928
1929 (defconst verilog-vmm-end-re
1930 (eval-when-compile
1931 (verilog-regexp-opt
1932 '(
1933 "`vmm_data_member_end"
1934 "`vmm_env_member_end"
1935 "`vmm_scenario_member_end"
1936 "`vmm_subenv_member_end"
1937 "`vmm_xactor_member_end"
1938 ) nil ) ) )
1939
1940 (defconst verilog-vmm-statement-re
1941 (eval-when-compile
1942 (verilog-regexp-opt
1943 '(
1944 ;; "`vmm_xactor_member_enum_array"
1945 "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
1946 ;; "`vmm_xactor_member_scalar_array"
1947 ;; "`vmm_xactor_member_scalar"
1948 ) nil )))
1949
1950 (defconst verilog-ovm-statement-re
1951 (eval-when-compile
1952 (verilog-regexp-opt
1953 '(
1954 ;; Statements
1955 "`DUT_ERROR"
1956 "`MESSAGE"
1957 "`dut_error"
1958 "`message"
1959 "`ovm_analysis_imp_decl"
1960 "`ovm_blocking_get_imp_decl"
1961 "`ovm_blocking_get_peek_imp_decl"
1962 "`ovm_blocking_master_imp_decl"
1963 "`ovm_blocking_peek_imp_decl"
1964 "`ovm_blocking_put_imp_decl"
1965 "`ovm_blocking_slave_imp_decl"
1966 "`ovm_blocking_transport_imp_decl"
1967 "`ovm_component_registry"
1968 "`ovm_component_registry_param"
1969 "`ovm_component_utils"
1970 "`ovm_create"
1971 "`ovm_create_seq"
1972 "`ovm_declare_sequence_lib"
1973 "`ovm_do"
1974 "`ovm_do_seq"
1975 "`ovm_do_seq_with"
1976 "`ovm_do_with"
1977 "`ovm_error"
1978 "`ovm_fatal"
1979 "`ovm_field_aa_int_byte"
1980 "`ovm_field_aa_int_byte_unsigned"
1981 "`ovm_field_aa_int_int"
1982 "`ovm_field_aa_int_int_unsigned"
1983 "`ovm_field_aa_int_integer"
1984 "`ovm_field_aa_int_integer_unsigned"
1985 "`ovm_field_aa_int_key"
1986 "`ovm_field_aa_int_longint"
1987 "`ovm_field_aa_int_longint_unsigned"
1988 "`ovm_field_aa_int_shortint"
1989 "`ovm_field_aa_int_shortint_unsigned"
1990 "`ovm_field_aa_int_string"
1991 "`ovm_field_aa_object_int"
1992 "`ovm_field_aa_object_string"
1993 "`ovm_field_aa_string_int"
1994 "`ovm_field_aa_string_string"
1995 "`ovm_field_array_int"
1996 "`ovm_field_array_object"
1997 "`ovm_field_array_string"
1998 "`ovm_field_enum"
1999 "`ovm_field_event"
2000 "`ovm_field_int"
2001 "`ovm_field_object"
2002 "`ovm_field_queue_int"
2003 "`ovm_field_queue_object"
2004 "`ovm_field_queue_string"
2005 "`ovm_field_sarray_int"
2006 "`ovm_field_string"
2007 "`ovm_field_utils"
2008 "`ovm_file"
2009 "`ovm_get_imp_decl"
2010 "`ovm_get_peek_imp_decl"
2011 "`ovm_info"
2012 "`ovm_info1"
2013 "`ovm_info2"
2014 "`ovm_info3"
2015 "`ovm_info4"
2016 "`ovm_line"
2017 "`ovm_master_imp_decl"
2018 "`ovm_msg_detail"
2019 "`ovm_non_blocking_transport_imp_decl"
2020 "`ovm_nonblocking_get_imp_decl"
2021 "`ovm_nonblocking_get_peek_imp_decl"
2022 "`ovm_nonblocking_master_imp_decl"
2023 "`ovm_nonblocking_peek_imp_decl"
2024 "`ovm_nonblocking_put_imp_decl"
2025 "`ovm_nonblocking_slave_imp_decl"
2026 "`ovm_object_registry"
2027 "`ovm_object_registry_param"
2028 "`ovm_object_utils"
2029 "`ovm_peek_imp_decl"
2030 "`ovm_phase_func_decl"
2031 "`ovm_phase_task_decl"
2032 "`ovm_print_aa_int_object"
2033 "`ovm_print_aa_string_int"
2034 "`ovm_print_aa_string_object"
2035 "`ovm_print_aa_string_string"
2036 "`ovm_print_array_int"
2037 "`ovm_print_array_object"
2038 "`ovm_print_array_string"
2039 "`ovm_print_object_queue"
2040 "`ovm_print_queue_int"
2041 "`ovm_print_string_queue"
2042 "`ovm_put_imp_decl"
2043 "`ovm_rand_send"
2044 "`ovm_rand_send_with"
2045 "`ovm_send"
2046 "`ovm_sequence_utils"
2047 "`ovm_slave_imp_decl"
2048 "`ovm_transport_imp_decl"
2049 "`ovm_update_sequence_lib"
2050 "`ovm_update_sequence_lib_and_item"
2051 "`ovm_warning"
2052 "`static_dut_error"
2053 "`static_message") nil )))
2054
2055 (defconst verilog-uvm-statement-re
2056 (eval-when-compile
2057 (verilog-regexp-opt
2058 '(
2059 ;; Statements
2060 "`uvm_analysis_imp_decl"
2061 "`uvm_blocking_get_imp_decl"
2062 "`uvm_blocking_get_peek_imp_decl"
2063 "`uvm_blocking_master_imp_decl"
2064 "`uvm_blocking_peek_imp_decl"
2065 "`uvm_blocking_put_imp_decl"
2066 "`uvm_blocking_slave_imp_decl"
2067 "`uvm_blocking_transport_imp_decl"
2068 "`uvm_component_param_utils"
2069 "`uvm_component_registry"
2070 "`uvm_component_registry_param"
2071 "`uvm_component_utils"
2072 "`uvm_create"
2073 "`uvm_create_on"
2074 "`uvm_create_seq" ;; Undocumented in 1.1
2075 "`uvm_declare_p_sequencer"
2076 "`uvm_declare_sequence_lib" ;; Deprecated in 1.1
2077 "`uvm_do"
2078 "`uvm_do_callbacks"
2079 "`uvm_do_callbacks_exit_on"
2080 "`uvm_do_obj_callbacks"
2081 "`uvm_do_obj_callbacks_exit_on"
2082 "`uvm_do_on"
2083 "`uvm_do_on_pri"
2084 "`uvm_do_on_pri_with"
2085 "`uvm_do_on_with"
2086 "`uvm_do_pri"
2087 "`uvm_do_pri_with"
2088 "`uvm_do_seq" ;; Undocumented in 1.1
2089 "`uvm_do_seq_with" ;; Undocumented in 1.1
2090 "`uvm_do_with"
2091 "`uvm_error"
2092 "`uvm_error_context"
2093 "`uvm_fatal"
2094 "`uvm_fatal_context"
2095 "`uvm_field_aa_int_byte"
2096 "`uvm_field_aa_int_byte_unsigned"
2097 "`uvm_field_aa_int_enum"
2098 "`uvm_field_aa_int_int"
2099 "`uvm_field_aa_int_int_unsigned"
2100 "`uvm_field_aa_int_integer"
2101 "`uvm_field_aa_int_integer_unsigned"
2102 "`uvm_field_aa_int_key"
2103 "`uvm_field_aa_int_longint"
2104 "`uvm_field_aa_int_longint_unsigned"
2105 "`uvm_field_aa_int_shortint"
2106 "`uvm_field_aa_int_shortint_unsigned"
2107 "`uvm_field_aa_int_string"
2108 "`uvm_field_aa_object_int"
2109 "`uvm_field_aa_object_string"
2110 "`uvm_field_aa_string_int"
2111 "`uvm_field_aa_string_string"
2112 "`uvm_field_array_enum"
2113 "`uvm_field_array_int"
2114 "`uvm_field_array_object"
2115 "`uvm_field_array_string"
2116 "`uvm_field_enum"
2117 "`uvm_field_event"
2118 "`uvm_field_int"
2119 "`uvm_field_object"
2120 "`uvm_field_queue_enum"
2121 "`uvm_field_queue_int"
2122 "`uvm_field_queue_object"
2123 "`uvm_field_queue_string"
2124 "`uvm_field_real"
2125 "`uvm_field_sarray_enum"
2126 "`uvm_field_sarray_int"
2127 "`uvm_field_sarray_object"
2128 "`uvm_field_sarray_string"
2129 "`uvm_field_string"
2130 "`uvm_field_utils"
2131 "`uvm_file" ;; Undocumented in 1.1, use `__FILE__
2132 "`uvm_get_imp_decl"
2133 "`uvm_get_peek_imp_decl"
2134 "`uvm_info"
2135 "`uvm_info_context"
2136 "`uvm_line" ;; Undocumented in 1.1, use `__LINE__
2137 "`uvm_master_imp_decl"
2138 "`uvm_non_blocking_transport_imp_decl" ;; Deprecated in 1.1
2139 "`uvm_nonblocking_get_imp_decl"
2140 "`uvm_nonblocking_get_peek_imp_decl"
2141 "`uvm_nonblocking_master_imp_decl"
2142 "`uvm_nonblocking_peek_imp_decl"
2143 "`uvm_nonblocking_put_imp_decl"
2144 "`uvm_nonblocking_slave_imp_decl"
2145 "`uvm_nonblocking_transport_imp_decl"
2146 "`uvm_object_param_utils"
2147 "`uvm_object_registry"
2148 "`uvm_object_registry_param" ;; Undocumented in 1.1
2149 "`uvm_object_utils"
2150 "`uvm_pack_array"
2151 "`uvm_pack_arrayN"
2152 "`uvm_pack_enum"
2153 "`uvm_pack_enumN"
2154 "`uvm_pack_int"
2155 "`uvm_pack_intN"
2156 "`uvm_pack_queue"
2157 "`uvm_pack_queueN"
2158 "`uvm_pack_real"
2159 "`uvm_pack_sarray"
2160 "`uvm_pack_sarrayN"
2161 "`uvm_pack_string"
2162 "`uvm_peek_imp_decl"
2163 "`uvm_put_imp_decl"
2164 "`uvm_rand_send"
2165 "`uvm_rand_send_pri"
2166 "`uvm_rand_send_pri_with"
2167 "`uvm_rand_send_with"
2168 "`uvm_record_attribute"
2169 "`uvm_record_field"
2170 "`uvm_register_cb"
2171 "`uvm_send"
2172 "`uvm_send_pri"
2173 "`uvm_sequence_utils" ;; Deprecated in 1.1
2174 "`uvm_set_super_type"
2175 "`uvm_slave_imp_decl"
2176 "`uvm_transport_imp_decl"
2177 "`uvm_unpack_array"
2178 "`uvm_unpack_arrayN"
2179 "`uvm_unpack_enum"
2180 "`uvm_unpack_enumN"
2181 "`uvm_unpack_int"
2182 "`uvm_unpack_intN"
2183 "`uvm_unpack_queue"
2184 "`uvm_unpack_queueN"
2185 "`uvm_unpack_real"
2186 "`uvm_unpack_sarray"
2187 "`uvm_unpack_sarrayN"
2188 "`uvm_unpack_string"
2189 "`uvm_update_sequence_lib" ;; Deprecated in 1.1
2190 "`uvm_update_sequence_lib_and_item" ;; Deprecated in 1.1
2191 "`uvm_warning"
2192 "`uvm_warning_context") nil )))
2193
2194
2195 ;;
2196 ;; Regular expressions used to calculate indent, etc.
2197 ;;
2198 (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
2199 ;; Want to match
2200 ;; aa :
2201 ;; aa,bb :
2202 ;; a[34:32] :
2203 ;; a,
2204 ;; b :
2205 (defconst verilog-assignment-operator-re
2206 (eval-when-compile
2207 (verilog-regexp-opt
2208 `(
2209 ;; blocking assignment_operator
2210 "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
2211 ;; non blocking assignment operator
2212 "<="
2213 ;; comparison
2214 "==" "!=" "===" "!===" "<=" ">=" "==\?" "!=\?"
2215 ;; event_trigger
2216 "->" "->>"
2217 ;; property_expr
2218 "|->" "|=>"
2219 ;; Is this a legal verilog operator?
2220 ":="
2221 ) 't
2222 )))
2223 (defconst verilog-assignment-operation-re
2224 (concat
2225 ; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
2226 ; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
2227 "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
2228
2229 (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
2230 (defconst verilog-property-re
2231 (concat "\\(" verilog-label-re "\\)?"
2232 "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
2233 ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
2234
2235 (defconst verilog-no-indent-begin-re
2236 "\\<\\(if\\|else\\|while\\|for\\|repeat\\|always\\|always_comb\\|always_ff\\|always_latch\\)\\>")
2237
2238 (defconst verilog-ends-re
2239 ;; Parenthesis indicate type of keyword found
2240 (concat
2241 "\\(\\<else\\>\\)\\|" ; 1
2242 "\\(\\<if\\>\\)\\|" ; 2
2243 "\\(\\<assert\\>\\)\\|" ; 3
2244 "\\(\\<end\\>\\)\\|" ; 3.1
2245 "\\(\\<endcase\\>\\)\\|" ; 4
2246 "\\(\\<endfunction\\>\\)\\|" ; 5
2247 "\\(\\<endtask\\>\\)\\|" ; 6
2248 "\\(\\<endspecify\\>\\)\\|" ; 7
2249 "\\(\\<endtable\\>\\)\\|" ; 8
2250 "\\(\\<endgenerate\\>\\)\\|" ; 9
2251 "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
2252 "\\(\\<endclass\\>\\)\\|" ; 11
2253 "\\(\\<endgroup\\>\\)\\|" ; 12
2254 ;; VMM
2255 "\\(\\<`vmm_data_member_end\\>\\)\\|"
2256 "\\(\\<`vmm_env_member_end\\>\\)\\|"
2257 "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
2258 "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
2259 "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
2260 ;; OVM
2261 "\\(\\<`ovm_component_utils_end\\>\\)\\|"
2262 "\\(\\<`ovm_field_utils_end\\>\\)\\|"
2263 "\\(\\<`ovm_object_utils_end\\>\\)\\|"
2264 "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
2265 "\\(\\<`ovm_sequencer_utils_end\\>\\)"
2266 ;; UVM
2267 "\\(\\<`uvm_component_utils_end\\>\\)\\|"
2268 "\\(\\<`uvm_field_utils_end\\>\\)\\|"
2269 "\\(\\<`uvm_object_utils_end\\>\\)\\|"
2270 "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
2271 "\\(\\<`uvm_sequencer_utils_end\\>\\)"
2272 ))
2273
2274 (defconst verilog-auto-end-comment-lines-re
2275 ;; Matches to names in this list cause auto-end-commenting
2276 (concat "\\("
2277 verilog-directive-re "\\)\\|\\("
2278 (eval-when-compile
2279 (verilog-regexp-words
2280 `( "begin"
2281 "else"
2282 "end"
2283 "endcase"
2284 "endclass"
2285 "endclocking"
2286 "endgroup"
2287 "endfunction"
2288 "endmodule"
2289 "endprogram"
2290 "endprimitive"
2291 "endinterface"
2292 "endpackage"
2293 "endsequence"
2294 "endspecify"
2295 "endtable"
2296 "endtask"
2297 "join"
2298 "join_any"
2299 "join_none"
2300 "module"
2301 "macromodule"
2302 "primitive"
2303 "interface"
2304 "package")))
2305 "\\)"))
2306
2307 ;;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
2308 ;;; verilog-end-block-ordered-re matches exactly the same strings.
2309 (defconst verilog-end-block-ordered-re
2310 ;; Parenthesis indicate type of keyword found
2311 (concat "\\(\\<endcase\\>\\)\\|" ; 1
2312 "\\(\\<end\\>\\)\\|" ; 2
2313 "\\(\\<end" ; 3, but not used
2314 "\\(" ; 4, but not used
2315 "\\(function\\)\\|" ; 5
2316 "\\(task\\)\\|" ; 6
2317 "\\(module\\)\\|" ; 7
2318 "\\(primitive\\)\\|" ; 8
2319 "\\(interface\\)\\|" ; 9
2320 "\\(package\\)\\|" ; 10
2321 "\\(class\\)\\|" ; 11
2322 "\\(group\\)\\|" ; 12
2323 "\\(program\\)\\|" ; 13
2324 "\\(sequence\\)\\|" ; 14
2325 "\\(clocking\\)\\|" ; 15
2326 "\\)\\>\\)"))
2327 (defconst verilog-end-block-re
2328 (eval-when-compile
2329 (verilog-regexp-words
2330
2331 `("end" ;; closes begin
2332 "endcase" ;; closes any of case, casex casez or randcase
2333 "join" "join_any" "join_none" ;; closes fork
2334 "endclass"
2335 "endtable"
2336 "endspecify"
2337 "endfunction"
2338 "endgenerate"
2339 "endtask"
2340 "endgroup"
2341 "endproperty"
2342 "endinterface"
2343 "endpackage"
2344 "endprogram"
2345 "endsequence"
2346 "endclocking"
2347 ;; OVM
2348 "`ovm_component_utils_end"
2349 "`ovm_field_utils_end"
2350 "`ovm_object_utils_end"
2351 "`ovm_sequence_utils_end"
2352 "`ovm_sequencer_utils_end"
2353 ;; UVM
2354 "`uvm_component_utils_end"
2355 "`uvm_field_utils_end"
2356 "`uvm_object_utils_end"
2357 "`uvm_sequence_utils_end"
2358 "`uvm_sequencer_utils_end"
2359 ;; VMM
2360 "`vmm_data_member_end"
2361 "`vmm_env_member_end"
2362 "`vmm_scenario_member_end"
2363 "`vmm_subenv_member_end"
2364 "`vmm_xactor_member_end"
2365 ))))
2366
2367
2368 (defconst verilog-endcomment-reason-re
2369 ;; Parenthesis indicate type of keyword found
2370 (concat
2371 "\\(\\<begin\\>\\)\\|" ; 1
2372 "\\(\\<else\\>\\)\\|" ; 2
2373 "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
2374 "\\(\\<always_comb\\>\\(\[ \t\]*@\\)?\\)\\|" ; 4
2375 "\\(\\<always_ff\\>\\(\[ \t\]*@\\)?\\)\\|" ; 5
2376 "\\(\\<always_latch\\>\\(\[ \t\]*@\\)?\\)\\|" ; 6
2377 "\\(\\<fork\\>\\)\\|" ; 7
2378 "\\(\\<always\\>\\(\[ \t\]*@\\)?\\)\\|"
2379 "\\(\\<if\\>\\)\\|"
2380 verilog-property-re "\\|"
2381 "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
2382 "\\(\\<clocking\\>\\)\\|"
2383 "\\(\\<task\\>\\)\\|"
2384 "\\(\\<function\\>\\)\\|"
2385 "\\(\\<initial\\>\\)\\|"
2386 "\\(\\<interface\\>\\)\\|"
2387 "\\(\\<package\\>\\)\\|"
2388 "\\(\\<final\\>\\)\\|"
2389 "\\(@\\)\\|"
2390 "\\(\\<while\\>\\)\\|"
2391 "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
2392 "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
2393 "#"))
2394
2395 (defconst verilog-named-block-re "begin[ \t]*:")
2396
2397 ;; These words begin a block which can occur inside a module which should be indented,
2398 ;; and closed with the respective word from the end-block list
2399
2400 (defconst verilog-beg-block-re
2401 (eval-when-compile
2402 (verilog-regexp-words
2403 `("begin"
2404 "case" "casex" "casez" "randcase"
2405 "clocking"
2406 "generate"
2407 "fork"
2408 "function"
2409 "property"
2410 "specify"
2411 "table"
2412 "task"
2413 ;; OVM
2414 "`ovm_component_utils_begin"
2415 "`ovm_component_param_utils_begin"
2416 "`ovm_field_utils_begin"
2417 "`ovm_object_utils_begin"
2418 "`ovm_object_param_utils_begin"
2419 "`ovm_sequence_utils_begin"
2420 "`ovm_sequencer_utils_begin"
2421 ;; UVM
2422 "`uvm_component_utils_begin"
2423 "`uvm_component_param_utils_begin"
2424 "`uvm_field_utils_begin"
2425 "`uvm_object_utils_begin"
2426 "`uvm_object_param_utils_begin"
2427 "`uvm_sequence_utils_begin"
2428 "`uvm_sequencer_utils_begin"
2429 ;; VMM
2430 "`vmm_data_member_begin"
2431 "`vmm_env_member_begin"
2432 "`vmm_scenario_member_begin"
2433 "`vmm_subenv_member_begin"
2434 "`vmm_xactor_member_begin"
2435 ))))
2436 ;; These are the same words, in a specific order in the regular
2437 ;; expression so that matching will work nicely for
2438 ;; verilog-forward-sexp and verilog-calc-indent
2439 (defconst verilog-beg-block-re-ordered
2440 ( concat "\\(\\<begin\\>\\)" ;1
2441 "\\|\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
2442 "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
2443 "\\|\\(\\<class\\>\\)" ;6
2444 "\\|\\(\\<table\\>\\)" ;7
2445 "\\|\\(\\<specify\\>\\)" ;8
2446 "\\|\\(\\<function\\>\\)" ;9
2447 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<function\\>\\)" ;10
2448 "\\|\\(\\<task\\>\\)" ;14
2449 "\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)*\\<task\\>\\)" ;15
2450 "\\|\\(\\<generate\\>\\)" ;18
2451 "\\|\\(\\<covergroup\\>\\)" ;16 20
2452 "\\|\\(\\(\\(\\<cover\\>\\s-+\\)\\|\\(\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;17 21
2453 "\\|\\(\\<\\(rand\\)?sequence\\>\\)" ;21 25
2454 "\\|\\(\\<clocking\\>\\)" ;22 27
2455 "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;28
2456 "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
2457 ;;
2458 ))
2459
2460 (defconst verilog-end-block-ordered-rry
2461 [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2462 "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
2463 "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
2464 "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
2465 "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
2466 "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
2467 "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
2468 "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
2469 "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
2470 "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
2471 "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
2472 "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
2473 "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
2474 ] )
2475
2476 (defconst verilog-nameable-item-re
2477 (eval-when-compile
2478 (verilog-regexp-words
2479 `("begin"
2480 "fork"
2481 "join" "join_any" "join_none"
2482 "end"
2483 "endcase"
2484 "endconfig"
2485 "endclass"
2486 "endclocking"
2487 "endfunction"
2488 "endgenerate"
2489 "endmodule"
2490 "endprimitive"
2491 "endinterface"
2492 "endpackage"
2493 "endspecify"
2494 "endtable"
2495 "endtask" )
2496 )))
2497
2498 (defconst verilog-declaration-opener
2499 (eval-when-compile
2500 (verilog-regexp-words
2501 `("module" "begin" "task" "function"))))
2502
2503 (defconst verilog-declaration-prefix-re
2504 (eval-when-compile
2505 (verilog-regexp-words
2506 `(
2507 ;; port direction
2508 "inout" "input" "output" "ref"
2509 ;; changeableness
2510 "const" "static" "protected" "local"
2511 ;; parameters
2512 "localparam" "parameter" "var"
2513 ;; type creation
2514 "typedef"
2515 ))))
2516 (defconst verilog-declaration-core-re
2517 (eval-when-compile
2518 (verilog-regexp-words
2519 `(
2520 ;; port direction (by themselves)
2521 "inout" "input" "output"
2522 ;; integer_atom_type
2523 "byte" "shortint" "int" "longint" "integer" "time"
2524 ;; integer_vector_type
2525 "bit" "logic" "reg"
2526 ;; non_integer_type
2527 "shortreal" "real" "realtime"
2528 ;; net_type
2529 "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
2530 ;; misc
2531 "string" "event" "chandle" "virtual" "enum" "genvar"
2532 "struct" "union"
2533 ;; builtin classes
2534 "mailbox" "semaphore"
2535 ))))
2536 (defconst verilog-declaration-re
2537 (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
2538 (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
2539 (defconst verilog-optional-signed-re "\\s-*\\(signed\\)?")
2540 (defconst verilog-optional-signed-range-re
2541 (concat
2542 "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
2543 (defconst verilog-macroexp-re "`\\sw+")
2544
2545 (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
2546 (defconst verilog-declaration-re-2-no-macro
2547 (concat "\\s-*" verilog-declaration-re
2548 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2549 "\\)?"))
2550 (defconst verilog-declaration-re-2-macro
2551 (concat "\\s-*" verilog-declaration-re
2552 "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
2553 "\\|\\(" verilog-macroexp-re "\\)"
2554 "\\)?"))
2555 (defconst verilog-declaration-re-1-macro
2556 (concat "^" verilog-declaration-re-2-macro))
2557
2558 (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
2559
2560 (defconst verilog-defun-re
2561 (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
2562 (defconst verilog-end-defun-re
2563 (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
2564 (defconst verilog-zero-indent-re
2565 (concat verilog-defun-re "\\|" verilog-end-defun-re))
2566 (defconst verilog-inst-comment-re
2567 (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
2568
2569 (defconst verilog-behavioral-block-beg-re
2570 (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
2571 "function" "task"))))
2572 (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
2573 (defconst verilog-indent-re
2574 (eval-when-compile
2575 (verilog-regexp-words
2576 `(
2577 "{"
2578 "always" "always_latch" "always_ff" "always_comb"
2579 "begin" "end"
2580 ; "unique" "priority"
2581 "case" "casex" "casez" "randcase" "endcase"
2582 "class" "endclass"
2583 "clocking" "endclocking"
2584 "config" "endconfig"
2585 "covergroup" "endgroup"
2586 "fork" "join" "join_any" "join_none"
2587 "function" "endfunction"
2588 "final"
2589 "generate" "endgenerate"
2590 "initial"
2591 "interface" "endinterface"
2592 "module" "macromodule" "endmodule"
2593 "package" "endpackage"
2594 "primitive" "endprimitive"
2595 "program" "endprogram"
2596 "property" "endproperty"
2597 "sequence" "randsequence" "endsequence"
2598 "specify" "endspecify"
2599 "table" "endtable"
2600 "task" "endtask"
2601 "virtual"
2602 "`case"
2603 "`default"
2604 "`define" "`undef"
2605 "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
2606 "`while" "`endwhile"
2607 "`for" "`endfor"
2608 "`format"
2609 "`include"
2610 "`let"
2611 "`protect" "`endprotect"
2612 "`switch" "`endswitch"
2613 "`timescale"
2614 "`time_scale"
2615 ;; OVM Begin tokens
2616 "`ovm_component_utils_begin"
2617 "`ovm_component_param_utils_begin"
2618 "`ovm_field_utils_begin"
2619 "`ovm_object_utils_begin"
2620 "`ovm_object_param_utils_begin"
2621 "`ovm_sequence_utils_begin"
2622 "`ovm_sequencer_utils_begin"
2623 ;; OVM End tokens
2624 "`ovm_component_utils_end"
2625 "`ovm_field_utils_end"
2626 "`ovm_object_utils_end"
2627 "`ovm_sequence_utils_end"
2628 "`ovm_sequencer_utils_end"
2629 ;; UVM Begin tokens
2630 "`uvm_component_utils_begin"
2631 "`uvm_component_param_utils_begin"
2632 "`uvm_field_utils_begin"
2633 "`uvm_object_utils_begin"
2634 "`uvm_object_param_utils_begin"
2635 "`uvm_sequence_utils_begin"
2636 "`uvm_sequencer_utils_begin"
2637 ;; UVM End tokens
2638 "`uvm_component_utils_end" ;; Typo in spec, it's not uvm_component_end
2639 "`uvm_field_utils_end"
2640 "`uvm_object_utils_end"
2641 "`uvm_sequence_utils_end"
2642 "`uvm_sequencer_utils_end"
2643 ;; VMM Begin tokens
2644 "`vmm_data_member_begin"
2645 "`vmm_env_member_begin"
2646 "`vmm_scenario_member_begin"
2647 "`vmm_subenv_member_begin"
2648 "`vmm_xactor_member_begin"
2649 ;; VMM End tokens
2650 "`vmm_data_member_end"
2651 "`vmm_env_member_end"
2652 "`vmm_scenario_member_end"
2653 "`vmm_subenv_member_end"
2654 "`vmm_xactor_member_end"
2655 ))))
2656
2657 (defconst verilog-defun-level-not-generate-re
2658 (eval-when-compile
2659 (verilog-regexp-words
2660 `( "module" "macromodule" "primitive" "class" "program"
2661 "interface" "package" "config"))))
2662
2663 (defconst verilog-defun-level-re
2664 (eval-when-compile
2665 (verilog-regexp-words
2666 (append
2667 `( "module" "macromodule" "primitive" "class" "program"
2668 "interface" "package" "config")
2669 `( "initial" "final" "always" "always_comb" "always_ff"
2670 "always_latch" "endtask" "endfunction" )))))
2671
2672 (defconst verilog-defun-level-generate-only-re
2673 (eval-when-compile
2674 (verilog-regexp-words
2675 `( "initial" "final" "always" "always_comb" "always_ff"
2676 "always_latch" "endtask" "endfunction" ))))
2677
2678 (defconst verilog-cpp-level-re
2679 (eval-when-compile
2680 (verilog-regexp-words
2681 `(
2682 "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
2683 ))))
2684 (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
2685 (defconst verilog-extended-case-re "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
2686 (defconst verilog-extended-complete-re
2687 (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<pure\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
2688 "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
2689 "\\|\\(\\(\\<import\\>\\s-+\\)?\\(\"DPI-C\"\\s-+\\)?\\(\\<pure\\>\\s-+\\)?\\(function\\>\\|task\\>\\)\\)"
2690 "\\|" verilog-extended-case-re ))
2691 (defconst verilog-basic-complete-re
2692 (eval-when-compile
2693 (verilog-regexp-words
2694 `(
2695 "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
2696 "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
2697 "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
2698 ))))
2699 (defconst verilog-complete-reg
2700 (concat
2701 verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
2702
2703 (defconst verilog-end-statement-re
2704 (concat "\\(" verilog-beg-block-re "\\)\\|\\("
2705 verilog-end-block-re "\\)"))
2706
2707 (defconst verilog-endcase-re
2708 (concat verilog-extended-case-re "\\|"
2709 "\\(endcase\\)\\|"
2710 verilog-defun-re
2711 ))
2712
2713 (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
2714 "String used to mark beginning of excluded text.")
2715 (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
2716 "String used to mark end of excluded text.")
2717 (defconst verilog-preprocessor-re
2718 (eval-when-compile
2719 (verilog-regexp-words
2720 `(
2721 "`define" "`include" "`ifdef" "`ifndef" "`if" "`endif" "`else"
2722 ))))
2723
2724 (defconst verilog-keywords
2725 '( "`case" "`default" "`define" "`else" "`endfor" "`endif"
2726 "`endprotect" "`endswitch" "`endwhile" "`for" "`format" "`if" "`ifdef"
2727 "`ifndef" "`include" "`let" "`protect" "`switch" "`timescale"
2728 "`time_scale" "`undef" "`while"
2729
2730 "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
2731 "assert" "assign" "assume" "automatic" "before" "begin" "bind"
2732 "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
2733 "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
2734 "config" "const" "constraint" "context" "continue" "cover"
2735 "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
2736 "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
2737 "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
2738 "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
2739 "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
2740 "endtask" "enum" "event" "expect" "export" "extends" "extern"
2741 "final" "first_match" "for" "force" "foreach" "forever" "fork"
2742 "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
2743 "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
2744 "include" "initial" "inout" "input" "inside" "instance" "int"
2745 "integer" "interface" "intersect" "join" "join_any" "join_none"
2746 "large" "liblist" "library" "local" "localparam" "logic"
2747 "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
2748 "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
2749 "notif0" "notif1" "null" "or" "output" "package" "packed"
2750 "parameter" "pmos" "posedge" "primitive" "priority" "program"
2751 "property" "protected" "pull0" "pull1" "pulldown" "pullup"
2752 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2753 "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
2754 "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
2755 "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
2756 "showcancelled" "signed" "small" "solve" "specify" "specparam"
2757 "static" "string" "strong0" "strong1" "struct" "super" "supply0"
2758 "supply1" "table" "tagged" "task" "this" "throughout" "time"
2759 "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
2760 "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
2761 "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
2762 "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
2763 "wire" "with" "within" "wor" "xnor" "xor"
2764 ;; 1800-2009
2765 "accept_on" "checker" "endchecker" "eventually" "global" "implies"
2766 "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
2767 "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
2768 "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
2769 )
2770 "List of Verilog keywords.")
2771
2772 (defconst verilog-comment-start-regexp "//\\|/\\*"
2773 "Dual comment value for `comment-start-regexp'.")
2774
2775 (defvar verilog-mode-syntax-table
2776 (let ((table (make-syntax-table)))
2777 ;; Populate the syntax TABLE.
2778 (modify-syntax-entry ?\\ "\\" table)
2779 (modify-syntax-entry ?+ "." table)
2780 (modify-syntax-entry ?- "." table)
2781 (modify-syntax-entry ?= "." table)
2782 (modify-syntax-entry ?% "." table)
2783 (modify-syntax-entry ?< "." table)
2784 (modify-syntax-entry ?> "." table)
2785 (modify-syntax-entry ?& "." table)
2786 (modify-syntax-entry ?| "." table)
2787 ;; FIXME: This goes against Emacs conventions. Use "_" syntax instead and
2788 ;; then use regexps with things like "\\_<...\\_>".
2789 (modify-syntax-entry ?` "w" table)
2790 (modify-syntax-entry ?_ "w" table)
2791 (modify-syntax-entry ?\' "." table)
2792
2793 ;; Set up TABLE to handle block and line style comments.
2794 (if (featurep 'xemacs)
2795 (progn
2796 ;; XEmacs (formerly Lucid) has the best implementation
2797 (modify-syntax-entry ?/ ". 1456" table)
2798 (modify-syntax-entry ?* ". 23" table)
2799 (modify-syntax-entry ?\n "> b" table))
2800 ;; Emacs does things differently, but we can work with it
2801 (modify-syntax-entry ?/ ". 124b" table)
2802 (modify-syntax-entry ?* ". 23" table)
2803 (modify-syntax-entry ?\n "> b" table))
2804 table)
2805 "Syntax table used in Verilog mode buffers.")
2806
2807 (defvar verilog-font-lock-keywords nil
2808 "Default highlighting for Verilog mode.")
2809
2810 (defvar verilog-font-lock-keywords-1 nil
2811 "Subdued level highlighting for Verilog mode.")
2812
2813 (defvar verilog-font-lock-keywords-2 nil
2814 "Medium level highlighting for Verilog mode.
2815 See also `verilog-font-lock-extra-types'.")
2816
2817 (defvar verilog-font-lock-keywords-3 nil
2818 "Gaudy level highlighting for Verilog mode.
2819 See also `verilog-font-lock-extra-types'.")
2820
2821 (defvar verilog-font-lock-translate-off-face
2822 'verilog-font-lock-translate-off-face
2823 "Font to use for translated off regions.")
2824 (defface verilog-font-lock-translate-off-face
2825 '((((class color)
2826 (background light))
2827 (:background "gray90" :italic t ))
2828 (((class color)
2829 (background dark))
2830 (:background "gray10" :italic t ))
2831 (((class grayscale) (background light))
2832 (:foreground "DimGray" :italic t))
2833 (((class grayscale) (background dark))
2834 (:foreground "LightGray" :italic t))
2835 (t (:italis t)))
2836 "Font lock mode face used to background highlight translate-off regions."
2837 :group 'font-lock-highlighting-faces)
2838
2839 (defvar verilog-font-lock-p1800-face
2840 'verilog-font-lock-p1800-face
2841 "Font to use for p1800 keywords.")
2842 (defface verilog-font-lock-p1800-face
2843 '((((class color)
2844 (background light))
2845 (:foreground "DarkOrange3" :bold t ))
2846 (((class color)
2847 (background dark))
2848 (:foreground "orange1" :bold t ))
2849 (t (:italic t)))
2850 "Font lock mode face used to highlight P1800 keywords."
2851 :group 'font-lock-highlighting-faces)
2852
2853 (defvar verilog-font-lock-ams-face
2854 'verilog-font-lock-ams-face
2855 "Font to use for Analog/Mixed Signal keywords.")
2856 (defface verilog-font-lock-ams-face
2857 '((((class color)
2858 (background light))
2859 (:foreground "Purple" :bold t ))
2860 (((class color)
2861 (background dark))
2862 (:foreground "orange1" :bold t ))
2863 (t (:italic t)))
2864 "Font lock mode face used to highlight AMS keywords."
2865 :group 'font-lock-highlighting-faces)
2866
2867 (defvar verilog-font-grouping-keywords-face
2868 'verilog-font-lock-grouping-keywords-face
2869 "Font to use for Verilog Grouping Keywords (such as begin..end).")
2870 (defface verilog-font-lock-grouping-keywords-face
2871 '((((class color)
2872 (background light))
2873 (:foreground "red4" :bold t ))
2874 (((class color)
2875 (background dark))
2876 (:foreground "red4" :bold t ))
2877 (t (:italic t)))
2878 "Font lock mode face used to highlight verilog grouping keywords."
2879 :group 'font-lock-highlighting-faces)
2880
2881 (let* ((verilog-type-font-keywords
2882 (eval-when-compile
2883 (verilog-regexp-opt
2884 '(
2885 "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
2886 "event" "genvar" "inout" "input" "integer" "localparam"
2887 "logic" "mailbox" "nand" "nmos" "not" "notif0" "notif1" "or"
2888 "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
2889 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
2890 "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
2891 "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
2892 "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
2893 "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
2894 ) nil )))
2895
2896 (verilog-pragma-keywords
2897 (eval-when-compile
2898 (verilog-regexp-opt
2899 '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
2900 ) nil )))
2901
2902 (verilog-1800-2005-keywords
2903 (eval-when-compile
2904 (verilog-regexp-opt
2905 '("alias" "assert" "assume" "automatic" "before" "bind"
2906 "bins" "binsof" "break" "byte" "cell" "chandle" "class"
2907 "clocking" "config" "const" "constraint" "context" "continue"
2908 "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
2909 "dist" "do" "edge" "endclass" "endclocking" "endconfig"
2910 "endgroup" "endprogram" "endproperty" "endsequence" "enum"
2911 "expect" "export" "extends" "extern" "first_match" "foreach"
2912 "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
2913 "illegal_bins" "import" "incdir" "include" "inside" "instance"
2914 "int" "intersect" "large" "liblist" "library" "local" "longint"
2915 "matches" "medium" "modport" "new" "noshowcancelled" "null"
2916 "packed" "program" "property" "protected" "pull0" "pull1"
2917 "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
2918 "randcase" "randsequence" "ref" "release" "return" "scalared"
2919 "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
2920 "specparam" "static" "string" "strong0" "strong1" "struct"
2921 "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
2922 "type" "union" "unsigned" "use" "var" "virtual" "void"
2923 "wait_order" "weak0" "weak1" "wildcard" "with" "within"
2924 ) nil )))
2925
2926 (verilog-1800-2009-keywords
2927 (eval-when-compile
2928 (verilog-regexp-opt
2929 '("accept_on" "checker" "endchecker" "eventually" "global"
2930 "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
2931 "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
2932 "sync_accept_on" "sync_reject_on" "unique0" "until"
2933 "until_with" "untyped" "weak" ) nil )))
2934
2935 (verilog-ams-keywords
2936 (eval-when-compile
2937 (verilog-regexp-opt
2938 '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
2939 "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
2940 "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
2941 "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
2942 "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
2943 "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
2944 "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
2945 "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
2946 "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
2947 "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
2948 "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
2949
2950 (verilog-font-keywords
2951 (eval-when-compile
2952 (verilog-regexp-opt
2953 '(
2954 "assign" "case" "casex" "casez" "randcase" "deassign"
2955 "default" "disable" "else" "endcase" "endfunction"
2956 "endgenerate" "endinterface" "endmodule" "endprimitive"
2957 "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
2958 "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
2959 "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
2960 "package" "endpackage" "always" "always_comb" "always_ff"
2961 "always_latch" "posedge" "primitive" "priority" "release"
2962 "repeat" "specify" "table" "task" "unique" "wait" "while"
2963 "class" "program" "endclass" "endprogram"
2964 ) nil )))
2965
2966 (verilog-font-grouping-keywords
2967 (eval-when-compile
2968 (verilog-regexp-opt
2969 '( "begin" "end" ) nil ))))
2970
2971 (setq verilog-font-lock-keywords
2972 (list
2973 ;; Fontify all builtin keywords
2974 (concat "\\<\\(" verilog-font-keywords "\\|"
2975 ;; And user/system tasks and functions
2976 "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
2977 "\\)\\>")
2978 ;; Fontify all types
2979 (if verilog-highlight-grouping-keywords
2980 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2981 'verilog-font-lock-ams-face)
2982 (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
2983 'font-lock-type-face))
2984 (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
2985 'font-lock-type-face)
2986 ;; Fontify IEEE-1800-2005 keywords appropriately
2987 (if verilog-highlight-p1800-keywords
2988 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2989 'verilog-font-lock-p1800-face)
2990 (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
2991 'font-lock-type-face))
2992 ;; Fontify IEEE-1800-2009 keywords appropriately
2993 (if verilog-highlight-p1800-keywords
2994 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2995 'verilog-font-lock-p1800-face)
2996 (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
2997 'font-lock-type-face))
2998 ;; Fontify Verilog-AMS keywords
2999 (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
3000 'verilog-font-lock-ams-face)))
3001
3002 (setq verilog-font-lock-keywords-1
3003 (append verilog-font-lock-keywords
3004 (list
3005 ;; Fontify module definitions
3006 (list
3007 "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
3008 '(1 font-lock-keyword-face)
3009 '(3 font-lock-function-name-face 'prepend))
3010 ;; Fontify function definitions
3011 (list
3012 (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
3013 '(1 font-lock-keyword-face)
3014 '(3 font-lock-constant-face prepend))
3015 '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
3016 (1 font-lock-keyword-face)
3017 (2 font-lock-constant-face append))
3018 '("\\<function\\>\\s-+\\(\\sw+\\)"
3019 1 'font-lock-constant-face append))))
3020
3021 (setq verilog-font-lock-keywords-2
3022 (append verilog-font-lock-keywords-1
3023 (list
3024 ;; Fontify pragmas
3025 (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
3026 ;; Fontify escaped names
3027 '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
3028 ;; Fontify macro definitions/ uses
3029 '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
3030 'font-lock-preprocessor-face
3031 'font-lock-type-face))
3032 ;; Fontify delays/numbers
3033 '("\\(@\\)\\|\\(#\\s-*\\(\\(\[0-9_.\]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
3034 0 font-lock-type-face append)
3035 ;; Fontify instantiation names
3036 '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
3037 )))
3038
3039 (setq verilog-font-lock-keywords-3
3040 (append verilog-font-lock-keywords-2
3041 (when verilog-highlight-translate-off
3042 (list
3043 ;; Fontify things in translate off regions
3044 '(verilog-match-translate-off
3045 (0 'verilog-font-lock-translate-off-face prepend))
3046 )))))
3047
3048 ;;
3049 ;; Buffer state preservation
3050
3051 (defmacro verilog-save-buffer-state (&rest body)
3052 "Execute BODY forms, saving state around insignificant change.
3053 Changes in text properties like `face' or `syntax-table' are
3054 considered insignificant. This macro allows text properties to
3055 be changed, even in a read-only buffer.
3056
3057 A change is considered significant if it affects the buffer text
3058 in any way that isn't completely restored again. Any
3059 user-visible changes to the buffer must not be within a
3060 `verilog-save-buffer-state'."
3061 ;; From c-save-buffer-state
3062 `(let* ((modified (buffer-modified-p))
3063 (buffer-undo-list t)
3064 (inhibit-read-only t)
3065 (inhibit-point-motion-hooks t)
3066 (verilog-no-change-functions t)
3067 before-change-functions
3068 after-change-functions
3069 deactivate-mark
3070 buffer-file-name ; Prevent primitives checking
3071 buffer-file-truename) ; for file modification
3072 (unwind-protect
3073 (progn ,@body)
3074 (and (not modified)
3075 (buffer-modified-p)
3076 (set-buffer-modified-p nil)))))
3077
3078 (defmacro verilog-save-no-change-functions (&rest body)
3079 "Execute BODY forms, disabling all change hooks in BODY.
3080 For insignificant changes, see instead `verilog-save-buffer-state'."
3081 `(let* ((inhibit-point-motion-hooks t)
3082 (verilog-no-change-functions t)
3083 before-change-functions
3084 after-change-functions)
3085 (progn ,@body)))
3086
3087 (defvar verilog-save-font-mod-hooked nil
3088 "Local variable when inside a `verilog-save-font-mods' block.")
3089 (make-variable-buffer-local 'verilog-save-font-mod-hooked)
3090
3091 (defmacro verilog-save-font-mods (&rest body)
3092 "Execute BODY forms, disabling text modifications to allow performing BODY.
3093 Includes temporary disabling of `font-lock' to restore the buffer
3094 to full text form for parsing. Additional actions may be specified with
3095 `verilog-before-save-font-hook' and `verilog-after-save-font-hook'."
3096 ;; Before version 20, match-string with font-lock returns a
3097 ;; vector that is not equal to the string. IE if on "input"
3098 ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
3099 `(let* ((hooked (unless verilog-save-font-mod-hooked
3100 (verilog-run-hooks 'verilog-before-save-font-hook)
3101 t))
3102 (verilog-save-font-mod-hooked t)
3103 (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
3104 (font-lock-mode 0)
3105 t)))
3106 (unwind-protect
3107 (progn ,@body)
3108 ;; Unwind forms
3109 (when fontlocked (font-lock-mode t))
3110 (when hooked (verilog-run-hooks 'verilog-after-save-font-hook)))))
3111
3112 ;;
3113 ;; Comment detection and caching
3114
3115 (defvar verilog-scan-cache-preserving nil
3116 "If set, the specified buffer's comment properties are static.
3117 Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
3118 and `verilog-scan'.")
3119
3120 (defvar verilog-scan-cache-tick nil
3121 "Modification tick at which `verilog-scan' was last completed.")
3122 (make-variable-buffer-local 'verilog-scan-cache-tick)
3123
3124 (defun verilog-scan-cache-flush ()
3125 "Flush the `verilog-scan' cache."
3126 (setq verilog-scan-cache-tick nil))
3127
3128 (defun verilog-scan-cache-ok-p ()
3129 "Return t iff the scan cache is up to date."
3130 (or (and verilog-scan-cache-preserving
3131 (eq verilog-scan-cache-preserving (current-buffer))
3132 verilog-scan-cache-tick)
3133 (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
3134
3135 (defmacro verilog-save-scan-cache (&rest body)
3136 "Execute the BODY forms, allowing scan cache preservation within BODY.
3137 This requires that insertions must use `verilog-insert'."
3138 ;; If the buffer is out of date, trash it, as we'll not check later the tick
3139 ;; Note this must work properly if there's multiple layers of calls
3140 ;; to verilog-save-scan-cache even with differing ticks.
3141 `(progn
3142 (unless (verilog-scan-cache-ok-p) ;; Must be before let
3143 (setq verilog-scan-cache-tick nil))
3144 (let* ((verilog-scan-cache-preserving (current-buffer)))
3145 (progn ,@body))))
3146
3147 (defun verilog-scan-region (beg end)
3148 "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
3149 This creates v-cmts properties where comments are in force."
3150 ;; Why properties and not overlays? Overlays have much slower non O(1)
3151 ;; lookup times.
3152 ;; This function is warm - called on every verilog-insert
3153 (save-excursion
3154 (save-match-data
3155 (verilog-save-buffer-state
3156 (let (pt)
3157 (goto-char beg)
3158 (while (< (point) end)
3159 (cond ((looking-at "//")
3160 (setq pt (point))
3161 (or (search-forward "\n" end t)
3162 (goto-char end))
3163 ;; "1+": The leading // or /* itself isn't considered as
3164 ;; being "inside" the comment, so that a (search-backward)
3165 ;; that lands at the start of the // won't mis-indicate
3166 ;; it's inside a comment. Also otherwise it would be
3167 ;; hard to find a commented out /*AS*/ vs one that isn't
3168 (put-text-property (1+ pt) (point) 'v-cmts t))
3169 ((looking-at "/\\*")
3170 (setq pt (point))
3171 (or (search-forward "*/" end t)
3172 ;; No error - let later code indicate it so we can
3173 ;; use inside functions on-the-fly
3174 ;;(error "%s: Unmatched /* */, at char %d"
3175 ;; (verilog-point-text) (point))
3176 (goto-char end))
3177 (put-text-property (1+ pt) (point) 'v-cmts t))
3178 ((looking-at "\"")
3179 (setq pt (point))
3180 (or (re-search-forward "[^\\]\"" end t) ;; don't forward-char first, since we look for a non backslash first
3181 ;; No error - let later code indicate it so we can
3182 (goto-char end))
3183 (put-text-property (1+ pt) (point) 'v-cmts t))
3184 (t
3185 (forward-char 1)
3186 (if (re-search-forward "[/\"]" end t)
3187 (backward-char 1)
3188 (goto-char end))))))))))
3189
3190 (defun verilog-scan ()
3191 "Parse the buffer, marking all comments with properties.
3192 Also assumes any text inserted since `verilog-scan-cache-tick'
3193 either is ok to parse as a non-comment, or `verilog-insert' was used."
3194 ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
3195 (unless (verilog-scan-cache-ok-p)
3196 (save-excursion
3197 (verilog-save-buffer-state
3198 (when verilog-debug
3199 (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
3200 verilog-scan-cache-preserving verilog-scan-cache-tick
3201 (buffer-chars-modified-tick)))
3202 (remove-text-properties (point-min) (point-max) '(v-cmts nil))
3203 (verilog-scan-region (point-min) (point-max))
3204 (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
3205 (when verilog-debug (message "Scanning... done"))))))
3206
3207 (defun verilog-scan-debug ()
3208 "For debugging, show with display face results of `verilog-scan'."
3209 (font-lock-mode 0)
3210 ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
3211 (save-excursion
3212 (goto-char (point-min))
3213 (remove-text-properties (point-min) (point-max) '(face nil))
3214 (while (not (eobp))
3215 (cond ((get-text-property (point) 'v-cmts)
3216 (put-text-property (point) (1+ (point)) `face 'underline)
3217 ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
3218 (forward-char 1))
3219 (t
3220 (goto-char (or (next-property-change (point)) (point-max))))))))
3221
3222 (defun verilog-scan-and-debug ()
3223 "For debugging, run `verilog-scan' and `verilog-scan-debug'."
3224 (let (verilog-scan-cache-preserving
3225 verilog-scan-cache-tick)
3226 (goto-char (point-min))
3227 (verilog-scan)
3228 (verilog-scan-debug)))
3229
3230 (defun verilog-inside-comment-or-string-p (&optional pos)
3231 "Check if optional point POS is inside a comment.
3232 This may require a slow pre-parse of the buffer with `verilog-scan'
3233 to establish comment properties on all text."
3234 ;; This function is very hot
3235 (verilog-scan)
3236 (if pos
3237 (and (>= pos (point-min))
3238 (get-text-property pos 'v-cmts))
3239 (get-text-property (point) 'v-cmts)))
3240
3241 (defun verilog-insert (&rest stuff)
3242 "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
3243 Any insert that includes a comment must have the entire comment
3244 inserted using a single call to `verilog-insert'."
3245 (let ((pt (point)))
3246 (while stuff
3247 (insert (car stuff))
3248 (setq stuff (cdr stuff)))
3249 (verilog-scan-region pt (point))))
3250
3251 ;; More searching
3252
3253 (defun verilog-declaration-end ()
3254 (search-forward ";"))
3255
3256 (defun verilog-point-text (&optional pointnum)
3257 "Return text describing where POINTNUM or current point is (for errors).
3258 Use filename, if current buffer being edited shorten to just buffer name."
3259 (concat (or (and (equal (window-buffer (selected-window)) (current-buffer))
3260 (buffer-name))
3261 buffer-file-name
3262 (buffer-name))
3263 ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
3264
3265 (defun electric-verilog-backward-sexp ()
3266 "Move backward over one balanced expression."
3267 (interactive)
3268 ;; before that see if we are in a comment
3269 (verilog-backward-sexp))
3270
3271 (defun electric-verilog-forward-sexp ()
3272 "Move forward over one balanced expression."
3273 (interactive)
3274 ;; before that see if we are in a comment
3275 (verilog-forward-sexp))
3276
3277 ;;;used by hs-minor-mode
3278 (defun verilog-forward-sexp-function (arg)
3279 (if (< arg 0)
3280 (verilog-backward-sexp)
3281 (verilog-forward-sexp)))
3282
3283
3284 (defun verilog-backward-sexp ()
3285 (let ((reg)
3286 (elsec 1)
3287 (found nil)
3288 (st (point)))
3289 (if (not (looking-at "\\<"))
3290 (forward-word -1))
3291 (cond
3292 ((verilog-skip-backward-comment-or-string))
3293 ((looking-at "\\<else\\>")
3294 (setq reg (concat
3295 verilog-end-block-re
3296 "\\|\\(\\<else\\>\\)"
3297 "\\|\\(\\<if\\>\\)"))
3298 (while (and (not found)
3299 (verilog-re-search-backward reg nil 'move))
3300 (cond
3301 ((match-end 1) ; matched verilog-end-block-re
3302 ; try to leap back to matching outward block by striding across
3303 ; indent level changing tokens then immediately
3304 ; previous line governs indentation.
3305 (verilog-leap-to-head))
3306 ((match-end 2) ; else, we're in deep
3307 (setq elsec (1+ elsec)))
3308 ((match-end 3) ; found it
3309 (setq elsec (1- elsec))
3310 (if (= 0 elsec)
3311 ;; Now previous line describes syntax
3312 (setq found 't))))))
3313 ((looking-at verilog-end-block-re)
3314 (verilog-leap-to-head))
3315 ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
3316 (cond
3317 ((match-end 1)
3318 (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
3319 ((match-end 2)
3320 (verilog-re-search-backward "\\<primitive\\>" nil 'move))
3321 ((match-end 3)
3322 (verilog-re-search-backward "\\<class\\>" nil 'move))
3323 ((match-end 4)
3324 (verilog-re-search-backward "\\<program\\>" nil 'move))
3325 ((match-end 5)
3326 (verilog-re-search-backward "\\<interface\\>" nil 'move))
3327 ((match-end 6)
3328 (verilog-re-search-backward "\\<package\\>" nil 'move))
3329 (t
3330 (goto-char st)
3331 (backward-sexp 1))))
3332 (t
3333 (goto-char st)
3334 (backward-sexp)))))
3335
3336 (defun verilog-forward-sexp ()
3337 (let ((reg)
3338 (md 2)
3339 (st (point))
3340 (nest 'yes))
3341 (if (not (looking-at "\\<"))
3342 (forward-word -1))
3343 (cond
3344 ((verilog-skip-forward-comment-or-string)
3345 (verilog-forward-syntactic-ws))
3346 ((looking-at verilog-beg-block-re-ordered)
3347 (cond
3348 ((match-end 1);
3349 ;; Search forward for matching end
3350 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
3351 ((match-end 2)
3352 ;; Search forward for matching endcase
3353 (setq reg "\\(\\<randcase\\>\\|\\(\\<unique\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
3354 (setq md 3) ;; ender is third item in regexp
3355 )
3356 ((match-end 4)
3357 ;; might be "disable fork" or "wait fork"
3358 (let
3359 (here)
3360 (if (or
3361 (looking-at verilog-disable-fork-re)
3362 (and (looking-at "fork")
3363 (progn
3364 (setq here (point)) ;; sometimes a fork is just a fork
3365 (forward-word -1)
3366 (looking-at verilog-disable-fork-re))))
3367 (progn ;; it is a disable fork; ignore it
3368 (goto-char (match-end 0))
3369 (forward-word 1)
3370 (setq reg nil))
3371 (progn ;; it is a nice simple fork
3372 (goto-char here) ;; return from looking for "disable fork"
3373 ;; Search forward for matching join
3374 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
3375 ((match-end 6)
3376 ;; Search forward for matching endclass
3377 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
3378
3379 ((match-end 7)
3380 ;; Search forward for matching endtable
3381 (setq reg "\\<endtable\\>" )
3382 (setq nest 'no))
3383 ((match-end 8)
3384 ;; Search forward for matching endspecify
3385 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
3386 ((match-end 9)
3387 ;; Search forward for matching endfunction
3388 (setq reg "\\<endfunction\\>" )
3389 (setq nest 'no))
3390 ((match-end 10)
3391 ;; Search forward for matching endfunction
3392 (setq reg "\\<endfunction\\>" )
3393 (setq nest 'no))
3394 ((match-end 14)
3395 ;; Search forward for matching endtask
3396 (setq reg "\\<endtask\\>" )
3397 (setq nest 'no))
3398 ((match-end 15)
3399 ;; Search forward for matching endtask
3400 (setq reg "\\<endtask\\>" )
3401 (setq nest 'no))
3402 ((match-end 19)
3403 ;; Search forward for matching endgenerate
3404 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
3405 ((match-end 20)
3406 ;; Search forward for matching endgroup
3407 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
3408 ((match-end 21)
3409 ;; Search forward for matching endproperty
3410 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
3411 ((match-end 25)
3412 ;; Search forward for matching endsequence
3413 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
3414 (setq md 3)) ; 3 to get to endsequence in the reg above
3415 ((match-end 27)
3416 ;; Search forward for matching endclocking
3417 (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
3418 (if (and reg
3419 (forward-word 1))
3420 (catch 'skip
3421 (if (eq nest 'yes)
3422 (let ((depth 1)
3423 here)
3424 (while (verilog-re-search-forward reg nil 'move)
3425 (cond
3426 ((match-end md) ; a closer in regular expression, so we are climbing out
3427 (setq depth (1- depth))
3428 (if (= 0 depth) ; we are out!
3429 (throw 'skip 1)))
3430 ((match-end 1) ; an opener in the r-e, so we are in deeper now
3431 (setq here (point)) ; remember where we started
3432 (goto-char (match-beginning 1))
3433 (cond
3434 ((if (or
3435 (looking-at verilog-disable-fork-re)
3436 (and (looking-at "fork")
3437 (progn
3438 (forward-word -1)
3439 (looking-at verilog-disable-fork-re))))
3440 (progn ;; it is a disable fork; another false alarm
3441 (goto-char (match-end 0)))
3442 (progn ;; it is a simple fork (or has nothing to do with fork)
3443 (goto-char here)
3444 (setq depth (1+ depth))))))))))
3445 (if (verilog-re-search-forward reg nil 'move)
3446 (throw 'skip 1))))))
3447
3448 ((looking-at (concat
3449 "\\(\\<\\(macro\\)?module\\>\\)\\|"
3450 "\\(\\<primitive\\>\\)\\|"
3451 "\\(\\<class\\>\\)\\|"
3452 "\\(\\<program\\>\\)\\|"
3453 "\\(\\<interface\\>\\)\\|"
3454 "\\(\\<package\\>\\)"))
3455 (cond
3456 ((match-end 1)
3457 (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
3458 ((match-end 2)
3459 (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
3460 ((match-end 3)
3461 (verilog-re-search-forward "\\<endclass\\>" nil 'move))
3462 ((match-end 4)
3463 (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
3464 ((match-end 5)
3465 (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
3466 ((match-end 6)
3467 (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
3468 (t
3469 (goto-char st)
3470 (if (= (following-char) ?\) )
3471 (forward-char 1)
3472 (forward-sexp 1)))))
3473 (t
3474 (goto-char st)
3475 (if (= (following-char) ?\) )
3476 (forward-char 1)
3477 (forward-sexp 1))))))
3478
3479 (defun verilog-declaration-beg ()
3480 (verilog-re-search-backward verilog-declaration-re (bobp) t))
3481
3482 ;;
3483 ;;
3484 ;; Mode
3485 ;;
3486 (defvar verilog-which-tool 1)
3487 ;;;###autoload
3488 (define-derived-mode verilog-mode prog-mode "Verilog"
3489 "Major mode for editing Verilog code.
3490 \\<verilog-mode-map>
3491 See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
3492 AUTOs can improve coding efficiency.
3493
3494 Use \\[verilog-faq] for a pointer to frequently asked questions.
3495
3496 NEWLINE, TAB indents for Verilog code.
3497 Delete converts tabs to spaces as it moves back.
3498
3499 Supports highlighting.
3500
3501 Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
3502 with no args, if that value is non-nil.
3503
3504 Variables controlling indentation/edit style:
3505
3506 variable `verilog-indent-level' (default 3)
3507 Indentation of Verilog statements with respect to containing block.
3508 `verilog-indent-level-module' (default 3)
3509 Absolute indentation of Module level Verilog statements.
3510 Set to 0 to get initial and always statements lined up
3511 on the left side of your screen.
3512 `verilog-indent-level-declaration' (default 3)
3513 Indentation of declarations with respect to containing block.
3514 Set to 0 to get them list right under containing block.
3515 `verilog-indent-level-behavioral' (default 3)
3516 Indentation of first begin in a task or function block
3517 Set to 0 to get such code to lined up underneath the task or
3518 function keyword.
3519 `verilog-indent-level-directive' (default 1)
3520 Indentation of `ifdef/`endif blocks.
3521 `verilog-cexp-indent' (default 1)
3522 Indentation of Verilog statements broken across lines i.e.:
3523 if (a)
3524 begin
3525 `verilog-case-indent' (default 2)
3526 Indentation for case statements.
3527 `verilog-auto-newline' (default nil)
3528 Non-nil means automatically newline after semicolons and the punctuation
3529 mark after an end.
3530 `verilog-auto-indent-on-newline' (default t)
3531 Non-nil means automatically indent line after newline.
3532 `verilog-tab-always-indent' (default t)
3533 Non-nil means TAB in Verilog mode should always reindent the current line,
3534 regardless of where in the line point is when the TAB command is used.
3535 `verilog-indent-begin-after-if' (default t)
3536 Non-nil means to indent begin statements following a preceding
3537 if, else, while, for and repeat statements, if any. Otherwise,
3538 the begin is lined up with the preceding token. If t, you get:
3539 if (a)
3540 begin // amount of indent based on `verilog-cexp-indent'
3541 otherwise you get:
3542 if (a)
3543 begin
3544 `verilog-auto-endcomments' (default t)
3545 Non-nil means a comment /* ... */ is set after the ends which ends
3546 cases, tasks, functions and modules.
3547 The type and name of the object will be set between the braces.
3548 `verilog-minimum-comment-distance' (default 10)
3549 Minimum distance (in lines) between begin and end required before a comment
3550 will be inserted. Setting this variable to zero results in every
3551 end acquiring a comment; the default avoids too many redundant
3552 comments in tight quarters.
3553 `verilog-auto-lineup' (default 'declarations)
3554 List of contexts where auto lineup of code should be done.
3555
3556 Variables controlling other actions:
3557
3558 `verilog-linter' (default surelint)
3559 Unix program to call to run the lint checker. This is the default
3560 command for \\[compile-command] and \\[verilog-auto-save-compile].
3561
3562 See \\[customize] for the complete list of variables.
3563
3564 AUTO expansion functions are, in part:
3565
3566 \\[verilog-auto] Expand AUTO statements.
3567 \\[verilog-delete-auto] Remove the AUTOs.
3568 \\[verilog-inject-auto] Insert AUTOs for the first time.
3569
3570 Some other functions are:
3571
3572 \\[verilog-complete-word] Complete word with appropriate possibilities.
3573 \\[verilog-mark-defun] Mark function.
3574 \\[verilog-beg-of-defun] Move to beginning of current function.
3575 \\[verilog-end-of-defun] Move to end of current function.
3576 \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
3577
3578 \\[verilog-comment-region] Put marked area in a comment.
3579 \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
3580 \\[verilog-insert-block] Insert begin ... end.
3581 \\[verilog-star-comment] Insert /* ... */.
3582
3583 \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
3584 \\[verilog-sk-begin] Insert a begin .. end block.
3585 \\[verilog-sk-case] Insert a case block, prompting for details.
3586 \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
3587 \\[verilog-sk-generate] Insert a generate .. endgenerate block.
3588 \\[verilog-sk-header] Insert a header block at the top of file.
3589 \\[verilog-sk-initial] Insert an initial begin .. end block.
3590 \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
3591 \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
3592 \\[verilog-sk-ovm-class] Insert an OVM Class block.
3593 \\[verilog-sk-uvm-class] Insert an UVM Class block.
3594 \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
3595 \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
3596 \\[verilog-sk-specify] Insert a specify .. endspecify block.
3597 \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
3598 \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
3599 \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
3600 \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
3601 \\[verilog-sk-if] Insert an if (..) begin .. end block.
3602 \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
3603 \\[verilog-sk-comment] Insert a comment block.
3604 \\[verilog-sk-assign] Insert an assign .. = ..; statement.
3605 \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
3606 \\[verilog-sk-input] Insert an input declaration, prompting for details.
3607 \\[verilog-sk-output] Insert an output declaration, prompting for details.
3608 \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
3609 \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
3610 \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
3611 \\[verilog-sk-reg] Insert a register declaration, prompting for details.
3612 \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
3613
3614 All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
3615 Key bindings specific to `verilog-mode-map' are:
3616
3617 \\{verilog-mode-map}"
3618 :abbrev-table verilog-mode-abbrev-table
3619 (set (make-local-variable 'beginning-of-defun-function)
3620 'verilog-beg-of-defun)
3621 (set (make-local-variable 'end-of-defun-function)
3622 'verilog-end-of-defun)
3623 (set-syntax-table verilog-mode-syntax-table)
3624 (set (make-local-variable 'indent-line-function)
3625 #'verilog-indent-line-relative)
3626 (setq comment-indent-function 'verilog-comment-indent)
3627 (set (make-local-variable 'parse-sexp-ignore-comments) nil)
3628 (set (make-local-variable 'comment-start) "// ")
3629 (set (make-local-variable 'comment-end) "")
3630 (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
3631 (set (make-local-variable 'comment-multi-line) nil)
3632 ;; Set up for compilation
3633 (setq verilog-which-tool 1)
3634 (setq verilog-tool 'verilog-linter)
3635 (verilog-set-compile-command)
3636 (when (boundp 'hack-local-variables-hook) ;; Also modify any file-local-variables
3637 (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
3638
3639 ;; Setting up menus
3640 (when (featurep 'xemacs)
3641 (easy-menu-add verilog-stmt-menu)
3642 (easy-menu-add verilog-menu)
3643 (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
3644
3645 ;; Stuff for GNU Emacs
3646 (set (make-local-variable 'font-lock-defaults)
3647 `((verilog-font-lock-keywords
3648 verilog-font-lock-keywords-1
3649 verilog-font-lock-keywords-2
3650 verilog-font-lock-keywords-3)
3651 nil nil nil
3652 ,(if (functionp 'syntax-ppss)
3653 ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
3654 ;; font-lock-beginning-of-syntax-function, so
3655 ;; font-lock-beginning-of-syntax-function, can't use
3656 ;; verilog-beg-of-defun.
3657 nil
3658 'verilog-beg-of-defun)))
3659 ;;------------------------------------------------------------
3660 ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
3661 ;; all buffer local:
3662 (unless noninteractive ;; Else can't see the result, and change hooks are slow
3663 (when (featurep 'xemacs)
3664 (make-local-hook 'font-lock-mode-hook)
3665 (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
3666 (make-local-hook 'after-change-functions))
3667 (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
3668 (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
3669 (add-hook 'after-change-functions 'verilog-highlight-region t t))
3670
3671 ;; Tell imenu how to handle Verilog.
3672 (set (make-local-variable 'imenu-generic-expression)
3673 verilog-imenu-generic-expression)
3674 ;; Tell which-func-modes that imenu knows about verilog
3675 (when (and (boundp 'which-func-modes) (listp which-func-modes))
3676 (add-to-list 'which-func-modes 'verilog-mode))
3677 ;; hideshow support
3678 (when (boundp 'hs-special-modes-alist)
3679 (unless (assq 'verilog-mode hs-special-modes-alist)
3680 (setq hs-special-modes-alist
3681 (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
3682 verilog-forward-sexp-function)
3683 hs-special-modes-alist))))
3684
3685 ;; Stuff for autos
3686 (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
3687 ;; verilog-mode-hook call added by define-derived-mode
3688 )
3689 \f
3690
3691 ;;
3692 ;; Electric functions
3693 ;;
3694 (defun electric-verilog-terminate-line (&optional arg)
3695 "Terminate line and indent next line.
3696 With optional ARG, remove existing end of line comments."
3697 (interactive)
3698 ;; before that see if we are in a comment
3699 (let ((state (save-excursion (verilog-syntax-ppss))))
3700 (cond
3701 ((nth 7 state) ; Inside // comment
3702 (if (eolp)
3703 (progn
3704 (delete-horizontal-space)
3705 (newline))
3706 (progn
3707 (newline)
3708 (insert "// ")
3709 (beginning-of-line)))
3710 (verilog-indent-line))
3711 ((nth 4 state) ; Inside any comment (hence /**/)
3712 (newline)
3713 (verilog-more-comment))
3714 ((eolp)
3715 ;; First, check if current line should be indented
3716 (if (save-excursion
3717 (delete-horizontal-space)
3718 (beginning-of-line)
3719 (skip-chars-forward " \t")
3720 (if (looking-at verilog-auto-end-comment-lines-re)
3721 (let ((indent-str (verilog-indent-line)))
3722 ;; Maybe we should set some endcomments
3723 (if verilog-auto-endcomments
3724 (verilog-set-auto-endcomments indent-str arg))
3725 (end-of-line)
3726 (delete-horizontal-space)
3727 (if arg
3728 ()
3729 (newline))
3730 nil)
3731 (progn
3732 (end-of-line)
3733 (delete-horizontal-space)
3734 't)))
3735 ;; see if we should line up assignments
3736 (progn
3737 (if (or (eq 'all verilog-auto-lineup)
3738 (eq 'assignments verilog-auto-lineup))
3739 (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
3740 (newline))
3741 (forward-line 1))
3742 ;; Indent next line
3743 (if verilog-auto-indent-on-newline
3744 (verilog-indent-line)))
3745 (t
3746 (newline)))))
3747
3748 (defun electric-verilog-terminate-and-indent ()
3749 "Insert a newline and indent for the next statement."
3750 (interactive)
3751 (electric-verilog-terminate-line 1))
3752
3753 (defun electric-verilog-semi ()
3754 "Insert `;' character and reindent the line."
3755 (interactive)
3756 (verilog-insert-last-command-event)
3757
3758 (if (or (verilog-in-comment-or-string-p)
3759 (verilog-in-escaped-name-p))
3760 ()
3761 (save-excursion
3762 (beginning-of-line)
3763 (verilog-forward-ws&directives)
3764 (verilog-indent-line))
3765 (if (and verilog-auto-newline
3766 (not (verilog-parenthesis-depth)))
3767 (electric-verilog-terminate-line))))
3768
3769 (defun electric-verilog-semi-with-comment ()
3770 "Insert `;' character, reindent the line and indent for comment."
3771 (interactive)
3772 (insert "\;")
3773 (save-excursion
3774 (beginning-of-line)
3775 (verilog-indent-line))
3776 (indent-for-comment))
3777
3778 (defun electric-verilog-colon ()
3779 "Insert `:' and do all indentations except line indent on this line."
3780 (interactive)
3781 (verilog-insert-last-command-event)
3782 ;; Do nothing if within string.
3783 (if (or
3784 (verilog-within-string)
3785 (not (verilog-in-case-region-p)))
3786 ()
3787 (save-excursion
3788 (let ((p (point))
3789 (lim (progn (verilog-beg-of-statement) (point))))
3790 (goto-char p)
3791 (verilog-backward-case-item lim)
3792 (verilog-indent-line)))
3793 ;; (let ((verilog-tab-always-indent nil))
3794 ;; (verilog-indent-line))
3795 ))
3796
3797 ;;(defun electric-verilog-equal ()
3798 ;; "Insert `=', and do indentation if within block."
3799 ;; (interactive)
3800 ;; (verilog-insert-last-command-event)
3801 ;; Could auto line up expressions, but not yet
3802 ;; (if (eq (car (verilog-calculate-indent)) 'block)
3803 ;; (let ((verilog-tab-always-indent nil))
3804 ;; (verilog-indent-command)))
3805 ;; )
3806
3807 (defun electric-verilog-tick ()
3808 "Insert back-tick, and indent to column 0 if this is a CPP directive."
3809 (interactive)
3810 (verilog-insert-last-command-event)
3811 (save-excursion
3812 (if (verilog-in-directive-p)
3813 (verilog-indent-line))))
3814
3815 (defun electric-verilog-tab ()
3816 "Function called when TAB is pressed in Verilog mode."
3817 (interactive)
3818 ;; If verilog-tab-always-indent, indent the beginning of the line.
3819 (cond
3820 ;; The region is active, indent it.
3821 ((and (region-active-p)
3822 (not (eq (region-beginning) (region-end))))
3823 (indent-region (region-beginning) (region-end) nil))
3824 ((or verilog-tab-always-indent
3825 (save-excursion
3826 (skip-chars-backward " \t")
3827 (bolp)))
3828 (let* ((oldpnt (point))
3829 (boi-point
3830 (save-excursion
3831 (beginning-of-line)
3832 (skip-chars-forward " \t")
3833 (verilog-indent-line)
3834 (back-to-indentation)
3835 (point))))
3836 (if (< (point) boi-point)
3837 (back-to-indentation)
3838 (cond ((not verilog-tab-to-comment))
3839 ((not (eolp))
3840 (end-of-line))
3841 (t
3842 (indent-for-comment)
3843 (when (and (eolp) (= oldpnt (point)))
3844 ; kill existing comment
3845 (beginning-of-line)
3846 (re-search-forward comment-start-skip oldpnt 'move)
3847 (goto-char (match-beginning 0))
3848 (skip-chars-backward " \t")
3849 (kill-region (point) oldpnt)))))))
3850 (t (progn (insert "\t")))))
3851
3852 \f
3853
3854 ;;
3855 ;; Interactive functions
3856 ;;
3857
3858 (defun verilog-indent-buffer ()
3859 "Indent-region the entire buffer as Verilog code.
3860 To call this from the command line, see \\[verilog-batch-indent]."
3861 (interactive)
3862 (verilog-mode)
3863 (indent-region (point-min) (point-max) nil))
3864
3865 (defun verilog-insert-block ()
3866 "Insert Verilog begin ... end; block in the code with right indentation."
3867 (interactive)
3868 (verilog-indent-line)
3869 (insert "begin")
3870 (electric-verilog-terminate-line)
3871 (save-excursion
3872 (electric-verilog-terminate-line)
3873 (insert "end")
3874 (beginning-of-line)
3875 (verilog-indent-line)))
3876
3877 (defun verilog-star-comment ()
3878 "Insert Verilog star comment at point."
3879 (interactive)
3880 (verilog-indent-line)
3881 (insert "/*")
3882 (save-excursion
3883 (newline)
3884 (insert " */"))
3885 (newline)
3886 (insert " * "))
3887
3888 (defun verilog-insert-1 (fmt max)
3889 "Use format string FMT to insert integers 0 to MAX - 1.
3890 Inserts one integer per line, at the current column. Stops early
3891 if it reaches the end of the buffer."
3892 (let ((col (current-column))
3893 (n 0))
3894 (save-excursion
3895 (while (< n max)
3896 (insert (format fmt n))
3897 (forward-line 1)
3898 ;; Note that this function does not bother to check for lines
3899 ;; shorter than col.
3900 (if (eobp)
3901 (setq n max)
3902 (setq n (1+ n))
3903 (move-to-column col))))))
3904
3905 (defun verilog-insert-indices (max)
3906 "Insert a set of indices into a rectangle.
3907 The upper left corner is defined by point. Indices begin with 0
3908 and extend to the MAX - 1. If no prefix arg is given, the user
3909 is prompted for a value. The indices are surrounded by square
3910 brackets \[]. For example, the following code with the point
3911 located after the first 'a' gives:
3912
3913 a = b a[ 0] = b
3914 a = b a[ 1] = b
3915 a = b a[ 2] = b
3916 a = b a[ 3] = b
3917 a = b ==> insert-indices ==> a[ 4] = b
3918 a = b a[ 5] = b
3919 a = b a[ 6] = b
3920 a = b a[ 7] = b
3921 a = b a[ 8] = b"
3922
3923 (interactive "NMAX: ")
3924 (verilog-insert-1 "[%3d]" max))
3925
3926 (defun verilog-generate-numbers (max)
3927 "Insert a set of generated numbers into a rectangle.
3928 The upper left corner is defined by point. The numbers are padded to three
3929 digits, starting with 000 and extending to (MAX - 1). If no prefix argument
3930 is supplied, then the user is prompted for the MAX number. Consider the
3931 following code fragment:
3932
3933 buf buf buf buf000
3934 buf buf buf buf001
3935 buf buf buf buf002
3936 buf buf buf buf003
3937 buf buf ==> generate-numbers ==> buf buf004
3938 buf buf buf buf005
3939 buf buf buf buf006
3940 buf buf buf buf007
3941 buf buf buf buf008"
3942
3943 (interactive "NMAX: ")
3944 (verilog-insert-1 "%3.3d" max))
3945
3946 (defun verilog-mark-defun ()
3947 "Mark the current Verilog function (or procedure).
3948 This puts the mark at the end, and point at the beginning."
3949 (interactive)
3950 (if (featurep 'xemacs)
3951 (progn
3952 (push-mark (point))
3953 (verilog-end-of-defun)
3954 (push-mark (point))
3955 (verilog-beg-of-defun)
3956 (if (fboundp 'zmacs-activate-region)
3957 (zmacs-activate-region)))
3958 (mark-defun)))
3959
3960 (defun verilog-comment-region (start end)
3961 ; checkdoc-params: (start end)
3962 "Put the region into a Verilog comment.
3963 The comments that are in this area are \"deformed\":
3964 `*)' becomes `!(*' and `}' becomes `!{'.
3965 These deformed comments are returned to normal if you use
3966 \\[verilog-uncomment-region] to undo the commenting.
3967
3968 The commented area starts with `verilog-exclude-str-start', and ends with
3969 `verilog-exclude-str-end'. But if you change these variables,
3970 \\[verilog-uncomment-region] won't recognize the comments."
3971 (interactive "r")
3972 (save-excursion
3973 ;; Insert start and endcomments
3974 (goto-char end)
3975 (if (and (save-excursion (skip-chars-forward " \t") (eolp))
3976 (not (save-excursion (skip-chars-backward " \t") (bolp))))
3977 (forward-line 1)
3978 (beginning-of-line))
3979 (insert verilog-exclude-str-end)
3980 (setq end (point))
3981 (newline)
3982 (goto-char start)
3983 (beginning-of-line)
3984 (insert verilog-exclude-str-start)
3985 (newline)
3986 ;; Replace end-comments within commented area
3987 (goto-char end)
3988 (save-excursion
3989 (while (re-search-backward "\\*/" start t)
3990 (replace-match "*-/" t t)))
3991 (save-excursion
3992 (let ((s+1 (1+ start)))
3993 (while (re-search-backward "/\\*" s+1 t)
3994 (replace-match "/-*" t t))))))
3995
3996 (defun verilog-uncomment-region ()
3997 "Uncomment a commented area; change deformed comments back to normal.
3998 This command does nothing if the pointer is not in a commented
3999 area. See also `verilog-comment-region'."
4000 (interactive)
4001 (save-excursion
4002 (let ((start (point))
4003 (end (point)))
4004 ;; Find the boundaries of the comment
4005 (save-excursion
4006 (setq start (progn (search-backward verilog-exclude-str-start nil t)
4007 (point)))
4008 (setq end (progn (search-forward verilog-exclude-str-end nil t)
4009 (point))))
4010 ;; Check if we're really inside a comment
4011 (if (or (equal start (point)) (<= end (point)))
4012 (message "Not standing within commented area.")
4013 (progn
4014 ;; Remove endcomment
4015 (goto-char end)
4016 (beginning-of-line)
4017 (let ((pos (point)))
4018 (end-of-line)
4019 (delete-region pos (1+ (point))))
4020 ;; Change comments back to normal
4021 (save-excursion
4022 (while (re-search-backward "\\*-/" start t)
4023 (replace-match "*/" t t)))
4024 (save-excursion
4025 (while (re-search-backward "/-\\*" start t)
4026 (replace-match "/*" t t)))
4027 ;; Remove start comment
4028 (goto-char start)
4029 (beginning-of-line)
4030 (let ((pos (point)))
4031 (end-of-line)
4032 (delete-region pos (1+ (point)))))))))
4033
4034 (defun verilog-beg-of-defun ()
4035 "Move backward to the beginning of the current function or procedure."
4036 (interactive)
4037 (verilog-re-search-backward verilog-defun-re nil 'move))
4038
4039 (defun verilog-beg-of-defun-quick ()
4040 "Move backward to the beginning of the current function or procedure.
4041 Uses `verilog-scan' cache."
4042 (interactive)
4043 (verilog-re-search-backward-quick verilog-defun-re nil 'move))
4044
4045 (defun verilog-end-of-defun ()
4046 "Move forward to the end of the current function or procedure."
4047 (interactive)
4048 (verilog-re-search-forward verilog-end-defun-re nil 'move))
4049
4050 (defun verilog-get-beg-of-defun (&optional warn)
4051 (save-excursion
4052 (cond ((verilog-re-search-forward-quick verilog-defun-re nil t)
4053 (point))
4054 (t
4055 (error "%s: Can't find module beginning" (verilog-point-text))
4056 (point-max)))))
4057 (defun verilog-get-end-of-defun (&optional warn)
4058 (save-excursion
4059 (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
4060 (point))
4061 (t
4062 (error "%s: Can't find endmodule" (verilog-point-text))
4063 (point-max)))))
4064
4065 (defun verilog-label-be (&optional arg)
4066 "Label matching begin ... end, fork ... join and case ... endcase statements.
4067 With ARG, first kill any existing labels."
4068 (interactive)
4069 (let ((cnt 0)
4070 (oldpos (point))
4071 (b (progn
4072 (verilog-beg-of-defun)
4073 (point-marker)))
4074 (e (progn
4075 (verilog-end-of-defun)
4076 (point-marker))))
4077 (goto-char (marker-position b))
4078 (if (> (- e b) 200)
4079 (message "Relabeling module..."))
4080 (while (and
4081 (> (marker-position e) (point))
4082 (verilog-re-search-forward
4083 (concat
4084 "\\<end\\(\\(function\\)\\|\\(task\\)\\|\\(module\\)\\|\\(primitive\\)\\|\\(interface\\)\\|\\(package\\)\\|\\(case\\)\\)?\\>"
4085 "\\|\\(`endif\\)\\|\\(`else\\)")
4086 nil 'move))
4087 (goto-char (match-beginning 0))
4088 (let ((indent-str (verilog-indent-line)))
4089 (verilog-set-auto-endcomments indent-str 't)
4090 (end-of-line)
4091 (delete-horizontal-space))
4092 (setq cnt (1+ cnt))
4093 (if (= 9 (% cnt 10))
4094 (message "%d..." cnt)))
4095 (goto-char oldpos)
4096 (if (or
4097 (> (- e b) 200)
4098 (> cnt 20))
4099 (message "%d lines auto commented" cnt))))
4100
4101 (defun verilog-beg-of-statement ()
4102 "Move backward to beginning of statement."
4103 (interactive)
4104 ;; Move back token by token until we see the end
4105 ;; of some earlier line.
4106 (let (h)
4107 (while
4108 ;; If the current point does not begin a new
4109 ;; statement, as in the character ahead of us is a ';', or SOF
4110 ;; or the string after us unambiguously starts a statement,
4111 ;; or the token before us unambiguously ends a statement,
4112 ;; then move back a token and test again.
4113 (not (or
4114 ;; stop if beginning of buffer
4115 (bolp)
4116 ;; stop if we find a ;
4117 (= (preceding-char) ?\;)
4118 ;; stop if we see a named coverpoint
4119 (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
4120 ;; keep going if we are in the middle of a word
4121 (not (or (looking-at "\\<") (forward-word -1)))
4122 ;; stop if we see an assertion (perhaps labeled)
4123 (and
4124 (looking-at "\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
4125 (progn
4126 (setq h (point))
4127 (save-excursion
4128 (verilog-backward-token)
4129 (if (looking-at verilog-label-re)
4130 (setq h (point))))
4131 (goto-char h)))
4132 ;; stop if we see an extended complete reg, perhaps a complete one
4133 (and
4134 (looking-at verilog-complete-reg)
4135 (let* ((p (point)))
4136 (while (and (looking-at verilog-extended-complete-re)
4137 (progn (setq p (point))
4138 (verilog-backward-token)
4139 (/= p (point)))))
4140 (goto-char p)))
4141 ;; stop if we see a complete reg (previous found extended ones)
4142 (looking-at verilog-basic-complete-re)
4143 ;; stop if previous token is an ender
4144 (save-excursion
4145 (verilog-backward-token)
4146 (or
4147 (looking-at verilog-end-block-re)
4148 (looking-at verilog-preprocessor-re))))) ;; end of test
4149 (verilog-backward-syntactic-ws)
4150 (verilog-backward-token))
4151 ;; Now point is where the previous line ended.
4152 (verilog-forward-syntactic-ws)))
4153
4154 (defun verilog-beg-of-statement-1 ()
4155 "Move backward to beginning of statement."
4156 (interactive)
4157 (if (verilog-in-comment-p)
4158 (verilog-backward-syntactic-ws))
4159 (let ((pt (point)))
4160 (catch 'done
4161 (while (not (looking-at verilog-complete-reg))
4162 (setq pt (point))
4163 (verilog-backward-syntactic-ws)
4164 (if (or (bolp)
4165 (= (preceding-char) ?\;)
4166 (save-excursion
4167 (verilog-backward-token)
4168 (looking-at verilog-ends-re)))
4169 (progn
4170 (goto-char pt)
4171 (throw 'done t))
4172 (verilog-backward-token))))
4173 (verilog-forward-syntactic-ws)))
4174 ;
4175 ; (while (and
4176 ; (not (looking-at verilog-complete-reg))
4177 ; (not (bolp))
4178 ; (not (= (preceding-char) ?\;)))
4179 ; (verilog-backward-token)
4180 ; (verilog-backward-syntactic-ws)
4181 ; (setq pt (point)))
4182 ; (goto-char pt)
4183 ; ;(verilog-forward-syntactic-ws)
4184
4185 (defun verilog-end-of-statement ()
4186 "Move forward to end of current statement."
4187 (interactive)
4188 (let ((nest 0) pos)
4189 (cond
4190 ((verilog-in-directive-p)
4191 (forward-line 1)
4192 (backward-char 1))
4193
4194 ((looking-at verilog-beg-block-re)
4195 (verilog-forward-sexp))
4196
4197 ((equal (char-after) ?\})
4198 (forward-char))
4199
4200 ;; Skip to end of statement
4201 ((condition-case nil
4202 (setq pos
4203 (catch 'found
4204 (while t
4205 (forward-sexp 1)
4206 (verilog-skip-forward-comment-or-string)
4207 (if (eolp)
4208 (forward-line 1))
4209 (cond ((looking-at "[ \t]*;")
4210 (skip-chars-forward "^;")
4211 (forward-char 1)
4212 (throw 'found (point)))
4213 ((save-excursion
4214 (forward-sexp -1)
4215 (looking-at verilog-beg-block-re))
4216 (goto-char (match-beginning 0))
4217 (throw 'found nil))
4218 ((looking-at "[ \t]*)")
4219 (throw 'found (point)))
4220 ((eobp)
4221 (throw 'found (point)))
4222 )))
4223
4224 )
4225 (error nil))
4226 (if (not pos)
4227 ;; Skip a whole block
4228 (catch 'found
4229 (while t
4230 (verilog-re-search-forward verilog-end-statement-re nil 'move)
4231 (setq nest (if (match-end 1)
4232 (1+ nest)
4233 (1- nest)))
4234 (cond ((eobp)
4235 (throw 'found (point)))
4236 ((= 0 nest)
4237 (throw 'found (verilog-end-of-statement))))))
4238 pos)))))
4239
4240 (defun verilog-in-case-region-p ()
4241 "Return true if in a case region.
4242 More specifically, point @ in the line foo : @ begin"
4243 (interactive)
4244 (save-excursion
4245 (if (and
4246 (progn (verilog-forward-syntactic-ws)
4247 (looking-at "\\<begin\\>"))
4248 (progn (verilog-backward-syntactic-ws)
4249 (= (preceding-char) ?\:)))
4250 (catch 'found
4251 (let ((nest 1))
4252 (while t
4253 (verilog-re-search-backward
4254 (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
4255 "\\(\\<endcase\\>\\)\\>")
4256 nil 'move)
4257 (cond
4258 ((match-end 3)
4259 (setq nest (1+ nest)))
4260 ((match-end 2)
4261 (if (= nest 1)
4262 (throw 'found 1))
4263 (setq nest (1- nest)))
4264 (t
4265 (throw 'found (= nest 0)))))))
4266 nil)))
4267
4268 (defun verilog-backward-up-list (arg)
4269 "Call `backward-up-list' ARG, ignoring comments."
4270 (let ((parse-sexp-ignore-comments t))
4271 (backward-up-list arg)))
4272
4273 (defun verilog-forward-sexp-cmt (arg)
4274 "Call `forward-sexp' ARG, inside comments."
4275 (let ((parse-sexp-ignore-comments nil))
4276 (forward-sexp arg)))
4277
4278 (defun verilog-forward-sexp-ign-cmt (arg)
4279 "Call `forward-sexp' ARG, ignoring comments."
4280 (let ((parse-sexp-ignore-comments t))
4281 (forward-sexp arg)))
4282
4283 (defun verilog-in-generate-region-p ()
4284 "Return true if in a generate region.
4285 More specifically, after a generate and before an endgenerate."
4286 (interactive)
4287 (let ((nest 1))
4288 (save-excursion
4289 (catch 'done
4290 (while (and
4291 (/= nest 0)
4292 (verilog-re-search-backward
4293 "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
4294 (cond
4295 ((match-end 1) ; module - we have crawled out
4296 (throw 'done 1))
4297 ((match-end 2) ; generate
4298 (setq nest (1- nest)))
4299 ((match-end 3) ; endgenerate
4300 (setq nest (1+ nest))))))))
4301 (= nest 0) )) ; return nest
4302
4303 (defun verilog-in-fork-region-p ()
4304 "Return true if between a fork and join."
4305 (interactive)
4306 (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
4307 (nest 1))
4308 (save-excursion
4309 (while (and
4310 (/= nest 0)
4311 (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
4312 (cond
4313 ((match-end 1) ; fork
4314 (setq nest (1- nest)))
4315 ((match-end 2) ; join
4316 (setq nest (1+ nest)))))))
4317 (= nest 0) )) ; return nest
4318
4319 (defun verilog-backward-case-item (lim)
4320 "Skip backward to nearest enclosing case item.
4321 Limit search to point LIM."
4322 (interactive)
4323 (let ((str 'nil)
4324 (lim1
4325 (progn
4326 (save-excursion
4327 (verilog-re-search-backward verilog-endcomment-reason-re
4328 lim 'move)
4329 (point)))))
4330 ;; Try to find the real :
4331 (if (save-excursion (search-backward ":" lim1 t))
4332 (let ((colon 0)
4333 b e )
4334 (while
4335 (and
4336 (< colon 1)
4337 (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
4338 lim1 'move))
4339 (cond
4340 ((match-end 1) ;; [
4341 (setq colon (1+ colon))
4342 (if (>= colon 0)
4343 (error "%s: unbalanced [" (verilog-point-text))))
4344 ((match-end 2) ;; ]
4345 (setq colon (1- colon)))
4346
4347 ((match-end 3) ;; :
4348 (setq colon (1+ colon)))))
4349 ;; Skip back to beginning of case item
4350 (skip-chars-backward "\t ")
4351 (verilog-skip-backward-comment-or-string)
4352 (setq e (point))
4353 (setq b
4354 (progn
4355 (if
4356 (verilog-re-search-backward
4357 "\\<\\(case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
4358 (progn
4359 (cond
4360 ((match-end 1)
4361 (goto-char (match-end 1))
4362 (verilog-forward-ws&directives)
4363 (if (looking-at "(")
4364 (progn
4365 (forward-sexp)
4366 (verilog-forward-ws&directives)))
4367 (point))
4368 (t
4369 (goto-char (match-end 0))
4370 (verilog-forward-ws&directives)
4371 (point))))
4372 (error "Malformed case item"))))
4373 (setq str (buffer-substring b e))
4374 (if
4375 (setq e
4376 (string-match
4377 "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4378 (setq str (concat (substring str 0 e) "...")))
4379 str)
4380 'nil)))
4381 \f
4382
4383 ;;
4384 ;; Other functions
4385 ;;
4386
4387 (defun verilog-kill-existing-comment ()
4388 "Kill auto comment on this line."
4389 (save-excursion
4390 (let* (
4391 (e (progn
4392 (end-of-line)
4393 (point)))
4394 (b (progn
4395 (beginning-of-line)
4396 (search-forward "//" e t))))
4397 (if b
4398 (delete-region (- b 2) e)))))
4399
4400 (defconst verilog-directive-nest-re
4401 (concat "\\(`else\\>\\)\\|"
4402 "\\(`endif\\>\\)\\|"
4403 "\\(`if\\>\\)\\|"
4404 "\\(`ifdef\\>\\)\\|"
4405 "\\(`ifndef\\>\\)\\|"
4406 "\\(`elsif\\>\\)"))
4407 (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
4408 "Add ending comment with given INDENT-STR.
4409 With KILL-EXISTING-COMMENT, remove what was there before.
4410 Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
4411 Insert `// case expr ' if this line ends a case block.
4412 Insert `// ifdef FOO ' if this line ends code conditional on FOO.
4413 Insert `// NAME ' if this line ends a function, task, module,
4414 primitive or interface named NAME."
4415 (save-excursion
4416 (cond
4417 (; Comment close preprocessor directives
4418 (and
4419 (looking-at "\\(`endif\\)\\|\\(`else\\)")
4420 (or kill-existing-comment
4421 (not (save-excursion
4422 (end-of-line)
4423 (search-backward "//" (point-at-bol) t)))))
4424 (let ((nest 1) b e
4425 m
4426 (else (if (match-end 2) "!" " ")))
4427 (end-of-line)
4428 (if kill-existing-comment
4429 (verilog-kill-existing-comment))
4430 (delete-horizontal-space)
4431 (save-excursion
4432 (backward-sexp 1)
4433 (while (and (/= nest 0)
4434 (verilog-re-search-backward verilog-directive-nest-re nil 'move))
4435 (cond
4436 ((match-end 1) ; `else
4437 (if (= nest 1)
4438 (setq else "!")))
4439 ((match-end 2) ; `endif
4440 (setq nest (1+ nest)))
4441 ((match-end 3) ; `if
4442 (setq nest (1- nest)))
4443 ((match-end 4) ; `ifdef
4444 (setq nest (1- nest)))
4445 ((match-end 5) ; `ifndef
4446 (setq nest (1- nest)))
4447 ((match-end 6) ; `elsif
4448 (if (= nest 1)
4449 (progn
4450 (setq else "!")
4451 (setq nest 0))))))
4452 (if (match-end 0)
4453 (setq
4454 m (buffer-substring
4455 (match-beginning 0)
4456 (match-end 0))
4457 b (progn
4458 (skip-chars-forward "^ \t")
4459 (verilog-forward-syntactic-ws)
4460 (point))
4461 e (progn
4462 (skip-chars-forward "a-zA-Z0-9_")
4463 (point)))))
4464 (if b
4465 (if (> (count-lines (point) b) verilog-minimum-comment-distance)
4466 (insert (concat " // " else m " " (buffer-substring b e))))
4467 (progn
4468 (insert " // unmatched `else, `elsif or `endif")
4469 (ding 't)))))
4470
4471 (; Comment close case/class/function/task/module and named block
4472 (and (looking-at "\\<end")
4473 (or kill-existing-comment
4474 (not (save-excursion
4475 (end-of-line)
4476 (search-backward "//" (point-at-bol) t)))))
4477 (let ((type (car indent-str)))
4478 (unless (eq type 'declaration)
4479 (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ;; ignore named ends
4480 (if (looking-at verilog-end-block-ordered-re)
4481 (cond
4482 (;- This is a case block; search back for the start of this case
4483 (match-end 1) ;; of verilog-end-block-ordered-re
4484
4485 (let ((err 't)
4486 (str "UNMATCHED!!"))
4487 (save-excursion
4488 (verilog-leap-to-head)
4489 (cond
4490 ((looking-at "\\<randcase\\>")
4491 (setq str "randcase")
4492 (setq err nil))
4493 ((looking-at "\\(\\(unique\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
4494 (goto-char (match-end 0))
4495 (setq str (concat (match-string 0) " " (verilog-get-expr)))
4496 (setq err nil))
4497 ))
4498 (end-of-line)
4499 (if kill-existing-comment
4500 (verilog-kill-existing-comment))
4501 (delete-horizontal-space)
4502 (insert (concat " // " str ))
4503 (if err (ding 't))))
4504
4505 (;- This is a begin..end block
4506 (match-end 2) ;; of verilog-end-block-ordered-re
4507 (let ((str " // UNMATCHED !!")
4508 (err 't)
4509 (here (point))
4510 there
4511 cntx)
4512 (save-excursion
4513 (verilog-leap-to-head)
4514 (setq there (point))
4515 (if (not (match-end 0))
4516 (progn
4517 (goto-char here)
4518 (end-of-line)
4519 (if kill-existing-comment
4520 (verilog-kill-existing-comment))
4521 (delete-horizontal-space)
4522 (insert str)
4523 (ding 't))
4524 (let ((lim
4525 (save-excursion (verilog-beg-of-defun) (point)))
4526 (here (point)))
4527 (cond
4528 (;-- handle named block differently
4529 (looking-at verilog-named-block-re)
4530 (search-forward ":")
4531 (setq there (point))
4532 (setq str (verilog-get-expr))
4533 (setq err nil)
4534 (setq str (concat " // block: " str )))
4535
4536 ((verilog-in-case-region-p) ;-- handle case item differently
4537 (goto-char here)
4538 (setq str (verilog-backward-case-item lim))
4539 (setq there (point))
4540 (setq err nil)
4541 (setq str (concat " // case: " str )))
4542
4543 (;- try to find "reason" for this begin
4544 (cond
4545 (;
4546 (eq here (progn
4547 ;; (verilog-backward-token)
4548 (verilog-beg-of-statement)
4549 (point)))
4550 (setq err nil)
4551 (setq str ""))
4552 ((looking-at verilog-endcomment-reason-re)
4553 (setq there (match-end 0))
4554 (setq cntx (concat (match-string 0) " "))
4555 (cond
4556 (;- begin
4557 (match-end 1)
4558 (setq err nil)
4559 (save-excursion
4560 (if (and (verilog-continued-line)
4561 (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
4562 (progn
4563 (goto-char (match-end 0))
4564 (setq there (point))
4565 (setq str
4566 (concat " // " (match-string 0) " " (verilog-get-expr))))
4567 (setq str ""))))
4568
4569 (;- else
4570 (match-end 2)
4571 (let ((nest 0)
4572 ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4573 (catch 'skip
4574 (while (verilog-re-search-backward reg nil 'move)
4575 (cond
4576 ((match-end 1) ; begin
4577 (setq nest (1- nest)))
4578 ((match-end 2) ; end
4579 (setq nest (1+ nest)))
4580 ((match-end 3)
4581 (if (= 0 nest)
4582 (progn
4583 (goto-char (match-end 0))
4584 (setq there (point))
4585 (setq err nil)
4586 (setq str (verilog-get-expr))
4587 (setq str (concat " // else: !if" str ))
4588 (throw 'skip 1))))
4589 ((match-end 4)
4590 (if (= 0 nest)
4591 (progn
4592 (goto-char (match-end 0))
4593 (setq there (point))
4594 (setq err nil)
4595 (setq str (verilog-get-expr))
4596 (setq str (concat " // else: !assert " str ))
4597 (throw 'skip 1)))))))))
4598 (;- end else
4599 (match-end 3)
4600 (goto-char there)
4601 (let ((nest 0)
4602 (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
4603 (catch 'skip
4604 (while (verilog-re-search-backward reg nil 'move)
4605 (cond
4606 ((match-end 1) ; begin
4607 (setq nest (1- nest)))
4608 ((match-end 2) ; end
4609 (setq nest (1+ nest)))
4610 ((match-end 3)
4611 (if (= 0 nest)
4612 (progn
4613 (goto-char (match-end 0))
4614 (setq there (point))
4615 (setq err nil)
4616 (setq str (verilog-get-expr))
4617 (setq str (concat " // else: !if" str ))
4618 (throw 'skip 1))))
4619 ((match-end 4)
4620 (if (= 0 nest)
4621 (progn
4622 (goto-char (match-end 0))
4623 (setq there (point))
4624 (setq err nil)
4625 (setq str (verilog-get-expr))
4626 (setq str (concat " // else: !assert " str ))
4627 (throw 'skip 1)))))))))
4628
4629 (; always_comb, always_ff, always_latch
4630 (or (match-end 4) (match-end 5) (match-end 6))
4631 (goto-char (match-end 0))
4632 (setq there (point))
4633 (setq err nil)
4634 (setq str (concat " // " cntx )))
4635
4636 (;- task/function/initial et cetera
4637 t
4638 (match-end 0)
4639 (goto-char (match-end 0))
4640 (setq there (point))
4641 (setq err nil)
4642 (setq str (concat " // " cntx (verilog-get-expr))))
4643
4644 (;-- otherwise...
4645 (setq str " // auto-endcomment confused "))))
4646
4647 ((and
4648 (verilog-in-case-region-p) ;-- handle case item differently
4649 (progn
4650 (setq there (point))
4651 (goto-char here)
4652 (setq str (verilog-backward-case-item lim))))
4653 (setq err nil)
4654 (setq str (concat " // case: " str )))
4655
4656 ((verilog-in-fork-region-p)
4657 (setq err nil)
4658 (setq str " // fork branch" ))
4659
4660 ((looking-at "\\<end\\>")
4661 ;; HERE
4662 (forward-word 1)
4663 (verilog-forward-syntactic-ws)
4664 (setq err nil)
4665 (setq str (verilog-get-expr))
4666 (setq str (concat " // " cntx str )))
4667
4668 ))))
4669 (goto-char here)
4670 (end-of-line)
4671 (if kill-existing-comment
4672 (verilog-kill-existing-comment))
4673 (delete-horizontal-space)
4674 (if (or err
4675 (> (count-lines here there) verilog-minimum-comment-distance))
4676 (insert str))
4677 (if err (ding 't))
4678 ))))
4679 (;- this is endclass, which can be nested
4680 (match-end 11) ;; of verilog-end-block-ordered-re
4681 ;;(goto-char there)
4682 (let ((nest 0)
4683 (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
4684 string)
4685 (save-excursion
4686 (catch 'skip
4687 (while (verilog-re-search-backward reg nil 'move)
4688 (cond
4689 ((match-end 3) ; endclass
4690 (ding 't)
4691 (setq string "unmatched endclass")
4692 (throw 'skip 1))
4693
4694 ((match-end 2) ; endclass
4695 (setq nest (1+ nest)))
4696
4697 ((match-end 1) ; class
4698 (setq nest (1- nest))
4699 (if (< nest 0)
4700 (progn
4701 (goto-char (match-end 0))
4702 (let (b e)
4703 (setq b (progn
4704 (skip-chars-forward "^ \t")
4705 (verilog-forward-ws&directives)
4706 (point))
4707 e (progn
4708 (skip-chars-forward "a-zA-Z0-9_")
4709 (point)))
4710 (setq string (buffer-substring b e)))
4711 (throw 'skip 1))))
4712 ))))
4713 (end-of-line)
4714 (insert (concat " // " string ))))
4715
4716 (;- this is end{function,generate,task,module,primitive,table,generate}
4717 ;- which can not be nested.
4718 t
4719 (let (string reg (name-re nil))
4720 (end-of-line)
4721 (if kill-existing-comment
4722 (save-match-data
4723 (verilog-kill-existing-comment)))
4724 (delete-horizontal-space)
4725 (backward-sexp)
4726 (cond
4727 ((match-end 5) ;; of verilog-end-block-ordered-re
4728 (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4729 (setq name-re "\\w+\\s-*("))
4730 ((match-end 6) ;; of verilog-end-block-ordered-re
4731 (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
4732 (setq name-re "\\w+\\s-*("))
4733 ((match-end 7) ;; of verilog-end-block-ordered-re
4734 (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
4735 ((match-end 8) ;; of verilog-end-block-ordered-re
4736 (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4737 ((match-end 9) ;; of verilog-end-block-ordered-re
4738 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
4739 ((match-end 10) ;; of verilog-end-block-ordered-re
4740 (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4741 ((match-end 11) ;; of verilog-end-block-ordered-re
4742 (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4743 ((match-end 12) ;; of verilog-end-block-ordered-re
4744 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4745 ((match-end 13) ;; of verilog-end-block-ordered-re
4746 (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4747 ((match-end 14) ;; of verilog-end-block-ordered-re
4748 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
4749 ((match-end 15) ;; of verilog-end-block-ordered-re
4750 (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
4751
4752 (t (error "Problem in verilog-set-auto-endcomments")))
4753 (let (b e)
4754 (save-excursion
4755 (verilog-re-search-backward reg nil 'move)
4756 (cond
4757 ((match-end 1)
4758 (setq b (progn
4759 (skip-chars-forward "^ \t")
4760 (verilog-forward-ws&directives)
4761 (if (looking-at "static\\|automatic")
4762 (progn
4763 (goto-char (match-end 0))
4764 (verilog-forward-ws&directives)))
4765 (if (and name-re (verilog-re-search-forward name-re nil 'move))
4766 (progn
4767 (goto-char (match-beginning 0))
4768 (verilog-forward-ws&directives)))
4769 (point))
4770 e (progn
4771 (skip-chars-forward "a-zA-Z0-9_")
4772 (point)))
4773 (setq string (buffer-substring b e)))
4774 (t
4775 (ding 't)
4776 (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
4777 (end-of-line)
4778 (insert (concat " // " string )))
4779 ))))))))))
4780
4781 (defun verilog-get-expr()
4782 "Grab expression at point, e.g., case ( a | b & (c ^d))."
4783 (let* ((b (progn
4784 (verilog-forward-syntactic-ws)
4785 (skip-chars-forward " \t")
4786 (point)))
4787 (e (let ((par 1))
4788 (cond
4789 ((looking-at "@")
4790 (forward-char 1)
4791 (verilog-forward-syntactic-ws)
4792 (if (looking-at "(")
4793 (progn
4794 (forward-char 1)
4795 (while (and (/= par 0)
4796 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4797 (cond
4798 ((match-end 1)
4799 (setq par (1+ par)))
4800 ((match-end 2)
4801 (setq par (1- par)))))))
4802 (point))
4803 ((looking-at "(")
4804 (forward-char 1)
4805 (while (and (/= par 0)
4806 (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
4807 (cond
4808 ((match-end 1)
4809 (setq par (1+ par)))
4810 ((match-end 2)
4811 (setq par (1- par)))))
4812 (point))
4813 ((looking-at "\\[")
4814 (forward-char 1)
4815 (while (and (/= par 0)
4816 (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
4817 (cond
4818 ((match-end 1)
4819 (setq par (1+ par)))
4820 ((match-end 2)
4821 (setq par (1- par)))))
4822 (verilog-forward-syntactic-ws)
4823 (skip-chars-forward "^ \t\n\f")
4824 (point))
4825 ((looking-at "/[/\\*]")
4826 b)
4827 ('t
4828 (skip-chars-forward "^: \t\n\f")
4829 (point)))))
4830 (str (buffer-substring b e)))
4831 (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
4832 (setq str (concat (substring str 0 e) "...")))
4833 str))
4834
4835 (defun verilog-expand-vector ()
4836 "Take a signal vector on the current line and expand it to multiple lines.
4837 Useful for creating tri's and other expanded fields."
4838 (interactive)
4839 (verilog-expand-vector-internal "[" "]"))
4840
4841 (defun verilog-expand-vector-internal (bra ket)
4842 "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
4843 (save-excursion
4844 (forward-line 0)
4845 (let ((signal-string (buffer-substring (point)
4846 (progn
4847 (end-of-line) (point)))))
4848 (if (string-match
4849 (concat "\\(.*\\)"
4850 (regexp-quote bra)
4851 "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
4852 (regexp-quote ket)
4853 "\\(.*\\)$") signal-string)
4854 (let* ((sig-head (match-string 1 signal-string))
4855 (vec-start (string-to-number (match-string 2 signal-string)))
4856 (vec-end (if (= (match-beginning 3) (match-end 3))
4857 vec-start
4858 (string-to-number
4859 (substring signal-string (1+ (match-beginning 3))
4860 (match-end 3)))))
4861 (vec-range
4862 (if (= (match-beginning 4) (match-end 4))
4863 1
4864 (string-to-number
4865 (substring signal-string (+ 2 (match-beginning 4))
4866 (match-end 4)))))
4867 (sig-tail (match-string 5 signal-string))
4868 vec)
4869 ;; Decode vectors
4870 (setq vec nil)
4871 (if (< vec-range 0)
4872 (let ((tmp vec-start))
4873 (setq vec-start vec-end
4874 vec-end tmp
4875 vec-range (- vec-range))))
4876 (if (< vec-end vec-start)
4877 (while (<= vec-end vec-start)
4878 (setq vec (append vec (list vec-start)))
4879 (setq vec-start (- vec-start vec-range)))
4880 (while (<= vec-start vec-end)
4881 (setq vec (append vec (list vec-start)))
4882 (setq vec-start (+ vec-start vec-range))))
4883 ;;
4884 ;; Delete current line
4885 (delete-region (point) (progn (forward-line 0) (point)))
4886 ;;
4887 ;; Expand vector
4888 (while vec
4889 (insert (concat sig-head bra
4890 (int-to-string (car vec)) ket sig-tail "\n"))
4891 (setq vec (cdr vec)))
4892 (delete-char -1)
4893 ;;
4894 )))))
4895
4896 (defun verilog-strip-comments ()
4897 "Strip all comments from the Verilog code."
4898 (interactive)
4899 (goto-char (point-min))
4900 (while (re-search-forward "//" nil t)
4901 (if (verilog-within-string)
4902 (re-search-forward "\"" nil t)
4903 (if (verilog-in-star-comment-p)
4904 (re-search-forward "\*/" nil t)
4905 (let ((bpt (- (point) 2)))
4906 (end-of-line)
4907 (delete-region bpt (point))))))
4908 ;;
4909 (goto-char (point-min))
4910 (while (re-search-forward "/\\*" nil t)
4911 (if (verilog-within-string)
4912 (re-search-forward "\"" nil t)
4913 (let ((bpt (- (point) 2)))
4914 (re-search-forward "\\*/")
4915 (delete-region bpt (point))))))
4916
4917 (defun verilog-one-line ()
4918 "Convert structural Verilog instances to occupy one line."
4919 (interactive)
4920 (goto-char (point-min))
4921 (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
4922 (replace-match "\\1 " nil nil)))
4923
4924 (defun verilog-linter-name ()
4925 "Return name of linter, either surelint or verilint."
4926 (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4927 compile-command))
4928 (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
4929 verilog-linter)))
4930 (cond ((equal compile-word1 "surelint") `surelint)
4931 ((equal compile-word1 "verilint") `verilint)
4932 ((equal lint-word1 "surelint") `surelint)
4933 ((equal lint-word1 "verilint") `verilint)
4934 (t `surelint)))) ;; back compatibility
4935
4936 (defun verilog-lint-off ()
4937 "Convert a Verilog linter warning line into a disable statement.
4938 For example:
4939 pci_bfm_null.v, line 46: Unused input: pci_rst_
4940 becomes a comment for the appropriate tool.
4941
4942 The first word of the `compile-command' or `verilog-linter'
4943 variables is used to determine which product is being used.
4944
4945 See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
4946 (interactive)
4947 (let ((linter (verilog-linter-name)))
4948 (cond ((equal linter `surelint)
4949 (verilog-surelint-off))
4950 ((equal linter `verilint)
4951 (verilog-verilint-off))
4952 (t (error "Linter name not set")))))
4953
4954 (defvar compilation-last-buffer)
4955 (defvar next-error-last-buffer)
4956
4957 (defun verilog-surelint-off ()
4958 "Convert a SureLint warning line into a disable statement.
4959 Run from Verilog source window; assumes there is a *compile* buffer
4960 with point set appropriately.
4961
4962 For example:
4963 WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
4964 becomes:
4965 // surefire lint_line_off UDDONX"
4966 (interactive)
4967 (let ((buff (if (boundp 'next-error-last-buffer)
4968 next-error-last-buffer
4969 compilation-last-buffer)))
4970 (when (buffer-live-p buff)
4971 (save-excursion
4972 (switch-to-buffer buff)
4973 (beginning-of-line)
4974 (when
4975 (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
4976 (let* ((code (match-string 2))
4977 (file (match-string 3))
4978 (line (match-string 4))
4979 (buffer (get-file-buffer file))
4980 dir filename)
4981 (unless buffer
4982 (progn
4983 (setq buffer
4984 (and (file-exists-p file)
4985 (find-file-noselect file)))
4986 (or buffer
4987 (let* ((pop-up-windows t))
4988 (let ((name (expand-file-name
4989 (read-file-name
4990 (format "Find this error in: (default %s) "
4991 file)
4992 dir file t))))
4993 (if (file-directory-p name)
4994 (setq name (expand-file-name filename name)))
4995 (setq buffer
4996 (and (file-exists-p name)
4997 (find-file-noselect name))))))))
4998 (switch-to-buffer buffer)
4999 (goto-char (point-min))
5000 (forward-line (- (string-to-number line)))
5001 (end-of-line)
5002 (catch 'already
5003 (cond
5004 ((verilog-in-slash-comment-p)
5005 (re-search-backward "//")
5006 (cond
5007 ((looking-at "// surefire lint_off_line ")
5008 (goto-char (match-end 0))
5009 (let ((lim (point-at-eol)))
5010 (if (re-search-forward code lim 'move)
5011 (throw 'already t)
5012 (insert (concat " " code)))))
5013 (t
5014 )))
5015 ((verilog-in-star-comment-p)
5016 (re-search-backward "/\*")
5017 (insert (format " // surefire lint_off_line %6s" code )))
5018 (t
5019 (insert (format " // surefire lint_off_line %6s" code ))
5020 )))))))))
5021
5022 (defun verilog-verilint-off ()
5023 "Convert a Verilint warning line into a disable statement.
5024
5025 For example:
5026 (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
5027 becomes:
5028 //Verilint 240 off // WARNING: Unused input"
5029 (interactive)
5030 (save-excursion
5031 (beginning-of-line)
5032 (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
5033 (replace-match (format
5034 ;; %3s makes numbers 1-999 line up nicely
5035 "\\1//Verilint %3s off // WARNING: \\3"
5036 (match-string 2)))
5037 (beginning-of-line)
5038 (verilog-indent-line))))
5039
5040 (defun verilog-auto-save-compile ()
5041 "Update automatics with \\[verilog-auto], save the buffer, and compile."
5042 (interactive)
5043 (verilog-auto) ; Always do it for safety
5044 (save-buffer)
5045 (compile compile-command))
5046
5047 (defun verilog-preprocess (&optional command filename)
5048 "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
5049 Takes optional COMMAND or defaults to `verilog-preprocessor', and
5050 FILENAME to find directory to run in, or defaults to `buffer-file-name`."
5051 (interactive
5052 (list
5053 (let ((default (verilog-expand-command verilog-preprocessor)))
5054 (set (make-local-variable `verilog-preprocessor)
5055 (read-from-minibuffer "Run Preprocessor (like this): "
5056 default nil nil
5057 'verilog-preprocess-history default)))))
5058 (unless command (setq command (verilog-expand-command verilog-preprocessor)))
5059 (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
5060 (dir (file-name-directory (or filename buffer-file-name)))
5061 (cmd (concat "cd " dir "; " command)))
5062 (with-output-to-temp-buffer "*Verilog-Preprocessed*"
5063 (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
5064 (insert (concat "// " cmd "\n"))
5065 (call-process shell-file-name nil t nil shell-command-switch cmd)
5066 (verilog-mode)
5067 ;; Without this force, it takes a few idle seconds
5068 ;; to get the color, which is very jarring
5069 (when fontlocked (font-lock-fontify-buffer))))))
5070 \f
5071
5072 ;;
5073 ;; Batch
5074 ;;
5075
5076 (defun verilog-warn (string &rest args)
5077 "Print a warning with `format' using STRING and optional ARGS."
5078 (apply 'message (concat "%%Warning: " string) args))
5079
5080 (defun verilog-warn-error (string &rest args)
5081 "Call `error' using STRING and optional ARGS.
5082 If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
5083 (if verilog-warn-fatal
5084 (apply 'error string args)
5085 (apply 'verilog-warn string args)))
5086
5087 (defmacro verilog-batch-error-wrapper (&rest body)
5088 "Execute BODY and add error prefix to any errors found.
5089 This lets programs calling batch mode to easily extract error messages."
5090 `(let ((verilog-warn-fatal nil))
5091 (condition-case err
5092 (progn ,@body)
5093 (error
5094 (error "%%Error: %s%s" (error-message-string err)
5095 (if (featurep 'xemacs) "\n" "")))))) ;; XEmacs forgets to add a newline
5096
5097 (defun verilog-batch-execute-func (funref &optional no-save)
5098 "Internal processing of a batch command.
5099 Runs FUNREF on all command arguments.
5100 Save the result unless optional NO-SAVE is t."
5101 (verilog-batch-error-wrapper
5102 ;; Setting global variables like that is *VERY NASTY* !!! --Stef
5103 ;; However, this function is called only when Emacs is being used as
5104 ;; a standalone language instead of as an editor, so we'll live.
5105 ;;
5106 ;; General globals needed
5107 (setq make-backup-files nil)
5108 (setq-default make-backup-files nil)
5109 (setq enable-local-variables t)
5110 (setq enable-local-eval t)
5111 ;; Make sure any sub-files we read get proper mode
5112 (setq-default major-mode 'verilog-mode)
5113 ;; Ditto files already read in
5114 (mapc (lambda (buf)
5115 (when (buffer-file-name buf)
5116 (with-current-buffer buf
5117 (verilog-mode))))
5118 (buffer-list))
5119 ;; Process the files
5120 (mapcar (lambda (buf)
5121 (when (buffer-file-name buf)
5122 (save-excursion
5123 (if (not (file-exists-p (buffer-file-name buf)))
5124 (error
5125 (concat "File not found: " (buffer-file-name buf))))
5126 (message (concat "Processing " (buffer-file-name buf)))
5127 (set-buffer buf)
5128 (funcall funref)
5129 (unless no-save (save-buffer)))))
5130 (buffer-list))))
5131
5132 (defun verilog-batch-auto ()
5133 "For use with --batch, perform automatic expansions as a stand-alone tool.
5134 This sets up the appropriate Verilog mode environment, updates automatics
5135 with \\[verilog-auto] on all command-line files, and saves the buffers.
5136 For proper results, multiple filenames need to be passed on the command
5137 line in bottom-up order."
5138 (unless noninteractive
5139 (error "Use verilog-batch-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5140 (verilog-batch-execute-func `verilog-auto))
5141
5142 (defun verilog-batch-delete-auto ()
5143 "For use with --batch, perform automatic deletion as a stand-alone tool.
5144 This sets up the appropriate Verilog mode environment, deletes automatics
5145 with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
5146 (unless noninteractive
5147 (error "Use verilog-batch-delete-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5148 (verilog-batch-execute-func `verilog-delete-auto))
5149
5150 (defun verilog-batch-delete-trailing-whitespace ()
5151 "For use with --batch, perform whitespace deletion as a stand-alone tool.
5152 This sets up the appropriate Verilog mode environment, removes
5153 whitespace with \\[verilog-delete-trailing-whitespace] on all
5154 command-line files, and saves the buffers."
5155 (unless noninteractive
5156 (error "Use verilog-batch-delete-trailing-whitespace only with --batch")) ;; Otherwise we'd mess up buffer modes
5157 (verilog-batch-execute-func `verilog-delete-trailing-whitespace))
5158
5159 (defun verilog-batch-diff-auto ()
5160 "For use with --batch, perform automatic differences as a stand-alone tool.
5161 This sets up the appropriate Verilog mode environment, expand automatics
5162 with \\[verilog-diff-auto] on all command-line files, and reports an error
5163 if any differences are observed. This is appropriate for adding to regressions
5164 to insure automatics are always properly maintained."
5165 (unless noninteractive
5166 (error "Use verilog-batch-diff-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5167 (verilog-batch-execute-func `verilog-diff-auto t))
5168
5169 (defun verilog-batch-inject-auto ()
5170 "For use with --batch, perform automatic injection as a stand-alone tool.
5171 This sets up the appropriate Verilog mode environment, injects new automatics
5172 with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
5173 For proper results, multiple filenames need to be passed on the command
5174 line in bottom-up order."
5175 (unless noninteractive
5176 (error "Use verilog-batch-inject-auto only with --batch")) ;; Otherwise we'd mess up buffer modes
5177 (verilog-batch-execute-func `verilog-inject-auto))
5178
5179 (defun verilog-batch-indent ()
5180 "For use with --batch, reindent an entire file as a stand-alone tool.
5181 This sets up the appropriate Verilog mode environment, calls
5182 \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
5183 (unless noninteractive
5184 (error "Use verilog-batch-indent only with --batch")) ;; Otherwise we'd mess up buffer modes
5185 (verilog-batch-execute-func `verilog-indent-buffer))
5186 \f
5187
5188 ;;
5189 ;; Indentation
5190 ;;
5191 (defconst verilog-indent-alist
5192 '((block . (+ ind verilog-indent-level))
5193 (case . (+ ind verilog-case-indent))
5194 (cparenexp . (+ ind verilog-indent-level))
5195 (cexp . (+ ind verilog-cexp-indent))
5196 (defun . verilog-indent-level-module)
5197 (declaration . verilog-indent-level-declaration)
5198 (directive . (verilog-calculate-indent-directive))
5199 (tf . verilog-indent-level)
5200 (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
5201 (statement . ind)
5202 (cpp . 0)
5203 (comment . (verilog-comment-indent))
5204 (unknown . 3)
5205 (string . 0)))
5206
5207 (defun verilog-continued-line-1 (lim)
5208 "Return true if this is a continued line.
5209 Set point to where line starts. Limit search to point LIM."
5210 (let ((continued 't))
5211 (if (eq 0 (forward-line -1))
5212 (progn
5213 (end-of-line)
5214 (verilog-backward-ws&directives lim)
5215 (if (bobp)
5216 (setq continued nil)
5217 (setq continued (verilog-backward-token))))
5218 (setq continued nil))
5219 continued))
5220
5221 (defun verilog-calculate-indent ()
5222 "Calculate the indent of the current Verilog line.
5223 Examine previous lines. Once a line is found that is definitive as to the
5224 type of the current line, return that lines' indent level and its type.
5225 Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
5226 (save-excursion
5227 (let* ((starting_position (point))
5228 (par 0)
5229 (begin (looking-at "[ \t]*begin\\>"))
5230 (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
5231 (type (catch 'nesting
5232 ;; Keep working backwards until we can figure out
5233 ;; what type of statement this is.
5234 ;; Basically we need to figure out
5235 ;; 1) if this is a continuation of the previous line;
5236 ;; 2) are we in a block scope (begin..end)
5237
5238 ;; if we are in a comment, done.
5239 (if (verilog-in-star-comment-p)
5240 (throw 'nesting 'comment))
5241
5242 ;; if we have a directive, done.
5243 (if (save-excursion (beginning-of-line)
5244 (and (looking-at verilog-directive-re-1)
5245 (not (or (looking-at "[ \t]*`[ou]vm_")
5246 (looking-at "[ \t]*`vmm_")))))
5247 (throw 'nesting 'directive))
5248 ;; indent structs as if there were module level
5249 (if (verilog-in-struct-p)
5250 (throw 'nesting 'block))
5251
5252 ;; if we are in a parenthesized list, and the user likes to indent these, return.
5253 ;; unless we are in the newfangled coverpoint or constraint blocks
5254 (if (and
5255 verilog-indent-lists
5256 (verilog-in-paren)
5257 (not (verilog-in-coverage-p))
5258 )
5259 (progn (setq par 1)
5260 (throw 'nesting 'block)))
5261
5262 ;; See if we are continuing a previous line
5263 (while t
5264 ;; trap out if we crawl off the top of the buffer
5265 (if (bobp) (throw 'nesting 'cpp))
5266
5267 (if (verilog-continued-line-1 lim)
5268 (let ((sp (point)))
5269 (if (and
5270 (not (looking-at verilog-complete-reg))
5271 (verilog-continued-line-1 lim))
5272 (progn (goto-char sp)
5273 (throw 'nesting 'cexp))
5274
5275 (goto-char sp))
5276
5277 (if (and begin
5278 (not verilog-indent-begin-after-if)
5279 (looking-at verilog-no-indent-begin-re))
5280 (progn
5281 (beginning-of-line)
5282 (skip-chars-forward " \t")
5283 (throw 'nesting 'statement))
5284 (progn
5285 (throw 'nesting 'cexp))))
5286 ;; not a continued line
5287 (goto-char starting_position))
5288
5289 (if (looking-at "\\<else\\>")
5290 ;; search back for governing if, striding across begin..end pairs
5291 ;; appropriately
5292 (let ((elsec 1))
5293 (while (verilog-re-search-backward verilog-ends-re nil 'move)
5294 (cond
5295 ((match-end 1) ; else, we're in deep
5296 (setq elsec (1+ elsec)))
5297 ((match-end 2) ; if
5298 (setq elsec (1- elsec))
5299 (if (= 0 elsec)
5300 (if verilog-align-ifelse
5301 (throw 'nesting 'statement)
5302 (progn ;; back up to first word on this line
5303 (beginning-of-line)
5304 (verilog-forward-syntactic-ws)
5305 (throw 'nesting 'statement)))))
5306 ((match-end 3) ; assert block
5307 (setq elsec (1- elsec))
5308 (verilog-beg-of-statement) ;; doesn't get to beginning
5309 (if (looking-at verilog-property-re)
5310 (throw 'nesting 'statement) ; We don't need an endproperty for these
5311 (throw 'nesting 'block) ;We still need an endproperty
5312 ))
5313 (t ; endblock
5314 ; try to leap back to matching outward block by striding across
5315 ; indent level changing tokens then immediately
5316 ; previous line governs indentation.
5317 (let (( reg) (nest 1))
5318 ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
5319 (cond
5320 ((match-end 4) ; end
5321 ;; Search back for matching begin
5322 (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
5323 ((match-end 5) ; endcase
5324 ;; Search back for matching case
5325 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5326 ((match-end 6) ; endfunction
5327 ;; Search back for matching function
5328 (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5329 ((match-end 7) ; endtask
5330 ;; Search back for matching task
5331 (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
5332 ((match-end 8) ; endspecify
5333 ;; Search back for matching specify
5334 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5335 ((match-end 9) ; endtable
5336 ;; Search back for matching table
5337 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5338 ((match-end 10) ; endgenerate
5339 ;; Search back for matching generate
5340 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5341 ((match-end 11) ; joins
5342 ;; Search back for matching fork
5343 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
5344 ((match-end 12) ; class
5345 ;; Search back for matching class
5346 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5347 ((match-end 13) ; covergroup
5348 ;; Search back for matching covergroup
5349 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
5350 (catch 'skip
5351 (while (verilog-re-search-backward reg nil 'move)
5352 (cond
5353 ((match-end 1) ; begin
5354 (setq nest (1- nest))
5355 (if (= 0 nest)
5356 (throw 'skip 1)))
5357 ((match-end 2) ; end
5358 (setq nest (1+ nest)))))
5359 )))))))
5360 (throw 'nesting (verilog-calc-1)))
5361 );; catch nesting
5362 );; type
5363 )
5364 ;; Return type of block and indent level.
5365 (if (not type)
5366 (setq type 'cpp))
5367 (if (> par 0) ; Unclosed Parenthesis
5368 (list 'cparenexp par)
5369 (cond
5370 ((eq type 'case)
5371 (list type (verilog-case-indent-level)))
5372 ((eq type 'statement)
5373 (list type (current-column)))
5374 ((eq type 'defun)
5375 (list type 0))
5376 (t
5377 (list type (verilog-current-indent-level))))))))
5378
5379 (defun verilog-wai ()
5380 "Show matching nesting block for debugging."
5381 (interactive)
5382 (save-excursion
5383 (let* ((type (verilog-calc-1))
5384 depth)
5385 ;; Return type of block and indent level.
5386 (if (not type)
5387 (setq type 'cpp))
5388 (if (and
5389 verilog-indent-lists
5390 (not(or (verilog-in-coverage-p)
5391 (verilog-in-struct-p)))
5392 (verilog-in-paren))
5393 (setq depth 1)
5394 (cond
5395 ((eq type 'case)
5396 (setq depth (verilog-case-indent-level)))
5397 ((eq type 'statement)
5398 (setq depth (current-column)))
5399 ((eq type 'defun)
5400 (setq depth 0))
5401 (t
5402 (setq depth (verilog-current-indent-level)))))
5403 (message "You are at nesting %s depth %d" type depth))))
5404 (defun verilog-calc-1 ()
5405 (catch 'nesting
5406 (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)")))
5407 (while (verilog-re-search-backward re nil 'move)
5408 (catch 'continue
5409 (cond
5410 ((equal (char-after) ?\{)
5411 (if (verilog-at-constraint-p)
5412 (throw 'nesting 'block)))
5413
5414 ((equal (char-after) ?\})
5415 (let ((there (verilog-at-close-constraint-p)))
5416 (if there ;; we are at the } that closes a constraint. Find the { that opens it
5417 (progn
5418 (forward-char 1)
5419 (backward-list 1)
5420 (verilog-beg-of-statement)))))
5421
5422 ((looking-at verilog-beg-block-re-ordered)
5423 (cond
5424 ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
5425 (let ((here (point)))
5426 (verilog-beg-of-statement)
5427 (if (looking-at verilog-extended-case-re)
5428 (throw 'nesting 'case)
5429 (goto-char here)))
5430 (throw 'nesting 'case))
5431
5432 ((match-end 4) ; *sigh* could be "disable fork"
5433 (let ((here (point)))
5434 (verilog-beg-of-statement)
5435 (if (looking-at verilog-disable-fork-re)
5436 t ; this is a normal statement
5437 (progn ; or is fork, starts a new block
5438 (goto-char here)
5439 (throw 'nesting 'block)))))
5440
5441 ((match-end 27) ; *sigh* might be a clocking declaration
5442 (let ((here (point)))
5443 (if (verilog-in-paren)
5444 t ; this is a normal statement
5445 (progn ; or is fork, starts a new block
5446 (goto-char here)
5447 (throw 'nesting 'block)))))
5448
5449 ;; need to consider typedef struct here...
5450 ((looking-at "\\<class\\|struct\\|function\\|task\\>")
5451 ; *sigh* These words have an optional prefix:
5452 ; extern {virtual|protected}? function a();
5453 ; typedef class foo;
5454 ; and we don't want to confuse this with
5455 ; function a();
5456 ; property
5457 ; ...
5458 ; endfunction
5459 (verilog-beg-of-statement)
5460 (if (looking-at verilog-beg-block-re-ordered)
5461 (throw 'nesting 'block)
5462 (throw 'nesting 'defun)))
5463
5464 ;;
5465 ((looking-at "\\<property\\>")
5466 ; *sigh*
5467 ; {assert|assume|cover} property (); are complete
5468 ; and could also be labeled: - foo: assert property
5469 ; but
5470 ; property ID () ... needs end_property
5471 (verilog-beg-of-statement)
5472 (if (looking-at verilog-property-re)
5473 (throw 'continue 'statement) ; We don't need an endproperty for these
5474 (throw 'nesting 'block) ;We still need an endproperty
5475 ))
5476
5477 (t (throw 'nesting 'block))))
5478
5479 ((looking-at verilog-end-block-re)
5480 (verilog-leap-to-head)
5481 (if (verilog-in-case-region-p)
5482 (progn
5483 (verilog-leap-to-case-head)
5484 (if (looking-at verilog-extended-case-re)
5485 (throw 'nesting 'case)))))
5486
5487 ((looking-at verilog-defun-level-re)
5488 (if (looking-at verilog-defun-level-generate-only-re)
5489 (if (verilog-in-generate-region-p)
5490 (throw 'continue 'foo) ; always block in a generate - keep looking
5491 (throw 'nesting 'defun))
5492 (throw 'nesting 'defun)))
5493
5494 ((looking-at verilog-cpp-level-re)
5495 (throw 'nesting 'cpp))
5496
5497 ((bobp)
5498 (throw 'nesting 'cpp)))))
5499
5500 (throw 'nesting 'cpp))))
5501
5502 (defun verilog-calculate-indent-directive ()
5503 "Return indentation level for directive.
5504 For speed, the searcher looks at the last directive, not the indent
5505 of the appropriate enclosing block."
5506 (let ((base -1) ;; Indent of the line that determines our indentation
5507 (ind 0)) ;; Relative offset caused by other directives (like `endif on same line as `else)
5508 ;; Start at current location, scan back for another directive
5509
5510 (save-excursion
5511 (beginning-of-line)
5512 (while (and (< base 0)
5513 (verilog-re-search-backward verilog-directive-re nil t))
5514 (cond ((save-excursion (skip-chars-backward " \t") (bolp))
5515 (setq base (current-indentation))))
5516 (cond ((and (looking-at verilog-directive-end) (< base 0)) ;; Only matters when not at BOL
5517 (setq ind (- ind verilog-indent-level-directive)))
5518 ((and (looking-at verilog-directive-middle) (>= base 0)) ;; Only matters when at BOL
5519 (setq ind (+ ind verilog-indent-level-directive)))
5520 ((looking-at verilog-directive-begin)
5521 (setq ind (+ ind verilog-indent-level-directive)))))
5522 ;; Adjust indent to starting indent of critical line
5523 (setq ind (max 0 (+ ind base))))
5524
5525 (save-excursion
5526 (beginning-of-line)
5527 (skip-chars-forward " \t")
5528 (cond ((or (looking-at verilog-directive-middle)
5529 (looking-at verilog-directive-end))
5530 (setq ind (max 0 (- ind verilog-indent-level-directive))))))
5531 ind))
5532
5533 (defun verilog-leap-to-case-head ()
5534 (let ((nest 1))
5535 (while (/= 0 nest)
5536 (verilog-re-search-backward
5537 (concat
5538 "\\(\\<randcase\\>\\|\\(\\<unique\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
5539 "\\|\\(\\<endcase\\>\\)" )
5540 nil 'move)
5541 (cond
5542 ((match-end 1)
5543 (let ((here (point)))
5544 (verilog-beg-of-statement)
5545 (unless (looking-at verilog-extended-case-re)
5546 (goto-char here)))
5547 (setq nest (1- nest)))
5548 ((match-end 3)
5549 (setq nest (1+ nest)))
5550 ((bobp)
5551 (ding 't)
5552 (setq nest 0))))))
5553
5554 (defun verilog-leap-to-head ()
5555 "Move point to the head of this block.
5556 Jump from end to matching begin, from endcase to matching case, and so on."
5557 (let ((reg nil)
5558 snest
5559 (nesting 'yes)
5560 (nest 1))
5561 (cond
5562 ((looking-at "\\<end\\>")
5563 ;; 1: Search back for matching begin
5564 (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
5565 "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
5566 ((looking-at "\\<endtask\\>")
5567 ;; 2: Search back for matching task
5568 (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<task\\>\\)")
5569 (setq nesting 'no))
5570 ((looking-at "\\<endcase\\>")
5571 (catch 'nesting
5572 (verilog-leap-to-case-head) )
5573 (setq reg nil) ; to force skip
5574 )
5575
5576 ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
5577 ;; 4: Search back for matching fork
5578 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5579 ((looking-at "\\<endclass\\>")
5580 ;; 5: Search back for matching class
5581 (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
5582 ((looking-at "\\<endtable\\>")
5583 ;; 6: Search back for matching table
5584 (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
5585 ((looking-at "\\<endspecify\\>")
5586 ;; 7: Search back for matching specify
5587 (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
5588 ((looking-at "\\<endfunction\\>")
5589 ;; 8: Search back for matching function
5590 (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\(\\<virtual\\>\\s-+\\)\\|\\(\\<protected\\>\\s-+\\)\\)+\\<function\\>\\)")
5591 (setq nesting 'no))
5592 ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
5593 ((looking-at "\\<endgenerate\\>")
5594 ;; 8: Search back for matching generate
5595 (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
5596 ((looking-at "\\<endgroup\\>")
5597 ;; 10: Search back for matching covergroup
5598 (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
5599 ((looking-at "\\<endproperty\\>")
5600 ;; 11: Search back for matching property
5601 (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
5602 ((looking-at verilog-uvm-end-re)
5603 ;; 12: Search back for matching sequence
5604 (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
5605 ((looking-at verilog-ovm-end-re)
5606 ;; 12: Search back for matching sequence
5607 (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
5608 ((looking-at verilog-vmm-end-re)
5609 ;; 12: Search back for matching sequence
5610 (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
5611 ((looking-at "\\<endinterface\\>")
5612 ;; 12: Search back for matching interface
5613 (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
5614 ((looking-at "\\<endsequence\\>")
5615 ;; 12: Search back for matching sequence
5616 (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
5617 ((looking-at "\\<endclocking\\>")
5618 ;; 12: Search back for matching clocking
5619 (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
5620 (if reg
5621 (catch 'skip
5622 (if (eq nesting 'yes)
5623 (let (sreg)
5624 (while (verilog-re-search-backward reg nil 'move)
5625 (cond
5626 ((match-end 1) ; begin
5627 (if (looking-at "fork")
5628 (let ((here (point)))
5629 (verilog-beg-of-statement)
5630 (unless (looking-at verilog-disable-fork-re)
5631 (goto-char here)
5632 (setq nest (1- nest))))
5633 (setq nest (1- nest)))
5634 (if (= 0 nest)
5635 ;; Now previous line describes syntax
5636 (throw 'skip 1))
5637 (if (and snest
5638 (= snest nest))
5639 (setq reg sreg)))
5640 ((match-end 2) ; end
5641 (setq nest (1+ nest)))
5642 ((match-end 3)
5643 ;; endcase, jump to case
5644 (setq snest nest)
5645 (setq nest (1+ nest))
5646 (setq sreg reg)
5647 (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
5648 ((match-end 4)
5649 ;; join, jump to fork
5650 (setq snest nest)
5651 (setq nest (1+ nest))
5652 (setq sreg reg)
5653 (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
5654 )))
5655 ;no nesting
5656 (if (and
5657 (verilog-re-search-backward reg nil 'move)
5658 (match-end 1)) ; task -> could be virtual and/or protected
5659 (progn
5660 (verilog-beg-of-statement)
5661 (throw 'skip 1))
5662 (throw 'skip 1)))))))
5663
5664 (defun verilog-continued-line ()
5665 "Return true if this is a continued line.
5666 Set point to where line starts."
5667 (let ((continued 't))
5668 (if (eq 0 (forward-line -1))
5669 (progn
5670 (end-of-line)
5671 (verilog-backward-ws&directives)
5672 (if (bobp)
5673 (setq continued nil)
5674 (while (and continued
5675 (save-excursion
5676 (skip-chars-backward " \t")
5677 (not (bolp))))
5678 (setq continued (verilog-backward-token)))))
5679 (setq continued nil))
5680 continued))
5681
5682 (defun verilog-backward-token ()
5683 "Step backward token, returning true if this is a continued line."
5684 (interactive)
5685 (verilog-backward-syntactic-ws)
5686 (cond
5687 ((bolp)
5688 nil)
5689 (;-- Anything ending in a ; is complete
5690 (= (preceding-char) ?\;)
5691 nil)
5692 (; If a "}" is prefixed by a ";", then this is a complete statement
5693 ; i.e.: constraint foo { a = b; }
5694 (= (preceding-char) ?\})
5695 (progn
5696 (backward-char)
5697 (not(verilog-at-close-constraint-p))))
5698 (;-- constraint foo { a = b }
5699 ; is a complete statement. *sigh*
5700 (= (preceding-char) ?\{)
5701 (progn
5702 (backward-char)
5703 (not (verilog-at-constraint-p))))
5704 (;" string "
5705 (= (preceding-char) ?\")
5706 (backward-char)
5707 (verilog-skip-backward-comment-or-string)
5708 nil)
5709
5710 (; [3:4]
5711 (= (preceding-char) ?\])
5712 (backward-char)
5713 (verilog-backward-open-bracket)
5714 t)
5715
5716 (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
5717 ; also could be simply '@(foo)'
5718 ; or foo u1 #(a=8)
5719 ; (b, ... which ISN'T complete
5720 ;;;; Do we need this???
5721 (= (preceding-char) ?\))
5722 (progn
5723 (backward-char)
5724 (verilog-backward-up-list 1)
5725 (verilog-backward-syntactic-ws)
5726 (let ((back (point)))
5727 (forward-word -1)
5728 (cond
5729 ;;XX
5730 ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
5731 (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
5732 ((looking-at verilog-uvm-statement-re)
5733 nil)
5734 ((looking-at verilog-uvm-begin-re)
5735 t)
5736 ((looking-at verilog-uvm-end-re)
5737 t)
5738 ((looking-at verilog-ovm-statement-re)
5739 nil)
5740 ((looking-at verilog-ovm-begin-re)
5741 t)
5742 ((looking-at verilog-ovm-end-re)
5743 t)
5744 ;; JBA find VMM macros
5745 ((looking-at verilog-vmm-statement-re)
5746 nil )
5747 ((looking-at verilog-vmm-begin-re)
5748 t)
5749 ((looking-at verilog-vmm-end-re)
5750 nil)
5751 ;; JBA trying to catch macro lines with no ; at end
5752 ((looking-at "\\<`")
5753 nil)
5754 (t
5755 (goto-char back)
5756 (cond
5757 ((= (preceding-char) ?\@)
5758 (backward-char)
5759 (save-excursion
5760 (verilog-backward-token)
5761 (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
5762 ((= (preceding-char) ?\#)
5763 (backward-char))
5764 (t t)))))))
5765
5766 (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
5767 t
5768 (forward-word -1)
5769 (while (= (preceding-char) ?\_)
5770 (forward-word -1))
5771 (cond
5772 ((looking-at "\\<else\\>")
5773 t)
5774 ((looking-at verilog-behavioral-block-beg-re)
5775 t)
5776 ((looking-at verilog-indent-re)
5777 nil)
5778 (t
5779 (let
5780 ((back (point)))
5781 (verilog-backward-syntactic-ws)
5782 (cond
5783 ((= (preceding-char) ?\:)
5784 (backward-char)
5785 (verilog-backward-syntactic-ws)
5786 (backward-sexp)
5787 (if (looking-at verilog-nameable-item-re )
5788 nil
5789 t))
5790 ((= (preceding-char) ?\#)
5791 (backward-char)
5792 t)
5793 ((= (preceding-char) ?\`)
5794 (backward-char)
5795 t)
5796
5797 (t
5798 (goto-char back)
5799 t))))))))
5800
5801 (defun verilog-backward-syntactic-ws ()
5802 "Move backwards putting point after first non-whitespace non-comment."
5803 (verilog-skip-backward-comments)
5804 (forward-comment (- (buffer-size))))
5805
5806 (defun verilog-backward-syntactic-ws-quick ()
5807 "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
5808 (while (cond ((bobp)
5809 nil) ; Done
5810 ((> (skip-syntax-backward " ") 0)
5811 t)
5812 ((eq (preceding-char) ?\n) ;; \n's terminate // so aren't space syntax
5813 (forward-char -1)
5814 t)
5815 ((or (verilog-inside-comment-or-string-p (1- (point)))
5816 (verilog-inside-comment-or-string-p (point)))
5817 (re-search-backward "[/\"]" nil t) ;; Only way a comment or quote can begin
5818 t))))
5819
5820 (defun verilog-forward-syntactic-ws ()
5821 (verilog-skip-forward-comment-p)
5822 (forward-comment (buffer-size)))
5823
5824 (defun verilog-backward-ws&directives (&optional bound)
5825 "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
5826 Optional BOUND limits search."
5827 (save-restriction
5828 (let* ((bound (or bound (point-min)))
5829 (here bound)
5830 (p nil) )
5831 (if (< bound (point))
5832 (progn
5833 (let ((state (save-excursion (verilog-syntax-ppss))))
5834 (cond
5835 ((nth 7 state) ;; in // comment
5836 (verilog-re-search-backward "//" nil 'move)
5837 (skip-chars-backward "/"))
5838 ((nth 4 state) ;; in /* */ comment
5839 (verilog-re-search-backward "/\*" nil 'move))))
5840 (narrow-to-region bound (point))
5841 (while (/= here (point))
5842 (setq here (point))
5843 (verilog-skip-backward-comments)
5844 (setq p
5845 (save-excursion
5846 (beginning-of-line)
5847 (cond
5848 ((and verilog-highlight-translate-off
5849 (verilog-within-translate-off))
5850 (verilog-back-to-start-translate-off (point-min)))
5851 ((looking-at verilog-directive-re-1)
5852 (point))
5853 (t
5854 nil))))
5855 (if p (goto-char p))))))))
5856
5857 (defun verilog-forward-ws&directives (&optional bound)
5858 "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
5859 Optional BOUND limits search."
5860 (save-restriction
5861 (let* ((bound (or bound (point-max)))
5862 (here bound)
5863 jump)
5864 (if (> bound (point))
5865 (progn
5866 (let ((state (save-excursion (verilog-syntax-ppss))))
5867 (cond
5868 ((nth 7 state) ;; in // comment
5869 (end-of-line)
5870 (forward-char 1)
5871 (skip-chars-forward " \t\n\f")
5872 )
5873 ((nth 4 state) ;; in /* */ comment
5874 (verilog-re-search-forward "\*\/\\s-*" nil 'move))))
5875 (narrow-to-region (point) bound)
5876 (while (/= here (point))
5877 (setq here (point)
5878 jump nil)
5879 (forward-comment (buffer-size))
5880 (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ;; Attribute
5881 (goto-char (match-end 0)))
5882 (save-excursion
5883 (beginning-of-line)
5884 (if (looking-at verilog-directive-re-1)
5885 (setq jump t)))
5886 (if jump
5887 (beginning-of-line 2))))))))
5888
5889 (defun verilog-in-comment-p ()
5890 "Return true if in a star or // comment."
5891 (let ((state (save-excursion (verilog-syntax-ppss))))
5892 (or (nth 4 state) (nth 7 state))))
5893
5894 (defun verilog-in-star-comment-p ()
5895 "Return true if in a star comment."
5896 (let ((state (save-excursion (verilog-syntax-ppss))))
5897 (and
5898 (nth 4 state) ; t if in a comment of style a // or b /**/
5899 (not
5900 (nth 7 state) ; t if in a comment of style b /**/
5901 ))))
5902
5903 (defun verilog-in-slash-comment-p ()
5904 "Return true if in a slash comment."
5905 (let ((state (save-excursion (verilog-syntax-ppss))))
5906 (nth 7 state)))
5907
5908 (defun verilog-in-comment-or-string-p ()
5909 "Return true if in a string or comment."
5910 (let ((state (save-excursion (verilog-syntax-ppss))))
5911 (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
5912
5913 (defun verilog-in-attribute-p ()
5914 "Return true if point is in an attribute (* [] attribute *)."
5915 (save-match-data
5916 (save-excursion
5917 (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
5918 (numberp (match-beginning 1)))))
5919
5920 (defun verilog-in-parameter-p ()
5921 "Return true if point is in a parameter assignment #( p1=1, p2=5)."
5922 (save-match-data
5923 (save-excursion
5924 (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
5925 (numberp (match-beginning 1)))))
5926
5927 (defun verilog-in-escaped-name-p ()
5928 "Return true if in an escaped name."
5929 (save-excursion
5930 (backward-char)
5931 (skip-chars-backward "^ \t\n\f")
5932 (if (equal (char-after (point) ) ?\\ )
5933 t
5934 nil)))
5935 (defun verilog-in-directive-p ()
5936 "Return true if in a directive."
5937 (save-excursion
5938 (beginning-of-line)
5939 (looking-at verilog-directive-re-1)))
5940
5941 (defun verilog-in-parenthesis-p ()
5942 "Return true if in a ( ) expression (but not { } or [ ])."
5943 (save-match-data
5944 (save-excursion
5945 (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
5946 (numberp (match-beginning 1)))))
5947
5948 (defun verilog-in-paren ()
5949 "Return true if in a parenthetical expression.
5950 May cache result using `verilog-syntax-ppss'."
5951 (let ((state (save-excursion (verilog-syntax-ppss))))
5952 (> (nth 0 state) 0 )))
5953
5954 (defun verilog-in-paren-quick ()
5955 "Return true if in a parenthetical expression.
5956 Always starts from `point-min', to allow inserts with hooks disabled."
5957 ;; The -quick refers to its use alongside the other -quick functions,
5958 ;; not that it's likely to be faster than verilog-in-paren.
5959 (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
5960 (> (nth 0 state) 0 )))
5961
5962 (defun verilog-in-struct-p ()
5963 "Return true if in a struct declaration."
5964 (interactive)
5965 (save-excursion
5966 (if (verilog-in-paren)
5967 (progn
5968 (verilog-backward-up-list 1)
5969 (verilog-at-struct-p)
5970 )
5971 nil)))
5972
5973 (defun verilog-in-coverage-p ()
5974 "Return true if in a constraint or coverpoint expression."
5975 (interactive)
5976 (save-excursion
5977 (if (verilog-in-paren)
5978 (progn
5979 (verilog-backward-up-list 1)
5980 (verilog-at-constraint-p)
5981 )
5982 nil)))
5983 (defun verilog-at-close-constraint-p ()
5984 "If at the } that closes a constraint or covergroup, return true."
5985 (if (and
5986 (equal (char-after) ?\})
5987 (verilog-in-paren))
5988
5989 (save-excursion
5990 (verilog-backward-ws&directives)
5991 (if (equal (char-before) ?\;)
5992 (point)
5993 nil))))
5994
5995 (defun verilog-at-constraint-p ()
5996 "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
5997 (if (save-excursion
5998 (and
5999 (equal (char-after) ?\{)
6000 (forward-list)
6001 (progn (backward-char 1)
6002 (verilog-backward-ws&directives)
6003 (equal (char-before) ?\;))))
6004 ;; maybe
6005 (verilog-re-search-backward "\\<constraint\\|coverpoint\\|cross\\>" nil 'move)
6006 ;; not
6007 nil))
6008
6009 (defun verilog-at-struct-p ()
6010 "If at the { of a struct, return true, moving point to struct."
6011 (save-excursion
6012 (if (and (equal (char-after) ?\{)
6013 (verilog-backward-token))
6014 (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
6015 nil)))
6016
6017 (defun verilog-parenthesis-depth ()
6018 "Return non zero if in parenthetical-expression."
6019 (save-excursion (nth 1 (verilog-syntax-ppss))))
6020
6021
6022 (defun verilog-skip-forward-comment-or-string ()
6023 "Return true if in a string or comment."
6024 (let ((state (save-excursion (verilog-syntax-ppss))))
6025 (cond
6026 ((nth 3 state) ;Inside string
6027 (search-forward "\"")
6028 t)
6029 ((nth 7 state) ;Inside // comment
6030 (forward-line 1)
6031 t)
6032 ((nth 4 state) ;Inside any comment (hence /**/)
6033 (search-forward "*/"))
6034 (t
6035 nil))))
6036
6037 (defun verilog-skip-backward-comment-or-string ()
6038 "Return true if in a string or comment."
6039 (let ((state (save-excursion (verilog-syntax-ppss))))
6040 (cond
6041 ((nth 3 state) ;Inside string
6042 (search-backward "\"")
6043 t)
6044 ((nth 7 state) ;Inside // comment
6045 (search-backward "//")
6046 (skip-chars-backward "/")
6047 t)
6048 ((nth 4 state) ;Inside /* */ comment
6049 (search-backward "/*")
6050 t)
6051 (t
6052 nil))))
6053
6054 (defun verilog-skip-backward-comments ()
6055 "Return true if a comment was skipped."
6056 (let ((more t))
6057 (while more
6058 (setq more
6059 (let ((state (save-excursion (verilog-syntax-ppss))))
6060 (cond
6061 ((nth 7 state) ;Inside // comment
6062 (search-backward "//")
6063 (skip-chars-backward "/")
6064 (skip-chars-backward " \t\n\f")
6065 t)
6066 ((nth 4 state) ;Inside /* */ comment
6067 (search-backward "/*")
6068 (skip-chars-backward " \t\n\f")
6069 t)
6070 ((and (not (bobp))
6071 (= (char-before) ?\/)
6072 (= (char-before (1- (point))) ?\*))
6073 (goto-char (- (point) 2))
6074 t) ;; Let nth 4 state handle the rest
6075 ((and (not (bobp))
6076 (= (char-before) ?\))
6077 (= (char-before (1- (point))) ?\*))
6078 (goto-char (- (point) 2))
6079 (if (search-backward "(*" nil t)
6080 (progn
6081 (skip-chars-backward " \t\n\f")
6082 t)
6083 (progn
6084 (goto-char (+ (point) 2))
6085 nil)))
6086 (t
6087 (/= (skip-chars-backward " \t\n\f") 0))))))))
6088
6089 (defun verilog-skip-forward-comment-p ()
6090 "If in comment, move to end and return true."
6091 (let* (h
6092 (state (save-excursion (verilog-syntax-ppss)))
6093 (skip (cond
6094 ((nth 3 state) ;Inside string
6095 t)
6096 ((nth 7 state) ;Inside // comment
6097 (end-of-line)
6098 (forward-char 1)
6099 t)
6100 ((nth 4 state) ;Inside /* comment
6101 (search-forward "*/")
6102 t)
6103 ((verilog-in-attribute-p) ;Inside (* attribute
6104 (search-forward "*)" nil t)
6105 t)
6106 (t nil))))
6107 (skip-chars-forward " \t\n\f")
6108 (while
6109 (cond
6110 ((looking-at "\\/\\*")
6111 (progn
6112 (setq h (point))
6113 (goto-char (match-end 0))
6114 (if (search-forward "*/" nil t)
6115 (progn
6116 (skip-chars-forward " \t\n\f")
6117 (setq skip 't))
6118 (progn
6119 (goto-char h)
6120 nil))))
6121 ((looking-at "(\\*")
6122 (progn
6123 (setq h (point))
6124 (goto-char (match-end 0))
6125 (if (search-forward "*)" nil t)
6126 (progn
6127 (skip-chars-forward " \t\n\f")
6128 (setq skip 't))
6129 (progn
6130 (goto-char h)
6131 nil))))
6132 (t nil)))
6133 skip))
6134
6135 (defun verilog-indent-line-relative ()
6136 "Cheap version of indent line.
6137 Only look at a few lines to determine indent level."
6138 (interactive)
6139 (let ((indent-str)
6140 (sp (point)))
6141 (if (looking-at "^[ \t]*$")
6142 (cond ;- A blank line; No need to be too smart.
6143 ((bobp)
6144 (setq indent-str (list 'cpp 0)))
6145 ((verilog-continued-line)
6146 (let ((sp1 (point)))
6147 (if (verilog-continued-line)
6148 (progn
6149 (goto-char sp)
6150 (setq indent-str
6151 (list 'statement (verilog-current-indent-level))))
6152 (goto-char sp1)
6153 (setq indent-str (list 'block (verilog-current-indent-level)))))
6154 (goto-char sp))
6155 ((goto-char sp)
6156 (setq indent-str (verilog-calculate-indent))))
6157 (progn (skip-chars-forward " \t")
6158 (setq indent-str (verilog-calculate-indent))))
6159 (verilog-do-indent indent-str)))
6160
6161 (defun verilog-indent-line ()
6162 "Indent for special part of code."
6163 (verilog-do-indent (verilog-calculate-indent)))
6164
6165 (defun verilog-do-indent (indent-str)
6166 (let ((type (car indent-str))
6167 (ind (car (cdr indent-str))))
6168 (cond
6169 (; handle continued exp
6170 (eq type 'cexp)
6171 (let ((here (point)))
6172 (verilog-backward-syntactic-ws)
6173 (cond
6174 ((or
6175 (= (preceding-char) ?\,)
6176 (= (preceding-char) ?\])
6177 (save-excursion
6178 (verilog-beg-of-statement-1)
6179 (looking-at verilog-declaration-re)))
6180 (let* ( fst
6181 (val
6182 (save-excursion
6183 (backward-char 1)
6184 (verilog-beg-of-statement-1)
6185 (setq fst (point))
6186 (if (looking-at verilog-declaration-re)
6187 (progn ;; we have multiple words
6188 (goto-char (match-end 0))
6189 (skip-chars-forward " \t")
6190 (cond
6191 ((and verilog-indent-declaration-macros
6192 (= (following-char) ?\`))
6193 (progn
6194 (forward-char 1)
6195 (forward-word 1)
6196 (skip-chars-forward " \t")))
6197 ((= (following-char) ?\[)
6198 (progn
6199 (forward-char 1)
6200 (verilog-backward-up-list -1)
6201 (skip-chars-forward " \t"))))
6202 (current-column))
6203 (progn
6204 (goto-char fst)
6205 (+ (current-column) verilog-cexp-indent))))))
6206 (goto-char here)
6207 (indent-line-to val)
6208 (if (and (not verilog-indent-lists)
6209 (verilog-in-paren))
6210 (verilog-pretty-declarations-auto))
6211 ))
6212 ((= (preceding-char) ?\) )
6213 (goto-char here)
6214 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6215 (indent-line-to val)))
6216 (t
6217 (goto-char here)
6218 (let ((val))
6219 (verilog-beg-of-statement-1)
6220 (if (and (< (point) here)
6221 (verilog-re-search-forward "=[ \\t]*" here 'move))
6222 (setq val (current-column))
6223 (setq val (eval (cdr (assoc type verilog-indent-alist)))))
6224 (goto-char here)
6225 (indent-line-to val))))))
6226
6227 (; handle inside parenthetical expressions
6228 (eq type 'cparenexp)
6229 (let* ( here
6230 (val (save-excursion
6231 (verilog-backward-up-list 1)
6232 (forward-char 1)
6233 (if verilog-indent-lists
6234 (skip-chars-forward " \t")
6235 (verilog-forward-syntactic-ws))
6236 (setq here (point))
6237 (current-column)))
6238
6239 (decl (save-excursion
6240 (goto-char here)
6241 (verilog-forward-syntactic-ws)
6242 (setq here (point))
6243 (looking-at verilog-declaration-re))))
6244 (indent-line-to val)
6245 (if decl
6246 (verilog-pretty-declarations-auto))))
6247
6248 (;-- Handle the ends
6249 (or
6250 (looking-at verilog-end-block-re )
6251 (verilog-at-close-constraint-p))
6252 (let ((val (if (eq type 'statement)
6253 (- ind verilog-indent-level)
6254 ind)))
6255 (indent-line-to val)))
6256
6257 (;-- Case -- maybe line 'em up
6258 (and (eq type 'case) (not (looking-at "^[ \t]*$")))
6259 (progn
6260 (cond
6261 ((looking-at "\\<endcase\\>")
6262 (indent-line-to ind))
6263 (t
6264 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6265 (indent-line-to val))))))
6266
6267 (;-- defun
6268 (and (eq type 'defun)
6269 (looking-at verilog-zero-indent-re))
6270 (indent-line-to 0))
6271
6272 (;-- declaration
6273 (and (or
6274 (eq type 'defun)
6275 (eq type 'block))
6276 (looking-at verilog-declaration-re))
6277 (verilog-indent-declaration ind))
6278
6279 (;-- Everything else
6280 t
6281 (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
6282 (indent-line-to val))))
6283
6284 (if (looking-at "[ \t]+$")
6285 (skip-chars-forward " \t"))
6286 indent-str ; Return indent data
6287 ))
6288
6289 (defun verilog-current-indent-level ()
6290 "Return the indent-level of the current statement."
6291 (save-excursion
6292 (let (par-pos)
6293 (beginning-of-line)
6294 (setq par-pos (verilog-parenthesis-depth))
6295 (while par-pos
6296 (goto-char par-pos)
6297 (beginning-of-line)
6298 (setq par-pos (verilog-parenthesis-depth)))
6299 (skip-chars-forward " \t")
6300 (current-column))))
6301
6302 (defun verilog-case-indent-level ()
6303 "Return the indent-level of the current statement.
6304 Do not count named blocks or case-statements."
6305 (save-excursion
6306 (skip-chars-forward " \t")
6307 (cond
6308 ((looking-at verilog-named-block-re)
6309 (current-column))
6310 ((and (not (looking-at verilog-extended-case-re))
6311 (looking-at "^[^:;]+[ \t]*:"))
6312 (verilog-re-search-forward ":" nil t)
6313 (skip-chars-forward " \t")
6314 (current-column))
6315 (t
6316 (current-column)))))
6317
6318 (defun verilog-indent-comment ()
6319 "Indent current line as comment."
6320 (let* ((stcol
6321 (cond
6322 ((verilog-in-star-comment-p)
6323 (save-excursion
6324 (re-search-backward "/\\*" nil t)
6325 (1+(current-column))))
6326 (comment-column
6327 comment-column )
6328 (t
6329 (save-excursion
6330 (re-search-backward "//" nil t)
6331 (current-column))))))
6332 (indent-line-to stcol)
6333 stcol))
6334
6335 (defun verilog-more-comment ()
6336 "Make more comment lines like the previous."
6337 (let* ((star 0)
6338 (stcol
6339 (cond
6340 ((verilog-in-star-comment-p)
6341 (save-excursion
6342 (setq star 1)
6343 (re-search-backward "/\\*" nil t)
6344 (1+(current-column))))
6345 (comment-column
6346 comment-column )
6347 (t
6348 (save-excursion
6349 (re-search-backward "//" nil t)
6350 (current-column))))))
6351 (progn
6352 (indent-to stcol)
6353 (if (and star
6354 (save-excursion
6355 (forward-line -1)
6356 (skip-chars-forward " \t")
6357 (looking-at "\*")))
6358 (insert "* ")))))
6359
6360 (defun verilog-comment-indent (&optional arg)
6361 "Return the column number the line should be indented to.
6362 ARG is ignored, for `comment-indent-function' compatibility."
6363 (cond
6364 ((verilog-in-star-comment-p)
6365 (save-excursion
6366 (re-search-backward "/\\*" nil t)
6367 (1+(current-column))))
6368 ( comment-column
6369 comment-column )
6370 (t
6371 (save-excursion
6372 (re-search-backward "//" nil t)
6373 (current-column)))))
6374
6375 ;;
6376
6377 (defun verilog-pretty-declarations-auto (&optional quiet)
6378 "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
6379 (when (or (eq 'all verilog-auto-lineup)
6380 (eq 'declarations verilog-auto-lineup))
6381 (verilog-pretty-declarations quiet)))
6382
6383 (defun verilog-pretty-declarations (&optional quiet)
6384 "Line up declarations around point.
6385 Be verbose about progress unless optional QUIET set."
6386 (interactive)
6387 (let* ((m1 (make-marker))
6388 (e (point))
6389 el
6390 r
6391 (here (point))
6392 ind
6393 start
6394 startpos
6395 end
6396 endpos
6397 base-ind
6398 )
6399 (save-excursion
6400 (if (progn
6401 ; (verilog-beg-of-statement-1)
6402 (beginning-of-line)
6403 (verilog-forward-syntactic-ws)
6404 (and (not (verilog-in-directive-p)) ;; could have `define input foo
6405 (looking-at verilog-declaration-re)))
6406 (progn
6407 (if (verilog-parenthesis-depth)
6408 ;; in an argument list or parameter block
6409 (setq el (verilog-backward-up-list -1)
6410 start (progn
6411 (goto-char e)
6412 (verilog-backward-up-list 1)
6413 (forward-line) ;; ignore ( input foo,
6414 (verilog-re-search-forward verilog-declaration-re el 'move)
6415 (goto-char (match-beginning 0))
6416 (skip-chars-backward " \t")
6417 (point))
6418 startpos (set-marker (make-marker) start)
6419 end (progn
6420 (goto-char start)
6421 (verilog-backward-up-list -1)
6422 (forward-char -1)
6423 (verilog-backward-syntactic-ws)
6424 (point))
6425 endpos (set-marker (make-marker) end)
6426 base-ind (progn
6427 (goto-char start)
6428 (forward-char 1)
6429 (skip-chars-forward " \t")
6430 (current-column))
6431 )
6432 ;; in a declaration block (not in argument list)
6433 (setq
6434 start (progn
6435 (verilog-beg-of-statement-1)
6436 (while (and (looking-at verilog-declaration-re)
6437 (not (bobp)))
6438 (skip-chars-backward " \t")
6439 (setq e (point))
6440 (beginning-of-line)
6441 (verilog-backward-syntactic-ws)
6442 (backward-char)
6443 (verilog-beg-of-statement-1))
6444 e)
6445 startpos (set-marker (make-marker) start)
6446 end (progn
6447 (goto-char here)
6448 (verilog-end-of-statement)
6449 (setq e (point)) ;Might be on last line
6450 (verilog-forward-syntactic-ws)
6451 (while (looking-at verilog-declaration-re)
6452 (verilog-end-of-statement)
6453 (setq e (point))
6454 (verilog-forward-syntactic-ws))
6455 e)
6456 endpos (set-marker (make-marker) end)
6457 base-ind (progn
6458 (goto-char start)
6459 (verilog-do-indent (verilog-calculate-indent))
6460 (verilog-forward-ws&directives)
6461 (current-column))))
6462 ;; OK, start and end are set
6463 (goto-char (marker-position startpos))
6464 (if (and (not quiet)
6465 (> (- end start) 100))
6466 (message "Lining up declarations..(please stand by)"))
6467 ;; Get the beginning of line indent first
6468 (while (progn (setq e (marker-position endpos))
6469 (< (point) e))
6470 (cond
6471 ((save-excursion (skip-chars-backward " \t")
6472 (bolp))
6473 (verilog-forward-ws&directives)
6474 (indent-line-to base-ind)
6475 (verilog-forward-ws&directives)
6476 (if (< (point) e)
6477 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6478 (t
6479 (just-one-space)
6480 (verilog-re-search-forward "[ \t\n\f]" e 'move)))
6481 ;;(forward-line)
6482 )
6483 ;; Now find biggest prefix
6484 (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
6485 ;; Now indent each line.
6486 (goto-char (marker-position startpos))
6487 (while (progn (setq e (marker-position endpos))
6488 (setq r (- e (point)))
6489 (> r 0))
6490 (setq e (point))
6491 (unless quiet (message "%d" r))
6492 ;;(verilog-do-indent (verilog-calculate-indent)))
6493 (verilog-forward-ws&directives)
6494 (cond
6495 ((or (and verilog-indent-declaration-macros
6496 (looking-at verilog-declaration-re-2-macro))
6497 (looking-at verilog-declaration-re-2-no-macro))
6498 (let ((p (match-end 0)))
6499 (set-marker m1 p)
6500 (if (verilog-re-search-forward "[[#`]" p 'move)
6501 (progn
6502 (forward-char -1)
6503 (just-one-space)
6504 (goto-char (marker-position m1))
6505 (just-one-space)
6506 (indent-to ind))
6507 (progn
6508 (just-one-space)
6509 (indent-to ind)))))
6510 ((verilog-continued-line-1 (marker-position startpos))
6511 (goto-char e)
6512 (indent-line-to ind))
6513 ((verilog-in-struct-p)
6514 ;; could have a declaration of a user defined item
6515 (goto-char e)
6516 (verilog-end-of-statement))
6517 (t ; Must be comment or white space
6518 (goto-char e)
6519 (verilog-forward-ws&directives)
6520 (forward-line -1)))
6521 (forward-line 1))
6522 (unless quiet (message "")))))))
6523
6524 (defun verilog-pretty-expr (&optional quiet myre)
6525 "Line up expressions around point, optionally QUIET with regexp MYRE ignored."
6526 (interactive)
6527 (if (not (verilog-in-comment-or-string-p))
6528 (save-excursion
6529 (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
6530 (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
6531 (beginning-of-line)
6532 (if (and (not (looking-at rexp ))
6533 (looking-at verilog-assignment-operation-re)
6534 (save-excursion
6535 (goto-char (match-end 2))
6536 (and (not (verilog-in-attribute-p))
6537 (not (verilog-in-parameter-p))
6538 (not (verilog-in-comment-or-string-p)))))
6539 (let* ((here (point))
6540 (e) (r)
6541 (start
6542 (progn
6543 (beginning-of-line)
6544 (setq e (point))
6545 (verilog-backward-syntactic-ws)
6546 (beginning-of-line)
6547 (while (and (not (looking-at rexp1))
6548 (looking-at verilog-assignment-operation-re)
6549 (not (bobp))
6550 )
6551 (setq e (point))
6552 (verilog-backward-syntactic-ws)
6553 (beginning-of-line)
6554 ) ;Ack, need to grok `define
6555 e))
6556 (end
6557 (progn
6558 (goto-char here)
6559 (end-of-line)
6560 (setq e (point)) ;Might be on last line
6561 (verilog-forward-syntactic-ws)
6562 (beginning-of-line)
6563 (while (and
6564 (not (looking-at rexp1 ))
6565 (looking-at verilog-assignment-operation-re)
6566 (progn
6567 (end-of-line)
6568 (not (eq e (point)))))
6569 (setq e (point))
6570 (verilog-forward-syntactic-ws)
6571 (beginning-of-line)
6572 )
6573 e))
6574 (endpos (set-marker (make-marker) end))
6575 (ind)
6576 )
6577 (goto-char start)
6578 (verilog-do-indent (verilog-calculate-indent))
6579 (if (and (not quiet)
6580 (> (- end start) 100))
6581 (message "Lining up expressions..(please stand by)"))
6582
6583 ;; Set indent to minimum throughout region
6584 (while (< (point) (marker-position endpos))
6585 (beginning-of-line)
6586 (verilog-just-one-space verilog-assignment-operation-re)
6587 (beginning-of-line)
6588 (verilog-do-indent (verilog-calculate-indent))
6589 (end-of-line)
6590 (verilog-forward-syntactic-ws)
6591 )
6592
6593 ;; Now find biggest prefix
6594 (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
6595
6596 ;; Now indent each line.
6597 (goto-char start)
6598 (while (progn (setq e (marker-position endpos))
6599 (setq r (- e (point)))
6600 (> r 0))
6601 (setq e (point))
6602 (if (not quiet) (message "%d" r))
6603 (cond
6604 ((looking-at verilog-assignment-operation-re)
6605 (goto-char (match-beginning 2))
6606 (if (not (or (verilog-in-parenthesis-p) ;; leave attributes and comparisons alone
6607 (verilog-in-coverage-p)))
6608 (if (eq (char-after) ?=)
6609 (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
6610 (indent-to ind)
6611 ))
6612 )
6613 ((verilog-continued-line-1 start)
6614 (goto-char e)
6615 (indent-line-to ind))
6616 (t ; Must be comment or white space
6617 (goto-char e)
6618 (verilog-forward-ws&directives)
6619 (forward-line -1))
6620 )
6621 (forward-line 1))
6622 (unless quiet (message ""))
6623 ))))))
6624
6625 (defun verilog-just-one-space (myre)
6626 "Remove extra spaces around regular expression MYRE."
6627 (interactive)
6628 (if (and (not(looking-at verilog-complete-reg))
6629 (looking-at myre))
6630 (let ((p1 (match-end 1))
6631 (p2 (match-end 2)))
6632 (progn
6633 (goto-char p2)
6634 (just-one-space)
6635 (goto-char p1)
6636 (just-one-space)))))
6637
6638 (defun verilog-indent-declaration (baseind)
6639 "Indent current lines as declaration.
6640 Line up the variable names based on previous declaration's indentation.
6641 BASEIND is the base indent to offset everything."
6642 (interactive)
6643 (let ((pos (point-marker))
6644 (lim (save-excursion
6645 ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
6646 (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
6647 (point)))
6648 (ind)
6649 (val)
6650 (m1 (make-marker)))
6651 (setq val
6652 (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6653 (indent-line-to val)
6654
6655 ;; Use previous declaration (in this module) as template.
6656 (if (or (eq 'all verilog-auto-lineup)
6657 (eq 'declarations verilog-auto-lineup))
6658 (if (verilog-re-search-backward
6659 (or (and verilog-indent-declaration-macros
6660 verilog-declaration-re-1-macro)
6661 verilog-declaration-re-1-no-macro) lim t)
6662 (progn
6663 (goto-char (match-end 0))
6664 (skip-chars-forward " \t")
6665 (setq ind (current-column))
6666 (goto-char pos)
6667 (setq val
6668 (+ baseind
6669 (eval (cdr (assoc 'declaration verilog-indent-alist)))))
6670 (indent-line-to val)
6671 (if (and verilog-indent-declaration-macros
6672 (looking-at verilog-declaration-re-2-macro))
6673 (let ((p (match-end 0)))
6674 (set-marker m1 p)
6675 (if (verilog-re-search-forward "[[#`]" p 'move)
6676 (progn
6677 (forward-char -1)
6678 (just-one-space)
6679 (goto-char (marker-position m1))
6680 (just-one-space)
6681 (indent-to ind))
6682 (if (/= (current-column) ind)
6683 (progn
6684 (just-one-space)
6685 (indent-to ind)))))
6686 (if (looking-at verilog-declaration-re-2-no-macro)
6687 (let ((p (match-end 0)))
6688 (set-marker m1 p)
6689 (if (verilog-re-search-forward "[[`#]" p 'move)
6690 (progn
6691 (forward-char -1)
6692 (just-one-space)
6693 (goto-char (marker-position m1))
6694 (just-one-space)
6695 (indent-to ind))
6696 (if (/= (current-column) ind)
6697 (progn
6698 (just-one-space)
6699 (indent-to ind))))))))))
6700 (goto-char pos)))
6701
6702 (defun verilog-get-lineup-indent (b edpos)
6703 "Return the indent level that will line up several lines within the region.
6704 Region is defined by B and EDPOS."
6705 (save-excursion
6706 (let ((ind 0) e)
6707 (goto-char b)
6708 ;; Get rightmost position
6709 (while (progn (setq e (marker-position edpos))
6710 (< (point) e))
6711 (if (verilog-re-search-forward
6712 (or (and verilog-indent-declaration-macros
6713 verilog-declaration-re-1-macro)
6714 verilog-declaration-re-1-no-macro) e 'move)
6715 (progn
6716 (goto-char (match-end 0))
6717 (verilog-backward-syntactic-ws)
6718 (if (> (current-column) ind)
6719 (setq ind (current-column)))
6720 (goto-char (match-end 0)))))
6721 (if (> ind 0)
6722 (1+ ind)
6723 ;; No lineup-string found
6724 (goto-char b)
6725 (end-of-line)
6726 (verilog-backward-syntactic-ws)
6727 ;;(skip-chars-backward " \t")
6728 (1+ (current-column))))))
6729
6730 (defun verilog-get-lineup-indent-2 (myre b edpos)
6731 "Return the indent level that will line up several lines within the region."
6732 (save-excursion
6733 (let ((ind 0) e)
6734 (goto-char b)
6735 ;; Get rightmost position
6736 (while (progn (setq e (marker-position edpos))
6737 (< (point) e))
6738 (if (and (verilog-re-search-forward myre e 'move)
6739 (not (verilog-in-attribute-p))) ;; skip attribute exprs
6740 (progn
6741 (goto-char (match-beginning 2))
6742 (verilog-backward-syntactic-ws)
6743 (if (> (current-column) ind)
6744 (setq ind (current-column)))
6745 (goto-char (match-end 0)))
6746 ))
6747 (if (> ind 0)
6748 (1+ ind)
6749 ;; No lineup-string found
6750 (goto-char b)
6751 (end-of-line)
6752 (skip-chars-backward " \t")
6753 (1+ (current-column))))))
6754
6755 (defun verilog-comment-depth (type val)
6756 "A useful mode debugging aide. TYPE and VAL are comments for insertion."
6757 (save-excursion
6758 (let
6759 ((b (prog2
6760 (beginning-of-line)
6761 (point-marker)
6762 (end-of-line)))
6763 (e (point-marker)))
6764 (if (re-search-backward " /\\* \[#-\]# \[a-zA-Z\]+ \[0-9\]+ ## \\*/" b t)
6765 (progn
6766 (replace-match " /* -# ## */")
6767 (end-of-line))
6768 (progn
6769 (end-of-line)
6770 (insert " /* ## ## */"))))
6771 (backward-char 6)
6772 (insert
6773 (format "%s %d" type val))))
6774
6775 ;; \f
6776 ;;
6777 ;; Completion
6778 ;;
6779 (defvar verilog-str nil)
6780 (defvar verilog-all nil)
6781 (defvar verilog-pred nil)
6782 (defvar verilog-buffer-to-use nil)
6783 (defvar verilog-flag nil)
6784 (defvar verilog-toggle-completions nil
6785 "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
6786 Repeated use of \\[verilog-complete-word] will show you all of them.
6787 Normally, when there is more than one possible completion,
6788 it displays a list of all possible completions.")
6789
6790
6791 (defvar verilog-type-keywords
6792 '(
6793 "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
6794 "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
6795 "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
6796 "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
6797 "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
6798 "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
6799 )
6800 "Keywords for types used when completing a word in a declaration or parmlist.
6801 \(integer, real, reg...)")
6802
6803 (defvar verilog-cpp-keywords
6804 '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
6805 "endif")
6806 "Keywords to complete when at first word of a line in declarative scope.
6807 \(initial, always, begin, assign...)
6808 The procedures and variables defined within the Verilog program
6809 will be completed at runtime and should not be added to this list.")
6810
6811 (defvar verilog-defun-keywords
6812 (append
6813 '(
6814 "always" "always_comb" "always_ff" "always_latch" "assign"
6815 "begin" "end" "generate" "endgenerate" "module" "endmodule"
6816 "specify" "endspecify" "function" "endfunction" "initial" "final"
6817 "task" "endtask" "primitive" "endprimitive"
6818 )
6819 verilog-type-keywords)
6820 "Keywords to complete when at first word of a line in declarative scope.
6821 \(initial, always, begin, assign...)
6822 The procedures and variables defined within the Verilog program
6823 will be completed at runtime and should not be added to this list.")
6824
6825 (defvar verilog-block-keywords
6826 '(
6827 "begin" "break" "case" "continue" "else" "end" "endfunction"
6828 "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
6829 "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
6830 "while")
6831 "Keywords to complete when at first word of a line in behavioral scope.
6832 \(begin, if, then, else, for, fork...)
6833 The procedures and variables defined within the Verilog program
6834 will be completed at runtime and should not be added to this list.")
6835
6836 (defvar verilog-tf-keywords
6837 '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
6838 "Keywords to complete when at first word of a line in a task or function.
6839 \(begin, if, then, else, for, fork.)
6840 The procedures and variables defined within the Verilog program
6841 will be completed at runtime and should not be added to this list.")
6842
6843 (defvar verilog-case-keywords
6844 '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
6845 "Keywords to complete when at first word of a line in case scope.
6846 \(begin, if, then, else, for, fork...)
6847 The procedures and variables defined within the Verilog program
6848 will be completed at runtime and should not be added to this list.")
6849
6850 (defvar verilog-separator-keywords
6851 '("else" "then" "begin")
6852 "Keywords to complete when NOT standing at the first word of a statement.
6853 \(else, then, begin...)
6854 Variables and function names defined within the Verilog program
6855 will be completed at runtime and should not be added to this list.")
6856
6857 (defvar verilog-gate-ios
6858 ;; All these have an implied {"input"...} at the end
6859 '(("and" "output")
6860 ("buf" "output")
6861 ("bufif0" "output")
6862 ("bufif1" "output")
6863 ("cmos" "output")
6864 ("nand" "output")
6865 ("nmos" "output")
6866 ("nor" "output")
6867 ("not" "output")
6868 ("notif0" "output")
6869 ("notif1" "output")
6870 ("or" "output")
6871 ("pmos" "output")
6872 ("pulldown" "output")
6873 ("pullup" "output")
6874 ("rcmos" "output")
6875 ("rnmos" "output")
6876 ("rpmos" "output")
6877 ("rtran" "inout" "inout")
6878 ("rtranif0" "inout" "inout")
6879 ("rtranif1" "inout" "inout")
6880 ("tran" "inout" "inout")
6881 ("tranif0" "inout" "inout")
6882 ("tranif1" "inout" "inout")
6883 ("xnor" "output")
6884 ("xor" "output"))
6885 "Map of direction for each positional argument to each gate primitive.")
6886
6887 (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
6888 "Keywords for gate primitives.")
6889
6890 (defun verilog-string-diff (str1 str2)
6891 "Return index of first letter where STR1 and STR2 differs."
6892 (catch 'done
6893 (let ((diff 0))
6894 (while t
6895 (if (or (> (1+ diff) (length str1))
6896 (> (1+ diff) (length str2)))
6897 (throw 'done diff))
6898 (or (equal (aref str1 diff) (aref str2 diff))
6899 (throw 'done diff))
6900 (setq diff (1+ diff))))))
6901
6902 ;; Calculate all possible completions for functions if argument is `function',
6903 ;; completions for procedures if argument is `procedure' or both functions and
6904 ;; procedures otherwise.
6905
6906 (defun verilog-func-completion (type)
6907 "Build regular expression for module/task/function names.
6908 TYPE is 'module, 'tf for task or function, or t if unknown."
6909 (if (string= verilog-str "")
6910 (setq verilog-str "[a-zA-Z_]"))
6911 (let ((verilog-str (concat (cond
6912 ((eq type 'module) "\\<\\(module\\)\\s +")
6913 ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
6914 (t "\\<\\(task\\|function\\|module\\)\\s +"))
6915 "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
6916 match)
6917
6918 (if (not (looking-at verilog-defun-re))
6919 (verilog-re-search-backward verilog-defun-re nil t))
6920 (forward-char 1)
6921
6922 ;; Search through all reachable functions
6923 (goto-char (point-min))
6924 (while (verilog-re-search-forward verilog-str (point-max) t)
6925 (progn (setq match (buffer-substring (match-beginning 2)
6926 (match-end 2)))
6927 (if (or (null verilog-pred)
6928 (funcall verilog-pred match))
6929 (setq verilog-all (cons match verilog-all)))))
6930 (if (match-beginning 0)
6931 (goto-char (match-beginning 0)))))
6932
6933 (defun verilog-get-completion-decl (end)
6934 "Macro for searching through current declaration (var, type or const)
6935 for matches of `str' and adding the occurrence tp `all' through point END."
6936 (let ((re (or (and verilog-indent-declaration-macros
6937 verilog-declaration-re-2-macro)
6938 verilog-declaration-re-2-no-macro))
6939 decl-end match)
6940 ;; Traverse lines
6941 (while (and (< (point) end)
6942 (verilog-re-search-forward re end t))
6943 ;; Traverse current line
6944 (setq decl-end (save-excursion (verilog-declaration-end)))
6945 (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
6946 (not (match-end 1)))
6947 (setq match (buffer-substring (match-beginning 0) (match-end 0)))
6948 (if (string-match (concat "\\<" verilog-str) match)
6949 (if (or (null verilog-pred)
6950 (funcall verilog-pred match))
6951 (setq verilog-all (cons match verilog-all)))))
6952 (forward-line 1)))
6953 verilog-all)
6954
6955 (defun verilog-type-completion ()
6956 "Calculate all possible completions for types."
6957 (let ((start (point))
6958 goon)
6959 ;; Search for all reachable type declarations
6960 (while (or (verilog-beg-of-defun)
6961 (setq goon (not goon)))
6962 (save-excursion
6963 (if (and (< start (prog1 (save-excursion (verilog-end-of-defun)
6964 (point))
6965 (forward-char 1)))
6966 (verilog-re-search-forward
6967 "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
6968 start t)
6969 (not (match-end 1)))
6970 ;; Check current type declaration
6971 (verilog-get-completion-decl start))))))
6972
6973 (defun verilog-var-completion ()
6974 "Calculate all possible completions for variables (or constants)."
6975 (let ((start (point)))
6976 ;; Search for all reachable var declarations
6977 (verilog-beg-of-defun)
6978 (save-excursion
6979 ;; Check var declarations
6980 (verilog-get-completion-decl start))))
6981
6982 (defun verilog-keyword-completion (keyword-list)
6983 "Give list of all possible completions of keywords in KEYWORD-LIST."
6984 (mapcar (lambda (s)
6985 (if (string-match (concat "\\<" verilog-str) s)
6986 (if (or (null verilog-pred)
6987 (funcall verilog-pred s))
6988 (setq verilog-all (cons s verilog-all)))))
6989 keyword-list))
6990
6991
6992 (defun verilog-completion (verilog-str verilog-pred verilog-flag)
6993 "Function passed to `completing-read', `try-completion' or `all-completions'.
6994 Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
6995 must be a function to be called for every match to check if this should
6996 really be a match. If VERILOG-FLAG is t, the function returns a list of
6997 all possible completions. If VERILOG-FLAG is nil it returns a string,
6998 the longest possible completion, or t if VERILOG-STR is an exact match.
6999 If VERILOG-FLAG is 'lambda, the function returns t if VERILOG-STR is an
7000 exact match, nil otherwise."
7001 (save-excursion
7002 (let ((verilog-all nil))
7003 ;; Set buffer to use for searching labels. This should be set
7004 ;; within functions which use verilog-completions
7005 (set-buffer verilog-buffer-to-use)
7006
7007 ;; Determine what should be completed
7008 (let ((state (car (verilog-calculate-indent))))
7009 (cond ((eq state 'defun)
7010 (save-excursion (verilog-var-completion))
7011 (verilog-func-completion 'module)
7012 (verilog-keyword-completion verilog-defun-keywords))
7013
7014 ((eq state 'behavioral)
7015 (save-excursion (verilog-var-completion))
7016 (verilog-func-completion 'module)
7017 (verilog-keyword-completion verilog-defun-keywords))
7018
7019 ((eq state 'block)
7020 (save-excursion (verilog-var-completion))
7021 (verilog-func-completion 'tf)
7022 (verilog-keyword-completion verilog-block-keywords))
7023
7024 ((eq state 'case)
7025 (save-excursion (verilog-var-completion))
7026 (verilog-func-completion 'tf)
7027 (verilog-keyword-completion verilog-case-keywords))
7028
7029 ((eq state 'tf)
7030 (save-excursion (verilog-var-completion))
7031 (verilog-func-completion 'tf)
7032 (verilog-keyword-completion verilog-tf-keywords))
7033
7034 ((eq state 'cpp)
7035 (save-excursion (verilog-var-completion))
7036 (verilog-keyword-completion verilog-cpp-keywords))
7037
7038 ((eq state 'cparenexp)
7039 (save-excursion (verilog-var-completion)))
7040
7041 (t;--Anywhere else
7042 (save-excursion (verilog-var-completion))
7043 (verilog-func-completion 'both)
7044 (verilog-keyword-completion verilog-separator-keywords))))
7045
7046 ;; Now we have built a list of all matches. Give response to caller
7047 (verilog-completion-response))))
7048
7049 (defun verilog-completion-response ()
7050 (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
7051 ;; This was not called by all-completions
7052 (if (null verilog-all)
7053 ;; Return nil if there was no matching label
7054 nil
7055 ;; Get longest string common in the labels
7056 (let* ((elm (cdr verilog-all))
7057 (match (car verilog-all))
7058 (min (length match))
7059 tmp)
7060 (if (string= match verilog-str)
7061 ;; Return t if first match was an exact match
7062 (setq match t)
7063 (while (not (null elm))
7064 ;; Find longest common string
7065 (if (< (setq tmp (verilog-string-diff match (car elm))) min)
7066 (progn
7067 (setq min tmp)
7068 (setq match (substring match 0 min))))
7069 ;; Terminate with match=t if this is an exact match
7070 (if (string= (car elm) verilog-str)
7071 (progn
7072 (setq match t)
7073 (setq elm nil))
7074 (setq elm (cdr elm)))))
7075 ;; If this is a test just for exact match, return nil ot t
7076 (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
7077 nil
7078 match))))
7079 ;; If flag is t, this was called by all-completions. Return
7080 ;; list of all possible completions
7081 (verilog-flag
7082 verilog-all)))
7083
7084 (defvar verilog-last-word-numb 0)
7085 (defvar verilog-last-word-shown nil)
7086 (defvar verilog-last-completions nil)
7087
7088 (defun verilog-complete-word ()
7089 "Complete word at current point.
7090 \(See also `verilog-toggle-completions', `verilog-type-keywords',
7091 and `verilog-separator-keywords'.)"
7092 (interactive)
7093 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7094 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7095 (verilog-str (buffer-substring b e))
7096 ;; The following variable is used in verilog-completion
7097 (verilog-buffer-to-use (current-buffer))
7098 (allcomp (if (and verilog-toggle-completions
7099 (string= verilog-last-word-shown verilog-str))
7100 verilog-last-completions
7101 (all-completions verilog-str 'verilog-completion)))
7102 (match (if verilog-toggle-completions
7103 "" (try-completion
7104 verilog-str (mapcar (lambda (elm)
7105 (cons elm 0)) allcomp)))))
7106 ;; Delete old string
7107 (delete-region b e)
7108
7109 ;; Toggle-completions inserts whole labels
7110 (if verilog-toggle-completions
7111 (progn
7112 ;; Update entry number in list
7113 (setq verilog-last-completions allcomp
7114 verilog-last-word-numb
7115 (if (>= verilog-last-word-numb (1- (length allcomp)))
7116 0
7117 (1+ verilog-last-word-numb)))
7118 (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
7119 ;; Display next match or same string if no match was found
7120 (if (not (null allcomp))
7121 (insert "" verilog-last-word-shown)
7122 (insert "" verilog-str)
7123 (message "(No match)")))
7124 ;; The other form of completion does not necessarily do that.
7125
7126 ;; Insert match if found, or the original string if no match
7127 (if (or (null match) (equal match 't))
7128 (progn (insert "" verilog-str)
7129 (message "(No match)"))
7130 (insert "" match))
7131 ;; Give message about current status of completion
7132 (cond ((equal match 't)
7133 (if (not (null (cdr allcomp)))
7134 (message "(Complete but not unique)")
7135 (message "(Sole completion)")))
7136 ;; Display buffer if the current completion didn't help
7137 ;; on completing the label.
7138 ((and (not (null (cdr allcomp))) (= (length verilog-str)
7139 (length match)))
7140 (with-output-to-temp-buffer "*Completions*"
7141 (display-completion-list allcomp))
7142 ;; Wait for a key press. Then delete *Completion* window
7143 (momentary-string-display "" (point))
7144 (delete-window (get-buffer-window (get-buffer "*Completions*")))
7145 )))))
7146
7147 (defun verilog-show-completions ()
7148 "Show all possible completions at current point."
7149 (interactive)
7150 (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
7151 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
7152 (verilog-str (buffer-substring b e))
7153 ;; The following variable is used in verilog-completion
7154 (verilog-buffer-to-use (current-buffer))
7155 (allcomp (if (and verilog-toggle-completions
7156 (string= verilog-last-word-shown verilog-str))
7157 verilog-last-completions
7158 (all-completions verilog-str 'verilog-completion))))
7159 ;; Show possible completions in a temporary buffer.
7160 (with-output-to-temp-buffer "*Completions*"
7161 (display-completion-list allcomp))
7162 ;; Wait for a key press. Then delete *Completion* window
7163 (momentary-string-display "" (point))
7164 (delete-window (get-buffer-window (get-buffer "*Completions*")))))
7165
7166
7167 (defun verilog-get-default-symbol ()
7168 "Return symbol around current point as a string."
7169 (save-excursion
7170 (buffer-substring (progn
7171 (skip-chars-backward " \t")
7172 (skip-chars-backward "a-zA-Z0-9_")
7173 (point))
7174 (progn
7175 (skip-chars-forward "a-zA-Z0-9_")
7176 (point)))))
7177
7178 (defun verilog-build-defun-re (str &optional arg)
7179 "Return function/task/module starting with STR as regular expression.
7180 With optional second ARG non-nil, STR is the complete name of the instruction."
7181 (if arg
7182 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
7183 (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
7184
7185 (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
7186 "Function passed to `completing-read', `try-completion' or `all-completions'.
7187 Returns a completion on any function name based on VERILOG-STR prefix. If
7188 VERILOG-PRED is non-nil, it must be a function to be called for every match
7189 to check if this should really be a match. If VERILOG-FLAG is t, the
7190 function returns a list of all possible completions. If it is nil it
7191 returns a string, the longest possible completion, or t if VERILOG-STR is
7192 an exact match. If VERILOG-FLAG is 'lambda, the function returns t if
7193 VERILOG-STR is an exact match, nil otherwise."
7194 (save-excursion
7195 (let ((verilog-all nil)
7196 match)
7197
7198 ;; Set buffer to use for searching labels. This should be set
7199 ;; within functions which use verilog-completions
7200 (set-buffer verilog-buffer-to-use)
7201
7202 (let ((verilog-str verilog-str))
7203 ;; Build regular expression for functions
7204 (if (string= verilog-str "")
7205 (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
7206 (setq verilog-str (verilog-build-defun-re verilog-str)))
7207 (goto-char (point-min))
7208
7209 ;; Build a list of all possible completions
7210 (while (verilog-re-search-forward verilog-str nil t)
7211 (setq match (buffer-substring (match-beginning 2) (match-end 2)))
7212 (if (or (null verilog-pred)
7213 (funcall verilog-pred match))
7214 (setq verilog-all (cons match verilog-all)))))
7215
7216 ;; Now we have built a list of all matches. Give response to caller
7217 (verilog-completion-response))))
7218
7219 (defun verilog-goto-defun ()
7220 "Move to specified Verilog module/interface/task/function.
7221 The default is a name found in the buffer around point.
7222 If search fails, other files are checked based on
7223 `verilog-library-flags'."
7224 (interactive)
7225 (let* ((default (verilog-get-default-symbol))
7226 ;; The following variable is used in verilog-comp-function
7227 (verilog-buffer-to-use (current-buffer))
7228 (label (if (not (string= default ""))
7229 ;; Do completion with default
7230 (completing-read (concat "Goto-Label: (default "
7231 default ") ")
7232 'verilog-comp-defun nil nil "")
7233 ;; There is no default value. Complete without it
7234 (completing-read "Goto-Label: "
7235 'verilog-comp-defun nil nil "")))
7236 pt)
7237 ;; Make sure library paths are correct, in case need to resolve module
7238 (verilog-auto-reeval-locals)
7239 (verilog-getopt-flags)
7240 ;; If there was no response on prompt, use default value
7241 (if (string= label "")
7242 (setq label default))
7243 ;; Goto right place in buffer if label is not an empty string
7244 (or (string= label "")
7245 (progn
7246 (save-excursion
7247 (goto-char (point-min))
7248 (setq pt
7249 (re-search-forward (verilog-build-defun-re label t) nil t)))
7250 (when pt
7251 (goto-char pt)
7252 (beginning-of-line))
7253 pt)
7254 (verilog-goto-defun-file label))))
7255
7256 ;; Eliminate compile warning
7257 (defvar occur-pos-list)
7258
7259 (defun verilog-showscopes ()
7260 "List all scopes in this module."
7261 (interactive)
7262 (let ((buffer (current-buffer))
7263 (linenum 1)
7264 (nlines 0)
7265 (first 1)
7266 (prevpos (point-min))
7267 (final-context-start (make-marker))
7268 (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
7269 (with-output-to-temp-buffer "*Occur*"
7270 (save-excursion
7271 (message (format "Searching for %s ..." regexp))
7272 ;; Find next match, but give up if prev match was at end of buffer.
7273 (while (and (not (= prevpos (point-max)))
7274 (verilog-re-search-forward regexp nil t))
7275 (goto-char (match-beginning 0))
7276 (beginning-of-line)
7277 (save-match-data
7278 (setq linenum (+ linenum (count-lines prevpos (point)))))
7279 (setq prevpos (point))
7280 (goto-char (match-end 0))
7281 (let* ((start (save-excursion
7282 (goto-char (match-beginning 0))
7283 (forward-line (if (< nlines 0) nlines (- nlines)))
7284 (point)))
7285 (end (save-excursion
7286 (goto-char (match-end 0))
7287 (if (> nlines 0)
7288 (forward-line (1+ nlines))
7289 (forward-line 1))
7290 (point)))
7291 (tag (format "%3d" linenum))
7292 (empty (make-string (length tag) ?\ ))
7293 tem)
7294 (save-excursion
7295 (setq tem (make-marker))
7296 (set-marker tem (point))
7297 (set-buffer standard-output)
7298 (setq occur-pos-list (cons tem occur-pos-list))
7299 (or first (zerop nlines)
7300 (insert "--------\n"))
7301 (setq first nil)
7302 (insert-buffer-substring buffer start end)
7303 (backward-char (- end start))
7304 (setq tem (if (< nlines 0) (- nlines) nlines))
7305 (while (> tem 0)
7306 (insert empty ?:)
7307 (forward-line 1)
7308 (setq tem (1- tem)))
7309 (let ((this-linenum linenum))
7310 (set-marker final-context-start
7311 (+ (point) (- (match-end 0) (match-beginning 0))))
7312 (while (< (point) final-context-start)
7313 (if (null tag)
7314 (setq tag (format "%3d" this-linenum)))
7315 (insert tag ?:)))))))
7316 (set-buffer-modified-p nil))))
7317
7318
7319 ;; Highlight helper functions
7320 (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
7321 (defun verilog-within-translate-off ()
7322 "Return point if within translate-off region, else nil."
7323 (and (save-excursion
7324 (re-search-backward
7325 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
7326 nil t))
7327 (equal "off" (match-string 2))
7328 (point)))
7329
7330 (defun verilog-start-translate-off (limit)
7331 "Return point before translate-off directive if before LIMIT, else nil."
7332 (when (re-search-forward
7333 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7334 limit t)
7335 (match-beginning 0)))
7336
7337 (defun verilog-back-to-start-translate-off (limit)
7338 "Return point before translate-off directive if before LIMIT, else nil."
7339 (when (re-search-backward
7340 (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
7341 limit t)
7342 (match-beginning 0)))
7343
7344 (defun verilog-end-translate-off (limit)
7345 "Return point after translate-on directive if before LIMIT, else nil."
7346
7347 (re-search-forward (concat
7348 "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
7349
7350 (defun verilog-match-translate-off (limit)
7351 "Match a translate-off block, setting `match-data' and returning t, else nil.
7352 Bound search by LIMIT."
7353 (when (< (point) limit)
7354 (let ((start (or (verilog-within-translate-off)
7355 (verilog-start-translate-off limit)))
7356 (case-fold-search t))
7357 (when start
7358 (let ((end (or (verilog-end-translate-off limit) limit)))
7359 (set-match-data (list start end))
7360 (goto-char end))))))
7361
7362 (defun verilog-font-lock-match-item (limit)
7363 "Match, and move over, any declaration item after point.
7364 Bound search by LIMIT. Adapted from
7365 `font-lock-match-c-style-declaration-item-and-skip-to-next'."
7366 (condition-case nil
7367 (save-restriction
7368 (narrow-to-region (point-min) limit)
7369 ;; match item
7370 (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
7371 (save-match-data
7372 (goto-char (match-end 1))
7373 ;; move to next item
7374 (if (looking-at "\\(\\s-*,\\)")
7375 (goto-char (match-end 1))
7376 (end-of-line) t))))
7377 (error nil)))
7378
7379
7380 ;; Added by Subbu Meiyappan for Header
7381
7382 (defun verilog-header ()
7383 "Insert a standard Verilog file header.
7384 See also `verilog-sk-header' for an alternative format."
7385 (interactive)
7386 (let ((start (point)))
7387 (insert "\
7388 //-----------------------------------------------------------------------------
7389 // Title : <title>
7390 // Project : <project>
7391 //-----------------------------------------------------------------------------
7392 // File : <filename>
7393 // Author : <author>
7394 // Created : <credate>
7395 // Last modified : <moddate>
7396 //-----------------------------------------------------------------------------
7397 // Description :
7398 // <description>
7399 //-----------------------------------------------------------------------------
7400 // Copyright (c) <copydate> by <company> This model is the confidential and
7401 // proprietary property of <company> and the possession or use of this
7402 // file requires a written license from <company>.
7403 //------------------------------------------------------------------------------
7404 // Modification history :
7405 // <modhist>
7406 //-----------------------------------------------------------------------------
7407
7408 ")
7409 (goto-char start)
7410 (search-forward "<filename>")
7411 (replace-match (buffer-name) t t)
7412 (search-forward "<author>") (replace-match "" t t)
7413 (insert (user-full-name))
7414 (insert " <" (user-login-name) "@" (system-name) ">")
7415 (search-forward "<credate>") (replace-match "" t t)
7416 (verilog-insert-date)
7417 (search-forward "<moddate>") (replace-match "" t t)
7418 (verilog-insert-date)
7419 (search-forward "<copydate>") (replace-match "" t t)
7420 (verilog-insert-year)
7421 (search-forward "<modhist>") (replace-match "" t t)
7422 (verilog-insert-date)
7423 (insert " : created")
7424 (goto-char start)
7425 (let (string)
7426 (setq string (read-string "title: "))
7427 (search-forward "<title>")
7428 (replace-match string t t)
7429 (setq string (read-string "project: " verilog-project))
7430 (setq verilog-project string)
7431 (search-forward "<project>")
7432 (replace-match string t t)
7433 (setq string (read-string "Company: " verilog-company))
7434 (setq verilog-company string)
7435 (search-forward "<company>")
7436 (replace-match string t t)
7437 (search-forward "<company>")
7438 (replace-match string t t)
7439 (search-forward "<company>")
7440 (replace-match string t t)
7441 (search-backward "<description>")
7442 (replace-match "" t t))))
7443
7444 ;; verilog-header Uses the verilog-insert-date function
7445
7446 (defun verilog-insert-date ()
7447 "Insert date from the system."
7448 (interactive)
7449 (if verilog-date-scientific-format
7450 (insert (format-time-string "%Y/%m/%d"))
7451 (insert (format-time-string "%d.%m.%Y"))))
7452
7453 (defun verilog-insert-year ()
7454 "Insert year from the system."
7455 (interactive)
7456 (insert (format-time-string "%Y")))
7457
7458 \f
7459 ;;
7460 ;; Signal list parsing
7461 ;;
7462
7463 ;; Elements of a signal list
7464 ;; Unfortunately we use 'assoc' on this, so can't be a vector
7465 (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
7466 (list name bits comment mem enum signed type multidim modport))
7467 (defsubst verilog-sig-name (sig)
7468 (car sig))
7469 (defsubst verilog-sig-bits (sig)
7470 (nth 1 sig))
7471 (defsubst verilog-sig-comment (sig)
7472 (nth 2 sig))
7473 (defsubst verilog-sig-memory (sig)
7474 (nth 3 sig))
7475 (defsubst verilog-sig-enum (sig)
7476 (nth 4 sig))
7477 (defsubst verilog-sig-signed (sig)
7478 (nth 5 sig))
7479 (defsubst verilog-sig-type (sig)
7480 (nth 6 sig))
7481 (defsubst verilog-sig-type-set (sig type)
7482 (setcar (nthcdr 6 sig) type))
7483 (defsubst verilog-sig-multidim (sig)
7484 (nth 7 sig))
7485 (defsubst verilog-sig-multidim-string (sig)
7486 (if (verilog-sig-multidim sig)
7487 (let ((str "") (args (verilog-sig-multidim sig)))
7488 (while args
7489 (setq str (concat str (car args)))
7490 (setq args (cdr args)))
7491 str)))
7492 (defsubst verilog-sig-modport (sig)
7493 (nth 8 sig))
7494 (defsubst verilog-sig-width (sig)
7495 (verilog-make-width-expression (verilog-sig-bits sig)))
7496
7497 (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
7498 (vector outputs-del outputs-imm temps inputs))
7499 (defsubst verilog-alw-get-outputs-delayed (sigs)
7500 (aref sigs 0))
7501 (defsubst verilog-alw-get-outputs-immediate (sigs)
7502 (aref sigs 1))
7503 (defsubst verilog-alw-get-temps (sigs)
7504 (aref sigs 2))
7505 (defsubst verilog-alw-get-inputs (sigs)
7506 (aref sigs 3))
7507 (defsubst verilog-alw-get-uses-delayed (sigs)
7508 (aref sigs 0))
7509
7510 (defsubst verilog-modport-new (name clockings decls)
7511 (list name clockings decls))
7512 (defsubst verilog-modport-name (sig)
7513 (car sig))
7514 (defsubst verilog-modport-clockings (sig)
7515 (nth 1 sig)) ;; Returns list of names
7516 (defsubst verilog-modport-clockings-add (sig val)
7517 (setcar (nthcdr 1 sig) (cons val (nth 1 sig))))
7518 (defsubst verilog-modport-decls (sig)
7519 (nth 2 sig)) ;; Returns verilog-decls-* structure
7520 (defsubst verilog-modport-decls-set (sig val)
7521 (setcar (nthcdr 2 sig) val))
7522
7523 (defsubst verilog-modi-new (name fob pt type)
7524 (vector name fob pt type))
7525 (defsubst verilog-modi-name (modi)
7526 (aref modi 0))
7527 (defsubst verilog-modi-file-or-buffer (modi)
7528 (aref modi 1))
7529 (defsubst verilog-modi-get-point (modi)
7530 (aref modi 2))
7531 (defsubst verilog-modi-get-type (modi) ;; "module" or "interface"
7532 (aref modi 3))
7533 (defsubst verilog-modi-get-decls (modi)
7534 (verilog-modi-cache-results modi 'verilog-read-decls))
7535 (defsubst verilog-modi-get-sub-decls (modi)
7536 (verilog-modi-cache-results modi 'verilog-read-sub-decls))
7537
7538 ;; Signal reading for given module
7539 ;; Note these all take modi's - as returned from verilog-modi-current
7540 (defsubst verilog-decls-new (out inout in vars modports assigns consts gparams interfaces)
7541 (vector out inout in vars modports assigns consts gparams interfaces))
7542 (defsubst verilog-decls-append (a b)
7543 (cond ((not a) b) ((not b) a)
7544 (t (vector (append (aref a 0) (aref b 0)) (append (aref a 1) (aref b 1))
7545 (append (aref a 2) (aref b 2)) (append (aref a 3) (aref b 3))
7546 (append (aref a 4) (aref b 4)) (append (aref a 5) (aref b 5))
7547 (append (aref a 6) (aref b 6)) (append (aref a 7) (aref b 7))
7548 (append (aref a 8) (aref b 8))))))
7549 (defsubst verilog-decls-get-outputs (decls)
7550 (aref decls 0))
7551 (defsubst verilog-decls-get-inouts (decls)
7552 (aref decls 1))
7553 (defsubst verilog-decls-get-inputs (decls)
7554 (aref decls 2))
7555 (defsubst verilog-decls-get-vars (decls)
7556 (aref decls 3))
7557 (defsubst verilog-decls-get-modports (decls) ;; Also for clocking blocks; contains another verilog-decls struct
7558 (aref decls 4)) ;; Returns verilog-modport* structure
7559 (defsubst verilog-decls-get-assigns (decls)
7560 (aref decls 5))
7561 (defsubst verilog-decls-get-consts (decls)
7562 (aref decls 6))
7563 (defsubst verilog-decls-get-gparams (decls)
7564 (aref decls 7))
7565 (defsubst verilog-decls-get-interfaces (decls)
7566 (aref decls 8))
7567
7568
7569 (defsubst verilog-subdecls-new (out inout in intf intfd)
7570 (vector out inout in intf intfd))
7571 (defsubst verilog-subdecls-get-outputs (subdecls)
7572 (aref subdecls 0))
7573 (defsubst verilog-subdecls-get-inouts (subdecls)
7574 (aref subdecls 1))
7575 (defsubst verilog-subdecls-get-inputs (subdecls)
7576 (aref subdecls 2))
7577 (defsubst verilog-subdecls-get-interfaces (subdecls)
7578 (aref subdecls 3))
7579 (defsubst verilog-subdecls-get-interfaced (subdecls)
7580 (aref subdecls 4))
7581
7582 (defun verilog-signals-from-signame (signame-list)
7583 "Return signals in standard form from SIGNAME-LIST, a simple list of names."
7584 (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
7585 signame-list))
7586
7587 (defun verilog-signals-in (in-list not-list)
7588 "Return list of signals in IN-LIST that are also in NOT-LIST.
7589 Also remove any duplicates in IN-LIST.
7590 Signals must be in standard (base vector) form."
7591 ;; This function is hot, so implemented as O(1)
7592 (cond ((eval-when-compile (fboundp 'make-hash-table))
7593 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7594 (ht-not (make-hash-table :test 'equal :rehash-size 4.0))
7595 out-list)
7596 (while not-list
7597 (puthash (car (car not-list)) t ht-not)
7598 (setq not-list (cdr not-list)))
7599 (while in-list
7600 (when (and (gethash (verilog-sig-name (car in-list)) ht-not)
7601 (not (gethash (verilog-sig-name (car in-list)) ht)))
7602 (setq out-list (cons (car in-list) out-list))
7603 (puthash (verilog-sig-name (car in-list)) t ht))
7604 (setq in-list (cdr in-list)))
7605 (nreverse out-list)))
7606 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7607 (t
7608 (let (out-list)
7609 (while in-list
7610 (if (and (assoc (verilog-sig-name (car in-list)) not-list)
7611 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7612 (setq out-list (cons (car in-list) out-list)))
7613 (setq in-list (cdr in-list)))
7614 (nreverse out-list)))))
7615 ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("C" "")))
7616
7617 (defun verilog-signals-not-in (in-list not-list)
7618 "Return list of signals in IN-LIST that aren't also in NOT-LIST.
7619 Also remove any duplicates in IN-LIST.
7620 Signals must be in standard (base vector) form."
7621 ;; This function is hot, so implemented as O(1)
7622 (cond ((eval-when-compile (fboundp 'make-hash-table))
7623 (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
7624 out-list)
7625 (while not-list
7626 (puthash (car (car not-list)) t ht)
7627 (setq not-list (cdr not-list)))
7628 (while in-list
7629 (when (not (gethash (verilog-sig-name (car in-list)) ht))
7630 (setq out-list (cons (car in-list) out-list))
7631 (puthash (verilog-sig-name (car in-list)) t ht))
7632 (setq in-list (cdr in-list)))
7633 (nreverse out-list)))
7634 ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
7635 (t
7636 (let (out-list)
7637 (while in-list
7638 (if (and (not (assoc (verilog-sig-name (car in-list)) not-list))
7639 (not (assoc (verilog-sig-name (car in-list)) out-list)))
7640 (setq out-list (cons (car in-list) out-list)))
7641 (setq in-list (cdr in-list)))
7642 (nreverse out-list)))))
7643 ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
7644
7645 (defun verilog-signals-memory (in-list)
7646 "Return list of signals in IN-LIST that are memorized (multidimensional)."
7647 (let (out-list)
7648 (while in-list
7649 (if (nth 3 (car in-list))
7650 (setq out-list (cons (car in-list) out-list)))
7651 (setq in-list (cdr in-list)))
7652 out-list))
7653 ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
7654
7655 (defun verilog-signals-sort-compare (a b)
7656 "Compare signal A and B for sorting."
7657 (string< (verilog-sig-name a) (verilog-sig-name b)))
7658
7659 (defun verilog-signals-not-params (in-list)
7660 "Return list of signals in IN-LIST that aren't parameters or numeric constants."
7661 (let (out-list)
7662 (while in-list
7663 (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
7664 (setq out-list (cons (car in-list) out-list)))
7665 (setq in-list (cdr in-list)))
7666 (nreverse out-list)))
7667
7668 (defun verilog-signals-with (func in-list)
7669 "Return IN-LIST with only signals where FUNC passed each signal is true."
7670 (let (out-list)
7671 (while in-list
7672 (when (funcall func (car in-list))
7673 (setq out-list (cons (car in-list) out-list)))
7674 (setq in-list (cdr in-list)))
7675 (nreverse out-list)))
7676
7677 (defun verilog-signals-combine-bus (in-list)
7678 "Return a list of signals in IN-LIST, with buses combined.
7679 Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
7680 (let (combo buswarn
7681 out-list
7682 sig highbit lowbit ; Temp information about current signal
7683 sv-name sv-highbit sv-lowbit ; Details about signal we are forming
7684 sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
7685 sv-modport
7686 bus)
7687 ;; Shove signals so duplicated signals will be adjacent
7688 (setq in-list (sort in-list `verilog-signals-sort-compare))
7689 (while in-list
7690 (setq sig (car in-list))
7691 ;; No current signal; form from existing details
7692 (unless sv-name
7693 (setq sv-name (verilog-sig-name sig)
7694 sv-highbit nil
7695 sv-busstring nil
7696 sv-comment (verilog-sig-comment sig)
7697 sv-memory (verilog-sig-memory sig)
7698 sv-enum (verilog-sig-enum sig)
7699 sv-signed (verilog-sig-signed sig)
7700 sv-type (verilog-sig-type sig)
7701 sv-multidim (verilog-sig-multidim sig)
7702 sv-modport (verilog-sig-modport sig)
7703 combo ""
7704 buswarn ""))
7705 ;; Extract bus details
7706 (setq bus (verilog-sig-bits sig))
7707 (setq bus (and bus (verilog-simplify-range-expression bus)))
7708 (cond ((and bus
7709 (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
7710 (setq highbit (string-to-number (match-string 1 bus))
7711 lowbit (string-to-number
7712 (match-string 2 bus))))
7713 (and (string-match "\\[\\([0-9]+\\)\\]" bus)
7714 (setq highbit (string-to-number (match-string 1 bus))
7715 lowbit highbit))))
7716 ;; Combine bits in bus
7717 (if sv-highbit
7718 (setq sv-highbit (max highbit sv-highbit)
7719 sv-lowbit (min lowbit sv-lowbit))
7720 (setq sv-highbit highbit
7721 sv-lowbit lowbit)))
7722 (bus
7723 ;; String, probably something like `preproc:0
7724 (setq sv-busstring bus)))
7725 ;; Peek ahead to next signal
7726 (setq in-list (cdr in-list))
7727 (setq sig (car in-list))
7728 (cond ((and sig (equal sv-name (verilog-sig-name sig)))
7729 ;; Combine with this signal
7730 (when (and sv-busstring
7731 (not (equal sv-busstring (verilog-sig-bits sig))))
7732 (when nil ;; Debugging
7733 (message (concat "Warning, can't merge into single bus "
7734 sv-name bus
7735 ", the AUTOs may be wrong")))
7736 (setq buswarn ", Couldn't Merge"))
7737 (if (verilog-sig-comment sig) (setq combo ", ..."))
7738 (setq sv-memory (or sv-memory (verilog-sig-memory sig))
7739 sv-enum (or sv-enum (verilog-sig-enum sig))
7740 sv-signed (or sv-signed (verilog-sig-signed sig))
7741 sv-type (or sv-type (verilog-sig-type sig))
7742 sv-multidim (or sv-multidim (verilog-sig-multidim sig))
7743 sv-modport (or sv-modport (verilog-sig-modport sig))))
7744 ;; Doesn't match next signal, add to queue, zero in prep for next
7745 ;; Note sig may also be nil for the last signal in the list
7746 (t
7747 (setq out-list
7748 (cons (verilog-sig-new
7749 sv-name
7750 (or sv-busstring
7751 (if sv-highbit
7752 (concat "[" (int-to-string sv-highbit) ":"
7753 (int-to-string sv-lowbit) "]")))
7754 (concat sv-comment combo buswarn)
7755 sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
7756 out-list)
7757 sv-name nil))))
7758 ;;
7759 out-list))
7760
7761 (defun verilog-sig-tieoff (sig)
7762 "Return tieoff expression for given SIG, with appropriate width.
7763 Tieoff value uses `verilog-active-low-regexp' and
7764 `verilog-auto-reset-widths'."
7765 (concat
7766 (if (and verilog-active-low-regexp
7767 (string-match verilog-active-low-regexp (verilog-sig-name sig)))
7768 "~" "")
7769 (cond ((not verilog-auto-reset-widths)
7770 "0")
7771 ((equal verilog-auto-reset-widths 'unbased)
7772 "'0")
7773 ;; Else presume verilog-auto-reset-widths is true
7774 (t
7775 (let* ((width (verilog-sig-width sig)))
7776 (cond ((not width)
7777 "`0/*NOWIDTH*/")
7778 ((string-match "^[0-9]+$" width)
7779 (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
7780 (t
7781 (concat "{" width "{1'b0}}"))))))))
7782
7783 ;;
7784 ;; Dumping
7785 ;;
7786
7787 (defun verilog-decls-princ (decls &optional header prefix)
7788 "For debug, dump the `verilog-read-decls' structure DECLS."
7789 (when decls
7790 (if header (princ header))
7791 (setq prefix (or prefix ""))
7792 (verilog-signals-princ (verilog-decls-get-outputs decls)
7793 (concat prefix "Outputs:\n") (concat prefix " "))
7794 (verilog-signals-princ (verilog-decls-get-inouts decls)
7795 (concat prefix "Inout:\n") (concat prefix " "))
7796 (verilog-signals-princ (verilog-decls-get-inputs decls)
7797 (concat prefix "Inputs:\n") (concat prefix " "))
7798 (verilog-signals-princ (verilog-decls-get-vars decls)
7799 (concat prefix "Vars:\n") (concat prefix " "))
7800 (verilog-signals-princ (verilog-decls-get-assigns decls)
7801 (concat prefix "Assigns:\n") (concat prefix " "))
7802 (verilog-signals-princ (verilog-decls-get-consts decls)
7803 (concat prefix "Consts:\n") (concat prefix " "))
7804 (verilog-signals-princ (verilog-decls-get-gparams decls)
7805 (concat prefix "Gparams:\n") (concat prefix " "))
7806 (verilog-signals-princ (verilog-decls-get-interfaces decls)
7807 (concat prefix "Interfaces:\n") (concat prefix " "))
7808 (verilog-modport-princ (verilog-decls-get-modports decls)
7809 (concat prefix "Modports:\n") (concat prefix " "))
7810 (princ "\n")))
7811
7812 (defun verilog-signals-princ (signals &optional header prefix)
7813 "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
7814 (when signals
7815 (if header (princ header))
7816 (while signals
7817 (let ((sig (car signals)))
7818 (setq signals (cdr signals))
7819 (princ prefix)
7820 (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
7821 (princ " bits=") (princ (verilog-sig-bits sig))
7822 (princ " cmt=") (princ (verilog-sig-comment sig))
7823 (princ " mem=") (princ (verilog-sig-memory sig))
7824 (princ " enum=") (princ (verilog-sig-enum sig))
7825 (princ " sign=") (princ (verilog-sig-signed sig))
7826 (princ " type=") (princ (verilog-sig-type sig))
7827 (princ " dim=") (princ (verilog-sig-multidim sig))
7828 (princ " modp=") (princ (verilog-sig-modport sig))
7829 (princ "\n")))))
7830
7831 (defun verilog-modport-princ (modports &optional header prefix)
7832 "For debug, dump internal MODPORT structures, with HEADER and PREFIX."
7833 (when modports
7834 (if header (princ header))
7835 (while modports
7836 (let ((sig (car modports)))
7837 (setq modports (cdr modports))
7838 (princ prefix)
7839 (princ "\"") (princ (verilog-modport-name sig)) (princ "\"")
7840 (princ " clockings=") (princ (verilog-modport-clockings sig))
7841 (princ "\n")
7842 (verilog-decls-princ (verilog-modport-decls sig)
7843 (concat prefix " syms:\n")
7844 (concat prefix " "))))))
7845
7846 ;;
7847 ;; Port/Wire/Etc Reading
7848 ;;
7849
7850 (defun verilog-read-inst-backward-name ()
7851 "Internal. Move point back to beginning of inst-name."
7852 (verilog-backward-open-paren)
7853 (let (done)
7854 (while (not done)
7855 (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil) ; ] isn't word boundary
7856 (cond ((looking-at ")")
7857 (verilog-backward-open-paren))
7858 (t (setq done t)))))
7859 (while (looking-at "\\]")
7860 (verilog-backward-open-bracket)
7861 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|\\]\\)" nil nil))
7862 (skip-chars-backward "a-zA-Z0-9`_$"))
7863
7864 (defun verilog-read-inst-module-matcher ()
7865 "Set match data 0 with module_name when point is inside instantiation."
7866 (verilog-read-inst-backward-name)
7867 ;; Skip over instantiation name
7868 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7869 ;; Check for parameterized instantiations
7870 (when (looking-at ")")
7871 (verilog-backward-open-paren)
7872 (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_\$]" nil nil))
7873 (skip-chars-backward "a-zA-Z0-9'_$")
7874 (looking-at "[a-zA-Z0-9`_\$]+")
7875 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7876 (buffer-substring-no-properties (match-beginning 0) (match-end 0))
7877 ;; Caller assumes match-beginning/match-end is still set
7878 )
7879
7880 (defun verilog-read-inst-module ()
7881 "Return module_name when point is inside instantiation."
7882 (save-excursion
7883 (verilog-read-inst-module-matcher)))
7884
7885 (defun verilog-read-inst-name ()
7886 "Return instance_name when point is inside instantiation."
7887 (save-excursion
7888 (verilog-read-inst-backward-name)
7889 (looking-at "[a-zA-Z0-9`_\$]+")
7890 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7891 (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
7892
7893 (defun verilog-read-module-name ()
7894 "Return module name when after its ( or ;."
7895 (save-excursion
7896 (re-search-backward "[(;]")
7897 ;; Due to "module x import y (" we must search for declaration begin
7898 (verilog-re-search-backward-quick verilog-defun-re nil nil)
7899 (goto-char (match-end 0))
7900 (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_\$]+" nil nil)
7901 ;; Important: don't use match string, this must work with Emacs 19 font-lock on
7902 (verilog-symbol-detick
7903 (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
7904
7905 (defun verilog-read-inst-param-value ()
7906 "Return list of parameters and values when point is inside instantiation."
7907 (save-excursion
7908 (verilog-read-inst-backward-name)
7909 ;; Skip over instantiation name
7910 (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_\$]\\|)\\)" nil nil) ; ) isn't word boundary
7911 ;; If there are parameterized instantiations
7912 (when (looking-at ")")
7913 (let ((end-pt (point))
7914 params
7915 param-name paren-beg-pt param-value)
7916 (verilog-backward-open-paren)
7917 (while (verilog-re-search-forward-quick "\\." end-pt t)
7918 (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_\$]\\)" nil nil)
7919 (skip-chars-backward "a-zA-Z0-9'_$")
7920 (looking-at "[a-zA-Z0-9`_\$]+")
7921 (setq param-name (buffer-substring-no-properties
7922 (match-beginning 0) (match-end 0)))
7923 (verilog-re-search-forward-quick "(" nil nil)
7924 (setq paren-beg-pt (point))
7925 (verilog-forward-close-paren)
7926 (setq param-value (verilog-string-remove-spaces
7927 (buffer-substring-no-properties
7928 paren-beg-pt (1- (point)))))
7929 (setq params (cons (list param-name param-value) params)))
7930 params))))
7931
7932 (defun verilog-read-auto-params (num-param &optional max-param)
7933 "Return parameter list inside auto.
7934 Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
7935 (let ((olist))
7936 (save-excursion
7937 ;; /*AUTOPUNT("parameter", "parameter")*/
7938 (backward-sexp 1)
7939 (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
7940 (setq olist (cons (match-string 1) olist))
7941 (goto-char (match-end 0))))
7942 (or (eq nil num-param)
7943 (<= num-param (length olist))
7944 (error "%s: Expected %d parameters" (verilog-point-text) num-param))
7945 (if (eq max-param nil) (setq max-param num-param))
7946 (or (eq nil max-param)
7947 (>= max-param (length olist))
7948 (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
7949 (nreverse olist)))
7950
7951 (defun verilog-read-decls ()
7952 "Compute signal declaration information for the current module at point.
7953 Return an array of [outputs inouts inputs wire reg assign const]."
7954 (let ((end-mod-point (or (verilog-get-end-of-defun t) (point-max)))
7955 (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
7956 in-modport in-clocking ptype ign-prop
7957 sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
7958 sigs-gparam sigs-intf sigs-modports
7959 vec expect-signal keywd newsig rvalue enum io signed typedefed multidim
7960 modport
7961 varstack tmp)
7962 ;;(if dbg (setq dbg (concat dbg (format "\n\nverilog-read-decls START PT %s END %s\n" (point) end-mod-point))))
7963 (save-excursion
7964 (verilog-beg-of-defun-quick)
7965 (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
7966 (while (< (point) end-mod-point)
7967 ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
7968 (cond
7969 ((looking-at "//")
7970 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7971 (setq enum (match-string 2)))
7972 (search-forward "\n"))
7973 ((looking-at "/\\*")
7974 (forward-char 2)
7975 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
7976 (setq enum (match-string 2)))
7977 (or (search-forward "*/")
7978 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
7979 ((looking-at "(\\*")
7980 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
7981 (forward-char 1)
7982 (or (search-forward "*)")
7983 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
7984 ((eq ?\" (following-char))
7985 (or (re-search-forward "[^\\]\"" nil t) ;; don't forward-char first, since we look for a non backslash first
7986 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
7987 ((eq ?\; (following-char))
7988 (when (and in-modport (not (eq in-modport t))) ;; end of a modport declaration
7989 (verilog-modport-decls-set
7990 in-modport
7991 (verilog-decls-new sigs-out sigs-inout sigs-in
7992 nil nil nil nil nil nil))
7993 ;; Pop from varstack to restore state to pre-clocking
7994 (setq tmp (car varstack)
7995 varstack (cdr varstack)
7996 sigs-out (aref tmp 0)
7997 sigs-inout (aref tmp 1)
7998 sigs-in (aref tmp 2)))
7999 (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
8000 v2kargs-ok nil in-modport nil ign-prop nil)
8001 (forward-char 1))
8002 ((eq ?= (following-char))
8003 (setq rvalue t newsig nil)
8004 (forward-char 1))
8005 ((and (eq ?, (following-char))
8006 (eq paren sig-paren))
8007 (setq rvalue nil)
8008 (forward-char 1))
8009 ;; ,'s can occur inside {} & funcs
8010 ((looking-at "[{(]")
8011 (setq paren (1+ paren))
8012 (forward-char 1))
8013 ((looking-at "[})]")
8014 (setq paren (1- paren))
8015 (forward-char 1)
8016 (when (< paren sig-paren)
8017 (setq expect-signal nil rvalue nil))) ; ) that ends variables inside v2k arg list
8018 ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
8019 (goto-char (match-end 0))
8020 (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
8021 (setcar (cdr (cdr (cdr newsig)))
8022 (if (verilog-sig-memory newsig)
8023 (concat (verilog-sig-memory newsig) (match-string 1))
8024 (match-string 1))))
8025 (vec ;; Multidimensional
8026 (setq multidim (cons vec multidim))
8027 (setq vec (verilog-string-replace-matches
8028 "\\s-+" "" nil nil (match-string 1))))
8029 (t ;; Bit width
8030 (setq vec (verilog-string-replace-matches
8031 "\\s-+" "" nil nil (match-string 1))))))
8032 ;; Normal or escaped identifier -- note we remember the \ if escaped
8033 ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8034 (goto-char (match-end 0))
8035 (setq keywd (match-string 1))
8036 (when (string-match "^\\\\" (match-string 1))
8037 (setq keywd (concat keywd " "))) ;; Escaped ID needs space at end
8038 ;; Add any :: package names to same identifier
8039 (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
8040 (goto-char (match-end 0))
8041 (setq keywd (concat keywd "::" (match-string 1)))
8042 (when (string-match "^\\\\" (match-string 1))
8043 (setq keywd (concat keywd " ")))) ;; Escaped ID needs space at end
8044 (cond ((equal keywd "input")
8045 (setq vec nil enum nil rvalue nil newsig nil signed nil
8046 typedefed nil multidim nil ptype nil modport nil
8047 expect-signal 'sigs-in io t sig-paren paren))
8048 ((equal keywd "output")
8049 (setq vec nil enum nil rvalue nil newsig nil signed nil
8050 typedefed nil multidim nil ptype nil modport nil
8051 expect-signal 'sigs-out io t sig-paren paren))
8052 ((equal keywd "inout")
8053 (setq vec nil enum nil rvalue nil newsig nil signed nil
8054 typedefed nil multidim nil ptype nil modport nil
8055 expect-signal 'sigs-inout io t sig-paren paren))
8056 ((equal keywd "parameter")
8057 (setq vec nil enum nil rvalue nil signed nil
8058 typedefed nil multidim nil ptype nil modport nil
8059 expect-signal 'sigs-gparam io t sig-paren paren))
8060 ((member keywd '("wire" "reg" ; Fast
8061 ;; net_type
8062 "tri" "tri0" "tri1" "triand" "trior" "trireg"
8063 "uwire" "wand" "wor"
8064 ;; integer_atom_type
8065 "byte" "shortint" "int" "longint" "integer" "time"
8066 "supply0" "supply1"
8067 ;; integer_vector_type - "reg" above
8068 "bit" "logic"
8069 ;; non_integer_type
8070 "shortreal" "real" "realtime"
8071 ;; data_type
8072 "string" "event" "chandle"))
8073 (cond (io
8074 (setq typedefed
8075 (if typedefed (concat typedefed " " keywd) keywd)))
8076 (t (setq vec nil enum nil rvalue nil signed nil
8077 typedefed nil multidim nil sig-paren paren
8078 expect-signal 'sigs-var modport nil))))
8079 ((equal keywd "assign")
8080 (setq vec nil enum nil rvalue nil signed nil
8081 typedefed nil multidim nil ptype nil modport nil
8082 expect-signal 'sigs-assign sig-paren paren))
8083 ((member keywd '("localparam" "genvar"))
8084 (unless io
8085 (setq vec nil enum nil rvalue nil signed nil
8086 typedefed nil multidim nil ptype nil modport nil
8087 expect-signal 'sigs-const sig-paren paren)))
8088 ((member keywd '("signed" "unsigned"))
8089 (setq signed keywd))
8090 ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
8091 (setq ign-prop t))
8092 ((member keywd '("class" "covergroup" "function"
8093 "property" "randsequence" "sequence" "task"))
8094 (unless ign-prop
8095 (setq functask (1+ functask))))
8096 ((member keywd '("endclass" "endgroup" "endfunction"
8097 "endproperty" "endsequence" "endtask"))
8098 (setq functask (1- functask)))
8099 ((equal keywd "modport")
8100 (setq in-modport t))
8101 ((equal keywd "clocking")
8102 (setq in-clocking t))
8103 ((equal keywd "type")
8104 (setq ptype t))
8105 ;; Ifdef? Ignore name of define
8106 ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8107 (setq rvalue t))
8108 ;; Type?
8109 ((unless ptype
8110 (verilog-typedef-name-p keywd))
8111 (setq typedefed keywd))
8112 ;; Interface with optional modport in v2k arglist?
8113 ;; Skip over parsing modport, and take the interface name as the type
8114 ((and v2kargs-ok
8115 (eq paren 1)
8116 (not rvalue)
8117 (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
8118 (when (match-end 2) (goto-char (match-end 2)))
8119 (setq vec nil enum nil rvalue nil signed nil
8120 typedefed keywd multidim nil ptype nil modport (match-string 2)
8121 newsig nil sig-paren paren
8122 expect-signal 'sigs-intf io t ))
8123 ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
8124 ((looking-at "\\s-*\\.")
8125 (goto-char (match-end 0))
8126 (when (not rvalue)
8127 (setq expect-signal nil)))
8128 ;; "modport <keywd>"
8129 ((and (eq in-modport t)
8130 (not (member keywd verilog-keywords)))
8131 (setq in-modport (verilog-modport-new keywd nil nil))
8132 (setq sigs-modports (cons in-modport sigs-modports))
8133 ;; Push old sig values to stack and point to new signal list
8134 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8135 varstack))
8136 (setq sigs-in nil sigs-inout nil sigs-out nil))
8137 ;; "modport x (clocking <keywd>)"
8138 ((and in-modport in-clocking)
8139 (verilog-modport-clockings-add in-modport keywd)
8140 (setq in-clocking nil))
8141 ;; endclocking
8142 ((and in-clocking
8143 (equal keywd "endclocking"))
8144 (unless (eq in-clocking t)
8145 (verilog-modport-decls-set
8146 in-clocking
8147 (verilog-decls-new sigs-out sigs-inout sigs-in
8148 nil nil nil nil nil nil))
8149 ;; Pop from varstack to restore state to pre-clocking
8150 (setq tmp (car varstack)
8151 varstack (cdr varstack)
8152 sigs-out (aref tmp 0)
8153 sigs-inout (aref tmp 1)
8154 sigs-in (aref tmp 2)))
8155 (setq in-clocking nil))
8156 ;; "clocking <keywd>"
8157 ((and (eq in-clocking t)
8158 (not (member keywd verilog-keywords)))
8159 (setq in-clocking (verilog-modport-new keywd nil nil))
8160 (setq sigs-modports (cons in-clocking sigs-modports))
8161 ;; Push old sig values to stack and point to new signal list
8162 (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
8163 varstack))
8164 (setq sigs-in nil sigs-inout nil sigs-out nil))
8165 ;; New signal, maybe?
8166 ((and expect-signal
8167 (not rvalue)
8168 (eq functask 0)
8169 (not (member keywd verilog-keywords)))
8170 ;; Add new signal to expect-signal's variable
8171 (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
8172 (set expect-signal (cons newsig
8173 (symbol-value expect-signal))))))
8174 (t
8175 (forward-char 1)))
8176 (skip-syntax-forward " "))
8177 ;; Return arguments
8178 (setq tmp (verilog-decls-new (nreverse sigs-out)
8179 (nreverse sigs-inout)
8180 (nreverse sigs-in)
8181 (nreverse sigs-var)
8182 (nreverse sigs-modports)
8183 (nreverse sigs-assign)
8184 (nreverse sigs-const)
8185 (nreverse sigs-gparam)
8186 (nreverse sigs-intf)))
8187 ;;(if dbg (verilog-decls-princ tmp))
8188 tmp)))
8189
8190 (defvar verilog-read-sub-decls-in-interfaced nil
8191 "For `verilog-read-sub-decls', process next signal as under interfaced block.")
8192
8193 (defvar verilog-read-sub-decls-gate-ios nil
8194 "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
8195
8196 (eval-when-compile
8197 ;; Prevent compile warnings; these are let's, not globals
8198 ;; Do not remove the eval-when-compile
8199 ;; - we want an error when we are debugging this code if they are refed.
8200 (defvar sigs-in)
8201 (defvar sigs-inout)
8202 (defvar sigs-out)
8203 (defvar sigs-intf)
8204 (defvar sigs-intfd))
8205
8206 (defun verilog-read-sub-decls-sig (submoddecls comment port sig vec multidim)
8207 "For `verilog-read-sub-decls-line', add a signal."
8208 ;; sig eq t to indicate .name syntax
8209 ;;(message "vrsds: %s(%S)" port sig)
8210 (let ((dotname (eq sig t))
8211 portdata)
8212 (when sig
8213 (setq port (verilog-symbol-detick-denumber port))
8214 (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
8215 (if vec (setq vec (verilog-symbol-detick-denumber vec)))
8216 (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
8217 (unless (or (not sig)
8218 (equal sig "")) ;; Ignore .foo(1'b1) assignments
8219 (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
8220 (equal "inout" verilog-read-sub-decls-gate-ios))
8221 (setq sigs-inout
8222 (cons (verilog-sig-new
8223 sig
8224 (if dotname (verilog-sig-bits portdata) vec)
8225 (concat "To/From " comment)
8226 (verilog-sig-memory portdata)
8227 nil
8228 (verilog-sig-signed portdata)
8229 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8230 (verilog-sig-type portdata))
8231 multidim nil)
8232 sigs-inout)))
8233 ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
8234 (equal "output" verilog-read-sub-decls-gate-ios))
8235 (setq sigs-out
8236 (cons (verilog-sig-new
8237 sig
8238 (if dotname (verilog-sig-bits portdata) vec)
8239 (concat "From " comment)
8240 (verilog-sig-memory portdata)
8241 nil
8242 (verilog-sig-signed portdata)
8243 ;; Though ok in SV, in V2K code, propagating the
8244 ;; "reg" in "output reg" upwards isn't legal.
8245 ;; Also for backwards compatibility we don't propagate
8246 ;; "input wire" upwards.
8247 ;; See also `verilog-signals-edit-wire-reg'.
8248 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8249 (verilog-sig-type portdata))
8250 multidim nil)
8251 sigs-out)))
8252 ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
8253 (equal "input" verilog-read-sub-decls-gate-ios))
8254 (setq sigs-in
8255 (cons (verilog-sig-new
8256 sig
8257 (if dotname (verilog-sig-bits portdata) vec)
8258 (concat "To " comment)
8259 (verilog-sig-memory portdata)
8260 nil
8261 (verilog-sig-signed portdata)
8262 (unless (member (verilog-sig-type portdata) '("wire" "reg"))
8263 (verilog-sig-type portdata))
8264 multidim nil)
8265 sigs-in)))
8266 ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
8267 (setq sigs-intf
8268 (cons (verilog-sig-new
8269 sig
8270 (if dotname (verilog-sig-bits portdata) vec)
8271 (concat "To/From " comment)
8272 (verilog-sig-memory portdata)
8273 nil
8274 (verilog-sig-signed portdata)
8275 (verilog-sig-type portdata)
8276 multidim nil)
8277 sigs-intf)))
8278 ((setq portdata (and verilog-read-sub-decls-in-interfaced
8279 (assoc port (verilog-decls-get-vars submoddecls))))
8280 (setq sigs-intfd
8281 (cons (verilog-sig-new
8282 sig
8283 (if dotname (verilog-sig-bits portdata) vec)
8284 (concat "To/From " comment)
8285 (verilog-sig-memory portdata)
8286 nil
8287 (verilog-sig-signed portdata)
8288 (verilog-sig-type portdata)
8289 multidim nil)
8290 sigs-intf)))
8291 ;; (t -- warning pin isn't defined.) ; Leave for lint tool
8292 )))))
8293
8294 (defun verilog-read-sub-decls-expr (submoddecls comment port expr)
8295 "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
8296 ;;(message "vrsde: '%s'" expr)
8297 ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
8298 (setq expr (verilog-string-replace-matches "/\\*\\(\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
8299 ;; Remove front operators
8300 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8301 ;;
8302 (cond
8303 ;; {..., a, b} requires us to recurse on a,b
8304 ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
8305 ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
8306 (unless verilog-auto-ignore-concat
8307 (let ((mlst (split-string (match-string 1 expr) "[{},]"))
8308 mstr)
8309 (while (setq mstr (pop mlst))
8310 (verilog-read-sub-decls-expr submoddecls comment port mstr)))))
8311 (t
8312 (let (sig vec multidim)
8313 ;; Remove leading reduction operators, etc
8314 (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
8315 ;;(message "vrsde-ptop: '%s'" expr)
8316 (cond ;; Find \signal. Final space is part of escaped signal name
8317 ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
8318 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8319 (setq sig (match-string 1 expr)
8320 expr (substring expr (match-end 0))))
8321 ;; Find signal
8322 ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
8323 ;;(message "vrsde-s: '%s'" (match-string 1 expr))
8324 (setq sig (verilog-string-remove-spaces (match-string 1 expr))
8325 expr (substring expr (match-end 0)))))
8326 ;; Find [vector] or [multi][multi][multi][vector]
8327 (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
8328 ;;(message "vrsde-v: '%s'" (match-string 1 expr))
8329 (when vec (setq multidim (cons vec multidim)))
8330 (setq vec (match-string 1 expr)
8331 expr (substring expr (match-end 0))))
8332 ;; If found signal, and nothing unrecognized, add the signal
8333 ;;(message "vrsde-rem: '%s'" expr)
8334 (when (and sig (string-match "^\\s-*$" expr))
8335 (verilog-read-sub-decls-sig submoddecls comment port sig vec multidim))))))
8336
8337 (defun verilog-read-sub-decls-line (submoddecls comment)
8338 "For `verilog-read-sub-decls', read lines of port defs until none match.
8339 Inserts the list of signals found, using submodi to look up each port."
8340 (let (done port)
8341 (save-excursion
8342 (forward-line 1)
8343 (while (not done)
8344 ;; Get port name
8345 (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
8346 (setq port (match-string 1))
8347 (goto-char (match-end 0)))
8348 ;; .\escaped (
8349 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
8350 (setq port (concat (match-string 1) " ")) ;; escaped id's need trailing space
8351 (goto-char (match-end 0)))
8352 ;; .name
8353 ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
8354 (verilog-read-sub-decls-sig
8355 submoddecls comment (match-string 1) t ; sig==t for .name
8356 nil nil) ; vec multidim
8357 (setq port nil))
8358 ;; .\escaped_name
8359 ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
8360 (verilog-read-sub-decls-sig
8361 submoddecls comment (concat (match-string 1) " ") t ; sig==t for .name
8362 nil nil) ; vec multidim
8363 (setq port nil))
8364 ;; random
8365 ((looking-at "\\s-*\\.[^(]*(")
8366 (setq port nil) ;; skip this line
8367 (goto-char (match-end 0)))
8368 (t
8369 (setq port nil done t))) ;; Unknown, ignore rest of line
8370 ;; Get signal name. Point is at the first-non-space after (
8371 ;; We intentionally ignore (non-escaped) signals with .s in them
8372 ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
8373 (when port
8374 (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
8375 (verilog-read-sub-decls-sig
8376 submoddecls comment port
8377 (verilog-string-remove-spaces (match-string 1)) ; sig
8378 nil nil)) ; vec multidim
8379 ;;
8380 ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
8381 (verilog-read-sub-decls-sig
8382 submoddecls comment port
8383 (verilog-string-remove-spaces (match-string 1)) ; sig
8384 (match-string 2) nil)) ; vec multidim
8385 ;; Fastpath was above looking-at's.
8386 ;; For something more complicated invoke a parser
8387 ((looking-at "[^)]+")
8388 (verilog-read-sub-decls-expr
8389 submoddecls comment port
8390 (buffer-substring
8391 (point) (1- (progn (search-backward "(") ; start at (
8392 (verilog-forward-sexp-ign-cmt 1)
8393 (point)))))))) ; expr
8394 ;;
8395 (forward-line 1)))))
8396
8397 (defun verilog-read-sub-decls-gate (submoddecls comment submod end-inst-point)
8398 "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
8399 Inserts the list of signals found."
8400 (save-excursion
8401 (let ((iolist (cdr (assoc submod verilog-gate-ios))))
8402 (while (< (point) end-inst-point)
8403 ;; Get primitive's signal name, as will never have port, and no trailing )
8404 (cond ((looking-at "//")
8405 (search-forward "\n"))
8406 ((looking-at "/\\*")
8407 (or (search-forward "*/")
8408 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8409 ((looking-at "(\\*")
8410 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8411 (forward-char 1)
8412 (or (search-forward "*)")
8413 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8414 ;; On pins, parse and advance to next pin
8415 ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
8416 ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
8417 (goto-char (match-end 0))
8418 (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
8419 iolist (cdr iolist))
8420 (verilog-read-sub-decls-expr
8421 submoddecls comment "primitive_port"
8422 (match-string 0)))
8423 (t
8424 (forward-char 1)
8425 (skip-syntax-forward " ")))))))
8426
8427 (defun verilog-read-sub-decls ()
8428 "Internally parse signals going to modules under this module.
8429 Return an array of [ outputs inouts inputs ] signals for modules that are
8430 instantiated in this module. For example if declare A A (.B(SIG)) and SIG
8431 is an output, then SIG will be included in the list.
8432
8433 This only works on instantiations created with /*AUTOINST*/ converted by
8434 \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
8435 component library to determine connectivity of the design.
8436
8437 One work around for this problem is to manually create // Inputs and //
8438 Outputs comments above subcell signals, for example:
8439
8440 module ModuleName (
8441 // Outputs
8442 .out (out),
8443 // Inputs
8444 .in (in));"
8445 (save-excursion
8446 (let ((end-mod-point (verilog-get-end-of-defun t))
8447 st-point end-inst-point
8448 ;; below 3 modified by verilog-read-sub-decls-line
8449 sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
8450 (verilog-beg-of-defun-quick)
8451 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
8452 (save-excursion
8453 (goto-char (match-beginning 0))
8454 (unless (verilog-inside-comment-or-string-p)
8455 ;; Attempt to snarf a comment
8456 (let* ((submod (verilog-read-inst-module))
8457 (inst (verilog-read-inst-name))
8458 (subprim (member submod verilog-gate-keywords))
8459 (comment (concat inst " of " submod ".v"))
8460 submodi submoddecls)
8461 (cond
8462 (subprim
8463 (setq submodi `primitive
8464 submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
8465 comment (concat inst " of " submod))
8466 (verilog-backward-open-paren)
8467 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8468 (point))
8469 st-point (point))
8470 (forward-char 1)
8471 (verilog-read-sub-decls-gate submoddecls comment submod end-inst-point))
8472 ;; Non-primitive
8473 (t
8474 (when (setq submodi (verilog-modi-lookup submod t))
8475 (setq submoddecls (verilog-modi-get-decls submodi)
8476 verilog-read-sub-decls-gate-ios nil)
8477 (verilog-backward-open-paren)
8478 (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
8479 (point))
8480 st-point (point))
8481 ;; This could have used a list created by verilog-auto-inst
8482 ;; However I want it to be runnable even on user's manually added signals
8483 (let ((verilog-read-sub-decls-in-interfaced t))
8484 (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
8485 (verilog-read-sub-decls-line submoddecls comment))) ;; Modifies sigs-ifd
8486 (goto-char st-point)
8487 (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
8488 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8489 (goto-char st-point)
8490 (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
8491 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-out
8492 (goto-char st-point)
8493 (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
8494 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-inout
8495 (goto-char st-point)
8496 (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
8497 (verilog-read-sub-decls-line submoddecls comment)) ;; Modifies sigs-in
8498 )))))))
8499 ;; Combine duplicate bits
8500 ;;(setq rr (vector sigs-out sigs-inout sigs-in))
8501 (verilog-subdecls-new
8502 (verilog-signals-combine-bus (nreverse sigs-out))
8503 (verilog-signals-combine-bus (nreverse sigs-inout))
8504 (verilog-signals-combine-bus (nreverse sigs-in))
8505 (verilog-signals-combine-bus (nreverse sigs-intf))
8506 (verilog-signals-combine-bus (nreverse sigs-intfd))))))
8507
8508 (defun verilog-read-inst-pins ()
8509 "Return an array of [ pins ] for the current instantiation at point.
8510 For example if declare A A (.B(SIG)) then B will be included in the list."
8511 (save-excursion
8512 (let ((end-mod-point (point)) ;; presume at /*AUTOINST*/ point
8513 pins pin)
8514 (verilog-backward-open-paren)
8515 (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
8516 (setq pin (match-string 1))
8517 (unless (verilog-inside-comment-or-string-p)
8518 (setq pins (cons (list pin) pins))
8519 (when (looking-at "(")
8520 (verilog-forward-sexp-ign-cmt 1))))
8521 (vector pins))))
8522
8523 (defun verilog-read-arg-pins ()
8524 "Return an array of [ pins ] for the current argument declaration at point."
8525 (save-excursion
8526 (let ((end-mod-point (point)) ;; presume at /*AUTOARG*/ point
8527 pins pin)
8528 (verilog-backward-open-paren)
8529 (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
8530 (setq pin (match-string 1))
8531 (unless (verilog-inside-comment-or-string-p)
8532 (setq pins (cons (list pin) pins))))
8533 (vector pins))))
8534
8535 (defun verilog-read-auto-constants (beg end-mod-point)
8536 "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
8537 ;; Insert new
8538 (save-excursion
8539 (let (sig-list tpl-end-pt)
8540 (goto-char beg)
8541 (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
8542 (if (not (looking-at "\\s *("))
8543 (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
8544 (search-forward "(" end-mod-point)
8545 (setq tpl-end-pt (save-excursion
8546 (backward-char 1)
8547 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8548 (backward-char 1)
8549 (point)))
8550 (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
8551 (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
8552 sig-list)))
8553
8554 (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
8555 (make-variable-buffer-local 'verilog-cache-has-lisp)
8556
8557 (defun verilog-read-auto-lisp-present ()
8558 "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
8559 (save-excursion
8560 (goto-char (point-min))
8561 (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
8562
8563 (defun verilog-read-auto-lisp (start end)
8564 "Look for and evaluate an AUTO_LISP between START and END.
8565 Must call `verilog-read-auto-lisp-present' before this function."
8566 ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
8567 (when verilog-cache-has-lisp
8568 (save-excursion
8569 (goto-char start)
8570 (while (re-search-forward "\\<AUTO_LISP(" end t)
8571 (backward-char)
8572 (let* ((beg-pt (prog1 (point)
8573 (verilog-forward-sexp-cmt 1))) ;; Closing paren
8574 (end-pt (point))
8575 (verilog-in-hooks t))
8576 (eval-region beg-pt end-pt nil))))))
8577
8578 (eval-when-compile
8579 ;; Prevent compile warnings; these are let's, not globals
8580 ;; Do not remove the eval-when-compile
8581 ;; - we want an error when we are debugging this code if they are refed.
8582 (defvar sigs-in)
8583 (defvar sigs-out-d)
8584 (defvar sigs-out-i)
8585 (defvar sigs-out-unk)
8586 (defvar sigs-temp)
8587 (defvar vector-skip-list))
8588
8589 (defun verilog-read-always-signals-recurse
8590 (exit-keywd rvalue temp-next)
8591 "Recursive routine for parentheses/bracket matching.
8592 EXIT-KEYWD is expression to stop at, nil if top level.
8593 RVALUE is true if at right hand side of equal.
8594 IGNORE-NEXT is true to ignore next token, fake from inside case statement."
8595 (let* ((semi-rvalue (equal "endcase" exit-keywd)) ;; true if after a ; we are looking for rvalue
8596 keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
8597 ignore-next)
8598 ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
8599 (while (not (or (eobp) gotend))
8600 (cond
8601 ((looking-at "//")
8602 (search-forward "\n"))
8603 ((looking-at "/\\*")
8604 (or (search-forward "*/")
8605 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8606 ((looking-at "(\\*")
8607 ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
8608 (forward-char 1)
8609 (or (search-forward "*)")
8610 (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
8611 (t (setq keywd (buffer-substring-no-properties
8612 (point)
8613 (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8614 (forward-char 1))
8615 (point)))
8616 sig-last-tolk sig-tolk
8617 sig-tolk nil)
8618 ;;(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))))
8619 (cond
8620 ((equal keywd "\"")
8621 (or (re-search-forward "[^\\]\"" nil t)
8622 (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
8623 ;; else at top level loop, keep parsing
8624 ((and end-else-check (equal keywd "else"))
8625 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
8626 ;; no forward movement, want to see else in lower loop
8627 (setq end-else-check nil))
8628 ;; End at top level loop
8629 ((and end-else-check (looking-at "[^ \t\n\f]"))
8630 ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
8631 (setq gotend t))
8632 ;; Final statement?
8633 ((and exit-keywd (equal keywd exit-keywd))
8634 (setq gotend t)
8635 (forward-char (length keywd)))
8636 ;; Standard tokens...
8637 ((equal keywd ";")
8638 (setq ignore-next nil rvalue semi-rvalue)
8639 ;; Final statement at top level loop?
8640 (when (not exit-keywd)
8641 ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
8642 (setq end-else-check t))
8643 (forward-char 1))
8644 ((equal keywd "'")
8645 (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
8646 (goto-char (match-end 0))
8647 (forward-char 1)))
8648 ((equal keywd ":") ;; Case statement, begin/end label, x?y:z
8649 (cond ((equal "endcase" exit-keywd) ;; case x: y=z; statement next
8650 (setq ignore-next nil rvalue nil))
8651 ((equal "?" exit-keywd) ;; x?y:z rvalue
8652 ) ;; NOP
8653 ((equal "]" exit-keywd) ;; [x:y] rvalue
8654 ) ;; NOP
8655 (got-sig ;; label: statement
8656 (setq ignore-next nil rvalue semi-rvalue got-sig nil))
8657 ((not rvalue) ;; begin label
8658 (setq ignore-next t rvalue nil)))
8659 (forward-char 1))
8660 ((equal keywd "=")
8661 (when got-sig
8662 ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
8663 (set got-list (cons got-sig (symbol-value got-list)))
8664 (setq got-sig nil))
8665 (when (not rvalue)
8666 (if (eq (char-before) ?< )
8667 (setq sigs-out-d (append sigs-out-d sigs-out-unk)
8668 sigs-out-unk nil)
8669 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8670 sigs-out-unk nil)))
8671 (setq ignore-next nil rvalue t)
8672 (forward-char 1))
8673 ((equal keywd "?")
8674 (forward-char 1)
8675 (verilog-read-always-signals-recurse ":" rvalue nil))
8676 ((equal keywd "[")
8677 (forward-char 1)
8678 (verilog-read-always-signals-recurse "]" t nil))
8679 ((equal keywd "(")
8680 (forward-char 1)
8681 (cond (sig-last-tolk ;; Function call; zap last signal
8682 (setq got-sig nil)))
8683 (cond ((equal last-keywd "for")
8684 ;; temp-next: Variables on LHS are lvalues, but generally we want
8685 ;; to ignore them, assuming they are loop increments
8686 (verilog-read-always-signals-recurse ";" nil t)
8687 (verilog-read-always-signals-recurse ";" t nil)
8688 (verilog-read-always-signals-recurse ")" nil nil))
8689 (t (verilog-read-always-signals-recurse ")" t nil))))
8690 ((equal keywd "begin")
8691 (skip-syntax-forward "w_")
8692 (verilog-read-always-signals-recurse "end" nil nil)
8693 ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
8694 (setq ignore-next nil rvalue semi-rvalue)
8695 (if (not exit-keywd) (setq end-else-check t)))
8696 ((member keywd '("case" "casex" "casez"))
8697 (skip-syntax-forward "w_")
8698 (verilog-read-always-signals-recurse "endcase" t nil)
8699 (setq ignore-next nil rvalue semi-rvalue)
8700 (if (not exit-keywd) (setq gotend t))) ;; top level begin/end
8701 ((string-match "^[$`a-zA-Z_]" keywd) ;; not exactly word constituent
8702 (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
8703 (setq ignore-next t))
8704 ((or ignore-next
8705 (member keywd verilog-keywords)
8706 (string-match "^\\$" keywd)) ;; PLI task
8707 (setq ignore-next nil))
8708 (t
8709 (setq keywd (verilog-symbol-detick-denumber keywd))
8710 (when got-sig
8711 (set got-list (cons got-sig (symbol-value got-list)))
8712 ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
8713 )
8714 (setq got-list (cond (temp-next 'sigs-temp)
8715 (rvalue 'sigs-in)
8716 (t 'sigs-out-unk))
8717 got-sig (if (or (not keywd)
8718 (assoc keywd (symbol-value got-list)))
8719 nil (list keywd nil nil))
8720 temp-next nil
8721 sig-tolk t)))
8722 (skip-chars-forward "a-zA-Z0-9$_.%`"))
8723 (t
8724 (forward-char 1)))
8725 ;; End of non-comment token
8726 (setq last-keywd keywd)))
8727 (skip-syntax-forward " "))
8728 ;; Append the final pending signal
8729 (when got-sig
8730 ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
8731 (set got-list (cons got-sig (symbol-value got-list)))
8732 (setq got-sig nil))
8733 ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
8734 ))
8735
8736 (defun verilog-read-always-signals ()
8737 "Parse always block at point and return list of (outputs inout inputs)."
8738 (save-excursion
8739 (let* (;;(dbg "")
8740 sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
8741 (search-forward ")")
8742 (verilog-read-always-signals-recurse nil nil nil)
8743 (setq sigs-out-i (append sigs-out-i sigs-out-unk)
8744 sigs-out-unk nil)
8745 ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*")) (delete-region (point-min) (point-max)) (insert dbg) (setq dbg ""))
8746 ;; Return what was found
8747 (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
8748
8749 (defun verilog-read-instants ()
8750 "Parse module at point and return list of ( ( file instance ) ... )."
8751 (verilog-beg-of-defun-quick)
8752 (let* ((end-mod-point (verilog-get-end-of-defun t))
8753 (state nil)
8754 (instants-list nil))
8755 (save-excursion
8756 (while (< (point) end-mod-point)
8757 ;; Stay at level 0, no comments
8758 (while (progn
8759 (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
8760 (or (> (car state) 0) ; in parens
8761 (nth 5 state) ; comment
8762 ))
8763 (forward-line 1))
8764 (beginning-of-line)
8765 (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
8766 ;;(if (looking-at "^\\(.+\\)$")
8767 (let ((module (match-string 1))
8768 (instant (match-string 2)))
8769 (if (not (member module verilog-keywords))
8770 (setq instants-list (cons (list module instant) instants-list)))))
8771 (forward-line 1)))
8772 instants-list))
8773
8774
8775 (defun verilog-read-auto-template-middle ()
8776 "With point in middle of an AUTO_TEMPLATE, parse it.
8777 Returns REGEXP and list of ( (signal_name connection_name)... )."
8778 (save-excursion
8779 ;; Find beginning
8780 (let ((tpl-regexp "\\([0-9]+\\)")
8781 (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
8782 (templateno 0)
8783 tpl-sig-list tpl-wild-list tpl-end-pt rep)
8784 ;; Parse "REGEXP"
8785 ;; We reserve @"..." for future lisp expressions that evaluate
8786 ;; once-per-AUTOINST
8787 (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
8788 (setq tpl-regexp (match-string 1))
8789 (goto-char (match-end 0)))
8790 (search-forward "(")
8791 ;; Parse lines in the template
8792 (when (or verilog-auto-inst-template-numbers
8793 verilog-auto-template-warn-unused)
8794 (save-excursion
8795 (let ((pre-pt (point)))
8796 (goto-char (point-min))
8797 (while (search-forward "AUTO_TEMPLATE" pre-pt t)
8798 (setq templateno (1+ templateno)))
8799 (while (< (point) pre-pt)
8800 (forward-line 1)
8801 (setq lineno (1+ lineno))))))
8802 (setq tpl-end-pt (save-excursion
8803 (backward-char 1)
8804 (verilog-forward-sexp-cmt 1) ;; Moves to paren that closes argdecl's
8805 (backward-char 1)
8806 (point)))
8807 ;;
8808 (while (< (point) tpl-end-pt)
8809 (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8810 (setq tpl-sig-list
8811 (cons (list
8812 (match-string-no-properties 1)
8813 (match-string-no-properties 2)
8814 templateno lineno)
8815 tpl-sig-list))
8816 (goto-char (match-end 0)))
8817 ;; Regexp form??
8818 ((looking-at
8819 ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
8820 "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
8821 (setq rep (match-string-no-properties 3))
8822 (goto-char (match-end 0))
8823 (setq tpl-wild-list
8824 (cons (list
8825 (concat "^"
8826 (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
8827 (match-string 1))
8828 "$")
8829 rep
8830 templateno lineno)
8831 tpl-wild-list)))
8832 ((looking-at "[ \t\f]+")
8833 (goto-char (match-end 0)))
8834 ((looking-at "\n")
8835 (setq lineno (1+ lineno))
8836 (goto-char (match-end 0)))
8837 ((looking-at "//")
8838 (search-forward "\n")
8839 (setq lineno (1+ lineno)))
8840 ((looking-at "/\\*")
8841 (forward-char 2)
8842 (or (search-forward "*/")
8843 (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
8844 (t
8845 (error "%s: AUTO_TEMPLATE parsing error: %s"
8846 (verilog-point-text)
8847 (progn (looking-at ".*$") (match-string 0))))))
8848 ;; Return
8849 (vector tpl-regexp
8850 (list tpl-sig-list tpl-wild-list)))))
8851
8852 (defun verilog-read-auto-template (module)
8853 "Look for an auto_template for the instantiation of the given MODULE.
8854 If found returns `verilog-read-auto-template-inside' structure."
8855 (save-excursion
8856 ;; Find beginning
8857 (let ((pt (point)))
8858 ;; Note this search is expensive, as we hunt from mod-begin to point
8859 ;; for every instantiation. Likewise in verilog-read-auto-lisp.
8860 ;; So, we look first for an exact string rather than a slow regexp.
8861 ;; Someday we may keep a cache of every template, but this would also
8862 ;; need to record the relative position of each AUTOINST, as multiple
8863 ;; templates exist for each module, and we're inserting lines.
8864 (cond ((or
8865 ;; See also regexp in `verilog-auto-template-lint'
8866 (verilog-re-search-backward-substr
8867 "AUTO_TEMPLATE"
8868 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
8869 ;; Also try forward of this AUTOINST
8870 ;; This is for historical support; this isn't speced as working
8871 (progn
8872 (goto-char pt)
8873 (verilog-re-search-forward-substr
8874 "AUTO_TEMPLATE"
8875 (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
8876 (goto-char (match-end 0))
8877 (verilog-read-auto-template-middle))
8878 ;; If no template found
8879 (t (vector "" nil))))))
8880 ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
8881
8882 (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
8883 (make-variable-buffer-local 'verilog-auto-template-hits)
8884
8885 (defun verilog-read-auto-template-hit (tpl-ass)
8886 "Record that TPL-ASS template from `verilog-read-auto-template' was used."
8887 (when (eval-when-compile (fboundp 'make-hash-table)) ;; else feature not allowed
8888 (when verilog-auto-template-warn-unused
8889 (unless verilog-auto-template-hits
8890 (setq verilog-auto-template-hits
8891 (make-hash-table :test 'equal :rehash-size 4.0)))
8892 (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
8893 verilog-auto-template-hits))))
8894
8895 (defun verilog-set-define (defname defvalue &optional buffer enumname)
8896 "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
8897 Optionally associate it with the specified enumeration ENUMNAME."
8898 (with-current-buffer (or buffer (current-buffer))
8899 (let ((mac (intern (concat "vh-" defname))))
8900 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
8901 ;; Need to define to a constant if no value given
8902 (set (make-local-variable mac)
8903 (if (equal defvalue "") "1" defvalue)))
8904 (if enumname
8905 (let ((enumvar (intern (concat "venum-" enumname))))
8906 ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
8907 (unless (boundp enumvar) (set enumvar nil))
8908 (add-to-list (make-local-variable enumvar) defname)))))
8909
8910 (defun verilog-read-defines (&optional filename recurse subcall)
8911 "Read `defines and parameters for the current file, or optional FILENAME.
8912 If the filename is provided, `verilog-library-flags' will be used to
8913 resolve it. If optional RECURSE is non-nil, recurse through `includes.
8914
8915 Parameters must be simple assignments to constants, or have their own
8916 \"parameter\" label rather than a list of parameters. Thus:
8917
8918 parameter X = 5, Y = 10; // Ok
8919 parameter X = {1'b1, 2'h2}; // Ok
8920 parameter X = {1'b1, 2'h2}, Y = 10; // Bad, make into 2 parameter lines
8921
8922 Defines must be simple text substitutions, one on a line, starting
8923 at the beginning of the line. Any ifdefs or multiline comments around the
8924 define are ignored.
8925
8926 Defines are stored inside Emacs variables using the name vh-{definename}.
8927
8928 This function is useful for setting vh-* variables. The file variables
8929 feature can be used to set defines that `verilog-mode' can see; put at the
8930 *END* of your file something like:
8931
8932 // Local Variables:
8933 // vh-macro:\"macro_definition\"
8934 // End:
8935
8936 If macros are defined earlier in the same file and you want their values,
8937 you can read them automatically (provided `enable-local-eval' is on):
8938
8939 // Local Variables:
8940 // eval:(verilog-read-defines)
8941 // eval:(verilog-read-defines \"group_standard_includes.v\")
8942 // End:
8943
8944 Note these are only read when the file is first visited, you must use
8945 \\[find-alternate-file] RET to have these take effect after editing them!
8946
8947 If you want to disable the \"Process `eval' or hook local variables\"
8948 warning message, you need to add to your init file:
8949
8950 (setq enable-local-eval t)"
8951 (let ((origbuf (current-buffer)))
8952 (save-excursion
8953 (unless subcall (verilog-getopt-flags))
8954 (when filename
8955 (let ((fns (verilog-library-filenames filename (buffer-file-name))))
8956 (if fns
8957 (set-buffer (find-file-noselect (car fns)))
8958 (error (concat (verilog-point-text)
8959 ": Can't find verilog-read-defines file: " filename)))))
8960 (when recurse
8961 (goto-char (point-min))
8962 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
8963 (let ((inc (verilog-string-replace-matches
8964 "\"" "" nil nil (match-string-no-properties 1))))
8965 (unless (verilog-inside-comment-or-string-p)
8966 (verilog-read-defines inc recurse t)))))
8967 ;; Read `defines
8968 ;; note we don't use verilog-re... it's faster this way, and that
8969 ;; function has problems when comments are at the end of the define
8970 (goto-char (point-min))
8971 (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
8972 (let ((defname (match-string-no-properties 1))
8973 (defvalue (match-string-no-properties 2)))
8974 (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
8975 (verilog-set-define defname defvalue origbuf)))
8976 ;; Hack: Read parameters
8977 (goto-char (point-min))
8978 (while (re-search-forward
8979 "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
8980 (let (enumname)
8981 ;; The primary way of getting defines is verilog-read-decls
8982 ;; However, that isn't called yet for included files, so we'll add another scheme
8983 (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
8984 (setq enumname (match-string-no-properties 2)))
8985 (forward-comment 99999)
8986 (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
8987 "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
8988 (verilog-set-define (match-string-no-properties 1)
8989 (match-string-no-properties 2) origbuf enumname)
8990 (goto-char (match-end 0))
8991 (forward-comment 99999)))))))
8992
8993 (defun verilog-read-includes ()
8994 "Read `includes for the current file.
8995 This will find all of the `includes which are at the beginning of lines,
8996 ignoring any ifdefs or multiline comments around them.
8997 `verilog-read-defines' is then performed on the current and each included
8998 file.
8999
9000 It is often useful put at the *END* of your file something like:
9001
9002 // Local Variables:
9003 // eval:(verilog-read-defines)
9004 // eval:(verilog-read-includes)
9005 // End:
9006
9007 Note includes are only read when the file is first visited, you must use
9008 \\[find-alternate-file] RET to have these take effect after editing them!
9009
9010 It is good to get in the habit of including all needed files in each .v
9011 file that needs it, rather than waiting for compile time. This will aid
9012 this process, Verilint, and readability. To prevent defining the same
9013 variable over and over when many modules are compiled together, put a test
9014 around the inside each include file:
9015
9016 foo.v (an include file):
9017 `ifdef _FOO_V // include if not already included
9018 `else
9019 `define _FOO_V
9020 ... contents of file
9021 `endif // _FOO_V"
9022 ;;slow: (verilog-read-defines nil t))
9023 (save-excursion
9024 (verilog-getopt-flags)
9025 (goto-char (point-min))
9026 (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
9027 (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
9028 (verilog-read-defines inc nil t)))))
9029
9030 (defun verilog-read-signals (&optional start end)
9031 "Return a simple list of all possible signals in the file.
9032 Bounded by optional region from START to END. Overly aggressive but fast.
9033 Some macros and such are also found and included. For dinotrace.el."
9034 (let (sigs-all keywd)
9035 (progn;save-excursion
9036 (goto-char (or start (point-min)))
9037 (setq end (or end (point-max)))
9038 (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
9039 (forward-char -1)
9040 (cond
9041 ((looking-at "//")
9042 (search-forward "\n"))
9043 ((looking-at "/\\*")
9044 (search-forward "*/"))
9045 ((looking-at "(\\*")
9046 (or (looking-at "(\\*\\s-*)") ; It's a "always @ (*)"
9047 (search-forward "*)")))
9048 ((eq ?\" (following-char))
9049 (re-search-forward "[^\\]\"")) ;; don't forward-char first, since we look for a non backslash first
9050 ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
9051 (goto-char (match-end 0))
9052 (setq keywd (match-string-no-properties 1))
9053 (or (member keywd verilog-keywords)
9054 (member keywd sigs-all)
9055 (setq sigs-all (cons keywd sigs-all))))
9056 (t (forward-char 1))))
9057 ;; Return list
9058 sigs-all)))
9059
9060 ;;
9061 ;; Argument file parsing
9062 ;;
9063
9064 (defun verilog-getopt (arglist)
9065 "Parse -f, -v etc arguments in ARGLIST list or string."
9066 (unless (listp arglist) (setq arglist (list arglist)))
9067 (let ((space-args '())
9068 arg next-param)
9069 ;; Split on spaces, so users can pass whole command lines
9070 (while arglist
9071 (setq arg (car arglist)
9072 arglist (cdr arglist))
9073 (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
9074 (setq space-args (append space-args
9075 (list (match-string-no-properties 1 arg))))
9076 (setq arg (match-string 2 arg))))
9077 ;; Parse arguments
9078 (while space-args
9079 (setq arg (car space-args)
9080 space-args (cdr space-args))
9081 (cond
9082 ;; Need another arg
9083 ((equal arg "-f")
9084 (setq next-param arg))
9085 ((equal arg "-v")
9086 (setq next-param arg))
9087 ((equal arg "-y")
9088 (setq next-param arg))
9089 ;; +libext+(ext1)+(ext2)...
9090 ((string-match "^\\+libext\\+\\(.*\\)" arg)
9091 (setq arg (match-string 1 arg))
9092 (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
9093 (verilog-add-list-unique `verilog-library-extensions
9094 (match-string 1 arg))
9095 (setq arg (match-string 2 arg))))
9096 ;;
9097 ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; -Ddefine=val
9098 (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ;; -Ddefine
9099 (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ;; +define+val
9100 (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ;; +define+define
9101 (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
9102 ;;
9103 ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ;; +incdir+dir
9104 (string-match "^-I\\(.*\\)" arg)) ;; -Idir
9105 (verilog-add-list-unique `verilog-library-directories
9106 (match-string 1 (substitute-in-file-name arg))))
9107 ;; Ignore
9108 ((equal "+librescan" arg))
9109 ((string-match "^-U\\(.*\\)" arg)) ;; -Udefine
9110 ;; Second parameters
9111 ((equal next-param "-f")
9112 (setq next-param nil)
9113 (verilog-getopt-file (substitute-in-file-name arg)))
9114 ((equal next-param "-v")
9115 (setq next-param nil)
9116 (verilog-add-list-unique `verilog-library-files
9117 (substitute-in-file-name arg)))
9118 ((equal next-param "-y")
9119 (setq next-param nil)
9120 (verilog-add-list-unique `verilog-library-directories
9121 (substitute-in-file-name arg)))
9122 ;; Filename
9123 ((string-match "^[^-+]" arg)
9124 (verilog-add-list-unique `verilog-library-files
9125 (substitute-in-file-name arg)))
9126 ;; Default - ignore; no warning
9127 ))))
9128 ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
9129
9130 (defun verilog-getopt-file (filename)
9131 "Read Verilog options from the specified FILENAME."
9132 (save-excursion
9133 (let ((fns (verilog-library-filenames filename (buffer-file-name)))
9134 (orig-buffer (current-buffer))
9135 line)
9136 (if fns
9137 (set-buffer (find-file-noselect (car fns)))
9138 (error (concat (verilog-point-text)
9139 ": Can't find verilog-getopt-file -f file: " filename)))
9140 (goto-char (point-min))
9141 (while (not (eobp))
9142 (setq line (buffer-substring (point) (point-at-eol)))
9143 (forward-line 1)
9144 (when (string-match "//" line)
9145 (setq line (substring line 0 (match-beginning 0))))
9146 (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
9147 (verilog-getopt line))))))
9148
9149 (defun verilog-getopt-flags ()
9150 "Convert `verilog-library-flags' into standard library variables."
9151 ;; If the flags are local, then all the outputs should be local also
9152 (when (local-variable-p `verilog-library-flags (current-buffer))
9153 (mapc 'make-local-variable '(verilog-library-extensions
9154 verilog-library-directories
9155 verilog-library-files
9156 verilog-library-flags)))
9157 ;; Allow user to customize
9158 (verilog-run-hooks 'verilog-before-getopt-flags-hook)
9159 ;; Process arguments
9160 (verilog-getopt verilog-library-flags)
9161 ;; Allow user to customize
9162 (verilog-run-hooks 'verilog-getopt-flags-hook))
9163
9164 (defun verilog-add-list-unique (varref object)
9165 "Append to VARREF list the given OBJECT,
9166 unless it is already a member of the variable's list."
9167 (unless (member object (symbol-value varref))
9168 (set varref (append (symbol-value varref) (list object))))
9169 varref)
9170 ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
9171
9172 (defun verilog-current-flags ()
9173 "Convert `verilog-library-flags' and similar variables to command line.
9174 Used for __FLAGS__ in `verilog-expand-command'."
9175 (let ((cmd (mapconcat `concat verilog-library-flags " ")))
9176 (when (equal cmd "")
9177 (setq cmd (concat
9178 "+libext+" (mapconcat `concat verilog-library-extensions "+")
9179 (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
9180 verilog-library-directories "")
9181 (mapconcat (lambda (i) (concat " -v " i))
9182 verilog-library-files ""))))
9183 cmd))
9184 ;;(verilog-current-flags)
9185
9186 \f
9187 ;;
9188 ;; Cached directory support
9189 ;;
9190
9191 (defvar verilog-dir-cache-preserving nil
9192 "If set, the directory cache is enabled, and file system changes are ignored.
9193 See `verilog-dir-exists-p' and `verilog-dir-files'.")
9194
9195 ;; If adding new cached variable, add also to verilog-preserve-dir-cache
9196 (defvar verilog-dir-cache-list nil
9197 "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
9198 (defvar verilog-dir-cache-lib-filenames nil
9199 "Cached data for `verilog-library-filenames'.")
9200
9201 (defmacro verilog-preserve-dir-cache (&rest body)
9202 "Execute the BODY forms, allowing directory cache preservation within BODY.
9203 This means that changes inside BODY made to the file system will not be
9204 seen by the `verilog-dir-files' and related functions."
9205 `(let ((verilog-dir-cache-preserving (current-buffer))
9206 verilog-dir-cache-list
9207 verilog-dir-cache-lib-filenames)
9208 (progn ,@body)))
9209
9210 (defun verilog-dir-files (dirname)
9211 "Return all filenames in the DIRNAME directory.
9212 Relative paths depend on the `default-directory'.
9213 Results are cached if inside `verilog-preserve-dir-cache'."
9214 (unless verilog-dir-cache-preserving
9215 (setq verilog-dir-cache-list nil)) ;; Cache disabled
9216 ;; We don't use expand-file-name on the dirname to make key, as it's slow
9217 (let* ((cache-key (list dirname default-directory))
9218 (fass (assoc cache-key verilog-dir-cache-list))
9219 exp-dirname data)
9220 (cond (fass ;; Return data from cache hit
9221 (nth 1 fass))
9222 (t
9223 (setq exp-dirname (expand-file-name dirname)
9224 data (and (file-directory-p exp-dirname)
9225 (directory-files exp-dirname nil nil nil)))
9226 ;; Note we also encache nil for non-existing dirs.
9227 (setq verilog-dir-cache-list (cons (list cache-key data)
9228 verilog-dir-cache-list))
9229 data))))
9230 ;; Miss-and-hit test:
9231 ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
9232 ;; (prin1 (verilog-dir-files ".")) nil)
9233
9234 (defun verilog-dir-file-exists-p (filename)
9235 "Return true if FILENAME exists.
9236 Like `file-exists-p' but results are cached if inside
9237 `verilog-preserve-dir-cache'."
9238 (let* ((dirname (file-name-directory filename))
9239 ;; Correct for file-name-nondirectory returning same if no slash.
9240 (dirnamed (if (or (not dirname) (equal dirname filename))
9241 default-directory dirname))
9242 (flist (verilog-dir-files dirnamed)))
9243 (and flist
9244 (member (file-name-nondirectory filename) flist)
9245 t)))
9246 ;;(verilog-dir-file-exists-p "verilog-mode.el")
9247 ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
9248
9249 \f
9250 ;;
9251 ;; Module name lookup
9252 ;;
9253
9254 (defun verilog-module-inside-filename-p (module filename)
9255 "Return modi if MODULE is specified inside FILENAME, else nil.
9256 Allows version control to check out the file if need be."
9257 (and (or (file-exists-p filename)
9258 (and (fboundp 'vc-backend)
9259 (vc-backend filename)))
9260 (let (modi type)
9261 (with-current-buffer (find-file-noselect filename)
9262 (save-excursion
9263 (goto-char (point-min))
9264 (while (and
9265 ;; It may be tempting to look for verilog-defun-re,
9266 ;; don't, it slows things down a lot!
9267 (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
9268 (setq type (match-string-no-properties 0))
9269 (verilog-re-search-forward-quick "[(;]" nil t))
9270 (if (equal module (verilog-read-module-name))
9271 (setq modi (verilog-modi-new module filename (point) type))))
9272 modi)))))
9273
9274 (defun verilog-is-number (symbol)
9275 "Return true if SYMBOL is number-like."
9276 (or (string-match "^[0-9 \t:]+$" symbol)
9277 (string-match "^[---]*[0-9]+$" symbol)
9278 (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
9279
9280 (defun verilog-symbol-detick (symbol wing-it)
9281 "Return an expanded SYMBOL name without any defines.
9282 If the variable vh-{symbol} is defined, return that value.
9283 If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
9284 (while (and symbol (string-match "^`" symbol))
9285 (setq symbol (substring symbol 1))
9286 (setq symbol
9287 (if (boundp (intern (concat "vh-" symbol)))
9288 ;; Emacs has a bug where boundp on a buffer-local
9289 ;; variable in only one buffer returns t in another.
9290 ;; This can confuse, so check for nil.
9291 (let ((val (eval (intern (concat "vh-" symbol)))))
9292 (if (eq val nil)
9293 (if wing-it symbol nil)
9294 val))
9295 (if wing-it symbol nil))))
9296 symbol)
9297 ;;(verilog-symbol-detick "`mod" nil)
9298
9299 (defun verilog-symbol-detick-denumber (symbol)
9300 "Return SYMBOL with defines converted and any numbers dropped to nil."
9301 (when (string-match "^`" symbol)
9302 ;; This only will work if the define is a simple signal, not
9303 ;; something like a[b]. Sorry, it should be substituted into the parser
9304 (setq symbol
9305 (verilog-string-replace-matches
9306 "\[[^0-9: \t]+\]" "" nil nil
9307 (or (verilog-symbol-detick symbol nil)
9308 (if verilog-auto-sense-defines-constant
9309 "0"
9310 symbol)))))
9311 (if (verilog-is-number symbol)
9312 nil
9313 symbol))
9314
9315 (defun verilog-symbol-detick-text (text)
9316 "Return TEXT without any known defines.
9317 If the variable vh-{symbol} is defined, substitute that value."
9318 (let ((ok t) symbol val)
9319 (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
9320 (setq symbol (match-string 1 text))
9321 ;;(message symbol)
9322 (cond ((and
9323 (boundp (intern (concat "vh-" symbol)))
9324 ;; Emacs has a bug where boundp on a buffer-local
9325 ;; variable in only one buffer returns t in another.
9326 ;; This can confuse, so check for nil.
9327 (setq val (eval (intern (concat "vh-" symbol)))))
9328 (setq text (replace-match val nil nil text)))
9329 (t (setq ok nil)))))
9330 text)
9331 ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
9332
9333 (defun verilog-expand-dirnames (&optional dirnames)
9334 "Return a list of existing directories given a list of wildcarded DIRNAMES.
9335 Or, just the existing dirnames themselves if there are no wildcards."
9336 ;; Note this function is performance critical.
9337 ;; Do not call anything that requires disk access that cannot be cached.
9338 (interactive)
9339 (unless dirnames (error "`verilog-library-directories' should include at least '.'"))
9340 (setq dirnames (reverse dirnames)) ; not nreverse
9341 (let ((dirlist nil)
9342 pattern dirfile dirfiles dirname root filename rest basefile)
9343 (while dirnames
9344 (setq dirname (substitute-in-file-name (car dirnames))
9345 dirnames (cdr dirnames))
9346 (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ;; root
9347 "\\([^/\\]*[*?][^/\\]*\\)" ;; filename with *?
9348 "\\(.*\\)") ;; rest
9349 dirname)
9350 (setq root (match-string 1 dirname)
9351 filename (match-string 2 dirname)
9352 rest (match-string 3 dirname)
9353 pattern filename)
9354 ;; now replace those * and ? with .+ and .
9355 ;; use ^ and /> to get only whole file names
9356 (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
9357 pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
9358 pattern (concat "^" pattern "$")
9359 dirfiles (verilog-dir-files root))
9360 (while dirfiles
9361 (setq basefile (car dirfiles)
9362 dirfile (expand-file-name (concat root basefile rest))
9363 dirfiles (cdr dirfiles))
9364 (if (and (string-match pattern basefile)
9365 ;; Don't allow abc/*/rtl to match abc/rtl via ..
9366 (not (equal basefile "."))
9367 (not (equal basefile ".."))
9368 (file-directory-p dirfile))
9369 (setq dirlist (cons dirfile dirlist)))))
9370 ;; Defaults
9371 (t
9372 (if (file-directory-p dirname)
9373 (setq dirlist (cons dirname dirlist))))))
9374 dirlist))
9375 ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
9376
9377 (defun verilog-library-filenames (filename &optional current check-ext)
9378 "Return a search path to find the given FILENAME or module name.
9379 Uses the optional CURRENT filename or variable `buffer-file-name', plus
9380 `verilog-library-directories' and `verilog-library-extensions'
9381 variables to build the path. With optional CHECK-EXT also check
9382 `verilog-library-extensions'."
9383 (unless current (setq current (buffer-file-name)))
9384 (unless verilog-dir-cache-preserving
9385 (setq verilog-dir-cache-lib-filenames nil))
9386 (let* ((cache-key (list filename current check-ext))
9387 (fass (assoc cache-key verilog-dir-cache-lib-filenames))
9388 chkdirs chkdir chkexts fn outlist)
9389 (cond (fass ;; Return data from cache hit
9390 (nth 1 fass))
9391 (t
9392 ;; Note this expand can't be easily cached, as we need to
9393 ;; pick up buffer-local variables for newly read sub-module files
9394 (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
9395 (while chkdirs
9396 (setq chkdir (expand-file-name (car chkdirs)
9397 (file-name-directory current))
9398 chkexts (if check-ext verilog-library-extensions `("")))
9399 (while chkexts
9400 (setq fn (expand-file-name (concat filename (car chkexts))
9401 chkdir))
9402 ;;(message "Check for %s" fn)
9403 (if (verilog-dir-file-exists-p fn)
9404 (setq outlist (cons (expand-file-name
9405 fn (file-name-directory current))
9406 outlist)))
9407 (setq chkexts (cdr chkexts)))
9408 (setq chkdirs (cdr chkdirs)))
9409 (setq outlist (nreverse outlist))
9410 (setq verilog-dir-cache-lib-filenames
9411 (cons (list cache-key outlist)
9412 verilog-dir-cache-lib-filenames))
9413 outlist))))
9414
9415 (defun verilog-module-filenames (module current)
9416 "Return a search path to find the given MODULE name.
9417 Uses the CURRENT filename, `verilog-library-extensions',
9418 `verilog-library-directories' and `verilog-library-files'
9419 variables to build the path."
9420 ;; Return search locations for it
9421 (append (list current) ; first, current buffer
9422 (verilog-library-filenames module current t)
9423 verilog-library-files)) ; finally, any libraries
9424
9425 ;;
9426 ;; Module Information
9427 ;;
9428 ;; Many of these functions work on "modi" a module information structure
9429 ;; A modi is: [module-name-string file-name begin-point]
9430
9431 (defvar verilog-cache-enabled t
9432 "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
9433
9434 (defvar verilog-modi-cache-list nil
9435 "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
9436 For speeding up verilog-modi-get-* commands.
9437 Buffer-local.")
9438 (make-variable-buffer-local 'verilog-modi-cache-list)
9439
9440 (defvar verilog-modi-cache-preserve-tick nil
9441 "Modification tick after which the cache is still considered valid.
9442 Use `verilog-preserve-modi-cache' to set it.")
9443 (defvar verilog-modi-cache-preserve-buffer nil
9444 "Modification tick after which the cache is still considered valid.
9445 Use `verilog-preserve-modi-cache' to set it.")
9446 (defvar verilog-modi-cache-current-enable nil
9447 "Non-nil means allow caching `verilog-modi-current', set by let().")
9448 (defvar verilog-modi-cache-current nil
9449 "Currently active `verilog-modi-current', if any, set by let().")
9450 (defvar verilog-modi-cache-current-max nil
9451 "Current endmodule point for `verilog-modi-cache-current', if any.")
9452
9453 (defun verilog-modi-current ()
9454 "Return the modi structure for the module currently at point, possibly cached."
9455 (cond ((and verilog-modi-cache-current
9456 (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
9457 (<= (point) verilog-modi-cache-current-max))
9458 ;; Slow assertion, for debugging the cache:
9459 ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
9460 verilog-modi-cache-current)
9461 (verilog-modi-cache-current-enable
9462 (setq verilog-modi-cache-current (verilog-modi-current-get)
9463 verilog-modi-cache-current-max
9464 ;; The cache expires when we pass "endmodule" as then the
9465 ;; current modi may change to the next module
9466 ;; This relies on the AUTOs generally inserting, not deleting text
9467 (save-excursion
9468 (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
9469 verilog-modi-cache-current)
9470 (t
9471 (verilog-modi-current-get))))
9472
9473 (defun verilog-modi-current-get ()
9474 "Return the modi structure for the module currently at point."
9475 (let* (name type pt)
9476 ;; read current module's name
9477 (save-excursion
9478 (verilog-re-search-backward-quick verilog-defun-re nil nil)
9479 (setq type (match-string-no-properties 0))
9480 (verilog-re-search-forward-quick "(" nil nil)
9481 (setq name (verilog-read-module-name))
9482 (setq pt (point)))
9483 ;; return modi - note this vector built two places
9484 (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
9485
9486 (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
9487 (make-variable-buffer-local 'verilog-modi-lookup-cache)
9488 (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
9489 (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
9490
9491 (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
9492 "Find the file and point at which MODULE is defined.
9493 If ALLOW-CACHE is set, check and remember cache of previous lookups.
9494 Return modi if successful, else print message unless IGNORE-ERROR is true."
9495 (let* ((current (or (buffer-file-name) (current-buffer)))
9496 modi)
9497 ;; Check cache
9498 ;;(message "verilog-modi-lookup: %s" module)
9499 (cond ((and verilog-modi-lookup-cache
9500 verilog-cache-enabled
9501 allow-cache
9502 (setq modi (gethash module verilog-modi-lookup-cache))
9503 (equal verilog-modi-lookup-last-current current)
9504 ;; Iff hit is in current buffer, then tick must match
9505 (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
9506 (not (equal current (verilog-modi-file-or-buffer modi)))))
9507 ;;(message "verilog-modi-lookup: HIT %S" modi)
9508 modi)
9509 ;; Miss
9510 (t (let* ((realname (verilog-symbol-detick module t))
9511 (orig-filenames (verilog-module-filenames realname current))
9512 (filenames orig-filenames)
9513 mif)
9514 (while (and filenames (not mif))
9515 (if (not (setq mif (verilog-module-inside-filename-p realname (car filenames))))
9516 (setq filenames (cdr filenames))))
9517 ;; mif has correct form to become later elements of modi
9518 (cond (mif (setq modi mif))
9519 (t (setq modi nil)
9520 (or ignore-error
9521 (error (concat (verilog-point-text)
9522 ": Can't locate " module " module definition"
9523 (if (not (equal module realname))
9524 (concat " (Expanded macro to " realname ")")
9525 "")
9526 "\n Check the verilog-library-directories variable."
9527 "\n I looked in (if not listed, doesn't exist):\n\t"
9528 (mapconcat 'concat orig-filenames "\n\t"))))))
9529 (when (eval-when-compile (fboundp 'make-hash-table))
9530 (unless verilog-modi-lookup-cache
9531 (setq verilog-modi-lookup-cache
9532 (make-hash-table :test 'equal :rehash-size 4.0)))
9533 (puthash module modi verilog-modi-lookup-cache))
9534 (setq verilog-modi-lookup-last-current current
9535 verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
9536 modi))
9537
9538 (defun verilog-modi-filename (modi)
9539 "Filename of MODI, or name of buffer if it's never been saved."
9540 (if (bufferp (verilog-modi-file-or-buffer modi))
9541 (or (buffer-file-name (verilog-modi-file-or-buffer modi))
9542 (buffer-name (verilog-modi-file-or-buffer modi)))
9543 (verilog-modi-file-or-buffer modi)))
9544
9545 (defun verilog-modi-goto (modi)
9546 "Move point/buffer to specified MODI."
9547 (or modi (error "Passed unfound modi to goto, check earlier"))
9548 (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
9549 (verilog-modi-file-or-buffer modi)
9550 (find-file-noselect (verilog-modi-file-or-buffer modi))))
9551 (or (equal major-mode `verilog-mode) ;; Put into Verilog mode to get syntax
9552 (verilog-mode))
9553 (goto-char (verilog-modi-get-point modi)))
9554
9555 (defun verilog-goto-defun-file (module)
9556 "Move point to the file at which a given MODULE is defined."
9557 (interactive "sGoto File for Module: ")
9558 (let* ((modi (verilog-modi-lookup module nil)))
9559 (when modi
9560 (verilog-modi-goto modi)
9561 (switch-to-buffer (current-buffer)))))
9562
9563 (defun verilog-modi-cache-results (modi function)
9564 "Run on MODI the given FUNCTION. Locate the module in a file.
9565 Cache the output of function so next call may have faster access."
9566 (let (fass)
9567 (save-excursion ;; Cache is buffer-local so can't avoid this.
9568 (verilog-modi-goto modi)
9569 (if (and (setq fass (assoc (list modi function)
9570 verilog-modi-cache-list))
9571 ;; Destroy caching when incorrect; Modified or file changed
9572 (not (and verilog-cache-enabled
9573 (or (equal (buffer-chars-modified-tick) (nth 1 fass))
9574 (and verilog-modi-cache-preserve-tick
9575 (<= verilog-modi-cache-preserve-tick (nth 1 fass))
9576 (equal verilog-modi-cache-preserve-buffer (current-buffer))))
9577 (equal (visited-file-modtime) (nth 2 fass)))))
9578 (setq verilog-modi-cache-list nil
9579 fass nil))
9580 (cond (fass
9581 ;; Return data from cache hit
9582 (nth 3 fass))
9583 (t
9584 ;; Read from file
9585 ;; Clear then restore any highlighting to make emacs19 happy
9586 (let (func-returns)
9587 (verilog-save-font-mods
9588 (setq func-returns (funcall function)))
9589 ;; Cache for next time
9590 (setq verilog-modi-cache-list
9591 (cons (list (list modi function)
9592 (buffer-chars-modified-tick)
9593 (visited-file-modtime)
9594 func-returns)
9595 verilog-modi-cache-list))
9596 func-returns))))))
9597
9598 (defun verilog-modi-cache-add (modi function element sig-list)
9599 "Add function return results to the module cache.
9600 Update MODI's cache for given FUNCTION so that the return ELEMENT of that
9601 function now contains the additional SIG-LIST parameters."
9602 (let (fass)
9603 (save-excursion
9604 (verilog-modi-goto modi)
9605 (if (setq fass (assoc (list modi function)
9606 verilog-modi-cache-list))
9607 (let ((func-returns (nth 3 fass)))
9608 (aset func-returns element
9609 (append sig-list (aref func-returns element))))))))
9610
9611 (defmacro verilog-preserve-modi-cache (&rest body)
9612 "Execute the BODY forms, allowing cache preservation within BODY.
9613 This means that changes to the buffer will not result in the cache being
9614 flushed. If the changes affect the modsig state, they must call the
9615 modsig-cache-add-* function, else the results of later calls may be
9616 incorrect. Without this, changes are assumed to be adding/removing signals
9617 and invalidating the cache."
9618 `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
9619 (verilog-modi-cache-preserve-buffer (current-buffer)))
9620 (progn ,@body)))
9621
9622
9623 (defun verilog-modi-modport-lookup-one (modi name &optional ignore-error)
9624 "Given a MODI, return the declarations related to the given modport NAME."
9625 ;; Recursive routine - see below
9626 (let* ((realname (verilog-symbol-detick name t))
9627 (modport (assoc name (verilog-decls-get-modports (verilog-modi-get-decls modi)))))
9628 (or modport ignore-error
9629 (error (concat (verilog-point-text)
9630 ": Can't locate " name " modport definition"
9631 (if (not (equal name realname))
9632 (concat " (Expanded macro to " realname ")")
9633 ""))))
9634 (let* ((decls (verilog-modport-decls modport))
9635 (clks (verilog-modport-clockings modport)))
9636 ;; Now expand any clocking's
9637 (while clks
9638 (setq decls (verilog-decls-append
9639 decls
9640 (verilog-modi-modport-lookup-one modi (car clks) ignore-error)))
9641 (setq clks (cdr clks)))
9642 decls)))
9643
9644 (defun verilog-modi-modport-lookup (modi name-re &optional ignore-error)
9645 "Given a MODI, return the declarations related to the given modport NAME-RE.
9646 If the modport points to any clocking blocks, expand the signals to include
9647 those clocking block's signals."
9648 ;; Recursive routine - see below
9649 (let* ((mod-decls (verilog-modi-get-decls modi))
9650 (clks (verilog-decls-get-modports mod-decls))
9651 (name-re (concat "^" name-re "$"))
9652 (decls (verilog-decls-new nil nil nil nil nil nil nil nil nil)))
9653 ;; Pull in all modports
9654 (while clks
9655 (when (string-match name-re (verilog-modport-name (car clks)))
9656 (setq decls (verilog-decls-append
9657 decls
9658 (verilog-modi-modport-lookup-one modi (verilog-modport-name (car clks)) ignore-error))))
9659 (setq clks (cdr clks)))
9660 decls))
9661
9662 (defun verilog-signals-matching-enum (in-list enum)
9663 "Return all signals in IN-LIST matching the given ENUM."
9664 (let (out-list)
9665 (while in-list
9666 (if (equal (verilog-sig-enum (car in-list)) enum)
9667 (setq out-list (cons (car in-list) out-list)))
9668 (setq in-list (cdr in-list)))
9669 ;; New scheme
9670 (let* ((enumvar (intern (concat "venum-" enum)))
9671 (enumlist (and (boundp enumvar) (eval enumvar))))
9672 (while enumlist
9673 (add-to-list 'out-list (list (car enumlist)))
9674 (setq enumlist (cdr enumlist))))
9675 (nreverse out-list)))
9676
9677 (defun verilog-signals-matching-regexp (in-list regexp)
9678 "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
9679 (if (or (not regexp) (equal regexp ""))
9680 in-list
9681 (let (out-list)
9682 (while in-list
9683 (if (string-match regexp (verilog-sig-name (car in-list)))
9684 (setq out-list (cons (car in-list) out-list)))
9685 (setq in-list (cdr in-list)))
9686 (nreverse out-list))))
9687
9688 (defun verilog-signals-not-matching-regexp (in-list regexp)
9689 "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
9690 (if (or (not regexp) (equal regexp ""))
9691 in-list
9692 (let (out-list)
9693 (while in-list
9694 (if (not (string-match regexp (verilog-sig-name (car in-list))))
9695 (setq out-list (cons (car in-list) out-list)))
9696 (setq in-list (cdr in-list)))
9697 (nreverse out-list))))
9698
9699 (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
9700 "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
9701 if non-nil."
9702 (if (or (not regexp) (equal regexp ""))
9703 in-list
9704 (let (out-list to-match)
9705 (while in-list
9706 ;; Note verilog-insert-one-definition matches on this order
9707 (setq to-match (concat
9708 decl-type
9709 " " (verilog-sig-signed (car in-list))
9710 " " (verilog-sig-multidim (car in-list))
9711 (verilog-sig-bits (car in-list))))
9712 (if (string-match regexp to-match)
9713 (setq out-list (cons (car in-list) out-list)))
9714 (setq in-list (cdr in-list)))
9715 (nreverse out-list))))
9716
9717 (defun verilog-signals-edit-wire-reg (in-list)
9718 "Return all signals in IN-LIST with wire/reg data types made blank."
9719 (mapcar (lambda (sig)
9720 (when (member (verilog-sig-type sig) '("wire" "reg"))
9721 (verilog-sig-type-set sig nil))
9722 sig) in-list))
9723
9724 ;; Combined
9725 (defun verilog-decls-get-signals (decls)
9726 "Return all declared signals in DECLS, excluding 'assign' statements."
9727 (append
9728 (verilog-decls-get-outputs decls)
9729 (verilog-decls-get-inouts decls)
9730 (verilog-decls-get-inputs decls)
9731 (verilog-decls-get-vars decls)
9732 (verilog-decls-get-consts decls)
9733 (verilog-decls-get-gparams decls)))
9734
9735 (defun verilog-decls-get-ports (decls)
9736 (append
9737 (verilog-decls-get-outputs decls)
9738 (verilog-decls-get-inouts decls)
9739 (verilog-decls-get-inputs decls)))
9740
9741 (defun verilog-decls-get-iovars (decls)
9742 (append
9743 (verilog-decls-get-vars decls)
9744 (verilog-decls-get-outputs decls)
9745 (verilog-decls-get-inouts decls)
9746 (verilog-decls-get-inputs decls)))
9747
9748 (defsubst verilog-modi-cache-add-outputs (modi sig-list)
9749 (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
9750 (defsubst verilog-modi-cache-add-inouts (modi sig-list)
9751 (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
9752 (defsubst verilog-modi-cache-add-inputs (modi sig-list)
9753 (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
9754 (defsubst verilog-modi-cache-add-vars (modi sig-list)
9755 (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
9756 (defsubst verilog-modi-cache-add-gparams (modi sig-list)
9757 (verilog-modi-cache-add modi 'verilog-read-decls 7 sig-list))
9758
9759 \f
9760 ;;
9761 ;; Auto creation utilities
9762 ;;
9763
9764 (defun verilog-auto-re-search-do (search-for func)
9765 "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
9766 (goto-char (point-min))
9767 (while (verilog-re-search-forward-quick search-for nil t)
9768 (funcall func)))
9769
9770 (defun verilog-insert-one-definition (sig type indent-pt)
9771 "Print out a definition for SIG of the given TYPE,
9772 with appropriate INDENT-PT indentation."
9773 (indent-to indent-pt)
9774 ;; Note verilog-signals-matching-dir-re matches on this order
9775 (insert type)
9776 (when (verilog-sig-modport sig)
9777 (insert "." (verilog-sig-modport sig)))
9778 (when (verilog-sig-signed sig)
9779 (insert " " (verilog-sig-signed sig)))
9780 (when (verilog-sig-multidim sig)
9781 (insert " " (verilog-sig-multidim-string sig)))
9782 (when (verilog-sig-bits sig)
9783 (insert " " (verilog-sig-bits sig)))
9784 (indent-to (max 24 (+ indent-pt 16)))
9785 (unless (= (char-syntax (preceding-char)) ?\ )
9786 (insert " ")) ; Need space between "]name" if indent-to did nothing
9787 (insert (verilog-sig-name sig))
9788 (when (verilog-sig-memory sig)
9789 (insert " " (verilog-sig-memory sig))))
9790
9791 (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
9792 "Print out a definition for MODI's list of SIGS of the given DIRECTION,
9793 with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
9794 format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
9795 When MODI is non-null, also add to modi-cache, for tracking."
9796 (when modi
9797 (cond ((equal direction "wire")
9798 (verilog-modi-cache-add-vars modi sigs))
9799 ((equal direction "reg")
9800 (verilog-modi-cache-add-vars modi sigs))
9801 ((equal direction "output")
9802 (verilog-modi-cache-add-outputs modi sigs)
9803 (when verilog-auto-declare-nettype
9804 (verilog-modi-cache-add-vars modi sigs)))
9805 ((equal direction "input")
9806 (verilog-modi-cache-add-inputs modi sigs)
9807 (when verilog-auto-declare-nettype
9808 (verilog-modi-cache-add-vars modi sigs)))
9809 ((equal direction "inout")
9810 (verilog-modi-cache-add-inouts modi sigs)
9811 (when verilog-auto-declare-nettype
9812 (verilog-modi-cache-add-vars modi sigs)))
9813 ((equal direction "interface"))
9814 ((equal direction "parameter")
9815 (verilog-modi-cache-add-gparams modi sigs))
9816 (t
9817 (error "Unsupported verilog-insert-definition direction: %s" direction))))
9818 (or dont-sort
9819 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
9820 (while sigs
9821 (let ((sig (car sigs)))
9822 (verilog-insert-one-definition
9823 sig
9824 ;; Want "type x" or "output type x", not "wire type x"
9825 (cond ((or (verilog-sig-type sig)
9826 verilog-auto-wire-type)
9827 (concat
9828 (when (member direction '("input" "output" "inout"))
9829 (concat direction " "))
9830 (or (verilog-sig-type sig)
9831 verilog-auto-wire-type)))
9832 ((and verilog-auto-declare-nettype
9833 (member direction '("input" "output" "inout")))
9834 (concat direction " " verilog-auto-declare-nettype))
9835 (t
9836 direction))
9837 indent-pt)
9838 (insert (if v2k "," ";"))
9839 (if (or (not (verilog-sig-comment sig))
9840 (equal "" (verilog-sig-comment sig)))
9841 (insert "\n")
9842 (indent-to (max 48 (+ indent-pt 40)))
9843 (verilog-insert "// " (verilog-sig-comment sig) "\n"))
9844 (setq sigs (cdr sigs)))))
9845
9846 (eval-when-compile
9847 (if (not (boundp 'indent-pt))
9848 (defvar indent-pt nil "Local used by insert-indent")))
9849
9850 (defun verilog-insert-indent (&rest stuff)
9851 "Indent to position stored in local `indent-pt' variable, then insert STUFF.
9852 Presumes that any newlines end a list element."
9853 (let ((need-indent t))
9854 (while stuff
9855 (if need-indent (indent-to indent-pt))
9856 (setq need-indent nil)
9857 (verilog-insert (car stuff))
9858 (setq need-indent (string-match "\n$" (car stuff))
9859 stuff (cdr stuff)))))
9860 ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
9861
9862 (defun verilog-forward-or-insert-line ()
9863 "Move forward a line, unless at EOB, then insert a newline."
9864 (if (eobp) (insert "\n")
9865 (forward-line)))
9866
9867 (defun verilog-repair-open-comma ()
9868 "Insert comma if previous argument is other than an open parenthesis or endif."
9869 ;; We can't just search backward for ) as it might be inside another expression.
9870 ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
9871 (save-excursion
9872 (verilog-backward-syntactic-ws-quick)
9873 (when (and (not (save-excursion ;; Not beginning (, or existing ,
9874 (backward-char 1)
9875 (looking-at "[(,]")))
9876 (not (save-excursion ;; Not `endif, or user define
9877 (backward-char 1)
9878 (skip-chars-backward "[a-zA-Z0-9_`]")
9879 (looking-at "`"))))
9880 (insert ","))))
9881
9882 (defun verilog-repair-close-comma ()
9883 "If point is at a comma followed by a close parenthesis, fix it.
9884 This repairs those mis-inserted by an AUTOARG."
9885 ;; It would be much nicer if Verilog allowed extra commas like Perl does!
9886 (save-excursion
9887 (verilog-forward-close-paren)
9888 (backward-char 1)
9889 (verilog-backward-syntactic-ws-quick)
9890 (backward-char 1)
9891 (when (looking-at ",")
9892 (delete-char 1))))
9893
9894 (defun verilog-get-list (start end)
9895 "Return the elements of a comma separated list between START and END."
9896 (interactive)
9897 (let ((my-list (list))
9898 my-string)
9899 (save-excursion
9900 (while (< (point) end)
9901 (when (re-search-forward "\\([^,{]+\\)" end t)
9902 (setq my-string (verilog-string-remove-spaces (match-string 1)))
9903 (setq my-list (nconc my-list (list my-string) ))
9904 (goto-char (match-end 0))))
9905 my-list)))
9906
9907 (defun verilog-make-width-expression (range-exp)
9908 "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
9909 ;; strip off the []
9910 (cond ((not range-exp)
9911 "1")
9912 (t
9913 (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
9914 (setq range-exp (match-string 1 range-exp)))
9915 (cond ((not range-exp)
9916 "1")
9917 ;; [#:#] We can compute a numeric result
9918 ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
9919 range-exp)
9920 (int-to-string
9921 (1+ (abs (- (string-to-number (match-string 1 range-exp))
9922 (string-to-number (match-string 2 range-exp)))))))
9923 ;; [PARAM-1:0] can just return PARAM
9924 ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
9925 (match-string 1 range-exp))
9926 ;; [arbitrary] need math
9927 ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
9928 (concat "(1+(" (match-string 1 range-exp) ")"
9929 (if (equal "0" (match-string 2 range-exp))
9930 "" ;; Don't bother with -(0)
9931 (concat "-(" (match-string 2 range-exp) ")"))
9932 ")"))
9933 (t nil)))))
9934 ;;(verilog-make-width-expression "`A:`B")
9935
9936 (defun verilog-simplify-range-expression (expr)
9937 "Return a simplified range expression with constants eliminated from EXPR."
9938 ;; Note this is always called with brackets; ie [z] or [z:z]
9939 (if (not (string-match "[---+*()]" expr))
9940 expr ;; short-circuit
9941 (let ((out expr)
9942 (last-pass ""))
9943 (while (not (equal last-pass out))
9944 (setq last-pass out)
9945 ;; Prefix regexp needs beginning of match, or some symbol of
9946 ;; lesser or equal precedence. We assume the [:]'s exist in expr.
9947 ;; Ditto the end.
9948 (while (string-match
9949 (concat "\\([[({:*+-]\\)" ; - must be last
9950 "(\\<\\([0-9A-Za-z_]+\\))"
9951 "\\([])}:*+-]\\)")
9952 out)
9953 (setq out (replace-match "\\1\\2\\3" nil nil out)))
9954 (while (string-match
9955 (concat "\\([[({:*+-]\\)" ; - must be last
9956 "\\$clog2\\s *(\\<\\([0-9]+\\))"
9957 "\\([])}:*+-]\\)")
9958 out)
9959 (setq out (replace-match
9960 (concat
9961 (match-string 1 out)
9962 (int-to-string (verilog-clog2 (string-to-number (match-string 2 out))))
9963 (match-string 3 out))
9964 nil nil out)))
9965 ;; For precedence do * before +/-
9966 (while (string-match
9967 (concat "\\([[({:*+-]\\)"
9968 "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
9969 "\\([])}:*+-]\\)")
9970 out)
9971 (setq out (replace-match
9972 (concat (match-string 1 out)
9973 (int-to-string (* (string-to-number (match-string 2 out))
9974 (string-to-number (match-string 4 out))))
9975 (match-string 5 out))
9976 nil nil out)))
9977 (while (string-match
9978 (concat "\\([[({:+-]\\)" ; No * here as higher prec
9979 "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
9980 "\\([])}:+-]\\)")
9981 out)
9982 (let ((pre (match-string 1 out))
9983 (lhs (string-to-number (match-string 2 out)))
9984 (rhs (string-to-number (match-string 4 out)))
9985 (post (match-string 5 out))
9986 val)
9987 (when (equal pre "-")
9988 (setq lhs (- lhs)))
9989 (setq val (if (equal (match-string 3 out) "-")
9990 (- lhs rhs)
9991 (+ lhs rhs))
9992 out (replace-match
9993 (concat (if (and (equal pre "-")
9994 (< val 0))
9995 "" ;; Not "--20" but just "-20"
9996 pre)
9997 (int-to-string val)
9998 post)
9999 nil nil out)) )))
10000 out)))
10001
10002 ;;(verilog-simplify-range-expression "[1:3]") ;; 1
10003 ;;(verilog-simplify-range-expression "[(1):3]") ;; 1
10004 ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ;;20
10005 ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ;; 48
10006 ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ;; FOO*4-2
10007 ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ;; FOO*4+0
10008 ;;(verilog-simplify-range-expression "[(func(BAR))]") ;; func(BAR)
10009 ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ;; FOO-0
10010 ;;(verilog-simplify-range-expression "[$clog2(2)]") ;; 1
10011 ;;(verilog-simplify-range-expression "[$clog2(7)]") ;; 3
10012
10013 (defun verilog-clog2 (value)
10014 "Compute $clog2 - ceiling log2 of VALUE."
10015 (if (< value 1)
10016 0
10017 (ceiling (/ (log value) (log 2)))))
10018
10019 (defun verilog-typedef-name-p (variable-name)
10020 "Return true if the VARIABLE-NAME is a type definition."
10021 (when verilog-typedef-regexp
10022 (string-match verilog-typedef-regexp variable-name)))
10023 \f
10024 ;;
10025 ;; Auto deletion
10026 ;;
10027
10028 (defun verilog-delete-autos-lined ()
10029 "Delete autos that occupy multiple lines, between begin and end comments."
10030 ;; The newline must not have a comment property, so we must
10031 ;; delete the end auto's newline, not the first newline
10032 (forward-line 1)
10033 (let ((pt (point)))
10034 (when (and
10035 (looking-at "\\s-*// Beginning")
10036 (search-forward "// End of automatic" nil t))
10037 ;; End exists
10038 (end-of-line)
10039 (forward-line 1)
10040 (delete-region pt (point)))))
10041
10042 (defun verilog-delete-empty-auto-pair ()
10043 "Delete begin/end auto pair at point, if empty."
10044 (forward-line 0)
10045 (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
10046 "\\s-*// End of automatics\n"))
10047 (delete-region (point) (save-excursion (forward-line 2) (point)))))
10048
10049 (defun verilog-forward-close-paren ()
10050 "Find the close parenthesis that match the current point.
10051 Ignore other close parenthesis with matching open parens."
10052 (let ((parens 1))
10053 (while (> parens 0)
10054 (unless (verilog-re-search-forward-quick "[()]" nil t)
10055 (error "%s: Mismatching ()" (verilog-point-text)))
10056 (cond ((= (preceding-char) ?\( )
10057 (setq parens (1+ parens)))
10058 ((= (preceding-char) ?\) )
10059 (setq parens (1- parens)))))))
10060
10061 (defun verilog-backward-open-paren ()
10062 "Find the open parenthesis that match the current point.
10063 Ignore other open parenthesis with matching close parens."
10064 (let ((parens 1))
10065 (while (> parens 0)
10066 (unless (verilog-re-search-backward-quick "[()]" nil t)
10067 (error "%s: Mismatching ()" (verilog-point-text)))
10068 (cond ((= (following-char) ?\) )
10069 (setq parens (1+ parens)))
10070 ((= (following-char) ?\( )
10071 (setq parens (1- parens)))))))
10072
10073 (defun verilog-backward-open-bracket ()
10074 "Find the open bracket that match the current point.
10075 Ignore other open bracket with matching close bracket."
10076 (let ((parens 1))
10077 (while (> parens 0)
10078 (unless (verilog-re-search-backward-quick "[][]" nil t)
10079 (error "%s: Mismatching []" (verilog-point-text)))
10080 (cond ((= (following-char) ?\] )
10081 (setq parens (1+ parens)))
10082 ((= (following-char) ?\[ )
10083 (setq parens (1- parens)))))))
10084
10085 (defun verilog-delete-to-paren ()
10086 "Delete the automatic inst/sense/arg created by autos.
10087 Deletion stops at the matching end parenthesis, outside comments."
10088 (delete-region (point)
10089 (save-excursion
10090 (verilog-backward-open-paren)
10091 (verilog-forward-sexp-ign-cmt 1) ;; Moves to paren that closes argdecl's
10092 (backward-char 1)
10093 (point))))
10094
10095 (defun verilog-auto-star-safe ()
10096 "Return if a .* AUTOINST is safe to delete or expand.
10097 It was created by the AUTOS themselves, or by the user."
10098 (and verilog-auto-star-expand
10099 (looking-at
10100 (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
10101
10102 (defun verilog-delete-auto-star-all ()
10103 "Delete a .* AUTOINST, if it is safe."
10104 (when (verilog-auto-star-safe)
10105 (verilog-delete-to-paren)))
10106
10107 (defun verilog-delete-auto-star-implicit ()
10108 "Delete all .* implicit connections created by `verilog-auto-star'.
10109 This function will be called automatically at save unless
10110 `verilog-auto-star-save' is set, any non-templated expanded pins will be
10111 removed."
10112 (interactive)
10113 (let (paren-pt indent have-close-paren)
10114 (save-excursion
10115 (goto-char (point-min))
10116 ;; We need to match these even outside of comments.
10117 ;; For reasonable performance, we don't check if inside comments, sorry.
10118 (while (re-search-forward "// Implicit \\.\\*" nil t)
10119 (setq paren-pt (point))
10120 (beginning-of-line)
10121 (setq have-close-paren
10122 (save-excursion
10123 (when (search-forward ");" paren-pt t)
10124 (setq indent (current-indentation))
10125 t)))
10126 (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
10127 (when have-close-paren
10128 ;; Delete extra commentary
10129 (save-excursion
10130 (while (progn
10131 (forward-line -1)
10132 (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
10133 (delete-region (match-beginning 0) (match-end 0))))
10134 ;; If it is simple, we can put the ); on the same line as the last text
10135 (let ((rtn-pt (point)))
10136 (save-excursion
10137 (while (progn (backward-char 1)
10138 (looking-at "[ \t\n\f]")))
10139 (when (looking-at ",")
10140 (delete-region (+ 1 (point)) rtn-pt))))
10141 (when (bolp)
10142 (indent-to indent))
10143 (insert ");\n")
10144 ;; Still need to kill final comma - always is one as we put one after the .*
10145 (re-search-backward ",")
10146 (delete-char 1))))))
10147
10148 (defun verilog-delete-auto ()
10149 "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
10150 Use \\[verilog-auto] to re-insert the updated AUTOs.
10151
10152 The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
10153 called before and after this function, respectively."
10154 (interactive)
10155 (save-excursion
10156 (if (buffer-file-name)
10157 (find-file-noselect (buffer-file-name))) ;; To check we have latest version
10158 (verilog-save-no-change-functions
10159 (verilog-save-scan-cache
10160 ;; Allow user to customize
10161 (verilog-run-hooks 'verilog-before-delete-auto-hook)
10162
10163 ;; Remove those that have multi-line insertions, possibly with parameters
10164 ;; We allow anything beginning with AUTO, so that users can add their own
10165 ;; patterns
10166 (verilog-auto-re-search-do
10167 (concat "/\\*AUTO[A-Za-z0-9_]+"
10168 ;; Optional parens or quoted parameter or .* for (((...)))
10169 "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
10170 "\\*/")
10171 'verilog-delete-autos-lined)
10172 ;; Remove those that are in parenthesis
10173 (verilog-auto-re-search-do
10174 (concat "/\\*"
10175 (eval-when-compile
10176 (verilog-regexp-words
10177 `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
10178 "AUTOSENSE")))
10179 "\\*/")
10180 'verilog-delete-to-paren)
10181 ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
10182 (verilog-auto-re-search-do "\\.\\*"
10183 'verilog-delete-auto-star-all)
10184 ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
10185 (goto-char (point-min))
10186 (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
10187 (replace-match ""))
10188
10189 ;; Final customize
10190 (verilog-run-hooks 'verilog-delete-auto-hook)))))
10191 \f
10192 ;;
10193 ;; Auto inject
10194 ;;
10195
10196 (defun verilog-inject-auto ()
10197 "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
10198
10199 Any always @ blocks with sensitivity lists that match computed lists will
10200 be replaced with /*AS*/ comments.
10201
10202 Any cells will get /*AUTOINST*/ added to the end of the pin list.
10203 Pins with have identical names will be deleted.
10204
10205 Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
10206 support adding new ports. You may wish to delete older ports yourself.
10207
10208 For example:
10209
10210 module ExampInject (i, o);
10211 input i;
10212 input j;
10213 output o;
10214 always @ (i or j)
10215 o = i | j;
10216 InstModule instName
10217 (.foobar(baz),
10218 j(j));
10219 endmodule
10220
10221 Typing \\[verilog-inject-auto] will make this into:
10222
10223 module ExampInject (i, o/*AUTOARG*/
10224 // Inputs
10225 j);
10226 input i;
10227 output o;
10228 always @ (/*AS*/i or j)
10229 o = i | j;
10230 InstModule instName
10231 (.foobar(baz),
10232 /*AUTOINST*/
10233 // Outputs
10234 j(j));
10235 endmodule"
10236 (interactive)
10237 (verilog-auto t))
10238
10239 (defun verilog-inject-arg ()
10240 "Inject AUTOARG into new code. See `verilog-inject-auto'."
10241 ;; Presume one module per file.
10242 (save-excursion
10243 (goto-char (point-min))
10244 (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
10245 (let ((endmodp (save-excursion
10246 (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
10247 (point))))
10248 ;; See if there's already a comment .. inside a comment so not verilog-re-search
10249 (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
10250 (verilog-re-search-forward-quick ";" nil t)
10251 (backward-char 1)
10252 (verilog-backward-syntactic-ws-quick)
10253 (backward-char 1) ; Moves to paren that closes argdecl's
10254 (when (looking-at ")")
10255 (verilog-insert "/*AUTOARG*/")))))))
10256
10257 (defun verilog-inject-sense ()
10258 "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
10259 (save-excursion
10260 (goto-char (point-min))
10261 (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
10262 (let* ((start-pt (point))
10263 (modi (verilog-modi-current))
10264 (moddecls (verilog-modi-get-decls modi))
10265 pre-sigs
10266 got-sigs)
10267 (backward-char 1)
10268 (verilog-forward-sexp-ign-cmt 1)
10269 (backward-char 1) ;; End )
10270 (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
10271 (setq pre-sigs (verilog-signals-from-signame
10272 (verilog-read-signals start-pt (point)))
10273 got-sigs (verilog-auto-sense-sigs moddecls nil))
10274 (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
10275 (verilog-signals-not-in got-sigs pre-sigs)))
10276 (delete-region start-pt (point))
10277 (verilog-insert "/*AS*/")))))))
10278
10279 (defun verilog-inject-inst ()
10280 "Inject AUTOINST into new code. See `verilog-inject-auto'."
10281 (save-excursion
10282 (goto-char (point-min))
10283 ;; It's hard to distinguish modules; we'll instead search for pins.
10284 (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_\$]+\\s *(\\s *[a-zA-Z0-9`_\$]+\\s *)" nil t)
10285 (verilog-backward-open-paren) ;; Inst start
10286 (cond
10287 ((= (preceding-char) ?\#) ;; #(...) parameter section, not pin. Skip.
10288 (forward-char 1)
10289 (verilog-forward-close-paren)) ;; Parameters done
10290 (t
10291 (forward-char 1)
10292 (let ((indent-pt (+ (current-column)))
10293 (end-pt (save-excursion (verilog-forward-close-paren) (point))))
10294 (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
10295 (goto-char end-pt)) ;; Already there, continue search with next instance
10296 (t
10297 ;; Delete identical interconnect
10298 (let ((case-fold-search nil)) ;; So we don't convert upper-to-lower, etc
10299 (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_\$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
10300 (delete-region (match-beginning 0) (match-end 0))
10301 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ;; Keep it correct
10302 (while (or (looking-at "[ \t\n\f,]+")
10303 (looking-at "//[^\n]*"))
10304 (delete-region (match-beginning 0) (match-end 0))
10305 (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
10306 (verilog-forward-close-paren)
10307 (backward-char 1)
10308 ;; Not verilog-re-search, as we don't want to strip comments
10309 (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
10310 (delete-region (match-beginning 0) (match-end 0)))
10311 (verilog-insert "\n")
10312 (verilog-insert-indent "/*AUTOINST*/")))))))))
10313 \f
10314 ;;
10315 ;; Auto diff
10316 ;;
10317
10318 (defun verilog-diff-buffers-p (b1 b2 &optional whitespace)
10319 "Return nil if buffers B1 and B2 have same contents.
10320 Else, return point in B1 that first mismatches.
10321 If optional WHITESPACE true, ignore whitespace."
10322 (save-excursion
10323 (let* ((case-fold-search nil) ;; compare-buffer-substrings cares
10324 (p1 (with-current-buffer b1 (goto-char (point-min))))
10325 (p2 (with-current-buffer b2 (goto-char (point-min))))
10326 (maxp1 (with-current-buffer b1 (point-max)))
10327 (maxp2 (with-current-buffer b2 (point-max)))
10328 (op1 -1) (op2 -1)
10329 progress size)
10330 (while (not (and (eq p1 op1) (eq p2 op2)))
10331 ;; If both windows have whitespace optionally skip over it.
10332 (when whitespace
10333 ;; skip-syntax-* doesn't count \n
10334 (with-current-buffer b1
10335 (goto-char p1)
10336 (skip-chars-forward " \t\n\r\f\v")
10337 (setq p1 (point)))
10338 (with-current-buffer b2
10339 (goto-char p2)
10340 (skip-chars-forward " \t\n\r\f\v")
10341 (setq p2 (point))))
10342 (setq size (min (- maxp1 p1) (- maxp2 p2)))
10343 (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
10344 b1 p1 (+ size p1)))
10345 (setq progress (if (zerop progress) size (1- (abs progress))))
10346 (setq op1 p1 op2 p2
10347 p1 (+ p1 progress)
10348 p2 (+ p2 progress)))
10349 ;; Return value
10350 (if (and (eq p1 maxp1) (eq p2 maxp2))
10351 nil p1))))
10352
10353 (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
10354 "View the differences between file F1 and buffer B2.
10355 This requires the external program `diff-command' to be in your `exec-path',
10356 and uses `diff-switches' in which you may want to have \"-u\" flag.
10357 Ignores WHITESPACE if t, and writes output to stdout if SHOW."
10358 ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
10359 ;; call `diff' as `diff' has different calling semantics on different
10360 ;; versions of Emacs.
10361 (if (not (file-exists-p f1))
10362 (message "Buffer %s has no associated file on disc" (buffer-name b2))
10363 (with-temp-buffer "*Verilog-Diff*"
10364 (let ((outbuf (current-buffer))
10365 (f2 (make-temp-file "vm-diff-auto-")))
10366 (unwind-protect
10367 (progn
10368 (with-current-buffer b2
10369 (save-restriction
10370 (widen)
10371 (write-region (point-min) (point-max) f2 nil 'nomessage)))
10372 (call-process diff-command nil outbuf t
10373 diff-switches ;; User may want -u in diff-switches
10374 (if whitespace "-b" "")
10375 f1 f2)
10376 ;; Print out results. Alternatively we could have call-processed
10377 ;; ourself, but this way we can reuse diff switches
10378 (when show
10379 (with-current-buffer outbuf (message "%s" (buffer-string))))))
10380 (sit-for 0)
10381 (when (file-exists-p f2)
10382 (delete-file f2))))))
10383
10384 (defun verilog-diff-report (b1 b2 diffpt)
10385 "Report differences detected with `verilog-diff-auto'.
10386 Differences are between buffers B1 and B2, starting at point
10387 DIFFPT. This function is called via `verilog-diff-function'."
10388 (let ((name1 (with-current-buffer b1 (buffer-file-name))))
10389 (verilog-warn "%s:%d: Difference in AUTO expansion found"
10390 name1 (with-current-buffer b1
10391 (1+ (count-lines (point-min) (point)))))
10392 (cond (noninteractive
10393 (verilog-diff-file-with-buffer name1 b2 t t))
10394 (t
10395 (ediff-buffers b1 b2)))))
10396
10397 (defun verilog-diff-auto ()
10398 "Expand AUTOs in a temporary buffer and indicate any change.
10399 Whitespace differences are ignored to determine identicalness, but
10400 once a difference is detected, whitespace differences may be shown.
10401
10402 To call this from the command line, see \\[verilog-batch-diff-auto].
10403
10404 The action on differences is selected with
10405 `verilog-diff-function'. The default is `verilog-diff-report'
10406 which will report an error and run `ediff' in interactive mode,
10407 or `diff' in batch mode."
10408 (interactive)
10409 (let ((b1 (current-buffer)) b2 diffpt
10410 (name1 (buffer-file-name))
10411 (newname "*Verilog-Diff*"))
10412 (save-excursion
10413 (when (get-buffer newname)
10414 (kill-buffer newname))
10415 (setq b2 (let (buffer-file-name) ;; Else clone is upset
10416 (clone-buffer newname)))
10417 (with-current-buffer b2
10418 ;; auto requires the filename, but can't have same filename in two
10419 ;; buffers; so override both b1 and b2's names
10420 (let ((buffer-file-name name1))
10421 (unwind-protect
10422 (progn
10423 (with-current-buffer b1 (setq buffer-file-name nil))
10424 (verilog-auto)
10425 (when (not verilog-auto-star-save)
10426 (verilog-delete-auto-star-implicit)))
10427 ;; Restore name if unwind
10428 (with-current-buffer b1 (setq buffer-file-name name1)))))
10429 ;;
10430 (setq diffpt (verilog-diff-buffers-p b1 b2 t))
10431 (cond ((not diffpt)
10432 (unless noninteractive (message "AUTO expansion identical"))
10433 (kill-buffer newname)) ;; Nice to cleanup after oneself
10434 (t
10435 (funcall verilog-diff-function b1 b2 diffpt)))
10436 ;; Return result of compare
10437 diffpt)))
10438
10439 \f
10440 ;;
10441 ;; Auto save
10442 ;;
10443
10444 (defun verilog-auto-save-check ()
10445 "On saving see if we need auto update."
10446 (cond ((not verilog-auto-save-policy)) ; disabled
10447 ((not (save-excursion
10448 (save-match-data
10449 (let ((case-fold-search nil))
10450 (goto-char (point-min))
10451 (re-search-forward "AUTO" nil t))))))
10452 ((eq verilog-auto-save-policy 'force)
10453 (verilog-auto))
10454 ((not (buffer-modified-p)))
10455 ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
10456 ((eq verilog-auto-save-policy 'detect)
10457 (verilog-auto))
10458 (t
10459 (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
10460 (verilog-auto))
10461 ;; Don't ask again if didn't update
10462 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
10463 (when (not verilog-auto-star-save)
10464 (verilog-delete-auto-star-implicit))
10465 nil) ;; Always return nil -- we don't write the file ourselves
10466
10467 (defun verilog-auto-read-locals ()
10468 "Return file local variable segment at bottom of file."
10469 (save-excursion
10470 (goto-char (point-max))
10471 (if (re-search-backward "Local Variables:" nil t)
10472 (buffer-substring-no-properties (point) (point-max))
10473 "")))
10474
10475 (defun verilog-auto-reeval-locals (&optional force)
10476 "Read file local variable segment at bottom of file if it has changed.
10477 If FORCE, always reread it."
10478 (let ((curlocal (verilog-auto-read-locals)))
10479 (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
10480 (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
10481 ;; Note this may cause this function to be recursively invoked,
10482 ;; because hack-local-variables may call (verilog-mode)
10483 ;; The above when statement will prevent it from recursing forever.
10484 (hack-local-variables)
10485 t)))
10486 \f
10487 ;;
10488 ;; Auto creation
10489 ;;
10490
10491 (defun verilog-auto-arg-ports (sigs message indent-pt)
10492 "Print a list of ports for an AUTOINST.
10493 Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
10494 (when sigs
10495 (when verilog-auto-arg-sort
10496 (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
10497 (insert "\n")
10498 (indent-to indent-pt)
10499 (insert message)
10500 (insert "\n")
10501 (let ((space ""))
10502 (indent-to indent-pt)
10503 (while sigs
10504 (cond ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
10505 (insert "\n")
10506 (indent-to indent-pt))
10507 (t (insert space)))
10508 (insert (verilog-sig-name (car sigs)) ",")
10509 (setq sigs (cdr sigs)
10510 space " ")))))
10511
10512 (defun verilog-auto-arg ()
10513 "Expand AUTOARG statements.
10514 Replace the argument declarations at the beginning of the
10515 module with ones automatically derived from input and output
10516 statements. This can be dangerous if the module is instantiated
10517 using position-based connections, so use only name-based when
10518 instantiating the resulting module. Long lines are split based
10519 on the `fill-column', see \\[set-fill-column].
10520
10521 Limitations:
10522 Concatenation and outputting partial buses is not supported.
10523
10524 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10525
10526 For example:
10527
10528 module ExampArg (/*AUTOARG*/);
10529 input i;
10530 output o;
10531 endmodule
10532
10533 Typing \\[verilog-auto] will make this into:
10534
10535 module ExampArg (/*AUTOARG*/
10536 // Outputs
10537 o,
10538 // Inputs
10539 i
10540 );
10541 input i;
10542 output o;
10543 endmodule
10544
10545 The argument declarations may be printed in declaration order to best suit
10546 order based instantiations, or alphabetically, based on the
10547 `verilog-auto-arg-sort' variable.
10548
10549 Any ports declared between the ( and /*AUTOARG*/ are presumed to be
10550 predeclared and are not redeclared by AUTOARG. AUTOARG will make a
10551 conservative guess on adding a comma for the first signal, if you have
10552 any ifdefs or complicated expressions before the AUTOARG you will need
10553 to choose the comma yourself.
10554
10555 Avoid declaring ports manually, as it makes code harder to maintain."
10556 (save-excursion
10557 (let* ((modi (verilog-modi-current))
10558 (moddecls (verilog-modi-get-decls modi))
10559 (skip-pins (aref (verilog-read-arg-pins) 0)))
10560 (verilog-repair-open-comma)
10561 (verilog-auto-arg-ports (verilog-signals-not-in
10562 (verilog-decls-get-outputs moddecls)
10563 skip-pins)
10564 "// Outputs"
10565 verilog-indent-level-declaration)
10566 (verilog-auto-arg-ports (verilog-signals-not-in
10567 (verilog-decls-get-inouts moddecls)
10568 skip-pins)
10569 "// Inouts"
10570 verilog-indent-level-declaration)
10571 (verilog-auto-arg-ports (verilog-signals-not-in
10572 (verilog-decls-get-inputs moddecls)
10573 skip-pins)
10574 "// Inputs"
10575 verilog-indent-level-declaration)
10576 (verilog-repair-close-comma)
10577 (unless (eq (char-before) ?/ )
10578 (insert "\n"))
10579 (indent-to verilog-indent-level-declaration))))
10580
10581 (defun verilog-auto-assign-modport ()
10582 "Expand AUTOASSIGNMODPORT statements, as part of \\[verilog-auto].
10583 Take input/output/inout statements from the specified interface
10584 and modport and use to build assignments into the modport, for
10585 making verification modules that connect to UVM interfaces.
10586
10587 The first parameter is the name of an interface.
10588
10589 The second parameter is a regexp of modports to read from in
10590 that interface.
10591
10592 The third parameter is the instance name to use to dot reference into.
10593
10594 The optional fourth parameter is a regular expression, and only
10595 signals matching the regular expression will be included.
10596
10597 Limitations:
10598
10599 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
10600
10601 Inouts are not supported, as assignments must be unidirectional.
10602
10603 If a signal is part of the interface header and in both a
10604 modport and the interface itself, it will not be listed. (As
10605 this would result in a syntax error when the connections are
10606 made.)
10607
10608 See the example in `verilog-auto-inout-modport'."
10609 (save-excursion
10610 (let* ((params (verilog-read-auto-params 3 4))
10611 (submod (nth 0 params))
10612 (modport-re (nth 1 params))
10613 (inst-name (nth 2 params))
10614 (regexp (nth 3 params))
10615 direction-re submodi) ;; direction argument not supported until requested
10616 ;; Lookup position, etc of co-module
10617 ;; Note this may raise an error
10618 (when (setq submodi (verilog-modi-lookup submod t))
10619 (let* ((indent-pt (current-indentation))
10620 (modi (verilog-modi-current))
10621 (submoddecls (verilog-modi-get-decls submodi))
10622 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
10623 (sig-list-i (verilog-signals-in ;; Decls doesn't have data types, must resolve
10624 (verilog-decls-get-vars submoddecls)
10625 (verilog-signals-not-in
10626 (verilog-decls-get-inputs submodportdecls)
10627 (verilog-decls-get-ports submoddecls))))
10628 (sig-list-o (verilog-signals-in ;; Decls doesn't have data types, must resolve
10629 (verilog-decls-get-vars submoddecls)
10630 (verilog-signals-not-in
10631 (verilog-decls-get-outputs submodportdecls)
10632 (verilog-decls-get-ports submoddecls)))))
10633 (forward-line 1)
10634 (setq sig-list-i (verilog-signals-edit-wire-reg
10635 (verilog-signals-matching-dir-re
10636 (verilog-signals-matching-regexp sig-list-i regexp)
10637 "input" direction-re))
10638 sig-list-o (verilog-signals-edit-wire-reg
10639 (verilog-signals-matching-dir-re
10640 (verilog-signals-matching-regexp sig-list-o regexp)
10641 "output" direction-re)))
10642 (setq sig-list-i (sort (copy-alist sig-list-i) `verilog-signals-sort-compare))
10643 (setq sig-list-o (sort (copy-alist sig-list-o) `verilog-signals-sort-compare))
10644 (when (or sig-list-i sig-list-o)
10645 (verilog-insert-indent "// Beginning of automatic assignments from modport\n")
10646 ;; Don't sort them so an upper AUTOINST will match the main module
10647 (let ((sigs sig-list-o))
10648 (while sigs
10649 (verilog-insert-indent "assign " (verilog-sig-name (car sigs))
10650 " = " inst-name
10651 "." (verilog-sig-name (car sigs)) ";\n")
10652 (setq sigs (cdr sigs))))
10653 (let ((sigs sig-list-i))
10654 (while sigs
10655 (verilog-insert-indent "assign " inst-name
10656 "." (verilog-sig-name (car sigs))
10657 " = " (verilog-sig-name (car sigs)) ";\n")
10658 (setq sigs (cdr sigs))))
10659 (verilog-insert-indent "// End of automatics\n")))))))
10660
10661 (defun verilog-auto-inst-port-map (port-st)
10662 nil)
10663
10664 (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
10665 (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10666 (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
10667 (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
10668 (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
10669 (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
10670 (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10671 (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
10672
10673 (defun verilog-auto-inst-port (port-st indent-pt tpl-list tpl-num for-star par-values)
10674 "Print out an instantiation connection for this PORT-ST.
10675 Insert to INDENT-PT, use template TPL-LIST.
10676 @ are instantiation numbers, replaced with TPL-NUM.
10677 @\"(expression @)\" are evaluated, with @ as a variable.
10678 If FOR-STAR add comment it is a .* expansion.
10679 If PAR-VALUES replace final strings with these parameter values."
10680 (let* ((port (verilog-sig-name port-st))
10681 (tpl-ass (or (assoc port (car tpl-list))
10682 (verilog-auto-inst-port-map port-st)))
10683 ;; vl-* are documented for user use
10684 (vl-name (verilog-sig-name port-st))
10685 (vl-width (verilog-sig-width port-st))
10686 (vl-modport (verilog-sig-modport port-st))
10687 (vl-mbits (if (verilog-sig-multidim port-st)
10688 (verilog-sig-multidim-string port-st) ""))
10689 (vl-bits (if (or verilog-auto-inst-vector
10690 (not (assoc port vector-skip-list))
10691 (not (equal (verilog-sig-bits port-st)
10692 (verilog-sig-bits (assoc port vector-skip-list)))))
10693 (or (verilog-sig-bits port-st) "")
10694 ""))
10695 (case-fold-search nil)
10696 (check-values par-values)
10697 tpl-net)
10698 ;; Replace parameters in bit-width
10699 (when (and check-values
10700 (not (equal vl-bits "")))
10701 (while check-values
10702 (setq vl-bits (verilog-string-replace-matches
10703 (concat "\\<" (nth 0 (car check-values)) "\\>")
10704 (concat "(" (nth 1 (car check-values)) ")")
10705 t t vl-bits)
10706 vl-mbits (verilog-string-replace-matches
10707 (concat "\\<" (nth 0 (car check-values)) "\\>")
10708 (concat "(" (nth 1 (car check-values)) ")")
10709 t t vl-mbits)
10710 check-values (cdr check-values)))
10711 (setq vl-bits (verilog-simplify-range-expression vl-bits)
10712 vl-mbits (verilog-simplify-range-expression vl-mbits)
10713 vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
10714 ;; Default net value if not found
10715 (setq tpl-net (concat port
10716 (if vl-modport (concat "." vl-modport) "")
10717 (if (verilog-sig-multidim port-st)
10718 (concat "/*" vl-mbits vl-bits "*/")
10719 (concat vl-bits))))
10720 ;; Find template
10721 (cond (tpl-ass ; Template of exact port name
10722 (setq tpl-net (nth 1 tpl-ass)))
10723 ((nth 1 tpl-list) ; Wildcards in template, search them
10724 (let ((wildcards (nth 1 tpl-list)))
10725 (while wildcards
10726 (when (string-match (nth 0 (car wildcards)) port)
10727 (setq tpl-ass (car wildcards) ; so allow @ parsing
10728 tpl-net (replace-match (nth 1 (car wildcards))
10729 t nil port)))
10730 (setq wildcards (cdr wildcards))))))
10731 ;; Parse Templated variable
10732 (when tpl-ass
10733 ;; Evaluate @"(lispcode)"
10734 (when (string-match "@\".*[^\\]\"" tpl-net)
10735 (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
10736 (setq tpl-net
10737 (concat
10738 (substring tpl-net 0 (match-beginning 0))
10739 (save-match-data
10740 (let* ((expr (match-string 1 tpl-net))
10741 (value
10742 (progn
10743 (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
10744 (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
10745 (prin1 (eval (car (read-from-string expr)))
10746 (lambda (ch) ())))))
10747 (if (numberp value) (setq value (number-to-string value)))
10748 value))
10749 (substring tpl-net (match-end 0))))))
10750 ;; Replace @ and [] magic variables in final output
10751 (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
10752 (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
10753 ;; Insert it
10754 (indent-to indent-pt)
10755 (insert "." port)
10756 (unless (and verilog-auto-inst-dot-name
10757 (equal port tpl-net))
10758 (indent-to verilog-auto-inst-column)
10759 (insert "(" tpl-net ")"))
10760 (insert ",")
10761 (cond (tpl-ass
10762 (verilog-read-auto-template-hit tpl-ass)
10763 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10764 verilog-auto-inst-column))
10765 ;; verilog-insert requires the complete comment in one call - including the newline
10766 (cond ((equal verilog-auto-inst-template-numbers `lhs)
10767 (verilog-insert " // Templated"
10768 " LHS: " (nth 0 tpl-ass)
10769 "\n"))
10770 (verilog-auto-inst-template-numbers
10771 (verilog-insert " // Templated"
10772 " T" (int-to-string (nth 2 tpl-ass))
10773 " L" (int-to-string (nth 3 tpl-ass))
10774 "\n"))
10775 (t
10776 (verilog-insert " // Templated\n"))))
10777 (for-star
10778 (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
10779 verilog-auto-inst-column))
10780 (verilog-insert " // Implicit .\*\n")) ;For some reason the . or * must be escaped...
10781 (t
10782 (insert "\n")))))
10783 ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
10784 ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
10785 ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
10786
10787 (defun verilog-auto-inst-port-list (sig-list indent-pt tpl-list tpl-num for-star par-values)
10788 "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
10789 (when verilog-auto-inst-sort
10790 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
10791 (mapc (lambda (port)
10792 (verilog-auto-inst-port port indent-pt
10793 tpl-list tpl-num for-star par-values))
10794 sig-list))
10795
10796 (defun verilog-auto-inst-first ()
10797 "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
10798 ;; Do we need a trailing comma?
10799 ;; There maybe an ifdef or something similar before us. What a mess. Thus
10800 ;; to avoid trouble we only insert on preceding ) or *.
10801 ;; Insert first port on new line
10802 (insert "\n") ;; Must insert before search, so point will move forward if insert comma
10803 (save-excursion
10804 (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
10805 (when (looking-at ")\\|\\*") ;; Generally don't insert, unless we are fairly sure
10806 (forward-char 1)
10807 (insert ","))))
10808
10809 (defun verilog-auto-star ()
10810 "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
10811
10812 If `verilog-auto-star-expand' is set, .* pins are treated if they were
10813 AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
10814 will also ignore any .* that are not last in your pin list (this prevents
10815 it from deleting pins following the .* when it expands the AUTOINST.)
10816
10817 On writing your file, unless `verilog-auto-star-save' is set, any
10818 non-templated expanded pins will be removed. You may do this at any time
10819 with \\[verilog-delete-auto-star-implicit].
10820
10821 If you are converting a module to use .* for the first time, you may wish
10822 to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
10823
10824 See `verilog-auto-inst' for examples, templates, and more information."
10825 (when (verilog-auto-star-safe)
10826 (verilog-auto-inst)))
10827
10828 (defun verilog-auto-inst ()
10829 "Expand AUTOINST statements, as part of \\[verilog-auto].
10830 Replace the pin connections to an instantiation or interface
10831 declaration with ones automatically derived from the module or
10832 interface header of the instantiated item.
10833
10834 If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
10835 and delete them before saving unless `verilog-auto-star-save' is set.
10836 See `verilog-auto-star' for more information.
10837
10838 The pins are printed in declaration order or alphabetically,
10839 based on the `verilog-auto-inst-sort' variable.
10840
10841 Limitations:
10842 Module names must be resolvable to filenames by adding a
10843 `verilog-library-extensions', and being found in the same directory, or
10844 by changing the variable `verilog-library-flags' or
10845 `verilog-library-directories'. Macros `modname are translated through the
10846 vh-{name} Emacs variable, if that is not found, it just ignores the `.
10847
10848 In templates you must have one signal per line, ending in a ), or ));,
10849 and have proper () nesting, including a final ); to end the template.
10850
10851 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
10852
10853 SystemVerilog multidimensional input/output has only experimental support.
10854
10855 SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
10856
10857 Parameters referenced by the instantiation will remain symbolic, unless
10858 `verilog-auto-inst-param-value' is set.
10859
10860 Gate primitives (and/or) may have AUTOINST for the purpose of
10861 AUTOWIRE declarations, etc. Gates are the only case when
10862 position based connections are passed.
10863
10864 For example, first take the submodule InstModule.v:
10865
10866 module InstModule (o,i);
10867 output [31:0] o;
10868 input i;
10869 wire [31:0] o = {32{i}};
10870 endmodule
10871
10872 This is then used in an upper level module:
10873
10874 module ExampInst (o,i);
10875 output o;
10876 input i;
10877 InstModule instName
10878 (/*AUTOINST*/);
10879 endmodule
10880
10881 Typing \\[verilog-auto] will make this into:
10882
10883 module ExampInst (o,i);
10884 output o;
10885 input i;
10886 InstModule instName
10887 (/*AUTOINST*/
10888 // Outputs
10889 .ov (ov[31:0]),
10890 // Inputs
10891 .i (i));
10892 endmodule
10893
10894 Where the list of inputs and outputs came from the inst module.
10895 \f
10896 Exceptions:
10897
10898 Unless you are instantiating a module multiple times, or the module is
10899 something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
10900 It just makes for unmaintainable code. To sanitize signal names, try
10901 vrename from URL `http://www.veripool.org'.
10902
10903 When you need to violate this suggestion there are two ways to list
10904 exceptions, placing them before the AUTOINST, or using templates.
10905
10906 Any ports defined before the /*AUTOINST*/ are not included in the list of
10907 automatics. This is similar to making a template as described below, but
10908 is restricted to simple connections just like you normally make. Also note
10909 that any signals before the AUTOINST will only be picked up by AUTOWIRE if
10910 you have the appropriate // Input or // Output comment, and exactly the
10911 same line formatting as AUTOINST itself uses.
10912
10913 InstModule instName
10914 (// Inputs
10915 .i (my_i_dont_mess_with_it),
10916 /*AUTOINST*/
10917 // Outputs
10918 .ov (ov[31:0]));
10919
10920 \f
10921 Templates:
10922
10923 For multiple instantiations based upon a single template, create a
10924 commented out template:
10925
10926 /* InstModule AUTO_TEMPLATE (
10927 .sig3 (sigz[]),
10928 );
10929 */
10930
10931 Templates go ABOVE the instantiation(s). When an instantiation is
10932 expanded `verilog-mode' simply searches up for the closest template.
10933 Thus you can have multiple templates for the same module, just alternate
10934 between the template for an instantiation and the instantiation itself.
10935 (For backward compatibility if no template is found above, it
10936 will also look below, but do not use this behavior in new designs.)
10937
10938 The module name must be the same as the name of the module in the
10939 instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
10940 words and capitalized. Only signals that must be different for each
10941 instantiation need to be listed.
10942
10943 Inside a template, a [] in a connection name (with nothing else inside
10944 the brackets) will be replaced by the same bus subscript as it is being
10945 connected to, or the [] will be removed if it is a single bit signal.
10946 Generally it is a good idea to do this for all connections in a template,
10947 as then they will work for any width signal, and with AUTOWIRE. See
10948 PTL_BUS becoming PTL_BUSNEW below.
10949
10950 If you have a complicated template, set `verilog-auto-inst-template-numbers'
10951 to see which regexps are matching. Don't leave that mode set after
10952 debugging is completed though, it will result in lots of extra differences
10953 and merge conflicts.
10954
10955 Setting `verilog-auto-template-warn-unused' will report errors
10956 if any template lines are unused.
10957
10958 For example:
10959
10960 /* InstModule AUTO_TEMPLATE (
10961 .ptl_bus (ptl_busnew[]),
10962 );
10963 */
10964 InstModule ms2m (/*AUTOINST*/);
10965
10966 Typing \\[verilog-auto] will make this into:
10967
10968 InstModule ms2m (/*AUTOINST*/
10969 // Outputs
10970 .NotInTemplate (NotInTemplate),
10971 .ptl_bus (ptl_busnew[3:0]), // Templated
10972 ....
10973
10974 \f
10975 Multiple Module Templates:
10976
10977 The same template lines can be applied to multiple modules with
10978 the syntax as follows:
10979
10980 /* InstModuleA AUTO_TEMPLATE
10981 InstModuleB AUTO_TEMPLATE
10982 InstModuleC AUTO_TEMPLATE
10983 InstModuleD AUTO_TEMPLATE (
10984 .ptl_bus (ptl_busnew[]),
10985 );
10986 */
10987
10988 Note there is only one AUTO_TEMPLATE opening parenthesis.
10989 \f
10990 @ Templates:
10991
10992 It is common to instantiate a cell multiple times, so templates make it
10993 trivial to substitute part of the cell name into the connection name.
10994
10995 /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
10996 .sig1 (sigx[@]),
10997 .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
10998 );
10999 */
11000
11001 If no regular expression is provided immediately after the AUTO_TEMPLATE
11002 keyword, then the @ character in any connection names will be replaced
11003 with the instantiation number; the first digits found in the cell's
11004 instantiation name.
11005
11006 If a regular expression is provided, the @ character will be replaced
11007 with the first \(\) grouping that matches against the cell name. Using a
11008 regexp of \"\\([0-9]+\\)\" provides identical values for @ as when no
11009 regexp is provided. If you use multiple layers of parenthesis,
11010 \"test\\([^0-9]+\\)_\\([0-9]+\\)\" would replace @ with non-number
11011 characters after test and before _, whereas
11012 \"\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)\" would replace @ with the entire
11013 match.
11014
11015 For example:
11016
11017 /* InstModule AUTO_TEMPLATE (
11018 .ptl_mapvalidx (ptl_mapvalid[@]),
11019 .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
11020 );
11021 */
11022 InstModule ms2m (/*AUTOINST*/);
11023
11024 Typing \\[verilog-auto] will make this into:
11025
11026 InstModule ms2m (/*AUTOINST*/
11027 // Outputs
11028 .ptl_mapvalidx (ptl_mapvalid[2]),
11029 .ptl_mapvalidp1x (ptl_mapvalid[3]));
11030
11031 Note the @ character was replaced with the 2 from \"ms2m\".
11032
11033 Alternatively, using a regular expression for @:
11034
11035 /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
11036 .ptl_mapvalidx (@_ptl_mapvalid),
11037 .ptl_mapvalidp1x (ptl_mapvalid_@),
11038 );
11039 */
11040 InstModule ms2_FOO (/*AUTOINST*/);
11041 InstModule ms2_BAR (/*AUTOINST*/);
11042
11043 Typing \\[verilog-auto] will make this into:
11044
11045 InstModule ms2_FOO (/*AUTOINST*/
11046 // Outputs
11047 .ptl_mapvalidx (FOO_ptl_mapvalid),
11048 .ptl_mapvalidp1x (ptl_mapvalid_FOO));
11049 InstModule ms2_BAR (/*AUTOINST*/
11050 // Outputs
11051 .ptl_mapvalidx (BAR_ptl_mapvalid),
11052 .ptl_mapvalidp1x (ptl_mapvalid_BAR));
11053
11054 \f
11055 Regexp Templates:
11056
11057 A template entry of the form
11058
11059 .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
11060
11061 will apply an Emacs style regular expression search for any port beginning
11062 in pci_req followed by numbers and ending in _l and connecting that to
11063 the pci_req_jtag_[] net, with the bus subscript coming from what matches
11064 inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
11065
11066 Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
11067 does the same thing. (Note a @ in the connection/replacement text is
11068 completely different -- still use \\1 there!) Thus this is the same as
11069 the above template:
11070
11071 .pci_req@_l (pci_req_jtag_[\\1]),
11072
11073 Here's another example to remove the _l, useful when naming conventions
11074 specify _ alone to mean active low. Note the use of [] to keep the bus
11075 subscript:
11076
11077 .\\(.*\\)_l (\\1_[]),
11078 \f
11079 Lisp Templates:
11080
11081 First any regular expression template is expanded.
11082
11083 If the syntax @\"( ... )\" is found in a connection, the expression in
11084 quotes will be evaluated as a Lisp expression, with @ replaced by the
11085 instantiation number. The MAPVALIDP1X example above would put @+1 modulo
11086 4 into the brackets. Quote all double-quotes inside the expression with
11087 a leading backslash (\\\"...\\\"); or if the Lisp template is also a
11088 regexp template backslash the backslash quote (\\\\\"...\\\\\").
11089
11090 There are special variables defined that are useful in these
11091 Lisp functions:
11092
11093 vl-name Name portion of the input/output port.
11094 vl-bits Bus bits portion of the input/output port ('[2:0]').
11095 vl-mbits Multidimensional array bits for port ('[2:0][3:0]').
11096 vl-width Width of the input/output port ('3' for [2:0]).
11097 May be a (...) expression if bits isn't a constant.
11098 vl-dir Direction of the pin input/output/inout/interface.
11099 vl-modport The modport, if an interface with a modport.
11100 vl-cell-type Module name/type of the cell ('InstModule').
11101 vl-cell-name Instance name of the cell ('instName').
11102
11103 Normal Lisp variables may be used in expressions. See
11104 `verilog-read-defines' which can set vh-{definename} variables for use
11105 here. Also, any comments of the form:
11106
11107 /*AUTO_LISP(setq foo 1)*/
11108
11109 will evaluate any Lisp expression inside the parenthesis between the
11110 beginning of the buffer and the point of the AUTOINST. This allows
11111 functions to be defined or variables to be changed between instantiations.
11112 (See also `verilog-auto-insert-lisp' if you want the output from your
11113 lisp function to be inserted.)
11114
11115 Note that when using lisp expressions errors may occur when @ is not a
11116 number; you may need to use the standard Emacs Lisp functions
11117 `number-to-string' and `string-to-number'.
11118
11119 After the evaluation is completed, @ substitution and [] substitution
11120 occur.
11121
11122 For more information see the \\[verilog-faq] and forums at URL
11123 `http://www.veripool.org'."
11124 (save-excursion
11125 ;; Find beginning
11126 (let* ((pt (point))
11127 (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
11128 (indent-pt (save-excursion (verilog-backward-open-paren)
11129 (1+ (current-column))))
11130 (verilog-auto-inst-column (max verilog-auto-inst-column
11131 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11132 (modi (verilog-modi-current))
11133 (moddecls (verilog-modi-get-decls modi))
11134 (vector-skip-list (unless verilog-auto-inst-vector
11135 (verilog-decls-get-signals moddecls)))
11136 submod submodi submoddecls
11137 inst skip-pins tpl-list tpl-num did-first par-values)
11138
11139 ;; Find module name that is instantiated
11140 (setq submod (verilog-read-inst-module)
11141 inst (verilog-read-inst-name)
11142 vl-cell-type submod
11143 vl-cell-name inst
11144 skip-pins (aref (verilog-read-inst-pins) 0))
11145
11146 ;; Parse any AUTO_LISP() before here
11147 (verilog-read-auto-lisp (point-min) pt)
11148
11149 ;; Read parameters (after AUTO_LISP)
11150 (setq par-values (and verilog-auto-inst-param-value
11151 (verilog-read-inst-param-value)))
11152
11153 ;; Lookup position, etc of submodule
11154 ;; Note this may raise an error
11155 (when (and (not (member submod verilog-gate-keywords))
11156 (setq submodi (verilog-modi-lookup submod t)))
11157 (setq submoddecls (verilog-modi-get-decls submodi))
11158 ;; If there's a number in the instantiation, it may be an argument to the
11159 ;; automatic variable instantiation program.
11160 (let* ((tpl-info (verilog-read-auto-template submod))
11161 (tpl-regexp (aref tpl-info 0)))
11162 (setq tpl-num (if (string-match tpl-regexp inst)
11163 (match-string 1 inst)
11164 "")
11165 tpl-list (aref tpl-info 1)))
11166 ;; Find submodule's signals and dump
11167 (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
11168 (verilog-signals-not-in
11169 (verilog-decls-get-vars submoddecls)
11170 skip-pins)))
11171 (vl-dir "interfaced"))
11172 (when (and sig-list
11173 verilog-auto-inst-interfaced-ports)
11174 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11175 ;; Note these are searched for in verilog-read-sub-decls.
11176 (verilog-insert-indent "// Interfaced\n")
11177 (verilog-auto-inst-port-list sig-list indent-pt
11178 tpl-list tpl-num for-star par-values)))
11179 (let ((sig-list (verilog-signals-not-in
11180 (verilog-decls-get-interfaces submoddecls)
11181 skip-pins))
11182 (vl-dir "interface"))
11183 (when sig-list
11184 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11185 ;; Note these are searched for in verilog-read-sub-decls.
11186 (verilog-insert-indent "// Interfaces\n")
11187 (verilog-auto-inst-port-list sig-list indent-pt
11188 tpl-list tpl-num for-star par-values)))
11189 (let ((sig-list (verilog-signals-not-in
11190 (verilog-decls-get-outputs submoddecls)
11191 skip-pins))
11192 (vl-dir "output"))
11193 (when sig-list
11194 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11195 (verilog-insert-indent "// Outputs\n")
11196 (verilog-auto-inst-port-list sig-list indent-pt
11197 tpl-list tpl-num for-star par-values)))
11198 (let ((sig-list (verilog-signals-not-in
11199 (verilog-decls-get-inouts submoddecls)
11200 skip-pins))
11201 (vl-dir "inout"))
11202 (when sig-list
11203 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11204 (verilog-insert-indent "// Inouts\n")
11205 (verilog-auto-inst-port-list sig-list indent-pt
11206 tpl-list tpl-num for-star par-values)))
11207 (let ((sig-list (verilog-signals-not-in
11208 (verilog-decls-get-inputs submoddecls)
11209 skip-pins))
11210 (vl-dir "input"))
11211 (when sig-list
11212 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11213 (verilog-insert-indent "// Inputs\n")
11214 (verilog-auto-inst-port-list sig-list indent-pt
11215 tpl-list tpl-num for-star par-values)))
11216 ;; Kill extra semi
11217 (save-excursion
11218 (cond (did-first
11219 (re-search-backward "," pt t)
11220 (delete-char 1)
11221 (insert ");")
11222 (search-forward "\n") ;; Added by inst-port
11223 (delete-char -1)
11224 (if (search-forward ")" nil t) ;; From user, moved up a line
11225 (delete-char -1))
11226 (if (search-forward ";" nil t) ;; Don't error if user had syntax error and forgot it
11227 (delete-char -1)))))))))
11228
11229 (defun verilog-auto-inst-param ()
11230 "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
11231 Replace the parameter connections to an instantiation with ones
11232 automatically derived from the module header of the instantiated netlist.
11233
11234 See \\[verilog-auto-inst] for limitations, and templates to customize the
11235 output.
11236
11237 For example, first take the submodule InstModule.v:
11238
11239 module InstModule (o,i);
11240 parameter PAR;
11241 endmodule
11242
11243 This is then used in an upper level module:
11244
11245 module ExampInst (o,i);
11246 parameter PAR;
11247 InstModule #(/*AUTOINSTPARAM*/)
11248 instName (/*AUTOINST*/);
11249 endmodule
11250
11251 Typing \\[verilog-auto] will make this into:
11252
11253 module ExampInst (o,i);
11254 output o;
11255 input i;
11256 InstModule #(/*AUTOINSTPARAM*/
11257 // Parameters
11258 .PAR (PAR));
11259 instName (/*AUTOINST*/);
11260 endmodule
11261
11262 Where the list of parameter connections come from the inst module.
11263 \f
11264 Templates:
11265
11266 You can customize the parameter connections using AUTO_TEMPLATEs,
11267 just as you would with \\[verilog-auto-inst]."
11268 (save-excursion
11269 ;; Find beginning
11270 (let* ((pt (point))
11271 (indent-pt (save-excursion (verilog-backward-open-paren)
11272 (1+ (current-column))))
11273 (verilog-auto-inst-column (max verilog-auto-inst-column
11274 (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
11275 (modi (verilog-modi-current))
11276 (moddecls (verilog-modi-get-decls modi))
11277 (vector-skip-list (unless verilog-auto-inst-vector
11278 (verilog-decls-get-signals moddecls)))
11279 submod submodi submoddecls
11280 inst skip-pins tpl-list tpl-num did-first)
11281 ;; Find module name that is instantiated
11282 (setq submod (save-excursion
11283 ;; Get to the point where AUTOINST normally is to read the module
11284 (verilog-re-search-forward-quick "[(;]" nil nil)
11285 (verilog-read-inst-module))
11286 inst (save-excursion
11287 ;; Get to the point where AUTOINST normally is to read the module
11288 (verilog-re-search-forward-quick "[(;]" nil nil)
11289 (verilog-read-inst-name))
11290 vl-cell-type submod
11291 vl-cell-name inst
11292 skip-pins (aref (verilog-read-inst-pins) 0))
11293
11294 ;; Parse any AUTO_LISP() before here
11295 (verilog-read-auto-lisp (point-min) pt)
11296
11297 ;; Lookup position, etc of submodule
11298 ;; Note this may raise an error
11299 (when (setq submodi (verilog-modi-lookup submod t))
11300 (setq submoddecls (verilog-modi-get-decls submodi))
11301 ;; If there's a number in the instantiation, it may be an argument to the
11302 ;; automatic variable instantiation program.
11303 (let* ((tpl-info (verilog-read-auto-template submod))
11304 (tpl-regexp (aref tpl-info 0)))
11305 (setq tpl-num (if (string-match tpl-regexp inst)
11306 (match-string 1 inst)
11307 "")
11308 tpl-list (aref tpl-info 1)))
11309 ;; Find submodule's signals and dump
11310 (let ((sig-list (verilog-signals-not-in
11311 (verilog-decls-get-gparams submoddecls)
11312 skip-pins))
11313 (vl-dir "parameter"))
11314 (when sig-list
11315 (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
11316 ;; Note these are searched for in verilog-read-sub-decls.
11317 (verilog-insert-indent "// Parameters\n")
11318 (verilog-auto-inst-port-list sig-list indent-pt
11319 tpl-list tpl-num nil nil)))
11320 ;; Kill extra semi
11321 (save-excursion
11322 (cond (did-first
11323 (re-search-backward "," pt t)
11324 (delete-char 1)
11325 (insert ")")
11326 (search-forward "\n") ;; Added by inst-port
11327 (delete-char -1)
11328 (if (search-forward ")" nil t) ;; From user, moved up a line
11329 (delete-char -1)))))))))
11330
11331 (defun verilog-auto-reg ()
11332 "Expand AUTOREG statements, as part of \\[verilog-auto].
11333 Make reg statements for any output that isn't already declared,
11334 and isn't a wire output from a block. `verilog-auto-wire-type'
11335 may be used to change the datatype of the declarations.
11336
11337 Limitations:
11338 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11339
11340 This does NOT work on memories, declare those yourself.
11341
11342 An example:
11343
11344 module ExampReg (o,i);
11345 output o;
11346 input i;
11347 /*AUTOREG*/
11348 always o = i;
11349 endmodule
11350
11351 Typing \\[verilog-auto] will make this into:
11352
11353 module ExampReg (o,i);
11354 output o;
11355 input i;
11356 /*AUTOREG*/
11357 // Beginning of automatic regs (for this module's undeclared outputs)
11358 reg o;
11359 // End of automatics
11360 always o = i;
11361 endmodule"
11362 (save-excursion
11363 ;; Point must be at insertion point.
11364 (let* ((indent-pt (current-indentation))
11365 (modi (verilog-modi-current))
11366 (moddecls (verilog-modi-get-decls modi))
11367 (modsubdecls (verilog-modi-get-sub-decls modi))
11368 (sig-list (verilog-signals-not-in
11369 (verilog-decls-get-outputs moddecls)
11370 (append (verilog-signals-with ;; ignore typed signals
11371 'verilog-sig-type
11372 (verilog-decls-get-outputs moddecls))
11373 (verilog-decls-get-vars moddecls)
11374 (verilog-decls-get-assigns moddecls)
11375 (verilog-decls-get-consts moddecls)
11376 (verilog-decls-get-gparams moddecls)
11377 (verilog-subdecls-get-interfaced modsubdecls)
11378 (verilog-subdecls-get-outputs modsubdecls)
11379 (verilog-subdecls-get-inouts modsubdecls)))))
11380 (when sig-list
11381 (verilog-forward-or-insert-line)
11382 (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
11383 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11384 (verilog-insert-indent "// End of automatics\n")))))
11385
11386 (defun verilog-auto-reg-input ()
11387 "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
11388 Make reg statements instantiation inputs that aren't already declared.
11389 This is useful for making a top level shell for testing the module that is
11390 to be instantiated.
11391
11392 Limitations:
11393 This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
11394
11395 This does NOT work on memories, declare those yourself.
11396
11397 An example (see `verilog-auto-inst' for what else is going on here):
11398
11399 module ExampRegInput (o,i);
11400 output o;
11401 input i;
11402 /*AUTOREGINPUT*/
11403 InstModule instName
11404 (/*AUTOINST*/);
11405 endmodule
11406
11407 Typing \\[verilog-auto] will make this into:
11408
11409 module ExampRegInput (o,i);
11410 output o;
11411 input i;
11412 /*AUTOREGINPUT*/
11413 // Beginning of automatic reg inputs (for undeclared ...
11414 reg [31:0] iv; // From inst of inst.v
11415 // End of automatics
11416 InstModule instName
11417 (/*AUTOINST*/
11418 // Outputs
11419 .o (o[31:0]),
11420 // Inputs
11421 .iv (iv));
11422 endmodule"
11423 (save-excursion
11424 ;; Point must be at insertion point.
11425 (let* ((indent-pt (current-indentation))
11426 (modi (verilog-modi-current))
11427 (moddecls (verilog-modi-get-decls modi))
11428 (modsubdecls (verilog-modi-get-sub-decls modi))
11429 (sig-list (verilog-signals-combine-bus
11430 (verilog-signals-not-in
11431 (append (verilog-subdecls-get-inputs modsubdecls)
11432 (verilog-subdecls-get-inouts modsubdecls))
11433 (append (verilog-decls-get-signals moddecls)
11434 (verilog-decls-get-assigns moddecls))))))
11435 (when sig-list
11436 (verilog-forward-or-insert-line)
11437 (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
11438 (verilog-insert-definition modi sig-list "reg" indent-pt nil)
11439 (verilog-insert-indent "// End of automatics\n")))))
11440
11441 (defun verilog-auto-logic-setup ()
11442 "Prepare variables due to AUTOLOGIC."
11443 (unless verilog-auto-wire-type
11444 (set (make-local-variable 'verilog-auto-wire-type)
11445 "logic")))
11446
11447 (defun verilog-auto-logic ()
11448 "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
11449 Make wire statements using the SystemVerilog logic keyword.
11450 This is currently equivalent to:
11451
11452 /*AUTOWIRE*/
11453
11454 with the below at the bottom of the file
11455
11456 // Local Variables:
11457 // verilog-auto-logic-type:\"logic\"
11458 // End:
11459
11460 In the future AUTOLOGIC may declare additional identifiers,
11461 while AUTOWIRE will not."
11462 (save-excursion
11463 (verilog-auto-logic-setup)
11464 (verilog-auto-wire)))
11465
11466 (defun verilog-auto-wire ()
11467 "Expand AUTOWIRE statements, as part of \\[verilog-auto].
11468 Make wire statements for instantiations outputs that aren't
11469 already declared. `verilog-auto-wire-type' may be used to change
11470 the datatype of the declarations.
11471
11472 Limitations:
11473 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
11474 and all buses must have widths, such as those from AUTOINST, or using []
11475 in AUTO_TEMPLATEs.
11476
11477 This does NOT work on memories or SystemVerilog .name connections,
11478 declare those yourself.
11479
11480 Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
11481 determine how to bus together. This occurs when you have ports with
11482 non-numeric or non-sequential bus subscripts. If Verilog mode
11483 mis-guessed, you'll have to declare them yourself.
11484
11485 An example (see `verilog-auto-inst' for what else is going on here):
11486
11487 module ExampWire (o,i);
11488 output o;
11489 input i;
11490 /*AUTOWIRE*/
11491 InstModule instName
11492 (/*AUTOINST*/);
11493 endmodule
11494
11495 Typing \\[verilog-auto] will make this into:
11496
11497 module ExampWire (o,i);
11498 output o;
11499 input i;
11500 /*AUTOWIRE*/
11501 // Beginning of automatic wires
11502 wire [31:0] ov; // From inst of inst.v
11503 // End of automatics
11504 InstModule instName
11505 (/*AUTOINST*/
11506 // Outputs
11507 .ov (ov[31:0]),
11508 // Inputs
11509 .i (i));
11510 wire o = | ov;
11511 endmodule"
11512 (save-excursion
11513 ;; Point must be at insertion point.
11514 (let* ((indent-pt (current-indentation))
11515 (modi (verilog-modi-current))
11516 (moddecls (verilog-modi-get-decls modi))
11517 (modsubdecls (verilog-modi-get-sub-decls modi))
11518 (sig-list (verilog-signals-combine-bus
11519 (verilog-signals-not-in
11520 (append (verilog-subdecls-get-outputs modsubdecls)
11521 (verilog-subdecls-get-inouts modsubdecls))
11522 (verilog-decls-get-signals moddecls)))))
11523 (when sig-list
11524 (verilog-forward-or-insert-line)
11525 (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
11526 (verilog-insert-definition modi sig-list "wire" indent-pt nil)
11527 (verilog-insert-indent "// End of automatics\n")
11528 ;; We used to optionally call verilog-pretty-declarations and
11529 ;; verilog-pretty-expr here, but it's too slow on huge modules,
11530 ;; plus makes everyone's module change. Finally those call
11531 ;; syntax-ppss which is broken when change hooks are disabled.
11532 ))))
11533
11534 (defun verilog-auto-output ()
11535 "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
11536 Make output statements for any output signal from an /*AUTOINST*/ that
11537 isn't an input to another AUTOINST. This is useful for modules which
11538 only instantiate other modules.
11539
11540 Limitations:
11541 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11542
11543 If placed inside the parenthesis of a module declaration, it creates
11544 Verilog 2001 style, else uses Verilog 1995 style.
11545
11546 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11547 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11548
11549 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11550
11551 Signals matching `verilog-auto-output-ignore-regexp' are not included.
11552
11553 An example (see `verilog-auto-inst' for what else is going on here):
11554
11555 module ExampOutput (ov,i);
11556 input i;
11557 /*AUTOOUTPUT*/
11558 InstModule instName
11559 (/*AUTOINST*/);
11560 endmodule
11561
11562 Typing \\[verilog-auto] will make this into:
11563
11564 module ExampOutput (ov,i);
11565 input i;
11566 /*AUTOOUTPUT*/
11567 // Beginning of automatic outputs (from unused autoinst outputs)
11568 output [31:0] ov; // From inst of inst.v
11569 // End of automatics
11570 InstModule instName
11571 (/*AUTOINST*/
11572 // Outputs
11573 .ov (ov[31:0]),
11574 // Inputs
11575 .i (i));
11576 endmodule
11577
11578 You may also provide an optional regular expression, in which case only
11579 signals matching the regular expression will be included. For example the
11580 same expansion will result from only extracting outputs starting with ov:
11581
11582 /*AUTOOUTPUT(\"^ov\")*/"
11583 (save-excursion
11584 ;; Point must be at insertion point.
11585 (let* ((indent-pt (current-indentation))
11586 (params (verilog-read-auto-params 0 1))
11587 (regexp (nth 0 params))
11588 (v2k (verilog-in-paren-quick))
11589 (modi (verilog-modi-current))
11590 (moddecls (verilog-modi-get-decls modi))
11591 (modsubdecls (verilog-modi-get-sub-decls modi))
11592 (sig-list (verilog-signals-not-in
11593 (verilog-subdecls-get-outputs modsubdecls)
11594 (append (verilog-decls-get-outputs moddecls)
11595 (verilog-decls-get-inouts moddecls)
11596 (verilog-subdecls-get-inputs modsubdecls)
11597 (verilog-subdecls-get-inouts modsubdecls)))))
11598 (when regexp
11599 (setq sig-list (verilog-signals-matching-regexp
11600 sig-list regexp)))
11601 (setq sig-list (verilog-signals-not-matching-regexp
11602 sig-list verilog-auto-output-ignore-regexp))
11603 (verilog-forward-or-insert-line)
11604 (when v2k (verilog-repair-open-comma))
11605 (when sig-list
11606 (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
11607 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11608 (verilog-insert-indent "// End of automatics\n"))
11609 (when v2k (verilog-repair-close-comma)))))
11610
11611 (defun verilog-auto-output-every ()
11612 "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
11613 Make output statements for any signals that aren't primary inputs or
11614 outputs already. This makes every signal in the design an output. This is
11615 useful to get Synopsys to preserve every signal in the design, since it
11616 won't optimize away the outputs.
11617
11618 An example:
11619
11620 module ExampOutputEvery (o,i,tempa,tempb);
11621 output o;
11622 input i;
11623 /*AUTOOUTPUTEVERY*/
11624 wire tempa = i;
11625 wire tempb = tempa;
11626 wire o = tempb;
11627 endmodule
11628
11629 Typing \\[verilog-auto] will make this into:
11630
11631 module ExampOutputEvery (o,i,tempa,tempb);
11632 output o;
11633 input i;
11634 /*AUTOOUTPUTEVERY*/
11635 // Beginning of automatic outputs (every signal)
11636 output tempb;
11637 output tempa;
11638 // End of automatics
11639 wire tempa = i;
11640 wire tempb = tempa;
11641 wire o = tempb;
11642 endmodule"
11643 (save-excursion
11644 ;;Point must be at insertion point
11645 (let* ((indent-pt (current-indentation))
11646 (v2k (verilog-in-paren-quick))
11647 (modi (verilog-modi-current))
11648 (moddecls (verilog-modi-get-decls modi))
11649 (sig-list (verilog-signals-combine-bus
11650 (verilog-signals-not-in
11651 (verilog-decls-get-signals moddecls)
11652 (verilog-decls-get-ports moddecls)))))
11653 (verilog-forward-or-insert-line)
11654 (when v2k (verilog-repair-open-comma))
11655 (when sig-list
11656 (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
11657 (verilog-insert-definition modi sig-list "output" indent-pt v2k)
11658 (verilog-insert-indent "// End of automatics\n"))
11659 (when v2k (verilog-repair-close-comma)))))
11660
11661 (defun verilog-auto-input ()
11662 "Expand AUTOINPUT statements, as part of \\[verilog-auto].
11663 Make input statements for any input signal into an /*AUTOINST*/ that
11664 isn't declared elsewhere inside the module. This is useful for modules which
11665 only instantiate other modules.
11666
11667 Limitations:
11668 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11669
11670 If placed inside the parenthesis of a module declaration, it creates
11671 Verilog 2001 style, else uses Verilog 1995 style.
11672
11673 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11674 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11675
11676 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11677
11678 Signals matching `verilog-auto-input-ignore-regexp' are not included.
11679
11680 An example (see `verilog-auto-inst' for what else is going on here):
11681
11682 module ExampInput (ov,i);
11683 output [31:0] ov;
11684 /*AUTOINPUT*/
11685 InstModule instName
11686 (/*AUTOINST*/);
11687 endmodule
11688
11689 Typing \\[verilog-auto] will make this into:
11690
11691 module ExampInput (ov,i);
11692 output [31:0] ov;
11693 /*AUTOINPUT*/
11694 // Beginning of automatic inputs (from unused autoinst inputs)
11695 input i; // From inst of inst.v
11696 // End of automatics
11697 InstModule instName
11698 (/*AUTOINST*/
11699 // Outputs
11700 .ov (ov[31:0]),
11701 // Inputs
11702 .i (i));
11703 endmodule
11704
11705 You may also provide an optional regular expression, in which case only
11706 signals matching the regular expression will be included. For example the
11707 same expansion will result from only extracting inputs starting with i:
11708
11709 /*AUTOINPUT(\"^i\")*/"
11710 (save-excursion
11711 (let* ((indent-pt (current-indentation))
11712 (params (verilog-read-auto-params 0 1))
11713 (regexp (nth 0 params))
11714 (v2k (verilog-in-paren-quick))
11715 (modi (verilog-modi-current))
11716 (moddecls (verilog-modi-get-decls modi))
11717 (modsubdecls (verilog-modi-get-sub-decls modi))
11718 (sig-list (verilog-signals-not-in
11719 (verilog-subdecls-get-inputs modsubdecls)
11720 (append (verilog-decls-get-inputs moddecls)
11721 (verilog-decls-get-inouts moddecls)
11722 (verilog-decls-get-vars moddecls)
11723 (verilog-decls-get-consts moddecls)
11724 (verilog-decls-get-gparams moddecls)
11725 (verilog-subdecls-get-interfaced modsubdecls)
11726 (verilog-subdecls-get-outputs modsubdecls)
11727 (verilog-subdecls-get-inouts modsubdecls)))))
11728 (when regexp
11729 (setq sig-list (verilog-signals-matching-regexp
11730 sig-list regexp)))
11731 (setq sig-list (verilog-signals-not-matching-regexp
11732 sig-list verilog-auto-input-ignore-regexp))
11733 (verilog-forward-or-insert-line)
11734 (when v2k (verilog-repair-open-comma))
11735 (when sig-list
11736 (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
11737 (verilog-insert-definition modi sig-list "input" indent-pt v2k)
11738 (verilog-insert-indent "// End of automatics\n"))
11739 (when v2k (verilog-repair-close-comma)))))
11740
11741 (defun verilog-auto-inout ()
11742 "Expand AUTOINOUT statements, as part of \\[verilog-auto].
11743 Make inout statements for any inout signal in an /*AUTOINST*/ that
11744 isn't declared elsewhere inside the module.
11745
11746 Limitations:
11747 This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
11748
11749 If placed inside the parenthesis of a module declaration, it creates
11750 Verilog 2001 style, else uses Verilog 1995 style.
11751
11752 If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
11753 instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
11754
11755 Typedefs must match `verilog-typedef-regexp', which is disabled by default.
11756
11757 Signals matching `verilog-auto-inout-ignore-regexp' are not included.
11758
11759 An example (see `verilog-auto-inst' for what else is going on here):
11760
11761 module ExampInout (ov,i);
11762 input i;
11763 /*AUTOINOUT*/
11764 InstModule instName
11765 (/*AUTOINST*/);
11766 endmodule
11767
11768 Typing \\[verilog-auto] will make this into:
11769
11770 module ExampInout (ov,i);
11771 input i;
11772 /*AUTOINOUT*/
11773 // Beginning of automatic inouts (from unused autoinst inouts)
11774 inout [31:0] ov; // From inst of inst.v
11775 // End of automatics
11776 InstModule instName
11777 (/*AUTOINST*/
11778 // Inouts
11779 .ov (ov[31:0]),
11780 // Inputs
11781 .i (i));
11782 endmodule
11783
11784 You may also provide an optional regular expression, in which case only
11785 signals matching the regular expression will be included. For example the
11786 same expansion will result from only extracting inouts starting with i:
11787
11788 /*AUTOINOUT(\"^i\")*/"
11789 (save-excursion
11790 ;; Point must be at insertion point.
11791 (let* ((indent-pt (current-indentation))
11792 (params (verilog-read-auto-params 0 1))
11793 (regexp (nth 0 params))
11794 (v2k (verilog-in-paren-quick))
11795 (modi (verilog-modi-current))
11796 (moddecls (verilog-modi-get-decls modi))
11797 (modsubdecls (verilog-modi-get-sub-decls modi))
11798 (sig-list (verilog-signals-not-in
11799 (verilog-subdecls-get-inouts modsubdecls)
11800 (append (verilog-decls-get-outputs moddecls)
11801 (verilog-decls-get-inouts moddecls)
11802 (verilog-decls-get-inputs moddecls)
11803 (verilog-subdecls-get-inputs modsubdecls)
11804 (verilog-subdecls-get-outputs modsubdecls)))))
11805 (when regexp
11806 (setq sig-list (verilog-signals-matching-regexp
11807 sig-list regexp)))
11808 (setq sig-list (verilog-signals-not-matching-regexp
11809 sig-list verilog-auto-inout-ignore-regexp))
11810 (verilog-forward-or-insert-line)
11811 (when v2k (verilog-repair-open-comma))
11812 (when sig-list
11813 (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
11814 (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
11815 (verilog-insert-indent "// End of automatics\n"))
11816 (when v2k (verilog-repair-close-comma)))))
11817
11818 (defun verilog-auto-inout-module (&optional complement all-in)
11819 "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
11820 Take input/output/inout statements from the specified module and insert
11821 into the current module. This is useful for making null templates and
11822 shell modules which need to have identical I/O with another module.
11823 Any I/O which are already defined in this module will not be redefined.
11824 For the complement of this function, see `verilog-auto-inout-comp',
11825 and to make monitors with all inputs, see `verilog-auto-inout-in'.
11826
11827 Limitations:
11828 If placed inside the parenthesis of a module declaration, it creates
11829 Verilog 2001 style, else uses Verilog 1995 style.
11830
11831 Concatenation and outputting partial buses is not supported.
11832
11833 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11834
11835 Signals are not inserted in the same order as in the original module,
11836 though they will appear to be in the same order to an AUTOINST
11837 instantiating either module.
11838
11839 Signals declared as \"output reg\" or \"output wire\" etc will
11840 lose the wire/reg declaration so that shell modules may
11841 generate those outputs differently. However, \"output logic\"
11842 is propagated.
11843
11844 An example:
11845
11846 module ExampShell (/*AUTOARG*/);
11847 /*AUTOINOUTMODULE(\"ExampMain\")*/
11848 endmodule
11849
11850 module ExampMain (i,o,io);
11851 input i;
11852 output o;
11853 inout io;
11854 endmodule
11855
11856 Typing \\[verilog-auto] will make this into:
11857
11858 module ExampShell (/*AUTOARG*/i,o,io);
11859 /*AUTOINOUTMODULE(\"ExampMain\")*/
11860 // Beginning of automatic in/out/inouts (from specific module)
11861 output o;
11862 inout io;
11863 input i;
11864 // End of automatics
11865 endmodule
11866
11867 You may also provide an optional regular expression, in which case only
11868 signals matching the regular expression will be included. For example the
11869 same expansion will result from only extracting signals starting with i:
11870
11871 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
11872
11873 You may also provide an optional second regular expression, in
11874 which case only signals which have that pin direction and data
11875 type will be included. This matches against everything before
11876 the signal name in the declaration, for example against
11877 \"input\" (single bit), \"output logic\" (direction and type) or
11878 \"output [1:0]\" (direction and implicit type). You also
11879 probably want to skip spaces in your regexp.
11880
11881 For example, the below will result in matching the output \"o\"
11882 against the previous example's module:
11883
11884 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/"
11885 (save-excursion
11886 (let* ((params (verilog-read-auto-params 1 3))
11887 (submod (nth 0 params))
11888 (regexp (nth 1 params))
11889 (direction-re (nth 2 params))
11890 submodi)
11891 ;; Lookup position, etc of co-module
11892 ;; Note this may raise an error
11893 (when (setq submodi (verilog-modi-lookup submod t))
11894 (let* ((indent-pt (current-indentation))
11895 (v2k (verilog-in-paren-quick))
11896 (modi (verilog-modi-current))
11897 (moddecls (verilog-modi-get-decls modi))
11898 (submoddecls (verilog-modi-get-decls submodi))
11899 (sig-list-i (verilog-signals-not-in
11900 (cond (all-in
11901 (append
11902 (verilog-decls-get-inputs submoddecls)
11903 (verilog-decls-get-inouts submoddecls)
11904 (verilog-decls-get-outputs submoddecls)))
11905 (complement
11906 (verilog-decls-get-outputs submoddecls))
11907 (t (verilog-decls-get-inputs submoddecls)))
11908 (append (verilog-decls-get-inputs moddecls))))
11909 (sig-list-o (verilog-signals-not-in
11910 (cond (all-in nil)
11911 (complement
11912 (verilog-decls-get-inputs submoddecls))
11913 (t (verilog-decls-get-outputs submoddecls)))
11914 (append (verilog-decls-get-outputs moddecls))))
11915 (sig-list-io (verilog-signals-not-in
11916 (cond (all-in nil)
11917 (t (verilog-decls-get-inouts submoddecls)))
11918 (append (verilog-decls-get-inouts moddecls))))
11919 (sig-list-if (verilog-signals-not-in
11920 (verilog-decls-get-interfaces submoddecls)
11921 (append (verilog-decls-get-interfaces moddecls)))))
11922 (forward-line 1)
11923 (setq sig-list-i (verilog-signals-edit-wire-reg
11924 (verilog-signals-matching-dir-re
11925 (verilog-signals-matching-regexp sig-list-i regexp)
11926 "input" direction-re))
11927 sig-list-o (verilog-signals-edit-wire-reg
11928 (verilog-signals-matching-dir-re
11929 (verilog-signals-matching-regexp sig-list-o regexp)
11930 "output" direction-re))
11931 sig-list-io (verilog-signals-edit-wire-reg
11932 (verilog-signals-matching-dir-re
11933 (verilog-signals-matching-regexp sig-list-io regexp)
11934 "inout" direction-re))
11935 sig-list-if (verilog-signals-matching-dir-re
11936 (verilog-signals-matching-regexp sig-list-if regexp)
11937 "interface" direction-re))
11938 (when v2k (verilog-repair-open-comma))
11939 (when (or sig-list-i sig-list-o sig-list-io)
11940 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
11941 ;; Don't sort them so an upper AUTOINST will match the main module
11942 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
11943 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
11944 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
11945 (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
11946 (verilog-insert-indent "// End of automatics\n"))
11947 (when v2k (verilog-repair-close-comma)))))))
11948
11949 (defun verilog-auto-inout-comp ()
11950 "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
11951 Take input/output/inout statements from the specified module and
11952 insert the inverse into the current module (inputs become outputs
11953 and vice-versa.) This is useful for making test and stimulus
11954 modules which need to have complementing I/O with another module.
11955 Any I/O which are already defined in this module will not be
11956 redefined. For the complement of this function, see
11957 `verilog-auto-inout-module'.
11958
11959 Limitations:
11960 If placed inside the parenthesis of a module declaration, it creates
11961 Verilog 2001 style, else uses Verilog 1995 style.
11962
11963 Concatenation and outputting partial buses is not supported.
11964
11965 Module names must be resolvable to filenames. See `verilog-auto-inst'.
11966
11967 Signals are not inserted in the same order as in the original module,
11968 though they will appear to be in the same order to an AUTOINST
11969 instantiating either module.
11970
11971 An example:
11972
11973 module ExampShell (/*AUTOARG*/);
11974 /*AUTOINOUTCOMP(\"ExampMain\")*/
11975 endmodule
11976
11977 module ExampMain (i,o,io);
11978 input i;
11979 output o;
11980 inout io;
11981 endmodule
11982
11983 Typing \\[verilog-auto] will make this into:
11984
11985 module ExampShell (/*AUTOARG*/i,o,io);
11986 /*AUTOINOUTCOMP(\"ExampMain\")*/
11987 // Beginning of automatic in/out/inouts (from specific module)
11988 output i;
11989 inout io;
11990 input o;
11991 // End of automatics
11992 endmodule
11993
11994 You may also provide an optional regular expression, in which case only
11995 signals matching the regular expression will be included. For example the
11996 same expansion will result from only extracting signals starting with i:
11997
11998 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
11999 (verilog-auto-inout-module t nil))
12000
12001 (defun verilog-auto-inout-in ()
12002 "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
12003 Take input/output/inout statements from the specified module and
12004 insert them as all inputs into the current module. This is
12005 useful for making monitor modules which need to see all signals
12006 as inputs based on another module. Any I/O which are already
12007 defined in this module will not be redefined. See also
12008 `verilog-auto-inout-module'.
12009
12010 Limitations:
12011 If placed inside the parenthesis of a module declaration, it creates
12012 Verilog 2001 style, else uses Verilog 1995 style.
12013
12014 Concatenation and outputting partial buses is not supported.
12015
12016 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12017
12018 Signals are not inserted in the same order as in the original module,
12019 though they will appear to be in the same order to an AUTOINST
12020 instantiating either module.
12021
12022 An example:
12023
12024 module ExampShell (/*AUTOARG*/);
12025 /*AUTOINOUTIN(\"ExampMain\")*/
12026 endmodule
12027
12028 module ExampMain (i,o,io);
12029 input i;
12030 output o;
12031 inout io;
12032 endmodule
12033
12034 Typing \\[verilog-auto] will make this into:
12035
12036 module ExampShell (/*AUTOARG*/i,o,io);
12037 /*AUTOINOUTIN(\"ExampMain\")*/
12038 // Beginning of automatic in/out/inouts (from specific module)
12039 input i;
12040 input io;
12041 input o;
12042 // End of automatics
12043 endmodule
12044
12045 You may also provide an optional regular expression, in which case only
12046 signals matching the regular expression will be included. For example the
12047 same expansion will result from only extracting signals starting with i:
12048
12049 /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/"
12050 (verilog-auto-inout-module nil t))
12051
12052 (defun verilog-auto-inout-param ()
12053 "Expand AUTOINOUTPARAM statements, as part of \\[verilog-auto].
12054 Take input/output/inout statements from the specified module and insert
12055 into the current module. This is useful for making null templates and
12056 shell modules which need to have identical I/O with another module.
12057 Any I/O which are already defined in this module will not be redefined.
12058 For the complement of this function, see `verilog-auto-inout-comp',
12059 and to make monitors with all inputs, see `verilog-auto-inout-in'.
12060
12061 Limitations:
12062 If placed inside the parenthesis of a module declaration, it creates
12063 Verilog 2001 style, else uses Verilog 1995 style.
12064
12065 Concatenation and outputting partial buses is not supported.
12066
12067 Module names must be resolvable to filenames. See `verilog-auto-inst'.
12068
12069 Signals are not inserted in the same order as in the original module,
12070 though they will appear to be in the same order to an AUTOINST
12071 instantiating either module.
12072
12073 Signals declared as \"output reg\" or \"output wire\" etc will
12074 lose the wire/reg declaration so that shell modules may
12075 generate those outputs differently. However, \"output logic\"
12076 is propagated.
12077
12078 An example:
12079
12080 module ExampShell (/*AUTOARG*/);
12081 /*AUTOINOUTMODULE(\"ExampMain\")*/
12082 endmodule
12083
12084 module ExampMain (i,o,io);
12085 input i;
12086 output o;
12087 inout io;
12088 endmodule
12089
12090 Typing \\[verilog-auto] will make this into:
12091
12092 module ExampShell (/*AUTOARG*/i,o,io);
12093 /*AUTOINOUTMODULE(\"ExampMain\")*/
12094 // Beginning of automatic in/out/inouts (from specific module)
12095 output o;
12096 inout io;
12097 input i;
12098 // End of automatics
12099 endmodule
12100
12101 You may also provide an optional regular expression, in which case only
12102 signals matching the regular expression will be included. For example the
12103 same expansion will result from only extracting signals starting with i:
12104
12105 /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
12106
12107 You may also provide an optional second regular expression, in
12108 which case only signals which have that pin direction and data
12109 type will be included. This matches against everything before
12110 the signal name in the declaration, for example against
12111 \"input\" (single bit), \"output logic\" (direction and type) or
12112 \"output [1:0]\" (direction and implicit type). You also
12113 probably want to skip spaces in your regexp.
12114
12115 For example, the below will result in matching the output \"o\"
12116 against the previous example's module:
12117
12118 /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/
12119
12120 You may also provide an optional third regular expression, in
12121 which case any parameter names that match the given regexp will
12122 be included. Including parameters is off by default. To include
12123 all signals and parameters, use:
12124
12125 /*AUTOINOUTMODULE(\"ExampMain\",\".*\",\".*\",\".*\")*/"
12126 (save-excursion
12127 (let* ((params (verilog-read-auto-params 1 2))
12128 (submod (nth 0 params))
12129 (regexp (nth 1 params))
12130 submodi)
12131 ;; Lookup position, etc of co-module
12132 ;; Note this may raise an error
12133 (when (setq submodi (verilog-modi-lookup submod t))
12134 (let* ((indent-pt (current-indentation))
12135 (v2k (verilog-in-paren-quick))
12136 (modi (verilog-modi-current))
12137 (moddecls (verilog-modi-get-decls modi))
12138 (submoddecls (verilog-modi-get-decls submodi))
12139 (sig-list-p (verilog-signals-not-in
12140 (verilog-decls-get-gparams submoddecls)
12141 (append (verilog-decls-get-gparams moddecls)))))
12142 (forward-line 1)
12143 (setq sig-list-p (verilog-signals-matching-regexp sig-list-p regexp))
12144 (when v2k (verilog-repair-open-comma))
12145 (when sig-list-p
12146 (verilog-insert-indent "// Beginning of automatic parameters (from specific module)\n")
12147 ;; Don't sort them so an upper AUTOINST will match the main module
12148 (verilog-insert-definition modi sig-list-p "parameter" indent-pt v2k t)
12149 (verilog-insert-indent "// End of automatics\n"))
12150 (when v2k (verilog-repair-close-comma)))))))
12151
12152 (defun verilog-auto-inout-modport ()
12153 "Expand AUTOINOUTMODPORT statements, as part of \\[verilog-auto].
12154 Take input/output/inout statements from the specified interface
12155 and modport and insert into the current module. This is useful
12156 for making verification modules that connect to UVM interfaces.
12157
12158 The first parameter is the name of an interface.
12159
12160 The second parameter is a regexp of modports to read from in
12161 that interface.
12162
12163 The optional third parameter is a regular expression, and only
12164 signals matching the regular expression will be included.
12165
12166 Limitations:
12167 If placed inside the parenthesis of a module declaration, it creates
12168 Verilog 2001 style, else uses Verilog 1995 style.
12169
12170 Interface names must be resolvable to filenames. See `verilog-auto-inst'.
12171
12172 As with other autos, any inputs/outputs declared in the module
12173 will suppress the AUTO from redeclaring an input/output by
12174 the same name.
12175
12176 An example:
12177
12178 interface ExampIf
12179 ( input logic clk );
12180 logic req_val;
12181 logic [7:0] req_dat;
12182 clocking mon_clkblk @(posedge clk);
12183 input req_val;
12184 input req_dat;
12185 endclocking
12186 modport mp(clocking mon_clkblk);
12187 endinterface
12188
12189 module ExampMain
12190 ( input clk,
12191 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12192 // Beginning of automatic in/out/inouts (from modport)
12193 input [7:0] req_dat,
12194 input req_val
12195 // End of automatics
12196 );
12197 /*AUTOASSIGNMODPORT(\"ExampIf\" \"mp\")*/
12198 endmodule
12199
12200 Typing \\[verilog-auto] will make this into:
12201
12202 ...
12203 module ExampMain
12204 ( input clk,
12205 /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
12206 // Beginning of automatic in/out/inouts (from modport)
12207 input req_dat,
12208 input req_val
12209 // End of automatics
12210 );
12211
12212 If the modport is part of a UVM monitor/driver class, this
12213 creates a wrapper module that may be used to instantiate the
12214 driver/monitor using AUTOINST in the testbench."
12215 (save-excursion
12216 (let* ((params (verilog-read-auto-params 2 3))
12217 (submod (nth 0 params))
12218 (modport-re (nth 1 params))
12219 (regexp (nth 2 params))
12220 direction-re submodi) ;; direction argument not supported until requested
12221 ;; Lookup position, etc of co-module
12222 ;; Note this may raise an error
12223 (when (setq submodi (verilog-modi-lookup submod t))
12224 (let* ((indent-pt (current-indentation))
12225 (v2k (verilog-in-paren-quick))
12226 (modi (verilog-modi-current))
12227 (moddecls (verilog-modi-get-decls modi))
12228 (submoddecls (verilog-modi-get-decls submodi))
12229 (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
12230 (sig-list-i (verilog-signals-in ;; Decls doesn't have data types, must resolve
12231 (verilog-decls-get-vars submoddecls)
12232 (verilog-signals-not-in
12233 (verilog-decls-get-inputs submodportdecls)
12234 (append (verilog-decls-get-ports submoddecls)
12235 (verilog-decls-get-ports moddecls)))))
12236 (sig-list-o (verilog-signals-in ;; Decls doesn't have data types, must resolve
12237 (verilog-decls-get-vars submoddecls)
12238 (verilog-signals-not-in
12239 (verilog-decls-get-outputs submodportdecls)
12240 (append (verilog-decls-get-ports submoddecls)
12241 (verilog-decls-get-ports moddecls)))))
12242 (sig-list-io (verilog-signals-in ;; Decls doesn't have data types, must resolve
12243 (verilog-decls-get-vars submoddecls)
12244 (verilog-signals-not-in
12245 (verilog-decls-get-inouts submodportdecls)
12246 (append (verilog-decls-get-ports submoddecls)
12247 (verilog-decls-get-ports moddecls))))))
12248 (forward-line 1)
12249 (setq sig-list-i (verilog-signals-edit-wire-reg
12250 (verilog-signals-matching-dir-re
12251 (verilog-signals-matching-regexp sig-list-i regexp)
12252 "input" direction-re))
12253 sig-list-o (verilog-signals-edit-wire-reg
12254 (verilog-signals-matching-dir-re
12255 (verilog-signals-matching-regexp sig-list-o regexp)
12256 "output" direction-re))
12257 sig-list-io (verilog-signals-edit-wire-reg
12258 (verilog-signals-matching-dir-re
12259 (verilog-signals-matching-regexp sig-list-io regexp)
12260 "inout" direction-re)))
12261 (when v2k (verilog-repair-open-comma))
12262 (when (or sig-list-i sig-list-o sig-list-io)
12263 (verilog-insert-indent "// Beginning of automatic in/out/inouts (from modport)\n")
12264 ;; Don't sort them so an upper AUTOINST will match the main module
12265 (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
12266 (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
12267 (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
12268 (verilog-insert-indent "// End of automatics\n"))
12269 (when v2k (verilog-repair-close-comma)))))))
12270
12271 (defun verilog-auto-insert-lisp ()
12272 "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
12273 The Lisp code provided is called, and the Lisp code calls
12274 `insert` to insert text into the current file beginning on the
12275 line after the AUTOINSERTLISP.
12276
12277 See also AUTO_LISP, which takes a Lisp expression and evaluates
12278 it during `verilog-auto-inst' but does not insert any text.
12279
12280 An example:
12281
12282 module ExampInsertLisp;
12283 /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
12284 endmodule
12285
12286 // For this example we declare the function in the
12287 // module's file itself. Often you'd define it instead
12288 // in a site-start.el or init file.
12289 /*
12290 Local Variables:
12291 eval:
12292 (defun my-verilog-insert-hello (who)
12293 (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
12294 End:
12295 */
12296
12297 Typing \\[verilog-auto] will call my-verilog-insert-hello and
12298 expand the above into:
12299
12300 // Beginning of automatic insert lisp
12301 initial $write(\"hello world\");
12302 // End of automatics
12303
12304 You can also call an external program and insert the returned
12305 text:
12306
12307 /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
12308 // Beginning of automatic insert lisp
12309 //hello
12310 // End of automatics"
12311 (save-excursion
12312 ;; Point is at end of /*AUTO...*/
12313 (let* ((indent-pt (current-indentation))
12314 (cmd-end-pt (save-excursion (search-backward ")")
12315 (forward-char)
12316 (point))) ;; Closing paren
12317 (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
12318 (backward-sexp 1) ;; Inside comment
12319 (point))) ;; Beginning paren
12320 (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
12321 (verilog-forward-or-insert-line)
12322 ;; Some commands don't move point (like insert-file) so we always
12323 ;; add the begin/end comments, then delete it if not needed
12324 (verilog-insert-indent "// Beginning of automatic insert lisp\n")
12325 (verilog-insert-indent "// End of automatics\n")
12326 (forward-line -1)
12327 (eval (read cmd))
12328 (forward-line -1)
12329 (setq verilog-scan-cache-tick nil) ;; Clear cache; inserted unknown text
12330 (verilog-delete-empty-auto-pair))))
12331
12332 (defun verilog-auto-sense-sigs (moddecls presense-sigs)
12333 "Return list of signals for current AUTOSENSE block."
12334 (let* ((sigss (verilog-read-always-signals))
12335 (sig-list (verilog-signals-not-params
12336 (verilog-signals-not-in (verilog-alw-get-inputs sigss)
12337 (append (and (not verilog-auto-sense-include-inputs)
12338 (verilog-alw-get-outputs-delayed sigss))
12339 (and (not verilog-auto-sense-include-inputs)
12340 (verilog-alw-get-outputs-immediate sigss))
12341 (verilog-alw-get-temps sigss)
12342 (verilog-decls-get-consts moddecls)
12343 (verilog-decls-get-gparams moddecls)
12344 presense-sigs)))))
12345 sig-list))
12346
12347 (defun verilog-auto-sense ()
12348 "Expand AUTOSENSE statements, as part of \\[verilog-auto].
12349 Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
12350 with one automatically derived from all inputs declared in the always
12351 statement. Signals that are generated within the same always block are NOT
12352 placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
12353 Long lines are split based on the `fill-column', see \\[set-fill-column].
12354
12355 Limitations:
12356 Verilog does not allow memories (multidimensional arrays) in sensitivity
12357 lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
12358
12359 Constant signals:
12360 AUTOSENSE cannot always determine if a `define is a constant or a signal
12361 (it could be in an include file for example). If a `define or other signal
12362 is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
12363 declaration anywhere in the module (parenthesis are required):
12364
12365 /* AUTO_CONSTANT ( `this_is_really_constant_dont_autosense_it ) */
12366
12367 Better yet, use a parameter, which will be understood to be constant
12368 automatically.
12369
12370 OOps!
12371 If AUTOSENSE makes a mistake, please report it. (First try putting
12372 a begin/end after your always!) As a workaround, if a signal that
12373 shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
12374 If a signal should be in the sensitivity list wasn't, placing it before
12375 the /*AUTOSENSE*/ comment will prevent it from being deleted when the
12376 autos are updated (or added if it occurs there already).
12377
12378 An example:
12379
12380 always @ (/*AS*/) begin
12381 /* AUTO_CONSTANT (`constant) */
12382 outin = ina | inb | `constant;
12383 out = outin;
12384 end
12385
12386 Typing \\[verilog-auto] will make this into:
12387
12388 always @ (/*AS*/ina or inb) begin
12389 /* AUTO_CONSTANT (`constant) */
12390 outin = ina | inb | `constant;
12391 out = outin;
12392 end
12393
12394 Note in Verilog 2001, you can often get the same result from the new @*
12395 operator. (This was added to the language in part due to AUTOSENSE!)
12396
12397 always @* begin
12398 outin = ina | inb | `constant;
12399 out = outin;
12400 end"
12401 (save-excursion
12402 ;; Find beginning
12403 (let* ((start-pt (save-excursion
12404 (verilog-re-search-backward-quick "(" nil t)
12405 (point)))
12406 (indent-pt (save-excursion
12407 (or (and (goto-char start-pt) (1+ (current-column)))
12408 (current-indentation))))
12409 (modi (verilog-modi-current))
12410 (moddecls (verilog-modi-get-decls modi))
12411 (sig-memories (verilog-signals-memory
12412 (verilog-decls-get-vars moddecls)))
12413 sig-list not-first presense-sigs)
12414 ;; Read signals in always, eliminate outputs from sense list
12415 (setq presense-sigs (verilog-signals-from-signame
12416 (save-excursion
12417 (verilog-read-signals start-pt (point)))))
12418 (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
12419 (when sig-memories
12420 (let ((tlen (length sig-list)))
12421 (setq sig-list (verilog-signals-not-in sig-list sig-memories))
12422 (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
12423 (if (and presense-sigs ;; Add a "or" if not "(.... or /*AUTOSENSE*/"
12424 (save-excursion (goto-char (point))
12425 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12426 (verilog-re-search-backward-quick "\\s-" start-pt t)
12427 (while (looking-at "\\s-`endif")
12428 (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
12429 (verilog-re-search-backward-quick "\\s-" start-pt t))
12430 (not (looking-at "\\s-or\\b"))))
12431 (setq not-first t))
12432 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12433 (while sig-list
12434 (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
12435 (insert "\n")
12436 (indent-to indent-pt)
12437 (if not-first (insert "or ")))
12438 (not-first (insert " or ")))
12439 (insert (verilog-sig-name (car sig-list)))
12440 (setq sig-list (cdr sig-list)
12441 not-first t)))))
12442
12443 (defun verilog-auto-reset ()
12444 "Expand AUTORESET statements, as part of \\[verilog-auto].
12445 Replace the /*AUTORESET*/ comment with code to initialize all
12446 registers set elsewhere in the always block.
12447
12448 Limitations:
12449 AUTORESET will not clear memories.
12450
12451 AUTORESET uses <= if the signal has a <= assignment in the block,
12452 else it uses =.
12453
12454 If <= is used, all = assigned variables are ignored if
12455 `verilog-auto-reset-blocking-in-non' is nil; they are presumed
12456 to be temporaries.
12457
12458 /*AUTORESET*/ presumes that any signals mentioned between the previous
12459 begin/case/if statement and the AUTORESET comment are being reset manually
12460 and should not be automatically reset. This includes omitting any signals
12461 used on the right hand side of assignments.
12462
12463 By default, AUTORESET will include the width of the signal in the
12464 autos, SystemVerilog designs may want to change this. To control
12465 this behavior, see `verilog-auto-reset-widths'. In some cases
12466 AUTORESET must use a '0 assignment and it will print NOWIDTH; use
12467 `verilog-auto-reset-widths' unbased to prevent this.
12468
12469 AUTORESET ties signals to deasserted, which is presumed to be zero.
12470 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12471 them to a one.
12472
12473 AUTORESET may try to reset arrays or structures that cannot be
12474 reset by a simple assignment, resulting in compile errors. This
12475 is a feature to be taken as a hint that you need to reset these
12476 signals manually (or put them into a \"`ifdef NEVER signal<=`0;
12477 `endif\" so Verilog-Mode ignores them.)
12478
12479 An example:
12480
12481 always @(posedge clk or negedge reset_l) begin
12482 if (!reset_l) begin
12483 c <= 1;
12484 /*AUTORESET*/
12485 end
12486 else begin
12487 a <= in_a;
12488 b <= in_b;
12489 c <= in_c;
12490 end
12491 end
12492
12493 Typing \\[verilog-auto] will make this into:
12494
12495 always @(posedge core_clk or negedge reset_l) begin
12496 if (!reset_l) begin
12497 c <= 1;
12498 /*AUTORESET*/
12499 // Beginning of autoreset for uninitialized flops
12500 a <= 0;
12501 b = 0; // if `verilog-auto-reset-blocking-in-non' true
12502 // End of automatics
12503 end
12504 else begin
12505 a <= in_a;
12506 b = in_b;
12507 c <= in_c;
12508 end
12509 end"
12510
12511 (interactive)
12512 (save-excursion
12513 ;; Find beginning
12514 (let* ((indent-pt (current-indentation))
12515 (modi (verilog-modi-current))
12516 (moddecls (verilog-modi-get-decls modi))
12517 (all-list (verilog-decls-get-signals moddecls))
12518 sigss sig-list dly-list prereset-sigs)
12519 ;; Read signals in always, eliminate outputs from reset list
12520 (setq prereset-sigs (verilog-signals-from-signame
12521 (save-excursion
12522 (verilog-read-signals
12523 (save-excursion
12524 (verilog-re-search-backward-quick "\\(@\\|\\<begin\\>\\|\\<if\\>\\|\\<case\\>\\)" nil t)
12525 (point))
12526 (point)))))
12527 (save-excursion
12528 (verilog-re-search-backward-quick "@" nil t)
12529 (setq sigss (verilog-read-always-signals)))
12530 (setq dly-list (verilog-alw-get-outputs-delayed sigss))
12531 (setq sig-list (verilog-signals-not-in (append
12532 (verilog-alw-get-outputs-delayed sigss)
12533 (when (or (not (verilog-alw-get-uses-delayed sigss))
12534 verilog-auto-reset-blocking-in-non)
12535 (verilog-alw-get-outputs-immediate sigss)))
12536 (append
12537 (verilog-alw-get-temps sigss)
12538 prereset-sigs)))
12539 (setq sig-list (sort sig-list `verilog-signals-sort-compare))
12540 (when sig-list
12541 (insert "\n");
12542 (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
12543 (while sig-list
12544 (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ;; As sig-list has no widths
12545 (car sig-list))))
12546 (indent-to indent-pt)
12547 (insert (verilog-sig-name sig)
12548 (if (assoc (verilog-sig-name sig) dly-list)
12549 (concat " <= " verilog-assignment-delay)
12550 " = ")
12551 (verilog-sig-tieoff sig)
12552 ";\n")
12553 (setq sig-list (cdr sig-list))))
12554 (verilog-insert-indent "// End of automatics")))))
12555
12556 (defun verilog-auto-tieoff ()
12557 "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
12558 Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
12559 signals to deasserted.
12560
12561 /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
12562 input/output list as another module, but no internals. Specifically, it
12563 finds all outputs in the module, and if that input is not otherwise declared
12564 as a register or wire, creates a tieoff.
12565
12566 AUTORESET ties signals to deasserted, which is presumed to be zero.
12567 Signals that match `verilog-active-low-regexp' will be deasserted by tying
12568 them to a one.
12569
12570 You can add signals you do not want included in AUTOTIEOFF with
12571 `verilog-auto-tieoff-ignore-regexp'.
12572
12573 `verilog-auto-wire-type' may be used to change the datatype of
12574 the declarations.
12575
12576 `verilog-auto-reset-widths' may be used to change how the tieoff
12577 value's width is generated.
12578
12579 An example of making a stub for another module:
12580
12581 module ExampStub (/*AUTOINST*/);
12582 /*AUTOINOUTPARAM(\"Foo\")*/
12583 /*AUTOINOUTMODULE(\"Foo\")*/
12584 /*AUTOTIEOFF*/
12585 // verilator lint_off UNUSED
12586 wire _unused_ok = &{1'b0,
12587 /*AUTOUNUSED*/
12588 1'b0};
12589 // verilator lint_on UNUSED
12590 endmodule
12591
12592 Typing \\[verilog-auto] will make this into:
12593
12594 module ExampStub (/*AUTOINST*/...);
12595 /*AUTOINOUTPARAM(\"Foo\")*/
12596 /*AUTOINOUTMODULE(\"Foo\")*/
12597 // Beginning of autotieoff
12598 output [2:0] foo;
12599 // End of automatics
12600
12601 /*AUTOTIEOFF*/
12602 // Beginning of autotieoff
12603 wire [2:0] foo = 3'b0;
12604 // End of automatics
12605 ...
12606 endmodule"
12607 (interactive)
12608 (save-excursion
12609 ;; Find beginning
12610 (let* ((indent-pt (current-indentation))
12611 (modi (verilog-modi-current))
12612 (moddecls (verilog-modi-get-decls modi))
12613 (modsubdecls (verilog-modi-get-sub-decls modi))
12614 (sig-list (verilog-signals-not-in
12615 (verilog-decls-get-outputs moddecls)
12616 (append (verilog-decls-get-vars moddecls)
12617 (verilog-decls-get-assigns moddecls)
12618 (verilog-decls-get-consts moddecls)
12619 (verilog-decls-get-gparams moddecls)
12620 (verilog-subdecls-get-interfaced modsubdecls)
12621 (verilog-subdecls-get-outputs modsubdecls)
12622 (verilog-subdecls-get-inouts modsubdecls)))))
12623 (setq sig-list (verilog-signals-not-matching-regexp
12624 sig-list verilog-auto-tieoff-ignore-regexp))
12625 (when sig-list
12626 (verilog-forward-or-insert-line)
12627 (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
12628 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12629 (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
12630 (while sig-list
12631 (let ((sig (car sig-list)))
12632 (cond ((equal verilog-auto-tieoff-declaration "assign")
12633 (indent-to indent-pt)
12634 (insert "assign " (verilog-sig-name sig)))
12635 (t
12636 (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
12637 (indent-to (max 48 (+ indent-pt 40)))
12638 (insert "= " (verilog-sig-tieoff sig)
12639 ";\n")
12640 (setq sig-list (cdr sig-list))))
12641 (verilog-insert-indent "// End of automatics\n")))))
12642
12643 (defun verilog-auto-undef ()
12644 "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
12645 Take any `defines since the last AUTOUNDEF in the current file
12646 and create `undefs for them. This is used to insure that
12647 file-local defines do not pollute the global `define name space.
12648
12649 Limitations:
12650 AUTOUNDEF presumes any identifier following `define is the
12651 name of a define. Any `ifdefs are ignored.
12652
12653 AUTOUNDEF suppresses creating an `undef for any define that was
12654 `undefed before the AUTOUNDEF. This may be used to work around
12655 the ignoring of `ifdefs as shown below.
12656
12657 An example:
12658
12659 `define XX_FOO
12660 `define M_BAR(x)
12661 `define M_BAZ
12662 ...
12663 `ifdef NEVER
12664 `undef M_BAZ // Emacs will see this and not `undef M_BAZ
12665 `endif
12666 ...
12667 /*AUTOUNDEF*/
12668
12669 Typing \\[verilog-auto] will make this into:
12670
12671 ...
12672 /*AUTOUNDEF*/
12673 // Beginning of automatic undefs
12674 `undef XX_FOO
12675 `undef M_BAR
12676 // End of automatics
12677
12678 You may also provide an optional regular expression, in which case only
12679 defines the regular expression will be undefed."
12680 (save-excursion
12681 (let* ((params (verilog-read-auto-params 0 1))
12682 (regexp (nth 0 params))
12683 (indent-pt (current-indentation))
12684 (end-pt (point))
12685 defs def)
12686 (save-excursion
12687 ;; Scan from start of file, or last AUTOUNDEF
12688 (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
12689 (goto-char (point-min)))
12690 (while (verilog-re-search-forward-quick
12691 "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
12692 (cond ((equal (match-string-no-properties 1) "define")
12693 (setq def (match-string-no-properties 2))
12694 (when (and (or (not regexp)
12695 (string-match regexp def))
12696 (not (member def defs))) ;; delete-dups not in 21.1
12697 (setq defs (cons def defs))))
12698 (t
12699 (setq defs (delete (match-string-no-properties 2) defs))))))
12700 ;; Insert
12701 (setq defs (sort defs 'string<))
12702 (when defs
12703 (verilog-forward-or-insert-line)
12704 (verilog-insert-indent "// Beginning of automatic undefs\n")
12705 (while defs
12706 (verilog-insert-indent "`undef " (car defs) "\n")
12707 (setq defs (cdr defs)))
12708 (verilog-insert-indent "// End of automatics\n")))))
12709
12710 (defun verilog-auto-unused ()
12711 "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
12712 Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
12713 input and inout signals.
12714
12715 /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
12716 input/output list as another module, but no internals. Specifically, it
12717 finds all inputs and inouts in the module, and if that input is not otherwise
12718 used, adds it to a comma separated list.
12719
12720 The comma separated list is intended to be used to create a _unused_ok
12721 signal. Using the exact name \"_unused_ok\" for name of the temporary
12722 signal is recommended as it will insure maximum forward compatibility, it
12723 also makes lint warnings easy to understand; ignore any unused warnings
12724 with \"unused\" in the signal name.
12725
12726 To reduce simulation time, the _unused_ok signal should be forced to a
12727 constant to prevent wiggling. The easiest thing to do is use a
12728 reduction-and with 1'b0 as shown.
12729
12730 This way all unused signals are in one place, making it convenient to add
12731 your tool's specific pragmas around the assignment to disable any unused
12732 warnings.
12733
12734 You can add signals you do not want included in AUTOUNUSED with
12735 `verilog-auto-unused-ignore-regexp'.
12736
12737 An example of making a stub for another module:
12738
12739 module ExampStub (/*AUTOINST*/);
12740 /*AUTOINOUTPARAM(\"Examp\")*/
12741 /*AUTOINOUTMODULE(\"Examp\")*/
12742 /*AUTOTIEOFF*/
12743 // verilator lint_off UNUSED
12744 wire _unused_ok = &{1'b0,
12745 /*AUTOUNUSED*/
12746 1'b0};
12747 // verilator lint_on UNUSED
12748 endmodule
12749
12750 Typing \\[verilog-auto] will make this into:
12751
12752 ...
12753 // verilator lint_off UNUSED
12754 wire _unused_ok = &{1'b0,
12755 /*AUTOUNUSED*/
12756 // Beginning of automatics
12757 unused_input_a,
12758 unused_input_b,
12759 unused_input_c,
12760 // End of automatics
12761 1'b0};
12762 // verilator lint_on UNUSED
12763 endmodule"
12764 (interactive)
12765 (save-excursion
12766 ;; Find beginning
12767 (let* ((indent-pt (progn (search-backward "/*") (current-column)))
12768 (modi (verilog-modi-current))
12769 (moddecls (verilog-modi-get-decls modi))
12770 (modsubdecls (verilog-modi-get-sub-decls modi))
12771 (sig-list (verilog-signals-not-in
12772 (append (verilog-decls-get-inputs moddecls)
12773 (verilog-decls-get-inouts moddecls))
12774 (append (verilog-subdecls-get-inputs modsubdecls)
12775 (verilog-subdecls-get-inouts modsubdecls)))))
12776 (setq sig-list (verilog-signals-not-matching-regexp
12777 sig-list verilog-auto-unused-ignore-regexp))
12778 (when sig-list
12779 (verilog-forward-or-insert-line)
12780 (verilog-insert-indent "// Beginning of automatic unused inputs\n")
12781 (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
12782 (while sig-list
12783 (let ((sig (car sig-list)))
12784 (indent-to indent-pt)
12785 (insert (verilog-sig-name sig) ",\n")
12786 (setq sig-list (cdr sig-list))))
12787 (verilog-insert-indent "// End of automatics\n")))))
12788
12789 (defun verilog-enum-ascii (signm elim-regexp)
12790 "Convert an enum name SIGNM to an ascii string for insertion.
12791 Remove user provided prefix ELIM-REGEXP."
12792 (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
12793 (let ((case-fold-search t))
12794 ;; All upper becomes all lower for readability
12795 (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
12796
12797 (defun verilog-auto-ascii-enum ()
12798 "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
12799 Create a register to contain the ASCII decode of an enumerated signal type.
12800 This will allow trace viewers to show the ASCII name of states.
12801
12802 First, parameters are built into an enumeration using the synopsys enum
12803 comment. The comment must be between the keyword and the symbol.
12804 \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
12805
12806 Next, registers which that enum applies to are also tagged with the same
12807 enum.
12808
12809 Finally, an AUTOASCIIENUM command is used.
12810
12811 The first parameter is the name of the signal to be decoded.
12812
12813 The second parameter is the name to store the ASCII code into. For the
12814 signal foo, I suggest the name _foo__ascii, where the leading _ indicates
12815 a signal that is just for simulation, and the magic characters _ascii
12816 tell viewers like Dinotrace to display in ASCII format.
12817
12818 The third optional parameter is a string which will be removed
12819 from the state names. It defaults to \"\" which removes nothing.
12820
12821 The fourth optional parameter is \"onehot\" to force one-hot
12822 decoding. If unspecified, if and only if the first parameter
12823 width is 2^(number of states in enum) and does NOT match the
12824 width of the enum, the signal is assumed to be a one-hot
12825 decode. Otherwise, it's a normal encoded state vector.
12826
12827 `verilog-auto-wire-type' may be used to change the datatype of
12828 the declarations.
12829
12830 \"auto enum\" may be used in place of \"synopsys enum\".
12831
12832 An example:
12833
12834 //== State enumeration
12835 parameter [2:0] // synopsys enum state_info
12836 SM_IDLE = 3'b000,
12837 SM_SEND = 3'b001,
12838 SM_WAIT1 = 3'b010;
12839 //== State variables
12840 reg [2:0] /* synopsys enum state_info */
12841 state_r; /* synopsys state_vector state_r */
12842 reg [2:0] /* synopsys enum state_info */
12843 state_e1;
12844
12845 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12846
12847 Typing \\[verilog-auto] will make this into:
12848
12849 ... same front matter ...
12850
12851 /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
12852 // Beginning of automatic ASCII enum decoding
12853 reg [39:0] state_ascii_r; // Decode of state_r
12854 always @(state_r) begin
12855 case ({state_r})
12856 SM_IDLE: state_ascii_r = \"idle \";
12857 SM_SEND: state_ascii_r = \"send \";
12858 SM_WAIT1: state_ascii_r = \"wait1\";
12859 default: state_ascii_r = \"%Erro\";
12860 endcase
12861 end
12862 // End of automatics"
12863 (save-excursion
12864 (let* ((params (verilog-read-auto-params 2 4))
12865 (undecode-name (nth 0 params))
12866 (ascii-name (nth 1 params))
12867 (elim-regexp (and (nth 2 params)
12868 (not (equal (nth 2 params) ""))
12869 (nth 2 params)))
12870 (one-hot-flag (nth 3 params))
12871 ;;
12872 (indent-pt (current-indentation))
12873 (modi (verilog-modi-current))
12874 (moddecls (verilog-modi-get-decls modi))
12875 ;;
12876 (sig-list-consts (append (verilog-decls-get-consts moddecls)
12877 (verilog-decls-get-gparams moddecls)))
12878 (sig-list-all (verilog-decls-get-iovars moddecls))
12879 ;;
12880 (undecode-sig (or (assoc undecode-name sig-list-all)
12881 (error "%s: Signal %s not found in design" (verilog-point-text) undecode-name)))
12882 (undecode-enum (or (verilog-sig-enum undecode-sig)
12883 (error "%s: Signal %s does not have an enum tag" (verilog-point-text) undecode-name)))
12884 ;;
12885 (enum-sigs (verilog-signals-not-in
12886 (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
12887 (error "%s: No state definitions for %s" (verilog-point-text) undecode-enum))
12888 nil))
12889 ;;
12890 (one-hot (or
12891 (string-match "onehot" (or one-hot-flag ""))
12892 (and ;; width(enum) != width(sig)
12893 (or (not (verilog-sig-bits (car enum-sigs)))
12894 (not (equal (verilog-sig-width (car enum-sigs))
12895 (verilog-sig-width undecode-sig))))
12896 ;; count(enums) == width(sig)
12897 (equal (number-to-string (length enum-sigs))
12898 (verilog-sig-width undecode-sig)))))
12899 (enum-chars 0)
12900 (ascii-chars 0))
12901 ;;
12902 ;; Find number of ascii chars needed
12903 (let ((tmp-sigs enum-sigs))
12904 (while tmp-sigs
12905 (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
12906 ascii-chars (max ascii-chars (length (verilog-enum-ascii
12907 (verilog-sig-name (car tmp-sigs))
12908 elim-regexp)))
12909 tmp-sigs (cdr tmp-sigs))))
12910 ;;
12911 (verilog-forward-or-insert-line)
12912 (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
12913 (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
12914 (concat "Decode of " undecode-name) nil nil))))
12915 (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
12916 ;;
12917 (verilog-insert-indent "always @(" undecode-name ") begin\n")
12918 (setq indent-pt (+ indent-pt verilog-indent-level))
12919 (verilog-insert-indent "case ({" undecode-name "})\n")
12920 (setq indent-pt (+ indent-pt verilog-case-indent))
12921 ;;
12922 (let ((tmp-sigs enum-sigs)
12923 (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
12924 (+ (if one-hot 9 1) (max 8 enum-chars))
12925 ascii-name ascii-chars))
12926 (errname (substring "%Error" 0 (min 6 ascii-chars))))
12927 (while tmp-sigs
12928 (verilog-insert-indent
12929 (concat
12930 (format chrfmt
12931 (concat (if one-hot "(")
12932 ;; Use enum-sigs length as that's numeric
12933 ;; verilog-sig-width undecode-sig might not be.
12934 (if one-hot (number-to-string (length enum-sigs)))
12935 ;; We use a shift instead of var[index]
12936 ;; so that a non-one hot value will show as error.
12937 (if one-hot "'b1<<")
12938 (verilog-sig-name (car tmp-sigs))
12939 (if one-hot ")") ":")
12940 (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
12941 elim-regexp))))
12942 (setq tmp-sigs (cdr tmp-sigs)))
12943 (verilog-insert-indent (format chrfmt "default:" errname)))
12944 ;;
12945 (setq indent-pt (- indent-pt verilog-case-indent))
12946 (verilog-insert-indent "endcase\n")
12947 (setq indent-pt (- indent-pt verilog-indent-level))
12948 (verilog-insert-indent "end\n"
12949 "// End of automatics\n"))))
12950
12951 (defun verilog-auto-templated-rel ()
12952 "Replace Templated relative line numbers with absolute line numbers.
12953 Internal use only. This hacks around the line numbers in AUTOINST Templates
12954 being different from the final output's line numbering."
12955 (let ((templateno 0) (template-line (list 0)) (buf-line 1))
12956 ;; Find line number each template is on
12957 ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
12958 (goto-char (point-min))
12959 (while (not (eobp))
12960 (when (looking-at ".*AUTO_TEMPLATE")
12961 (setq templateno (1+ templateno))
12962 (setq template-line (cons buf-line template-line)))
12963 (setq buf-line (1+ buf-line))
12964 (forward-line 1))
12965 (setq template-line (nreverse template-line))
12966 ;; Replace T# L# with absolute line number
12967 (goto-char (point-min))
12968 (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
12969 (replace-match
12970 (concat " Templated "
12971 (int-to-string (+ (nth (string-to-number (match-string 1))
12972 template-line)
12973 (string-to-number (match-string 2)))))
12974 t t))))
12975
12976 (defun verilog-auto-template-lint ()
12977 "Check AUTO_TEMPLATEs for unused lines.
12978 Enable with `verilog-auto-template-warn-unused'."
12979 (let ((name1 (or (buffer-file-name) (buffer-name))))
12980 (save-excursion
12981 (goto-char (point-min))
12982 (while (re-search-forward
12983 "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
12984 (let* ((tpl-info (verilog-read-auto-template-middle))
12985 (tpl-list (aref tpl-info 1))
12986 (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
12987 tpl-ass)
12988 (while tlines
12989 (setq tpl-ass (car tlines)
12990 tlines (cdr tlines))
12991 ;;;
12992 (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ;; Not supported, no warning
12993 (not verilog-auto-template-hits)
12994 (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
12995 verilog-auto-template-hits))
12996 (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
12997 name1
12998 (+ (elt tpl-ass 3) ;; Template line number
12999 (count-lines (point-min) (point)))
13000 (elt tpl-ass 0) (elt tpl-ass 1))
13001 )))))))
13002
13003 \f
13004 ;;
13005 ;; Auto top level
13006 ;;
13007
13008 (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
13009 "Expand AUTO statements.
13010 Look for any /*AUTO...*/ commands in the code, as used in
13011 instantiations or argument headers. Update the list of signals
13012 following the /*AUTO...*/ command.
13013
13014 Use \\[verilog-delete-auto] to remove the AUTOs.
13015
13016 Use \\[verilog-diff-auto] to see differences in AUTO expansion.
13017
13018 Use \\[verilog-inject-auto] to insert AUTOs for the first time.
13019
13020 Use \\[verilog-faq] for a pointer to frequently asked questions.
13021
13022 The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
13023 called before and after this function, respectively.
13024
13025 For example:
13026 module ModuleName (/*AUTOARG*/);
13027 /*AUTOINPUT*/
13028 /*AUTOOUTPUT*/
13029 /*AUTOWIRE*/
13030 /*AUTOREG*/
13031 InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
13032
13033 You can also update the AUTOs from the shell using:
13034 emacs --batch <filenames.v> -f verilog-batch-auto
13035 Or fix indentation with:
13036 emacs --batch <filenames.v> -f verilog-batch-indent
13037 Likewise, you can delete or inject AUTOs with:
13038 emacs --batch <filenames.v> -f verilog-batch-delete-auto
13039 emacs --batch <filenames.v> -f verilog-batch-inject-auto
13040 Or check if AUTOs have the same expansion
13041 emacs --batch <filenames.v> -f verilog-batch-diff-auto
13042
13043 Using \\[describe-function], see also:
13044 `verilog-auto-arg' for AUTOARG module instantiations
13045 `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
13046 `verilog-auto-assign-modport' for AUTOASSIGNMODPORT assignment to/from modport
13047 `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
13048 `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
13049 `verilog-auto-inout-modport' for AUTOINOUTMODPORT i/o from an interface modport
13050 `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
13051 `verilog-auto-inout-param' for AUTOINOUTPARAM copying params from elsewhere
13052 `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
13053 `verilog-auto-input' for AUTOINPUT making hierarchy inputs
13054 `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
13055 `verilog-auto-inst' for AUTOINST instantiation pins
13056 `verilog-auto-star' for AUTOINST .* SystemVerilog pins
13057 `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
13058 `verilog-auto-logic' for AUTOLOGIC declaring logic signals
13059 `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
13060 `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
13061 `verilog-auto-reg' for AUTOREG registers
13062 `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
13063 `verilog-auto-reset' for AUTORESET flop resets
13064 `verilog-auto-sense' for AUTOSENSE always sensitivity lists
13065 `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
13066 `verilog-auto-undef' for AUTOUNDEF `undef of local `defines
13067 `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
13068 `verilog-auto-wire' for AUTOWIRE instantiation wires
13069
13070 `verilog-read-defines' for reading `define values
13071 `verilog-read-includes' for reading `includes
13072
13073 If you have bugs with these autos, please file an issue at
13074 URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
13075 Wilson Snyder (wsnyder@wsnyder.org)."
13076 (interactive)
13077 (unless noninteractive (message "Updating AUTOs..."))
13078 (if (fboundp 'dinotrace-unannotate-all)
13079 (dinotrace-unannotate-all))
13080 (verilog-save-font-mods
13081 (let ((oldbuf (if (not (buffer-modified-p))
13082 (buffer-string)))
13083 ;; Cache directories; we don't write new files, so can't change
13084 (verilog-dir-cache-preserving t)
13085 ;; Cache current module
13086 (verilog-modi-cache-current-enable t)
13087 (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
13088 verilog-modi-cache-current)
13089 (unwind-protect
13090 ;; Disable change hooks for speed
13091 ;; This let can't be part of above let; must restore
13092 ;; after-change-functions before font-lock resumes
13093 (verilog-save-no-change-functions
13094 (verilog-save-scan-cache
13095 (save-excursion
13096 ;; Wipe cache; otherwise if we AUTOed a block above this one,
13097 ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
13098 (setq verilog-modi-cache-list nil)
13099 ;; Local state
13100 (setq verilog-auto-template-hits nil)
13101 ;; If we're not in verilog-mode, change syntax table so parsing works right
13102 (unless (eq major-mode `verilog-mode) (verilog-mode))
13103 ;; Allow user to customize
13104 (verilog-run-hooks 'verilog-before-auto-hook)
13105 ;; Try to save the user from needing to revert-file to reread file local-variables
13106 (verilog-auto-reeval-locals)
13107 (verilog-read-auto-lisp-present)
13108 (verilog-read-auto-lisp (point-min) (point-max))
13109 (verilog-getopt-flags)
13110 ;; From here on out, we can cache anything we read from disk
13111 (verilog-preserve-dir-cache
13112 ;; These two may seem obvious to do always, but on large includes it can be way too slow
13113 (when verilog-auto-read-includes
13114 (verilog-read-includes)
13115 (verilog-read-defines nil nil t))
13116 ;; Setup variables due to SystemVerilog expansion
13117 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
13118 ;; This particular ordering is important
13119 ;; INST: Lower modules correct, no internal dependencies, FIRST
13120 (verilog-preserve-modi-cache
13121 ;; Clear existing autos else we'll be screwed by existing ones
13122 (verilog-delete-auto)
13123 ;; Injection if appropriate
13124 (when inject
13125 (verilog-inject-inst)
13126 (verilog-inject-sense)
13127 (verilog-inject-arg))
13128 ;;
13129 ;; Do user inserts first, so their code can insert AUTOs
13130 ;; We may provide an AUTOINSERTLISPLAST if another cleanup pass is needed
13131 (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
13132 'verilog-auto-insert-lisp)
13133 ;; Expand instances before need the signals the instances input/output
13134 (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
13135 (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
13136 (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
13137 ;; Doesn't matter when done, but combine it with a common changer
13138 (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
13139 (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
13140 ;; Must be done before autoin/out as creates a reg
13141 (verilog-auto-re-search-do "/\\*AUTOASCIIENUM(.*?)\\*/" 'verilog-auto-ascii-enum)
13142 ;;
13143 ;; first in/outs from other files
13144 (verilog-auto-re-search-do "/\\*AUTOINOUTMODPORT(.*?)\\*/" 'verilog-auto-inout-modport)
13145 (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE(.*?)\\*/" 'verilog-auto-inout-module)
13146 (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP(.*?)\\*/" 'verilog-auto-inout-comp)
13147 (verilog-auto-re-search-do "/\\*AUTOINOUTIN(.*?)\\*/" 'verilog-auto-inout-in)
13148 (verilog-auto-re-search-do "/\\*AUTOINOUTPARAM(.*?)\\*/" 'verilog-auto-inout-param)
13149 ;; next in/outs which need previous sucked inputs first
13150 (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((.*?)\\)?\\*/" 'verilog-auto-output)
13151 (verilog-auto-re-search-do "/\\*AUTOINPUT\\((.*?)\\)?\\*/" 'verilog-auto-input)
13152 (verilog-auto-re-search-do "/\\*AUTOINOUT\\((.*?)\\)?\\*/" 'verilog-auto-inout)
13153 ;; Then tie off those in/outs
13154 (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
13155 ;; These can be anywhere after AUTOINSERTLISP
13156 (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((.*?)\\)?\\*/" 'verilog-auto-undef)
13157 ;; Wires/regs must be after inputs/outputs
13158 (verilog-auto-re-search-do "/\\*AUTOASSIGNMODPORT(.*?)\\*/" 'verilog-auto-assign-modport)
13159 (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
13160 (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
13161 (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
13162 (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
13163 ;; outputevery needs AUTOOUTPUTs done first
13164 (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\*/" 'verilog-auto-output-every)
13165 ;; After we've created all new variables
13166 (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
13167 ;; Must be after all inputs outputs are generated
13168 (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
13169 ;; Fix line numbers (comments only)
13170 (when verilog-auto-inst-template-numbers
13171 (verilog-auto-templated-rel))
13172 (when verilog-auto-template-warn-unused
13173 (verilog-auto-template-lint))))
13174 ;;
13175 (verilog-run-hooks 'verilog-auto-hook)
13176 ;;
13177 (when verilog-auto-delete-trailing-whitespace
13178 (verilog-delete-trailing-whitespace))
13179 ;;
13180 (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
13181 ;;
13182 ;; If end result is same as when started, clear modified flag
13183 (cond ((and oldbuf (equal oldbuf (buffer-string)))
13184 (set-buffer-modified-p nil)
13185 (unless noninteractive (message "Updating AUTOs...done (no changes)")))
13186 (t (unless noninteractive (message "Updating AUTOs...done"))))
13187 ;; End of after-change protection
13188 )))
13189 ;; Unwind forms
13190 ;; Currently handled in verilog-save-font-mods
13191 ))))
13192 \f
13193
13194 ;;
13195 ;; Skeleton based code insertion
13196 ;;
13197 (defvar verilog-template-map
13198 (let ((map (make-sparse-keymap)))
13199 (define-key map "a" 'verilog-sk-always)
13200 (define-key map "b" 'verilog-sk-begin)
13201 (define-key map "c" 'verilog-sk-case)
13202 (define-key map "f" 'verilog-sk-for)
13203 (define-key map "g" 'verilog-sk-generate)
13204 (define-key map "h" 'verilog-sk-header)
13205 (define-key map "i" 'verilog-sk-initial)
13206 (define-key map "j" 'verilog-sk-fork)
13207 (define-key map "m" 'verilog-sk-module)
13208 (define-key map "o" 'verilog-sk-ovm-class)
13209 (define-key map "p" 'verilog-sk-primitive)
13210 (define-key map "r" 'verilog-sk-repeat)
13211 (define-key map "s" 'verilog-sk-specify)
13212 (define-key map "t" 'verilog-sk-task)
13213 (define-key map "u" 'verilog-sk-uvm-class)
13214 (define-key map "w" 'verilog-sk-while)
13215 (define-key map "x" 'verilog-sk-casex)
13216 (define-key map "z" 'verilog-sk-casez)
13217 (define-key map "?" 'verilog-sk-if)
13218 (define-key map ":" 'verilog-sk-else-if)
13219 (define-key map "/" 'verilog-sk-comment)
13220 (define-key map "A" 'verilog-sk-assign)
13221 (define-key map "F" 'verilog-sk-function)
13222 (define-key map "I" 'verilog-sk-input)
13223 (define-key map "O" 'verilog-sk-output)
13224 (define-key map "S" 'verilog-sk-state-machine)
13225 (define-key map "=" 'verilog-sk-inout)
13226 (define-key map "W" 'verilog-sk-wire)
13227 (define-key map "R" 'verilog-sk-reg)
13228 (define-key map "D" 'verilog-sk-define-signal)
13229 map)
13230 "Keymap used in Verilog mode for smart template operations.")
13231
13232
13233 ;;
13234 ;; Place the templates into Verilog Mode. They may be inserted under any key.
13235 ;; C-c C-t will be the default. If you use templates a lot, you
13236 ;; may want to consider moving the binding to another key in your init
13237 ;; file.
13238 ;;
13239 ;; Note \C-c and letter are reserved for users
13240 (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
13241
13242 ;;; ---- statement skeletons ------------------------------------------
13243
13244 (define-skeleton verilog-sk-prompt-condition
13245 "Prompt for the loop condition."
13246 "[condition]: " str )
13247
13248 (define-skeleton verilog-sk-prompt-init
13249 "Prompt for the loop init statement."
13250 "[initial statement]: " str )
13251
13252 (define-skeleton verilog-sk-prompt-inc
13253 "Prompt for the loop increment statement."
13254 "[increment statement]: " str )
13255
13256 (define-skeleton verilog-sk-prompt-name
13257 "Prompt for the name of something."
13258 "[name]: " str)
13259
13260 (define-skeleton verilog-sk-prompt-clock
13261 "Prompt for the name of something."
13262 "name and edge of clock(s): " str)
13263
13264 (defvar verilog-sk-reset nil)
13265 (defun verilog-sk-prompt-reset ()
13266 "Prompt for the name of a state machine reset."
13267 (setq verilog-sk-reset (read-string "name of reset: " "rst")))
13268
13269
13270 (define-skeleton verilog-sk-prompt-state-selector
13271 "Prompt for the name of a state machine selector."
13272 "name of selector (eg {a,b,c,d}): " str )
13273
13274 (define-skeleton verilog-sk-prompt-output
13275 "Prompt for the name of something."
13276 "output: " str)
13277
13278 (define-skeleton verilog-sk-prompt-msb
13279 "Prompt for most significant bit specification."
13280 "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
13281
13282 (define-skeleton verilog-sk-prompt-lsb
13283 "Prompt for least significant bit specification."
13284 "lsb:" str )
13285
13286 (defvar verilog-sk-p nil)
13287 (define-skeleton verilog-sk-prompt-width
13288 "Prompt for a width specification."
13289 ()
13290 (progn
13291 (setq verilog-sk-p (point))
13292 (verilog-sk-prompt-msb)
13293 (if (> (point) verilog-sk-p) "] " " ")))
13294
13295 (defun verilog-sk-header ()
13296 "Insert a descriptive header at the top of the file.
13297 See also `verilog-header' for an alternative format."
13298 (interactive "*")
13299 (save-excursion
13300 (goto-char (point-min))
13301 (verilog-sk-header-tmpl)))
13302
13303 (define-skeleton verilog-sk-header-tmpl
13304 "Insert a comment block containing the module title, author, etc."
13305 "[Description]: "
13306 "// -*- Mode: Verilog -*-"
13307 "\n// Filename : " (buffer-name)
13308 "\n// Description : " str
13309 "\n// Author : " (user-full-name)
13310 "\n// Created On : " (current-time-string)
13311 "\n// Last Modified By: " (user-full-name)
13312 "\n// Last Modified On: " (current-time-string)
13313 "\n// Update Count : 0"
13314 "\n// Status : Unknown, Use with caution!"
13315 "\n")
13316
13317 (define-skeleton verilog-sk-module
13318 "Insert a module definition."
13319 ()
13320 > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
13321 > _ \n
13322 > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
13323
13324 ;;; ------------------------------------------------------------------------
13325 ;;; Define a default OVM class, with macros and new()
13326 ;;; ------------------------------------------------------------------------
13327
13328 (define-skeleton verilog-sk-ovm-class
13329 "Insert a class definition"
13330 ()
13331 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13332 > _ \n
13333 > "`ovm_object_utils_begin(" name ")" \n
13334 > (- verilog-indent-level) " `ovm_object_utils_end" \n
13335 > _ \n
13336 > "function new(name=\"" name "\");" \n
13337 > "super.new(name);" \n
13338 > (- verilog-indent-level) "endfunction" \n
13339 > _ \n
13340 > "endclass" (progn (electric-verilog-terminate-line) nil))
13341
13342 (define-skeleton verilog-sk-uvm-class
13343 "Insert a class definition"
13344 ()
13345 > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
13346 > _ \n
13347 > "`uvm_object_utils_begin(" name ")" \n
13348 > (- verilog-indent-level) " `uvm_object_utils_end" \n
13349 > _ \n
13350 > "function new(name=\"" name "\");" \n
13351 > "super.new(name);" \n
13352 > (- verilog-indent-level) "endfunction" \n
13353 > _ \n
13354 > "endclass" (progn (electric-verilog-terminate-line) nil))
13355
13356 (define-skeleton verilog-sk-primitive
13357 "Insert a task definition."
13358 ()
13359 > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
13360 > _ \n
13361 > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
13362
13363 (define-skeleton verilog-sk-task
13364 "Insert a task definition."
13365 ()
13366 > "task " '(verilog-sk-prompt-name) & ?; \n
13367 > _ \n
13368 > "begin" \n
13369 > \n
13370 > (- verilog-indent-level-behavioral) "end" \n
13371 > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
13372
13373 (define-skeleton verilog-sk-function
13374 "Insert a function definition."
13375 ()
13376 > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
13377 > _ \n
13378 > "begin" \n
13379 > \n
13380 > (- verilog-indent-level-behavioral) "end" \n
13381 > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
13382
13383 (define-skeleton verilog-sk-always
13384 "Insert always block. Uses the minibuffer to prompt
13385 for sensitivity list."
13386 ()
13387 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13388 > _ \n
13389 > (- verilog-indent-level-behavioral) "end" \n >
13390 )
13391
13392 (define-skeleton verilog-sk-initial
13393 "Insert an initial block."
13394 ()
13395 > "initial begin\n"
13396 > _ \n
13397 > (- verilog-indent-level-behavioral) "end" \n > )
13398
13399 (define-skeleton verilog-sk-specify
13400 "Insert specify block. "
13401 ()
13402 > "specify\n"
13403 > _ \n
13404 > (- verilog-indent-level-behavioral) "endspecify" \n > )
13405
13406 (define-skeleton verilog-sk-generate
13407 "Insert generate block. "
13408 ()
13409 > "generate\n"
13410 > _ \n
13411 > (- verilog-indent-level-behavioral) "endgenerate" \n > )
13412
13413 (define-skeleton verilog-sk-begin
13414 "Insert begin end block. Uses the minibuffer to prompt for name."
13415 ()
13416 > "begin" '(verilog-sk-prompt-name) \n
13417 > _ \n
13418 > (- verilog-indent-level-behavioral) "end"
13419 )
13420
13421 (define-skeleton verilog-sk-fork
13422 "Insert a fork join block."
13423 ()
13424 > "fork\n"
13425 > "begin" \n
13426 > _ \n
13427 > (- verilog-indent-level-behavioral) "end" \n
13428 > "begin" \n
13429 > \n
13430 > (- verilog-indent-level-behavioral) "end" \n
13431 > (- verilog-indent-level-behavioral) "join" \n
13432 > )
13433
13434
13435 (define-skeleton verilog-sk-case
13436 "Build skeleton case statement, prompting for the selector expression,
13437 and the case items."
13438 "[selector expression]: "
13439 > "case (" str ") " \n
13440 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13441 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13442
13443 (define-skeleton verilog-sk-casex
13444 "Build skeleton casex statement, prompting for the selector expression,
13445 and the case items."
13446 "[selector expression]: "
13447 > "casex (" str ") " \n
13448 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13449 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13450
13451 (define-skeleton verilog-sk-casez
13452 "Build skeleton casez statement, prompting for the selector expression,
13453 and the case items."
13454 "[selector expression]: "
13455 > "casez (" str ") " \n
13456 > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
13457 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
13458
13459 (define-skeleton verilog-sk-if
13460 "Insert a skeleton if statement."
13461 > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
13462 > _ \n
13463 > (- verilog-indent-level-behavioral) "end " \n )
13464
13465 (define-skeleton verilog-sk-else-if
13466 "Insert a skeleton else if statement."
13467 > (verilog-indent-line) "else if ("
13468 (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
13469 > _ \n
13470 > "end" (progn (electric-verilog-terminate-line) nil))
13471
13472 (define-skeleton verilog-sk-datadef
13473 "Common routine to get data definition."
13474 ()
13475 '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
13476
13477 (define-skeleton verilog-sk-input
13478 "Insert an input definition."
13479 ()
13480 > "input [" '(verilog-sk-datadef))
13481
13482 (define-skeleton verilog-sk-output
13483 "Insert an output definition."
13484 ()
13485 > "output [" '(verilog-sk-datadef))
13486
13487 (define-skeleton verilog-sk-inout
13488 "Insert an inout definition."
13489 ()
13490 > "inout [" '(verilog-sk-datadef))
13491
13492 (defvar verilog-sk-signal nil)
13493 (define-skeleton verilog-sk-def-reg
13494 "Insert a reg definition."
13495 ()
13496 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
13497
13498 (defun verilog-sk-define-signal ()
13499 "Insert a definition of signal under point at top of module."
13500 (interactive "*")
13501 (let* ((sig-re "[a-zA-Z0-9_]*")
13502 (v1 (buffer-substring
13503 (save-excursion
13504 (skip-chars-backward sig-re)
13505 (point))
13506 (save-excursion
13507 (skip-chars-forward sig-re)
13508 (point)))))
13509 (if (not (member v1 verilog-keywords))
13510 (save-excursion
13511 (setq verilog-sk-signal v1)
13512 (verilog-beg-of-defun)
13513 (verilog-end-of-statement)
13514 (verilog-forward-syntactic-ws)
13515 (verilog-sk-def-reg)
13516 (message "signal at point is %s" v1))
13517 (message "object at point (%s) is a keyword" v1))))
13518
13519 (define-skeleton verilog-sk-wire
13520 "Insert a wire definition."
13521 ()
13522 > "wire [" '(verilog-sk-datadef))
13523
13524 (define-skeleton verilog-sk-reg
13525 "Insert a reg definition."
13526 ()
13527 > "reg [" '(verilog-sk-datadef))
13528
13529 (define-skeleton verilog-sk-assign
13530 "Insert a skeleton assign statement."
13531 ()
13532 > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
13533
13534 (define-skeleton verilog-sk-while
13535 "Insert a skeleton while loop statement."
13536 ()
13537 > "while (" '(verilog-sk-prompt-condition) ") begin" \n
13538 > _ \n
13539 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13540
13541 (define-skeleton verilog-sk-repeat
13542 "Insert a skeleton repeat loop statement."
13543 ()
13544 > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
13545 > _ \n
13546 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13547
13548 (define-skeleton verilog-sk-for
13549 "Insert a skeleton while loop statement."
13550 ()
13551 > "for ("
13552 '(verilog-sk-prompt-init) "; "
13553 '(verilog-sk-prompt-condition) "; "
13554 '(verilog-sk-prompt-inc)
13555 ") begin" \n
13556 > _ \n
13557 > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
13558
13559 (define-skeleton verilog-sk-comment
13560 "Inserts three comment lines, making a display comment."
13561 ()
13562 > "/*\n"
13563 > "* " _ \n
13564 > "*/")
13565
13566 (define-skeleton verilog-sk-state-machine
13567 "Insert a state machine definition."
13568 "Name of state variable: "
13569 '(setq input "state")
13570 > "// State registers for " str | -23 \n
13571 '(setq verilog-sk-state str)
13572 > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
13573 '(setq input nil)
13574 > \n
13575 > "// State FF for " verilog-sk-state \n
13576 > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
13577 > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
13578 > verilog-sk-state " = next_" verilog-sk-state ?; \n
13579 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
13580 > \n
13581 > "// Next State Logic for " verilog-sk-state \n
13582 > "always @ ( /*AUTOSENSE*/ ) begin\n"
13583 > "case (" '(verilog-sk-prompt-state-selector) ") " \n
13584 > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
13585 resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
13586 > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
13587 \f
13588
13589 ;;
13590 ;; Include file loading with mouse/return event
13591 ;;
13592 ;; idea & first impl.: M. Rouat (eldo-mode.el)
13593 ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
13594
13595 (if (featurep 'xemacs)
13596 (require 'overlay))
13597
13598 (defconst verilog-include-file-regexp
13599 "^`include\\s-+\"\\([^\n\"]*\\)\""
13600 "Regexp that matches the include file.")
13601
13602 (defvar verilog-mode-mouse-map
13603 (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
13604 (set-keymap-parent map verilog-mode-map)
13605 ;; mouse button bindings
13606 (define-key map "\r" 'verilog-load-file-at-point)
13607 (if (featurep 'xemacs)
13608 (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
13609 (define-key map [mouse-2] 'verilog-load-file-at-mouse))
13610 (if (featurep 'xemacs)
13611 (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
13612 (define-key map [S-mouse-2] 'mouse-yank-at-click))
13613 map)
13614 "Map containing mouse bindings for `verilog-mode'.")
13615
13616
13617 (defun verilog-highlight-region (beg end old-len)
13618 "Colorize included files and modules in the (changed?) region.
13619 Clicking on the middle-mouse button loads them in a buffer (as in dired)."
13620 (when (or verilog-highlight-includes
13621 verilog-highlight-modules)
13622 (save-excursion
13623 (save-match-data ;; A query-replace may call this function - do not disturb
13624 (verilog-save-buffer-state
13625 (verilog-save-scan-cache
13626 (let (end-point)
13627 (goto-char end)
13628 (setq end-point (point-at-eol))
13629 (goto-char beg)
13630 (beginning-of-line) ; scan entire line
13631 ;; delete overlays existing on this line
13632 (let ((overlays (overlays-in (point) end-point)))
13633 (while overlays
13634 (if (and
13635 (overlay-get (car overlays) 'detachable)
13636 (or (overlay-get (car overlays) 'verilog-include-file)
13637 (overlay-get (car overlays) 'verilog-inst-module)))
13638 (delete-overlay (car overlays)))
13639 (setq overlays (cdr overlays))))
13640 ;;
13641 ;; make new include overlays
13642 (when verilog-highlight-includes
13643 (while (search-forward-regexp verilog-include-file-regexp end-point t)
13644 (goto-char (match-beginning 1))
13645 (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
13646 (overlay-put ov 'start-closed 't)
13647 (overlay-put ov 'end-closed 't)
13648 (overlay-put ov 'evaporate 't)
13649 (overlay-put ov 'verilog-include-file 't)
13650 (overlay-put ov 'mouse-face 'highlight)
13651 (overlay-put ov 'local-map verilog-mode-mouse-map))))
13652 ;;
13653 ;; make new module overlays
13654 (goto-char beg)
13655 ;; This scanner is syntax-fragile, so don't get bent
13656 (when verilog-highlight-modules
13657 (condition-case nil
13658 (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
13659 (save-excursion
13660 (goto-char (match-beginning 0))
13661 (unless (verilog-inside-comment-or-string-p)
13662 (verilog-read-inst-module-matcher) ;; sets match 0
13663 (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
13664 (overlay-put ov 'start-closed 't)
13665 (overlay-put ov 'end-closed 't)
13666 (overlay-put ov 'evaporate 't)
13667 (overlay-put ov 'verilog-inst-module 't)
13668 (overlay-put ov 'mouse-face 'highlight)
13669 (overlay-put ov 'local-map verilog-mode-mouse-map)))))
13670 (error nil)))
13671 ;;
13672 ;; Future highlights:
13673 ;; variables - make an Occur buffer of where referenced
13674 ;; pins - make an Occur buffer of the sig in the declaration module
13675 )))))))
13676
13677 (defun verilog-highlight-buffer ()
13678 "Colorize included files and modules across the whole buffer."
13679 ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
13680 (interactive)
13681 ;; delete and remake overlays
13682 (verilog-highlight-region (point-min) (point-max) nil))
13683
13684 ;; Deprecated, but was interactive, so we'll keep it around
13685 (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
13686
13687 ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
13688 ;; so define this function to do more or less the same as ffap-at-mouse
13689 ;; but first resolve filename...
13690 (defun verilog-load-file-at-mouse (event)
13691 "Load file under button 2 click's EVENT.
13692 Files are checked based on `verilog-library-flags'."
13693 (interactive "@e")
13694 (save-excursion ;; implement a Verilog specific ffap-at-mouse
13695 (mouse-set-point event)
13696 (verilog-load-file-at-point t)))
13697
13698 ;; ffap isn't usable for Verilog mode. It uses library paths.
13699 ;; so define this function to do more or less the same as ffap
13700 ;; but first resolve filename...
13701 (defun verilog-load-file-at-point (&optional warn)
13702 "Load file under point.
13703 If WARN, throw warning if not found.
13704 Files are checked based on `verilog-library-flags'."
13705 (interactive)
13706 (save-excursion ;; implement a Verilog specific ffap
13707 (let ((overlays (overlays-in (point) (point)))
13708 hit)
13709 (while (and overlays (not hit))
13710 (when (overlay-get (car overlays) 'verilog-inst-module)
13711 (verilog-goto-defun-file (buffer-substring
13712 (overlay-start (car overlays))
13713 (overlay-end (car overlays))))
13714 (setq hit t))
13715 (setq overlays (cdr overlays)))
13716 ;; Include?
13717 (beginning-of-line)
13718 (when (and (not hit)
13719 (looking-at verilog-include-file-regexp))
13720 (if (and (car (verilog-library-filenames
13721 (match-string 1) (buffer-file-name)))
13722 (file-readable-p (car (verilog-library-filenames
13723 (match-string 1) (buffer-file-name)))))
13724 (find-file (car (verilog-library-filenames
13725 (match-string 1) (buffer-file-name))))
13726 (when warn
13727 (message
13728 "File '%s' isn't readable, use shift-mouse2 to paste in this field"
13729 (match-string 1))))))))
13730
13731 ;;
13732 ;; Bug reporting
13733 ;;
13734
13735 (defun verilog-faq ()
13736 "Tell the user their current version, and where to get the FAQ etc."
13737 (interactive)
13738 (with-output-to-temp-buffer "*verilog-mode help*"
13739 (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
13740 (princ "\n")
13741 (princ "For new releases, see http://www.verilog.com\n")
13742 (princ "\n")
13743 (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
13744 (princ "\n")
13745 (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
13746 (princ "\n")))
13747
13748 (autoload 'reporter-submit-bug-report "reporter")
13749 (defvar reporter-prompt-for-summary-p)
13750
13751 (defun verilog-submit-bug-report ()
13752 "Submit via mail a bug report on verilog-mode.el."
13753 (interactive)
13754 (let ((reporter-prompt-for-summary-p t))
13755 (reporter-submit-bug-report
13756 "mac@verilog.com, wsnyder@wsnyder.org"
13757 (concat "verilog-mode v" verilog-mode-version)
13758 '(
13759 verilog-active-low-regexp
13760 verilog-after-save-font-hook
13761 verilog-align-ifelse
13762 verilog-assignment-delay
13763 verilog-auto-arg-sort
13764 verilog-auto-declare-nettype
13765 verilog-auto-delete-trailing-whitespace
13766 verilog-auto-endcomments
13767 verilog-auto-hook
13768 verilog-auto-ignore-concat
13769 verilog-auto-indent-on-newline
13770 verilog-auto-inout-ignore-regexp
13771 verilog-auto-input-ignore-regexp
13772 verilog-auto-inst-column
13773 verilog-auto-inst-dot-name
13774 verilog-auto-inst-interfaced-ports
13775 verilog-auto-inst-param-value
13776 verilog-auto-inst-sort
13777 verilog-auto-inst-template-numbers
13778 verilog-auto-inst-vector
13779 verilog-auto-lineup
13780 verilog-auto-newline
13781 verilog-auto-output-ignore-regexp
13782 verilog-auto-read-includes
13783 verilog-auto-reset-blocking-in-non
13784 verilog-auto-reset-widths
13785 verilog-auto-save-policy
13786 verilog-auto-sense-defines-constant
13787 verilog-auto-sense-include-inputs
13788 verilog-auto-star-expand
13789 verilog-auto-star-save
13790 verilog-auto-template-warn-unused
13791 verilog-auto-tieoff-declaration
13792 verilog-auto-tieoff-ignore-regexp
13793 verilog-auto-unused-ignore-regexp
13794 verilog-auto-wire-type
13795 verilog-before-auto-hook
13796 verilog-before-delete-auto-hook
13797 verilog-before-getopt-flags-hook
13798 verilog-before-save-font-hook
13799 verilog-cache-enabled
13800 verilog-case-indent
13801 verilog-cexp-indent
13802 verilog-compiler
13803 verilog-coverage
13804 verilog-delete-auto-hook
13805 verilog-getopt-flags-hook
13806 verilog-highlight-grouping-keywords
13807 verilog-highlight-includes
13808 verilog-highlight-modules
13809 verilog-highlight-p1800-keywords
13810 verilog-highlight-translate-off
13811 verilog-indent-begin-after-if
13812 verilog-indent-declaration-macros
13813 verilog-indent-level
13814 verilog-indent-level-behavioral
13815 verilog-indent-level-declaration
13816 verilog-indent-level-directive
13817 verilog-indent-level-module
13818 verilog-indent-lists
13819 verilog-library-directories
13820 verilog-library-extensions
13821 verilog-library-files
13822 verilog-library-flags
13823 verilog-linter
13824 verilog-minimum-comment-distance
13825 verilog-mode-hook
13826 verilog-mode-release-date
13827 verilog-mode-release-emacs
13828 verilog-mode-version
13829 verilog-preprocessor
13830 verilog-simulator
13831 verilog-tab-always-indent
13832 verilog-tab-to-comment
13833 verilog-typedef-regexp
13834 verilog-warn-fatal
13835 )
13836 nil nil
13837 (concat "Hi Mac,
13838
13839 I want to report a bug.
13840
13841 Before I go further, I want to say that Verilog mode has changed my life.
13842 I save so much time, my files are colored nicely, my co workers respect
13843 my coding ability... until now. I'd really appreciate anything you
13844 could do to help me out with this minor deficiency in the product.
13845
13846 I've taken a look at the Verilog-Mode FAQ at
13847 http://www.veripool.org/verilog-mode-faq.html.
13848
13849 And, I've considered filing the bug on the issue tracker at
13850 http://www.veripool.org/verilog-mode-bugs
13851 since I realize that public bugs are easier for you to track,
13852 and for others to search, but would prefer to email.
13853
13854 So, to reproduce the bug, start a fresh Emacs via " invocation-name "
13855 -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
13856 the code included below.
13857
13858 Given those lines, I expected [[Fill in here]] to happen;
13859 but instead, [[Fill in here]] happens!.
13860
13861 == The code: =="))))
13862
13863 (provide 'verilog-mode)
13864
13865 ;; Local Variables:
13866 ;; checkdoc-permit-comma-termination-flag:t
13867 ;; checkdoc-force-docstrings-flag:nil
13868 ;; End:
13869
13870 ;;; verilog-mode.el ends here